File: png.c

package info (click to toggle)
xloadimage 4.1-25
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,820 kB
  • sloc: ansic: 36,084; asm: 284; makefile: 282; sh: 280
file content (367 lines) | stat: -rw-r--r-- 9,190 bytes parent folder | download | duplicates (4)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * PNG - Portable Network Graphics
 *
 * Alpha channle is linear.
 * Color   \   Bit Depth      1   2   4   8  16  palette  bKGD
 * PNG_COLOR_TYPE_PALETTE     O   O   O   O      must      8 bit
 * PNG_COLOR_TYPE_GRAY        O   O   O   O   O           16
 * PNG_COLOR_TYPE_GRAY_ALPHA              O   O           16
 * PNG_COLOR_TYPE_RGB                     O   O  possible 16 * 3
 * PNG_COLOR_TYPE_RGB_ALPHA               O   O  possible 16 * 3
 * 
 * This code is based on jpeg.c and sample code from the libpng-1.0.5.
 * 2000/01/10: YOSHIDA Hiroshi
 *
 * TODO:
 * pngLoad(): Alpha channel, Transparency palette.
 * pngDump(): Dump.
 * 
 */


#include "image.h"	/* xloadimage declarations */
#ifdef HAVE_LIBPNG
#include "options.h"
#include <png.h>
#include <setjmp.h>

#undef  DEBUG
/* #define  DEBUG */
#undef  debug

#ifdef DEBUG
# define debug(xx)	fprintf(stderr,xx)
#else
# define debug(xx)
#endif

#define PNG_BYTES_TO_CHECK 4

static Image *image;    /* xloadimage image being returned */
static ZFILE *zinput_file;
static char *filename;

int pngIdent(char *fullname, char *name);
Image *pngLoad(char *fullname, char *name, unsigned int verbose);
/* void pngDump(Image *image, char *options, char *file, int verbose); */


/*
 * png read handler
 */
static void png_read_data(png_structp png_ptr,
			  png_bytep data, png_size_t length)
{
  if (zread(zinput_file, data, length) != length)
    png_error(png_ptr, "Read Error");
}


/*
 * png warn handler
 */
static void output_warn(png_structp png_ptr, png_const_charp str)
{
  debug(" #warn ");
  fprintf(stderr, " PNG file: %s - %s\n", filename, str);
  fflush(stderr);
}


/*
 * png error handler
 */
static void output_error(png_structp png_ptr, png_const_charp str)
{
  debug(" #error ");
  output_warn( png_ptr, str);
#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
  longjmp(png_jmpbuf((png_ptr)),1);
#else
  longjmp(png_ptr->jmpbuf, 1);	/* return control to outer routine */
#endif
}


static const char *pngColor(int color_type)
{
  const char *str;

  switch (color_type) {
  case PNG_COLOR_TYPE_GRAY:
    str = "GRAY";
    break;
  case PNG_COLOR_TYPE_GRAY_ALPHA:
    str = "GRAY_ALPHA";
    break;
  case PNG_COLOR_TYPE_PALETTE:
    str = "PALETTE";
    break;
  case PNG_COLOR_TYPE_RGB:
    str = "RGB";
    break;
  case PNG_COLOR_TYPE_RGB_ALPHA:
    str = "RGB_ALPHA";
    break;
  default:
    str = "UNKNOWN_COLOR_TYPE";
  }
  return str;
}


static const char *pngInterlace(int interlace_type)
{
  const char *str;

  switch (interlace_type) {
  case PNG_INTERLACE_NONE:
    str = "NONE";
    break;
  case PNG_INTERLACE_ADAM7:
    str = "ADAM7";
    break;
  default:
    str = "UNKNOWN_TYPE";
  }
  return str;
}


/*
 * Output PNG file infomation.
 */
static void pngInfo( png_uint_32 width, png_uint_32 height,
		     int bit_depth, int color_type, int interlace_type,
		     double file_gamma)
{
  printf("%s is %ldx%ld PNG image, color type %s, %d bit",
	 filename, width, height, pngColor(color_type), bit_depth);
  if( interlace_type != PNG_INTERLACE_NONE)
    printf(", interlace %s", pngInterlace(interlace_type));
  if( file_gamma >= 0.0)
    printf(", file gamma %.4f", file_gamma);
  putchar('\n');
}


/*
 * pngIdent(), pngLoad()
 * Read PNG header & allocate png's struct:
 * return 1: success
 */
static int pngHeader(png_structpp png_pp,
		     png_infopp info_pp, png_infopp end_pp)
{
  *info_pp = *end_pp = NULL;
  *png_pp = png_create_read_struct(PNG_LIBPNG_VER_STRING,
				   NULL, output_error, output_warn);
  if (!*png_pp)
    return 0;
  *info_pp = png_create_info_struct(*png_pp);
  if (!*info_pp) {
    png_destroy_read_struct(png_pp, info_pp, end_pp);
    return 0;
  }
  *end_pp = png_create_info_struct(*png_pp);
  if (!*end_pp) {
    png_destroy_read_struct(png_pp, info_pp, end_pp);
    return 0;
  }
#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
  if (setjmp(png_jmpbuf(*png_pp))) {
#else
  if (setjmp((*png_pp)->jmpbuf)) {
#endif
    /* On error */
    png_destroy_read_struct(png_pp, info_pp, end_pp);
    return 0;
  }
  png_set_sig_bytes(*png_pp, PNG_BYTES_TO_CHECK);
  png_set_read_fn(*png_pp, NULL, png_read_data);
  png_read_info(*png_pp, *info_pp);

  return 1;
}


/*
 * return !0: It is a PNG.
 */
static int is_png(ZFILE *zf)
{
  byte png_read_buff[PNG_BYTES_TO_CHECK];

  /* Read in some of the signature bytes */
  if (zread(zf, png_read_buff,PNG_BYTES_TO_CHECK) != PNG_BYTES_TO_CHECK)
    return 0;
  return !png_sig_cmp(png_read_buff, (png_size_t)0, PNG_BYTES_TO_CHECK);
}


/*
 * Main control routine for identifying PNG without loading
 * return 1: PNG file.
 */
int pngIdent(char *fullname, char *name)
{
  png_structp png_ptr;
  png_infop info_ptr, end_info;
  png_uint_32 width, height;
  int color_type, bit_depth, interlace_type;
  double file_gamma;

  zinput_file = zopen(fullname);
  if (zinput_file == NULL) {
    zclose(zinput_file);
    return 0;
  }
  /* check at early timing */
  if (is_png(zinput_file) == 0) {
    zclose(zinput_file);
    return 0;
  }
  filename = name;

  /* read infomation header */
  if (!pngHeader(&png_ptr, &info_ptr, &end_info)) {
    zclose(zinput_file);
    return 0;
  }

#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
  if (setjmp(png_jmpbuf(png_ptr))) {
#else
  if (setjmp(png_ptr->jmpbuf)) {
#endif
    /* On error */
    freeImage(image);
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    zclose(zinput_file);
    return 0;
  }
  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
	       &color_type, &interlace_type, NULL, NULL);
  if(!png_get_gAMA( png_ptr, info_ptr, &file_gamma))
    file_gamma = -1.0;
  /* print out PNG infomation */
  pngInfo( width, height, bit_depth, color_type, interlace_type, file_gamma);

  znocache(zinput_file);
  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
  zclose(zinput_file);
  return 1;
}


/*
 * Main control routine for loading
 */
Image *pngLoad(char *fullname, char *name, unsigned int verbose)
{
  png_structp png_ptr;
  png_infop info_ptr, end_info;
  png_colorp palette;
  png_color_16p background;
  png_bytep bufp, *row_pointers;
  png_uint_32 width, height;
  int i, row_stride, color_type, bit_depth, num_palette, interlace_type;
  double file_gamma;

  zinput_file = zopen(fullname);
  if (zinput_file == NULL) {
    zclose(zinput_file);
    return NULL;
  }
  /* check at early timing */
  if (is_png(zinput_file) == 0) {
    zclose(zinput_file);
    return NULL;
  }
  filename = name;

  /* read infomation header */
  if (!pngHeader(&png_ptr, &info_ptr, &end_info)) {
    zclose(zinput_file);
    return NULL;
  }
  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
	       &color_type, &interlace_type, NULL, NULL);
  if(!png_get_gAMA( png_ptr, info_ptr, &file_gamma))
    file_gamma = -1.0;
  /* print out PNG infomation */
  if (verbose)
    pngInfo( width, height, bit_depth, color_type, interlace_type, file_gamma);
  znocache(zinput_file);
  image = NULL;

  if (file_gamma <= 0.0)
    file_gamma = 1.0;
  png_set_gamma(png_ptr, 1.0, file_gamma);
  if (bit_depth > 8)
    png_set_strip_16(png_ptr);      /* 16 bit -> 8 bit */
  /*  if (color_type & PNG_COLOR_MASK_ALPHA) */
  png_set_strip_alpha(png_ptr);
  if (png_get_bKGD(png_ptr, info_ptr, &background))
    png_set_background(png_ptr, background, file_gamma, 1, 1.0);
  switch (color_type) {
  case PNG_COLOR_TYPE_PALETTE:
    if (bit_depth < 8)
      png_set_packing(png_ptr);     /* 1 pixlel 1 byte */
    image = newRGBImage(width, height, 8);
    png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
    image->rgb.used = num_palette;
    for (i = 0; i < num_palette; i++) {
      *(image->rgb.red + i) = palette->red << 8;
      *(image->rgb.green + i) = palette->green << 8;
      *(image->rgb.blue + i) = palette->blue << 8;
      palette++;
    }
    break;
  case PNG_COLOR_TYPE_GRAY_ALPHA:
  case PNG_COLOR_TYPE_GRAY:
    if (bit_depth < 8)
#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
      png_set_expand_gray_1_2_4_to_8(png_ptr);  /* 1 pixlel 1 byte */
#else
      png_set_gray_1_2_4_to_8(png_ptr);  /* 1 pixlel 1 byte */
#endif
    image = newRGBImage(width, height, 8);
    image->rgb.used = 256;
    for (i = 0; i < 256; i++) {
      *(image->rgb.red + i) = 
	*(image->rgb.green + i) = 
	*(image->rgb.blue + i) = i << 8;
    }
    break;
  case PNG_COLOR_TYPE_RGB_ALPHA:
  case PNG_COLOR_TYPE_RGB:
    image = newTrueImage(width, height);
    break;
  default:
    fprintf(stderr, "Unknown color type PNG.");
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    zclose(zinput_file);
    return NULL;
  }
  image->title = dupString(filename);

  bufp = image->data;
  png_read_update_info(png_ptr, info_ptr);
  row_stride = png_get_rowbytes(png_ptr, info_ptr);
  row_pointers = (png_bytep *)lmalloc(sizeof(png_bytep) * height);
  for (i = 0; i < height; i++) {
    *(row_pointers + i) = bufp;
    bufp += row_stride;
  }
  png_read_image(png_ptr, row_pointers);
  lfree((byte *)row_pointers);
  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
  zclose(zinput_file);

  return image;
}

#else /* !HAVE_LIBPNG */
static int unused;
#endif /* !HAVE_LIBPNG */