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 * $Id: jpc_dec.c 14449 2005-10-20 12:15:56Z stippi $ 115 */ 116 117/******************************************************************************\ 118* Includes. 119\******************************************************************************/ 120 121#include <stdio.h> 122#include <stdlib.h> 123#include <assert.h> 124 125#include "jasper/jas_types.h" 126#include "jasper/jas_math.h" 127#include "jasper/jas_tvp.h" 128#include "jasper/jas_malloc.h" 129#include "jasper/jas_debug.h" 130 131#include "jpc_fix.h" 132#include "jpc_dec.h" 133#include "jpc_cs.h" 134#include "jpc_mct.h" 135#include "jpc_t2dec.h" 136#include "jpc_t1dec.h" 137#include "jpc_math.h" 138 139/******************************************************************************\ 140* 141\******************************************************************************/ 142 143#define JPC_MHSOC 0x0001 144 /* In the main header, expecting a SOC marker segment. */ 145#define JPC_MHSIZ 0x0002 146 /* In the main header, expecting a SIZ marker segment. */ 147#define JPC_MH 0x0004 148 /* In the main header, expecting "other" marker segments. */ 149#define JPC_TPHSOT 0x0008 150 /* In a tile-part header, expecting a SOT marker segment. */ 151#define JPC_TPH 0x0010 152 /* In a tile-part header, expecting "other" marker segments. */ 153#define JPC_MT 0x0020 154 /* In the main trailer. */ 155 156typedef struct { 157 158 uint_fast16_t id; 159 /* The marker segment type. */ 160 161 int validstates; 162 /* The states in which this type of marker segment can be 163 validly encountered. */ 164 165 int (*action)(jpc_dec_t *dec, jpc_ms_t *ms); 166 /* The action to take upon encountering this type of marker segment. */ 167 168} jpc_dec_mstabent_t; 169 170/******************************************************************************\ 171* 172\******************************************************************************/ 173 174/* COD/COC parameters have been specified. */ 175#define JPC_CSET 0x0001 176/* QCD/QCC parameters have been specified. */ 177#define JPC_QSET 0x0002 178/* COD/COC parameters set from a COC marker segment. */ 179#define JPC_COC 0x0004 180/* QCD/QCC parameters set from a QCC marker segment. */ 181#define JPC_QCC 0x0008 182 183/******************************************************************************\ 184* Local function prototypes. 185\******************************************************************************/ 186 187static int jpc_dec_dump(jpc_dec_t *dec, FILE *out); 188 189jpc_ppxstab_t *jpc_ppxstab_create(); 190void jpc_ppxstab_destroy(jpc_ppxstab_t *tab); 191int jpc_ppxstab_grow(jpc_ppxstab_t *tab, int maxents); 192int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent); 193jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab); 194int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab); 195jpc_ppxstabent_t *jpc_ppxstabent_create(); 196void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent); 197 198int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist); 199jpc_streamlist_t *jpc_streamlist_create(); 200int jpc_streamlist_insert(jpc_streamlist_t *streamlist, int streamno, 201 jas_stream_t *stream); 202jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, int streamno); 203void jpc_streamlist_destroy(jpc_streamlist_t *streamlist); 204jas_stream_t *jpc_streamlist_get(jpc_streamlist_t *streamlist, int streamno); 205 206static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp); 207static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps); 208static int jpc_dec_cp_isvalid(jpc_dec_cp_t *cp); 209static jpc_dec_cp_t *jpc_dec_cp_copy(jpc_dec_cp_t *cp); 210static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, jpc_cod_t *cod); 211static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, jpc_coc_t *coc); 212static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, 213 jpc_coxcp_t *compparms, int flags); 214static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, jpc_qcd_t *qcd); 215static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, jpc_qcc_t *qcc); 216static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, 217 jpc_qcxcp_t *compparms, int flags); 218static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, jpc_rgn_t *rgn); 219static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp); 220static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp); 221static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, jpc_poc_t *poc, int reset); 222static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, jpc_poc_t *poc); 223 224static int jpc_dec_decode(jpc_dec_t *dec); 225static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in); 226static void jpc_dec_destroy(jpc_dec_t *dec); 227static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize); 228static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps); 229static jpc_fix_t jpc_calcabsstepsize(int stepsize, int numbits); 230static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile); 231static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile); 232static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile); 233static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms); 234static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms); 235static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms); 236static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms); 237static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms); 238static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms); 239static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms); 240static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms); 241static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms); 242static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms); 243static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms); 244static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms); 245static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms); 246static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms); 247static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms); 248static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms); 249static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts); 250 251/******************************************************************************\ 252* Global data. 253\******************************************************************************/ 254 255jpc_dec_mstabent_t jpc_dec_mstab[] = { 256 {JPC_MS_SOC, JPC_MHSOC, jpc_dec_process_soc}, 257 {JPC_MS_SOT, JPC_MH | JPC_TPHSOT, jpc_dec_process_sot}, 258 {JPC_MS_SOD, JPC_TPH, jpc_dec_process_sod}, 259 {JPC_MS_EOC, JPC_TPHSOT, jpc_dec_process_eoc}, 260 {JPC_MS_SIZ, JPC_MHSIZ, jpc_dec_process_siz}, 261 {JPC_MS_COD, JPC_MH | JPC_TPH, jpc_dec_process_cod}, 262 {JPC_MS_COC, JPC_MH | JPC_TPH, jpc_dec_process_coc}, 263 {JPC_MS_RGN, JPC_MH | JPC_TPH, jpc_dec_process_rgn}, 264 {JPC_MS_QCD, JPC_MH | JPC_TPH, jpc_dec_process_qcd}, 265 {JPC_MS_QCC, JPC_MH | JPC_TPH, jpc_dec_process_qcc}, 266 {JPC_MS_POC, JPC_MH | JPC_TPH, jpc_dec_process_poc}, 267 {JPC_MS_TLM, JPC_MH, 0}, 268 {JPC_MS_PLM, JPC_MH, 0}, 269 {JPC_MS_PLT, JPC_TPH, 0}, 270 {JPC_MS_PPM, JPC_MH, jpc_dec_process_ppm}, 271 {JPC_MS_PPT, JPC_TPH, jpc_dec_process_ppt}, 272 {JPC_MS_SOP, 0, 0}, 273 {JPC_MS_CRG, JPC_MH, jpc_dec_process_crg}, 274 {JPC_MS_COM, JPC_MH | JPC_TPH, jpc_dec_process_com}, 275 {0, JPC_MH | JPC_TPH, jpc_dec_process_unk} 276}; 277 278/******************************************************************************\ 279* The main entry point for the JPEG-2000 decoder. 280\******************************************************************************/ 281 282jas_image_t *jpc_decode(jas_stream_t *in, char *optstr) 283{ 284 jpc_dec_importopts_t opts; 285 jpc_dec_t *dec; 286 jas_image_t *image; 287 288 dec = 0; 289 290 if (jpc_dec_parseopts(optstr, &opts)) { 291 goto error; 292 } 293 294 jpc_initluts(); 295 296 if (!(dec = jpc_dec_create(&opts, in))) { 297 goto error; 298 } 299 300 /* Do most of the work. */ 301 if (jpc_dec_decode(dec)) { 302 goto error; 303 } 304 305 if (jas_image_numcmpts(dec->image) >= 3) { 306 jas_image_setcolorspace(dec->image, JAS_IMAGE_CS_RGB); 307 jas_image_setcmpttype(dec->image, 0, 308 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_R)); 309 jas_image_setcmpttype(dec->image, 1, 310 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_G)); 311 jas_image_setcmpttype(dec->image, 2, 312 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_RGB_B)); 313 } else { 314 jas_image_setcolorspace(dec->image, JAS_IMAGE_CS_GRAY); 315 jas_image_setcmpttype(dec->image, 0, 316 JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_GRAY_Y)); 317 } 318 319 /* Save the return value. */ 320 image = dec->image; 321 322 /* Stop the image from being discarded. */ 323 dec->image = 0; 324 325 /* Destroy decoder. */ 326 jpc_dec_destroy(dec); 327 328 return image; 329 330error: 331 if (dec) { 332 jpc_dec_destroy(dec); 333 } 334 return 0; 335} 336 337typedef enum { 338 OPT_MAXLYRS, 339 OPT_MAXPKTS, 340 OPT_DEBUG 341} optid_t; 342 343jas_taginfo_t decopts[] = { 344 {OPT_MAXLYRS, "maxlyrs"}, 345 {OPT_MAXPKTS, "maxpkts"}, 346 {OPT_DEBUG, "debug"}, 347 {-1, 0} 348}; 349 350static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts) 351{ 352 jas_tvparser_t *tvp; 353 354 opts->debug = 0; 355 opts->maxlyrs = JPC_MAXLYRS; 356 opts->maxpkts = -1; 357 358 if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { 359 return -1; 360 } 361 362 while (!jas_tvparser_next(tvp)) { 363 switch (jas_taginfo_nonull(jas_taginfos_lookup(decopts, 364 jas_tvparser_gettag(tvp)))->id) { 365 case OPT_MAXLYRS: 366 opts->maxlyrs = atoi(jas_tvparser_getval(tvp)); 367 break; 368 case OPT_DEBUG: 369 opts->debug = atoi(jas_tvparser_getval(tvp)); 370 break; 371 case OPT_MAXPKTS: 372 opts->maxpkts = atoi(jas_tvparser_getval(tvp)); 373 break; 374 default: 375 fprintf(stderr, "warning: ignoring invalid option %s\n", 376 jas_tvparser_gettag(tvp)); 377 break; 378 } 379 } 380 381 jas_tvparser_destroy(tvp); 382 383 return 0; 384} 385 386/******************************************************************************\ 387* Code for table-driven code stream decoder. 388\******************************************************************************/ 389 390jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id) 391{ 392 jpc_dec_mstabent_t *mstabent; 393 for (mstabent = jpc_dec_mstab; mstabent->id != 0; ++mstabent) { 394 if (mstabent->id == id) { 395 break; 396 } 397 } 398 return mstabent; 399} 400 401static int jpc_dec_decode(jpc_dec_t *dec) 402{ 403 jpc_ms_t *ms; 404 jpc_dec_mstabent_t *mstabent; 405 int ret; 406 jpc_cstate_t *cstate; 407 408 if (!(cstate = jpc_cstate_create())) { 409 return -1; 410 } 411 dec->cstate = cstate; 412 413 /* Initially, we should expect to encounter a SOC marker segment. */ 414 dec->state = JPC_MHSOC; 415 416 for (;;) { 417 418 /* Get the next marker segment in the code stream. */ 419 if (!(ms = jpc_getms(dec->in, cstate))) { 420 fprintf(stderr, "cannot get marker segment\n"); 421 return -1; 422 } 423 424 mstabent = jpc_dec_mstab_lookup(ms->id); 425 assert(mstabent); 426 427 /* Ensure that this type of marker segment is permitted 428 at this point in the code stream. */ 429 if (!(dec->state & mstabent->validstates)) { 430 fprintf(stderr, "unexpected marker segment type\n"); 431 jpc_ms_destroy(ms); 432 return -1; 433 } 434 435 /* Process the marker segment. */ 436 if (mstabent->action) { 437 ret = (*mstabent->action)(dec, ms); 438 } else { 439 /* No explicit action is required. */ 440 ret = 0; 441 } 442 443 /* Destroy the marker segment. */ 444 jpc_ms_destroy(ms); 445 446 if (ret < 0) { 447 return -1; 448 } else if (ret > 0) { 449 break; 450 } 451 452 } 453 454 return 0; 455} 456 457static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms) 458{ 459 uint_fast16_t cmptno; 460 jpc_dec_cmpt_t *cmpt; 461 jpc_crg_t *crg; 462 463 crg = &ms->parms.crg; 464 for (cmptno = 0, cmpt = dec->cmpts; cmptno < dec->numcomps; ++cmptno, 465 ++cmpt) { 466 /* Ignore the information in the CRG marker segment for now. 467 This information serves no useful purpose for decoding anyhow. 468 Some other parts of the code need to be changed if these lines 469 are uncommented. 470 cmpt->hsubstep = crg->comps[cmptno].hoff; 471 cmpt->vsubstep = crg->comps[cmptno].voff; 472 */ 473 } 474 return 0; 475} 476 477static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms) 478{ 479 /* We should expect to encounter a SIZ marker segment next. */ 480 dec->state = JPC_MHSIZ; 481 482 return 0; 483} 484 485static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms) 486{ 487 jpc_dec_tile_t *tile; 488 jpc_sot_t *sot = &ms->parms.sot; 489 jas_image_cmptparm_t *compinfos; 490 jas_image_cmptparm_t *compinfo; 491 jpc_dec_cmpt_t *cmpt; 492 uint_fast16_t cmptno; 493 494 if (dec->state == JPC_MH) { 495 496 compinfos = jas_malloc(dec->numcomps * sizeof(jas_image_cmptparm_t)); 497 assert(compinfos); 498 for (cmptno = 0, cmpt = dec->cmpts, compinfo = compinfos; 499 cmptno < dec->numcomps; ++cmptno, ++cmpt, ++compinfo) { 500 compinfo->tlx = 0; 501 compinfo->tly = 0; 502 compinfo->prec = cmpt->prec; 503 compinfo->sgnd = cmpt->sgnd; 504 compinfo->width = cmpt->width; 505 compinfo->height = cmpt->height; 506 compinfo->hstep = cmpt->hstep; 507 compinfo->vstep = cmpt->vstep; 508 } 509 510 if (!(dec->image = jas_image_create(dec->numcomps, compinfos, 511 JAS_IMAGE_CS_UNKNOWN))) { 512 return -1; 513 } 514 jas_free(compinfos); 515 516 /* Is the packet header information stored in PPM marker segments in 517 the main header? */ 518 if (dec->ppmstab) { 519 /* Convert the PPM marker segment data into a collection of streams 520 (one stream per tile-part). */ 521 if (!(dec->pkthdrstreams = jpc_ppmstabtostreams(dec->ppmstab))) { 522 abort(); 523 } 524 jpc_ppxstab_destroy(dec->ppmstab); 525 dec->ppmstab = 0; 526 } 527 } 528 529 if (sot->len > 0) { 530 dec->curtileendoff = jas_stream_getrwcount(dec->in) - ms->len - 531 4 + sot->len; 532 } else { 533 dec->curtileendoff = 0; 534 } 535 536 if (sot->tileno > dec->numtiles) { 537 fprintf(stderr, "invalid tile number in SOT marker segment\n"); 538 return -1; 539 } 540 /* Set the current tile. */ 541 dec->curtile = &dec->tiles[sot->tileno]; 542 tile = dec->curtile; 543 /* Ensure that this is the expected part number. */ 544 if (sot->partno != tile->partno) { 545 return -1; 546 } 547 if (tile->numparts > 0 && sot->partno >= tile->numparts) { 548 return -1; 549 } 550 if (!tile->numparts && sot->numparts > 0) { 551 tile->numparts = sot->numparts; 552 } 553 554 tile->pptstab = 0; 555 556 switch (tile->state) { 557 case JPC_TILE_INIT: 558 /* This is the first tile-part for this tile. */ 559 tile->state = JPC_TILE_ACTIVE; 560 assert(!tile->cp); 561 if (!(tile->cp = jpc_dec_cp_copy(dec->cp))) { 562 return -1; 563 } 564 jpc_dec_cp_resetflags(dec->cp); 565 break; 566 default: 567 if (sot->numparts == sot->partno - 1) { 568 tile->state = JPC_TILE_ACTIVELAST; 569 } 570 break; 571 } 572 573 /* Note: We do not increment the expected tile-part number until 574 all processing for this tile-part is complete. */ 575 576 /* We should expect to encounter other tile-part header marker 577 segments next. */ 578 dec->state = JPC_TPH; 579 580 return 0; 581} 582 583static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms) 584{ 585 jpc_dec_tile_t *tile; 586 int pos; 587 588 if (!(tile = dec->curtile)) { 589 return -1; 590 } 591 592 if (!tile->partno) { 593 if (!jpc_dec_cp_isvalid(tile->cp)) { 594 return -1; 595 } 596 jpc_dec_cp_prepare(tile->cp); 597 if (jpc_dec_tileinit(dec, tile)) { 598 return -1; 599 } 600 } 601 602 /* Are packet headers stored in the main header or tile-part header? */ 603 if (dec->pkthdrstreams) { 604 /* Get the stream containing the packet header data for this 605 tile-part. */ 606 if (!(tile->pkthdrstream = jpc_streamlist_remove(dec->pkthdrstreams, 0))) { 607 return -1; 608 } 609 } 610 611 if (tile->pptstab) { 612 if (!tile->pkthdrstream) { 613 if (!(tile->pkthdrstream = jas_stream_memopen(0, 0))) { 614 return -1; 615 } 616 } 617 pos = jas_stream_tell(tile->pkthdrstream); 618 jas_stream_seek(tile->pkthdrstream, 0, SEEK_END); 619 if (jpc_pptstabwrite(tile->pkthdrstream, tile->pptstab)) { 620 return -1; 621 } 622 jas_stream_seek(tile->pkthdrstream, pos, SEEK_SET); 623 jpc_ppxstab_destroy(tile->pptstab); 624 tile->pptstab = 0; 625 } 626 627 if (jas_getdbglevel() >= 10) { 628 jpc_dec_dump(dec, stderr); 629 } 630 631 if (jpc_dec_decodepkts(dec, (tile->pkthdrstream) ? tile->pkthdrstream : 632 dec->in, dec->in)) { 633 fprintf(stderr, "jpc_dec_decodepkts failed\n"); 634 return -1; 635 } 636 637 /* Gobble any unconsumed tile data. */ 638 if (dec->curtileendoff > 0) { 639 uint_fast32_t curoff; 640 uint_fast32_t n; 641 curoff = jas_stream_getrwcount(dec->in); 642 if (curoff < dec->curtileendoff) { 643 n = dec->curtileendoff - curoff; 644 fprintf(stderr, 645 "warning: ignoring trailing garbage (%lu bytes)\n", 646 (unsigned long) n); 647 648 while (n-- > 0) { 649 if (jas_stream_getc(dec->in) == EOF) { 650 fprintf(stderr, "read error\n"); 651 return -1; 652 } 653 } 654 } else if (curoff > dec->curtileendoff) { 655 fprintf(stderr, 656 "warning: not enough tile data (%lu bytes)\n", 657 (unsigned long) curoff - dec->curtileendoff); 658 } 659 660 } 661 662 if (tile->numparts > 0 && tile->partno == tile->numparts - 1) { 663 if (jpc_dec_tiledecode(dec, tile)) { 664 return -1; 665 } 666 jpc_dec_tilefini(dec, tile); 667 } 668 669 dec->curtile = 0; 670 671 /* Increment the expected tile-part number. */ 672 ++tile->partno; 673 674 /* We should expect to encounter a SOT marker segment next. */ 675 dec->state = JPC_TPHSOT; 676 677 return 0; 678} 679 680static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile) 681{ 682 jpc_dec_tcomp_t *tcomp; 683 uint_fast16_t compno; 684 int rlvlno; 685 jpc_dec_rlvl_t *rlvl; 686 jpc_dec_band_t *band; 687 jpc_dec_prc_t *prc; 688 int bndno; 689 jpc_tsfb_band_t *bnd; 690 int bandno; 691 jpc_dec_ccp_t *ccp; 692 int prccnt; 693 jpc_dec_cblk_t *cblk; 694 int cblkcnt; 695 uint_fast32_t tlprcxstart; 696 uint_fast32_t tlprcystart; 697 uint_fast32_t brprcxend; 698 uint_fast32_t brprcyend; 699 uint_fast32_t tlcbgxstart; 700 uint_fast32_t tlcbgystart; 701 uint_fast32_t brcbgxend; 702 uint_fast32_t brcbgyend; 703 uint_fast32_t cbgxstart; 704 uint_fast32_t cbgystart; 705 uint_fast32_t cbgxend; 706 uint_fast32_t cbgyend; 707 uint_fast32_t tlcblkxstart; 708 uint_fast32_t tlcblkystart; 709 uint_fast32_t brcblkxend; 710 uint_fast32_t brcblkyend; 711 uint_fast32_t cblkxstart; 712 uint_fast32_t cblkystart; 713 uint_fast32_t cblkxend; 714 uint_fast32_t cblkyend; 715 uint_fast32_t tmpxstart; 716 uint_fast32_t tmpystart; 717 uint_fast32_t tmpxend; 718 uint_fast32_t tmpyend; 719 jpc_dec_cp_t *cp; 720 jpc_tsfb_band_t bnds[64]; 721 jpc_pchg_t *pchg; 722 int pchgno; 723 jpc_dec_cmpt_t *cmpt; 724 725 cp = tile->cp; 726 tile->realmode = 0; 727 if (cp->mctid == JPC_MCT_ICT) { 728 tile->realmode = 1; 729 } 730 731 for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < 732 dec->numcomps; ++compno, ++tcomp, ++cmpt) { 733 ccp = &tile->cp->ccps[compno]; 734 if (ccp->qmfbid == JPC_COX_INS) { 735 tile->realmode = 1; 736 } 737 tcomp->numrlvls = ccp->numrlvls; 738 if (!(tcomp->rlvls = jas_malloc(tcomp->numrlvls * 739 sizeof(jpc_dec_rlvl_t)))) { 740 return -1; 741 } 742 if (!(tcomp->data = jas_seq2d_create(JPC_CEILDIV(tile->xstart, 743 cmpt->hstep), JPC_CEILDIV(tile->ystart, cmpt->vstep), 744 JPC_CEILDIV(tile->xend, cmpt->hstep), JPC_CEILDIV(tile->yend, 745 cmpt->vstep)))) { 746 return -1; 747 } 748 if (!(tcomp->tsfb = jpc_cod_gettsfb(ccp->qmfbid, 749 tcomp->numrlvls - 1))) { 750 return -1; 751 } 752{ 753 jpc_tsfb_getbands(tcomp->tsfb, jas_seq2d_xstart(tcomp->data), jas_seq2d_ystart(tcomp->data), jas_seq2d_xend(tcomp->data), jas_seq2d_yend(tcomp->data), bnds); 754} 755 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; 756 ++rlvlno, ++rlvl) { 757rlvl->bands = 0; 758 rlvl->xstart = JPC_CEILDIVPOW2(tcomp->xstart, 759 tcomp->numrlvls - 1 - rlvlno); 760 rlvl->ystart = JPC_CEILDIVPOW2(tcomp->ystart, 761 tcomp->numrlvls - 1 - rlvlno); 762 rlvl->xend = JPC_CEILDIVPOW2(tcomp->xend, 763 tcomp->numrlvls - 1 - rlvlno); 764 rlvl->yend = JPC_CEILDIVPOW2(tcomp->yend, 765 tcomp->numrlvls - 1 - rlvlno); 766 rlvl->prcwidthexpn = ccp->prcwidthexpns[rlvlno]; 767 rlvl->prcheightexpn = ccp->prcheightexpns[rlvlno]; 768 tlprcxstart = JPC_FLOORDIVPOW2(rlvl->xstart, 769 rlvl->prcwidthexpn) << rlvl->prcwidthexpn; 770 tlprcystart = JPC_FLOORDIVPOW2(rlvl->ystart, 771 rlvl->prcheightexpn) << rlvl->prcheightexpn; 772 brprcxend = JPC_CEILDIVPOW2(rlvl->xend, 773 rlvl->prcwidthexpn) << rlvl->prcwidthexpn; 774 brprcyend = JPC_CEILDIVPOW2(rlvl->yend, 775 rlvl->prcheightexpn) << rlvl->prcheightexpn; 776 rlvl->numhprcs = (brprcxend - tlprcxstart) >> 777 rlvl->prcwidthexpn; 778 rlvl->numvprcs = (brprcyend - tlprcystart) >> 779 rlvl->prcheightexpn; 780 rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; 781 782 if (rlvl->xstart >= rlvl->xend || rlvl->ystart >= rlvl->yend) { 783 rlvl->bands = 0; 784 rlvl->numprcs = 0; 785 rlvl->numhprcs = 0; 786 rlvl->numvprcs = 0; 787 continue; 788 } 789 if (!rlvlno) { 790 tlcbgxstart = tlprcxstart; 791 tlcbgystart = tlprcystart; 792 brcbgxend = brprcxend; 793 brcbgyend = brprcyend; 794 rlvl->cbgwidthexpn = rlvl->prcwidthexpn; 795 rlvl->cbgheightexpn = rlvl->prcheightexpn; 796 } else { 797 tlcbgxstart = JPC_CEILDIVPOW2(tlprcxstart, 1); 798 tlcbgystart = JPC_CEILDIVPOW2(tlprcystart, 1); 799 brcbgxend = JPC_CEILDIVPOW2(brprcxend, 1); 800 brcbgyend = JPC_CEILDIVPOW2(brprcyend, 1); 801 rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; 802 rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; 803 } 804 rlvl->cblkwidthexpn = JAS_MIN(ccp->cblkwidthexpn, 805 rlvl->cbgwidthexpn); 806 rlvl->cblkheightexpn = JAS_MIN(ccp->cblkheightexpn, 807 rlvl->cbgheightexpn); 808 809 rlvl->numbands = (!rlvlno) ? 1 : 3; 810 if (!(rlvl->bands = jas_malloc(rlvl->numbands * 811 sizeof(jpc_dec_band_t)))) { 812 return -1; 813 } 814 for (bandno = 0, band = rlvl->bands; 815 bandno < rlvl->numbands; ++bandno, ++band) { 816 bndno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + 817 bandno + 1); 818 bnd = &bnds[bndno]; 819 820 band->orient = bnd->orient; 821 band->stepsize = ccp->stepsizes[bndno]; 822 band->analgain = JPC_NOMINALGAIN(ccp->qmfbid, 823 tcomp->numrlvls - 1, rlvlno, band->orient); 824 band->absstepsize = jpc_calcabsstepsize(band->stepsize, 825 cmpt->prec + band->analgain); 826 band->numbps = ccp->numguardbits + 827 JPC_QCX_GETEXPN(band->stepsize) - 1; 828 band->roishift = (ccp->roishift + band->numbps >= JPC_PREC) ? 829 (JPC_PREC - 1 - band->numbps) : ccp->roishift; 830 band->data = 0; 831 band->prcs = 0; 832 if (bnd->xstart == bnd->xend || bnd->ystart == bnd->yend) { 833 continue; 834 } 835 if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) { 836 return -1; 837 } 838 jas_seq2d_bindsub(band->data, tcomp->data, bnd->locxstart, bnd->locystart, bnd->locxend, bnd->locyend); 839 jas_seq2d_setshift(band->data, bnd->xstart, bnd->ystart); 840 841 assert(rlvl->numprcs); 842 843 if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_dec_prc_t)))) { 844 return -1; 845 } 846 847/************************************************/ 848 cbgxstart = tlcbgxstart; 849 cbgystart = tlcbgystart; 850 for (prccnt = rlvl->numprcs, prc = band->prcs; 851 prccnt > 0; --prccnt, ++prc) { 852 cbgxend = cbgxstart + (1 << rlvl->cbgwidthexpn); 853 cbgyend = cbgystart + (1 << rlvl->cbgheightexpn); 854 prc->xstart = JAS_MAX(cbgxstart, jas_seq2d_xstart(band->data)); 855 prc->ystart = JAS_MAX(cbgystart, jas_seq2d_ystart(band->data)); 856 prc->xend = JAS_MIN(cbgxend, jas_seq2d_xend(band->data)); 857 prc->yend = JAS_MIN(cbgyend, jas_seq2d_yend(band->data)); 858 if (prc->xend > prc->xstart && prc->yend > prc->ystart) { 859 tlcblkxstart = JPC_FLOORDIVPOW2(prc->xstart, 860 rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn; 861 tlcblkystart = JPC_FLOORDIVPOW2(prc->ystart, 862 rlvl->cblkheightexpn) << rlvl->cblkheightexpn; 863 brcblkxend = JPC_CEILDIVPOW2(prc->xend, 864 rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn; 865 brcblkyend = JPC_CEILDIVPOW2(prc->yend, 866 rlvl->cblkheightexpn) << rlvl->cblkheightexpn; 867 prc->numhcblks = (brcblkxend - tlcblkxstart) >> 868 rlvl->cblkwidthexpn; 869 prc->numvcblks = (brcblkyend - tlcblkystart) >> 870 rlvl->cblkheightexpn; 871 prc->numcblks = prc->numhcblks * prc->numvcblks; 872 assert(prc->numcblks > 0); 873 874 if (!(prc->incltagtree = jpc_tagtree_create(prc->numhcblks, prc->numvcblks))) { 875 return -1; 876 } 877 if (!(prc->numimsbstagtree = jpc_tagtree_create(prc->numhcblks, prc->numvcblks))) { 878 return -1; 879 } 880 if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_dec_cblk_t)))) { 881 return -1; 882 } 883 884 cblkxstart = cbgxstart; 885 cblkystart = cbgystart; 886 for (cblkcnt = prc->numcblks, cblk = prc->cblks; cblkcnt > 0;) { 887 cblkxend = cblkxstart + (1 << rlvl->cblkwidthexpn); 888 cblkyend = cblkystart + (1 << rlvl->cblkheightexpn); 889 tmpxstart = JAS_MAX(cblkxstart, prc->xstart); 890 tmpystart = JAS_MAX(cblkystart, prc->ystart); 891 tmpxend = JAS_MIN(cblkxend, prc->xend); 892 tmpyend = JAS_MIN(cblkyend, prc->yend); 893 if (tmpxend > tmpxstart && tmpyend > tmpystart) { 894 cblk->firstpassno = -1; 895 cblk->mqdec = 0; 896 cblk->nulldec = 0; 897 cblk->flags = 0; 898 cblk->numpasses = 0; 899 cblk->segs.head = 0; 900 cblk->segs.tail = 0; 901 cblk->curseg = 0; 902 cblk->numimsbs = 0; 903 cblk->numlenbits = 3; 904 cblk->flags = 0; 905 if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) { 906 return -1; 907 } 908 jas_seq2d_bindsub(cblk->data, band->data, tmpxstart, tmpystart, tmpxend, tmpyend); 909 ++cblk; 910 --cblkcnt; 911 } 912 cblkxstart += 1 << rlvl->cblkwidthexpn; 913 if (cblkxstart >= cbgxend) { 914 cblkxstart = cbgxstart; 915 cblkystart += 1 << rlvl->cblkheightexpn; 916 } 917 } 918 919 } else { 920 prc->cblks = 0; 921 prc->incltagtree = 0; 922 prc->numimsbstagtree = 0; 923 } 924 cbgxstart += 1 << rlvl->cbgwidthexpn; 925 if (cbgxstart >= brcbgxend) { 926 cbgxstart = tlcbgxstart; 927 cbgystart += 1 << rlvl->cbgheightexpn; 928 } 929 930 } 931/********************************************/ 932 } 933 } 934 } 935 936if (!(tile->pi = jpc_dec_pi_create(dec, tile))) 937{ 938 return -1; 939} 940 941 for (pchgno = 0; pchgno < jpc_pchglist_numpchgs(tile->cp->pchglist); 942 ++pchgno) { 943 pchg = jpc_pchg_copy(jpc_pchglist_get(tile->cp->pchglist, pchgno)); 944 assert(pchg); 945 jpc_pi_addpchg(tile->pi, pchg); 946 } 947 jpc_pi_init(tile->pi); 948 949 return 0; 950} 951 952static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile) 953{ 954 jpc_dec_tcomp_t *tcomp; 955 int compno; 956 int bandno; 957 int rlvlno; 958 jpc_dec_band_t *band; 959 jpc_dec_rlvl_t *rlvl; 960 int prcno; 961 jpc_dec_prc_t *prc; 962 jpc_dec_seg_t *seg; 963 jpc_dec_cblk_t *cblk; 964 int cblkno; 965 966if (tile->tcomps) { 967 968 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; 969 ++compno, ++tcomp) { 970 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; 971 ++rlvlno, ++rlvl) { 972if (!rlvl->bands) { 973 continue; 974} 975 for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; ++bandno, ++band) { 976if (band->prcs) { 977 for (prcno = 0, prc = band->prcs; prcno < 978 rlvl->numprcs; ++prcno, ++prc) { 979if (!prc->cblks) { 980 continue; 981} 982 for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; ++cblkno, ++cblk) { 983 984 while (cblk->segs.head) { 985 seg = cblk->segs.head; 986 jpc_seglist_remove(&cblk->segs, seg); 987 jpc_seg_destroy(seg); 988 } 989 jas_matrix_destroy(cblk->data); 990 if (cblk->mqdec) { 991 jpc_mqdec_destroy(cblk->mqdec); 992 } 993 if (cblk->nulldec) { 994 jpc_bitstream_close(cblk->nulldec); 995 } 996 if (cblk->flags) { 997 jas_matrix_destroy(cblk->flags); 998 } 999 } 1000 if (prc->incltagtree) { 1001 jpc_tagtree_destroy(prc->incltagtree); 1002 } 1003 if (prc->numimsbstagtree) { 1004 jpc_tagtree_destroy(prc->numimsbstagtree); 1005 } 1006 if (prc->cblks) { 1007 jas_free(prc->cblks); 1008 } 1009 } 1010} 1011 if (band->data) { 1012 jas_matrix_destroy(band->data); 1013 } 1014 if (band->prcs) { 1015 jas_free(band->prcs); 1016 } 1017 } 1018 if (rlvl->bands) { 1019 jas_free(rlvl->bands); 1020 } 1021 } 1022 if (tcomp->rlvls) { 1023 jas_free(tcomp->rlvls); 1024 } 1025 if (tcomp->data) { 1026 jas_matrix_destroy(tcomp->data); 1027 } 1028 if (tcomp->tsfb) { 1029 jpc_tsfb_destroy(tcomp->tsfb); 1030 } 1031 } 1032} 1033 if (tile->cp) { 1034 jpc_dec_cp_destroy(tile->cp); 1035 tile->cp = 0; 1036 } 1037 if (tile->tcomps) { 1038 jas_free(tile->tcomps); 1039 tile->tcomps = 0; 1040 } 1041 if (tile->pi) { 1042 jpc_pi_destroy(tile->pi); 1043 tile->pi = 0; 1044 } 1045 if (tile->pkthdrstream) { 1046 jas_stream_close(tile->pkthdrstream); 1047 tile->pkthdrstream = 0; 1048 } 1049 if (tile->pptstab) { 1050 jpc_ppxstab_destroy(tile->pptstab); 1051 tile->pptstab = 0; 1052 } 1053 1054 tile->state = JPC_TILE_DONE; 1055 1056 return 0; 1057} 1058 1059static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile) 1060{ 1061 int i; 1062 int j; 1063 jpc_dec_tcomp_t *tcomp; 1064 jpc_dec_rlvl_t *rlvl; 1065 jpc_dec_band_t *band; 1066 int compno; 1067 int rlvlno; 1068 int bandno; 1069 int adjust; 1070 int v; 1071 jpc_dec_ccp_t *ccp; 1072 jpc_dec_cmpt_t *cmpt; 1073 1074 if (jpc_dec_decodecblks(dec, tile)) { 1075 fprintf(stderr, "jpc_dec_decodecblks failed\n"); 1076 return -1; 1077 } 1078 1079 /* Perform dequantization. */ 1080 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; 1081 ++compno, ++tcomp) { 1082 ccp = &tile->cp->ccps[compno]; 1083 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; 1084 ++rlvlno, ++rlvl) { 1085 if (!rlvl->bands) { 1086 continue; 1087 } 1088 for (bandno = 0, band = rlvl->bands; 1089 bandno < rlvl->numbands; ++bandno, ++band) { 1090 if (!band->data) { 1091 continue; 1092 } 1093 jpc_undo_roi(band->data, band->roishift, ccp->roishift - 1094 band->roishift, band->numbps); 1095 if (tile->realmode) { 1096 jas_matrix_asl(band->data, JPC_FIX_FRACBITS); 1097 jpc_dequantize(band->data, band->absstepsize); 1098 } 1099 1100 } 1101 } 1102 } 1103 1104 /* Apply an inverse wavelet transform if necessary. */ 1105 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; 1106 ++compno, ++tcomp) { 1107 ccp = &tile->cp->ccps[compno]; 1108 jpc_tsfb_synthesize(tcomp->tsfb, ((ccp->qmfbid == 1109 JPC_COX_RFT) ? JPC_TSFB_RITIMODE : 0), tcomp->data); 1110 } 1111 1112 1113 /* Apply an inverse intercomponent transform if necessary. */ 1114 switch (tile->cp->mctid) { 1115 case JPC_MCT_RCT: 1116 assert(dec->numcomps == 3); 1117 jpc_irct(tile->tcomps[0].data, tile->tcomps[1].data, 1118 tile->tcomps[2].data); 1119 break; 1120 case JPC_MCT_ICT: 1121 assert(dec->numcomps == 3); 1122 jpc_iict(tile->tcomps[0].data, tile->tcomps[1].data, 1123 tile->tcomps[2].data); 1124 break; 1125 } 1126 1127 /* Perform rounding and convert to integer values. */ 1128 if (tile->realmode) { 1129 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; 1130 ++compno, ++tcomp) { 1131 for (i = 0; i < jas_matrix_numrows(tcomp->data); ++i) { 1132 for (j = 0; j < jas_matrix_numcols(tcomp->data); ++j) { 1133 v = jas_matrix_get(tcomp->data, i, j); 1134 v = jpc_fix_round(v); 1135 jas_matrix_set(tcomp->data, i, j, jpc_fixtoint(v)); 1136 } 1137 } 1138 } 1139 } 1140 1141 /* Perform level shift. */ 1142 for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < 1143 dec->numcomps; ++compno, ++tcomp, ++cmpt) { 1144 adjust = cmpt->sgnd ? 0 : (1 << (cmpt->prec - 1)); 1145 for (i = 0; i < jas_matrix_numrows(tcomp->data); ++i) { 1146 for (j = 0; j < jas_matrix_numcols(tcomp->data); ++j) { 1147 *jas_matrix_getref(tcomp->data, i, j) += adjust; 1148 } 1149 } 1150 } 1151 1152 /* Perform clipping. */ 1153 for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < 1154 dec->numcomps; ++compno, ++tcomp, ++cmpt) { 1155 jpc_fix_t mn; 1156 jpc_fix_t mx; 1157 mn = cmpt->sgnd ? (-(1 << (cmpt->prec - 1))) : (0); 1158 mx = cmpt->sgnd ? ((1 << (cmpt->prec - 1)) - 1) : ((1 << 1159 cmpt->prec) - 1); 1160 jas_matrix_clip(tcomp->data, mn, mx); 1161 } 1162 1163 /* XXX need to free tsfb struct */ 1164 1165 /* Write the data for each component of the image. */ 1166 for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < 1167 dec->numcomps; ++compno, ++tcomp, ++cmpt) { 1168 if (jas_image_writecmpt(dec->image, compno, tcomp->xstart - 1169 JPC_CEILDIV(dec->xstart, cmpt->hstep), tcomp->ystart - 1170 JPC_CEILDIV(dec->ystart, cmpt->vstep), jas_matrix_numcols( 1171 tcomp->data), jas_matrix_numrows(tcomp->data), tcomp->data)) { 1172 fprintf(stderr, "write component failed\n"); 1173 return -4; 1174 } 1175 } 1176 1177 return 0; 1178} 1179 1180static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms) 1181{ 1182 int tileno; 1183 jpc_dec_tile_t *tile; 1184 for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno, 1185 ++tile) { 1186 if (tile->state == JPC_TILE_ACTIVE) { 1187 if (jpc_dec_tiledecode(dec, tile)) { 1188 return -1; 1189 } 1190 } 1191 jpc_dec_tilefini(dec, tile); 1192 } 1193 1194 /* We are done processing the code stream. */ 1195 dec->state = JPC_MT; 1196 1197 return 1; 1198} 1199 1200static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms) 1201{ 1202 jpc_siz_t *siz = &ms->parms.siz; 1203 uint_fast16_t compno; 1204 uint_fast32_t tileno; 1205 jpc_dec_tile_t *tile; 1206 jpc_dec_tcomp_t *tcomp; 1207 uint_fast32_t htileno; 1208 uint_fast32_t vtileno; 1209 jpc_dec_cmpt_t *cmpt; 1210 1211 dec->xstart = siz->xoff; 1212 dec->ystart = siz->yoff; 1213 dec->xend = siz->width; 1214 dec->yend = siz->height; 1215 dec->tilewidth = siz->tilewidth; 1216 dec->tileheight = siz->tileheight; 1217 dec->tilexoff = siz->tilexoff; 1218 dec->tileyoff = siz->tileyoff; 1219 dec->numcomps = siz->numcomps; 1220 if (!(dec->cp = jpc_dec_cp_create(dec->numcomps))) { 1221 return -1; 1222 } 1223 1224 if (!(dec->cmpts = jas_malloc(dec->numcomps * sizeof(jpc_dec_cmpt_t)))) { 1225 return -1; 1226 } 1227 1228 for (compno = 0, cmpt = dec->cmpts; compno < dec->numcomps; ++compno, 1229 ++cmpt) { 1230 cmpt->prec = siz->comps[compno].prec; 1231 cmpt->sgnd = siz->comps[compno].sgnd; 1232 cmpt->hstep = siz->comps[compno].hsamp; 1233 cmpt->vstep = siz->comps[compno].vsamp; 1234 cmpt->width = JPC_CEILDIV(dec->xend, cmpt->hstep) - 1235 JPC_CEILDIV(dec->xstart, cmpt->hstep); 1236 cmpt->height = JPC_CEILDIV(dec->yend, cmpt->vstep) - 1237 JPC_CEILDIV(dec->ystart, cmpt->vstep); 1238 cmpt->hsubstep = 0; 1239 cmpt->vsubstep = 0; 1240 } 1241 1242 dec->image = 0; 1243 1244 dec->numhtiles = JPC_CEILDIV(dec->xend - dec->tilexoff, dec->tilewidth); 1245 dec->numvtiles = JPC_CEILDIV(dec->yend - dec->tileyoff, dec->tileheight); 1246 dec->numtiles = dec->numhtiles * dec->numvtiles; 1247 if (!(dec->tiles = jas_malloc(dec->numtiles * sizeof(jpc_dec_tile_t)))) { 1248 return -1; 1249 } 1250 1251 for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno, 1252 ++tile) { 1253 htileno = tileno % dec->numhtiles; 1254 vtileno = tileno / dec->numhtiles; 1255 tile->realmode = 0; 1256 tile->state = JPC_TILE_INIT; 1257 tile->xstart = JAS_MAX(dec->tilexoff + htileno * dec->tilewidth, 1258 dec->xstart); 1259 tile->ystart = JAS_MAX(dec->tileyoff + vtileno * dec->tileheight, 1260 dec->ystart); 1261 tile->xend = JAS_MIN(dec->tilexoff + (htileno + 1) * 1262 dec->tilewidth, dec->xend); 1263 tile->yend = JAS_MIN(dec->tileyoff + (vtileno + 1) * 1264 dec->tileheight, dec->yend); 1265 tile->numparts = 0; 1266 tile->partno = 0; 1267 tile->pkthdrstream = 0; 1268 tile->pkthdrstreampos = 0; 1269 tile->pptstab = 0; 1270 tile->cp = 0; 1271 if (!(tile->tcomps = jas_malloc(dec->numcomps * 1272 sizeof(jpc_dec_tcomp_t)))) { 1273 return -1; 1274 } 1275 for (compno = 0, cmpt = dec->cmpts, tcomp = tile->tcomps; 1276 compno < dec->numcomps; ++compno, ++cmpt, ++tcomp) { 1277 tcomp->rlvls = 0; 1278 tcomp->data = 0; 1279 tcomp->xstart = JPC_CEILDIV(tile->xstart, cmpt->hstep); 1280 tcomp->ystart = JPC_CEILDIV(tile->ystart, cmpt->vstep); 1281 tcomp->xend = JPC_CEILDIV(tile->xend, cmpt->hstep); 1282 tcomp->yend = JPC_CEILDIV(tile->yend, cmpt->vstep); 1283 tcomp->tsfb = 0; 1284 } 1285 } 1286 1287 dec->pkthdrstreams = 0; 1288 1289 /* We should expect to encounter other main header marker segments 1290 or an SOT marker segment next. */ 1291 dec->state = JPC_MH; 1292 1293 return 0; 1294} 1295 1296static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms) 1297{ 1298 jpc_cod_t *cod = &ms->parms.cod; 1299 jpc_dec_tile_t *tile; 1300 1301 switch (dec->state) { 1302 case JPC_MH: 1303 jpc_dec_cp_setfromcod(dec->cp, cod); 1304 break; 1305 case JPC_TPH: 1306 if (!(tile = dec->curtile)) { 1307 return -1; 1308 } 1309 if (tile->partno != 0) { 1310 return -1; 1311 } 1312 jpc_dec_cp_setfromcod(tile->cp, cod); 1313 break; 1314 } 1315 return 0; 1316} 1317 1318static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms) 1319{ 1320 jpc_coc_t *coc = &ms->parms.coc; 1321 jpc_dec_tile_t *tile; 1322 1323 if (coc->compno > dec->numcomps) { 1324 fprintf(stderr, 1325 "invalid component number in COC marker segment\n"); 1326 return -1; 1327 } 1328 switch (dec->state) { 1329 case JPC_MH: 1330 jpc_dec_cp_setfromcoc(dec->cp, coc); 1331 break; 1332 case JPC_TPH: 1333 if (!(tile = dec->curtile)) { 1334 return -1; 1335 } 1336 if (tile->partno > 0) { 1337 return -1; 1338 } 1339 jpc_dec_cp_setfromcoc(tile->cp, coc); 1340 break; 1341 } 1342 return 0; 1343} 1344 1345static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms) 1346{ 1347 jpc_rgn_t *rgn = &ms->parms.rgn; 1348 jpc_dec_tile_t *tile; 1349 1350 if (rgn->compno > dec->numcomps) { 1351 fprintf(stderr, 1352 "invalid component number in RGN marker segment\n"); 1353 return -1; 1354 } 1355 switch (dec->state) { 1356 case JPC_MH: 1357 jpc_dec_cp_setfromrgn(dec->cp, rgn); 1358 break; 1359 case JPC_TPH: 1360 if (!(tile = dec->curtile)) { 1361 return -1; 1362 } 1363 if (tile->partno > 0) { 1364 return -1; 1365 } 1366 jpc_dec_cp_setfromrgn(tile->cp, rgn); 1367 break; 1368 } 1369 1370 return 0; 1371} 1372 1373static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms) 1374{ 1375 jpc_qcd_t *qcd = &ms->parms.qcd; 1376 jpc_dec_tile_t *tile; 1377 1378 switch (dec->state) { 1379 case JPC_MH: 1380 jpc_dec_cp_setfromqcd(dec->cp, qcd); 1381 break; 1382 case JPC_TPH: 1383 if (!(tile = dec->curtile)) { 1384 return -1; 1385 } 1386 if (tile->partno > 0) { 1387 return -1; 1388 } 1389 jpc_dec_cp_setfromqcd(tile->cp, qcd); 1390 break; 1391 } 1392 return 0; 1393} 1394 1395static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms) 1396{ 1397 jpc_qcc_t *qcc = &ms->parms.qcc; 1398 jpc_dec_tile_t *tile; 1399 1400 if (qcc->compno > dec->numcomps) { 1401 fprintf(stderr, 1402 "invalid component number in QCC marker segment\n"); 1403 return -1; 1404 } 1405 switch (dec->state) { 1406 case JPC_MH: 1407 jpc_dec_cp_setfromqcc(dec->cp, qcc); 1408 break; 1409 case JPC_TPH: 1410 if (!(tile = dec->curtile)) { 1411 return -1; 1412 } 1413 if (tile->partno > 0) { 1414 return -1; 1415 } 1416 jpc_dec_cp_setfromqcc(tile->cp, qcc); 1417 break; 1418 } 1419 return 0; 1420} 1421 1422static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms) 1423{ 1424 jpc_poc_t *poc = &ms->parms.poc; 1425 jpc_dec_tile_t *tile; 1426 switch (dec->state) { 1427 case JPC_MH: 1428 if (jpc_dec_cp_setfrompoc(dec->cp, poc, 1)) { 1429 return -1; 1430 } 1431 break; 1432 case JPC_TPH: 1433 if (!(tile = dec->curtile)) { 1434 return -1; 1435 } 1436 if (!tile->partno) { 1437 if (jpc_dec_cp_setfrompoc(tile->cp, poc, (!tile->partno))) { 1438 return -1; 1439 } 1440 } else { 1441 jpc_pi_addpchgfrompoc(tile->pi, poc); 1442 } 1443 break; 1444 } 1445 return 0; 1446} 1447 1448static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms) 1449{ 1450 jpc_ppm_t *ppm = &ms->parms.ppm; 1451 jpc_ppxstabent_t *ppmstabent; 1452 1453 if (!dec->ppmstab) { 1454 if (!(dec->ppmstab = jpc_ppxstab_create())) { 1455 return -1; 1456 } 1457 } 1458 1459 if (!(ppmstabent = jpc_ppxstabent_create())) { 1460 return -1; 1461 } 1462 ppmstabent->ind = ppm->ind; 1463 ppmstabent->data = ppm->data; 1464 ppm->data = 0; 1465 ppmstabent->len = ppm->len; 1466 if (jpc_ppxstab_insert(dec->ppmstab, ppmstabent)) { 1467 return -1; 1468 } 1469 return 0; 1470} 1471 1472static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms) 1473{ 1474 jpc_ppt_t *ppt = &ms->parms.ppt; 1475 jpc_dec_tile_t *tile; 1476 jpc_ppxstabent_t *pptstabent; 1477 1478 tile = dec->curtile; 1479 if (!tile->pptstab) { 1480 if (!(tile->pptstab = jpc_ppxstab_create())) { 1481 return -1; 1482 } 1483 } 1484 if (!(pptstabent = jpc_ppxstabent_create())) { 1485 return -1; 1486 } 1487 pptstabent->ind = ppt->ind; 1488 pptstabent->data = ppt->data; 1489 ppt->data = 0; 1490 pptstabent->len = ppt->len; 1491 if (jpc_ppxstab_insert(tile->pptstab, pptstabent)) { 1492 return -1; 1493 } 1494 return 0; 1495} 1496 1497static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms) 1498{ 1499 return 0; 1500} 1501 1502static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms) 1503{ 1504 fprintf(stderr, "warning: ignoring unknown marker segment\n"); 1505 jpc_ms_dump(ms, stderr); 1506 return 0; 1507} 1508 1509/******************************************************************************\ 1510* 1511\******************************************************************************/ 1512 1513static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps) 1514{ 1515 jpc_dec_cp_t *cp; 1516 jpc_dec_ccp_t *ccp; 1517 int compno; 1518 1519 if (!(cp = jas_malloc(sizeof(jpc_dec_cp_t)))) { 1520 return 0; 1521 } 1522 cp->flags = 0; 1523 cp->numcomps = numcomps; 1524 cp->prgord = 0; 1525 cp->numlyrs = 0; 1526 cp->mctid = 0; 1527 cp->csty = 0; 1528 if (!(cp->ccps = jas_malloc(cp->numcomps * sizeof(jpc_dec_ccp_t)))) { 1529 return 0; 1530 } 1531 if (!(cp->pchglist = jpc_pchglist_create())) { 1532 jas_free(cp->ccps); 1533 return 0; 1534 } 1535 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; 1536 ++compno, ++ccp) { 1537 ccp->flags = 0; 1538 ccp->numrlvls = 0; 1539 ccp->cblkwidthexpn = 0; 1540 ccp->cblkheightexpn = 0; 1541 ccp->qmfbid = 0; 1542 ccp->numstepsizes = 0; 1543 ccp->numguardbits = 0; 1544 ccp->roishift = 0; 1545 ccp->cblkctx = 0; 1546 } 1547 return cp; 1548} 1549 1550static jpc_dec_cp_t *jpc_dec_cp_copy(jpc_dec_cp_t *cp) 1551{ 1552 jpc_dec_cp_t *newcp; 1553 jpc_dec_ccp_t *newccp; 1554 jpc_dec_ccp_t *ccp; 1555 int compno; 1556 1557 if (!(newcp = jpc_dec_cp_create(cp->numcomps))) { 1558 return 0; 1559 } 1560 newcp->flags = cp->flags; 1561 newcp->prgord = cp->prgord; 1562 newcp->numlyrs = cp->numlyrs; 1563 newcp->mctid = cp->mctid; 1564 newcp->csty = cp->csty; 1565 jpc_pchglist_destroy(newcp->pchglist); 1566 newcp->pchglist = 0; 1567 if (!(newcp->pchglist = jpc_pchglist_copy(cp->pchglist))) { 1568 jas_free(newcp); 1569 return 0; 1570 } 1571 for (compno = 0, newccp = newcp->ccps, ccp = cp->ccps; 1572 compno < cp->numcomps; 1573 ++compno, ++newccp, ++ccp) { 1574 *newccp = *ccp; 1575 } 1576 return newcp; 1577} 1578 1579static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp) 1580{ 1581 int compno; 1582 jpc_dec_ccp_t *ccp; 1583 cp->flags &= (JPC_CSET | JPC_QSET); 1584 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; 1585 ++compno, ++ccp) { 1586 ccp->flags = 0; 1587 } 1588} 1589 1590static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp) 1591{ 1592 if (cp->ccps) { 1593 jas_free(cp->ccps); 1594 } 1595 if (cp->pchglist) { 1596 jpc_pchglist_destroy(cp->pchglist); 1597 } 1598 jas_free(cp); 1599} 1600 1601static int jpc_dec_cp_isvalid(jpc_dec_cp_t *cp) 1602{ 1603 uint_fast16_t compcnt; 1604 jpc_dec_ccp_t *ccp; 1605 1606 if (!(cp->flags & JPC_CSET) || !(cp->flags & JPC_QSET)) { 1607 return 0; 1608 } 1609 for (compcnt = cp->numcomps, ccp = cp->ccps; compcnt > 0; --compcnt, 1610 ++ccp) { 1611 /* Is there enough step sizes for the number of bands? */ 1612 if ((ccp->qsty != JPC_QCX_SIQNT && ccp->numstepsizes < 3 * 1613 ccp->numrlvls - 2) || (ccp->qsty == JPC_QCX_SIQNT && 1614 ccp->numstepsizes != 1)) { 1615 return 0; 1616 } 1617 } 1618 return 1; 1619} 1620 1621static void calcstepsizes(uint_fast16_t refstepsize, int numrlvls, 1622 uint_fast16_t *stepsizes) 1623{ 1624 int bandno; 1625 int numbands; 1626 uint_fast16_t expn; 1627 uint_fast16_t mant; 1628 expn = JPC_QCX_GETEXPN(refstepsize); 1629 mant = JPC_QCX_GETMANT(refstepsize); 1630 numbands = 3 * numrlvls - 2; 1631 for (bandno = 0; bandno < numbands; ++bandno) { 1632 stepsizes[bandno] = JPC_QCX_MANT(mant) | JPC_QCX_EXPN(expn + 1633 (numrlvls - 1) - (numrlvls - 1 - ((bandno > 0) ? ((bandno + 2) / 3) : (0)))); 1634 } 1635} 1636 1637static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp) 1638{ 1639 jpc_dec_ccp_t *ccp; 1640 int compno; 1641 int i; 1642 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; 1643 ++compno, ++ccp) { 1644 if (!(ccp->csty & JPC_COX_PRT)) { 1645 for (i = 0; i < JPC_MAXRLVLS; ++i) { 1646 ccp->prcwidthexpns[i] = 15; 1647 ccp->prcheightexpns[i] = 15; 1648 } 1649 } 1650 if (ccp->qsty == JPC_QCX_SIQNT) { 1651 calcstepsizes(ccp->stepsizes[0], ccp->numrlvls, ccp->stepsizes); 1652 } 1653 } 1654 return 0; 1655} 1656 1657static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, jpc_cod_t *cod) 1658{ 1659 jpc_dec_ccp_t *ccp; 1660 int compno; 1661 cp->flags |= JPC_CSET; 1662 cp->prgord = cod->prg; 1663 if (cod->mctrans) { 1664 cp->mctid = (cod->compparms.qmfbid == JPC_COX_INS) ? (JPC_MCT_ICT) : (JPC_MCT_RCT); 1665 } else { 1666 cp->mctid = JPC_MCT_NONE; 1667 } 1668 cp->numlyrs = cod->numlyrs; 1669 cp->csty = cod->csty & (JPC_COD_SOP | JPC_COD_EPH); 1670 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; 1671 ++compno, ++ccp) { 1672 jpc_dec_cp_setfromcox(cp, ccp, &cod->compparms, 0); 1673 } 1674 cp->flags |= JPC_CSET; 1675 return 0; 1676} 1677 1678static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, jpc_coc_t *coc) 1679{ 1680 jpc_dec_cp_setfromcox(cp, &cp->ccps[coc->compno], &coc->compparms, JPC_COC); 1681 return 0; 1682} 1683 1684static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, 1685 jpc_coxcp_t *compparms, int flags) 1686{ 1687 int rlvlno; 1688 if ((flags & JPC_COC) || !(ccp->flags & JPC_COC)) { 1689 ccp->numrlvls = compparms->numdlvls + 1; 1690 ccp->cblkwidthexpn = JPC_COX_GETCBLKSIZEEXPN( 1691 compparms->cblkwidthval); 1692 ccp->cblkheightexpn = JPC_COX_GETCBLKSIZEEXPN( 1693 compparms->cblkheightval); 1694 ccp->qmfbid = compparms->qmfbid; 1695 ccp->cblkctx = compparms->cblksty; 1696 ccp->csty = compparms->csty & JPC_COX_PRT; 1697 for (rlvlno = 0; rlvlno < compparms->numrlvls; ++rlvlno) { 1698 ccp->prcwidthexpns[rlvlno] = 1699 compparms->rlvls[rlvlno].parwidthval; 1700 ccp->prcheightexpns[rlvlno] = 1701 compparms->rlvls[rlvlno].parheightval; 1702 } 1703 ccp->flags |= flags | JPC_CSET; 1704 } 1705 return 0; 1706} 1707 1708static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, jpc_qcd_t *qcd) 1709{ 1710 int compno; 1711 jpc_dec_ccp_t *ccp; 1712 for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; 1713 ++compno, ++ccp) { 1714 jpc_dec_cp_setfromqcx(cp, ccp, &qcd->compparms, 0); 1715 } 1716 cp->flags |= JPC_QSET; 1717 return 0; 1718} 1719 1720static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, jpc_qcc_t *qcc) 1721{ 1722 return jpc_dec_cp_setfromqcx(cp, &cp->ccps[qcc->compno], &qcc->compparms, JPC_QCC); 1723} 1724 1725static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, 1726 jpc_qcxcp_t *compparms, int flags) 1727{ 1728 int bandno; 1729 if ((flags & JPC_QCC) || !(ccp->flags & JPC_QCC)) { 1730 ccp->flags |= flags | JPC_QSET; 1731 for (bandno = 0; bandno < compparms->numstepsizes; ++bandno) { 1732 ccp->stepsizes[bandno] = compparms->stepsizes[bandno]; 1733 } 1734 ccp->numstepsizes = compparms->numstepsizes; 1735 ccp->numguardbits = compparms->numguard; 1736 ccp->qsty = compparms->qntsty; 1737 } 1738 return 0; 1739} 1740 1741static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, jpc_rgn_t *rgn) 1742{ 1743 jpc_dec_ccp_t *ccp; 1744 ccp = &cp->ccps[rgn->compno]; 1745 ccp->roishift = rgn->roishift; 1746 return 0; 1747} 1748 1749static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, jpc_poc_t *poc) 1750{ 1751 int pchgno; 1752 jpc_pchg_t *pchg; 1753 for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) { 1754 if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) { 1755 return -1; 1756 } 1757 if (jpc_pchglist_insert(pi->pchglist, -1, pchg)) { 1758 return -1; 1759 } 1760 } 1761 return 0; 1762} 1763 1764static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, jpc_poc_t *poc, int reset) 1765{ 1766 int pchgno; 1767 jpc_pchg_t *pchg; 1768 if (reset) { 1769 while (jpc_pchglist_numpchgs(cp->pchglist) > 0) { 1770 pchg = jpc_pchglist_remove(cp->pchglist, 0); 1771 jpc_pchg_destroy(pchg); 1772 } 1773 } 1774 for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) { 1775 if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) { 1776 return -1; 1777 } 1778 if (jpc_pchglist_insert(cp->pchglist, -1, pchg)) { 1779 return -1; 1780 } 1781 } 1782 return 0; 1783} 1784 1785static jpc_fix_t jpc_calcabsstepsize(int stepsize, int numbits) 1786{ 1787 jpc_fix_t absstepsize; 1788 int n; 1789 1790 absstepsize = jpc_inttofix(1); 1791 n = JPC_FIX_FRACBITS - 11; 1792 absstepsize |= (n >= 0) ? (JPC_QCX_GETMANT(stepsize) << n) : 1793 (JPC_QCX_GETMANT(stepsize) >> (-n)); 1794 n = numbits - JPC_QCX_GETEXPN(stepsize); 1795 absstepsize = (n >= 0) ? (absstepsize << n) : (absstepsize >> (-n)); 1796 return absstepsize; 1797} 1798 1799static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize) 1800{ 1801 int i; 1802 int j; 1803 int t; 1804 1805 assert(absstepsize >= 0); 1806 if (absstepsize == jpc_inttofix(1)) { 1807 return; 1808 } 1809 1810 for (i = 0; i < jas_matrix_numrows(x); ++i) { 1811 for (j = 0; j < jas_matrix_numcols(x); ++j) { 1812 t = jas_matrix_get(x, i, j); 1813 if (t) { 1814 t = jpc_fix_mul(t, absstepsize); 1815 } else { 1816 t = 0; 1817 } 1818 jas_matrix_set(x, i, j, t); 1819 } 1820 } 1821 1822} 1823 1824static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps) 1825{ 1826 int i; 1827 int j; 1828 int thresh; 1829 jpc_fix_t val; 1830 jpc_fix_t mag; 1831 JPR_BOOL warn; 1832 uint_fast32_t mask; 1833 1834 if (roishift == 0 && bgshift == 0) { 1835 return; 1836 } 1837 thresh = 1 << roishift; 1838 1839 warn = JPR_FALSE; 1840 for (i = 0; i < jas_matrix_numrows(x); ++i) { 1841 for (j = 0; j < jas_matrix_numcols(x); ++j) { 1842 val = jas_matrix_get(x, i, j); 1843 mag = JAS_ABS(val); 1844 if (mag >= thresh) { 1845 /* We are dealing with ROI data. */ 1846 mag >>= roishift; 1847 val = (val < 0) ? (-mag) : mag; 1848 jas_matrix_set(x, i, j, val); 1849 } else { 1850 /* We are dealing with non-ROI (i.e., background) data. */ 1851 mag <<= bgshift; 1852 mask = (1 << numbps) - 1; 1853 /* Perform a basic sanity check on the sample value. */ 1854 /* Some implementations write garbage in the unused 1855 most-significant bit planes introduced by ROI shifting. 1856 Here we ensure that any such bits are masked off. */ 1857 if (mag & (~mask)) { 1858 if (!warn) { 1859 fprintf(stderr, 1860 "warning: possibly corrupt code stream\n"); 1861 warn = JPR_TRUE; 1862 } 1863 mag &= mask; 1864 } 1865 val = (val < 0) ? (-mag) : mag; 1866 jas_matrix_set(x, i, j, val); 1867 } 1868 } 1869 } 1870} 1871 1872static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in) 1873{ 1874 jpc_dec_t *dec; 1875 1876 if (!(dec = jas_malloc(sizeof(jpc_dec_t)))) { 1877 return 0; 1878 } 1879 1880 dec->image = 0; 1881 dec->xstart = 0; 1882 dec->ystart = 0; 1883 dec->xend = 0; 1884 dec->yend = 0; 1885 dec->tilewidth = 0; 1886 dec->tileheight = 0; 1887 dec->tilexoff = 0; 1888 dec->tileyoff = 0; 1889 dec->numhtiles = 0; 1890 dec->numvtiles = 0; 1891 dec->numtiles = 0; 1892 dec->tiles = 0; 1893 dec->curtile = 0; 1894 dec->numcomps = 0; 1895 dec->in = in; 1896 dec->cp = 0; 1897 dec->maxlyrs = impopts->maxlyrs; 1898 dec->maxpkts = impopts->maxpkts; 1899dec->numpkts = 0; 1900 dec->ppmseqno = 0; 1901 dec->state = 0; 1902 dec->cmpts = 0; 1903 dec->pkthdrstreams = 0; 1904 dec->ppmstab = 0; 1905 dec->curtileendoff = 0; 1906 1907 return dec; 1908} 1909 1910static void jpc_dec_destroy(jpc_dec_t *dec) 1911{ 1912 if (dec->cstate) { 1913 jpc_cstate_destroy(dec->cstate); 1914 } 1915 if (dec->pkthdrstreams) { 1916 jpc_streamlist_destroy(dec->pkthdrstreams); 1917 } 1918 if (dec->image) { 1919 jas_image_destroy(dec->image); 1920 } 1921 1922 if (dec->cp) { 1923 jpc_dec_cp_destroy(dec->cp); 1924 } 1925 1926 if (dec->cmpts) { 1927 jas_free(dec->cmpts); 1928 } 1929 1930 if (dec->tiles) { 1931 jas_free(dec->tiles); 1932 } 1933 1934 jas_free(dec); 1935} 1936 1937/******************************************************************************\ 1938* 1939\******************************************************************************/ 1940 1941void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins, jpc_dec_seg_t *node) 1942{ 1943 jpc_dec_seg_t *prev; 1944 jpc_dec_seg_t *next; 1945 1946 prev = ins; 1947 node->prev = prev; 1948 next = prev ? (prev->next) : 0; 1949 node->prev = prev; 1950 node->next = next; 1951 if (prev) { 1952 prev->next = node; 1953 } else { 1954 list->head = node; 1955 } 1956 if (next) { 1957 next->prev = node; 1958 } else { 1959 list->tail = node; 1960 } 1961} 1962 1963void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *seg) 1964{ 1965 jpc_dec_seg_t *prev; 1966 jpc_dec_seg_t *next; 1967 1968 prev = seg->prev; 1969 next = seg->next; 1970 if (prev) { 1971 prev->next = next; 1972 } else { 1973 list->head = next; 1974 } 1975 if (next) { 1976 next->prev = prev; 1977 } else { 1978 list->tail = prev; 1979 } 1980 seg->prev = 0; 1981 seg->next = 0; 1982} 1983 1984jpc_dec_seg_t *jpc_seg_alloc() 1985{ 1986 jpc_dec_seg_t *seg; 1987 1988 if (!(seg = jas_malloc(sizeof(jpc_dec_seg_t)))) { 1989 return 0; 1990 } 1991 seg->prev = 0; 1992 seg->next = 0; 1993 seg->passno = -1; 1994 seg->numpasses = 0; 1995 seg->maxpasses = 0; 1996 seg->type = JPC_SEG_INVALID; 1997 seg->stream = 0; 1998 seg->cnt = 0; 1999 seg->complete = 0; 2000 seg->lyrno = -1; 2001 return seg; 2002} 2003 2004void jpc_seg_destroy(jpc_dec_seg_t *seg) 2005{ 2006 if (seg->stream) { 2007 jas_stream_close(seg->stream); 2008 } 2009 jas_free(seg); 2010} 2011 2012static int jpc_dec_dump(jpc_dec_t *dec, FILE *out) 2013{ 2014 jpc_dec_tile_t *tile; 2015 int tileno; 2016 jpc_dec_tcomp_t *tcomp; 2017 uint_fast16_t compno; 2018 jpc_dec_rlvl_t *rlvl; 2019 int rlvlno; 2020 jpc_dec_band_t *band; 2021 int bandno; 2022 jpc_dec_prc_t *prc; 2023 int prcno; 2024 jpc_dec_cblk_t *cblk; 2025 int cblkno; 2026 2027 for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; 2028 ++tileno, ++tile) { 2029 for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; 2030 ++compno, ++tcomp) { 2031 for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < 2032 tcomp->numrlvls; ++rlvlno, ++rlvl) { 2033fprintf(out, "RESOLUTION LEVEL %d\n", rlvlno); 2034fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", 2035 rlvl->xstart, rlvl->ystart, rlvl->xend, rlvl->yend, rlvl->xend - 2036 rlvl->xstart, rlvl->yend - rlvl->ystart); 2037 for (bandno = 0, band = rlvl->bands; 2038 bandno < rlvl->numbands; ++bandno, ++band) { 2039fprintf(out, "BAND %d\n", bandno); 2040fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", 2041 jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data), 2042 jas_seq2d_yend(band->data), jas_seq2d_xend(band->data) - jas_seq2d_xstart(band->data), 2043 jas_seq2d_yend(band->data) - jas_seq2d_ystart(band->data)); 2044 for (prcno = 0, prc = band->prcs; 2045 prcno < rlvl->numprcs; ++prcno, 2046 ++prc) { 2047fprintf(out, "CODE BLOCK GROUP %d\n", prcno); 2048fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", 2049 prc->xstart, prc->ystart, prc->xend, prc->yend, prc->xend - 2050 prc->xstart, prc->yend - prc->ystart); 2051 for (cblkno = 0, cblk = 2052 prc->cblks; cblkno < 2053 prc->numcblks; ++cblkno, 2054 ++cblk) { 2055fprintf(out, "CODE BLOCK %d\n", cblkno); 2056fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", 2057 jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data), 2058 jas_seq2d_yend(cblk->data), jas_seq2d_xend(cblk->data) - jas_seq2d_xstart(cblk->data), 2059 jas_seq2d_yend(cblk->data) - jas_seq2d_ystart(cblk->data)); 2060 } 2061 } 2062 } 2063 } 2064 } 2065 } 2066 2067 return 0; 2068} 2069 2070jpc_streamlist_t *jpc_streamlist_create() 2071{ 2072 jpc_streamlist_t *streamlist; 2073 int i; 2074 2075 if (!(streamlist = jas_malloc(sizeof(jpc_streamlist_t)))) { 2076 return 0; 2077 } 2078 streamlist->numstreams = 0; 2079 streamlist->maxstreams = 100; 2080 if (!(streamlist->streams = jas_malloc(streamlist->maxstreams * 2081 sizeof(jas_stream_t *)))) { 2082 jas_free(streamlist); 2083 return 0; 2084 } 2085 for (i = 0; i < streamlist->maxstreams; ++i) { 2086 streamlist->streams[i] = 0; 2087 } 2088 return streamlist; 2089} 2090 2091int jpc_streamlist_insert(jpc_streamlist_t *streamlist, int streamno, 2092 jas_stream_t *stream) 2093{ 2094 jas_stream_t **newstreams; 2095 int newmaxstreams; 2096 int i; 2097 /* Grow the array of streams if necessary. */ 2098 if (streamlist->numstreams >= streamlist->maxstreams) { 2099 newmaxstreams = streamlist->maxstreams + 1024; 2100 if (!(newstreams = jas_realloc(streamlist->streams, 2101 (newmaxstreams + 1024) * sizeof(jas_stream_t *)))) { 2102 return -1; 2103 } 2104 for (i = streamlist->numstreams; i < streamlist->maxstreams; ++i) { 2105 streamlist->streams[i] = 0; 2106 } 2107 streamlist->maxstreams = newmaxstreams; 2108 streamlist->streams = newstreams; 2109 } 2110 if (streamno != streamlist->numstreams) { 2111 /* Can only handle insertion at start of list. */ 2112 return -1; 2113 } 2114 streamlist->streams[streamno] = stream; 2115 ++streamlist->numstreams; 2116 return 0; 2117} 2118 2119jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, int streamno) 2120{ 2121 jas_stream_t *stream; 2122 int i; 2123 if (streamno >= streamlist->numstreams) { 2124 abort(); 2125 } 2126 stream = streamlist->streams[streamno]; 2127 for (i = streamno + 1; i < streamlist->numstreams; ++i) { 2128 streamlist->streams[i - 1] = streamlist->streams[i]; 2129 } 2130 --streamlist->numstreams; 2131 return stream; 2132} 2133 2134void jpc_streamlist_destroy(jpc_streamlist_t *streamlist) 2135{ 2136 int streamno; 2137 if (streamlist->streams) { 2138 for (streamno = 0; streamno < streamlist->numstreams; 2139 ++streamno) { 2140 jas_stream_close(streamlist->streams[streamno]); 2141 } 2142 jas_free(streamlist->streams); 2143 } 2144 jas_free(streamlist); 2145} 2146 2147jas_stream_t *jpc_streamlist_get(jpc_streamlist_t *streamlist, int streamno) 2148{ 2149 assert(streamno < streamlist->numstreams); 2150 return streamlist->streams[streamno]; 2151} 2152 2153int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist) 2154{ 2155 return streamlist->numstreams; 2156} 2157 2158jpc_ppxstab_t *jpc_ppxstab_create() 2159{ 2160 jpc_ppxstab_t *tab; 2161 2162 if (!(tab = jas_malloc(sizeof(jpc_ppxstab_t)))) { 2163 return 0; 2164 } 2165 tab->numents = 0; 2166 tab->maxents = 0; 2167 tab->ents = 0; 2168 return tab; 2169} 2170 2171void jpc_ppxstab_destroy(jpc_ppxstab_t *tab) 2172{ 2173 int i; 2174 for (i = 0; i < tab->numents; ++i) { 2175 jpc_ppxstabent_destroy(tab->ents[i]); 2176 } 2177 if (tab->ents) { 2178 jas_free(tab->ents); 2179 } 2180 jas_free(tab); 2181} 2182 2183int jpc_ppxstab_grow(jpc_ppxstab_t *tab, int maxents) 2184{ 2185 jpc_ppxstabent_t **newents; 2186 if (tab->maxents < maxents) { 2187 newents = (tab->ents) ? jas_realloc(tab->ents, maxents * 2188 sizeof(jpc_ppxstabent_t *)) : jas_malloc(maxents * sizeof(jpc_ppxstabent_t *)); 2189 if (!newents) { 2190 return -1; 2191 } 2192 tab->ents = newents; 2193 tab->maxents = maxents; 2194 } 2195 return 0; 2196} 2197 2198int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent) 2199{ 2200 int inspt; 2201 int i; 2202 2203 for (i = 0; i < tab->numents; ++i) { 2204 if (tab->ents[i]->ind > ent->ind) { 2205 break; 2206 } 2207 } 2208 inspt = i; 2209 2210 if (tab->numents >= tab->maxents) { 2211 if (jpc_ppxstab_grow(tab, tab->maxents + 128)) { 2212 return -1; 2213 } 2214 } 2215 2216 for (i = tab->numents; i > inspt; --i) { 2217 tab->ents[i] = tab->ents[i - 1]; 2218 } 2219 tab->ents[i] = ent; 2220 ++tab->numents; 2221 2222 return 0; 2223} 2224 2225jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab) 2226{ 2227 jpc_streamlist_t *streams; 2228 jpr_uchar_t *dataptr; 2229 uint_fast32_t datacnt; 2230 uint_fast32_t tpcnt; 2231 jpc_ppxstabent_t *ent; 2232 int entno; 2233 jas_stream_t *stream; 2234 int n; 2235 2236 if (!(streams = jpc_streamlist_create())) { 2237 goto error; 2238 } 2239 2240 if (!tab->numents) { 2241 return streams; 2242 } 2243 2244 entno = 0; 2245 ent = tab->ents[entno]; 2246 dataptr = ent->data; 2247 datacnt = ent->len; 2248 for (;;) { 2249 2250 /* Get the length of the packet header data for the current 2251 tile-part. */ 2252 if (datacnt < 4) { 2253 goto error; 2254 } 2255 if (!(stream = jas_stream_memopen(0, 0))) { 2256 goto error; 2257 } 2258 if (jpc_streamlist_insert(streams, jpc_streamlist_numstreams(streams), 2259 stream)) { 2260 goto error; 2261 } 2262 tpcnt = (dataptr[0] << 24) | (dataptr[1] << 16) | (dataptr[2] << 8) 2263 | dataptr[3]; 2264 datacnt -= 4; 2265 dataptr += 4; 2266 2267 /* Get the packet header data for the current tile-part. */ 2268 while (tpcnt) { 2269 if (!datacnt) { 2270 if (++entno >= tab->numents) { 2271 goto error; 2272 } 2273 ent = tab->ents[entno]; 2274 dataptr = ent->data; 2275 datacnt = ent->len; 2276 } 2277 n = JAS_MIN(tpcnt, datacnt); 2278 if (jas_stream_write(stream, dataptr, n) != n) { 2279 goto error; 2280 } 2281 tpcnt -= n; 2282 dataptr += n; 2283 datacnt -= n; 2284 } 2285 jas_stream_rewind(stream); 2286 if (!datacnt) { 2287 if (++entno >= tab->numents) { 2288 break; 2289 } 2290 ent = tab->ents[entno]; 2291 dataptr = ent->data; 2292 datacnt = ent->len; 2293 } 2294 } 2295 2296 return streams; 2297 2298error: 2299 jpc_streamlist_destroy(streams); 2300 return 0; 2301} 2302 2303int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab) 2304{ 2305 int i; 2306 jpc_ppxstabent_t *ent; 2307 for (i = 0; i < tab->numents; ++i) { 2308 ent = tab->ents[i]; 2309 if (jas_stream_write(out, ent->data, ent->len) != ent->len) { 2310 return -1; 2311 } 2312 } 2313 return 0; 2314} 2315 2316jpc_ppxstabent_t *jpc_ppxstabent_create() 2317{ 2318 jpc_ppxstabent_t *ent; 2319 if (!(ent = jas_malloc(sizeof(jpc_ppxstabent_t)))) { 2320 return 0; 2321 } 2322 ent->data = 0; 2323 ent->len = 0; 2324 ent->ind = 0; 2325 return ent; 2326} 2327 2328void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent) 2329{ 2330 if (ent->data) { 2331 jas_free(ent->data); 2332 } 2333 jas_free(ent); 2334} 2335