2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2003 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
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
16 * All rights reserved.
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:
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.
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
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.
61 * __END_OF_JASPER_LICENSE__
65 * Sun Rasterfile Library
67 * $Id: ras_dec.c,v 1.1 2005/05/22 18:33:10 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
77 #include "jasper/jas_stream.h"
78 #include "jasper/jas_image.h"
79 #include "jasper/jas_debug.h"
83 /******************************************************************************\
85 \******************************************************************************/
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);
90 static int ras_getdata(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
92 static int ras_getdatastd(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
94 static int ras_getcmap(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap);
96 /******************************************************************************\
98 \******************************************************************************/
100 jas_image_t *ras_decode(jas_stream_t *in, char *optstr)
105 jas_image_cmptparm_t cmptparms[3];
106 jas_image_cmptparm_t *cmptparm;
112 fprintf(stderr, "warning: ignoring RAS decoder options\n");
115 /* Read the header. */
116 if (ras_gethdr(in, &hdr)) {
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) {
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;
132 /* Calculate some quantities needed for creation of the image
134 if (RAS_ISRGB(&hdr)) {
135 clrspc = JAS_CLRSPC_SRGB;
138 clrspc = JAS_CLRSPC_SGRAY;
141 for (i = 0, cmptparm = cmptparms; i < numcmpts; ++i, ++cmptparm) {
146 cmptparm->width = hdr.width;
147 cmptparm->height = hdr.height;
148 cmptparm->prec = RAS_ISRGB(&hdr) ? 8 : hdr.depth;
149 cmptparm->sgnd = false;
151 /* Create the image object. */
152 if (!(image = jas_image_create(numcmpts, cmptparms, JAS_CLRSPC_UNKNOWN))) {
156 /* Read the color map (if there is one). */
157 if (ras_getcmap(in, &hdr, &cmap)) {
158 jas_image_destroy(image);
162 /* Read the pixel data. */
163 if (ras_getdata(in, &hdr, &cmap, image)) {
164 jas_image_destroy(image);
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));
177 jas_image_setcmpttype(image, 0,
178 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
184 int ras_validate(jas_stream_t *in)
186 uchar buf[RAS_MAGICLEN];
191 assert(JAS_STREAM_MAXPUTBACK >= RAS_MAGICLEN);
193 /* Read the validation data (i.e., the data used for detecting
195 if ((n = jas_stream_read(in, buf, RAS_MAGICLEN)) < 0) {
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) {
207 /* Did we read enough data? */
208 if (n < RAS_MAGICLEN) {
212 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
214 /* Is the signature correct for the Sun Rasterfile format? */
215 if (magic != RAS_MAGIC) {
221 static int ras_getdata(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
229 ret = ras_getdatastd(in, hdr, cmap, image);
232 jas_eprintf("error: RLE encoding method not supported\n");
236 jas_eprintf("error: encoding method not supported\n");
243 static int ras_getdatastd(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap,
254 jas_matrix_t *data[3];
256 /* Note: This function does not properly handle images with a colormap. */
257 /* Avoid compiler warnings about unused parameters. */
260 for (i = 0; i < jas_image_numcmpts(image); ++i) {
261 data[i] = jas_matrix_create(1, jas_image_width(image));
265 pad = RAS_ROWSIZE(hdr) - (hdr->width * hdr->depth + 7) / 8;
267 for (y = 0; y < hdr->height; y++) {
270 for (x = 0; x < hdr->width; x++) {
271 while (nz < hdr->depth) {
272 if ((c = jas_stream_getc(in)) == EOF) {
279 v = (z >> (nz - hdr->depth)) & RAS_ONES(hdr->depth);
280 z &= RAS_ONES(nz - hdr->depth);
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)));
288 jas_matrix_setv(data[0], x, (v));
292 if ((c = jas_stream_getc(in)) == EOF) {
296 for (i = 0; i < jas_image_numcmpts(image); ++i) {
297 if (jas_image_writecmpt(image, i, 0, y, hdr->width, 1,
304 for (i = 0; i < jas_image_numcmpts(image); ++i) {
305 jas_matrix_destroy(data[i]);
311 static int ras_getcmap(jas_stream_t *in, ras_hdr_t *hdr, ras_cmap_t *cmap)
320 switch (hdr->maptype) {
323 case RAS_MT_EQUALRGB:
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++) {
332 if ((hdr->maplength % 3) || hdr->maplength < 0 ||
333 hdr->maplength > 3 * numcolors) {
336 for (i = 0; i < 3; i++) {
337 for (j = 0; j < actualnumcolors; j++) {
338 if ((c = jas_stream_getc(in)) == EOF) {
366 static int ras_gethdr(jas_stream_t *in, ras_hdr_t *hdr)
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)) {
375 if (hdr->magic != RAS_MAGIC) {
382 static int ras_getint(jas_stream_t *in, int_fast32_t *val)
389 for (i = 0; i < 4; i++) {
390 if ((c = jas_stream_getc(in)) == EOF) {
393 x = (x << 8) | (c & 0xff);