OSDN Git Service

parallel
[psychlops/cpp.git] / psychlops / core / graphic / psychlops_g_image.h
1 /*
2  *  psychlops_g_image.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2006/01/04 by Kenchi HOSOKAWA
6  *  (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
7  */
8
9 #ifndef HEADER_PSYCHLOPS_GRAPHIC_IMAGE
10 #define HEADER_PSYCHLOPS_GRAPHIC_IMAGE
11
12 #include <string>
13
14
15
16 #include "../math/psychlops_math.h"
17 #include "psychlops_g_fundamental.h"
18 #include "psychlops_g_color.h"
19 #include "psychlops_g_shape.h"
20 #include "psychlops_g_canvas.h"
21 #include "psychlops_g_font.h"
22 \r
23 namespace cv{ class Mat; }\r
24
25 namespace Psychlops {
26
27 namespace IMAGE_FORMATS {\r
28         enum FILE_EXT { UNKNOWN, PNG, JPG, JP2, TIFF, CVMAT_TXT, MATLAB_MAT };\r
29         static FILE_EXT getImageFileFormatFromExt(const std::string &s);\r
30
31         class IMAGE_FORMAT {
32                 protected:
33                 float * target_bitmap_f_;
34                 unsigned char * target_bitmap_ub_;
35                 unsigned int target_bytes_per_line_;
36                 unsigned int pix_components_;
37                 unsigned int pix_precision_;
38
39                 public:
40                 virtual ~IMAGE_FORMAT();
41                 virtual void load(const char *file_name, Image * target) = 0;
42                 virtual void save(const char *file_name, Image * target) = 0;
43                 protected:
44                 void readTargetMemoryAlignment(Image * target);
45         };
46 }
47
48
49         class APIImageCache;
50         class APIImageProperties;
51         class APIFontProperties;
52         class ImageManipulator;
53         class ShaderAPI;
54         namespace Devices
55         {
56                 class CanvasBits;
57         }
58
59         class ImageCache_ {
60                 friend class Canvas;
61                 friend class ShaderAPI;
62                 friend class Devices::CanvasBits;
63                 APIImageCache* id;
64                 bool dirty;
65         public:
66                 ImageCache_();
67                 ImageCache_(APIImageCache* id__, bool dirty__);
68         };
69
70 //      class Image : public Drawable {
71         class Image : virtual public Figure {
72                 friend class APICanvasProperties;
73                 friend class APIImageProperties;
74                 friend class APIFontProperties;
75                 friend class ImageManipulator;
76                 friend class IMAGE_FORMATS::IMAGE_FORMAT;
77                 friend class ShaderAPI;
78                 friend class Canvas;
79                 friend class Recangle;
80                 friend class Devices::CanvasBits;
81
82                 public:
83                 enum PixelComponentsCnt {
84                         GRAY=0, RGB=1, RGBA=2
85                 };
86                 enum PixelComponentsPrecision {
87                         BYTE=0, FLOAT=1
88                 };
89                 static const int PixCompSize_[3];
90                 static const int PixPrecSize_[2];
91
92
93                 protected:
94                 APIImageProperties *api_;
95                 typedef std::map<DrawableWithCache*, ImageCache_> CacheID;
96                 mutable CacheID caches;
97
98
99                 inline static unsigned char round8bit(double value) {
100                         double val = value*255.0+0.5, integer_part, frac;
101                         frac = modf(val, &integer_part);
102                         if(frac!=0.0) return (unsigned char)integer_part;
103                         if((int)(integer_part)%2==0) return (unsigned char)integer_part; else return (unsigned char)integer_part-1;
104                 }
105                 public:
106                 inline void pix_direct(int x, int iy, double l)
107                 {
108                         int offset = (height_-iy-1)*lineValue_ + x*PixCompSize_[pixcomp_];
109                         if(pixprec_ == Image::BYTE) {
110                                 unsigned char *bitmap = bitmapub_ + offset;
111                                 *(bitmap) = round8bit(l);
112                                 if(pixcomp_==Image::GRAY) return; else *(++bitmap) = round8bit(l);
113                                 *(++bitmap) = round8bit(l);
114                                 if(pixcomp_==Image::RGB) return; else *(++bitmap) = 255;
115                         } else {
116                                 float *bitmapf = bitmapf_ + offset;
117                                 *(bitmapf) = l;
118                                 if(pixcomp_==Image::GRAY) return; else *(++bitmapf) = l;
119                                 *(++bitmapf) = l;
120                                 if(pixcomp_==Image::RGB) return; else *(++bitmapf) = 1;
121                         }
122                 }
123                 inline void pix_direct(int x, int iy, int l)
124                 {
125                         int offset = (height_-iy-1)*lineValue_ + x*PixCompSize_[pixcomp_];
126                         if(pixprec_ == Image::BYTE) {
127                                 unsigned char *bitmap = bitmapub_ + offset;
128                                 *(bitmap) = l;
129                                 if(pixcomp_==Image::GRAY) return; else *(++bitmap) = l;
130                                 *(++bitmap) = l;
131                                 if(pixcomp_==Image::RGB) return; else *(++bitmap) = 255;
132                         } else {
133                                 float *bitmapf = bitmapf_ + offset;
134                                 *(bitmapf) = l/255.0;
135                                 if(pixcomp_==Image::GRAY) return; else *(++bitmapf) = l/255.0;
136                                 *(++bitmapf) = l/255.0;
137                                 if(pixcomp_==Image::RGB) return; else *(++bitmapf) = 1.0;
138                         }
139                 }
140                 inline void pix_direct(int x, int iy, double r, double g, double b, double a)
141                 {
142                         int offset = (height_-iy-1)*lineValue_ + x*PixCompSize_[pixcomp_];
143                         if(pixprec_ == Image::BYTE) {
144                                 unsigned char *bitmap = bitmapub_ + offset;
145                                 *(bitmap) = round8bit(r);
146                                 if(pixcomp_==Image::GRAY) return; else *(++bitmap) = round8bit(g);
147                                 *(++bitmap) = round8bit(b);
148                                 if(pixcomp_==Image::RGB) return; else *(++bitmap) = a;
149                         } else {
150                                 float *bitmapf = bitmapf_ + offset;
151                                 *(bitmapf) = r;
152                                 if(pixcomp_==Image::GRAY) return; else *(++bitmapf) = g;
153                                 *(++bitmapf) = b;
154                                 if(pixcomp_==Image::RGB) return; else *(++bitmapf) = a;
155                         }
156                 }
157                 inline void pix_direct(int x, int iy, const Color &c)
158                 {
159                         pix_direct(x,iy, c.Red, c.Green, c.Blue, c.Alpha);
160                 }
161                 inline void pix_direct(int x, int iy, int r, int g, int b, int a)
162                 {
163                         int offset = (height_-iy-1)*lineValue_ + x*PixCompSize_[pixcomp_];
164                         if(pixprec_ == Image::BYTE) {
165                                 unsigned char *bitmap = bitmapub_ + offset;
166                                 *(bitmap) = r;
167                                 if(pixcomp_==Image::GRAY) return; else *(++bitmap) = g;
168                                 *(++bitmap) = b;
169                                 if(pixcomp_==Image::RGB) return; else *(++bitmap) = a;
170                         } else {
171                                 float *bitmapf = bitmapf_ + offset;
172                                 *(bitmapf) = r/255.0;
173                                 if(pixcomp_==Image::GRAY) return; else *(++bitmapf) = g/255.0;
174                                 *(++bitmapf) = b/255.0;
175                                 if(pixcomp_==Image::RGB) return; else *(++bitmapf) = a;
176                         }
177                 }
178
179                 protected:
180                 int width_, height_;
181                 PixelComponentsCnt pixcomp_;
182                 PixelComponentsPrecision pixprec_;
183                 //int VRAMtop_, VRAMleft_;
184                 Rectangle area_, localarea_, targetarea_;
185
186                 //bool quickened_;
187                 bool have_instance_;
188                 mutable float *bitmapf_;
189                 mutable unsigned char *bitmapub_;
190                 unsigned int pixBytes_, lineBytes_, bitmapBytes_, lineValue_, bitmapValue_;
191                 static int lineBytesAlingnment_;
192                 static bool setWithoutClear_; // make it local?
193
194                 void (* pix_)(const Image &, const int, const int, const Color&);
195                 void (* pix_direct_)(const Image &, const int, const int, const Color&);
196                 void (* pix_alpha_)(const Image &, const int, const int, const double);
197                 Color (* getpix_)(const Image &, const int, const int);
198                 static void line_direct_copy_(const Image &src, Image &tgt, const int iy, const int jy=-1);
199
200
201                 private:
202                 Image& operator =(const Image &source);
203                 Image(const Image &readimage);
204                 Image(const Image &readimage, bool dummy);
205                 inline void initPointersNull();
206                 void releasebitmap();
207                 const void * getBitmapPtr() const;
208
209                 public:
210                 Image();
211                 Image(std::string filename);
212                 Image(const char * filename);
213                 Image(Rectangle rectangle, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
214                 Image(Rectangle rectangle, PixelComponentsPrecision pixcompprec);
215                 Image(long x, long y, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
216                 Image(long x, long y, PixelComponentsPrecision pixcompprec);
217                 void set(long x, long y, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
218                 void set(long x, long y, PixelComponentsPrecision pixcompprec);
219                 void set(Rectangle rectangle, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
220                 void set(Rectangle rectangle, PixelComponentsPrecision pixcompprec);
221                 static void setWithoutClear(bool y);
222                 ~Image(void);
223                 void release();
224                 bool hasInstance() const;
225                 Image dup() const;
226                 Image duplicate() const;
227                 Image& convert(PixelComponentsCnt pixcompcntval);
228                 Image& convertColorCalibration(bool on_off);
229
230 //void pix_ub_bits_mono_(int ix, int iy, double lum);
231                 Image& clear(const Color &col=Color::black);
232                 inline Image& pix(const int x, const int y, const Color &col) { pix_(*this, x, y, col); return *this; }
233                 inline Image& pix(const double x, const double y, const Color &col) { return pix(Math::round(x), Math::round(y), col); }
234                 inline Image& pix(const Point &po, const Color &col) { return pix(Math::round(po.x), Math::round(po.y), col); }
235                 inline Image& pix_raw(const int x, const int y, const Color &col) { pix_direct_(*this, x, y, col); return *this; }
236                 inline Image& alpha(const int x, const int y, const double a) { pix_alpha_(*this, x, y, a); return *this; }\r
237                 Image& alpha(const double a);\r
238                 Image& alpha(const Matrix &a);
239                 void line(const double x1, const double y1, const double x2, const double y2, const Color &col);
240                 void line(const Point &po1, const Point &po2, const Color &col);
241                 void rect(const Rectangle &rectnagle, const Color &col);
242                 void ellipse(const Rectangle &rect, const Color &col);
243                         // aliases
244                         void oval(const Rectangle &rect, const Color &col);
245                         void fillRect(const Rectangle &rectangle, const Color &col);
246                         void drawText(int x, int y, const char* str, int strlength);
247 //                              void msg(Letters &letters, int x, int y, const Color &col);
248
249                 virtual Image& draw(double left, double top, Drawable &target = *Drawable::prime);
250                 virtual Image& draw(Drawable &target = *Drawable::prime);
251                 virtual Image& draw(double alpha, Drawable &target = *Drawable::prime);
252                         //obsolete
253                         void display(double left, double top);
254                         void display();
255                 virtual Image& cache(DrawableWithCache &target = *DrawableWithCache::prime);
256                 virtual Image& uncache(DrawableWithCache &target = *DrawableWithCache::prime);
257                 inline Image& quicken(bool on_off=true) { if(on_off) return cache(); else return uncache(); }
258                 inline Color getPix(int x, int y) const { return getpix_(*this, x, y); }
259
260                 class PartialView : public Figure
261                 {
262                         Image *img;
263                         Rectangle source, target;
264
265                 public:
266                         PartialView(Image *src, const Rectangle &d_source);
267                         PartialView& set(double width, double height);
268                         virtual ~PartialView();
269                         virtual const Point getDatum() const;
270                         virtual PartialView& setDatum(const Point &p);
271                         virtual PartialView& centering(const Point &p);
272                         virtual PartialView& draw(Drawable &drawable = *Drawable::prime);
273                 };
274                 PartialView operator ()(const Rectangle &scope);
275
276
277                 virtual const Point getDatum() const;
278                 virtual Image& setDatum(const Point& p);
279                 virtual Image& centering(const Drawable& target = *Drawable::prime);
280                 virtual Image& centering(const Figure& fig);
281                 virtual Image& centering(const Point& p);
282                 virtual Image& centering(const double x, const double y, const double z = 0);
283                 virtual Image& move_to(const double x, const double y, const double z = 0);
284                 virtual Image& shift(const double x, const double y, const double z = 0);
285                 virtual int getWidth() const;
286                 virtual int getHeight() const;
287                 virtual double getHcenter() const;
288                 virtual double getVcenter() const;
289                 virtual const Point getCenter() const;
290                 double getLeft() const;
291                 double getRight() const;
292                 double getTop() const;
293                 double getBottom() const;
294
295
296                 float* getFloatPrt();
297                 unsigned char* getElementPtr();
298                 int getPixBytes();      // Number of bytes of a pixel
299                 int getLineBytes();     // Number of bytes of a line
300                 int getBitmapBytes();   // Number of bytes of the whole bitmap
301                 int getLineNumber();    // Array length of line
302                 int getElementNumber(); // Array length of the whole bitmap\r
303                 const PixelComponentsCnt getComponentKind() const;      // GRAY, RGB, RGBA\r
304                 const PixelComponentsPrecision getPrecisionKind() const;        // BYTE, FLOAT
305
306                 void to(const Rectangle &source__, Image &other, const Rectangle target__) const;
307                 void to(const Rectangle &source, Image &other) const;
308
309                 void load(std::string filename);
310                 void load(const char * filename);
311                 void save(std::string filename);
312                 void save(const char * filename);
313                 void from(const Image &img);
314                 void from(const Matrix &gray);
315                 void from(const Matrix &r, const Matrix &g, const Matrix &b);
316                 void from(const Matrix &r, const Matrix &g, const Matrix &b, const Matrix &a);
317                 void to(Matrix &gray) const;
318                 void to(Point p, int width, int height, Matrix &gray) const;
319                 void to(Point p, const Rectangle &rect, Matrix &gray) const;
320                 void to(const Interval &horiz, const Interval &vert, Matrix &gray) const;
321                 void to(Matrix &r, Matrix &g, Matrix &b) const;
322                 void to(Point p, int width, int height, Matrix &r, Matrix &g, Matrix &b) const;
323                 void to(Point p, const Rectangle &rect, Matrix &r, Matrix &g, Matrix &b) const;
324                 void to(const Interval &horiz, const Interval &vert, Matrix &r, Matrix &g, Matrix &b) const;
325                 void to(Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
326                 void to(Point p, int width, int height, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
327                 void to(Point p, const Rectangle &rect, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
328                 void to(const Interval &horiz, const Interval &vert, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;\r
329 \r
330
331                 void to(cv::Mat &target) const;
332                 void from(cv::Mat &target);
333                 // test
334                         Image & operator -=(Image &rhs);
335         };
336
337
338
339 }       /*      <- namespace Psycholops         */
340
341
342 #endif