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;
181 PixelComponentsCnt pixcomp_;
182 PixelComponentsPrecision pixprec_;
183 //int VRAMtop_, VRAMleft_;
184 Rectangle area_, localarea_, targetarea_;
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?
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);
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;
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);
224 bool hasInstance() const;
226 Image duplicate() const;
227 Image& convert(PixelComponentsCnt pixcompcntval);
228 Image& convertColorCalibration(bool on_off);
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);
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);
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);
253 void display(double left, double top);
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); }
260 class PartialView : public Figure
263 Rectangle source, target;
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);
274 PartialView operator ()(const Rectangle &scope);
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;
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
306 void to(const Rectangle &source__, Image &other, const Rectangle target__) const;
307 void to(const Rectangle &source, Image &other) const;
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
331 void to(cv::Mat &target) const;
332 void from(cv::Mat &target);
334 Image & operator -=(Image &rhs);
339 } /* <- namespace Psycholops */