Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXBitmap.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                             B i t m a p    O b j e c t                        *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1998,2009 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 *********************************************************************************
00021 * $Id: FXBitmap.h,v 1.45 2009/01/06 13:07:22 fox Exp $                          *
00022 ********************************************************************************/
00023 #ifndef FXBITMAP_H
00024 #define FXBITMAP_H
00025 
00026 #ifndef FXDRAWABLE_H
00027 #include "FXDrawable.h"
00028 #endif
00029 
00030 namespace FX {
00031 
00032 // Image rendering hints
00033 enum {
00034   BITMAP_KEEP       = 0x00000001,       // Keep pixel data in client
00035   BITMAP_OWNED      = 0x00000002,       // Pixel data is owned by image
00036   BITMAP_SHMI       = 0x00000020,       // Using shared memory image
00037   BITMAP_SHMP       = 0x00000040        // Using shared memory pixmap
00038   };
00039 
00040 
00041 // Forward declarations
00042 class FXDC;
00043 class FXDCWindow;
00044 
00045 
00046 /**
00047 * A Bitmap is a rectangular array of pixels.  It supports two representations
00048 * of these pixels: a client-side pixel buffer, and a server-side pixmap which
00049 * is stored in an organization directly compatible with the screen, for fast
00050 * drawing onto the device.
00051 * The server-side representation is not directly accessible from the current
00052 * process as it lives in the process of the X Server or GDI.
00053 * The client-side pixel array is of size height x (width+7)/8 bytes, in other
00054 * words 8 pixels packed into a single byte, starting at bit 0 on the left.
00055 */
00056 class FXAPI FXBitmap : public FXDrawable {
00057   FXDECLARE(FXBitmap)
00058   friend class FXDC;
00059   friend class FXDCWindow;
00060 private:
00061 #ifdef WIN32
00062   virtual FXID GetDC() const;
00063   virtual int ReleaseDC(FXID) const;
00064 #endif
00065 protected:
00066   FXuchar *data;        // Pixel data
00067   FXint    bytewidth;   // Number of bytes across
00068   FXuint   options;     // Options
00069 protected:
00070   FXBitmap();
00071 private:
00072   FXBitmap(const FXBitmap&);
00073   FXBitmap &operator=(const FXBitmap&);
00074 public:
00075 
00076   /**
00077   * Create a bitmap.  If a client-side pixel buffer has been specified,
00078   * the bitmap does not own the pixel buffer unless the BITMAP_OWNED flag is
00079   * set.  If the BITMAP_OWNED flag is set but a NULL pixel buffer is
00080   * passed, a pixel buffer will be automatically created and will be owned
00081   * by the bitmap. The flags BITMAP_SHMI and BITMAP_SHMP may be specified for
00082   * large bitmaps to instruct render() to use shared memory to communicate
00083   * with the server.
00084   */
00085   FXBitmap(FXApp* a,const void *pix=NULL,FXuint opts=0,FXint w=1,FXint h=1);
00086 
00087   /// Change options
00088   void setOptions(FXuint opts);
00089 
00090   /// To get to the option flags
00091   FXuint getOptions() const { return options; }
00092 
00093   /// Set pixel data ownership flag
00094   void setOwned(FXbool owned);
00095 
00096   /// Get pixel data ownership flag
00097   FXbool isOwned() const;
00098 
00099   /**
00100   * Populate the bitmap with new pixel data of the same size; it will assume
00101   * ownership of the pixel data if image BITMAP_OWNED option is passed.
00102   * The server-side representation of the image, if it exists, is not updated.
00103   * This can be done by calling render().
00104   */
00105   virtual void setData(FXuchar *pix,FXuint opts=0);
00106 
00107   /**
00108   * Populate the bitmap with new pixel data of a new size; it will assume ownership
00109   * of the pixel data if image BITMAP_OWNED option is passed.  The size of the server-
00110   * side representation of the image, if it exists, is adjusted but the contents are
00111   * not updated yet. This can be done by calling render().
00112   */
00113   virtual void setData(FXuchar *pix,FXuint opts,FXint w,FXint h);
00114 
00115   /// To get to the pixel data
00116   FXuchar* getData() const { return data; }
00117 
00118   /// Get pixel at x,y
00119   FXbool getPixel(FXint x,FXint y) const { return (FXbool)((data[y*bytewidth+(x>>3)]>>(x&7))&1); }
00120 
00121   /// Change pixel at x,y
00122   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)); }
00123 
00124   /**
00125   * Create the server side pixmap, then call render() to fill it with the
00126   * pixel data from the client-side buffer.  After the server-side image has
00127   * been created, the client-side pixel buffer will be deleted unless
00128   * BITMAP_KEEP has been specified.  If the pixel buffer is not owned, i.e.
00129   * the flag BITMAP_OWNED is not set, the pixel buffer will not be deleted.
00130   */
00131   virtual void create();
00132 
00133   /**
00134   * Detach the server side pixmap from the Bitmap.
00135   * Afterwards, the Bitmap is left as if it never had a server-side resources.
00136   */
00137   virtual void detach();
00138 
00139   /**
00140   * Destroy the server-side pixmap.
00141   * The client-side pixel buffer is not affected.
00142   */
00143   virtual void destroy();
00144 
00145   /**
00146   * Retrieves pixels from the server-side bitmap.
00147   */
00148   virtual void restore();
00149 
00150   /**
00151   * Render the server-side representation of the bitmap from client-side
00152   * pixels.
00153   */
00154   virtual void render();
00155 
00156   /**
00157   * Release the client-side pixels buffer, free it if it was owned.
00158   * If it is not owned, the image just forgets about the buffer.
00159   */
00160   virtual void release();
00161 
00162   /**
00163   * Resize both client-side and server-side representations (if any) to the
00164   * given width and height.  The new representations typically contain garbage
00165   * after this operation and need to be re-filled.
00166   */
00167   virtual void resize(FXint w,FXint h);
00168 
00169   /**
00170   * Rescale pixels image to the specified width and height; this calls
00171   * resize() to adjust the client and server side representations.
00172   */
00173   virtual void scale(FXint w,FXint h);
00174 
00175   /// Mirror bitmap horizontally and/or vertically
00176   virtual void mirror(FXbool horizontal,FXbool vertical);
00177 
00178   /// Rotate bitmap by degrees ccw
00179   virtual void rotate(FXint degrees);
00180 
00181   /**
00182   * Crop bitmap to given rectangle; this calls resize() to adjust the client
00183   * and server side representations.  The new bitmap may be smaller or larger
00184   * than the old one; blank areas are filled with color. There must be at
00185   * least one pixel of overlap between the old and the new bitmap.
00186   */
00187   virtual void crop(FXint x,FXint y,FXint w,FXint h,FXbool color=false);
00188 
00189   /// Fill bitmap with uniform value
00190   virtual void fill(FXbool color);
00191 
00192   /// Save object to stream
00193   virtual void save(FXStream& store) const;
00194 
00195   /// Load object from stream
00196   virtual void load(FXStream& store);
00197 
00198   /// Save pixel data only
00199   virtual FXbool savePixels(FXStream& store) const;
00200 
00201   /// Load pixel data only
00202   virtual FXbool loadPixels(FXStream& store);
00203 
00204   /// Cleanup
00205   virtual ~FXBitmap();
00206   };
00207 
00208 }
00209 
00210 #endif
00211 

Copyright © 1997-2009 Jeroen van der Zijp