2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2020 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /* calculate int-aligned offset to colormap, right after the end of the pixel data */
28 #define CMAPPTR(fb, fbsz) \
29 (struct img_colormap*)((((uintptr_t)fb) + (fbsz) + sizeof(int) - 1) & ~(sizeof(int) - 1))
31 static int pixel_size(enum img_fmt fmt);
32 static size_t def_read(void *buf, size_t bytes, void *uptr);
33 static size_t def_write(void *buf, size_t bytes, void *uptr);
34 static long def_seek(long offset, int whence, void *uptr);
37 void img_init(struct img_pixmap *img)
40 img->width = img->height = 0;
41 img->fmt = IMG_FMT_RGBA32;
42 img->pixelsz = pixel_size(img->fmt);
47 void img_destroy(struct img_pixmap *img)
49 chk_free(img->pixels);
50 img->pixels = 0; /* just in case... */
51 img->width = img->height = 0xbadbeef;
55 struct img_pixmap *img_create(void)
59 if(!(p = chk_malloc(sizeof *p))) {
66 void img_free(struct img_pixmap *img)
72 int img_set_name(struct img_pixmap *img, const char *name)
76 if(!(tmp = chk_malloc(strlen(name) + 1))) {
84 int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
87 return img_convert(img, fmt);
93 int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
95 return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
98 int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
101 int pixsz = pixel_size(fmt);
102 int bsz = w * h * pixsz;
104 if(fmt == IMG_FMT_IDX8) {
105 /* add space for the colormap, and space to align it to sizeof(int) */
106 bsz += sizeof(struct img_colormap) + sizeof(int) - 1;
109 if(!(newpix = chk_malloc(bsz))) {
114 memcpy(newpix, pix, w * h * pixsz);
116 memset(newpix, 0, bsz);
119 chk_free(img->pixels);
120 img->pixels = newpix;
123 img->pixelsz = pixsz;
128 void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
130 struct img_pixmap img;
134 if(img_load(&img, fname) == -1) {
138 if(img_convert(&img, fmt) == -1) {
149 int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
152 struct img_pixmap img;
156 img.pixelsz = pixel_size(fmt);
161 res = img_save(&img, fname);
167 void img_free_pixels(void *pix)
172 int img_load(struct img_pixmap *img, const char *fname)
177 if(!(fp = fopen(fname, "rb"))) {
180 res = img_read_file(img, fp);
185 /* TODO implement filetype selection */
186 int img_save(struct img_pixmap *img, const char *fname)
191 img_set_name(img, fname);
193 if(!(fp = fopen(fname, "wb"))) {
196 res = img_write_file(img, fp);
201 int img_read_file(struct img_pixmap *img, FILE *fp)
203 struct img_io io = {0, def_read, def_write, def_seek};
206 return img_read(img, &io);
209 int img_write_file(struct img_pixmap *img, FILE *fp)
211 struct img_io io = {0, def_read, def_write, def_seek};
214 return img_write(img, &io);
217 int img_read(struct img_pixmap *img, struct img_io *io)
219 struct ftype_module *mod;
221 if((mod = img_find_format_module(io))) {
222 return mod->read(img, io);
227 int img_write(struct img_pixmap *img, struct img_io *io)
229 struct ftype_module *mod;
231 if(!img->name || !(mod = img_guess_format(img->name))) {
232 /* TODO throw some sort of warning? */
233 /* TODO implement some sort of module priority or let the user specify? */
234 if(!(mod = img_get_module(0))) {
239 return mod->write(img, io);
242 int img_to_float(struct img_pixmap *img)
244 enum img_fmt targ_fmt;
248 targ_fmt = IMG_FMT_GREYF;
252 targ_fmt = IMG_FMT_RGBF;
256 targ_fmt = IMG_FMT_RGBAF;
260 return 0; /* already float */
263 return img_convert(img, targ_fmt);
266 int img_to_integer(struct img_pixmap *img)
268 enum img_fmt targ_fmt;
272 targ_fmt = IMG_FMT_GREY8;
276 targ_fmt = IMG_FMT_RGB24;
280 targ_fmt = IMG_FMT_RGBA32;
284 return 0; /* already integer */
287 return img_convert(img, targ_fmt);
290 int img_is_float(struct img_pixmap *img)
292 return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
295 int img_has_alpha(struct img_pixmap *img)
297 if(img->fmt == IMG_FMT_RGBA32 || img->fmt == IMG_FMT_RGBAF) {
303 int img_is_greyscale(struct img_pixmap *img)
305 return img->fmt == IMG_FMT_GREY8 || img->fmt == IMG_FMT_GREYF;
309 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
311 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
312 memcpy(dest, pixel, img->pixelsz);
315 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
317 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
318 memcpy(pixel, dest, img->pixelsz);
321 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
323 img_setpixel4i(img, x, y, pix, pix, pix, pix);
326 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
328 img_setpixel4f(img, x, y, pix, pix, pix, pix);
331 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
333 if(img_is_float(img)) {
334 img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
336 unsigned char pixel[4];
342 img_setpixel(img, x, y, pixel);
346 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
348 if(img_is_float(img)) {
355 img_setpixel(img, x, y, pixel);
357 img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
361 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
364 img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
367 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
370 img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
373 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
375 if(img_is_float(img)) {
376 float pixel[4] = {0, 0, 0, 0};
377 img_getpixel(img, x, y, pixel);
378 *r = pixel[0] * 255.0;
379 *g = pixel[1] * 255.0;
380 *b = pixel[2] * 255.0;
381 *a = pixel[3] * 255.0;
383 unsigned char pixel[4];
384 img_getpixel(img, x, y, pixel);
392 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
394 if(img_is_float(img)) {
395 float pixel[4] = {0, 0, 0, 0};
396 img_getpixel(img, x, y, pixel);
402 unsigned char pixel[4];
403 img_getpixel(img, x, y, pixel);
404 *r = pixel[0] / 255.0;
405 *g = pixel[1] / 255.0;
406 *b = pixel[2] / 255.0;
407 *a = pixel[3] / 255.0;
411 struct img_colormap *img_colormap(struct img_pixmap *img)
415 if(img->fmt != IMG_FMT_IDX8 || !img->pixels) {
419 return CMAPPTR(img->pixels, img->width * img->height * img->pixelsz);
422 void img_io_set_user_data(struct img_io *io, void *uptr)
427 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
432 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
437 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
443 static int pixel_size(enum img_fmt fmt)
455 return sizeof(float);
457 return 3 * sizeof(float);
459 return 4 * sizeof(float);
468 static size_t def_read(void *buf, size_t bytes, void *uptr)
470 return uptr ? fread(buf, 1, bytes, uptr) : 0;
473 static size_t def_write(void *buf, size_t bytes, void *uptr)
475 return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
478 static long def_seek(long offset, int whence, void *uptr)
480 if(!uptr || fseek(uptr, offset, whence) == -1) {