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_;
184 PixelComponentsCnt pixcomp_;
185 PixelComponentsPrecision pixprec_;
186 //int VRAMtop_, VRAMleft_;
187 Rectangle area_, localarea_, targetarea_;
191 mutable float *bitmapf_;
192 mutable unsigned char *bitmapub_;
193 unsigned int pixBytes_, lineBytes_, bitmapBytes_, lineValue_, bitmapValue_;
194 static int lineBytesAlingnment_;
195 static bool setWithoutClear_; // make it local?
197 void (* pix_)(const Image &, const int, const int, const Color&);
198 void (* pix_direct_)(const Image &, const int, const int, const Color&);
199 void (* pix_alpha_)(const Image &, const int, const int, const double);
200 Color (* getpix_)(const Image &, const int, const int);
201 static void line_direct_copy_(const Image &src, Image &tgt, const int iy, const int jy=-1);
205 Image& operator =(const Image &source);
206 Image(const Image &readimage);
207 Image(const Image &readimage, bool dummy);
208 inline void initPointersNull();
209 void releasebitmap();
210 const void * getBitmapPtr() const;
214 Image(std::string filename);
215 Image(const char * filename);
216 Image(Rectangle rectangle, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
217 Image(Rectangle rectangle, PixelComponentsPrecision pixcompprec);
218 Image(long x, long y, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
219 Image(long x, long y, PixelComponentsPrecision pixcompprec);
220 void set(long x, long y, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
221 void set(long x, long y, PixelComponentsPrecision pixcompprec);
222 void set(Rectangle rectangle, PixelComponentsCnt pixcompcntval=RGB, PixelComponentsPrecision pixcompprec=BYTE);
223 void set(Rectangle rectangle, PixelComponentsPrecision pixcompprec);
224 static void setWithoutClear(bool y);
227 bool hasInstance() const;
229 Image duplicate() const;
230 Image& convert(PixelComponentsCnt pixcompcntval);
231 Image& convertColorCalibration(bool on_off);
233 //void pix_ub_bits_mono_(int ix, int iy, double lum);
234 Image& clear(const Color &col=Color::black);
235 inline Image& pix(const int x, const int y, const Color &col) { pix_(*this, x, y, col); return *this; }
236 inline Image& pix(const double x, const double y, const Color &col) { return pix(Math::round(x), Math::round(y), col); }
237 inline Image& pix(const Point &po, const Color &col) { return pix(Math::round(po.x), Math::round(po.y), col); }
238 inline Image& pix_raw(const int x, const int y, const Color &col) { pix_direct_(*this, x, y, col); return *this; }
239 inline Image& alpha(const int x, const int y, const double a) { pix_alpha_(*this, x, y, a); return *this; }
\r
240 Image& alpha(const double a);
\r
241 Image& alpha(const Matrix &a);
242 void line(const double x1, const double y1, const double x2, const double y2, const Color &col);
243 void line(const Point &po1, const Point &po2, const Color &col);
244 void rect(const Rectangle &rectnagle, const Color &col);
245 void ellipse(const Rectangle &rect, const Color &col);
247 void oval(const Rectangle &rect, const Color &col);
248 void fillRect(const Rectangle &rectangle, const Color &col);
249 void drawText(int x, int y, const char* str, int strlength);
250 // void msg(Letters &letters, int x, int y, const Color &col);
252 virtual Image& draw(double left, double top, Drawable &target = *Drawable::prime);
253 virtual Image& draw(Drawable &target = *Drawable::prime);
254 virtual Image& draw(double alpha, Drawable &target = *Drawable::prime);
256 void display(double left, double top);
258 virtual Image& cache(DrawableWithCache &target = *DrawableWithCache::prime);
259 virtual Image& uncache(DrawableWithCache &target = *DrawableWithCache::prime);
260 inline Image& quicken(bool on_off=true) { if(on_off) return cache(); else return uncache(); }
261 inline Color getPix(int x, int y) const { return getpix_(*this, x, y); }
263 class PartialView : public Figure
266 Rectangle source, target;
269 PartialView(Image *src, const Rectangle &d_source);
270 PartialView& set(double width, double height);
271 virtual ~PartialView();
272 virtual const Point getDatum() const;
273 virtual PartialView& setDatum(const Point &p);
274 virtual PartialView& centering(const Point &p);
275 virtual PartialView& draw(Drawable &drawable = *Drawable::prime);
277 PartialView operator ()(const Rectangle &scope);
280 virtual const Point getDatum() const;
281 virtual Image& setDatum(const Point& p);
282 virtual Image& centering(const Drawable& target = *Drawable::prime);
283 virtual Image& centering(const Figure& fig);
284 virtual Image& centering(const Point& p);
285 virtual Image& centering(const double x, const double y, const double z = 0);
286 virtual Image& move_to(const double x, const double y, const double z = 0);
287 virtual Image& shift(const double x, const double y, const double z = 0);
288 virtual int getWidth() const;
289 virtual int getHeight() const;
290 virtual double getHcenter() const;
291 virtual double getVcenter() const;
292 virtual const Point getCenter() const;
293 double getLeft() const;
294 double getRight() const;
295 double getTop() const;
296 double getBottom() const;
299 float* getFloatPrt();
300 unsigned char* getElementPtr();
301 int getPixBytes(); // Number of bytes of a pixel
302 int getLineBytes(); // Number of bytes of a line
303 int getBitmapBytes(); // Number of bytes of the whole bitmap
304 int getLineNumber(); // Array length of line
305 int getElementNumber(); // Array length of the whole bitmap
\r
306 const PixelComponentsCnt getComponentKind() const; // GRAY, RGB, RGBA
\r
307 const PixelComponentsPrecision getPrecisionKind() const; // BYTE, FLOAT
309 void to(const Rectangle &source__, Image &other, const Rectangle target__) const;
310 void to(const Rectangle &source, Image &other) const;
312 void load(std::string filename);
313 void load(const char * filename);
314 void save(std::string filename);
315 void save(const char * filename);
316 void from(const Image &img);
317 void from(const Matrix &gray);
318 void from(const Matrix &r, const Matrix &g, const Matrix &b);
319 void from(const Matrix &r, const Matrix &g, const Matrix &b, const Matrix &a);
320 void to(Matrix &gray) const;
321 void to(Point p, int width, int height, Matrix &gray) const;
322 void to(Point p, const Rectangle &rect, Matrix &gray) const;
323 void to(const Interval &horiz, const Interval &vert, Matrix &gray) const;
324 void to(Matrix &r, Matrix &g, Matrix &b) const;
325 void to(Point p, int width, int height, Matrix &r, Matrix &g, Matrix &b) const;
326 void to(Point p, const Rectangle &rect, Matrix &r, Matrix &g, Matrix &b) const;
327 void to(const Interval &horiz, const Interval &vert, Matrix &r, Matrix &g, Matrix &b) const;
328 void to(Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
329 void to(Point p, int width, int height, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
330 void to(Point p, const Rectangle &rect, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
331 void to(const Interval &horiz, const Interval &vert, Matrix &r, Matrix &g, Matrix &b, Matrix &a) const;
\r
334 void to(cv::Mat &target) const;
335 void from(cv::Mat &target);
337 Image & operator -=(Image &rhs);
342 } /* <- namespace Psycholops */