[go: up one dir, main page]

Menu

[r59]: / engine / loadpng.c  Maximize  Restore  History

Download this file

349 lines (277 with data), 9.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* $Id: loadpng.c,v 1.1 2004/04/26 20:24:47 mlefebvr Exp $ */
#include <stdio.h>
#include <png.h>
#include <allegro.h>
#include "ithelib.h"
#include "media.h" /* IRE transparent colors */
#ifdef _WIN32
#define snprintf _snprintf // God I hate Microsoft
#endif
BITMAP *iload_png(char *filename)
{
IFILE *img_fp;
#ifdef _WIN32
#define signature_size 8
#else
const int signature_size = 8; /* Nb of magic bytes in a png signature */
#endif
unsigned char magic[signature_size];
char *img_pixels = NULL; /* Pixels stream from the PNG image */
/*
* png variables
*/
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
int color_type; /* Type of the png image */
int bit_depth; /* bits per _channel_ (red, green, blue, alpha) */
png_byte channels; /* nb of channels. channels * bit_depth = bpp */
png_uint_32 width, height;
png_colorp img_palette; /* palette of the png file, if any */
int num_palette; /* number of colors in the png file palette, if any */
png_bytep trans; /* array of transparencies attached to the palette */
int num_trans; /* number of transparency entries */
png_color_16p trans_values; /* RGB samples for the transparent color (>8bpp) */
/* array of pointers to scanlines, needed to load the image
* matches the lines of img_pixels */
png_bytep *row_pointers = NULL;
png_uint_32 rowbytes; /* size of a row in bytes */
/*
* Allegro variables
*/
BITMAP *bmp = NULL; /* Allegro bitmap */
PALETTE bmp_palette; /* Palette associated to the Allegro bitmap */
/*
* Open png file and check signature
*/
img_fp = iopen(filename); /* handles errors */
memset (magic, '\0', sizeof magic);
iread(magic, signature_size, img_fp);
/* Check whether the signature matches a png file */
if (png_sig_cmp(magic, 0, signature_size) != 0) {
iclose(img_fp);
ithe_panic("PNG: Not a png file", filename);
}
/*
* Create the main png variables
*/
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
(png_voidp) NULL,
(png_error_ptr) NULL,
(png_error_ptr) NULL);
if (png_ptr == NULL) { /* error */
iclose(img_fp);
ithe_panic("PNG: png_create_read_struct() error", filename);
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) { /* error */
iclose(img_fp);
png_destroy_read_struct(&png_ptr, NULL, NULL);
ithe_panic("PNG: png_create_info_struct() error", filename);
}
/* libpng requires this */
if (setjmp(png_jmpbuf(png_ptr)) != 0) { /* error */
iclose(img_fp);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
ithe_panic("PNG: setjmp() failed", filename);
}
/* Set the png input stream */
png_init_io(png_ptr, img_fp->fp); /*Caveat to the IRE fs funcs.?*/
/* Tell libpng we have already read the signature of the png file */
png_set_sig_bytes(png_ptr, signature_size);
/* Get info header from the png image */
png_read_info(png_ptr, info_ptr);
/*
* Transform image
*/
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
/* Strip 16 bits per channel to 8 bits */
if (bit_depth == 16) {
png_set_strip_16(png_ptr);
}
if (bit_depth < 8) {
png_set_packing(png_ptr); /* At least 1 byte per pixel */
}
/* Get the image type:
* grayscale, grayscale + alpha, paletted, RGB, RGBA */
color_type = png_get_color_type(png_ptr, info_ptr);
/* I don't want to deal with grayscale images */
if ((color_type == PNG_COLOR_TYPE_GRAY)
|| (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) {
png_set_gray_to_rgb(png_ptr);
}
/* Update the "info" structure of the png image */
png_read_update_info(png_ptr, info_ptr);
/* Get updated informations about the image */
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
/*
* Simple transparency (i.e without Alpha channel)
*/
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_get_tRNS(png_ptr, info_ptr,
&trans /* array of transparent entries */,
&num_trans /* nb of transparent entries */,
&trans_values);
} else {
/* Sanity */
trans = NULL;
num_trans = 0;
trans_values = NULL;
}
/* Create the Allegro bitmap */
bmp = create_bitmap(width, height);
if (bmp == NULL) {
ithe_panic("PNG: failed to create bitmap", filename);
}
/* Get the number of channels in the image
* _libpng_ says:
* (valid values are 1 (GRAY, PALETTE), 2 (GRAY_ALPHA),
* 3 (RGB), 4 (RGB_ALPHA or RGB + filler byte))
*/
channels = png_get_channels(png_ptr, info_ptr);
/*
* Allocate a memory segment to store the image
*/
rowbytes = png_get_rowbytes(png_ptr, info_ptr); /* nb of bytes per row */
img_pixels = (char *) M_get(1, height * rowbytes);
/* Allocate an array of row pointers, as required by libpng */
row_pointers = (png_bytep *) M_get(height, sizeof (png_bytep));
/* Match it to the data segment previously allocated */
{
int row;
for (row = 0; row < height; row++) {
row_pointers[row] = (png_bytep) (img_pixels + (row * rowbytes));
}
}
/* Load the whole (transformed) png image in memory */
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, NULL); /* required, but useless */
iclose(img_fp);
/*
* Fill in the Allegro bitmap
*/
if (channels == 1) /* Paletted image */ {
/*
* Pixels are byte-length indexes to the palette.
*/
int i;
int x, y;
unsigned char pixel;
int color; /* Allegro point of view */
/*
* Get the palette
*/
png_get_PLTE(png_ptr, info_ptr, &img_palette, &num_palette);
/* Transparent indexes first */
for (i = 0; i < num_trans; i++) {
bmp_palette[i].r = ire_transparent_r;
bmp_palette[i].g = ire_transparent_g;
bmp_palette[i].b = ire_transparent_b;
}
/* go on with other indexes */
for (; i < num_palette; i++) {
bmp_palette[i].r = (unsigned char) img_palette[i].red;
bmp_palette[i].g = (unsigned char) img_palette[i].green;
bmp_palette[i].b = (unsigned char) img_palette[i].blue;
}
/*
* Fill in the pixels grid of the bitmap
*/
for (y = 0; y < height; y ++) {
for (x = 0; x < width; x ++) {
pixel = (unsigned char) row_pointers[y][x];
color = makecol(bmp_palette[pixel].r,
bmp_palette[pixel].g,
bmp_palette[pixel].b);
putpixel(bmp, x, y, color);
}
}
} else if (channels >= 3) /* RGB or RGBA image */ {
/*
* Pixels are a stream of RGB or RGBA samples, each byte-length.
*/
int x,y;
unsigned char *pixel;
/* RGB offsets relatively to a pixel starting position */
const int red_offset = 0,
green_offset = 1,
blue_offset = 2,
alpha_offset = 3;
/* RGB samples of a pixel color */
int red_sample, green_sample, blue_sample;
int color; /* Allegro point of view */
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
/*
* The pixel info is at:
* img_pixels + (y * rowbytes) + (x * channels)
* also written:
* row_pointers[y] + (x * channels)
*/
pixel = (unsigned char *)
(row_pointers[y] + (x * channels));
/*
* Translate transparent pixels to IRE transparency
* if there is an alpha channel or a transparent color
* in the image.
*/
/* Transparency through alpha channel
* The alpha channel in a PNG image means a level
* of opacity, ranging from 0 (transparency) to 255.
*
* From _this_ program point of view,
* a pixel can be either transparent or opaque,
* translucency is not supported.
* We arbitrarily use the value "0" as the only
* meaning of transparency; any other value will
* be taken as _opaque_.
*/
if ((channels == 4)
&& (*(pixel + alpha_offset) == 0)) {
putpixel(bmp, x, y, ire_transparent);
continue; /* next pixel */
}
red_sample = *(pixel + red_offset),
green_sample = *(pixel + green_offset),
blue_sample = *(pixel + blue_offset);
/* Single transparent color
* Note:
* Maybe it should be an "else if" relative to the
* alpha channel test, I don't know if both an alpha
* channel _and_ a single transparent color are
* possible in a png image
*/
if ((trans_values != NULL)
&& (trans_values->red == red_sample)
&& (trans_values->green == green_sample)
&& (trans_values->blue == blue_sample)) {
putpixel(bmp, x, y, ire_transparent);
continue; /* next pixel */
}
/*
* Set the pixel in the Allegro bitmap
*/
color = makecol(red_sample, green_sample, blue_sample);
putpixel(bmp, x, y, color);
}
}
} else /* a freaky png image */ {
char msg[80];
#ifndef __DJGPP__
snprintf(msg, sizeof msg,
"PNG: strange number of channels: %d in file", channels);
#else
sprintf(msg,"PNG: strange number of channels: %d in file", channels);
#endif
/* Free memory */
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
M_free(row_pointers);
M_free(img_pixels);
ithe_panic(msg, filename);
} /* if (channels...) */
/* Free memory */
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
M_free(row_pointers);
M_free(img_pixels);
return bmp;
}