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

Copyright © 1997-2009 Jeroen van der Zijp