| 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 |
* Tier 1 Encoder |
| 115 |
* |
| 116 |
* $Id$ |
| 117 |
*/ |
| 118 |
|
| 119 |
/******************************************************************************\ |
| 120 |
* Includes. |
| 121 |
\******************************************************************************/ |
| 122 |
|
| 123 |
#include <stdio.h> |
| 124 |
#include <stdlib.h> |
| 125 |
#include <assert.h> |
| 126 |
|
| 127 |
#include "netpbm/nstring.h" |
| 128 |
|
| 129 |
#include "jasper/jas_malloc.h" |
| 130 |
#include "jasper/jas_math.h" |
| 131 |
|
| 132 |
#include "jpc_t1enc.h" |
| 133 |
#include "jpc_t1cod.h" |
| 134 |
#include "jpc_enc.h" |
| 135 |
#include "jpc_cod.h" |
| 136 |
#include "jpc_math.h" |
| 137 |
|
| 138 |
static int jpc_encsigpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int, |
| 139 |
jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec); |
| 140 |
|
| 141 |
static int jpc_encrefpass(jpc_mqenc_t *mqenc, int bitpos, int, jas_matrix_t *flags, |
| 142 |
jas_matrix_t *data, int term, long *nmsedec); |
| 143 |
|
| 144 |
static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int, |
| 145 |
int, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec); |
| 146 |
|
| 147 |
static int jpc_encrawsigpass(jpc_bitstream_t *out, int bitpos, int, |
| 148 |
jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec); |
| 149 |
|
| 150 |
static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int, |
| 151 |
jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec); |
| 152 |
|
| 153 |
/*****************************************************************************\ |
| 154 |
* Code for encoding code blocks. |
| 155 |
\*****************************************************************************/ |
| 156 |
|
| 157 |
static void |
| 158 |
encodeBlocksOfPrecinct(jpc_enc_prc_t * const prcP, |
| 159 |
jpc_enc_band_t * const bandP, |
| 160 |
jpc_enc_tcmpt_t * const tcmptP, |
| 161 |
jpc_enc_t * const encoderP, |
| 162 |
const char ** const errorP) { |
| 163 |
|
| 164 |
if (prcP->cblks) { |
| 165 |
int bmx; |
| 166 |
uint_fast32_t cblkno; |
| 167 |
|
| 168 |
for (cblkno = 0, bmx = 0; cblkno < prcP->numcblks; ++ cblkno) { |
| 169 |
jpc_enc_cblk_t * const cblkP = &prcP->cblks[cblkno]; |
| 170 |
|
| 171 |
int mx; |
| 172 |
uint_fast32_t row; |
| 173 |
|
| 174 |
for (row = 0, mx = 0; |
| 175 |
row < jas_matrix_numrows(cblkP->data); |
| 176 |
++row) { |
| 177 |
|
| 178 |
uint_fast32_t col; |
| 179 |
|
| 180 |
for (col = 0; col < jas_matrix_numcols(cblkP->data); ++col) { |
| 181 |
int const v = abs(jas_matrix_get(cblkP->data, row, col)); |
| 182 |
if (v > mx) |
| 183 |
mx = v; |
| 184 |
} |
| 185 |
} |
| 186 |
if (mx > bmx) |
| 187 |
bmx = mx; |
| 188 |
|
| 189 |
cblkP->numbps = MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0); |
| 190 |
} |
| 191 |
|
| 192 |
for (cblkno = 0; cblkno < prcP->numcblks; ++ cblkno) |
| 193 |
assert(prcP->cblks[cblkno].numbps <= bandP->numbps); |
| 194 |
|
| 195 |
for (cblkno = 0, *errorP = NULL; |
| 196 |
cblkno < prcP->numcblks && !*errorP; |
| 197 |
++ cblkno) { |
| 198 |
|
| 199 |
jpc_enc_cblk_t * const cblkP = &prcP->cblks[cblkno]; |
| 200 |
|
| 201 |
int rc; |
| 202 |
|
| 203 |
rc = jpc_enc_enccblk(encoderP, |
| 204 |
cblkP->stream, tcmptP, bandP, cblkP); |
| 205 |
if (rc != 0) |
| 206 |
pm_asprintf(errorP, "Encoding failed on code block %u " |
| 207 |
"of %u", (unsigned)cblkno, |
| 208 |
(unsigned)prcP->numcblks); |
| 209 |
} |
| 210 |
} else |
| 211 |
*errorP = NULL; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
int |
| 217 |
jpc_enc_enccblks(jpc_enc_t * const encoderP) { |
| 218 |
/*---------------------------------------------------------------------------- |
| 219 |
Encode all of the code blocks associated with the current tile. |
| 220 |
-----------------------------------------------------------------------------*/ |
| 221 |
jpc_enc_tile_t * const tileP = encoderP->curtile; |
| 222 |
|
| 223 |
uint_fast32_t cmptno; |
| 224 |
|
| 225 |
for (cmptno = 0; cmptno < tileP->numtcmpts; ++cmptno) { |
| 226 |
jpc_enc_tcmpt_t * const tcmptP = &tileP->tcmpts[cmptno]; |
| 227 |
|
| 228 |
unsigned int lvlno; |
| 229 |
for (lvlno = 0; lvlno < tcmptP->numrlvls; ++ lvlno) { |
| 230 |
jpc_enc_rlvl_t * const lvlP = &tcmptP->rlvls[lvlno]; |
| 231 |
|
| 232 |
if (lvlP->bands) { |
| 233 |
uint_fast32_t bandno; |
| 234 |
|
| 235 |
for (bandno = 0; bandno < lvlP->numbands; ++bandno) { |
| 236 |
jpc_enc_band_t * const bandP = &lvlP->bands[bandno]; |
| 237 |
|
| 238 |
if (bandP->data) { |
| 239 |
uint_fast32_t prcno; |
| 240 |
|
| 241 |
for (prcno = 0; prcno < lvlP->numprcs; ++prcno) { |
| 242 |
|
| 243 |
const char * error; |
| 244 |
|
| 245 |
encodeBlocksOfPrecinct(&bandP->prcs[prcno], |
| 246 |
bandP, |
| 247 |
tcmptP, |
| 248 |
encoderP, |
| 249 |
&error); |
| 250 |
|
| 251 |
if (error) { |
| 252 |
pm_strfree(error); |
| 253 |
return -1; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static int getthebyte(jas_stream_t *in, long off) |
| 267 |
{ |
| 268 |
int c; |
| 269 |
long oldpos; |
| 270 |
oldpos = jas_stream_tell(in); |
| 271 |
assert(oldpos >= 0); |
| 272 |
jas_stream_seek(in, off, SEEK_SET); |
| 273 |
c = jas_stream_peekc(in); |
| 274 |
jas_stream_seek(in, oldpos, SEEK_SET); |
| 275 |
return c; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
int |
| 281 |
jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *tcmpt, |
| 282 |
jpc_enc_band_t *band, jpc_enc_cblk_t *cblk) { |
| 283 |
/*---------------------------------------------------------------------------- |
| 284 |
Encode a single code block. |
| 285 |
-----------------------------------------------------------------------------*/ |
| 286 |
jpc_enc_pass_t *pass; |
| 287 |
jpc_enc_pass_t *endpasses; |
| 288 |
int bitpos; |
| 289 |
int n; |
| 290 |
int adjust; |
| 291 |
int passtype; |
| 292 |
int t; |
| 293 |
jpc_bitstream_t *bout; |
| 294 |
jpc_enc_pass_t *termpass; |
| 295 |
int vcausal; |
| 296 |
int segsym; |
| 297 |
int termmode; |
| 298 |
int c; |
| 299 |
|
| 300 |
bout = 0; |
| 301 |
|
| 302 |
cblk->stream = jas_stream_memopen(0, 0); |
| 303 |
assert(cblk->stream); |
| 304 |
cblk->mqenc = jpc_mqenc_create(JPC_NUMCTXS, cblk->stream); |
| 305 |
assert(cblk->mqenc); |
| 306 |
jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); |
| 307 |
|
| 308 |
cblk->numpasses = (cblk->numbps > 0) ? (3 * cblk->numbps - 2) : 0; |
| 309 |
if (cblk->numpasses > 0) { |
| 310 |
cblk->passes = jas_malloc(cblk->numpasses * sizeof(jpc_enc_pass_t)); |
| 311 |
assert(cblk->passes); |
| 312 |
} else { |
| 313 |
cblk->passes = 0; |
| 314 |
} |
| 315 |
endpasses = &cblk->passes[cblk->numpasses]; |
| 316 |
for (pass = cblk->passes; pass != endpasses; ++pass) { |
| 317 |
pass->start = 0; |
| 318 |
pass->end = 0; |
| 319 |
pass->term = |
| 320 |
JPC_ISTERMINATED(pass - cblk->passes, 0, |
| 321 |
cblk->numpasses, |
| 322 |
(tcmpt->cblksty & JPC_COX_TERMALL) != 0, |
| 323 |
(tcmpt->cblksty & JPC_COX_LAZY) != 0); |
| 324 |
pass->type = JPC_SEGTYPE(pass - cblk->passes, 0, |
| 325 |
(tcmpt->cblksty & JPC_COX_LAZY) != 0); |
| 326 |
pass->lyrno = -1; |
| 327 |
if (pass == endpasses - 1) { |
| 328 |
assert(pass->term == 1); |
| 329 |
pass->term = 1; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2, |
| 334 |
jas_matrix_numcols(cblk->data) + 2); |
| 335 |
assert(cblk->flags); |
| 336 |
|
| 337 |
|
| 338 |
bitpos = cblk->numbps - 1; |
| 339 |
pass = cblk->passes; |
| 340 |
n = cblk->numpasses; |
| 341 |
while (--n >= 0) { |
| 342 |
|
| 343 |
if (pass->type == JPC_SEG_MQ) { |
| 344 |
/* NOP */ |
| 345 |
} else { |
| 346 |
assert(pass->type == JPC_SEG_RAW); |
| 347 |
if (!bout) { |
| 348 |
bout = jpc_bitstream_sopen(cblk->stream, "w"); |
| 349 |
assert(bout); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
#if 1 |
| 354 |
passtype = (pass - cblk->passes + 2) % 3; |
| 355 |
#else |
| 356 |
passtype = JPC_PASSTYPE(pass - cblk->passes + 2); |
| 357 |
#endif |
| 358 |
pass->start = jas_stream_tell(cblk->stream); |
| 359 |
#if 0 |
| 360 |
assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream)); |
| 361 |
#endif |
| 362 |
assert(bitpos >= 0); |
| 363 |
vcausal = (tcmpt->cblksty & JPC_COX_VSC) != 0; |
| 364 |
segsym = (tcmpt->cblksty & JPC_COX_SEGSYM) != 0; |
| 365 |
if (pass->term) { |
| 366 |
termmode = ((tcmpt->cblksty & JPC_COX_PTERM) ? |
| 367 |
JPC_MQENC_PTERM : JPC_MQENC_DEFTERM) + 1; |
| 368 |
} else { |
| 369 |
termmode = 0; |
| 370 |
} |
| 371 |
switch (passtype) { |
| 372 |
case JPC_SIGPASS: |
| 373 |
if (pass->type == JPC_SEG_MQ) |
| 374 |
jpc_encsigpass(cblk->mqenc, |
| 375 |
bitpos, band->orient, vcausal, cblk->flags, |
| 376 |
cblk->data, termmode, &pass->nmsedec); |
| 377 |
else |
| 378 |
jpc_encrawsigpass(bout, bitpos, vcausal, cblk->flags, |
| 379 |
cblk->data, termmode, &pass->nmsedec); |
| 380 |
break; |
| 381 |
case JPC_REFPASS: |
| 382 |
if (pass->type == JPC_SEG_MQ) |
| 383 |
jpc_encrefpass( |
| 384 |
cblk->mqenc, |
| 385 |
bitpos, vcausal, cblk->flags, cblk->data, termmode, |
| 386 |
&pass->nmsedec); |
| 387 |
else |
| 388 |
jpc_encrawrefpass(bout, bitpos, |
| 389 |
vcausal, cblk->flags, cblk->data, termmode, |
| 390 |
&pass->nmsedec); |
| 391 |
break; |
| 392 |
case JPC_CLNPASS: |
| 393 |
assert(pass->type == JPC_SEG_MQ); |
| 394 |
jpc_encclnpass( |
| 395 |
cblk->mqenc, bitpos, band->orient, |
| 396 |
vcausal, segsym, cblk->flags, cblk->data, termmode, |
| 397 |
&pass->nmsedec); |
| 398 |
break; |
| 399 |
default: |
| 400 |
assert(false); |
| 401 |
break; |
| 402 |
} |
| 403 |
|
| 404 |
if (pass->type == JPC_SEG_MQ) { |
| 405 |
if (pass->term) { |
| 406 |
jpc_mqenc_init(cblk->mqenc); |
| 407 |
} |
| 408 |
jpc_mqenc_getstate(cblk->mqenc, &pass->mqencstate); |
| 409 |
pass->end = jas_stream_tell(cblk->stream); |
| 410 |
if (tcmpt->cblksty & JPC_COX_RESET) { |
| 411 |
jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); |
| 412 |
} |
| 413 |
} else { |
| 414 |
if (pass->term) { |
| 415 |
if (jpc_bitstream_pending(bout)) { |
| 416 |
jpc_bitstream_outalign(bout, 0x2a); |
| 417 |
} |
| 418 |
jpc_bitstream_close(bout); |
| 419 |
bout = 0; |
| 420 |
pass->end = jas_stream_tell(cblk->stream); |
| 421 |
} else { |
| 422 |
pass->end = jas_stream_tell(cblk->stream) + |
| 423 |
jpc_bitstream_pending(bout); |
| 424 |
/* NOTE - This will not work. need to adjust by # of pending |
| 425 |
output bytes |
| 426 |
*/ |
| 427 |
} |
| 428 |
} |
| 429 |
#if 0 |
| 430 |
/* XXX - This assertion fails sometimes when various coding modes are used. |
| 431 |
This seems to be harmless, but why does it happen at all? */ |
| 432 |
assert(jas_stream_tell(cblk->stream) == |
| 433 |
jas_stream_getrwcount(cblk->stream)); |
| 434 |
#endif |
| 435 |
|
| 436 |
pass->wmsedec = jpc_fixtodbl(band->rlvl->tcmpt->synweight) * |
| 437 |
jpc_fixtodbl(band->rlvl->tcmpt->synweight) * |
| 438 |
jpc_fixtodbl(band->synweight) * |
| 439 |
jpc_fixtodbl(band->synweight) * |
| 440 |
jpc_fixtodbl(band->absstepsize) * jpc_fixtodbl(band->absstepsize) * |
| 441 |
((double) (1 << bitpos)) * ((double)(1 << bitpos)) * |
| 442 |
jpc_fixtodbl(pass->nmsedec); |
| 443 |
pass->cumwmsedec = pass->wmsedec; |
| 444 |
if (pass != cblk->passes) { |
| 445 |
pass->cumwmsedec += pass[-1].cumwmsedec; |
| 446 |
} |
| 447 |
if (passtype == JPC_CLNPASS) { |
| 448 |
--bitpos; |
| 449 |
} |
| 450 |
++pass; |
| 451 |
} |
| 452 |
|
| 453 |
#if 0 |
| 454 |
dump_passes(cblk->passes, cblk->numpasses, cblk); |
| 455 |
#endif |
| 456 |
|
| 457 |
n = 0; |
| 458 |
endpasses = &cblk->passes[cblk->numpasses]; |
| 459 |
for (pass = cblk->passes; pass != endpasses; ++pass) { |
| 460 |
if (pass->start < n) { |
| 461 |
pass->start = n; |
| 462 |
} |
| 463 |
if (pass->end < n) { |
| 464 |
pass->end = n; |
| 465 |
} |
| 466 |
if (!pass->term) { |
| 467 |
termpass = pass; |
| 468 |
while (termpass - pass < cblk->numpasses && |
| 469 |
!termpass->term) { |
| 470 |
++termpass; |
| 471 |
} |
| 472 |
if (pass->type == JPC_SEG_MQ) { |
| 473 |
t = (pass->mqencstate.lastbyte == 0xff) ? 1 : 0; |
| 474 |
if (pass->mqencstate.ctreg >= 5) { |
| 475 |
adjust = 4 + t; |
| 476 |
} else { |
| 477 |
adjust = 5 + t; |
| 478 |
} |
| 479 |
pass->end += adjust; |
| 480 |
} |
| 481 |
if (pass->end > termpass->end) { |
| 482 |
pass->end = termpass->end; |
| 483 |
} |
| 484 |
if ((c = getthebyte(cblk->stream, pass->end - 1)) == EOF) { |
| 485 |
abort(); |
| 486 |
} |
| 487 |
if (c == 0xff) { |
| 488 |
++pass->end; |
| 489 |
} |
| 490 |
n = JAS_MAX(n, pass->end); |
| 491 |
} else { |
| 492 |
n = JAS_MAX(n, pass->end); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
#if 0 |
| 497 |
dump_passes(cblk->passes, cblk->numpasses, cblk); |
| 498 |
#endif |
| 499 |
|
| 500 |
if (bout) { |
| 501 |
jpc_bitstream_close(bout); |
| 502 |
} |
| 503 |
|
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
/*****************************************************************************\ |
| 510 |
* Code for significance pass. |
| 511 |
\*****************************************************************************/ |
| 512 |
|
| 513 |
#define sigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, orient, mqenc, vcausalflag) \ |
| 514 |
{ \ |
| 515 |
int f; \ |
| 516 |
int v; \ |
| 517 |
f = *(fp); \ |
| 518 |
if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ |
| 519 |
v = (abs(*(dp)) & (one)) ? 1 : 0; \ |
| 520 |
jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \ |
| 521 |
jpc_mqenc_putbit(mqenc, v); \ |
| 522 |
if (v) { \ |
| 523 |
*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ |
| 524 |
v = ((*(dp) < 0) ? 1 : 0); \ |
| 525 |
jpc_mqenc_setcurctx(mqenc, JPC_GETSCCTXNO(f)); \ |
| 526 |
jpc_mqenc_putbit(mqenc, v ^ JPC_GETSPB(f)); \ |
| 527 |
JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \ |
| 528 |
*(fp) |= JPC_SIG; \ |
| 529 |
} \ |
| 530 |
*(fp) |= JPC_VISIT; \ |
| 531 |
} \ |
| 532 |
} |
| 533 |
|
| 534 |
static int jpc_encsigpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, |
| 535 |
jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) |
| 536 |
{ |
| 537 |
int i; |
| 538 |
int j; |
| 539 |
int one; |
| 540 |
int vscanlen; |
| 541 |
int width; |
| 542 |
int height; |
| 543 |
int frowstep; |
| 544 |
int drowstep; |
| 545 |
int fstripestep; |
| 546 |
int dstripestep; |
| 547 |
jpc_fix_t *fstripestart; |
| 548 |
jpc_fix_t *dstripestart; |
| 549 |
jpc_fix_t *fp; |
| 550 |
jpc_fix_t *dp; |
| 551 |
jpc_fix_t *fvscanstart; |
| 552 |
jpc_fix_t *dvscanstart; |
| 553 |
int k; |
| 554 |
|
| 555 |
*nmsedec = 0; |
| 556 |
width = jas_matrix_numcols(data); |
| 557 |
height = jas_matrix_numrows(data); |
| 558 |
frowstep = jas_matrix_rowstep(flags); |
| 559 |
drowstep = jas_matrix_rowstep(data); |
| 560 |
fstripestep = frowstep << 2; |
| 561 |
dstripestep = drowstep << 2; |
| 562 |
|
| 563 |
one = 1 << (bitpos + JPC_NUMEXTRABITS); |
| 564 |
|
| 565 |
fstripestart = jas_matrix_getref(flags, 1, 1); |
| 566 |
dstripestart = jas_matrix_getref(data, 0, 0); |
| 567 |
for (i = height; i > 0; i -= 4, fstripestart += fstripestep, |
| 568 |
dstripestart += dstripestep) { |
| 569 |
fvscanstart = fstripestart; |
| 570 |
dvscanstart = dstripestart; |
| 571 |
vscanlen = JAS_MIN(i, 4); |
| 572 |
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { |
| 573 |
fp = fvscanstart; |
| 574 |
dp = dvscanstart; |
| 575 |
k = vscanlen; |
| 576 |
|
| 577 |
sigpass_step(fp, frowstep, dp, bitpos, one, |
| 578 |
nmsedec, orient, mqenc, vcausalflag); |
| 579 |
if (--k <= 0) { |
| 580 |
continue; |
| 581 |
} |
| 582 |
fp += frowstep; |
| 583 |
dp += drowstep; |
| 584 |
sigpass_step(fp, frowstep, dp, bitpos, one, |
| 585 |
nmsedec, orient, mqenc, 0); |
| 586 |
if (--k <= 0) { |
| 587 |
continue; |
| 588 |
} |
| 589 |
fp += frowstep; |
| 590 |
dp += drowstep; |
| 591 |
sigpass_step(fp, frowstep, dp, bitpos, one, |
| 592 |
nmsedec, orient, mqenc, 0); |
| 593 |
if (--k <= 0) { |
| 594 |
continue; |
| 595 |
} |
| 596 |
fp += frowstep; |
| 597 |
dp += drowstep; |
| 598 |
sigpass_step(fp, frowstep, dp, bitpos, one, |
| 599 |
nmsedec, orient, mqenc, 0); |
| 600 |
|
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
if (term) { |
| 605 |
jpc_mqenc_flush(mqenc, term - 1); |
| 606 |
} |
| 607 |
|
| 608 |
return jpc_mqenc_error(mqenc) ? (-1) : 0; |
| 609 |
} |
| 610 |
|
| 611 |
#define rawsigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, out, vcausalflag) \ |
| 612 |
{ \ |
| 613 |
jpc_fix_t f = *(fp); \ |
| 614 |
jpc_fix_t v; \ |
| 615 |
if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ |
| 616 |
v = (abs(*(dp)) & (one)) ? 1 : 0; \ |
| 617 |
if ((jpc_bitstream_putbit((out), v)) == EOF) { \ |
| 618 |
return -1; \ |
| 619 |
} \ |
| 620 |
if (v) { \ |
| 621 |
*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ |
| 622 |
v = ((*(dp) < 0) ? 1 : 0); \ |
| 623 |
if (jpc_bitstream_putbit(out, v) == EOF) { \ |
| 624 |
return -1; \ |
| 625 |
} \ |
| 626 |
JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \ |
| 627 |
*(fp) |= JPC_SIG; \ |
| 628 |
} \ |
| 629 |
*(fp) |= JPC_VISIT; \ |
| 630 |
} \ |
| 631 |
} |
| 632 |
|
| 633 |
static int jpc_encrawsigpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags, |
| 634 |
jas_matrix_t *data, int term, long *nmsedec) |
| 635 |
{ |
| 636 |
int i; |
| 637 |
int j; |
| 638 |
int k; |
| 639 |
int one; |
| 640 |
int vscanlen; |
| 641 |
int width; |
| 642 |
int height; |
| 643 |
int frowstep; |
| 644 |
int drowstep; |
| 645 |
int fstripestep; |
| 646 |
int dstripestep; |
| 647 |
jpc_fix_t *fstripestart; |
| 648 |
jpc_fix_t *dstripestart; |
| 649 |
jpc_fix_t *fp; |
| 650 |
jpc_fix_t *dp; |
| 651 |
jpc_fix_t *fvscanstart; |
| 652 |
jpc_fix_t *dvscanstart; |
| 653 |
|
| 654 |
*nmsedec = 0; |
| 655 |
width = jas_matrix_numcols(data); |
| 656 |
height = jas_matrix_numrows(data); |
| 657 |
frowstep = jas_matrix_rowstep(flags); |
| 658 |
drowstep = jas_matrix_rowstep(data); |
| 659 |
fstripestep = frowstep << 2; |
| 660 |
dstripestep = drowstep << 2; |
| 661 |
|
| 662 |
one = 1 << (bitpos + JPC_NUMEXTRABITS); |
| 663 |
|
| 664 |
fstripestart = jas_matrix_getref(flags, 1, 1); |
| 665 |
dstripestart = jas_matrix_getref(data, 0, 0); |
| 666 |
for (i = height; i > 0; i -= 4, fstripestart += fstripestep, |
| 667 |
dstripestart += dstripestep) { |
| 668 |
fvscanstart = fstripestart; |
| 669 |
dvscanstart = dstripestart; |
| 670 |
vscanlen = JAS_MIN(i, 4); |
| 671 |
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { |
| 672 |
fp = fvscanstart; |
| 673 |
dp = dvscanstart; |
| 674 |
k = vscanlen; |
| 675 |
|
| 676 |
rawsigpass_step(fp, frowstep, dp, bitpos, one, |
| 677 |
nmsedec, out, vcausalflag); |
| 678 |
if (--k <= 0) { |
| 679 |
continue; |
| 680 |
} |
| 681 |
fp += frowstep; |
| 682 |
dp += drowstep; |
| 683 |
|
| 684 |
rawsigpass_step(fp, frowstep, dp, bitpos, one, |
| 685 |
nmsedec, out, 0); |
| 686 |
if (--k <= 0) { |
| 687 |
continue; |
| 688 |
} |
| 689 |
fp += frowstep; |
| 690 |
dp += drowstep; |
| 691 |
|
| 692 |
rawsigpass_step(fp, frowstep, dp, bitpos, one, |
| 693 |
nmsedec, out, 0); |
| 694 |
if (--k <= 0) { |
| 695 |
continue; |
| 696 |
} |
| 697 |
fp += frowstep; |
| 698 |
dp += drowstep; |
| 699 |
|
| 700 |
rawsigpass_step(fp, frowstep, dp, bitpos, one, |
| 701 |
nmsedec, out, 0); |
| 702 |
if (--k <= 0) { |
| 703 |
continue; |
| 704 |
} |
| 705 |
fp += frowstep; |
| 706 |
dp += drowstep; |
| 707 |
|
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
if (term) { |
| 712 |
jpc_bitstream_outalign(out, 0x2a); |
| 713 |
} |
| 714 |
|
| 715 |
return 0; |
| 716 |
} |
| 717 |
|
| 718 |
/******************************************************************************\ |
| 719 |
* Code for refinement pass. |
| 720 |
\******************************************************************************/ |
| 721 |
|
| 722 |
#define refpass_step(fp, dp, bitpos, one, nmsedec, mqenc, vcausalflag) \ |
| 723 |
{ \ |
| 724 |
int v; \ |
| 725 |
if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ |
| 726 |
(d) = *(dp); \ |
| 727 |
*(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \ |
| 728 |
jpc_mqenc_setcurctx((mqenc), JPC_GETMAGCTXNO(*(fp))); \ |
| 729 |
v = (abs(d) & (one)) ? 1 : 0; \ |
| 730 |
jpc_mqenc_putbit((mqenc), v); \ |
| 731 |
*(fp) |= JPC_REFINE; \ |
| 732 |
} \ |
| 733 |
} |
| 734 |
|
| 735 |
static int jpc_encrefpass(jpc_mqenc_t *mqenc, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data, |
| 736 |
int term, long *nmsedec) |
| 737 |
{ |
| 738 |
int i; |
| 739 |
int j; |
| 740 |
int one; |
| 741 |
int vscanlen; |
| 742 |
int d; |
| 743 |
int width; |
| 744 |
int height; |
| 745 |
int frowstep; |
| 746 |
int drowstep; |
| 747 |
int fstripestep; |
| 748 |
int dstripestep; |
| 749 |
jpc_fix_t *fstripestart; |
| 750 |
jpc_fix_t *dstripestart; |
| 751 |
jpc_fix_t *fvscanstart; |
| 752 |
jpc_fix_t *dvscanstart; |
| 753 |
jpc_fix_t *dp; |
| 754 |
jpc_fix_t *fp; |
| 755 |
int k; |
| 756 |
|
| 757 |
*nmsedec = 0; |
| 758 |
width = jas_matrix_numcols(data); |
| 759 |
height = jas_matrix_numrows(data); |
| 760 |
frowstep = jas_matrix_rowstep(flags); |
| 761 |
drowstep = jas_matrix_rowstep(data); |
| 762 |
fstripestep = frowstep << 2; |
| 763 |
dstripestep = drowstep << 2; |
| 764 |
|
| 765 |
one = 1 << (bitpos + JPC_NUMEXTRABITS); |
| 766 |
|
| 767 |
fstripestart = jas_matrix_getref(flags, 1, 1); |
| 768 |
dstripestart = jas_matrix_getref(data, 0, 0); |
| 769 |
for (i = height; i > 0; i -= 4, fstripestart += fstripestep, |
| 770 |
dstripestart += dstripestep) { |
| 771 |
fvscanstart = fstripestart; |
| 772 |
dvscanstart = dstripestart; |
| 773 |
vscanlen = JAS_MIN(i, 4); |
| 774 |
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { |
| 775 |
fp = fvscanstart; |
| 776 |
dp = dvscanstart; |
| 777 |
k = vscanlen; |
| 778 |
|
| 779 |
refpass_step(fp, dp, bitpos, one, nmsedec, |
| 780 |
mqenc, vcausalflag); |
| 781 |
if (--k <= 0) { |
| 782 |
continue; |
| 783 |
} |
| 784 |
fp += frowstep; |
| 785 |
dp += drowstep; |
| 786 |
refpass_step(fp, dp, bitpos, one, nmsedec, |
| 787 |
mqenc, 0); |
| 788 |
if (--k <= 0) { |
| 789 |
continue; |
| 790 |
} |
| 791 |
fp += frowstep; |
| 792 |
dp += drowstep; |
| 793 |
refpass_step(fp, dp, bitpos, one, nmsedec, |
| 794 |
mqenc, 0); |
| 795 |
if (--k <= 0) { |
| 796 |
continue; |
| 797 |
} |
| 798 |
fp += frowstep; |
| 799 |
dp += drowstep; |
| 800 |
refpass_step(fp, dp, bitpos, one, nmsedec, |
| 801 |
mqenc, 0); |
| 802 |
|
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
if (term) { |
| 807 |
jpc_mqenc_flush(mqenc, term - 1); |
| 808 |
} |
| 809 |
|
| 810 |
return jpc_mqenc_error(mqenc) ? (-1) : 0; |
| 811 |
} |
| 812 |
|
| 813 |
#define rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag) \ |
| 814 |
{ \ |
| 815 |
jpc_fix_t d; \ |
| 816 |
jpc_fix_t v; \ |
| 817 |
if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ |
| 818 |
d = *(dp); \ |
| 819 |
*(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \ |
| 820 |
v = (abs(d) & (one)) ? 1 : 0; \ |
| 821 |
if (jpc_bitstream_putbit((out), v) == EOF) { \ |
| 822 |
return -1; \ |
| 823 |
} \ |
| 824 |
*(fp) |= JPC_REFINE; \ |
| 825 |
} \ |
| 826 |
} |
| 827 |
|
| 828 |
static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags, |
| 829 |
jas_matrix_t *data, int term, long *nmsedec) |
| 830 |
{ |
| 831 |
int i; |
| 832 |
int j; |
| 833 |
int k; |
| 834 |
int one; |
| 835 |
int vscanlen; |
| 836 |
int width; |
| 837 |
int height; |
| 838 |
int frowstep; |
| 839 |
int drowstep; |
| 840 |
int fstripestep; |
| 841 |
int dstripestep; |
| 842 |
jpc_fix_t *fstripestart; |
| 843 |
jpc_fix_t *dstripestart; |
| 844 |
jpc_fix_t *fvscanstart; |
| 845 |
jpc_fix_t *dvscanstart; |
| 846 |
jpc_fix_t *dp; |
| 847 |
jpc_fix_t *fp; |
| 848 |
|
| 849 |
*nmsedec = 0; |
| 850 |
width = jas_matrix_numcols(data); |
| 851 |
height = jas_matrix_numrows(data); |
| 852 |
frowstep = jas_matrix_rowstep(flags); |
| 853 |
drowstep = jas_matrix_rowstep(data); |
| 854 |
fstripestep = frowstep << 2; |
| 855 |
dstripestep = drowstep << 2; |
| 856 |
|
| 857 |
one = 1 << (bitpos + JPC_NUMEXTRABITS); |
| 858 |
|
| 859 |
fstripestart = jas_matrix_getref(flags, 1, 1); |
| 860 |
dstripestart = jas_matrix_getref(data, 0, 0); |
| 861 |
for (i = height; i > 0; i -= 4, fstripestart += fstripestep, |
| 862 |
dstripestart += dstripestep) { |
| 863 |
fvscanstart = fstripestart; |
| 864 |
dvscanstart = dstripestart; |
| 865 |
vscanlen = JAS_MIN(i, 4); |
| 866 |
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { |
| 867 |
fp = fvscanstart; |
| 868 |
dp = dvscanstart; |
| 869 |
k = vscanlen; |
| 870 |
|
| 871 |
rawrefpass_step(fp, dp, bitpos, one, nmsedec, |
| 872 |
out, vcausalflag); |
| 873 |
if (--k <= 0) { |
| 874 |
continue; |
| 875 |
} |
| 876 |
fp += frowstep; |
| 877 |
dp += drowstep; |
| 878 |
rawrefpass_step(fp, dp, bitpos, one, nmsedec, |
| 879 |
out, vcausalflag); |
| 880 |
if (--k <= 0) { |
| 881 |
continue; |
| 882 |
} |
| 883 |
fp += frowstep; |
| 884 |
dp += drowstep; |
| 885 |
rawrefpass_step(fp, dp, bitpos, one, nmsedec, |
| 886 |
out, vcausalflag); |
| 887 |
if (--k <= 0) { |
| 888 |
continue; |
| 889 |
} |
| 890 |
fp += frowstep; |
| 891 |
dp += drowstep; |
| 892 |
rawrefpass_step(fp, dp, bitpos, one, nmsedec, |
| 893 |
out, vcausalflag); |
| 894 |
|
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
if (term) { |
| 899 |
jpc_bitstream_outalign(out, 0x2a); |
| 900 |
} |
| 901 |
|
| 902 |
return 0; |
| 903 |
} |
| 904 |
|
| 905 |
/******************************************************************************\ |
| 906 |
* Code for cleanup pass. |
| 907 |
\******************************************************************************/ |
| 908 |
|
| 909 |
#define clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, label1, label2, vcausalflag) \ |
| 910 |
{ \ |
| 911 |
int f; \ |
| 912 |
int v; \ |
| 913 |
label1 \ |
| 914 |
f = *(fp); \ |
| 915 |
if (!(f & (JPC_SIG | JPC_VISIT))) { \ |
| 916 |
jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \ |
| 917 |
v = (abs(*(dp)) & (one)) ? 1 : 0; \ |
| 918 |
jpc_mqenc_putbit((mqenc), v); \ |
| 919 |
if (v) { \ |
| 920 |
label2 \ |
| 921 |
f = *(fp); \ |
| 922 |
/* Coefficient is significant. */ \ |
| 923 |
*(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ |
| 924 |
jpc_mqenc_setcurctx((mqenc), JPC_GETSCCTXNO(f)); \ |
| 925 |
v = ((*(dp) < 0) ? 1 : 0); \ |
| 926 |
jpc_mqenc_putbit((mqenc), v ^ JPC_GETSPB(f)); \ |
| 927 |
JPC_UPDATEFLAGS4((fp), (frowstep), v, vcausalflag); \ |
| 928 |
*(fp) |= JPC_SIG; \ |
| 929 |
} \ |
| 930 |
} \ |
| 931 |
*(fp) &= ~JPC_VISIT; \ |
| 932 |
} |
| 933 |
|
| 934 |
static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags, |
| 935 |
jas_matrix_t *data, int term, long *nmsedec) |
| 936 |
{ |
| 937 |
int i; |
| 938 |
int j; |
| 939 |
int k; |
| 940 |
int vscanlen; |
| 941 |
int v; |
| 942 |
int runlen; |
| 943 |
jpc_fix_t *fp; |
| 944 |
int width; |
| 945 |
int height; |
| 946 |
jpc_fix_t *dp; |
| 947 |
int one; |
| 948 |
int frowstep; |
| 949 |
int drowstep; |
| 950 |
int fstripestep; |
| 951 |
int dstripestep; |
| 952 |
jpc_fix_t *fstripestart; |
| 953 |
jpc_fix_t *dstripestart; |
| 954 |
jpc_fix_t *fvscanstart; |
| 955 |
jpc_fix_t *dvscanstart; |
| 956 |
|
| 957 |
*nmsedec = 0; |
| 958 |
width = jas_matrix_numcols(data); |
| 959 |
height = jas_matrix_numrows(data); |
| 960 |
frowstep = jas_matrix_rowstep(flags); |
| 961 |
drowstep = jas_matrix_rowstep(data); |
| 962 |
fstripestep = frowstep << 2; |
| 963 |
dstripestep = drowstep << 2; |
| 964 |
|
| 965 |
one = 1 << (bitpos + JPC_NUMEXTRABITS); |
| 966 |
|
| 967 |
fstripestart = jas_matrix_getref(flags, 1, 1); |
| 968 |
dstripestart = jas_matrix_getref(data, 0, 0); |
| 969 |
for (i = height; i > 0; i -= 4, fstripestart += fstripestep, |
| 970 |
dstripestart += dstripestep) { |
| 971 |
fvscanstart = fstripestart; |
| 972 |
dvscanstart = dstripestart; |
| 973 |
vscanlen = JAS_MIN(i, 4); |
| 974 |
for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { |
| 975 |
|
| 976 |
fp = fvscanstart; |
| 977 |
if (vscanlen >= 4 && !((*fp) & (JPC_SIG | JPC_VISIT | |
| 978 |
JPC_OTHSIGMSK)) && (fp += frowstep, !((*fp) & (JPC_SIG | |
| 979 |
JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & |
| 980 |
(JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, |
| 981 |
!((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) { |
| 982 |
dp = dvscanstart; |
| 983 |
for (k = 0; k < vscanlen; ++k) { |
| 984 |
v = (abs(*dp) & one) ? 1 : 0; |
| 985 |
if (v) { |
| 986 |
break; |
| 987 |
} |
| 988 |
dp += drowstep; |
| 989 |
} |
| 990 |
runlen = k; |
| 991 |
if (runlen >= 4) { |
| 992 |
jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); |
| 993 |
jpc_mqenc_putbit(mqenc, 0); |
| 994 |
continue; |
| 995 |
} |
| 996 |
jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); |
| 997 |
jpc_mqenc_putbit(mqenc, 1); |
| 998 |
jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); |
| 999 |
jpc_mqenc_putbit(mqenc, runlen >> 1); |
| 1000 |
jpc_mqenc_putbit(mqenc, runlen & 1); |
| 1001 |
fp = fvscanstart + frowstep * runlen; |
| 1002 |
dp = dvscanstart + drowstep * runlen; |
| 1003 |
k = vscanlen - runlen; |
| 1004 |
switch (runlen) { |
| 1005 |
case 0: |
| 1006 |
goto clnpass_partial0; |
| 1007 |
break; |
| 1008 |
case 1: |
| 1009 |
goto clnpass_partial1; |
| 1010 |
break; |
| 1011 |
case 2: |
| 1012 |
goto clnpass_partial2; |
| 1013 |
break; |
| 1014 |
case 3: |
| 1015 |
goto clnpass_partial3; |
| 1016 |
break; |
| 1017 |
} |
| 1018 |
} else { |
| 1019 |
runlen = 0; |
| 1020 |
fp = fvscanstart; |
| 1021 |
dp = dvscanstart; |
| 1022 |
k = vscanlen; |
| 1023 |
goto clnpass_full0; |
| 1024 |
} |
| 1025 |
clnpass_step(fp, frowstep, dp, bitpos, one, |
| 1026 |
orient, nmsedec, mqenc, clnpass_full0:, clnpass_partial0:, vcausalflag); |
| 1027 |
if (--k <= 0) { |
| 1028 |
continue; |
| 1029 |
} |
| 1030 |
fp += frowstep; |
| 1031 |
dp += drowstep; |
| 1032 |
clnpass_step(fp, frowstep, dp, bitpos, one, |
| 1033 |
orient, nmsedec, mqenc, ;, clnpass_partial1:, 0); |
| 1034 |
if (--k <= 0) { |
| 1035 |
continue; |
| 1036 |
} |
| 1037 |
fp += frowstep; |
| 1038 |
dp += drowstep; |
| 1039 |
clnpass_step(fp, frowstep, dp, bitpos, one, |
| 1040 |
orient, nmsedec, mqenc, ;, clnpass_partial2:, 0); |
| 1041 |
if (--k <= 0) { |
| 1042 |
continue; |
| 1043 |
} |
| 1044 |
fp += frowstep; |
| 1045 |
dp += drowstep; |
| 1046 |
clnpass_step(fp, frowstep, dp, bitpos, one, |
| 1047 |
orient, nmsedec, mqenc, ;, clnpass_partial3:, 0); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
if (segsymflag) { |
| 1052 |
jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); |
| 1053 |
jpc_mqenc_putbit(mqenc, 1); |
| 1054 |
jpc_mqenc_putbit(mqenc, 0); |
| 1055 |
jpc_mqenc_putbit(mqenc, 1); |
| 1056 |
jpc_mqenc_putbit(mqenc, 0); |
| 1057 |
} |
| 1058 |
|
| 1059 |
if (term) { |
| 1060 |
jpc_mqenc_flush(mqenc, term - 1); |
| 1061 |
} |
| 1062 |
|
| 1063 |
return jpc_mqenc_error(mqenc) ? (-1) : 0; |
| 1064 |
} |