Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 244 | chris | 1 | /* |
| 2 | * Copyright (c) 1999-2000 Image Power, Inc. and the University of |
||
| 3 | * British Columbia. |
||
| 4 | * Copyright (c) 2001-2003 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 | * Tier 2 Encoder |
||
| 115 | * |
||
| 116 | * $Id: jpc_t2enc.c,v 1.1 2003/05/15 01:30:32 ace Exp $ |
||
| 117 | */ |
||
| 118 | |||
| 119 | /******************************************************************************\ |
||
| 120 | * Includes. |
||
| 121 | \******************************************************************************/ |
||
| 122 | |||
| 123 | #include <stdio.h> |
||
| 124 | #include <stdlib.h> |
||
| 125 | #include <assert.h> |
||
| 126 | |||
| 127 | #include "jas_fix.h" |
||
| 128 | #include "jas_malloc.h" |
||
| 129 | #include "jas_math.h" |
||
| 130 | #include "jas_debug.h" |
||
| 131 | |||
| 132 | #include "jpc_flt.h" |
||
| 133 | #include "jpc_t2enc.h" |
||
| 134 | #include "jpc_t2cod.h" |
||
| 135 | #include "jpc_tagtree.h" |
||
| 136 | #include "jpc_enc.h" |
||
| 137 | #include "jpc_math.h" |
||
| 138 | |||
| 139 | /******************************************************************************\ |
||
| 140 | * Code. |
||
| 141 | \******************************************************************************/ |
||
| 142 | |||
| 143 | static int jpc_putcommacode(jpc_bitstream_t *out, int n) |
||
| 144 | { |
||
| 145 | assert(n >= 0); |
||
| 146 | |||
| 147 | while (--n >= 0) { |
||
| 148 | if (jpc_bitstream_putbit(out, 1) == EOF) { |
||
| 149 | return -1; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | if (jpc_bitstream_putbit(out, 0) == EOF) { |
||
| 153 | return -1; |
||
| 154 | } |
||
| 155 | return 0; |
||
| 156 | } |
||
| 157 | |||
| 158 | static int jpc_putnumnewpasses(jpc_bitstream_t *out, int n) |
||
| 159 | { |
||
| 160 | int ret; |
||
| 161 | |||
| 162 | if (n <= 0) { |
||
| 163 | return -1; |
||
| 164 | } else if (n == 1) { |
||
| 165 | ret = jpc_bitstream_putbit(out, 0); |
||
| 166 | } else if (n == 2) { |
||
| 167 | ret = jpc_bitstream_putbits(out, 2, 2); |
||
| 168 | } else if (n <= 5) { |
||
| 169 | ret = jpc_bitstream_putbits(out, 4, 0xc | (n - 3)); |
||
| 170 | } else if (n <= 36) { |
||
| 171 | ret = jpc_bitstream_putbits(out, 9, 0x1e0 | (n - 6)); |
||
| 172 | } else if (n <= 164) { |
||
| 173 | ret = jpc_bitstream_putbits(out, 16, 0xff80 | (n - 37)); |
||
| 174 | } else { |
||
| 175 | /* The standard has no provision for encoding a larger value. |
||
| 176 | In practice, however, it is highly unlikely that this |
||
| 177 | limitation will ever be encountered. */ |
||
| 178 | return -1; |
||
| 179 | } |
||
| 180 | |||
| 181 | return (ret != EOF) ? 0 : (-1); |
||
| 182 | } |
||
| 183 | |||
| 184 | int jpc_enc_encpkts(jpc_enc_t *enc, jas_stream_t *out) |
||
| 185 | { |
||
| 186 | jpc_enc_tile_t *tile; |
||
| 187 | jpc_pi_t *pi; |
||
| 188 | |||
| 189 | tile = enc->curtile; |
||
| 190 | |||
| 191 | jpc_init_t2state(enc, 0); |
||
| 192 | pi = tile->pi; |
||
| 193 | jpc_pi_init(pi); |
||
| 194 | |||
| 195 | if (!jpc_pi_next(pi)) { |
||
| 196 | for (;;) { |
||
| 197 | if (jpc_enc_encpkt(enc, out, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi), |
||
| 198 | jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) { |
||
| 199 | return -1; |
||
| 200 | } |
||
| 201 | if (jpc_pi_next(pi)) { |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | return 0; |
||
| 208 | } |
||
| 209 | |||
| 210 | int jpc_enc_encpkt(jpc_enc_t *enc, jas_stream_t *out, int compno, int lvlno, int prcno, int lyrno) |
||
| 211 | { |
||
| 212 | jpc_enc_tcmpt_t *comp; |
||
| 213 | jpc_enc_rlvl_t *lvl; |
||
| 214 | jpc_enc_band_t *band; |
||
| 215 | jpc_enc_band_t *endbands; |
||
| 216 | jpc_enc_cblk_t *cblk; |
||
| 217 | jpc_enc_cblk_t *endcblks; |
||
| 218 | jpc_bitstream_t *outb; |
||
| 219 | jpc_enc_pass_t *pass; |
||
| 220 | jpc_enc_pass_t *startpass; |
||
| 221 | jpc_enc_pass_t *lastpass; |
||
| 222 | jpc_enc_pass_t *endpass; |
||
| 223 | jpc_enc_pass_t *endpasses; |
||
| 224 | int i; |
||
| 225 | int included; |
||
| 226 | int ret; |
||
| 227 | jpc_tagtreenode_t *leaf; |
||
| 228 | int n; |
||
| 229 | int t1; |
||
| 230 | int t2; |
||
| 231 | int adjust; |
||
| 232 | int maxadjust; |
||
| 233 | int datalen; |
||
| 234 | int numnewpasses; |
||
| 235 | int passcount; |
||
| 236 | jpc_enc_tile_t *tile; |
||
| 237 | jpc_enc_prc_t *prc; |
||
| 238 | jpc_enc_cp_t *cp; |
||
| 239 | jpc_ms_t *ms; |
||
| 240 | |||
| 241 | tile = enc->curtile; |
||
| 242 | cp = enc->cp; |
||
| 243 | |||
| 244 | if (cp->tcp.csty & JPC_COD_SOP) { |
||
| 245 | if (!(ms = jpc_ms_create(JPC_MS_SOP))) { |
||
| 246 | return -1; |
||
| 247 | } |
||
| 248 | ms->parms.sop.seqno = jpc_pi_getind(tile->pi); |
||
| 249 | if (jpc_putms(out, enc->cstate, ms)) { |
||
| 250 | return -1; |
||
| 251 | } |
||
| 252 | jpc_ms_destroy(ms); |
||
| 253 | } |
||
| 254 | |||
| 255 | outb = jpc_bitstream_sopen(out, "w+"); |
||
| 256 | assert(outb); |
||
| 257 | |||
| 258 | if (jpc_bitstream_putbit(outb, 1) == EOF) { |
||
| 259 | return -1; |
||
| 260 | } |
||
| 261 | JAS_DBGLOG(10, ("\n")); |
||
| 262 | JAS_DBGLOG(10, ("present. ")); |
||
| 263 | |||
| 264 | comp = &tile->tcmpts[compno]; |
||
| 265 | lvl = &comp->rlvls[lvlno]; |
||
| 266 | endbands = &lvl->bands[lvl->numbands]; |
||
| 267 | for (band = lvl->bands; band != endbands; ++band) { |
||
| 268 | if (!band->data) { |
||
| 269 | continue; |
||
| 270 | } |
||
| 271 | prc = &band->prcs[prcno]; |
||
| 272 | if (!prc->cblks) { |
||
| 273 | continue; |
||
| 274 | } |
||
| 275 | |||
| 276 | endcblks = &prc->cblks[prc->numcblks]; |
||
| 277 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
||
| 278 | if (!lyrno) { |
||
| 279 | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
||
| 280 | jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs); |
||
| 281 | } |
||
| 282 | pass = cblk->curpass; |
||
| 283 | included = (pass && pass->lyrno == lyrno); |
||
| 284 | if (included && (!cblk->numencpasses)) { |
||
| 285 | assert(pass->lyrno == lyrno); |
||
| 286 | leaf = jpc_tagtree_getleaf(prc->incltree, |
||
| 287 | cblk - prc->cblks); |
||
| 288 | jpc_tagtree_setvalue(prc->incltree, leaf, pass->lyrno); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | endcblks = &prc->cblks[prc->numcblks]; |
||
| 293 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
||
| 294 | pass = cblk->curpass; |
||
| 295 | included = (pass && pass->lyrno == lyrno); |
||
| 296 | if (!cblk->numencpasses) { |
||
| 297 | leaf = jpc_tagtree_getleaf(prc->incltree, |
||
| 298 | cblk - prc->cblks); |
||
| 299 | if (jpc_tagtree_encode(prc->incltree, leaf, lyrno |
||
| 300 | + 1, outb) < 0) { |
||
| 301 | return -1; |
||
| 302 | } |
||
| 303 | } else { |
||
| 304 | if (jpc_bitstream_putbit(outb, included) == EOF) { |
||
| 305 | return -1; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | JAS_DBGLOG(10, ("included=%d ", included)); |
||
| 309 | if (!included) { |
||
| 310 | continue; |
||
| 311 | } |
||
| 312 | if (!cblk->numencpasses) { |
||
| 313 | i = 1; |
||
| 314 | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
||
| 315 | for (;;) { |
||
| 316 | if ((ret = jpc_tagtree_encode(prc->nlibtree, leaf, i, outb)) < 0) { |
||
| 317 | return -1; |
||
| 318 | } |
||
| 319 | if (ret) { |
||
| 320 | break; |
||
| 321 | } |
||
| 322 | ++i; |
||
| 323 | } |
||
| 324 | assert(leaf->known_ && i == leaf->value_ + 1); |
||
| 325 | } |
||
| 326 | |||
| 327 | endpasses = &cblk->passes[cblk->numpasses]; |
||
| 328 | startpass = pass; |
||
| 329 | endpass = startpass; |
||
| 330 | while (endpass != endpasses && endpass->lyrno == lyrno){ |
||
| 331 | ++endpass; |
||
| 332 | } |
||
| 333 | numnewpasses = endpass - startpass; |
||
| 334 | if (jpc_putnumnewpasses(outb, numnewpasses)) { |
||
| 335 | return -1; |
||
| 336 | } |
||
| 337 | JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses)); |
||
| 338 | |||
| 339 | lastpass = endpass - 1; |
||
| 340 | n = startpass->start; |
||
| 341 | passcount = 1; |
||
| 342 | maxadjust = 0; |
||
| 343 | for (pass = startpass; pass != endpass; ++pass) { |
||
| 344 | if (pass->term || pass == lastpass) { |
||
| 345 | datalen = pass->end - n; |
||
| 346 | t1 = jpc_firstone(datalen) + 1; |
||
| 347 | t2 = cblk->numlenbits + jpc_floorlog2(passcount); |
||
| 348 | adjust = JAS_MAX(t1 - t2, 0); |
||
| 349 | maxadjust = JAS_MAX(adjust, maxadjust); |
||
| 350 | n += datalen; |
||
| 351 | passcount = 1; |
||
| 352 | } else { |
||
| 353 | ++passcount; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | if (jpc_putcommacode(outb, maxadjust)) { |
||
| 357 | return -1; |
||
| 358 | } |
||
| 359 | cblk->numlenbits += maxadjust; |
||
| 360 | |||
| 361 | lastpass = endpass - 1; |
||
| 362 | n = startpass->start; |
||
| 363 | passcount = 1; |
||
| 364 | for (pass = startpass; pass != endpass; ++pass) { |
||
| 365 | if (pass->term || pass == lastpass) { |
||
| 366 | datalen = pass->end - n; |
||
| 367 | assert(jpc_firstone(datalen) < cblk->numlenbits + jpc_floorlog2(passcount)); |
||
| 368 | if (jpc_bitstream_putbits(outb, cblk->numlenbits + jpc_floorlog2(passcount), datalen) == EOF) { |
||
| 369 | return -1; |
||
| 370 | } |
||
| 371 | n += datalen; |
||
| 372 | passcount = 1; |
||
| 373 | } else { |
||
| 374 | ++passcount; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | jpc_bitstream_outalign(outb, 0); |
||
| 381 | jpc_bitstream_close(outb); |
||
| 382 | |||
| 383 | if (cp->tcp.csty & JPC_COD_EPH) { |
||
| 384 | if (!(ms = jpc_ms_create(JPC_MS_EPH))) { |
||
| 385 | return -1; |
||
| 386 | } |
||
| 387 | jpc_putms(out, enc->cstate, ms); |
||
| 388 | jpc_ms_destroy(ms); |
||
| 389 | } |
||
| 390 | |||
| 391 | comp = &tile->tcmpts[compno]; |
||
| 392 | lvl = &comp->rlvls[lvlno]; |
||
| 393 | endbands = &lvl->bands[lvl->numbands]; |
||
| 394 | for (band = lvl->bands; band != endbands; ++band) { |
||
| 395 | if (!band->data) { |
||
| 396 | continue; |
||
| 397 | } |
||
| 398 | prc = &band->prcs[prcno]; |
||
| 399 | if (!prc->cblks) { |
||
| 400 | continue; |
||
| 401 | } |
||
| 402 | endcblks = &prc->cblks[prc->numcblks]; |
||
| 403 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
||
| 404 | pass = cblk->curpass; |
||
| 405 | |||
| 406 | if (!pass) { |
||
| 407 | continue; |
||
| 408 | } |
||
| 409 | if (pass->lyrno != lyrno) { |
||
| 410 | assert(pass->lyrno < 0 || pass->lyrno > lyrno); |
||
| 411 | continue; |
||
| 412 | } |
||
| 413 | |||
| 414 | endpasses = &cblk->passes[cblk->numpasses]; |
||
| 415 | startpass = pass; |
||
| 416 | endpass = startpass; |
||
| 417 | while (endpass != endpasses && endpass->lyrno == lyrno){ |
||
| 418 | ++endpass; |
||
| 419 | } |
||
| 420 | lastpass = endpass - 1; |
||
| 421 | numnewpasses = endpass - startpass; |
||
| 422 | |||
| 423 | jas_stream_seek(cblk->stream, startpass->start, SEEK_SET); |
||
| 424 | assert(jas_stream_tell(cblk->stream) == startpass->start); |
||
| 425 | if (jas_stream_copy(out, cblk->stream, lastpass->end - startpass->start)) { |
||
| 426 | return -1; |
||
| 427 | } |
||
| 428 | cblk->curpass = (endpass != endpasses) ? endpass : 0; |
||
| 429 | cblk->numencpasses += numnewpasses; |
||
| 430 | |||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | return 0; |
||
| 435 | } |
||
| 436 | |||
| 437 | void jpc_save_t2state(jpc_enc_t *enc) |
||
| 438 | { |
||
| 439 | /* stream pos in embedded T1 stream may be wrong since not saved/restored! */ |
||
| 440 | |||
| 441 | jpc_enc_tcmpt_t *comp; |
||
| 442 | jpc_enc_tcmpt_t *endcomps; |
||
| 443 | jpc_enc_rlvl_t *lvl; |
||
| 444 | jpc_enc_rlvl_t *endlvls; |
||
| 445 | jpc_enc_band_t *band; |
||
| 446 | jpc_enc_band_t *endbands; |
||
| 447 | jpc_enc_cblk_t *cblk; |
||
| 448 | jpc_enc_cblk_t *endcblks; |
||
| 449 | jpc_enc_tile_t *tile; |
||
| 450 | int prcno; |
||
| 451 | jpc_enc_prc_t *prc; |
||
| 452 | |||
| 453 | tile = enc->curtile; |
||
| 454 | |||
| 455 | endcomps = &tile->tcmpts[tile->numtcmpts]; |
||
| 456 | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
||
| 457 | endlvls = &comp->rlvls[comp->numrlvls]; |
||
| 458 | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
||
| 459 | if (!lvl->bands) { |
||
| 460 | continue; |
||
| 461 | } |
||
| 462 | endbands = &lvl->bands[lvl->numbands]; |
||
| 463 | for (band = lvl->bands; band != endbands; ++band) { |
||
| 464 | if (!band->data) { |
||
| 465 | continue; |
||
| 466 | } |
||
| 467 | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
||
| 468 | if (!prc->cblks) { |
||
| 469 | continue; |
||
| 470 | } |
||
| 471 | jpc_tagtree_copy(prc->savincltree, prc->incltree); |
||
| 472 | jpc_tagtree_copy(prc->savnlibtree, prc->nlibtree); |
||
| 473 | endcblks = &prc->cblks[prc->numcblks]; |
||
| 474 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
||
| 475 | cblk->savedcurpass = cblk->curpass; |
||
| 476 | cblk->savednumencpasses = cblk->numencpasses; |
||
| 477 | cblk->savednumlenbits = cblk->numlenbits; |
||
| 478 | } |
||
| 479 | } |
||
| 480 | } |
||
| 481 | } |
||
| 482 | } |
||
| 483 | |||
| 484 | } |
||
| 485 | |||
| 486 | void jpc_restore_t2state(jpc_enc_t *enc) |
||
| 487 | { |
||
| 488 | |||
| 489 | jpc_enc_tcmpt_t *comp; |
||
| 490 | jpc_enc_tcmpt_t *endcomps; |
||
| 491 | jpc_enc_rlvl_t *lvl; |
||
| 492 | jpc_enc_rlvl_t *endlvls; |
||
| 493 | jpc_enc_band_t *band; |
||
| 494 | jpc_enc_band_t *endbands; |
||
| 495 | jpc_enc_cblk_t *cblk; |
||
| 496 | jpc_enc_cblk_t *endcblks; |
||
| 497 | jpc_enc_tile_t *tile; |
||
| 498 | int prcno; |
||
| 499 | jpc_enc_prc_t *prc; |
||
| 500 | |||
| 501 | tile = enc->curtile; |
||
| 502 | |||
| 503 | endcomps = &tile->tcmpts[tile->numtcmpts]; |
||
| 504 | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
||
| 505 | endlvls = &comp->rlvls[comp->numrlvls]; |
||
| 506 | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
||
| 507 | if (!lvl->bands) { |
||
| 508 | continue; |
||
| 509 | } |
||
| 510 | endbands = &lvl->bands[lvl->numbands]; |
||
| 511 | for (band = lvl->bands; band != endbands; ++band) { |
||
| 512 | if (!band->data) { |
||
| 513 | continue; |
||
| 514 | } |
||
| 515 | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
||
| 516 | if (!prc->cblks) { |
||
| 517 | continue; |
||
| 518 | } |
||
| 519 | jpc_tagtree_copy(prc->incltree, prc->savincltree); |
||
| 520 | jpc_tagtree_copy(prc->nlibtree, prc->savnlibtree); |
||
| 521 | endcblks = &prc->cblks[prc->numcblks]; |
||
| 522 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
||
| 523 | cblk->curpass = cblk->savedcurpass; |
||
| 524 | cblk->numencpasses = cblk->savednumencpasses; |
||
| 525 | cblk->numlenbits = cblk->savednumlenbits; |
||
| 526 | } |
||
| 527 | } |
||
| 528 | } |
||
| 529 | } |
||
| 530 | } |
||
| 531 | } |
||
| 532 | |||
| 533 | void jpc_init_t2state(jpc_enc_t *enc, int raflag) |
||
| 534 | { |
||
| 535 | /* It is assumed that band->numbps and cblk->numbps precomputed */ |
||
| 536 | |||
| 537 | jpc_enc_tcmpt_t *comp; |
||
| 538 | jpc_enc_tcmpt_t *endcomps; |
||
| 539 | jpc_enc_rlvl_t *lvl; |
||
| 540 | jpc_enc_rlvl_t *endlvls; |
||
| 541 | jpc_enc_band_t *band; |
||
| 542 | jpc_enc_band_t *endbands; |
||
| 543 | jpc_enc_cblk_t *cblk; |
||
| 544 | jpc_enc_cblk_t *endcblks; |
||
| 545 | jpc_enc_pass_t *pass; |
||
| 546 | jpc_enc_pass_t *endpasses; |
||
| 547 | jpc_tagtreenode_t *leaf; |
||
| 548 | jpc_enc_tile_t *tile; |
||
| 549 | int prcno; |
||
| 550 | jpc_enc_prc_t *prc; |
||
| 551 | |||
| 552 | tile = enc->curtile; |
||
| 553 | |||
| 554 | endcomps = &tile->tcmpts[tile->numtcmpts]; |
||
| 555 | for (comp = tile->tcmpts; comp != endcomps; ++comp) { |
||
| 556 | endlvls = &comp->rlvls[comp->numrlvls]; |
||
| 557 | for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { |
||
| 558 | if (!lvl->bands) { |
||
| 559 | continue; |
||
| 560 | } |
||
| 561 | endbands = &lvl->bands[lvl->numbands]; |
||
| 562 | for (band = lvl->bands; band != endbands; ++band) { |
||
| 563 | if (!band->data) { |
||
| 564 | continue; |
||
| 565 | } |
||
| 566 | for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { |
||
| 567 | if (!prc->cblks) { |
||
| 568 | continue; |
||
| 569 | } |
||
| 570 | jpc_tagtree_reset(prc->incltree); |
||
| 571 | jpc_tagtree_reset(prc->nlibtree); |
||
| 572 | endcblks = &prc->cblks[prc->numcblks]; |
||
| 573 | for (cblk = prc->cblks; cblk != endcblks; ++cblk) { |
||
| 574 | if (jas_stream_rewind(cblk->stream)) { |
||
| 575 | assert(0); |
||
| 576 | } |
||
| 577 | cblk->curpass = (cblk->numpasses > 0) ? cblk->passes : 0; |
||
| 578 | cblk->numencpasses = 0; |
||
| 579 | cblk->numlenbits = 3; |
||
| 580 | cblk->numimsbs = band->numbps - cblk->numbps; |
||
| 581 | assert(cblk->numimsbs >= 0); |
||
| 582 | leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); |
||
| 583 | jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs); |
||
| 584 | |||
| 585 | if (raflag) { |
||
| 586 | endpasses = &cblk->passes[cblk->numpasses]; |
||
| 587 | for (pass = cblk->passes; pass != endpasses; ++pass) { |
||
| 588 | pass->lyrno = -1; |
||
| 589 | pass->lyrno = 0; |
||
| 590 | } |
||
| 591 | } |
||
| 592 | } |
||
| 593 | } |
||
| 594 | } |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | } |
||
| 599 | |||
| 600 | jpc_pi_t *jpc_enc_pi_create(jpc_enc_cp_t *cp, jpc_enc_tile_t *tile) |
||
| 601 | { |
||
| 602 | jpc_pi_t *pi; |
||
| 603 | int compno; |
||
| 604 | jpc_picomp_t *picomp; |
||
| 605 | jpc_pirlvl_t *pirlvl; |
||
| 606 | jpc_enc_tcmpt_t *tcomp; |
||
| 607 | int rlvlno; |
||
| 608 | jpc_enc_rlvl_t *rlvl; |
||
| 609 | int prcno; |
||
| 610 | int *prclyrno; |
||
| 611 | |||
| 612 | if (!(pi = jpc_pi_create0())) { |
||
| 613 | return 0; |
||
| 614 | } |
||
| 615 | pi->pktno = -1; |
||
| 616 | pi->numcomps = cp->numcmpts; |
||
| 617 | if (!(pi->picomps = jas_malloc(pi->numcomps * sizeof(jpc_picomp_t)))) { |
||
| 618 | jpc_pi_destroy(pi); |
||
| 619 | return 0; |
||
| 620 | } |
||
| 621 | for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno, |
||
| 622 | ++picomp) { |
||
| 623 | picomp->pirlvls = 0; |
||
| 624 | } |
||
| 625 | |||
| 626 | for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps; |
||
| 627 | compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { |
||
| 628 | picomp->numrlvls = tcomp->numrlvls; |
||
| 629 | if (!(picomp->pirlvls = jas_malloc(picomp->numrlvls * |
||
| 630 | sizeof(jpc_pirlvl_t)))) { |
||
| 631 | jpc_pi_destroy(pi); |
||
| 632 | return 0; |
||
| 633 | } |
||
| 634 | for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < |
||
| 635 | picomp->numrlvls; ++rlvlno, ++pirlvl) { |
||
| 636 | pirlvl->prclyrnos = 0; |
||
| 637 | } |
||
| 638 | for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
||
| 639 | rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
||
| 640 | /* XXX sizeof(long) should be sizeof different type */ |
||
| 641 | pirlvl->numprcs = rlvl->numprcs; |
||
| 642 | if (rlvl->numprcs) { |
||
| 643 | if (!(pirlvl->prclyrnos = jas_malloc(pirlvl->numprcs * |
||
| 644 | sizeof(long)))) { |
||
| 645 | jpc_pi_destroy(pi); |
||
| 646 | return 0; |
||
| 647 | } |
||
| 648 | } else { |
||
| 649 | pirlvl->prclyrnos = 0; |
||
| 650 | } |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | pi->maxrlvls = 0; |
||
| 655 | for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps; |
||
| 656 | compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { |
||
| 657 | picomp->hsamp = cp->ccps[compno].sampgrdstepx; |
||
| 658 | picomp->vsamp = cp->ccps[compno].sampgrdstepy; |
||
| 659 | for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; |
||
| 660 | rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { |
||
| 661 | pirlvl->prcwidthexpn = rlvl->prcwidthexpn; |
||
| 662 | pirlvl->prcheightexpn = rlvl->prcheightexpn; |
||
| 663 | for (prcno = 0, prclyrno = pirlvl->prclyrnos; |
||
| 664 | prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { |
||
| 665 | *prclyrno = 0; |
||
| 666 | } |
||
| 667 | pirlvl->numhprcs = rlvl->numhprcs; |
||
| 668 | } |
||
| 669 | if (pi->maxrlvls < tcomp->numrlvls) { |
||
| 670 | pi->maxrlvls = tcomp->numrlvls; |
||
| 671 | } |
||
| 672 | } |
||
| 673 | |||
| 674 | pi->numlyrs = tile->numlyrs; |
||
| 675 | pi->xstart = tile->tlx; |
||
| 676 | pi->ystart = tile->tly; |
||
| 677 | pi->xend = tile->brx; |
||
| 678 | pi->yend = tile->bry; |
||
| 679 | |||
| 680 | pi->picomp = 0; |
||
| 681 | pi->pirlvl = 0; |
||
| 682 | pi->x = 0; |
||
| 683 | pi->y = 0; |
||
| 684 | pi->compno = 0; |
||
| 685 | pi->rlvlno = 0; |
||
| 686 | pi->prcno = 0; |
||
| 687 | pi->lyrno = 0; |
||
| 688 | pi->xstep = 0; |
||
| 689 | pi->ystep = 0; |
||
| 690 | |||
| 691 | pi->pchgno = -1; |
||
| 692 | |||
| 693 | pi->defaultpchg.prgord = tile->prg; |
||
| 694 | pi->defaultpchg.compnostart = 0; |
||
| 695 | pi->defaultpchg.compnoend = pi->numcomps; |
||
| 696 | pi->defaultpchg.rlvlnostart = 0; |
||
| 697 | pi->defaultpchg.rlvlnoend = pi->maxrlvls; |
||
| 698 | pi->defaultpchg.lyrnoend = pi->numlyrs; |
||
| 699 | pi->pchg = 0; |
||
| 700 | |||
| 701 | pi->valid = 0; |
||
| 702 | |||
| 703 | return pi; |
||
| 704 | } |