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.
20 #include "base_config.h"
22 /*---------------------------------------------------------------------------*/
24 void image_size(int *W, int *H, int w, int h)
26 /* Round the image size up to the next power-of-two. */
31 while (*W < w) *W *= 2;
32 while (*H < h) *H *= 2;
35 /*---------------------------------------------------------------------------*/
37 static void *image_load_png(const char *filename, int *width,
43 png_structp readp = NULL;
44 png_infop infop = NULL;
45 png_bytep *bytep = NULL;
46 unsigned char *p = NULL;
48 /* Initialize all PNG import data structures. */
50 if (!(fp = fopen(filename, FMODE_RB)))
53 if (!(readp = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)))
56 if (!(infop = png_create_info_struct(readp)))
59 /* Enable the default PNG error handler. */
61 if (setjmp(png_jmpbuf(readp)) == 0)
65 /* Read the PNG header. */
67 png_init_io(readp, fp);
68 png_read_png(readp, infop,
69 PNG_TRANSFORM_EXPAND |
70 PNG_TRANSFORM_STRIP_16 |
71 PNG_TRANSFORM_PACKING, NULL);
73 /* Extract and check image properties. */
75 w = (int) png_get_image_width (readp, infop);
76 h = (int) png_get_image_height(readp, infop);
78 switch (png_get_color_type(readp, infop))
80 case PNG_COLOR_TYPE_GRAY: b = 1; break;
81 case PNG_COLOR_TYPE_GRAY_ALPHA: b = 2; break;
82 case PNG_COLOR_TYPE_RGB: b = 3; break;
83 case PNG_COLOR_TYPE_RGB_ALPHA: b = 4; break;
85 default: longjmp(png_jmpbuf(readp), -1);
88 /* Read the pixel data. */
90 if (!(bytep = png_get_rows(readp, infop)))
91 longjmp(png_jmpbuf(readp), -1);
93 /* Allocate the final pixel buffer and copy pixels there. */
95 if ((p = (unsigned char *) malloc(w * h * b)))
97 for (r = 0; r < h; r++)
98 for (c = 0; c < w; c++)
99 for (i = 0; i < b; i++)
100 p[r*w*b+c*b+i] = (unsigned char) bytep[r][c*b+i];
102 if (width) *width = w;
103 if (height) *height = h;
104 if (bytes) *bytes = b;
109 /* Free all resources. */
111 png_destroy_read_struct(&readp, &infop, NULL);
117 static void *image_load_jpg(const char *filename, int *width,
124 if ((fp = fopen(filename, FMODE_RB)))
126 struct jpeg_decompress_struct cinfo;
127 struct jpeg_error_mgr jerr;
131 /* Initialize the JPG decompressor. */
133 cinfo.err = jpeg_std_error(&jerr);
134 jpeg_create_decompress(&cinfo);
135 jpeg_stdio_src(&cinfo, fp);
137 /* Grab the JPG header info. */
139 jpeg_read_header(&cinfo, TRUE);
140 jpeg_start_decompress(&cinfo);
142 w = cinfo.output_width;
143 h = cinfo.output_height;
144 b = cinfo.output_components;
146 /* Allocate the final pixel buffer and copy pixels there. */
148 if ((p = (GLubyte *) malloc (w * h * b)))
150 while (cinfo.output_scanline < cinfo.output_height)
152 GLubyte *buffer = p + w * b * i;
153 i += jpeg_read_scanlines(&cinfo, &buffer, 1);
156 if (width) *width = w;
157 if (height) *height = h;
158 if (bytes) *bytes = b;
161 jpeg_finish_decompress(&cinfo);
162 jpeg_destroy_decompress(&cinfo);
170 void *image_load(const char *filename, int *width,
174 const char *ext = filename + strlen(filename) - 4;
176 if (strcmp(ext, ".png") == 0 || strcmp(ext, ".PNG") == 0)
177 return image_load_png(filename, width, height, bytes);
178 else if (strcmp(ext, ".jpg") == 0 || strcmp(ext, ".JPG") == 0)
179 return image_load_jpg(filename, width, height, bytes);
184 /*---------------------------------------------------------------------------*/
187 * Allocate and return a power-of-two image buffer with the given pixel buffer
188 * centered within in.
190 void *image_next2(const void *p, int w, int h, int b, int *w2, int *h2)
192 unsigned char *src = (unsigned char *) p;
193 unsigned char *dst = NULL;
198 image_size(&W, &H, w, h);
200 if ((dst = (unsigned char *) calloc(W * H * b, sizeof (unsigned char))))
202 int r, dr = (H - h) / 2;
203 int c, dc = (W - w) / 2;
206 for (r = 0; r < h; ++r)
207 for (c = 0; c < w; ++c)
208 for (i = 0; i < b; ++i)
213 dst[(R * W + C) * b + i] = src[(r * w + c) * b + i];
224 * Allocate and return a new down-sampled image buffer.
226 void *image_scale(const void *p, int w, int h, int b, int *wn, int *hn, int n)
228 unsigned char *src = (unsigned char *) p;
229 unsigned char *dst = NULL;
234 if ((dst = (unsigned char *) calloc(W * H * b, sizeof (unsigned char))))
240 /* Iterate each component of each destination pixel. */
242 for (di = 0; di < H; di++)
243 for (dj = 0; dj < W; dj++)
244 for (i = 0; i < b; i++)
248 /* Average the NxN source pixel block for each. */
250 for (si = di * n; si < (di + 1) * n; si++)
251 for (sj = dj * n; sj < (dj + 1) * n; sj++)
252 c += src[(si * w + sj) * b + i];
254 dst[(di * W + dj) * b + i] =
255 (unsigned char) (c / (n * n));
266 * Whiten the RGB channels of the given image without touching any alpha.
268 void image_white(void *p, int w, int h, int b)
270 unsigned char *s = (unsigned char *) p;
275 for (r = 0; r < h; r++)
276 for (c = 0; c < w; c++)
278 int k = (r * w + c) * b;
290 /*---------------------------------------------------------------------------*/