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 * Portable Pixmap/Graymap Format Support
67 * $Id: pnm_enc.c,v 1.1 2005/05/22 18:33:09 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
79 #include "jasper/jas_types.h"
80 #include "jasper/jas_tvp.h"
81 #include "jasper/jas_image.h"
82 #include "jasper/jas_stream.h"
83 #include "jasper/jas_debug.h"
88 /******************************************************************************\
90 \******************************************************************************/
100 jas_taginfo_t pnm_opttab[] = {
105 /******************************************************************************\
106 * Local function prototypes.
107 \******************************************************************************/
109 static int pnm_parseencopts(char *optstr, pnm_encopts_t *encopts);
110 static int pnm_puthdr(jas_stream_t *out, pnm_hdr_t *hdr);
111 static int pnm_putdata(jas_stream_t *out, pnm_hdr_t *hdr, jas_image_t *image, int numcmpts, int *cmpts);
113 static int pnm_putsint(jas_stream_t *out, int wordsize, int_fast32_t *val);
114 static int pnm_putuint(jas_stream_t *out, int wordsize, uint_fast32_t *val);
115 static int pnm_putuint16(jas_stream_t *out, uint_fast16_t val);
117 /******************************************************************************\
119 \******************************************************************************/
121 int pnm_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
127 pnm_encopts_t encopts;
131 pnm_enc_t *enc = &encbuf;
133 /* Parse the encoder option string. */
134 if (pnm_parseencopts(optstr, &encopts)) {
135 fprintf(stderr, "invalid PNM encoder options specified\n");
139 switch (jas_clrspc_fam(jas_image_clrspc(image))) {
140 case JAS_CLRSPC_FAM_RGB:
141 if (jas_image_clrspc(image) != JAS_CLRSPC_SRGB)
142 jas_eprintf("warning: inaccurate color\n");
144 if ((enc->cmpts[0] = jas_image_getcmptbytype(image,
145 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 ||
146 (enc->cmpts[1] = jas_image_getcmptbytype(image,
147 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G))) < 0 ||
148 (enc->cmpts[2] = jas_image_getcmptbytype(image,
149 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))) < 0) {
150 jas_eprintf("error: missing color component\n");
154 case JAS_CLRSPC_FAM_GRAY:
155 if (jas_image_clrspc(image) != JAS_CLRSPC_SGRAY)
156 jas_eprintf("warning: inaccurate color\n");
158 if ((enc->cmpts[0] = jas_image_getcmptbytype(image,
159 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y))) < 0) {
160 jas_eprintf("error: missing color component\n");
165 jas_eprintf("error: unsupported color space\n");
171 width = jas_image_cmptwidth(image, enc->cmpts[0]);
172 height = jas_image_cmptheight(image, enc->cmpts[0]);
173 prec = jas_image_cmptprec(image, enc->cmpts[0]);
174 sgnd = jas_image_cmptsgnd(image, enc->cmpts[0]);
176 /* The PNM format is quite limited in the set of image geometries
177 that it can handle. Here, we check to ensure that the image to
178 be saved can actually be represented reasonably accurately using the
180 /* All of the components must have the same width and height. */
181 /* All of the components must have unsigned samples with the same
183 /* All of the components must have their top-left corner located at
185 for (cmptno = 0; cmptno < enc->numcmpts; ++cmptno) {
186 if (jas_image_cmptwidth(image, enc->cmpts[cmptno]) != width ||
187 jas_image_cmptheight(image, enc->cmpts[cmptno]) != height ||
188 jas_image_cmptprec(image, enc->cmpts[cmptno]) != prec ||
189 jas_image_cmptsgnd(image, enc->cmpts[cmptno]) != sgnd ||
190 jas_image_cmpthstep(image, enc->cmpts[cmptno]) != jas_image_cmpthstep(image, 0) ||
191 jas_image_cmptvstep(image, enc->cmpts[cmptno]) != jas_image_cmptvstep(image, 0) ||
192 jas_image_cmpttlx(image, enc->cmpts[cmptno]) != jas_image_cmpttlx(image, 0) ||
193 jas_image_cmpttly(image, enc->cmpts[cmptno]) != jas_image_cmpttly(image, 0)) {
194 fprintf(stderr, "The PNM format cannot be used to represent an image with this geometry.\n");
200 fprintf(stderr, "warning: support for signed sample data requires use of nonstandard extension to PNM format\n");
201 fprintf(stderr, "You may not be able to read or correctly display the resulting PNM data with other software.\n");
204 /* Initialize the header. */
205 if (enc->numcmpts == 1) {
206 hdr.magic = encopts.bin ? PNM_MAGIC_BINPGM : PNM_MAGIC_TXTPGM;
207 } else if (enc->numcmpts == 3) {
208 hdr.magic = encopts.bin ? PNM_MAGIC_BINPPM : PNM_MAGIC_TXTPPM;
214 hdr.maxval = (1 << prec) - 1;
217 /* Write the header. */
218 if (pnm_puthdr(out, &hdr)) {
222 /* Write the image data. */
223 if (pnm_putdata(out, &hdr, image, enc->numcmpts, enc->cmpts)) {
227 /* Flush the output stream. */
228 if (jas_stream_flush(out)) {
235 /******************************************************************************\
236 * Code for parsing options.
237 \******************************************************************************/
239 /* Parse the encoder options string. */
240 static int pnm_parseencopts(char *optstr, pnm_encopts_t *encopts)
247 /* Initialize default values for encoder options. */
250 /* Create the tag-value parser. */
251 if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) {
255 /* Get tag-value pairs, and process as necessary. */
256 while (!(ret = jas_tvparser_next(tvp))) {
257 switch (jas_taginfo_nonull(jas_taginfos_lookup(pnm_opttab,
258 jas_tvparser_gettag(tvp)))->id) {
260 encopts->bin = false;
263 fprintf(stderr, "warning: ignoring invalid option %s\n",
264 jas_tvparser_gettag(tvp));
272 /* Destroy the tag-value parser. */
273 jas_tvparser_destroy(tvp);
279 jas_tvparser_destroy(tvp);
284 /******************************************************************************\
285 * Function for writing header.
286 \******************************************************************************/
288 /* Write the header. */
289 static int pnm_puthdr(jas_stream_t *out, pnm_hdr_t *hdr)
293 if (pnm_putuint16(out, hdr->magic)) {
297 maxval = -hdr->maxval;
299 maxval = hdr->maxval;
301 jas_stream_printf(out, "\n%lu %lu\n%ld\n", (unsigned long) hdr->width,
302 (unsigned long) hdr->height, (long) maxval);
303 if (jas_stream_error(out)) {
309 /******************************************************************************\
310 * Functions for processing the sample data.
311 \******************************************************************************/
313 /* Write the image sample data. */
314 static int pnm_putdata(jas_stream_t *out, pnm_hdr_t *hdr, jas_image_t *image, int numcmpts, int *cmpts)
320 jas_matrix_t *data[3];
331 fmt = pnm_fmt(hdr->magic);
332 minval = -((int) hdr->maxval + 1);
333 depth = pnm_maxvaltodepth(hdr->maxval);
338 for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
339 if (!(data[cmptno] = jas_matrix_create(1, hdr->width))) {
344 for (y = 0; y < hdr->height; ++y) {
345 for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
346 if (jas_image_readcmpt(image, cmpts[cmptno], 0, y, hdr->width, 1,
350 d[cmptno] = jas_matrix_getref(data[cmptno], 0, 0);
353 for (x = 0; x < hdr->width; ++x) {
354 for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
359 if (v > ((int) hdr->maxval)) {
362 if (fmt == PNM_FMT_BIN) {
366 if (pnm_putsint(out, depth, &sv)) {
372 if (pnm_putuint(out, depth, &uv)) {
377 n = sprintf(buf, "%s%ld", ((!(!x && !cmptno)) ? " " : ""),
379 if (linelen > 0 && linelen + n > PNM_MAXLINELEN) {
380 jas_stream_printf(out, "\n");
383 jas_stream_printf(out, "%s", buf);
389 if (fmt != PNM_FMT_BIN) {
390 jas_stream_printf(out, "\n");
393 if (jas_stream_error(out)) {
402 for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
404 jas_matrix_destroy(data[cmptno]);
411 /******************************************************************************\
412 * Miscellaneous functions.
413 \******************************************************************************/
415 static int pnm_putsint(jas_stream_t *out, int wordsize, int_fast32_t *val)
417 uint_fast32_t tmpval;
418 tmpval = (*val < 0) ?
419 ((~(JAS_CAST(uint_fast32_t, -(*val)) + 1)) & PNM_ONES(wordsize)) :
420 JAS_CAST(uint_fast32_t, (*val));
421 return pnm_putuint(out, wordsize, &tmpval);
424 static int pnm_putuint(jas_stream_t *out, int wordsize, uint_fast32_t *val)
427 uint_fast32_t tmpval;
430 n = (wordsize + 7) / 8;
431 tmpval &= PNM_ONES(8 * n);
432 tmpval = (*val) << (8 * (4 - n));
434 c = (tmpval >> 24) & 0xff;
435 if (jas_stream_putc(out, c) == EOF) {
438 tmpval = (tmpval << 8) & 0xffffffff;
443 /* Write a 16-bit unsigned integer to a stream. */
444 static int pnm_putuint16(jas_stream_t *out, uint_fast16_t val)
446 if (jas_stream_putc(out, (unsigned char)(val >> 8)) == EOF ||
447 jas_stream_putc(out, (unsigned char)(val & 0xff)) == EOF) {