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/>.
26 static int pixel_size(enum img_fmt fmt);
27 static size_t def_read(void *buf, size_t bytes, void *uptr);
28 static size_t def_write(void *buf, size_t bytes, void *uptr);
29 static long def_seek(long offset, int whence, void *uptr);
32 void img_init(struct img_pixmap *img)
35 img->width = img->height = 0;
36 img->fmt = IMG_FMT_RGBA32;
37 img->pixelsz = pixel_size(img->fmt);
42 void img_destroy(struct img_pixmap *img)
44 chk_free(img->pixels);
45 img->pixels = 0; /* just in case... */
46 img->width = img->height = 0xbadbeef;
50 struct img_pixmap *img_create(void)
54 if(!(p = chk_malloc(sizeof *p))) {
61 void img_free(struct img_pixmap *img)
67 int img_set_name(struct img_pixmap *img, const char *name)
71 if(!(tmp = chk_malloc(strlen(name) + 1))) {
79 int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
82 return img_convert(img, fmt);
88 int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
90 return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
93 int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
96 int pixsz = pixel_size(fmt);
98 if(!(newpix = chk_malloc(w * h * pixsz))) {
103 memcpy(newpix, pix, w * h * pixsz);
105 memset(newpix, 0, w * h * pixsz);
108 chk_free(img->pixels);
109 img->pixels = newpix;
112 img->pixelsz = pixsz;
117 void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
119 struct img_pixmap img;
123 if(img_load(&img, fname) == -1) {
127 if(img_convert(&img, fmt) == -1) {
138 int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
141 struct img_pixmap img;
145 img.pixelsz = pixel_size(fmt);
150 res = img_save(&img, fname);
156 void img_free_pixels(void *pix)
161 int img_load(struct img_pixmap *img, const char *fname)
166 if(!(fp = fopen(fname, "rb"))) {
169 res = img_read_file(img, fp);
174 /* TODO implement filetype selection */
175 int img_save(struct img_pixmap *img, const char *fname)
180 img_set_name(img, fname);
182 if(!(fp = fopen(fname, "wb"))) {
185 res = img_write_file(img, fp);
190 int img_read_file(struct img_pixmap *img, FILE *fp)
192 struct img_io io = {0, def_read, def_write, def_seek};
195 return img_read(img, &io);
198 int img_write_file(struct img_pixmap *img, FILE *fp)
200 struct img_io io = {0, def_read, def_write, def_seek};
203 return img_write(img, &io);
206 int img_read(struct img_pixmap *img, struct img_io *io)
208 struct ftype_module *mod;
210 if((mod = img_find_format_module(io))) {
211 return mod->read(img, io);
216 int img_write(struct img_pixmap *img, struct img_io *io)
218 struct ftype_module *mod;
220 if(!img->name || !(mod = img_guess_format(img->name))) {
221 /* TODO throw some sort of warning? */
222 /* TODO implement some sort of module priority or let the user specify? */
223 if(!(mod = img_get_module(0))) {
228 return mod->write(img, io);
231 int img_to_float(struct img_pixmap *img)
233 enum img_fmt targ_fmt;
237 targ_fmt = IMG_FMT_GREYF;
241 targ_fmt = IMG_FMT_RGBF;
245 targ_fmt = IMG_FMT_RGBAF;
249 return 0; /* already float */
252 return img_convert(img, targ_fmt);
255 int img_to_integer(struct img_pixmap *img)
257 enum img_fmt targ_fmt;
261 targ_fmt = IMG_FMT_GREY8;
265 targ_fmt = IMG_FMT_RGB24;
269 targ_fmt = IMG_FMT_RGBA32;
273 return 0; /* already integer */
276 return img_convert(img, targ_fmt);
279 int img_is_float(struct img_pixmap *img)
281 return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
284 int img_has_alpha(struct img_pixmap *img)
286 if(img->fmt == IMG_FMT_RGBA32 || img->fmt == IMG_FMT_RGBAF) {
292 int img_is_greyscale(struct img_pixmap *img)
294 return img->fmt == IMG_FMT_GREY8 || img->fmt == IMG_FMT_GREYF;
298 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
300 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
301 memcpy(dest, pixel, img->pixelsz);
304 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
306 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
307 memcpy(pixel, dest, img->pixelsz);
310 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
312 img_setpixel4i(img, x, y, pix, pix, pix, pix);
315 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
317 img_setpixel4f(img, x, y, pix, pix, pix, pix);
320 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
322 if(img_is_float(img)) {
323 img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
325 unsigned char pixel[4];
331 img_setpixel(img, x, y, pixel);
335 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
337 if(img_is_float(img)) {
344 img_setpixel(img, x, y, pixel);
346 img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
350 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
353 img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
356 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
359 img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
362 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
364 if(img_is_float(img)) {
365 float pixel[4] = {0, 0, 0, 0};
366 img_getpixel(img, x, y, pixel);
367 *r = pixel[0] * 255.0;
368 *g = pixel[1] * 255.0;
369 *b = pixel[2] * 255.0;
370 *a = pixel[3] * 255.0;
372 unsigned char pixel[4];
373 img_getpixel(img, x, y, pixel);
381 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
383 if(img_is_float(img)) {
384 float pixel[4] = {0, 0, 0, 0};
385 img_getpixel(img, x, y, pixel);
391 unsigned char pixel[4];
392 img_getpixel(img, x, y, pixel);
393 *r = pixel[0] / 255.0;
394 *g = pixel[1] / 255.0;
395 *b = pixel[2] / 255.0;
396 *a = pixel[3] / 255.0;
400 void img_io_set_user_data(struct img_io *io, void *uptr)
405 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
410 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
415 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
421 static int pixel_size(enum img_fmt fmt)
432 return sizeof(float);
434 return 3 * sizeof(float);
436 return 4 * sizeof(float);
445 static size_t def_read(void *buf, size_t bytes, void *uptr)
447 return uptr ? fread(buf, 1, bytes, uptr) : 0;
450 static size_t def_write(void *buf, size_t bytes, void *uptr)
452 return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
455 static long def_seek(long offset, int whence, void *uptr)
457 if(!uptr || fseek(uptr, offset, whence) == -1) {