00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __CS_CSGFX_MEMIMAGE_H__
00021 #define __CS_CSGFX_MEMIMAGE_H__
00022
00023 #include "csextern.h"
00024 #include "csutil/leakguard.h"
00025 #include "csutil/scf_implementation.h"
00026
00027 #include "csgfx/imagebase.h"
00028 #include "csgfx/imagetools.h"
00029 #include "csgfx/rgbpixel.h"
00030
00040 class CS_CRYSTALSPACE_EXPORT csImageMemory :
00041 public scfImplementationExt0<csImageMemory, csImageBase>
00042 {
00043 private:
00045 void ConstructCommon ();
00047 void ConstructWHDF (int width, int height, int depth, int format);
00049 void ConstructSource (iImage* source);
00051 void ConstructBuffers (int width, int height, void* buffer,
00052 bool destroy, int format, csRGBpixel* palette);
00053 protected:
00055 int Width;
00057 int Height;
00059 int Depth;
00066 csRef<iDataBuffer> databuf;
00068 csRGBpixel* Palette;
00070 uint8* Alpha;
00072 int Format;
00074 bool has_keycolour;
00076 csRGBpixel keycolour;
00078 bool destroy_image;
00080 csImageType imageType;
00082
00083
00084
00085 csArray<csRef<iImage> > mipmaps;
00086
00091 csImageMemory (int iFormat);
00101 void SetDimensions (int newWidth, int newHeight);
00102 void SetDimensions (int newWidth, int newHeight, int newDepth);
00103
00105 void AllocImage ();
00107 void EnsureImage ();
00111 void FreeImage ();
00112
00113 void InternalConvertFromRGBA (iDataBuffer* imageData);
00114 void InternalConvertFromPal8 (iDataBuffer* imageData, uint8* alpha,
00115 csRGBpixel* iPalette, int nPalColors = 256);
00116 public:
00117 CS_LEAKGUARD_DECLARE (csImageMemory);
00118
00126 csImageMemory (int width, int height, int format = CS_IMGFMT_TRUECOLOR);
00135 csImageMemory (int width, int height, int depth, int format);
00148 csImageMemory (int width, int height, void* buffer, bool destroy,
00149 int format = CS_IMGFMT_TRUECOLOR, csRGBpixel* palette = 0);
00160 csImageMemory (int width, int height, const void* buffer,
00161 int format = CS_IMGFMT_TRUECOLOR, const csRGBpixel* palette = 0);
00166 csImageMemory (iImage* source);
00171 csImageMemory (iImage* source, int newFormat);
00172
00173 virtual ~csImageMemory ();
00174
00176 void* GetImagePtr ();
00178 csRGBpixel* GetPalettePtr ();
00180 uint8* GetAlphaPtr ();
00181
00182 virtual const void* GetImageData () { return GetImagePtr (); }
00183 virtual int GetWidth () const { return Width; }
00184 virtual int GetHeight () const { return Height; }
00185 virtual int GetDepth () const { return Depth; }
00186
00187 virtual const char* GetRawFormat () const
00188 {
00189 return "a8b8g8r8";
00190 }
00191 virtual csRef<iDataBuffer> GetRawData () const
00192 {
00193
00194 if ((Format & CS_IMGFMT_MASK) == CS_IMGFMT_TRUECOLOR)
00195 return databuf;
00196 CS_ASSERT_MSG ("Not implemented yet: get RGB data from palette", false);
00197 return 0;
00198 }
00199 virtual int GetFormat () const { return Format; }
00200 virtual const csRGBpixel* GetPalette () { return GetPalettePtr (); }
00201 virtual const uint8* GetAlpha () { return GetAlphaPtr (); }
00202
00203 virtual bool HasKeyColor () const { return has_keycolour; }
00204
00205 virtual void GetKeyColor (int &r, int &g, int &b) const
00206 { r = keycolour.red; g = keycolour.green; b = keycolour.blue; }
00207
00209 void Clear (const csRGBpixel &colour);
00210
00212 void CheckAlpha ();
00222 void SetFormat (int iFormat);
00223
00225 virtual void SetKeyColor (int r, int g, int b);
00227 virtual void ClearKeyColor ();
00228
00233 virtual void ApplyKeyColor ();
00234
00235 virtual csImageType GetImageType () const { return imageType; }
00236 void SetImageType (csImageType type) { imageType = type; }
00237
00238 virtual uint HasMipmaps () const
00239 {
00240 size_t num = mipmaps.GetSize ();
00241 while ((num > 0) && (mipmaps[num-1] == 0)) num--;
00242 return (uint)num;
00243 }
00244 virtual csRef<iImage> GetMipmap (uint num)
00245 {
00246 if (num == 0) return this;
00247 if (num <= mipmaps.GetSize ()) return mipmaps[num-1];
00248 return 0;
00249 }
00258 bool SetMipmap (uint num, iImage* mip)
00259 {
00260 if (num == 0) return false;
00261 mipmaps.GetExtend (num-1) = mip;
00262 return true;
00263 }
00264
00266 bool Copy (iImage* srcImage, int x, int y, int width, int height);
00271 bool CopyScale (iImage* srcImage, int x, int y, int width, int height);
00276 bool CopyTile (iImage* srcImage, int x, int y, int width, int height);
00277
00289 void ConvertFromRGBA (csRGBpixel* iImage);
00300 void ConvertFromPal8 (uint8* iImage, uint8* alpha, csRGBpixel* iPalette,
00301 int nPalColors = 256);
00312 void ConvertFromPal8 (uint8* iImage, uint8* alpha,
00313 const csRGBcolor* iPalette, int nPalColors = 256);
00314 };
00315
00318 #endif // __CS_CSGFX_MEMIMAGE_H__