initial import
[dosrtxon] / libs / imago / src / imago2.c
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
4
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.
9
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.
14
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/>.
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "imago2.h"
23 #include "ftmodule.h"
24
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);
29
30
31 void img_init(struct img_pixmap *img)
32 {
33         img->pixels = 0;
34         img->width = img->height = 0;
35         img->fmt = IMG_FMT_RGBA32;
36         img->pixelsz = pixel_size(img->fmt);
37         img->name = 0;
38 }
39
40
41 void img_destroy(struct img_pixmap *img)
42 {
43         free(img->pixels);
44         img->pixels = 0;        /* just in case... */
45         img->width = img->height = 0xbadbeef;
46         free(img->name);
47 }
48
49 struct img_pixmap *img_create(void)
50 {
51         struct img_pixmap *p;
52
53         if(!(p = malloc(sizeof *p))) {
54                 return 0;
55         }
56         img_init(p);
57         return p;
58 }
59
60 void img_free(struct img_pixmap *img)
61 {
62         img_destroy(img);
63         free(img);
64 }
65
66 int img_set_name(struct img_pixmap *img, const char *name)
67 {
68         char *tmp;
69
70         if(!(tmp = malloc(strlen(name) + 1))) {
71                 return -1;
72         }
73         strcpy(tmp, name);
74         img->name = tmp;
75         return 0;
76 }
77
78 int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
79 {
80         if(img->pixels) {
81                 return img_convert(img, fmt);
82         }
83         img->fmt = fmt;
84         return 0;
85 }
86
87 int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
88 {
89         return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
90 }
91
92 int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
93 {
94         void *newpix;
95         int pixsz = pixel_size(fmt);
96
97         if(!(newpix = malloc(w * h * pixsz))) {
98                 return -1;
99         }
100
101         if(pix) {
102                 memcpy(newpix, pix, w * h * pixsz);
103         } else {
104                 memset(newpix, 0, w * h * pixsz);
105         }
106
107         free(img->pixels);
108         img->pixels = newpix;
109         img->width = w;
110         img->height = h;
111         img->pixelsz = pixsz;
112         img->fmt = fmt;
113         return 0;
114 }
115
116 void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
117 {
118         struct img_pixmap img;
119
120         img_init(&img);
121
122         if(img_load(&img, fname) == -1) {
123                 return 0;
124         }
125         if(img.fmt != fmt) {
126                 if(img_convert(&img, fmt) == -1) {
127                         img_destroy(&img);
128                         return 0;
129                 }
130         }
131
132         *xsz = img.width;
133         *ysz = img.height;
134         return img.pixels;
135 }
136
137 int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
138 {
139         int res;
140         struct img_pixmap img;
141
142         img_init(&img);
143         img.fmt = fmt;
144         img.width = xsz;
145         img.height = ysz;
146         img.pixels = pix;
147
148         res = img_save(&img, fname);
149         img.pixels = 0;
150         img_destroy(&img);
151         return res;
152 }
153
154 void img_free_pixels(void *pix)
155 {
156         free(pix);
157 }
158
159 int img_load(struct img_pixmap *img, const char *fname)
160 {
161         int res;
162         FILE *fp;
163
164         if(!(fp = fopen(fname, "rb"))) {
165                 return -1;
166         }
167         res = img_read_file(img, fp);
168         fclose(fp);
169         return res;
170 }
171
172 /* TODO implement filetype selection */
173 int img_save(struct img_pixmap *img, const char *fname)
174 {
175         int res;
176         FILE *fp;
177
178         img_set_name(img, fname);
179
180         if(!(fp = fopen(fname, "wb"))) {
181                 return -1;
182         }
183         res = img_write_file(img, fp);
184         fclose(fp);
185         return res;
186 }
187
188 int img_read_file(struct img_pixmap *img, FILE *fp)
189 {
190         struct img_io io = {0, def_read, def_write, def_seek};
191
192         io.uptr = fp;
193         return img_read(img, &io);
194 }
195
196 int img_write_file(struct img_pixmap *img, FILE *fp)
197 {
198         struct img_io io = {0, def_read, def_write, def_seek};
199
200         io.uptr = fp;
201         return img_write(img, &io);
202 }
203
204 int img_read(struct img_pixmap *img, struct img_io *io)
205 {
206         struct ftype_module *mod;
207
208         if((mod = img_find_format_module(io))) {
209                 return mod->read(img, io);
210         }
211         return -1;
212 }
213
214 int img_write(struct img_pixmap *img, struct img_io *io)
215 {
216         struct ftype_module *mod;
217
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))) {
222                         return -1;
223                 }
224         }
225
226         return mod->write(img, io);
227 }
228
229 int img_to_float(struct img_pixmap *img)
230 {
231         enum img_fmt targ_fmt;
232
233         switch(img->fmt) {
234         case IMG_FMT_GREY8:
235                 targ_fmt = IMG_FMT_GREYF;
236                 break;
237
238         case IMG_FMT_RGB24:
239                 targ_fmt = IMG_FMT_RGBF;
240                 break;
241
242         case IMG_FMT_RGBA32:
243                 targ_fmt = IMG_FMT_RGBAF;
244                 break;
245
246         default:
247                 return 0;       /* already float */
248         }
249
250         return img_convert(img, targ_fmt);
251 }
252
253 int img_to_integer(struct img_pixmap *img)
254 {
255         enum img_fmt targ_fmt;
256
257         switch(img->fmt) {
258         case IMG_FMT_GREYF:
259                 targ_fmt = IMG_FMT_GREY8;
260                 break;
261
262         case IMG_FMT_RGBF:
263                 targ_fmt = IMG_FMT_RGB24;
264                 break;
265
266         case IMG_FMT_RGBAF:
267                 targ_fmt = IMG_FMT_RGBA32;
268                 break;
269
270         default:
271                 return 0;       /* already integer */
272         }
273
274         return img_convert(img, targ_fmt);
275 }
276
277 int img_is_float(struct img_pixmap *img)
278 {
279         return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
280 }
281
282 int img_has_alpha(struct img_pixmap *img)
283 {
284         if(img->fmt == IMG_FMT_RGBA32 || img->fmt == IMG_FMT_RGBAF) {
285                 return 1;
286         }
287         return 0;
288 }
289
290
291 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
292 {
293         char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
294         memcpy(dest, pixel, img->pixelsz);
295 }
296
297 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
298 {
299         char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
300         memcpy(pixel, dest, img->pixelsz);
301 }
302
303 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
304 {
305         img_setpixel4i(img, x, y, pix, pix, pix, pix);
306 }
307
308 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
309 {
310         img_setpixel4f(img, x, y, pix, pix, pix, pix);
311 }
312
313 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
314 {
315         if(img_is_float(img)) {
316                 img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
317         } else {
318                 unsigned char pixel[4];
319                 pixel[0] = r;
320                 pixel[1] = g;
321                 pixel[2] = b;
322                 pixel[3] = a;
323
324                 img_setpixel(img, x, y, pixel);
325         }
326 }
327
328 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
329 {
330         if(img_is_float(img)) {
331                 float pixel[4];
332                 pixel[0] = r;
333                 pixel[1] = g;
334                 pixel[2] = b;
335                 pixel[3] = a;
336
337                 img_setpixel(img, x, y, pixel);
338         } else {
339                 img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
340         }
341 }
342
343 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
344 {
345         int junk[3];
346         img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
347 }
348
349 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
350 {
351         float junk[3];
352         img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
353 }
354
355 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
356 {
357         if(img_is_float(img)) {
358                 float pixel[4] = {0, 0, 0, 0};
359                 img_getpixel(img, x, y, pixel);
360                 *r = pixel[0] * 255.0;
361                 *g = pixel[1] * 255.0;
362                 *b = pixel[2] * 255.0;
363                 *a = pixel[3] * 255.0;
364         } else {
365                 unsigned char pixel[4];
366                 img_getpixel(img, x, y, pixel);
367                 *r = pixel[0];
368                 *g = pixel[1];
369                 *b = pixel[2];
370                 *a = pixel[3];
371         }
372 }
373
374 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
375 {
376         if(img_is_float(img)) {
377                 float pixel[4] = {0, 0, 0, 0};
378                 img_getpixel(img, x, y, pixel);
379                 *r = pixel[0];
380                 *g = pixel[1];
381                 *b = pixel[2];
382                 *a = pixel[3];
383         } else {
384                 unsigned char pixel[4];
385                 img_getpixel(img, x, y, pixel);
386                 *r = pixel[0] / 255.0;
387                 *g = pixel[1] / 255.0;
388                 *b = pixel[2] / 255.0;
389                 *a = pixel[3] / 255.0;
390         }
391 }
392
393 void img_io_set_user_data(struct img_io *io, void *uptr)
394 {
395         io->uptr = uptr;
396 }
397
398 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
399 {
400         io->read = read;
401 }
402
403 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
404 {
405         io->write = write;
406 }
407
408 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
409 {
410         io->seek = seek;
411 }
412
413
414 static int pixel_size(enum img_fmt fmt)
415 {
416         switch(fmt) {
417         case IMG_FMT_GREY8:
418                 return 1;
419         case IMG_FMT_RGB24:
420                 return 3;
421         case IMG_FMT_RGBA32:
422                 return 4;
423         case IMG_FMT_GREYF:
424                 return sizeof(float);
425         case IMG_FMT_RGBF:
426                 return 3 * sizeof(float);
427         case IMG_FMT_RGBAF:
428                 return 4 * sizeof(float);
429         default:
430                 break;
431         }
432         return 0;
433 }
434
435 static size_t def_read(void *buf, size_t bytes, void *uptr)
436 {
437         return uptr ? fread(buf, 1, bytes, uptr) : 0;
438 }
439
440 static size_t def_write(void *buf, size_t bytes, void *uptr)
441 {
442         return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
443 }
444
445 static long def_seek(long offset, int whence, void *uptr)
446 {
447         if(!uptr || fseek(uptr, offset, whence) == -1) {
448                 return -1;
449         }
450         return ftell(uptr);
451 }
452