Fix mapc dependence on share/glext.h
[neverball] / share / base_image.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <png.h>
16 #include <jpeglib.h>
17 #include <stdlib.h>
18 #include <assert.h>
19
20 #include "base_config.h"
21 #include "base_image.h"
22
23 #include "fs.h"
24 #include "fs_png.h"
25 #include "fs_jpg.h"
26
27 /*---------------------------------------------------------------------------*/
28
29 void image_size(int *W, int *H, int w, int h)
30 {
31     /* Round the image size up to the next power-of-two. */
32
33     *W = w ? 1 : 0;
34     *H = h ? 1 : 0;
35
36     while (*W < w) *W *= 2;
37     while (*H < h) *H *= 2;
38 }
39
40 /*---------------------------------------------------------------------------*/
41
42 static void *image_load_png(const char *filename, int *width,
43                                                   int *height,
44                                                   int *bytes)
45 {
46     fs_file fh;
47
48     png_structp readp = NULL;
49     png_infop   infop = NULL;
50     png_bytep  *bytep = NULL;
51     unsigned char  *p = NULL;
52
53     /* Initialize all PNG import data structures. */
54
55     if (!(fh = fs_open(filename, "r")))
56         return NULL;
57
58     if (!(readp = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
59         return NULL;
60
61     if (!(infop = png_create_info_struct(readp)))
62         return NULL;
63
64     /* Enable the default PNG error handler. */
65
66     if (setjmp(png_jmpbuf(readp)) == 0)
67     {
68         int w, h, b, i;
69
70         /* Read the PNG header. */
71
72         png_set_read_fn(readp, fh, fs_png_read);
73         png_read_info(readp, infop);
74
75         png_set_expand(readp);
76         png_set_strip_16(readp);
77         png_set_packing(readp);
78
79         png_read_update_info(readp, infop);
80
81         /* Extract and check image properties. */
82
83         w = (int) png_get_image_width (readp, infop);
84         h = (int) png_get_image_height(readp, infop);
85
86         switch (png_get_color_type(readp, infop))
87         {
88         case PNG_COLOR_TYPE_GRAY:       b = 1; break;
89         case PNG_COLOR_TYPE_GRAY_ALPHA: b = 2; break;
90         case PNG_COLOR_TYPE_RGB:        b = 3; break;
91         case PNG_COLOR_TYPE_RGB_ALPHA:  b = 4; break;
92
93         default: longjmp(png_jmpbuf(readp), -1);
94         }
95
96         if (!(bytep = png_malloc(readp, h * png_sizeof(png_bytep))))
97             longjmp(png_jmpbuf(readp), -1);
98
99         /* Allocate the final pixel buffer and read pixels there. */
100
101         if ((p = (unsigned char *) malloc(w * h * b)))
102         {
103             for (i = 0; i < h; i++)
104                 bytep[i] = p + w * b * (h - i - 1);
105
106             png_read_image(readp, bytep);
107             png_read_end(readp, NULL);
108
109             if (width)  *width  = w;
110             if (height) *height = h;
111             if (bytes)  *bytes  = b;
112         }
113
114         png_free(readp, bytep);
115     }
116     else p = NULL;
117
118     /* Free all resources. */
119
120     png_destroy_read_struct(&readp, &infop, NULL);
121     fs_close(fh);
122
123     return p;
124 }
125
126 static void *image_load_jpg(const char *filename, int *width,
127                                                   int *height,
128                                                   int *bytes)
129 {
130     unsigned char *p = NULL;
131     fs_file fp;
132
133     if ((fp = fs_open(filename, "r")))
134     {
135         struct jpeg_decompress_struct cinfo;
136         struct jpeg_error_mgr         jerr;
137
138         int w, h, b, i = 0;
139
140         /* Initialize the JPG decompressor. */
141
142         cinfo.err = jpeg_std_error(&jerr);
143         jpeg_create_decompress(&cinfo);
144
145         /* Set up a VFS source manager. */
146
147         fs_jpg_src(&cinfo, fp);
148
149         /* Grab the JPG header info. */
150
151         jpeg_read_header(&cinfo, TRUE);
152         jpeg_start_decompress(&cinfo);
153
154         w = cinfo.output_width;
155         h = cinfo.output_height;
156         b = cinfo.output_components;
157
158         /* Allocate the final pixel buffer and copy pixels there. */
159
160         if ((p = (unsigned char *) malloc (w * h * b)))
161         {
162             while (cinfo.output_scanline < cinfo.output_height)
163             {
164                 unsigned char *buffer = p + w * b * (h - i - 1);
165                 i += jpeg_read_scanlines(&cinfo, &buffer, 1);
166             }
167
168             if (width)  *width  = w;
169             if (height) *height = h;
170             if (bytes)  *bytes  = b;
171         }
172
173         jpeg_finish_decompress(&cinfo);
174         jpeg_destroy_decompress(&cinfo);
175
176         fs_close(fp);
177     }
178
179     return p;
180 }
181
182 void *image_load(const char *filename, int *width,
183                                        int *height,
184                                        int *bytes)
185 {
186     const char *ext = filename + strlen(filename) - 4;
187
188     if      (strcmp(ext, ".png") == 0 || strcmp(ext, ".PNG") == 0)
189         return image_load_png(filename, width, height, bytes);
190     else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".JPG") == 0)
191         return image_load_jpg(filename, width, height, bytes);
192
193     return NULL;
194 }
195
196 /*---------------------------------------------------------------------------*/
197
198 /*
199  * Allocate and return a power-of-two image buffer with the given pixel buffer
200  * centered within in.
201  */
202 void *image_next2(const void *p, int w, int h, int b, int *w2, int *h2)
203 {
204     unsigned char *src = (unsigned char *) p;
205     unsigned char *dst = NULL;
206
207     int W;
208     int H;
209
210     image_size(&W, &H, w, h);
211
212     if ((dst = (unsigned char *) calloc(W * H * b, sizeof (unsigned char))))
213     {
214         int r, dr = (H - h) / 2;
215         int c, dc = (W - w) / 2;
216         int i;
217
218         for (r = 0; r < h; ++r)
219             for (c = 0; c < w; ++c)
220                 for (i = 0; i < b; ++i)
221                 {
222                     int R = r + dr;
223                     int C = c + dc;
224
225                     dst[(R * W + C) * b + i] = src[(r * w + c) * b + i];
226                 }
227
228         if (w2) *w2 = W;
229         if (h2) *h2 = H;
230     }
231
232     return dst;
233 }
234
235 /*
236  * Allocate and return a new down-sampled image buffer.
237  */
238 void *image_scale(const void *p, int w, int h, int b, int *wn, int *hn, int n)
239 {
240     unsigned char *src = (unsigned char *) p;
241     unsigned char *dst = NULL;
242
243     int W = w / n;
244     int H = h / n;
245
246     if ((dst = (unsigned char *) calloc(W * H * b, sizeof (unsigned char))))
247     {
248         int si, di;
249         int sj, dj;
250         int i;
251
252         /* Iterate each component of each destination pixel. */
253
254         for (di = 0; di < H; di++)
255             for (dj = 0; dj < W; dj++)
256                 for (i = 0; i < b; i++)
257                 {
258                     int c = 0;
259
260                     /* Average the NxN source pixel block for each. */
261
262                     for (si = di * n; si < (di + 1) * n; si++)
263                         for (sj = dj * n; sj < (dj + 1) * n; sj++)
264                             c += src[(si * w + sj) * b + i];
265
266                     dst[(di * W + dj) * b + i] =
267                         (unsigned char) (c / (n * n));
268                 }
269
270         if (wn) *wn = W;
271         if (hn) *hn = H;
272     }
273
274     return dst;
275 }
276
277 /*
278  * Whiten the RGB channels of the given image without touching any alpha.
279  */
280 void image_white(void *p, int w, int h, int b)
281 {
282     unsigned char *s = (unsigned char *) p;
283
284     int r;
285     int c;
286
287     for (r = 0; r < h; r++)
288         for (c = 0; c < w; c++)
289         {
290             int k = (r * w + c) * b;
291
292             s[k + 0] = 0xFF;
293
294             if (b > 2)
295             {
296                 s[k + 1] = 0xFF;
297                 s[k + 2] = 0xFF;
298             }
299         }
300 }
301
302 /*
303  * Allocate and return an image buffer of the given image flipped horizontally
304  * and/or vertically.
305  */
306 void *image_flip(const void *p, int w, int h, int b, int hflip, int vflip)
307 {
308     unsigned char *q;
309
310     assert(hflip || vflip);
311
312     if (!p)
313         return NULL;
314
315     if ((q = malloc(w * b * h)))
316     {
317         int r, c, i;
318
319         for (r = 0; r < h; r++)
320             for (c = 0; c < w; c++)
321                 for (i = 0; i < b; i++)
322                 {
323                     int pr = vflip ? h - r - 1 : r;
324                     int pc = hflip ? w - c - 1 : c;
325
326                     int qi = r  * w * b + c  * b + i;
327                     int pi = pr * w * b + pc * b + i;
328
329                     q[qi] = ((const unsigned char *) p)[pi];
330                 }
331         return q;
332     }
333     return NULL;
334 }
335
336 /*---------------------------------------------------------------------------*/