00001 /* 00002 File: Image.h 00003 00004 Function: Defines an image: a 2D array of pixels. Provides common 00005 operations on images. 00006 00007 Author(s): Andrew Willmott 00008 00009 Copyright: (c) 1997-2000, Andrew Willmott 00010 */ 00011 00012 00013 #ifndef __Image__ 00014 #define __Image__ 00015 00016 #include "gcl/Colour.h" 00017 #include "cl/FileName.h" 00018 00019 // --- Useful enums ----------------------------------------------------------- 00020 00021 enum ImgChannel 00022 { 00023 chMono, 00024 chRed, 00025 chGreen, 00026 chBlue, 00027 chAlpha, 00028 chMatte, 00029 chDepth, 00030 chMax = 16 00031 }; 00032 00033 enum ImgStoreType 00034 { 00035 imgByte, 00036 imgUInt16, 00037 imgUInt32, 00038 imgFloat, 00039 imgDouble, 00040 imgFloat3, 00041 imgDouble3, 00042 imgStoreMax 00043 }; 00044 00045 enum 00046 { 00047 rgba_R = 0, 00048 rgba_G = 1, 00049 rgba_B = 2, 00050 rgba_A = 3 00051 }; 00052 00053 struct RGBAPixel 00054 { 00055 Byte ch[4]; 00056 }; 00057 00058 // Ward's RGB + exponent format 00059 typedef RGBAPixel RGBEPixel; 00060 00061 enum ImgTag 00062 { 00063 imgRGBATag, 00064 imgByteTag, 00065 imgRGBETag, 00066 imgChannelTag, 00067 imgMaxTags 00068 }; 00069 00070 // --- Generic image class ---------------------------------------------------- 00071 00072 class Image 00073 { 00074 public: 00075 Image(ImgTag it) : width(0), height(0), tag(it) {}; 00076 00077 virtual Void SetSize(Int width, Int height) = 0; 00078 00079 Int Height() const { return(height); }; 00080 Int Width() const { return(width); }; 00081 00082 virtual Void Clear(const Colour &c) = 0; 00083 virtual Void CopyFrom(Int x, Int y, Image &i); 00084 00085 virtual Void GammaCorrect(ClrReal gamma = 2.2); 00086 virtual Void Scale(ClrReal scale); 00087 virtual ClrReal MaxComponent(); 00088 virtual Colour AverageColour(); 00089 virtual Void FindComponentBounds(Colour &min, Colour &max); 00090 virtual Void Transform(const ClrTrans &trans); 00092 virtual Void Over(Int x, Int y, Image &i); 00093 virtual Void DownSample(Image &out); 00094 00095 // pixel operations 00096 virtual Void SetPixel(Int x, Int y, const Colour &c) = 0; 00097 virtual Colour GetPixel(Int x, Int y) const = 0; 00098 00099 virtual Void SetRealPixel(Int x, Int y, ClrReal r, 00100 ImgChannel channel = chMono); 00101 virtual ClrReal GetRealPixel(Int x, Int y, 00102 ImgChannel channel = chMono) const; 00103 00104 // span operations 00105 virtual Void SetSpan(Int row, Int start, Int length, 00106 const Colour *src) = 0; 00107 virtual Void GetSpan(Int row, Int start, Int length, 00108 Colour *dst) const = 0; 00109 00110 virtual Void SetRGBASpan(Int row, Int start, Int length, 00111 const RGBAPixel *src); 00112 virtual Void GetRGBASpan(Int row, Int start, Int length, 00113 RGBAPixel *dst) const; 00114 00115 virtual Void SetRealSpan(Int row, Int start, Int length, 00116 const ClrReal *src, ImgChannel channel = chMono); 00117 virtual Void GetRealSpan(Int row, Int start, Int length, 00118 ClrReal *dst, ImgChannel channel = chMono) const; 00119 00120 virtual Void SetByteSpan(Int row, Int start, Int length, 00121 const Byte *src, ImgChannel channel = chMono); 00122 virtual Void GetByteSpan(Int row, Int start, Int length, 00123 Byte *dst, ImgChannel channel = chMono) const; 00124 00125 // generic load/save : type is inferred from the extension 00126 Int Save(FileName &filename); 00127 Int Load(FileName &filename); 00128 static Void PrintSupportedFormats(ostream &s); 00129 00130 // specialised load/save 00131 Int SavePPM(const Char *filename); 00132 Int LoadPPM(const Char *filename); 00133 Int SaveTIFF(const Char *filename); 00134 Int LoadTIFF(const Char *filename); 00135 Int SaveJPEG(const Char *filename); 00136 Int LoadJPEG(const Char *filename); 00137 Int SaveGIF(const Char *filename); 00138 Int LoadGIF(const Char *filename); 00139 00140 ImgTag Tag() const { return(tag); }; 00141 00142 static Int sJPEGQuality; // 0..100, default = 90 00143 00144 protected: 00145 Int width; 00146 Int height; 00147 ImgTag tag; 00148 }; 00149 00150 00151 // --- Specific image classes ------------------------------------------------- 00152 00153 class RGBAImage : public Image 00154 { 00155 public: 00156 RGBAImage() : Image(imgRGBATag), data(0) {}; 00157 00158 Void SetSize(Int width, Int height); 00159 00160 Void Clear(const Colour &c); 00161 Void SetPixel(Int x, Int y, const Colour &c); 00162 Colour GetPixel(Int x, Int y) const; 00163 Void SetSpan(Int row, Int start, Int length, const Colour *src); 00164 Void GetSpan(Int row, Int start, Int length, Colour *dst) const; 00165 Void SetRGBASpan(Int row, Int start, Int length, const RGBAPixel *src); 00166 Void GetRGBASpan(Int row, Int start, Int length, RGBAPixel *dst) const; 00167 00168 RGBAPixel 00169 *RGBAData() const {return(data);}; 00170 00171 protected: 00172 RGBAPixel *data; 00173 }; 00174 00175 class ByteImage : public Image 00176 { 00177 public: 00178 ByteImage() : Image(imgByteTag), data(0) {}; 00179 00180 Void SetSize(Int width, Int height); 00181 00182 Void Clear(const Colour &c); 00183 Void SetPixel(Int x, Int y, const Colour &c); 00184 Colour GetPixel(Int x, Int y) const; 00185 Void SetRealPixel(Int x, Int y, ClrReal r, ImgChannel channel = chMono); 00186 ClrReal GetRealPixel(Int x, Int y, ImgChannel channel = chMono) const; 00187 00188 Void SetSpan(Int row, Int start, Int length, const Colour *src); 00189 Void GetSpan(Int row, Int start, Int length, Colour *dst) const; 00190 Void SetByteSpan(Int row, Int start, Int length, 00191 const Byte *src, ImgChannel channel = chMono); 00192 Void GetByteSpan(Int row, Int start, Int length, 00193 Byte *dst, ImgChannel channel = chMono) const; 00194 00195 Byte *ByteData() const {return(data);}; 00196 00197 protected: 00198 Byte *data; 00199 }; 00200 00201 class RGBEImage : public Image 00202 { 00203 public: 00204 RGBEImage() : Image(imgRGBETag), data(0) {}; 00205 00206 Void SetSize(Int width, Int height); 00207 00208 Void Clear(const Colour &c); 00209 Void SetPixel(Int x, Int y, const Colour &c); 00210 Colour GetPixel(Int x, Int y) const; 00211 Void SetSpan(Int row, Int start, Int length, const Colour *src); 00212 Void GetSpan(Int row, Int start, Int length, Colour *dst) const; 00213 00214 Int SavePIC(const Char *filename); 00215 Int LoadPIC(const Char *filename); 00216 00217 RGBEPixel 00218 *RGBEData() const {return(data);}; 00219 00220 protected: 00221 RGBEPixel *data; 00222 }; 00223 00224 Colour RGBEToColour(RGBEPixel rgbe); 00225 RGBEPixel ColourToRGBE(const Colour &c); 00226 00227 00228 // --- Channel-based image ---------------------------------------------------- 00229 00230 #ifdef UNFINISHED 00231 class ChannelImage : public Image 00232 { 00233 public: 00234 ChannelImage(); 00235 00236 SetImageType(); 00237 SetImageLayer(); 00238 00239 protected: 00240 Void *chData[chMax]; 00241 ImgStoreType chType[chMax]; 00242 } 00243 #endif 00244 00245 00246 // --- Conversion routines ---------------------------------------------------- 00247 00248 inline Colour RGBAToColour(RGBAPixel rgba) 00249 { 00250 Colour result; 00251 result[0] = rgba.ch[rgba_R] / 255.0; 00252 result[1] = rgba.ch[rgba_G] / 255.0; 00253 result[2] = rgba.ch[rgba_B] / 255.0; 00254 return(result); 00255 } 00256 00257 inline RGBAPixel ColourToRGBA(const Colour &c) 00258 { 00259 RGBAPixel result; 00260 result.ch[rgba_R] = (Byte) (c[0] * 255.0); 00261 result.ch[rgba_G] = (Byte) (c[1] * 255.0); 00262 result.ch[rgba_B] = (Byte) (c[2] * 255.0); 00263 return(result); 00264 } 00265 00266 inline Colour ByteToColour(Byte b) 00267 { 00268 Colour result; 00269 ClrReal temp = b / 255.0; 00270 00271 result[0] = temp; 00272 result[1] = temp; 00273 result[2] = temp; 00274 return(result); 00275 } 00276 00277 inline Byte ColourToByte(const Colour &c) 00278 { 00279 ClrReal temp; 00280 00281 temp = c[0] + c[1] + c[2]; 00282 return((Byte) (temp * (255.0 / 3.0))); 00283 } 00284 00285 #endif