OSDN Git Service

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