/src/jasper/src/libjasper/pgx/pgx_enc.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2001-2003 Michael David Adams. |
3 | | * All rights reserved. |
4 | | */ |
5 | | |
6 | | /* __START_OF_JASPER_LICENSE__ |
7 | | * |
8 | | * JasPer License Version 2.0 |
9 | | * |
10 | | * Copyright (c) 2001-2006 Michael David Adams |
11 | | * Copyright (c) 1999-2000 Image Power, Inc. |
12 | | * Copyright (c) 1999-2000 The University of British Columbia |
13 | | * |
14 | | * All rights reserved. |
15 | | * |
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: |
23 | | * |
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. |
27 | | * |
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 |
30 | | * written permission. |
31 | | * |
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. |
58 | | * |
59 | | * __END_OF_JASPER_LICENSE__ |
60 | | */ |
61 | | |
62 | | /******************************************************************************\ |
63 | | * Includes. |
64 | | \******************************************************************************/ |
65 | | |
66 | | #include "pgx_enc.h" |
67 | | #include "pgx_cod.h" |
68 | | |
69 | | #include "jasper/jas_stream.h" |
70 | | #include "jasper/jas_image.h" |
71 | | #include "jasper/jas_debug.h" |
72 | | |
73 | | /******************************************************************************\ |
74 | | * Local functions. |
75 | | \******************************************************************************/ |
76 | | |
77 | | static int pgx_puthdr(jas_stream_t *out, pgx_hdr_t *hdr); |
78 | | static int pgx_putdata(jas_stream_t *out, pgx_hdr_t *hdr, jas_image_t *image, int cmpt); |
79 | | static int pgx_putword(jas_stream_t *out, bool bigendian, int prec, |
80 | | uint_fast32_t val); |
81 | | static uint_fast32_t pgx_inttoword(jas_seqent_t val, int prec, bool sgnd); |
82 | | |
83 | | /******************************************************************************\ |
84 | | * Code for save operation. |
85 | | \******************************************************************************/ |
86 | | |
87 | | /* Save an image to a stream in the the PGX format. */ |
88 | | |
89 | | int pgx_encode(jas_image_t *image, jas_stream_t *out, const char *optstr) |
90 | 225 | { |
91 | 225 | pgx_hdr_t hdr; |
92 | 225 | uint_fast32_t width; |
93 | 225 | uint_fast32_t height; |
94 | 225 | bool sgnd; |
95 | 225 | int prec; |
96 | 225 | pgx_enc_t encbuf; |
97 | 225 | pgx_enc_t *enc = &encbuf; |
98 | | |
99 | 225 | JAS_UNUSED(optstr); |
100 | | |
101 | 225 | switch (jas_clrspc_fam(jas_image_clrspc(image))) { |
102 | 225 | case JAS_CLRSPC_FAM_GRAY: |
103 | 225 | if ((enc->cmpt = jas_image_getcmptbytype(image, |
104 | 225 | JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y))) < 0) { |
105 | 0 | jas_logerrorf("error: missing color component\n"); |
106 | 0 | return -1; |
107 | 0 | } |
108 | 225 | break; |
109 | 225 | default: |
110 | 0 | jas_logerrorf("error: PGX format does not support color space\n"); |
111 | 0 | return -1; |
112 | 225 | } |
113 | | |
114 | 225 | width = jas_image_cmptwidth(image, enc->cmpt); |
115 | 225 | height = jas_image_cmptheight(image, enc->cmpt); |
116 | 225 | prec = jas_image_cmptprec(image, enc->cmpt); |
117 | 225 | sgnd = jas_image_cmptsgnd(image, enc->cmpt); |
118 | | |
119 | | /* The PGX format is quite limited in the set of image geometries |
120 | | that it can handle. Here, we check to ensure that the image to |
121 | | be saved can actually be represented reasonably accurately using the |
122 | | PGX format. */ |
123 | | /* There must be exactly one component. */ |
124 | 225 | if (jas_image_numcmpts(image) > 1 || prec > 16) { |
125 | 0 | jas_logerrorf("The PGX format cannot be used to represent an image with this geometry.\n"); |
126 | 0 | return -1; |
127 | 0 | } |
128 | | |
129 | 225 | hdr.magic = PGX_MAGIC; |
130 | 225 | hdr.bigendian = true; |
131 | 225 | hdr.sgnd = sgnd; |
132 | 225 | hdr.prec = prec; |
133 | 225 | hdr.width = width; |
134 | 225 | hdr.height = height; |
135 | | |
136 | 225 | if (jas_get_debug_level() >= 10) { |
137 | 0 | pgx_dumphdr(stderr, &hdr); |
138 | 0 | } |
139 | | |
140 | 225 | if (pgx_puthdr(out, &hdr)) { |
141 | 0 | return -1; |
142 | 0 | } |
143 | | |
144 | 225 | if (pgx_putdata(out, &hdr, image, enc->cmpt)) { |
145 | 0 | return -1; |
146 | 0 | } |
147 | | |
148 | 225 | return 0; |
149 | 225 | } |
150 | | |
151 | | /******************************************************************************\ |
152 | | \******************************************************************************/ |
153 | | |
154 | | static int pgx_puthdr(jas_stream_t *out, pgx_hdr_t *hdr) |
155 | 225 | { |
156 | 225 | jas_stream_printf(out, "%c%c", hdr->magic >> 8, hdr->magic & 0xff); |
157 | 225 | jas_stream_printf(out, " %s %s %d %ld %ld\n", hdr->bigendian ? "ML" : "LM", |
158 | 225 | hdr->sgnd ? "-" : "+", hdr->prec, (long) hdr->width, (long) hdr->height); |
159 | 225 | if (jas_stream_error(out)) { |
160 | 0 | return -1; |
161 | 0 | } |
162 | 225 | return 0; |
163 | 225 | } |
164 | | |
165 | | static int pgx_putdata(jas_stream_t *out, pgx_hdr_t *hdr, jas_image_t *image, int cmpt) |
166 | 225 | { |
167 | 225 | jas_matrix_t *data; |
168 | 225 | uint_fast32_t x; |
169 | 225 | uint_fast32_t y; |
170 | 225 | int_fast32_t v; |
171 | 225 | uint_fast32_t word; |
172 | | |
173 | 225 | data = 0; |
174 | | |
175 | 225 | if (!(data = jas_matrix_create(1, hdr->width))) { |
176 | 0 | goto error; |
177 | 0 | } |
178 | 52.6k | for (y = 0; y < hdr->height; ++y) { |
179 | 52.3k | if (jas_image_readcmpt(image, cmpt, 0, y, hdr->width, 1, data)) { |
180 | 0 | goto error; |
181 | 0 | } |
182 | 2.24M | for (x = 0; x < hdr->width; ++x) { |
183 | 2.19M | v = jas_matrix_get(data, 0, x); |
184 | 2.19M | word = pgx_inttoword(v, hdr->prec, hdr->sgnd); |
185 | 2.19M | if (pgx_putword(out, hdr->bigendian, hdr->prec, word)) { |
186 | 0 | goto error; |
187 | 0 | } |
188 | 2.19M | } |
189 | 52.3k | } |
190 | 225 | jas_matrix_destroy(data); |
191 | 225 | data = 0; |
192 | 225 | return 0; |
193 | | |
194 | 0 | error: |
195 | 0 | if (data) { |
196 | 0 | jas_matrix_destroy(data); |
197 | 0 | } |
198 | 0 | return -1; |
199 | 225 | } |
200 | | |
201 | | static int pgx_putword(jas_stream_t *out, bool bigendian, int prec, |
202 | | uint_fast32_t val) |
203 | 2.19M | { |
204 | 2.19M | int i; |
205 | 2.19M | int j; |
206 | 2.19M | int wordsize; |
207 | | |
208 | 2.19M | val &= (1 << prec) - 1; |
209 | 2.19M | wordsize = (prec + 7) /8; |
210 | 5.73M | for (i = 0; i < wordsize; ++i) { |
211 | 3.54M | j = bigendian ? (wordsize - 1 - i) : i; |
212 | 3.54M | if (jas_stream_putc(out, (val >> (8 * j)) & 0xff) == EOF) { |
213 | 0 | return -1; |
214 | 0 | } |
215 | 3.54M | } |
216 | 2.19M | return 0; |
217 | 2.19M | } |
218 | | |
219 | | static uint_fast32_t pgx_inttoword(jas_seqent_t v, int prec, bool sgnd) |
220 | 2.19M | { |
221 | 2.19M | uint_fast32_t ret; |
222 | 2.19M | ret = ((sgnd && v < 0) ? (JAS_POW2_X(jas_seqent_t, prec) + v) : v) & |
223 | 2.19M | ((1 << prec) - 1); |
224 | 2.19M | return ret; |
225 | 2.19M | } |