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_enc.c,v 1.1 2005/05/22 18:33:10 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
77 #include "jasper/jas_image.h"
78 #include "jasper/jas_stream.h"
79 #include "jasper/jas_debug.h"
84 /******************************************************************************\
86 \******************************************************************************/
88 static int ras_puthdr(jas_stream_t *out, ras_hdr_t *hdr);
89 static int ras_putint(jas_stream_t *out, int val);
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);
94 /******************************************************************************\
96 \******************************************************************************/
98 int ras_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
105 uint_fast16_t numcmpts;
111 ras_enc_t *enc = &encbuf;
114 fprintf(stderr, "warning: ignoring RAS encoder options\n");
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");
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");
132 case JAS_CLRSPC_FAM_GRAY:
133 if (jas_image_clrspc(image) != JAS_CLRSPC_SGRAY)
134 jas_eprintf("warning: inaccurate color\n");
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");
143 jas_eprintf("error: unsupported color space\n");
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]);
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");
164 /* Ensure that the image can be encoded in the desired format. */
165 if (enc->numcmpts == 3) {
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) {
176 } else if (enc->numcmpts == 1) {
182 hdr.magic = RAS_MAGIC;
185 hdr.depth = (enc->numcmpts == 3) ? 24 : depth;
187 rowsize = RAS_ROWSIZE(&hdr);
188 hdr.length = rowsize * hdr.height;
189 hdr.type = RAS_TYPE_STD;
191 hdr.maptype = RAS_MT_NONE;
194 if (ras_puthdr(out, &hdr)) {
198 if (ras_putdata(out, &hdr, image, enc->numcmpts, enc->cmpts)) {
205 static int ras_putdata(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image, int numcmpts, int *cmpts)
211 ret = ras_putdatastd(out, hdr, image, numcmpts, cmpts);
220 static int ras_putdatastd(jas_stream_t *out, ras_hdr_t *hdr, jas_image_t *image, int numcmpts, int *cmpts)
230 jas_matrix_t *data[3];
233 for (i = 0; i < numcmpts; ++i) {
234 data[i] = jas_matrix_create(jas_image_height(image), jas_image_width(image));
238 rowsize = RAS_ROWSIZE(hdr);
239 pad = rowsize - (hdr->width * hdr->depth + 7) / 8;
241 hdr->length = hdr->height * rowsize;
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),
250 for (x = 0; x < hdr->width; x++) {
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)));
257 v = (jas_matrix_getv(data[0], x));
259 z |= v & RAS_ONES(hdr->depth);
262 c = (z >> (nz - 8)) & 0xff;
263 if (jas_stream_putc(out, c) == EOF) {
271 c = (z >> (8 - nz)) & RAS_ONES(nz);
272 if (jas_stream_putc(out, c) == EOF) {
277 if (jas_stream_putc(out, 0) == EOF) {
283 for (i = 0; i < numcmpts; ++i) {
284 jas_matrix_destroy(data[i]);
290 static int ras_puthdr(jas_stream_t *out, ras_hdr_t *hdr)
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)) {
302 static int ras_putint(jas_stream_t *out, int val)
309 for (i = 0; i < 4; i++) {
310 c = (x >> 24) & 0xff;
311 if (jas_stream_putc(out, c) == EOF) {