00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef IMAGE_H_HAS_BEEN_INCLUDED
00027 #define IMAGE_H_HAS_BEEN_INCLUDED
00028
00029 #define OUTSIDE_GLUE_CORE
00030
00031 #include "std/config.H"
00032 #include <math.h>
00033 #include "std/list.H"
00034 #include "std/strings.H"
00035 #include "mlib/point2i.H"
00036
00037
00038
00039
00040 inline unsigned int
00041 getPower2Size(int n)
00042 {
00043 return (1 << ((n>0) ? (int)ceil(log((double) n)/log(2.0)) : 0));
00044 }
00045
00046 typedef unsigned int uint;
00047 typedef unsigned char uchar;
00048
00049 typedef const class Image cImage;
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 class Image {
00060 protected:
00061 uint _width;
00062 uint _height;
00063 uint _bpp;
00064 uchar* _data;
00065 bool _no_delete;
00066
00067 enum { PNG_BYTES_TO_CHECK = 8 };
00068 FILE* open_png(char* file);
00069
00070 str_ptr _file;
00071
00072 public:
00073 Image() : _width(0), _height(0), _bpp(1), _data(0), _no_delete(0) {}
00074 Image(Cstr_ptr& file) :
00075 _width(0),
00076 _height(0),
00077 _bpp(1),
00078 _data(0),
00079 _no_delete(0)
00080 {
00081 if (file)
00082 load_file(file);
00083 }
00084 Image(cImage &img) { *this = img; }
00085 Image(uint w, uint h, uint bpp, uchar* data, bool nd=1) {
00086 set(w,h,bpp,data,nd);
00087 }
00088 Image(uint w, uint h, uint bpp) :
00089 _width(0), _height(0), _bpp(1), _data(0), _no_delete(0) {
00090 resize(w,h,bpp);
00091 }
00092
00093 void operator=(const Image &img) {
00094 if (img._file) {
00095 _data = 0; clear();
00096 load_file(img._file);
00097 } else {
00098 set(img._width, img._height, img._bpp, img.copy(), true);
00099 }
00100 }
00101
00102 void set(int w, int h, uint bpp, uchar* data, bool nd=1) {
00103 if (bpp < 1 || bpp > 4) {
00104 err_msg("Image::Image: %d bytes/pixel not supported", bpp);
00105 clear();
00106 } else {
00107 clear();
00108 _width = w;
00109 _height = h;
00110 _bpp = bpp;
00111 _data = data;
00112 _no_delete = nd;
00113 }
00114 }
00115
00116 void clear() {
00117 if (!_no_delete)
00118 delete [] _data;
00119 _width = _height = 0;
00120 _bpp = 1;
00121 _data = 0;
00122 _no_delete = 0;
00123 _file = str_ptr::null_str();
00124 }
00125
00126 int resize(uint w, uint h, uint b) {
00127
00128
00129
00130
00131
00132
00133 if (_width != w || _height != h || _bpp != b) {
00134 _width = w;
00135 _height = h;
00136 _bpp = b;
00137 if (!_no_delete)
00138 delete [] _data;
00139 _data = 0;
00140 _no_delete = 0;
00141 if ((_data = new uchar [ size() ]) == 0) {
00142 err_ret("Image::resize: can't allocate data");
00143 return 0;
00144 }
00145 }
00146 return 1;
00147 }
00148
00149 int copy_tile(const Image& tile, uint i, uint j);
00150
00151 virtual ~Image() { clear(); }
00152
00153
00154 int width() const { return _width; }
00155 int height() const { return _height; }
00156 uint bpp() const { return _bpp; }
00157 int size() const { return _width*_height*_bpp; }
00158 int row_size() const { return _width*_bpp; }
00159 point2i dims() const { return point2i(_width,_height); }
00160 uchar* data() const { return _data; }
00161 uchar* row(int k) const { return _data + k*row_size(); }
00162 Cstr_ptr &file() const { return _file;}
00163 uchar* copy() const;
00164
00165 bool empty() const { return !(_width && _height && _bpp && _data); }
00166
00167 void expand_power2();
00168 int resize_rows_mult_4();
00169 int load_file(Cstr_ptr &file);
00170
00171 int read_png(Cstr_ptr &file);
00172 int write_png(char* file) const;
00173
00174 int read_pnm (Cstr_ptr &file);
00175 int read_pnm (istream &in);
00176 int read_pgm (istream& in, bool ascii);
00177 int read_ppm (istream& in, bool ascii);
00178 int write_pnm(char* file, bool ascii=false) const;
00179 int write_pnm(ostream &os, bool ascii) const;
00180
00181 static int write_png(int w, int h, uint bpp, uchar* data, Cstr_ptr& file) {
00182 Image img(w,h,bpp,data,1);
00183 return img.write_png(**file);
00184 }
00185 friend ostream & operator<<(ostream &os, cImage &img);
00186 friend istream & operator>>(istream &is, Image &img);
00187 };
00188
00189
00190 #endif // IMAGE_H_HAS_BEEN_INCLUDED
00191