3 * Psychlops Standard Library (Universal)
5 * Last Modified 2006/01/04 by Kenchi HOSOKAWA
6 * (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
9 #ifndef HEADER_PSYCHLOPS_GRAPHIC_IMAGE
10 #define HEADER_PSYCHLOPS_GRAPHIC_IMAGE
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"
23 namespace cv{ class Mat; }
\r
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
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_;
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;
44 void readTargetMemoryAlignment(Image * target);
50 class APIImageProperties;
51 class APIFontProperties;
52 class ImageManipulator;
61 friend class ShaderAPI;
62 friend class Devices::CanvasBits;
67 ImageCache_(APIImageCache* id__, bool dirty__);
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;
79 friend class Recangle;
80 friend class Devices::CanvasBits;
83 enum PixelComponentsCnt {
86 enum PixelComponentsPrecision {
89 static const int PixCompSize_[3];
90 static const int PixPrecSize_[2];
94 APIImageProperties *api_;
95 typedef std::map<DrawableWithCache*, ImageCache_> CacheID;
96 mutable CacheID caches;
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;
106 inline void pix_direct(int x, int iy, double l)
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;
116 float *bitmapf = bitmapf_ + offset;
118 if(pixcomp_==Image::GRAY) return; else *(++bitmapf) = l;
120 if(pixcomp_==Image::RGB) return; else *(++bitmapf) = 1;
123 inline void pix_direct(int x, int iy, int l)
125 int offset = (height_-iy-1)*lineValue_ + x*PixCompSize_[pixcomp_];
126 if(pixprec_ == Image::BYTE) {
127 unsigned char *bitmap = bitmapub_ + offset;
129 if(pixcomp_==Image::GRAY) return; else *(++bitmap) = l;
131 if(pixcomp_==Image::RGB) return; else *(++bitmap) = 255;
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;
140 inline void pix_direct(int x, int iy, double r, double g, double b, double a)
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;
150 float *bitmapf = bitmapf_ + offset;
152 if(pixcomp_==Image::GRAY) return; else *(++bitmapf) = g;
154 if(pixcomp_==Image::RGB) return; else *(++bitmapf) = a;
157 inline void pix_direct(int x, int iy, const Color &c)
159 pix_direct(x,iy, c.Red, c.Green, c.Blue, c.Alpha);
161 inline void pix_direct(int x, int iy, int r, int g, int b, int a)
163 int offset = (height_-iy-1)*lineValue_ + x*PixCompSize_[pixcomp_];
164 if(pixprec_ == Image::BYTE) {
165 unsigned char *bitmap = bitmapub_ + offset;
167 if(pixcomp_==Image::GRAY) return; else *(++bitmap) = g;
169 if(pixcomp_==Image::RGB) return; else *(++bitmap) = a;
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;
180 double _zoom_percentage_;
\r
181 Image& _zoom_(const double percentage);
185 PixelComponentsCnt pixcomp_;
186 PixelComponentsPrecision pixprec_;
187 //int VRAMtop_, VRAMleft_;
188 Rectangle area_, localarea_, targetarea_;
192 mutable float *bitmapf_;
193 mutable unsigned char *bitmapub_;
194 unsigned int pixBytes_, lineBytes_, bitmapBytes_, lineValue_, bitmapValue_;
195 static int lineBytesAlingnment_;
196 static bool setWithoutClear_; // make it local?
198 void (* pix_)(const Image &, const int, const int, const Color&);
199 void (* pix_direct_)(const Image &, const int, const int, const Color&);
200 void (* pix_alpha_)(const Image &, const int, const int, const double);
201 Color (* getpix_)(const Image &, const int, const int);
202 static void line_direct_copy_(const Image &src, Image &tgt, const int iy, const int jy=-1);
206 Image& operator =(const Image &source);
207 Image(const Image &readimage);
208 Image(const Image &readimage, bool dummy);
209 inline void initPointersNull();
210 void releasebitmap();
211 const void * getBitmapPtr() const;
215 Image(std::string filename);
216 Image(const char * filename);
217 Image(Rectangle rectangle, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
218 Image(Rectangle rectangle, PixelComponentsPrecision pixcompprec);
219 Image(long x, long y, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
220 Image(long x, long y, PixelComponentsPrecision pixcompprec);
221 void set(long x, long y, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
222 void set(long x, long y, PixelComponentsPrecision pixcompprec);
223 void set(Rectangle rectangle, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
224 void set(Rectangle rectangle, PixelComponentsPrecision pixcompprec);
225 static void setWithoutClear(bool y);
228 bool hasInstance() const;
230 Image duplicate() const;
231 Image& convert(PixelComponentsCnt pixcompcntval);
232 Image& convertColorCalibration(bool on_off);
234 //void pix_ub_bits_mono_(int ix, int iy, double lum);
235 Image& clear(const Color &col=Color::black);
236 inline Image& pix(const int x, const int y, const Color &col) { pix_(*this, x, y, col); return *this; }
237 inline Image& pix(const double x, const double y, const Color &col) { return pix(Math::round(x), Math::round(y), col); }
238 inline Image& pix(const Point &po, const Color &col) { return pix(Math::round(po.x), Math::round(po.y), col); }
239 inline Image& pix_raw(const int x, const int y, const Color &col) { pix_direct_(*this, x, y, col); return *this; }
240 inline Image& alpha(const int x, const int y, const double a) { pix_alpha_(*this, x, y, a); return *this; }
\r
241 Image& alpha(const double a);
\r
242 Image& alpha(const Matrix &a);
243 void line(const double x1, const double y1, const double x2, const double y2, const Color &col);
244 void line(const Point &po1, const Point &po2, const Color &col);
245 void rect(const Rectangle &rectnagle, const Color &col);
246 void ellipse(const Rectangle &rect, const Color &col);
248 void oval(const Rectangle &rect, const Color &col);
249 void fillRect(const Rectangle &rectangle, const Color &col);
250 void drawText(int x, int y, const char* str, int strlength);
251 // void msg(Letters &letters, int x, int y, const Color &col);
253 virtual Image& draw(double left, double top, Drawable &target = *Drawable::prime);
254 virtual Image& draw(Drawable &target = *Drawable::prime);
255 virtual Image& draw(double alpha, Drawable &target = *Drawable::prime);
257 void display(double left, double top);
259 virtual Image& cache(DrawableWithCache &target = *DrawableWithCache::prime);
260 virtual Image& uncache(DrawableWithCache &target = *DrawableWithCache::prime);
261 inline Image& quicken(bool on_off=true) { if(on_off) return cache(); else return uncache(); }
262 inline Color getPix(int x, int y) const { return getpix_(*this, x, y); }
264 class PartialView : public Figure
267 Rectangle source, target;
270 PartialView(Image *src, const Rectangle &d_source);
271 PartialView& set(double width, double height);
272 virtual ~PartialView();
273 virtual const Point getDatum() const;
274 virtual PartialView& setDatum(const Point &p);
275 virtual PartialView& centering(const Point &p);
276 virtual PartialView& draw(Drawable &drawable = *Drawable::prime);
278 PartialView operator ()(const Rectangle &scope);
281 virtual const Point getDatum() const;
282 virtual Image& setDatum(const Point& p);
283 virtual Image& centering(const Drawable& target = *Drawable::prime);
284 virtual Image& centering(const Figure& fig);
285 virtual Image& centering(const Point& p);
286 virtual Image& centering(const double x, const double y, const double z = 0);
287 virtual Image& move_to(const double x, const double y, const double z = 0);
288 virtual Image& shift(const double x, const double y, const double z = 0);
289 virtual int getWidth() const;
290 virtual int getHeight() const;
291 virtual double getHcenter() const;
292 virtual double getVcenter() const;
293 virtual const Point getCenter() const;
294 double getLeft() const;
295 double getRight() const;
296 double getTop() const;
297 double getBottom() const;
300 float* getFloatPrt();
301 unsigned char* getElementPtr();
302 int getPixBytes(); // Number of bytes of a pixel
303 int getLineBytes(); // Number of bytes of a line
304 int getBitmapBytes(); // Number of bytes of the whole bitmap
305 int getLineNumber(); // Array length of line
306 int getElementNumber(); // Array length of the whole bitmap
\r
307 const PixelComponentsCnt getComponentKind() const; // GRAY, RGB, RGBA
\r
308 const PixelComponentsPrecision getPrecisionKind() const; // BYTE, FLOAT
310 void to(const Rectangle &source__, Image &other, const Rectangle target__) const;
311 void to(const Rectangle &source, Image &other) const;
313 void load(std::string filename);
314 void load(const char * filename);
315 void save(std::string filename);
316 void save(const char * filename);
317 void from(const Image &img);
318 void from(const Matrix &gray);
319 void from(const Matrix &r, const Matrix &g, const Matrix &b);
320 void from(const Matrix &r, const Matrix &g, const Matrix &b, const Matrix &a);
321 void to(Matrix &gray) const;
322 void to(Point p, int width, int height, Matrix &gray) const;
323 void to(Point p, const Rectangle &rect, Matrix &gray) const;
324 void to(const Interval &horiz, const Interval &vert, Matrix &gray) const;
325 void to(Matrix &r, Matrix &g, Matrix &b) const;
326 void to(Point p, int width, int height, Matrix &r, Matrix &g, Matrix &b) const;
327 void to(Point p, const Rectangle &rect, Matrix &r, Matrix &g, Matrix &b) const;
328 void to(const Interval &horiz, const Interval &vert, Matrix &r, Matrix &g, Matrix &b) const;
329 void to(Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
330 void to(Point p, int width, int height, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
331 void to(Point p, const Rectangle &rect, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
332 void to(const Interval &horiz, const Interval &vert, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
\r
335 void to(cv::Mat &target) const;
336 void from(cv::Mat &target);
338 Image & operator -=(Image &rhs);
343 } /* <- namespace Psycholops */