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