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

FXImage.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                             I m a g e    O b j e c t                          *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1997,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (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 GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXImage.h,v 1.64 2006/01/22 17:58:05 fox Exp $                           *
00023 ********************************************************************************/
00024 #ifndef FXIMAGE_H
00025 #define FXIMAGE_H
00026 
00027 #ifndef FXDRAWABLE_H
00028 #include "FXDrawable.h"
00029 #endif
00030 
00031 namespace FX {
00032 
00033 
00034 /// Image rendering hints
00035 enum {
00036   IMAGE_KEEP       = 0x00000001,      /// Keep pixel data in client
00037   IMAGE_OWNED      = 0x00000002,      /// Pixel data is owned by image
00038   IMAGE_DITHER     = 0,               /// Dither image to look better
00039   IMAGE_NEAREST    = 0x00000004,      /// Turn off dithering and map to nearest color
00040   IMAGE_OPAQUE     = 0x00000008,      /// Force opaque background
00041   IMAGE_ALPHACOLOR = 0x00000010,      /// Override transparancy color
00042   IMAGE_SHMI       = 0x00000020,      /// Using shared memory image
00043   IMAGE_SHMP       = 0x00000040,      /// Using shared memory pixmap
00044   IMAGE_ALPHAGUESS = 0x00000080       /// Guess transparency color from corners
00045   };
00046 
00047 
00048 class FXDC;
00049 class FXDCWindow;
00050 
00051 
00052 /**
00053 * An Image is a rectangular array of pixels.  It supports two representations
00054 * of these pixels: a client-side pixel buffer which is stored as an array of
00055 * FXColor, and a server-side pixmap which is stored in an organization directly
00056 * compatible with the screen, for fast drawing onto the device.
00057 * The server-side representation is not directly accessible from the current
00058 * process as it lives in the process of the X Server or GDI.
00059 */
00060 class FXAPI FXImage : public FXDrawable {
00061   FXDECLARE(FXImage)
00062   friend class FXDC;
00063   friend class FXDCWindow;
00064 protected:
00065   FXColor *data;        // Pixel data
00066   FXuint   options;     // Options
00067 private:
00068 #ifdef WIN32
00069   virtual FXID GetDC() const;
00070   virtual int ReleaseDC(FXID) const;
00071 #endif
00072 #ifndef WIN32
00073   void render_true_32(void *xim,FXuchar *img);
00074   void render_true_24(void *xim,FXuchar *img);
00075   void render_true_16_fast(void *xim,FXuchar *img);
00076   void render_true_16_dither(void *xim,FXuchar *img);
00077   void render_true_8_fast(void *xim,FXuchar *img);
00078   void render_true_8_dither(void *xim,FXuchar *img);
00079   void render_true_N_fast(void *xim,FXuchar *img);
00080   void render_true_N_dither(void *xim,FXuchar *img);
00081   void render_index_4_fast(void *xim,FXuchar *img);
00082   void render_index_4_dither(void *xim,FXuchar *img);
00083   void render_index_8_fast(void *xim,FXuchar *img);
00084   void render_index_8_dither(void *xim,FXuchar *img);
00085   void render_index_N_fast(void *xim,FXuchar *img);
00086   void render_index_N_dither(void *xim,FXuchar *img);
00087   void render_gray_8_fast(void *xim,FXuchar *img);
00088   void render_gray_8_dither(void *xim,FXuchar *img);
00089   void render_gray_N_fast(void *xim,FXuchar *img);
00090   void render_gray_N_dither(void *xim,FXuchar *img);
00091   void render_mono_1_fast(void *xim,FXuchar *img);
00092   void render_mono_1_dither(void *xim,FXuchar *img);
00093 #endif
00094 protected:
00095   FXImage();
00096 private:
00097   FXImage(const FXImage&);
00098   FXImage &operator=(const FXImage&);
00099 public:
00100 
00101   /**
00102   * Create an image.  If a client-side pixel buffer has been specified,
00103   * the image does not own the pixel buffer unless the IMAGE_OWNED flag is
00104   * set.  If the IMAGE_OWNED flag is set but a NULL pixel buffer is
00105   * passed, a pixel buffer will be automatically created and will be owned
00106   * by the image. The flags IMAGE_SHMI and IMAGE_SHMP may be specified for
00107   * large images to instruct render() to use shared memory to communicate
00108   * with the server.
00109   */
00110   FXImage(FXApp* a,const FXColor *pix=NULL,FXuint opts=0,FXint w=1,FXint h=1);
00111 
00112   /// Change options
00113   void setOptions(FXuint opts);
00114 
00115   /// To get to the option flags
00116   FXuint getOptions() const { return options; }
00117 
00118   /**
00119   * Populate the image with new pixel data of the same size; it will assume
00120   * ownership of the pixel data if image IMAGE_OWNED option is passed.
00121   * The server-side representation of the image, if it exists, is not updated.
00122   * This can be done by calling render().
00123   */
00124   virtual void setData(FXColor *pix,FXuint opts=0);
00125 
00126   /**
00127   * Populate the image with new pixel data of a new size; it will assume ownership
00128   * of the pixel data if image IMAGE_OWNED option is passed.  The size of the server-
00129   * side representation of the image, if it exists, is adjusted but the contents are
00130   * not updated yet. This can be done by calling render().
00131   */
00132   virtual void setData(FXColor *pix,FXuint opts,FXint w,FXint h);
00133 
00134   /// Return pointer to the pixel data of the image
00135   FXColor* getData() const { return data; }
00136 
00137   /// Get pixel at x,y
00138   FXColor getPixel(FXint x,FXint y) const { return data[y*width+x]; }
00139 
00140   /// Change pixel at x,y
00141   void setPixel(FXint x,FXint y,FXColor color){ data[y*width+x]=color; }
00142 
00143   /// Scan the image and return false if fully opaque
00144   bool hasAlpha() const;
00145 
00146   /**
00147   * Create the server side pixmap, then call render() to fill it with the
00148   * pixel data from the client-side buffer.  After the server-side image has
00149   * been created, the client-side pixel buffer will be deleted unless
00150   * IMAGE_KEEP has been specified.  If the pixel buffer is not owned, i.e.
00151   * the flag IMAGE_OWNED is not set, the pixel buffer will not be deleted,
00152   * however the pixel buffer will be set to NULL.
00153   */
00154   virtual void create();
00155 
00156   /**
00157   * Detach the server side pixmap from the Image.
00158   * Afterwards, the Image is left as if it never had a server-side resources.
00159   */
00160   virtual void detach();
00161 
00162   /**
00163   * Destroy the server-side pixmap.
00164   * The client-side pixel buffer is not affected.
00165   */
00166   virtual void destroy();
00167 
00168   /**
00169   * Retrieves pixels from the server-side image.  For example, to make
00170   * screen snapshots, or to retrieve an image after it has been drawn
00171   * into by various means.
00172   */
00173   virtual void restore();
00174 
00175   /**
00176   * Render the server-side representation of the image from client-side
00177   * pixels.  Normally, IMAGE_DITHER is used which causes the server-side
00178   * representation to be rendered using a 16x16 ordered dither if necessary;
00179   * however if IMAGE_NEAREST is used a faster (but uglier-looking), nearest
00180   * neighbor algorithm is used.
00181   */
00182   virtual void render();
00183 
00184   /**
00185   * Release the client-side pixels buffer, free it if it was owned.
00186   * If it is not owned, the image just forgets about the buffer.
00187   */
00188   virtual void release();
00189 
00190   /**
00191   * Resize both client-side and server-side representations (if any) to the
00192   * given width and height.  The new representations typically contain garbage
00193   * after this operation and need to be re-filled.
00194   */
00195   virtual void resize(FXint w,FXint h);
00196 
00197   /**
00198   * Rescale pixels image to the specified width and height; this calls
00199   * resize() to adjust the client and server side representations.
00200   */
00201   virtual void scale(FXint w,FXint h,FXint quality=0);
00202 
00203   /// Mirror image horizontally and/or vertically
00204   virtual void mirror(bool horizontal,bool vertical);
00205 
00206   /**
00207   * Rotate image by degrees ccw; this calls resize() to adjust the client
00208   * and server side representations if necessary.
00209   */
00210   virtual void rotate(FXint degrees);
00211 
00212   /**
00213   * Crop image to given rectangle; this calls resize() to adjust the client
00214   * and server side representations.  The new image may be smaller or larger
00215   * than the old one; blank areas are filled with color. There must be at
00216   * least one pixel of overlap between the old and the new image.
00217   */
00218   virtual void crop(FXint x,FXint y,FXint w,FXint h,FXColor color=0);
00219 
00220   /// Fill image with uniform color
00221   virtual void fill(FXColor color);
00222 
00223   /// Fade image to uniform color
00224   virtual void fade(FXColor color,FXint factor=255);
00225 
00226   /**
00227   * Shear image horizontally; the number of pixels is equal to the
00228   * shear parameter times 256.  The area outside the image is filled
00229   * with transparent black, unless another color is specified.
00230   */
00231   virtual void xshear(FXint shear,FXColor clr=0);
00232 
00233   /**
00234   * Shear image vertically; the number of pixels is equal to the
00235   * shear parameter times 256.  The area outside the image is filled
00236   * with transparent black, unless another color is specified.
00237   */
00238   virtual void yshear(FXint shear,FXColor clr=0);
00239 
00240   /// Fill horizontal gradient
00241   virtual void hgradient(FXColor left,FXColor right);
00242 
00243   /// Fill vertical gradient
00244   virtual void vgradient(FXColor top,FXColor bottom);
00245 
00246   /// Fill with gradient
00247   virtual void gradient(FXColor topleft,FXColor topright,FXColor bottomleft,FXColor bottomright);
00248 
00249   /// Blend image over uniform color
00250   virtual void blend(FXColor color);
00251 
00252   /// Save object to stream
00253   virtual void save(FXStream& store) const;
00254 
00255   /// Load object from stream
00256   virtual void load(FXStream& store);
00257 
00258   /// Save pixel data only
00259   virtual bool savePixels(FXStream& store) const;
00260 
00261   /// Load pixel data only
00262   virtual bool loadPixels(FXStream& store);
00263 
00264   /// Destructor
00265   virtual ~FXImage();
00266   };
00267 
00268 }
00269 
00270 #endif

Copyright © 1997-2005 Jeroen van der Zijp