imagebuf.h

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 1998 Jeffrey S. Freedman
00009 
00010 This library is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU Library General Public
00012 License as published by the Free Software Foundation; either
00013 version 2 of the License, or (at your option) any later version.
00014 
00015 This library is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 Library General Public License for more details.
00019 
00020 You should have received a copy of the GNU Library General Public
00021 License along with this library; if not, write to the
00022 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023 Boston, MA  02111-1307, USA.
00024 */
00025 
00026 #ifndef INCL_IMAGEBUF
00027 #define INCL_IMAGEBUF 1
00028 
00029 
00030           // Table for translating palette vals.:
00031 //typedef unsigned char *Xform_palette; // Should be 256-bytes.
00032 
00033 /*
00034  *  This class represents a single transparent color by providing a
00035  *  palette for its effect on all the other colors.
00036  */
00037 class Xform_palette
00038   {
00039 public:
00040   unsigned char colors[256];  // For transforming 8-bit colors.
00041   unsigned char r,g,b,a;    // Actual colour and alpha.
00042   void set_color(int cr, int cg, int cb, int ca)
00043     { r = cr; g = cg; b = cb; a = ca;  }
00044   unsigned char operator[](int i) const
00045     { return colors[i]; }
00046   };
00047 
00048 /*
00049  *  Here's a generic off-screen buffer.  It's up to the derived classes
00050  *  to set the data.
00051  */
00052 class Image_buffer
00053   {
00054 protected:
00055   unsigned int width, height; // Dimensions (in pixels).
00056   int depth;      // # bits/pixel.
00057   int pixel_size;     // # bytes/pixel.
00058   unsigned char *bits;    // Allocated image buffer.
00059   unsigned int line_width;  // # words/scan-line.
00060 private:
00061   int clipx, clipy, clipw, cliph; // Clip rectangle.
00062           // Clip.  Rets. 0 if nothing to draw.
00063   int clip_internal(int& srcx, int& srcw, int& destx, 
00064               int clips, int clipl)
00065     {
00066     if (destx < clips)
00067       {
00068       if ((srcw += (destx - clips)) <= 0)
00069         return (0);
00070       srcx -= (destx - clips);
00071       destx = clips;
00072       }
00073     if (destx + srcw > (clips + clipl))
00074       if ((srcw = ((clips + clipl) - destx)) <= 0)
00075         return (0);
00076     return (1);
00077     }
00078 protected:
00079   int clip_x(int& srcx, int& srcw, int& destx, int desty)
00080     { return desty < clipy || desty >= clipy + cliph ? 0
00081       : clip_internal(srcx, srcw, destx, clipx, clipw); }
00082   int clip(int& srcx, int& srcy, int& srcw, int& srch, 
00083             int& destx, int& desty)
00084     {
00085           // Start with x-dim.
00086     return (clip_internal(srcx, srcw, destx, clipx, clipw) &&
00087             clip_internal(srcy, srch, desty, clipy, cliph));
00088     }
00089   Image_buffer(unsigned int w, unsigned int h, int dpth);
00090 public:
00091   friend class Image_buffer8;
00092   friend class Image_buffer16;
00093   virtual ~Image_buffer()
00094     {
00095     delete [] bits;   // In case Image_window didn't.
00096     }
00097   friend class Image_window;
00098   unsigned char *get_bits() // Get ->data.
00099     { return bits; }
00100   unsigned int get_width()
00101     { return width; }
00102   unsigned int get_height()
00103     { return height; }
00104   unsigned int get_line_width()
00105     { return line_width; }
00106   void clear_clip()   // Reset clip to whole window.
00107     { clipx = clipy = 0; clipw = width; cliph = height; }
00108           // Set clip.
00109   void set_clip(int x, int y, int w, int h)
00110     {
00111     clipx = x;
00112     clipy = y;
00113     clipw = w;
00114     cliph = h;
00115     }
00116           // Is rect. visible within clip?
00117   int is_visible(int x, int y, int w, int h)
00118     { return (!(x >= clipx + clipw || y >= clipy + cliph ||
00119       x + w <= clipx || y + h <= clipy)); }
00120   /*
00121    *  16-bit color methods.  Default is to ignore them.
00122    */
00123           // Fill with given pixel.
00124   virtual void fill16(unsigned short pix)
00125     {  }
00126           // Fill rect. wth pixel.
00127   virtual void fill16(unsigned short pix, int srcw, int srch,
00128             int destx, int desty)
00129     {  }
00130           // Copy rectangle into here.
00131   virtual void copy16(unsigned short *src_pixels,
00132         int srcw, int srch, int destx, int desty)
00133     {  }
00134           // Copy rect. with transp. color.
00135   virtual void copy_transparent16(unsigned char *src_pixels, int srcw,
00136           int srch, int destx, int desty)
00137     {  }
00138   /*
00139    *  8-bit color methods:
00140    */
00141           // Fill with given (8-bit) value.
00142   virtual void fill8(unsigned char val) = 0;
00143           // Fill rect. with pixel.
00144   virtual void fill8(unsigned char val, int srcw, int srch,
00145             int destx, int desty) = 0;
00146           // Fill line with pixel.
00147   virtual void fill_line8(unsigned char val, int srcw,
00148             int destx, int desty) = 0;
00149           // Copy rectangle into here.
00150   virtual void copy8(unsigned char *src_pixels,
00151         int srcw, int srch, int destx, int desty) = 0;
00152           // Copy line to here.
00153   virtual void copy_line8(unsigned char *src_pixels, int srcw,
00154             int destx, int desty) = 0;
00155           // Copy with translucency table.
00156   virtual void copy_line_translucent8(
00157     unsigned char *src_pixels, int srcw,
00158     int destx, int desty, int first_translucent,
00159     int last_translucent, Xform_palette *xforms) = 0;
00160           // Apply translucency to a line.
00161   virtual void fill_line_translucent8(unsigned char val,
00162     int srcw, int destx, int desty, Xform_palette& xform) = 0;
00163           // Apply translucency to a rectangle
00164   virtual void fill_translucent8(unsigned char val, int srcw, int srch, 
00165       int destx, int desty, Xform_palette& xform) = 0;
00166           // Copy rect. with transp. color.
00167   virtual void copy_transparent8(unsigned char *src_pixels, int srcw,
00168           int srch, int destx, int desty) = 0;
00169   /*
00170    *  Depth-independent methods:
00171    */
00172   virtual Image_buffer *create_another(int w, int h) = 0;
00173           // Copy within itself.
00174   virtual void copy(int srcx, int srcy, int srcw, int srch, 
00175             int destx, int desty) = 0;
00176           // Get rect. into another buf.
00177   virtual void get(Image_buffer *dest, int srcx, int srcy) = 0;
00178           // Put rect. back.
00179   virtual void put(Image_buffer *src, int destx, int desty) = 0;
00180   
00181   virtual void fill_static(int black, int gray, int white) = 0;
00182 
00183   };
00184 
00185 #endif

Generated on Mon Jul 9 14:42:47 2007 for ExultEngine by  doxygen 1.5.1