textures, overlay images, libimago
[demo_prior] / libs / imago2 / src / imago2.c
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2020 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 "ftype_module.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 int img_is_greyscale(struct img_pixmap *img)
291 {
292         return img->fmt == IMG_FMT_GREY8 || img->fmt == IMG_FMT_GREYF;
293 }
294
295
296 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
297 {
298         char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
299         memcpy(dest, pixel, img->pixelsz);
300 }
301
302 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
303 {
304         char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
305         memcpy(pixel, dest, img->pixelsz);
306 }
307
308 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
309 {
310         img_setpixel4i(img, x, y, pix, pix, pix, pix);
311 }
312
313 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
314 {
315         img_setpixel4f(img, x, y, pix, pix, pix, pix);
316 }
317
318 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
319 {
320         if(img_is_float(img)) {
321                 img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
322         } else {
323                 unsigned char pixel[4];
324                 pixel[0] = r;
325                 pixel[1] = g;
326                 pixel[2] = b;
327                 pixel[3] = a;
328
329                 img_setpixel(img, x, y, pixel);
330         }
331 }
332
333 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
334 {
335         if(img_is_float(img)) {
336                 float pixel[4];
337                 pixel[0] = r;
338                 pixel[1] = g;
339                 pixel[2] = b;
340                 pixel[3] = a;
341
342                 img_setpixel(img, x, y, pixel);
343         } else {
344                 img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
345         }
346 }
347
348 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
349 {
350         int junk[3];
351         img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
352 }
353
354 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
355 {
356         float junk[3];
357         img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
358 }
359
360 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
361 {
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;
369         } else {
370                 unsigned char pixel[4];
371                 img_getpixel(img, x, y, pixel);
372                 *r = pixel[0];
373                 *g = pixel[1];
374                 *b = pixel[2];
375                 *a = pixel[3];
376         }
377 }
378
379 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
380 {
381         if(img_is_float(img)) {
382                 float pixel[4] = {0, 0, 0, 0};
383                 img_getpixel(img, x, y, pixel);
384                 *r = pixel[0];
385                 *g = pixel[1];
386                 *b = pixel[2];
387                 *a = pixel[3];
388         } else {
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;
395         }
396 }
397
398 void img_io_set_user_data(struct img_io *io, void *uptr)
399 {
400         io->uptr = uptr;
401 }
402
403 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
404 {
405         io->read = read;
406 }
407
408 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
409 {
410         io->write = write;
411 }
412
413 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
414 {
415         io->seek = seek;
416 }
417
418
419 static int pixel_size(enum img_fmt fmt)
420 {
421         switch(fmt) {
422         case IMG_FMT_GREY8:
423                 return 1;
424         case IMG_FMT_RGB24:
425                 return 3;
426         case IMG_FMT_RGBA32:
427         case IMG_FMT_BGRA32:
428                 return 4;
429         case IMG_FMT_GREYF:
430                 return sizeof(float);
431         case IMG_FMT_RGBF:
432                 return 3 * sizeof(float);
433         case IMG_FMT_RGBAF:
434                 return 4 * sizeof(float);
435         case IMG_FMT_RGB565:
436                 return 2;
437         default:
438                 break;
439         }
440         return 0;
441 }
442
443 static size_t def_read(void *buf, size_t bytes, void *uptr)
444 {
445         return uptr ? fread(buf, 1, bytes, uptr) : 0;
446 }
447
448 static size_t def_write(void *buf, size_t bytes, void *uptr)
449 {
450         return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
451 }
452
453 static long def_seek(long offset, int whence, void *uptr)
454 {
455         if(!uptr || fseek(uptr, offset, whence) == -1) {
456                 return -1;
457         }
458         return ftell(uptr);
459 }
460