vrshoot

view src/image.cc @ 3:c179c72369be

rename candy->vr
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Feb 2014 08:52:13 +0200
parents
children
line source
1 #include <string.h>
3 #ifndef _MSC_VER
4 #include <alloca.h>
5 #else
6 #include <malloc.h>
7 #endif
9 #include "imago2.h"
10 #include "image.h"
11 #include "logger.h"
13 static int pixel_elements(Image::Format fmt);
14 static int elem_size(Image::Format fmt);
15 static int pixel_size(Image::Format fmt);
17 Image::Image()
18 {
19 fmt = FMT_RGBA;
20 width = height = 0;
21 pixels = 0;
22 }
24 Image::~Image()
25 {
26 delete [] (char*)pixels;
27 }
29 int Image::get_width() const
30 {
31 return width;
32 }
34 int Image::get_height() const
35 {
36 return height;
37 }
39 Image::Format Image::get_format() const
40 {
41 return fmt;
42 }
44 bool Image::create(int x, int y, Format fmt)
45 {
46 width = x;
47 height = y;
48 this->fmt = fmt;
50 try {
51 pixels = new char[x * y * pixel_size(fmt)];
52 }
53 catch(...) {
54 return false;
55 }
56 return true;
57 }
59 bool Image::set_pixels(int xsz, int ysz, void *pixels, Format fmt)
60 {
61 if(!create(xsz, ysz, fmt)) {
62 return false;
63 }
64 memcpy(this->pixels, pixels, xsz * ysz * pixel_size(fmt));
65 return true;
66 }
68 bool Image::set_pixels(int xsz, int ysz, void *pixels, int scan_width, Format fmt)
69 {
70 return set_pixels(xsz, ysz, pixels, 0, 0, scan_width, fmt);
71 }
73 bool Image::set_pixels(int xsz, int ysz, void *pixels, int x, int y, int scan_width, Format fmt)
74 {
75 if(scan_width <= 0) {
76 scan_width = xsz;
77 }
79 if(!create(xsz, ysz, fmt)) {
80 return false;
81 }
83 int pixsz = pixel_size(fmt);
85 unsigned char *dest = (unsigned char*)this->pixels;
86 unsigned char *src = (unsigned char*)pixels + (y * scan_width + x) * pixsz;
87 for(int i=0; i<ysz; i++) {
88 memcpy(dest, src, xsz * pixsz);
89 dest += xsz * pixsz;
90 src += scan_width * pixsz;
91 }
92 return true;
93 }
95 void *Image::get_pixels() const
96 {
97 return pixels;
98 }
100 void Image::flip_horizontal()
101 {
102 int pixsz = pixel_size(fmt);
104 unsigned char *tmppix = (unsigned char*)alloca(pixsz);
106 unsigned char *scan = (unsigned char*)pixels;
107 for(int i=0; i<height; i++) {
108 unsigned char *dest = scan;
109 unsigned char *src = scan + (width - 1) * pixsz;
111 while(src > dest) {
112 memcpy(tmppix, src, pixsz);
113 memcpy(src, dest, pixsz);
114 memcpy(dest, tmppix, pixsz);
115 dest += pixsz;
116 src -= pixsz;
117 }
119 scan += width * pixsz;
120 }
121 }
123 void Image::flip_vertical()
124 {
125 int pixsz = pixel_size(fmt);
127 unsigned char *tmpscan = (unsigned char*)alloca(width * pixsz);
129 unsigned char *dest = (unsigned char*)pixels;
130 unsigned char *src = (unsigned char*)pixels + (height - 1) * width * pixsz;
132 while(src > dest) {
133 memcpy(tmpscan, src, width * pixsz);
134 memcpy(src, dest, width * pixsz);
135 memcpy(dest, tmpscan, width * pixsz);
136 dest += width * pixsz;
137 src -= width * pixsz;
138 }
139 }
141 void Image::rotate_180()
142 {
143 flip_vertical();
144 flip_horizontal();
145 }
147 bool Image::load(const char *fname)
148 {
149 struct img_pixmap pixmap;
151 img_init(&pixmap);
152 if(img_load(&pixmap, fname) == -1) {
153 return false;
154 }
156 Format fmt;
157 switch(pixmap.fmt) {
158 case IMG_FMT_GREY8:
159 fmt = FMT_GREY;
160 break;
161 case IMG_FMT_RGB24:
162 fmt = FMT_RGB;
163 break;
164 case IMG_FMT_RGBA32:
165 fmt = FMT_RGBA;
166 break;
167 case IMG_FMT_GREYF:
168 fmt = FMT_GREY_FLOAT;
169 break;
170 case IMG_FMT_RGBF:
171 fmt = FMT_RGB_FLOAT;
172 break;
173 case IMG_FMT_RGBAF:
174 fmt = FMT_RGBA_FLOAT;
175 break;
176 default:
177 img_destroy(&pixmap);
178 return false;
179 }
181 if(!set_pixels(pixmap.width, pixmap.height, pixmap.pixels, fmt)) {
182 img_destroy(&pixmap);
183 return false;
184 }
185 img_destroy(&pixmap);
186 return true;
187 }
189 bool Image::save(const char *fname) const
190 {
191 struct img_pixmap pixmap;
193 img_init(&pixmap);
195 switch(fmt) {
196 case FMT_GREY:
197 pixmap.fmt = IMG_FMT_GREY8;
198 break;
199 case FMT_GREY_FLOAT:
200 pixmap.fmt = IMG_FMT_GREYF;
201 break;
202 case FMT_RGB:
203 pixmap.fmt = IMG_FMT_RGB24;
204 break;
205 case FMT_RGB_FLOAT:
206 pixmap.fmt = IMG_FMT_RGBF;
207 break;
208 case FMT_RGBA:
209 pixmap.fmt = IMG_FMT_RGBA32;
210 break;
211 case FMT_RGBA_FLOAT:
212 pixmap.fmt = IMG_FMT_RGBAF;
213 break;
214 default:
215 return false;
216 }
218 pixmap.width = width;
219 pixmap.height = height;
220 pixmap.pixels = pixels;
221 pixmap.pixelsz = pixel_size(fmt);
223 if(img_save(&pixmap, fname) == -1) {
224 return false;
225 }
226 return true;
227 }
229 static int pixel_elements(Image::Format fmt)
230 {
231 switch(fmt) {
232 case Image::FMT_GREY:
233 case Image::FMT_GREY_FLOAT:
234 return 1;
236 case Image::FMT_RGB:
237 case Image::FMT_RGB_FLOAT:
238 return 3;
240 case Image::FMT_RGBA:
241 case Image::FMT_RGBA_FLOAT:
242 return 4;
244 default:
245 break;
246 }
247 return 0;
248 }
250 static int elem_size(Image::Format fmt)
251 {
252 switch(fmt) {
253 case Image::FMT_GREY:
254 case Image::FMT_RGB:
255 case Image::FMT_RGBA:
256 return 1;
258 case Image::FMT_GREY_FLOAT:
259 case Image::FMT_RGB_FLOAT:
260 case Image::FMT_RGBA_FLOAT:
261 return sizeof(float);
263 default:
264 break;
265 }
266 return 0;
267 }
269 static int pixel_size(Image::Format fmt)
270 {
271 return elem_size(fmt) * pixel_elements(fmt);
272 }