initial commit, eq circuit emulator
[eqemu] / libs / libimago / 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 "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         struct img_pixmap img;
140
141         img_init(&img);
142         img.fmt = fmt;
143         img.name = (char*)fname;
144         img.width = xsz;
145         img.height = ysz;
146         img.pixels = pix;
147
148         return img_save(&img, fname);
149 }
150
151 void img_free_pixels(void *pix)
152 {
153         free(pix);
154 }
155
156 int img_load(struct img_pixmap *img, const char *fname)
157 {
158         int res;
159         FILE *fp;
160
161         if(!(fp = fopen(fname, "rb"))) {
162                 return -1;
163         }
164         res = img_read_file(img, fp);
165         fclose(fp);
166         return res;
167 }
168
169 /* TODO implement filetype selection */
170 int img_save(struct img_pixmap *img, const char *fname)
171 {
172         int res;
173         FILE *fp;
174
175         img_set_name(img, fname);
176
177         if(!(fp = fopen(fname, "wb"))) {
178                 return -1;
179         }
180         res = img_write_file(img, fp);
181         fclose(fp);
182         return res;
183 }
184
185 int img_read_file(struct img_pixmap *img, FILE *fp)
186 {
187         struct img_io io = {0, def_read, def_write, def_seek};
188
189         io.uptr = fp;
190         return img_read(img, &io);
191 }
192
193 int img_write_file(struct img_pixmap *img, FILE *fp)
194 {
195         struct img_io io = {0, def_read, def_write, def_seek};
196
197         io.uptr = fp;
198         return img_write(img, &io);
199 }
200
201 int img_read(struct img_pixmap *img, struct img_io *io)
202 {
203         struct ftype_module *mod;
204
205         if((mod = img_find_format_module(io))) {
206                 return mod->read(img, io);
207         }
208         return -1;
209 }
210
211 int img_write(struct img_pixmap *img, struct img_io *io)
212 {
213         struct ftype_module *mod;
214
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))) {
219                         return -1;
220                 }
221         }
222
223         return mod->write(img, io);
224 }
225
226 int img_to_float(struct img_pixmap *img)
227 {
228         enum img_fmt targ_fmt;
229
230         switch(img->fmt) {
231         case IMG_FMT_GREY8:
232                 targ_fmt = IMG_FMT_GREYF;
233                 break;
234
235         case IMG_FMT_RGB24:
236                 targ_fmt = IMG_FMT_RGBF;
237                 break;
238
239         case IMG_FMT_RGBA32:
240                 targ_fmt = IMG_FMT_RGBAF;
241                 break;
242
243         default:
244                 return 0;       /* already float */
245         }
246
247         return img_convert(img, targ_fmt);
248 }
249
250 int img_to_integer(struct img_pixmap *img)
251 {
252         enum img_fmt targ_fmt;
253
254         switch(img->fmt) {
255         case IMG_FMT_GREYF:
256                 targ_fmt = IMG_FMT_GREY8;
257                 break;
258
259         case IMG_FMT_RGBF:
260                 targ_fmt = IMG_FMT_RGB24;
261                 break;
262
263         case IMG_FMT_RGBAF:
264                 targ_fmt = IMG_FMT_RGBA32;
265                 break;
266
267         default:
268                 return 0;       /* already integer */
269         }
270
271         return img_convert(img, targ_fmt);
272 }
273
274 int img_is_float(struct img_pixmap *img)
275 {
276         return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
277 }
278
279 int img_has_alpha(struct img_pixmap *img)
280 {
281         if(img->fmt == IMG_FMT_RGBA32 || img->fmt == IMG_FMT_RGBAF) {
282                 return 1;
283         }
284         return 0;
285 }
286
287
288 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
289 {
290         char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
291         memcpy(dest, pixel, img->pixelsz);
292 }
293
294 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
295 {
296         char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
297         memcpy(pixel, dest, img->pixelsz);
298 }
299
300 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
301 {
302         img_setpixel4i(img, x, y, pix, pix, pix, pix);
303 }
304
305 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
306 {
307         img_setpixel4f(img, x, y, pix, pix, pix, pix);
308 }
309
310 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
311 {
312         if(img_is_float(img)) {
313                 img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
314         } else {
315                 unsigned char pixel[4];
316                 pixel[0] = r;
317                 pixel[1] = g;
318                 pixel[2] = b;
319                 pixel[3] = a;
320
321                 img_setpixel(img, x, y, pixel);
322         }
323 }
324
325 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
326 {
327         if(img_is_float(img)) {
328                 float pixel[4];
329                 pixel[0] = r;
330                 pixel[1] = g;
331                 pixel[2] = b;
332                 pixel[3] = a;
333
334                 img_setpixel(img, x, y, pixel);
335         } else {
336                 img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
337         }
338 }
339
340 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
341 {
342         int junk[3];
343         img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
344 }
345
346 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
347 {
348         float junk[3];
349         img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
350 }
351
352 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
353 {
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;
361         } else {
362                 unsigned char pixel[4];
363                 img_getpixel(img, x, y, pixel);
364                 *r = pixel[0];
365                 *g = pixel[1];
366                 *b = pixel[2];
367                 *a = pixel[3];
368         }
369 }
370
371 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
372 {
373         if(img_is_float(img)) {
374                 float pixel[4] = {0, 0, 0, 0};
375                 img_getpixel(img, x, y, pixel);
376                 *r = pixel[0];
377                 *g = pixel[1];
378                 *b = pixel[2];
379                 *a = pixel[3];
380         } else {
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;
387         }
388 }
389
390 void img_io_set_user_data(struct img_io *io, void *uptr)
391 {
392         io->uptr = uptr;
393 }
394
395 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
396 {
397         io->read = read;
398 }
399
400 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
401 {
402         io->write = write;
403 }
404
405 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
406 {
407         io->seek = seek;
408 }
409
410
411 static int pixel_size(enum img_fmt fmt)
412 {
413         switch(fmt) {
414         case IMG_FMT_GREY8:
415                 return 1;
416         case IMG_FMT_RGB24:
417                 return 3;
418         case IMG_FMT_RGBA32:
419                 return 4;
420         case IMG_FMT_GREYF:
421                 return sizeof(float);
422         case IMG_FMT_RGBF:
423                 return 3 * sizeof(float);
424         case IMG_FMT_RGBAF:
425                 return 4 * sizeof(float);
426         default:
427                 break;
428         }
429         return 0;
430 }
431
432 static size_t def_read(void *buf, size_t bytes, void *uptr)
433 {
434         return uptr ? fread(buf, 1, bytes, uptr) : 0;
435 }
436
437 static size_t def_write(void *buf, size_t bytes, void *uptr)
438 {
439         return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
440 }
441
442 static long def_seek(long offset, int whence, void *uptr)
443 {
444         if(!uptr || fseek(uptr, offset, whence) == -1) {
445                 return -1;
446         }
447         return ftell(uptr);
448 }
449