]> Creatis software - gdcm.git/blob - src/gdcmjasper/src/libjasper/ras/ras_dec.c
ENH: Ok since OJ warnings are not going to be fixed anytime soon remove them
[gdcm.git] / src / gdcmjasper / src / libjasper / ras / ras_dec.c
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  *   British Columbia.
4  * Copyright (c) 2001-2003 Michael David Adams.
5  * All rights reserved.
6  */
7
8 /* __START_OF_JASPER_LICENSE__
9  * 
10  * JasPer License Version 2.0
11  * 
12  * Copyright (c) 1999-2000 Image Power, Inc.
13  * Copyright (c) 1999-2000 The University of British Columbia
14  * Copyright (c) 2001-2003 Michael David Adams
15  * 
16  * All rights reserved.
17  * 
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  * 
26  * 1.  The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  * 
30  * 2.  The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  * 
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE.  NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER.  THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.  IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE.  AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY.  THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES").  THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  * 
61  * __END_OF_JASPER_LICENSE__
62  */
63
64 /*
65  * Sun Rasterfile Library
66  *
67  * $Id: ras_dec.c,v 1.1 2005/05/22 18:33:10 malaterre Exp $
68  */
69
70 /******************************************************************************\
71 * Includes.
72 \******************************************************************************/
73
74 #include <assert.h>
75 #include <stdlib.h>
76
77 #include "jasper/jas_stream.h"
78 #include "jasper/jas_image.h"
79 #include "jasper/jas_debug.h"
80
81 #include "ras_cod.h"
82
83 /******************************************************************************\
84 * Prototypes.
85 \******************************************************************************/
86
87 static int ras_gethdr(jas_stream_t *in, ras_hdr_t *hdr);
88 static int ras_getint(jas_stream_t *in, int_fast32_t *val);
89
90 static int ras_getdata(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
91   jas_image_t *image);
92 static int ras_getdatastd(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
93   jas_image_t *image);
94 static int ras_getcmap(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap);
95
96 /******************************************************************************\
97 * Code.
98 \******************************************************************************/
99
100 jas_image_t *ras_decode(jas_stream_t *in, char *optstr)
101 {
102   ras_hdr_t hdr;
103   ras_cmap_t cmap;
104   jas_image_t *image;
105   jas_image_cmptparm_t cmptparms[3];
106   jas_image_cmptparm_t *cmptparm;
107   int clrspc;
108   int numcmpts;
109   int i;
110
111   if (optstr) {
112     fprintf(stderr, "warning: ignoring RAS decoder options\n");
113   }
114
115   /* Read the header. */
116   if (ras_gethdr(in, &hdr)) {
117     return 0;
118   }
119
120   /* Does the header information look reasonably sane? */
121   if (hdr.magic != RAS_MAGIC || hdr.width <= 0 || hdr.height <= 0 ||
122     hdr.depth <= 0 || hdr.depth > 32) {
123     return 0;
124   }
125
126   /* In the case of the old format, do not rely on the length field
127   being properly specified.  Calculate the quantity from scratch. */
128   if (hdr.type == RAS_TYPE_OLD) {
129     hdr.length = RAS_ROWSIZE(&hdr) * hdr.height;
130   }
131
132   /* Calculate some quantities needed for creation of the image
133   object. */
134   if (RAS_ISRGB(&hdr)) {
135     clrspc = JAS_CLRSPC_SRGB;
136     numcmpts = 3;
137   } else {
138     clrspc = JAS_CLRSPC_SGRAY;
139     numcmpts = 1;
140   }
141   for (i = 0, cmptparm = cmptparms; i < numcmpts; ++i, ++cmptparm) {
142     cmptparm->tlx = 0;
143     cmptparm->tly = 0;
144     cmptparm->hstep = 1;
145     cmptparm->vstep = 1;
146     cmptparm->width = hdr.width;
147     cmptparm->height = hdr.height;
148     cmptparm->prec = RAS_ISRGB(&hdr) ? 8 : hdr.depth;
149     cmptparm->sgnd = false;
150   }
151   /* Create the image object. */
152   if (!(image = jas_image_create(numcmpts, cmptparms, JAS_CLRSPC_UNKNOWN))) {
153     return 0;
154   }
155
156   /* Read the color map (if there is one). */
157   if (ras_getcmap(in, &hdr, &cmap)) {
158     jas_image_destroy(image);
159     return 0;
160   }
161
162   /* Read the pixel data. */
163   if (ras_getdata(in, &hdr, &cmap, image)) {
164     jas_image_destroy(image);
165     return 0;
166   }
167
168   jas_image_setclrspc(image, clrspc);
169   if (clrspc == JAS_CLRSPC_SRGB) {
170     jas_image_setcmpttype(image, 0,
171       JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
172     jas_image_setcmpttype(image, 1,
173       JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
174     jas_image_setcmpttype(image, 2,
175       JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
176   } else {
177     jas_image_setcmpttype(image, 0,
178       JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
179   }
180
181   return image;
182 }
183
184 int ras_validate(jas_stream_t *in)
185 {
186   uchar buf[RAS_MAGICLEN];
187   int i;
188   int n;
189   uint_fast32_t magic;
190
191   assert(JAS_STREAM_MAXPUTBACK >= RAS_MAGICLEN);
192
193   /* Read the validation data (i.e., the data used for detecting
194     the format). */
195   if ((n = jas_stream_read(in, buf, RAS_MAGICLEN)) < 0) {
196     return -1;
197   }
198
199   /* Put the validation data back onto the stream, so that the
200     stream position will not be changed. */
201   for (i = n - 1; i >= 0; --i) {
202     if (jas_stream_ungetc(in, buf[i]) == EOF) {
203       return -1;
204     }
205   }
206
207   /* Did we read enough data? */
208   if (n < RAS_MAGICLEN) {
209     return -1;
210   }
211
212   magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
213
214   /* Is the signature correct for the Sun Rasterfile format? */
215   if (magic != RAS_MAGIC) {
216     return -1;
217   }
218   return 0;
219 }
220
221 static int ras_getdata(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
222   jas_image_t *image)
223 {
224   int ret;
225
226   switch (hdr->type) {
227   case RAS_TYPE_OLD:
228   case RAS_TYPE_STD:
229     ret = ras_getdatastd(in, hdr, cmap, image);
230     break;
231   case RAS_TYPE_RLE:
232     jas_eprintf("error: RLE encoding method not supported\n");
233     ret = -1;
234     break;
235   default:
236     jas_eprintf("error: encoding method not supported\n");
237     ret = -1;
238     break;
239   }
240   return ret;
241 }
242
243 static int ras_getdatastd(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
244   jas_image_t *image)
245 {
246   int pad;
247   int nz;
248   int z;
249   int c;
250   int y;
251   int x;
252   int v;
253   int i;
254   jas_matrix_t *data[3];
255
256 /* Note: This function does not properly handle images with a colormap. */
257   /* Avoid compiler warnings about unused parameters. */
258   cmap = 0;
259
260   for (i = 0; i < jas_image_numcmpts(image); ++i) {
261     data[i] = jas_matrix_create(1, jas_image_width(image));
262     assert(data[i]);
263   }
264
265   pad = RAS_ROWSIZE(hdr) - (hdr->width * hdr->depth + 7) / 8;
266
267   for (y = 0; y < hdr->height; y++) {
268     nz = 0;
269     z = 0;
270     for (x = 0; x < hdr->width; x++) {
271       while (nz < hdr->depth) {
272         if ((c = jas_stream_getc(in)) == EOF) {
273           return -1;
274         }
275         z = (z << 8) | c;
276         nz += 8;
277       }
278
279       v = (z >> (nz - hdr->depth)) & RAS_ONES(hdr->depth);
280       z &= RAS_ONES(nz - hdr->depth);
281       nz -= hdr->depth;
282
283       if (jas_image_numcmpts(image) == 3) {
284         jas_matrix_setv(data[0], x, (RAS_GETRED(v)));
285         jas_matrix_setv(data[1], x, (RAS_GETGREEN(v)));
286         jas_matrix_setv(data[2], x, (RAS_GETBLUE(v)));
287       } else {
288         jas_matrix_setv(data[0], x, (v));
289       }
290     }
291     if (pad) {
292       if ((c = jas_stream_getc(in)) == EOF) {
293         return -1;
294       }
295     }
296     for (i = 0; i < jas_image_numcmpts(image); ++i) {
297       if (jas_image_writecmpt(image, i, 0, y, hdr->width, 1,
298         data[i])) {
299         return -1;
300       }
301     }
302   }
303
304   for (i = 0; i < jas_image_numcmpts(image); ++i) {
305     jas_matrix_destroy(data[i]);
306   }
307
308   return 0;
309 }
310
311 static int ras_getcmap(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap)
312 {
313   int i;
314   int j;
315   int x;
316   int c;
317   int numcolors;
318   int actualnumcolors;
319
320   switch (hdr->maptype) {
321   case RAS_MT_NONE:
322     break;
323   case RAS_MT_EQUALRGB:
324     {
325 fprintf(stderr, "warning: palettized images not fully supported\n");
326     numcolors = 1 << hdr->depth;
327     assert(numcolors <= RAS_CMAP_MAXSIZ);
328     actualnumcolors = hdr->maplength / 3;
329     for (i = 0; i < numcolors; i++) {
330       cmap->data[i] = 0;
331     }
332     if ((hdr->maplength % 3) || hdr->maplength < 0 ||
333       hdr->maplength > 3 * numcolors) {
334       return -1;
335     }
336     for (i = 0; i < 3; i++) {
337       for (j = 0; j < actualnumcolors; j++) {
338         if ((c = jas_stream_getc(in)) == EOF) {
339           return -1;
340         }
341         x = 0;
342         switch (i) {
343         case 0:
344           x = RAS_RED(c);
345           break;
346         case 1:
347           x = RAS_GREEN(c);
348           break;
349         case 2:
350           x = RAS_BLUE(c);
351           break;
352         }
353         cmap->data[j] |= x;
354       }
355     }
356     }
357     break;
358   default:
359     return -1;
360     break;
361   }
362
363   return 0;
364 }
365
366 static int ras_gethdr(jas_stream_t *in, ras_hdr_t *hdr)
367 {
368   if (ras_getint(in, &hdr->magic) || ras_getint(in, &hdr->width) ||
369     ras_getint(in, &hdr->height) || ras_getint(in, &hdr->depth) ||
370     ras_getint(in, &hdr->length) || ras_getint(in, &hdr->type) ||
371     ras_getint(in, &hdr->maptype) || ras_getint(in, &hdr->maplength)) {
372     return -1;
373   }
374
375   if (hdr->magic != RAS_MAGIC) {
376     return -1;
377   }
378
379   return 0;
380 }
381
382 static int ras_getint(jas_stream_t *in, int_fast32_t *val)
383 {
384   int x;
385   int c;
386   int i;
387
388   x = 0;
389   for (i = 0; i < 4; i++) {
390     if ((c = jas_stream_getc(in)) == EOF) {
391       return -1;
392     }
393     x = (x << 8) | (c & 0xff);
394   }
395
396   *val = x;
397   return 0;
398 }