1 /********************************************************************************
2 *                                                                               *
3 *                             B i t m a p    O b j e c t                        *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1998,2021 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify          *
9 * it under the terms of the GNU Lesser General Public License as published by   *
10 * the Free Software Foundation; either version 3 of the License, or             *
11 * (at your option) any later version.                                           *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
16 * GNU Lesser General Public License for more details.                           *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public License      *
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
20 ********************************************************************************/
21 #ifndef FXBITMAP_H
22 #define FXBITMAP_H
23 
24 #ifndef FXDRAWABLE_H
25 #include "FXDrawable.h"
26 #endif
27 
28 namespace FX {
29 
30 
31 // Image rendering hints
32 enum {
33   BITMAP_KEEP       = 0x00000001,       // Keep pixel data in client
34   BITMAP_OWNED      = 0x00000002,       // Pixel data is owned by image
35   BITMAP_SHMI       = 0x00000020,       // Using shared memory image
36   BITMAP_SHMP       = 0x00000040        // Using shared memory pixmap
37   };
38 
39 
40 // Forward declarations
41 class FXDC;
42 class FXDCWindow;
43 
44 
45 /**
46 * A Bitmap is a rectangular array of pixels.  It supports two representations
47 * of these pixels: a client-side pixel buffer, and a server-side pixmap which
48 * is stored in an organization directly compatible with the screen, for fast
49 * drawing onto the device.
50 * The server-side representation is not directly accessible from the current
51 * process as it lives in the process of the X Server or GDI.
52 * The client-side pixel array is of size height x (width+7)/8 bytes, in other
53 * words 8 pixels packed into a single byte, starting at bit 0 on the left.
54 */
55 class FXAPI FXBitmap : public FXDrawable {
56   FXDECLARE(FXBitmap)
57   friend class FXDC;
58   friend class FXDCWindow;
59 private:
60 #ifdef WIN32
61   virtual FXID GetDC() const;
62   virtual int ReleaseDC(FXID) const;
63 #endif
64 protected:
65   FXuchar *data;        // Pixel data
66   FXint    bytewidth;   // Number of bytes across
67   FXuint   options;     // Options
68 protected:
69   FXBitmap();
70 private:
71   FXBitmap(const FXBitmap&);
72   FXBitmap &operator=(const FXBitmap&);
73 public:
74 
75   /**
76   * Create a bitmap.  If a client-side pixel buffer has been specified,
77   * the bitmap does not own the pixel buffer unless the BITMAP_OWNED flag is
78   * set.  If the BITMAP_OWNED flag is set but a NULL pixel buffer is
79   * passed, a pixel buffer will be automatically created and will be owned
80   * by the bitmap. The flags BITMAP_SHMI and BITMAP_SHMP may be specified for
81   * large bitmaps to instruct render() to use shared memory to communicate
82   * with the server.
83   */
84   FXBitmap(FXApp* a,const void *pix=NULL,FXuint opts=0,FXint w=1,FXint h=1);
85 
86   /// Change options
87   void setOptions(FXuint opts);
88 
89   /// To get to the option flags
getOptions()90   FXuint getOptions() const { return options; }
91 
92   /// Set pixel data ownership flag
93   void setOwned(FXbool owned);
94 
95   /// Get pixel data ownership flag
96   FXbool isOwned() const;
97 
98   /**
99   * Populate the bitmap with new pixel data of the same size; it will assume
100   * ownership of the pixel data if image BITMAP_OWNED option is passed.
101   * The server-side representation of the image, if it exists, is not updated.
102   * This can be done by calling render().
103   */
104   virtual void setData(FXuchar *pix,FXuint opts=0);
105 
106   /**
107   * Populate the bitmap with new pixel data of a new size; it will assume ownership
108   * of the pixel data if image BITMAP_OWNED option is passed.  The size of the server-
109   * side representation of the image, if it exists, is adjusted but the contents are
110   * not updated yet. This can be done by calling render().
111   */
112   virtual void setData(FXuchar *pix,FXuint opts,FXint w,FXint h);
113 
114   /// To get to the pixel data
getData()115   FXuchar* getData() const { return data; }
116 
117   /// Get pixel at x,y
getPixel(FXint x,FXint y)118   FXbool getPixel(FXint x,FXint y) const { return (FXbool)((data[y*bytewidth+(x>>3)]>>(x&7))&1); }
119 
120   /// Change pixel at x,y
setPixel(FXint x,FXint y,FXbool color)121   void setPixel(FXint x,FXint y,FXbool color){ color ? data[y*bytewidth+(x>>3)]|=(1<<(x&7)) : data[y*bytewidth+(x>>3)]&=~(1<<(x&7)); }
122 
123   /**
124   * Create the server side pixmap, then call render() to fill it with the
125   * pixel data from the client-side buffer.  After the server-side image has
126   * been created, the client-side pixel buffer will be deleted unless
127   * BITMAP_KEEP has been specified.  If the pixel buffer is not owned, i.e.
128   * the flag BITMAP_OWNED is not set, the pixel buffer will not be deleted.
129   */
130   virtual void create();
131 
132   /**
133   * Detach the server side pixmap from the Bitmap.
134   * Afterwards, the Bitmap is left as if it never had a server-side resources.
135   */
136   virtual void detach();
137 
138   /**
139   * Destroy the server-side pixmap.
140   * The client-side pixel buffer is not affected.
141   */
142   virtual void destroy();
143 
144   /**
145   * Retrieves pixels from the server-side bitmap.
146   */
147   virtual void restore();
148 
149   /**
150   * Render the server-side representation of the bitmap from client-side
151   * pixels.
152   */
153   virtual void render();
154 
155   /**
156   * Release the client-side pixels buffer, free it if it was owned.
157   * If it is not owned, the image just forgets about the buffer.
158   */
159   virtual void release();
160 
161   /**
162   * Resize both client-side and server-side representations (if any) to the
163   * given width and height.  The new representations typically contain garbage
164   * after this operation and need to be re-filled.
165   */
166   virtual void resize(FXint w,FXint h);
167 
168   /**
169   * Rescale pixels image to the specified width and height; this calls
170   * resize() to adjust the client and server side representations.
171   */
172   virtual void scale(FXint w,FXint h);
173 
174   /// Mirror bitmap horizontally and/or vertically
175   virtual void mirror(FXbool horizontal,FXbool vertical);
176 
177   /// Rotate bitmap by degrees ccw
178   virtual void rotate(FXint degrees);
179 
180   /**
181   * Crop bitmap to given rectangle; this calls resize() to adjust the client
182   * and server side representations.  The new bitmap may be smaller or larger
183   * than the old one; blank areas are filled with color. There must be at
184   * least one pixel of overlap between the old and the new bitmap.
185   */
186   virtual void crop(FXint x,FXint y,FXint w,FXint h,FXbool color=false);
187 
188   /// Fill bitmap with uniform value
189   virtual void fill(FXbool color);
190 
191   /// Save object to stream
192   virtual void save(FXStream& store) const;
193 
194   /// Load object from stream
195   virtual void load(FXStream& store);
196 
197   /// Save pixel data only
198   virtual FXbool savePixels(FXStream& store) const;
199 
200   /// Load pixel data only
201   virtual FXbool loadPixels(FXStream& store);
202 
203   /// Cleanup
204   virtual ~FXBitmap();
205   };
206 
207 }
208 
209 #endif
210 
211