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"
24 /*---------------------------------------------------------------------------*/
26 void image_size(int *W, int *H, int w, int h)
28 /* Round the image size up to the next power-of-two. */
33 while (*W < w) *W *= 2;
34 while (*H < h) *H *= 2;
37 /*---------------------------------------------------------------------------*/
39 static void *image_load_png(const char *filename, int *width,
45 png_structp readp = NULL;
46 png_infop infop = NULL;
47 png_bytep *bytep = NULL;
48 unsigned char *p = NULL;
50 /* Initialize all PNG import data structures. */
52 if (!(fp = fopen(filename, FMODE_RB)))
55 if (!(readp = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
58 if (!(infop = png_create_info_struct(readp)))
61 /* Enable the default PNG error handler. */
63 if (setjmp(png_jmpbuf(readp)) == 0)
67 /* Read the PNG header. */
69 png_init_io(readp, fp);
70 png_read_info(readp, infop);
72 png_set_expand(readp);
73 png_set_strip_16(readp);
74 png_set_packing(readp);
76 png_read_update_info(readp, infop);
78 /* Extract and check image properties. */
80 w = (int) png_get_image_width (readp, infop);
81 h = (int) png_get_image_height(readp, infop);
83 switch (png_get_color_type(readp, infop))
85 case PNG_COLOR_TYPE_GRAY: b = 1; break;
86 case PNG_COLOR_TYPE_GRAY_ALPHA: b = 2; break;
87 case PNG_COLOR_TYPE_RGB: b = 3; break;
88 case PNG_COLOR_TYPE_RGB_ALPHA: b = 4; break;
90 default: longjmp(png_jmpbuf(readp), -1);
93 if (!(bytep = png_malloc(readp, h * png_sizeof(png_bytep))))
94 longjmp(png_jmpbuf(readp), -1);
96 /* Allocate the final pixel buffer and read pixels there. */
98 if ((p = (unsigned char *) malloc(w * h * b)))
100 for (i = 0; i < h; i++)
101 bytep[i] = p + w * b * (h - i - 1);
103 png_read_image(readp, bytep);
104 png_read_end(readp, NULL);
106 if (width) *width = w;
107 if (height) *height = h;
108 if (bytes) *bytes = b;
111 png_free(readp, bytep);
115 /* Free all resources. */
117 png_destroy_read_struct(&readp, &infop, NULL);
123 static void *image_load_jpg(const char *filename, int *width,
130 if ((fp = fopen(filename, FMODE_RB)))
132 struct jpeg_decompress_struct cinfo;
133 struct jpeg_error_mgr jerr;
137 /* Initialize the JPG decompressor. */
139 cinfo.err = jpeg_std_error(&jerr);
140 jpeg_create_decompress(&cinfo);
141 jpeg_stdio_src(&cinfo, fp);
143 /* Grab the JPG header info. */
145 jpeg_read_header(&cinfo, TRUE);
146 jpeg_start_decompress(&cinfo);
148 w = cinfo.output_width;
149 h = cinfo.output_height;
150 b = cinfo.output_components;
152 /* Allocate the final pixel buffer and copy pixels there. */
154 if ((p = (GLubyte *) malloc (w * h * b)))
156 while (cinfo.output_scanline < cinfo.output_height)
158 GLubyte *buffer = p + w * b * (h - i - 1);
159 i += jpeg_read_scanlines(&cinfo, &buffer, 1);
162 if (width) *width = w;
163 if (height) *height = h;
164 if (bytes) *bytes = b;
167 jpeg_finish_decompress(&cinfo);
168 jpeg_destroy_decompress(&cinfo);
176 void *image_load(const char *filename, int *width,
180 const char *ext = filename + strlen(filename) - 4;
182 if (strcmp(ext, ".png") == 0 || strcmp(ext, ".PNG") == 0)
183 return image_load_png(filename, width, height, bytes);
184 else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".JPG") == 0)
185 return image_load_jpg(filename, width, height, bytes);
190 /*---------------------------------------------------------------------------*/
193 * Allocate and return a power-of-two image buffer with the given pixel buffer
194 * centered within in.
196 void *image_next2(const void *p, int w, int h, int b, int *w2, int *h2)
198 unsigned char *src = (unsigned char *) p;
199 unsigned char *dst = NULL;
204 image_size(&W, &H, w, h);
206 if ((dst = (unsigned char *) calloc(W * H * b, sizeof (unsigned char))))
208 int r, dr = (H - h) / 2;
209 int c, dc = (W - w) / 2;
212 for (r = 0; r < h; ++r)
213 for (c = 0; c < w; ++c)
214 for (i = 0; i < b; ++i)
219 dst[(R * W + C) * b + i] = src[(r * w + c) * b + i];
230 * Allocate and return a new down-sampled image buffer.
232 void *image_scale(const void *p, int w, int h, int b, int *wn, int *hn, int n)
234 unsigned char *src = (unsigned char *) p;
235 unsigned char *dst = NULL;
240 if ((dst = (unsigned char *) calloc(W * H * b, sizeof (unsigned char))))
246 /* Iterate each component of each destination pixel. */
248 for (di = 0; di < H; di++)
249 for (dj = 0; dj < W; dj++)
250 for (i = 0; i < b; i++)
254 /* Average the NxN source pixel block for each. */
256 for (si = di * n; si < (di + 1) * n; si++)
257 for (sj = dj * n; sj < (dj + 1) * n; sj++)
258 c += src[(si * w + sj) * b + i];
260 dst[(di * W + dj) * b + i] =
261 (unsigned char) (c / (n * n));
272 * Whiten the RGB channels of the given image without touching any alpha.
274 void image_white(void *p, int w, int h, int b)
276 unsigned char *s = (unsigned char *) p;
281 for (r = 0; r < h; r++)
282 for (c = 0; c < w; c++)
284 int k = (r * w + c) * b;
297 * Allocate and return an image buffer of the given image flipped horizontally
300 void *image_flip(const void *p, int w, int h, int b, int hflip, int vflip)
304 assert(hflip || vflip);
309 if ((q = malloc(w * b * h)))
313 for (r = 0; r < h; r++)
314 for (c = 0; c < w; c++)
315 for (i = 0; i < b; i++)
317 int pr = vflip ? h - r - 1 : r;
318 int pc = hflip ? w - c - 1 : c;
320 int qi = r * w * b + c * b + i;
321 int pi = pr * w * b + pc * b + i;
323 q[qi] = ((const unsigned char *) p)[pi];
330 /*---------------------------------------------------------------------------*/