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 void Image::resize_half()
148 int pixsz = pixel_size(fmt);
149 int newxsz = width / 2;
150 int newysz = height / 2;
152 if(!newxsz || !newysz) return;
154 unsigned char *newpix = new unsigned char[newxsz * newysz * pixsz];
156 unsigned char *sptr = (unsigned char*)pixels;
157 unsigned char *dptr = newpix;
159 for(int i=0; i<newysz; i++) {
160 if(i & 1) sptr += width * pixsz;
161 for(int j=0; j<newxsz; j++) {
162 memcpy(dptr, sptr, pixsz);
168 delete [] (char*)pixels;
174 bool Image::load(const char *fname)
176 struct img_pixmap pixmap;
179 if(img_load(&pixmap, fname) == -1) {
195 fmt = FMT_GREY_FLOAT;
201 fmt = FMT_RGBA_FLOAT;
204 img_destroy(&pixmap);
208 if(!set_pixels(pixmap.width, pixmap.height, pixmap.pixels, fmt)) {
209 img_destroy(&pixmap);
212 img_destroy(&pixmap);
216 bool Image::save(const char *fname) const
218 struct img_pixmap pixmap;
224 pixmap.fmt = IMG_FMT_GREY8;
227 pixmap.fmt = IMG_FMT_GREYF;
230 pixmap.fmt = IMG_FMT_RGB24;
233 pixmap.fmt = IMG_FMT_RGBF;
236 pixmap.fmt = IMG_FMT_RGBA32;
239 pixmap.fmt = IMG_FMT_RGBAF;
245 pixmap.width = width;
246 pixmap.height = height;
247 pixmap.pixels = pixels;
248 pixmap.pixelsz = pixel_size(fmt);
250 if(img_save(&pixmap, fname) == -1) {
256 static int pixel_elements(Image::Format fmt)
259 case Image::FMT_GREY:
260 case Image::FMT_GREY_FLOAT:
264 case Image::FMT_RGB_FLOAT:
267 case Image::FMT_RGBA:
268 case Image::FMT_RGBA_FLOAT:
277 static int elem_size(Image::Format fmt)
280 case Image::FMT_GREY:
282 case Image::FMT_RGBA:
285 case Image::FMT_GREY_FLOAT:
286 case Image::FMT_RGB_FLOAT:
287 case Image::FMT_RGBA_FLOAT:
288 return sizeof(float);
296 static int pixel_size(Image::Format fmt)
298 return elem_size(fmt) * pixel_elements(fmt);