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/>.
23 #include "ftype_module.h"
25 static int pixel_size(enum img_fmt fmt);
26 static size_t def_read(void *buf, size_t bytes, void *uptr);
27 static size_t def_write(void *buf, size_t bytes, void *uptr);
28 static long def_seek(long offset, int whence, void *uptr);
31 void img_init(struct img_pixmap *img)
34 img->width = img->height = 0;
35 img->fmt = IMG_FMT_RGBA32;
36 img->pixelsz = pixel_size(img->fmt);
41 void img_destroy(struct img_pixmap *img)
44 img->pixels = 0; /* just in case... */
45 img->width = img->height = 0xbadbeef;
49 struct img_pixmap *img_create(void)
53 if(!(p = malloc(sizeof *p))) {
60 void img_free(struct img_pixmap *img)
66 int img_set_name(struct img_pixmap *img, const char *name)
70 if(!(tmp = malloc(strlen(name) + 1))) {
78 int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
81 return img_convert(img, fmt);
87 int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
89 return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
92 int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
95 int pixsz = pixel_size(fmt);
97 if(!(newpix = malloc(w * h * pixsz))) {
102 memcpy(newpix, pix, w * h * pixsz);
104 memset(newpix, 0, w * h * pixsz);
108 img->pixels = newpix;
111 img->pixelsz = pixsz;
116 void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
118 struct img_pixmap img;
122 if(img_load(&img, fname) == -1) {
126 if(img_convert(&img, fmt) == -1) {
137 int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
140 struct img_pixmap img;
148 res = img_save(&img, fname);
154 void img_free_pixels(void *pix)
159 int img_load(struct img_pixmap *img, const char *fname)
164 if(!(fp = fopen(fname, "rb"))) {
167 res = img_read_file(img, fp);
172 /* TODO implement filetype selection */
173 int img_save(struct img_pixmap *img, const char *fname)
178 img_set_name(img, fname);
180 if(!(fp = fopen(fname, "wb"))) {
183 res = img_write_file(img, fp);
188 int img_read_file(struct img_pixmap *img, FILE *fp)
190 struct img_io io = {0, def_read, def_write, def_seek};
193 return img_read(img, &io);
196 int img_write_file(struct img_pixmap *img, FILE *fp)
198 struct img_io io = {0, def_read, def_write, def_seek};
201 return img_write(img, &io);
204 int img_read(struct img_pixmap *img, struct img_io *io)
206 struct ftype_module *mod;
208 if((mod = img_find_format_module(io))) {
209 return mod->read(img, io);
214 int img_write(struct img_pixmap *img, struct img_io *io)
216 struct ftype_module *mod;
218 if(!img->name || !(mod = img_guess_format(img->name))) {
219 /* TODO throw some sort of warning? */
220 /* TODO implement some sort of module priority or let the user specify? */
221 if(!(mod = img_get_module(0))) {
226 return mod->write(img, io);
229 int img_to_float(struct img_pixmap *img)
231 enum img_fmt targ_fmt;
235 targ_fmt = IMG_FMT_GREYF;
239 targ_fmt = IMG_FMT_RGBF;
243 targ_fmt = IMG_FMT_RGBAF;
247 return 0; /* already float */
250 return img_convert(img, targ_fmt);
253 int img_to_integer(struct img_pixmap *img)
255 enum img_fmt targ_fmt;
259 targ_fmt = IMG_FMT_GREY8;
263 targ_fmt = IMG_FMT_RGB24;
267 targ_fmt = IMG_FMT_RGBA32;
271 return 0; /* already integer */
274 return img_convert(img, targ_fmt);
277 int img_is_float(struct img_pixmap *img)
279 return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
282 int img_has_alpha(struct img_pixmap *img)
284 if(img->fmt == IMG_FMT_RGBA32 || img->fmt == IMG_FMT_RGBAF) {
290 int img_is_greyscale(struct img_pixmap *img)
292 return img->fmt == IMG_FMT_GREY8 || img->fmt == IMG_FMT_GREYF;
296 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
298 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
299 memcpy(dest, pixel, img->pixelsz);
302 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
304 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
305 memcpy(pixel, dest, img->pixelsz);
308 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
310 img_setpixel4i(img, x, y, pix, pix, pix, pix);
313 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
315 img_setpixel4f(img, x, y, pix, pix, pix, pix);
318 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
320 if(img_is_float(img)) {
321 img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
323 unsigned char pixel[4];
329 img_setpixel(img, x, y, pixel);
333 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
335 if(img_is_float(img)) {
342 img_setpixel(img, x, y, pixel);
344 img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
348 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
351 img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
354 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
357 img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
360 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
362 if(img_is_float(img)) {
363 float pixel[4] = {0, 0, 0, 0};
364 img_getpixel(img, x, y, pixel);
365 *r = pixel[0] * 255.0;
366 *g = pixel[1] * 255.0;
367 *b = pixel[2] * 255.0;
368 *a = pixel[3] * 255.0;
370 unsigned char pixel[4];
371 img_getpixel(img, x, y, pixel);
379 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
381 if(img_is_float(img)) {
382 float pixel[4] = {0, 0, 0, 0};
383 img_getpixel(img, x, y, pixel);
389 unsigned char pixel[4];
390 img_getpixel(img, x, y, pixel);
391 *r = pixel[0] / 255.0;
392 *g = pixel[1] / 255.0;
393 *b = pixel[2] / 255.0;
394 *a = pixel[3] / 255.0;
398 void img_io_set_user_data(struct img_io *io, void *uptr)
403 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
408 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
413 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
419 static int pixel_size(enum img_fmt fmt)
430 return sizeof(float);
432 return 3 * sizeof(float);
434 return 4 * sizeof(float);
443 static size_t def_read(void *buf, size_t bytes, void *uptr)
445 return uptr ? fread(buf, 1, bytes, uptr) : 0;
448 static size_t def_write(void *buf, size_t bytes, void *uptr)
450 return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
453 static long def_seek(long offset, int whence, void *uptr)
455 if(!uptr || fseek(uptr, offset, whence) == -1) {