2 * Copyright (c) 2001-2002 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 \******************************************************************************/
68 #include "jasper/jas_tvp.h"
69 #include "jasper/jas_stream.h"
70 #include "jasper/jas_image.h"
71 #include "jasper/jas_string.h"
72 #include "jasper/jas_malloc.h"
76 /******************************************************************************\
78 \******************************************************************************/
97 /******************************************************************************\
99 \******************************************************************************/
101 static mif_hdr_t *mif_hdr_create(int maxcmpts);
102 static void mif_hdr_destroy(mif_hdr_t *hdr);
103 static int mif_hdr_growcmpts(mif_hdr_t *hdr, int maxcmpts);
104 static mif_hdr_t *mif_hdr_get(jas_stream_t *in);
105 static int mif_process_cmpt(mif_hdr_t *hdr, char *buf);
106 static int mif_hdr_put(mif_hdr_t *hdr, jas_stream_t *out);
107 static int mif_hdr_addcmpt(mif_hdr_t *hdr, int cmptno, mif_cmpt_t *cmpt);
108 static mif_cmpt_t *mif_cmpt_create(void);
109 static void mif_cmpt_destroy(mif_cmpt_t *cmpt);
110 static char *mif_getline(jas_stream_t *jas_stream, char *buf, int bufsize);
111 static int mif_getc(jas_stream_t *in);
112 static mif_hdr_t *mif_makehdrfromimage(jas_image_t *image);
114 /******************************************************************************\
116 \******************************************************************************/
118 jas_taginfo_t mif_tags2[] = {
119 {MIF_CMPT, "component"},
124 jas_taginfo_t mif_tags[] = {
127 {MIF_WIDTH, "width"},
128 {MIF_HEIGHT, "height"},
129 {MIF_HSAMP, "sampperx"},
130 {MIF_VSAMP, "samppery"},
137 /******************************************************************************\
138 * Code for load operation.
139 \******************************************************************************/
141 /* Load an image from a stream in the MIF format. */
143 jas_image_t *mif_decode(jas_stream_t *in, char *optstr)
147 jas_image_t *tmpimage;
148 jas_stream_t *tmpstream;
151 jas_image_cmptparm_t cmptparm;
157 /* Avoid warnings about unused parameters. */
166 if (!(hdr = mif_hdr_get(in))) {
170 if (!(image = jas_image_create0())) {
174 for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) {
175 cmpt = hdr->cmpts[cmptno];
176 tmpstream = cmpt->data ? jas_stream_fopen(cmpt->data, "rb") : in;
180 if (!(tmpimage = jas_image_decode(tmpstream, -1, 0))) {
183 if (tmpstream != in) {
184 jas_stream_close(tmpstream);
188 cmpt->width = jas_image_cmptwidth(tmpimage, 0);
191 cmpt->height = jas_image_cmptwidth(tmpimage, 0);
194 cmpt->prec = jas_image_cmptprec(tmpimage, 0);
196 if (cmpt->sgnd < 0) {
197 cmpt->sgnd = jas_image_cmptsgnd(tmpimage, 0);
199 cmptparm.tlx = cmpt->tlx;
200 cmptparm.tly = cmpt->tly;
201 cmptparm.hstep = cmpt->sampperx;
202 cmptparm.vstep = cmpt->samppery;
203 cmptparm.width = cmpt->width;
204 cmptparm.height = cmpt->height;
205 cmptparm.prec = cmpt->prec;
206 cmptparm.sgnd = cmpt->sgnd;
207 if (jas_image_addcmpt(image, jas_image_numcmpts(image), &cmptparm)) {
210 if (!(data = jas_seq2d_create(0, 0, cmpt->width, cmpt->height))) {
213 if (jas_image_readcmpt(tmpimage, 0, 0, 0, cmpt->width, cmpt->height,
218 bias = 1 << (cmpt->prec - 1);
219 for (y = 0; y < cmpt->height; ++y) {
220 for (x = 0; x < cmpt->width; ++x) {
221 *jas_seq2d_getref(data, x, y) -= bias;
225 if (jas_image_writecmpt(image, jas_image_numcmpts(image) - 1, 0, 0,
226 cmpt->width, cmpt->height, data)) {
229 jas_seq2d_destroy(data);
231 jas_image_destroy(tmpimage);
235 mif_hdr_destroy(hdr);
241 jas_image_destroy(image);
244 mif_hdr_destroy(hdr);
246 if (tmpstream && tmpstream != in) {
247 jas_stream_close(tmpstream);
250 jas_image_destroy(tmpimage);
253 jas_seq2d_destroy(data);
258 /******************************************************************************\
259 * Code for save operation.
260 \******************************************************************************/
262 /* Save an image to a stream in the the MIF format. */
264 int mif_encode(jas_image_t *image, jas_stream_t *out, char *optstr)
267 jas_image_t *tmpimage;
271 jas_image_cmptparm_t cmptparm;
281 if (optstr && *optstr != '\0') {
282 fprintf(stderr, "warning: ignoring unsupported options\n");
285 if ((fmt = jas_image_strtofmt("pnm")) < 0) {
286 fprintf(stderr, "error: PNM support required\n");
290 if (!(hdr = mif_makehdrfromimage(image))) {
293 if (mif_hdr_put(hdr, out)) {
297 /* Output component data. */
298 for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) {
299 cmpt = hdr->cmpts[cmptno];
301 if (!(tmpimage = jas_image_create0())) {
306 cmptparm.hstep = cmpt->sampperx;
307 cmptparm.vstep = cmpt->samppery;
308 cmptparm.width = cmpt->width;
309 cmptparm.height = cmpt->height;
310 cmptparm.prec = cmpt->prec;
311 cmptparm.sgnd = false;
312 if (jas_image_addcmpt(tmpimage, jas_image_numcmpts(tmpimage), &cmptparm)) {
315 if (!(data = jas_seq2d_create(0, 0, cmpt->width, cmpt->height))) {
318 if (jas_image_readcmpt(image, cmptno, 0, 0, cmpt->width, cmpt->height,
323 bias = 1 << (cmpt->prec - 1);
324 for (y = 0; y < cmpt->height; ++y) {
325 for (x = 0; x < cmpt->width; ++x) {
326 *jas_seq2d_getref(data, x, y) += bias;
330 if (jas_image_writecmpt(tmpimage, 0, 0, 0, cmpt->width, cmpt->height,
334 jas_seq2d_destroy(data);
336 if (jas_image_encode(tmpimage, out, fmt, 0)) {
339 jas_image_destroy(tmpimage);
344 mif_hdr_destroy(hdr);
350 mif_hdr_destroy(hdr);
353 jas_image_destroy(tmpimage);
356 jas_seq2d_destroy(data);
361 /******************************************************************************\
362 * Code for validate operation.
363 \******************************************************************************/
365 int mif_validate(jas_stream_t *in)
367 uchar buf[MIF_MAGICLEN];
372 assert(JAS_STREAM_MAXPUTBACK >= MIF_MAGICLEN);
374 /* Read the validation data (i.e., the data used for detecting
376 if ((n = jas_stream_read(in, buf, MIF_MAGICLEN)) < 0) {
380 /* Put the validation data back onto the stream, so that the
381 stream position will not be changed. */
382 for (i = n - 1; i >= 0; --i) {
383 if (jas_stream_ungetc(in, buf[i]) == EOF) {
388 /* Was enough data read? */
389 if (n < MIF_MAGICLEN) {
393 /* Compute the signature value. */
394 magic = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
396 /* Ensure that the signature is correct for this format. */
397 if (magic != MIF_MAGIC) {
404 /******************************************************************************\
405 * Code for MIF header class.
406 \******************************************************************************/
408 static mif_hdr_t *mif_hdr_create(int maxcmpts)
411 if (!(hdr = jas_malloc(sizeof(mif_hdr_t)))) {
417 if (mif_hdr_growcmpts(hdr, maxcmpts)) {
418 mif_hdr_destroy(hdr);
424 static void mif_hdr_destroy(mif_hdr_t *hdr)
428 for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) {
429 mif_cmpt_destroy(hdr->cmpts[cmptno]);
431 jas_free(hdr->cmpts);
436 static int mif_hdr_growcmpts(mif_hdr_t *hdr, int maxcmpts)
439 mif_cmpt_t **newcmpts;
440 assert(maxcmpts >= hdr->numcmpts);
441 newcmpts = (!hdr->cmpts) ? jas_malloc(maxcmpts * sizeof(mif_cmpt_t *)) :
442 jas_realloc(hdr->cmpts, maxcmpts * sizeof(mif_cmpt_t *));
446 hdr->maxcmpts = maxcmpts;
447 hdr->cmpts = newcmpts;
448 for (cmptno = hdr->numcmpts; cmptno < hdr->maxcmpts; ++cmptno) {
449 hdr->cmpts[cmptno] = 0;
454 static mif_hdr_t *mif_hdr_get(jas_stream_t *in)
456 uchar magicbuf[MIF_MAGICLEN];
465 if (jas_stream_read(in, magicbuf, MIF_MAGICLEN) != MIF_MAGICLEN) {
468 if (magicbuf[0] != (MIF_MAGIC >> 24) || magicbuf[1] != ((MIF_MAGIC >> 16) &
469 0xff) || magicbuf[2] != ((MIF_MAGIC >> 8) & 0xff) || magicbuf[3] !=
470 (MIF_MAGIC & 0xff)) {
471 fprintf(stderr, "error: bad signature\n");
475 if (!(hdr = mif_hdr_create(0))) {
481 if (!mif_getline(in, buf, sizeof(buf))) {
484 if (buf[0] == '\0') {
487 if (!(tvp = jas_tvparser_create(buf))) {
490 if (jas_tvparser_next(tvp)) {
493 id = jas_taginfo_nonull(jas_taginfos_lookup(mif_tags2, jas_tvparser_gettag(tvp)))->id;
494 jas_tvparser_destroy(tvp);
497 mif_process_cmpt(hdr, buf);
509 mif_hdr_destroy(hdr);
514 static int mif_process_cmpt(mif_hdr_t *hdr, char *buf)
523 if (!(cmpt = mif_cmpt_create())) {
536 if (!(tvp = jas_tvparser_create(buf))) {
539 while (!(id = jas_tvparser_next(tvp))) {
540 switch (jas_taginfo_nonull(jas_taginfos_lookup(mif_tags,
541 jas_tvparser_gettag(tvp)))->id) {
543 cmpt->tlx = atoi(jas_tvparser_getval(tvp));
546 cmpt->tly = atoi(jas_tvparser_getval(tvp));
549 cmpt->width = atoi(jas_tvparser_getval(tvp));
552 cmpt->height = atoi(jas_tvparser_getval(tvp));
555 cmpt->sampperx = atoi(jas_tvparser_getval(tvp));
558 cmpt->samppery = atoi(jas_tvparser_getval(tvp));
561 cmpt->prec = atoi(jas_tvparser_getval(tvp));
564 cmpt->sgnd = atoi(jas_tvparser_getval(tvp));
567 if (!(cmpt->data = jas_strdup(jas_tvparser_getval(tvp)))) {
573 jas_tvparser_destroy(tvp);
574 if (!cmpt->sampperx || !cmpt->samppery) {
577 if (mif_hdr_addcmpt(hdr, hdr->numcmpts, cmpt)) {
580 (void)id; /* is is assigned a value that is never used */
585 mif_cmpt_destroy(cmpt);
588 jas_tvparser_destroy(tvp);
593 static int mif_hdr_put(mif_hdr_t *hdr, jas_stream_t *out)
598 /* Output signature. */
599 jas_stream_putc(out, (MIF_MAGIC >> 24) & 0xff);
600 jas_stream_putc(out, (MIF_MAGIC >> 16) & 0xff);
601 jas_stream_putc(out, (MIF_MAGIC >> 8) & 0xff);
602 jas_stream_putc(out, MIF_MAGIC & 0xff);
604 /* Output component information. */
605 for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) {
606 cmpt = hdr->cmpts[cmptno];
607 jas_stream_printf(out, "component tlx=%ld tly=%ld "
608 "sampperx=%ld samppery=%ld width=%ld height=%ld prec=%d sgnd=%d",
609 cmpt->tlx, cmpt->tly, cmpt->sampperx, cmpt->samppery, cmpt->width,
610 cmpt->height, cmpt->prec, cmpt->sgnd);
612 jas_stream_printf(out, " data=%s", cmpt->data);
614 jas_stream_printf(out, "\n");
617 /* Output end of header indicator. */
618 jas_stream_printf(out, "end\n");
623 static int mif_hdr_addcmpt(mif_hdr_t *hdr, int cmptno, mif_cmpt_t *cmpt)
625 assert(cmptno >= hdr->numcmpts);
626 if (hdr->numcmpts >= hdr->maxcmpts) {
627 if (mif_hdr_growcmpts(hdr, hdr->numcmpts + 128)) {
631 hdr->cmpts[hdr->numcmpts] = cmpt;
636 /******************************************************************************\
637 * Code for MIF component class.
638 \******************************************************************************/
640 static mif_cmpt_t *mif_cmpt_create()
643 if (!(cmpt = jas_malloc(sizeof(mif_cmpt_t)))) {
646 memset(cmpt, 0, sizeof(mif_cmpt_t));
650 static void mif_cmpt_destroy(mif_cmpt_t *cmpt)
653 jas_free(cmpt->data);
658 /******************************************************************************\
660 \******************************************************************************/
662 static char *mif_getline(jas_stream_t *stream, char *buf, int bufsize)
669 while (bufsize > 1) {
670 if ((c = mif_getc(stream)) == EOF) {
680 if (!(bufptr = strchr(buf, '\n'))) {
687 static int mif_getc(jas_stream_t *in)
694 switch (c = jas_stream_getc(in)) {
700 if ((c = jas_stream_getc(in)) == EOF) {
710 if (jas_stream_peekc(in) == '\n') {
723 /******************************************************************************\
724 * Miscellaneous functions.
725 \******************************************************************************/
727 static mif_hdr_t *mif_makehdrfromimage(jas_image_t *image)
733 if (!(hdr = mif_hdr_create(jas_image_numcmpts(image)))) {
736 hdr->magic = MIF_MAGIC;
737 hdr->numcmpts = jas_image_numcmpts(image);
738 for (cmptno = 0; cmptno < hdr->numcmpts; ++cmptno) {
739 hdr->cmpts[cmptno] = jas_malloc(sizeof(mif_cmpt_t));
740 cmpt = hdr->cmpts[cmptno];
741 cmpt->tlx = jas_image_cmpttlx(image, cmptno);
742 cmpt->tly = jas_image_cmpttly(image, cmptno);
743 cmpt->width = jas_image_cmptwidth(image, cmptno);
744 cmpt->height = jas_image_cmptheight(image, cmptno);
745 cmpt->sampperx = jas_image_cmpthstep(image, cmptno);
746 cmpt->samppery = jas_image_cmptvstep(image, cmptno);
747 cmpt->prec = jas_image_cmptprec(image, cmptno);
748 cmpt->sgnd = jas_image_cmptsgnd(image, cmptno);