Debian Codegraph @

converter/other/rast.c

raw
1 /* rast.c
2 ** (Formerly named libpnm4.c - pnm utility library part 4)
3 **
4 ** Copyright (C) 1988 by Jef Poskanzer.
5 **
6 ** Permission to use, copy, modify, and distribute this software and its
7 ** documentation for any purpose and without fee is hereby granted, provided
8 ** that the above copyright notice appear in all copies and that both that
9 ** copyright notice and this permission notice appear in supporting
10 ** documentation. This software is provided "as is" without express or
11 ** implied warranty.
12 */
13
14 #include "pnm.h"
15 #include "rast.h"
16 #include "mallocvar.h"
17
18 /*
19 ** Semi-work-alike versions of some Sun pixrect routines. Just enough
20 ** for rasterfile reading and writing to work.
21 */
22
23 struct pixrect*
24 mem_create(int const w,
25 int const h,
26 int const depth) {
27
28 struct pixrect* p;
29 struct mpr_data* m;
30
31 MALLOCVAR(p);
32 if ( p == NULL )
33 return NULL;
34 p->pr_ops = NULL;
35 p->pr_size.x = w;
36 p->pr_size.y = h;
37 p->pr_depth = depth;
38 MALLOCVAR(m);
39 if ( m == NULL )
40 {
41 free( p );
42 return NULL;
43 }
44 p->pr_data = m;
45
46 /* According to the documentation, linebytes is supposed to be rounded
47 ** up to a longword (except on 386 boxes). However, this turns out
48 ** not to be the case. In reality, all of Sun's code rounds up to
49 ** a short, not a long.
50 */
51 m->md_linebytes = ( w * depth + 15 ) / 16 * 2;
52 m->md_offset.x = 0;
53 m->md_offset.y = 0;
54 m->md_flags = 0;
55 MALLOCARRAY(m->md_image, m->md_linebytes * h );
56 if ( m->md_image == NULL )
57 {
58 free( m );
59 free( p );
60 return NULL;
61 }
62
63 return p;
64 }
65
66
67
68 void
69 mem_free(struct pixrect* const p) {
70 free( p->pr_data->md_image );
71 free( p->pr_data );
72 free( p );
73 }
74
75
76
77 int
78 pr_dump(struct pixrect* const p,
79 FILE* const out,
80 colormap_t* const colormap,
81 int const type,
82 int const copy_flag) {
83
84 struct rasterfile h;
85 int size, besize, count;
86 unsigned char* beimage;
87 unsigned char* bp;
88 unsigned char c, pc;
89 int i, j;
90
91 h.ras_magic = RAS_MAGIC;
92 h.ras_width = p->pr_size.x;
93 h.ras_height = p->pr_size.y;
94 h.ras_depth = p->pr_depth;
95
96 h.ras_type = type;
97 switch ( type )
98 {
99 case RT_OLD:
100 pm_error( "This program does not know the Old rasterfile type" );
101
102 case RT_FORMAT_TIFF:
103 pm_error( "This program does not know the TIFF rasterfile type" );
104
105 case RT_FORMAT_IFF:
106 pm_error( "This program does not know the IFF rasterfile type" );
107
108 case RT_EXPERIMENTAL:
109 pm_error( "This program does not know the Experimental "
110 "rasterfile type" );
111
112 case RT_STANDARD:
113 case RT_FORMAT_RGB:
114 /* Ignore hP->ras_length. */
115 h.ras_length = p->pr_size.y * p->pr_data->md_linebytes;
116 break;
117
118 case RT_BYTE_ENCODED:
119 size = p->pr_size.y * p->pr_data->md_linebytes;
120 bp = p->pr_data->md_image;
121 MALLOCARRAY(beimage, size * 3 / 2); /* worst case */
122 if ( beimage == NULL )
123 return PIX_ERR;
124 besize = 0;
125 count = 0;
126 for ( i = 0; i < size; ++i )
127 {
128 c = *bp++;
129 if ( count > 0 )
130 {
131 if ( pc != c )
132 {
133 if ( count == 1 && pc == 128 )
134 {
135 beimage[besize++] = 128;
136 beimage[besize++] = 0;
137 count = 0;
138 }
139 else if ( count > 2 || pc == 128 )
140 {
141 beimage[besize++] = 128;
142 beimage[besize++] = count - 1;
143 beimage[besize++] = pc;
144 count = 0;
145 }
146 else
147 {
148 for ( j = 0; j < count; ++j )
149 beimage[besize++] = pc;
150 count = 0;
151 }
152 }
153 }
154 pc = c;
155 ++count;
156 if ( count == 256 )
157 {
158 beimage[besize++] = 128;
159 beimage[besize++] = count - 1;
160 beimage[besize++] = c;
161 count = 0;
162 }
163 }
164 if ( count > 0 )
165 {
166 if ( count == 1 && c == 128 )
167 {
168 beimage[besize++] = 128;
169 beimage[besize++] = 0;
170 }
171 if ( count > 2 || c == 128 )
172 {
173 beimage[besize++] = 128;
174 beimage[besize++] = count - 1;
175 beimage[besize++] = c;
176 }
177 else
178 {
179 for ( j = 0; j < count; ++j )
180 beimage[besize++] = c;
181 }
182 }
183 h.ras_length = besize;
184 break;
185
186 default:
187 pm_error( "unknown rasterfile type" );
188 }
189
190 if ( colormap == NULL )
191 {
192 h.ras_maptype = RMT_NONE;
193 h.ras_maplength = 0;
194 }
195 else
196 {
197 h.ras_maptype = colormap->type;
198 switch ( colormap->type )
199 {
200 case RMT_EQUAL_RGB:
201 h.ras_maplength = colormap->length * 3;
202 break;
203
204 case RMT_RAW:
205 h.ras_maplength = colormap->length;
206 break;
207
208 default:
209 pm_error( "unknown colormap type" );
210 }
211 }
212
213 if ( pm_writebiglong( out, h.ras_magic ) == -1 )
214 return PIX_ERR;
215 if ( pm_writebiglong( out, h.ras_width ) == -1 )
216 return PIX_ERR;
217 if ( pm_writebiglong( out, h.ras_height ) == -1 )
218 return PIX_ERR;
219 if ( pm_writebiglong( out, h.ras_depth ) == -1 )
220 return PIX_ERR;
221 if ( pm_writebiglong( out, h.ras_length ) == -1 )
222 return PIX_ERR;
223 if ( pm_writebiglong( out, h.ras_type ) == -1 )
224 return PIX_ERR;
225 if ( pm_writebiglong( out, h.ras_maptype ) == -1 )
226 return PIX_ERR;
227 if ( pm_writebiglong( out, h.ras_maplength ) == -1 )
228 return PIX_ERR;
229
230 if ( colormap != NULL )
231 {
232 switch ( colormap->type )
233 {
234 case RMT_EQUAL_RGB:
235 if ( fwrite( colormap->map[0], 1, colormap->length, out ) !=
236 colormap->length )
237 return PIX_ERR;
238 if ( fwrite( colormap->map[1], 1, colormap->length, out ) !=
239 colormap->length )
240 return PIX_ERR;
241 if ( fwrite( colormap->map[2], 1, colormap->length, out ) !=
242 colormap->length )
243 return PIX_ERR;
244 break;
245
246 case RMT_RAW:
247 if ( fwrite( colormap->map[0], 1, colormap->length, out ) !=
248 colormap->length )
249 return PIX_ERR;
250 break;
251 }
252 }
253
254 switch ( type )
255 {
256 case RT_STANDARD:
257 case RT_FORMAT_RGB:
258 if ( fwrite( p->pr_data->md_image, 1, h.ras_length, out ) !=
259 h.ras_length )
260 return PIX_ERR;
261 break;
262
263 case RT_BYTE_ENCODED:
264 if ( fwrite( beimage, 1, besize, out ) != besize )
265 {
266 free( beimage );
267 return PIX_ERR;
268 }
269 free( beimage );
270 break;
271 }
272
273 return 0;
274 }
275
276
277
278 int
279 pr_load_header(FILE * const ifP,
280 struct rasterfile * const headerP) {
281
282 {
283 long int magic;
284
285 pm_readbiglong(ifP, &magic);
286 if (magic != RAS_MAGIC)
287 pm_error("Wrong magic number for a RAST file");
288 }
289 {
290 long int width;
291 pm_readbiglong(ifP, &width);
292
293 if (width < 0)
294 pm_error("Negative width in RAST header");
295 else
296 headerP->ras_width = width;
297 }
298 {
299 long int height;
300 pm_readbiglong(ifP, &height);
301
302 if (height < 0)
303 pm_error("Negative height in RAST header");
304 else
305 headerP->ras_height = height;
306 }
307 {
308 long int depth;
309 pm_readbiglong(ifP, &depth);
310
311 if (depth < 0)
312 pm_error("Negative depth in RAST header");
313 else
314 headerP->ras_depth = depth;
315 }
316 {
317 long int length;
318 pm_readbiglong(ifP, &length);
319
320 if (length < 0)
321 pm_error("Negative length in RAST header");
322 else
323 headerP->ras_length = length;
324 }
325 pm_readbiglong(ifP, &headerP->ras_type);
326
327 pm_readbiglong(ifP, &headerP->ras_maptype);
328 {
329 long int mapLength;
330 pm_readbiglong(ifP, &mapLength);
331
332 if (mapLength < 0)
333 pm_error("Negative map length in RAST header");
334 else
335 headerP->ras_maplength = mapLength;
336 }
337 return 0;
338 }
339
340
341
342 int
343 pr_load_colormap(FILE* const in,
344 struct rasterfile* const hP,
345 colormap_t* const colormap) {
346
347 if ( colormap == NULL || hP->ras_maptype == RMT_NONE )
348 {
349 int i;
350
351 for ( i = 0; i < hP->ras_maplength; ++i )
352 if ( getc( in ) == EOF )
353 return PIX_ERR;
354 }
355 else
356 {
357 colormap->type = hP->ras_maptype;
358 switch ( hP->ras_maptype )
359 {
360 case RMT_EQUAL_RGB:
361 colormap->length = hP->ras_maplength / 3;
362 MALLOCARRAY( colormap->map[0], colormap->length );
363 if ( colormap->map[0] == NULL )
364 return PIX_ERR;
365 MALLOCARRAY( colormap->map[1], colormap->length );
366 if ( colormap->map[1] == NULL )
367 {
368 free( colormap->map[0] );
369 return PIX_ERR;
370 }
371 MALLOCARRAY( colormap->map[2], colormap->length );
372 if ( colormap->map[2] == NULL )
373 {
374 free( colormap->map[0] );
375 free( colormap->map[1] );
376 return PIX_ERR;
377 }
378 if ( fread( colormap->map[0], 1, colormap->length, in ) !=
379 colormap->length ||
380 fread( colormap->map[1], 1, colormap->length, in ) !=
381 colormap->length ||
382 fread( colormap->map[2], 1, colormap->length, in ) !=
383 colormap->length )
384 {
385 free( colormap->map[0] );
386 free( colormap->map[1] );
387 free( colormap->map[2] );
388 return PIX_ERR;
389 }
390 break;
391
392 case RMT_RAW: {
393 size_t bytesRead;
394
395 colormap->length = hP->ras_maplength;
396 MALLOCARRAY( colormap->map[0], colormap->length );
397 if ( colormap->map[0] == NULL )
398 return PIX_ERR;
399 colormap->map[2] = colormap->map[1] = colormap->map[0];
400 bytesRead = fread( colormap->map[0], 1, hP->ras_maplength, in );
401 if ( bytesRead != hP->ras_maplength )
402 {
403 free( colormap->map[0] );
404 return PIX_ERR;
405 }
406 }
407 break;
408
409 default:
410 pm_error( "unknown colormap type" );
411 }
412 }
413 return 0;
414 }
415
416
417
418 struct pixrect*
419 pr_load_image(FILE* const in,
420 struct rasterfile* const hP,
421 colormap_t* const colormap) {
422
423 struct pixrect* p;
424 unsigned char* beimage;
425 unsigned char* bep;
426 unsigned char* bp;
427 unsigned char c;
428 int i;
429 int j, count;
430
431 p = mem_create( hP->ras_width, hP->ras_height, hP->ras_depth );
432 if ( p == NULL )
433 return NULL;
434
435 switch ( hP->ras_type )
436 {
437 case RT_OLD:
438 pm_error( "This program does not know the Old rasterfile type" );
439
440 case RT_FORMAT_TIFF:
441 pm_error( "This program does not know the TIFF rasterfile type" );
442
443 case RT_FORMAT_IFF:
444 pm_error( "This program does not know the IFF rasterfile type" );
445
446 case RT_EXPERIMENTAL:
447 pm_error( "This program does not know the Experimental "
448 "rasterfile type" );
449
450 case RT_STANDARD:
451 case RT_FORMAT_RGB:
452 /* Ignore hP->ras_length. */
453 i = p->pr_size.y * p->pr_data->md_linebytes;
454 if ( fread( p->pr_data->md_image, 1, i, in ) != i )
455 {
456 mem_free( p );
457 return NULL;
458 }
459 break;
460
461 case RT_BYTE_ENCODED:
462 MALLOCARRAY( beimage, hP->ras_length );
463 if ( beimage == NULL )
464 {
465 mem_free( p );
466 return NULL;
467 }
468 if ( fread( beimage, 1, hP->ras_length, in ) != hP->ras_length )
469 {
470 mem_free( p );
471 free( beimage );
472 return NULL;
473 }
474 bep = beimage;
475 bp = p->pr_data->md_image;
476 for ( i = 0; i < hP->ras_length; )
477 {
478 c = *bep++;
479 if ( c == 128 )
480 {
481 count = ( *bep++ ) + 1;
482 if ( count == 1 )
483 {
484 *bp++ = 128;
485 i += 2;
486 }
487 else
488 {
489 c = *bep++;
490 for ( j = 0; j < count; ++j )
491 *bp++ = c;
492 i += 3;
493 }
494 }
495 else
496 {
497 *bp++ = c;
498 ++i;
499 }
500 }
501 free( beimage );
502 break;
503
504 default:
505 pm_error( "unknown rasterfile type" );
506 }
507
508 return p;
509 }
510
511
512