12 static int pixel_elements(Image::Format fmt);
13 static int elem_size(Image::Format fmt);
14 static int pixel_size(Image::Format fmt);
25 delete [] (char*)pixels;
28 int Image::get_width() const
33 int Image::get_height() const
38 Image::Format Image::get_format() const
43 bool Image::create(int x, int y, Format fmt)
50 pixels = new char[x * y * pixel_size(fmt)];
58 bool Image::set_pixels(int xsz, int ysz, void *pixels, Format fmt)
60 if(!create(xsz, ysz, fmt)) {
63 memcpy(this->pixels, pixels, xsz * ysz * pixel_size(fmt));
67 bool Image::set_pixels(int xsz, int ysz, void *pixels, int scan_width, Format fmt)
69 return set_pixels(xsz, ysz, pixels, 0, 0, scan_width, fmt);
72 bool Image::set_pixels(int xsz, int ysz, void *pixels, int x, int y, int scan_width, Format fmt)
78 if(!create(xsz, ysz, fmt)) {
82 int pixsz = pixel_size(fmt);
84 unsigned char *dest = (unsigned char*)this->pixels;
85 unsigned char *src = (unsigned char*)pixels + (y * scan_width + x) * pixsz;
86 for(int i=0; i<ysz; i++) {
87 memcpy(dest, src, xsz * pixsz);
89 src += scan_width * pixsz;
94 void *Image::get_pixels() const
99 void Image::flip_horizontal()
101 int pixsz = pixel_size(fmt);
103 unsigned char *tmppix = (unsigned char*)alloca(pixsz);
105 unsigned char *scan = (unsigned char*)pixels;
106 for(int i=0; i<height; i++) {
107 unsigned char *dest = scan;
108 unsigned char *src = scan + (width - 1) * pixsz;
111 memcpy(tmppix, src, pixsz);
112 memcpy(src, dest, pixsz);
113 memcpy(dest, tmppix, pixsz);
118 scan += width * pixsz;
122 void Image::flip_vertical()
124 int pixsz = pixel_size(fmt);
126 unsigned char *tmpscan = (unsigned char*)alloca(width * pixsz);
128 unsigned char *dest = (unsigned char*)pixels;
129 unsigned char *src = (unsigned char*)pixels + (height - 1) * width * pixsz;
132 memcpy(tmpscan, src, width * pixsz);
133 memcpy(src, dest, width * pixsz);
134 memcpy(dest, tmpscan, width * pixsz);
135 dest += width * pixsz;
136 src -= width * pixsz;
140 void Image::rotate_180()
146 bool Image::load(const char *fname)
148 struct img_pixmap pixmap;
151 if(img_load(&pixmap, fname) == -1) {
167 fmt = FMT_GREY_FLOAT;
173 fmt = FMT_RGBA_FLOAT;
176 img_destroy(&pixmap);
180 if(!set_pixels(pixmap.width, pixmap.height, pixmap.pixels, fmt)) {
181 img_destroy(&pixmap);
184 img_destroy(&pixmap);
188 bool Image::save(const char *fname) const
190 struct img_pixmap pixmap;
196 pixmap.fmt = IMG_FMT_GREY8;
199 pixmap.fmt = IMG_FMT_GREYF;
202 pixmap.fmt = IMG_FMT_RGB24;
205 pixmap.fmt = IMG_FMT_RGBF;
208 pixmap.fmt = IMG_FMT_RGBA32;
211 pixmap.fmt = IMG_FMT_RGBAF;
217 pixmap.width = width;
218 pixmap.height = height;
219 pixmap.pixels = pixels;
220 pixmap.pixelsz = pixel_size(fmt);
222 if(img_save(&pixmap, fname) == -1) {
228 static int pixel_elements(Image::Format fmt)
231 case Image::FMT_GREY:
232 case Image::FMT_GREY_FLOAT:
236 case Image::FMT_RGB_FLOAT:
239 case Image::FMT_RGBA:
240 case Image::FMT_RGBA_FLOAT:
249 static int elem_size(Image::Format fmt)
252 case Image::FMT_GREY:
254 case Image::FMT_RGBA:
257 case Image::FMT_GREY_FLOAT:
258 case Image::FMT_RGB_FLOAT:
259 case Image::FMT_RGBA_FLOAT:
260 return sizeof(float);
268 static int pixel_size(Image::Format fmt)
270 return elem_size(fmt) * pixel_elements(fmt);