2 * Copyright (c) 2001-2003 Michael David Adams.
6 /* __START_OF_JASPER_LICENSE__
8 * JasPer License Version 2.0
10 * Copyright (c) 1999-2000 Image Power, Inc.
11 * Copyright (c) 1999-2000 The University of British Columbia
12 * Copyright (c) 2001-2003 Michael David Adams
14 * All rights reserved.
16 * Permission is hereby granted, free of charge, to any person (the
17 * "User") obtaining a copy of this software and associated documentation
18 * files (the "Software"), to deal in the Software without restriction,
19 * including without limitation the rights to use, copy, modify, merge,
20 * publish, distribute, and/or sell copies of the Software, and to permit
21 * persons to whom the Software is furnished to do so, subject to the
22 * following conditions:
24 * 1. The above copyright notices and this permission notice (which
25 * includes the disclaimer below) shall be included in all copies or
26 * substantial portions of the Software.
28 * 2. The name of a copyright holder shall not be used to endorse or
29 * promote products derived from the Software without specific prior
32 * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
33 * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
34 * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
35 * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
36 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
37 * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
38 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
39 * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
40 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
41 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
42 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
43 * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
44 * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
45 * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
46 * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
47 * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
48 * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
49 * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
50 * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
51 * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
52 * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
53 * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
54 * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
55 * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
56 * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
57 * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
59 * __END_OF_JASPER_LICENSE__
62 /******************************************************************************\
64 \******************************************************************************/
69 #include "jasper/jas_tvp.h"
70 #include "jasper/jas_stream.h"
71 #include "jasper/jas_image.h"
72 #include "jasper/jas_string.h"
76 /******************************************************************************\
78 \******************************************************************************/
80 static int pgx_gethdr(jas_stream_t *in, pgx_hdr_t *hdr);
81 static int pgx_getdata(jas_stream_t *in, pgx_hdr_t *hdr, jas_image_t *image);
82 static int_fast32_t pgx_getword(jas_stream_t *in, bool bigendian, int prec);
83 static int pgx_getsgnd(jas_stream_t *in, bool *sgnd);
84 static int pgx_getbyteorder(jas_stream_t *in, bool *bigendian);
85 static int pgx_getc(jas_stream_t *in);
86 static int pgx_getuint32(jas_stream_t *in, uint_fast32_t *val);
87 static jas_seqent_t pgx_wordtoint(uint_fast32_t word, int prec, bool sgnd);
89 /******************************************************************************\
90 * Code for load operation.
91 \******************************************************************************/
93 /* Load an image from a stream in the PGX format. */
95 jas_image_t *pgx_decode(jas_stream_t *in, char *optstr)
99 jas_image_cmptparm_t cmptparm;
101 /* Avoid compiler warnings about unused parameters. */
106 if (pgx_gethdr(in, &hdr)) {
111 pgx_dumphdr(stderr, &hdr);
114 if (!(image = jas_image_create0())) {
121 cmptparm.width = hdr.width;
122 cmptparm.height = hdr.height;
123 cmptparm.prec = hdr.prec;
124 cmptparm.sgnd = hdr.sgnd;
125 if (jas_image_addcmpt(image, 0, &cmptparm)) {
128 if (pgx_getdata(in, &hdr, image)) {
132 jas_image_setclrspc(image, JAS_CLRSPC_SGRAY);
133 jas_image_setcmpttype(image, 0,
134 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
140 jas_image_destroy(image);
145 /******************************************************************************\
146 * Code for validate operation.
147 \******************************************************************************/
149 int pgx_validate(jas_stream_t *in)
151 uchar buf[PGX_MAGICLEN];
156 assert(JAS_STREAM_MAXPUTBACK >= PGX_MAGICLEN);
158 /* Read the validation data (i.e., the data used for detecting
160 if ((n = jas_stream_read(in, buf, PGX_MAGICLEN)) < 0) {
164 /* Put the validation data back onto the stream, so that the
165 stream position will not be changed. */
166 for (i = n - 1; i >= 0; --i) {
167 if (jas_stream_ungetc(in, buf[i]) == EOF) {
172 /* Did we read enough data? */
173 if (n < PGX_MAGICLEN) {
177 /* Compute the signature value. */
178 magic = (buf[0] << 8) | buf[1];
180 /* Ensure that the signature is correct for this format. */
181 if (magic != PGX_MAGIC) {
188 /******************************************************************************\
190 \******************************************************************************/
192 static int pgx_gethdr(jas_stream_t *in, pgx_hdr_t *hdr)
197 if ((c = jas_stream_getc(in)) == EOF) {
201 if ((c = jas_stream_getc(in)) == EOF) {
205 hdr->magic = buf[0] << 8 | buf[1];
206 if (hdr->magic != PGX_MAGIC) {
209 if ((c = pgx_getc(in)) == EOF || !isspace(c)) {
212 if (pgx_getbyteorder(in, &hdr->bigendian)) {
215 if (pgx_getsgnd(in, &hdr->sgnd)) {
218 if (pgx_getuint32(in, &hdr->prec)) {
221 if (pgx_getuint32(in, &hdr->width)) {
224 if (pgx_getuint32(in, &hdr->height)) {
233 static int pgx_getdata(jas_stream_t *in, pgx_hdr_t *hdr, jas_image_t *image)
243 if (!(data = jas_matrix_create(1, hdr->width))) {
246 for (y = 0; y < hdr->height; ++y) {
247 for (x = 0; x < hdr->width; ++x) {
248 /* Need to adjust signed value. */
249 if ((v = pgx_getword(in, hdr->bigendian, hdr->prec)) < 0) {
253 v = pgx_wordtoint(word, hdr->prec, hdr->sgnd);
254 jas_matrix_set(data, 0, x, v);
256 if (jas_image_writecmpt(image, 0, 0, y, hdr->width, 1, data)) {
260 jas_matrix_destroy(data);
265 jas_matrix_destroy(data);
270 static int_fast32_t pgx_getword(jas_stream_t *in, bool bigendian, int prec)
278 wordsize = (prec + 7) / 8;
285 for (i = 0; i < wordsize; ++i) {
286 if ((c = jas_stream_getc(in)) == EOF) {
289 j = bigendian ? (wordsize - 1 - i) : i;
290 val = val | ((c & 0xff) << (8 * j));
292 val &= (1 << prec) - 1;
299 static int pgx_getc(jas_stream_t *in)
303 if ((c = jas_stream_getc(in)) == EOF) {
310 if ((c = jas_stream_getc(in)) == EOF) {
313 } while (c != '\n' && c != '\r');
317 static int pgx_getbyteorder(jas_stream_t *in, bool *bigendian)
323 if ((c = pgx_getc(in)) == EOF) {
326 } while (isspace(c));
329 if ((c = pgx_getc(in)) == EOF) {
333 if (buf[0] == 'M' && buf[1] == 'L') {
335 } else if (buf[0] == 'L' && buf[1] == 'M') {
341 while ((c = pgx_getc(in)) != EOF && !isspace(c)) {
353 static int pgx_getsgnd(jas_stream_t *in, bool *sgnd)
358 if ((c = pgx_getc(in)) == EOF) {
361 } while (isspace(c));
365 } else if (c == '-') {
370 while ((c = pgx_getc(in)) != EOF && !isspace(c)) {
382 static int pgx_getuint32(jas_stream_t *in, uint_fast32_t *val)
388 if ((c = pgx_getc(in)) == EOF) {
391 } while (isspace(c));
395 v = 10 * v + c - '0';
396 if ((c = pgx_getc(in)) < 0) {
408 static jas_seqent_t pgx_wordtoint(uint_fast32_t v, int prec, bool sgnd)
411 v &= (1 << prec) - 1;
412 ret = (sgnd && (v & (1 << (prec - 1)))) ? (v - (1 << prec)) : v;