| 1 |
/* |
| 2 |
* Copyright (c) 1999-2000 Image Power, Inc. and the University of |
| 3 |
* British Columbia. |
| 4 |
* Copyright (c) 2001-2002 Michael David Adams. |
| 5 |
* All rights reserved. |
| 6 |
*/ |
| 7 |
|
| 8 |
/* __START_OF_JASPER_LICENSE__ |
| 9 |
* |
| 10 |
* JasPer Software License |
| 11 |
* |
| 12 |
* IMAGE POWER JPEG-2000 PUBLIC LICENSE |
| 13 |
* ************************************ |
| 14 |
* |
| 15 |
* GRANT: |
| 16 |
* |
| 17 |
* Permission is hereby granted, free of charge, to any person (the "User") |
| 18 |
* obtaining a copy of this software and associated documentation, to deal |
| 19 |
* in the JasPer Software without restriction, including without limitation |
| 20 |
* the right to use, copy, modify, merge, publish, distribute, sublicense, |
| 21 |
* and/or sell copies of the JasPer Software (in source and binary forms), |
| 22 |
* and to permit persons to whom the JasPer Software is furnished to do so, |
| 23 |
* provided further that the License Conditions below are met. |
| 24 |
* |
| 25 |
* License Conditions |
| 26 |
* ****************** |
| 27 |
* |
| 28 |
* A. Redistributions of source code must retain the above copyright notice, |
| 29 |
* and this list of conditions, and the following disclaimer. |
| 30 |
* |
| 31 |
* B. Redistributions in binary form must reproduce the above copyright |
| 32 |
* notice, and this list of conditions, and the following disclaimer in |
| 33 |
* the documentation and/or other materials provided with the distribution. |
| 34 |
* |
| 35 |
* C. Neither the name of Image Power, Inc. nor any other contributor |
| 36 |
* (including, but not limited to, the University of British Columbia and |
| 37 |
* Michael David Adams) may be used to endorse or promote products derived |
| 38 |
* from this software without specific prior written permission. |
| 39 |
* |
| 40 |
* D. User agrees that it shall not commence any action against Image Power, |
| 41 |
* Inc., the University of British Columbia, Michael David Adams, or any |
| 42 |
* other contributors (collectively "Licensors") for infringement of any |
| 43 |
* intellectual property rights ("IPR") held by the User in respect of any |
| 44 |
* technology that User owns or has a right to license or sublicense and |
| 45 |
* which is an element required in order to claim compliance with ISO/IEC |
| 46 |
* 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property |
| 47 |
* rights worldwide arising under statutory or common law, and whether |
| 48 |
* or not perfected, including, without limitation, all (i) patents and |
| 49 |
* patent applications owned or licensable by User; (ii) rights associated |
| 50 |
* with works of authorship including copyrights, copyright applications, |
| 51 |
* copyright registrations, mask work rights, mask work applications, |
| 52 |
* mask work registrations; (iii) rights relating to the protection of |
| 53 |
* trade secrets and confidential information; (iv) any right analogous |
| 54 |
* to those set forth in subsections (i), (ii), or (iii) and any other |
| 55 |
* proprietary rights relating to intangible property (other than trademark, |
| 56 |
* trade dress, or service mark rights); and (v) divisions, continuations, |
| 57 |
* renewals, reissues and extensions of the foregoing (as and to the extent |
| 58 |
* applicable) now existing, hereafter filed, issued or acquired. |
| 59 |
* |
| 60 |
* E. If User commences an infringement action against any Licensor(s) then |
| 61 |
* such Licensor(s) shall have the right to terminate User's license and |
| 62 |
* all sublicenses that have been granted hereunder by User to other parties. |
| 63 |
* |
| 64 |
* F. This software is for use only in hardware or software products that |
| 65 |
* are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license |
| 66 |
* or right to this Software is granted for products that do not comply |
| 67 |
* with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased |
| 68 |
* from the ISO. |
| 69 |
* |
| 70 |
* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. |
| 71 |
* NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
| 72 |
* THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND |
| 73 |
* CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY |
| 74 |
* OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, |
| 75 |
* WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE, |
| 76 |
* IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING |
| 77 |
* TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE |
| 78 |
* OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING |
| 79 |
* PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS. |
| 80 |
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE |
| 81 |
* IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE |
| 82 |
* IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY |
| 83 |
* OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY |
| 84 |
* OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING, |
| 85 |
* REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, |
| 86 |
* WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE |
| 87 |
* INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC., |
| 88 |
* MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE |
| 89 |
* JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO |
| 90 |
* THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR |
| 91 |
* CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, |
| 92 |
* DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR |
| 93 |
* MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF |
| 94 |
* SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY |
| 95 |
* OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT |
| 96 |
* FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR |
| 97 |
* RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING |
| 98 |
* FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, |
| 99 |
* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT |
| 100 |
* LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE |
| 101 |
* JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY |
| 102 |
* TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE |
| 103 |
* ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS |
| 104 |
* OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT |
| 105 |
* KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING |
| 106 |
* TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS |
| 107 |
* CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE |
| 108 |
* NOTICE SPECIFIED IN THIS SECTION. |
| 109 |
* |
| 110 |
* __END_OF_JASPER_LICENSE__ |
| 111 |
*/ |
| 112 |
|
| 113 |
/* |
| 114 |
* Image Library |
| 115 |
* |
| 116 |
* $Id$ |
| 117 |
*/ |
| 118 |
|
| 119 |
/******************************************************************************\ |
| 120 |
* Includes. |
| 121 |
\******************************************************************************/ |
| 122 |
|
| 123 |
#include <stdlib.h> |
| 124 |
#include <stdio.h> |
| 125 |
#include <string.h> |
| 126 |
#include <assert.h> |
| 127 |
#include <ctype.h> |
| 128 |
|
| 129 |
#include "netpbm/nstring.h" |
| 130 |
|
| 131 |
#include "jasper/jas_math.h" |
| 132 |
#include "jasper/jas_image.h" |
| 133 |
#include "jasper/jas_malloc.h" |
| 134 |
#include "jasper/jas_string.h" |
| 135 |
|
| 136 |
/******************************************************************************\ |
| 137 |
* Types. |
| 138 |
\******************************************************************************/ |
| 139 |
|
| 140 |
/******************************************************************************\ |
| 141 |
* Local prototypes. |
| 142 |
\******************************************************************************/ |
| 143 |
|
| 144 |
static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt); |
| 145 |
static jas_image_cmpt_t *jas_image_cmpt_create(uint_fast32_t tlx, uint_fast32_t tly, |
| 146 |
uint_fast32_t hstep, uint_fast32_t vstep, uint_fast32_t width, uint_fast32_t |
| 147 |
height, uint_fast16_t depth, bool sgnd, uint_fast32_t inmem); |
| 148 |
static void jas_image_setbbox(jas_image_t *image); |
| 149 |
static jas_image_cmpt_t *jas_image_cmpt_copy(jas_image_cmpt_t *cmpt); |
| 150 |
static jas_image_cmpt_t *jas_image_cmpt_create0(); |
| 151 |
static int jas_image_growcmpts(jas_image_t *image, int maxcmpts); |
| 152 |
static uint_fast32_t inttobits(jas_seqent_t v, int prec, bool sgnd); |
| 153 |
static jas_seqent_t bitstoint(uint_fast32_t v, int prec, bool sgnd); |
| 154 |
|
| 155 |
/******************************************************************************\ |
| 156 |
* Global data. |
| 157 |
\******************************************************************************/ |
| 158 |
|
| 159 |
static int jas_image_numfmts = 0; |
| 160 |
static jas_image_fmtinfo_t jas_image_fmtinfos[JAS_IMAGE_MAXFMTS]; |
| 161 |
|
| 162 |
/******************************************************************************\ |
| 163 |
* Create and destroy operations. |
| 164 |
\******************************************************************************/ |
| 165 |
|
| 166 |
jas_image_t *jas_image_create(uint_fast16_t numcmpts, jas_image_cmptparm_t *cmptparms, |
| 167 |
int colorspace) |
| 168 |
{ |
| 169 |
jas_image_t *image; |
| 170 |
uint_fast32_t rawsize; |
| 171 |
uint_fast32_t inmem; |
| 172 |
uint_fast16_t cmptno; |
| 173 |
jas_image_cmptparm_t *cmptparm; |
| 174 |
|
| 175 |
if (!(image = jas_image_create0())) { |
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
image->colorspace_ = colorspace; |
| 180 |
image->maxcmpts_ = numcmpts; |
| 181 |
image->inmem_ = true; |
| 182 |
|
| 183 |
/* Allocate memory for the per-component information. */ |
| 184 |
if (!(image->cmpts_ = jas_malloc(image->maxcmpts_ * |
| 185 |
sizeof(jas_image_cmpt_t *)))) { |
| 186 |
jas_image_destroy(image); |
| 187 |
return 0; |
| 188 |
} |
| 189 |
/* Initialize in case of failure. */ |
| 190 |
for (cmptno = 0; cmptno < image->maxcmpts_; ++cmptno) { |
| 191 |
image->cmpts_[cmptno] = 0; |
| 192 |
} |
| 193 |
|
| 194 |
/* Compute the approximate raw size of the image. */ |
| 195 |
rawsize = 0; |
| 196 |
for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, |
| 197 |
++cmptparm) { |
| 198 |
rawsize += cmptparm->width * cmptparm->height * |
| 199 |
(cmptparm->prec + 7) / 8; |
| 200 |
} |
| 201 |
/* Decide whether to buffer the image data in memory, based on the |
| 202 |
raw size of the image. */ |
| 203 |
inmem = (rawsize < JAS_IMAGE_INMEMTHRESH); |
| 204 |
|
| 205 |
/* Create the individual image components. */ |
| 206 |
for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, |
| 207 |
++cmptparm) { |
| 208 |
if (!(image->cmpts_[cmptno] = jas_image_cmpt_create(cmptparm->tlx, |
| 209 |
cmptparm->tly, cmptparm->hstep, cmptparm->vstep, |
| 210 |
cmptparm->width, cmptparm->height, cmptparm->prec, |
| 211 |
cmptparm->sgnd, inmem))) { |
| 212 |
jas_image_destroy(image); |
| 213 |
return 0; |
| 214 |
} |
| 215 |
++image->numcmpts_; |
| 216 |
} |
| 217 |
|
| 218 |
/* Determine the bounding box for all of the components on the |
| 219 |
reference grid (i.e., the image area) */ |
| 220 |
jas_image_setbbox(image); |
| 221 |
|
| 222 |
return image; |
| 223 |
} |
| 224 |
|
| 225 |
jas_image_t *jas_image_create0() |
| 226 |
{ |
| 227 |
jas_image_t *image; |
| 228 |
|
| 229 |
if (!(image = jas_malloc(sizeof(jas_image_t)))) { |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
image->tlx_ = 0; |
| 234 |
image->tly_ = 0; |
| 235 |
image->brx_ = 0; |
| 236 |
image->bry_ = 0; |
| 237 |
image->colorspace_ = JAS_IMAGE_CS_UNKNOWN; |
| 238 |
image->numcmpts_ = 0; |
| 239 |
image->maxcmpts_ = 0; |
| 240 |
image->cmpts_ = 0; |
| 241 |
image->inmem_ = true; |
| 242 |
image->iccp_ = 0; |
| 243 |
image->iccplen_ = 0; |
| 244 |
|
| 245 |
return image; |
| 246 |
} |
| 247 |
|
| 248 |
jas_image_t *jas_image_copy(jas_image_t *image) |
| 249 |
{ |
| 250 |
jas_image_t *newimage; |
| 251 |
int cmptno; |
| 252 |
|
| 253 |
newimage = jas_image_create0(); |
| 254 |
if (jas_image_growcmpts(newimage, image->numcmpts_)) { |
| 255 |
goto error; |
| 256 |
} |
| 257 |
for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) { |
| 258 |
if (!(newimage->cmpts_[cmptno] = jas_image_cmpt_copy(image->cmpts_[cmptno]))) { |
| 259 |
goto error; |
| 260 |
} |
| 261 |
++newimage->numcmpts_; |
| 262 |
} |
| 263 |
|
| 264 |
jas_image_setbbox(newimage); |
| 265 |
|
| 266 |
return newimage; |
| 267 |
error: |
| 268 |
if (newimage) { |
| 269 |
jas_image_destroy(newimage); |
| 270 |
} |
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
static jas_image_cmpt_t *jas_image_cmpt_create0() |
| 275 |
{ |
| 276 |
jas_image_cmpt_t *cmpt; |
| 277 |
if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) { |
| 278 |
return 0; |
| 279 |
} |
| 280 |
memset(cmpt, 0, sizeof(jas_image_cmpt_t)); |
| 281 |
cmpt->type_ = JAS_IMAGE_CT_UNKNOWN; |
| 282 |
return cmpt; |
| 283 |
} |
| 284 |
|
| 285 |
static jas_image_cmpt_t *jas_image_cmpt_copy(jas_image_cmpt_t *cmpt) |
| 286 |
{ |
| 287 |
jas_image_cmpt_t *newcmpt; |
| 288 |
|
| 289 |
if (!(newcmpt = jas_image_cmpt_create0())) { |
| 290 |
return 0; |
| 291 |
} |
| 292 |
newcmpt->tlx_ = cmpt->tlx_; |
| 293 |
newcmpt->tly_ = cmpt->tly_; |
| 294 |
newcmpt->hstep_ = cmpt->hstep_; |
| 295 |
newcmpt->vstep_ = cmpt->vstep_; |
| 296 |
newcmpt->width_ = cmpt->width_; |
| 297 |
newcmpt->height_ = cmpt->height_; |
| 298 |
newcmpt->prec_ = cmpt->prec_; |
| 299 |
newcmpt->sgnd_ = cmpt->sgnd_; |
| 300 |
newcmpt->cps_ = cmpt->cps_; |
| 301 |
newcmpt->type_ = cmpt->type_; |
| 302 |
if (!(newcmpt->stream_ = jas_stream_memopen(0, 0))) { |
| 303 |
return 0; |
| 304 |
} |
| 305 |
if (jas_stream_seek(cmpt->stream_, 0, SEEK_SET)) { |
| 306 |
return 0; |
| 307 |
} |
| 308 |
if (jas_stream_copy(newcmpt->stream_, cmpt->stream_, -1)) { |
| 309 |
return 0; |
| 310 |
} |
| 311 |
if (jas_stream_seek(newcmpt->stream_, 0, SEEK_SET)) { |
| 312 |
return 0; |
| 313 |
} |
| 314 |
return newcmpt; |
| 315 |
} |
| 316 |
|
| 317 |
void jas_image_destroy(jas_image_t *image) |
| 318 |
{ |
| 319 |
int i; |
| 320 |
|
| 321 |
if (image->cmpts_) { |
| 322 |
for (i = 0; i < image->numcmpts_; ++i) { |
| 323 |
jas_image_cmpt_destroy(image->cmpts_[i]); |
| 324 |
image->cmpts_[i] = 0; |
| 325 |
} |
| 326 |
jas_free(image->cmpts_); |
| 327 |
} |
| 328 |
jas_free(image); |
| 329 |
} |
| 330 |
|
| 331 |
static jas_image_cmpt_t *jas_image_cmpt_create(uint_fast32_t tlx, uint_fast32_t tly, |
| 332 |
uint_fast32_t hstep, uint_fast32_t vstep, uint_fast32_t width, uint_fast32_t |
| 333 |
height, uint_fast16_t depth, bool sgnd, uint_fast32_t inmem) |
| 334 |
{ |
| 335 |
jas_image_cmpt_t *cmpt; |
| 336 |
long size; |
| 337 |
|
| 338 |
if (!(cmpt = jas_malloc(sizeof(jas_image_cmpt_t)))) { |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
cmpt->tlx_ = tlx; |
| 343 |
cmpt->tly_ = tly; |
| 344 |
cmpt->hstep_ = hstep; |
| 345 |
cmpt->vstep_ = vstep; |
| 346 |
cmpt->width_ = width; |
| 347 |
cmpt->height_ = height; |
| 348 |
cmpt->prec_ = depth; |
| 349 |
cmpt->sgnd_ = sgnd; |
| 350 |
cmpt->stream_ = 0; |
| 351 |
cmpt->cps_ = (depth + 7) / 8; |
| 352 |
|
| 353 |
size = cmpt->width_ * cmpt->height_ * cmpt->cps_; |
| 354 |
cmpt->stream_ = (inmem) ? jas_stream_memopen(0, size) : jas_stream_tmpfile(); |
| 355 |
if (!cmpt->stream_) { |
| 356 |
jas_image_cmpt_destroy(cmpt); |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
/* Zero the component data. This isn't necessary, but it is |
| 361 |
convenient for debugging purposes. */ |
| 362 |
if (jas_stream_seek(cmpt->stream_, size - 1, SEEK_SET) < 0 || |
| 363 |
jas_stream_putc(cmpt->stream_, 0) == EOF || |
| 364 |
jas_stream_seek(cmpt->stream_, 0, SEEK_SET) < 0) { |
| 365 |
jas_image_cmpt_destroy(cmpt); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
return cmpt; |
| 370 |
} |
| 371 |
|
| 372 |
static void jas_image_cmpt_destroy(jas_image_cmpt_t *cmpt) |
| 373 |
{ |
| 374 |
if (cmpt->stream_) { |
| 375 |
jas_stream_close(cmpt->stream_); |
| 376 |
} |
| 377 |
jas_free(cmpt); |
| 378 |
} |
| 379 |
|
| 380 |
/*****************************************************************************\ |
| 381 |
* Load and save operations. |
| 382 |
\*****************************************************************************/ |
| 383 |
|
| 384 |
void |
| 385 |
pmjas_image_decode(jas_stream_t * const in, |
| 386 |
int const fmtArg, |
| 387 |
const char * const optstr, |
| 388 |
jas_image_t ** const imagePP, |
| 389 |
const char ** const errorP) { |
| 390 |
/*---------------------------------------------------------------------------- |
| 391 |
Create an image from a stream in some specified format |
| 392 |
-----------------------------------------------------------------------------*/ |
| 393 |
jas_image_fmtinfo_t *fmtinfo; |
| 394 |
int fmt; |
| 395 |
|
| 396 |
/* If possible, try to determine the format of the input data. */ |
| 397 |
if (fmtArg < 0) { |
| 398 |
if ((fmt = jas_image_getfmt(in)) < 0) { |
| 399 |
pm_asprintf(errorP, "jas_image_getfmt failed"); |
| 400 |
return; |
| 401 |
} |
| 402 |
} else |
| 403 |
fmt = fmtArg; |
| 404 |
|
| 405 |
if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) { |
| 406 |
pm_asprintf(errorP, "jas_image_lookupfmtbyid of format %d failed", |
| 407 |
fmt); |
| 408 |
return; |
| 409 |
} |
| 410 |
{ |
| 411 |
const char * error; |
| 412 |
|
| 413 |
(*fmtinfo->ops.decode)(in, optstr, imagePP, &error); |
| 414 |
if (error) { |
| 415 |
pm_asprintf(errorP, "decoder failed. %s", error); |
| 416 |
pm_strfree(error); |
| 417 |
} else { |
| 418 |
*errorP = NULL; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
int jas_image_encode(jas_image_t *image, jas_stream_t *out, int fmt, char *optstr) |
| 426 |
{ |
| 427 |
jas_image_fmtinfo_t *fmtinfo; |
| 428 |
if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) { |
| 429 |
return -1; |
| 430 |
} |
| 431 |
return (fmtinfo->ops.encode) ? (*fmtinfo->ops.encode)(image, out, |
| 432 |
optstr) : (-1); |
| 433 |
} |
| 434 |
|
| 435 |
/******************************************************************************\ |
| 436 |
* Component read and write operations. |
| 437 |
\******************************************************************************/ |
| 438 |
|
| 439 |
int jas_image_readcmpt(jas_image_t *image, uint_fast16_t cmptno, uint_fast32_t x, uint_fast32_t y, uint_fast32_t width, |
| 440 |
uint_fast32_t height, jas_matrix_t *data) |
| 441 |
{ |
| 442 |
jas_image_cmpt_t *cmpt; |
| 443 |
uint_fast32_t i; |
| 444 |
uint_fast32_t j; |
| 445 |
int k; |
| 446 |
jas_seqent_t v; |
| 447 |
int c; |
| 448 |
jas_seqent_t *dr; |
| 449 |
jas_seqent_t *d; |
| 450 |
int drs; |
| 451 |
|
| 452 |
if (cmptno >= image->numcmpts_) { |
| 453 |
return -1; |
| 454 |
} |
| 455 |
|
| 456 |
cmpt = image->cmpts_[cmptno]; |
| 457 |
if (x >= cmpt->width_ || y >= cmpt->height_ || |
| 458 |
x + width > cmpt->width_ || |
| 459 |
y + height > cmpt->height_) { |
| 460 |
return -1; |
| 461 |
} |
| 462 |
|
| 463 |
if (jas_matrix_numrows(data) != height || jas_matrix_numcols(data) != width) { |
| 464 |
if (jas_matrix_resize(data, height, width)) { |
| 465 |
return -1; |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
dr = jas_matrix_getref(data, 0, 0); |
| 470 |
drs = jas_matrix_rowstep(data); |
| 471 |
for (i = 0; i < height; ++i, dr += drs) { |
| 472 |
d = dr; |
| 473 |
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x) |
| 474 |
* cmpt->cps_, SEEK_SET) < 0) { |
| 475 |
return -1; |
| 476 |
} |
| 477 |
for (j = width; j > 0; --j, ++d) { |
| 478 |
v = 0; |
| 479 |
for (k = cmpt->cps_; k > 0; --k) { |
| 480 |
if ((c = jas_stream_getc(cmpt->stream_)) == EOF) { |
| 481 |
return -1; |
| 482 |
} |
| 483 |
v = (v << 8) | (c & 0xff); |
| 484 |
} |
| 485 |
*d = bitstoint(v, cmpt->prec_, cmpt->sgnd_); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
#if 0 |
| 493 |
int_fast64_t jas_image_readcmpt1(jas_image_t *image, uint_fast16_t cmptno, |
| 494 |
uint_fast32_t x, uint_fast32_t y) |
| 495 |
{ |
| 496 |
jas_image_cmpt_t *cmpt; |
| 497 |
int k; |
| 498 |
int c; |
| 499 |
int_fast64_t v; |
| 500 |
cmpt = image->cmpts_[cmptno]; |
| 501 |
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * y + x) * cmpt->cps_, |
| 502 |
SEEK_SET) < 0) { |
| 503 |
goto error; |
| 504 |
} |
| 505 |
v = 0; |
| 506 |
for (k = cmpt->cps_; k > 0; --k) { |
| 507 |
if ((c = jas_stream_getc(cmpt->stream_)) == EOF) { |
| 508 |
goto error; |
| 509 |
} |
| 510 |
v = (v << 8) | (c & 0xff); |
| 511 |
} |
| 512 |
if (cmpt->sgnd_) { |
| 513 |
abort(); |
| 514 |
} |
| 515 |
|
| 516 |
return v; |
| 517 |
|
| 518 |
error: |
| 519 |
return 0; |
| 520 |
} |
| 521 |
#endif |
| 522 |
|
| 523 |
int jas_image_writecmpt(jas_image_t *image, uint_fast16_t cmptno, uint_fast32_t x, uint_fast32_t y, uint_fast32_t width, |
| 524 |
uint_fast32_t height, jas_matrix_t *data) |
| 525 |
{ |
| 526 |
jas_image_cmpt_t *cmpt; |
| 527 |
uint_fast32_t i; |
| 528 |
uint_fast32_t j; |
| 529 |
jas_seqent_t *d; |
| 530 |
jas_seqent_t *dr; |
| 531 |
int drs; |
| 532 |
jas_seqent_t v; |
| 533 |
int k; |
| 534 |
int c; |
| 535 |
|
| 536 |
if (cmptno >= image->numcmpts_) { |
| 537 |
return -1; |
| 538 |
} |
| 539 |
|
| 540 |
cmpt = image->cmpts_[cmptno]; |
| 541 |
if (x >= cmpt->width_ || y >= cmpt->height_ || |
| 542 |
x + width > cmpt->width_ || |
| 543 |
y + height > cmpt->height_) { |
| 544 |
return -1; |
| 545 |
} |
| 546 |
|
| 547 |
if (jas_matrix_numrows(data) != height || jas_matrix_numcols(data) != width) { |
| 548 |
return -1; |
| 549 |
} |
| 550 |
|
| 551 |
dr = jas_matrix_getref(data, 0, 0); |
| 552 |
drs = jas_matrix_rowstep(data); |
| 553 |
for (i = 0; i < height; ++i, dr += drs) { |
| 554 |
d = dr; |
| 555 |
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * (y + i) + x) |
| 556 |
* cmpt->cps_, SEEK_SET) < 0) { |
| 557 |
return -1; |
| 558 |
} |
| 559 |
for (j = width; j > 0; --j, ++d) { |
| 560 |
v = inttobits(*d, cmpt->prec_, cmpt->sgnd_); |
| 561 |
for (k = cmpt->cps_; k > 0; --k) { |
| 562 |
c = (v >> (8 * (cmpt->cps_ - 1))) & 0xff; |
| 563 |
if (jas_stream_putc(cmpt->stream_, |
| 564 |
(unsigned char) c) == EOF) { |
| 565 |
return -1; |
| 566 |
} |
| 567 |
v <<= 8; |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
return 0; |
| 573 |
} |
| 574 |
|
| 575 |
/******************************************************************************\ |
| 576 |
* File format operations. |
| 577 |
\******************************************************************************/ |
| 578 |
|
| 579 |
void jas_image_clearfmts() |
| 580 |
{ |
| 581 |
int i; |
| 582 |
jas_image_fmtinfo_t *fmtinfo; |
| 583 |
for (i = 0; i < jas_image_numfmts; ++i) { |
| 584 |
fmtinfo = &jas_image_fmtinfos[i]; |
| 585 |
if (fmtinfo->name) { |
| 586 |
jas_free(fmtinfo->name); |
| 587 |
fmtinfo->name = 0; |
| 588 |
} |
| 589 |
if (fmtinfo->ext) { |
| 590 |
jas_free(fmtinfo->ext); |
| 591 |
fmtinfo->ext = 0; |
| 592 |
} |
| 593 |
if (fmtinfo->desc) { |
| 594 |
jas_free(fmtinfo->desc); |
| 595 |
fmtinfo->desc = 0; |
| 596 |
} |
| 597 |
} |
| 598 |
jas_image_numfmts = 0; |
| 599 |
} |
| 600 |
|
| 601 |
int jas_image_addfmt(int id, const char *name, const char *ext, |
| 602 |
const char *desc, |
| 603 |
jas_image_fmtops_t *ops) |
| 604 |
{ |
| 605 |
jas_image_fmtinfo_t *fmtinfo; |
| 606 |
assert(id >= 0 && name && ext && ops); |
| 607 |
if (jas_image_numfmts >= JAS_IMAGE_MAXFMTS) { |
| 608 |
return -1; |
| 609 |
} |
| 610 |
fmtinfo = &jas_image_fmtinfos[jas_image_numfmts]; |
| 611 |
fmtinfo->id = id; |
| 612 |
if (!(fmtinfo->name = jas_strdup(name))) { |
| 613 |
return -1; |
| 614 |
} |
| 615 |
if (!(fmtinfo->ext = jas_strdup(ext))) { |
| 616 |
jas_free(fmtinfo->name); |
| 617 |
return -1; |
| 618 |
} |
| 619 |
if (!(fmtinfo->desc = jas_strdup(desc))) { |
| 620 |
jas_free(fmtinfo->name); |
| 621 |
jas_free(fmtinfo->ext); |
| 622 |
return -1; |
| 623 |
} |
| 624 |
fmtinfo->ops = *ops; |
| 625 |
++jas_image_numfmts; |
| 626 |
return 0; |
| 627 |
} |
| 628 |
|
| 629 |
int jas_image_strtofmt(char *name) |
| 630 |
{ |
| 631 |
jas_image_fmtinfo_t *fmtinfo; |
| 632 |
if (!(fmtinfo = jas_image_lookupfmtbyname(name))) { |
| 633 |
return -1; |
| 634 |
} |
| 635 |
return fmtinfo->id; |
| 636 |
} |
| 637 |
|
| 638 |
char *jas_image_fmttostr(int fmt) |
| 639 |
{ |
| 640 |
jas_image_fmtinfo_t *fmtinfo; |
| 641 |
if (!(fmtinfo = jas_image_lookupfmtbyid(fmt))) { |
| 642 |
return 0; |
| 643 |
} |
| 644 |
return fmtinfo->name; |
| 645 |
} |
| 646 |
|
| 647 |
int jas_image_getfmt(jas_stream_t *in) |
| 648 |
{ |
| 649 |
jas_image_fmtinfo_t *fmtinfo; |
| 650 |
int found; |
| 651 |
int i; |
| 652 |
|
| 653 |
/* Check for data in each of the formats we know. */ |
| 654 |
found = 0; |
| 655 |
for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i, |
| 656 |
++fmtinfo) { |
| 657 |
if (fmtinfo->ops.validate) { |
| 658 |
/* Is the input data valid for this format? */ |
| 659 |
if (!(*fmtinfo->ops.validate)(in)) { |
| 660 |
found = 1; |
| 661 |
break; |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
return found ? fmtinfo->id : (-1); |
| 666 |
} |
| 667 |
|
| 668 |
int jas_image_fmtfromname(char *name) |
| 669 |
{ |
| 670 |
int i; |
| 671 |
char *ext; |
| 672 |
jas_image_fmtinfo_t *fmtinfo; |
| 673 |
/* Get the file name extension. */ |
| 674 |
if (!(ext = strrchr(name, '.'))) { |
| 675 |
return -1; |
| 676 |
} |
| 677 |
++ext; |
| 678 |
/* Try to find a format that uses this extension. */ |
| 679 |
for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i, |
| 680 |
++fmtinfo) { |
| 681 |
/* Do we have a match? */ |
| 682 |
if (!strcmp(ext, fmtinfo->ext)) { |
| 683 |
return fmtinfo->id; |
| 684 |
} |
| 685 |
} |
| 686 |
return -1; |
| 687 |
} |
| 688 |
|
| 689 |
/*****************************************************************************\ |
| 690 |
* Miscellaneous operations. |
| 691 |
\*****************************************************************************/ |
| 692 |
|
| 693 |
uint_fast32_t |
| 694 |
jas_image_rawsize(jas_image_t * const imageP) { |
| 695 |
/*---------------------------------------------------------------------------- |
| 696 |
The raw size of the image, i.e. the number of bytes the raster of the image |
| 697 |
would take if just represented simply, with no compression. |
| 698 |
-----------------------------------------------------------------------------*/ |
| 699 |
uint_fast32_t rawsize; |
| 700 |
uint_fast32_t cmptno; |
| 701 |
|
| 702 |
for (cmptno = 0, rawsize = 0; cmptno < imageP->numcmpts_; ++cmptno) { |
| 703 |
jas_image_cmpt_t * const cmptP = imageP->cmpts_[cmptno]; |
| 704 |
rawsize += (cmptP->width_ * cmptP->height_ * cmptP->prec_ + 7) / 8; |
| 705 |
} |
| 706 |
return rawsize; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
void jas_image_delcmpt(jas_image_t *image, uint_fast16_t cmptno) |
| 712 |
{ |
| 713 |
if (cmptno >= image->numcmpts_) { |
| 714 |
return; |
| 715 |
} |
| 716 |
jas_image_cmpt_destroy(image->cmpts_[cmptno]); |
| 717 |
if (cmptno < image->numcmpts_) { |
| 718 |
memmove(&image->cmpts_[cmptno], &image->cmpts_[cmptno + 1], |
| 719 |
(image->numcmpts_ - 1 - cmptno) * sizeof(jas_image_cmpt_t *)); |
| 720 |
} |
| 721 |
--image->numcmpts_; |
| 722 |
|
| 723 |
jas_image_setbbox(image); |
| 724 |
} |
| 725 |
|
| 726 |
int jas_image_addcmpt(jas_image_t *image, uint_fast16_t cmptno, |
| 727 |
jas_image_cmptparm_t *cmptparm) |
| 728 |
{ |
| 729 |
jas_image_cmpt_t *newcmpt; |
| 730 |
assert(cmptno <= image->numcmpts_); |
| 731 |
if (image->numcmpts_ >= image->maxcmpts_) { |
| 732 |
if (jas_image_growcmpts(image, image->maxcmpts_ + 128)) { |
| 733 |
return -1; |
| 734 |
} |
| 735 |
} |
| 736 |
if (!(newcmpt = jas_image_cmpt_create(cmptparm->tlx, |
| 737 |
cmptparm->tly, cmptparm->hstep, cmptparm->vstep, |
| 738 |
cmptparm->width, cmptparm->height, cmptparm->prec, |
| 739 |
cmptparm->sgnd, 1))) { |
| 740 |
return -1; |
| 741 |
} |
| 742 |
if (cmptno < image->numcmpts_) { |
| 743 |
memmove(&image->cmpts_[cmptno + 1], &image->cmpts_[cmptno], |
| 744 |
(image->numcmpts_ - cmptno) * sizeof(jas_image_cmpt_t *)); |
| 745 |
} |
| 746 |
image->cmpts_[cmptno] = newcmpt; |
| 747 |
++image->numcmpts_; |
| 748 |
|
| 749 |
jas_image_setbbox(image); |
| 750 |
|
| 751 |
return 0; |
| 752 |
} |
| 753 |
|
| 754 |
jas_image_fmtinfo_t *jas_image_lookupfmtbyid(int id) |
| 755 |
{ |
| 756 |
int i; |
| 757 |
jas_image_fmtinfo_t *fmtinfo; |
| 758 |
|
| 759 |
for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i, ++fmtinfo) { |
| 760 |
if (fmtinfo->id == id) { |
| 761 |
return fmtinfo; |
| 762 |
} |
| 763 |
} |
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
jas_image_fmtinfo_t *jas_image_lookupfmtbyname(const char *name) |
| 768 |
{ |
| 769 |
int i; |
| 770 |
jas_image_fmtinfo_t *fmtinfo; |
| 771 |
|
| 772 |
for (i = 0, fmtinfo = jas_image_fmtinfos; i < jas_image_numfmts; ++i, ++fmtinfo) { |
| 773 |
if (!strcmp(fmtinfo->name, name)) { |
| 774 |
return fmtinfo; |
| 775 |
} |
| 776 |
} |
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
static uint_fast32_t inttobits(jas_seqent_t v, int prec, bool sgnd) |
| 785 |
{ |
| 786 |
uint_fast32_t ret; |
| 787 |
ret = ((sgnd && v < 0) ? ((1 << prec) + v) : v) & JAS_ONES(prec); |
| 788 |
return ret; |
| 789 |
} |
| 790 |
|
| 791 |
static jas_seqent_t bitstoint(uint_fast32_t v, int prec, bool sgnd) |
| 792 |
{ |
| 793 |
jas_seqent_t ret; |
| 794 |
v &= JAS_ONES(prec); |
| 795 |
ret = (sgnd && (v & (1 << (prec - 1)))) ? (v - (1 << prec)) : v; |
| 796 |
return ret; |
| 797 |
} |
| 798 |
|
| 799 |
static void jas_image_setbbox(jas_image_t *image) |
| 800 |
{ |
| 801 |
jas_image_cmpt_t *cmpt; |
| 802 |
int cmptno; |
| 803 |
int_fast32_t x; |
| 804 |
int_fast32_t y; |
| 805 |
|
| 806 |
if (image->numcmpts_ > 0) { |
| 807 |
/* Determine the bounding box for all of the components on the |
| 808 |
reference grid (i.e., the image area) */ |
| 809 |
cmpt = image->cmpts_[0]; |
| 810 |
image->tlx_ = cmpt->tlx_; |
| 811 |
image->tly_ = cmpt->tly_; |
| 812 |
image->brx_ = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1) + 1; |
| 813 |
image->bry_ = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1) + 1; |
| 814 |
for (cmptno = 1; cmptno < image->numcmpts_; ++cmptno) { |
| 815 |
cmpt = image->cmpts_[cmptno]; |
| 816 |
if (image->tlx_ > cmpt->tlx_) { |
| 817 |
image->tlx_ = cmpt->tlx_; |
| 818 |
} |
| 819 |
if (image->tly_ > cmpt->tly_) { |
| 820 |
image->tly_ = cmpt->tly_; |
| 821 |
} |
| 822 |
x = cmpt->tlx_ + cmpt->hstep_ * (cmpt->width_ - 1) + 1; |
| 823 |
if (image->brx_ < x) { |
| 824 |
image->brx_ = x; |
| 825 |
} |
| 826 |
y = cmpt->tly_ + cmpt->vstep_ * (cmpt->height_ - 1) + 1; |
| 827 |
if (image->bry_ < y) { |
| 828 |
image->bry_ = y; |
| 829 |
} |
| 830 |
} |
| 831 |
} else { |
| 832 |
image->tlx_ = 0; |
| 833 |
image->tly_ = 0; |
| 834 |
image->brx_ = 0; |
| 835 |
image->bry_ = 0; |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
static int jas_image_growcmpts(jas_image_t *image, int maxcmpts) |
| 840 |
{ |
| 841 |
jas_image_cmpt_t **newcmpts; |
| 842 |
int cmptno; |
| 843 |
|
| 844 |
newcmpts = (!image->cmpts_) ? jas_malloc(maxcmpts * sizeof(jas_image_cmpt_t *)) : |
| 845 |
jas_realloc(image->cmpts_, maxcmpts * sizeof(jas_image_cmpt_t *)); |
| 846 |
if (!newcmpts) { |
| 847 |
return -1; |
| 848 |
} |
| 849 |
image->cmpts_ = newcmpts; |
| 850 |
image->maxcmpts_ = maxcmpts; |
| 851 |
for (cmptno = image->numcmpts_; cmptno < image->maxcmpts_; ++cmptno) { |
| 852 |
image->cmpts_[cmptno] = 0; |
| 853 |
} |
| 854 |
return 0; |
| 855 |
} |
| 856 |
|
| 857 |
int jas_image_copycmpt(jas_image_t *dstimage, int dstcmptno, jas_image_t *srcimage, |
| 858 |
int srccmptno) |
| 859 |
{ |
| 860 |
jas_image_cmpt_t *newcmpt; |
| 861 |
if (dstimage->numcmpts_ >= dstimage->maxcmpts_) { |
| 862 |
if (jas_image_growcmpts(dstimage, dstimage->maxcmpts_ + 128)) { |
| 863 |
return -1; |
| 864 |
} |
| 865 |
} |
| 866 |
if (!(newcmpt = jas_image_cmpt_copy(srcimage->cmpts_[srccmptno]))) { |
| 867 |
return -1; |
| 868 |
} |
| 869 |
if (dstcmptno < dstimage->numcmpts_) { |
| 870 |
memmove(&dstimage->cmpts_[dstcmptno + 1], &dstimage->cmpts_[dstcmptno], |
| 871 |
(dstimage->numcmpts_ - dstcmptno) * sizeof(jas_image_cmpt_t *)); |
| 872 |
} |
| 873 |
dstimage->cmpts_[dstcmptno] = newcmpt; |
| 874 |
++dstimage->numcmpts_; |
| 875 |
|
| 876 |
jas_image_setbbox(dstimage); |
| 877 |
return 0; |
| 878 |
} |
| 879 |
|
| 880 |
void jas_image_dump(jas_image_t *image, FILE *out) |
| 881 |
{ |
| 882 |
int cmptno; |
| 883 |
jas_seq2d_t *data; |
| 884 |
jas_image_cmpt_t *cmpt; |
| 885 |
if (!(data = jas_seq2d_create(0, 0, 1, 1))) { |
| 886 |
abort(); |
| 887 |
} |
| 888 |
for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) { |
| 889 |
cmpt = image->cmpts_[cmptno]; |
| 890 |
fprintf(out, "prec=%d sgnd=%d\n", (int)cmpt->prec_, cmpt->sgnd_); |
| 891 |
if (jas_image_readcmpt(image, cmptno, 0, 0, 1, 1, data)) { |
| 892 |
abort(); |
| 893 |
} |
| 894 |
fprintf(out, "tlsample %ld\n", (long) jas_seq2d_get(data, 0, 0)); |
| 895 |
} |
| 896 |
jas_seq2d_destroy(data); |
| 897 |
} |
| 898 |
|
| 899 |
int jas_image_depalettize(jas_image_t *image, int cmptno, int numlutents, |
| 900 |
int_fast32_t *lutents, int dtype, int newcmptno) |
| 901 |
{ |
| 902 |
jas_image_cmptparm_t cmptparms; |
| 903 |
int_fast32_t v; |
| 904 |
int i; |
| 905 |
int j; |
| 906 |
jas_image_cmpt_t *cmpt; |
| 907 |
|
| 908 |
cmpt = image->cmpts_[cmptno]; |
| 909 |
cmptparms.tlx = cmpt->tlx_; |
| 910 |
cmptparms.tly = cmpt->tly_; |
| 911 |
cmptparms.hstep = cmpt->hstep_; |
| 912 |
cmptparms.vstep = cmpt->vstep_; |
| 913 |
cmptparms.width = cmpt->width_; |
| 914 |
cmptparms.height = cmpt->height_; |
| 915 |
cmptparms.prec = JAS_IMAGE_CDT_GETPREC(dtype); |
| 916 |
cmptparms.sgnd = JAS_IMAGE_CDT_GETSGND(dtype); |
| 917 |
|
| 918 |
if (numlutents < 1) { |
| 919 |
return -1; |
| 920 |
} |
| 921 |
|
| 922 |
if (jas_image_addcmpt(image, newcmptno, &cmptparms)) { |
| 923 |
return -1; |
| 924 |
} |
| 925 |
if (newcmptno <= cmptno) { |
| 926 |
++cmptno; |
| 927 |
cmpt = image->cmpts_[cmptno]; |
| 928 |
} |
| 929 |
|
| 930 |
for (j = 0; j < cmpt->height_; ++j) { |
| 931 |
for (i = 0; i < cmpt->width_; ++i) { |
| 932 |
v = jas_image_readcmptsample(image, cmptno, i, j); |
| 933 |
if (v < 0) { |
| 934 |
v = 0; |
| 935 |
} else if (v >= numlutents) { |
| 936 |
v = numlutents - 1; |
| 937 |
} |
| 938 |
jas_image_writecmptsample(image, newcmptno, i, j, |
| 939 |
lutents[v]); |
| 940 |
} |
| 941 |
} |
| 942 |
return 0; |
| 943 |
} |
| 944 |
|
| 945 |
int jas_image_readcmptsample(jas_image_t *image, int cmptno, int x, int y) |
| 946 |
{ |
| 947 |
jas_image_cmpt_t *cmpt; |
| 948 |
uint_fast32_t v; |
| 949 |
int k; |
| 950 |
int c; |
| 951 |
|
| 952 |
cmpt = image->cmpts_[cmptno]; |
| 953 |
|
| 954 |
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * y + x) * cmpt->cps_, |
| 955 |
SEEK_SET) < 0) { |
| 956 |
return -1; |
| 957 |
} |
| 958 |
v = 0; |
| 959 |
for (k = cmpt->cps_; k > 0; --k) { |
| 960 |
if ((c = jas_stream_getc(cmpt->stream_)) == EOF) { |
| 961 |
return -1; |
| 962 |
} |
| 963 |
v = (v << 8) | (c & 0xff); |
| 964 |
} |
| 965 |
return bitstoint(v, cmpt->prec_, cmpt->sgnd_); |
| 966 |
} |
| 967 |
|
| 968 |
void jas_image_writecmptsample(jas_image_t *image, int cmptno, int x, int y, |
| 969 |
int_fast32_t v) |
| 970 |
{ |
| 971 |
jas_image_cmpt_t *cmpt; |
| 972 |
uint_fast32_t t; |
| 973 |
int k; |
| 974 |
int c; |
| 975 |
|
| 976 |
cmpt = image->cmpts_[cmptno]; |
| 977 |
|
| 978 |
if (jas_stream_seek(cmpt->stream_, (cmpt->width_ * y + x) * cmpt->cps_, |
| 979 |
SEEK_SET) < 0) { |
| 980 |
return; |
| 981 |
} |
| 982 |
t = inttobits(v, cmpt->prec_, cmpt->sgnd_); |
| 983 |
for (k = cmpt->cps_; k > 0; --k) { |
| 984 |
c = (t >> (8 * (cmpt->cps_ - 1))) & 0xff; |
| 985 |
if (jas_stream_putc(cmpt->stream_, (unsigned char) c) == EOF) { |
| 986 |
return; |
| 987 |
} |
| 988 |
t <<= 8; |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
int jas_image_getcmptbytype(jas_image_t *image, int ctype) |
| 993 |
{ |
| 994 |
int cmptno; |
| 995 |
|
| 996 |
for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) { |
| 997 |
if (image->cmpts_[cmptno]->type_ == ctype) { |
| 998 |
return cmptno; |
| 999 |
} |
| 1000 |
} |
| 1001 |
return -1; |
| 1002 |
} |