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__
67 * $Id: jas_image.c,v 1.1 2005/05/22 18:32:59 malaterre Exp $
70 /******************************************************************************\
72 \******************************************************************************/
80 #include "jasper/jas_math.h"
81 #include "jasper/jas_image.h"
82 #include "jasper/jas_malloc.h"
83 #include "jasper/jas_string.h"
85 /******************************************************************************\
87 \******************************************************************************/
89 #define FLOORDIV(x, y) ((x) / (y))
91 /******************************************************************************\
93 \******************************************************************************/
95 static jas_image_cmpt_t *jas_image_cmpt_create0(void);
96 static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt);
97 static jas_image_cmpt_t *jas_image_cmpt_create(uint_fast32_t tlx, uint_fast32_t tly,
98 uint_fast32_t hstep, uint_fast32_t vstep, uint_fast32_t width, uint_fast32_t
99 height, uint_fast16_t depth, bool sgnd, uint_fast32_t inmem);
100 static void jas_image_setbbox(jas_image_t *image);
101 static jas_image_cmpt_t *jas_image_cmpt_copy(jas_image_cmpt_t *cmpt);
102 static int jas_image_growcmpts(jas_image_t *image, int maxcmpts);
103 static uint_fast32_t inttobits(jas_seqent_t v, int prec, bool sgnd);
104 static jas_seqent_t bitstoint(uint_fast32_t v, int prec, bool sgnd);
105 static int putint(jas_stream_t *out, int sgnd, int prec, long val);
106 static int getint(jas_stream_t *in, int sgnd, int prec, long *val);
107 static void jas_image_calcbbox2(jas_image_t *image, jas_image_coord_t *tlx,
108 jas_image_coord_t *tly, jas_image_coord_t *brx, jas_image_coord_t *bry);
109 static long uptomult(long x, long y);
110 static long downtomult(long x, long y);
111 static long convert(long val, int oldsgnd, int oldprec, int newsgnd,
113 static void jas_image_calcbbox2(jas_image_t *image, jas_image_coord_t *tlx,
114 jas_image_coord_t *tly, jas_image_coord_t *brx, jas_image_coord_t *bry);
116 /******************************************************************************\
118 \******************************************************************************/
120 static int jas_image_numfmts = 0;
121 static jas_image_fmtinfo_t jas_image_fmtinfos[JAS_IMAGE_MAXFMTS];
123 /******************************************************************************\
124 * Create and destroy operations.
125 \******************************************************************************/
127 jas_image_t *jas_image_create(int numcmpts, jas_image_cmptparm_t *cmptparms,
131 uint_fast32_t rawsize;
134 jas_image_cmptparm_t *cmptparm;
136 if (!(image = jas_image_create0())) {
140 image->clrspc_ = clrspc;
141 image->maxcmpts_ = numcmpts;
142 image->inmem_ = true;
144 /* Allocate memory for the per-component information. */
145 if (!(image->cmpts_ = jas_malloc(image->maxcmpts_ *
146 sizeof(jas_image_cmpt_t *)))) {
147 jas_image_destroy(image);
150 /* Initialize in case of failure. */
151 for (cmptno = 0; cmptno < image->maxcmpts_; ++cmptno) {
152 image->cmpts_[cmptno] = 0;
155 /* Compute the approximate raw size of the image. */
157 for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno,
159 rawsize += cmptparm->width * cmptparm->height *
160 (cmptparm->prec + 7) / 8;
162 /* Decide whether to buffer the image data in memory, based on the
163 raw size of the image. */
164 inmem = (rawsize < JAS_IMAGE_INMEMTHRESH);
166 /* Create the individual image components. */
167 for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno,
169 if (!(image->cmpts_[cmptno] = jas_image_cmpt_create(cmptparm->tlx,
170 cmptparm->tly, cmptparm->hstep, cmptparm->vstep,
171 cmptparm->width, cmptparm->height, cmptparm->prec,
172 cmptparm->sgnd, inmem))) {
173 jas_image_destroy(image);
179 /* Determine the bounding box for all of the components on the
180 reference grid (i.e., the image area) */
181 jas_image_setbbox(image);
186 jas_image_t *jas_image_create0()
190 if (!(image = jas_malloc(sizeof(jas_image_t)))) {
198 image->clrspc_ = JAS_CLRSPC_UNKNOWN;
199 image->numcmpts_ = 0;
200 image->maxcmpts_ = 0;
202 image->inmem_ = true;
208 jas_image_t *jas_image_copy(jas_image_t *image)
210 jas_image_t *newimage;
213 newimage = jas_image_create0();
214 if (jas_image_growcmpts(newimage, image->numcmpts_)) {
217 for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
218 if (!(newimage->cmpts_[cmptno] = jas_image_cmpt_copy(image->cmpts_[cmptno]))) {
221 ++newimage->numcmpts_;
224 jas_image_setbbox(newimage);
226 if (image->cmprof_) {
227 if (!(newimage->cmprof_ = jas_cmprof_copy(image->cmprof_)))
234 jas_image_destroy(newimage);
239 static jas_image_cmpt_t *jas_image_cmpt_create0()
241 jas_image_cmpt_t *cmpt;
242 if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) {
245 memset(cmpt, 0, sizeof(jas_image_cmpt_t));
246 cmpt->type_ = JAS_IMAGE_CT_UNKNOWN;
250 static jas_image_cmpt_t *jas_image_cmpt_copy(jas_image_cmpt_t *cmpt)
252 jas_image_cmpt_t *newcmpt;
254 if (!(newcmpt = jas_image_cmpt_create0())) {
257 newcmpt->tlx_ = cmpt->tlx_;
258 newcmpt->tly_ = cmpt->tly_;
259 newcmpt->hstep_ = cmpt->hstep_;
260 newcmpt->vstep_ = cmpt->vstep_;
261 newcmpt->width_ = cmpt->width_;
262 newcmpt->height_ = cmpt->height_;
263 newcmpt->prec_ = cmpt->prec_;
264 newcmpt->sgnd_ = cmpt->sgnd_;
265 newcmpt->cps_ = cmpt->cps_;
266 newcmpt->type_ = cmpt->type_;
267 if (!(newcmpt->stream_ = jas_stream_memopen(0, 0))) {
270 if (jas_stream_seek(cmpt->stream_, 0, SEEK_SET)) {
273 if (jas_stream_copy(newcmpt->stream_, cmpt->stream_, -1)) {
276 if (jas_stream_seek(newcmpt->stream_, 0, SEEK_SET)) {
282 void jas_image_destroy(jas_image_t *image)
287 for (i = 0; i < image->numcmpts_; ++i) {
288 jas_image_cmpt_destroy(image->cmpts_[i]);
289 image->cmpts_[i] = 0;
291 jas_free(image->cmpts_);
294 jas_cmprof_destroy(image->cmprof_);
298 static jas_image_cmpt_t *jas_image_cmpt_create(uint_fast32_t tlx, uint_fast32_t tly,
299 uint_fast32_t hstep, uint_fast32_t vstep, uint_fast32_t width, uint_fast32_t
300 height, uint_fast16_t depth, bool sgnd, uint_fast32_t inmem)
302 jas_image_cmpt_t *cmpt;
305 if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) {
311 cmpt->hstep_ = hstep;
312 cmpt->vstep_ = vstep;
313 cmpt->width_ = width;
314 cmpt->height_ = height;
318 cmpt->cps_ = (depth + 7) / 8;
320 size = cmpt->width_ * cmpt->height_ * cmpt->cps_;
321 cmpt->stream_ = (inmem) ? jas_stream_memopen(0, size) : jas_stream_tmpfile();
322 if (!cmpt->stream_) {
323 jas_image_cmpt_destroy(cmpt);
327 /* Zero the component data. This isn't necessary, but it is
328 convenient for debugging purposes. */
329 if (jas_stream_seek(cmpt->stream_, size - 1, SEEK_SET) < 0 ||
330 jas_stream_putc(cmpt->stream_, 0) == EOF ||
331 jas_stream_seek(cmpt->stream_, 0, SEEK_SET) < 0) {
332 jas_image_cmpt_destroy(cmpt);
339 static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt)
342 jas_stream_close(cmpt->stream_);
347 /******************************************************************************\
348 * Load and save operations.
349 \******************************************************************************/
351 jas_image_t *jas_image_decode(jas_stream_t *in, int fmt, char *optstr)
353 jas_image_fmtinfo_t *fmtinfo;
358 /* If possible, try to determine the format of the input data. */
360 if ((fmt = jas_image_getfmt(in)) < 0)
364 /* Is it possible to decode an image represented in this format? */
365 if (!(fmtinfo = jas_image_lookupfmtbyid(fmt)))
367 if (!fmtinfo->ops.decode)
370 /* Decode the image. */
371 if (!(image = (*fmtinfo->ops.decode)(in, optstr)))
374 /* Create a color profile if needed. */
375 if (!jas_clrspc_isunknown(image->clrspc_) &&
376 !jas_clrspc_isgeneric(image->clrspc_) && !image->cmprof_) {
377 if (!(image->cmprof_ =
378 jas_cmprof_createfromclrspc(jas_image_clrspc(image))))
385 jas_image_destroy(image);
389 int jas_image_encode(jas_image_t *image, jas_stream_t *out, int fmt, char *optstr)
391 jas_image_fmtinfo_t *fmtinfo;
392 if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) {
395 return (fmtinfo->ops.encode) ? (*fmtinfo->ops.encode)(image, out,
399 /******************************************************************************\
400 * Component read and write operations.
401 \******************************************************************************/
403 int jas_image_readcmpt(jas_image_t *image, int cmptno, jas_image_coord_t x,
404 jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
407 jas_image_cmpt_t *cmpt;
417 if (cmptno < 0 || cmptno >= image->numcmpts_) {
421 cmpt = image->cmpts_[cmptno];
422 if (x >= cmpt->width_ || y >= cmpt->height_ ||
423 x + width > cmpt->width_ ||
424 y + height > cmpt->height_) {
428 if (jas_matrix_numrows(data) != height || jas_matrix_numcols(data) != width) {
429 if (jas_matrix_resize(data, height, width)) {
434 dr = jas_matrix_getref(data, 0, 0);
435 drs = jas_matrix_rowstep(data);
436 for (i = 0; i < height; ++i, dr += drs) {
438 if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x)
439 * cmpt->cps_, SEEK_SET) < 0) {
442 for (j = width; j > 0; --j, ++d) {
444 for (k = cmpt->cps_; k > 0; --k) {
445 if ((c = jas_stream_getc(cmpt->stream_)) == EOF) {
448 v = (v << 8) | (c & 0xff);
450 *d = bitstoint(v, cmpt->prec_, cmpt->sgnd_);
457 int jas_image_writecmpt(jas_image_t *image, int cmptno, jas_image_coord_t x, jas_image_coord_t y, jas_image_coord_t width,
458 jas_image_coord_t height, jas_matrix_t *data)
460 jas_image_cmpt_t *cmpt;
470 if (cmptno < 0 || cmptno >= image->numcmpts_) {
474 cmpt = image->cmpts_[cmptno];
475 if (x >= cmpt->width_ || y >= cmpt->height_ ||
476 x + width > cmpt->width_ ||
477 y + height > cmpt->height_) {
481 if (jas_matrix_numrows(data) != height || jas_matrix_numcols(data) != width) {
485 dr = jas_matrix_getref(data, 0, 0);
486 drs = jas_matrix_rowstep(data);
487 for (i = 0; i < height; ++i, dr += drs) {
489 if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x)
490 * cmpt->cps_, SEEK_SET) < 0) {
493 for (j = width; j > 0; --j, ++d) {
494 v = inttobits(*d, cmpt->prec_, cmpt->sgnd_);
495 for (k = cmpt->cps_; k > 0; --k) {
496 c = (v >> (8 * (cmpt->cps_ - 1))) & 0xff;
497 if (jas_stream_putc(cmpt->stream_,
498 (unsigned char) c) == EOF) {
509 /******************************************************************************\
510 * File format operations.
511 \******************************************************************************/
513 void jas_image_clearfmts()
516 jas_image_fmtinfo_t *fmtinfo;
517 for (i = 0; i < jas_image_numfmts; ++i) {
518 fmtinfo = &jas_image_fmtinfos[i];
520 jas_free(fmtinfo->name);
524 jas_free(fmtinfo->ext);
528 jas_free(fmtinfo->desc);
532 jas_image_numfmts = 0;
535 int jas_image_addfmt(int id, char *name, char *ext, char *desc,
536 jas_image_fmtops_t *ops)
538 jas_image_fmtinfo_t *fmtinfo;
539 assert(id >= 0 && name && ext && ops);
540 if (jas_image_numfmts >= JAS_IMAGE_MAXFMTS) {
543 fmtinfo = &jas_image_fmtinfos[jas_image_numfmts];
545 if (!(fmtinfo->name = jas_strdup(name))) {
548 if (!(fmtinfo->ext = jas_strdup(ext))) {
549 jas_free(fmtinfo->name);
552 if (!(fmtinfo->desc = jas_strdup(desc))) {
553 jas_free(fmtinfo->name);
554 jas_free(fmtinfo->ext);
562 int jas_image_strtofmt(char *name)
564 jas_image_fmtinfo_t *fmtinfo;
565 if (!(fmtinfo = jas_image_lookupfmtbyname(name))) {
571 char *jas_image_fmttostr(int fmt)
573 jas_image_fmtinfo_t *fmtinfo;
574 if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) {
577 return fmtinfo->name;
580 int jas_image_getfmt(jas_stream_t *in)
582 jas_image_fmtinfo_t *fmtinfo;
586 /* Check for data in each of the supported formats. */
588 for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i,
590 if (fmtinfo->ops.validate) {
591 /* Is the input data valid for this format? */
592 if (!(*fmtinfo->ops.validate)(in)) {
598 return found ? fmtinfo->id : (-1);
601 int jas_image_fmtfromname(char *name)
605 jas_image_fmtinfo_t *fmtinfo;
606 /* Get the file name extension. */
607 if (!(ext = strrchr(name, '.'))) {
611 /* Try to find a format that uses this extension. */
612 for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i,
614 /* Do we have a match? */
615 if (!strcmp(ext, fmtinfo->ext)) {
622 /******************************************************************************\
623 * Miscellaneous operations.
624 \******************************************************************************/
626 uint_fast32_t jas_image_rawsize(jas_image_t *image)
628 uint_fast32_t rawsize;
630 jas_image_cmpt_t *cmpt;
633 for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
634 cmpt = image->cmpts_[cmptno];
635 rawsize += (cmpt->width_ * cmpt->height_ * cmpt->prec_ +
641 void jas_image_delcmpt(jas_image_t *image, int cmptno)
643 if (cmptno >= image->numcmpts_) {
646 jas_image_cmpt_destroy(image->cmpts_[cmptno]);
647 if (cmptno < image->numcmpts_) {
648 memmove(&image->cmpts_[cmptno], &image->cmpts_[cmptno + 1],
649 (image->numcmpts_ - 1 - cmptno) * sizeof(jas_image_cmpt_t *));
653 jas_image_setbbox(image);
656 int jas_image_addcmpt(jas_image_t *image, int cmptno,
657 jas_image_cmptparm_t *cmptparm)
659 jas_image_cmpt_t *newcmpt;
661 cmptno = image->numcmpts_;
662 assert(cmptno >= 0 && cmptno <= image->numcmpts_);
663 if (image->numcmpts_ >= image->maxcmpts_) {
664 if (jas_image_growcmpts(image, image->maxcmpts_ + 128)) {
668 if (!(newcmpt = jas_image_cmpt_create(cmptparm->tlx,
669 cmptparm->tly, cmptparm->hstep, cmptparm->vstep,
670 cmptparm->width, cmptparm->height, cmptparm->prec,
671 cmptparm->sgnd, 1))) {
674 if (cmptno < image->numcmpts_) {
675 memmove(&image->cmpts_[cmptno + 1], &image->cmpts_[cmptno],
676 (image->numcmpts_ - cmptno) * sizeof(jas_image_cmpt_t *));
678 image->cmpts_[cmptno] = newcmpt;
681 jas_image_setbbox(image);
686 jas_image_fmtinfo_t *jas_image_lookupfmtbyid(int id)
689 jas_image_fmtinfo_t *fmtinfo;
691 for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i, ++fmtinfo) {
692 if (fmtinfo->id == id) {
699 jas_image_fmtinfo_t *jas_image_lookupfmtbyname(const char *name)
702 jas_image_fmtinfo_t *fmtinfo;
704 for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i, ++fmtinfo) {
705 if (!strcmp(fmtinfo->name, name)) {
716 static uint_fast32_t inttobits(jas_seqent_t v, int prec, bool sgnd)
719 ret = ((sgnd && v < 0) ? ((1 << prec) + v) : v) & JAS_ONES(prec);
723 static jas_seqent_t bitstoint(uint_fast32_t v, int prec, bool sgnd)
727 ret = (sgnd && (v & (1 << (prec - 1)))) ? (v - (1 << prec)) : v;
731 static void jas_image_setbbox(jas_image_t *image)
733 jas_image_cmpt_t *cmpt;
738 if (image->numcmpts_ > 0) {
739 /* Determine the bounding box for all of the components on the
740 reference grid (i.e., the image area) */
741 cmpt = image->cmpts_[0];
742 image->tlx_ = cmpt->tlx_;
743 image->tly_ = cmpt->tly_;
744 image->brx_ = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1) + 1;
745 image->bry_ = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1) + 1;
746 for (cmptno = 1; cmptno < image->numcmpts_; ++cmptno) {
747 cmpt = image->cmpts_[cmptno];
748 if (image->tlx_ > cmpt->tlx_) {
749 image->tlx_ = cmpt->tlx_;
751 if (image->tly_ > cmpt->tly_) {
752 image->tly_ = cmpt->tly_;
754 x = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1) + 1;
755 if (image->brx_ < x) {
758 y = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1) + 1;
759 if (image->bry_ < y) {
771 static int jas_image_growcmpts(jas_image_t *image, int maxcmpts)
773 jas_image_cmpt_t **newcmpts;
776 newcmpts = (!image->cmpts_) ? jas_malloc(maxcmpts * sizeof(jas_image_cmpt_t *)) :
777 jas_realloc(image->cmpts_, maxcmpts * sizeof(jas_image_cmpt_t *));
781 image->cmpts_ = newcmpts;
782 image->maxcmpts_ = maxcmpts;
783 for (cmptno = image->numcmpts_; cmptno < image->maxcmpts_; ++cmptno) {
784 image->cmpts_[cmptno] = 0;
789 int jas_image_copycmpt(jas_image_t *dstimage, int dstcmptno, jas_image_t *srcimage,
792 jas_image_cmpt_t *newcmpt;
793 if (dstimage->numcmpts_ >= dstimage->maxcmpts_) {
794 if (jas_image_growcmpts(dstimage, dstimage->maxcmpts_ + 128)) {
798 if (!(newcmpt = jas_image_cmpt_copy(srcimage->cmpts_[srccmptno]))) {
801 if (dstcmptno < dstimage->numcmpts_) {
802 memmove(&dstimage->cmpts_[dstcmptno + 1], &dstimage->cmpts_[dstcmptno],
803 (dstimage->numcmpts_ - dstcmptno) * sizeof(jas_image_cmpt_t *));
805 dstimage->cmpts_[dstcmptno] = newcmpt;
806 ++dstimage->numcmpts_;
808 jas_image_setbbox(dstimage);
812 void jas_image_dump(jas_image_t *image, FILE *out)
820 jas_image_cmpt_t *cmpt;
821 for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
822 cmpt = image->cmpts_[cmptno];
823 fprintf(out, "prec=%d, sgnd=%d, cmpttype=%d\n", cmpt->prec_,
824 cmpt->sgnd_, cmpt->type_);
825 width = jas_image_cmptwidth(image, cmptno);
826 height = jas_image_cmptheight(image, cmptno);
827 n = JAS_MIN(16, width);
828 if (jas_image_readcmpt2(image, cmptno, 0, 0, n, 1, buf)) {
831 for (i = 0; i < n; ++i) {
832 fprintf(out, " f(%d,%d)=%ld", i, 0, buf[i]);
835 if (jas_image_readcmpt2(image, cmptno, width - n, height - 1, n, 1, buf)) {
838 for (i = 0; i < n; ++i) {
839 fprintf(out, " f(%d,%d)=%ld", width - n + i, height - 1, buf[i]);
845 int jas_image_depalettize(jas_image_t *image, int cmptno, int numlutents,
846 int_fast32_t *lutents, int dtype, int newcmptno)
848 jas_image_cmptparm_t cmptparms;
852 jas_image_cmpt_t *cmpt;
854 cmpt = image->cmpts_[cmptno];
855 cmptparms.tlx = cmpt->tlx_;
856 cmptparms.tly = cmpt->tly_;
857 cmptparms.hstep = cmpt->hstep_;
858 cmptparms.vstep = cmpt->vstep_;
859 cmptparms.width = cmpt->width_;
860 cmptparms.height = cmpt->height_;
861 cmptparms.prec = JAS_IMAGE_CDT_GETPREC(dtype);
862 cmptparms.sgnd = JAS_IMAGE_CDT_GETSGND(dtype);
864 if (jas_image_addcmpt(image, newcmptno, &cmptparms)) {
867 if (newcmptno <= cmptno) {
869 cmpt = image->cmpts_[cmptno];
872 for (j = 0; j < cmpt->height_; ++j) {
873 for (i = 0; i < cmpt->width_; ++i) {
874 v = jas_image_readcmptsample(image, cmptno, i, j);
877 } else if (v >= numlutents) {
880 jas_image_writecmptsample(image, newcmptno, i, j,
887 int jas_image_readcmptsample(jas_image_t *image, int cmptno, int x, int y)
889 jas_image_cmpt_t *cmpt;
894 cmpt = image->cmpts_[cmptno];
896 if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * y + x) * cmpt->cps_,
901 for (k = cmpt->cps_; k > 0; --k) {
902 if ((c = jas_stream_getc(cmpt->stream_)) == EOF) {
905 v = (v << 8) | (c & 0xff);
907 return bitstoint(v, cmpt->prec_, cmpt->sgnd_);
910 void jas_image_writecmptsample(jas_image_t *image, int cmptno, int x, int y,
913 jas_image_cmpt_t *cmpt;
918 cmpt = image->cmpts_[cmptno];
920 if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * y + x) * cmpt->cps_,
924 t = inttobits(v, cmpt->prec_, cmpt->sgnd_);
925 for (k = cmpt->cps_; k > 0; --k) {
926 c = (t >> (8 * (cmpt->cps_ - 1))) & 0xff;
927 if (jas_stream_putc(cmpt->stream_, (unsigned char) c) == EOF) {
934 int jas_image_getcmptbytype(jas_image_t *image, int ctype)
938 for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
939 if (image->cmpts_[cmptno]->type_ == ctype) {
961 /***********************************************/
962 /***********************************************/
963 /***********************************************/
964 /***********************************************/
966 int jas_image_readcmpt2(jas_image_t *image, int cmptno, jas_image_coord_t x,
967 jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
970 jas_image_cmpt_t *cmpt;
976 if (cmptno < 0 || cmptno >= image->numcmpts_)
978 cmpt = image->cmpts_[cmptno];
979 if (x < 0 || x >= cmpt->width_ || y < 0 || y >= cmpt->height_ ||
980 width < 0 || height < 0 || x + width > cmpt->width_ ||
981 y + height > cmpt->height_)
985 for (i = 0; i < height; ++i) {
986 if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x)
987 * cmpt->cps_, SEEK_SET) < 0)
989 for (j = 0; j < width; ++j) {
990 if (getint(cmpt->stream_, cmpt->sgnd_, cmpt->prec_, &v))
1001 int jas_image_writecmpt2(jas_image_t *image, int cmptno, jas_image_coord_t x,
1002 jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
1005 jas_image_cmpt_t *cmpt;
1006 jas_image_coord_t i;
1007 jas_image_coord_t j;
1011 if (cmptno < 0 || cmptno >= image->numcmpts_)
1013 cmpt = image->cmpts_[cmptno];
1014 if (x < 0 || x >= cmpt->width_ || y < 0 || y >= cmpt->height_ ||
1015 width < 0 || height < 0 || x + width > cmpt->width_ ||
1016 y + height > cmpt->height_)
1020 for (i = 0; i < height; ++i) {
1021 if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x)
1022 * cmpt->cps_, SEEK_SET) < 0)
1024 for (j = 0; j < width; ++j) {
1026 if (putint(cmpt->stream_, cmpt->sgnd_, cmpt->prec_, v))
1036 int jas_image_sampcmpt(jas_image_t *image, int cmptno, int newcmptno,
1037 jas_image_coord_t ho, jas_image_coord_t vo, jas_image_coord_t hs,
1038 jas_image_coord_t vs, int sgnd, int prec)
1040 jas_image_cmpt_t *oldcmpt;
1041 jas_image_cmpt_t *newcmpt;
1044 jas_image_coord_t tlx;
1045 jas_image_coord_t tly;
1046 jas_image_coord_t brx;
1047 jas_image_coord_t bry;
1050 jas_image_cmptparm_t cmptparm;
1051 jas_image_coord_t ax;
1052 jas_image_coord_t ay;
1053 jas_image_coord_t bx;
1054 jas_image_coord_t by;
1055 jas_image_coord_t d0;
1056 jas_image_coord_t d1;
1057 jas_image_coord_t d2;
1058 jas_image_coord_t d3;
1059 jas_image_coord_t oldx;
1060 jas_image_coord_t oldy;
1061 jas_image_coord_t x;
1062 jas_image_coord_t y;
1064 jas_image_coord_t cmptbrx;
1065 jas_image_coord_t cmptbry;
1067 assert(cmptno >= 0 && cmptno < image->numcmpts_);
1068 oldcmpt = image->cmpts_[cmptno];
1069 assert(oldcmpt->tlx_ == 0 && oldcmpt->tly_ == 0);
1070 jas_image_calcbbox2(image, &tlx, &tly, &brx, &bry);
1071 width = FLOORDIV(brx - ho + hs, hs);
1072 height = FLOORDIV(bry - vo + vs, vs);
1075 cmptparm.hstep = hs;
1076 cmptparm.vstep = vs;
1077 cmptparm.width = width;
1078 cmptparm.height = height;
1079 cmptparm.prec = prec;
1080 cmptparm.sgnd = sgnd;
1081 if (jas_image_addcmpt(image, newcmptno, &cmptparm))
1083 cmptbrx = oldcmpt->tlx_ + (oldcmpt->width_ - 1) * oldcmpt->hstep_;
1084 cmptbry = oldcmpt->tly_ + (oldcmpt->height_ - 1) * oldcmpt->vstep_;
1085 newcmpt = image->cmpts_[newcmptno];
1086 jas_stream_rewind(newcmpt->stream_);
1087 for (i = 0; i < height; ++i) {
1088 y = newcmpt->tly_ + newcmpt->vstep_ * i;
1089 for (j = 0; j < width; ++j) {
1090 x = newcmpt->tlx_ + newcmpt->hstep_ * j;
1091 ax = downtomult(x - oldcmpt->tlx_, oldcmpt->hstep_) + oldcmpt->tlx_;
1092 ay = downtomult(y - oldcmpt->tly_, oldcmpt->vstep_) + oldcmpt->tly_;
1093 bx = uptomult(x - oldcmpt->tlx_, oldcmpt->hstep_) + oldcmpt->tlx_;
1096 by = uptomult(y - oldcmpt->tly_, oldcmpt->vstep_) + oldcmpt->tly_;
1099 d0 = (ax - x) * (ax - x) + (ay - y) * (ay - y);
1100 d1 = (bx - x) * (bx - x) + (ay - y) * (ay - y);
1101 d2 = (bx - x) * (bx - x) + (by - y) * (by - y);
1102 d3 = (ax - x) * (ax - x) + (by - y) * (by - y);
1103 if (d0 <= d1 && d0 <= d2 && d0 <= d3) {
1104 oldx = (ax - oldcmpt->tlx_) / oldcmpt->hstep_;
1105 oldy = (ay - oldcmpt->tly_) / oldcmpt->vstep_;
1106 } else if (d1 <= d0 && d1 <= d2 && d1 <= d3) {
1107 oldx = (bx - oldcmpt->tlx_) / oldcmpt->hstep_;
1108 oldy = (ay - oldcmpt->tly_) / oldcmpt->vstep_;
1109 } else if (d2 <= d0 && d2 <= d1 && d1 <= d3) {
1110 oldx = (bx - oldcmpt->tlx_) / oldcmpt->hstep_;
1111 oldy = (by - oldcmpt->tly_) / oldcmpt->vstep_;
1113 oldx = (ax - oldcmpt->tlx_) / oldcmpt->hstep_;
1114 oldy = (by - oldcmpt->tly_) / oldcmpt->vstep_;
1116 assert(oldx >= 0 && oldx < oldcmpt->width_ &&
1117 oldy >= 0 && oldy < oldcmpt->height_);
1118 if (jas_stream_seek(oldcmpt->stream_, oldcmpt->cps_ *
1119 (oldy * oldcmpt->width_ + oldx), SEEK_SET) < 0)
1121 if (getint(oldcmpt->stream_, oldcmpt->sgnd_,
1122 oldcmpt->prec_, &v))
1124 if (newcmpt->prec_ != oldcmpt->prec_ ||
1125 newcmpt->sgnd_ != oldcmpt->sgnd_) {
1126 v = convert(v, oldcmpt->sgnd_, oldcmpt->prec_,
1127 newcmpt->sgnd_, newcmpt->prec_);
1129 if (putint(newcmpt->stream_, newcmpt->sgnd_,
1139 int jas_image_ishomosamp(jas_image_t *image)
1141 jas_image_coord_t hstep;
1142 jas_image_coord_t vstep;
1145 hstep = jas_image_cmpthstep(image, 0);
1146 vstep = jas_image_cmptvstep(image, 0);
1148 for (i = 0; i < image->numcmpts_; ++i) {
1149 if (jas_image_cmpthstep(image, i) != hstep ||
1150 jas_image_cmptvstep(image, i) != vstep) {
1158 /* Note: This function defines a bounding box differently. */
1159 static void jas_image_calcbbox2(jas_image_t *image, jas_image_coord_t *tlx,
1160 jas_image_coord_t *tly, jas_image_coord_t *brx, jas_image_coord_t *bry)
1162 jas_image_cmpt_t *cmpt;
1163 jas_image_coord_t tmptlx;
1164 jas_image_coord_t tmptly;
1165 jas_image_coord_t tmpbrx;
1166 jas_image_coord_t tmpbry;
1167 jas_image_coord_t t;
1169 if (image->numcmpts_ > 0) {
1170 cmpt = image->cmpts_[0];
1171 tmptlx = cmpt->tlx_;
1172 tmptly = cmpt->tly_;
1173 tmpbrx = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1);
1174 tmpbry = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1);
1175 for (i = 0; i < image->numcmpts_; ++i) {
1176 cmpt = image->cmpts_[i];
1177 if (cmpt->tlx_ < tmptlx)
1178 tmptlx = cmpt->tlx_;
1179 if (cmpt->tly_ < tmptly)
1180 tmptly = cmpt->tly_;
1181 t = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1);
1184 t = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1);
1202 static int getint(jas_stream_t *in, int sgnd, int prec, long *val)
1210 if ((c = jas_stream_getc(in)) == EOF)
1214 v &= ((1 << prec) - 1);
1216 /* XXX - Do something here. */
1224 static int putint(jas_stream_t *out, int sgnd, int prec, long val)
1229 /* XXX - Do something here. */
1232 val &= (1 << prec) - 1;
1235 c = (val >> (n * 8)) & 0xff;
1236 if (jas_stream_putc(out, c) != c)
1242 static long convert(long val, int oldsgnd, int oldprec, int newsgnd,
1245 if (newsgnd != oldsgnd) {
1247 if (newprec != oldprec) {
1248 if (newprec > oldprec) {
1249 val <<= newprec - oldprec;
1250 } else if (oldprec > newprec) {
1251 val >>= oldprec - newprec;
1257 static long downtomult(long x, long y)
1263 static long uptomult(long x, long y)
1266 return ((x + y - 1) / y) * y;
1269 jas_image_t *jas_image_chclrspc(jas_image_t *image, jas_cmprof_t *outprof,
1272 jas_image_t *inimage;
1286 jas_image_t *outimage;
1289 jas_cmprof_t *inprof;
1290 jas_cmprof_t *tmpprof;
1291 jas_image_cmptparm_t cmptparm;
1294 jas_cmxform_t *xform;
1295 jas_cmpixmap_t inpixmap;
1296 jas_cmpixmap_t outpixmap;
1297 jas_cmcmptfmt_t *incmptfmts;
1298 jas_cmcmptfmt_t *outcmptfmts;
1301 fprintf(stderr, "IMAGE\n");
1302 jas_image_dump(image, stderr);
1305 if (!(inimage = jas_image_copy(image)))
1309 if (!jas_image_ishomosamp(inimage)) {
1310 minhstep = jas_image_cmpthstep(inimage, 0);
1311 minvstep = jas_image_cmptvstep(inimage, 0);
1312 for (i = 1; i < jas_image_numcmpts(inimage); ++i) {
1313 hstep = jas_image_cmpthstep(inimage, i);
1314 vstep = jas_image_cmptvstep(inimage, i);
1315 if (hstep < minhstep)
1317 if (vstep < minvstep)
1320 n = jas_image_numcmpts(inimage);
1321 for (i = 0; i < n; ++i) {
1322 cmpttype = jas_image_cmpttype(inimage, i);
1323 if (jas_image_sampcmpt(inimage, i, i + 1, 0, 0, minhstep, minvstep, jas_image_cmptsgnd(inimage, i), jas_image_cmptprec(inimage, i)))
1325 jas_image_setcmpttype(inimage, i + 1, cmpttype);
1326 jas_image_delcmpt(inimage, i);
1330 width = jas_image_cmptwidth(inimage, 0);
1331 height = jas_image_cmptheight(inimage, 0);
1332 hstep = jas_image_cmpthstep(inimage, 0);
1333 vstep = jas_image_cmptvstep(inimage, 0);
1335 inprof = jas_image_cmprof(inimage);
1337 numinclrchans = jas_clrspc_numchans(jas_cmprof_clrspc(inprof));
1338 numinauxchans = jas_image_numcmpts(inimage) - numinclrchans;
1339 numoutclrchans = jas_clrspc_numchans(jas_cmprof_clrspc(outprof));
1341 numoutchans = numoutclrchans + numoutauxchans;
1344 if (!(outimage = jas_image_create0()))
1347 /* Create a component for each of the colorants. */
1348 for (i = 0; i < numoutclrchans; ++i) {
1351 cmptparm.hstep = hstep;
1352 cmptparm.vstep = vstep;
1353 cmptparm.width = width;
1354 cmptparm.height = height;
1355 cmptparm.prec = prec;
1357 if (jas_image_addcmpt(outimage, -1, &cmptparm))
1359 jas_image_setcmpttype(outimage, i, JAS_IMAGE_CT_COLOR(i));
1362 /* Copy the auxiliary components without modification. */
1363 for (i = 0; i < jas_image_numcmpts(inimage); ++i) {
1364 if (!ISCOLOR(jas_image_cmpttype(inimage, i))) {
1365 jas_image_copycmpt(outimage, -1, inimage, i);
1366 /* XXX - need to specify laydown of component on ref. grid */
1371 if (!(tmpprof = jas_cmprof_copy(outprof)))
1373 assert(!jas_image_cmprof(outimage));
1374 jas_image_setcmprof(outimage, tmpprof);
1376 jas_image_setclrspc(outimage, jas_cmprof_clrspc(outprof));
1378 if (!(xform = jas_cmxform_create(inprof, outprof, 0, JAS_CMXFORM_OP_FWD, intent, 0)))
1381 inpixmap.numcmpts = numinclrchans;
1382 incmptfmts = malloc(numinclrchans * sizeof(jas_cmcmptfmt_t));
1384 inpixmap.cmptfmts = incmptfmts;
1385 for (i = 0; i < numinclrchans; ++i) {
1386 j = jas_image_getcmptbytype(inimage, JAS_IMAGE_CT_COLOR(i));
1388 if (!(incmptfmts[i].buf = malloc(width * sizeof(long))))
1390 incmptfmts[i].prec = jas_image_cmptprec(inimage, j);
1391 incmptfmts[i].sgnd = jas_image_cmptsgnd(inimage, j);
1392 incmptfmts[i].width = width;
1393 incmptfmts[i].height = 1;
1396 outpixmap.numcmpts = numoutclrchans;
1397 outcmptfmts = malloc(numoutclrchans * sizeof(jas_cmcmptfmt_t));
1398 assert(outcmptfmts);
1399 outpixmap.cmptfmts = outcmptfmts;
1401 for (i = 0; i < numoutclrchans; ++i) {
1402 j = jas_image_getcmptbytype(outimage, JAS_IMAGE_CT_COLOR(i));
1404 if (!(outcmptfmts[i].buf = malloc(width * sizeof(long))))
1406 outcmptfmts[i].prec = jas_image_cmptprec(outimage, j);
1407 outcmptfmts[i].sgnd = jas_image_cmptsgnd(outimage, j);
1408 outcmptfmts[i].width = width;
1409 outcmptfmts[i].height = 1;
1412 for (i = 0; i < height; ++i) {
1413 for (j = 0; j < numinclrchans; ++j) {
1414 k = jas_image_getcmptbytype(inimage, JAS_IMAGE_CT_COLOR(j));
1415 if (jas_image_readcmpt2(inimage, k, 0, i, width, 1, incmptfmts[j].buf))
1418 jas_cmxform_apply(xform, &inpixmap, &outpixmap);
1419 for (j = 0; j < numoutclrchans; ++j) {
1420 k = jas_image_getcmptbytype(outimage, JAS_IMAGE_CT_COLOR(j));
1421 if (jas_image_writecmpt2(outimage, k, 0, i, width, 1, outcmptfmts[j].buf))
1426 for (i = 0; i < numoutclrchans; ++i)
1427 jas_free(outcmptfmts[i].buf);
1428 jas_free(outcmptfmts);
1429 for (i = 0; i < numinclrchans; ++i)
1430 jas_free(incmptfmts[i].buf);
1431 jas_free(incmptfmts);
1432 jas_cmxform_destroy(xform);
1433 jas_image_destroy(inimage);
1436 fprintf(stderr, "INIMAGE\n");
1437 jas_image_dump(inimage, stderr);
1438 fprintf(stderr, "OUTIMAGE\n");
1439 jas_image_dump(outimage, stderr);