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) 2001-2006 Michael David Adams
13
* Copyright (c) 1999-2000 Image Power, Inc.
14
* Copyright (c) 1999-2000 The University of British Columbia
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__
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)))) {
309
cmpt->type_ = JAS_IMAGE_CT_UNKNOWN;
312
cmpt->hstep_ = hstep;
313
cmpt->vstep_ = vstep;
314
cmpt->width_ = width;
315
cmpt->height_ = height;
319
cmpt->cps_ = (depth + 7) / 8;
321
size = cmpt->width_ * cmpt->height_ * cmpt->cps_;
322
cmpt->stream_ = (inmem) ? jas_stream_memopen(0, size) : jas_stream_tmpfile();
323
if (!cmpt->stream_) {
324
jas_image_cmpt_destroy(cmpt);
328
/* Zero the component data. This isn't necessary, but it is
329
convenient for debugging purposes. */
330
if (jas_stream_seek(cmpt->stream_, size - 1, SEEK_SET) < 0 ||
331
jas_stream_putc(cmpt->stream_, 0) == EOF ||
332
jas_stream_seek(cmpt->stream_, 0, SEEK_SET) < 0) {
333
jas_image_cmpt_destroy(cmpt);
340
static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt)
343
jas_stream_close(cmpt->stream_);
348
/******************************************************************************\
349
* Load and save operations.
350
\******************************************************************************/
352
jas_image_t *jas_image_decode(jas_stream_t *in, int fmt, char *optstr)
354
jas_image_fmtinfo_t *fmtinfo;
359
/* If possible, try to determine the format of the input data. */
361
if ((fmt = jas_image_getfmt(in)) < 0)
365
/* Is it possible to decode an image represented in this format? */
366
if (!(fmtinfo = jas_image_lookupfmtbyid(fmt)))
368
if (!fmtinfo->ops.decode)
371
/* Decode the image. */
372
if (!(image = (*fmtinfo->ops.decode)(in, optstr)))
375
/* Create a color profile if needed. */
376
if (!jas_clrspc_isunknown(image->clrspc_) &&
377
!jas_clrspc_isgeneric(image->clrspc_) && !image->cmprof_) {
378
if (!(image->cmprof_ =
379
jas_cmprof_createfromclrspc(jas_image_clrspc(image))))
386
jas_image_destroy(image);
390
int jas_image_encode(jas_image_t *image, jas_stream_t *out, int fmt, char *optstr)
392
jas_image_fmtinfo_t *fmtinfo;
393
if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) {
396
return (fmtinfo->ops.encode) ? (*fmtinfo->ops.encode)(image, out,
400
/******************************************************************************\
401
* Component read and write operations.
402
\******************************************************************************/
404
int jas_image_readcmpt(jas_image_t *image, int cmptno, jas_image_coord_t x,
405
jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
408
jas_image_cmpt_t *cmpt;
418
if (cmptno < 0 || cmptno >= image->numcmpts_) {
422
cmpt = image->cmpts_[cmptno];
423
if (x >= cmpt->width_ || y >= cmpt->height_ ||
424
x + width > cmpt->width_ ||
425
y + height > cmpt->height_) {
429
if (jas_matrix_numrows(data) != height || jas_matrix_numcols(data) != width) {
430
if (jas_matrix_resize(data, height, width)) {
435
dr = jas_matrix_getref(data, 0, 0);
436
drs = jas_matrix_rowstep(data);
437
for (i = 0; i < height; ++i, dr += drs) {
439
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x)
440
* cmpt->cps_, SEEK_SET) < 0) {
443
for (j = width; j > 0; --j, ++d) {
445
for (k = cmpt->cps_; k > 0; --k) {
446
if ((c = jas_stream_getc(cmpt->stream_)) == EOF) {
449
v = (v << 8) | (c & 0xff);
451
*d = bitstoint(v, cmpt->prec_, cmpt->sgnd_);
458
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,
459
jas_image_coord_t height, jas_matrix_t *data)
461
jas_image_cmpt_t *cmpt;
471
if (cmptno < 0 || cmptno >= image->numcmpts_) {
475
cmpt = image->cmpts_[cmptno];
476
if (x >= cmpt->width_ || y >= cmpt->height_ ||
477
x + width > cmpt->width_ ||
478
y + height > cmpt->height_) {
482
if (jas_matrix_numrows(data) != height || jas_matrix_numcols(data) != width) {
486
dr = jas_matrix_getref(data, 0, 0);
487
drs = jas_matrix_rowstep(data);
488
for (i = 0; i < height; ++i, dr += drs) {
490
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x)
491
* cmpt->cps_, SEEK_SET) < 0) {
494
for (j = width; j > 0; --j, ++d) {
495
v = inttobits(*d, cmpt->prec_, cmpt->sgnd_);
496
for (k = cmpt->cps_; k > 0; --k) {
497
c = (v >> (8 * (cmpt->cps_ - 1))) & 0xff;
498
if (jas_stream_putc(cmpt->stream_,
499
(unsigned char) c) == EOF) {
510
/******************************************************************************\
511
* File format operations.
512
\******************************************************************************/
514
void jas_image_clearfmts()
517
jas_image_fmtinfo_t *fmtinfo;
518
for (i = 0; i < jas_image_numfmts; ++i) {
519
fmtinfo = &jas_image_fmtinfos[i];
521
jas_free(fmtinfo->name);
525
jas_free(fmtinfo->ext);
529
jas_free(fmtinfo->desc);
533
jas_image_numfmts = 0;
536
int jas_image_addfmt(int id, char *name, char *ext, char *desc,
537
jas_image_fmtops_t *ops)
539
jas_image_fmtinfo_t *fmtinfo;
540
assert(id >= 0 && name && ext && ops);
541
if (jas_image_numfmts >= JAS_IMAGE_MAXFMTS) {
544
fmtinfo = &jas_image_fmtinfos[jas_image_numfmts];
546
if (!(fmtinfo->name = jas_strdup(name))) {
549
if (!(fmtinfo->ext = jas_strdup(ext))) {
550
jas_free(fmtinfo->name);
553
if (!(fmtinfo->desc = jas_strdup(desc))) {
554
jas_free(fmtinfo->name);
555
jas_free(fmtinfo->ext);
563
int jas_image_strtofmt(char *name)
565
jas_image_fmtinfo_t *fmtinfo;
566
if (!(fmtinfo = jas_image_lookupfmtbyname(name))) {
572
char *jas_image_fmttostr(int fmt)
574
jas_image_fmtinfo_t *fmtinfo;
575
if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) {
578
return fmtinfo->name;
581
int jas_image_getfmt(jas_stream_t *in)
583
jas_image_fmtinfo_t *fmtinfo;
587
/* Check for data in each of the supported formats. */
589
for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i,
591
if (fmtinfo->ops.validate) {
592
/* Is the input data valid for this format? */
593
if (!(*fmtinfo->ops.validate)(in)) {
599
return found ? fmtinfo->id : (-1);
602
int jas_image_fmtfromname(char *name)
606
jas_image_fmtinfo_t *fmtinfo;
607
/* Get the file name extension. */
608
if (!(ext = strrchr(name, '.'))) {
612
/* Try to find a format that uses this extension. */
613
for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i,
615
/* Do we have a match? */
616
if (!strcmp(ext, fmtinfo->ext)) {
623
/******************************************************************************\
624
* Miscellaneous operations.
625
\******************************************************************************/
627
uint_fast32_t jas_image_rawsize(jas_image_t *image)
629
uint_fast32_t rawsize;
631
jas_image_cmpt_t *cmpt;
634
for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
635
cmpt = image->cmpts_[cmptno];
636
rawsize += (cmpt->width_ * cmpt->height_ * cmpt->prec_ +
642
void jas_image_delcmpt(jas_image_t *image, int cmptno)
644
if (cmptno >= image->numcmpts_) {
647
jas_image_cmpt_destroy(image->cmpts_[cmptno]);
648
if (cmptno < image->numcmpts_) {
649
memmove(&image->cmpts_[cmptno], &image->cmpts_[cmptno + 1],
650
(image->numcmpts_ - 1 - cmptno) * sizeof(jas_image_cmpt_t *));
654
jas_image_setbbox(image);
657
int jas_image_addcmpt(jas_image_t *image, int cmptno,
658
jas_image_cmptparm_t *cmptparm)
660
jas_image_cmpt_t *newcmpt;
662
cmptno = image->numcmpts_;
663
assert(cmptno >= 0 && cmptno <= image->numcmpts_);
664
if (image->numcmpts_ >= image->maxcmpts_) {
665
if (jas_image_growcmpts(image, image->maxcmpts_ + 128)) {
669
if (!(newcmpt = jas_image_cmpt_create(cmptparm->tlx,
670
cmptparm->tly, cmptparm->hstep, cmptparm->vstep,
671
cmptparm->width, cmptparm->height, cmptparm->prec,
672
cmptparm->sgnd, 1))) {
675
if (cmptno < image->numcmpts_) {
676
memmove(&image->cmpts_[cmptno + 1], &image->cmpts_[cmptno],
677
(image->numcmpts_ - cmptno) * sizeof(jas_image_cmpt_t *));
679
image->cmpts_[cmptno] = newcmpt;
682
jas_image_setbbox(image);
687
jas_image_fmtinfo_t *jas_image_lookupfmtbyid(int id)
690
jas_image_fmtinfo_t *fmtinfo;
692
for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i, ++fmtinfo) {
693
if (fmtinfo->id == id) {
700
jas_image_fmtinfo_t *jas_image_lookupfmtbyname(const char *name)
703
jas_image_fmtinfo_t *fmtinfo;
705
for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i, ++fmtinfo) {
706
if (!strcmp(fmtinfo->name, name)) {
717
static uint_fast32_t inttobits(jas_seqent_t v, int prec, bool sgnd)
720
ret = ((sgnd && v < 0) ? ((1 << prec) + v) : v) & JAS_ONES(prec);
724
static jas_seqent_t bitstoint(uint_fast32_t v, int prec, bool sgnd)
728
ret = (sgnd && (v & (1 << (prec - 1)))) ? (v - (1 << prec)) : v;
732
static void jas_image_setbbox(jas_image_t *image)
734
jas_image_cmpt_t *cmpt;
739
if (image->numcmpts_ > 0) {
740
/* Determine the bounding box for all of the components on the
741
reference grid (i.e., the image area) */
742
cmpt = image->cmpts_[0];
743
image->tlx_ = cmpt->tlx_;
744
image->tly_ = cmpt->tly_;
745
image->brx_ = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1) + 1;
746
image->bry_ = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1) + 1;
747
for (cmptno = 1; cmptno < image->numcmpts_; ++cmptno) {
748
cmpt = image->cmpts_[cmptno];
749
if (image->tlx_ > cmpt->tlx_) {
750
image->tlx_ = cmpt->tlx_;
752
if (image->tly_ > cmpt->tly_) {
753
image->tly_ = cmpt->tly_;
755
x = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1) + 1;
756
if (image->brx_ < x) {
759
y = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1) + 1;
760
if (image->bry_ < y) {
772
static int jas_image_growcmpts(jas_image_t *image, int maxcmpts)
774
jas_image_cmpt_t **newcmpts;
777
newcmpts = (!image->cmpts_) ? jas_malloc(maxcmpts * sizeof(jas_image_cmpt_t *)) :
778
jas_realloc(image->cmpts_, maxcmpts * sizeof(jas_image_cmpt_t *));
782
image->cmpts_ = newcmpts;
783
image->maxcmpts_ = maxcmpts;
784
for (cmptno = image->numcmpts_; cmptno < image->maxcmpts_; ++cmptno) {
785
image->cmpts_[cmptno] = 0;
790
int jas_image_copycmpt(jas_image_t *dstimage, int dstcmptno, jas_image_t *srcimage,
793
jas_image_cmpt_t *newcmpt;
794
if (dstimage->numcmpts_ >= dstimage->maxcmpts_) {
795
if (jas_image_growcmpts(dstimage, dstimage->maxcmpts_ + 128)) {
799
if (!(newcmpt = jas_image_cmpt_copy(srcimage->cmpts_[srccmptno]))) {
802
if (dstcmptno < dstimage->numcmpts_) {
803
memmove(&dstimage->cmpts_[dstcmptno + 1], &dstimage->cmpts_[dstcmptno],
804
(dstimage->numcmpts_ - dstcmptno) * sizeof(jas_image_cmpt_t *));
806
dstimage->cmpts_[dstcmptno] = newcmpt;
807
++dstimage->numcmpts_;
809
jas_image_setbbox(dstimage);
813
void jas_image_dump(jas_image_t *image, FILE *out)
821
jas_image_cmpt_t *cmpt;
822
for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
823
cmpt = image->cmpts_[cmptno];
824
fprintf(out, "prec=%d, sgnd=%d, cmpttype=%d\n", cmpt->prec_,
825
cmpt->sgnd_, cmpt->type_);
826
width = jas_image_cmptwidth(image, cmptno);
827
height = jas_image_cmptheight(image, cmptno);
828
n = JAS_MIN(16, width);
829
if (jas_image_readcmpt2(image, cmptno, 0, 0, n, 1, buf)) {
832
for (i = 0; i < n; ++i) {
833
fprintf(out, " f(%d,%d)=%ld", i, 0, buf[i]);
836
if (jas_image_readcmpt2(image, cmptno, width - n, height - 1, n, 1, buf)) {
839
for (i = 0; i < n; ++i) {
840
fprintf(out, " f(%d,%d)=%ld", width - n + i, height - 1, buf[i]);
846
int jas_image_depalettize(jas_image_t *image, int cmptno, int numlutents,
847
int_fast32_t *lutents, int dtype, int newcmptno)
849
jas_image_cmptparm_t cmptparms;
853
jas_image_cmpt_t *cmpt;
855
cmpt = image->cmpts_[cmptno];
856
cmptparms.tlx = cmpt->tlx_;
857
cmptparms.tly = cmpt->tly_;
858
cmptparms.hstep = cmpt->hstep_;
859
cmptparms.vstep = cmpt->vstep_;
860
cmptparms.width = cmpt->width_;
861
cmptparms.height = cmpt->height_;
862
cmptparms.prec = JAS_IMAGE_CDT_GETPREC(dtype);
863
cmptparms.sgnd = JAS_IMAGE_CDT_GETSGND(dtype);
865
if (jas_image_addcmpt(image, newcmptno, &cmptparms)) {
868
if (newcmptno <= cmptno) {
870
cmpt = image->cmpts_[cmptno];
873
for (j = 0; j < cmpt->height_; ++j) {
874
for (i = 0; i < cmpt->width_; ++i) {
875
v = jas_image_readcmptsample(image, cmptno, i, j);
878
} else if (v >= numlutents) {
881
jas_image_writecmptsample(image, newcmptno, i, j,
888
int jas_image_readcmptsample(jas_image_t *image, int cmptno, int x, int y)
890
jas_image_cmpt_t *cmpt;
895
cmpt = image->cmpts_[cmptno];
897
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * y + x) * cmpt->cps_,
902
for (k = cmpt->cps_; k > 0; --k) {
903
if ((c = jas_stream_getc(cmpt->stream_)) == EOF) {
906
v = (v << 8) | (c & 0xff);
908
return bitstoint(v, cmpt->prec_, cmpt->sgnd_);
911
void jas_image_writecmptsample(jas_image_t *image, int cmptno, int x, int y,
914
jas_image_cmpt_t *cmpt;
919
cmpt = image->cmpts_[cmptno];
921
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * y + x) * cmpt->cps_,
925
t = inttobits(v, cmpt->prec_, cmpt->sgnd_);
926
for (k = cmpt->cps_; k > 0; --k) {
927
c = (t >> (8 * (cmpt->cps_ - 1))) & 0xff;
928
if (jas_stream_putc(cmpt->stream_, (unsigned char) c) == EOF) {
935
int jas_image_getcmptbytype(jas_image_t *image, int ctype)
939
for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
940
if (image->cmpts_[cmptno]->type_ == ctype) {
962
/***********************************************/
963
/***********************************************/
964
/***********************************************/
965
/***********************************************/
967
int jas_image_readcmpt2(jas_image_t *image, int cmptno, jas_image_coord_t x,
968
jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
971
jas_image_cmpt_t *cmpt;
977
if (cmptno < 0 || cmptno >= image->numcmpts_)
979
cmpt = image->cmpts_[cmptno];
980
if (x < 0 || x >= cmpt->width_ || y < 0 || y >= cmpt->height_ ||
981
width < 0 || height < 0 || x + width > cmpt->width_ ||
982
y + height > cmpt->height_)
986
for (i = 0; i < height; ++i) {
987
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x)
988
* cmpt->cps_, SEEK_SET) < 0)
990
for (j = 0; j < width; ++j) {
991
if (getint(cmpt->stream_, cmpt->sgnd_, cmpt->prec_, &v))
1002
int jas_image_writecmpt2(jas_image_t *image, int cmptno, jas_image_coord_t x,
1003
jas_image_coord_t y, jas_image_coord_t width, jas_image_coord_t height,
1006
jas_image_cmpt_t *cmpt;
1007
jas_image_coord_t i;
1008
jas_image_coord_t j;
1012
if (cmptno < 0 || cmptno >= image->numcmpts_)
1014
cmpt = image->cmpts_[cmptno];
1015
if (x < 0 || x >= cmpt->width_ || y < 0 || y >= cmpt->height_ ||
1016
width < 0 || height < 0 || x + width > cmpt->width_ ||
1017
y + height > cmpt->height_)
1021
for (i = 0; i < height; ++i) {
1022
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x)
1023
* cmpt->cps_, SEEK_SET) < 0)
1025
for (j = 0; j < width; ++j) {
1027
if (putint(cmpt->stream_, cmpt->sgnd_, cmpt->prec_, v))
1037
int jas_image_sampcmpt(jas_image_t *image, int cmptno, int newcmptno,
1038
jas_image_coord_t ho, jas_image_coord_t vo, jas_image_coord_t hs,
1039
jas_image_coord_t vs, int sgnd, int prec)
1041
jas_image_cmpt_t *oldcmpt;
1042
jas_image_cmpt_t *newcmpt;
1045
jas_image_coord_t tlx;
1046
jas_image_coord_t tly;
1047
jas_image_coord_t brx;
1048
jas_image_coord_t bry;
1051
jas_image_cmptparm_t cmptparm;
1052
jas_image_coord_t ax;
1053
jas_image_coord_t ay;
1054
jas_image_coord_t bx;
1055
jas_image_coord_t by;
1056
jas_image_coord_t d0;
1057
jas_image_coord_t d1;
1058
jas_image_coord_t d2;
1059
jas_image_coord_t d3;
1060
jas_image_coord_t oldx;
1061
jas_image_coord_t oldy;
1062
jas_image_coord_t x;
1063
jas_image_coord_t y;
1065
jas_image_coord_t cmptbrx;
1066
jas_image_coord_t cmptbry;
1068
assert(cmptno >= 0 && cmptno < image->numcmpts_);
1069
oldcmpt = image->cmpts_[cmptno];
1070
assert(oldcmpt->tlx_ == 0 && oldcmpt->tly_ == 0);
1071
jas_image_calcbbox2(image, &tlx, &tly, &brx, &bry);
1072
width = FLOORDIV(brx - ho + hs, hs);
1073
height = FLOORDIV(bry - vo + vs, vs);
1076
cmptparm.hstep = hs;
1077
cmptparm.vstep = vs;
1078
cmptparm.width = width;
1079
cmptparm.height = height;
1080
cmptparm.prec = prec;
1081
cmptparm.sgnd = sgnd;
1082
if (jas_image_addcmpt(image, newcmptno, &cmptparm))
1084
cmptbrx = oldcmpt->tlx_ + (oldcmpt->width_ - 1) * oldcmpt->hstep_;
1085
cmptbry = oldcmpt->tly_ + (oldcmpt->height_ - 1) * oldcmpt->vstep_;
1086
newcmpt = image->cmpts_[newcmptno];
1087
jas_stream_rewind(newcmpt->stream_);
1088
for (i = 0; i < height; ++i) {
1089
y = newcmpt->tly_ + newcmpt->vstep_ * i;
1090
for (j = 0; j < width; ++j) {
1091
x = newcmpt->tlx_ + newcmpt->hstep_ * j;
1092
ax = downtomult(x - oldcmpt->tlx_, oldcmpt->hstep_) + oldcmpt->tlx_;
1093
ay = downtomult(y - oldcmpt->tly_, oldcmpt->vstep_) + oldcmpt->tly_;
1094
bx = uptomult(x - oldcmpt->tlx_, oldcmpt->hstep_) + oldcmpt->tlx_;
1097
by = uptomult(y - oldcmpt->tly_, oldcmpt->vstep_) + oldcmpt->tly_;
1100
d0 = (ax - x) * (ax - x) + (ay - y) * (ay - y);
1101
d1 = (bx - x) * (bx - x) + (ay - y) * (ay - y);
1102
d2 = (bx - x) * (bx - x) + (by - y) * (by - y);
1103
d3 = (ax - x) * (ax - x) + (by - y) * (by - y);
1104
if (d0 <= d1 && d0 <= d2 && d0 <= d3) {
1105
oldx = (ax - oldcmpt->tlx_) / oldcmpt->hstep_;
1106
oldy = (ay - oldcmpt->tly_) / oldcmpt->vstep_;
1107
} else if (d1 <= d0 && d1 <= d2 && d1 <= d3) {
1108
oldx = (bx - oldcmpt->tlx_) / oldcmpt->hstep_;
1109
oldy = (ay - oldcmpt->tly_) / oldcmpt->vstep_;
1110
} else if (d2 <= d0 && d2 <= d1 && d1 <= d3) {
1111
oldx = (bx - oldcmpt->tlx_) / oldcmpt->hstep_;
1112
oldy = (by - oldcmpt->tly_) / oldcmpt->vstep_;
1114
oldx = (ax - oldcmpt->tlx_) / oldcmpt->hstep_;
1115
oldy = (by - oldcmpt->tly_) / oldcmpt->vstep_;
1117
assert(oldx >= 0 && oldx < oldcmpt->width_ &&
1118
oldy >= 0 && oldy < oldcmpt->height_);
1119
if (jas_stream_seek(oldcmpt->stream_, oldcmpt->cps_ *
1120
(oldy * oldcmpt->width_ + oldx), SEEK_SET) < 0)
1122
if (getint(oldcmpt->stream_, oldcmpt->sgnd_,
1123
oldcmpt->prec_, &v))
1125
if (newcmpt->prec_ != oldcmpt->prec_ ||
1126
newcmpt->sgnd_ != oldcmpt->sgnd_) {
1127
v = convert(v, oldcmpt->sgnd_, oldcmpt->prec_,
1128
newcmpt->sgnd_, newcmpt->prec_);
1130
if (putint(newcmpt->stream_, newcmpt->sgnd_,
1140
int jas_image_ishomosamp(jas_image_t *image)
1142
jas_image_coord_t hstep;
1143
jas_image_coord_t vstep;
1146
hstep = jas_image_cmpthstep(image, 0);
1147
vstep = jas_image_cmptvstep(image, 0);
1149
for (i = 0; i < image->numcmpts_; ++i) {
1150
if (jas_image_cmpthstep(image, i) != hstep ||
1151
jas_image_cmptvstep(image, i) != vstep) {
1159
/* Note: This function defines a bounding box differently. */
1160
static void jas_image_calcbbox2(jas_image_t *image, jas_image_coord_t *tlx,
1161
jas_image_coord_t *tly, jas_image_coord_t *brx, jas_image_coord_t *bry)
1163
jas_image_cmpt_t *cmpt;
1164
jas_image_coord_t tmptlx;
1165
jas_image_coord_t tmptly;
1166
jas_image_coord_t tmpbrx;
1167
jas_image_coord_t tmpbry;
1168
jas_image_coord_t t;
1170
if (image->numcmpts_ > 0) {
1171
cmpt = image->cmpts_[0];
1172
tmptlx = cmpt->tlx_;
1173
tmptly = cmpt->tly_;
1174
tmpbrx = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1);
1175
tmpbry = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1);
1176
for (i = 0; i < image->numcmpts_; ++i) {
1177
cmpt = image->cmpts_[i];
1178
if (cmpt->tlx_ < tmptlx)
1179
tmptlx = cmpt->tlx_;
1180
if (cmpt->tly_ < tmptly)
1181
tmptly = cmpt->tly_;
1182
t = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1);
1185
t = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1);
1203
static int getint(jas_stream_t *in, int sgnd, int prec, long *val)
1211
if ((c = jas_stream_getc(in)) == EOF)
1215
v &= ((1 << prec) - 1);
1217
/* XXX - Do something here. */
1225
static int putint(jas_stream_t *out, int sgnd, int prec, long val)
1230
/* XXX - Do something here. */
1233
val &= (1 << prec) - 1;
1236
c = (val >> (n * 8)) & 0xff;
1237
if (jas_stream_putc(out, c) != c)
1243
static long convert(long val, int oldsgnd, int oldprec, int newsgnd,
1246
if (newsgnd != oldsgnd) {
1248
if (newprec != oldprec) {
1249
if (newprec > oldprec) {
1250
val <<= newprec - oldprec;
1251
} else if (oldprec > newprec) {
1252
val >>= oldprec - newprec;
1258
static long downtomult(long x, long y)
1264
static long uptomult(long x, long y)
1267
return ((x + y - 1) / y) * y;
1270
jas_image_t *jas_image_chclrspc(jas_image_t *image, jas_cmprof_t *outprof,
1273
jas_image_t *inimage;
1287
jas_image_t *outimage;
1290
jas_cmprof_t *inprof;
1291
jas_cmprof_t *tmpprof;
1292
jas_image_cmptparm_t cmptparm;
1295
jas_cmxform_t *xform;
1296
jas_cmpixmap_t inpixmap;
1297
jas_cmpixmap_t outpixmap;
1298
jas_cmcmptfmt_t *incmptfmts;
1299
jas_cmcmptfmt_t *outcmptfmts;
1302
jas_eprintf("IMAGE\n");
1303
jas_image_dump(image, stderr);
1306
if (!(inimage = jas_image_copy(image)))
1310
if (!jas_image_ishomosamp(inimage)) {
1311
minhstep = jas_image_cmpthstep(inimage, 0);
1312
minvstep = jas_image_cmptvstep(inimage, 0);
1313
for (i = 1; i < jas_image_numcmpts(inimage); ++i) {
1314
hstep = jas_image_cmpthstep(inimage, i);
1315
vstep = jas_image_cmptvstep(inimage, i);
1316
if (hstep < minhstep)
1318
if (vstep < minvstep)
1321
n = jas_image_numcmpts(inimage);
1322
for (i = 0; i < n; ++i) {
1323
cmpttype = jas_image_cmpttype(inimage, i);
1324
if (jas_image_sampcmpt(inimage, i, i + 1, 0, 0, minhstep, minvstep, jas_image_cmptsgnd(inimage, i), jas_image_cmptprec(inimage, i)))
1326
jas_image_setcmpttype(inimage, i + 1, cmpttype);
1327
jas_image_delcmpt(inimage, i);
1331
width = jas_image_cmptwidth(inimage, 0);
1332
height = jas_image_cmptheight(inimage, 0);
1333
hstep = jas_image_cmpthstep(inimage, 0);
1334
vstep = jas_image_cmptvstep(inimage, 0);
1336
inprof = jas_image_cmprof(inimage);
1338
numinclrchans = jas_clrspc_numchans(jas_cmprof_clrspc(inprof));
1339
numinauxchans = jas_image_numcmpts(inimage) - numinclrchans;
1340
numoutclrchans = jas_clrspc_numchans(jas_cmprof_clrspc(outprof));
1342
numoutchans = numoutclrchans + numoutauxchans;
1345
if (!(outimage = jas_image_create0()))
1348
/* Create a component for each of the colorants. */
1349
for (i = 0; i < numoutclrchans; ++i) {
1352
cmptparm.hstep = hstep;
1353
cmptparm.vstep = vstep;
1354
cmptparm.width = width;
1355
cmptparm.height = height;
1356
cmptparm.prec = prec;
1358
if (jas_image_addcmpt(outimage, -1, &cmptparm))
1360
jas_image_setcmpttype(outimage, i, JAS_IMAGE_CT_COLOR(i));
1363
/* Copy the auxiliary components without modification. */
1364
for (i = 0; i < jas_image_numcmpts(inimage); ++i) {
1365
if (!ISCOLOR(jas_image_cmpttype(inimage, i))) {
1366
jas_image_copycmpt(outimage, -1, inimage, i);
1367
/* XXX - need to specify laydown of component on ref. grid */
1372
if (!(tmpprof = jas_cmprof_copy(outprof)))
1374
assert(!jas_image_cmprof(outimage));
1375
jas_image_setcmprof(outimage, tmpprof);
1377
jas_image_setclrspc(outimage, jas_cmprof_clrspc(outprof));
1379
if (!(xform = jas_cmxform_create(inprof, outprof, 0, JAS_CMXFORM_OP_FWD, intent, 0)))
1382
inpixmap.numcmpts = numinclrchans;
1383
incmptfmts = malloc(numinclrchans * sizeof(jas_cmcmptfmt_t));
1385
inpixmap.cmptfmts = incmptfmts;
1386
for (i = 0; i < numinclrchans; ++i) {
1387
j = jas_image_getcmptbytype(inimage, JAS_IMAGE_CT_COLOR(i));
1389
if (!(incmptfmts[i].buf = malloc(width * sizeof(long))))
1391
incmptfmts[i].prec = jas_image_cmptprec(inimage, j);
1392
incmptfmts[i].sgnd = jas_image_cmptsgnd(inimage, j);
1393
incmptfmts[i].width = width;
1394
incmptfmts[i].height = 1;
1397
outpixmap.numcmpts = numoutclrchans;
1398
outcmptfmts = malloc(numoutclrchans * sizeof(jas_cmcmptfmt_t));
1399
assert(outcmptfmts);
1400
outpixmap.cmptfmts = outcmptfmts;
1402
for (i = 0; i < numoutclrchans; ++i) {
1403
j = jas_image_getcmptbytype(outimage, JAS_IMAGE_CT_COLOR(i));
1405
if (!(outcmptfmts[i].buf = malloc(width * sizeof(long))))
1407
outcmptfmts[i].prec = jas_image_cmptprec(outimage, j);
1408
outcmptfmts[i].sgnd = jas_image_cmptsgnd(outimage, j);
1409
outcmptfmts[i].width = width;
1410
outcmptfmts[i].height = 1;
1413
for (i = 0; i < height; ++i) {
1414
for (j = 0; j < numinclrchans; ++j) {
1415
k = jas_image_getcmptbytype(inimage, JAS_IMAGE_CT_COLOR(j));
1416
if (jas_image_readcmpt2(inimage, k, 0, i, width, 1, incmptfmts[j].buf))
1419
jas_cmxform_apply(xform, &inpixmap, &outpixmap);
1420
for (j = 0; j < numoutclrchans; ++j) {
1421
k = jas_image_getcmptbytype(outimage, JAS_IMAGE_CT_COLOR(j));
1422
if (jas_image_writecmpt2(outimage, k, 0, i, width, 1, outcmptfmts[j].buf))
1427
for (i = 0; i < numoutclrchans; ++i)
1428
jas_free(outcmptfmts[i].buf);
1429
jas_free(outcmptfmts);
1430
for (i = 0; i < numinclrchans; ++i)
1431
jas_free(incmptfmts[i].buf);
1432
jas_free(incmptfmts);
1433
jas_cmxform_destroy(xform);
1434
jas_image_destroy(inimage);
1437
jas_eprintf("INIMAGE\n");
1438
jas_image_dump(inimage, stderr);
1439
jas_eprintf("OUTIMAGE\n");
1440
jas_image_dump(outimage, stderr);