7 static int pixel_elements(Image::Format fmt);
8 static int elem_size(Image::Format fmt);
9 static int pixel_size(Image::Format fmt);
20 delete [] (char*)pixels;
23 Image::Image(const Image &img)
26 set_pixels(img.width, img.height, img.pixels, img.fmt);
29 Image &Image::operator =(const Image &img)
32 delete [] (char*)pixels;
34 set_pixels(img.width, img.height, img.pixels, img.fmt);
39 Image::Image(Image &&img)
49 Image &Image::operator =(Image &&img)
62 int Image::get_width() const
67 int Image::get_height() const
72 Image::Format Image::get_format() const
77 bool Image::create(int x, int y, Format fmt)
84 pixels = new char[x * y * pixel_size(fmt)];
92 bool Image::set_pixels(int x, int y, void *pixels, Format fmt)
94 if(!create(x, y, fmt)) {
97 memcpy(this->pixels, pixels, x * y * pixel_size(fmt));
101 void *Image::get_pixels() const
106 static size_t io_read(void *buf, size_t bytes, void *uptr)
108 return ass_fread(buf, 1, bytes, uptr);
111 static long io_seek(long offs, int whence, void *uptr)
113 return ass_fseek(uptr, offs, whence);
116 bool Image::load(const char *fname)
118 struct img_pixmap pixmap;
121 if(!(io.uptr = ass_fopen(fname, "rb"))) {
122 fprintf(stderr, "failed to open image: %s\n", fname);
130 if(img_read(&pixmap, &io) == -1) {
147 fmt = FMT_GREY_FLOAT;
153 fmt = FMT_RGBA_FLOAT;
156 img_destroy(&pixmap);
160 if(!set_pixels(pixmap.width, pixmap.height, pixmap.pixels, fmt)) {
161 img_destroy(&pixmap);
164 img_destroy(&pixmap);
168 bool Image::save(const char *fname) const
170 struct img_pixmap pixmap;
176 pixmap.fmt = IMG_FMT_GREY8;
179 pixmap.fmt = IMG_FMT_GREYF;
182 pixmap.fmt = IMG_FMT_RGB24;
185 pixmap.fmt = IMG_FMT_RGBF;
188 pixmap.fmt = IMG_FMT_RGBA32;
191 pixmap.fmt = IMG_FMT_RGBAF;
197 pixmap.width = width;
198 pixmap.height = height;
199 pixmap.pixels = pixels;
200 pixmap.pixelsz = pixel_size(fmt);
202 if(img_save(&pixmap, fname) == -1) {
208 static int pixel_elements(Image::Format fmt)
211 case Image::FMT_GREY:
212 case Image::FMT_GREY_FLOAT:
216 case Image::FMT_RGB_FLOAT:
219 case Image::FMT_RGBA:
220 case Image::FMT_RGBA_FLOAT:
229 static int elem_size(Image::Format fmt)
232 case Image::FMT_GREY:
234 case Image::FMT_RGBA:
237 case Image::FMT_GREY_FLOAT:
238 case Image::FMT_RGB_FLOAT:
239 case Image::FMT_RGBA_FLOAT:
240 return sizeof(float);
248 static int pixel_size(Image::Format fmt)
250 return elem_size(fmt) * pixel_elements(fmt);