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 {
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_;
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;
41 void readTargetMemoryAlignment(Image * target);
47 class APIImageProperties;
48 class APIFontProperties;
49 class ImageManipulator;
58 friend class ShaderAPI;
59 friend class Devices::CanvasBits;
64 ImageCache_(APIImageCache* id__, bool dirty__);
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;
76 friend class Recangle;
77 friend class Devices::CanvasBits;
80 enum PixelComponentsCnt {
83 enum PixelComponentsPrecision {
86 static const int PixCompSize_[3];
87 static const int PixPrecSize_[2];
91 APIImageProperties *api_;
92 typedef std::map<DrawableWithCache*, ImageCache_> CacheID;
93 mutable CacheID caches;
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;
103 inline void pix_direct(int x, int iy, double l)
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;
113 float *bitmapf = bitmapf_ + offset;
115 if(pixcomp_==Image::GRAY) return; else *(++bitmapf) = l;
117 if(pixcomp_==Image::RGB) return; else *(++bitmapf) = 1;
120 inline void pix_direct(int x, int iy, int l)
122 int offset = (height_-iy-1)*lineValue_ + x*PixCompSize_[pixcomp_];
123 if(pixprec_ == Image::BYTE) {
124 unsigned char *bitmap = bitmapub_ + offset;
126 if(pixcomp_==Image::GRAY) return; else *(++bitmap) = l;
128 if(pixcomp_==Image::RGB) return; else *(++bitmap) = 255;
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;
137 inline void pix_direct(int x, int iy, double r, double g, double b, double a)
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;
147 float *bitmapf = bitmapf_ + offset;
149 if(pixcomp_==Image::GRAY) return; else *(++bitmapf) = g;
151 if(pixcomp_==Image::RGB) return; else *(++bitmapf) = a;
154 inline void pix_direct(int x, int iy, const Color &c)
156 pix_direct(x,iy, c.Red, c.Green, c.Blue, c.Alpha);
158 inline void pix_direct(int x, int iy, int r, int g, int b, int a)
160 int offset = (height_-iy-1)*lineValue_ + x*PixCompSize_[pixcomp_];
161 if(pixprec_ == Image::BYTE) {
162 unsigned char *bitmap = bitmapub_ + offset;
164 if(pixcomp_==Image::GRAY) return; else *(++bitmap) = g;
166 if(pixcomp_==Image::RGB) return; else *(++bitmap) = a;
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;
178 PixelComponentsCnt pixcomp_;
179 PixelComponentsPrecision pixprec_;
180 //int VRAMtop_, VRAMleft_;
181 Rectangle area_, localarea_, targetarea_;
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?
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);
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;
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);
221 bool hasInstance() const;
223 Image duplicate() const;
224 Image& convert(PixelComponentsCnt pixcompcntval);
225 Image& convertColorCalibration(bool on_off);
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);
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);
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);
250 void display(double left, double top);
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); }
257 class PartialView : public Figure
260 Rectangle source, target;
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);
271 PartialView operator ()(const Rectangle &scope);
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;
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
303 void to(const Rectangle &source__, Image &other, const Rectangle target__) const;
304 void to(const Rectangle &source, Image &other) const;
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
328 void to(cv::Mat &target) const;
329 void from(cv::Mat &target);
331 Image & operator -=(Image &rhs);
336 } /* <- namespace Psycholops */