aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/pngminus/pnm2png.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/pngminus/pnm2png.c')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/pngminus/pnm2png.c1066
1 files changed, 533 insertions, 533 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/pngminus/pnm2png.c b/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/pngminus/pnm2png.c
index 2f321cc..4cdfad8 100644
--- a/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/pngminus/pnm2png.c
+++ b/libraries/irrlicht-1.8/source/Irrlicht/libpng/contrib/pngminus/pnm2png.c
@@ -1,533 +1,533 @@
1/* 1/*
2 * pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file 2 * pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
3 * copyright (C) 1999 by Willem van Schaik <willem@schaik.com> 3 * copyright (C) 1999 by Willem van Schaik <willem@schaik.com>
4 * 4 *
5 * version 1.0 - 1999.10.15 - First version. 5 * version 1.0 - 1999.10.15 - First version.
6 * 6 *
7 * Permission to use, copy, modify, and distribute this software and 7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby granted, 8 * its documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and 9 * provided that the above copyright notice appear in all copies and
10 * that both that copyright notice and this permission notice appear in 10 * that both that copyright notice and this permission notice appear in
11 * supporting documentation. This software is provided "as is" without 11 * supporting documentation. This software is provided "as is" without
12 * express or implied warranty. 12 * express or implied warranty.
13 */ 13 */
14 14
15#include <stdio.h> 15#include <stdio.h>
16#include <stdlib.h> 16#include <stdlib.h>
17#ifdef __TURBOC__ 17#ifdef __TURBOC__
18#include <mem.h> 18#include <mem.h>
19#include <fcntl.h> 19#include <fcntl.h>
20#endif 20#endif
21 21
22#ifndef BOOL 22#ifndef BOOL
23#define BOOL unsigned char 23#define BOOL unsigned char
24#endif 24#endif
25#ifndef TRUE 25#ifndef TRUE
26#define TRUE (BOOL) 1 26#define TRUE (BOOL) 1
27#endif 27#endif
28#ifndef FALSE 28#ifndef FALSE
29#define FALSE (BOOL) 0 29#define FALSE (BOOL) 0
30#endif 30#endif
31 31
32#define STDIN 0 32#define STDIN 0
33#define STDOUT 1 33#define STDOUT 1
34#define STDERR 2 34#define STDERR 2
35 35
36/* to make pnm2png verbose so we can find problems (needs to be before png.h) */ 36/* to make pnm2png verbose so we can find problems (needs to be before png.h) */
37#ifndef PNG_DEBUG 37#ifndef PNG_DEBUG
38#define PNG_DEBUG 0 38#define PNG_DEBUG 0
39#endif 39#endif
40 40
41#include "png.h" 41#include "png.h"
42 42
43/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */ 43/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
44#ifndef png_jmpbuf 44#ifndef png_jmpbuf
45# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) 45# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
46#endif 46#endif
47 47
48/* function prototypes */ 48/* function prototypes */
49 49
50int main (int argc, char *argv[]); 50int main (int argc, char *argv[]);
51void usage (); 51void usage ();
52BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha); 52BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha);
53void get_token(FILE *pnm_file, char *token); 53void get_token(FILE *pnm_file, char *token);
54png_uint_32 get_data (FILE *pnm_file, int depth); 54png_uint_32 get_data (FILE *pnm_file, int depth);
55png_uint_32 get_value (FILE *pnm_file, int depth); 55png_uint_32 get_value (FILE *pnm_file, int depth);
56 56
57/* 57/*
58 * main 58 * main
59 */ 59 */
60 60
61int main(int argc, char *argv[]) 61int main(int argc, char *argv[])
62{ 62{
63 FILE *fp_rd = stdin; 63 FILE *fp_rd = stdin;
64 FILE *fp_al = NULL; 64 FILE *fp_al = NULL;
65 FILE *fp_wr = stdout; 65 FILE *fp_wr = stdout;
66 BOOL interlace = FALSE; 66 BOOL interlace = FALSE;
67 BOOL alpha = FALSE; 67 BOOL alpha = FALSE;
68 int argi; 68 int argi;
69 69
70 for (argi = 1; argi < argc; argi++) 70 for (argi = 1; argi < argc; argi++)
71 { 71 {
72 if (argv[argi][0] == '-') 72 if (argv[argi][0] == '-')
73 { 73 {
74 switch (argv[argi][1]) 74 switch (argv[argi][1])
75 { 75 {
76 case 'i': 76 case 'i':
77 interlace = TRUE; 77 interlace = TRUE;
78 break; 78 break;
79 case 'a': 79 case 'a':
80 alpha = TRUE; 80 alpha = TRUE;
81 argi++; 81 argi++;
82 if ((fp_al = fopen (argv[argi], "rb")) == NULL) 82 if ((fp_al = fopen (argv[argi], "rb")) == NULL)
83 { 83 {
84 fprintf (stderr, "PNM2PNG\n"); 84 fprintf (stderr, "PNM2PNG\n");
85 fprintf (stderr, "Error: alpha-channel file %s does not exist\n", 85 fprintf (stderr, "Error: alpha-channel file %s does not exist\n",
86 argv[argi]); 86 argv[argi]);
87 exit (1); 87 exit (1);
88 } 88 }
89 break; 89 break;
90 case 'h': 90 case 'h':
91 case '?': 91 case '?':
92 usage(); 92 usage();
93 exit(0); 93 exit(0);
94 break; 94 break;
95 default: 95 default:
96 fprintf (stderr, "PNM2PNG\n"); 96 fprintf (stderr, "PNM2PNG\n");
97 fprintf (stderr, "Error: unknown option %s\n", argv[argi]); 97 fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
98 usage(); 98 usage();
99 exit(1); 99 exit(1);
100 break; 100 break;
101 } /* end switch */ 101 } /* end switch */
102 } 102 }
103 else if (fp_rd == stdin) 103 else if (fp_rd == stdin)
104 { 104 {
105 if ((fp_rd = fopen (argv[argi], "rb")) == NULL) 105 if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
106 { 106 {
107 fprintf (stderr, "PNM2PNG\n"); 107 fprintf (stderr, "PNM2PNG\n");
108 fprintf (stderr, "Error: file %s does not exist\n", argv[argi]); 108 fprintf (stderr, "Error: file %s does not exist\n", argv[argi]);
109 exit (1); 109 exit (1);
110 } 110 }
111 } 111 }
112 else if (fp_wr == stdout) 112 else if (fp_wr == stdout)
113 { 113 {
114 if ((fp_wr = fopen (argv[argi], "wb")) == NULL) 114 if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
115 { 115 {
116 fprintf (stderr, "PNM2PNG\n"); 116 fprintf (stderr, "PNM2PNG\n");
117 fprintf (stderr, "Error: can not create PNG-file %s\n", argv[argi]); 117 fprintf (stderr, "Error: can not create PNG-file %s\n", argv[argi]);
118 exit (1); 118 exit (1);
119 } 119 }
120 } 120 }
121 else 121 else
122 { 122 {
123 fprintf (stderr, "PNM2PNG\n"); 123 fprintf (stderr, "PNM2PNG\n");
124 fprintf (stderr, "Error: too many parameters\n"); 124 fprintf (stderr, "Error: too many parameters\n");
125 usage(); 125 usage();
126 exit (1); 126 exit (1);
127 } 127 }
128 } /* end for */ 128 } /* end for */
129 129
130#ifdef __TURBOC__ 130#ifdef __TURBOC__
131 /* set stdin/stdout to binary, we're reading the PNM always! in binary format */ 131 /* set stdin/stdout to binary, we're reading the PNM always! in binary format */
132 if (fp_rd == stdin) 132 if (fp_rd == stdin)
133 { 133 {
134 setmode (STDIN, O_BINARY); 134 setmode (STDIN, O_BINARY);
135 } 135 }
136 if (fp_wr == stdout) 136 if (fp_wr == stdout)
137 { 137 {
138 setmode (STDOUT, O_BINARY); 138 setmode (STDOUT, O_BINARY);
139 } 139 }
140#endif 140#endif
141 141
142 /* call the conversion program itself */ 142 /* call the conversion program itself */
143 if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE) 143 if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
144 { 144 {
145 fprintf (stderr, "PNM2PNG\n"); 145 fprintf (stderr, "PNM2PNG\n");
146 fprintf (stderr, "Error: unsuccessful converting to PNG-image\n"); 146 fprintf (stderr, "Error: unsuccessful converting to PNG-image\n");
147 exit (1); 147 exit (1);
148 } 148 }
149 149
150 /* close input file */ 150 /* close input file */
151 fclose (fp_rd); 151 fclose (fp_rd);
152 /* close output file */ 152 /* close output file */
153 fclose (fp_wr); 153 fclose (fp_wr);
154 /* close alpha file */ 154 /* close alpha file */
155 if (alpha) 155 if (alpha)
156 fclose (fp_al); 156 fclose (fp_al);
157 157
158 return 0; 158 return 0;
159} 159}
160 160
161/* 161/*
162 * usage 162 * usage
163 */ 163 */
164 164
165void usage() 165void usage()
166{ 166{
167 fprintf (stderr, "PNM2PNG\n"); 167 fprintf (stderr, "PNM2PNG\n");
168 fprintf (stderr, " by Willem van Schaik, 1999\n"); 168 fprintf (stderr, " by Willem van Schaik, 1999\n");
169#ifdef __TURBOC__ 169#ifdef __TURBOC__
170 fprintf (stderr, " for Turbo-C and Borland-C compilers\n"); 170 fprintf (stderr, " for Turbo-C and Borland-C compilers\n");
171#else 171#else
172 fprintf (stderr, " for Linux (and Unix) compilers\n"); 172 fprintf (stderr, " for Linux (and Unix) compilers\n");
173#endif 173#endif
174 fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n"); 174 fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
175 fprintf (stderr, " or: ... | pnm2png [options]\n"); 175 fprintf (stderr, " or: ... | pnm2png [options]\n");
176 fprintf (stderr, "Options:\n"); 176 fprintf (stderr, "Options:\n");
177 fprintf (stderr, " -i[nterlace] write png-file with interlacing on\n"); 177 fprintf (stderr, " -i[nterlace] write png-file with interlacing on\n");
178 fprintf (stderr, " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n"); 178 fprintf (stderr, " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
179 fprintf (stderr, " -h | -? print this help-information\n"); 179 fprintf (stderr, " -h | -? print this help-information\n");
180} 180}
181 181
182/* 182/*
183 * pnm2png 183 * pnm2png
184 */ 184 */
185 185
186BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha) 186BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha)
187{ 187{
188 png_struct *png_ptr = NULL; 188 png_struct *png_ptr = NULL;
189 png_info *info_ptr = NULL; 189 png_info *info_ptr = NULL;
190 png_byte *png_pixels = NULL; 190 png_byte *png_pixels = NULL;
191 png_byte **row_pointers = NULL; 191 png_byte **row_pointers = NULL;
192 png_byte *pix_ptr = NULL; 192 png_byte *pix_ptr = NULL;
193 png_uint_32 row_bytes; 193 png_uint_32 row_bytes;
194 194
195 char type_token[16]; 195 char type_token[16];
196 char width_token[16]; 196 char width_token[16];
197 char height_token[16]; 197 char height_token[16];
198 char maxval_token[16]; 198 char maxval_token[16];
199 int color_type; 199 int color_type;
200 png_uint_32 width, alpha_width; 200 png_uint_32 width, alpha_width;
201 png_uint_32 height, alpha_height; 201 png_uint_32 height, alpha_height;
202 png_uint_32 maxval; 202 png_uint_32 maxval;
203 int bit_depth = 0; 203 int bit_depth = 0;
204 int channels; 204 int channels;
205 int alpha_depth = 0; 205 int alpha_depth = 0;
206 int alpha_present; 206 int alpha_present;
207 int row, col; 207 int row, col;
208 BOOL raw, alpha_raw = FALSE; 208 BOOL raw, alpha_raw = FALSE;
209 png_uint_32 tmp16; 209 png_uint_32 tmp16;
210 int i; 210 int i;
211 211
212 /* read header of PNM file */ 212 /* read header of PNM file */
213 213
214 get_token(pnm_file, type_token); 214 get_token(pnm_file, type_token);
215 if (type_token[0] != 'P') 215 if (type_token[0] != 'P')
216 { 216 {
217 return FALSE; 217 return FALSE;
218 } 218 }
219 else if ((type_token[1] == '1') || (type_token[1] == '4')) 219 else if ((type_token[1] == '1') || (type_token[1] == '4'))
220 { 220 {
221 raw = (type_token[1] == '4'); 221 raw = (type_token[1] == '4');
222 color_type = PNG_COLOR_TYPE_GRAY; 222 color_type = PNG_COLOR_TYPE_GRAY;
223 bit_depth = 1; 223 bit_depth = 1;
224 } 224 }
225 else if ((type_token[1] == '2') || (type_token[1] == '5')) 225 else if ((type_token[1] == '2') || (type_token[1] == '5'))
226 { 226 {
227 raw = (type_token[1] == '5'); 227 raw = (type_token[1] == '5');
228 color_type = PNG_COLOR_TYPE_GRAY; 228 color_type = PNG_COLOR_TYPE_GRAY;
229 get_token(pnm_file, width_token); 229 get_token(pnm_file, width_token);
230 sscanf (width_token, "%lu", &width); 230 sscanf (width_token, "%lu", &width);
231 get_token(pnm_file, height_token); 231 get_token(pnm_file, height_token);
232 sscanf (height_token, "%lu", &height); 232 sscanf (height_token, "%lu", &height);
233 get_token(pnm_file, maxval_token); 233 get_token(pnm_file, maxval_token);
234 sscanf (maxval_token, "%lu", &maxval); 234 sscanf (maxval_token, "%lu", &maxval);
235 if (maxval <= 1) 235 if (maxval <= 1)
236 bit_depth = 1; 236 bit_depth = 1;
237 else if (maxval <= 3) 237 else if (maxval <= 3)
238 bit_depth = 2; 238 bit_depth = 2;
239 else if (maxval <= 15) 239 else if (maxval <= 15)
240 bit_depth = 4; 240 bit_depth = 4;
241 else if (maxval <= 255) 241 else if (maxval <= 255)
242 bit_depth = 8; 242 bit_depth = 8;
243 else /* if (maxval <= 65535) */ 243 else /* if (maxval <= 65535) */
244 bit_depth = 16; 244 bit_depth = 16;
245 } 245 }
246 else if ((type_token[1] == '3') || (type_token[1] == '6')) 246 else if ((type_token[1] == '3') || (type_token[1] == '6'))
247 { 247 {
248 raw = (type_token[1] == '6'); 248 raw = (type_token[1] == '6');
249 color_type = PNG_COLOR_TYPE_RGB; 249 color_type = PNG_COLOR_TYPE_RGB;
250 get_token(pnm_file, width_token); 250 get_token(pnm_file, width_token);
251 sscanf (width_token, "%lu", &width); 251 sscanf (width_token, "%lu", &width);
252 get_token(pnm_file, height_token); 252 get_token(pnm_file, height_token);
253 sscanf (height_token, "%lu", &height); 253 sscanf (height_token, "%lu", &height);
254 get_token(pnm_file, maxval_token); 254 get_token(pnm_file, maxval_token);
255 sscanf (maxval_token, "%lu", &maxval); 255 sscanf (maxval_token, "%lu", &maxval);
256 if (maxval <= 1) 256 if (maxval <= 1)
257 bit_depth = 1; 257 bit_depth = 1;
258 else if (maxval <= 3) 258 else if (maxval <= 3)
259 bit_depth = 2; 259 bit_depth = 2;
260 else if (maxval <= 15) 260 else if (maxval <= 15)
261 bit_depth = 4; 261 bit_depth = 4;
262 else if (maxval <= 255) 262 else if (maxval <= 255)
263 bit_depth = 8; 263 bit_depth = 8;
264 else /* if (maxval <= 65535) */ 264 else /* if (maxval <= 65535) */
265 bit_depth = 16; 265 bit_depth = 16;
266 } 266 }
267 else 267 else
268 { 268 {
269 return FALSE; 269 return FALSE;
270 } 270 }
271 271
272 /* read header of PGM file with alpha channel */ 272 /* read header of PGM file with alpha channel */
273 273
274 if (alpha) 274 if (alpha)
275 { 275 {
276 if (color_type == PNG_COLOR_TYPE_GRAY) 276 if (color_type == PNG_COLOR_TYPE_GRAY)
277 color_type = PNG_COLOR_TYPE_GRAY_ALPHA; 277 color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
278 if (color_type == PNG_COLOR_TYPE_RGB) 278 if (color_type == PNG_COLOR_TYPE_RGB)
279 color_type = PNG_COLOR_TYPE_RGB_ALPHA; 279 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
280 280
281 get_token(alpha_file, type_token); 281 get_token(alpha_file, type_token);
282 if (type_token[0] != 'P') 282 if (type_token[0] != 'P')
283 { 283 {
284 return FALSE; 284 return FALSE;
285 } 285 }
286 else if ((type_token[1] == '2') || (type_token[1] == '5')) 286 else if ((type_token[1] == '2') || (type_token[1] == '5'))
287 { 287 {
288 alpha_raw = (type_token[1] == '5'); 288 alpha_raw = (type_token[1] == '5');
289 get_token(alpha_file, width_token); 289 get_token(alpha_file, width_token);
290 sscanf (width_token, "%lu", &alpha_width); 290 sscanf (width_token, "%lu", &alpha_width);
291 if (alpha_width != width) 291 if (alpha_width != width)
292 return FALSE; 292 return FALSE;
293 get_token(alpha_file, height_token); 293 get_token(alpha_file, height_token);
294 sscanf (height_token, "%lu", &alpha_height); 294 sscanf (height_token, "%lu", &alpha_height);
295 if (alpha_height != height) 295 if (alpha_height != height)
296 return FALSE; 296 return FALSE;
297 get_token(alpha_file, maxval_token); 297 get_token(alpha_file, maxval_token);
298 sscanf (maxval_token, "%lu", &maxval); 298 sscanf (maxval_token, "%lu", &maxval);
299 if (maxval <= 1) 299 if (maxval <= 1)
300 alpha_depth = 1; 300 alpha_depth = 1;
301 else if (maxval <= 3) 301 else if (maxval <= 3)
302 alpha_depth = 2; 302 alpha_depth = 2;
303 else if (maxval <= 15) 303 else if (maxval <= 15)
304 alpha_depth = 4; 304 alpha_depth = 4;
305 else if (maxval <= 255) 305 else if (maxval <= 255)
306 alpha_depth = 8; 306 alpha_depth = 8;
307 else /* if (maxval <= 65535) */ 307 else /* if (maxval <= 65535) */
308 alpha_depth = 16; 308 alpha_depth = 16;
309 if (alpha_depth != bit_depth) 309 if (alpha_depth != bit_depth)
310 return FALSE; 310 return FALSE;
311 } 311 }
312 else 312 else
313 { 313 {
314 return FALSE; 314 return FALSE;
315 } 315 }
316 } /* end if alpha */ 316 } /* end if alpha */
317 317
318 /* calculate the number of channels and store alpha-presence */ 318 /* calculate the number of channels and store alpha-presence */
319 if (color_type == PNG_COLOR_TYPE_GRAY) 319 if (color_type == PNG_COLOR_TYPE_GRAY)
320 channels = 1; 320 channels = 1;
321 else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) 321 else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
322 channels = 2; 322 channels = 2;
323 else if (color_type == PNG_COLOR_TYPE_RGB) 323 else if (color_type == PNG_COLOR_TYPE_RGB)
324 channels = 3; 324 channels = 3;
325 else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) 325 else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
326 channels = 4; 326 channels = 4;
327 else 327 else
328 channels = 0; /* should not happen */ 328 channels = 0; /* should not happen */
329 329
330 alpha_present = (channels - 1) % 2; 330 alpha_present = (channels - 1) % 2;
331 331
332 /* row_bytes is the width x number of channels x (bit-depth / 8) */ 332 /* row_bytes is the width x number of channels x (bit-depth / 8) */
333 row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2); 333 row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
334 334
335 if ((png_pixels = (png_byte *) malloc (row_bytes * height * sizeof (png_byte))) == NULL) 335 if ((png_pixels = (png_byte *) malloc (row_bytes * height * sizeof (png_byte))) == NULL)
336 return FALSE; 336 return FALSE;
337 337
338 /* read data from PNM file */ 338 /* read data from PNM file */
339 pix_ptr = png_pixels; 339 pix_ptr = png_pixels;
340 340
341 for (row = 0; row < height; row++) 341 for (row = 0; row < height; row++)
342 { 342 {
343 for (col = 0; col < width; col++) 343 for (col = 0; col < width; col++)
344 { 344 {
345 for (i = 0; i < (channels - alpha_present); i++) 345 for (i = 0; i < (channels - alpha_present); i++)
346 { 346 {
347 if (raw) 347 if (raw)
348 *pix_ptr++ = get_data (pnm_file, bit_depth); 348 *pix_ptr++ = get_data (pnm_file, bit_depth);
349 else 349 else
350 if (bit_depth <= 8) 350 if (bit_depth <= 8)
351 *pix_ptr++ = get_value (pnm_file, bit_depth); 351 *pix_ptr++ = get_value (pnm_file, bit_depth);
352 else 352 else
353 { 353 {
354 tmp16 = get_value (pnm_file, bit_depth); 354 tmp16 = get_value (pnm_file, bit_depth);
355 *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF); 355 *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
356 pix_ptr++; 356 pix_ptr++;
357 *pix_ptr = (png_byte) (tmp16 & 0xFF); 357 *pix_ptr = (png_byte) (tmp16 & 0xFF);
358 pix_ptr++; 358 pix_ptr++;
359 } 359 }
360 } 360 }
361 361
362 if (alpha) /* read alpha-channel from pgm file */ 362 if (alpha) /* read alpha-channel from pgm file */
363 { 363 {
364 if (alpha_raw) 364 if (alpha_raw)
365 *pix_ptr++ = get_data (alpha_file, alpha_depth); 365 *pix_ptr++ = get_data (alpha_file, alpha_depth);
366 else 366 else
367 if (alpha_depth <= 8) 367 if (alpha_depth <= 8)
368 *pix_ptr++ = get_value (alpha_file, bit_depth); 368 *pix_ptr++ = get_value (alpha_file, bit_depth);
369 else 369 else
370 { 370 {
371 tmp16 = get_value (alpha_file, bit_depth); 371 tmp16 = get_value (alpha_file, bit_depth);
372 *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF); 372 *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
373 *pix_ptr++ = (png_byte) (tmp16 & 0xFF); 373 *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
374 } 374 }
375 } /* if alpha */ 375 } /* if alpha */
376 376
377 } /* end for col */ 377 } /* end for col */
378 } /* end for row */ 378 } /* end for row */
379 379
380 /* prepare the standard PNG structures */ 380 /* prepare the standard PNG structures */
381 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 381 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
382 if (!png_ptr) 382 if (!png_ptr)
383 { 383 {
384 return FALSE; 384 return FALSE;
385 } 385 }
386 info_ptr = png_create_info_struct (png_ptr); 386 info_ptr = png_create_info_struct (png_ptr);
387 if (!info_ptr) 387 if (!info_ptr)
388 { 388 {
389 png_destroy_write_struct (&png_ptr, (png_infopp) NULL); 389 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
390 return FALSE; 390 return FALSE;
391 } 391 }
392 392
393 /* setjmp() must be called in every function that calls a PNG-reading libpng function */ 393 /* setjmp() must be called in every function that calls a PNG-reading libpng function */
394 if (setjmp (png_jmpbuf(png_ptr))) 394 if (setjmp (png_jmpbuf(png_ptr)))
395 { 395 {
396 png_destroy_write_struct (&png_ptr, (png_infopp) NULL); 396 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
397 return FALSE; 397 return FALSE;
398 } 398 }
399 399
400 /* initialize the png structure */ 400 /* initialize the png structure */
401 png_init_io (png_ptr, png_file); 401 png_init_io (png_ptr, png_file);
402 402
403 /* we're going to write more or less the same PNG as the input file */ 403 /* we're going to write more or less the same PNG as the input file */
404 png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type, 404 png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
405 (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7, 405 (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
406 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 406 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
407 407
408 /* write the file header information */ 408 /* write the file header information */
409 png_write_info (png_ptr, info_ptr); 409 png_write_info (png_ptr, info_ptr);
410 410
411 /* if needed we will allocate memory for an new array of row-pointers */ 411 /* if needed we will allocate memory for an new array of row-pointers */
412 if (row_pointers == (unsigned char**) NULL) 412 if (row_pointers == (unsigned char**) NULL)
413 { 413 {
414 if ((row_pointers = (png_byte **) malloc (height * sizeof (png_bytep))) == NULL) 414 if ((row_pointers = (png_byte **) malloc (height * sizeof (png_bytep))) == NULL)
415 { 415 {
416 png_destroy_write_struct (&png_ptr, (png_infopp) NULL); 416 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
417 return FALSE; 417 return FALSE;
418 } 418 }
419 } 419 }
420 420
421 /* set the individual row_pointers to point at the correct offsets */ 421 /* set the individual row_pointers to point at the correct offsets */
422 for (i = 0; i < (height); i++) 422 for (i = 0; i < (height); i++)
423 row_pointers[i] = png_pixels + i * row_bytes; 423 row_pointers[i] = png_pixels + i * row_bytes;
424 424
425 /* write out the entire image data in one call */ 425 /* write out the entire image data in one call */
426 png_write_image (png_ptr, row_pointers); 426 png_write_image (png_ptr, row_pointers);
427 427
428 /* write the additional chuncks to the PNG file (not really needed) */ 428 /* write the additional chuncks to the PNG file (not really needed) */
429 png_write_end (png_ptr, info_ptr); 429 png_write_end (png_ptr, info_ptr);
430 430
431 /* clean up after the write, and free any memory allocated */ 431 /* clean up after the write, and free any memory allocated */
432 png_destroy_write_struct (&png_ptr, (png_infopp) NULL); 432 png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
433 433
434 if (row_pointers != (unsigned char**) NULL) 434 if (row_pointers != (unsigned char**) NULL)
435 free (row_pointers); 435 free (row_pointers);
436 if (png_pixels != (unsigned char*) NULL) 436 if (png_pixels != (unsigned char*) NULL)
437 free (png_pixels); 437 free (png_pixels);
438 438
439 return TRUE; 439 return TRUE;
440} /* end of pnm2png */ 440} /* end of pnm2png */
441 441
442/* 442/*
443 * get_token() - gets the first string after whitespace 443 * get_token() - gets the first string after whitespace
444 */ 444 */
445 445
446void get_token(FILE *pnm_file, char *token) 446void get_token(FILE *pnm_file, char *token)
447{ 447{
448 int i = 0; 448 int i = 0;
449 449
450 /* remove white-space */ 450 /* remove white-space */
451 do 451 do
452 { 452 {
453 token[i] = (unsigned char) fgetc (pnm_file); 453 token[i] = (unsigned char) fgetc (pnm_file);
454 } 454 }
455 while ((token[i] == '\n') || (token[i] == '\r') || (token[i] == ' ')); 455 while ((token[i] == '\n') || (token[i] == '\r') || (token[i] == ' '));
456 456
457 /* read string */ 457 /* read string */
458 do 458 do
459 { 459 {
460 i++; 460 i++;
461 token[i] = (unsigned char) fgetc (pnm_file); 461 token[i] = (unsigned char) fgetc (pnm_file);
462 } 462 }
463 while ((token[i] != '\n') && (token[i] != '\r') && (token[i] != ' ')); 463 while ((token[i] != '\n') && (token[i] != '\r') && (token[i] != ' '));
464 464
465 token[i] = '\0'; 465 token[i] = '\0';
466 466
467 return; 467 return;
468} 468}
469 469
470/* 470/*
471 * get_data() - takes first byte and converts into next pixel value, 471 * get_data() - takes first byte and converts into next pixel value,
472 * taking as much bits as defined by bit-depth and 472 * taking as much bits as defined by bit-depth and
473 * using the bit-depth to fill up a byte (0Ah -> AAh) 473 * using the bit-depth to fill up a byte (0Ah -> AAh)
474 */ 474 */
475 475
476png_uint_32 get_data (FILE *pnm_file, int depth) 476png_uint_32 get_data (FILE *pnm_file, int depth)
477{ 477{
478 static int bits_left = 0; 478 static int bits_left = 0;
479 static int old_value = 0; 479 static int old_value = 0;
480 static int mask = 0; 480 static int mask = 0;
481 int i; 481 int i;
482 png_uint_32 ret_value; 482 png_uint_32 ret_value;
483 483
484 if (mask == 0) 484 if (mask == 0)
485 for (i = 0; i < depth; i++) 485 for (i = 0; i < depth; i++)
486 mask = (mask >> 1) | 0x80; 486 mask = (mask >> 1) | 0x80;
487 487
488 if (bits_left <= 0) 488 if (bits_left <= 0)
489 { 489 {
490 old_value = fgetc (pnm_file); 490 old_value = fgetc (pnm_file);
491 bits_left = 8; 491 bits_left = 8;
492 } 492 }
493 493
494 ret_value = old_value & mask; 494 ret_value = old_value & mask;
495 for (i = 1; i < (8 / depth); i++) 495 for (i = 1; i < (8 / depth); i++)
496 ret_value = ret_value || (ret_value >> depth); 496 ret_value = ret_value || (ret_value >> depth);
497 497
498 old_value = (old_value << depth) & 0xFF; 498 old_value = (old_value << depth) & 0xFF;
499 bits_left -= depth; 499 bits_left -= depth;
500 500
501 return ret_value; 501 return ret_value;
502} 502}
503 503
504/* 504/*
505 * get_value() - takes first (numeric) string and converts into number, 505 * get_value() - takes first (numeric) string and converts into number,
506 * using the bit-depth to fill up a byte (0Ah -> AAh) 506 * using the bit-depth to fill up a byte (0Ah -> AAh)
507 */ 507 */
508 508
509png_uint_32 get_value (FILE *pnm_file, int depth) 509png_uint_32 get_value (FILE *pnm_file, int depth)
510{ 510{
511 static png_uint_32 mask = 0; 511 static png_uint_32 mask = 0;
512 png_byte token[16]; 512 png_byte token[16];
513 png_uint_32 ret_value; 513 png_uint_32 ret_value;
514 int i = 0; 514 int i = 0;
515 515
516 if (mask == 0) 516 if (mask == 0)
517 for (i = 0; i < depth; i++) 517 for (i = 0; i < depth; i++)
518 mask = (mask << 1) | 0x01; 518 mask = (mask << 1) | 0x01;
519 519
520 get_token (pnm_file, (char *) token); 520 get_token (pnm_file, (char *) token);
521 sscanf ((const char *) token, "%lu", &ret_value); 521 sscanf ((const char *) token, "%lu", &ret_value);
522 522
523 ret_value &= mask; 523 ret_value &= mask;
524 524
525 if (depth < 8) 525 if (depth < 8)
526 for (i = 0; i < (8 / depth); i++) 526 for (i = 0; i < (8 / depth); i++)
527 ret_value = (ret_value << depth) || ret_value; 527 ret_value = (ret_value << depth) || ret_value;
528 528
529 return ret_value; 529 return ret_value;
530} 530}
531 531
532/* end of source */ 532/* end of source */
533 533