00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _IMAGE_H_
00014 #define _IMAGE_H_
00015
00016 #include <iostream>
00017 #include <string>
00018
00019 using namespace std;
00020 const int MAXGRAY=256;
00021
00022
00023 enum IMAGE_TYPE { GRAY, COLOR };
00024 enum FILE_TYPE { BW, PGM, PPM, JPG, BMP, FT_NA };
00025
00026
00027 FILE_TYPE getFileType(const string& in_file_name);
00028
00029
00030
00031 class LookUpTable
00032 {
00033 public:
00034 LookUpTable();
00035
00036
00037 LookUpTable( const LookUpTable&);
00038
00039
00040 LookUpTable& operator = (const LookUpTable&);
00041
00042 void set(int,int);
00043
00044 int get(int) const;
00045 private:
00046 unsigned char table[256];
00047 };
00048
00049
00050
00051 class AImage
00052 {
00053 public:
00054
00055 AImage(): width(0), height(0), size(0){ }
00056
00057
00058 AImage(int w,int h)
00059 : width(w), height(h), size( w*h) { }
00060
00061
00062 AImage( const string& in_file_name) : width(0), height(0), size(0){ }
00063
00064
00065 AImage( const AImage& i) : width(i.width), height(i.height), size(i.width * i.height) { }
00066
00067
00068 AImage( const AImage& image, int tlx, int tly, int brx, int bry, bool zero_pading)
00069 : width(image.width + tlx + brx),
00070 height(image.height + bry + bry),
00071 size((image.width + tlx + brx) * (image.height + tly + bry))
00072 { }
00073
00074
00075 int getSize() const { return size; }
00076
00077 int getWidth() const { return width; }
00078
00079 int getHeight() const { return height; }
00080
00081
00082
00083 virtual void saveTo( const string& file_name ) = 0;
00084
00085 virtual IMAGE_TYPE getType() const = 0;
00086
00087 protected:
00088
00089 int width;
00090
00091 int height;
00092
00093 int size;
00094
00095
00096 };
00097
00098
00099
00100
00101 class LUTImage
00102 {
00103 public:
00104
00105 LUTImage() { }
00106
00107
00108 LUTImage(const AImage& i) { }
00109
00110
00111 LUTImage(int w, int h) { }
00112
00113 LUTImage(const LUTImage& li) : theLookUpTable( li.theLookUpTable ) { }
00114
00115 void setLookUpTable( const LookUpTable& t){
00116 theLookUpTable = t;
00117 }
00118
00119 void setInLT( int i, int v){
00120 theLookUpTable.set(i,v);
00121 }
00122
00123 int getInLT( int i) const {
00124 return theLookUpTable.get(i);
00125 }
00126
00127 protected:
00128
00129 LookUpTable theLookUpTable;
00130 };
00131
00132
00133
00134
00135 class GRImage : public AImage
00136 {
00137 public:
00138
00139 GRImage(): AImage(), image_string( NULL ) { }
00140
00141
00142 GRImage(int w,int h): AImage(w,h), image_string( new unsigned char[h*w] )
00143 {
00144 for (int i=0;i<size;i++)
00145 image_string[i] = 0;
00146 }
00147
00148
00149 GRImage( const string& in_file_name);
00150
00151
00152 GRImage( const GRImage& image);
00153
00154
00155 GRImage( const GRImage&, int, int , int , int , bool );
00156
00157
00158 ~GRImage( );
00159
00160 void setPixel(int i, int j,int v);
00161
00162 virtual int getPixel(int i, int j) const;
00163
00164 void setPixel(int n,int v);
00165
00166 void setPixelCliped(int n,int v);
00167
00168 void setPixelCliped(int i,int j,int v);
00169
00170 virtual int getPixel(int n) const;
00171
00172
00173
00174 void saveTo( const string& file_name );
00175
00176 IMAGE_TYPE getType() const {return GRAY; }
00177
00178 GRImage& operator = (const GRImage&);
00179
00180 protected:
00181
00182 friend class GRLUTImage;
00183
00184 virtual void printOnPGM( ostream& out)const;
00185 virtual void printOnBW( ostream& )const;
00186
00187
00188 unsigned char* image_string;
00189
00190
00191 void readFromBW( istream& in);
00192 void readFromPGM( istream& in);
00193 };
00194
00195
00196
00197
00198 class GRLUTImage : public GRImage, public LUTImage
00199 {
00200 public:
00201
00202
00203 GRLUTImage(const GRLUTImage& li) : GRImage( li ), LUTImage((const LUTImage&) li ) { }
00204
00205
00206 GRLUTImage() : GRImage() { }
00207
00208
00209 GRLUTImage(const GRImage& i) : GRImage( i ) { }
00210
00211
00212 GRLUTImage(int w, int h) : GRImage(w,h) { }
00213
00214
00215 int getPixel(int i, int j) const;
00216
00217 int getPixel(int n) const;
00218
00219
00220 inline friend ostream& operator << ( ostream& o, const GRLUTImage& i ) {
00221 i.printOn( o );
00222 return o;
00223 }
00224
00225
00226
00227 private:
00228
00229 void printOn( ostream& )const;
00230 };
00231
00232
00233
00234
00235
00236
00237 class CImage : public AImage
00238 {
00239 public:
00240 CImage(): AImage() { }
00241
00242
00243 CImage(int w,int h): AImage(w,h), redImage(w,h), blueImage(w,h), greenImage(w,h) { }
00244
00245
00246 CImage( const string& in_file_name, FILE_TYPE ft = PPM);
00247
00248
00249 CImage( const CImage& image) :
00250 AImage(image.width, image.height),
00251 redImage( image.redImage ),
00252 blueImage( image.blueImage ),
00253 greenImage( image.greenImage ) { }
00254
00255
00256 CImage( const CImage& i, int p1, int p2, int p3, int p4, bool p5) :
00257 AImage(i, p1, p2, p3, p4, p5),
00258 redImage(i.redImage, p1, p2, p3, p4, p5),
00259 blueImage(i.blueImage, p1, p2, p3, p4, p5),
00260 greenImage(i.greenImage, p1, p2, p3, p4, p5) { }
00261
00262
00263
00265
00266 void setRedPixel(int i, int j,int v) { redImage.setPixel(i,j,v); }
00267 void setRedPixel(int n,int v) { redImage.setPixel(n,v); }
00268
00269 void setBluePixel(int i, int j,int v) { blueImage.setPixel(i,j,v); }
00270 void setBluePixel(int n,int v) { blueImage.setPixel(n,v); }
00271
00272 void setGreenPixel(int i, int j,int v) { greenImage.setPixel(i,j,v); }
00273 void setGreenPixel(int n,int v) { greenImage.setPixel(n,v); }
00274
00275 void setRedPixelCliped(int n,int v) { redImage.setPixelCliped(n,v); }
00276 void setRedPixelCliped(int i,int j,int v) { redImage.setPixelCliped(i,j,v); }
00277
00278 void setBluePixelCliped(int n,int v) { blueImage.setPixelCliped(n,v); }
00279 void setBluePixelCliped(int i,int j,int v){ blueImage.setPixelCliped(i,j,v); }
00280
00281 void setGreenPixelCliped(int n,int v) { greenImage.setPixelCliped(n,v); }
00282 void setGreenPixelCliped(int i,int j,int v) { greenImage.setPixelCliped(i,j,v); }
00283
00284
00285
00286 virtual unsigned char getRedPixel(int i, int j) const { return redImage.getPixel(i,j); }
00287 virtual unsigned char getRedPixel(int n) const { return redImage.getPixel(n); }
00288
00289 virtual unsigned char getBluePixel(int i, int j) const { return blueImage.getPixel(i,j); }
00290 virtual unsigned char getBluePixel(int n) const { return blueImage.getPixel(n); }
00291
00292 virtual unsigned char getGreenPixel(int i, int j) const { return greenImage.getPixel(i,j); }
00293 virtual unsigned char getGreenPixel(int n) const { return greenImage.getPixel(n); }
00294
00295 const GRImage& getRedImage() const { return redImage; }
00296
00297 const GRImage& getGreenImage() const { return greenImage; }
00298
00299 const GRImage& getBlueImage() const { return blueImage; }
00300
00301
00302
00303 void saveTo( const string& file_name );
00304
00305 IMAGE_TYPE getType() const { return COLOR; }
00306
00307
00308 protected:
00309
00310 friend class CLUTImage;
00311 CImage& operator = (const CImage&);
00312
00313
00314
00315 void printOnPPM( ostream& out)const;
00316
00317 void readFromPPM( istream& in);
00318
00319
00320 GRImage redImage;
00321 GRImage blueImage;
00322 GRImage greenImage;
00323 };
00324
00325
00326
00327
00328 class CLUTImage : public CImage, public LUTImage
00329 {
00330 public:
00331
00332 CLUTImage(const CLUTImage& li) : CImage( li ), LUTImage((const LUTImage&)li){ }
00333
00334 CLUTImage() : CImage() { }
00335
00336
00337 CLUTImage(const CImage& i) : CImage( i ){ }
00338
00339
00340 CLUTImage(int w, int h) : CImage(w,h) { }
00341
00342
00343
00344
00345
00346
00348
00349
00350
00351 virtual unsigned char getRedPixel(int i, int j) const {
00352 return theLookUpTable.get(CImage::getRedPixel(i,j));
00353 }
00354 virtual unsigned char getRedPixel(int n) const {
00355 return theLookUpTable.get(CImage::getRedPixel(n));
00356 }
00357
00358 virtual unsigned char getBluePixel(int i, int j) const {
00359 return theLookUpTable.get(CImage::getBluePixel(i,j));
00360 }
00361 virtual unsigned char getBluePixel(int n) const {
00362 return theLookUpTable.get(CImage::getBluePixel(n));
00363 }
00364 virtual unsigned char getGreenPixel(int i, int j) const {
00365 return theLookUpTable.get(CImage::getGreenPixel(i,j));
00366 }
00367 virtual unsigned char getGreenPixel(int n) const{
00368 return theLookUpTable.get(CImage::getGreenPixel(n));
00369 }
00370
00371
00372 private:
00373
00374
00375 };
00376
00377
00378
00379
00380
00381 void convert(const CImage* ci, GRImage* gi);
00382
00383
00384 #endif