]> Creatis software - gdcm.git/blob - src/gdcmjasper/src/libjasper/ras/ras_enc.c
ENH: Ok since OJ warnings are not going to be fixed anytime soon remove them
[gdcm.git] / src / gdcmjasper / src / libjasper / ras / ras_enc.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_enc.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_image.h"
78 #include "jasper/jas_stream.h"
79 #include "jasper/jas_debug.h"
80
81 #include "ras_cod.h"
82 #include "ras_enc.h"
83
84 /******************************************************************************\
85 * Prototypes.
86 \******************************************************************************/
87
88 static int ras_puthdr(jas_stream_t *out, ras_hdr_t *hdr);
89 static int ras_putint(jas_stream_t *out, int val);
90
91 static int ras_putdata(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image, int numcmpts, int *cmpts);
92 static int ras_putdatastd(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image, int numcmpts, int *cmpts);
93
94 /******************************************************************************\
95 * Code.
96 \******************************************************************************/
97
98 int ras_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
99 {
100   int_fast32_t width;
101   int_fast32_t height;
102   int_fast32_t depth;
103   int cmptno;
104 #if 0
105   uint_fast16_t numcmpts;
106 #endif
107   int i;
108   ras_hdr_t hdr;
109   int rowsize;
110   ras_enc_t encbuf;
111   ras_enc_t *enc = &encbuf;
112
113   if (optstr) {
114     fprintf(stderr, "warning: ignoring RAS encoder options\n");
115   }
116
117   switch (jas_clrspc_fam(jas_image_clrspc(image))) {
118   case JAS_CLRSPC_FAM_RGB:
119     if (jas_image_clrspc(image) != JAS_CLRSPC_SRGB)
120       jas_eprintf("warning: inaccurate color\n");
121     enc->numcmpts = 3;
122     if ((enc->cmpts[0] = jas_image_getcmptbytype(image,
123       JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 ||
124       (enc->cmpts[1] = jas_image_getcmptbytype(image,
125       JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G))) < 0 ||
126       (enc->cmpts[2] = jas_image_getcmptbytype(image,
127       JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))) < 0) {
128       jas_eprintf("error: missing color component\n");
129       return -1;
130     }
131     break;
132   case JAS_CLRSPC_FAM_GRAY:
133     if (jas_image_clrspc(image) != JAS_CLRSPC_SGRAY)
134       jas_eprintf("warning: inaccurate color\n");
135     enc->numcmpts = 1;
136     if ((enc->cmpts[0] = jas_image_getcmptbytype(image,
137       JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y))) < 0) {
138       jas_eprintf("error: missing color component\n");
139       return -1;
140     }
141     break;
142   default:
143     jas_eprintf("error: unsupported color space\n");
144     return -1;
145     break;
146   }
147
148   width = jas_image_cmptwidth(image, enc->cmpts[0]);
149   height = jas_image_cmptheight(image, enc->cmpts[0]);
150   depth = jas_image_cmptprec(image, enc->cmpts[0]);
151
152   for (cmptno = 0; cmptno < enc->numcmpts; ++cmptno) {
153     if (jas_image_cmptwidth(image, enc->cmpts[cmptno]) != width ||
154       jas_image_cmptheight(image, enc->cmpts[cmptno]) != height ||
155       jas_image_cmptprec(image, enc->cmpts[cmptno]) != depth ||
156       jas_image_cmptsgnd(image, enc->cmpts[cmptno]) != false ||
157       jas_image_cmpttlx(image, enc->cmpts[cmptno]) != 0 ||
158       jas_image_cmpttly(image, enc->cmpts[cmptno]) != 0) {
159       fprintf(stderr, "The RAS format cannot be used to represent an image with this geometry.\n");
160       return -1;
161     }
162   }
163
164   /* Ensure that the image can be encoded in the desired format. */
165   if (enc->numcmpts == 3) {
166
167     /* All three components must have the same subsampling
168       factor and have a precision of eight bits. */
169     if (enc->numcmpts > 1) {
170       for (i = 0; i < enc->numcmpts; ++i) {
171         if (jas_image_cmptprec(image, enc->cmpts[i]) != 8) {
172           return -1;
173         }
174       }
175     }
176   } else if (enc->numcmpts == 1) {
177     /* NOP */
178   } else {
179     return -1;
180   }
181
182   hdr.magic = RAS_MAGIC;
183   hdr.width = width;
184   hdr.height = height;
185   hdr.depth = (enc->numcmpts == 3) ? 24 : depth;
186
187   rowsize = RAS_ROWSIZE(&hdr);
188   hdr.length = rowsize * hdr.height;
189   hdr.type = RAS_TYPE_STD;
190
191   hdr.maptype = RAS_MT_NONE;
192   hdr.maplength = 0;
193
194   if (ras_puthdr(out, &hdr)) {
195     return -1;
196   }
197
198   if (ras_putdata(out, &hdr, image, enc->numcmpts, enc->cmpts)) {
199     return -1;
200   }
201
202   return 0;
203 }
204
205 static int ras_putdata(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image, int numcmpts, int *cmpts)
206 {
207   int ret;
208
209   switch (hdr->type) {
210   case RAS_TYPE_STD:
211     ret = ras_putdatastd(out, hdr, image, numcmpts, cmpts);
212     break;
213   default:
214     ret = -1;
215     break;
216   }
217   return ret;
218 }
219
220 static int ras_putdatastd(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image,  int numcmpts, int *cmpts)
221 {
222   int rowsize;
223   int pad;
224   unsigned int z;
225   int nz;
226   int c;
227   int x;
228   int y;
229   int v;
230   jas_matrix_t *data[3];
231   int i;
232
233   for (i = 0; i < numcmpts; ++i) {
234     data[i] = jas_matrix_create(jas_image_height(image), jas_image_width(image));
235     assert(data[i]);
236   }
237
238   rowsize = RAS_ROWSIZE(hdr);
239   pad = rowsize - (hdr->width * hdr->depth + 7) / 8;
240
241   hdr->length = hdr->height * rowsize;
242
243   for (y = 0; y < hdr->height; y++) {
244     for (i = 0; i < numcmpts; ++i) {
245       jas_image_readcmpt(image, cmpts[i], 0, y, jas_image_width(image),
246         1, data[i]);
247     }
248     z = 0;
249     nz = 0;
250     for (x = 0; x < hdr->width; x++) {
251       z <<= hdr->depth;
252       if (RAS_ISRGB(hdr)) {
253         v = RAS_RED((jas_matrix_getv(data[0], x))) |
254           RAS_GREEN((jas_matrix_getv(data[1], x))) |
255           RAS_BLUE((jas_matrix_getv(data[2], x)));
256       } else {
257         v = (jas_matrix_getv(data[0], x));
258       }
259       z |= v & RAS_ONES(hdr->depth);
260       nz += hdr->depth;
261       while (nz >= 8) {
262         c = (z >> (nz - 8)) & 0xff;
263         if (jas_stream_putc(out, c) == EOF) {
264           return -1;
265         }
266         nz -= 8;
267         z &= RAS_ONES(nz);
268       }
269     }
270     if (nz > 0) {
271       c = (z >> (8 - nz)) & RAS_ONES(nz);
272       if (jas_stream_putc(out, c) == EOF) {
273         return -1;
274       }
275     }
276     if (pad % 2) {
277       if (jas_stream_putc(out, 0) == EOF) {
278         return -1;
279       }
280     }
281   }
282
283   for (i = 0; i < numcmpts; ++i) {
284     jas_matrix_destroy(data[i]);
285   }
286
287   return 0;
288 }
289
290 static int ras_puthdr(jas_stream_t *out, ras_hdr_t *hdr)
291 {
292   if (ras_putint(out, RAS_MAGIC) || ras_putint(out, hdr->width) ||
293     ras_putint(out, hdr->height) || ras_putint(out, hdr->depth) ||
294     ras_putint(out, hdr->length) || ras_putint(out, hdr->type) ||
295     ras_putint(out, hdr->maptype) || ras_putint(out, hdr->maplength)) {
296     return -1;
297   }
298
299   return 0;
300 }
301
302 static int ras_putint(jas_stream_t *out, int val)
303 {
304   int x;
305   int i;
306   int c;
307
308   x = val;
309   for (i = 0; i < 4; i++) {
310     c = (x >> 24) & 0xff;
311     if (jas_stream_putc(out, c) == EOF) {
312       return -1;
313     }
314     x <<= 8;
315   }
316
317   return 0;
318 }