1 /********************************************************************************
2 *                                                                               *
3 *                             B i t m a p    O b j e c t                        *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 1998,2005 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.33 2005/01/16 16:06:06 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   /// To get to the pixel data
getData()89   FXuchar* getData() const { return data; }
90 
91   /// To get to the option flags
getOptions()92   FXuint getOptions() const { return options; }
93 
94   /// Change options
95   void setOptions(FXuint opts);
96 
97   /// Get pixel at x,y
getPixel(FXint x,FXint y)98   FXbool getPixel(FXint x,FXint y) const { return (FXbool)((data[y*bytewidth+(x>>3)]>>(x&7))&1); }
99 
100   /// Change pixel at x,y
setPixel(FXint x,FXint y,FXbool color)101   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)); }
102 
103   /**
104   * Create the server side pixmap, then call render() to fill it with the
105   * pixel data from the client-side buffer.  After the server-side image has
106   * been created, the client-side pixel buffer will be deleted unless
107   * BITMAP_KEEP has been specified.  If the pixel buffer is not owned, i.e.
108   * the flag BITMAP_OWNED is not set, the pixel buffer will not be deleted.
109   */
110   virtual void create();
111 
112   /**
113   * Detach the server side pixmap from the Bitmap.
114   * Afterwards, the Bitmap is left as if it never had a server-side resources.
115   */
116   virtual void detach();
117 
118   /**
119   * Destroy the server-side pixmap.
120   * The client-side pixel buffer is not affected.
121   */
122   virtual void destroy();
123 
124   /**
125   * Render the server-side representation of the bitmap from client-side
126   * pixels.
127   */
128   virtual void render();
129 
130   /**
131   * Release the client-side pixels buffer, free it if it was owned.
132   * If it is not owned, the image just forgets about the buffer.
133   */
134   virtual void release();
135 
136   /**
137   * Resize both client-side and server-side representations (if any) to the
138   * given width and height.  The new representations typically contain garbage
139   * after this operation and need to be re-filled.
140   */
141   virtual void resize(FXint w,FXint h);
142 
143   /**
144   * Rescale pixels image to the specified width and height; this calls
145   * resize() to adjust the client and server side representations.
146   */
147   virtual void scale(FXint w,FXint h);
148 
149   /// Mirror bitmap horizontally and/or vertically
150   virtual void mirror(FXbool horizontal,FXbool vertical);
151 
152   /// Rotate bitmap by degrees ccw
153   virtual void rotate(FXint degrees);
154 
155   /**
156   * Crop bitmap to given rectangle; this calls resize() to adjust the client
157   * and server side representations.  The new bitmap may be smaller or larger
158   * than the old one; blank areas are filled with color. There must be at
159   * least one pixel of overlap between the old and the new bitmap.
160   */
161   virtual void crop(FXint x,FXint y,FXint w,FXint h,FXbool color=0);
162 
163   /// Fill bitmap with uniform value
164   virtual void fill(FXbool color);
165 
166   /// Save object to stream
167   virtual void save(FXStream& store) const;
168 
169   /// Load object from stream
170   virtual void load(FXStream& store);
171 
172   /// Save pixel data only
173   virtual FXbool savePixels(FXStream& store) const;
174 
175   /// Load pixel data only
176   virtual FXbool loadPixels(FXStream& store);
177 
178   /// Cleanup
179   virtual ~FXBitmap();
180   };
181 
182 }
183 
184 #endif
185