| 1 |
#include <stdarg.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <assert.h> |
| 5 |
#include <math.h> |
| 6 |
#include <float.h> |
| 7 |
|
| 8 |
#include "netpbm/pm.h" |
| 9 |
#include "netpbm/nstring.h" |
| 10 |
|
| 11 |
#include "jasper/jas_string.h" |
| 12 |
#include "jasper/jas_malloc.h" |
| 13 |
#include "jasper/jas_image.h" |
| 14 |
#include "jasper/jas_tvp.h" |
| 15 |
#include "jasper/jas_version.h" |
| 16 |
#include "jasper/jas_math.h" |
| 17 |
#include "jasper/jas_debug.h" |
| 18 |
|
| 19 |
#include "jpc_flt.h" |
| 20 |
#include "jpc_fix.h" |
| 21 |
#include "jpc_tagtree.h" |
| 22 |
#include "jpc_enc.h" |
| 23 |
#include "jpc_cs.h" |
| 24 |
#include "jpc_mct.h" |
| 25 |
#include "jpc_tsfb.h" |
| 26 |
#include "jpc_qmfb.h" |
| 27 |
#include "jpc_t1enc.h" |
| 28 |
#include "jpc_t2enc.h" |
| 29 |
#include "jpc_cod.h" |
| 30 |
#include "jpc_math.h" |
| 31 |
#include "jpc_util.h" |
| 32 |
|
| 33 |
/*****************************************************************************\ |
| 34 |
* |
| 35 |
\*****************************************************************************/ |
| 36 |
|
| 37 |
#define JPC_POW2(n) \ |
| 38 |
(1 << (n)) |
| 39 |
|
| 40 |
#define JPC_FLOORTOMULTPOW2(x, n) \ |
| 41 |
(((n) > 0) ? ((x) & (~((1 << n) - 1))) : (x)) |
| 42 |
/* Round to the nearest multiple of the specified power of two in the |
| 43 |
direction of negative infinity. */ |
| 44 |
|
| 45 |
#define JPC_CEILTOMULTPOW2(x, n) \ |
| 46 |
(((n) > 0) ? JPC_FLOORTOMULTPOW2(((x) + (1 << (n)) - 1), n) : (x)) |
| 47 |
/* Round to the nearest multiple of the specified power of two in the |
| 48 |
direction of positive infinity. */ |
| 49 |
|
| 50 |
#define JPC_POW2(n) \ |
| 51 |
(1 << (n)) |
| 52 |
|
| 53 |
jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno); |
| 54 |
void jpc_enc_tile_destroy(jpc_enc_tile_t *tile); |
| 55 |
|
| 56 |
static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp, |
| 57 |
jas_image_t *image, jpc_enc_tile_t *tile); |
| 58 |
static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt); |
| 59 |
static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, |
| 60 |
jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos); |
| 61 |
static void rlvl_destroy(jpc_enc_rlvl_t *rlvl); |
| 62 |
static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp, |
| 63 |
jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos); |
| 64 |
static void band_destroy(jpc_enc_band_t *bands); |
| 65 |
static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, |
| 66 |
jpc_enc_band_t *band); |
| 67 |
static void prc_destroy(jpc_enc_prc_t *prcs); |
| 68 |
static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, |
| 69 |
jpc_enc_prc_t *prc); |
| 70 |
static void cblk_destroy(jpc_enc_cblk_t *cblks); |
| 71 |
static void pass_destroy(jpc_enc_pass_t *pass); |
| 72 |
void jpc_enc_dump(jpc_enc_t *enc); |
| 73 |
|
| 74 |
/*****************************************************************************\ |
| 75 |
* Local prototypes. |
| 76 |
\*****************************************************************************/ |
| 77 |
|
| 78 |
void quantize(jas_matrix_t *data, jpc_fix_t stepsize); |
| 79 |
jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image); |
| 80 |
void jpc_enc_destroy(jpc_enc_t *enc); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static uint_fast32_t |
| 85 |
jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn) { |
| 86 |
|
| 87 |
int p; |
| 88 |
uint_fast32_t mant; |
| 89 |
uint_fast32_t expn; |
| 90 |
int n; |
| 91 |
|
| 92 |
if (absdelta < 0) { |
| 93 |
abort(); |
| 94 |
} |
| 95 |
|
| 96 |
p = jpc_firstone(absdelta) - JPC_FIX_FRACBITS; |
| 97 |
n = 11 - jpc_firstone(absdelta); |
| 98 |
mant = ((n < 0) ? (absdelta >> (-n)) : (absdelta << n)) & 0x7ff; |
| 99 |
expn = scaleexpn - p; |
| 100 |
if (scaleexpn < p) { |
| 101 |
abort(); |
| 102 |
} |
| 103 |
return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
typedef enum { |
| 109 |
OPT_DEBUG, |
| 110 |
OPT_IMGAREAOFFX, |
| 111 |
OPT_IMGAREAOFFY, |
| 112 |
OPT_TILEGRDOFFX, |
| 113 |
OPT_TILEGRDOFFY, |
| 114 |
OPT_TILEWIDTH, |
| 115 |
OPT_TILEHEIGHT, |
| 116 |
OPT_PRCWIDTH, |
| 117 |
OPT_PRCHEIGHT, |
| 118 |
OPT_CBLKWIDTH, |
| 119 |
OPT_CBLKHEIGHT, |
| 120 |
OPT_MODE, |
| 121 |
OPT_PRG, |
| 122 |
OPT_NOMCT, |
| 123 |
OPT_MAXRLVLS, |
| 124 |
OPT_SOP, |
| 125 |
OPT_EPH, |
| 126 |
OPT_LAZY, |
| 127 |
OPT_TERMALL, |
| 128 |
OPT_SEGSYM, |
| 129 |
OPT_VCAUSAL, |
| 130 |
OPT_RESET, |
| 131 |
OPT_PTERM, |
| 132 |
OPT_NUMGBITS, |
| 133 |
OPT_RATE, |
| 134 |
OPT_ILYRRATES, |
| 135 |
} optid_t; |
| 136 |
|
| 137 |
jas_taginfo_t encopts[] = { |
| 138 |
{OPT_DEBUG, "debug"}, |
| 139 |
{OPT_IMGAREAOFFX, "imgareatlx"}, |
| 140 |
{OPT_IMGAREAOFFY, "imgareatly"}, |
| 141 |
{OPT_TILEGRDOFFX, "tilegrdtlx"}, |
| 142 |
{OPT_TILEGRDOFFY, "tilegrdtly"}, |
| 143 |
{OPT_TILEWIDTH, "tilewidth"}, |
| 144 |
{OPT_TILEHEIGHT, "tileheight"}, |
| 145 |
{OPT_PRCWIDTH, "prcwidth"}, |
| 146 |
{OPT_PRCHEIGHT, "prcheight"}, |
| 147 |
{OPT_CBLKWIDTH, "cblkwidth"}, |
| 148 |
{OPT_CBLKHEIGHT, "cblkheight"}, |
| 149 |
{OPT_MODE, "mode"}, |
| 150 |
{OPT_PRG, "prg"}, |
| 151 |
{OPT_NOMCT, "nomct"}, |
| 152 |
{OPT_MAXRLVLS, "numrlvls"}, |
| 153 |
{OPT_SOP, "sop"}, |
| 154 |
{OPT_EPH, "eph"}, |
| 155 |
{OPT_LAZY, "lazy"}, |
| 156 |
{OPT_TERMALL, "termall"}, |
| 157 |
{OPT_SEGSYM, "segsym"}, |
| 158 |
{OPT_VCAUSAL, "vcausal"}, |
| 159 |
{OPT_PTERM, "pterm"}, |
| 160 |
{OPT_RESET, "resetprob"}, |
| 161 |
{OPT_NUMGBITS, "numgbits"}, |
| 162 |
{OPT_RATE, "rate"}, |
| 163 |
{OPT_ILYRRATES, "ilyrrates"}, |
| 164 |
{-1, 0} |
| 165 |
}; |
| 166 |
|
| 167 |
typedef enum { |
| 168 |
PO_L = 0, |
| 169 |
PO_R |
| 170 |
} poid_t; |
| 171 |
|
| 172 |
|
| 173 |
jas_taginfo_t prgordtab[] = { |
| 174 |
{JPC_COD_LRCPPRG, "lrcp"}, |
| 175 |
{JPC_COD_RLCPPRG, "rlcp"}, |
| 176 |
{JPC_COD_RPCLPRG, "rpcl"}, |
| 177 |
{JPC_COD_PCRLPRG, "pcrl"}, |
| 178 |
{JPC_COD_CPRLPRG, "cprl"}, |
| 179 |
{-1, 0} |
| 180 |
}; |
| 181 |
|
| 182 |
typedef enum { |
| 183 |
MODE_INT, |
| 184 |
MODE_REAL |
| 185 |
} modeid_t; |
| 186 |
|
| 187 |
jas_taginfo_t modetab[] = { |
| 188 |
{MODE_INT, "int"}, |
| 189 |
{MODE_REAL, "real"}, |
| 190 |
{-1, 0} |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static void |
| 196 |
tracev(const char * const fmt, |
| 197 |
va_list args) { |
| 198 |
|
| 199 |
vfprintf(stderr, fmt, args); |
| 200 |
|
| 201 |
fprintf(stderr, "\n"); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static void |
| 207 |
trace(const char * const fmt, ...) { |
| 208 |
|
| 209 |
if (jas_getdbglevel() > 0) { |
| 210 |
va_list args; |
| 211 |
|
| 212 |
va_start(args, fmt); |
| 213 |
tracev(fmt, args); |
| 214 |
va_end(args); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
/*****************************************************************************\ |
| 221 |
* Option parsing code. |
| 222 |
\*****************************************************************************/ |
| 223 |
|
| 224 |
static void |
| 225 |
ratestrtosize(const char * const s, |
| 226 |
uint_fast32_t const rawsize, |
| 227 |
uint_fast32_t * const sizeP) { |
| 228 |
|
| 229 |
if (strchr(s, 'B')) { |
| 230 |
*sizeP = atoi(s); |
| 231 |
} else { |
| 232 |
jpc_flt_t const f = atof(s); |
| 233 |
|
| 234 |
if (f < 0) { |
| 235 |
*sizeP = 0; |
| 236 |
} else if (f > 1.0) { |
| 237 |
*sizeP = rawsize + 1; |
| 238 |
} else { |
| 239 |
*sizeP = f * rawsize; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
static void |
| 247 |
cp_destroy(jpc_enc_cp_t *cp) { |
| 248 |
|
| 249 |
if (cp->ccps) { |
| 250 |
if (cp->tcp.ilyrrates) { |
| 251 |
jas_free(cp->tcp.ilyrrates); |
| 252 |
} |
| 253 |
jas_free(cp->ccps); |
| 254 |
} |
| 255 |
jas_free(cp); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
static jpc_enc_cp_t * |
| 261 |
cp_create(char *optstr, jas_image_t *image) { |
| 262 |
|
| 263 |
jpc_enc_cp_t *cp; |
| 264 |
jas_tvparser_t *tvp; |
| 265 |
int ret; |
| 266 |
int numilyrrates; |
| 267 |
double *ilyrrates; |
| 268 |
int i; |
| 269 |
int tagid; |
| 270 |
jpc_enc_tcp_t *tcp; |
| 271 |
jpc_enc_tccp_t *tccp; |
| 272 |
jpc_enc_ccp_t *ccp; |
| 273 |
uint_fast16_t cmptno; |
| 274 |
uint_fast16_t rlvlno; |
| 275 |
uint_fast16_t prcwidthexpn; |
| 276 |
uint_fast16_t prcheightexpn; |
| 277 |
bool enablemct; |
| 278 |
uint_fast16_t lyrno; |
| 279 |
uint_fast32_t hsteplcm; |
| 280 |
uint_fast32_t vsteplcm; |
| 281 |
bool mctvalid; |
| 282 |
|
| 283 |
tvp = 0; |
| 284 |
cp = 0; |
| 285 |
ilyrrates = 0; |
| 286 |
numilyrrates = 0; |
| 287 |
|
| 288 |
if (!(cp = jas_malloc(sizeof(jpc_enc_cp_t)))) { |
| 289 |
goto error; |
| 290 |
} |
| 291 |
|
| 292 |
prcwidthexpn = 15; |
| 293 |
prcheightexpn = 15; |
| 294 |
enablemct = true; |
| 295 |
|
| 296 |
cp->ccps = 0; |
| 297 |
cp->debug = 0; |
| 298 |
cp->imgareatlx = UINT_FAST32_MAX; |
| 299 |
cp->imgareatly = UINT_FAST32_MAX; |
| 300 |
cp->refgrdwidth = 0; |
| 301 |
cp->refgrdheight = 0; |
| 302 |
cp->tilegrdoffx = UINT_FAST32_MAX; |
| 303 |
cp->tilegrdoffy = UINT_FAST32_MAX; |
| 304 |
cp->tilewidth = 0; |
| 305 |
cp->tileheight = 0; |
| 306 |
cp->numcmpts = jas_image_numcmpts(image); |
| 307 |
|
| 308 |
hsteplcm = 1; |
| 309 |
vsteplcm = 1; |
| 310 |
for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) { |
| 311 |
if (jas_image_cmptbrx(image, cmptno) + |
| 312 |
jas_image_cmpthstep(image, cmptno) <= |
| 313 |
jas_image_brx(image) || jas_image_cmptbry(image, cmptno) + |
| 314 |
jas_image_cmptvstep(image, cmptno) <= jas_image_bry(image)) { |
| 315 |
fprintf(stderr, |
| 316 |
"We don't know how to interpret this image type\n"); |
| 317 |
goto error; |
| 318 |
} |
| 319 |
/* Note: We ought to be calculating the LCMs here. Fix some day. */ |
| 320 |
hsteplcm *= jas_image_cmpthstep(image, cmptno); |
| 321 |
vsteplcm *= jas_image_cmptvstep(image, cmptno); |
| 322 |
} |
| 323 |
|
| 324 |
if (!(cp->ccps = jas_malloc(cp->numcmpts * sizeof(jpc_enc_ccp_t)))) { |
| 325 |
goto error; |
| 326 |
} |
| 327 |
for (cmptno = 0, ccp = cp->ccps; cmptno < cp->numcmpts; ++cmptno, |
| 328 |
++ccp) { |
| 329 |
ccp->sampgrdstepx = jas_image_cmpthstep(image, cmptno); |
| 330 |
ccp->sampgrdstepy = jas_image_cmptvstep(image, cmptno); |
| 331 |
/* XXX - this isn't quite correct for more general image */ |
| 332 |
ccp->sampgrdsubstepx = 0; |
| 333 |
ccp->sampgrdsubstepx = 0; |
| 334 |
ccp->prec = jas_image_cmptprec(image, cmptno); |
| 335 |
ccp->sgnd = jas_image_cmptsgnd(image, cmptno); |
| 336 |
ccp->numstepsizes = 0; |
| 337 |
memset(ccp->stepsizes, 0, sizeof(ccp->stepsizes)); |
| 338 |
} |
| 339 |
|
| 340 |
cp->rawsize = jas_image_rawsize(image); |
| 341 |
cp->totalsize = UINT_FAST32_MAX; |
| 342 |
/* Set default value, the special value that means size is unlimited |
| 343 |
(so lossless coding is called for). To be overridden if user |
| 344 |
specified |
| 345 |
*/ |
| 346 |
|
| 347 |
tcp = &cp->tcp; |
| 348 |
tcp->csty = 0; |
| 349 |
tcp->intmode = true; |
| 350 |
tcp->prg = JPC_COD_LRCPPRG; |
| 351 |
tcp->numlyrs = 1; |
| 352 |
tcp->ilyrrates = 0; |
| 353 |
|
| 354 |
tccp = &cp->tccp; |
| 355 |
tccp->csty = 0; |
| 356 |
tccp->maxrlvls = 6; |
| 357 |
tccp->cblkwidthexpn = 6; |
| 358 |
tccp->cblkheightexpn = 6; |
| 359 |
tccp->cblksty = 0; |
| 360 |
tccp->numgbits = 2; |
| 361 |
|
| 362 |
if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { |
| 363 |
goto error; |
| 364 |
} |
| 365 |
|
| 366 |
while (!(ret = jas_tvparser_next(tvp))) { |
| 367 |
switch (jas_taginfo_nonull(jas_taginfos_lookup(encopts, |
| 368 |
jas_tvparser_gettag(tvp)))->id) { |
| 369 |
case OPT_DEBUG: |
| 370 |
cp->debug = atoi(jas_tvparser_getval(tvp)); |
| 371 |
break; |
| 372 |
case OPT_IMGAREAOFFX: |
| 373 |
cp->imgareatlx = atoi(jas_tvparser_getval(tvp)); |
| 374 |
break; |
| 375 |
case OPT_IMGAREAOFFY: |
| 376 |
cp->imgareatly = atoi(jas_tvparser_getval(tvp)); |
| 377 |
break; |
| 378 |
case OPT_TILEGRDOFFX: |
| 379 |
cp->tilegrdoffx = atoi(jas_tvparser_getval(tvp)); |
| 380 |
break; |
| 381 |
case OPT_TILEGRDOFFY: |
| 382 |
cp->tilegrdoffy = atoi(jas_tvparser_getval(tvp)); |
| 383 |
break; |
| 384 |
case OPT_TILEWIDTH: |
| 385 |
cp->tilewidth = atoi(jas_tvparser_getval(tvp)); |
| 386 |
break; |
| 387 |
case OPT_TILEHEIGHT: |
| 388 |
cp->tileheight = atoi(jas_tvparser_getval(tvp)); |
| 389 |
break; |
| 390 |
case OPT_PRCWIDTH: |
| 391 |
prcwidthexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); |
| 392 |
break; |
| 393 |
case OPT_PRCHEIGHT: |
| 394 |
prcheightexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); |
| 395 |
break; |
| 396 |
case OPT_CBLKWIDTH: |
| 397 |
tccp->cblkwidthexpn = |
| 398 |
jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); |
| 399 |
break; |
| 400 |
case OPT_CBLKHEIGHT: |
| 401 |
tccp->cblkheightexpn = |
| 402 |
jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); |
| 403 |
break; |
| 404 |
case OPT_MODE: |
| 405 |
if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(modetab, |
| 406 |
jas_tvparser_getval(tvp)))->id) < 0) { |
| 407 |
fprintf(stderr, |
| 408 |
"ignoring invalid mode %s\n", |
| 409 |
jas_tvparser_getval(tvp)); |
| 410 |
} else { |
| 411 |
tcp->intmode = (tagid == MODE_INT); |
| 412 |
} |
| 413 |
break; |
| 414 |
case OPT_PRG: |
| 415 |
if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(prgordtab, |
| 416 |
jas_tvparser_getval(tvp)))->id) < 0) { |
| 417 |
fprintf(stderr, |
| 418 |
"ignoring invalid progression order %s\n", |
| 419 |
jas_tvparser_getval(tvp)); |
| 420 |
} else { |
| 421 |
tcp->prg = tagid; |
| 422 |
} |
| 423 |
break; |
| 424 |
case OPT_NOMCT: |
| 425 |
enablemct = false; |
| 426 |
break; |
| 427 |
case OPT_MAXRLVLS: |
| 428 |
tccp->maxrlvls = atoi(jas_tvparser_getval(tvp)); |
| 429 |
break; |
| 430 |
case OPT_SOP: |
| 431 |
cp->tcp.csty |= JPC_COD_SOP; |
| 432 |
break; |
| 433 |
case OPT_EPH: |
| 434 |
cp->tcp.csty |= JPC_COD_EPH; |
| 435 |
break; |
| 436 |
case OPT_LAZY: |
| 437 |
tccp->cblksty |= JPC_COX_LAZY; |
| 438 |
break; |
| 439 |
case OPT_TERMALL: |
| 440 |
tccp->cblksty |= JPC_COX_TERMALL; |
| 441 |
break; |
| 442 |
case OPT_SEGSYM: |
| 443 |
tccp->cblksty |= JPC_COX_SEGSYM; |
| 444 |
break; |
| 445 |
case OPT_VCAUSAL: |
| 446 |
tccp->cblksty |= JPC_COX_VSC; |
| 447 |
break; |
| 448 |
case OPT_RESET: |
| 449 |
tccp->cblksty |= JPC_COX_RESET; |
| 450 |
break; |
| 451 |
case OPT_PTERM: |
| 452 |
tccp->cblksty |= JPC_COX_PTERM; |
| 453 |
break; |
| 454 |
case OPT_NUMGBITS: |
| 455 |
cp->tccp.numgbits = atoi(jas_tvparser_getval(tvp)); |
| 456 |
break; |
| 457 |
case OPT_RATE: |
| 458 |
ratestrtosize(jas_tvparser_getval(tvp), cp->rawsize, |
| 459 |
&cp->totalsize); |
| 460 |
break; |
| 461 |
case OPT_ILYRRATES: |
| 462 |
if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates, |
| 463 |
&ilyrrates)) { |
| 464 |
fprintf(stderr, |
| 465 |
"warning: invalid intermediate layer rates specifier " |
| 466 |
"ignored (%s)\n", |
| 467 |
jas_tvparser_getval(tvp)); |
| 468 |
} |
| 469 |
break; |
| 470 |
|
| 471 |
default: |
| 472 |
fprintf(stderr, "warning: ignoring invalid option %s\n", |
| 473 |
jas_tvparser_gettag(tvp)); |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
jas_tvparser_destroy(tvp); |
| 479 |
tvp = 0; |
| 480 |
|
| 481 |
if (cp->imgareatlx == UINT_FAST32_MAX) { |
| 482 |
cp->imgareatlx = 0; |
| 483 |
} else { |
| 484 |
if (hsteplcm != 1) { |
| 485 |
fprintf(stderr, "warning: overriding imgareatlx value\n"); |
| 486 |
} |
| 487 |
cp->imgareatlx *= hsteplcm; |
| 488 |
} |
| 489 |
if (cp->imgareatly == UINT_FAST32_MAX) { |
| 490 |
cp->imgareatly = 0; |
| 491 |
} else { |
| 492 |
if (vsteplcm != 1) { |
| 493 |
fprintf(stderr, "warning: overriding imgareatly value\n"); |
| 494 |
} |
| 495 |
cp->imgareatly *= vsteplcm; |
| 496 |
} |
| 497 |
cp->refgrdwidth = cp->imgareatlx + jas_image_width(image); |
| 498 |
cp->refgrdheight = cp->imgareatly + jas_image_height(image); |
| 499 |
if (cp->tilegrdoffx == UINT_FAST32_MAX) { |
| 500 |
cp->tilegrdoffx = cp->imgareatlx; |
| 501 |
} |
| 502 |
if (cp->tilegrdoffy == UINT_FAST32_MAX) { |
| 503 |
cp->tilegrdoffy = cp->imgareatly; |
| 504 |
} |
| 505 |
if (!cp->tilewidth) { |
| 506 |
cp->tilewidth = cp->refgrdwidth - cp->tilegrdoffx; |
| 507 |
} |
| 508 |
if (!cp->tileheight) { |
| 509 |
cp->tileheight = cp->refgrdheight - cp->tilegrdoffy; |
| 510 |
} |
| 511 |
|
| 512 |
if (cp->numcmpts == 3) { |
| 513 |
mctvalid = true; |
| 514 |
for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) { |
| 515 |
if (jas_image_cmptprec(image, cmptno) != |
| 516 |
jas_image_cmptprec(image, 0) || |
| 517 |
jas_image_cmptsgnd(image, cmptno) != |
| 518 |
jas_image_cmptsgnd(image, 0) || |
| 519 |
jas_image_cmptwidth(image, cmptno) != |
| 520 |
jas_image_cmptwidth(image, 0) || |
| 521 |
jas_image_cmptheight(image, cmptno) != |
| 522 |
jas_image_cmptheight(image, 0)) { |
| 523 |
mctvalid = false; |
| 524 |
} |
| 525 |
} |
| 526 |
} else { |
| 527 |
mctvalid = false; |
| 528 |
} |
| 529 |
if (mctvalid && enablemct && jas_image_colorspace(image) != |
| 530 |
JAS_IMAGE_CS_RGB) { |
| 531 |
fprintf(stderr, "warning: color model apparently not RGB\n"); |
| 532 |
} |
| 533 |
if (mctvalid && enablemct && jas_image_colorspace(image) == |
| 534 |
JAS_IMAGE_CS_RGB) { |
| 535 |
tcp->mctid = (tcp->intmode) ? (JPC_MCT_RCT) : (JPC_MCT_ICT); |
| 536 |
} else { |
| 537 |
tcp->mctid = JPC_MCT_NONE; |
| 538 |
} |
| 539 |
tccp->qmfbid = (tcp->intmode) ? (JPC_COX_RFT) : (JPC_COX_INS); |
| 540 |
|
| 541 |
for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) { |
| 542 |
tccp->prcwidthexpns[rlvlno] = prcwidthexpn; |
| 543 |
tccp->prcheightexpns[rlvlno] = prcheightexpn; |
| 544 |
} |
| 545 |
if (prcwidthexpn != 15 || prcheightexpn != 15) { |
| 546 |
tccp->csty |= JPC_COX_PRT; |
| 547 |
} |
| 548 |
|
| 549 |
/* Ensure that the tile width and height is valid. */ |
| 550 |
if (!cp->tilewidth) { |
| 551 |
fprintf(stderr, "invalid tile width %lu\n", (unsigned long) |
| 552 |
cp->tilewidth); |
| 553 |
goto error; |
| 554 |
} |
| 555 |
if (!cp->tileheight) { |
| 556 |
fprintf(stderr, "invalid tile height %lu\n", (unsigned long) |
| 557 |
cp->tileheight); |
| 558 |
goto error; |
| 559 |
} |
| 560 |
|
| 561 |
/* Ensure that the tile grid offset is valid. */ |
| 562 |
if (cp->tilegrdoffx > cp->imgareatlx || |
| 563 |
cp->tilegrdoffy > cp->imgareatly || |
| 564 |
cp->tilegrdoffx + cp->tilewidth < cp->imgareatlx || |
| 565 |
cp->tilegrdoffy + cp->tileheight < cp->imgareatly) { |
| 566 |
fprintf(stderr, "invalid tile grid offset (%lu, %lu)\n", |
| 567 |
(unsigned long) cp->tilegrdoffx, (unsigned long) |
| 568 |
cp->tilegrdoffy); |
| 569 |
goto error; |
| 570 |
} |
| 571 |
|
| 572 |
cp->numhtiles = JPC_CEILDIV(cp->refgrdwidth - cp->tilegrdoffx, |
| 573 |
cp->tilewidth); |
| 574 |
cp->numvtiles = JPC_CEILDIV(cp->refgrdheight - cp->tilegrdoffy, |
| 575 |
cp->tileheight); |
| 576 |
cp->numtiles = cp->numhtiles * cp->numvtiles; |
| 577 |
|
| 578 |
if (ilyrrates && numilyrrates > 0) { |
| 579 |
tcp->numlyrs = numilyrrates + 1; |
| 580 |
if (!(tcp->ilyrrates = jas_malloc((tcp->numlyrs - 1) * |
| 581 |
sizeof(jpc_fix_t)))) { |
| 582 |
goto error; |
| 583 |
} |
| 584 |
for (i = 0; i < tcp->numlyrs - 1; ++i) { |
| 585 |
tcp->ilyrrates[i] = jpc_dbltofix(ilyrrates[i]); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
/* Ensure that the integer mode is used in the case of lossless |
| 590 |
coding. */ |
| 591 |
if (cp->totalsize == UINT_FAST32_MAX && (!cp->tcp.intmode)) { |
| 592 |
fprintf(stderr, "cannot use real mode for lossless coding\n"); |
| 593 |
goto error; |
| 594 |
} |
| 595 |
|
| 596 |
/* Ensure that the precinct width is valid. */ |
| 597 |
if (prcwidthexpn > 15) { |
| 598 |
fprintf(stderr, "invalid precinct width\n"); |
| 599 |
goto error; |
| 600 |
} |
| 601 |
|
| 602 |
/* Ensure that the precinct height is valid. */ |
| 603 |
if (prcheightexpn > 15) { |
| 604 |
fprintf(stderr, "invalid precinct height\n"); |
| 605 |
goto error; |
| 606 |
} |
| 607 |
|
| 608 |
/* Ensure that the code block width is valid. */ |
| 609 |
if (cp->tccp.cblkwidthexpn < 2 || cp->tccp.cblkwidthexpn > 12) { |
| 610 |
fprintf(stderr, "invalid code block width %d\n", |
| 611 |
JPC_POW2(cp->tccp.cblkwidthexpn)); |
| 612 |
goto error; |
| 613 |
} |
| 614 |
|
| 615 |
/* Ensure that the code block height is valid. */ |
| 616 |
if (cp->tccp.cblkheightexpn < 2 || cp->tccp.cblkheightexpn > 12) { |
| 617 |
fprintf(stderr, "invalid code block height %d\n", |
| 618 |
JPC_POW2(cp->tccp.cblkheightexpn)); |
| 619 |
goto error; |
| 620 |
} |
| 621 |
|
| 622 |
/* Ensure that the code block size is not too large. */ |
| 623 |
if (cp->tccp.cblkwidthexpn + cp->tccp.cblkheightexpn > 12) { |
| 624 |
fprintf(stderr, "code block size too large\n"); |
| 625 |
goto error; |
| 626 |
} |
| 627 |
|
| 628 |
/* Ensure that the number of layers is valid. */ |
| 629 |
if (cp->tcp.numlyrs > 16384) { |
| 630 |
fprintf(stderr, "too many layers\n"); |
| 631 |
goto error; |
| 632 |
} |
| 633 |
|
| 634 |
/* There must be at least one resolution level. */ |
| 635 |
if (cp->tccp.maxrlvls < 1) { |
| 636 |
fprintf(stderr, "must be at least one resolution level\n"); |
| 637 |
goto error; |
| 638 |
} |
| 639 |
|
| 640 |
/* Ensure that the number of guard bits is valid. */ |
| 641 |
if (cp->tccp.numgbits > 8) { |
| 642 |
fprintf(stderr, "invalid number of guard bits\n"); |
| 643 |
goto error; |
| 644 |
} |
| 645 |
|
| 646 |
/* Ensure that the intermediate layer rates are valid. */ |
| 647 |
if (tcp->numlyrs > 1) { |
| 648 |
/* The intermediate layers rates must increase monotonically. */ |
| 649 |
for (lyrno = 0; lyrno + 2 < tcp->numlyrs; ++lyrno) { |
| 650 |
if (tcp->ilyrrates[lyrno] >= tcp->ilyrrates[lyrno + 1]) { |
| 651 |
pm_message("Compression rate for Layer %u (%f) " |
| 652 |
"is not greater than that for Layer %u (%f). " |
| 653 |
"Rates must increase at every layer", |
| 654 |
(unsigned)(lyrno+1), |
| 655 |
jpc_fixtodbl(tcp->ilyrrates[lyrno + 1]), |
| 656 |
(unsigned)lyrno, |
| 657 |
jpc_fixtodbl(tcp->ilyrrates[lyrno])); |
| 658 |
goto error; |
| 659 |
} |
| 660 |
} |
| 661 |
/* The intermediate layer rates must be less than the overall rate. */ |
| 662 |
if (cp->totalsize != UINT_FAST32_MAX) { |
| 663 |
for (lyrno = 0; lyrno < tcp->numlyrs - 1; ++lyrno) { |
| 664 |
double const thisLyrRate = jpc_fixtodbl(tcp->ilyrrates[lyrno]); |
| 665 |
double const completeRate = |
| 666 |
((double) cp->totalsize) / cp->rawsize; |
| 667 |
if (thisLyrRate > completeRate) { |
| 668 |
pm_message( |
| 669 |
"Compression rate for Layer %u is %f, " |
| 670 |
"which is greater than the rate for the complete " |
| 671 |
"image (%f)", |
| 672 |
(unsigned)lyrno, thisLyrRate, completeRate); |
| 673 |
goto error; |
| 674 |
} |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
if (ilyrrates) { |
| 680 |
jas_free(ilyrrates); |
| 681 |
} |
| 682 |
|
| 683 |
return cp; |
| 684 |
|
| 685 |
error: |
| 686 |
|
| 687 |
if (ilyrrates) { |
| 688 |
jas_free(ilyrrates); |
| 689 |
} |
| 690 |
if (tvp) { |
| 691 |
jas_tvparser_destroy(tvp); |
| 692 |
} |
| 693 |
if (cp) { |
| 694 |
cp_destroy(cp); |
| 695 |
} |
| 696 |
return 0; |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
static int |
| 702 |
encodemainhdr(jpc_enc_t *enc) { |
| 703 |
|
| 704 |
uint_fast32_t const maintlrlen = 2; |
| 705 |
|
| 706 |
jpc_siz_t *siz; |
| 707 |
jpc_cod_t *cod; |
| 708 |
jpc_qcd_t *qcd; |
| 709 |
int i; |
| 710 |
long startoff; |
| 711 |
long mainhdrlen; |
| 712 |
jpc_enc_cp_t *cp; |
| 713 |
jpc_qcc_t *qcc; |
| 714 |
jpc_enc_tccp_t *tccp; |
| 715 |
uint_fast16_t cmptno; |
| 716 |
jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; |
| 717 |
jpc_enc_tcp_t *tcp; |
| 718 |
jpc_tsfb_t *tsfb; |
| 719 |
jpc_tsfb_band_t *bandinfo; |
| 720 |
uint_fast16_t numbands; |
| 721 |
uint_fast16_t bandno; |
| 722 |
uint_fast16_t rlvlno; |
| 723 |
uint_fast16_t analgain; |
| 724 |
jpc_fix_t absstepsize; |
| 725 |
char buf[1024]; |
| 726 |
jpc_com_t *com; |
| 727 |
|
| 728 |
cp = enc->cp; |
| 729 |
|
| 730 |
startoff = jas_stream_getrwcount(enc->out); |
| 731 |
|
| 732 |
/* Write SOC marker segment. */ |
| 733 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) { |
| 734 |
return -1; |
| 735 |
} |
| 736 |
if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { |
| 737 |
fprintf(stderr, "cannot write SOC marker\n"); |
| 738 |
return -1; |
| 739 |
} |
| 740 |
jpc_ms_destroy(enc->mrk); |
| 741 |
enc->mrk = 0; |
| 742 |
|
| 743 |
/* Write SIZ marker segment. */ |
| 744 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_SIZ))) { |
| 745 |
return -1; |
| 746 |
} |
| 747 |
siz = &enc->mrk->parms.siz; |
| 748 |
siz->caps = 0; |
| 749 |
siz->xoff = cp->imgareatlx; |
| 750 |
siz->yoff = cp->imgareatly; |
| 751 |
siz->width = cp->refgrdwidth; |
| 752 |
siz->height = cp->refgrdheight; |
| 753 |
siz->tilexoff = cp->tilegrdoffx; |
| 754 |
siz->tileyoff = cp->tilegrdoffy; |
| 755 |
siz->tilewidth = cp->tilewidth; |
| 756 |
siz->tileheight = cp->tileheight; |
| 757 |
siz->numcomps = cp->numcmpts; |
| 758 |
siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)); |
| 759 |
assert(siz->comps); |
| 760 |
for (i = 0; i < cp->numcmpts; ++i) { |
| 761 |
siz->comps[i].prec = cp->ccps[i].prec; |
| 762 |
siz->comps[i].sgnd = cp->ccps[i].sgnd; |
| 763 |
siz->comps[i].hsamp = cp->ccps[i].sampgrdstepx; |
| 764 |
siz->comps[i].vsamp = cp->ccps[i].sampgrdstepy; |
| 765 |
} |
| 766 |
if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { |
| 767 |
fprintf(stderr, "cannot write SIZ marker\n"); |
| 768 |
return -1; |
| 769 |
} |
| 770 |
jpc_ms_destroy(enc->mrk); |
| 771 |
enc->mrk = 0; |
| 772 |
|
| 773 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_COM))) { |
| 774 |
return -1; |
| 775 |
} |
| 776 |
sprintf(buf, "Creator: JasPer Version %s", jas_getversion()); |
| 777 |
com = &enc->mrk->parms.com; |
| 778 |
com->len = strlen(buf); |
| 779 |
com->regid = JPC_COM_LATIN; |
| 780 |
if (!(com->data = JAS_CAST(unsigned char *, jas_strdup(buf)))) { |
| 781 |
abort(); |
| 782 |
} |
| 783 |
if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { |
| 784 |
fprintf(stderr, "cannot write COM marker\n"); |
| 785 |
return -1; |
| 786 |
} |
| 787 |
jpc_ms_destroy(enc->mrk); |
| 788 |
enc->mrk = 0; |
| 789 |
|
| 790 |
#if 0 |
| 791 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_CRG))) { |
| 792 |
return -1; |
| 793 |
} |
| 794 |
crg = &enc->mrk->parms.crg; |
| 795 |
crg->comps = jas_malloc(crg->numcomps * sizeof(jpc_crgcomp_t)); |
| 796 |
if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { |
| 797 |
fprintf(stderr, "cannot write CRG marker\n"); |
| 798 |
return -1; |
| 799 |
} |
| 800 |
jpc_ms_destroy(enc->mrk); |
| 801 |
enc->mrk = 0; |
| 802 |
#endif |
| 803 |
|
| 804 |
tcp = &cp->tcp; |
| 805 |
tccp = &cp->tccp; |
| 806 |
for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { |
| 807 |
tsfb = jpc_cod_gettsfb(tccp->qmfbid, tccp->maxrlvls - 1); |
| 808 |
jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls, |
| 809 |
bandinfos); |
| 810 |
jpc_tsfb_destroy(tsfb); |
| 811 |
numbands = 3 * tccp->maxrlvls - 2; |
| 812 |
for (bandno = 0, bandinfo = bandinfos; bandno < numbands; |
| 813 |
++bandno, ++bandinfo) { |
| 814 |
rlvlno = (bandno) ? ((bandno - 1) / 3 + 1) : 0; |
| 815 |
analgain = JPC_NOMINALGAIN(tccp->qmfbid, tccp->maxrlvls, |
| 816 |
rlvlno, bandinfo->orient); |
| 817 |
if (!tcp->intmode) { |
| 818 |
absstepsize = jpc_fix_div(jpc_inttofix(1 << |
| 819 |
(analgain + 1)), bandinfo->synenergywt); |
| 820 |
} else { |
| 821 |
absstepsize = jpc_inttofix(1); |
| 822 |
} |
| 823 |
cp->ccps[cmptno].stepsizes[bandno] = |
| 824 |
jpc_abstorelstepsize(absstepsize, |
| 825 |
cp->ccps[cmptno].prec + analgain); |
| 826 |
} |
| 827 |
cp->ccps[cmptno].numstepsizes = numbands; |
| 828 |
} |
| 829 |
|
| 830 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { |
| 831 |
return -1; |
| 832 |
} |
| 833 |
cod = &enc->mrk->parms.cod; |
| 834 |
cod->csty = cp->tccp.csty | cp->tcp.csty; |
| 835 |
cod->compparms.csty = cp->tccp.csty | cp->tcp.csty; |
| 836 |
cod->compparms.numdlvls = cp->tccp.maxrlvls - 1; |
| 837 |
cod->compparms.numrlvls = cp->tccp.maxrlvls; |
| 838 |
cod->prg = cp->tcp.prg; |
| 839 |
cod->numlyrs = cp->tcp.numlyrs; |
| 840 |
cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn); |
| 841 |
cod->compparms.cblkheightval = |
| 842 |
JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn); |
| 843 |
cod->compparms.cblksty = cp->tccp.cblksty; |
| 844 |
cod->compparms.qmfbid = cp->tccp.qmfbid; |
| 845 |
cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE); |
| 846 |
if (tccp->csty & JPC_COX_PRT) { |
| 847 |
for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) { |
| 848 |
cod->compparms.rlvls[rlvlno].parwidthval = |
| 849 |
tccp->prcwidthexpns[rlvlno]; |
| 850 |
cod->compparms.rlvls[rlvlno].parheightval = |
| 851 |
tccp->prcheightexpns[rlvlno]; |
| 852 |
} |
| 853 |
} |
| 854 |
if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { |
| 855 |
fprintf(stderr, "cannot write COD marker\n"); |
| 856 |
return -1; |
| 857 |
} |
| 858 |
jpc_ms_destroy(enc->mrk); |
| 859 |
enc->mrk = 0; |
| 860 |
|
| 861 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_QCD))) { |
| 862 |
return -1; |
| 863 |
} |
| 864 |
qcd = &enc->mrk->parms.qcd; |
| 865 |
qcd->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ? |
| 866 |
JPC_QCX_SEQNT : JPC_QCX_NOQNT; |
| 867 |
qcd->compparms.numstepsizes = cp->ccps[0].numstepsizes; |
| 868 |
qcd->compparms.numguard = cp->tccp.numgbits; |
| 869 |
qcd->compparms.stepsizes = cp->ccps[0].stepsizes; |
| 870 |
if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { |
| 871 |
return -1; |
| 872 |
} |
| 873 |
/* We do not want the step size array to be freed! */ |
| 874 |
qcd->compparms.stepsizes = 0; |
| 875 |
jpc_ms_destroy(enc->mrk); |
| 876 |
enc->mrk = 0; |
| 877 |
|
| 878 |
tccp = &cp->tccp; |
| 879 |
for (cmptno = 1; cmptno < cp->numcmpts; ++cmptno) { |
| 880 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { |
| 881 |
return -1; |
| 882 |
} |
| 883 |
qcc = &enc->mrk->parms.qcc; |
| 884 |
qcc->compno = cmptno; |
| 885 |
qcc->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ? |
| 886 |
JPC_QCX_SEQNT : JPC_QCX_NOQNT; |
| 887 |
qcc->compparms.numstepsizes = cp->ccps[cmptno].numstepsizes; |
| 888 |
qcc->compparms.numguard = cp->tccp.numgbits; |
| 889 |
qcc->compparms.stepsizes = cp->ccps[cmptno].stepsizes; |
| 890 |
if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { |
| 891 |
return -1; |
| 892 |
} |
| 893 |
/* We do not want the step size array to be freed! */ |
| 894 |
qcc->compparms.stepsizes = 0; |
| 895 |
jpc_ms_destroy(enc->mrk); |
| 896 |
enc->mrk = 0; |
| 897 |
} |
| 898 |
|
| 899 |
mainhdrlen = jas_stream_getrwcount(enc->out) - startoff; |
| 900 |
enc->len += mainhdrlen; |
| 901 |
if (enc->cp->totalsize != UINT_FAST32_MAX) { |
| 902 |
uint_fast32_t const overhead = mainhdrlen + maintlrlen; |
| 903 |
|
| 904 |
if (overhead > enc->cp->totalsize) { |
| 905 |
pm_message("Requested limit on image size of %u bytes " |
| 906 |
"is not possible because it is less than " |
| 907 |
"the image metadata size (%u bytes)", |
| 908 |
(unsigned)enc->cp->totalsize, (unsigned)overhead); |
| 909 |
return -1; |
| 910 |
} |
| 911 |
enc->mainbodysize = enc->cp->totalsize - overhead; |
| 912 |
/* This has never actually worked. 'totalsize' is supposed to be |
| 913 |
the total all-in, so if you request total size 200, you should |
| 914 |
get an output file 200 bytes or smaller; but we see 209 bytes. |
| 915 |
Furthermore, at 194 bytes, we get a warning that an empty layer |
| 916 |
is generated, which probably is actually an error. |
| 917 |
|
| 918 |
We should fix this some day. |
| 919 |
*/ |
| 920 |
} else { |
| 921 |
enc->mainbodysize = UINT_FAST32_MAX; |
| 922 |
} |
| 923 |
|
| 924 |
return 0; |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
static void |
| 930 |
calcrdslopes(jpc_enc_cblk_t *cblk) { |
| 931 |
|
| 932 |
jpc_enc_pass_t *endpasses; |
| 933 |
jpc_enc_pass_t *pass0; |
| 934 |
jpc_enc_pass_t *pass1; |
| 935 |
jpc_enc_pass_t *pass2; |
| 936 |
jpc_flt_t slope0; |
| 937 |
jpc_flt_t slope; |
| 938 |
jpc_flt_t dd; |
| 939 |
long dr; |
| 940 |
|
| 941 |
endpasses = &cblk->passes[cblk->numpasses]; |
| 942 |
pass2 = cblk->passes; |
| 943 |
slope0 = 0; |
| 944 |
while (pass2 != endpasses) { |
| 945 |
pass0 = 0; |
| 946 |
for (pass1 = cblk->passes; pass1 != endpasses; ++pass1) { |
| 947 |
dd = pass1->cumwmsedec; |
| 948 |
dr = pass1->end; |
| 949 |
if (pass0) { |
| 950 |
dd -= pass0->cumwmsedec; |
| 951 |
dr -= pass0->end; |
| 952 |
} |
| 953 |
if (dd <= 0) { |
| 954 |
pass1->rdslope = JPC_BADRDSLOPE; |
| 955 |
if (pass1 >= pass2) { |
| 956 |
pass2 = &pass1[1]; |
| 957 |
} |
| 958 |
continue; |
| 959 |
} |
| 960 |
if (pass1 < pass2 && pass1->rdslope <= 0) { |
| 961 |
continue; |
| 962 |
} |
| 963 |
if (!dr) { |
| 964 |
assert(pass0); |
| 965 |
pass0->rdslope = 0; |
| 966 |
break; |
| 967 |
} |
| 968 |
slope = dd / dr; |
| 969 |
if (pass0 && slope >= slope0) { |
| 970 |
pass0->rdslope = 0; |
| 971 |
break; |
| 972 |
} |
| 973 |
pass1->rdslope = slope; |
| 974 |
if (pass1 >= pass2) { |
| 975 |
pass2 = &pass1[1]; |
| 976 |
} |
| 977 |
pass0 = pass1; |
| 978 |
slope0 = slope; |
| 979 |
} |
| 980 |
} |
| 981 |
|
| 982 |
#if 0 |
| 983 |
for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) { |
| 984 |
if (pass0->rdslope > 0.0) { |
| 985 |
fprintf(stderr, "pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", |
| 986 |
pass0 - cblk->passes, |
| 987 |
fixtodbl(pass0->nmsedec), pass0->wmsedec, |
| 988 |
pass0->end, pass0->rdslope); |
| 989 |
} |
| 990 |
} |
| 991 |
#endif |
| 992 |
} |
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
static void |
| 997 |
traceLayerSizes(const uint_fast32_t * const lyrSizes, |
| 998 |
uint_fast32_t const layerCt) { |
| 999 |
|
| 1000 |
if (jas_getdbglevel() > 0) { |
| 1001 |
uint_fast32_t i; |
| 1002 |
for (i = 0; i < layerCt; ++i) { |
| 1003 |
fprintf(stderr, "Layer %u size = ", (unsigned)i); |
| 1004 |
|
| 1005 |
if (lyrSizes[i] == UINT_FAST32_MAX) |
| 1006 |
fprintf(stderr, "Unlimited"); |
| 1007 |
else |
| 1008 |
fprintf(stderr, "%u", (unsigned)lyrSizes[i]); |
| 1009 |
fprintf(stderr, "\n"); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
static void |
| 1017 |
computeLayerSizes(jpc_enc_t * const encP, |
| 1018 |
jpc_enc_tile_t * const tileP, |
| 1019 |
jpc_enc_cp_t * const cpP, |
| 1020 |
double const rho, |
| 1021 |
long const tilehdrlen) { |
| 1022 |
|
| 1023 |
/* Note that in allowed sizes, UINT_FAST32_MAX is a special value meaning |
| 1024 |
"unlimited". |
| 1025 |
*/ |
| 1026 |
|
| 1027 |
uint_fast32_t const lastLyrno = tileP->numlyrs - 1; |
| 1028 |
|
| 1029 |
uint_fast32_t lyrno; |
| 1030 |
|
| 1031 |
assert(tileP->numlyrs > 0); |
| 1032 |
|
| 1033 |
for (lyrno = 0; lyrno < lastLyrno; ++lyrno) { |
| 1034 |
tileP->lyrsizes[lyrno] = |
| 1035 |
MAX(tileP->rawsize * |
| 1036 |
jpc_fixtodbl(cpP->tcp.ilyrrates[lyrno]), |
| 1037 |
tilehdrlen + 1) - tilehdrlen; |
| 1038 |
} |
| 1039 |
|
| 1040 |
tileP->lyrsizes[lastLyrno] = |
| 1041 |
(cpP->totalsize == UINT_FAST32_MAX) ? |
| 1042 |
UINT_FAST32_MAX : (rho * encP->mainbodysize); |
| 1043 |
|
| 1044 |
traceLayerSizes(tileP->lyrsizes, tileP->numlyrs); |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
static void |
| 1050 |
dump_layeringinfo(jpc_enc_t *enc) { |
| 1051 |
|
| 1052 |
jpc_enc_tcmpt_t *tcmpt; |
| 1053 |
uint_fast16_t tcmptno; |
| 1054 |
jpc_enc_rlvl_t *rlvl; |
| 1055 |
uint_fast16_t rlvlno; |
| 1056 |
jpc_enc_band_t *band; |
| 1057 |
uint_fast16_t bandno; |
| 1058 |
jpc_enc_prc_t *prc; |
| 1059 |
uint_fast32_t prcno; |
| 1060 |
jpc_enc_cblk_t *cblk; |
| 1061 |
uint_fast16_t cblkno; |
| 1062 |
jpc_enc_pass_t *pass; |
| 1063 |
uint_fast16_t passno; |
| 1064 |
int lyrno; |
| 1065 |
jpc_enc_tile_t *tile; |
| 1066 |
|
| 1067 |
tile = enc->curtile; |
| 1068 |
|
| 1069 |
for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { |
| 1070 |
fprintf(stderr, "lyrno = %02d\n", lyrno); |
| 1071 |
for (tcmptno = 0, tcmpt = tile->tcmpts; tcmptno < tile->numtcmpts; |
| 1072 |
++tcmptno, ++tcmpt) { |
| 1073 |
for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; |
| 1074 |
++rlvlno, ++rlvl) { |
| 1075 |
if (!rlvl->bands) { |
| 1076 |
continue; |
| 1077 |
} |
| 1078 |
for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
| 1079 |
++bandno, ++band) { |
| 1080 |
if (!band->data) { |
| 1081 |
continue; |
| 1082 |
} |
| 1083 |
for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; |
| 1084 |
++prcno, ++prc) { |
| 1085 |
if (!prc->cblks) { |
| 1086 |
continue; |
| 1087 |
} |
| 1088 |
for (cblkno = 0, cblk = prc->cblks; cblkno < |
| 1089 |
prc->numcblks; ++cblkno, ++cblk) { |
| 1090 |
for (passno = 0, pass = cblk->passes; |
| 1091 |
passno < cblk->numpasses && |
| 1092 |
pass->lyrno == lyrno; |
| 1093 |
++passno, ++pass) { |
| 1094 |
fprintf(stderr, |
| 1095 |
"lyrno=%02d cmptno=%02d " |
| 1096 |
"rlvlno=%02d bandno=%02d " |
| 1097 |
"prcno=%02d cblkno=%03d " |
| 1098 |
"passno=%03d\n", |
| 1099 |
lyrno, (int)tcmptno, (int)rlvlno, |
| 1100 |
(int)bandno, (int)prcno, (int) cblkno, |
| 1101 |
(int)passno); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
} |
| 1105 |
} |
| 1106 |
} |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
static void |
| 1115 |
trace_layeringinfo(jpc_enc_t * const encP) { |
| 1116 |
|
| 1117 |
if (jas_getdbglevel() >= 5) |
| 1118 |
dump_layeringinfo(encP); |
| 1119 |
} |
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
static void |
| 1124 |
validateCumlensIncreases(const uint_fast32_t * const cumlens, |
| 1125 |
uint_fast32_t const numlyrs) { |
| 1126 |
uint_fast32_t lyrno; |
| 1127 |
|
| 1128 |
for (lyrno = 1; lyrno < numlyrs - 1; ++lyrno) { |
| 1129 |
if (cumlens[lyrno - 1] > cumlens[lyrno]) { |
| 1130 |
abort(); |
| 1131 |
} |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
static void |
| 1138 |
findMinMaxRDSlopeValues(jpc_enc_tile_t * const tileP, |
| 1139 |
jpc_flt_t * const mnrdslopeP, |
| 1140 |
jpc_flt_t * const mxrdslopeP) { |
| 1141 |
/*---------------------------------------------------------------------------- |
| 1142 |
Find minimum and maximum R-D slope values. |
| 1143 |
-----------------------------------------------------------------------------*/ |
| 1144 |
jpc_flt_t mxrdslope; |
| 1145 |
jpc_flt_t mnrdslope; |
| 1146 |
jpc_enc_tcmpt_t * endcomps; |
| 1147 |
jpc_enc_tcmpt_t * compP; |
| 1148 |
|
| 1149 |
mnrdslope = DBL_MAX; |
| 1150 |
mxrdslope = 0; |
| 1151 |
endcomps = &tileP->tcmpts[tileP->numtcmpts]; |
| 1152 |
|
| 1153 |
for (compP = tileP->tcmpts; compP != endcomps; ++compP) { |
| 1154 |
jpc_enc_rlvl_t * const endlvlsP = &compP->rlvls[compP->numrlvls]; |
| 1155 |
|
| 1156 |
jpc_enc_rlvl_t * lvlP; |
| 1157 |
|
| 1158 |
for (lvlP = compP->rlvls; lvlP != endlvlsP; ++lvlP) { |
| 1159 |
jpc_enc_band_t * endbandsP; |
| 1160 |
jpc_enc_band_t * bandP; |
| 1161 |
|
| 1162 |
if (lvlP->bands) { |
| 1163 |
endbandsP = &lvlP->bands[lvlP->numbands]; |
| 1164 |
for (bandP = lvlP->bands; bandP != endbandsP; ++bandP) { |
| 1165 |
uint_fast32_t prcno; |
| 1166 |
jpc_enc_prc_t * prcP; |
| 1167 |
|
| 1168 |
if (bandP->data) { |
| 1169 |
for (prcno = 0, prcP = bandP->prcs; |
| 1170 |
prcno < lvlP->numprcs; |
| 1171 |
++prcno, ++prcP) { |
| 1172 |
|
| 1173 |
jpc_enc_cblk_t * endcblksP; |
| 1174 |
jpc_enc_cblk_t * cblkP; |
| 1175 |
|
| 1176 |
if (prcP->cblks) { |
| 1177 |
endcblksP = &prcP->cblks[prcP->numcblks]; |
| 1178 |
for (cblkP = prcP->cblks; |
| 1179 |
cblkP != endcblksP; |
| 1180 |
++cblkP) { |
| 1181 |
jpc_enc_pass_t * endpassesP; |
| 1182 |
jpc_enc_pass_t * passP; |
| 1183 |
|
| 1184 |
calcrdslopes(cblkP); |
| 1185 |
endpassesP = |
| 1186 |
&cblkP->passes[cblkP->numpasses]; |
| 1187 |
for (passP = cblkP->passes; |
| 1188 |
passP != endpassesP; |
| 1189 |
++passP) { |
| 1190 |
if (passP->rdslope > 0) { |
| 1191 |
mnrdslope = |
| 1192 |
MIN(passP->rdslope, mnrdslope); |
| 1193 |
mxrdslope = |
| 1194 |
MAX(passP->rdslope, mxrdslope); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
} |
| 1198 |
} |
| 1199 |
} |
| 1200 |
} |
| 1201 |
} |
| 1202 |
} |
| 1203 |
} |
| 1204 |
} |
| 1205 |
trace("min rdslope = %f max rdslope = %f", mnrdslope, mxrdslope); |
| 1206 |
|
| 1207 |
*mnrdslopeP = mnrdslope; |
| 1208 |
*mxrdslopeP = mxrdslope; |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
static void |
| 1214 |
performTier2CodingOneLayer(jpc_enc_t * const encP, |
| 1215 |
jpc_enc_tile_t * const tileP, |
| 1216 |
uint_fast32_t const lyrno, |
| 1217 |
jas_stream_t * const outP, |
| 1218 |
const char ** const errorP) { |
| 1219 |
/*---------------------------------------------------------------------------- |
| 1220 |
Encode Layer 'lyrno' of tile *tileP to stream *outP. |
| 1221 |
|
| 1222 |
Use the pass assignment already in *tileP. |
| 1223 |
-----------------------------------------------------------------------------*/ |
| 1224 |
jpc_enc_tcmpt_t * const endcompsP = &tileP->tcmpts[tileP->numtcmpts]; |
| 1225 |
|
| 1226 |
jpc_enc_tcmpt_t * compP; |
| 1227 |
|
| 1228 |
*errorP = NULL; /* initial assumption */ |
| 1229 |
|
| 1230 |
for (compP = tileP->tcmpts; compP != endcompsP && !*errorP; ++compP) { |
| 1231 |
jpc_enc_rlvl_t * const endlvlsP = &compP->rlvls[compP->numrlvls]; |
| 1232 |
|
| 1233 |
jpc_enc_rlvl_t * lvlP; |
| 1234 |
|
| 1235 |
for (lvlP = compP->rlvls; lvlP != endlvlsP && !*errorP; ++lvlP) { |
| 1236 |
if (lvlP->bands) { |
| 1237 |
uint_fast32_t prcno; |
| 1238 |
for (prcno = 0; prcno < lvlP->numprcs && !*errorP; ++prcno) { |
| 1239 |
int rc; |
| 1240 |
rc = jpc_enc_encpkt(encP, outP, compP - tileP->tcmpts, |
| 1241 |
lvlP - compP->rlvls, prcno, lyrno); |
| 1242 |
if (rc != 0) |
| 1243 |
pm_asprintf(errorP, "jpc_enc_encpkt() failed on " |
| 1244 |
"precinct %u", (unsigned)prcno); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
} |
| 1248 |
} |
| 1249 |
} |
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
static void |
| 1254 |
assignHighSlopePassesToLayer(jpc_enc_t * const encP, |
| 1255 |
jpc_enc_tile_t * const tileP, |
| 1256 |
uint_fast32_t const lyrno, |
| 1257 |
bool const haveThresh, |
| 1258 |
jpc_flt_t const thresh) { |
| 1259 |
/*---------------------------------------------------------------------------- |
| 1260 |
Assign all passes with R-D slopes greater than or equal to 'thresh' to layer |
| 1261 |
'lyrno' and the rest to no layer. |
| 1262 |
|
| 1263 |
If 'haveThresh' is false, assign all passes to no layer. |
| 1264 |
-----------------------------------------------------------------------------*/ |
| 1265 |
jpc_enc_tcmpt_t * endcompsP; |
| 1266 |
jpc_enc_tcmpt_t * compP; |
| 1267 |
|
| 1268 |
endcompsP = &tileP->tcmpts[tileP->numtcmpts]; |
| 1269 |
for (compP = tileP->tcmpts; compP != endcompsP; ++compP) { |
| 1270 |
jpc_enc_rlvl_t * const endlvlsP = &compP->rlvls[compP->numrlvls]; |
| 1271 |
|
| 1272 |
jpc_enc_rlvl_t * lvlP; |
| 1273 |
|
| 1274 |
for (lvlP = compP->rlvls; lvlP != endlvlsP; ++lvlP) { |
| 1275 |
if (lvlP->bands) { |
| 1276 |
jpc_enc_band_t * const endbandsP = |
| 1277 |
&lvlP->bands[lvlP->numbands]; |
| 1278 |
jpc_enc_band_t * bandP; |
| 1279 |
for (bandP = lvlP->bands; bandP != endbandsP; ++bandP) { |
| 1280 |
if (bandP->data) { |
| 1281 |
jpc_enc_prc_t * prcP; |
| 1282 |
uint_fast32_t prcno; |
| 1283 |
for (prcno = 0, prcP = bandP->prcs; |
| 1284 |
prcno < lvlP->numprcs; |
| 1285 |
++prcno, ++prcP) { |
| 1286 |
if (prcP->cblks) { |
| 1287 |
jpc_enc_cblk_t * const endcblksP = |
| 1288 |
&prcP->cblks[prcP->numcblks]; |
| 1289 |
jpc_enc_cblk_t * cblkP; |
| 1290 |
for (cblkP = prcP->cblks; |
| 1291 |
cblkP != endcblksP; |
| 1292 |
++cblkP) { |
| 1293 |
if (cblkP->curpass) { |
| 1294 |
jpc_enc_pass_t * const endpassesP = |
| 1295 |
&cblkP->passes[cblkP->numpasses]; |
| 1296 |
jpc_enc_pass_t * pass1P; |
| 1297 |
jpc_enc_pass_t * passP; |
| 1298 |
|
| 1299 |
pass1P = cblkP->curpass; |
| 1300 |
if (haveThresh) { |
| 1301 |
jpc_enc_pass_t * passP; |
| 1302 |
for (passP = cblkP->curpass; |
| 1303 |
passP != endpassesP; |
| 1304 |
++passP) { |
| 1305 |
if (passP->rdslope >= thresh) |
| 1306 |
pass1P = passP + 1; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
for (passP = cblkP->curpass; |
| 1310 |
passP != pass1P; |
| 1311 |
++passP) { |
| 1312 |
passP->lyrno = lyrno; |
| 1313 |
} |
| 1314 |
for (; passP != endpassesP; ++passP) { |
| 1315 |
passP->lyrno = -1; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
} |
| 1319 |
} |
| 1320 |
} |
| 1321 |
} |
| 1322 |
} |
| 1323 |
} |
| 1324 |
} |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
static void |
| 1331 |
doLayer(jpc_enc_t * const encP, |
| 1332 |
jpc_enc_tile_t * const tileP, |
| 1333 |
uint_fast32_t const lyrno, |
| 1334 |
uint_fast32_t const allowedSize, |
| 1335 |
jpc_flt_t const mnrdslope, |
| 1336 |
jpc_flt_t const mxrdslope, |
| 1337 |
jas_stream_t * const outP, |
| 1338 |
const char ** const errorP) { |
| 1339 |
/*---------------------------------------------------------------------------- |
| 1340 |
Assign passes to layer 'lyrno' such that the cumulative size through |
| 1341 |
this layer is as close as possible to, but not exceeding, 'allowedSize'. |
| 1342 |
-----------------------------------------------------------------------------*/ |
| 1343 |
bool haveGoodThresh; |
| 1344 |
jpc_flt_t goodThresh; |
| 1345 |
|
| 1346 |
if (allowedSize == UINT_FAST32_MAX) { |
| 1347 |
/* There's no rate constraint (This can be true of the last layer, |
| 1348 |
e.g. for lossless coding). */ |
| 1349 |
goodThresh = -1; |
| 1350 |
haveGoodThresh = true; |
| 1351 |
*errorP = NULL; |
| 1352 |
} else { |
| 1353 |
/* Iterate through successive approximations of the threshold, finding |
| 1354 |
the threshold that gets us closest to 'allowedSize' without going |
| 1355 |
over. In each iteration, we do the full encoding, note the size, |
| 1356 |
and then restore the previous state. |
| 1357 |
*/ |
| 1358 |
long pos; |
| 1359 |
jpc_flt_t lo; |
| 1360 |
jpc_flt_t hi; |
| 1361 |
uint_fast32_t numiters; |
| 1362 |
|
| 1363 |
lo = mnrdslope; /* initial value */ |
| 1364 |
hi = mxrdslope; /* initial value */ |
| 1365 |
numiters = 0; /* initial value */ |
| 1366 |
haveGoodThresh = false; /* initial value */ |
| 1367 |
goodThresh = 0; /* initial value */ |
| 1368 |
|
| 1369 |
do { |
| 1370 |
jpc_flt_t const thresh = (lo + hi) / 2; |
| 1371 |
|
| 1372 |
int rc; |
| 1373 |
long oldpos; |
| 1374 |
|
| 1375 |
/* Save the tier 2 coding state. */ |
| 1376 |
jpc_save_t2state(encP); |
| 1377 |
oldpos = jas_stream_tell(outP); |
| 1378 |
assert(oldpos >= 0); |
| 1379 |
|
| 1380 |
assignHighSlopePassesToLayer(encP, tileP, lyrno, true, thresh); |
| 1381 |
|
| 1382 |
performTier2CodingOneLayer(encP, tileP, lyrno, outP, errorP); |
| 1383 |
|
| 1384 |
if (!*errorP) { |
| 1385 |
pos = jas_stream_tell(outP); |
| 1386 |
|
| 1387 |
/* Check the rate constraint. */ |
| 1388 |
assert(pos >= 0); |
| 1389 |
if (pos > allowedSize) { |
| 1390 |
/* The rate is too high. */ |
| 1391 |
lo = thresh; |
| 1392 |
} else if (pos <= allowedSize) { |
| 1393 |
/* The rate is low enough, so try higher. */ |
| 1394 |
hi = thresh; |
| 1395 |
if (!haveGoodThresh || thresh < goodThresh) { |
| 1396 |
goodThresh = thresh; |
| 1397 |
haveGoodThresh = true; |
| 1398 |
} |
| 1399 |
} |
| 1400 |
} |
| 1401 |
/* Restore the tier 2 coding state. */ |
| 1402 |
jpc_restore_t2state(encP); |
| 1403 |
rc = jas_stream_seek(outP, oldpos, SEEK_SET); |
| 1404 |
if (rc < 0) |
| 1405 |
abort(); |
| 1406 |
|
| 1407 |
trace("iter %u: allowedlen=%08ld actuallen=%08ld thresh=%f", |
| 1408 |
numiters, allowedSize, pos, thresh); |
| 1409 |
|
| 1410 |
++numiters; |
| 1411 |
} while (lo < hi - 1e-3 && numiters < 32 && !*errorP); |
| 1412 |
} |
| 1413 |
|
| 1414 |
if (!*errorP) { |
| 1415 |
if (!haveGoodThresh) |
| 1416 |
fprintf(stderr, "warning: empty layer generated\n"); |
| 1417 |
|
| 1418 |
trace("haveGoodThresh %u goodthresh %f", haveGoodThresh, goodThresh); |
| 1419 |
|
| 1420 |
assignHighSlopePassesToLayer(encP, tileP, lyrno, |
| 1421 |
haveGoodThresh, goodThresh); |
| 1422 |
|
| 1423 |
performTier2CodingOneLayer(encP, tileP, lyrno, outP, errorP); |
| 1424 |
} |
| 1425 |
} |
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
static void |
| 1430 |
performTier2Coding(jpc_enc_t * const encP, |
| 1431 |
uint_fast32_t const numlyrs, |
| 1432 |
uint_fast32_t * const cumlens, |
| 1433 |
const char ** const errorP) { |
| 1434 |
/*---------------------------------------------------------------------------- |
| 1435 |
Encode in 'numlyrs' layers, such that at each layer L, the size is |
| 1436 |
cumlens[L]. |
| 1437 |
-----------------------------------------------------------------------------*/ |
| 1438 |
jpc_enc_tile_t * const tileP = encP->curtile; |
| 1439 |
|
| 1440 |
jas_stream_t * outP; |
| 1441 |
uint_fast32_t lyrno; |
| 1442 |
jpc_flt_t mnrdslope; |
| 1443 |
jpc_flt_t mxrdslope; |
| 1444 |
|
| 1445 |
validateCumlensIncreases(cumlens, numlyrs); |
| 1446 |
|
| 1447 |
outP = jas_stream_memopen(0, 0); |
| 1448 |
|
| 1449 |
if (!outP) |
| 1450 |
pm_asprintf(errorP, "jas_stream_memopen() failed"); |
| 1451 |
else { |
| 1452 |
findMinMaxRDSlopeValues(tileP, &mnrdslope, &mxrdslope); |
| 1453 |
|
| 1454 |
jpc_init_t2state(encP, 1); |
| 1455 |
|
| 1456 |
for (lyrno = 0, *errorP = NULL; |
| 1457 |
lyrno < numlyrs && !*errorP; |
| 1458 |
++lyrno) { |
| 1459 |
doLayer(encP, tileP, lyrno, cumlens[lyrno], |
| 1460 |
mnrdslope, mxrdslope, outP, errorP); |
| 1461 |
} |
| 1462 |
|
| 1463 |
if (!*errorP) { |
| 1464 |
trace_layeringinfo(encP); |
| 1465 |
|
| 1466 |
jas_stream_close(outP); |
| 1467 |
} |
| 1468 |
} |
| 1469 |
JAS_DBGLOG(10, ("done doing rateallocation\n")); |
| 1470 |
} |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
static void |
| 1477 |
encodeTileBody(jpc_enc_t * const encoderP, |
| 1478 |
long const tilehdrlen, |
| 1479 |
const char ** const errorP) { |
| 1480 |
/*---------------------------------------------------------------------------- |
| 1481 |
Encode the body of encoder *encoderP's current tile, writing the encoded |
| 1482 |
result to the encoder's output stream. |
| 1483 |
|
| 1484 |
Assume the tile header is already in that stream, and its length is |
| 1485 |
'tilehdrlen'. |
| 1486 |
-----------------------------------------------------------------------------*/ |
| 1487 |
jpc_enc_tile_t * const tileP = encoderP->curtile; |
| 1488 |
jpc_enc_cp_t * const cp = encoderP->cp; |
| 1489 |
|
| 1490 |
int rc; |
| 1491 |
|
| 1492 |
rc = jpc_enc_enccblks(encoderP); |
| 1493 |
if (rc != 0) |
| 1494 |
pm_asprintf(errorP, "jpc_enc_enccblks() failed"); |
| 1495 |
else { |
| 1496 |
double const rho = |
| 1497 |
(double) (tileP->brx - tileP->tlx) * (tileP->bry - tileP->tly) / |
| 1498 |
((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight - |
| 1499 |
cp->imgareatly)); |
| 1500 |
const char * error; |
| 1501 |
|
| 1502 |
tileP->rawsize = cp->rawsize * rho; |
| 1503 |
|
| 1504 |
computeLayerSizes(encoderP, tileP, cp, rho, tilehdrlen); |
| 1505 |
|
| 1506 |
performTier2Coding(encoderP, tileP->numlyrs, tileP->lyrsizes, &error); |
| 1507 |
|
| 1508 |
if (error) { |
| 1509 |
pm_asprintf(errorP, "Tier 2 coding failed. %s", error); |
| 1510 |
pm_strfree(error); |
| 1511 |
} else { |
| 1512 |
int rc; |
| 1513 |
|
| 1514 |
rc = jpc_enc_encpkts(encoderP, encoderP->tmpstream); |
| 1515 |
if (rc != 0) |
| 1516 |
pm_asprintf(errorP, "jpc_enc_encpkts() failed\n"); |
| 1517 |
else |
| 1518 |
*errorP = NULL; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
static void |
| 1526 |
quantizeBand(jpc_enc_band_t * const bandP, |
| 1527 |
jpc_enc_tile_t * const tileP, |
| 1528 |
jpc_enc_cp_t * const cp, |
| 1529 |
int const prec, |
| 1530 |
int const tccp_numgbits, |
| 1531 |
int * const numgbitsP) { |
| 1532 |
|
| 1533 |
if (bandP->data) { |
| 1534 |
int actualnumbps; |
| 1535 |
uint_fast32_t y; |
| 1536 |
jpc_fix_t mxmag; |
| 1537 |
|
| 1538 |
for (y = 0, actualnumbps = 0, mxmag = 0; |
| 1539 |
y < jas_matrix_numrows(bandP->data); |
| 1540 |
++y) { |
| 1541 |
uint_fast32_t x; |
| 1542 |
|
| 1543 |
for (x = 0; x < jas_matrix_numcols(bandP->data); ++x) |
| 1544 |
mxmag = MAX(mxmag, abs(jas_matrix_get(bandP->data, y, x))); |
| 1545 |
} |
| 1546 |
if (tileP->intmode) |
| 1547 |
actualnumbps = jpc_firstone(mxmag) + 1; |
| 1548 |
else |
| 1549 |
actualnumbps = jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS; |
| 1550 |
|
| 1551 |
*numgbitsP = actualnumbps - (prec - 1 + bandP->analgain); |
| 1552 |
|
| 1553 |
if (!tileP->intmode) { |
| 1554 |
bandP->absstepsize = |
| 1555 |
jpc_fix_div( |
| 1556 |
jpc_inttofix(1 << (bandP->analgain + 1)), |
| 1557 |
bandP->synweight); |
| 1558 |
} else { |
| 1559 |
bandP->absstepsize = jpc_inttofix(1); |
| 1560 |
} |
| 1561 |
bandP->stepsize = jpc_abstorelstepsize( |
| 1562 |
bandP->absstepsize, prec + bandP->analgain); |
| 1563 |
/* I couldn't figure out what the calculation with tccp_numgbits and |
| 1564 |
stepsize does (or even what a step size is), but there is an |
| 1565 |
assertion elsewhere than the number here is at least at large as |
| 1566 |
the 'numbps' value for every code block, which means |
| 1567 |
'actualnumbps'. In practice, we saw that not be true, so we added |
| 1568 |
the code to make 'actualnumbps' the floor here in hopes that would |
| 1569 |
fix the problem. But with the change, the image that caused the |
| 1570 |
assertion failure produces incorrect output. 22.11.07 |
| 1571 |
*/ |
| 1572 |
bandP->numbps = |
| 1573 |
MAX(actualnumbps, |
| 1574 |
tccp_numgbits + JPC_QCX_GETEXPN(bandP->stepsize) - 1); |
| 1575 |
|
| 1576 |
if (!tileP->intmode && bandP->data) |
| 1577 |
quantize(bandP->data, bandP->absstepsize); |
| 1578 |
} else |
| 1579 |
*numgbitsP = 0; |
| 1580 |
} |
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
static int |
| 1585 |
encodemainbody(jpc_enc_t *enc) { |
| 1586 |
|
| 1587 |
int tileno; |
| 1588 |
int i; |
| 1589 |
jpc_sot_t *sot; |
| 1590 |
int rlvlno; |
| 1591 |
jpc_qcc_t *qcc; |
| 1592 |
jpc_cod_t *cod; |
| 1593 |
int adjust; |
| 1594 |
int j; |
| 1595 |
int absbandno; |
| 1596 |
long tilehdrlen; |
| 1597 |
long tilelen; |
| 1598 |
jpc_enc_tile_t *tile; |
| 1599 |
jpc_enc_cp_t *cp; |
| 1600 |
int samestepsizes; |
| 1601 |
jpc_enc_ccp_t *ccps; |
| 1602 |
jpc_enc_tccp_t *tccp; |
| 1603 |
int mingbits; /* Minimum number of guard bits needed */ |
| 1604 |
const char * error; |
| 1605 |
|
| 1606 |
cp = enc->cp; |
| 1607 |
|
| 1608 |
for (tileno = 0; tileno < cp->numtiles; ++tileno) { |
| 1609 |
uint_fast16_t cmptno; |
| 1610 |
|
| 1611 |
enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno); |
| 1612 |
if (!enc->curtile) |
| 1613 |
abort(); |
| 1614 |
|
| 1615 |
tile = enc->curtile; |
| 1616 |
|
| 1617 |
if (jas_getdbglevel() >= 10) { |
| 1618 |
jpc_enc_dump(enc); |
| 1619 |
} |
| 1620 |
|
| 1621 |
for (cmptno = 0; cmptno < tile->numtcmpts; ++cmptno) { |
| 1622 |
|
| 1623 |
jpc_enc_tcmpt_t * const comp = &tile->tcmpts[cmptno]; |
| 1624 |
|
| 1625 |
if (!cp->ccps[cmptno].sgnd) { |
| 1626 |
adjust = 1 << (cp->ccps[cmptno].prec - 1); |
| 1627 |
for (i = 0; i < jas_matrix_numrows(comp->data); ++i) { |
| 1628 |
for (j = 0; j < jas_matrix_numcols(comp->data); ++j) { |
| 1629 |
*jas_matrix_getref(comp->data, i, j) -= adjust; |
| 1630 |
} |
| 1631 |
} |
| 1632 |
} |
| 1633 |
} |
| 1634 |
|
| 1635 |
if (!tile->intmode) { |
| 1636 |
uint_fast16_t cmptno; |
| 1637 |
for (cmptno = 0; cmptno < tile->numtcmpts; ++cmptno) { |
| 1638 |
jpc_enc_tcmpt_t * const comp = &tile->tcmpts[cmptno]; |
| 1639 |
jas_matrix_asl(comp->data, JPC_FIX_FRACBITS); |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
switch (tile->mctid) { |
| 1644 |
case JPC_MCT_RCT: |
| 1645 |
assert(jas_image_numcmpts(enc->image) == 3); |
| 1646 |
jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data, |
| 1647 |
tile->tcmpts[2].data); |
| 1648 |
break; |
| 1649 |
case JPC_MCT_ICT: |
| 1650 |
assert(jas_image_numcmpts(enc->image) == 3); |
| 1651 |
jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data, |
| 1652 |
tile->tcmpts[2].data); |
| 1653 |
break; |
| 1654 |
default: |
| 1655 |
break; |
| 1656 |
} |
| 1657 |
|
| 1658 |
for (i = 0; i < jas_image_numcmpts(enc->image); ++i) { |
| 1659 |
jpc_enc_tcmpt_t * const comp = &tile->tcmpts[i]; |
| 1660 |
jpc_tsfb_analyze(comp->tsfb, |
| 1661 |
((comp->qmfbid == JPC_COX_RFT) ? |
| 1662 |
JPC_TSFB_RITIMODE : 0), comp->data); |
| 1663 |
|
| 1664 |
} |
| 1665 |
|
| 1666 |
for (cmptno = 0; cmptno < tile->numtcmpts; ++cmptno) { |
| 1667 |
|
| 1668 |
jpc_enc_tcmpt_t * const comp = &tile->tcmpts[cmptno]; |
| 1669 |
|
| 1670 |
mingbits = 0; |
| 1671 |
absbandno = 0; |
| 1672 |
/* All bands must have a corresponding quantizer step size, |
| 1673 |
even if they contain no samples and are never coded. */ |
| 1674 |
/* Some bands may not be hit by the loop below, so we must |
| 1675 |
initialize all of the step sizes to a sane value. */ |
| 1676 |
memset(comp->stepsizes, 0, sizeof(comp->stepsizes)); |
| 1677 |
for (rlvlno = 0; rlvlno < comp->numrlvls; ++rlvlno) { |
| 1678 |
jpc_enc_rlvl_t * const lvl = &comp->rlvls[rlvlno]; |
| 1679 |
|
| 1680 |
unsigned int bandno; |
| 1681 |
|
| 1682 |
if (!lvl->bands) { |
| 1683 |
absbandno += rlvlno ? 3 : 1; |
| 1684 |
continue; |
| 1685 |
} |
| 1686 |
for (bandno = 0; bandno < lvl->numbands; ++bandno) { |
| 1687 |
jpc_enc_band_t * const band = &lvl->bands[bandno]; |
| 1688 |
|
| 1689 |
int numgbits; |
| 1690 |
|
| 1691 |
quantizeBand(band, tile, cp, |
| 1692 |
cp->ccps[cmptno].prec, |
| 1693 |
cp->tccp.numgbits, |
| 1694 |
&numgbits); |
| 1695 |
|
| 1696 |
mingbits = MAX(mingbits, numgbits); |
| 1697 |
|
| 1698 |
comp->stepsizes[absbandno] = band->stepsize; |
| 1699 |
|
| 1700 |
++absbandno; |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS); |
| 1705 |
if (!tile->intmode) { |
| 1706 |
jas_matrix_divpow2(comp->data, |
| 1707 |
JPC_FIX_FRACBITS - JPC_NUMEXTRABITS); |
| 1708 |
} else { |
| 1709 |
jas_matrix_asl(comp->data, JPC_NUMEXTRABITS); |
| 1710 |
} |
| 1711 |
} |
| 1712 |
|
| 1713 |
if (mingbits > cp->tccp.numgbits) { |
| 1714 |
fprintf(stderr, "error: too few guard bits (need at least %d)\n", |
| 1715 |
mingbits); |
| 1716 |
return -1; |
| 1717 |
} |
| 1718 |
|
| 1719 |
enc->tmpstream = jas_stream_memopen(0, 0); |
| 1720 |
if (!enc->tmpstream) { |
| 1721 |
fprintf(stderr, "cannot open tmp file\n"); |
| 1722 |
return -1; |
| 1723 |
} |
| 1724 |
|
| 1725 |
/* Write the tile header. */ |
| 1726 |
enc->mrk = jpc_ms_create(JPC_MS_SOT); |
| 1727 |
if (!enc->mrk) |
| 1728 |
return -1; |
| 1729 |
sot = &enc->mrk->parms.sot; |
| 1730 |
sot->len = 0; |
| 1731 |
sot->tileno = tileno; |
| 1732 |
sot->partno = 0; |
| 1733 |
sot->numparts = 1; |
| 1734 |
if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { |
| 1735 |
fprintf(stderr, "cannot write SOT marker\n"); |
| 1736 |
return -1; |
| 1737 |
} |
| 1738 |
jpc_ms_destroy(enc->mrk); |
| 1739 |
enc->mrk = 0; |
| 1740 |
|
| 1741 |
/************************************************************************/ |
| 1742 |
/************************************************************************/ |
| 1743 |
/************************************************************************/ |
| 1744 |
|
| 1745 |
tccp = &cp->tccp; |
| 1746 |
for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { |
| 1747 |
jpc_enc_tcmpt_t * const comp = &tile->tcmpts[cmptno]; |
| 1748 |
jpc_enc_tcmpt_t * const comp0 = &tile->tcmpts[0]; |
| 1749 |
|
| 1750 |
if (comp->numrlvls != tccp->maxrlvls) { |
| 1751 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { |
| 1752 |
return -1; |
| 1753 |
} |
| 1754 |
/* XXX = this is not really correct. we are using comp #0's |
| 1755 |
precint sizes and other characteristics */ |
| 1756 |
cod = &enc->mrk->parms.cod; |
| 1757 |
cod->compparms.csty = 0; |
| 1758 |
cod->compparms.numdlvls = comp0->numrlvls - 1; |
| 1759 |
cod->prg = tile->prg; |
| 1760 |
cod->numlyrs = tile->numlyrs; |
| 1761 |
cod->compparms.cblkwidthval = |
| 1762 |
JPC_COX_CBLKSIZEEXPN(comp0->cblkwidthexpn); |
| 1763 |
cod->compparms.cblkheightval = |
| 1764 |
JPC_COX_CBLKSIZEEXPN(comp0->cblkheightexpn); |
| 1765 |
cod->compparms.cblksty = comp0->cblksty; |
| 1766 |
cod->compparms.qmfbid = comp0->qmfbid; |
| 1767 |
cod->mctrans = (tile->mctid != JPC_MCT_NONE); |
| 1768 |
for (i = 0; i < comp0->numrlvls; ++i) { |
| 1769 |
cod->compparms.rlvls[i].parwidthval = |
| 1770 |
comp0->rlvls[i].prcwidthexpn; |
| 1771 |
cod->compparms.rlvls[i].parheightval = |
| 1772 |
comp0->rlvls[i].prcheightexpn; |
| 1773 |
} |
| 1774 |
if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { |
| 1775 |
return -1; |
| 1776 |
} |
| 1777 |
jpc_ms_destroy(enc->mrk); |
| 1778 |
enc->mrk = 0; |
| 1779 |
} |
| 1780 |
} |
| 1781 |
|
| 1782 |
for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { |
| 1783 |
jpc_enc_tcmpt_t * const comp = &tile->tcmpts[cmptno]; |
| 1784 |
|
| 1785 |
ccps = &cp->ccps[cmptno]; |
| 1786 |
if (ccps->numstepsizes == comp->numstepsizes) { |
| 1787 |
unsigned int bandno; |
| 1788 |
samestepsizes = 1; |
| 1789 |
for (bandno = 0; bandno < ccps->numstepsizes; ++bandno) { |
| 1790 |
if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) { |
| 1791 |
samestepsizes = 0; |
| 1792 |
break; |
| 1793 |
} |
| 1794 |
} |
| 1795 |
} else { |
| 1796 |
samestepsizes = 0; |
| 1797 |
} |
| 1798 |
if (!samestepsizes) { |
| 1799 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { |
| 1800 |
return -1; |
| 1801 |
} |
| 1802 |
qcc = &enc->mrk->parms.qcc; |
| 1803 |
qcc->compno = cmptno; |
| 1804 |
qcc->compparms.numguard = cp->tccp.numgbits; |
| 1805 |
qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ? |
| 1806 |
JPC_QCX_SEQNT : JPC_QCX_NOQNT; |
| 1807 |
qcc->compparms.numstepsizes = comp->numstepsizes; |
| 1808 |
qcc->compparms.stepsizes = comp->stepsizes; |
| 1809 |
if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { |
| 1810 |
return -1; |
| 1811 |
} |
| 1812 |
qcc->compparms.stepsizes = 0; |
| 1813 |
jpc_ms_destroy(enc->mrk); |
| 1814 |
enc->mrk = 0; |
| 1815 |
} |
| 1816 |
} |
| 1817 |
|
| 1818 |
/* Write a SOD marker to indicate the end of the tile header. */ |
| 1819 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) { |
| 1820 |
return -1; |
| 1821 |
} |
| 1822 |
if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { |
| 1823 |
fprintf(stderr, "cannot write SOD marker\n"); |
| 1824 |
return -1; |
| 1825 |
} |
| 1826 |
jpc_ms_destroy(enc->mrk); |
| 1827 |
enc->mrk = 0; |
| 1828 |
tilehdrlen = jas_stream_getrwcount(enc->tmpstream); |
| 1829 |
|
| 1830 |
/************************************************************************/ |
| 1831 |
/************************************************************************/ |
| 1832 |
/************************************************************************/ |
| 1833 |
|
| 1834 |
encodeTileBody(enc, tilehdrlen, &error); |
| 1835 |
/* Encodes current tile; writes to output file */ |
| 1836 |
|
| 1837 |
if (error) { |
| 1838 |
fprintf(stderr, "Failed to encode body of tile %u. %s\n", |
| 1839 |
tileno, error); |
| 1840 |
pm_strfree(error); |
| 1841 |
return -1; |
| 1842 |
} |
| 1843 |
tilelen = jas_stream_tell(enc->tmpstream); |
| 1844 |
|
| 1845 |
if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) { |
| 1846 |
return -1; |
| 1847 |
} |
| 1848 |
jpc_putuint32(enc->tmpstream, tilelen); |
| 1849 |
|
| 1850 |
if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) { |
| 1851 |
return -1; |
| 1852 |
} |
| 1853 |
if (jpc_putdata(enc->out, enc->tmpstream, -1)) { |
| 1854 |
return -1; |
| 1855 |
} |
| 1856 |
enc->len += tilelen; |
| 1857 |
|
| 1858 |
jas_stream_close(enc->tmpstream); |
| 1859 |
enc->tmpstream = 0; |
| 1860 |
|
| 1861 |
jpc_enc_tile_destroy(enc->curtile); |
| 1862 |
enc->curtile = 0; |
| 1863 |
|
| 1864 |
} |
| 1865 |
|
| 1866 |
return 0; |
| 1867 |
} |
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
int |
| 1872 |
jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr) { |
| 1873 |
|
| 1874 |
jpc_enc_t *enc; |
| 1875 |
jpc_enc_cp_t *cp; |
| 1876 |
|
| 1877 |
enc = 0; |
| 1878 |
cp = 0; |
| 1879 |
|
| 1880 |
jpc_initluts(); |
| 1881 |
|
| 1882 |
if (!(cp = cp_create(optstr, image))) { |
| 1883 |
fprintf(stderr, "invalid JP encoder options\n"); |
| 1884 |
goto error; |
| 1885 |
} |
| 1886 |
|
| 1887 |
if (!(enc = jpc_enc_create(cp, out, image))) { |
| 1888 |
goto error; |
| 1889 |
} |
| 1890 |
cp = 0; |
| 1891 |
|
| 1892 |
/* Encode the main header. */ |
| 1893 |
if (encodemainhdr(enc)) { |
| 1894 |
goto error; |
| 1895 |
} |
| 1896 |
|
| 1897 |
/* Encode the main body. This constitutes most of the encoding work. */ |
| 1898 |
if (encodemainbody(enc)) { |
| 1899 |
goto error; |
| 1900 |
} |
| 1901 |
|
| 1902 |
/* Write EOC marker segment. */ |
| 1903 |
if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) { |
| 1904 |
goto error; |
| 1905 |
} |
| 1906 |
if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { |
| 1907 |
fprintf(stderr, "cannot write EOI marker\n"); |
| 1908 |
goto error; |
| 1909 |
} |
| 1910 |
jpc_ms_destroy(enc->mrk); |
| 1911 |
enc->mrk = 0; |
| 1912 |
|
| 1913 |
if (jas_stream_flush(enc->out)) { |
| 1914 |
goto error; |
| 1915 |
} |
| 1916 |
|
| 1917 |
jpc_enc_destroy(enc); |
| 1918 |
|
| 1919 |
return 0; |
| 1920 |
|
| 1921 |
error: |
| 1922 |
if (cp) { |
| 1923 |
cp_destroy(cp); |
| 1924 |
} |
| 1925 |
if (enc) { |
| 1926 |
jpc_enc_destroy(enc); |
| 1927 |
} |
| 1928 |
return -1; |
| 1929 |
} |
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
/*****************************************************************************\ |
| 1934 |
* Encoder constructor and destructor. |
| 1935 |
\*****************************************************************************/ |
| 1936 |
|
| 1937 |
jpc_enc_t * |
| 1938 |
jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image) { |
| 1939 |
|
| 1940 |
jpc_enc_t *enc; |
| 1941 |
|
| 1942 |
enc = 0; |
| 1943 |
|
| 1944 |
if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) { |
| 1945 |
goto error; |
| 1946 |
} |
| 1947 |
|
| 1948 |
enc->image = image; |
| 1949 |
enc->out = out; |
| 1950 |
enc->cp = cp; |
| 1951 |
enc->cstate = 0; |
| 1952 |
enc->tmpstream = 0; |
| 1953 |
enc->mrk = 0; |
| 1954 |
enc->curtile = 0; |
| 1955 |
|
| 1956 |
if (!(enc->cstate = jpc_cstate_create())) { |
| 1957 |
goto error; |
| 1958 |
} |
| 1959 |
enc->len = 0; |
| 1960 |
enc->mainbodysize = 0; |
| 1961 |
|
| 1962 |
return enc; |
| 1963 |
|
| 1964 |
error: |
| 1965 |
|
| 1966 |
if (enc) { |
| 1967 |
jpc_enc_destroy(enc); |
| 1968 |
} |
| 1969 |
return 0; |
| 1970 |
} |
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
void |
| 1975 |
jpc_enc_destroy(jpc_enc_t *enc) { |
| 1976 |
|
| 1977 |
/* The image object (i.e., enc->image) and output stream object |
| 1978 |
(i.e., enc->out) are created outside of the encoder. |
| 1979 |
Therefore, they must not be destroyed here. |
| 1980 |
*/ |
| 1981 |
|
| 1982 |
if (enc->curtile) { |
| 1983 |
jpc_enc_tile_destroy(enc->curtile); |
| 1984 |
} |
| 1985 |
if (enc->cp) { |
| 1986 |
cp_destroy(enc->cp); |
| 1987 |
} |
| 1988 |
if (enc->cstate) { |
| 1989 |
jpc_cstate_destroy(enc->cstate); |
| 1990 |
} |
| 1991 |
if (enc->tmpstream) { |
| 1992 |
jas_stream_close(enc->tmpstream); |
| 1993 |
} |
| 1994 |
|
| 1995 |
jas_free(enc); |
| 1996 |
} |
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
void |
| 2001 |
quantize(jas_matrix_t *data, jpc_fix_t stepsize) { |
| 2002 |
|
| 2003 |
int i; |
| 2004 |
int j; |
| 2005 |
jpc_fix_t t; |
| 2006 |
|
| 2007 |
if (stepsize == jpc_inttofix(1)) { |
| 2008 |
return; |
| 2009 |
} |
| 2010 |
|
| 2011 |
for (i = 0; i < jas_matrix_numrows(data); ++i) { |
| 2012 |
for (j = 0; j < jas_matrix_numcols(data); ++j) { |
| 2013 |
t = jas_matrix_get(data, i, j); |
| 2014 |
|
| 2015 |
{ |
| 2016 |
if (t < 0) { |
| 2017 |
t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize)); |
| 2018 |
} else { |
| 2019 |
t = jpc_fix_div(t, stepsize); |
| 2020 |
} |
| 2021 |
} |
| 2022 |
|
| 2023 |
jas_matrix_set(data, i, j, t); |
| 2024 |
} |
| 2025 |
} |
| 2026 |
} |
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
/*****************************************************************************\ |
| 2031 |
* Tile constructors and destructors. |
| 2032 |
\*****************************************************************************/ |
| 2033 |
|
| 2034 |
jpc_enc_tile_t * |
| 2035 |
jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno) { |
| 2036 |
|
| 2037 |
jpc_enc_tile_t *tile; |
| 2038 |
uint_fast32_t htileno; |
| 2039 |
uint_fast32_t vtileno; |
| 2040 |
uint_fast16_t lyrno; |
| 2041 |
uint_fast16_t cmptno; |
| 2042 |
jpc_enc_tcmpt_t *tcmpt; |
| 2043 |
|
| 2044 |
if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) { |
| 2045 |
goto error; |
| 2046 |
} |
| 2047 |
|
| 2048 |
/* Initialize a few members used in error recovery. */ |
| 2049 |
tile->tcmpts = 0; |
| 2050 |
tile->lyrsizes = 0; |
| 2051 |
tile->numtcmpts = cp->numcmpts; |
| 2052 |
tile->pi = 0; |
| 2053 |
|
| 2054 |
tile->tileno = tileno; |
| 2055 |
htileno = tileno % cp->numhtiles; |
| 2056 |
vtileno = tileno / cp->numhtiles; |
| 2057 |
|
| 2058 |
/* Calculate the coordinates of the top-left and bottom-right |
| 2059 |
corners of the tile. */ |
| 2060 |
tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth, |
| 2061 |
cp->imgareatlx); |
| 2062 |
tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight, |
| 2063 |
cp->imgareatly); |
| 2064 |
tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth, |
| 2065 |
cp->refgrdwidth); |
| 2066 |
tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight, |
| 2067 |
cp->refgrdheight); |
| 2068 |
|
| 2069 |
/* Initialize some tile coding parameters. */ |
| 2070 |
tile->intmode = cp->tcp.intmode; |
| 2071 |
tile->csty = cp->tcp.csty; |
| 2072 |
tile->prg = cp->tcp.prg; |
| 2073 |
tile->mctid = cp->tcp.mctid; |
| 2074 |
|
| 2075 |
tile->numlyrs = cp->tcp.numlyrs; |
| 2076 |
if (!(tile->lyrsizes = jas_malloc(tile->numlyrs * |
| 2077 |
sizeof(uint_fast32_t)))) { |
| 2078 |
goto error; |
| 2079 |
} |
| 2080 |
for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { |
| 2081 |
tile->lyrsizes[lyrno] = 0; |
| 2082 |
} |
| 2083 |
|
| 2084 |
/* Allocate an array for the per-tile-component information. */ |
| 2085 |
if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) { |
| 2086 |
goto error; |
| 2087 |
} |
| 2088 |
/* Initialize a few members critical for error recovery. */ |
| 2089 |
for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; |
| 2090 |
++cmptno, ++tcmpt) { |
| 2091 |
tcmpt->rlvls = 0; |
| 2092 |
tcmpt->tsfb = 0; |
| 2093 |
tcmpt->data = 0; |
| 2094 |
} |
| 2095 |
/* Initialize the per-tile-component information. */ |
| 2096 |
for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; |
| 2097 |
++cmptno, ++tcmpt) { |
| 2098 |
if (!tcmpt_create(tcmpt, cp, image, tile)) { |
| 2099 |
goto error; |
| 2100 |
} |
| 2101 |
} |
| 2102 |
|
| 2103 |
/* Initialize the synthesis weights for the MCT. */ |
| 2104 |
switch (tile->mctid) { |
| 2105 |
case JPC_MCT_RCT: |
| 2106 |
tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0)); |
| 2107 |
tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875)); |
| 2108 |
tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875)); |
| 2109 |
break; |
| 2110 |
case JPC_MCT_ICT: |
| 2111 |
tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000)); |
| 2112 |
tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584)); |
| 2113 |
tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755)); |
| 2114 |
break; |
| 2115 |
default: |
| 2116 |
case JPC_MCT_NONE: |
| 2117 |
for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; |
| 2118 |
++cmptno, ++tcmpt) { |
| 2119 |
tcmpt->synweight = JPC_FIX_ONE; |
| 2120 |
} |
| 2121 |
break; |
| 2122 |
} |
| 2123 |
|
| 2124 |
if (!(tile->pi = jpc_enc_pi_create(cp, tile))) { |
| 2125 |
goto error; |
| 2126 |
} |
| 2127 |
|
| 2128 |
return tile; |
| 2129 |
|
| 2130 |
error: |
| 2131 |
|
| 2132 |
if (tile) { |
| 2133 |
jpc_enc_tile_destroy(tile); |
| 2134 |
} |
| 2135 |
return 0; |
| 2136 |
} |
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
void |
| 2141 |
jpc_enc_tile_destroy(jpc_enc_tile_t *tile) { |
| 2142 |
|
| 2143 |
jpc_enc_tcmpt_t *tcmpt; |
| 2144 |
uint_fast16_t cmptno; |
| 2145 |
|
| 2146 |
if (tile->tcmpts) { |
| 2147 |
for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < |
| 2148 |
tile->numtcmpts; ++cmptno, ++tcmpt) { |
| 2149 |
tcmpt_destroy(tcmpt); |
| 2150 |
} |
| 2151 |
jas_free(tile->tcmpts); |
| 2152 |
} |
| 2153 |
if (tile->lyrsizes) { |
| 2154 |
jas_free(tile->lyrsizes); |
| 2155 |
} |
| 2156 |
if (tile->pi) { |
| 2157 |
jpc_pi_destroy(tile->pi); |
| 2158 |
} |
| 2159 |
jas_free(tile); |
| 2160 |
} |
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
static jpc_enc_tcmpt_t * |
| 2165 |
tcmpt_create(jpc_enc_tcmpt_t * const tcmpt, |
| 2166 |
jpc_enc_cp_t * const cp, |
| 2167 |
jas_image_t * const image, |
| 2168 |
jpc_enc_tile_t * const tile) { |
| 2169 |
|
| 2170 |
uint_fast16_t cmptno; |
| 2171 |
uint_fast16_t rlvlno; |
| 2172 |
jpc_enc_rlvl_t *rlvl; |
| 2173 |
uint_fast32_t tlx; |
| 2174 |
uint_fast32_t tly; |
| 2175 |
uint_fast32_t brx; |
| 2176 |
uint_fast32_t bry; |
| 2177 |
uint_fast32_t cmpttlx; |
| 2178 |
uint_fast32_t cmpttly; |
| 2179 |
jpc_enc_ccp_t *ccp; |
| 2180 |
jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; |
| 2181 |
|
| 2182 |
tcmpt->tile = tile; |
| 2183 |
tcmpt->tsfb = 0; |
| 2184 |
tcmpt->data = 0; |
| 2185 |
tcmpt->rlvls = 0; |
| 2186 |
|
| 2187 |
/* Deduce the component number. */ |
| 2188 |
cmptno = tcmpt - tile->tcmpts; |
| 2189 |
|
| 2190 |
ccp = &cp->ccps[cmptno]; |
| 2191 |
|
| 2192 |
/* Compute the coordinates of the top-left and bottom-right |
| 2193 |
corners of this tile-component. */ |
| 2194 |
tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx); |
| 2195 |
tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy); |
| 2196 |
brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx); |
| 2197 |
bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy); |
| 2198 |
|
| 2199 |
/* Create a sequence to hold the tile-component sample data. */ |
| 2200 |
tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry); |
| 2201 |
if (!tcmpt->data) |
| 2202 |
goto error; |
| 2203 |
|
| 2204 |
/* Get the image data associated with this tile-component. */ |
| 2205 |
cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx); |
| 2206 |
cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy); |
| 2207 |
if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly, |
| 2208 |
brx - tlx, bry - tly, tcmpt->data)) { |
| 2209 |
goto error; |
| 2210 |
} |
| 2211 |
|
| 2212 |
tcmpt->synweight = 0; |
| 2213 |
tcmpt->qmfbid = cp->tccp.qmfbid; |
| 2214 |
tcmpt->numrlvls = cp->tccp.maxrlvls; |
| 2215 |
tcmpt->numbands = 3 * tcmpt->numrlvls - 2; |
| 2216 |
|
| 2217 |
tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1); |
| 2218 |
|
| 2219 |
if (!tcmpt->tsfb) |
| 2220 |
goto error; |
| 2221 |
|
| 2222 |
for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) { |
| 2223 |
tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno]; |
| 2224 |
tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno]; |
| 2225 |
} |
| 2226 |
tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn; |
| 2227 |
tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn; |
| 2228 |
tcmpt->cblksty = cp->tccp.cblksty; |
| 2229 |
tcmpt->csty = cp->tccp.csty; |
| 2230 |
|
| 2231 |
tcmpt->numstepsizes = tcmpt->numbands; |
| 2232 |
assert(tcmpt->numstepsizes <= JPC_MAXBANDS); |
| 2233 |
memset(tcmpt->stepsizes, 0, |
| 2234 |
sizeof(tcmpt->numstepsizes * sizeof(uint_fast16_t))); |
| 2235 |
|
| 2236 |
/* Retrieve information about the various bands. */ |
| 2237 |
jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data), |
| 2238 |
jas_seq2d_ystart(tcmpt->data), |
| 2239 |
jas_seq2d_xend(tcmpt->data), |
| 2240 |
jas_seq2d_yend(tcmpt->data), bandinfos); |
| 2241 |
|
| 2242 |
tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)); |
| 2243 |
if (!tcmpt->rlvls) |
| 2244 |
goto error; |
| 2245 |
|
| 2246 |
for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; |
| 2247 |
++rlvlno, ++rlvl) { |
| 2248 |
rlvl->bands = 0; |
| 2249 |
rlvl->tcmpt = tcmpt; |
| 2250 |
} |
| 2251 |
for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; |
| 2252 |
++rlvlno, ++rlvl) { |
| 2253 |
if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) { |
| 2254 |
goto error; |
| 2255 |
} |
| 2256 |
} |
| 2257 |
|
| 2258 |
return tcmpt; |
| 2259 |
|
| 2260 |
error: |
| 2261 |
|
| 2262 |
tcmpt_destroy(tcmpt); |
| 2263 |
return 0; |
| 2264 |
|
| 2265 |
} |
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
static void |
| 2270 |
tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) { |
| 2271 |
|
| 2272 |
jpc_enc_rlvl_t *rlvl; |
| 2273 |
uint_fast16_t rlvlno; |
| 2274 |
|
| 2275 |
if (tcmpt->rlvls) { |
| 2276 |
for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; |
| 2277 |
++rlvlno, ++rlvl) { |
| 2278 |
rlvl_destroy(rlvl); |
| 2279 |
} |
| 2280 |
jas_free(tcmpt->rlvls); |
| 2281 |
} |
| 2282 |
|
| 2283 |
if (tcmpt->data) { |
| 2284 |
jas_seq2d_destroy(tcmpt->data); |
| 2285 |
} |
| 2286 |
if (tcmpt->tsfb) { |
| 2287 |
jpc_tsfb_destroy(tcmpt->tsfb); |
| 2288 |
} |
| 2289 |
} |
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
static jpc_enc_rlvl_t * |
| 2294 |
rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, |
| 2295 |
jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos) { |
| 2296 |
|
| 2297 |
uint_fast16_t rlvlno; |
| 2298 |
uint_fast32_t tlprctlx; |
| 2299 |
uint_fast32_t tlprctly; |
| 2300 |
uint_fast32_t brprcbrx; |
| 2301 |
uint_fast32_t brprcbry; |
| 2302 |
uint_fast16_t bandno; |
| 2303 |
jpc_enc_band_t *band; |
| 2304 |
|
| 2305 |
/* Deduce the resolution level. */ |
| 2306 |
rlvlno = rlvl - tcmpt->rlvls; |
| 2307 |
|
| 2308 |
/* Initialize members required for error recovery. */ |
| 2309 |
rlvl->bands = 0; |
| 2310 |
rlvl->tcmpt = tcmpt; |
| 2311 |
|
| 2312 |
/* Compute the coordinates of the top-left and bottom-right |
| 2313 |
corners of the tile-component at this resolution. */ |
| 2314 |
rlvl->tlx = |
| 2315 |
JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls - |
| 2316 |
1 - rlvlno); |
| 2317 |
rlvl->tly = |
| 2318 |
JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls - |
| 2319 |
1 - rlvlno); |
| 2320 |
rlvl->brx = |
| 2321 |
JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls - |
| 2322 |
1 - rlvlno); |
| 2323 |
rlvl->bry = |
| 2324 |
JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls - |
| 2325 |
1 - rlvlno); |
| 2326 |
|
| 2327 |
if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) { |
| 2328 |
rlvl->numhprcs = 0; |
| 2329 |
rlvl->numvprcs = 0; |
| 2330 |
rlvl->numprcs = 0; |
| 2331 |
return rlvl; |
| 2332 |
} |
| 2333 |
|
| 2334 |
rlvl->numbands = (!rlvlno) ? 1 : 3; |
| 2335 |
rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno]; |
| 2336 |
rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno]; |
| 2337 |
if (!rlvlno) { |
| 2338 |
rlvl->cbgwidthexpn = rlvl->prcwidthexpn; |
| 2339 |
rlvl->cbgheightexpn = rlvl->prcheightexpn; |
| 2340 |
} else { |
| 2341 |
rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; |
| 2342 |
rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; |
| 2343 |
} |
| 2344 |
rlvl->cblkwidthexpn = |
| 2345 |
JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn); |
| 2346 |
rlvl->cblkheightexpn = |
| 2347 |
JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn); |
| 2348 |
|
| 2349 |
/* Compute the number of precincts. */ |
| 2350 |
tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); |
| 2351 |
tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); |
| 2352 |
brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn); |
| 2353 |
brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn); |
| 2354 |
rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn); |
| 2355 |
rlvl->numvprcs = |
| 2356 |
JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn); |
| 2357 |
rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; |
| 2358 |
|
| 2359 |
rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)); |
| 2360 |
if (!rlvl->bands) |
| 2361 |
goto error; |
| 2362 |
|
| 2363 |
for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
| 2364 |
++bandno, ++band) { |
| 2365 |
band->prcs = 0; |
| 2366 |
band->data = 0; |
| 2367 |
band->rlvl = rlvl; |
| 2368 |
} |
| 2369 |
for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
| 2370 |
++bandno, ++band) { |
| 2371 |
if (!band_create(band, cp, rlvl, bandinfos)) { |
| 2372 |
goto error; |
| 2373 |
} |
| 2374 |
} |
| 2375 |
|
| 2376 |
return rlvl; |
| 2377 |
error: |
| 2378 |
|
| 2379 |
rlvl_destroy(rlvl); |
| 2380 |
return 0; |
| 2381 |
} |
| 2382 |
|
| 2383 |
|
| 2384 |
|
| 2385 |
static void |
| 2386 |
rlvl_destroy(jpc_enc_rlvl_t *rlvl) { |
| 2387 |
|
| 2388 |
jpc_enc_band_t *band; |
| 2389 |
uint_fast16_t bandno; |
| 2390 |
|
| 2391 |
if (rlvl->bands) { |
| 2392 |
for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
| 2393 |
++bandno, ++band) { |
| 2394 |
band_destroy(band); |
| 2395 |
} |
| 2396 |
jas_free(rlvl->bands); |
| 2397 |
} |
| 2398 |
} |
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
static jpc_enc_band_t * |
| 2403 |
band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp, |
| 2404 |
jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos) { |
| 2405 |
|
| 2406 |
uint_fast16_t bandno; |
| 2407 |
uint_fast16_t gblbandno; |
| 2408 |
uint_fast16_t rlvlno; |
| 2409 |
jpc_tsfb_band_t *bandinfo; |
| 2410 |
jpc_enc_tcmpt_t *tcmpt; |
| 2411 |
uint_fast32_t prcno; |
| 2412 |
jpc_enc_prc_t *prc; |
| 2413 |
|
| 2414 |
tcmpt = rlvl->tcmpt; |
| 2415 |
band->data = 0; |
| 2416 |
band->prcs = 0; |
| 2417 |
band->rlvl = rlvl; |
| 2418 |
|
| 2419 |
/* Deduce the resolution level and band number. */ |
| 2420 |
rlvlno = rlvl - rlvl->tcmpt->rlvls; |
| 2421 |
bandno = band - rlvl->bands; |
| 2422 |
gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1); |
| 2423 |
|
| 2424 |
bandinfo = &bandinfos[gblbandno]; |
| 2425 |
|
| 2426 |
if (bandinfo->xstart != bandinfo->xend && |
| 2427 |
bandinfo->ystart != bandinfo->yend) { |
| 2428 |
band->data = jas_seq2d_create(0, 0, 0, 0); |
| 2429 |
if (!band->data) |
| 2430 |
goto error; |
| 2431 |
jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart, |
| 2432 |
bandinfo->locystart, bandinfo->locxend, |
| 2433 |
bandinfo->locyend); |
| 2434 |
jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart); |
| 2435 |
} |
| 2436 |
band->orient = bandinfo->orient; |
| 2437 |
band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno, |
| 2438 |
band->orient); |
| 2439 |
band->numbps = 0; |
| 2440 |
band->absstepsize = 0; |
| 2441 |
band->stepsize = 0; |
| 2442 |
band->synweight = bandinfo->synenergywt; |
| 2443 |
|
| 2444 |
if (band->data) { |
| 2445 |
band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)); |
| 2446 |
if (!band->prcs) |
| 2447 |
goto error; |
| 2448 |
for (prcno = 0, prc = band->prcs; |
| 2449 |
prcno < rlvl->numprcs; |
| 2450 |
++prcno, ++prc) { |
| 2451 |
prc->cblks = 0; |
| 2452 |
prc->incltree = 0; |
| 2453 |
prc->nlibtree = 0; |
| 2454 |
prc->savincltree = 0; |
| 2455 |
prc->savnlibtree = 0; |
| 2456 |
prc->band = band; |
| 2457 |
} |
| 2458 |
for (prcno = 0, prc = band->prcs; |
| 2459 |
prcno < rlvl->numprcs; |
| 2460 |
++prcno, ++prc) { |
| 2461 |
if (!prc_create(prc, cp, band)) { |
| 2462 |
goto error; |
| 2463 |
} |
| 2464 |
} |
| 2465 |
} |
| 2466 |
|
| 2467 |
return band; |
| 2468 |
|
| 2469 |
error: |
| 2470 |
band_destroy(band); |
| 2471 |
return 0; |
| 2472 |
} |
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
static void |
| 2477 |
band_destroy(jpc_enc_band_t *band) { |
| 2478 |
|
| 2479 |
jpc_enc_prc_t *prc; |
| 2480 |
jpc_enc_rlvl_t *rlvl; |
| 2481 |
uint_fast32_t prcno; |
| 2482 |
|
| 2483 |
if (band->prcs) { |
| 2484 |
rlvl = band->rlvl; |
| 2485 |
for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; |
| 2486 |
++prcno, ++prc) { |
| 2487 |
prc_destroy(prc); |
| 2488 |
} |
| 2489 |
jas_free(band->prcs); |
| 2490 |
} |
| 2491 |
if (band->data) { |
| 2492 |
jas_seq2d_destroy(band->data); |
| 2493 |
} |
| 2494 |
} |
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
static jpc_enc_prc_t * |
| 2499 |
prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band) { |
| 2500 |
|
| 2501 |
uint_fast32_t prcno; |
| 2502 |
uint_fast32_t prcxind; |
| 2503 |
uint_fast32_t prcyind; |
| 2504 |
uint_fast32_t cbgtlx; |
| 2505 |
uint_fast32_t cbgtly; |
| 2506 |
uint_fast32_t tlprctlx; |
| 2507 |
uint_fast32_t tlprctly; |
| 2508 |
uint_fast32_t tlcbgtlx; |
| 2509 |
uint_fast32_t tlcbgtly; |
| 2510 |
uint_fast16_t rlvlno; |
| 2511 |
jpc_enc_rlvl_t *rlvl; |
| 2512 |
uint_fast32_t tlcblktlx; |
| 2513 |
uint_fast32_t tlcblktly; |
| 2514 |
uint_fast32_t brcblkbrx; |
| 2515 |
uint_fast32_t brcblkbry; |
| 2516 |
uint_fast32_t cblkno; |
| 2517 |
jpc_enc_cblk_t *cblk; |
| 2518 |
jpc_enc_tcmpt_t *tcmpt; |
| 2519 |
|
| 2520 |
prc->cblks = 0; |
| 2521 |
prc->incltree = 0; |
| 2522 |
prc->savincltree = 0; |
| 2523 |
prc->nlibtree = 0; |
| 2524 |
prc->savnlibtree = 0; |
| 2525 |
|
| 2526 |
rlvl = band->rlvl; |
| 2527 |
tcmpt = rlvl->tcmpt; |
| 2528 |
rlvlno = rlvl - tcmpt->rlvls; |
| 2529 |
prcno = prc - band->prcs; |
| 2530 |
prcxind = prcno % rlvl->numhprcs; |
| 2531 |
prcyind = prcno / rlvl->numhprcs; |
| 2532 |
prc->band = band; |
| 2533 |
|
| 2534 |
tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); |
| 2535 |
tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); |
| 2536 |
|
| 2537 |
if (!rlvlno) { |
| 2538 |
tlcbgtlx = tlprctlx; |
| 2539 |
tlcbgtly = tlprctly; |
| 2540 |
} else { |
| 2541 |
tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1); |
| 2542 |
tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1); |
| 2543 |
} |
| 2544 |
|
| 2545 |
/* Compute the coordinates of the top-left and bottom-right corners of the |
| 2546 |
precinct. |
| 2547 |
*/ |
| 2548 |
cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn); |
| 2549 |
cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn); |
| 2550 |
prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx); |
| 2551 |
prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly); |
| 2552 |
prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx + |
| 2553 |
(1 << rlvl->cbgwidthexpn)); |
| 2554 |
prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly + |
| 2555 |
(1 << rlvl->cbgheightexpn)); |
| 2556 |
|
| 2557 |
if (prc->tlx < prc->brx && prc->tly < prc->bry) { |
| 2558 |
/* The precinct contains at least one code block. */ |
| 2559 |
|
| 2560 |
tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); |
| 2561 |
tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); |
| 2562 |
brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn); |
| 2563 |
brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn); |
| 2564 |
prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx, |
| 2565 |
rlvl->cblkwidthexpn); |
| 2566 |
prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly, |
| 2567 |
rlvl->cblkheightexpn); |
| 2568 |
prc->numcblks = prc->numhcblks * prc->numvcblks; |
| 2569 |
|
| 2570 |
if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks, |
| 2571 |
prc->numvcblks))) { |
| 2572 |
goto error; |
| 2573 |
} |
| 2574 |
if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks, |
| 2575 |
prc->numvcblks))) { |
| 2576 |
goto error; |
| 2577 |
} |
| 2578 |
if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks, |
| 2579 |
prc->numvcblks))) { |
| 2580 |
goto error; |
| 2581 |
} |
| 2582 |
if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks, |
| 2583 |
prc->numvcblks))) { |
| 2584 |
goto error; |
| 2585 |
} |
| 2586 |
|
| 2587 |
prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)); |
| 2588 |
|
| 2589 |
if (!prc->cblks) |
| 2590 |
goto error; |
| 2591 |
for (cblkno = 0, cblk = prc->cblks; |
| 2592 |
cblkno < prc->numcblks; |
| 2593 |
++cblkno, ++cblk) { |
| 2594 |
cblk->passes = 0; |
| 2595 |
cblk->stream = 0; |
| 2596 |
cblk->mqenc = 0; |
| 2597 |
cblk->data = 0; |
| 2598 |
cblk->flags = 0; |
| 2599 |
cblk->prc = prc; |
| 2600 |
} |
| 2601 |
for (cblkno = 0, cblk = prc->cblks; |
| 2602 |
cblkno < prc->numcblks; |
| 2603 |
++cblkno, ++cblk) { |
| 2604 |
if (!cblk_create(cblk, cp, prc)) { |
| 2605 |
goto error; |
| 2606 |
} |
| 2607 |
} |
| 2608 |
} else { |
| 2609 |
/* The precinct does not contain any code blocks. */ |
| 2610 |
prc->tlx = prc->brx; |
| 2611 |
prc->tly = prc->bry; |
| 2612 |
prc->numcblks = 0; |
| 2613 |
prc->numhcblks = 0; |
| 2614 |
prc->numvcblks = 0; |
| 2615 |
prc->cblks = 0; |
| 2616 |
prc->incltree = 0; |
| 2617 |
prc->nlibtree = 0; |
| 2618 |
prc->savincltree = 0; |
| 2619 |
prc->savnlibtree = 0; |
| 2620 |
} |
| 2621 |
|
| 2622 |
return prc; |
| 2623 |
|
| 2624 |
error: |
| 2625 |
prc_destroy(prc); |
| 2626 |
return 0; |
| 2627 |
} |
| 2628 |
|
| 2629 |
|
| 2630 |
|
| 2631 |
static void |
| 2632 |
prc_destroy(jpc_enc_prc_t *prc) { |
| 2633 |
|
| 2634 |
jpc_enc_cblk_t *cblk; |
| 2635 |
uint_fast32_t cblkno; |
| 2636 |
|
| 2637 |
if (prc->cblks) { |
| 2638 |
for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; |
| 2639 |
++cblkno, ++cblk) { |
| 2640 |
cblk_destroy(cblk); |
| 2641 |
} |
| 2642 |
jas_free(prc->cblks); |
| 2643 |
} |
| 2644 |
if (prc->incltree) { |
| 2645 |
jpc_tagtree_destroy(prc->incltree); |
| 2646 |
} |
| 2647 |
if (prc->nlibtree) { |
| 2648 |
jpc_tagtree_destroy(prc->nlibtree); |
| 2649 |
} |
| 2650 |
if (prc->savincltree) { |
| 2651 |
jpc_tagtree_destroy(prc->savincltree); |
| 2652 |
} |
| 2653 |
if (prc->savnlibtree) { |
| 2654 |
jpc_tagtree_destroy(prc->savnlibtree); |
| 2655 |
} |
| 2656 |
} |
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
static jpc_enc_cblk_t * |
| 2661 |
cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc) { |
| 2662 |
|
| 2663 |
jpc_enc_band_t *band; |
| 2664 |
uint_fast32_t cblktlx; |
| 2665 |
uint_fast32_t cblktly; |
| 2666 |
uint_fast32_t cblkbrx; |
| 2667 |
uint_fast32_t cblkbry; |
| 2668 |
jpc_enc_rlvl_t *rlvl; |
| 2669 |
uint_fast32_t cblkxind; |
| 2670 |
uint_fast32_t cblkyind; |
| 2671 |
uint_fast32_t cblkno; |
| 2672 |
uint_fast32_t tlcblktlx; |
| 2673 |
uint_fast32_t tlcblktly; |
| 2674 |
|
| 2675 |
cblkno = cblk - prc->cblks; |
| 2676 |
cblkxind = cblkno % prc->numhcblks; |
| 2677 |
cblkyind = cblkno / prc->numhcblks; |
| 2678 |
rlvl = prc->band->rlvl; |
| 2679 |
cblk->prc = prc; |
| 2680 |
|
| 2681 |
cblk->numpasses = 0; |
| 2682 |
cblk->passes = 0; |
| 2683 |
cblk->numencpasses = 0; |
| 2684 |
cblk->numimsbs = 0; |
| 2685 |
cblk->numlenbits = 0; |
| 2686 |
cblk->stream = 0; |
| 2687 |
cblk->mqenc = 0; |
| 2688 |
cblk->flags = 0; |
| 2689 |
cblk->numbps = 0; |
| 2690 |
cblk->curpass = 0; |
| 2691 |
cblk->data = 0; |
| 2692 |
cblk->savedcurpass = 0; |
| 2693 |
cblk->savednumlenbits = 0; |
| 2694 |
cblk->savednumencpasses = 0; |
| 2695 |
|
| 2696 |
band = prc->band; |
| 2697 |
tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); |
| 2698 |
tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); |
| 2699 |
cblktlx = |
| 2700 |
JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx); |
| 2701 |
cblktly = |
| 2702 |
JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly); |
| 2703 |
cblkbrx = |
| 2704 |
JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn), prc->brx); |
| 2705 |
cblkbry = |
| 2706 |
JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn), |
| 2707 |
prc->bry); |
| 2708 |
|
| 2709 |
assert(cblktlx < cblkbrx && cblktly < cblkbry); |
| 2710 |
cblk->data = jas_seq2d_create(0, 0, 0, 0); |
| 2711 |
if (!cblk->data) |
| 2712 |
goto error; |
| 2713 |
jas_seq2d_bindsub(cblk->data, band->data, |
| 2714 |
cblktlx, cblktly, cblkbrx, cblkbry); |
| 2715 |
|
| 2716 |
return cblk; |
| 2717 |
|
| 2718 |
error: |
| 2719 |
cblk_destroy(cblk); |
| 2720 |
return 0; |
| 2721 |
} |
| 2722 |
|
| 2723 |
|
| 2724 |
|
| 2725 |
static void |
| 2726 |
cblk_destroy(jpc_enc_cblk_t *cblk) { |
| 2727 |
|
| 2728 |
uint_fast16_t passno; |
| 2729 |
jpc_enc_pass_t *pass; |
| 2730 |
if (cblk->passes) { |
| 2731 |
for (passno = 0, pass = cblk->passes; passno < cblk->numpasses; |
| 2732 |
++passno, ++pass) { |
| 2733 |
pass_destroy(pass); |
| 2734 |
} |
| 2735 |
jas_free(cblk->passes); |
| 2736 |
} |
| 2737 |
if (cblk->stream) { |
| 2738 |
jas_stream_close(cblk->stream); |
| 2739 |
} |
| 2740 |
if (cblk->mqenc) { |
| 2741 |
jpc_mqenc_destroy(cblk->mqenc); |
| 2742 |
} |
| 2743 |
if (cblk->data) { |
| 2744 |
jas_seq2d_destroy(cblk->data); |
| 2745 |
} |
| 2746 |
if (cblk->flags) { |
| 2747 |
jas_seq2d_destroy(cblk->flags); |
| 2748 |
} |
| 2749 |
} |
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
static void |
| 2754 |
pass_destroy(jpc_enc_pass_t *pass) { |
| 2755 |
|
| 2756 |
/* XXX - need to free resources here */ |
| 2757 |
} |
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
void |
| 2762 |
jpc_enc_dump(jpc_enc_t *enc) { |
| 2763 |
|
| 2764 |
jpc_enc_tile_t *tile; |
| 2765 |
jpc_enc_tcmpt_t *tcmpt; |
| 2766 |
jpc_enc_rlvl_t *rlvl; |
| 2767 |
jpc_enc_band_t *band; |
| 2768 |
jpc_enc_prc_t *prc; |
| 2769 |
jpc_enc_cblk_t *cblk; |
| 2770 |
uint_fast16_t cmptno; |
| 2771 |
uint_fast16_t rlvlno; |
| 2772 |
uint_fast16_t bandno; |
| 2773 |
uint_fast32_t prcno; |
| 2774 |
uint_fast32_t cblkno; |
| 2775 |
|
| 2776 |
tile = enc->curtile; |
| 2777 |
|
| 2778 |
for (cmptno = 0, tcmpt = tile->tcmpts; |
| 2779 |
cmptno < tile->numtcmpts; |
| 2780 |
++cmptno, ++tcmpt) { |
| 2781 |
fprintf(stderr, " tcmpt %5d %5d %5d %5d\n", |
| 2782 |
(int)jas_seq2d_xstart(tcmpt->data), |
| 2783 |
(int)jas_seq2d_ystart(tcmpt->data), |
| 2784 |
(int)jas_seq2d_xend(tcmpt->data), |
| 2785 |
(int)jas_seq2d_yend(tcmpt->data)); |
| 2786 |
for (rlvlno = 0, rlvl = tcmpt->rlvls; |
| 2787 |
rlvlno < tcmpt->numrlvls; |
| 2788 |
++rlvlno, ++rlvl) { |
| 2789 |
fprintf(stderr, " rlvl %5d %5d %5d %5d\n", |
| 2790 |
(int)rlvl->tlx, (int)rlvl->tly, |
| 2791 |
(int)rlvl->brx, (int)rlvl->bry); |
| 2792 |
for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; |
| 2793 |
++bandno, ++band) { |
| 2794 |
if (!band->data) { |
| 2795 |
continue; |
| 2796 |
} |
| 2797 |
fprintf(stderr, " band %5d %5d %5d %5d\n", |
| 2798 |
(int)jas_seq2d_xstart(band->data), |
| 2799 |
(int)jas_seq2d_ystart(band->data), |
| 2800 |
(int)jas_seq2d_xend(band->data), |
| 2801 |
(int)jas_seq2d_yend(band->data)); |
| 2802 |
for (prcno = 0, prc = band->prcs; |
| 2803 |
prcno < rlvl->numprcs; |
| 2804 |
++prcno, ++prc) { |
| 2805 |
fprintf(stderr, " prc %5d %5d %5d %5d (%5d %5d)\n", |
| 2806 |
(int)prc->tlx, (int)prc->tly, |
| 2807 |
(int)prc->brx, (int)prc->bry, |
| 2808 |
(int)(prc->brx - prc->tlx), |
| 2809 |
(int)(prc->bry - prc->tly)); |
| 2810 |
if (!prc->cblks) { |
| 2811 |
continue; |
| 2812 |
} |
| 2813 |
for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; |
| 2814 |
++cblkno, ++cblk) { |
| 2815 |
fprintf(stderr, " cblk %5d %5d %5d %5d\n", |
| 2816 |
(int)jas_seq2d_xstart(cblk->data), |
| 2817 |
(int)jas_seq2d_ystart(cblk->data), |
| 2818 |
(int)jas_seq2d_xend(cblk->data), |
| 2819 |
(int)jas_seq2d_yend(cblk->data)); |
| 2820 |
} |
| 2821 |
} |
| 2822 |
} |
| 2823 |
} |
| 2824 |
} |
| 2825 |
} |
| 2826 |
|
| 2827 |
|
| 2828 |
|
| 2829 |
/* |
| 2830 |
* Copyright (c) 1999-2000 Image Power, Inc. and the University of |
| 2831 |
* British Columbia. |
| 2832 |
* Copyright (c) 2001-2002 Michael David Adams. |
| 2833 |
* All rights reserved. |
| 2834 |
*/ |
| 2835 |
|
| 2836 |
/* __START_OF_JASPER_LICENSE__ |
| 2837 |
* |
| 2838 |
* JasPer Software License |
| 2839 |
* |
| 2840 |
* IMAGE POWER JPEG-2000 PUBLIC LICENSE |
| 2841 |
* ************************************ |
| 2842 |
* |
| 2843 |
* GRANT: |
| 2844 |
* |
| 2845 |
* Permission is hereby granted, free of charge, to any person (the "User") |
| 2846 |
* obtaining a copy of this software and associated documentation, to deal |
| 2847 |
* in the JasPer Software without restriction, including without limitation |
| 2848 |
* the right to use, copy, modify, merge, publish, distribute, sublicense, |
| 2849 |
* and/or sell copies of the JasPer Software (in source and binary forms), |
| 2850 |
* and to permit persons to whom the JasPer Software is furnished to do so, |
| 2851 |
* provided further that the License Conditions below are met. |
| 2852 |
* |
| 2853 |
* License Conditions |
| 2854 |
* ****************** |
| 2855 |
* |
| 2856 |
* A. Redistributions of source code must retain the above copyright notice, |
| 2857 |
* and this list of conditions, and the following disclaimer. |
| 2858 |
* |
| 2859 |
* B. Redistributions in binary form must reproduce the above copyright |
| 2860 |
* notice, and this list of conditions, and the following disclaimer in |
| 2861 |
* the documentation and/or other materials provided with the distribution. |
| 2862 |
* |
| 2863 |
* C. Neither the name of Image Power, Inc. nor any other contributor |
| 2864 |
* (including, but not limited to, the University of British Columbia and |
| 2865 |
* Michael David Adams) may be used to endorse or promote products derived |
| 2866 |
* from this software without specific prior written permission. |
| 2867 |
* |
| 2868 |
* D. User agrees that it shall not commence any action against Image Power, |
| 2869 |
* Inc., the University of British Columbia, Michael David Adams, or any |
| 2870 |
* other contributors (collectively "Licensors") for infringement of any |
| 2871 |
* intellectual property rights ("IPR") held by the User in respect of any |
| 2872 |
* technology that User owns or has a right to license or sublicense and |
| 2873 |
* which is an element required in order to claim compliance with ISO/IEC |
| 2874 |
* 15444-1 (i.e., JPEG-2000 Part 1). "IPR" means all intellectual property |
| 2875 |
* rights worldwide arising under statutory or common law, and whether |
| 2876 |
* or not perfected, including, without limitation, all (i) patents and |
| 2877 |
* patent applications owned or licensable by User; (ii) rights associated |
| 2878 |
* with works of authorship including copyrights, copyright applications, |
| 2879 |
* copyright registrations, mask work rights, mask work applications, |
| 2880 |
* mask work registrations; (iii) rights relating to the protection of |
| 2881 |
* trade secrets and confidential information; (iv) any right analogous |
| 2882 |
* to those set forth in subsections (i), (ii), or (iii) and any other |
| 2883 |
* proprietary rights relating to intangible property (other than trademark, |
| 2884 |
* trade dress, or service mark rights); and (v) divisions, continuations, |
| 2885 |
* renewals, reissues and extensions of the foregoing (as and to the extent |
| 2886 |
* applicable) now existing, hereafter filed, issued or acquired. |
| 2887 |
* |
| 2888 |
* E. If User commences an infringement action against any Licensor(s) then |
| 2889 |
* such Licensor(s) shall have the right to terminate User's license and |
| 2890 |
* all sublicenses that have been granted hereunder by User to other parties. |
| 2891 |
* |
| 2892 |
* F. This software is for use only in hardware or software products that |
| 2893 |
* are compliant with ISO/IEC 15444-1 (i.e., JPEG-2000 Part 1). No license |
| 2894 |
* or right to this Software is granted for products that do not comply |
| 2895 |
* with ISO/IEC 15444-1. The JPEG-2000 Part 1 standard can be purchased |
| 2896 |
* from the ISO. |
| 2897 |
* |
| 2898 |
* THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. |
| 2899 |
* NO USE OF THE JASPER SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER |
| 2900 |
* THIS DISCLAIMER. THE JASPER SOFTWARE IS PROVIDED BY THE LICENSORS AND |
| 2901 |
* CONTRIBUTORS UNDER THIS LICENSE ON AN ``AS-IS'' BASIS, WITHOUT WARRANTY |
| 2902 |
* OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, |
| 2903 |
* WARRANTIES THAT THE JASPER SOFTWARE IS FREE OF DEFECTS, IS MERCHANTABLE, |
| 2904 |
* IS FIT FOR A PARTICULAR PURPOSE OR IS NON-INFRINGING. THOSE INTENDING |
| 2905 |
* TO USE THE JASPER SOFTWARE OR MODIFICATIONS THEREOF FOR USE IN HARDWARE |
| 2906 |
* OR SOFTWARE PRODUCTS ARE ADVISED THAT THEIR USE MAY INFRINGE EXISTING |
| 2907 |
* PATENTS, COPYRIGHTS, TRADEMARKS, OR OTHER INTELLECTUAL PROPERTY RIGHTS. |
| 2908 |
* THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE JASPER SOFTWARE |
| 2909 |
* IS WITH THE USER. SHOULD ANY PART OF THE JASPER SOFTWARE PROVE DEFECTIVE |
| 2910 |
* IN ANY RESPECT, THE USER (AND NOT THE INITIAL DEVELOPERS, THE UNIVERSITY |
| 2911 |
* OF BRITISH COLUMBIA, IMAGE POWER, INC., MICHAEL DAVID ADAMS, OR ANY |
| 2912 |
* OTHER CONTRIBUTOR) SHALL ASSUME THE COST OF ANY NECESSARY SERVICING, |
| 2913 |
* REPAIR OR CORRECTION. UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, |
| 2914 |
* WHETHER TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL THE |
| 2915 |
* INITIAL DEVELOPER, THE UNIVERSITY OF BRITISH COLUMBIA, IMAGE POWER, INC., |
| 2916 |
* MICHAEL DAVID ADAMS, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF THE |
| 2917 |
* JASPER SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE LIABLE TO |
| 2918 |
* THE USER OR ANY OTHER PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR |
| 2919 |
* CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT LIMITATION, |
| 2920 |
* DAMAGES FOR LOSS OF GOODWILL, WORK STOPPAGE, COMPUTER FAILURE OR |
| 2921 |
* MALFUNCTION, OR ANY AND ALL OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF |
| 2922 |
* SUCH PARTY HAD BEEN INFORMED, OR OUGHT TO HAVE KNOWN, OF THE POSSIBILITY |
| 2923 |
* OF SUCH DAMAGES. THE JASPER SOFTWARE AND UNDERLYING TECHNOLOGY ARE NOT |
| 2924 |
* FAULT-TOLERANT AND ARE NOT DESIGNED, MANUFACTURED OR INTENDED FOR USE OR |
| 2925 |
* RESALE AS ON-LINE CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING |
| 2926 |
* FAIL-SAFE PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, |
| 2927 |
* AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT |
| 2928 |
* LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE |
| 2929 |
* JASPER SOFTWARE OR UNDERLYING TECHNOLOGY OR PRODUCT COULD LEAD DIRECTLY |
| 2930 |
* TO DEATH, PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE |
| 2931 |
* ("HIGH RISK ACTIVITIES"). LICENSOR SPECIFICALLY DISCLAIMS ANY EXPRESS |
| 2932 |
* OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. USER WILL NOT |
| 2933 |
* KNOWINGLY USE, DISTRIBUTE OR RESELL THE JASPER SOFTWARE OR UNDERLYING |
| 2934 |
* TECHNOLOGY OR PRODUCTS FOR HIGH RISK ACTIVITIES AND WILL ENSURE THAT ITS |
| 2935 |
* CUSTOMERS AND END-USERS OF ITS PRODUCTS ARE PROVIDED WITH A COPY OF THE |
| 2936 |
* NOTICE SPECIFIED IN THIS SECTION. |
| 2937 |
* |
| 2938 |
* __END_OF_JASPER_LICENSE__ |
| 2939 |
*/ |
| 2940 |
|