2 * Copyright (C) 2003 Robert Kooima
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.
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.
21 #include "base_config.h"
22 #include "base_image.h"
28 /*---------------------------------------------------------------------------*/
30 void image_size(int *W, int *H, int w, int h)
32 /* Round the image size up to the next power-of-two. */
37 while (*W < w) *W *= 2;
38 while (*H < h) *H *= 2;
41 /*---------------------------------------------------------------------------*/
43 static void *image_load_png(const char *filename, int *width,
49 png_structp readp = NULL;
50 png_infop infop = NULL;
51 png_bytep *bytep = NULL;
52 unsigned char *p = NULL;
54 /* Initialize all PNG import data structures. */
56 if (!(fh = fs_open(filename, "r")))
59 if (!(readp = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
62 if (!(infop = png_create_info_struct(readp)))
65 /* Enable the default PNG error handler. */
67 if (setjmp(png_jmpbuf(readp)) == 0)
71 /* Read the PNG header. */
73 png_set_read_fn(readp, fh, fs_png_read);
74 png_read_info(readp, infop);
76 png_set_expand(readp);
77 png_set_strip_16(readp);
78 png_set_packing(readp);
80 png_read_update_info(readp, infop);
82 /* Extract and check image properties. */
84 w = (int) png_get_image_width (readp, infop);
85 h = (int) png_get_image_height(readp, infop);
87 switch (png_get_color_type(readp, infop))
89 case PNG_COLOR_TYPE_GRAY: b = 1; break;
90 case PNG_COLOR_TYPE_GRAY_ALPHA: b = 2; break;
91 case PNG_COLOR_TYPE_RGB: b = 3; break;
92 case PNG_COLOR_TYPE_RGB_ALPHA: b = 4; break;
94 default: longjmp(png_jmpbuf(readp), -1);
97 if (!(bytep = png_malloc(readp, h * png_sizeof(png_bytep))))
98 longjmp(png_jmpbuf(readp), -1);
100 /* Allocate the final pixel buffer and read pixels there. */
102 if ((p = (unsigned char *) malloc(w * h * b)))
104 for (i = 0; i < h; i++)
105 bytep[i] = p + w * b * (h - i - 1);
107 png_read_image(readp, bytep);
108 png_read_end(readp, NULL);
110 if (width) *width = w;
111 if (height) *height = h;
112 if (bytes) *bytes = b;
115 png_free(readp, bytep);
119 /* Free all resources. */
121 png_destroy_read_struct(&readp, &infop, NULL);
127 static void *image_load_jpg(const char *filename, int *width,
134 if ((fp = fs_open(filename, "r")))
136 struct jpeg_decompress_struct cinfo;
137 struct jpeg_error_mgr jerr;
141 /* Initialize the JPG decompressor. */
143 cinfo.err = jpeg_std_error(&jerr);
144 jpeg_create_decompress(&cinfo);
146 /* Set up a VFS source manager. */
148 fs_jpg_src(&cinfo, fp);
150 /* Grab the JPG header info. */
152 jpeg_read_header(&cinfo, TRUE);
153 jpeg_start_decompress(&cinfo);
155 w = cinfo.output_width;
156 h = cinfo.output_height;
157 b = cinfo.output_components;
159 /* Allocate the final pixel buffer and copy pixels there. */
161 if ((p = (GLubyte *) malloc (w * h * b)))
163 while (cinfo.output_scanline < cinfo.output_height)
165 GLubyte *buffer = p + w * b * (h - i - 1);
166 i += jpeg_read_scanlines(&cinfo, &buffer, 1);
169 if (width) *width = w;
170 if (height) *height = h;
171 if (bytes) *bytes = b;
174 jpeg_finish_decompress(&cinfo);
175 jpeg_destroy_decompress(&cinfo);
183 void *image_load(const char *filename, int *width,
187 const char *ext = filename + strlen(filename) - 4;
189 if (strcmp(ext, ".png") == 0 || strcmp(ext, ".PNG") == 0)
190 return image_load_png(filename, width, height, bytes);
191 else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".JPG") == 0)
192 return image_load_jpg(filename, width, height, bytes);
197 /*---------------------------------------------------------------------------*/
200 * Allocate and return a power-of-two image buffer with the given pixel buffer
201 * centered within in.
203 void *image_next2(const void *p, int w, int h, int b, int *w2, int *h2)
205 unsigned char *src = (unsigned char *) p;
206 unsigned char *dst = NULL;
211 image_size(&W, &H, w, h);
213 if ((dst = (unsigned char *) calloc(W * H * b, sizeof (unsigned char))))
215 int r, dr = (H - h) / 2;
216 int c, dc = (W - w) / 2;
219 for (r = 0; r < h; ++r)
220 for (c = 0; c < w; ++c)
221 for (i = 0; i < b; ++i)
226 dst[(R * W + C) * b + i] = src[(r * w + c) * b + i];
237 * Allocate and return a new down-sampled image buffer.
239 void *image_scale(const void *p, int w, int h, int b, int *wn, int *hn, int n)
241 unsigned char *src = (unsigned char *) p;
242 unsigned char *dst = NULL;
247 if ((dst = (unsigned char *) calloc(W * H * b, sizeof (unsigned char))))
253 /* Iterate each component of each destination pixel. */
255 for (di = 0; di < H; di++)
256 for (dj = 0; dj < W; dj++)
257 for (i = 0; i < b; i++)
261 /* Average the NxN source pixel block for each. */
263 for (si = di * n; si < (di + 1) * n; si++)
264 for (sj = dj * n; sj < (dj + 1) * n; sj++)
265 c += src[(si * w + sj) * b + i];
267 dst[(di * W + dj) * b + i] =
268 (unsigned char) (c / (n * n));
279 * Whiten the RGB channels of the given image without touching any alpha.
281 void image_white(void *p, int w, int h, int b)
283 unsigned char *s = (unsigned char *) p;
288 for (r = 0; r < h; r++)
289 for (c = 0; c < w; c++)
291 int k = (r * w + c) * b;
304 * Allocate and return an image buffer of the given image flipped horizontally
307 void *image_flip(const void *p, int w, int h, int b, int hflip, int vflip)
311 assert(hflip || vflip);
316 if ((q = malloc(w * b * h)))
320 for (r = 0; r < h; r++)
321 for (c = 0; c < w; c++)
322 for (i = 0; i < b; i++)
324 int pr = vflip ? h - r - 1 : r;
325 int pc = hflip ? w - c - 1 : c;
327 int qi = r * w * b + c * b + i;
328 int pi = pr * w * b + pc * b + i;
330 q[qi] = ((const unsigned char *) p)[pi];
337 /*---------------------------------------------------------------------------*/