2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010 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)
139 struct img_pixmap img;
143 img.name = (char*)fname;
148 return img_save(&img, fname);
151 void img_free_pixels(void *pix)
156 int img_load(struct img_pixmap *img, const char *fname)
161 if(!(fp = fopen(fname, "rb"))) {
164 res = img_read_file(img, fp);
169 /* TODO implement filetype selection */
170 int img_save(struct img_pixmap *img, const char *fname)
175 img_set_name(img, fname);
177 if(!(fp = fopen(fname, "wb"))) {
180 res = img_write_file(img, fp);
185 int img_read_file(struct img_pixmap *img, FILE *fp)
187 struct img_io io = {0, def_read, def_write, def_seek};
190 return img_read(img, &io);
193 int img_write_file(struct img_pixmap *img, FILE *fp)
195 struct img_io io = {0, def_read, def_write, def_seek};
198 return img_write(img, &io);
201 int img_read(struct img_pixmap *img, struct img_io *io)
203 struct ftype_module *mod;
205 if((mod = img_find_format_module(io))) {
206 return mod->read(img, io);
211 int img_write(struct img_pixmap *img, struct img_io *io)
213 struct ftype_module *mod;
215 if(!img->name || !(mod = img_guess_format(img->name))) {
216 /* TODO throw some sort of warning? */
217 /* TODO implement some sort of module priority or let the user specify? */
218 if(!(mod = img_get_module(0))) {
223 return mod->write(img, io);
226 int img_to_float(struct img_pixmap *img)
228 enum img_fmt targ_fmt;
232 targ_fmt = IMG_FMT_GREYF;
236 targ_fmt = IMG_FMT_RGBF;
240 targ_fmt = IMG_FMT_RGBAF;
244 return 0; /* already float */
247 return img_convert(img, targ_fmt);
250 int img_to_integer(struct img_pixmap *img)
252 enum img_fmt targ_fmt;
256 targ_fmt = IMG_FMT_GREY8;
260 targ_fmt = IMG_FMT_RGB24;
264 targ_fmt = IMG_FMT_RGBA32;
268 return 0; /* already integer */
271 return img_convert(img, targ_fmt);
274 int img_is_float(struct img_pixmap *img)
276 return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
279 int img_has_alpha(struct img_pixmap *img)
281 if(img->fmt == IMG_FMT_RGBA32 || img->fmt == IMG_FMT_RGBAF) {
288 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
290 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
291 memcpy(dest, pixel, img->pixelsz);
294 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
296 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
297 memcpy(pixel, dest, img->pixelsz);
300 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
302 img_setpixel4i(img, x, y, pix, pix, pix, pix);
305 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
307 img_setpixel4f(img, x, y, pix, pix, pix, pix);
310 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
312 if(img_is_float(img)) {
313 img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
315 unsigned char pixel[4];
321 img_setpixel(img, x, y, pixel);
325 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
327 if(img_is_float(img)) {
334 img_setpixel(img, x, y, pixel);
336 img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
340 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
343 img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
346 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
349 img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
352 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
354 if(img_is_float(img)) {
355 float pixel[4] = {0, 0, 0, 0};
356 img_getpixel(img, x, y, pixel);
357 *r = pixel[0] * 255.0;
358 *g = pixel[1] * 255.0;
359 *b = pixel[2] * 255.0;
360 *a = pixel[3] * 255.0;
362 unsigned char pixel[4];
363 img_getpixel(img, x, y, pixel);
371 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
373 if(img_is_float(img)) {
374 float pixel[4] = {0, 0, 0, 0};
375 img_getpixel(img, x, y, pixel);
381 unsigned char pixel[4];
382 img_getpixel(img, x, y, pixel);
383 *r = pixel[0] / 255.0;
384 *g = pixel[1] / 255.0;
385 *b = pixel[2] / 255.0;
386 *a = pixel[3] / 255.0;
390 void img_io_set_user_data(struct img_io *io, void *uptr)
395 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
400 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
405 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
411 static int pixel_size(enum img_fmt fmt)
421 return sizeof(float);
423 return 3 * sizeof(float);
425 return 4 * sizeof(float);
432 static size_t def_read(void *buf, size_t bytes, void *uptr)
434 return uptr ? fread(buf, 1, bytes, uptr) : 0;
437 static size_t def_write(void *buf, size_t bytes, void *uptr)
439 return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
442 static long def_seek(long offset, int whence, void *uptr)
444 if(!uptr || fseek(uptr, offset, whence) == -1) {