3 #if defined(WIN32) || defined(__WIN32__)
13 static int pixel_elements(Image::Format fmt);
14 static int elem_size(Image::Format fmt);
15 static int pixel_size(Image::Format fmt);
26 delete [] (char*)pixels;
29 int Image::get_width() const
34 int Image::get_height() const
39 Image::Format Image::get_format() const
44 bool Image::create(int x, int y, Format fmt)
51 pixels = new char[x * y * pixel_size(fmt)];
59 bool Image::set_pixels(int xsz, int ysz, void *pixels, Format fmt)
61 if(!create(xsz, ysz, fmt)) {
64 memcpy(this->pixels, pixels, xsz * ysz * pixel_size(fmt));
68 bool Image::set_pixels(int xsz, int ysz, void *pixels, int scan_width, Format fmt)
70 return set_pixels(xsz, ysz, pixels, 0, 0, scan_width, fmt);
73 bool Image::set_pixels(int xsz, int ysz, void *pixels, int x, int y, int scan_width, Format fmt)
79 if(!create(xsz, ysz, fmt)) {
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);
90 src += scan_width * pixsz;
95 void *Image::get_pixels() const
100 void Image::flip_horizontal()
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;
112 memcpy(tmppix, src, pixsz);
113 memcpy(src, dest, pixsz);
114 memcpy(dest, tmppix, pixsz);
119 scan += width * pixsz;
123 void Image::flip_vertical()
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;
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;
141 void Image::rotate_180()
147 void Image::resize_half()
149 int pixsz = pixel_size(fmt);
150 int newxsz = width / 2;
151 int newysz = height / 2;
153 if(!newxsz || !newysz) return;
155 unsigned char *newpix = new unsigned char[newxsz * newysz * pixsz];
157 unsigned char *sptr = (unsigned char*)pixels;
158 unsigned char *dptr = newpix;
160 for(int i=0; i<newysz; i++) {
161 if(i & 1) sptr += width * pixsz;
162 for(int j=0; j<newxsz; j++) {
163 memcpy(dptr, sptr, pixsz);
169 delete [] (char*)pixels;
175 static size_t io_read(void *buf, size_t bytes, void *fp)
177 return ass_fread(buf, 1, bytes, fp);
180 static long io_seek(long offs, int whence, void *fp)
182 ass_fseek(fp, offs, whence);
183 return ass_ftell(fp);
186 bool Image::load(const char *fname)
188 struct img_pixmap pixmap;
189 struct img_io io = {0, io_read, 0, io_seek};
192 if(!(fp = ass_fopen(fname, "rb"))) {
198 if(img_read(&pixmap, &io) == -1) {
214 fmt = FMT_GREY_FLOAT;
220 fmt = FMT_RGBA_FLOAT;
223 img_destroy(&pixmap);
227 if(!set_pixels(pixmap.width, pixmap.height, pixmap.pixels, fmt)) {
228 img_destroy(&pixmap);
231 img_destroy(&pixmap);
235 bool Image::save(const char *fname) const
237 struct img_pixmap pixmap;
243 pixmap.fmt = IMG_FMT_GREY8;
246 pixmap.fmt = IMG_FMT_GREYF;
249 pixmap.fmt = IMG_FMT_RGB24;
252 pixmap.fmt = IMG_FMT_RGBF;
255 pixmap.fmt = IMG_FMT_RGBA32;
258 pixmap.fmt = IMG_FMT_RGBAF;
264 pixmap.width = width;
265 pixmap.height = height;
266 pixmap.pixels = pixels;
267 pixmap.pixelsz = pixel_size(fmt);
269 if(img_save(&pixmap, fname) == -1) {
275 static int pixel_elements(Image::Format fmt)
278 case Image::FMT_GREY:
279 case Image::FMT_GREY_FLOAT:
283 case Image::FMT_RGB_FLOAT:
286 case Image::FMT_RGBA:
287 case Image::FMT_RGBA_FLOAT:
296 static int elem_size(Image::Format fmt)
299 case Image::FMT_GREY:
301 case Image::FMT_RGBA:
304 case Image::FMT_GREY_FLOAT:
305 case Image::FMT_RGB_FLOAT:
306 case Image::FMT_RGBA_FLOAT:
307 return sizeof(float);
315 static int pixel_size(Image::Format fmt)
317 return elem_size(fmt) * pixel_elements(fmt);