~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to gdchart0.94c/gd-1.8.3/libpng-1.0.8/contrib/pngminus/png2pnm.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  png2pnm.c --- conversion from PNG-file to PGM/PPM-file
3
 
 *  copyright (C) 1999 by Willem van Schaik <willem@schaik.com>
4
 
 *
5
 
 *  version 1.0 - 1999.10.15 - First version.
6
 
 *
7
 
 *  Permission to use, copy, modify, and distribute this software and
8
 
 *  its documentation for any purpose and without fee is hereby granted,
9
 
 *  provided that the above copyright notice appear in all copies and
10
 
 *  that both that copyright notice and this permission notice appear in
11
 
 *  supporting documentation. This software is provided "as is" without
12
 
 *  express or implied warranty.
13
 
 */
14
 
 
15
 
#include <stdio.h>
16
 
#include <stdlib.h>
17
 
#ifdef __TURBOC__
18
 
#include <mem.h>
19
 
#include <fcntl.h>
20
 
#endif
21
 
 
22
 
#ifndef BOOL
23
 
#define BOOL unsigned char
24
 
#endif
25
 
#ifndef TRUE
26
 
#define TRUE (BOOL) 1
27
 
#endif
28
 
#ifndef FALSE
29
 
#define FALSE (BOOL) 0
30
 
#endif
31
 
 
32
 
#ifdef __TURBOC__
33
 
#define STDIN  0
34
 
#define STDOUT 1
35
 
#define STDERR 2
36
 
#endif
37
 
 
38
 
/* to make png2pnm verbose so we can find problems (needs to be before png.h) */
39
 
#ifndef PNG_DEBUG
40
 
#define PNG_DEBUG 0
41
 
#endif
42
 
 
43
 
#include "png.h"
44
 
 
45
 
/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
46
 
#ifndef png_jmpbuf
47
 
#  define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
48
 
#endif
49
 
 
50
 
/* function prototypes */
51
 
 
52
 
int  main (int argc, char *argv[]);
53
 
void usage ();
54
 
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file, BOOL raw, BOOL alpha);
55
 
 
56
 
/*
57
 
 *  main
58
 
 */
59
 
 
60
 
int main(int argc, char *argv[])
61
 
{
62
 
  FILE *fp_rd = stdin;
63
 
  FILE *fp_wr = stdout;
64
 
  FILE *fp_al = NULL;
65
 
  BOOL raw = TRUE;
66
 
  BOOL alpha = FALSE;
67
 
  int argi;
68
 
 
69
 
  for (argi = 1; argi < argc; argi++)
70
 
  {
71
 
    if (argv[argi][0] == '-')
72
 
    {
73
 
      switch (argv[argi][1])
74
 
      {
75
 
        case 'n':
76
 
          raw = FALSE;
77
 
          break;
78
 
        case 'r':
79
 
          raw = TRUE;
80
 
          break;
81
 
        case 'a':
82
 
          alpha = TRUE;
83
 
          argi++;
84
 
          if ((fp_al = fopen (argv[argi], "wb")) == NULL)
85
 
          {
86
 
            fprintf (stderr, "PNM2PNG\n");
87
 
            fprintf (stderr, "Error:  can not create alpha-channel file %s\n", argv[argi]);
88
 
            exit (1);
89
 
          }
90
 
          break;
91
 
        case 'h':
92
 
        case '?':
93
 
          usage();
94
 
          exit(0);
95
 
          break;
96
 
        default:
97
 
          fprintf (stderr, "PNG2PNM\n");
98
 
          fprintf (stderr, "Error:  unknown option %s\n", argv[argi]);
99
 
          usage();
100
 
          exit(1);
101
 
          break;
102
 
      } /* end switch */
103
 
    }
104
 
    else if (fp_rd == stdin)
105
 
    {
106
 
      if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
107
 
      {
108
 
             fprintf (stderr, "PNG2PNM\n");
109
 
            fprintf (stderr, "Error:  file %s does not exist\n", argv[argi]);
110
 
            exit (1);
111
 
      }
112
 
    }
113
 
    else if (fp_wr == stdout)
114
 
    {
115
 
      if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
116
 
      {
117
 
        fprintf (stderr, "PNG2PNM\n");
118
 
        fprintf (stderr, "Error:  can not create file %s\n", argv[argi]);
119
 
        exit (1);
120
 
      }
121
 
    }
122
 
    else
123
 
    {
124
 
      fprintf (stderr, "PNG2PNM\n");
125
 
      fprintf (stderr, "Error:  too many parameters\n");
126
 
      usage();
127
 
      exit(1);
128
 
    }
129
 
  } /* end for */
130
 
 
131
 
#ifdef __TURBOC__
132
 
  /* set stdin/stdout if required to binary */
133
 
  if (fp_rd == stdin)
134
 
  {
135
 
    setmode (STDIN, O_BINARY);
136
 
  }
137
 
  if ((raw) && (fp_wr == stdout))
138
 
  {
139
 
    setmode (STDOUT, O_BINARY);
140
 
  }
141
 
#endif
142
 
 
143
 
  /* call the conversion program itself */
144
 
  if (png2pnm (fp_rd, fp_wr, fp_al, raw, alpha) == FALSE)
145
 
  {
146
 
    fprintf (stderr, "PNG2PNM\n");
147
 
    fprintf (stderr, "Error:  unsuccessful convertion of PNG-image\n");
148
 
    exit(1);
149
 
  }
150
 
 
151
 
  /* close input file */
152
 
  fclose (fp_rd);
153
 
  /* close output file */
154
 
  fclose (fp_wr);
155
 
  /* close alpha file */
156
 
  if (alpha)
157
 
    fclose (fp_al);
158
 
 
159
 
  return 0;
160
 
}
161
 
 
162
 
/*
163
 
 *  usage
164
 
 */
165
 
 
166
 
void usage()
167
 
{
168
 
  fprintf (stderr, "PNG2PNM\n");
169
 
  fprintf (stderr, "   by Willem van Schaik, 1999\n");
170
 
#ifdef __TURBOC__
171
 
  fprintf (stderr, "   for Turbo-C and Borland-C compilers\n");
172
 
#else
173
 
  fprintf (stderr, "   for Linux (and Unix) compilers\n");
174
 
#endif
175
 
  fprintf (stderr, "Usage:  png2pnm [options] <file>.png [<file>.pnm]\n");
176
 
  fprintf (stderr, "   or:  ... | png2pnm [options]\n");
177
 
  fprintf (stderr, "Options:\n");
178
 
  fprintf (stderr, "   -r[aw]   write pnm-file in binary format (P4/P5/P6) (default)\n");
179
 
  fprintf (stderr, "   -n[oraw] write pnm-file in ascii format (P1/P2/P3)\n");
180
 
  fprintf (stderr, "   -a[lpha] <file>.pgm write PNG alpha channel as pgm-file\n");
181
 
  fprintf (stderr, "   -h | -?  print this help-information\n");
182
 
}
183
 
 
184
 
/*
185
 
 *  png2pnm
186
 
 */
187
 
 
188
 
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file, BOOL raw, BOOL alpha)
189
 
{
190
 
  png_struct    *png_ptr = NULL;
191
 
  png_info      *info_ptr = NULL;
192
 
  png_byte      buf[8];
193
 
  png_byte      *png_pixels = NULL;
194
 
  png_byte      **row_pointers = NULL;
195
 
  png_byte      *pix_ptr = NULL;
196
 
  png_uint_32   row_bytes;
197
 
 
198
 
  png_uint_32   width;
199
 
  png_uint_32   height;
200
 
  int           bit_depth;
201
 
  int           channels;
202
 
  int           color_type;
203
 
  int           alpha_present;
204
 
  int           row, col;
205
 
  int           ret;
206
 
  int           i;
207
 
 
208
 
  /* read and check signature in PNG file */
209
 
  ret = fread (buf, 1, 8, png_file);
210
 
  if (ret != 8)
211
 
    return FALSE;
212
 
 
213
 
  ret = png_check_sig (buf, 8);
214
 
  if (!ret)
215
 
    return FALSE;
216
 
 
217
 
  /* create png and info structures */
218
 
 
219
 
  png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
220
 
    NULL, NULL, NULL);
221
 
  if (!png_ptr)
222
 
    return FALSE;   /* out of memory */
223
 
 
224
 
  info_ptr = png_create_info_struct (png_ptr);
225
 
  if (!info_ptr)
226
 
  {
227
 
    png_destroy_read_struct (&png_ptr, NULL, NULL);
228
 
    return FALSE;   /* out of memory */
229
 
  }
230
 
 
231
 
  if (setjmp (png_jmpbuf(png_ptr)))
232
 
  {
233
 
    png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
234
 
    return FALSE;
235
 
  }
236
 
 
237
 
  /* set up the input control for C streams */
238
 
  png_init_io (png_ptr, png_file);
239
 
  png_set_sig_bytes (png_ptr, 8);  /* we already read the 8 signature bytes */
240
 
 
241
 
  /* read the file information */
242
 
  png_read_info (png_ptr, info_ptr);
243
 
 
244
 
  /* get size and bit-depth of the PNG-image */
245
 
  png_get_IHDR (png_ptr, info_ptr,
246
 
    &width, &height, &bit_depth, &color_type,
247
 
    NULL, NULL, NULL);
248
 
 
249
 
  /* set-up the transformations */
250
 
 
251
 
  /* transform paletted images into full-color rgb */
252
 
  if (color_type == PNG_COLOR_TYPE_PALETTE)
253
 
    png_set_expand (png_ptr);
254
 
  /* expand images to bit-depth 8 (only applicable for grayscale images) */
255
 
  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
256
 
    png_set_expand (png_ptr);
257
 
  /* transform transparency maps into full alpha-channel */
258
 
  if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
259
 
    png_set_expand (png_ptr);
260
 
 
261
 
#ifdef NJET
262
 
  /* downgrade 16-bit images to 8 bit */
263
 
  if (bit_depth == 16)
264
 
    png_set_strip_16 (png_ptr);
265
 
  /* transform grayscale images into full-color */
266
 
  if (color_type == PNG_COLOR_TYPE_GRAY ||
267
 
    color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
268
 
    png_set_gray_to_rgb (png_ptr);
269
 
  /* only if file has a file gamma, we do a correction */
270
 
  if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
271
 
    png_set_gamma (png_ptr, (double) 2.2, file_gamma);
272
 
#endif
273
 
 
274
 
  /* all transformations have been registered; now update info_ptr data,
275
 
   * get rowbytes and channels, and allocate image memory */
276
 
 
277
 
  png_read_update_info (png_ptr, info_ptr);
278
 
 
279
 
  /* get the new color-type and bit-depth (after expansion/stripping) */
280
 
  png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
281
 
    NULL, NULL, NULL);
282
 
 
283
 
  /* check for 16-bit files */
284
 
  if (bit_depth == 16)
285
 
  {
286
 
    raw = FALSE;
287
 
#ifdef __TURBOC__
288
 
    pnm_file->flags &= ~((unsigned) _F_BIN);
289
 
#endif
290
 
  }
291
 
 
292
 
  /* calculate new number of channels and store alpha-presence */
293
 
  if (color_type == PNG_COLOR_TYPE_GRAY)
294
 
    channels = 1;
295
 
  else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
296
 
    channels = 2;
297
 
  else if (color_type == PNG_COLOR_TYPE_RGB)
298
 
    channels = 3;
299
 
  else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
300
 
    channels = 4;
301
 
  else
302
 
    channels = 0; /* should never happen */
303
 
  alpha_present = (channels - 1) % 2;
304
 
 
305
 
  /* check if alpha is expected to be present in file */
306
 
  if (alpha && !alpha_present)
307
 
  {
308
 
    fprintf (stderr, "PNG2PNM\n");
309
 
    fprintf (stderr, "Error:  PNG-file doesn't contain alpha channel\n");
310
 
    exit (1);
311
 
  }
312
 
 
313
 
  /* row_bytes is the width x number of channels x (bit-depth / 8) */
314
 
  row_bytes = png_get_rowbytes (png_ptr, info_ptr);
315
 
 
316
 
  if ((png_pixels = (png_byte *) malloc (row_bytes * height * sizeof (png_byte))) == NULL) {
317
 
    png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
318
 
    return FALSE;
319
 
  }
320
 
 
321
 
  if ((row_pointers = (png_byte **) malloc (height * sizeof (png_bytep))) == NULL)
322
 
  {
323
 
    png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
324
 
    free (png_pixels);
325
 
    png_pixels = NULL;
326
 
    return FALSE;
327
 
  }
328
 
 
329
 
  /* set the individual row_pointers to point at the correct offsets */
330
 
  for (i = 0; i < (height); i++)
331
 
    row_pointers[i] = png_pixels + i * row_bytes;
332
 
 
333
 
  /* now we can go ahead and just read the whole image */
334
 
  png_read_image (png_ptr, row_pointers);
335
 
 
336
 
  /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
337
 
  png_read_end (png_ptr, info_ptr);
338
 
 
339
 
  /* clean up after the read, and free any memory allocated - REQUIRED */
340
 
  png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
341
 
 
342
 
  /* write header of PNM file */
343
 
 
344
 
  if ((color_type == PNG_COLOR_TYPE_GRAY) ||
345
 
      (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
346
 
  {
347
 
    fprintf (pnm_file, "%s\n", (raw) ? "P5" : "P2");
348
 
    fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
349
 
    fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
350
 
  }
351
 
  else if ((color_type == PNG_COLOR_TYPE_RGB) ||
352
 
           (color_type == PNG_COLOR_TYPE_RGB_ALPHA))
353
 
  {
354
 
    fprintf (pnm_file, "%s\n", (raw) ? "P6" : "P3");
355
 
    fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
356
 
    fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
357
 
  }
358
 
 
359
 
  /* write header of PGM file with alpha channel */
360
 
 
361
 
  if ((alpha) &&
362
 
      ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
363
 
       (color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
364
 
  {
365
 
    fprintf (alpha_file, "%s\n", (raw) ? "P5" : "P2");
366
 
    fprintf (alpha_file, "%d %d\n", (int) width, (int) height);
367
 
    fprintf (alpha_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
368
 
  }
369
 
 
370
 
  /* write data to PNM file */
371
 
  pix_ptr = png_pixels;
372
 
 
373
 
  for (row = 0; row < height; row++)
374
 
  {
375
 
    for (col = 0; col < width; col++)
376
 
    {
377
 
      for (i = 0; i < (channels - alpha_present); i++)
378
 
      {
379
 
        if (raw)
380
 
          fputc ((int) *pix_ptr++ , pnm_file);
381
 
        else
382
 
          if (bit_depth == 16)
383
 
            fprintf (pnm_file, "%ld ", ((long) *pix_ptr++ << 8) + (long) *pix_ptr++);
384
 
          else
385
 
            fprintf (pnm_file, "%ld ", (long) *pix_ptr++);
386
 
      }
387
 
      if (alpha_present)
388
 
      {
389
 
        if (!alpha)
390
 
        {
391
 
          pix_ptr++; /* alpha */
392
 
          if (bit_depth == 16)
393
 
            pix_ptr++;
394
 
        }
395
 
        else /* output alpha-channel as pgm file */
396
 
        {
397
 
          if (raw)
398
 
            fputc ((int) *pix_ptr++ , alpha_file);
399
 
          else
400
 
            if (bit_depth == 16)
401
 
              fprintf (alpha_file, "%ld ", ((long) *pix_ptr++ << 8) + (long) *pix_ptr++);
402
 
            else
403
 
              fprintf (alpha_file, "%ld ", (long) *pix_ptr++);
404
 
        }
405
 
      } /* if alpha_present */
406
 
 
407
 
      if (!raw)
408
 
        if (col % 4 == 3)
409
 
          fprintf (pnm_file, "\n");
410
 
    } /* end for col */
411
 
 
412
 
    if (!raw)
413
 
      if (col % 4 != 0)
414
 
        fprintf (pnm_file, "\n");
415
 
  } /* end for row */
416
 
 
417
 
  if (row_pointers != (unsigned char**) NULL)
418
 
    free (row_pointers);
419
 
  if (png_pixels != (unsigned char*) NULL)
420
 
    free (png_pixels);
421
 
 
422
 
  return TRUE;
423
 
 
424
 
} /* end of source */
425