From 5a422aba704c375a307a902bafe658342e209906 Mon Sep 17 00:00:00 2001 From: scuri Date: Fri, 17 Oct 2008 06:10:15 +0000 Subject: First commit - moving from LuaForge to SourceForge --- src/libjasper/jpc/jpc_bs.c | 440 ++++++ src/libjasper/jpc/jpc_bs.h | 231 +++ src/libjasper/jpc/jpc_cod.h | 78 + src/libjasper/jpc/jpc_cs.c | 1644 ++++++++++++++++++++ src/libjasper/jpc/jpc_cs.h | 763 ++++++++++ src/libjasper/jpc/jpc_dec.c | 2348 +++++++++++++++++++++++++++++ src/libjasper/jpc/jpc_dec.h | 696 +++++++++ src/libjasper/jpc/jpc_enc.c | 2626 ++++++++++++++++++++++++++++++++ src/libjasper/jpc/jpc_enc.h | 646 ++++++++ src/libjasper/jpc/jpc_fix.h | 144 ++ src/libjasper/jpc/jpc_flt.h | 80 + src/libjasper/jpc/jpc_math.c | 121 ++ src/libjasper/jpc/jpc_math.h | 99 ++ src/libjasper/jpc/jpc_mct.c | 291 ++++ src/libjasper/jpc/jpc_mct.h | 111 ++ src/libjasper/jpc/jpc_mqcod.c | 179 +++ src/libjasper/jpc/jpc_mqcod.h | 124 ++ src/libjasper/jpc/jpc_mqdec.c | 306 ++++ src/libjasper/jpc/jpc_mqdec.h | 271 ++++ src/libjasper/jpc/jpc_mqenc.c | 392 +++++ src/libjasper/jpc/jpc_mqenc.h | 236 +++ src/libjasper/jpc/jpc_qmfb.c | 3152 +++++++++++++++++++++++++++++++++++++++ src/libjasper/jpc/jpc_qmfb.h | 113 ++ src/libjasper/jpc/jpc_t1cod.c | 497 ++++++ src/libjasper/jpc/jpc_t1cod.h | 295 ++++ src/libjasper/jpc/jpc_t1dec.c | 927 ++++++++++++ src/libjasper/jpc/jpc_t1dec.h | 88 ++ src/libjasper/jpc/jpc_t1enc.c | 964 ++++++++++++ src/libjasper/jpc/jpc_t1enc.h | 93 ++ src/libjasper/jpc/jpc_t2cod.c | 684 +++++++++ src/libjasper/jpc/jpc_t2cod.h | 299 ++++ src/libjasper/jpc/jpc_t2dec.c | 581 ++++++++ src/libjasper/jpc/jpc_t2dec.h | 95 ++ src/libjasper/jpc/jpc_t2enc.c | 655 ++++++++ src/libjasper/jpc/jpc_t2enc.h | 106 ++ src/libjasper/jpc/jpc_tagtree.c | 393 +++++ src/libjasper/jpc/jpc_tagtree.h | 167 +++ src/libjasper/jpc/jpc_tsfb.c | 288 ++++ src/libjasper/jpc/jpc_tsfb.h | 138 ++ src/libjasper/jpc/jpc_util.c | 194 +++ src/libjasper/jpc/jpc_util.h | 77 + 41 files changed, 21632 insertions(+) create mode 100644 src/libjasper/jpc/jpc_bs.c create mode 100644 src/libjasper/jpc/jpc_bs.h create mode 100644 src/libjasper/jpc/jpc_cod.h create mode 100644 src/libjasper/jpc/jpc_cs.c create mode 100644 src/libjasper/jpc/jpc_cs.h create mode 100644 src/libjasper/jpc/jpc_dec.c create mode 100644 src/libjasper/jpc/jpc_dec.h create mode 100644 src/libjasper/jpc/jpc_enc.c create mode 100644 src/libjasper/jpc/jpc_enc.h create mode 100644 src/libjasper/jpc/jpc_fix.h create mode 100644 src/libjasper/jpc/jpc_flt.h create mode 100644 src/libjasper/jpc/jpc_math.c create mode 100644 src/libjasper/jpc/jpc_math.h create mode 100644 src/libjasper/jpc/jpc_mct.c create mode 100644 src/libjasper/jpc/jpc_mct.h create mode 100644 src/libjasper/jpc/jpc_mqcod.c create mode 100644 src/libjasper/jpc/jpc_mqcod.h create mode 100644 src/libjasper/jpc/jpc_mqdec.c create mode 100644 src/libjasper/jpc/jpc_mqdec.h create mode 100644 src/libjasper/jpc/jpc_mqenc.c create mode 100644 src/libjasper/jpc/jpc_mqenc.h create mode 100644 src/libjasper/jpc/jpc_qmfb.c create mode 100644 src/libjasper/jpc/jpc_qmfb.h create mode 100644 src/libjasper/jpc/jpc_t1cod.c create mode 100644 src/libjasper/jpc/jpc_t1cod.h create mode 100644 src/libjasper/jpc/jpc_t1dec.c create mode 100644 src/libjasper/jpc/jpc_t1dec.h create mode 100644 src/libjasper/jpc/jpc_t1enc.c create mode 100644 src/libjasper/jpc/jpc_t1enc.h create mode 100644 src/libjasper/jpc/jpc_t2cod.c create mode 100644 src/libjasper/jpc/jpc_t2cod.h create mode 100644 src/libjasper/jpc/jpc_t2dec.c create mode 100644 src/libjasper/jpc/jpc_t2dec.h create mode 100644 src/libjasper/jpc/jpc_t2enc.c create mode 100644 src/libjasper/jpc/jpc_t2enc.h create mode 100644 src/libjasper/jpc/jpc_tagtree.c create mode 100644 src/libjasper/jpc/jpc_tagtree.h create mode 100644 src/libjasper/jpc/jpc_tsfb.c create mode 100644 src/libjasper/jpc/jpc_tsfb.h create mode 100644 src/libjasper/jpc/jpc_util.c create mode 100644 src/libjasper/jpc/jpc_util.h (limited to 'src/libjasper/jpc') diff --git a/src/libjasper/jpc/jpc_bs.c b/src/libjasper/jpc/jpc_bs.c new file mode 100644 index 0000000..0471665 --- /dev/null +++ b/src/libjasper/jpc/jpc_bs.c @@ -0,0 +1,440 @@ +/* + * Copyright (c) 1999-2000, Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Bit Stream Class + * + * $Id: jpc_bs.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jasper/jas_malloc.h" +#include "jasper/jas_math.h" +#include "jasper/jas_debug.h" + +#include "jpc_bs.h" + +/******************************************************************************\ +* Local function prototypes. +\******************************************************************************/ + +static jpc_bitstream_t *jpc_bitstream_alloc(void); + +/******************************************************************************\ +* Code for opening and closing bit streams. +\******************************************************************************/ + +/* Open a bit stream from a stream. */ +jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, char *mode) +{ + jpc_bitstream_t *bitstream; + + /* Ensure that the open mode is valid. */ +#if 1 +/* This causes a string literal too long error (with c99 pedantic mode). */ + assert(!strcmp(mode, "r") || !strcmp(mode, "w") || !strcmp(mode, "r+") + || !strcmp(mode, "w+")); +#endif + + if (!(bitstream = jpc_bitstream_alloc())) { + return 0; + } + + /* By default, do not close the underlying (character) stream, upon + the close of the bit stream. */ + bitstream->flags_ = JPC_BITSTREAM_NOCLOSE; + + bitstream->stream_ = stream; + bitstream->openmode_ = (mode[0] == 'w') ? JPC_BITSTREAM_WRITE : + JPC_BITSTREAM_READ; + + /* Mark the data buffer as empty. */ + bitstream->cnt_ = (bitstream->openmode_ == JPC_BITSTREAM_READ) ? 0 : 8; + bitstream->buf_ = 0; + + return bitstream; +} + +/* Close a bit stream. */ +int jpc_bitstream_close(jpc_bitstream_t *bitstream) +{ + int ret = 0; + + /* Align to the next byte boundary while considering the effects of + bit stuffing. */ + if (jpc_bitstream_align(bitstream)) { + ret = -1; + } + + /* If necessary, close the underlying (character) stream. */ + if (!(bitstream->flags_ & JPC_BITSTREAM_NOCLOSE) && bitstream->stream_) { + if (jas_stream_close(bitstream->stream_)) { + ret = -1; + } + bitstream->stream_ = 0; + } + + jas_free(bitstream); + return ret; +} + +/* Allocate a new bit stream. */ +static jpc_bitstream_t *jpc_bitstream_alloc() +{ + jpc_bitstream_t *bitstream; + + /* Allocate memory for the new bit stream object. */ + if (!(bitstream = jas_malloc(sizeof(jpc_bitstream_t)))) { + return 0; + } + /* Initialize all of the data members. */ + bitstream->stream_ = 0; + bitstream->cnt_ = 0; + bitstream->flags_ = 0; + bitstream->openmode_ = 0; + + return bitstream; +} + +/******************************************************************************\ +* Code for reading/writing from/to bit streams. +\******************************************************************************/ + +/* Get a bit from a bit stream. */ +int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream) +{ + int ret; + JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func(%p)\n", bitstream)); + ret = jpc_bitstream_getbit_macro(bitstream); + JAS_DBGLOG(1000, ("jpc_bitstream_getbit_func -> %d\n", ret)); + return ret; +} + +/* Put a bit to a bit stream. */ +int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int b) +{ + int ret; + JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func(%p, %d)\n", bitstream, b)); + ret = jpc_bitstream_putbit_macro(bitstream, b); + JAS_DBGLOG(1000, ("jpc_bitstream_putbit_func() -> %d\n", ret)); + return ret; +} + +/* Get one or more bits from a bit stream. */ +long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n) +{ + long v; + int u; + + /* We can reliably get at most 31 bits since ISO/IEC 9899 only + guarantees that a long can represent values up to 2^31-1. */ + assert(n >= 0 && n < 32); + + /* Get the number of bits requested from the specified bit stream. */ + v = 0; + while (--n >= 0) { + if ((u = jpc_bitstream_getbit(bitstream)) < 0) { + return -1; + } + v = (v << 1) | u; + } + return v; +} + +/* Put one or more bits to a bit stream. */ +int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v) +{ + int m; + + /* We can reliably put at most 31 bits since ISO/IEC 9899 only + guarantees that a long can represent values up to 2^31-1. */ + assert(n >= 0 && n < 32); + /* Ensure that only the bits to be output are nonzero. */ + assert(!(v & (~JAS_ONES(n)))); + + /* Put the desired number of bits to the specified bit stream. */ + m = n - 1; + while (--n >= 0) { + if (jpc_bitstream_putbit(bitstream, (v >> m) & 1) == EOF) { + return EOF; + } + v <<= 1; + } + return 0; +} + +/******************************************************************************\ +* Code for buffer filling and flushing. +\******************************************************************************/ + +/* Fill the buffer for a bit stream. */ +int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream) +{ + int c; + /* Note: The count has already been decremented by the caller. */ + assert(bitstream->openmode_ & JPC_BITSTREAM_READ); + assert(bitstream->cnt_ <= 0); + + if (bitstream->flags_ & JPC_BITSTREAM_ERR) { + bitstream->cnt_ = 0; + return -1; + } + + if (bitstream->flags_ & JPC_BITSTREAM_EOF) { + bitstream->buf_ = 0x7f; + bitstream->cnt_ = 7; + return 1; + } + + bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff; + if ((c = jas_stream_getc((bitstream)->stream_)) == EOF) { + bitstream->flags_ |= JPC_BITSTREAM_EOF; + return 1; + } + bitstream->cnt_ = (bitstream->buf_ == 0xff00) ? 6 : 7; + bitstream->buf_ |= c & ((1 << (bitstream->cnt_ + 1)) - 1); + return (bitstream->buf_ >> bitstream->cnt_) & 1; +} + + +/******************************************************************************\ +* Code related to flushing. +\******************************************************************************/ + +/* Does the bit stream need to be aligned to a byte boundary (considering + the effects of bit stuffing)? */ +int jpc_bitstream_needalign(jpc_bitstream_t *bitstream) +{ + if (bitstream->openmode_ & JPC_BITSTREAM_READ) { + /* The bit stream is open for reading. */ + /* If there are any bits buffered for reading, or the + previous byte forced a stuffed bit, alignment is + required. */ + if ((bitstream->cnt_ < 8 && bitstream->cnt_ > 0) || + ((bitstream->buf_ >> 8) & 0xff) == 0xff) { + return 1; + } + } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) { + /* The bit stream is open for writing. */ + /* If there are any bits buffered for writing, or the + previous byte forced a stuffed bit, alignment is + required. */ + if ((bitstream->cnt_ < 8 && bitstream->cnt_ >= 0) || + ((bitstream->buf_ >> 8) & 0xff) == 0xff) { + return 1; + } + } else { + /* This should not happen. Famous last words, eh? :-) */ + assert(0); + return -1; + } + return 0; +} + +/* How many additional bytes would be output if we align the bit stream? */ +int jpc_bitstream_pending(jpc_bitstream_t *bitstream) +{ + if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) { + /* The bit stream is being used for writing. */ +#if 1 + /* XXX - Is this really correct? Check someday... */ + if (bitstream->cnt_ < 8) { + return 1; + } +#else + if (bitstream->cnt_ < 8) { + if (((bitstream->buf_ >> 8) & 0xff) == 0xff) { + return 2; + } + return 1; + } +#endif + return 0; + } else { + /* This operation should not be invoked on a bit stream that + is being used for reading. */ + return -1; + } +} + +/* Align the bit stream to a byte boundary. */ +int jpc_bitstream_align(jpc_bitstream_t *bitstream) +{ + int ret; + if (bitstream->openmode_ & JPC_BITSTREAM_READ) { + ret = jpc_bitstream_inalign(bitstream, 0, 0); + } else if (bitstream->openmode_ & JPC_BITSTREAM_WRITE) { + ret = jpc_bitstream_outalign(bitstream, 0); + } else { + abort(); + } + return ret; +} + +/* Align a bit stream in the input case. */ +int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask, + int filldata) +{ + int n; + int v; + int u; + int numfill; + int m; + + numfill = 7; + m = 0; + v = 0; + if (bitstream->cnt_ > 0) { + n = bitstream->cnt_; + } else if (!bitstream->cnt_) { + n = ((bitstream->buf_ & 0xff) == 0xff) ? 7 : 0; + } else { + n = 0; + } + if (n > 0) { + if ((u = jpc_bitstream_getbits(bitstream, n)) < 0) { + return -1; + } + m += n; + v = (v << n) | u; + } + if ((bitstream->buf_ & 0xff) == 0xff) { + if ((u = jpc_bitstream_getbits(bitstream, 7)) < 0) { + return -1; + } + v = (v << 7) | u; + m += 7; + } + if (m > numfill) { + v >>= m - numfill; + } else { + filldata >>= numfill - m; + fillmask >>= numfill - m; + } + if (((~(v ^ filldata)) & fillmask) != fillmask) { + /* The actual fill pattern does not match the expected one. */ + return 1; + } + + return 0; +} + +/* Align a bit stream in the output case. */ +int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata) +{ + int n; + int v; + + /* Ensure that this bit stream is open for writing. */ + assert(bitstream->openmode_ & JPC_BITSTREAM_WRITE); + + /* Ensure that the first bit of fill data is zero. */ + /* Note: The first bit of fill data must be zero. If this were not + the case, the fill data itself could cause further bit stuffing to + be required (which would cause numerous complications). */ + assert(!(filldata & (~0x3f))); + + if (!bitstream->cnt_) { + if ((bitstream->buf_ & 0xff) == 0xff) { + n = 7; + v = filldata; + } else { + n = 0; + v = 0; + } + } else if (bitstream->cnt_ > 0 && bitstream->cnt_ < 8) { + n = bitstream->cnt_; + v = filldata >> (7 - n); + } else { + n = 0; + v = 0; + return 0; + } + + /* Write the appropriate fill data to the bit stream. */ + if (n > 0) { + if (jpc_bitstream_putbits(bitstream, n, v)) { + return -1; + } + } + if (bitstream->cnt_ < 8) { + assert(bitstream->cnt_ >= 0 && bitstream->cnt_ < 8); + assert((bitstream->buf_ & 0xff) != 0xff); + /* Force the pending byte of output to be written to the + underlying (character) stream. */ + if (jas_stream_putc(bitstream->stream_, bitstream->buf_ & 0xff) == EOF) { + return -1; + } + bitstream->cnt_ = 8; + bitstream->buf_ = (bitstream->buf_ << 8) & 0xffff; + } + + return 0; +} diff --git a/src/libjasper/jpc/jpc_bs.h b/src/libjasper/jpc/jpc_bs.h new file mode 100644 index 0000000..78f2974 --- /dev/null +++ b/src/libjasper/jpc/jpc_bs.h @@ -0,0 +1,231 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Bit Stream Class + * + * $Id: jpc_bs.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_BS_H +#define JPC_BS_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include + +#include "jasper/jas_types.h" +#include "jasper/jas_stream.h" + +/******************************************************************************\ +* Constants. +\******************************************************************************/ + +/* + * Bit stream open mode flags. + */ + +/* Bit stream open for reading. */ +#define JPC_BITSTREAM_READ 0x01 +/* Bit stream open for writing. */ +#define JPC_BITSTREAM_WRITE 0x02 + +/* + * Bit stream flags. + */ + +/* Do not close underlying character stream. */ +#define JPC_BITSTREAM_NOCLOSE 0x01 +/* End of file has been reached while reading. */ +#define JPC_BITSTREAM_EOF 0x02 +/* An I/O error has occured. */ +#define JPC_BITSTREAM_ERR 0x04 + +/******************************************************************************\ +* Types. +\******************************************************************************/ + +/* Bit stream class. */ + +typedef struct { + + /* Some miscellaneous flags. */ + int flags_; + + /* The input/output buffer. */ + uint_fast16_t buf_; + + /* The number of bits remaining in the byte being read/written. */ + int cnt_; + + /* The underlying stream associated with this bit stream. */ + jas_stream_t *stream_; + + /* The mode in which this bit stream was opened. */ + int openmode_; + +} jpc_bitstream_t; + +/******************************************************************************\ +* Functions/macros for opening and closing bit streams.. +\******************************************************************************/ + +/* Open a stream as a bit stream. */ +jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, char *mode); + +/* Close a bit stream. */ +int jpc_bitstream_close(jpc_bitstream_t *bitstream); + +/******************************************************************************\ +* Functions/macros for reading from and writing to bit streams.. +\******************************************************************************/ + +/* Read a bit from a bit stream. */ +#if defined(DEBUG) +#define jpc_bitstream_getbit(bitstream) \ + jpc_bitstream_getbit_func(bitstream) +#else +#define jpc_bitstream_getbit(bitstream) \ + jpc_bitstream_getbit_macro(bitstream) +#endif + +/* Write a bit to a bit stream. */ +#if defined(DEBUG) +#define jpc_bitstream_putbit(bitstream, v) \ + jpc_bitstream_putbit_func(bitstream, v) +#else +#define jpc_bitstream_putbit(bitstream, v) \ + jpc_bitstream_putbit_macro(bitstream, v) +#endif + +/* Read one or more bits from a bit stream. */ +long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n); + +/* Write one or more bits to a bit stream. */ +int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v); + +/******************************************************************************\ +* Functions/macros for flushing and aligning bit streams. +\******************************************************************************/ + +/* Align the current position within the bit stream to the next byte + boundary. */ +int jpc_bitstream_align(jpc_bitstream_t *bitstream); + +/* Align the current position in the bit stream with the next byte boundary, + ensuring that certain bits consumed in the process match a particular + pattern. */ +int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask, + int filldata); + +/* Align the current position in the bit stream with the next byte boundary, + writing bits from the specified pattern (if necessary) in the process. */ +int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata); + +/* Check if a bit stream needs alignment. */ +int jpc_bitstream_needalign(jpc_bitstream_t *bitstream); + +/* How many additional bytes would be output if the bit stream was aligned? */ +int jpc_bitstream_pending(jpc_bitstream_t *bitstream); + +/******************************************************************************\ +* Functions/macros for querying state information for bit streams. +\******************************************************************************/ + +/* Has EOF been encountered on a bit stream? */ +#define jpc_bitstream_eof(bitstream) \ + ((bitstream)->flags_ & JPC_BITSTREAM_EOF) + +/******************************************************************************\ +* Internals. +\******************************************************************************/ + +/* DO NOT DIRECTLY INVOKE ANY OF THE MACROS OR FUNCTIONS BELOW. THEY ARE + FOR INTERNAL USE ONLY. */ + +int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream); + +int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int v); + +int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream); + +#define jpc_bitstream_getbit_macro(bitstream) \ + (assert((bitstream)->openmode_ & JPC_BITSTREAM_READ), \ + (--(bitstream)->cnt_ >= 0) ? \ + ((int)(((bitstream)->buf_ >> (bitstream)->cnt_) & 1)) : \ + jpc_bitstream_fillbuf(bitstream)) + +#define jpc_bitstream_putbit_macro(bitstream, bit) \ + (assert((bitstream)->openmode_ & JPC_BITSTREAM_WRITE), \ + (--(bitstream)->cnt_ < 0) ? \ + ((bitstream)->buf_ = ((bitstream)->buf_ << 8) & 0xffff, \ + (bitstream)->cnt_ = ((bitstream)->buf_ == 0xff00) ? 6 : 7, \ + (bitstream)->buf_ |= ((bit) & 1) << (bitstream)->cnt_, \ + (jas_stream_putc((bitstream)->stream_, (bitstream)->buf_ >> 8) == EOF) \ + ? (EOF) : ((bit) & 1)) : \ + ((bitstream)->buf_ |= ((bit) & 1) << (bitstream)->cnt_, \ + (bit) & 1)) + +#endif diff --git a/src/libjasper/jpc/jpc_cod.h b/src/libjasper/jpc/jpc_cod.h new file mode 100644 index 0000000..31325e2 --- /dev/null +++ b/src/libjasper/jpc/jpc_cod.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * $Id: jpc_cod.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_COD_H +#define JPC_COD_H + +/******************************************************************************\ +* Constants. +\******************************************************************************/ + +/* The nominal word size used by this implementation. */ +#define JPC_PREC 32 + +#endif diff --git a/src/libjasper/jpc/jpc_cs.c b/src/libjasper/jpc/jpc_cs.c new file mode 100644 index 0000000..0bd8400 --- /dev/null +++ b/src/libjasper/jpc/jpc_cs.c @@ -0,0 +1,1644 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * JPEG-2000 Code Stream Library + * + * $Id: jpc_cs.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jasper/jas_malloc.h" +#include "jasper/jas_debug.h" + +#include "jpc_cs.h" + +/******************************************************************************\ +* Types. +\******************************************************************************/ + +/* Marker segment table entry. */ +typedef struct { + int id; + char *name; + jpc_msops_t ops; +} jpc_mstabent_t; + +/******************************************************************************\ +* Local prototypes. +\******************************************************************************/ + +static jpc_mstabent_t *jpc_mstab_lookup(int id); + +static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static void jpc_poc_destroyparms(jpc_ms_t *ms); + +static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); +static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); + +static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); +static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); + +static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out); +static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out); + +static void jpc_siz_destroyparms(jpc_ms_t *ms); +static void jpc_qcd_destroyparms(jpc_ms_t *ms); +static void jpc_qcc_destroyparms(jpc_ms_t *ms); +static void jpc_cod_destroyparms(jpc_ms_t *ms); +static void jpc_coc_destroyparms(jpc_ms_t *ms); +static void jpc_unk_destroyparms(jpc_ms_t *ms); +static void jpc_ppm_destroyparms(jpc_ms_t *ms); +static void jpc_ppt_destroyparms(jpc_ms_t *ms); +static void jpc_crg_destroyparms(jpc_ms_t *ms); +static void jpc_com_destroyparms(jpc_ms_t *ms); + +static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms); +static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, + jas_stream_t *in, uint_fast16_t len); +static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, + jas_stream_t *out); +static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms); +static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, + jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms); +static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, + jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms); + +/******************************************************************************\ +* Global data. +\******************************************************************************/ + +static jpc_mstabent_t jpc_mstab[] = { + {JPC_MS_SOC, "SOC", {0, 0, 0, 0}}, + {JPC_MS_SOT, "SOT", {0, jpc_sot_getparms, jpc_sot_putparms, + jpc_sot_dumpparms}}, + {JPC_MS_SOD, "SOD", {0, 0, 0, 0}}, + {JPC_MS_EOC, "EOC", {0, 0, 0, 0}}, + {JPC_MS_SIZ, "SIZ", {jpc_siz_destroyparms, jpc_siz_getparms, + jpc_siz_putparms, jpc_siz_dumpparms}}, + {JPC_MS_COD, "COD", {jpc_cod_destroyparms, jpc_cod_getparms, + jpc_cod_putparms, jpc_cod_dumpparms}}, + {JPC_MS_COC, "COC", {jpc_coc_destroyparms, jpc_coc_getparms, + jpc_coc_putparms, jpc_coc_dumpparms}}, + {JPC_MS_RGN, "RGN", {0, jpc_rgn_getparms, jpc_rgn_putparms, + jpc_rgn_dumpparms}}, + {JPC_MS_QCD, "QCD", {jpc_qcd_destroyparms, jpc_qcd_getparms, + jpc_qcd_putparms, jpc_qcd_dumpparms}}, + {JPC_MS_QCC, "QCC", {jpc_qcc_destroyparms, jpc_qcc_getparms, + jpc_qcc_putparms, jpc_qcc_dumpparms}}, + {JPC_MS_POC, "POC", {jpc_poc_destroyparms, jpc_poc_getparms, + jpc_poc_putparms, jpc_poc_dumpparms}}, + {JPC_MS_TLM, "TLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}}, + {JPC_MS_PLM, "PLM", {0, jpc_unk_getparms, jpc_unk_putparms, 0}}, + {JPC_MS_PPM, "PPM", {jpc_ppm_destroyparms, jpc_ppm_getparms, + jpc_ppm_putparms, jpc_ppm_dumpparms}}, + {JPC_MS_PPT, "PPT", {jpc_ppt_destroyparms, jpc_ppt_getparms, + jpc_ppt_putparms, jpc_ppt_dumpparms}}, + {JPC_MS_SOP, "SOP", {0, jpc_sop_getparms, jpc_sop_putparms, + jpc_sop_dumpparms}}, + {JPC_MS_EPH, "EPH", {0, 0, 0, 0}}, + {JPC_MS_CRG, "CRG", {0, jpc_crg_getparms, jpc_crg_putparms, + jpc_crg_dumpparms}}, + {JPC_MS_COM, "COM", {jpc_com_destroyparms, jpc_com_getparms, + jpc_com_putparms, jpc_com_dumpparms}}, + {-1, "UNKNOWN", {jpc_unk_destroyparms, jpc_unk_getparms, + jpc_unk_putparms, jpc_unk_dumpparms}} +}; + +/******************************************************************************\ +* Code stream manipulation functions. +\******************************************************************************/ + +/* Create a code stream state object. */ +jpc_cstate_t *jpc_cstate_create() +{ + jpc_cstate_t *cstate; + if (!(cstate = jas_malloc(sizeof(jpc_cstate_t)))) { + return 0; + } + cstate->numcomps = 0; + return cstate; +} + +/* Destroy a code stream state object. */ +void jpc_cstate_destroy(jpc_cstate_t *cstate) +{ + jas_free(cstate); +} + +/* Read a marker segment from a stream. */ +jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate) +{ + jpc_ms_t *ms; + jpc_mstabent_t *mstabent; + jas_stream_t *tmpstream; + + if (!(ms = jpc_ms_create(0))) { + return 0; + } + + /* Get the marker type. */ + if (jpc_getuint16(in, &ms->id) || ms->id < JPC_MS_MIN || + ms->id > JPC_MS_MAX) { + jpc_ms_destroy(ms); + return 0; + } + + mstabent = jpc_mstab_lookup(ms->id); + ms->ops = &mstabent->ops; + + /* Get the marker segment length and parameters if present. */ + /* Note: It is tacitly assumed that a marker segment cannot have + parameters unless it has a length field. That is, there cannot + be a parameters field without a length field and vice versa. */ + if (JPC_MS_HASPARMS(ms->id)) { + /* Get the length of the marker segment. */ + if (jpc_getuint16(in, &ms->len) || ms->len < 3) { + jpc_ms_destroy(ms); + return 0; + } + /* Calculate the length of the marker segment parameters. */ + ms->len -= 2; + /* Create and prepare a temporary memory stream from which to + read the marker segment parameters. */ + /* Note: This approach provides a simple way of ensuring that + we never read beyond the end of the marker segment (even if + the marker segment length is errantly set too small). */ + if (!(tmpstream = jas_stream_memopen(0, 0))) { + jpc_ms_destroy(ms); + return 0; + } + if (jas_stream_copy(tmpstream, in, ms->len) || + jas_stream_seek(tmpstream, 0, SEEK_SET) < 0) { + jas_stream_close(tmpstream); + jpc_ms_destroy(ms); + return 0; + } + /* Get the marker segment parameters. */ + if ((*ms->ops->getparms)(ms, cstate, tmpstream)) { + ms->ops = 0; + jpc_ms_destroy(ms); + jas_stream_close(tmpstream); + return 0; + } + + if (jas_getdbglevel() > 0) { + jpc_ms_dump(ms, stderr); + } + + if (JAS_CAST(unsigned long, jas_stream_tell(tmpstream)) != ms->len) { /* IMLIB - changed ulong to unsigned long */ + jas_eprintf("warning: trailing garbage in marker segment (%ld bytes)\n", + ms->len - jas_stream_tell(tmpstream)); + } + + /* Close the temporary stream. */ + jas_stream_close(tmpstream); + + } else { + /* There are no marker segment parameters. */ + ms->len = 0; + + if (jas_getdbglevel() > 0) { + jpc_ms_dump(ms, stderr); + } + } + + /* Update the code stream state information based on the type of + marker segment read. */ + /* Note: This is a bit of a hack, but I'm not going to define another + type of virtual function for this one special case. */ + if (ms->id == JPC_MS_SIZ) { + cstate->numcomps = ms->parms.siz.numcomps; + } + + return ms; +} + +/* Write a marker segment to a stream. */ +int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms) +{ + jas_stream_t *tmpstream; + int len; + + /* Output the marker segment type. */ + if (jpc_putuint16(out, ms->id)) { + return -1; + } + + /* Output the marker segment length and parameters if necessary. */ + if (ms->ops->putparms) { + /* Create a temporary stream in which to buffer the + parameter data. */ + if (!(tmpstream = jas_stream_memopen(0, 0))) { + return -1; + } + if ((*ms->ops->putparms)(ms, cstate, tmpstream)) { + jas_stream_close(tmpstream); + return -1; + } + /* Get the number of bytes of parameter data written. */ + if ((len = jas_stream_tell(tmpstream)) < 0) { + jas_stream_close(tmpstream); + return -1; + } + ms->len = len; + /* Write the marker segment length and parameter data to + the output stream. */ + if (jas_stream_seek(tmpstream, 0, SEEK_SET) < 0 || + jpc_putuint16(out, ms->len + 2) || + jas_stream_copy(out, tmpstream, ms->len) < 0) { + jas_stream_close(tmpstream); + return -1; + } + /* Close the temporary stream. */ + jas_stream_close(tmpstream); + } + + /* This is a bit of a hack, but I'm not going to define another + type of virtual function for this one special case. */ + if (ms->id == JPC_MS_SIZ) { + cstate->numcomps = ms->parms.siz.numcomps; + } + + if (jas_getdbglevel() > 0) { + jpc_ms_dump(ms, stderr); + } + + return 0; +} + +/******************************************************************************\ +* Marker segment operations. +\******************************************************************************/ + +/* Create a marker segment of the specified type. */ +jpc_ms_t *jpc_ms_create(int type) +{ + jpc_ms_t *ms; + jpc_mstabent_t *mstabent; + + if (!(ms = jas_malloc(sizeof(jpc_ms_t)))) { + return 0; + } + ms->id = type; + ms->len = 0; + mstabent = jpc_mstab_lookup(ms->id); + ms->ops = &mstabent->ops; + memset(&ms->parms, 0, sizeof(jpc_msparms_t)); + return ms; +} + +/* Destroy a marker segment. */ +void jpc_ms_destroy(jpc_ms_t *ms) +{ + if (ms->ops && ms->ops->destroyparms) { + (*ms->ops->destroyparms)(ms); + } + jas_free(ms); +} + +/* Dump a marker segment to a stream for debugging. */ +void jpc_ms_dump(jpc_ms_t *ms, FILE *out) +{ + jpc_mstabent_t *mstabent; + mstabent = jpc_mstab_lookup(ms->id); + fprintf(out, "type = 0x%04x (%s);", ms->id, mstabent->name); + if (JPC_MS_HASPARMS(ms->id)) { + fprintf(out, " len = %d;", ms->len + 2); + if (ms->ops->dumpparms) { + (*ms->ops->dumpparms)(ms, out); + } else { + fprintf(out, "\n"); + } + } else { + fprintf(out, "\n"); + } +} + +/******************************************************************************\ +* SOT marker segment operations. +\******************************************************************************/ + +static int jpc_sot_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_sot_t *sot = &ms->parms.sot; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + if (jpc_getuint16(in, &sot->tileno) || + jpc_getuint32(in, &sot->len) || + jpc_getuint8(in, &sot->partno) || + jpc_getuint8(in, &sot->numparts)) { + return -1; + } + if (jas_stream_eof(in)) { + return -1; + } + return 0; +} + +static int jpc_sot_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_sot_t *sot = &ms->parms.sot; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + if (jpc_putuint16(out, sot->tileno) || + jpc_putuint32(out, sot->len) || + jpc_putuint8(out, sot->partno) || + jpc_putuint8(out, sot->numparts)) { + return -1; + } + return 0; +} + +static int jpc_sot_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_sot_t *sot = &ms->parms.sot; + fprintf(out, "tileno = %d; len = %d; partno = %d; numparts = %d\n", + sot->tileno, sot->len, sot->partno, sot->numparts); + return 0; +} + +/******************************************************************************\ +* SIZ marker segment operations. +\******************************************************************************/ + +static void jpc_siz_destroyparms(jpc_ms_t *ms) +{ + jpc_siz_t *siz = &ms->parms.siz; + if (siz->comps) { + jas_free(siz->comps); + } +} + +static int jpc_siz_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, + jas_stream_t *in) +{ + jpc_siz_t *siz = &ms->parms.siz; + unsigned int i; + uint_fast8_t tmp; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + if (jpc_getuint16(in, &siz->caps) || + jpc_getuint32(in, &siz->width) || + jpc_getuint32(in, &siz->height) || + jpc_getuint32(in, &siz->xoff) || + jpc_getuint32(in, &siz->yoff) || + jpc_getuint32(in, &siz->tilewidth) || + jpc_getuint32(in, &siz->tileheight) || + jpc_getuint32(in, &siz->tilexoff) || + jpc_getuint32(in, &siz->tileyoff) || + jpc_getuint16(in, &siz->numcomps)) { + return -1; + } + if (!siz->width || !siz->height || !siz->tilewidth || + !siz->tileheight || !siz->numcomps) { + return -1; + } + if (!(siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)))) { + return -1; + } + for (i = 0; i < siz->numcomps; ++i) { + if (jpc_getuint8(in, &tmp) || + jpc_getuint8(in, &siz->comps[i].hsamp) || + jpc_getuint8(in, &siz->comps[i].vsamp)) { + jas_free(siz->comps); + return -1; + } + siz->comps[i].sgnd = (tmp >> 7) & 1; + siz->comps[i].prec = (tmp & 0x7f) + 1; + } + if (jas_stream_eof(in)) { + jas_free(siz->comps); + return -1; + } + return 0; +} + +static int jpc_siz_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_siz_t *siz = &ms->parms.siz; + unsigned int i; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + assert(siz->width && siz->height && siz->tilewidth && + siz->tileheight && siz->numcomps); + if (jpc_putuint16(out, siz->caps) || + jpc_putuint32(out, siz->width) || + jpc_putuint32(out, siz->height) || + jpc_putuint32(out, siz->xoff) || + jpc_putuint32(out, siz->yoff) || + jpc_putuint32(out, siz->tilewidth) || + jpc_putuint32(out, siz->tileheight) || + jpc_putuint32(out, siz->tilexoff) || + jpc_putuint32(out, siz->tileyoff) || + jpc_putuint16(out, siz->numcomps)) { + return -1; + } + for (i = 0; i < siz->numcomps; ++i) { + if (jpc_putuint8(out, ((siz->comps[i].sgnd & 1) << 7) | + ((siz->comps[i].prec - 1) & 0x7f)) || + jpc_putuint8(out, siz->comps[i].hsamp) || + jpc_putuint8(out, siz->comps[i].vsamp)) { + return -1; + } + } + return 0; +} + +static int jpc_siz_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_siz_t *siz = &ms->parms.siz; + unsigned int i; + fprintf(out, "caps = 0x%02x;\n", siz->caps); + fprintf(out, "width = %d; height = %d; xoff = %d; yoff = %d;\n", + siz->width, siz->height, siz->xoff, siz->yoff); + fprintf(out, "tilewidth = %d; tileheight = %d; tilexoff = %d; " + "tileyoff = %d;\n", siz->tilewidth, siz->tileheight, siz->tilexoff, + siz->tileyoff); + for (i = 0; i < siz->numcomps; ++i) { + fprintf(out, "prec[%d] = %d; sgnd[%d] = %d; hsamp[%d] = %d; " + "vsamp[%d] = %d\n", i, siz->comps[i].prec, i, + siz->comps[i].sgnd, i, siz->comps[i].hsamp, i, + siz->comps[i].vsamp); + } + return 0; +} + +/******************************************************************************\ +* COD marker segment operations. +\******************************************************************************/ + +static void jpc_cod_destroyparms(jpc_ms_t *ms) +{ + jpc_cod_t *cod = &ms->parms.cod; + jpc_cox_destroycompparms(&cod->compparms); +} + +static int jpc_cod_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_cod_t *cod = &ms->parms.cod; + if (jpc_getuint8(in, &cod->csty)) { + return -1; + } + if (jpc_getuint8(in, &cod->prg) || + jpc_getuint16(in, &cod->numlyrs) || + jpc_getuint8(in, &cod->mctrans)) { + return -1; + } + if (jpc_cox_getcompparms(ms, cstate, in, + (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) { + return -1; + } + if (jas_stream_eof(in)) { + jpc_cod_destroyparms(ms); + return -1; + } + return 0; +} + +static int jpc_cod_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_cod_t *cod = &ms->parms.cod; + assert(cod->numlyrs > 0 && cod->compparms.numdlvls <= 32); + assert(cod->compparms.numdlvls == cod->compparms.numrlvls - 1); + if (jpc_putuint8(out, cod->compparms.csty) || + jpc_putuint8(out, cod->prg) || + jpc_putuint16(out, cod->numlyrs) || + jpc_putuint8(out, cod->mctrans)) { + return -1; + } + if (jpc_cox_putcompparms(ms, cstate, out, + (cod->csty & JPC_COX_PRT) != 0, &cod->compparms)) { + return -1; + } + return 0; +} + +static int jpc_cod_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_cod_t *cod = &ms->parms.cod; + int i; + fprintf(out, "csty = 0x%02x;\n", cod->compparms.csty); + fprintf(out, "numdlvls = %d; qmfbid = %d; mctrans = %d\n", + cod->compparms.numdlvls, cod->compparms.qmfbid, cod->mctrans); + fprintf(out, "prg = %d; numlyrs = %d;\n", + cod->prg, cod->numlyrs); + fprintf(out, "cblkwidthval = %d; cblkheightval = %d; " + "cblksty = 0x%02x;\n", cod->compparms.cblkwidthval, cod->compparms.cblkheightval, + cod->compparms.cblksty); + if (cod->csty & JPC_COX_PRT) { + for (i = 0; i < cod->compparms.numrlvls; ++i) { + jas_eprintf("prcwidth[%d] = %d, prcheight[%d] = %d\n", + i, cod->compparms.rlvls[i].parwidthval, + i, cod->compparms.rlvls[i].parheightval); + } + } + return 0; +} + +/******************************************************************************\ +* COC marker segment operations. +\******************************************************************************/ + +static void jpc_coc_destroyparms(jpc_ms_t *ms) +{ + jpc_coc_t *coc = &ms->parms.coc; + jpc_cox_destroycompparms(&coc->compparms); +} + +static int jpc_coc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_coc_t *coc = &ms->parms.coc; + uint_fast8_t tmp; + if (cstate->numcomps <= 256) { + if (jpc_getuint8(in, &tmp)) { + return -1; + } + coc->compno = tmp; + } else { + if (jpc_getuint16(in, &coc->compno)) { + return -1; + } + } + if (jpc_getuint8(in, &coc->compparms.csty)) { + return -1; + } + if (jpc_cox_getcompparms(ms, cstate, in, + (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) { + return -1; + } + if (jas_stream_eof(in)) { + return -1; + } + return 0; +} + +static int jpc_coc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_coc_t *coc = &ms->parms.coc; + assert(coc->compparms.numdlvls <= 32); + if (cstate->numcomps <= 256) { + if (jpc_putuint8(out, coc->compno)) { + return -1; + } + } else { + if (jpc_putuint16(out, coc->compno)) { + return -1; + } + } + if (jpc_putuint8(out, coc->compparms.csty)) { + return -1; + } + if (jpc_cox_putcompparms(ms, cstate, out, + (coc->compparms.csty & JPC_COX_PRT) != 0, &coc->compparms)) { + return -1; + } + return 0; +} + +static int jpc_coc_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_coc_t *coc = &ms->parms.coc; + fprintf(out, "compno = %d; csty = 0x%02x; numdlvls = %d;\n", + coc->compno, coc->compparms.csty, coc->compparms.numdlvls); + fprintf(out, "cblkwidthval = %d; cblkheightval = %d; " + "cblksty = 0x%02x; qmfbid = %d;\n", coc->compparms.cblkwidthval, + coc->compparms.cblkheightval, coc->compparms.cblksty, coc->compparms.qmfbid); + return 0; +} +/******************************************************************************\ +* COD/COC marker segment operation helper functions. +\******************************************************************************/ + +static void jpc_cox_destroycompparms(jpc_coxcp_t *compparms) +{ + /* Eliminate compiler warning about unused variables. */ + compparms = 0; +} + +static int jpc_cox_getcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, + jas_stream_t *in, int prtflag, jpc_coxcp_t *compparms) +{ + uint_fast8_t tmp; + int i; + + /* Eliminate compiler warning about unused variables. */ + ms = 0; + cstate = 0; + + if (jpc_getuint8(in, &compparms->numdlvls) || + jpc_getuint8(in, &compparms->cblkwidthval) || + jpc_getuint8(in, &compparms->cblkheightval) || + jpc_getuint8(in, &compparms->cblksty) || + jpc_getuint8(in, &compparms->qmfbid)) { + return -1; + } + compparms->numrlvls = compparms->numdlvls + 1; + if (prtflag) { + for (i = 0; i < compparms->numrlvls; ++i) { + if (jpc_getuint8(in, &tmp)) { + jpc_cox_destroycompparms(compparms); + return -1; + } + compparms->rlvls[i].parwidthval = tmp & 0xf; + compparms->rlvls[i].parheightval = (tmp >> 4) & 0xf; + } +/* Sigh. This bit should be in the same field in both COC and COD mrk segs. */ +compparms->csty |= JPC_COX_PRT; + } else { + } + if (jas_stream_eof(in)) { + jpc_cox_destroycompparms(compparms); + return -1; + } + return 0; +} + +static int jpc_cox_putcompparms(jpc_ms_t *ms, jpc_cstate_t *cstate, + jas_stream_t *out, int prtflag, jpc_coxcp_t *compparms) +{ + int i; + assert(compparms->numdlvls <= 32); + + /* Eliminate compiler warning about unused variables. */ + ms = 0; + cstate = 0; + + if (jpc_putuint8(out, compparms->numdlvls) || + jpc_putuint8(out, compparms->cblkwidthval) || + jpc_putuint8(out, compparms->cblkheightval) || + jpc_putuint8(out, compparms->cblksty) || + jpc_putuint8(out, compparms->qmfbid)) { + return -1; + } + if (prtflag) { + for (i = 0; i < compparms->numrlvls; ++i) { + if (jpc_putuint8(out, + ((compparms->rlvls[i].parheightval & 0xf) << 4) | + (compparms->rlvls[i].parwidthval & 0xf))) { + return -1; + } + } + } + return 0; +} + +/******************************************************************************\ +* RGN marker segment operations. +\******************************************************************************/ + +static int jpc_rgn_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_rgn_t *rgn = &ms->parms.rgn; + uint_fast8_t tmp; + if (cstate->numcomps <= 256) { + if (jpc_getuint8(in, &tmp)) { + return -1; + } + rgn->compno = tmp; + } else { + if (jpc_getuint16(in, &rgn->compno)) { + return -1; + } + } + if (jpc_getuint8(in, &rgn->roisty) || + jpc_getuint8(in, &rgn->roishift)) { + return -1; + } + return 0; +} + +static int jpc_rgn_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_rgn_t *rgn = &ms->parms.rgn; + if (cstate->numcomps <= 256) { + if (jpc_putuint8(out, rgn->compno)) { + return -1; + } + } else { + if (jpc_putuint16(out, rgn->compno)) { + return -1; + } + } + if (jpc_putuint8(out, rgn->roisty) || + jpc_putuint8(out, rgn->roishift)) { + return -1; + } + return 0; +} + +static int jpc_rgn_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_rgn_t *rgn = &ms->parms.rgn; + fprintf(out, "compno = %d; roisty = %d; roishift = %d\n", + rgn->compno, rgn->roisty, rgn->roishift); + return 0; +} + +/******************************************************************************\ +* QCD marker segment operations. +\******************************************************************************/ + +static void jpc_qcd_destroyparms(jpc_ms_t *ms) +{ + jpc_qcd_t *qcd = &ms->parms.qcd; + jpc_qcx_destroycompparms(&qcd->compparms); +} + +static int jpc_qcd_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms; + return jpc_qcx_getcompparms(compparms, cstate, in, ms->len); +} + +static int jpc_qcd_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_qcxcp_t *compparms = &ms->parms.qcd.compparms; + return jpc_qcx_putcompparms(compparms, cstate, out); +} + +static int jpc_qcd_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_qcd_t *qcd = &ms->parms.qcd; + int i; + fprintf(out, "qntsty = %d; numguard = %d; numstepsizes = %d\n", + (int) qcd->compparms.qntsty, qcd->compparms.numguard, qcd->compparms.numstepsizes); + for (i = 0; i < qcd->compparms.numstepsizes; ++i) { + fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n", + i, (unsigned) JPC_QCX_GETEXPN(qcd->compparms.stepsizes[i]), + i, (unsigned) JPC_QCX_GETMANT(qcd->compparms.stepsizes[i])); + } + return 0; +} + +/******************************************************************************\ +* QCC marker segment operations. +\******************************************************************************/ + +static void jpc_qcc_destroyparms(jpc_ms_t *ms) +{ + jpc_qcc_t *qcc = &ms->parms.qcc; + jpc_qcx_destroycompparms(&qcc->compparms); +} + +static int jpc_qcc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_qcc_t *qcc = &ms->parms.qcc; + uint_fast8_t tmp; + int len; + len = ms->len; + if (cstate->numcomps <= 256) { + jpc_getuint8(in, &tmp); + qcc->compno = tmp; + --len; + } else { + jpc_getuint16(in, &qcc->compno); + len -= 2; + } + if (jpc_qcx_getcompparms(&qcc->compparms, cstate, in, len)) { + return -1; + } + if (jas_stream_eof(in)) { + jpc_qcc_destroyparms(ms); + return -1; + } + return 0; +} + +static int jpc_qcc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_qcc_t *qcc = &ms->parms.qcc; + if (cstate->numcomps <= 256) { + jpc_putuint8(out, qcc->compno); + } else { + jpc_putuint16(out, qcc->compno); + } + if (jpc_qcx_putcompparms(&qcc->compparms, cstate, out)) { + return -1; + } + return 0; +} + +static int jpc_qcc_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_qcc_t *qcc = &ms->parms.qcc; + int i; + fprintf(out, "compno = %d; qntsty = %d; numguard = %d; " + "numstepsizes = %d\n", qcc->compno, qcc->compparms.qntsty, qcc->compparms.numguard, + qcc->compparms.numstepsizes); + for (i = 0; i < qcc->compparms.numstepsizes; ++i) { + fprintf(out, "expn[%d] = 0x%04x; mant[%d] = 0x%04x;\n", + i, (unsigned) JPC_QCX_GETEXPN(qcc->compparms.stepsizes[i]), + i, (unsigned) JPC_QCX_GETMANT(qcc->compparms.stepsizes[i])); + } + return 0; +} + +/******************************************************************************\ +* QCD/QCC marker segment helper functions. +\******************************************************************************/ + +static void jpc_qcx_destroycompparms(jpc_qcxcp_t *compparms) +{ + if (compparms->stepsizes) { + jas_free(compparms->stepsizes); + } +} + +static int jpc_qcx_getcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, + jas_stream_t *in, uint_fast16_t len) +{ + uint_fast8_t tmp; + int n; + int i; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + n = 0; + jpc_getuint8(in, &tmp); + ++n; + compparms->qntsty = tmp & 0x1f; + compparms->numguard = (tmp >> 5) & 7; + switch (compparms->qntsty) { + case JPC_QCX_SIQNT: + compparms->numstepsizes = 1; + break; + case JPC_QCX_NOQNT: + compparms->numstepsizes = (len - n); + break; + case JPC_QCX_SEQNT: + /* XXX - this is a hack */ + compparms->numstepsizes = (len - n) / 2; + break; + } + if (compparms->numstepsizes > 0) { + compparms->stepsizes = jas_malloc(compparms->numstepsizes * + sizeof(uint_fast16_t)); + assert(compparms->stepsizes); + for (i = 0; i < compparms->numstepsizes; ++i) { + if (compparms->qntsty == JPC_QCX_NOQNT) { + jpc_getuint8(in, &tmp); + compparms->stepsizes[i] = JPC_QCX_EXPN(tmp >> 3); + } else { + jpc_getuint16(in, &compparms->stepsizes[i]); + } + } + } else { + compparms->stepsizes = 0; + } + if (jas_stream_error(in) || jas_stream_eof(in)) { + jpc_qcx_destroycompparms(compparms); + return -1; + } + return 0; +} + +static int jpc_qcx_putcompparms(jpc_qcxcp_t *compparms, jpc_cstate_t *cstate, + jas_stream_t *out) +{ + int i; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + jpc_putuint8(out, ((compparms->numguard & 7) << 5) | compparms->qntsty); + for (i = 0; i < compparms->numstepsizes; ++i) { + if (compparms->qntsty == JPC_QCX_NOQNT) { + jpc_putuint8(out, JPC_QCX_GETEXPN( + compparms->stepsizes[i]) << 3); + } else { + jpc_putuint16(out, compparms->stepsizes[i]); + } + } + return 0; +} + +/******************************************************************************\ +* SOP marker segment operations. +\******************************************************************************/ + +static int jpc_sop_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_sop_t *sop = &ms->parms.sop; + + /* Eliminate compiler warning about unused variable. */ + cstate = 0; + + if (jpc_getuint16(in, &sop->seqno)) { + return -1; + } + return 0; +} + +static int jpc_sop_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_sop_t *sop = &ms->parms.sop; + + /* Eliminate compiler warning about unused variable. */ + cstate = 0; + + if (jpc_putuint16(out, sop->seqno)) { + return -1; + } + return 0; +} + +static int jpc_sop_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_sop_t *sop = &ms->parms.sop; + fprintf(out, "seqno = %d;\n", sop->seqno); + return 0; +} + +/******************************************************************************\ +* PPM marker segment operations. +\******************************************************************************/ + +static void jpc_ppm_destroyparms(jpc_ms_t *ms) +{ + jpc_ppm_t *ppm = &ms->parms.ppm; + if (ppm->data) { + jas_free(ppm->data); + } +} + +static int jpc_ppm_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_ppm_t *ppm = &ms->parms.ppm; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + ppm->data = 0; + + if (ms->len < 1) { + goto error; + } + if (jpc_getuint8(in, &ppm->ind)) { + goto error; + } + + ppm->len = ms->len - 1; + if (ppm->len > 0) { + if (!(ppm->data = jas_malloc(ppm->len * sizeof(unsigned char)))) { + goto error; + } + if (JAS_CAST(unsigned int, jas_stream_read(in, ppm->data, ppm->len)) != ppm->len) { /* IMLIB - changed uint to unsigned int */ + goto error; + } + } else { + ppm->data = 0; + } + return 0; + +error: + jpc_ppm_destroyparms(ms); + return -1; +} + +static int jpc_ppm_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_ppm_t *ppm = &ms->parms.ppm; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + if (JAS_CAST(unsigned int, jas_stream_write(out, (char *) ppm->data, ppm->len)) != ppm->len) { /* IMLIB - changed uint to unsigned int */ + return -1; + } + return 0; +} + +static int jpc_ppm_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_ppm_t *ppm = &ms->parms.ppm; + fprintf(out, "ind=%d; len = %d;\n", ppm->ind, ppm->len); + if (ppm->len > 0) { + fprintf(out, "data =\n"); + jas_memdump(out, ppm->data, ppm->len); + } + return 0; +} + +/******************************************************************************\ +* PPT marker segment operations. +\******************************************************************************/ + +static void jpc_ppt_destroyparms(jpc_ms_t *ms) +{ + jpc_ppt_t *ppt = &ms->parms.ppt; + if (ppt->data) { + jas_free(ppt->data); + } +} + +static int jpc_ppt_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_ppt_t *ppt = &ms->parms.ppt; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + ppt->data = 0; + + if (ms->len < 1) { + goto error; + } + if (jpc_getuint8(in, &ppt->ind)) { + goto error; + } + ppt->len = ms->len - 1; + if (ppt->len > 0) { + if (!(ppt->data = jas_malloc(ppt->len * sizeof(unsigned char)))) { + goto error; + } + if (jas_stream_read(in, (char *) ppt->data, ppt->len) != JAS_CAST(int, ppt->len)) { + goto error; + } + } else { + ppt->data = 0; + } + return 0; + +error: + jpc_ppt_destroyparms(ms); + return -1; +} + +static int jpc_ppt_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_ppt_t *ppt = &ms->parms.ppt; + + /* Eliminate compiler warning about unused variable. */ + cstate = 0; + + if (jpc_putuint8(out, ppt->ind)) { + return -1; + } + if (jas_stream_write(out, (char *) ppt->data, ppt->len) != JAS_CAST(int, ppt->len)) { + return -1; + } + return 0; +} + +static int jpc_ppt_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_ppt_t *ppt = &ms->parms.ppt; + fprintf(out, "ind=%d; len = %d;\n", ppt->ind, ppt->len); + if (ppt->len > 0) { + fprintf(out, "data =\n"); + jas_memdump(out, ppt->data, ppt->len); + } + return 0; +} + +/******************************************************************************\ +* POC marker segment operations. +\******************************************************************************/ + +static void jpc_poc_destroyparms(jpc_ms_t *ms) +{ + jpc_poc_t *poc = &ms->parms.poc; + if (poc->pchgs) { + jas_free(poc->pchgs); + } +} + +static int jpc_poc_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_poc_t *poc = &ms->parms.poc; + jpc_pocpchg_t *pchg; + int pchgno; + uint_fast8_t tmp; + poc->numpchgs = (cstate->numcomps > 256) ? (ms->len / 9) : + (ms->len / 7); + if (!(poc->pchgs = jas_malloc(poc->numpchgs * sizeof(jpc_pocpchg_t)))) { + goto error; + } + for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno, + ++pchg) { + if (jpc_getuint8(in, &pchg->rlvlnostart)) { + goto error; + } + if (cstate->numcomps > 256) { + if (jpc_getuint16(in, &pchg->compnostart)) { + goto error; + } + } else { + if (jpc_getuint8(in, &tmp)) { + goto error; + }; + pchg->compnostart = tmp; + } + if (jpc_getuint16(in, &pchg->lyrnoend) || + jpc_getuint8(in, &pchg->rlvlnoend)) { + goto error; + } + if (cstate->numcomps > 256) { + if (jpc_getuint16(in, &pchg->compnoend)) { + goto error; + } + } else { + if (jpc_getuint8(in, &tmp)) { + goto error; + } + pchg->compnoend = tmp; + } + if (jpc_getuint8(in, &pchg->prgord)) { + goto error; + } + if (pchg->rlvlnostart > pchg->rlvlnoend || + pchg->compnostart > pchg->compnoend) { + goto error; + } + } + return 0; + +error: + jpc_poc_destroyparms(ms); + return -1; +} + +static int jpc_poc_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_poc_t *poc = &ms->parms.poc; + jpc_pocpchg_t *pchg; + int pchgno; + for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; ++pchgno, + ++pchg) { + if (jpc_putuint8(out, pchg->rlvlnostart) || + ((cstate->numcomps > 256) ? + jpc_putuint16(out, pchg->compnostart) : + jpc_putuint8(out, pchg->compnostart)) || + jpc_putuint16(out, pchg->lyrnoend) || + jpc_putuint8(out, pchg->rlvlnoend) || + ((cstate->numcomps > 256) ? + jpc_putuint16(out, pchg->compnoend) : + jpc_putuint8(out, pchg->compnoend)) || + jpc_putuint8(out, pchg->prgord)) { + return -1; + } + } + return 0; +} + +static int jpc_poc_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_poc_t *poc = &ms->parms.poc; + jpc_pocpchg_t *pchg; + int pchgno; + for (pchgno = 0, pchg = poc->pchgs; pchgno < poc->numpchgs; + ++pchgno, ++pchg) { + fprintf(out, "po[%d] = %d; ", pchgno, pchg->prgord); + fprintf(out, "cs[%d] = %d; ce[%d] = %d; ", + pchgno, pchg->compnostart, pchgno, pchg->compnoend); + fprintf(out, "rs[%d] = %d; re[%d] = %d; ", + pchgno, pchg->rlvlnostart, pchgno, pchg->rlvlnoend); + fprintf(out, "le[%d] = %d\n", pchgno, pchg->lyrnoend); + } + return 0; +} + +/******************************************************************************\ +* CRG marker segment operations. +\******************************************************************************/ + +static void jpc_crg_destroyparms(jpc_ms_t *ms) +{ + jpc_crg_t *crg = &ms->parms.crg; + if (crg->comps) { + jas_free(crg->comps); + } +} + +static int jpc_crg_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_crg_t *crg = &ms->parms.crg; + jpc_crgcomp_t *comp; + uint_fast16_t compno; + crg->numcomps = cstate->numcomps; + if (!(crg->comps = jas_malloc(cstate->numcomps * sizeof(uint_fast16_t)))) { + return -1; + } + for (compno = 0, comp = crg->comps; compno < cstate->numcomps; + ++compno, ++comp) { + if (jpc_getuint16(in, &comp->hoff) || + jpc_getuint16(in, &comp->voff)) { + jpc_crg_destroyparms(ms); + return -1; + } + } + return 0; +} + +static int jpc_crg_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_crg_t *crg = &ms->parms.crg; + int compno; + jpc_crgcomp_t *comp; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno, + ++comp) { + if (jpc_putuint16(out, comp->hoff) || + jpc_putuint16(out, comp->voff)) { + return -1; + } + } + return 0; +} + +static int jpc_crg_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_crg_t *crg = &ms->parms.crg; + int compno; + jpc_crgcomp_t *comp; + for (compno = 0, comp = crg->comps; compno < crg->numcomps; ++compno, + ++comp) { + fprintf(out, "hoff[%d] = %d; voff[%d] = %d\n", compno, + comp->hoff, compno, comp->voff); + } + return 0; +} + +/******************************************************************************\ +* Operations for COM marker segment. +\******************************************************************************/ + +static void jpc_com_destroyparms(jpc_ms_t *ms) +{ + jpc_com_t *com = &ms->parms.com; + if (com->data) { + jas_free(com->data); + } +} + +static int jpc_com_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_com_t *com = &ms->parms.com; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + if (jpc_getuint16(in, &com->regid)) { + return -1; + } + com->len = ms->len - 2; + if (com->len > 0) { + if (!(com->data = jas_malloc(com->len))) { + return -1; + } + if (jas_stream_read(in, com->data, com->len) != JAS_CAST(int, com->len)) { + return -1; + } + } else { + com->data = 0; + } + return 0; +} + +static int jpc_com_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + jpc_com_t *com = &ms->parms.com; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + if (jpc_putuint16(out, com->regid)) { + return -1; + } + if (jas_stream_write(out, com->data, com->len) != JAS_CAST(int, com->len)) { + return -1; + } + return 0; +} + +static int jpc_com_dumpparms(jpc_ms_t *ms, FILE *out) +{ + jpc_com_t *com = &ms->parms.com; + unsigned int i; + int printable; + fprintf(out, "regid = %d;\n", com->regid); + printable = 1; + for (i = 0; i < com->len; ++i) { + if (!isprint(com->data[i])) { + printable = 0; + break; + } + } + if (printable) { + fprintf(out, "data = "); + fwrite(com->data, sizeof(char), com->len, out); + fprintf(out, "\n"); + } + return 0; +} + +/******************************************************************************\ +* Operations for unknown types of marker segments. +\******************************************************************************/ + +static void jpc_unk_destroyparms(jpc_ms_t *ms) +{ + jpc_unk_t *unk = &ms->parms.unk; + if (unk->data) { + jas_free(unk->data); + } +} + +static int jpc_unk_getparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in) +{ + jpc_unk_t *unk = &ms->parms.unk; + + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + + if (ms->len > 0) { + if (!(unk->data = jas_malloc(ms->len * sizeof(unsigned char)))) { + return -1; + } + if (jas_stream_read(in, (char *) unk->data, ms->len) != JAS_CAST(int, ms->len)) { + jas_free(unk->data); + return -1; + } + unk->len = ms->len; + } else { + unk->data = 0; + unk->len = 0; + } + return 0; +} + +static int jpc_unk_putparms(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out) +{ + /* Eliminate compiler warning about unused variables. */ + cstate = 0; + ms = 0; + out = 0; + + /* If this function is called, we are trying to write an unsupported + type of marker segment. Return with an error indication. */ + return -1; +} + +static int jpc_unk_dumpparms(jpc_ms_t *ms, FILE *out) +{ + unsigned int i; + jpc_unk_t *unk = &ms->parms.unk; + for (i = 0; i < unk->len; ++i) { + fprintf(out, "%02x ", unk->data[i]); + } + return 0; +} + +/******************************************************************************\ +* Primitive I/O operations. +\******************************************************************************/ + +int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val) +{ + int c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + if (val) { + *val = c; + } + return 0; +} + +int jpc_putuint8(jas_stream_t *out, uint_fast8_t val) +{ + if (jas_stream_putc(out, val & 0xff) == EOF) { + return -1; + } + return 0; +} + +int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val) +{ + uint_fast16_t v; + int c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + v = c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + v = (v << 8) | c; + if (val) { + *val = v; + } + return 0; +} + +int jpc_putuint16(jas_stream_t *out, uint_fast16_t val) +{ + if (jas_stream_putc(out, (val >> 8) & 0xff) == EOF || + jas_stream_putc(out, val & 0xff) == EOF) { + return -1; + } + return 0; +} + +int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val) +{ + uint_fast32_t v; + int c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + v = c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + v = (v << 8) | c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + v = (v << 8) | c; + if ((c = jas_stream_getc(in)) == EOF) { + return -1; + } + v = (v << 8) | c; + if (val) { + *val = v; + } + return 0; +} + +int jpc_putuint32(jas_stream_t *out, uint_fast32_t val) +{ + if (jas_stream_putc(out, (val >> 24) & 0xff) == EOF || + jas_stream_putc(out, (val >> 16) & 0xff) == EOF || + jas_stream_putc(out, (val >> 8) & 0xff) == EOF || + jas_stream_putc(out, val & 0xff) == EOF) { + return -1; + } + return 0; +} + +/******************************************************************************\ +* Miscellany +\******************************************************************************/ + +static jpc_mstabent_t *jpc_mstab_lookup(int id) +{ + jpc_mstabent_t *mstabent; + for (mstabent = jpc_mstab;; ++mstabent) { + if (mstabent->id == id || mstabent->id < 0) { + return mstabent; + } + } + assert(0); + return 0; +} + +int jpc_validate(jas_stream_t *in) +{ + int n; + int i; + unsigned char buf[2]; + + assert(JAS_STREAM_MAXPUTBACK >= 2); + + if ((n = jas_stream_read(in, (char *) buf, 2)) < 0) { + return -1; + } + for (i = n - 1; i >= 0; --i) { + if (jas_stream_ungetc(in, buf[i]) == EOF) { + return -1; + } + } + if (n < 2) { + return -1; + } + if (buf[0] == (JPC_MS_SOC >> 8) && buf[1] == (JPC_MS_SOC & 0xff)) { + return 0; + } + return -1; +} + +int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long len) +{ + return jas_stream_copy(out, in, len); +} + +int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long len) +{ + return jas_stream_copy(out, in, len); +} diff --git a/src/libjasper/jpc/jpc_cs.h b/src/libjasper/jpc/jpc_cs.h new file mode 100644 index 0000000..49140d4 --- /dev/null +++ b/src/libjasper/jpc/jpc_cs.h @@ -0,0 +1,763 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * JPEG-2000 Code Stream Library + * + * $Id: jpc_cs.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_CS_H +#define JPC_CS_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_image.h" +#include "jasper/jas_stream.h" + +#include "jpc_cod.h" + +/******************************************************************************\ +* Constants and Types. +\******************************************************************************/ + +/* The maximum number of resolution levels. */ +#define JPC_MAXRLVLS 33 + +/* The maximum number of bands. */ +#define JPC_MAXBANDS (3 * JPC_MAXRLVLS + 1) + +/* The maximum number of layers. */ +#define JPC_MAXLYRS 16384 + +/**************************************\ +* Code stream. +\**************************************/ + +/* + * Code stream states. + */ + +/* Initial. */ +#define JPC_CS_INIT 0 +/* Main header. */ +#define JPC_CS_MHDR 1 +/* Tile-part header. */ +#define JPC_CS_THDR 2 +/* Main trailer. */ +#define JPC_CS_MTLR 3 +/* Tile-part data. */ +#define JPC_CS_TDATA 4 + +/* + * Unfortunately, the code stream syntax was not designed in such a way that + * any given marker segment can be correctly decoded without additional state + * derived from previously decoded marker segments. + * For example, a RGN/COC/QCC marker segment cannot be decoded unless the + * number of components is known. + */ + +/* + * Code stream state information. + */ + +typedef struct { + + /* The number of components. */ + uint_fast16_t numcomps; + +} jpc_cstate_t; + +/**************************************\ +* SOT marker segment parameters. +\**************************************/ + +typedef struct { + + /* The tile number. */ + uint_fast16_t tileno; + + /* The combined length of the marker segment and its auxilary data + (i.e., packet data). */ + uint_fast32_t len; + + /* The tile-part instance. */ + uint_fast8_t partno; + + /* The number of tile-parts. */ + uint_fast8_t numparts; + +} jpc_sot_t; + +/**************************************\ +* SIZ marker segment parameters. +\**************************************/ + +/* Per component information. */ + +typedef struct { + + /* The precision of the samples. */ + uint_fast8_t prec; + + /* The signedness of the samples. */ + uint_fast8_t sgnd; + + /* The horizontal separation of samples with respect to the reference + grid. */ + uint_fast8_t hsamp; + + /* The vertical separation of samples with respect to the reference + grid. */ + uint_fast8_t vsamp; + +} jpc_sizcomp_t; + +/* SIZ marker segment parameters. */ + +typedef struct { + + /* The code stream capabilities. */ + uint_fast16_t caps; + + /* The width of the image in units of the reference grid. */ + uint_fast32_t width; + + /* The height of the image in units of the reference grid. */ + uint_fast32_t height; + + /* The horizontal offset from the origin of the reference grid to the + left side of the image area. */ + uint_fast32_t xoff; + + /* The vertical offset from the origin of the reference grid to the + top side of the image area. */ + uint_fast32_t yoff; + + /* The nominal width of a tile in units of the reference grid. */ + uint_fast32_t tilewidth; + + /* The nominal height of a tile in units of the reference grid. */ + uint_fast32_t tileheight; + + /* The horizontal offset from the origin of the reference grid to the + left side of the first tile. */ + uint_fast32_t tilexoff; + + /* The vertical offset from the origin of the reference grid to the + top side of the first tile. */ + uint_fast32_t tileyoff; + + /* The number of components. */ + uint_fast16_t numcomps; + + /* The per-component information. */ + jpc_sizcomp_t *comps; + +} jpc_siz_t; + +/**************************************\ +* COD marker segment parameters. +\**************************************/ + +/* + * Coding style constants. + */ + +/* Precincts may be used. */ +#define JPC_COX_PRT 0x01 +/* SOP marker segments may be used. */ +#define JPC_COD_SOP 0x02 +/* EPH marker segments may be used. */ +#define JPC_COD_EPH 0x04 + +/* + * Progression order constants. + */ + +/* Layer-resolution-component-precinct progressive + (i.e., progressive by fidelity). */ +#define JPC_COD_LRCPPRG 0 +/* Resolution-layer-component-precinct progressive + (i.e., progressive by resolution). */ +#define JPC_COD_RLCPPRG 1 +/* Resolution-precinct-component-layer progressive. */ +#define JPC_COD_RPCLPRG 2 +/* Precinct-component-resolution-layer progressive. */ +#define JPC_COD_PCRLPRG 3 +/* Component-position-resolution-layer progressive. */ +#define JPC_COD_CPRLPRG 4 + +/* + * Code block style constants. + */ + +#define JPC_COX_LAZY 0x01 /* Selective arithmetic coding bypass. */ +#define JPC_COX_RESET 0x02 /* Reset context probabilities. */ +#define JPC_COX_TERMALL 0x04 /* Terminate all coding passes. */ +#define JPC_COX_VSC 0x08 /* Vertical stripe causal context formation. */ +#define JPC_COX_PTERM 0x10 /* Predictable termination. */ +#define JPC_COX_SEGSYM 0x20 /* Use segmentation symbols. */ + +/* Transform constants. */ +#define JPC_COX_INS 0x00 /* Irreversible 9/7. */ +#define JPC_COX_RFT 0x01 /* Reversible 5/3. */ + +/* Multicomponent transform constants. */ +#define JPC_COD_NOMCT 0x00 /* No multicomponent transform. */ +#define JPC_COD_MCT 0x01 /* Multicomponent transform. */ + +/* Get the code block size value from the code block size exponent. */ +#define JPC_COX_CBLKSIZEEXPN(x) ((x) - 2) +/* Get the code block size exponent from the code block size value. */ +#define JPC_COX_GETCBLKSIZEEXPN(x) ((x) + 2) + +/* Per resolution-level information. */ + +typedef struct { + + /* The packet partition width. */ + uint_fast8_t parwidthval; + + /* The packet partition height. */ + uint_fast8_t parheightval; + +} jpc_coxrlvl_t; + +/* Per component information. */ + +typedef struct { + + /* The coding style. */ + uint_fast8_t csty; + + /* The number of decomposition levels. */ + uint_fast8_t numdlvls; + + /* The nominal code block width specifier. */ + uint_fast8_t cblkwidthval; + + /* The nominal code block height specifier. */ + uint_fast8_t cblkheightval; + + /* The style of coding passes. */ + uint_fast8_t cblksty; + + /* The QMFB employed. */ + uint_fast8_t qmfbid; + + /* The number of resolution levels. */ + int numrlvls; + + /* The per-resolution-level information. */ + jpc_coxrlvl_t rlvls[JPC_MAXRLVLS]; + +} jpc_coxcp_t; + +/* COD marker segment parameters. */ + +typedef struct { + + /* The general coding style. */ + uint_fast8_t csty; + + /* The progression order. */ + uint_fast8_t prg; + + /* The number of layers. */ + uint_fast16_t numlyrs; + + /* The multicomponent transform. */ + uint_fast8_t mctrans; + + /* Component-related parameters. */ + jpc_coxcp_t compparms; + +} jpc_cod_t; + +/* COC marker segment parameters. */ + +typedef struct { + + /* The component number. */ + uint_fast16_t compno; + + /* Component-related parameters. */ + jpc_coxcp_t compparms; + +} jpc_coc_t; + +/**************************************\ +* RGN marker segment parameters. +\**************************************/ + +/* The maxshift ROI style. */ +#define JPC_RGN_MAXSHIFT 0x00 + +typedef struct { + + /* The component to which the marker applies. */ + uint_fast16_t compno; + + /* The ROI style. */ + uint_fast8_t roisty; + + /* The ROI shift value. */ + uint_fast8_t roishift; + +} jpc_rgn_t; + +/**************************************\ +* QCD/QCC marker segment parameters. +\**************************************/ + +/* + * Quantization style constants. + */ + +#define JPC_QCX_NOQNT 0 /* No quantization. */ +#define JPC_QCX_SIQNT 1 /* Scalar quantization, implicit. */ +#define JPC_QCX_SEQNT 2 /* Scalar quantization, explicit. */ + +/* + * Stepsize manipulation macros. + */ + +#define JPC_QCX_GETEXPN(x) ((x) >> 11) +#define JPC_QCX_GETMANT(x) ((x) & 0x07ff) +#define JPC_QCX_EXPN(x) (assert(!((x) & (~0x1f))), (((x) & 0x1f) << 11)) +#define JPC_QCX_MANT(x) (assert(!((x) & (~0x7ff))), ((x) & 0x7ff)) + +/* Per component information. */ + +typedef struct { + + /* The quantization style. */ + uint_fast8_t qntsty; + + /* The number of step sizes. */ + int numstepsizes; + + /* The step sizes. */ + uint_fast16_t *stepsizes; + + /* The number of guard bits. */ + uint_fast8_t numguard; + +} jpc_qcxcp_t; + +/* QCC marker segment parameters. */ + +typedef struct { + + /* The component associated with this marker segment. */ + uint_fast16_t compno; + + /* The parameters. */ + jpc_qcxcp_t compparms; + +} jpc_qcc_t; + +/* QCD marker segment parameters. */ + +typedef struct { + + /* The parameters. */ + jpc_qcxcp_t compparms; + +} jpc_qcd_t; + +/**************************************\ +* POD marker segment parameters. +\**************************************/ + +typedef struct { + + /* The progression order. */ + uint_fast8_t prgord; + + /* The lower bound (inclusive) on the resolution level for the + progression order volume. */ + uint_fast8_t rlvlnostart; + + /* The upper bound (exclusive) on the resolution level for the + progression order volume. */ + uint_fast8_t rlvlnoend; + + /* The lower bound (inclusive) on the component for the progression + order volume. */ + uint_fast16_t compnostart; + + /* The upper bound (exclusive) on the component for the progression + order volume. */ + uint_fast16_t compnoend; + + /* The upper bound (exclusive) on the layer for the progression + order volume. */ + uint_fast16_t lyrnoend; + +} jpc_pocpchg_t; + +/* An alias for the above type. */ +typedef jpc_pocpchg_t jpc_pchg_t; + +/* POC marker segment parameters. */ + +typedef struct { + + /* The number of progression order changes. */ + int numpchgs; + + /* The per-progression-order-change information. */ + jpc_pocpchg_t *pchgs; + +} jpc_poc_t; + +/**************************************\ +* PPM/PPT marker segment parameters. +\**************************************/ + +/* PPM marker segment parameters. */ + +typedef struct { + + /* The index. */ + uint_fast8_t ind; + + /* The length. */ + uint_fast16_t len; + + /* The data. */ + uchar *data; + +} jpc_ppm_t; + +/* PPT marker segment parameters. */ + +typedef struct { + + /* The index. */ + uint_fast8_t ind; + + /* The length. */ + uint_fast32_t len; + + /* The data. */ + unsigned char *data; + +} jpc_ppt_t; + +/**************************************\ +* COM marker segment parameters. +\**************************************/ + +/* + * Registration IDs. + */ + +#define JPC_COM_BIN 0x00 +#define JPC_COM_LATIN 0x01 + +typedef struct { + + /* The registration ID. */ + uint_fast16_t regid; + + /* The length of the data in bytes. */ + uint_fast16_t len; + + /* The data. */ + uchar *data; + +} jpc_com_t; + +/**************************************\ +* SOP marker segment parameters. +\**************************************/ + +typedef struct { + + /* The sequence number. */ + uint_fast16_t seqno; + +} jpc_sop_t; + +/**************************************\ +* CRG marker segment parameters. +\**************************************/ + +/* Per component information. */ + +typedef struct { + + /* The horizontal offset. */ + uint_fast16_t hoff; + + /* The vertical offset. */ + uint_fast16_t voff; + +} jpc_crgcomp_t; + +typedef struct { + + /* The number of components. */ + int numcomps; + + /* Per component information. */ + jpc_crgcomp_t *comps; + +} jpc_crg_t; + +/**************************************\ +* Marker segment parameters for unknown marker type. +\**************************************/ + +typedef struct { + + /* The data. */ + uchar *data; + + /* The length. */ + uint_fast16_t len; + +} jpc_unk_t; + +/**************************************\ +* Generic marker segment parameters. +\**************************************/ + +typedef union { + int soc; /* unused */ + jpc_sot_t sot; + int sod; /* unused */ + int eoc; /* unused */ + jpc_siz_t siz; + jpc_cod_t cod; + jpc_coc_t coc; + jpc_rgn_t rgn; + jpc_qcd_t qcd; + jpc_qcc_t qcc; + jpc_poc_t poc; + /* jpc_plm_t plm; */ + /* jpc_plt_t plt; */ + jpc_ppm_t ppm; + jpc_ppt_t ppt; + jpc_sop_t sop; + int eph; /* unused */ + jpc_com_t com; + jpc_crg_t crg; + jpc_unk_t unk; +} jpc_msparms_t; + +/**************************************\ +* Marker segment. +\**************************************/ + +/* Marker segment IDs. */ + +/* The smallest valid marker value. */ +#define JPC_MS_MIN 0xff00 + +/* The largest valid marker value. */ +#define JPC_MS_MAX 0xffff + +/* The minimum marker value that cannot occur within packet data. */ +#define JPC_MS_INMIN 0xff80 +/* The maximum marker value that cannot occur within packet data. */ +#define JPC_MS_INMAX 0xffff + +/* Delimiting marker segments. */ +#define JPC_MS_SOC 0xff4f /* Start of code stream (SOC). */ +#define JPC_MS_SOT 0xff90 /* Start of tile-part (SOT). */ +#define JPC_MS_SOD 0xff93 /* Start of data (SOD). */ +#define JPC_MS_EOC 0xffd9 /* End of code stream (EOC). */ + +/* Fixed information marker segments. */ +#define JPC_MS_SIZ 0xff51 /* Image and tile size (SIZ). */ + +/* Functional marker segments. */ +#define JPC_MS_COD 0xff52 /* Coding style default (COD). */ +#define JPC_MS_COC 0xff53 /* Coding style component (COC). */ +#define JPC_MS_RGN 0xff5e /* Region of interest (RGN). */ +#define JPC_MS_QCD 0xff5c /* Quantization default (QCD). */ +#define JPC_MS_QCC 0xff5d /* Quantization component (QCC). */ +#define JPC_MS_POC 0xff5f /* Progression order default (POC). */ + +/* Pointer marker segments. */ +#define JPC_MS_TLM 0xff55 /* Tile-part lengths, main header (TLM). */ +#define JPC_MS_PLM 0xff57 /* Packet length, main header (PLM). */ +#define JPC_MS_PLT 0xff58 /* Packet length, tile-part header (PLT). */ +#define JPC_MS_PPM 0xff60 /* Packed packet headers, main header (PPM). */ +#define JPC_MS_PPT 0xff61 /* Packet packet headers, tile-part header (PPT). */ + +/* In bit stream marker segments. */ +#define JPC_MS_SOP 0xff91 /* Start of packet (SOP). */ +#define JPC_MS_EPH 0xff92 /* End of packet header (EPH). */ + +/* Informational marker segments. */ +#define JPC_MS_CRG 0xff63 /* Component registration (CRG). */ +#define JPC_MS_COM 0xff64 /* Comment (COM). */ + +/* Forward declaration. */ +struct jpc_msops_s; + +/* Generic marker segment class. */ + +typedef struct { + + /* The type of marker segment. */ + uint_fast16_t id; + + /* The length of the marker segment. */ + uint_fast16_t len; + + /* The starting offset within the stream. */ + uint_fast32_t off; + + /* The parameters of the marker segment. */ + jpc_msparms_t parms; + + /* The marker segment operations. */ + struct jpc_msops_s *ops; + +} jpc_ms_t; + +/* Marker segment operations (which depend on the marker segment type). */ + +typedef struct jpc_msops_s { + + /* Destroy the marker segment parameters. */ + void (*destroyparms)(jpc_ms_t *ms); + + /* Get the marker segment parameters from a stream. */ + int (*getparms)(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *in); + + /* Put the marker segment parameters to a stream. */ + int (*putparms)(jpc_ms_t *ms, jpc_cstate_t *cstate, jas_stream_t *out); + + /* Dump the marker segment parameters (for debugging). */ + int (*dumpparms)(jpc_ms_t *ms, FILE *out); + +} jpc_msops_t; + +/******************************************************************************\ +* Macros/Functions. +\******************************************************************************/ + +/* Create a code-stream state object. */ +jpc_cstate_t *jpc_cstate_create(void); + +/* Destroy a code-stream state object. */ +void jpc_cstate_destroy(jpc_cstate_t *cstate); + +/* Create a marker segment. */ +jpc_ms_t *jpc_ms_create(int type); + +/* Destroy a marker segment. */ +void jpc_ms_destroy(jpc_ms_t *ms); + +/* Does a marker segment have parameters? */ +#define JPC_MS_HASPARMS(x) \ + (!((x) == JPC_MS_SOC || (x) == JPC_MS_SOD || (x) == JPC_MS_EOC || \ + (x) == JPC_MS_EPH || ((x) >= 0xff30 && (x) <= 0xff3f))) + +/* Get the marker segment type. */ +#define jpc_ms_gettype(ms) \ + ((ms)->id) + +/* Read a marker segment from a stream. */ +jpc_ms_t *jpc_getms(jas_stream_t *in, jpc_cstate_t *cstate); + +/* Write a marker segment to a stream. */ +int jpc_putms(jas_stream_t *out, jpc_cstate_t *cstate, jpc_ms_t *ms); + +/* Copy code stream data from one stream to another. */ +int jpc_getdata(jas_stream_t *in, jas_stream_t *out, long n); + +/* Copy code stream data from one stream to another. */ +int jpc_putdata(jas_stream_t *out, jas_stream_t *in, long n); + +/* Dump a marker segment (for debugging). */ +void jpc_ms_dump(jpc_ms_t *ms, FILE *out); + +/* Read a 8-bit unsigned integer from a stream. */ +int jpc_getuint8(jas_stream_t *in, uint_fast8_t *val); + +/* Read a 16-bit unsigned integer from a stream. */ +int jpc_getuint16(jas_stream_t *in, uint_fast16_t *val); + +/* Read a 32-bit unsigned integer from a stream. */ +int jpc_getuint32(jas_stream_t *in, uint_fast32_t *val); + +/* Write a 8-bit unsigned integer to a stream. */ +int jpc_putuint8(jas_stream_t *out, uint_fast8_t val); + +/* Write a 16-bit unsigned integer to a stream. */ +int jpc_putuint16(jas_stream_t *out, uint_fast16_t val); + +/* Write a 32-bit unsigned integer to a stream. */ +int jpc_putuint32(jas_stream_t *out, uint_fast32_t val); + +#endif diff --git a/src/libjasper/jpc/jpc_dec.c b/src/libjasper/jpc/jpc_dec.c new file mode 100644 index 0000000..f61c7e6 --- /dev/null +++ b/src/libjasper/jpc/jpc_dec.c @@ -0,0 +1,2348 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + + GeoJasper revision: Dima - multiple chanels and photoshop cs2 support + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * $Id: jpc_dec.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jasper/jas_types.h" +#include "jasper/jas_math.h" +#include "jasper/jas_tvp.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_debug.h" + +#include "jpc_fix.h" +#include "jpc_dec.h" +#include "jpc_cs.h" +#include "jpc_mct.h" +#include "jpc_t2dec.h" +#include "jpc_t1dec.h" +#include "jpc_math.h" + +/******************************************************************************\ +* +\******************************************************************************/ + +#define JPC_MHSOC 0x0001 + /* In the main header, expecting a SOC marker segment. */ +#define JPC_MHSIZ 0x0002 + /* In the main header, expecting a SIZ marker segment. */ +#define JPC_MH 0x0004 + /* In the main header, expecting "other" marker segments. */ +#define JPC_TPHSOT 0x0008 + /* In a tile-part header, expecting a SOT marker segment. */ +#define JPC_TPH 0x0010 + /* In a tile-part header, expecting "other" marker segments. */ +#define JPC_MT 0x0020 + /* In the main trailer. */ + +typedef struct { + + uint_fast16_t id; + /* The marker segment type. */ + + int validstates; + /* The states in which this type of marker segment can be + validly encountered. */ + + int (*action)(jpc_dec_t *dec, jpc_ms_t *ms); + /* The action to take upon encountering this type of marker segment. */ + +} jpc_dec_mstabent_t; + +/******************************************************************************\ +* +\******************************************************************************/ + +/* COD/COC parameters have been specified. */ +#define JPC_CSET 0x0001 +/* QCD/QCC parameters have been specified. */ +#define JPC_QSET 0x0002 +/* COD/COC parameters set from a COC marker segment. */ +#define JPC_COC 0x0004 +/* QCD/QCC parameters set from a QCC marker segment. */ +#define JPC_QCC 0x0008 + +/******************************************************************************\ +* Local function prototypes. +\******************************************************************************/ + +static int jpc_dec_dump(jpc_dec_t *dec, FILE *out); + +jpc_ppxstab_t *jpc_ppxstab_create(void); +void jpc_ppxstab_destroy(jpc_ppxstab_t *tab); +int jpc_ppxstab_grow(jpc_ppxstab_t *tab, int maxents); +int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent); +jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab); +int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab); +jpc_ppxstabent_t *jpc_ppxstabent_create(void); +void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent); + +int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist); +jpc_streamlist_t *jpc_streamlist_create(void); +int jpc_streamlist_insert(jpc_streamlist_t *streamlist, int streamno, + jas_stream_t *stream); +jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, int streamno); +void jpc_streamlist_destroy(jpc_streamlist_t *streamlist); +jas_stream_t *jpc_streamlist_get(jpc_streamlist_t *streamlist, int streamno); + +static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp); +static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps); +static int jpc_dec_cp_isvalid(jpc_dec_cp_t *cp); +static jpc_dec_cp_t *jpc_dec_cp_copy(jpc_dec_cp_t *cp); +static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, jpc_cod_t *cod); +static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, jpc_coc_t *coc); +static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, + jpc_coxcp_t *compparms, int flags); +static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, jpc_qcd_t *qcd); +static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, jpc_qcc_t *qcc); +static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, + jpc_qcxcp_t *compparms, int flags); +static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, jpc_rgn_t *rgn); +static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp); +static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp); +static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, jpc_poc_t *poc, int reset); +static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, jpc_poc_t *poc); + +static int jpc_dec_decode(jpc_dec_t *dec); +static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in); +static void jpc_dec_destroy(jpc_dec_t *dec); +static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize); +static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps); +static jpc_fix_t jpc_calcabsstepsize(int stepsize, int numbits); +static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile); +static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile); +static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile); +static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms); +static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts); + +static jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id); + +/******************************************************************************\ +* Global data. +\******************************************************************************/ + +jpc_dec_mstabent_t jpc_dec_mstab[] = { + {JPC_MS_SOC, JPC_MHSOC, jpc_dec_process_soc}, + {JPC_MS_SOT, JPC_MH | JPC_TPHSOT, jpc_dec_process_sot}, + {JPC_MS_SOD, JPC_TPH, jpc_dec_process_sod}, + {JPC_MS_EOC, JPC_TPHSOT, jpc_dec_process_eoc}, + {JPC_MS_SIZ, JPC_MHSIZ, jpc_dec_process_siz}, + {JPC_MS_COD, JPC_MH | JPC_TPH, jpc_dec_process_cod}, + {JPC_MS_COC, JPC_MH | JPC_TPH, jpc_dec_process_coc}, + {JPC_MS_RGN, JPC_MH | JPC_TPH, jpc_dec_process_rgn}, + {JPC_MS_QCD, JPC_MH | JPC_TPH, jpc_dec_process_qcd}, + {JPC_MS_QCC, JPC_MH | JPC_TPH, jpc_dec_process_qcc}, + {JPC_MS_POC, JPC_MH | JPC_TPH, jpc_dec_process_poc}, + {JPC_MS_TLM, JPC_MH, 0}, + {JPC_MS_PLM, JPC_MH, 0}, + {JPC_MS_PLT, JPC_TPH, 0}, + {JPC_MS_PPM, JPC_MH, jpc_dec_process_ppm}, + {JPC_MS_PPT, JPC_TPH, jpc_dec_process_ppt}, + {JPC_MS_SOP, 0, 0}, + {JPC_MS_CRG, JPC_MH, jpc_dec_process_crg}, + {JPC_MS_COM, JPC_MH | JPC_TPH, jpc_dec_process_com}, + {0, JPC_MH | JPC_TPH, jpc_dec_process_unk} +}; + +/******************************************************************************\ +* The main entry point for the JPEG-2000 decoder. +\******************************************************************************/ + +jas_image_t *jpc_decode(jas_stream_t *in, char *optstr) +{ + jpc_dec_importopts_t opts; + jpc_dec_t *dec; + jas_image_t *image; + unsigned int i; + + dec = 0; + + if (jpc_dec_parseopts(optstr, &opts)) { + goto error; + } + + jpc_initluts(); + + if (!(dec = jpc_dec_create(&opts, in))) { + goto error; + } + + /* Do most of the work. */ + if (jpc_dec_decode(dec)) { + goto error; + } + + // GeoJasper: dima - begin - first declare all components as GRAY and if there are more than 3 set first 3 as RGB + /* + if (jas_image_numcmpts(dec->image) >= 3) { + jas_image_setclrspc(dec->image, JAS_CLRSPC_SRGB); + jas_image_setcmpttype(dec->image, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); + jas_image_setcmpttype(dec->image, 1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); + jas_image_setcmpttype(dec->image, 2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); + } else { + jas_image_setclrspc(dec->image, JAS_CLRSPC_SGRAY); + jas_image_setcmpttype(dec->image, 0, + JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y)); + } + */ + + // GeoJasper: dima: fisrt define the default for color space to GRAY + jas_image_setclrspc(dec->image, JAS_CLRSPC_SGRAY); + for (i=0; iimage); ++i) + jas_image_setcmpttype(dec->image, i, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y)); + + if (jas_image_numcmpts(dec->image) >= 3) { + jas_image_setclrspc(dec->image, JAS_CLRSPC_SRGB); + jas_image_setcmpttype(dec->image, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); + jas_image_setcmpttype(dec->image, 1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); + jas_image_setcmpttype(dec->image, 2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); + } + // GeoJasper: dima - end - first declare all components as GRAY and if there are more than 3 set first 3 as RGB + + /* Save the return value. */ + image = dec->image; + + /* Stop the image from being discarded. */ + dec->image = 0; + + /* Destroy decoder. */ + jpc_dec_destroy(dec); + + return image; + +error: + if (dec) { + jpc_dec_destroy(dec); + } + return 0; +} + +typedef enum { + OPT_MAXLYRS, + OPT_MAXPKTS, + OPT_DEBUG +} optid_t; + +jas_taginfo_t decopts[] = { + {OPT_MAXLYRS, "maxlyrs"}, + {OPT_MAXPKTS, "maxpkts"}, + {OPT_DEBUG, "debug"}, + {-1, 0} +}; + +static int jpc_dec_parseopts(char *optstr, jpc_dec_importopts_t *opts) +{ + jas_tvparser_t *tvp; + + opts->debug = 0; + opts->maxlyrs = JPC_MAXLYRS; + opts->maxpkts = -1; + + if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { + return -1; + } + + while (!jas_tvparser_next(tvp)) { + switch (jas_taginfo_nonull(jas_taginfos_lookup(decopts, + jas_tvparser_gettag(tvp)))->id) { + case OPT_MAXLYRS: + opts->maxlyrs = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_DEBUG: + opts->debug = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_MAXPKTS: + opts->maxpkts = atoi(jas_tvparser_getval(tvp)); + break; + default: + jas_eprintf("warning: ignoring invalid option %s\n", + jas_tvparser_gettag(tvp)); + break; + } + } + + jas_tvparser_destroy(tvp); + + return 0; +} + +/******************************************************************************\ +* Code for table-driven code stream decoder. +\******************************************************************************/ + +static jpc_dec_mstabent_t *jpc_dec_mstab_lookup(uint_fast16_t id) +{ + jpc_dec_mstabent_t *mstabent; + for (mstabent = jpc_dec_mstab; mstabent->id != 0; ++mstabent) { + if (mstabent->id == id) { + break; + } + } + return mstabent; +} + +static int jpc_dec_decode(jpc_dec_t *dec) +{ + jpc_ms_t *ms; + jpc_dec_mstabent_t *mstabent; + int ret; + jpc_cstate_t *cstate; + + if (!(cstate = jpc_cstate_create())) { + return -1; + } + dec->cstate = cstate; + + /* Initially, we should expect to encounter a SOC marker segment. */ + dec->state = JPC_MHSOC; + + for (;;) { + + /* Get the next marker segment in the code stream. */ + if (!(ms = jpc_getms(dec->in, cstate))) { + + // GeoJasper: dima - adobe photoshop cs2 files seem not to end with the EOC marker + // although they carry additional pair of SOT/SOD markers + // we can catch this by checking for tile number and leave + if (dec->tiles && dec->tiles->partno >= dec->tiles->numparts && dec->state == JPC_TPHSOT) { + return 0; + } + + jas_eprintf("cannot get marker segment\n"); + return -1; + } + + mstabent = jpc_dec_mstab_lookup(ms->id); + assert(mstabent); + + /* Ensure that this type of marker segment is permitted + at this point in the code stream. */ + if (!(dec->state & mstabent->validstates)) { + jas_eprintf("unexpected marker segment type\n"); + jpc_ms_destroy(ms); + return -1; + } + + /* Process the marker segment. */ + if (mstabent->action) { + ret = (*mstabent->action)(dec, ms); + } else { + /* No explicit action is required. */ + ret = 0; + } + + /* Destroy the marker segment. */ + jpc_ms_destroy(ms); + + if (ret < 0) { + return -1; + } else if (ret > 0) { + break; + } + + } + + return 0; +} + +static int jpc_dec_process_crg(jpc_dec_t *dec, jpc_ms_t *ms) +{ + int cmptno; + jpc_dec_cmpt_t *cmpt; + jpc_crg_t *crg; + + crg = &ms->parms.crg; + for (cmptno = 0, cmpt = dec->cmpts; cmptno < dec->numcomps; ++cmptno, + ++cmpt) { + /* Ignore the information in the CRG marker segment for now. + This information serves no useful purpose for decoding anyhow. + Some other parts of the code need to be changed if these lines + are uncommented. + cmpt->hsubstep = crg->comps[cmptno].hoff; + cmpt->vsubstep = crg->comps[cmptno].voff; + */ + } + return 0; +} + +static int jpc_dec_process_soc(jpc_dec_t *dec, jpc_ms_t *ms) +{ + /* Eliminate warnings about unused variables. */ + ms = 0; + + /* We should expect to encounter a SIZ marker segment next. */ + dec->state = JPC_MHSIZ; + + return 0; +} + +static int jpc_dec_process_sot(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_dec_tile_t *tile; + jpc_sot_t *sot = &ms->parms.sot; + jas_image_cmptparm_t *compinfos; + jas_image_cmptparm_t *compinfo; + jpc_dec_cmpt_t *cmpt; + int cmptno; + + if (dec->state == JPC_MH) { + + compinfos = jas_malloc(dec->numcomps * sizeof(jas_image_cmptparm_t)); + assert(compinfos); + for (cmptno = 0, cmpt = dec->cmpts, compinfo = compinfos; + cmptno < dec->numcomps; ++cmptno, ++cmpt, ++compinfo) { + compinfo->tlx = 0; + compinfo->tly = 0; + compinfo->prec = cmpt->prec; + compinfo->sgnd = cmpt->sgnd; + compinfo->width = cmpt->width; + compinfo->height = cmpt->height; + compinfo->hstep = cmpt->hstep; + compinfo->vstep = cmpt->vstep; + } + + if (!(dec->image = jas_image_create(dec->numcomps, compinfos, + JAS_CLRSPC_UNKNOWN))) { + return -1; + } + jas_free(compinfos); + + /* Is the packet header information stored in PPM marker segments in + the main header? */ + if (dec->ppmstab) { + /* Convert the PPM marker segment data into a collection of streams + (one stream per tile-part). */ + if (!(dec->pkthdrstreams = jpc_ppmstabtostreams(dec->ppmstab))) { + abort(); + } + jpc_ppxstab_destroy(dec->ppmstab); + dec->ppmstab = 0; + } + } + + if (sot->len > 0) { + dec->curtileendoff = jas_stream_getrwcount(dec->in) - ms->len - + 4 + sot->len; + } else { + dec->curtileendoff = 0; + } + + if (JAS_CAST(int, sot->tileno) > dec->numtiles) { + jas_eprintf("invalid tile number in SOT marker segment\n"); + return -1; + } + /* Set the current tile. */ + dec->curtile = &dec->tiles[sot->tileno]; + tile = dec->curtile; + /* Ensure that this is the expected part number. */ + if (sot->partno != tile->partno) { + return -1; + } + if (tile->numparts > 0 && sot->partno >= tile->numparts) { + + // GeoJasper: dima - photoshop cs2 saves jpeg2000 with additional group of SOT/SOD + // here we simply ignore these boxes + if (tile->state == JPC_TILE_DONE) { + dec->state = JPC_TPH; + return 0; + } + + return -1; + } + if (!tile->numparts && sot->numparts > 0) { + tile->numparts = sot->numparts; + } + + tile->pptstab = 0; + + switch (tile->state) { + case JPC_TILE_INIT: + /* This is the first tile-part for this tile. */ + tile->state = JPC_TILE_ACTIVE; + assert(!tile->cp); + if (!(tile->cp = jpc_dec_cp_copy(dec->cp))) { + return -1; + } + jpc_dec_cp_resetflags(dec->cp); + break; + default: + if (sot->numparts == sot->partno - 1) { + tile->state = JPC_TILE_ACTIVELAST; + } + break; + } + + /* Note: We do not increment the expected tile-part number until + all processing for this tile-part is complete. */ + + /* We should expect to encounter other tile-part header marker + segments next. */ + dec->state = JPC_TPH; + + return 0; +} + +static int jpc_dec_process_sod(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_dec_tile_t *tile; + int pos; + + /* Eliminate compiler warnings about unused variables. */ + ms = 0; + + if (!(tile = dec->curtile)) { + return -1; + } + + if (!tile->partno) { + if (!jpc_dec_cp_isvalid(tile->cp)) { + return -1; + } + jpc_dec_cp_prepare(tile->cp); + if (jpc_dec_tileinit(dec, tile)) { + return -1; + } + } + + // GeoJasper: dima - photoshop cs2 saves jpeg2000 with additional group of SOT/SOD + // here we simply ignore these markers + if (tile->numparts > 0 && tile->partno >= tile->numparts) { + dec->curtile = 0; + // Increment the expected tile-part number. + ++tile->partno; + // We should expect to encounter a SOT marker segment next. + dec->state = JPC_TPHSOT; + return 0; + } + + /* Are packet headers stored in the main header or tile-part header? */ + if (dec->pkthdrstreams) { + /* Get the stream containing the packet header data for this + tile-part. */ + if (!(tile->pkthdrstream = jpc_streamlist_remove(dec->pkthdrstreams, 0))) { + return -1; + } + } + + if (tile->pptstab) { + if (!tile->pkthdrstream) { + if (!(tile->pkthdrstream = jas_stream_memopen(0, 0))) { + return -1; + } + } + pos = jas_stream_tell(tile->pkthdrstream); + jas_stream_seek(tile->pkthdrstream, 0, SEEK_END); + if (jpc_pptstabwrite(tile->pkthdrstream, tile->pptstab)) { + return -1; + } + jas_stream_seek(tile->pkthdrstream, pos, SEEK_SET); + jpc_ppxstab_destroy(tile->pptstab); + tile->pptstab = 0; + } + + if (jas_getdbglevel() >= 10) { + jpc_dec_dump(dec, stderr); + } + + if (jpc_dec_decodepkts(dec, (tile->pkthdrstream) ? tile->pkthdrstream : + dec->in, dec->in)) { + jas_eprintf("jpc_dec_decodepkts failed\n"); + return -1; + } + + /* Gobble any unconsumed tile data. */ + if (dec->curtileendoff > 0) { + long curoff; + uint_fast32_t n; + curoff = jas_stream_getrwcount(dec->in); + if (curoff < dec->curtileendoff) { + n = dec->curtileendoff - curoff; + jas_eprintf("warning: ignoring trailing garbage (%lu bytes)\n", + (unsigned long) n); + + while (n-- > 0) { + if (jas_stream_getc(dec->in) == EOF) { + jas_eprintf("read error\n"); + return -1; + } + } + } else if (curoff > dec->curtileendoff) { + jas_eprintf("warning: not enough tile data (%lu bytes)\n", + (unsigned long) curoff - dec->curtileendoff); + } + + } + + if (tile->numparts > 0 && tile->partno == tile->numparts - 1) { + if (jpc_dec_tiledecode(dec, tile)) { + return -1; + } + jpc_dec_tilefini(dec, tile); + } + + dec->curtile = 0; + + /* Increment the expected tile-part number. */ + ++tile->partno; + + /* We should expect to encounter a SOT marker segment next. */ + dec->state = JPC_TPHSOT; + + return 0; +} + +static int jpc_dec_tileinit(jpc_dec_t *dec, jpc_dec_tile_t *tile) +{ + jpc_dec_tcomp_t *tcomp; + int compno; + int rlvlno; + jpc_dec_rlvl_t *rlvl; + jpc_dec_band_t *band; + jpc_dec_prc_t *prc; + int bndno; + jpc_tsfb_band_t *bnd; + int bandno; + jpc_dec_ccp_t *ccp; + int prccnt; + jpc_dec_cblk_t *cblk; + int cblkcnt; + uint_fast32_t tlprcxstart; + uint_fast32_t tlprcystart; + uint_fast32_t brprcxend; + uint_fast32_t brprcyend; + uint_fast32_t tlcbgxstart; + uint_fast32_t tlcbgystart; + uint_fast32_t brcbgxend; + uint_fast32_t brcbgyend; + uint_fast32_t cbgxstart; + uint_fast32_t cbgystart; + uint_fast32_t cbgxend; + uint_fast32_t cbgyend; + uint_fast32_t tlcblkxstart; + uint_fast32_t tlcblkystart; + uint_fast32_t brcblkxend; + uint_fast32_t brcblkyend; + uint_fast32_t cblkxstart; + uint_fast32_t cblkystart; + uint_fast32_t cblkxend; + uint_fast32_t cblkyend; + uint_fast32_t tmpxstart; + uint_fast32_t tmpystart; + uint_fast32_t tmpxend; + uint_fast32_t tmpyend; + jpc_dec_cp_t *cp; + jpc_tsfb_band_t bnds[64]; + jpc_pchg_t *pchg; + int pchgno; + jpc_dec_cmpt_t *cmpt; + + cp = tile->cp; + tile->realmode = 0; + if (cp->mctid == JPC_MCT_ICT) { + tile->realmode = 1; + } + + for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < + dec->numcomps; ++compno, ++tcomp, ++cmpt) { + ccp = &tile->cp->ccps[compno]; + if (ccp->qmfbid == JPC_COX_INS) { + tile->realmode = 1; + } + tcomp->numrlvls = ccp->numrlvls; + if (!(tcomp->rlvls = jas_malloc(tcomp->numrlvls * + sizeof(jpc_dec_rlvl_t)))) { + return -1; + } + if (!(tcomp->data = jas_seq2d_create(JPC_CEILDIV(tile->xstart, + cmpt->hstep), JPC_CEILDIV(tile->ystart, cmpt->vstep), + JPC_CEILDIV(tile->xend, cmpt->hstep), JPC_CEILDIV(tile->yend, + cmpt->vstep)))) { + return -1; + } + if (!(tcomp->tsfb = jpc_cod_gettsfb(ccp->qmfbid, + tcomp->numrlvls - 1))) { + return -1; + } +{ + jpc_tsfb_getbands(tcomp->tsfb, jas_seq2d_xstart(tcomp->data), jas_seq2d_ystart(tcomp->data), jas_seq2d_xend(tcomp->data), jas_seq2d_yend(tcomp->data), bnds); +} + for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; + ++rlvlno, ++rlvl) { +rlvl->bands = 0; + rlvl->xstart = JPC_CEILDIVPOW2(tcomp->xstart, + tcomp->numrlvls - 1 - rlvlno); + rlvl->ystart = JPC_CEILDIVPOW2(tcomp->ystart, + tcomp->numrlvls - 1 - rlvlno); + rlvl->xend = JPC_CEILDIVPOW2(tcomp->xend, + tcomp->numrlvls - 1 - rlvlno); + rlvl->yend = JPC_CEILDIVPOW2(tcomp->yend, + tcomp->numrlvls - 1 - rlvlno); + rlvl->prcwidthexpn = ccp->prcwidthexpns[rlvlno]; + rlvl->prcheightexpn = ccp->prcheightexpns[rlvlno]; + tlprcxstart = JPC_FLOORDIVPOW2(rlvl->xstart, + rlvl->prcwidthexpn) << rlvl->prcwidthexpn; + tlprcystart = JPC_FLOORDIVPOW2(rlvl->ystart, + rlvl->prcheightexpn) << rlvl->prcheightexpn; + brprcxend = JPC_CEILDIVPOW2(rlvl->xend, + rlvl->prcwidthexpn) << rlvl->prcwidthexpn; + brprcyend = JPC_CEILDIVPOW2(rlvl->yend, + rlvl->prcheightexpn) << rlvl->prcheightexpn; + rlvl->numhprcs = (brprcxend - tlprcxstart) >> + rlvl->prcwidthexpn; + rlvl->numvprcs = (brprcyend - tlprcystart) >> + rlvl->prcheightexpn; + rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; + + if (rlvl->xstart >= rlvl->xend || rlvl->ystart >= rlvl->yend) { + rlvl->bands = 0; + rlvl->numprcs = 0; + rlvl->numhprcs = 0; + rlvl->numvprcs = 0; + continue; + } + if (!rlvlno) { + tlcbgxstart = tlprcxstart; + tlcbgystart = tlprcystart; + brcbgxend = brprcxend; + brcbgyend = brprcyend; + rlvl->cbgwidthexpn = rlvl->prcwidthexpn; + rlvl->cbgheightexpn = rlvl->prcheightexpn; + } else { + tlcbgxstart = JPC_CEILDIVPOW2(tlprcxstart, 1); + tlcbgystart = JPC_CEILDIVPOW2(tlprcystart, 1); + brcbgxend = JPC_CEILDIVPOW2(brprcxend, 1); + brcbgyend = JPC_CEILDIVPOW2(brprcyend, 1); + rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; + rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; + } + rlvl->cblkwidthexpn = JAS_MIN(ccp->cblkwidthexpn, + rlvl->cbgwidthexpn); + rlvl->cblkheightexpn = JAS_MIN(ccp->cblkheightexpn, + rlvl->cbgheightexpn); + + rlvl->numbands = (!rlvlno) ? 1 : 3; + if (!(rlvl->bands = jas_malloc(rlvl->numbands * + sizeof(jpc_dec_band_t)))) { + return -1; + } + for (bandno = 0, band = rlvl->bands; + bandno < rlvl->numbands; ++bandno, ++band) { + bndno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + + bandno + 1); + bnd = &bnds[bndno]; + + band->orient = bnd->orient; + band->stepsize = ccp->stepsizes[bndno]; + band->analgain = JPC_NOMINALGAIN(ccp->qmfbid, + tcomp->numrlvls - 1, rlvlno, band->orient); + band->absstepsize = jpc_calcabsstepsize(band->stepsize, + cmpt->prec + band->analgain); + band->numbps = ccp->numguardbits + + JPC_QCX_GETEXPN(band->stepsize) - 1; + band->roishift = (ccp->roishift + band->numbps >= JPC_PREC) ? + (JPC_PREC - 1 - band->numbps) : ccp->roishift; + band->data = 0; + band->prcs = 0; + if (bnd->xstart == bnd->xend || bnd->ystart == bnd->yend) { + continue; + } + if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) { + return -1; + } + jas_seq2d_bindsub(band->data, tcomp->data, bnd->locxstart, bnd->locystart, bnd->locxend, bnd->locyend); + jas_seq2d_setshift(band->data, bnd->xstart, bnd->ystart); + + assert(rlvl->numprcs); + + if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_dec_prc_t)))) { + return -1; + } + +/************************************************/ + cbgxstart = tlcbgxstart; + cbgystart = tlcbgystart; + for (prccnt = rlvl->numprcs, prc = band->prcs; + prccnt > 0; --prccnt, ++prc) { + cbgxend = cbgxstart + (1 << rlvl->cbgwidthexpn); + cbgyend = cbgystart + (1 << rlvl->cbgheightexpn); + prc->xstart = JAS_MAX(cbgxstart, JAS_CAST(uint_fast32_t, jas_seq2d_xstart(band->data))); + prc->ystart = JAS_MAX(cbgystart, JAS_CAST(uint_fast32_t, jas_seq2d_ystart(band->data))); + prc->xend = JAS_MIN(cbgxend, JAS_CAST(uint_fast32_t, jas_seq2d_xend(band->data))); + prc->yend = JAS_MIN(cbgyend, JAS_CAST(uint_fast32_t, jas_seq2d_yend(band->data))); + if (prc->xend > prc->xstart && prc->yend > prc->ystart) { + tlcblkxstart = JPC_FLOORDIVPOW2(prc->xstart, + rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn; + tlcblkystart = JPC_FLOORDIVPOW2(prc->ystart, + rlvl->cblkheightexpn) << rlvl->cblkheightexpn; + brcblkxend = JPC_CEILDIVPOW2(prc->xend, + rlvl->cblkwidthexpn) << rlvl->cblkwidthexpn; + brcblkyend = JPC_CEILDIVPOW2(prc->yend, + rlvl->cblkheightexpn) << rlvl->cblkheightexpn; + prc->numhcblks = (brcblkxend - tlcblkxstart) >> + rlvl->cblkwidthexpn; + prc->numvcblks = (brcblkyend - tlcblkystart) >> + rlvl->cblkheightexpn; + prc->numcblks = prc->numhcblks * prc->numvcblks; + assert(prc->numcblks > 0); + + if (!(prc->incltagtree = jpc_tagtree_create(prc->numhcblks, prc->numvcblks))) { + return -1; + } + if (!(prc->numimsbstagtree = jpc_tagtree_create(prc->numhcblks, prc->numvcblks))) { + return -1; + } + if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_dec_cblk_t)))) { + return -1; + } + + cblkxstart = cbgxstart; + cblkystart = cbgystart; + for (cblkcnt = prc->numcblks, cblk = prc->cblks; cblkcnt > 0;) { + cblkxend = cblkxstart + (1 << rlvl->cblkwidthexpn); + cblkyend = cblkystart + (1 << rlvl->cblkheightexpn); + tmpxstart = JAS_MAX(cblkxstart, prc->xstart); + tmpystart = JAS_MAX(cblkystart, prc->ystart); + tmpxend = JAS_MIN(cblkxend, prc->xend); + tmpyend = JAS_MIN(cblkyend, prc->yend); + if (tmpxend > tmpxstart && tmpyend > tmpystart) { + cblk->firstpassno = -1; + cblk->mqdec = 0; + cblk->nulldec = 0; + cblk->flags = 0; + cblk->numpasses = 0; + cblk->segs.head = 0; + cblk->segs.tail = 0; + cblk->curseg = 0; + cblk->numimsbs = 0; + cblk->numlenbits = 3; + cblk->flags = 0; + if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) { + return -1; + } + jas_seq2d_bindsub(cblk->data, band->data, tmpxstart, tmpystart, tmpxend, tmpyend); + ++cblk; + --cblkcnt; + } + cblkxstart += 1 << rlvl->cblkwidthexpn; + if (cblkxstart >= cbgxend) { + cblkxstart = cbgxstart; + cblkystart += 1 << rlvl->cblkheightexpn; + } + } + + } else { + prc->cblks = 0; + prc->incltagtree = 0; + prc->numimsbstagtree = 0; + } + cbgxstart += 1 << rlvl->cbgwidthexpn; + if (cbgxstart >= brcbgxend) { + cbgxstart = tlcbgxstart; + cbgystart += 1 << rlvl->cbgheightexpn; + } + + } +/********************************************/ + } + } + } + +if (!(tile->pi = jpc_dec_pi_create(dec, tile))) +{ + return -1; +} + + for (pchgno = 0; pchgno < jpc_pchglist_numpchgs(tile->cp->pchglist); + ++pchgno) { + pchg = jpc_pchg_copy(jpc_pchglist_get(tile->cp->pchglist, pchgno)); + assert(pchg); + jpc_pi_addpchg(tile->pi, pchg); + } + jpc_pi_init(tile->pi); + + return 0; +} + +static int jpc_dec_tilefini(jpc_dec_t *dec, jpc_dec_tile_t *tile) +{ + jpc_dec_tcomp_t *tcomp; + int compno; + int bandno; + int rlvlno; + jpc_dec_band_t *band; + jpc_dec_rlvl_t *rlvl; + int prcno; + jpc_dec_prc_t *prc; + jpc_dec_seg_t *seg; + jpc_dec_cblk_t *cblk; + int cblkno; + +if (tile->tcomps) { + + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; + ++rlvlno, ++rlvl) { +if (!rlvl->bands) { + continue; +} + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; ++bandno, ++band) { +if (band->prcs) { + for (prcno = 0, prc = band->prcs; prcno < + rlvl->numprcs; ++prcno, ++prc) { +if (!prc->cblks) { + continue; +} + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; ++cblkno, ++cblk) { + + while (cblk->segs.head) { + seg = cblk->segs.head; + jpc_seglist_remove(&cblk->segs, seg); + jpc_seg_destroy(seg); + } + jas_matrix_destroy(cblk->data); + if (cblk->mqdec) { + jpc_mqdec_destroy(cblk->mqdec); + } + if (cblk->nulldec) { + jpc_bitstream_close(cblk->nulldec); + } + if (cblk->flags) { + jas_matrix_destroy(cblk->flags); + } + } + if (prc->incltagtree) { + jpc_tagtree_destroy(prc->incltagtree); + } + if (prc->numimsbstagtree) { + jpc_tagtree_destroy(prc->numimsbstagtree); + } + if (prc->cblks) { + jas_free(prc->cblks); + } + } +} + if (band->data) { + jas_matrix_destroy(band->data); + } + if (band->prcs) { + jas_free(band->prcs); + } + } + if (rlvl->bands) { + jas_free(rlvl->bands); + } + } + if (tcomp->rlvls) { + jas_free(tcomp->rlvls); + } + if (tcomp->data) { + jas_matrix_destroy(tcomp->data); + } + if (tcomp->tsfb) { + jpc_tsfb_destroy(tcomp->tsfb); + } + } +} + if (tile->cp) { + jpc_dec_cp_destroy(tile->cp); + tile->cp = 0; + } + if (tile->tcomps) { + jas_free(tile->tcomps); + tile->tcomps = 0; + } + if (tile->pi) { + jpc_pi_destroy(tile->pi); + tile->pi = 0; + } + if (tile->pkthdrstream) { + jas_stream_close(tile->pkthdrstream); + tile->pkthdrstream = 0; + } + if (tile->pptstab) { + jpc_ppxstab_destroy(tile->pptstab); + tile->pptstab = 0; + } + + tile->state = JPC_TILE_DONE; + + return 0; +} + +static int jpc_dec_tiledecode(jpc_dec_t *dec, jpc_dec_tile_t *tile) +{ + int i; + int j; + jpc_dec_tcomp_t *tcomp; + jpc_dec_rlvl_t *rlvl; + jpc_dec_band_t *band; + int compno; + int rlvlno; + int bandno; + int adjust; + int v; + jpc_dec_ccp_t *ccp; + jpc_dec_cmpt_t *cmpt; + + if (jpc_dec_decodecblks(dec, tile)) { + jas_eprintf("jpc_dec_decodecblks failed\n"); + return -1; + } + + /* Perform dequantization. */ + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + ccp = &tile->cp->ccps[compno]; + for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < tcomp->numrlvls; + ++rlvlno, ++rlvl) { + if (!rlvl->bands) { + continue; + } + for (bandno = 0, band = rlvl->bands; + bandno < rlvl->numbands; ++bandno, ++band) { + if (!band->data) { + continue; + } + jpc_undo_roi(band->data, band->roishift, ccp->roishift - + band->roishift, band->numbps); + if (tile->realmode) { + jas_matrix_asl(band->data, JPC_FIX_FRACBITS); + jpc_dequantize(band->data, band->absstepsize); + } + + } + } + } + + /* Apply an inverse wavelet transform if necessary. */ + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + ccp = &tile->cp->ccps[compno]; + jpc_tsfb_synthesize(tcomp->tsfb, tcomp->data); + } + + + /* Apply an inverse intercomponent transform if necessary. */ + // GeoJasper: dima - if there are more components than 3 then do apply the intercomponent transform on first three + if (dec->numcomps >= 3) + switch (tile->cp->mctid) { + case JPC_MCT_RCT: + //assert(dec->numcomps == 3); // GeoJasper: dima - still apply if there are more component + jpc_irct(tile->tcomps[0].data, tile->tcomps[1].data, tile->tcomps[2].data); + break; + case JPC_MCT_ICT: + //assert(dec->numcomps == 3); // GeoJasper: dima - still apply if there are more component + jpc_iict(tile->tcomps[0].data, tile->tcomps[1].data, tile->tcomps[2].data); + break; + } + + /* Perform rounding and convert to integer values. */ + if (tile->realmode) { + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + for (i = 0; i < jas_matrix_numrows(tcomp->data); ++i) { + for (j = 0; j < jas_matrix_numcols(tcomp->data); ++j) { + v = jas_matrix_get(tcomp->data, i, j); + v = jpc_fix_round(v); + jas_matrix_set(tcomp->data, i, j, jpc_fixtoint(v)); + } + } + } + } + + /* Perform level shift. */ + for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < + dec->numcomps; ++compno, ++tcomp, ++cmpt) { + adjust = cmpt->sgnd ? 0 : (1 << (cmpt->prec - 1)); + for (i = 0; i < jas_matrix_numrows(tcomp->data); ++i) { + for (j = 0; j < jas_matrix_numcols(tcomp->data); ++j) { + *jas_matrix_getref(tcomp->data, i, j) += adjust; + } + } + } + + /* Perform clipping. */ + for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < + dec->numcomps; ++compno, ++tcomp, ++cmpt) { + jpc_fix_t mn; + jpc_fix_t mx; + mn = cmpt->sgnd ? (-(1 << (cmpt->prec - 1))) : (0); + mx = cmpt->sgnd ? ((1 << (cmpt->prec - 1)) - 1) : ((1 << + cmpt->prec) - 1); + jas_matrix_clip(tcomp->data, mn, mx); + } + + /* XXX need to free tsfb struct */ + + /* Write the data for each component of the image. */ + for (compno = 0, tcomp = tile->tcomps, cmpt = dec->cmpts; compno < + dec->numcomps; ++compno, ++tcomp, ++cmpt) { + if (jas_image_writecmpt(dec->image, compno, tcomp->xstart - + JPC_CEILDIV(dec->xstart, cmpt->hstep), tcomp->ystart - + JPC_CEILDIV(dec->ystart, cmpt->vstep), jas_matrix_numcols( + tcomp->data), jas_matrix_numrows(tcomp->data), tcomp->data)) { + jas_eprintf("write component failed\n"); + return -4; + } + } + + return 0; +} + +static int jpc_dec_process_eoc(jpc_dec_t *dec, jpc_ms_t *ms) +{ + int tileno; + jpc_dec_tile_t *tile; + + /* Eliminate compiler warnings about unused variables. */ + ms = 0; + + for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno, + ++tile) { + if (tile->state == JPC_TILE_ACTIVE) { + if (jpc_dec_tiledecode(dec, tile)) { + return -1; + } + } + jpc_dec_tilefini(dec, tile); + } + + /* We are done processing the code stream. */ + dec->state = JPC_MT; + + return 1; +} + +static int jpc_dec_process_siz(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_siz_t *siz = &ms->parms.siz; + int compno; + int tileno; + jpc_dec_tile_t *tile; + jpc_dec_tcomp_t *tcomp; + int htileno; + int vtileno; + jpc_dec_cmpt_t *cmpt; + + dec->xstart = siz->xoff; + dec->ystart = siz->yoff; + dec->xend = siz->width; + dec->yend = siz->height; + dec->tilewidth = siz->tilewidth; + dec->tileheight = siz->tileheight; + dec->tilexoff = siz->tilexoff; + dec->tileyoff = siz->tileyoff; + dec->numcomps = siz->numcomps; + if (!(dec->cp = jpc_dec_cp_create(dec->numcomps))) { + return -1; + } + + if (!(dec->cmpts = jas_malloc(dec->numcomps * sizeof(jpc_dec_cmpt_t)))) { + return -1; + } + + for (compno = 0, cmpt = dec->cmpts; compno < dec->numcomps; ++compno, + ++cmpt) { + cmpt->prec = siz->comps[compno].prec; + cmpt->sgnd = siz->comps[compno].sgnd; + cmpt->hstep = siz->comps[compno].hsamp; + cmpt->vstep = siz->comps[compno].vsamp; + cmpt->width = JPC_CEILDIV(dec->xend, cmpt->hstep) - + JPC_CEILDIV(dec->xstart, cmpt->hstep); + cmpt->height = JPC_CEILDIV(dec->yend, cmpt->vstep) - + JPC_CEILDIV(dec->ystart, cmpt->vstep); + cmpt->hsubstep = 0; + cmpt->vsubstep = 0; + } + + dec->image = 0; + + dec->numhtiles = JPC_CEILDIV(dec->xend - dec->tilexoff, dec->tilewidth); + dec->numvtiles = JPC_CEILDIV(dec->yend - dec->tileyoff, dec->tileheight); + dec->numtiles = dec->numhtiles * dec->numvtiles; + if (!(dec->tiles = jas_malloc(dec->numtiles * sizeof(jpc_dec_tile_t)))) { + return -1; + } + + for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; ++tileno, + ++tile) { + htileno = tileno % dec->numhtiles; + vtileno = tileno / dec->numhtiles; + tile->realmode = 0; + tile->state = JPC_TILE_INIT; + tile->xstart = JAS_MAX(dec->tilexoff + htileno * dec->tilewidth, + dec->xstart); + tile->ystart = JAS_MAX(dec->tileyoff + vtileno * dec->tileheight, + dec->ystart); + tile->xend = JAS_MIN(dec->tilexoff + (htileno + 1) * + dec->tilewidth, dec->xend); + tile->yend = JAS_MIN(dec->tileyoff + (vtileno + 1) * + dec->tileheight, dec->yend); + tile->numparts = 0; + tile->partno = 0; + tile->pkthdrstream = 0; + tile->pkthdrstreampos = 0; + tile->pptstab = 0; + tile->cp = 0; + if (!(tile->tcomps = jas_malloc(dec->numcomps * + sizeof(jpc_dec_tcomp_t)))) { + return -1; + } + for (compno = 0, cmpt = dec->cmpts, tcomp = tile->tcomps; + compno < dec->numcomps; ++compno, ++cmpt, ++tcomp) { + tcomp->rlvls = 0; + tcomp->data = 0; + tcomp->xstart = JPC_CEILDIV(tile->xstart, cmpt->hstep); + tcomp->ystart = JPC_CEILDIV(tile->ystart, cmpt->vstep); + tcomp->xend = JPC_CEILDIV(tile->xend, cmpt->hstep); + tcomp->yend = JPC_CEILDIV(tile->yend, cmpt->vstep); + tcomp->tsfb = 0; + } + } + + dec->pkthdrstreams = 0; + + /* We should expect to encounter other main header marker segments + or an SOT marker segment next. */ + dec->state = JPC_MH; + + return 0; +} + +static int jpc_dec_process_cod(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_cod_t *cod = &ms->parms.cod; + jpc_dec_tile_t *tile; + + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromcod(dec->cp, cod); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno != 0) { + return -1; + } + jpc_dec_cp_setfromcod(tile->cp, cod); + break; + } + return 0; +} + +static int jpc_dec_process_coc(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_coc_t *coc = &ms->parms.coc; + jpc_dec_tile_t *tile; + + if (JAS_CAST(int, coc->compno) > dec->numcomps) { + jas_eprintf("invalid component number in COC marker segment\n"); + return -1; + } + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromcoc(dec->cp, coc); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno > 0) { + return -1; + } + jpc_dec_cp_setfromcoc(tile->cp, coc); + break; + } + return 0; +} + +static int jpc_dec_process_rgn(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_rgn_t *rgn = &ms->parms.rgn; + jpc_dec_tile_t *tile; + + if (JAS_CAST(int, rgn->compno) > dec->numcomps) { + jas_eprintf("invalid component number in RGN marker segment\n"); + return -1; + } + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromrgn(dec->cp, rgn); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno > 0) { + return -1; + } + jpc_dec_cp_setfromrgn(tile->cp, rgn); + break; + } + + return 0; +} + +static int jpc_dec_process_qcd(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_qcd_t *qcd = &ms->parms.qcd; + jpc_dec_tile_t *tile; + + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromqcd(dec->cp, qcd); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno > 0) { + return -1; + } + jpc_dec_cp_setfromqcd(tile->cp, qcd); + break; + } + return 0; +} + +static int jpc_dec_process_qcc(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_qcc_t *qcc = &ms->parms.qcc; + jpc_dec_tile_t *tile; + + if (JAS_CAST(int, qcc->compno) > dec->numcomps) { + jas_eprintf("invalid component number in QCC marker segment\n"); + return -1; + } + switch (dec->state) { + case JPC_MH: + jpc_dec_cp_setfromqcc(dec->cp, qcc); + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (tile->partno > 0) { + return -1; + } + jpc_dec_cp_setfromqcc(tile->cp, qcc); + break; + } + return 0; +} + +static int jpc_dec_process_poc(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_poc_t *poc = &ms->parms.poc; + jpc_dec_tile_t *tile; + switch (dec->state) { + case JPC_MH: + if (jpc_dec_cp_setfrompoc(dec->cp, poc, 1)) { + return -1; + } + break; + case JPC_TPH: + if (!(tile = dec->curtile)) { + return -1; + } + if (!tile->partno) { + if (jpc_dec_cp_setfrompoc(tile->cp, poc, (!tile->partno))) { + return -1; + } + } else { + jpc_pi_addpchgfrompoc(tile->pi, poc); + } + break; + } + return 0; +} + +static int jpc_dec_process_ppm(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_ppm_t *ppm = &ms->parms.ppm; + jpc_ppxstabent_t *ppmstabent; + + if (!dec->ppmstab) { + if (!(dec->ppmstab = jpc_ppxstab_create())) { + return -1; + } + } + + if (!(ppmstabent = jpc_ppxstabent_create())) { + return -1; + } + ppmstabent->ind = ppm->ind; + ppmstabent->data = ppm->data; + ppm->data = 0; + ppmstabent->len = ppm->len; + if (jpc_ppxstab_insert(dec->ppmstab, ppmstabent)) { + return -1; + } + return 0; +} + +static int jpc_dec_process_ppt(jpc_dec_t *dec, jpc_ms_t *ms) +{ + jpc_ppt_t *ppt = &ms->parms.ppt; + jpc_dec_tile_t *tile; + jpc_ppxstabent_t *pptstabent; + + tile = dec->curtile; + if (!tile->pptstab) { + if (!(tile->pptstab = jpc_ppxstab_create())) { + return -1; + } + } + if (!(pptstabent = jpc_ppxstabent_create())) { + return -1; + } + pptstabent->ind = ppt->ind; + pptstabent->data = ppt->data; + ppt->data = 0; + pptstabent->len = ppt->len; + if (jpc_ppxstab_insert(tile->pptstab, pptstabent)) { + return -1; + } + return 0; +} + +static int jpc_dec_process_com(jpc_dec_t *dec, jpc_ms_t *ms) +{ + /* Eliminate compiler warnings about unused variables. */ + dec = 0; + ms = 0; + + return 0; +} + +static int jpc_dec_process_unk(jpc_dec_t *dec, jpc_ms_t *ms) +{ + /* Eliminate compiler warnings about unused variables. */ + dec = 0; + + jas_eprintf("warning: ignoring unknown marker segment\n"); + jpc_ms_dump(ms, stderr); + return 0; +} + +/******************************************************************************\ +* +\******************************************************************************/ + +static jpc_dec_cp_t *jpc_dec_cp_create(uint_fast16_t numcomps) +{ + jpc_dec_cp_t *cp; + jpc_dec_ccp_t *ccp; + int compno; + + if (!(cp = jas_malloc(sizeof(jpc_dec_cp_t)))) { + return 0; + } + cp->flags = 0; + cp->numcomps = numcomps; + cp->prgord = 0; + cp->numlyrs = 0; + cp->mctid = 0; + cp->csty = 0; + if (!(cp->ccps = jas_malloc(cp->numcomps * sizeof(jpc_dec_ccp_t)))) { + return 0; + } + if (!(cp->pchglist = jpc_pchglist_create())) { + jas_free(cp->ccps); + return 0; + } + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + ccp->flags = 0; + ccp->numrlvls = 0; + ccp->cblkwidthexpn = 0; + ccp->cblkheightexpn = 0; + ccp->qmfbid = 0; + ccp->numstepsizes = 0; + ccp->numguardbits = 0; + ccp->roishift = 0; + ccp->cblkctx = 0; + } + return cp; +} + +static jpc_dec_cp_t *jpc_dec_cp_copy(jpc_dec_cp_t *cp) +{ + jpc_dec_cp_t *newcp; + jpc_dec_ccp_t *newccp; + jpc_dec_ccp_t *ccp; + int compno; + + if (!(newcp = jpc_dec_cp_create(cp->numcomps))) { + return 0; + } + newcp->flags = cp->flags; + newcp->prgord = cp->prgord; + newcp->numlyrs = cp->numlyrs; + newcp->mctid = cp->mctid; + newcp->csty = cp->csty; + jpc_pchglist_destroy(newcp->pchglist); + newcp->pchglist = 0; + if (!(newcp->pchglist = jpc_pchglist_copy(cp->pchglist))) { + jas_free(newcp); + return 0; + } + for (compno = 0, newccp = newcp->ccps, ccp = cp->ccps; + compno < cp->numcomps; + ++compno, ++newccp, ++ccp) { + *newccp = *ccp; + } + return newcp; +} + +static void jpc_dec_cp_resetflags(jpc_dec_cp_t *cp) +{ + int compno; + jpc_dec_ccp_t *ccp; + cp->flags &= (JPC_CSET | JPC_QSET); + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + ccp->flags = 0; + } +} + +static void jpc_dec_cp_destroy(jpc_dec_cp_t *cp) +{ + if (cp->ccps) { + jas_free(cp->ccps); + } + if (cp->pchglist) { + jpc_pchglist_destroy(cp->pchglist); + } + jas_free(cp); +} + +static int jpc_dec_cp_isvalid(jpc_dec_cp_t *cp) +{ + uint_fast16_t compcnt; + jpc_dec_ccp_t *ccp; + + if (!(cp->flags & JPC_CSET) || !(cp->flags & JPC_QSET)) { + return 0; + } + for (compcnt = cp->numcomps, ccp = cp->ccps; compcnt > 0; --compcnt, + ++ccp) { + /* Is there enough step sizes for the number of bands? */ + if ((ccp->qsty != JPC_QCX_SIQNT && JAS_CAST(int, ccp->numstepsizes) < 3 * + ccp->numrlvls - 2) || (ccp->qsty == JPC_QCX_SIQNT && + ccp->numstepsizes != 1)) { + return 0; + } + } + return 1; +} + +static void calcstepsizes(uint_fast16_t refstepsize, int numrlvls, + uint_fast16_t *stepsizes) +{ + int bandno; + int numbands; + uint_fast16_t expn; + uint_fast16_t mant; + expn = JPC_QCX_GETEXPN(refstepsize); + mant = JPC_QCX_GETMANT(refstepsize); + numbands = 3 * numrlvls - 2; + for (bandno = 0; bandno < numbands; ++bandno) { + stepsizes[bandno] = JPC_QCX_MANT(mant) | JPC_QCX_EXPN(expn + + (numrlvls - 1) - (numrlvls - 1 - ((bandno > 0) ? ((bandno + 2) / 3) : (0)))); + } +} + +static int jpc_dec_cp_prepare(jpc_dec_cp_t *cp) +{ + jpc_dec_ccp_t *ccp; + int compno; + int i; + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + if (!(ccp->csty & JPC_COX_PRT)) { + for (i = 0; i < JPC_MAXRLVLS; ++i) { + ccp->prcwidthexpns[i] = 15; + ccp->prcheightexpns[i] = 15; + } + } + if (ccp->qsty == JPC_QCX_SIQNT) { + calcstepsizes(ccp->stepsizes[0], ccp->numrlvls, ccp->stepsizes); + } + } + return 0; +} + +static int jpc_dec_cp_setfromcod(jpc_dec_cp_t *cp, jpc_cod_t *cod) +{ + jpc_dec_ccp_t *ccp; + int compno; + cp->flags |= JPC_CSET; + cp->prgord = cod->prg; + if (cod->mctrans) { + cp->mctid = (cod->compparms.qmfbid == JPC_COX_INS) ? (JPC_MCT_ICT) : (JPC_MCT_RCT); + } else { + cp->mctid = JPC_MCT_NONE; + } + cp->numlyrs = cod->numlyrs; + cp->csty = cod->csty & (JPC_COD_SOP | JPC_COD_EPH); + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + jpc_dec_cp_setfromcox(cp, ccp, &cod->compparms, 0); + } + cp->flags |= JPC_CSET; + return 0; +} + +static int jpc_dec_cp_setfromcoc(jpc_dec_cp_t *cp, jpc_coc_t *coc) +{ + jpc_dec_cp_setfromcox(cp, &cp->ccps[coc->compno], &coc->compparms, JPC_COC); + return 0; +} + +static int jpc_dec_cp_setfromcox(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, + jpc_coxcp_t *compparms, int flags) +{ + int rlvlno; + + /* Eliminate compiler warnings about unused variables. */ + cp = 0; + + if ((flags & JPC_COC) || !(ccp->flags & JPC_COC)) { + ccp->numrlvls = compparms->numdlvls + 1; + ccp->cblkwidthexpn = JPC_COX_GETCBLKSIZEEXPN( + compparms->cblkwidthval); + ccp->cblkheightexpn = JPC_COX_GETCBLKSIZEEXPN( + compparms->cblkheightval); + ccp->qmfbid = compparms->qmfbid; + ccp->cblkctx = compparms->cblksty; + ccp->csty = compparms->csty & JPC_COX_PRT; + for (rlvlno = 0; rlvlno < compparms->numrlvls; ++rlvlno) { + ccp->prcwidthexpns[rlvlno] = + compparms->rlvls[rlvlno].parwidthval; + ccp->prcheightexpns[rlvlno] = + compparms->rlvls[rlvlno].parheightval; + } + ccp->flags |= flags | JPC_CSET; + } + return 0; +} + +static int jpc_dec_cp_setfromqcd(jpc_dec_cp_t *cp, jpc_qcd_t *qcd) +{ + int compno; + jpc_dec_ccp_t *ccp; + for (compno = 0, ccp = cp->ccps; compno < cp->numcomps; + ++compno, ++ccp) { + jpc_dec_cp_setfromqcx(cp, ccp, &qcd->compparms, 0); + } + cp->flags |= JPC_QSET; + return 0; +} + +static int jpc_dec_cp_setfromqcc(jpc_dec_cp_t *cp, jpc_qcc_t *qcc) +{ + return jpc_dec_cp_setfromqcx(cp, &cp->ccps[qcc->compno], &qcc->compparms, JPC_QCC); +} + +static int jpc_dec_cp_setfromqcx(jpc_dec_cp_t *cp, jpc_dec_ccp_t *ccp, + jpc_qcxcp_t *compparms, int flags) +{ + int bandno; + + /* Eliminate compiler warnings about unused variables. */ + cp = 0; + + if ((flags & JPC_QCC) || !(ccp->flags & JPC_QCC)) { + ccp->flags |= flags | JPC_QSET; + for (bandno = 0; bandno < compparms->numstepsizes; ++bandno) { + ccp->stepsizes[bandno] = compparms->stepsizes[bandno]; + } + ccp->numstepsizes = compparms->numstepsizes; + ccp->numguardbits = compparms->numguard; + ccp->qsty = compparms->qntsty; + } + return 0; +} + +static int jpc_dec_cp_setfromrgn(jpc_dec_cp_t *cp, jpc_rgn_t *rgn) +{ + jpc_dec_ccp_t *ccp; + ccp = &cp->ccps[rgn->compno]; + ccp->roishift = rgn->roishift; + return 0; +} + +static int jpc_pi_addpchgfrompoc(jpc_pi_t *pi, jpc_poc_t *poc) +{ + int pchgno; + jpc_pchg_t *pchg; + for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) { + if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) { + return -1; + } + if (jpc_pchglist_insert(pi->pchglist, -1, pchg)) { + return -1; + } + } + return 0; +} + +static int jpc_dec_cp_setfrompoc(jpc_dec_cp_t *cp, jpc_poc_t *poc, int reset) +{ + int pchgno; + jpc_pchg_t *pchg; + if (reset) { + while (jpc_pchglist_numpchgs(cp->pchglist) > 0) { + pchg = jpc_pchglist_remove(cp->pchglist, 0); + jpc_pchg_destroy(pchg); + } + } + for (pchgno = 0; pchgno < poc->numpchgs; ++pchgno) { + if (!(pchg = jpc_pchg_copy(&poc->pchgs[pchgno]))) { + return -1; + } + if (jpc_pchglist_insert(cp->pchglist, -1, pchg)) { + return -1; + } + } + return 0; +} + +static jpc_fix_t jpc_calcabsstepsize(int stepsize, int numbits) +{ + jpc_fix_t absstepsize; + int n; + + absstepsize = jpc_inttofix(1); + n = JPC_FIX_FRACBITS - 11; + absstepsize |= (n >= 0) ? (JPC_QCX_GETMANT(stepsize) << n) : + (JPC_QCX_GETMANT(stepsize) >> (-n)); + n = numbits - JPC_QCX_GETEXPN(stepsize); + absstepsize = (n >= 0) ? (absstepsize << n) : (absstepsize >> (-n)); + return absstepsize; +} + +static void jpc_dequantize(jas_matrix_t *x, jpc_fix_t absstepsize) +{ + int i; + int j; + int t; + + assert(absstepsize >= 0); + if (absstepsize == jpc_inttofix(1)) { + return; + } + + for (i = 0; i < jas_matrix_numrows(x); ++i) { + for (j = 0; j < jas_matrix_numcols(x); ++j) { + t = jas_matrix_get(x, i, j); + if (t) { + t = jpc_fix_mul(t, absstepsize); + } else { + t = 0; + } + jas_matrix_set(x, i, j, t); + } + } + +} + +static void jpc_undo_roi(jas_matrix_t *x, int roishift, int bgshift, int numbps) +{ + int i; + int j; + int thresh; + jpc_fix_t val; + jpc_fix_t mag; + bool warn; + uint_fast32_t mask; + + if (roishift == 0 && bgshift == 0) { + return; + } + thresh = 1 << roishift; + + warn = false; + for (i = 0; i < jas_matrix_numrows(x); ++i) { + for (j = 0; j < jas_matrix_numcols(x); ++j) { + val = jas_matrix_get(x, i, j); + mag = JAS_ABS(val); + if (mag >= thresh) { + /* We are dealing with ROI data. */ + mag >>= roishift; + val = (val < 0) ? (-mag) : mag; + jas_matrix_set(x, i, j, val); + } else { + /* We are dealing with non-ROI (i.e., background) data. */ + mag <<= bgshift; + mask = (1 << numbps) - 1; + /* Perform a basic sanity check on the sample value. */ + /* Some implementations write garbage in the unused + most-significant bit planes introduced by ROI shifting. + Here we ensure that any such bits are masked off. */ + if (mag & (~mask)) { + if (!warn) { + jas_eprintf("warning: possibly corrupt code stream\n"); + warn = true; + } + mag &= mask; + } + val = (val < 0) ? (-mag) : mag; + jas_matrix_set(x, i, j, val); + } + } + } +} + +static jpc_dec_t *jpc_dec_create(jpc_dec_importopts_t *impopts, jas_stream_t *in) +{ + jpc_dec_t *dec; + + if (!(dec = jas_malloc(sizeof(jpc_dec_t)))) { + return 0; + } + + dec->image = 0; + dec->xstart = 0; + dec->ystart = 0; + dec->xend = 0; + dec->yend = 0; + dec->tilewidth = 0; + dec->tileheight = 0; + dec->tilexoff = 0; + dec->tileyoff = 0; + dec->numhtiles = 0; + dec->numvtiles = 0; + dec->numtiles = 0; + dec->tiles = 0; + dec->curtile = 0; + dec->numcomps = 0; + dec->in = in; + dec->cp = 0; + dec->maxlyrs = impopts->maxlyrs; + dec->maxpkts = impopts->maxpkts; +dec->numpkts = 0; + dec->ppmseqno = 0; + dec->state = 0; + dec->cmpts = 0; + dec->pkthdrstreams = 0; + dec->ppmstab = 0; + dec->curtileendoff = 0; + + return dec; +} + +static void jpc_dec_destroy(jpc_dec_t *dec) +{ + if (dec->cstate) { + jpc_cstate_destroy(dec->cstate); + } + if (dec->pkthdrstreams) { + jpc_streamlist_destroy(dec->pkthdrstreams); + } + if (dec->image) { + jas_image_destroy(dec->image); + } + + if (dec->cp) { + jpc_dec_cp_destroy(dec->cp); + } + + if (dec->cmpts) { + jas_free(dec->cmpts); + } + + if (dec->tiles) { + jas_free(dec->tiles); + } + + jas_free(dec); +} + +/******************************************************************************\ +* +\******************************************************************************/ + +void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins, jpc_dec_seg_t *node) +{ + jpc_dec_seg_t *prev; + jpc_dec_seg_t *next; + + prev = ins; + node->prev = prev; + next = prev ? (prev->next) : 0; + node->prev = prev; + node->next = next; + if (prev) { + prev->next = node; + } else { + list->head = node; + } + if (next) { + next->prev = node; + } else { + list->tail = node; + } +} + +void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *seg) +{ + jpc_dec_seg_t *prev; + jpc_dec_seg_t *next; + + prev = seg->prev; + next = seg->next; + if (prev) { + prev->next = next; + } else { + list->head = next; + } + if (next) { + next->prev = prev; + } else { + list->tail = prev; + } + seg->prev = 0; + seg->next = 0; +} + +jpc_dec_seg_t *jpc_seg_alloc() +{ + jpc_dec_seg_t *seg; + + if (!(seg = jas_malloc(sizeof(jpc_dec_seg_t)))) { + return 0; + } + seg->prev = 0; + seg->next = 0; + seg->passno = -1; + seg->numpasses = 0; + seg->maxpasses = 0; + seg->type = JPC_SEG_INVALID; + seg->stream = 0; + seg->cnt = 0; + seg->complete = 0; + seg->lyrno = -1; + return seg; +} + +void jpc_seg_destroy(jpc_dec_seg_t *seg) +{ + if (seg->stream) { + jas_stream_close(seg->stream); + } + jas_free(seg); +} + +static int jpc_dec_dump(jpc_dec_t *dec, FILE *out) +{ + jpc_dec_tile_t *tile; + int tileno; + jpc_dec_tcomp_t *tcomp; + int compno; + jpc_dec_rlvl_t *rlvl; + int rlvlno; + jpc_dec_band_t *band; + int bandno; + jpc_dec_prc_t *prc; + int prcno; + jpc_dec_cblk_t *cblk; + int cblkno; + + for (tileno = 0, tile = dec->tiles; tileno < dec->numtiles; + ++tileno, ++tile) { + for (compno = 0, tcomp = tile->tcomps; compno < dec->numcomps; + ++compno, ++tcomp) { + for (rlvlno = 0, rlvl = tcomp->rlvls; rlvlno < + tcomp->numrlvls; ++rlvlno, ++rlvl) { +fprintf(out, "RESOLUTION LEVEL %d\n", rlvlno); +fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", + rlvl->xstart, rlvl->ystart, rlvl->xend, rlvl->yend, rlvl->xend - + rlvl->xstart, rlvl->yend - rlvl->ystart); + for (bandno = 0, band = rlvl->bands; + bandno < rlvl->numbands; ++bandno, ++band) { +fprintf(out, "BAND %d\n", bandno); +fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", + jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data), + jas_seq2d_yend(band->data), jas_seq2d_xend(band->data) - jas_seq2d_xstart(band->data), + jas_seq2d_yend(band->data) - jas_seq2d_ystart(band->data)); + for (prcno = 0, prc = band->prcs; + prcno < rlvl->numprcs; ++prcno, + ++prc) { +fprintf(out, "CODE BLOCK GROUP %d\n", prcno); +fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", + prc->xstart, prc->ystart, prc->xend, prc->yend, prc->xend - + prc->xstart, prc->yend - prc->ystart); + for (cblkno = 0, cblk = + prc->cblks; cblkno < + prc->numcblks; ++cblkno, + ++cblk) { +fprintf(out, "CODE BLOCK %d\n", cblkno); +fprintf(out, "xs =%d, ys = %d, xe = %d, ye = %d, w = %d, h = %d\n", + jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data), + jas_seq2d_yend(cblk->data), jas_seq2d_xend(cblk->data) - jas_seq2d_xstart(cblk->data), + jas_seq2d_yend(cblk->data) - jas_seq2d_ystart(cblk->data)); + } + } + } + } + } + } + + return 0; +} + +jpc_streamlist_t *jpc_streamlist_create() +{ + jpc_streamlist_t *streamlist; + int i; + + if (!(streamlist = jas_malloc(sizeof(jpc_streamlist_t)))) { + return 0; + } + streamlist->numstreams = 0; + streamlist->maxstreams = 100; + if (!(streamlist->streams = jas_malloc(streamlist->maxstreams * + sizeof(jas_stream_t *)))) { + jas_free(streamlist); + return 0; + } + for (i = 0; i < streamlist->maxstreams; ++i) { + streamlist->streams[i] = 0; + } + return streamlist; +} + +int jpc_streamlist_insert(jpc_streamlist_t *streamlist, int streamno, + jas_stream_t *stream) +{ + jas_stream_t **newstreams; + int newmaxstreams; + int i; + /* Grow the array of streams if necessary. */ + if (streamlist->numstreams >= streamlist->maxstreams) { + newmaxstreams = streamlist->maxstreams + 1024; + if (!(newstreams = jas_realloc(streamlist->streams, + (newmaxstreams + 1024) * sizeof(jas_stream_t *)))) { + return -1; + } + for (i = streamlist->numstreams; i < streamlist->maxstreams; ++i) { + streamlist->streams[i] = 0; + } + streamlist->maxstreams = newmaxstreams; + streamlist->streams = newstreams; + } + if (streamno != streamlist->numstreams) { + /* Can only handle insertion at start of list. */ + return -1; + } + streamlist->streams[streamno] = stream; + ++streamlist->numstreams; + return 0; +} + +jas_stream_t *jpc_streamlist_remove(jpc_streamlist_t *streamlist, int streamno) +{ + jas_stream_t *stream; + int i; + if (streamno >= streamlist->numstreams) { + abort(); + } + stream = streamlist->streams[streamno]; + for (i = streamno + 1; i < streamlist->numstreams; ++i) { + streamlist->streams[i - 1] = streamlist->streams[i]; + } + --streamlist->numstreams; + return stream; +} + +void jpc_streamlist_destroy(jpc_streamlist_t *streamlist) +{ + int streamno; + if (streamlist->streams) { + for (streamno = 0; streamno < streamlist->numstreams; + ++streamno) { + jas_stream_close(streamlist->streams[streamno]); + } + jas_free(streamlist->streams); + } + jas_free(streamlist); +} + +jas_stream_t *jpc_streamlist_get(jpc_streamlist_t *streamlist, int streamno) +{ + assert(streamno < streamlist->numstreams); + return streamlist->streams[streamno]; +} + +int jpc_streamlist_numstreams(jpc_streamlist_t *streamlist) +{ + return streamlist->numstreams; +} + +jpc_ppxstab_t *jpc_ppxstab_create() +{ + jpc_ppxstab_t *tab; + + if (!(tab = jas_malloc(sizeof(jpc_ppxstab_t)))) { + return 0; + } + tab->numents = 0; + tab->maxents = 0; + tab->ents = 0; + return tab; +} + +void jpc_ppxstab_destroy(jpc_ppxstab_t *tab) +{ + int i; + for (i = 0; i < tab->numents; ++i) { + jpc_ppxstabent_destroy(tab->ents[i]); + } + if (tab->ents) { + jas_free(tab->ents); + } + jas_free(tab); +} + +int jpc_ppxstab_grow(jpc_ppxstab_t *tab, int maxents) +{ + jpc_ppxstabent_t **newents; + if (tab->maxents < maxents) { + newents = (tab->ents) ? jas_realloc(tab->ents, maxents * + sizeof(jpc_ppxstabent_t *)) : jas_malloc(maxents * sizeof(jpc_ppxstabent_t *)); + if (!newents) { + return -1; + } + tab->ents = newents; + tab->maxents = maxents; + } + return 0; +} + +int jpc_ppxstab_insert(jpc_ppxstab_t *tab, jpc_ppxstabent_t *ent) +{ + int inspt; + int i; + + for (i = 0; i < tab->numents; ++i) { + if (tab->ents[i]->ind > ent->ind) { + break; + } + } + inspt = i; + + if (tab->numents >= tab->maxents) { + if (jpc_ppxstab_grow(tab, tab->maxents + 128)) { + return -1; + } + } + + for (i = tab->numents; i > inspt; --i) { + tab->ents[i] = tab->ents[i - 1]; + } + tab->ents[i] = ent; + ++tab->numents; + + return 0; +} + +jpc_streamlist_t *jpc_ppmstabtostreams(jpc_ppxstab_t *tab) +{ + jpc_streamlist_t *streams; + uchar *dataptr; + uint_fast32_t datacnt; + uint_fast32_t tpcnt; + jpc_ppxstabent_t *ent; + int entno; + jas_stream_t *stream; + int n; + + if (!(streams = jpc_streamlist_create())) { + goto error; + } + + if (!tab->numents) { + return streams; + } + + entno = 0; + ent = tab->ents[entno]; + dataptr = ent->data; + datacnt = ent->len; + for (;;) { + + /* Get the length of the packet header data for the current + tile-part. */ + if (datacnt < 4) { + goto error; + } + if (!(stream = jas_stream_memopen(0, 0))) { + goto error; + } + if (jpc_streamlist_insert(streams, jpc_streamlist_numstreams(streams), + stream)) { + goto error; + } + tpcnt = (dataptr[0] << 24) | (dataptr[1] << 16) | (dataptr[2] << 8) + | dataptr[3]; + datacnt -= 4; + dataptr += 4; + + /* Get the packet header data for the current tile-part. */ + while (tpcnt) { + if (!datacnt) { + if (++entno >= tab->numents) { + goto error; + } + ent = tab->ents[entno]; + dataptr = ent->data; + datacnt = ent->len; + } + n = JAS_MIN(tpcnt, datacnt); + if (jas_stream_write(stream, dataptr, n) != n) { + goto error; + } + tpcnt -= n; + dataptr += n; + datacnt -= n; + } + jas_stream_rewind(stream); + if (!datacnt) { + if (++entno >= tab->numents) { + break; + } + ent = tab->ents[entno]; + dataptr = ent->data; + datacnt = ent->len; + } + } + + return streams; + +error: + jpc_streamlist_destroy(streams); + return 0; +} + +int jpc_pptstabwrite(jas_stream_t *out, jpc_ppxstab_t *tab) +{ + int i; + jpc_ppxstabent_t *ent; + for (i = 0; i < tab->numents; ++i) { + ent = tab->ents[i]; + if (jas_stream_write(out, ent->data, ent->len) != JAS_CAST(int, ent->len)) { + return -1; + } + } + return 0; +} + +jpc_ppxstabent_t *jpc_ppxstabent_create() +{ + jpc_ppxstabent_t *ent; + if (!(ent = jas_malloc(sizeof(jpc_ppxstabent_t)))) { + return 0; + } + ent->data = 0; + ent->len = 0; + ent->ind = 0; + return ent; +} + +void jpc_ppxstabent_destroy(jpc_ppxstabent_t *ent) +{ + if (ent->data) { + jas_free(ent->data); + } + jas_free(ent); +} diff --git a/src/libjasper/jpc/jpc_dec.h b/src/libjasper/jpc/jpc_dec.h new file mode 100644 index 0000000..20e114a --- /dev/null +++ b/src/libjasper/jpc/jpc_dec.h @@ -0,0 +1,696 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * JPEG-2000 Decoder + * + * $Id: jpc_dec.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_DEC_H +#define JPC_DEC_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_stream.h" + +#include "jpc_tsfb.h" +#include "jpc_bs.h" +#include "jpc_tagtree.h" +#include "jpc_cs.h" +#include "jpc_cod.h" +#include "jpc_mqdec.h" +#include "jpc_t2cod.h" + +/******************************************************************************\ +* Below are some ugly warts necessary to support packed packet headers. +\******************************************************************************/ + +/* PPM/PPT marker segment table entry. */ + +typedef struct { + + /* The index for this entry. */ + uint_fast16_t ind; + + /* The data length. */ + uint_fast32_t len; + + /* The data. */ + uchar *data; + +} jpc_ppxstabent_t; + +/* PPM/PPT marker segment table. */ + +typedef struct { + + /* The number of entries. */ + int numents; + + /* The maximum number of entries (i.e., the allocated size of the array + below). */ + int maxents; + + /* The table entries. */ + jpc_ppxstabent_t **ents; + +} jpc_ppxstab_t; + +/* Stream list class. */ + +typedef struct { + + /* The number of streams in this list. */ + int numstreams; + + /* The maximum number of streams that can be accomodated without + growing the streams array. */ + int maxstreams; + + /* The streams. */ + jas_stream_t **streams; + +} jpc_streamlist_t; + +/******************************************************************************\ +* Coding parameters class. +\******************************************************************************/ + +/* Per-component coding parameters. */ + +typedef struct { + + /* How were various coding parameters set? */ + int flags; + + /* Per-component coding style parameters (e.g., explicit precinct sizes) */ + uint_fast8_t csty; + + /* The number of resolution levels. */ + uint_fast8_t numrlvls; + + /* The code block width exponent. */ + uint_fast8_t cblkwidthexpn; + + /* The code block height exponent. */ + uint_fast8_t cblkheightexpn; + + /* The QMFB ID. */ + uint_fast8_t qmfbid; + + /* The quantization style. */ + uint_fast8_t qsty; + + /* The number of quantizer step sizes. */ + uint_fast16_t numstepsizes; + + /* The step sizes. */ + uint_fast16_t stepsizes[3 * JPC_MAXRLVLS + 1]; + + /* The number of guard bits. */ + uint_fast8_t numguardbits; + + /* The ROI shift value. */ + uint_fast8_t roishift; + + /* The code block parameters. */ + uint_fast8_t cblkctx; + + /* The precinct width exponents. */ + uint_fast8_t prcwidthexpns[JPC_MAXRLVLS]; + + /* The precinct height exponents. */ + uint_fast8_t prcheightexpns[JPC_MAXRLVLS]; + +} jpc_dec_ccp_t; + +/* Coding paramters. */ + +typedef struct { + + /* How were these coding parameters set? */ + int flags; + + /* Progression change list. */ + jpc_pchglist_t *pchglist; + + /* Progression order. */ + uint_fast8_t prgord; + + /* The number of layers. */ + uint_fast16_t numlyrs; + + /* The MCT ID. */ + uint_fast8_t mctid; + + /* The coding style parameters (e.g., SOP, EPH). */ + uint_fast8_t csty; + + /* The number of components. */ + int numcomps; + + /* The per-component coding parameters. */ + jpc_dec_ccp_t *ccps; + +} jpc_dec_cp_t; + +/******************************************************************************\ +* Decoder class. +\******************************************************************************/ + +/* Decoder per-segment state information. */ + +typedef struct jpc_dec_seg_s { + + /* The next segment in the list. */ + struct jpc_dec_seg_s *next; + + /* The previous segment in the list. */ + struct jpc_dec_seg_s *prev; + + /* The starting pass number for this segment. */ + int passno; + + /* The number of passes in this segment. */ + int numpasses; + + /* The maximum number of passes in this segment. */ + int maxpasses; + + /* The type of data in this segment (i.e., MQ or raw). */ + int type; + + /* A stream containing the data for this segment. */ + jas_stream_t *stream; + + /* The number of bytes destined for this segment from the packet + currently being decoded. */ + int cnt; + + /* A flag indicating if this segment has been terminated. */ + int complete; + + /* The layer number to which this segment belongs. */ + /* If the segment spans multiple layers, then the largest layer number + spanned by the segment is used. */ + int lyrno; + +} jpc_dec_seg_t; + +/* Decoder segment list. */ + +typedef struct { + + /* The first entry in the list. */ + jpc_dec_seg_t *head; + + /* The last entry in the list. */ + jpc_dec_seg_t *tail; + +} jpc_dec_seglist_t; + +/* Decoder per-code-block state information. */ + +typedef struct { + + /* The number of passes. */ + int numpasses; + + /* A list of segments that still need to be decoded. */ + jpc_dec_seglist_t segs; + + /* The first incomplete/partial segment. */ + jpc_dec_seg_t *curseg; + + /* The number of leading insignificant bit planes for this code block. */ + int numimsbs; + + /* The number of bits used to encode pass data lengths. */ + int numlenbits; + + /* The first pass number containing data for this code block. */ + int firstpassno; + + /* The MQ decoder. */ + jpc_mqdec_t *mqdec; + + /* The raw bit stream decoder. */ + jpc_bitstream_t *nulldec; + + /* The per-sample state information for this code block. */ + jas_matrix_t *flags; + + /* The sample data associated with this code block. */ + jas_matrix_t *data; + +} jpc_dec_cblk_t; + +/* Decoder per-code-block-group state information. */ + +typedef struct { + + /* The x-coordinate of the top-left corner of the precinct. */ + uint_fast32_t xstart; + + /* The y-coordinate of the top-left corner of the precinct. */ + uint_fast32_t ystart; + + /* The x-coordinate of the bottom-right corner of the precinct + (plus one). */ + uint_fast32_t xend; + + /* The y-coordinate of the bottom-right corner of the precinct + (plus one). */ + uint_fast32_t yend; + + /* The number of code blocks spanning this precinct in the horizontal + direction. */ + int numhcblks; + + /* The number of code blocks spanning this precinct in the vertical + direction. */ + int numvcblks; + + /* The total number of code blocks in this precinct. */ + int numcblks; + + /* The per code block information. */ + jpc_dec_cblk_t *cblks; + + /* The inclusion tag tree. */ + jpc_tagtree_t *incltagtree; + + /* The insignificant MSBs tag tree. */ + jpc_tagtree_t *numimsbstagtree; + +} jpc_dec_prc_t; + +/* Decoder per-band state information. */ + +typedef struct { + + /* The per-code-block-group state information. */ + jpc_dec_prc_t *prcs; + + /* The sample data associated with this band. */ + jas_matrix_t *data; + + /* The orientation of this band (i.e., LL, LH, HL, or HH). */ + int orient; + + /* The encoded quantizer step size. */ + int stepsize; + + /* The absolute quantizer step size. */ + jpc_fix_t absstepsize; + + /* The number of bit planes for this band. */ + int numbps; + + /* The analysis gain associated with this band. */ + int analgain; + + /* The ROI shift value for this band. */ + int roishift; + +} jpc_dec_band_t; + +/* Decoder per-resolution-level state information. */ + +typedef struct { + + /* The number of bands associated with this resolution level. */ + int numbands; + + /* The per-band information. */ + jpc_dec_band_t *bands; + + /* The x-coordinate of the top-left corner of the tile-component + at this resolution. */ + uint_fast32_t xstart; + + /* The y-coordinate of the top-left corner of the tile-component + at this resolution. */ + uint_fast32_t ystart; + + /* The x-coordinate of the bottom-right corner of the tile-component + at this resolution (plus one). */ + uint_fast32_t xend; + + /* The y-coordinate of the bottom-right corner of the tile-component + at this resolution (plus one). */ + uint_fast32_t yend; + + /* The exponent value for the nominal precinct width measured + relative to the associated LL band. */ + int prcwidthexpn; + + /* The exponent value for the nominal precinct height measured + relative to the associated LL band. */ + int prcheightexpn; + + /* The number of precincts in the horizontal direction. */ + int numhprcs; + + /* The number of precincts in the vertical direction. */ + int numvprcs; + + /* The total number of precincts. */ + int numprcs; + + /* The exponent value for the nominal code block group width. + This quantity is associated with the next lower resolution level + (assuming that there is one). */ + int cbgwidthexpn; + + /* The exponent value for the nominal code block group height + This quantity is associated with the next lower resolution level + (assuming that there is one). */ + int cbgheightexpn; + + /* The exponent value for the code block width. */ + uint_fast16_t cblkwidthexpn; + + /* The exponent value for the code block height. */ + uint_fast16_t cblkheightexpn; + +} jpc_dec_rlvl_t; + +/* Decoder per-tile-component state information. */ + +typedef struct { + + /* The x-coordinate of the top-left corner of the tile-component + in the coordinate system of the tile-component. */ + uint_fast32_t xstart; + + /* The y-coordinate of the top-left corner of the tile-component + in the coordinate system of the tile-component. */ + uint_fast32_t ystart; + + /* The x-coordinate of the bottom-right corner of the tile-component + in the coordinate system of the tile-component (plus one). */ + uint_fast32_t xend; + + /* The y-coordinate of the bottom-right corner of the tile-component + in the coordinate system of the tile-component (plus one). */ + uint_fast32_t yend; + + /* The component data for the current tile. */ + jas_matrix_t *data; + + /* The number of resolution levels. */ + int numrlvls; + + /* The per resolution level information. */ + jpc_dec_rlvl_t *rlvls; + + /* The TSFB. */ + jpc_tsfb_t *tsfb; + +} jpc_dec_tcomp_t; + +/* + * Tile states. + */ + +#define JPC_TILE_INIT 0 +#define JPC_TILE_ACTIVE 1 +#define JPC_TILE_ACTIVELAST 2 +#define JPC_TILE_DONE 3 + +/* Decoder per-tile state information. */ + +typedef struct { + + /* The processing state for this tile. */ + int state; + + /* The x-coordinate of the top-left corner of the tile on the reference + grid. */ + uint_fast32_t xstart; + + /* The y-coordinate of the top-left corner of the tile on the reference + grid. */ + uint_fast32_t ystart; + + /* The x-coordinate of the bottom-right corner of the tile on the + reference grid (plus one). */ + uint_fast32_t xend; + + /* The y-coordinate of the bottom-right corner of the tile on the + reference grid (plus one). */ + uint_fast32_t yend; + + /* The packed packet header data for this tile. */ + jpc_ppxstab_t *pptstab; + + /* A stream containing the packed packet header data for this tile. */ + jas_stream_t *pkthdrstream; + + /* The current position within the packed packet header stream. */ + long pkthdrstreampos; + + /* The coding parameters for this tile. */ + jpc_dec_cp_t *cp; + + /* The per tile-component information. */ + jpc_dec_tcomp_t *tcomps; + + /* The next expected tile-part number. */ + int partno; + + /* The number of tile-parts. */ + int numparts; + + /* The coding mode. */ + int realmode; + + /* The packet iterator for this tile. */ + jpc_pi_t *pi; + +} jpc_dec_tile_t; + +/* Decoder per-component state information. */ + +typedef struct { + + /* The horizontal sampling period. */ + uint_fast32_t hstep; + + /* The vertical sampling period. */ + uint_fast32_t vstep; + + /* The number of samples in the horizontal direction. */ + uint_fast32_t width; + + /* The number of samples in the vertical direction. */ + uint_fast32_t height; + + /* The precision of the sample data. */ + uint_fast16_t prec; + + /* The signedness of the sample data. */ + bool sgnd; + + /* The sample alignment horizontal offset. */ + uint_fast32_t hsubstep; + + /* The sample alignment vertical offset. */ + uint_fast32_t vsubstep; + +} jpc_dec_cmpt_t; + +/* Decoder state information. */ + +typedef struct { + + /* The decoded image. */ + jas_image_t *image; + + /* The x-coordinate of the top-left corner of the image area on + the reference grid. */ + uint_fast32_t xstart; + + /* The y-coordinate of the top-left corner of the image area on + the reference grid. */ + uint_fast32_t ystart; + + /* The x-coordinate of the bottom-right corner of the image area on + the reference grid (plus one). */ + uint_fast32_t xend; + + /* The y-coordinate of the bottom-right corner of the image area on + the reference grid (plus one). */ + uint_fast32_t yend; + + /* The nominal tile width in units of the image reference grid. */ + uint_fast32_t tilewidth; + + /* The nominal tile height in units of the image reference grid. */ + uint_fast32_t tileheight; + + /* The horizontal offset from the origin of the reference grid to the + left side of the first tile. */ + uint_fast32_t tilexoff; + + /* The vertical offset from the origin of the reference grid to the + top side of the first tile. */ + uint_fast32_t tileyoff; + + /* The number of tiles spanning the image area in the vertical + direction. */ + int numhtiles; + + /* The number of tiles spanning the image area in the horizontal + direction. */ + int numvtiles; + + /* The total number of tiles. */ + int numtiles; + + /* The per-tile information. */ + jpc_dec_tile_t *tiles; + + /* The tile currently being processed. */ + jpc_dec_tile_t *curtile; + + /* The number of components. */ + int numcomps; + + /* The stream containing the input JPEG-2000 code stream data. */ + jas_stream_t *in; + + /* The default coding parameters for all tiles. */ + jpc_dec_cp_t *cp; + + /* The maximum number of layers that may be decoded. */ + int maxlyrs; + + /* The maximum number of packets that may be decoded. */ + int maxpkts; + + /* The number of packets decoded so far in the processing of the entire + code stream. */ + int numpkts; + + /* The next expected PPM marker segment sequence number. */ + int ppmseqno; + + /* The current state for code stream processing. */ + int state; + + /* The per-component information. */ + jpc_dec_cmpt_t *cmpts; + + /* The information from PPM marker segments. */ + jpc_ppxstab_t *ppmstab; + + /* A list of streams containing packet header data from PPM marker + segments. */ + jpc_streamlist_t *pkthdrstreams; + + /* The expected ending offset for a tile-part. */ + long curtileendoff; + + /* This is required by the tier-2 decoder. */ + jpc_cstate_t *cstate; + +} jpc_dec_t; + +/* Decoder options. */ + +typedef struct { + + /* The debug level for the decoder. */ + int debug; + + /* The maximum number of layers to decode. */ + int maxlyrs; + + /* The maximum number of packets to decode. */ + int maxpkts; + +} jpc_dec_importopts_t; + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Create a decoder segment object. */ +jpc_dec_seg_t *jpc_seg_alloc(void); + +/* Destroy a decoder segment object. */ +void jpc_seg_destroy(jpc_dec_seg_t *seg); + +/* Remove a segment from a segment list. */ +void jpc_seglist_remove(jpc_dec_seglist_t *list, jpc_dec_seg_t *node); + +/* Insert a segment into a segment list. */ +void jpc_seglist_insert(jpc_dec_seglist_t *list, jpc_dec_seg_t *ins, + jpc_dec_seg_t *node); + +#endif diff --git a/src/libjasper/jpc/jpc_enc.c b/src/libjasper/jpc/jpc_enc.c new file mode 100644 index 0000000..95afad2 --- /dev/null +++ b/src/libjasper/jpc/jpc_enc.c @@ -0,0 +1,2626 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * $Id: jpc_enc.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include +#include +#include + +#include "jasper/jas_types.h" +#include "jasper/jas_string.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_image.h" +#include "jasper/jas_fix.h" +#include "jasper/jas_tvp.h" +#include "jasper/jas_version.h" +#include "jasper/jas_math.h" +#include "jasper/jas_debug.h" + +#include "jpc_flt.h" +#include "jpc_fix.h" +#include "jpc_tagtree.h" +#include "jpc_enc.h" +#include "jpc_cs.h" +#include "jpc_mct.h" +#include "jpc_tsfb.h" +#include "jpc_qmfb.h" +#include "jpc_t1enc.h" +#include "jpc_t2enc.h" +#include "jpc_cod.h" +#include "jpc_math.h" +#include "jpc_util.h" + +/******************************************************************************\ +* +\******************************************************************************/ + +#define JPC_POW2(n) \ + (1 << (n)) + +#define JPC_FLOORTOMULTPOW2(x, n) \ + (((n) > 0) ? ((x) & (~((1 << n) - 1))) : (x)) +/* Round to the nearest multiple of the specified power of two in the + direction of negative infinity. */ + +#define JPC_CEILTOMULTPOW2(x, n) \ + (((n) > 0) ? JPC_FLOORTOMULTPOW2(((x) + (1 << (n)) - 1), n) : (x)) +/* Round to the nearest multiple of the specified power of two in the + direction of positive infinity. */ + +#define JPC_POW2(n) \ + (1 << (n)) + +jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno); +void jpc_enc_tile_destroy(jpc_enc_tile_t *tile); + +static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp, + jas_image_t *image, jpc_enc_tile_t *tile); +static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt); +static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, + jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos); +static void rlvl_destroy(jpc_enc_rlvl_t *rlvl); +static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp, + jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos); +static void band_destroy(jpc_enc_band_t *bands); +static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, + jpc_enc_band_t *band); +static void prc_destroy(jpc_enc_prc_t *prcs); +static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, + jpc_enc_prc_t *prc); +static void cblk_destroy(jpc_enc_cblk_t *cblks); +int ratestrtosize(char *s, uint_fast32_t rawsize, uint_fast32_t *size); +static void pass_destroy(jpc_enc_pass_t *pass); +void jpc_enc_dump(jpc_enc_t *enc); + +/******************************************************************************\ +* Local prototypes. +\******************************************************************************/ + +int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk); +void calcrdslopes(jpc_enc_cblk_t *cblk); +void dump_layeringinfo(jpc_enc_t *enc); +static int jpc_calcssexp(jpc_fix_t stepsize); +static int jpc_calcssmant(jpc_fix_t stepsize); +void jpc_quantize(jas_matrix_t *data, jpc_fix_t stepsize); +static int jpc_enc_encodemainhdr(jpc_enc_t *enc); +static int jpc_enc_encodemainbody(jpc_enc_t *enc); +int jpc_enc_encodetiledata(jpc_enc_t *enc); +jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image); +void jpc_enc_destroy(jpc_enc_t *enc); +static int jpc_enc_encodemainhdr(jpc_enc_t *enc); +static int jpc_enc_encodemainbody(jpc_enc_t *enc); +int jpc_enc_encodetiledata(jpc_enc_t *enc); +int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens); +int setins(int numvalues, jpc_flt_t *values, jpc_flt_t value); +static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image); +void jpc_enc_cp_destroy(jpc_enc_cp_t *cp); +static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn); + +#define GJPC_QCX_EXPN(x) (((x) & (~0x1f)), (((x) & 0x1f) << 11)) +#define GJPC_QCX_MANT(x) (((x) & (~0x7ff)), ((x) & 0x7ff)) + +static uint_fast32_t jpc_abstorelstepsize(jpc_fix_t absdelta, int scaleexpn) +{ + int p; + uint_fast32_t mant; + uint_fast32_t expn; + int n; + + if (absdelta < 0) { + abort(); + } + + p = jpc_firstone(absdelta) - JPC_FIX_FRACBITS; + n = 11 - jpc_firstone(absdelta); + mant = ((n < 0) ? (absdelta >> (-n)) : (absdelta << n)) & 0x7ff; + expn = scaleexpn - p; + if (scaleexpn < p) { + abort(); + } + return JPC_QCX_EXPN(expn) | JPC_QCX_MANT(mant); +} + +typedef enum { + OPT_DEBUG, + OPT_IMGAREAOFFX, + OPT_IMGAREAOFFY, + OPT_TILEGRDOFFX, + OPT_TILEGRDOFFY, + OPT_TILEWIDTH, + OPT_TILEHEIGHT, + OPT_PRCWIDTH, + OPT_PRCHEIGHT, + OPT_CBLKWIDTH, + OPT_CBLKHEIGHT, + OPT_MODE, + OPT_PRG, + OPT_NOMCT, + OPT_MAXRLVLS, + OPT_SOP, + OPT_EPH, + OPT_LAZY, + OPT_TERMALL, + OPT_SEGSYM, + OPT_VCAUSAL, + OPT_RESET, + OPT_PTERM, + OPT_NUMGBITS, + OPT_RATE, + OPT_ILYRRATES, + OPT_JP2OVERHEAD +} optid_t; + +jas_taginfo_t encopts[] = { + {OPT_DEBUG, "debug"}, + {OPT_IMGAREAOFFX, "imgareatlx"}, + {OPT_IMGAREAOFFY, "imgareatly"}, + {OPT_TILEGRDOFFX, "tilegrdtlx"}, + {OPT_TILEGRDOFFY, "tilegrdtly"}, + {OPT_TILEWIDTH, "tilewidth"}, + {OPT_TILEHEIGHT, "tileheight"}, + {OPT_PRCWIDTH, "prcwidth"}, + {OPT_PRCHEIGHT, "prcheight"}, + {OPT_CBLKWIDTH, "cblkwidth"}, + {OPT_CBLKHEIGHT, "cblkheight"}, + {OPT_MODE, "mode"}, + {OPT_PRG, "prg"}, + {OPT_NOMCT, "nomct"}, + {OPT_MAXRLVLS, "numrlvls"}, + {OPT_SOP, "sop"}, + {OPT_EPH, "eph"}, + {OPT_LAZY, "lazy"}, + {OPT_TERMALL, "termall"}, + {OPT_SEGSYM, "segsym"}, + {OPT_VCAUSAL, "vcausal"}, + {OPT_PTERM, "pterm"}, + {OPT_RESET, "resetprob"}, + {OPT_NUMGBITS, "numgbits"}, + {OPT_RATE, "rate"}, + {OPT_ILYRRATES, "ilyrrates"}, + {OPT_JP2OVERHEAD, "_jp2overhead"}, + {-1, 0} +}; + +typedef enum { + PO_L = 0, + PO_R +} poid_t; + + +jas_taginfo_t prgordtab[] = { + {JPC_COD_LRCPPRG, "lrcp"}, + {JPC_COD_RLCPPRG, "rlcp"}, + {JPC_COD_RPCLPRG, "rpcl"}, + {JPC_COD_PCRLPRG, "pcrl"}, + {JPC_COD_CPRLPRG, "cprl"}, + {-1, 0} +}; + +typedef enum { + MODE_INT, + MODE_REAL +} modeid_t; + +jas_taginfo_t modetab[] = { + {MODE_INT, "int"}, + {MODE_REAL, "real"}, + {-1, 0} +}; + +/******************************************************************************\ +* The main encoder entry point. +\******************************************************************************/ + +int jpc_encode(jas_image_t *image, jas_stream_t *out, char *optstr) +{ + jpc_enc_t *enc; + jpc_enc_cp_t *cp; + + enc = 0; + cp = 0; + + jpc_initluts(); + + if (!(cp = cp_create(optstr, image))) { + jas_eprintf("invalid JP encoder options\n"); + goto error; + } + + if (!(enc = jpc_enc_create(cp, out, image))) { + goto error; + } + cp = 0; + + /* Encode the main header. */ + if (jpc_enc_encodemainhdr(enc)) { + goto error; + } + + /* Encode the main body. This constitutes most of the encoding work. */ + if (jpc_enc_encodemainbody(enc)) { + goto error; + } + + /* Write EOC marker segment. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_EOC))) { + goto error; + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + jas_eprintf("cannot write EOI marker\n"); + goto error; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + if (jas_stream_flush(enc->out)) { + goto error; + } + + jpc_enc_destroy(enc); + + return 0; + +error: + if (cp) { + jpc_enc_cp_destroy(cp); + } + if (enc) { + jpc_enc_destroy(enc); + } + return -1; +} + +/******************************************************************************\ +* Option parsing code. +\******************************************************************************/ + +static jpc_enc_cp_t *cp_create(char *optstr, jas_image_t *image) +{ + jpc_enc_cp_t *cp; + jas_tvparser_t *tvp; + int ret; + int numilyrrates; + double *ilyrrates; + int i; + int tagid; + jpc_enc_tcp_t *tcp; + jpc_enc_tccp_t *tccp; + jpc_enc_ccp_t *ccp; + int cmptno; + uint_fast16_t rlvlno; + uint_fast16_t prcwidthexpn; + uint_fast16_t prcheightexpn; + bool enablemct; + uint_fast32_t jp2overhead; + uint_fast16_t lyrno; + uint_fast32_t hsteplcm; + uint_fast32_t vsteplcm; + bool mctvalid; + + tvp = 0; + cp = 0; + ilyrrates = 0; + numilyrrates = 0; + + if (!(cp = jas_malloc(sizeof(jpc_enc_cp_t)))) { + goto error; + } + + prcwidthexpn = 15; + prcheightexpn = 15; + enablemct = true; + jp2overhead = 0; + + cp->ccps = 0; + cp->debug = 0; + cp->imgareatlx = UINT_FAST32_MAX; + cp->imgareatly = UINT_FAST32_MAX; + cp->refgrdwidth = 0; + cp->refgrdheight = 0; + cp->tilegrdoffx = UINT_FAST32_MAX; + cp->tilegrdoffy = UINT_FAST32_MAX; + cp->tilewidth = 0; + cp->tileheight = 0; + cp->numcmpts = jas_image_numcmpts(image); + + hsteplcm = 1; + vsteplcm = 1; + for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) { + if (jas_image_cmptbrx(image, cmptno) + jas_image_cmpthstep(image, cmptno) <= + jas_image_brx(image) || jas_image_cmptbry(image, cmptno) + + jas_image_cmptvstep(image, cmptno) <= jas_image_bry(image)) { + jas_eprintf("unsupported image type\n"); + goto error; + } + /* Note: We ought to be calculating the LCMs here. Fix some day. */ + hsteplcm *= jas_image_cmpthstep(image, cmptno); + vsteplcm *= jas_image_cmptvstep(image, cmptno); + } + + if (!(cp->ccps = jas_malloc(cp->numcmpts * sizeof(jpc_enc_ccp_t)))) { + goto error; + } + for (cmptno = 0, ccp = cp->ccps; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno, + ++ccp) { + ccp->sampgrdstepx = jas_image_cmpthstep(image, cmptno); + ccp->sampgrdstepy = jas_image_cmptvstep(image, cmptno); + /* XXX - this isn't quite correct for more general image */ + ccp->sampgrdsubstepx = 0; + ccp->sampgrdsubstepx = 0; + ccp->prec = jas_image_cmptprec(image, cmptno); + ccp->sgnd = jas_image_cmptsgnd(image, cmptno); + ccp->numstepsizes = 0; + memset(ccp->stepsizes, 0, sizeof(ccp->stepsizes)); + } + + cp->rawsize = jas_image_rawsize(image); + cp->totalsize = UINT_FAST32_MAX; + + tcp = &cp->tcp; + tcp->csty = 0; + tcp->intmode = true; + tcp->prg = JPC_COD_LRCPPRG; + tcp->numlyrs = 1; + tcp->ilyrrates = 0; + + tccp = &cp->tccp; + tccp->csty = 0; + tccp->maxrlvls = 6; + tccp->cblkwidthexpn = 6; + tccp->cblkheightexpn = 6; + tccp->cblksty = 0; + tccp->numgbits = 2; + + if (!(tvp = jas_tvparser_create(optstr ? optstr : ""))) { + goto error; + } + + while (!(ret = jas_tvparser_next(tvp))) { + switch (jas_taginfo_nonull(jas_taginfos_lookup(encopts, + jas_tvparser_gettag(tvp)))->id) { + case OPT_DEBUG: + cp->debug = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_IMGAREAOFFX: + cp->imgareatlx = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_IMGAREAOFFY: + cp->imgareatly = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_TILEGRDOFFX: + cp->tilegrdoffx = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_TILEGRDOFFY: + cp->tilegrdoffy = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_TILEWIDTH: + cp->tilewidth = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_TILEHEIGHT: + cp->tileheight = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_PRCWIDTH: + prcwidthexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); + break; + case OPT_PRCHEIGHT: + prcheightexpn = jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); + break; + case OPT_CBLKWIDTH: + tccp->cblkwidthexpn = + jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); + break; + case OPT_CBLKHEIGHT: + tccp->cblkheightexpn = + jpc_floorlog2(atoi(jas_tvparser_getval(tvp))); + break; + case OPT_MODE: + if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(modetab, + jas_tvparser_getval(tvp)))->id) < 0) { + jas_eprintf("ignoring invalid mode %s\n", + jas_tvparser_getval(tvp)); + } else { + tcp->intmode = (tagid == MODE_INT); + } + break; + case OPT_PRG: + if ((tagid = jas_taginfo_nonull(jas_taginfos_lookup(prgordtab, + jas_tvparser_getval(tvp)))->id) < 0) { + jas_eprintf("ignoring invalid progression order %s\n", + jas_tvparser_getval(tvp)); + } else { + tcp->prg = tagid; + } + break; + case OPT_NOMCT: + enablemct = false; + break; + case OPT_MAXRLVLS: + tccp->maxrlvls = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_SOP: + cp->tcp.csty |= JPC_COD_SOP; + break; + case OPT_EPH: + cp->tcp.csty |= JPC_COD_EPH; + break; + case OPT_LAZY: + tccp->cblksty |= JPC_COX_LAZY; + break; + case OPT_TERMALL: + tccp->cblksty |= JPC_COX_TERMALL; + break; + case OPT_SEGSYM: + tccp->cblksty |= JPC_COX_SEGSYM; + break; + case OPT_VCAUSAL: + tccp->cblksty |= JPC_COX_VSC; + break; + case OPT_RESET: + tccp->cblksty |= JPC_COX_RESET; + break; + case OPT_PTERM: + tccp->cblksty |= JPC_COX_PTERM; + break; + case OPT_NUMGBITS: + cp->tccp.numgbits = atoi(jas_tvparser_getval(tvp)); + break; + case OPT_RATE: + if (ratestrtosize(jas_tvparser_getval(tvp), cp->rawsize, + &cp->totalsize)) { + jas_eprintf("ignoring bad rate specifier %s\n", + jas_tvparser_getval(tvp)); + } + break; + case OPT_ILYRRATES: + if (jpc_atoaf(jas_tvparser_getval(tvp), &numilyrrates, + &ilyrrates)) { + jas_eprintf("warning: invalid intermediate layer rates specifier ignored (%s)\n", + jas_tvparser_getval(tvp)); + } + break; + + case OPT_JP2OVERHEAD: + jp2overhead = atoi(jas_tvparser_getval(tvp)); + break; + default: + jas_eprintf("warning: ignoring invalid option %s\n", + jas_tvparser_gettag(tvp)); + break; + } + } + + jas_tvparser_destroy(tvp); + tvp = 0; + + if (cp->totalsize != UINT_FAST32_MAX) { + cp->totalsize = (cp->totalsize > jp2overhead) ? + (cp->totalsize - jp2overhead) : 0; + } + + if (cp->imgareatlx == UINT_FAST32_MAX) { + cp->imgareatlx = 0; + } else { + if (hsteplcm != 1) { + jas_eprintf("warning: overriding imgareatlx value\n"); + } + cp->imgareatlx *= hsteplcm; + } + if (cp->imgareatly == UINT_FAST32_MAX) { + cp->imgareatly = 0; + } else { + if (vsteplcm != 1) { + jas_eprintf("warning: overriding imgareatly value\n"); + } + cp->imgareatly *= vsteplcm; + } + cp->refgrdwidth = cp->imgareatlx + jas_image_width(image); + cp->refgrdheight = cp->imgareatly + jas_image_height(image); + if (cp->tilegrdoffx == UINT_FAST32_MAX) { + cp->tilegrdoffx = cp->imgareatlx; + } + if (cp->tilegrdoffy == UINT_FAST32_MAX) { + cp->tilegrdoffy = cp->imgareatly; + } + if (!cp->tilewidth) { + cp->tilewidth = cp->refgrdwidth - cp->tilegrdoffx; + } + if (!cp->tileheight) { + cp->tileheight = cp->refgrdheight - cp->tilegrdoffy; + } + + if (cp->numcmpts == 3) { + mctvalid = true; + for (cmptno = 0; cmptno < jas_image_numcmpts(image); ++cmptno) { + if (jas_image_cmptprec(image, cmptno) != jas_image_cmptprec(image, 0) || + jas_image_cmptsgnd(image, cmptno) != jas_image_cmptsgnd(image, 0) || + jas_image_cmptwidth(image, cmptno) != jas_image_cmptwidth(image, 0) || + jas_image_cmptheight(image, cmptno) != jas_image_cmptheight(image, 0)) { + mctvalid = false; + } + } + } else { + mctvalid = false; + } + if (mctvalid && enablemct && jas_clrspc_fam(jas_image_clrspc(image)) != JAS_CLRSPC_FAM_RGB) { + jas_eprintf("warning: color space apparently not RGB\n"); + } + if (mctvalid && enablemct && jas_clrspc_fam(jas_image_clrspc(image)) == JAS_CLRSPC_FAM_RGB) { + tcp->mctid = (tcp->intmode) ? (JPC_MCT_RCT) : (JPC_MCT_ICT); + } else { + tcp->mctid = JPC_MCT_NONE; + } + tccp->qmfbid = (tcp->intmode) ? (JPC_COX_RFT) : (JPC_COX_INS); + + for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) { + tccp->prcwidthexpns[rlvlno] = prcwidthexpn; + tccp->prcheightexpns[rlvlno] = prcheightexpn; + } + if (prcwidthexpn != 15 || prcheightexpn != 15) { + tccp->csty |= JPC_COX_PRT; + } + + /* Ensure that the tile width and height is valid. */ + if (!cp->tilewidth) { + jas_eprintf("invalid tile width %lu\n", (unsigned long) + cp->tilewidth); + goto error; + } + if (!cp->tileheight) { + jas_eprintf("invalid tile height %lu\n", (unsigned long) + cp->tileheight); + goto error; + } + + /* Ensure that the tile grid offset is valid. */ + if (cp->tilegrdoffx > cp->imgareatlx || + cp->tilegrdoffy > cp->imgareatly || + cp->tilegrdoffx + cp->tilewidth < cp->imgareatlx || + cp->tilegrdoffy + cp->tileheight < cp->imgareatly) { + jas_eprintf("invalid tile grid offset (%lu, %lu)\n", + (unsigned long) cp->tilegrdoffx, (unsigned long) + cp->tilegrdoffy); + goto error; + } + + cp->numhtiles = JPC_CEILDIV(cp->refgrdwidth - cp->tilegrdoffx, + cp->tilewidth); + cp->numvtiles = JPC_CEILDIV(cp->refgrdheight - cp->tilegrdoffy, + cp->tileheight); + cp->numtiles = cp->numhtiles * cp->numvtiles; + + if (ilyrrates && numilyrrates > 0) { + tcp->numlyrs = numilyrrates + 1; + if (!(tcp->ilyrrates = jas_malloc((tcp->numlyrs - 1) * + sizeof(jpc_fix_t)))) { + goto error; + } + for (i = 0; i < JAS_CAST(int, tcp->numlyrs - 1); ++i) { + tcp->ilyrrates[i] = jpc_dbltofix(ilyrrates[i]); + } + } + + /* Ensure that the integer mode is used in the case of lossless + coding. */ + if (cp->totalsize == UINT_FAST32_MAX && (!cp->tcp.intmode)) { + jas_eprintf("cannot use real mode for lossless coding\n"); + goto error; + } + + /* Ensure that the precinct width is valid. */ + if (prcwidthexpn > 15) { + jas_eprintf("invalid precinct width\n"); + goto error; + } + + /* Ensure that the precinct height is valid. */ + if (prcheightexpn > 15) { + jas_eprintf("invalid precinct height\n"); + goto error; + } + + /* Ensure that the code block width is valid. */ + if (cp->tccp.cblkwidthexpn < 2 || cp->tccp.cblkwidthexpn > 12) { + jas_eprintf("invalid code block width %d\n", + JPC_POW2(cp->tccp.cblkwidthexpn)); + goto error; + } + + /* Ensure that the code block height is valid. */ + if (cp->tccp.cblkheightexpn < 2 || cp->tccp.cblkheightexpn > 12) { + jas_eprintf("invalid code block height %d\n", + JPC_POW2(cp->tccp.cblkheightexpn)); + goto error; + } + + /* Ensure that the code block size is not too large. */ + if (cp->tccp.cblkwidthexpn + cp->tccp.cblkheightexpn > 12) { + jas_eprintf("code block size too large\n"); + goto error; + } + + /* Ensure that the number of layers is valid. */ + if (cp->tcp.numlyrs > 16384) { + jas_eprintf("too many layers\n"); + goto error; + } + + /* There must be at least one resolution level. */ + if (cp->tccp.maxrlvls < 1) { + jas_eprintf("must be at least one resolution level\n"); + goto error; + } + + /* Ensure that the number of guard bits is valid. */ + if (cp->tccp.numgbits > 8) { + jas_eprintf("invalid number of guard bits\n"); + goto error; + } + + /* Ensure that the rate is within the legal range. */ + if (cp->totalsize != UINT_FAST32_MAX && cp->totalsize > cp->rawsize) { + jas_eprintf("warning: specified rate is unreasonably large (%lu > %lu)\n", (unsigned long) cp->totalsize, (unsigned long) cp->rawsize); + } + + /* Ensure that the intermediate layer rates are valid. */ + if (tcp->numlyrs > 1) { + /* The intermediate layers rates must increase monotonically. */ + for (lyrno = 0; lyrno + 2 < tcp->numlyrs; ++lyrno) { + if (tcp->ilyrrates[lyrno] >= tcp->ilyrrates[lyrno + 1]) { + jas_eprintf("intermediate layer rates must increase monotonically\n"); + goto error; + } + } + /* The intermediate layer rates must be less than the overall rate. */ + if (cp->totalsize != UINT_FAST32_MAX) { + for (lyrno = 0; lyrno < tcp->numlyrs - 1; ++lyrno) { + if (jpc_fixtodbl(tcp->ilyrrates[lyrno]) > ((double) cp->totalsize) + / cp->rawsize) { + jas_eprintf("warning: intermediate layer rates must be less than overall rate\n"); + goto error; + } + } + } + } + + if (ilyrrates) { + jas_free(ilyrrates); + } + + return cp; + +error: + + if (ilyrrates) { + jas_free(ilyrrates); + } + if (tvp) { + jas_tvparser_destroy(tvp); + } + if (cp) { + jpc_enc_cp_destroy(cp); + } + return 0; +} + +void jpc_enc_cp_destroy(jpc_enc_cp_t *cp) +{ + if (cp->ccps) { + if (cp->tcp.ilyrrates) { + jas_free(cp->tcp.ilyrrates); + } + jas_free(cp->ccps); + } + jas_free(cp); +} + +int ratestrtosize(char *s, uint_fast32_t rawsize, uint_fast32_t *size) +{ + char *cp; + jpc_flt_t f; + + /* Note: This function must not modify output size on failure. */ + if ((cp = strchr(s, 'B'))) { + *size = atoi(s); + } else { + f = atof(s); + if (f < 0) { + *size = 0; + } else if (f > 1.0) { + *size = rawsize + 1; + } else { + *size = f * rawsize; + } + } + return 0; +} + +/******************************************************************************\ +* Encoder constructor and destructor. +\******************************************************************************/ + +jpc_enc_t *jpc_enc_create(jpc_enc_cp_t *cp, jas_stream_t *out, jas_image_t *image) +{ + jpc_enc_t *enc; + + enc = 0; + + if (!(enc = jas_malloc(sizeof(jpc_enc_t)))) { + goto error; + } + + enc->image = image; + enc->out = out; + enc->cp = cp; + enc->cstate = 0; + enc->tmpstream = 0; + enc->mrk = 0; + enc->curtile = 0; + + if (!(enc->cstate = jpc_cstate_create())) { + goto error; + } + enc->len = 0; + enc->mainbodysize = 0; + + return enc; + +error: + + if (enc) { + jpc_enc_destroy(enc); + } + return 0; +} + +void jpc_enc_destroy(jpc_enc_t *enc) +{ + /* The image object (i.e., enc->image) and output stream object + (i.e., enc->out) are created outside of the encoder. + Therefore, they must not be destroyed here. */ + + if (enc->curtile) { + jpc_enc_tile_destroy(enc->curtile); + } + if (enc->cp) { + jpc_enc_cp_destroy(enc->cp); + } + if (enc->cstate) { + jpc_cstate_destroy(enc->cstate); + } + if (enc->tmpstream) { + jas_stream_close(enc->tmpstream); + } + + jas_free(enc); +} + +/******************************************************************************\ +* Code. +\******************************************************************************/ + +static int jpc_calcssmant(jpc_fix_t stepsize) +{ + int n; + int e; + int m; + + n = jpc_firstone(stepsize); + e = n - JPC_FIX_FRACBITS; + if (n >= 11) { + m = (stepsize >> (n - 11)) & 0x7ff; + } else { + m = (stepsize & ((1 << n) - 1)) << (11 - n); + } + return m; +} + +static int jpc_calcssexp(jpc_fix_t stepsize) +{ + return jpc_firstone(stepsize) - JPC_FIX_FRACBITS; +} + +static int jpc_enc_encodemainhdr(jpc_enc_t *enc) +{ + jpc_siz_t *siz; + jpc_cod_t *cod; + jpc_qcd_t *qcd; + int i; +long startoff; +long mainhdrlen; + jpc_enc_cp_t *cp; + jpc_qcc_t *qcc; + jpc_enc_tccp_t *tccp; + uint_fast16_t cmptno; + jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; + jpc_fix_t mctsynweight; + jpc_enc_tcp_t *tcp; + jpc_tsfb_t *tsfb; + jpc_tsfb_band_t *bandinfo; + uint_fast16_t numbands; + uint_fast16_t bandno; + uint_fast16_t rlvlno; + uint_fast16_t analgain; + jpc_fix_t absstepsize; + char buf[1024]; + jpc_com_t *com; + + cp = enc->cp; + +startoff = jas_stream_getrwcount(enc->out); + + /* Write SOC marker segment. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SOC))) { + return -1; + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + jas_eprintf("cannot write SOC marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + /* Write SIZ marker segment. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SIZ))) { + return -1; + } + siz = &enc->mrk->parms.siz; + siz->caps = 0; + siz->xoff = cp->imgareatlx; + siz->yoff = cp->imgareatly; + siz->width = cp->refgrdwidth; + siz->height = cp->refgrdheight; + siz->tilexoff = cp->tilegrdoffx; + siz->tileyoff = cp->tilegrdoffy; + siz->tilewidth = cp->tilewidth; + siz->tileheight = cp->tileheight; + siz->numcomps = cp->numcmpts; + siz->comps = jas_malloc(siz->numcomps * sizeof(jpc_sizcomp_t)); + assert(siz->comps); + for (i = 0; i < JAS_CAST(int, cp->numcmpts); ++i) { + siz->comps[i].prec = cp->ccps[i].prec; + siz->comps[i].sgnd = cp->ccps[i].sgnd; + siz->comps[i].hsamp = cp->ccps[i].sampgrdstepx; + siz->comps[i].vsamp = cp->ccps[i].sampgrdstepy; + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + jas_eprintf("cannot write SIZ marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + if (!(enc->mrk = jpc_ms_create(JPC_MS_COM))) { + return -1; + } + + // GeoJasper: dima - add GeoJasper info + //sprintf(buf, "Creator: JasPer Version %s", jas_getversion()); + sprintf(buf, "Creator: GeoJasPer %s JasPer %s", GJAS_VERSION, jas_getversion()); + com = &enc->mrk->parms.com; + com->len = strlen(buf); + com->regid = JPC_COM_LATIN; + if (!(com->data = JAS_CAST(uchar *, jas_strdup(buf)))) { + abort(); + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + jas_eprintf("cannot write COM marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + +#if 0 + if (!(enc->mrk = jpc_ms_create(JPC_MS_CRG))) { + return -1; + } + crg = &enc->mrk->parms.crg; + crg->comps = jas_malloc(crg->numcomps * sizeof(jpc_crgcomp_t)); + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + jas_eprintf("cannot write CRG marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; +#endif + + tcp = &cp->tcp; + tccp = &cp->tccp; + for (cmptno = 0; cmptno < cp->numcmpts; ++cmptno) { + tsfb = jpc_cod_gettsfb(tccp->qmfbid, tccp->maxrlvls - 1); + jpc_tsfb_getbands(tsfb, 0, 0, 1 << tccp->maxrlvls, 1 << tccp->maxrlvls, + bandinfos); + jpc_tsfb_destroy(tsfb); + mctsynweight = jpc_mct_getsynweight(tcp->mctid, cmptno); + numbands = 3 * tccp->maxrlvls - 2; + for (bandno = 0, bandinfo = bandinfos; bandno < numbands; + ++bandno, ++bandinfo) { + rlvlno = (bandno) ? ((bandno - 1) / 3 + 1) : 0; + analgain = JPC_NOMINALGAIN(tccp->qmfbid, tccp->maxrlvls, + rlvlno, bandinfo->orient); + if (!tcp->intmode) { + absstepsize = jpc_fix_div(jpc_inttofix(1 << + (analgain + 1)), bandinfo->synenergywt); + } else { + absstepsize = jpc_inttofix(1); + } + cp->ccps[cmptno].stepsizes[bandno] = + jpc_abstorelstepsize(absstepsize, + cp->ccps[cmptno].prec + analgain); + } + cp->ccps[cmptno].numstepsizes = numbands; + } + + if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { + return -1; + } + cod = &enc->mrk->parms.cod; + cod->csty = cp->tccp.csty | cp->tcp.csty; + cod->compparms.csty = cp->tccp.csty | cp->tcp.csty; + cod->compparms.numdlvls = cp->tccp.maxrlvls - 1; + cod->compparms.numrlvls = cp->tccp.maxrlvls; + cod->prg = cp->tcp.prg; + cod->numlyrs = cp->tcp.numlyrs; + cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkwidthexpn); + cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(cp->tccp.cblkheightexpn); + cod->compparms.cblksty = cp->tccp.cblksty; + cod->compparms.qmfbid = cp->tccp.qmfbid; + cod->mctrans = (cp->tcp.mctid != JPC_MCT_NONE); + if (tccp->csty & JPC_COX_PRT) { + for (rlvlno = 0; rlvlno < tccp->maxrlvls; ++rlvlno) { + cod->compparms.rlvls[rlvlno].parwidthval = tccp->prcwidthexpns[rlvlno]; + cod->compparms.rlvls[rlvlno].parheightval = tccp->prcheightexpns[rlvlno]; + } + } + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + jas_eprintf("cannot write COD marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + if (!(enc->mrk = jpc_ms_create(JPC_MS_QCD))) { + return -1; + } + qcd = &enc->mrk->parms.qcd; + qcd->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ? + JPC_QCX_SEQNT : JPC_QCX_NOQNT; + qcd->compparms.numstepsizes = cp->ccps[0].numstepsizes; + qcd->compparms.numguard = cp->tccp.numgbits; + qcd->compparms.stepsizes = cp->ccps[0].stepsizes; + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + return -1; + } + /* We do not want the step size array to be freed! */ + qcd->compparms.stepsizes = 0; + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + + tccp = &cp->tccp; + for (cmptno = 1; cmptno < cp->numcmpts; ++cmptno) { + if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { + return -1; + } + qcc = &enc->mrk->parms.qcc; + qcc->compno = cmptno; + qcc->compparms.qntsty = (tccp->qmfbid == JPC_COX_INS) ? + JPC_QCX_SEQNT : JPC_QCX_NOQNT; + qcc->compparms.numstepsizes = cp->ccps[cmptno].numstepsizes; + qcc->compparms.numguard = cp->tccp.numgbits; + qcc->compparms.stepsizes = cp->ccps[cmptno].stepsizes; + if (jpc_putms(enc->out, enc->cstate, enc->mrk)) { + return -1; + } + /* We do not want the step size array to be freed! */ + qcc->compparms.stepsizes = 0; + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + } + +#define MAINTLRLEN 2 + mainhdrlen = jas_stream_getrwcount(enc->out) - startoff; + enc->len += mainhdrlen; + if (enc->cp->totalsize != UINT_FAST32_MAX) { + uint_fast32_t overhead; + overhead = mainhdrlen + MAINTLRLEN; + enc->mainbodysize = (enc->cp->totalsize >= overhead) ? + (enc->cp->totalsize - overhead) : 0; + } else { + enc->mainbodysize = UINT_FAST32_MAX; + } + + return 0; +} + +static int jpc_enc_encodemainbody(jpc_enc_t *enc) +{ + int tileno; + int tilex; + int tiley; + int i; + jpc_sot_t *sot; + jpc_enc_tcmpt_t *comp; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_rlvl_t *lvl; + int rlvlno; + jpc_qcc_t *qcc; + jpc_cod_t *cod; + int adjust; + int j; + int absbandno; + long numbytes; + long tilehdrlen; + long tilelen; + jpc_enc_tile_t *tile; + jpc_enc_cp_t *cp; + double rho; + int lyrno; + int cmptno; + int samestepsizes; + jpc_enc_ccp_t *ccps; + jpc_enc_tccp_t *tccp; +int bandno; +uint_fast32_t x; +uint_fast32_t y; +int mingbits; +int actualnumbps; +jpc_fix_t mxmag; +jpc_fix_t mag; +int numgbits; + + cp = enc->cp; + + /* Avoid compile warnings. */ + numbytes = 0; + + for (tileno = 0; tileno < JAS_CAST(int, cp->numtiles); ++tileno) { + tilex = tileno % cp->numhtiles; + tiley = tileno / cp->numhtiles; + + if (!(enc->curtile = jpc_enc_tile_create(enc->cp, enc->image, tileno))) { + abort(); + } + + tile = enc->curtile; + + if (jas_getdbglevel() >= 10) { + jpc_enc_dump(enc); + } + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (cmptno = 0, comp = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, ++comp) { + if (!cp->ccps[cmptno].sgnd) { + adjust = 1 << (cp->ccps[cmptno].prec - 1); + for (i = 0; i < jas_matrix_numrows(comp->data); ++i) { + for (j = 0; j < jas_matrix_numcols(comp->data); ++j) { + *jas_matrix_getref(comp->data, i, j) -= adjust; + } + } + } + } + + if (!tile->intmode) { + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + jas_matrix_asl(comp->data, JPC_FIX_FRACBITS); + } + } + + switch (tile->mctid) { + case JPC_MCT_RCT: +assert(jas_image_numcmpts(enc->image) == 3); + jpc_rct(tile->tcmpts[0].data, tile->tcmpts[1].data, + tile->tcmpts[2].data); + break; + case JPC_MCT_ICT: +assert(jas_image_numcmpts(enc->image) == 3); + jpc_ict(tile->tcmpts[0].data, tile->tcmpts[1].data, + tile->tcmpts[2].data); + break; + default: + break; + } + + for (i = 0; i < jas_image_numcmpts(enc->image); ++i) { + comp = &tile->tcmpts[i]; + jpc_tsfb_analyze(comp->tsfb, comp->data); + + } + + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (cmptno = 0, comp = tile->tcmpts; comp != endcomps; ++cmptno, ++comp) { + mingbits = 0; + absbandno = 0; + /* All bands must have a corresponding quantizer step size, + even if they contain no samples and are never coded. */ + /* Some bands may not be hit by the loop below, so we must + initialize all of the step sizes to a sane value. */ + memset(comp->stepsizes, 0, sizeof(comp->stepsizes)); + for (rlvlno = 0, lvl = comp->rlvls; rlvlno < comp->numrlvls; ++rlvlno, ++lvl) { + if (!lvl->bands) { + absbandno += rlvlno ? 3 : 1; + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + ++absbandno; + continue; + } + actualnumbps = 0; + mxmag = 0; + for (y = 0; y < JAS_CAST(uint_fast32_t, jas_matrix_numrows(band->data)); ++y) { + for (x = 0; x < JAS_CAST(uint_fast32_t, jas_matrix_numcols(band->data)); ++x) { + mag = abs(jas_matrix_get(band->data, y, x)); + if (mag > mxmag) { + mxmag = mag; + } + } + } + if (tile->intmode) { + actualnumbps = jpc_firstone(mxmag) + 1; + } else { + actualnumbps = jpc_firstone(mxmag) + 1 - JPC_FIX_FRACBITS; + } + numgbits = actualnumbps - (cp->ccps[cmptno].prec - 1 + + band->analgain); +#if 0 +jas_eprintf("%d %d mag=%d actual=%d numgbits=%d\n", cp->ccps[cmptno].prec, band->analgain, mxmag, actualnumbps, numgbits); +#endif + if (numgbits > mingbits) { + mingbits = numgbits; + } + if (!tile->intmode) { + band->absstepsize = jpc_fix_div(jpc_inttofix(1 + << (band->analgain + 1)), + band->synweight); + } else { + band->absstepsize = jpc_inttofix(1); + } + band->stepsize = jpc_abstorelstepsize( + band->absstepsize, cp->ccps[cmptno].prec + + band->analgain); + band->numbps = cp->tccp.numgbits + + JPC_QCX_GETEXPN(band->stepsize) - 1; + + if ((!tile->intmode) && band->data) { + jpc_quantize(band->data, band->absstepsize); + } + + comp->stepsizes[absbandno] = band->stepsize; + ++absbandno; + } + } + + assert(JPC_FIX_FRACBITS >= JPC_NUMEXTRABITS); + if (!tile->intmode) { + jas_matrix_divpow2(comp->data, JPC_FIX_FRACBITS - JPC_NUMEXTRABITS); + } else { + jas_matrix_asl(comp->data, JPC_NUMEXTRABITS); + } + +#if 0 +jas_eprintf("mingbits %d\n", mingbits); +#endif + if (mingbits > cp->tccp.numgbits) { + jas_eprintf("error: too few guard bits (need at least %d)\n", + mingbits); + return -1; + } + } + + if (!(enc->tmpstream = jas_stream_memopen(0, 0))) { + jas_eprintf("cannot open tmp file\n"); + return -1; + } + + /* Write the tile header. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SOT))) { + return -1; + } + sot = &enc->mrk->parms.sot; + sot->len = 0; + sot->tileno = tileno; + sot->partno = 0; + sot->numparts = 1; + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + jas_eprintf("cannot write SOT marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + +/************************************************************************/ +/************************************************************************/ +/************************************************************************/ + + tccp = &cp->tccp; + for (cmptno = 0; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno) { + comp = &tile->tcmpts[cmptno]; + if (comp->numrlvls != tccp->maxrlvls) { + if (!(enc->mrk = jpc_ms_create(JPC_MS_COD))) { + return -1; + } +/* XXX = this is not really correct. we are using comp #0's precint sizes +and other characteristics */ + comp = &tile->tcmpts[0]; + cod = &enc->mrk->parms.cod; + cod->compparms.csty = 0; + cod->compparms.numdlvls = comp->numrlvls - 1; + cod->prg = tile->prg; + cod->numlyrs = tile->numlyrs; + cod->compparms.cblkwidthval = JPC_COX_CBLKSIZEEXPN(comp->cblkwidthexpn); + cod->compparms.cblkheightval = JPC_COX_CBLKSIZEEXPN(comp->cblkheightexpn); + cod->compparms.cblksty = comp->cblksty; + cod->compparms.qmfbid = comp->qmfbid; + cod->mctrans = (tile->mctid != JPC_MCT_NONE); + for (i = 0; i < comp->numrlvls; ++i) { + cod->compparms.rlvls[i].parwidthval = comp->rlvls[i].prcwidthexpn; + cod->compparms.rlvls[i].parheightval = comp->rlvls[i].prcheightexpn; + } + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + } + } + + for (cmptno = 0, comp = tile->tcmpts; cmptno < JAS_CAST(int, cp->numcmpts); ++cmptno, ++comp) { + ccps = &cp->ccps[cmptno]; + if (JAS_CAST(int, ccps->numstepsizes) == comp->numstepsizes) { + samestepsizes = 1; + for (bandno = 0; bandno < JAS_CAST(int, ccps->numstepsizes); ++bandno) { + if (ccps->stepsizes[bandno] != comp->stepsizes[bandno]) { + samestepsizes = 0; + break; + } + } + } else { + samestepsizes = 0; + } + if (!samestepsizes) { + if (!(enc->mrk = jpc_ms_create(JPC_MS_QCC))) { + return -1; + } + qcc = &enc->mrk->parms.qcc; + qcc->compno = cmptno; + qcc->compparms.numguard = cp->tccp.numgbits; + qcc->compparms.qntsty = (comp->qmfbid == JPC_COX_INS) ? + JPC_QCX_SEQNT : JPC_QCX_NOQNT; + qcc->compparms.numstepsizes = comp->numstepsizes; + qcc->compparms.stepsizes = comp->stepsizes; + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + return -1; + } + qcc->compparms.stepsizes = 0; + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; + } + } + + /* Write a SOD marker to indicate the end of the tile header. */ + if (!(enc->mrk = jpc_ms_create(JPC_MS_SOD))) { + return -1; + } + if (jpc_putms(enc->tmpstream, enc->cstate, enc->mrk)) { + jas_eprintf("cannot write SOD marker\n"); + return -1; + } + jpc_ms_destroy(enc->mrk); + enc->mrk = 0; +tilehdrlen = jas_stream_getrwcount(enc->tmpstream); + +/************************************************************************/ +/************************************************************************/ +/************************************************************************/ + +if (jpc_enc_enccblks(enc)) { + abort(); + return -1; +} + + cp = enc->cp; + rho = (double) (tile->brx - tile->tlx) * (tile->bry - tile->tly) / + ((cp->refgrdwidth - cp->imgareatlx) * (cp->refgrdheight - + cp->imgareatly)); + tile->rawsize = cp->rawsize * rho; + + for (lyrno = 0; lyrno < tile->numlyrs - 1; ++lyrno) { + tile->lyrsizes[lyrno] = tile->rawsize * jpc_fixtodbl( + cp->tcp.ilyrrates[lyrno]); + } + tile->lyrsizes[tile->numlyrs - 1] = (cp->totalsize != UINT_FAST32_MAX) ? + (rho * enc->mainbodysize) : UINT_FAST32_MAX; + for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { + if (tile->lyrsizes[lyrno] != UINT_FAST32_MAX) { + if (tilehdrlen <= JAS_CAST(long, tile->lyrsizes[lyrno])) { + tile->lyrsizes[lyrno] -= tilehdrlen; + } else { + tile->lyrsizes[lyrno] = 0; + } + } + } + + if (rateallocate(enc, tile->numlyrs, tile->lyrsizes)) { + return -1; + } + +#if 0 +jas_eprintf("ENCODE TILE DATA\n"); +#endif + if (jpc_enc_encodetiledata(enc)) { + jas_eprintf("dotile failed\n"); + return -1; + } + +/************************************************************************/ +/************************************************************************/ +/************************************************************************/ + +/************************************************************************/ +/************************************************************************/ +/************************************************************************/ + + tilelen = jas_stream_tell(enc->tmpstream); + + if (jas_stream_seek(enc->tmpstream, 6, SEEK_SET) < 0) { + return -1; + } + jpc_putuint32(enc->tmpstream, tilelen); + + if (jas_stream_seek(enc->tmpstream, 0, SEEK_SET) < 0) { + return -1; + } + if (jpc_putdata(enc->out, enc->tmpstream, -1)) { + return -1; + } + enc->len += tilelen; + + jas_stream_close(enc->tmpstream); + enc->tmpstream = 0; + + jpc_enc_tile_destroy(enc->curtile); + enc->curtile = 0; + + } + + return 0; +} + +int jpc_enc_encodetiledata(jpc_enc_t *enc) +{ +assert(enc->tmpstream); + if (jpc_enc_encpkts(enc, enc->tmpstream)) { + return -1; + } + return 0; +} + +int dump_passes(jpc_enc_pass_t *passes, int numpasses, jpc_enc_cblk_t *cblk) +{ + jpc_enc_pass_t *pass; + int i; + jas_stream_memobj_t *smo; + + smo = cblk->stream->obj_; + + pass = passes; + for (i = 0; i < numpasses; ++i) { + jas_eprintf("start=%d end=%d type=%d term=%d lyrno=%d firstchar=%02x size=%ld pos=%ld\n", + (int)pass->start, (int)pass->end, (int)pass->type, (int)pass->term, (int)pass->lyrno, + smo->buf_[pass->start], (long)smo->len_, (long)smo->pos_); +#if 0 + jas_memdump(stderr, &smo->buf_[pass->start], pass->end - pass->start); +#endif + ++pass; + } + return 0; +} + +void jpc_quantize(jas_matrix_t *data, jpc_fix_t stepsize) +{ + int i; + int j; + jpc_fix_t t; + + if (stepsize == jpc_inttofix(1)) { + return; + } + + for (i = 0; i < jas_matrix_numrows(data); ++i) { + for (j = 0; j < jas_matrix_numcols(data); ++j) { + t = jas_matrix_get(data, i, j); + +{ + if (t < 0) { + t = jpc_fix_neg(jpc_fix_div(jpc_fix_neg(t), stepsize)); + } else { + t = jpc_fix_div(t, stepsize); + } +} + + jas_matrix_set(data, i, j, t); + } + } +} + +void calcrdslopes(jpc_enc_cblk_t *cblk) +{ + jpc_enc_pass_t *endpasses; + jpc_enc_pass_t *pass0; + jpc_enc_pass_t *pass1; + jpc_enc_pass_t *pass2; + jpc_flt_t slope0; + jpc_flt_t slope; + jpc_flt_t dd; + long dr; + + endpasses = &cblk->passes[cblk->numpasses]; + pass2 = cblk->passes; + slope0 = 0; + while (pass2 != endpasses) { + pass0 = 0; + for (pass1 = cblk->passes; pass1 != endpasses; ++pass1) { + dd = pass1->cumwmsedec; + dr = pass1->end; + if (pass0) { + dd -= pass0->cumwmsedec; + dr -= pass0->end; + } + if (dd <= 0) { + pass1->rdslope = JPC_BADRDSLOPE; + if (pass1 >= pass2) { + pass2 = &pass1[1]; + } + continue; + } + if (pass1 < pass2 && pass1->rdslope <= 0) { + continue; + } + if (!dr) { + assert(pass0); + pass0->rdslope = 0; + break; + } + slope = dd / dr; + if (pass0 && slope >= slope0) { + pass0->rdslope = 0; + break; + } + pass1->rdslope = slope; + if (pass1 >= pass2) { + pass2 = &pass1[1]; + } + pass0 = pass1; + slope0 = slope; + } + } + +#if 0 + for (pass0 = cblk->passes; pass0 != endpasses; ++pass0) { +if (pass0->rdslope > 0.0) { + jas_eprintf("pass %02d nmsedec=%lf dec=%lf end=%d %lf\n", pass0 - cblk->passes, + fixtodbl(pass0->nmsedec), pass0->wmsedec, pass0->end, pass0->rdslope); +} + } +#endif +} + +void dump_layeringinfo(jpc_enc_t *enc) +{ + + jpc_enc_tcmpt_t *tcmpt; + int tcmptno; + jpc_enc_rlvl_t *rlvl; + int rlvlno; + jpc_enc_band_t *band; + int bandno; + jpc_enc_prc_t *prc; + int prcno; + jpc_enc_cblk_t *cblk; + int cblkno; + jpc_enc_pass_t *pass; + int passno; + int lyrno; + jpc_enc_tile_t *tile; + + tile = enc->curtile; + + for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { + jas_eprintf("lyrno = %02d\n", lyrno); + for (tcmptno = 0, tcmpt = tile->tcmpts; tcmptno < tile->numtcmpts; + ++tcmptno, ++tcmpt) { + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + if (!rlvl->bands) { + continue; + } + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; + ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < + prc->numcblks; ++cblkno, ++cblk) { + for (passno = 0, pass = cblk->passes; passno < + cblk->numpasses && pass->lyrno == lyrno; + ++passno, ++pass) { + jas_eprintf("lyrno=%02d cmptno=%02d rlvlno=%02d bandno=%02d prcno=%02d cblkno=%03d passno=%03d\n", lyrno, tcmptno, rlvlno, bandno, prcno, cblkno, passno); + } + } + } + } + } + } + } +} + +int rateallocate(jpc_enc_t *enc, int numlyrs, uint_fast32_t *cumlens) +{ + jpc_flt_t lo; + jpc_flt_t hi; + jas_stream_t *out; + long cumlen; + int lyrno; + jpc_flt_t thresh; + jpc_flt_t goodthresh; + int success; + long pos; + long oldpos; + int numiters; + + jpc_enc_tcmpt_t *comp; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_rlvl_t *lvl; + jpc_enc_rlvl_t *endlvls; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_cblk_t *cblk; + jpc_enc_cblk_t *endcblks; + jpc_enc_pass_t *pass; + jpc_enc_pass_t *endpasses; + jpc_enc_pass_t *pass1; + jpc_flt_t mxrdslope; + jpc_flt_t mnrdslope; + jpc_enc_tile_t *tile; + jpc_enc_prc_t *prc; + int prcno; + + tile = enc->curtile; + + for (lyrno = 1; lyrno < numlyrs - 1; ++lyrno) { + if (cumlens[lyrno - 1] > cumlens[lyrno]) { + abort(); + } + } + + if (!(out = jas_stream_memopen(0, 0))) { + return -1; + } + + + /* Find minimum and maximum R-D slope values. */ + mnrdslope = DBL_MAX; + mxrdslope = 0; + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + calcrdslopes(cblk); + endpasses = &cblk->passes[cblk->numpasses]; + for (pass = cblk->passes; pass != endpasses; ++pass) { + if (pass->rdslope > 0) { + if (pass->rdslope < mnrdslope) { + mnrdslope = pass->rdslope; + } + if (pass->rdslope > mxrdslope) { + mxrdslope = pass->rdslope; + } + } + } + } + } + } + } + } +if (jas_getdbglevel()) { + jas_eprintf("min rdslope = %f max rdslope = %f\n", mnrdslope, mxrdslope); +} + + jpc_init_t2state(enc, 1); + + for (lyrno = 0; lyrno < numlyrs; ++lyrno) { + + lo = mnrdslope; + hi = mxrdslope; + + success = 0; + goodthresh = 0; + numiters = 0; + + do { + + cumlen = cumlens[lyrno]; + if (cumlen == UINT_FAST32_MAX) { + /* Only the last layer can be free of a rate + constraint (e.g., for lossless coding). */ + assert(lyrno == numlyrs - 1); + goodthresh = -1; + success = 1; + break; + } + + thresh = (lo + hi) / 2; + + /* Save the tier 2 coding state. */ + jpc_save_t2state(enc); + oldpos = jas_stream_tell(out); + assert(oldpos >= 0); + + /* Assign all passes with R-D slopes greater than or + equal to the current threshold to this layer. */ + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + if (cblk->curpass) { + endpasses = &cblk->passes[cblk->numpasses]; + pass1 = cblk->curpass; + for (pass = cblk->curpass; pass != endpasses; ++pass) { + if (pass->rdslope >= thresh) { + pass1 = &pass[1]; + } + } + for (pass = cblk->curpass; pass != pass1; ++pass) { + pass->lyrno = lyrno; + } + for (; pass != endpasses; ++pass) { + pass->lyrno = -1; + } + } + } + } + } + } + } + + /* Perform tier 2 coding. */ + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + for (prcno = 0; prcno < lvl->numprcs; ++prcno) { + if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) { + return -1; + } + } + } + } + + pos = jas_stream_tell(out); + + /* Check the rate constraint. */ + assert(pos >= 0); + if (pos > cumlen) { + /* The rate is too high. */ + lo = thresh; + } else if (pos <= cumlen) { + /* The rate is low enough, so try higher. */ + hi = thresh; + if (!success || thresh < goodthresh) { + goodthresh = thresh; + success = 1; + } + } + + /* Save the tier 2 coding state. */ + jpc_restore_t2state(enc); + if (jas_stream_seek(out, oldpos, SEEK_SET) < 0) { + abort(); + } + +if (jas_getdbglevel()) { +jas_eprintf("maxlen=%08ld actuallen=%08ld thresh=%f\n", cumlen, pos, thresh); +} + + ++numiters; + } while (lo < hi - 1e-3 && numiters < 32); + + if (!success) { + jas_eprintf("warning: empty layer generated\n"); + } + +if (jas_getdbglevel()) { +jas_eprintf("success %d goodthresh %f\n", success, goodthresh); +} + + /* Assign all passes with R-D slopes greater than or + equal to the selected threshold to this layer. */ + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { +if (!lvl->bands) { + continue; +} + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + if (cblk->curpass) { + endpasses = &cblk->passes[cblk->numpasses]; + pass1 = cblk->curpass; + if (success) { + for (pass = cblk->curpass; pass != endpasses; ++pass) { + if (pass->rdslope >= goodthresh) { + pass1 = &pass[1]; + } + } + } + for (pass = cblk->curpass; pass != pass1; ++pass) { + pass->lyrno = lyrno; + } + for (; pass != endpasses; ++pass) { + pass->lyrno = -1; + } + } + } + } + } + } + } + + /* Perform tier 2 coding. */ + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + for (prcno = 0; prcno < lvl->numprcs; ++prcno) { + if (jpc_enc_encpkt(enc, out, comp - tile->tcmpts, lvl - comp->rlvls, prcno, lyrno)) { + return -1; + } + } + } + } + } + + if (jas_getdbglevel() >= 5) { + dump_layeringinfo(enc); + } + + jas_stream_close(out); + + JAS_DBGLOG(10, ("done doing rateallocation\n")); +#if 0 +jas_eprintf("DONE RATE ALLOCATE\n"); +#endif + + return 0; +} + +/******************************************************************************\ +* Tile constructors and destructors. +\******************************************************************************/ + +jpc_enc_tile_t *jpc_enc_tile_create(jpc_enc_cp_t *cp, jas_image_t *image, int tileno) +{ + jpc_enc_tile_t *tile; + uint_fast32_t htileno; + uint_fast32_t vtileno; + uint_fast16_t lyrno; + uint_fast16_t cmptno; + jpc_enc_tcmpt_t *tcmpt; + + if (!(tile = jas_malloc(sizeof(jpc_enc_tile_t)))) { + goto error; + } + + /* Initialize a few members used in error recovery. */ + tile->tcmpts = 0; + tile->lyrsizes = 0; + tile->numtcmpts = cp->numcmpts; + tile->pi = 0; + + tile->tileno = tileno; + htileno = tileno % cp->numhtiles; + vtileno = tileno / cp->numhtiles; + + /* Calculate the coordinates of the top-left and bottom-right + corners of the tile. */ + tile->tlx = JAS_MAX(cp->tilegrdoffx + htileno * cp->tilewidth, + cp->imgareatlx); + tile->tly = JAS_MAX(cp->tilegrdoffy + vtileno * cp->tileheight, + cp->imgareatly); + tile->brx = JAS_MIN(cp->tilegrdoffx + (htileno + 1) * cp->tilewidth, + cp->refgrdwidth); + tile->bry = JAS_MIN(cp->tilegrdoffy + (vtileno + 1) * cp->tileheight, + cp->refgrdheight); + + /* Initialize some tile coding parameters. */ + tile->intmode = cp->tcp.intmode; + tile->csty = cp->tcp.csty; + tile->prg = cp->tcp.prg; + tile->mctid = cp->tcp.mctid; + + tile->numlyrs = cp->tcp.numlyrs; + if (!(tile->lyrsizes = jas_malloc(tile->numlyrs * + sizeof(uint_fast32_t)))) { + goto error; + } + for (lyrno = 0; lyrno < tile->numlyrs; ++lyrno) { + tile->lyrsizes[lyrno] = 0; + } + + /* Allocate an array for the per-tile-component information. */ + if (!(tile->tcmpts = jas_malloc(cp->numcmpts * sizeof(jpc_enc_tcmpt_t)))) { + goto error; + } + /* Initialize a few members critical for error recovery. */ + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + tcmpt->rlvls = 0; + tcmpt->tsfb = 0; + tcmpt->data = 0; + } + /* Initialize the per-tile-component information. */ + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + if (!tcmpt_create(tcmpt, cp, image, tile)) { + goto error; + } + } + + /* Initialize the synthesis weights for the MCT. */ + switch (tile->mctid) { + case JPC_MCT_RCT: + tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0)); + tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(0.6875)); + tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(0.6875)); + break; + case JPC_MCT_ICT: + tile->tcmpts[0].synweight = jpc_dbltofix(sqrt(3.0000)); + tile->tcmpts[1].synweight = jpc_dbltofix(sqrt(3.2584)); + tile->tcmpts[2].synweight = jpc_dbltofix(sqrt(2.4755)); + break; + default: + case JPC_MCT_NONE: + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < cp->numcmpts; + ++cmptno, ++tcmpt) { + tcmpt->synweight = JPC_FIX_ONE; + } + break; + } + + if (!(tile->pi = jpc_enc_pi_create(cp, tile))) { + goto error; + } + + return tile; + +error: + + if (tile) { + jpc_enc_tile_destroy(tile); + } + return 0; +} + +void jpc_enc_tile_destroy(jpc_enc_tile_t *tile) +{ + jpc_enc_tcmpt_t *tcmpt; + uint_fast16_t cmptno; + + if (tile->tcmpts) { + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < + tile->numtcmpts; ++cmptno, ++tcmpt) { + tcmpt_destroy(tcmpt); + } + jas_free(tile->tcmpts); + } + if (tile->lyrsizes) { + jas_free(tile->lyrsizes); + } + if (tile->pi) { + jpc_pi_destroy(tile->pi); + } + jas_free(tile); +} + +static jpc_enc_tcmpt_t *tcmpt_create(jpc_enc_tcmpt_t *tcmpt, jpc_enc_cp_t *cp, + jas_image_t *image, jpc_enc_tile_t *tile) +{ + uint_fast16_t cmptno; + uint_fast16_t rlvlno; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t tlx; + uint_fast32_t tly; + uint_fast32_t brx; + uint_fast32_t bry; + uint_fast32_t cmpttlx; + uint_fast32_t cmpttly; + jpc_enc_ccp_t *ccp; + jpc_tsfb_band_t bandinfos[JPC_MAXBANDS]; + + tcmpt->tile = tile; + tcmpt->tsfb = 0; + tcmpt->data = 0; + tcmpt->rlvls = 0; + + /* Deduce the component number. */ + cmptno = tcmpt - tile->tcmpts; + + ccp = &cp->ccps[cmptno]; + + /* Compute the coordinates of the top-left and bottom-right + corners of this tile-component. */ + tlx = JPC_CEILDIV(tile->tlx, ccp->sampgrdstepx); + tly = JPC_CEILDIV(tile->tly, ccp->sampgrdstepy); + brx = JPC_CEILDIV(tile->brx, ccp->sampgrdstepx); + bry = JPC_CEILDIV(tile->bry, ccp->sampgrdstepy); + + /* Create a sequence to hold the tile-component sample data. */ + if (!(tcmpt->data = jas_seq2d_create(tlx, tly, brx, bry))) { + goto error; + } + + /* Get the image data associated with this tile-component. */ + cmpttlx = JPC_CEILDIV(cp->imgareatlx, ccp->sampgrdstepx); + cmpttly = JPC_CEILDIV(cp->imgareatly, ccp->sampgrdstepy); + if (jas_image_readcmpt(image, cmptno, tlx - cmpttlx, tly - cmpttly, + brx - tlx, bry - tly, tcmpt->data)) { + goto error; + } + + tcmpt->synweight = 0; + tcmpt->qmfbid = cp->tccp.qmfbid; + tcmpt->numrlvls = cp->tccp.maxrlvls; + tcmpt->numbands = 3 * tcmpt->numrlvls - 2; + if (!(tcmpt->tsfb = jpc_cod_gettsfb(tcmpt->qmfbid, tcmpt->numrlvls - 1))) { + goto error; + } + + for (rlvlno = 0; rlvlno < tcmpt->numrlvls; ++rlvlno) { + tcmpt->prcwidthexpns[rlvlno] = cp->tccp.prcwidthexpns[rlvlno]; + tcmpt->prcheightexpns[rlvlno] = cp->tccp.prcheightexpns[rlvlno]; + } + tcmpt->cblkwidthexpn = cp->tccp.cblkwidthexpn; + tcmpt->cblkheightexpn = cp->tccp.cblkheightexpn; + tcmpt->cblksty = cp->tccp.cblksty; + tcmpt->csty = cp->tccp.csty; + + tcmpt->numstepsizes = tcmpt->numbands; + assert(tcmpt->numstepsizes <= JPC_MAXBANDS); + memset(tcmpt->stepsizes, 0, sizeof(tcmpt->numstepsizes * + sizeof(uint_fast16_t))); + + /* Retrieve information about the various bands. */ + jpc_tsfb_getbands(tcmpt->tsfb, jas_seq2d_xstart(tcmpt->data), + jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data), + jas_seq2d_yend(tcmpt->data), bandinfos); + + if (!(tcmpt->rlvls = jas_malloc(tcmpt->numrlvls * sizeof(jpc_enc_rlvl_t)))) { + goto error; + } + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + rlvl->bands = 0; + rlvl->tcmpt = tcmpt; + } + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + if (!rlvl_create(rlvl, cp, tcmpt, bandinfos)) { + goto error; + } + } + + return tcmpt; + +error: + + tcmpt_destroy(tcmpt); + return 0; + +} + +static void tcmpt_destroy(jpc_enc_tcmpt_t *tcmpt) +{ + jpc_enc_rlvl_t *rlvl; + uint_fast16_t rlvlno; + + if (tcmpt->rlvls) { + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + rlvl_destroy(rlvl); + } + jas_free(tcmpt->rlvls); + } + + if (tcmpt->data) { + jas_seq2d_destroy(tcmpt->data); + } + if (tcmpt->tsfb) { + jpc_tsfb_destroy(tcmpt->tsfb); + } +} + +static jpc_enc_rlvl_t *rlvl_create(jpc_enc_rlvl_t *rlvl, jpc_enc_cp_t *cp, + jpc_enc_tcmpt_t *tcmpt, jpc_tsfb_band_t *bandinfos) +{ + uint_fast16_t rlvlno; + uint_fast32_t tlprctlx; + uint_fast32_t tlprctly; + uint_fast32_t brprcbrx; + uint_fast32_t brprcbry; + uint_fast16_t bandno; + jpc_enc_band_t *band; + + /* Deduce the resolution level. */ + rlvlno = rlvl - tcmpt->rlvls; + + /* Initialize members required for error recovery. */ + rlvl->bands = 0; + rlvl->tcmpt = tcmpt; + + /* Compute the coordinates of the top-left and bottom-right + corners of the tile-component at this resolution. */ + rlvl->tlx = JPC_CEILDIVPOW2(jas_seq2d_xstart(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->tly = JPC_CEILDIVPOW2(jas_seq2d_ystart(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->brx = JPC_CEILDIVPOW2(jas_seq2d_xend(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + rlvl->bry = JPC_CEILDIVPOW2(jas_seq2d_yend(tcmpt->data), tcmpt->numrlvls - + 1 - rlvlno); + + if (rlvl->tlx >= rlvl->brx || rlvl->tly >= rlvl->bry) { + rlvl->numhprcs = 0; + rlvl->numvprcs = 0; + rlvl->numprcs = 0; + return rlvl; + } + + rlvl->numbands = (!rlvlno) ? 1 : 3; + rlvl->prcwidthexpn = cp->tccp.prcwidthexpns[rlvlno]; + rlvl->prcheightexpn = cp->tccp.prcheightexpns[rlvlno]; + if (!rlvlno) { + rlvl->cbgwidthexpn = rlvl->prcwidthexpn; + rlvl->cbgheightexpn = rlvl->prcheightexpn; + } else { + rlvl->cbgwidthexpn = rlvl->prcwidthexpn - 1; + rlvl->cbgheightexpn = rlvl->prcheightexpn - 1; + } + rlvl->cblkwidthexpn = JAS_MIN(cp->tccp.cblkwidthexpn, rlvl->cbgwidthexpn); + rlvl->cblkheightexpn = JAS_MIN(cp->tccp.cblkheightexpn, rlvl->cbgheightexpn); + + /* Compute the number of precincts. */ + tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); + tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); + brprcbrx = JPC_CEILTOMULTPOW2(rlvl->brx, rlvl->prcwidthexpn); + brprcbry = JPC_CEILTOMULTPOW2(rlvl->bry, rlvl->prcheightexpn); + rlvl->numhprcs = JPC_FLOORDIVPOW2(brprcbrx - tlprctlx, rlvl->prcwidthexpn); + rlvl->numvprcs = JPC_FLOORDIVPOW2(brprcbry - tlprctly, rlvl->prcheightexpn); + rlvl->numprcs = rlvl->numhprcs * rlvl->numvprcs; + + if (!(rlvl->bands = jas_malloc(rlvl->numbands * sizeof(jpc_enc_band_t)))) { + goto error; + } + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + band->prcs = 0; + band->data = 0; + band->rlvl = rlvl; + } + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band_create(band, cp, rlvl, bandinfos)) { + goto error; + } + } + + return rlvl; +error: + + rlvl_destroy(rlvl); + return 0; +} + +static void rlvl_destroy(jpc_enc_rlvl_t *rlvl) +{ + jpc_enc_band_t *band; + uint_fast16_t bandno; + + if (rlvl->bands) { + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + band_destroy(band); + } + jas_free(rlvl->bands); + } +} + +static jpc_enc_band_t *band_create(jpc_enc_band_t *band, jpc_enc_cp_t *cp, + jpc_enc_rlvl_t *rlvl, jpc_tsfb_band_t *bandinfos) +{ + uint_fast16_t bandno; + uint_fast16_t gblbandno; + uint_fast16_t rlvlno; + jpc_tsfb_band_t *bandinfo; + jpc_enc_tcmpt_t *tcmpt; + uint_fast32_t prcno; + jpc_enc_prc_t *prc; + + tcmpt = rlvl->tcmpt; + band->data = 0; + band->prcs = 0; + band->rlvl = rlvl; + + /* Deduce the resolution level and band number. */ + rlvlno = rlvl - rlvl->tcmpt->rlvls; + bandno = band - rlvl->bands; + gblbandno = (!rlvlno) ? 0 : (3 * (rlvlno - 1) + bandno + 1); + + bandinfo = &bandinfos[gblbandno]; + +if (bandinfo->xstart != bandinfo->xend && bandinfo->ystart != bandinfo->yend) { + if (!(band->data = jas_seq2d_create(0, 0, 0, 0))) { + goto error; + } + jas_seq2d_bindsub(band->data, tcmpt->data, bandinfo->locxstart, + bandinfo->locystart, bandinfo->locxend, bandinfo->locyend); + jas_seq2d_setshift(band->data, bandinfo->xstart, bandinfo->ystart); +} + band->orient = bandinfo->orient; + band->analgain = JPC_NOMINALGAIN(cp->tccp.qmfbid, tcmpt->numrlvls, rlvlno, + band->orient); + band->numbps = 0; + band->absstepsize = 0; + band->stepsize = 0; + band->synweight = bandinfo->synenergywt; + +if (band->data) { + if (!(band->prcs = jas_malloc(rlvl->numprcs * sizeof(jpc_enc_prc_t)))) { + goto error; + } + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno, + ++prc) { + prc->cblks = 0; + prc->incltree = 0; + prc->nlibtree = 0; + prc->savincltree = 0; + prc->savnlibtree = 0; + prc->band = band; + } + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; ++prcno, + ++prc) { + if (!prc_create(prc, cp, band)) { + goto error; + } + } +} + + return band; + +error: + band_destroy(band); + return 0; +} + +static void band_destroy(jpc_enc_band_t *band) +{ + jpc_enc_prc_t *prc; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t prcno; + + if (band->prcs) { + rlvl = band->rlvl; + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; + ++prcno, ++prc) { + prc_destroy(prc); + } + jas_free(band->prcs); + } + if (band->data) { + jas_seq2d_destroy(band->data); + } +} + +static jpc_enc_prc_t *prc_create(jpc_enc_prc_t *prc, jpc_enc_cp_t *cp, jpc_enc_band_t *band) +{ + uint_fast32_t prcno; + uint_fast32_t prcxind; + uint_fast32_t prcyind; + uint_fast32_t cbgtlx; + uint_fast32_t cbgtly; + uint_fast32_t tlprctlx; + uint_fast32_t tlprctly; + uint_fast32_t tlcbgtlx; + uint_fast32_t tlcbgtly; + uint_fast16_t rlvlno; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t tlcblktlx; + uint_fast32_t tlcblktly; + uint_fast32_t brcblkbrx; + uint_fast32_t brcblkbry; + uint_fast32_t cblkno; + jpc_enc_cblk_t *cblk; + jpc_enc_tcmpt_t *tcmpt; + + prc->cblks = 0; + prc->incltree = 0; + prc->savincltree = 0; + prc->nlibtree = 0; + prc->savnlibtree = 0; + + rlvl = band->rlvl; + tcmpt = rlvl->tcmpt; +rlvlno = rlvl - tcmpt->rlvls; + prcno = prc - band->prcs; + prcxind = prcno % rlvl->numhprcs; + prcyind = prcno / rlvl->numhprcs; + prc->band = band; + +tlprctlx = JPC_FLOORTOMULTPOW2(rlvl->tlx, rlvl->prcwidthexpn); +tlprctly = JPC_FLOORTOMULTPOW2(rlvl->tly, rlvl->prcheightexpn); +if (!rlvlno) { + tlcbgtlx = tlprctlx; + tlcbgtly = tlprctly; +} else { + tlcbgtlx = JPC_CEILDIVPOW2(tlprctlx, 1); + tlcbgtly = JPC_CEILDIVPOW2(tlprctly, 1); +} + + /* Compute the coordinates of the top-left and bottom-right + corners of the precinct. */ + cbgtlx = tlcbgtlx + (prcxind << rlvl->cbgwidthexpn); + cbgtly = tlcbgtly + (prcyind << rlvl->cbgheightexpn); + prc->tlx = JAS_MAX(jas_seq2d_xstart(band->data), cbgtlx); + prc->tly = JAS_MAX(jas_seq2d_ystart(band->data), cbgtly); + prc->brx = JAS_MIN(jas_seq2d_xend(band->data), cbgtlx + + (1 << rlvl->cbgwidthexpn)); + prc->bry = JAS_MIN(jas_seq2d_yend(band->data), cbgtly + + (1 << rlvl->cbgheightexpn)); + + if (prc->tlx < prc->brx && prc->tly < prc->bry) { + /* The precinct contains at least one code block. */ + + tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); + tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); + brcblkbrx = JPC_CEILTOMULTPOW2(prc->brx, rlvl->cblkwidthexpn); + brcblkbry = JPC_CEILTOMULTPOW2(prc->bry, rlvl->cblkheightexpn); + prc->numhcblks = JPC_FLOORDIVPOW2(brcblkbrx - tlcblktlx, + rlvl->cblkwidthexpn); + prc->numvcblks = JPC_FLOORDIVPOW2(brcblkbry - tlcblktly, + rlvl->cblkheightexpn); + prc->numcblks = prc->numhcblks * prc->numvcblks; + + if (!(prc->incltree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + if (!(prc->nlibtree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + if (!(prc->savincltree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + if (!(prc->savnlibtree = jpc_tagtree_create(prc->numhcblks, + prc->numvcblks))) { + goto error; + } + + if (!(prc->cblks = jas_malloc(prc->numcblks * sizeof(jpc_enc_cblk_t)))) { + goto error; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + cblk->passes = 0; + cblk->stream = 0; + cblk->mqenc = 0; + cblk->data = 0; + cblk->flags = 0; + cblk->prc = prc; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + if (!cblk_create(cblk, cp, prc)) { + goto error; + } + } + } else { + /* The precinct does not contain any code blocks. */ + prc->tlx = prc->brx; + prc->tly = prc->bry; + prc->numcblks = 0; + prc->numhcblks = 0; + prc->numvcblks = 0; + prc->cblks = 0; + prc->incltree = 0; + prc->nlibtree = 0; + prc->savincltree = 0; + prc->savnlibtree = 0; + } + + return prc; + +error: + prc_destroy(prc); + return 0; +} + +static void prc_destroy(jpc_enc_prc_t *prc) +{ + jpc_enc_cblk_t *cblk; + uint_fast32_t cblkno; + + if (prc->cblks) { + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + cblk_destroy(cblk); + } + jas_free(prc->cblks); + } + if (prc->incltree) { + jpc_tagtree_destroy(prc->incltree); + } + if (prc->nlibtree) { + jpc_tagtree_destroy(prc->nlibtree); + } + if (prc->savincltree) { + jpc_tagtree_destroy(prc->savincltree); + } + if (prc->savnlibtree) { + jpc_tagtree_destroy(prc->savnlibtree); + } +} + +static jpc_enc_cblk_t *cblk_create(jpc_enc_cblk_t *cblk, jpc_enc_cp_t *cp, jpc_enc_prc_t *prc) +{ + jpc_enc_band_t *band; + uint_fast32_t cblktlx; + uint_fast32_t cblktly; + uint_fast32_t cblkbrx; + uint_fast32_t cblkbry; + jpc_enc_rlvl_t *rlvl; + uint_fast32_t cblkxind; + uint_fast32_t cblkyind; + uint_fast32_t cblkno; + uint_fast32_t tlcblktlx; + uint_fast32_t tlcblktly; + + cblkno = cblk - prc->cblks; + cblkxind = cblkno % prc->numhcblks; + cblkyind = cblkno / prc->numhcblks; + rlvl = prc->band->rlvl; + cblk->prc = prc; + + cblk->numpasses = 0; + cblk->passes = 0; + cblk->numencpasses = 0; + cblk->numimsbs = 0; + cblk->numlenbits = 0; + cblk->stream = 0; + cblk->mqenc = 0; + cblk->flags = 0; + cblk->numbps = 0; + cblk->curpass = 0; + cblk->data = 0; + cblk->savedcurpass = 0; + cblk->savednumlenbits = 0; + cblk->savednumencpasses = 0; + + band = prc->band; + tlcblktlx = JPC_FLOORTOMULTPOW2(prc->tlx, rlvl->cblkwidthexpn); + tlcblktly = JPC_FLOORTOMULTPOW2(prc->tly, rlvl->cblkheightexpn); + cblktlx = JAS_MAX(tlcblktlx + (cblkxind << rlvl->cblkwidthexpn), prc->tlx); + cblktly = JAS_MAX(tlcblktly + (cblkyind << rlvl->cblkheightexpn), prc->tly); + cblkbrx = JAS_MIN(tlcblktlx + ((cblkxind + 1) << rlvl->cblkwidthexpn), + prc->brx); + cblkbry = JAS_MIN(tlcblktly + ((cblkyind + 1) << rlvl->cblkheightexpn), + prc->bry); + + assert(cblktlx < cblkbrx && cblktly < cblkbry); + if (!(cblk->data = jas_seq2d_create(0, 0, 0, 0))) { + goto error; + } + jas_seq2d_bindsub(cblk->data, band->data, cblktlx, cblktly, cblkbrx, cblkbry); + + return cblk; + +error: + cblk_destroy(cblk); + return 0; +} + +static void cblk_destroy(jpc_enc_cblk_t *cblk) +{ + uint_fast16_t passno; + jpc_enc_pass_t *pass; + if (cblk->passes) { + for (passno = 0, pass = cblk->passes; passno < cblk->numpasses; + ++passno, ++pass) { + pass_destroy(pass); + } + jas_free(cblk->passes); + } + if (cblk->stream) { + jas_stream_close(cblk->stream); + } + if (cblk->mqenc) { + jpc_mqenc_destroy(cblk->mqenc); + } + if (cblk->data) { + jas_seq2d_destroy(cblk->data); + } + if (cblk->flags) { + jas_seq2d_destroy(cblk->flags); + } +} + +static void pass_destroy(jpc_enc_pass_t *pass) +{ + /* XXX - need to free resources here */ +} + +void jpc_enc_dump(jpc_enc_t *enc) +{ + jpc_enc_tile_t *tile; + jpc_enc_tcmpt_t *tcmpt; + jpc_enc_rlvl_t *rlvl; + jpc_enc_band_t *band; + jpc_enc_prc_t *prc; + jpc_enc_cblk_t *cblk; + uint_fast16_t cmptno; + uint_fast16_t rlvlno; + uint_fast16_t bandno; + uint_fast32_t prcno; + uint_fast32_t cblkno; + + tile = enc->curtile; + + for (cmptno = 0, tcmpt = tile->tcmpts; cmptno < tile->numtcmpts; ++cmptno, + ++tcmpt) { + jas_eprintf(" tcmpt %5d %5d %5d %5d\n", jas_seq2d_xstart(tcmpt->data), jas_seq2d_ystart(tcmpt->data), jas_seq2d_xend(tcmpt->data), jas_seq2d_yend(tcmpt->data)); + for (rlvlno = 0, rlvl = tcmpt->rlvls; rlvlno < tcmpt->numrlvls; + ++rlvlno, ++rlvl) { + jas_eprintf(" rlvl %5d %5d %5d %5d\n", rlvl->tlx, rlvl->tly, rlvl->brx, rlvl->bry); + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band->data) { + continue; + } + jas_eprintf(" band %5d %5d %5d %5d\n", jas_seq2d_xstart(band->data), jas_seq2d_ystart(band->data), jas_seq2d_xend(band->data), jas_seq2d_yend(band->data)); + for (prcno = 0, prc = band->prcs; prcno < rlvl->numprcs; + ++prcno, ++prc) { + jas_eprintf(" prc %5d %5d %5d %5d (%5d %5d)\n", prc->tlx, prc->tly, prc->brx, prc->bry, prc->brx - prc->tlx, prc->bry - prc->tly); + if (!prc->cblks) { + continue; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + jas_eprintf(" cblk %5d %5d %5d %5d\n", jas_seq2d_xstart(cblk->data), jas_seq2d_ystart(cblk->data), jas_seq2d_xend(cblk->data), jas_seq2d_yend(cblk->data)); + } + } + } + } + } +} diff --git a/src/libjasper/jpc/jpc_enc.h b/src/libjasper/jpc/jpc_enc.h new file mode 100644 index 0000000..a29720b --- /dev/null +++ b/src/libjasper/jpc/jpc_enc.h @@ -0,0 +1,646 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * $Id: jpc_enc.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_ENC_H +#define JPC_ENC_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_seq.h" + +#include "jpc_t2cod.h" +#include "jpc_mqenc.h" +#include "jpc_cod.h" +#include "jpc_tagtree.h" +#include "jpc_cs.h" +#include "jpc_flt.h" +#include "jpc_tsfb.h" + +/******************************************************************************\ +* Constants. +\******************************************************************************/ + +/* The number of bits used in various lookup tables. */ +#define JPC_NUMEXTRABITS JPC_NMSEDEC_FRACBITS + +/* An invalid R-D slope value. */ +#define JPC_BADRDSLOPE (-1) + +/******************************************************************************\ +* Coding parameters types. +\******************************************************************************/ + +/* Per-component coding paramters. */ + +typedef struct { + + /* The horizontal sampling period. */ + uint_fast8_t sampgrdstepx; + + /* The vertical sampling period. */ + uint_fast8_t sampgrdstepy; + + /* The sample alignment horizontal offset. */ + uint_fast8_t sampgrdsubstepx; + + /* The sample alignment vertical offset. */ + uint_fast8_t sampgrdsubstepy; + + /* The precision of the samples. */ + uint_fast8_t prec; + + /* The signedness of the samples. */ + bool sgnd; + + /* The number of step sizes. */ + uint_fast16_t numstepsizes; + + /* The quantizer step sizes. */ + uint_fast16_t stepsizes[JPC_MAXBANDS]; + +} jpc_enc_ccp_t; + +/* Per-tile coding parameters. */ + +typedef struct { + + /* The coding mode. */ + bool intmode; + + /* The coding style (i.e., SOP, EPH). */ + uint_fast8_t csty; + + /* The progression order. */ + uint_fast8_t prg; + + /* The multicomponent transform. */ + uint_fast8_t mctid; + + /* The number of layers. */ + uint_fast16_t numlyrs; + + /* The normalized bit rates associated with the various + intermediate layers. */ + jpc_fix_t *ilyrrates; + +} jpc_enc_tcp_t; + +/* Per tile-component coding parameters. */ + +typedef struct { + + /* The coding style (i.e., explicit precinct sizes). */ + uint_fast8_t csty; + + /* The maximum number of resolution levels allowed. */ + uint_fast8_t maxrlvls; + + /* The exponent for the nominal code block width. */ + uint_fast16_t cblkwidthexpn; + + /* The exponent for the nominal code block height. */ + uint_fast16_t cblkheightexpn; + + /* The code block style parameters (e.g., lazy, terminate all, + segmentation symbols, causal, reset probability models). */ + uint_fast8_t cblksty; + + /* The QMFB. */ + uint_fast8_t qmfbid; + + /* The precinct width values. */ + uint_fast16_t prcwidthexpns[JPC_MAXRLVLS]; + + /* The precinct height values. */ + uint_fast16_t prcheightexpns[JPC_MAXRLVLS]; + + /* The number of guard bits. */ + uint_fast8_t numgbits; + +} jpc_enc_tccp_t; + +/* Coding parameters. */ + +typedef struct { + + /* The debug level. */ + int debug; + + /* The horizontal offset from the origin of the reference grid to the + left edge of the image area. */ + uint_fast32_t imgareatlx; + + /* The vertical offset from the origin of the reference grid to the + top edge of the image area. */ + uint_fast32_t imgareatly; + + /* The horizontal offset from the origin of the reference grid to the + right edge of the image area (plus one). */ + uint_fast32_t refgrdwidth; + + /* The vertical offset from the origin of the reference grid to the + bottom edge of the image area (plus one). */ + uint_fast32_t refgrdheight; + + /* The horizontal offset from the origin of the tile grid to the + origin of the reference grid. */ + uint_fast32_t tilegrdoffx; + + /* The vertical offset from the origin of the tile grid to the + origin of the reference grid. */ + uint_fast32_t tilegrdoffy; + + /* The nominal tile width in units of the image reference grid. */ + uint_fast32_t tilewidth; + + /* The nominal tile height in units of the image reference grid. */ + uint_fast32_t tileheight; + + /* The number of tiles spanning the image area in the horizontal + direction. */ + uint_fast32_t numhtiles; + + /* The number of tiles spanning the image area in the vertical + direction. */ + uint_fast32_t numvtiles; + + /* The number of tiles. */ + uint_fast32_t numtiles; + + /* The number of components. */ + uint_fast16_t numcmpts; + + /* The per-component coding parameters. */ + jpc_enc_ccp_t *ccps; + + /* The per-tile coding parameters. */ + jpc_enc_tcp_t tcp; + + /* The per-tile-component coding parameters. */ + jpc_enc_tccp_t tccp; + + /* The target code stream length in bytes. */ + uint_fast32_t totalsize; + + /* The raw (i.e., uncompressed) size of the image in bytes. */ + uint_fast32_t rawsize; + +} jpc_enc_cp_t; + +/******************************************************************************\ +* Encoder class. +\******************************************************************************/ + +/* Encoder per-coding-pass state information. */ + +typedef struct { + + /* The starting offset for this pass. */ + int start; + + /* The ending offset for this pass. */ + int end; + + /* The type of data in this pass (i.e., MQ or raw). */ + int type; + + /* Flag indicating that this pass is terminated. */ + int term; + + /* The entropy coder state after coding this pass. */ + jpc_mqencstate_t mqencstate; + + /* The layer to which this pass has been assigned. */ + int lyrno; + + /* The R-D slope for this pass. */ + jpc_flt_t rdslope; + + /* The weighted MSE reduction associated with this pass. */ + jpc_flt_t wmsedec; + + /* The cumulative weighted MSE reduction. */ + jpc_flt_t cumwmsedec; + + /* The normalized MSE reduction. */ + long nmsedec; + +} jpc_enc_pass_t; + +/* Encoder per-code-block state information. */ + +typedef struct { + + /* The number of passes. */ + int numpasses; + + /* The per-pass information. */ + jpc_enc_pass_t *passes; + + /* The number of passes encoded so far. */ + int numencpasses; + + /* The number of insignificant MSBs. */ + int numimsbs; + + /* The number of bits used to encode pass data lengths. */ + int numlenbits; + + /* The byte stream for this code block. */ + jas_stream_t *stream; + + /* The entropy encoder. */ + jpc_mqenc_t *mqenc; + + /* The data for this code block. */ + jas_matrix_t *data; + + /* The state for this code block. */ + jas_matrix_t *flags; + + /* The number of bit planes required for this code block. */ + int numbps; + + /* The next pass to be encoded. */ + jpc_enc_pass_t *curpass; + + /* The per-code-block-group state information. */ + struct jpc_enc_prc_s *prc; + + /* The saved current pass. */ + /* This is used by the rate control code. */ + jpc_enc_pass_t *savedcurpass; + + /* The saved length indicator size. */ + /* This is used by the rate control code. */ + int savednumlenbits; + + /* The saved number of encoded passes. */ + /* This is used by the rate control code. */ + int savednumencpasses; + +} jpc_enc_cblk_t; + +/* Encoder per-code-block-group state information. */ + +typedef struct jpc_enc_prc_s { + + /* The x-coordinate of the top-left corner of the precinct. */ + uint_fast32_t tlx; + + /* The y-coordinate of the top-left corner of the precinct. */ + uint_fast32_t tly; + + /* The x-coordinate of the bottom-right corner of the precinct + (plus one). */ + uint_fast32_t brx; + + /* The y-coordinate of the bottom-right corner of the precinct + (plus one). */ + uint_fast32_t bry; + + /* The number of code blocks spanning the precinct in the horizontal + direction. */ + int numhcblks; + + /* The number of code blocks spanning the precinct in the vertical + direction. */ + int numvcblks; + + /* The total number of code blocks. */ + int numcblks; + + /* The per-code-block information. */ + jpc_enc_cblk_t *cblks; + + /* The inclusion tag tree. */ + jpc_tagtree_t *incltree; + + /* The insignifcant MSBs tag tree. */ + jpc_tagtree_t *nlibtree; + + /* The per-band information. */ + struct jpc_enc_band_s *band; + + /* The saved inclusion tag tree. */ + /* This is used by rate control. */ + jpc_tagtree_t *savincltree; + + /* The saved leading-insignificant-bit-planes tag tree. */ + /* This is used by rate control. */ + jpc_tagtree_t *savnlibtree; + +} jpc_enc_prc_t; + +/* Encoder per-band state information. */ + +typedef struct jpc_enc_band_s { + + /* The per precinct information. */ + jpc_enc_prc_t *prcs; + + /* The coefficient data for this band. */ + jas_matrix_t *data; + + /* The orientation of this band (i.e., LL, LH, HL, or HH). */ + int orient; + + /* The number of bit planes associated with this band. */ + int numbps; + + /* The quantizer step size. */ + jpc_fix_t absstepsize; + + /* The encoded quantizer step size. */ + int stepsize; + + /* The L2 norm of the synthesis basis functions associated with + this band. (The MCT is not considered in this value.) */ + jpc_fix_t synweight; + + /* The analysis gain for this band. */ + int analgain; + + /* The per-resolution-level information. */ + struct jpc_enc_rlvl_s *rlvl; + +} jpc_enc_band_t; + +/* Encoder per-resolution-level state information. */ + +typedef struct jpc_enc_rlvl_s { + + /* The x-coordinate of the top-left corner of the tile-component + at this resolution. */ + uint_fast32_t tlx; + + /* The y-coordinate of the top-left corner of the tile-component + at this resolution. */ + uint_fast32_t tly; + + /* The x-coordinate of the bottom-right corner of the tile-component + at this resolution (plus one). */ + uint_fast32_t brx; + + /* The y-coordinate of the bottom-right corner of the tile-component + at this resolution (plus one). */ + uint_fast32_t bry; + + /* The exponent value for the nominal precinct width measured + relative to the associated LL band. */ + int prcwidthexpn; + + /* The exponent value for the nominal precinct height measured + relative to the associated LL band. */ + int prcheightexpn; + + /* The number of precincts spanning the resolution level in the + horizontal direction. */ + int numhprcs; + + /* The number of precincts spanning the resolution level in the + vertical direction. */ + int numvprcs; + + /* The total number of precincts. */ + int numprcs; + + /* The exponent value for the nominal code block group width. + This quantity is associated with the next lower resolution level + (assuming that there is one). */ + int cbgwidthexpn; + + /* The exponent value for the nominal code block group height. + This quantity is associated with the next lower resolution level + (assuming that there is one). */ + int cbgheightexpn; + + /* The exponent value for the code block width. */ + uint_fast16_t cblkwidthexpn; + + /* The exponent value for the code block height. */ + uint_fast16_t cblkheightexpn; + + /* The number of bands associated with this resolution level. */ + int numbands; + + /* The per-band information. */ + jpc_enc_band_t *bands; + + /* The parent tile-component. */ + struct jpc_enc_tcmpt_s *tcmpt; + +} jpc_enc_rlvl_t; + +/* Encoder per-tile-component state information. */ + +typedef struct jpc_enc_tcmpt_s { + + /* The number of resolution levels. */ + int numrlvls; + + /* The per-resolution-level information. */ + jpc_enc_rlvl_t *rlvls; + + /* The tile-component data. */ + jas_matrix_t *data; + + /* The QMFB. */ + int qmfbid; + + /* The number of bands. */ + int numbands; + + /* The TSFB. */ + jpc_tsfb_t *tsfb; + + /* The synthesis energy weight (for the MCT). */ + jpc_fix_t synweight; + + /* The precinct width exponents. */ + int prcwidthexpns[JPC_MAXRLVLS]; + + /* The precinct height exponents. */ + int prcheightexpns[JPC_MAXRLVLS]; + + /* The code block width exponent. */ + int cblkwidthexpn; + + /* The code block height exponent. */ + int cblkheightexpn; + + /* Coding style (i.e., explicit precinct sizes). */ + int csty; + + /* Code block style. */ + int cblksty; + + /* The number of quantizer step sizes. */ + int numstepsizes; + + /* The encoded quantizer step sizes. */ + uint_fast16_t stepsizes[JPC_MAXBANDS]; + + /* The parent tile. */ + struct jpc_enc_tile_s *tile; + +} jpc_enc_tcmpt_t; + +/* Encoder per-tile state information. */ + +typedef struct jpc_enc_tile_s { + + /* The tile number. */ + uint_fast32_t tileno; + + /* The x-coordinate of the top-left corner of the tile measured with + respect to the reference grid. */ + uint_fast32_t tlx; + + /* The y-coordinate of the top-left corner of the tile measured with + respect to the reference grid. */ + uint_fast32_t tly; + + /* The x-coordinate of the bottom-right corner of the tile measured + with respect to the reference grid (plus one). */ + uint_fast32_t brx; + + /* The y-coordinate of the bottom-right corner of the tile measured + with respect to the reference grid (plus one). */ + uint_fast32_t bry; + + /* The coding style. */ + uint_fast8_t csty; + + /* The progression order. */ + uint_fast8_t prg; + + /* The number of layers. */ + int numlyrs; + + /* The MCT to employ (if any). */ + uint_fast8_t mctid; + + /* The packet iterator (used to determine the order of packet + generation). */ + jpc_pi_t *pi; + + /* The coding mode (i.e., integer or real). */ + bool intmode; + + /* The number of bytes to allocate to the various layers. */ + uint_fast32_t *lyrsizes; + + /* The number of tile-components. */ + int numtcmpts; + + /* The per tile-component information. */ + jpc_enc_tcmpt_t *tcmpts; + + /* The raw (i.e., uncompressed) size of this tile. */ + uint_fast32_t rawsize; + +} jpc_enc_tile_t; + +/* Encoder class. */ + +typedef struct jpc_enc_s { + + /* The image being encoded. */ + jas_image_t *image; + + /* The output stream. */ + jas_stream_t *out; + + /* The coding parameters. */ + jpc_enc_cp_t *cp; + + /* The tile currently being processed. */ + jpc_enc_tile_t *curtile; + + /* The code stream state. */ + jpc_cstate_t *cstate; + + /* The number of bytes output so far. */ + uint_fast32_t len; + + /* The number of bytes available for the main body of the code stream. */ + /* This is used for rate allocation purposes. */ + uint_fast32_t mainbodysize; + + /* The marker segment currently being processed. */ + /* This member is a convenience for making cleanup easier. */ + jpc_ms_t *mrk; + + /* The stream used to temporarily hold tile-part data. */ + jas_stream_t *tmpstream; + +} jpc_enc_t; + +#endif diff --git a/src/libjasper/jpc/jpc_fix.h b/src/libjasper/jpc/jpc_fix.h new file mode 100644 index 0000000..d2b88ed --- /dev/null +++ b/src/libjasper/jpc/jpc_fix.h @@ -0,0 +1,144 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Fixed-Point Number Class + * + * $Id: jpc_fix.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_FIX_H +#define JPC_FIX_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_types.h" +#include "jasper/jas_fix.h" + +/******************************************************************************\ +* Basic parameters of the fixed-point type. +\******************************************************************************/ + +/* The integral type used to represent a fixed-point number. This + type must be capable of representing values from -(2^31) to 2^31-1 + (inclusive). */ +typedef int_fast32_t jpc_fix_t; + +/* The integral type used to respresent higher-precision intermediate results. + This type should be capable of representing values from -(2^63) to 2^63-1 + (inclusive). */ +typedef int_fast64_t jpc_fix_big_t; + +/* The number of bits used for the fractional part of a fixed-point number. */ +#define JPC_FIX_FRACBITS 13 + +/******************************************************************************\ +* Instantiations of the generic fixed-point number macros for the +* parameters given above. (Too bad C does not support templates, eh?) +* The purpose of these macros is self-evident if one examines the +* corresponding macros in the jasper/jas_fix.h header file. +\******************************************************************************/ + +#define JPC_FIX_ZERO JAS_FIX_ZERO(jpc_fix_t, JPC_FIX_FRACBITS) +#define JPC_FIX_ONE JAS_FIX_ONE(jpc_fix_t, JPC_FIX_FRACBITS) +#define JPC_FIX_HALF JAS_FIX_HALF(jpc_fix_t, JPC_FIX_FRACBITS) + +#define jpc_inttofix(x) JAS_INTTOFIX(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_fixtoint(x) JAS_FIXTOINT(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_fixtodbl(x) JAS_FIXTODBL(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_dbltofix(x) JAS_DBLTOFIX(jpc_fix_t, JPC_FIX_FRACBITS, x) + +#define jpc_fix_add(x, y) JAS_FIX_ADD(jpc_fix_t, JPC_FIX_FRACBITS, x, y) +#define jpc_fix_sub(x, y) JAS_FIX_SUB(jpc_fix_t, JPC_FIX_FRACBITS, x, y) +#define jpc_fix_mul(x, y) \ + JAS_FIX_MUL(jpc_fix_t, JPC_FIX_FRACBITS, jpc_fix_big_t, x, y) +#define jpc_fix_mulbyint(x, y) \ + JAS_FIX_MULBYINT(jpc_fix_t, JPC_FIX_FRACBITS, x, y) +#define jpc_fix_div(x, y) \ + JAS_FIX_DIV(jpc_fix_t, JPC_FIX_FRACBITS, jpc_fix_big_t, x, y) +#define jpc_fix_neg(x) JAS_FIX_NEG(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_fix_asl(x, n) JAS_FIX_ASL(jpc_fix_t, JPC_FIX_FRACBITS, x, n) +#define jpc_fix_asr(x, n) JAS_FIX_ASR(jpc_fix_t, JPC_FIX_FRACBITS, x, n) + +#define jpc_fix_pluseq(x, y) JAS_FIX_PLUSEQ(jpc_fix_t, JPC_FIX_FRACBITS, x, y) +#define jpc_fix_minuseq(x, y) JAS_FIX_MINUSEQ(jpc_fix_t, JPC_FIX_FRACBITS, x, y) +#define jpc_fix_muleq(x, y) \ + JAS_FIX_MULEQ(jpc_fix_t, JPC_FIX_FRACBITS, jpc_fix_big_t, x, y) + +#define jpc_fix_abs(x) JAS_FIX_ABS(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_fix_isint(x) JAS_FIX_ISINT(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_fix_sgn(x) JAS_FIX_SGN(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_fix_round(x) JAS_FIX_ROUND(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_fix_floor(x) JAS_FIX_FLOOR(jpc_fix_t, JPC_FIX_FRACBITS, x) +#define jpc_fix_trunc(x) JAS_FIX_TRUNC(jpc_fix_t, JPC_FIX_FRACBITS, x) + +/******************************************************************************\ +* Extra macros for convenience. +\******************************************************************************/ + +/* Compute the sum of three fixed-point numbers. */ +#define jpc_fix_add3(x, y, z) jpc_fix_add(jpc_fix_add(x, y), z) + +#endif diff --git a/src/libjasper/jpc/jpc_flt.h b/src/libjasper/jpc/jpc_flt.h new file mode 100644 index 0000000..f942f3d --- /dev/null +++ b/src/libjasper/jpc/jpc_flt.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Floating-Point Class + * + * $Id: jpc_flt.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_FLT_H +#define JPC_FLT_H + +#include + +/* The code ought to be modified so this type is not used at all. */ +/* Very few places in the code rely on floating-point arithmetic, aside + from conversions in printf's. */ +typedef double jpc_flt_t; + +#endif diff --git a/src/libjasper/jpc/jpc_math.c b/src/libjasper/jpc/jpc_math.c new file mode 100644 index 0000000..f268554 --- /dev/null +++ b/src/libjasper/jpc/jpc_math.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Math Library + * + * $Id: jpc_math.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes +\******************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include "jpc_math.h" + +/******************************************************************************\ +* Miscellaneous Functions +\******************************************************************************/ + +/* Calculate the integer quantity floor(log2(x)), where x is a positive + integer. */ +int jpc_floorlog2(int x) +{ + int y; + + /* The argument must be positive. */ + assert(x > 0); + + y = 0; + while (x > 1) { + x >>= 1; + ++y; + } + return y; +} + +/* Calculate the bit position of the first leading one in a nonnegative + integer. */ +/* This function is the basically the same as ceillog2(x), except that the + allowable range for x is slightly different. */ +int jpc_firstone(int x) +{ + int n; + + /* The argument must be nonnegative. */ + assert(x >= 0); + + n = -1; + while (x > 0) { + x >>= 1; + ++n; + } + return n; +} diff --git a/src/libjasper/jpc/jpc_math.h b/src/libjasper/jpc/jpc_math.h new file mode 100644 index 0000000..e8e0978 --- /dev/null +++ b/src/libjasper/jpc/jpc_math.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +#ifndef JPC_MATH_H +#define JPC_MATH_H + +/******************************************************************************\ +* Includes +\******************************************************************************/ + +#include + +/******************************************************************************\ +* Macros +\******************************************************************************/ + +/* Compute the floor of the quotient of two integers. */ +#define JPC_FLOORDIV(x, y) ((x) / (y)) + +/* Compute the ceiling of the quotient of two integers. */ +#define JPC_CEILDIV(x, y) (((x) + (y) - 1) / (y)) + +/* Compute the floor of (x / 2^y). */ +#define JPC_FLOORDIVPOW2(x, y) ((x) >> (y)) + +/* Compute the ceiling of (x / 2^y). */ +#define JPC_CEILDIVPOW2(x, y) (((x) + (1 << (y)) - 1) >> (y)) + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Calculate the bit position of the first leading one in a nonnegative + integer. */ +int jpc_firstone(int x); + +/* Calculate the integer quantity floor(log2(x)), where x is a positive + integer. */ +int jpc_floorlog2(int x); + +#endif diff --git a/src/libjasper/jpc/jpc_mct.c b/src/libjasper/jpc/jpc_mct.c new file mode 100644 index 0000000..c5b33e4 --- /dev/null +++ b/src/libjasper/jpc/jpc_mct.c @@ -0,0 +1,291 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Multicomponent Transform Code + * + * $Id: jpc_mct.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include + +#include "jasper/jas_seq.h" + +#include "jpc_fix.h" +#include "jpc_mct.h" + +/******************************************************************************\ +* Code. +\******************************************************************************/ + +/* Compute the forward RCT. */ + +void jpc_rct(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2) +{ + int numrows; + int numcols; + int i; + int j; + jpc_fix_t *c0p; + jpc_fix_t *c1p; + jpc_fix_t *c2p; + + numrows = jas_matrix_numrows(c0); + numcols = jas_matrix_numcols(c0); + + /* All three matrices must have the same dimensions. */ + assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numcols(c1) == numcols + && jas_matrix_numrows(c2) == numrows && jas_matrix_numcols(c2) == numcols); + + for (i = 0; i < numrows; i++) { + c0p = jas_matrix_getref(c0, i, 0); + c1p = jas_matrix_getref(c1, i, 0); + c2p = jas_matrix_getref(c2, i, 0); + for (j = numcols; j > 0; --j) { + int r; + int g; + int b; + int y; + int u; + int v; + r = *c0p; + g = *c1p; + b = *c2p; + y = (r + (g << 1) + b) >> 2; + u = b - g; + v = r - g; + *c0p++ = y; + *c1p++ = u; + *c2p++ = v; + } + } +} + +/* Compute the inverse RCT. */ + +void jpc_irct(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2) +{ + int numrows; + int numcols; + int i; + int j; + jpc_fix_t *c0p; + jpc_fix_t *c1p; + jpc_fix_t *c2p; + + numrows = jas_matrix_numrows(c0); + numcols = jas_matrix_numcols(c0); + + /* All three matrices must have the same dimensions. */ + assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numcols(c1) == numcols + && jas_matrix_numrows(c2) == numrows && jas_matrix_numcols(c2) == numcols); + + for (i = 0; i < numrows; i++) { + c0p = jas_matrix_getref(c0, i, 0); + c1p = jas_matrix_getref(c1, i, 0); + c2p = jas_matrix_getref(c2, i, 0); + for (j = numcols; j > 0; --j) { + int r; + int g; + int b; + int y; + int u; + int v; + y = *c0p; + u = *c1p; + v = *c2p; + g = y - ((u + v) >> 2); + r = v + g; + b = u + g; + *c0p++ = r; + *c1p++ = g; + *c2p++ = b; + } + } +} + +void jpc_ict(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2) +{ + int numrows; + int numcols; + int i; + int j; + jpc_fix_t r; + jpc_fix_t g; + jpc_fix_t b; + jpc_fix_t y; + jpc_fix_t u; + jpc_fix_t v; + jpc_fix_t *c0p; + jpc_fix_t *c1p; + jpc_fix_t *c2p; + + numrows = jas_matrix_numrows(c0); + assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numrows(c2) == numrows); + numcols = jas_matrix_numcols(c0); + assert(jas_matrix_numcols(c1) == numcols && jas_matrix_numcols(c2) == numcols); + for (i = 0; i < numrows; ++i) { + c0p = jas_matrix_getref(c0, i, 0); + c1p = jas_matrix_getref(c1, i, 0); + c2p = jas_matrix_getref(c2, i, 0); + for (j = numcols; j > 0; --j) { + r = *c0p; + g = *c1p; + b = *c2p; + y = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(0.299), r), jpc_fix_mul(jpc_dbltofix(0.587), g), + jpc_fix_mul(jpc_dbltofix(0.114), b)); + u = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(-0.16875), r), jpc_fix_mul(jpc_dbltofix(-0.33126), g), + jpc_fix_mul(jpc_dbltofix(0.5), b)); + v = jpc_fix_add3(jpc_fix_mul(jpc_dbltofix(0.5), r), jpc_fix_mul(jpc_dbltofix(-0.41869), g), + jpc_fix_mul(jpc_dbltofix(-0.08131), b)); + *c0p++ = y; + *c1p++ = u; + *c2p++ = v; + } + } +} + +void jpc_iict(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2) +{ + int numrows; + int numcols; + int i; + int j; + jpc_fix_t r; + jpc_fix_t g; + jpc_fix_t b; + jpc_fix_t y; + jpc_fix_t u; + jpc_fix_t v; + jpc_fix_t *c0p; + jpc_fix_t *c1p; + jpc_fix_t *c2p; + + numrows = jas_matrix_numrows(c0); + assert(jas_matrix_numrows(c1) == numrows && jas_matrix_numrows(c2) == numrows); + numcols = jas_matrix_numcols(c0); + assert(jas_matrix_numcols(c1) == numcols && jas_matrix_numcols(c2) == numcols); + for (i = 0; i < numrows; ++i) { + c0p = jas_matrix_getref(c0, i, 0); + c1p = jas_matrix_getref(c1, i, 0); + c2p = jas_matrix_getref(c2, i, 0); + for (j = numcols; j > 0; --j) { + y = *c0p; + u = *c1p; + v = *c2p; + r = jpc_fix_add(y, jpc_fix_mul(jpc_dbltofix(1.402), v)); + g = jpc_fix_add3(y, jpc_fix_mul(jpc_dbltofix(-0.34413), u), + jpc_fix_mul(jpc_dbltofix(-0.71414), v)); + b = jpc_fix_add(y, jpc_fix_mul(jpc_dbltofix(1.772), u)); + *c0p++ = r; + *c1p++ = g; + *c2p++ = b; + } + } +} + +jpc_fix_t jpc_mct_getsynweight(int mctid, int cmptno) +{ + jpc_fix_t synweight; + + synweight = JPC_FIX_ONE; + switch (mctid) { + case JPC_MCT_RCT: + switch (cmptno) { + case 0: + synweight = jpc_dbltofix(sqrt(3.0)); + break; + case 1: + synweight = jpc_dbltofix(sqrt(0.6875)); + break; + case 2: + synweight = jpc_dbltofix(sqrt(0.6875)); + break; + } + break; + case JPC_MCT_ICT: + switch (cmptno) { + case 0: + synweight = jpc_dbltofix(sqrt(3.0000)); + break; + case 1: + synweight = jpc_dbltofix(sqrt(3.2584)); + break; + case 2: + synweight = jpc_dbltofix(sqrt(2.4755)); + break; + } + break; +#if 0 + default: + synweight = JPC_FIX_ONE; + break; +#endif + } + + return synweight; +} diff --git a/src/libjasper/jpc/jpc_mct.h b/src/libjasper/jpc/jpc_mct.h new file mode 100644 index 0000000..7d0176b --- /dev/null +++ b/src/libjasper/jpc/jpc_mct.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Multicomponent Transform Code + * + * $Id: jpc_mct.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_MCT_H +#define JPC_MCT_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_seq.h" +#include "jasper/jas_fix.h" + +/******************************************************************************\ +* Constants. +\******************************************************************************/ + +/* + * Multicomponent transform IDs. + */ + +#define JPC_MCT_NONE 0 +#define JPC_MCT_ICT 1 +#define JPC_MCT_RCT 2 + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Calculate the forward RCT. */ +void jpc_rct(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2); + +/* Calculate the inverse RCT. */ +void jpc_irct(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2); + +/* Calculate the forward ICT. */ +void jpc_ict(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2); + +/* Calculate the inverse ICT. */ +void jpc_iict(jas_matrix_t *c0, jas_matrix_t *c1, jas_matrix_t *c2); + +/* Get the synthesis weight associated with a particular component. */ +jpc_fix_t jpc_mct_getsynweight(int mctid, int cmptno); + +#endif diff --git a/src/libjasper/jpc/jpc_mqcod.c b/src/libjasper/jpc/jpc_mqcod.c new file mode 100644 index 0000000..f6149b3 --- /dev/null +++ b/src/libjasper/jpc/jpc_mqcod.c @@ -0,0 +1,179 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * MQ Arithmetic Coder + * + * $Id: jpc_mqcod.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_malloc.h" + +#include "jpc_mqcod.h" + +/******************************************************************************\ +* Data. +\******************************************************************************/ + +/* MQ coder per-state information. */ + +jpc_mqstate_t jpc_mqstates[47 * 2] = { + {0x5601, 0, &jpc_mqstates[ 2], &jpc_mqstates[ 3]}, + {0x5601, 1, &jpc_mqstates[ 3], &jpc_mqstates[ 2]}, + {0x3401, 0, &jpc_mqstates[ 4], &jpc_mqstates[12]}, + {0x3401, 1, &jpc_mqstates[ 5], &jpc_mqstates[13]}, + {0x1801, 0, &jpc_mqstates[ 6], &jpc_mqstates[18]}, + {0x1801, 1, &jpc_mqstates[ 7], &jpc_mqstates[19]}, + {0x0ac1, 0, &jpc_mqstates[ 8], &jpc_mqstates[24]}, + {0x0ac1, 1, &jpc_mqstates[ 9], &jpc_mqstates[25]}, + {0x0521, 0, &jpc_mqstates[10], &jpc_mqstates[58]}, + {0x0521, 1, &jpc_mqstates[11], &jpc_mqstates[59]}, + {0x0221, 0, &jpc_mqstates[76], &jpc_mqstates[66]}, + {0x0221, 1, &jpc_mqstates[77], &jpc_mqstates[67]}, + {0x5601, 0, &jpc_mqstates[14], &jpc_mqstates[13]}, + {0x5601, 1, &jpc_mqstates[15], &jpc_mqstates[12]}, + {0x5401, 0, &jpc_mqstates[16], &jpc_mqstates[28]}, + {0x5401, 1, &jpc_mqstates[17], &jpc_mqstates[29]}, + {0x4801, 0, &jpc_mqstates[18], &jpc_mqstates[28]}, + {0x4801, 1, &jpc_mqstates[19], &jpc_mqstates[29]}, + {0x3801, 0, &jpc_mqstates[20], &jpc_mqstates[28]}, + {0x3801, 1, &jpc_mqstates[21], &jpc_mqstates[29]}, + {0x3001, 0, &jpc_mqstates[22], &jpc_mqstates[34]}, + {0x3001, 1, &jpc_mqstates[23], &jpc_mqstates[35]}, + {0x2401, 0, &jpc_mqstates[24], &jpc_mqstates[36]}, + {0x2401, 1, &jpc_mqstates[25], &jpc_mqstates[37]}, + {0x1c01, 0, &jpc_mqstates[26], &jpc_mqstates[40]}, + {0x1c01, 1, &jpc_mqstates[27], &jpc_mqstates[41]}, + {0x1601, 0, &jpc_mqstates[58], &jpc_mqstates[42]}, + {0x1601, 1, &jpc_mqstates[59], &jpc_mqstates[43]}, + {0x5601, 0, &jpc_mqstates[30], &jpc_mqstates[29]}, + {0x5601, 1, &jpc_mqstates[31], &jpc_mqstates[28]}, + {0x5401, 0, &jpc_mqstates[32], &jpc_mqstates[28]}, + {0x5401, 1, &jpc_mqstates[33], &jpc_mqstates[29]}, + {0x5101, 0, &jpc_mqstates[34], &jpc_mqstates[30]}, + {0x5101, 1, &jpc_mqstates[35], &jpc_mqstates[31]}, + {0x4801, 0, &jpc_mqstates[36], &jpc_mqstates[32]}, + {0x4801, 1, &jpc_mqstates[37], &jpc_mqstates[33]}, + {0x3801, 0, &jpc_mqstates[38], &jpc_mqstates[34]}, + {0x3801, 1, &jpc_mqstates[39], &jpc_mqstates[35]}, + {0x3401, 0, &jpc_mqstates[40], &jpc_mqstates[36]}, + {0x3401, 1, &jpc_mqstates[41], &jpc_mqstates[37]}, + {0x3001, 0, &jpc_mqstates[42], &jpc_mqstates[38]}, + {0x3001, 1, &jpc_mqstates[43], &jpc_mqstates[39]}, + {0x2801, 0, &jpc_mqstates[44], &jpc_mqstates[38]}, + {0x2801, 1, &jpc_mqstates[45], &jpc_mqstates[39]}, + {0x2401, 0, &jpc_mqstates[46], &jpc_mqstates[40]}, + {0x2401, 1, &jpc_mqstates[47], &jpc_mqstates[41]}, + {0x2201, 0, &jpc_mqstates[48], &jpc_mqstates[42]}, + {0x2201, 1, &jpc_mqstates[49], &jpc_mqstates[43]}, + {0x1c01, 0, &jpc_mqstates[50], &jpc_mqstates[44]}, + {0x1c01, 1, &jpc_mqstates[51], &jpc_mqstates[45]}, + {0x1801, 0, &jpc_mqstates[52], &jpc_mqstates[46]}, + {0x1801, 1, &jpc_mqstates[53], &jpc_mqstates[47]}, + {0x1601, 0, &jpc_mqstates[54], &jpc_mqstates[48]}, + {0x1601, 1, &jpc_mqstates[55], &jpc_mqstates[49]}, + {0x1401, 0, &jpc_mqstates[56], &jpc_mqstates[50]}, + {0x1401, 1, &jpc_mqstates[57], &jpc_mqstates[51]}, + {0x1201, 0, &jpc_mqstates[58], &jpc_mqstates[52]}, + {0x1201, 1, &jpc_mqstates[59], &jpc_mqstates[53]}, + {0x1101, 0, &jpc_mqstates[60], &jpc_mqstates[54]}, + {0x1101, 1, &jpc_mqstates[61], &jpc_mqstates[55]}, + {0x0ac1, 0, &jpc_mqstates[62], &jpc_mqstates[56]}, + {0x0ac1, 1, &jpc_mqstates[63], &jpc_mqstates[57]}, + {0x09c1, 0, &jpc_mqstates[64], &jpc_mqstates[58]}, + {0x09c1, 1, &jpc_mqstates[65], &jpc_mqstates[59]}, + {0x08a1, 0, &jpc_mqstates[66], &jpc_mqstates[60]}, + {0x08a1, 1, &jpc_mqstates[67], &jpc_mqstates[61]}, + {0x0521, 0, &jpc_mqstates[68], &jpc_mqstates[62]}, + {0x0521, 1, &jpc_mqstates[69], &jpc_mqstates[63]}, + {0x0441, 0, &jpc_mqstates[70], &jpc_mqstates[64]}, + {0x0441, 1, &jpc_mqstates[71], &jpc_mqstates[65]}, + {0x02a1, 0, &jpc_mqstates[72], &jpc_mqstates[66]}, + {0x02a1, 1, &jpc_mqstates[73], &jpc_mqstates[67]}, + {0x0221, 0, &jpc_mqstates[74], &jpc_mqstates[68]}, + {0x0221, 1, &jpc_mqstates[75], &jpc_mqstates[69]}, + {0x0141, 0, &jpc_mqstates[76], &jpc_mqstates[70]}, + {0x0141, 1, &jpc_mqstates[77], &jpc_mqstates[71]}, + {0x0111, 0, &jpc_mqstates[78], &jpc_mqstates[72]}, + {0x0111, 1, &jpc_mqstates[79], &jpc_mqstates[73]}, + {0x0085, 0, &jpc_mqstates[80], &jpc_mqstates[74]}, + {0x0085, 1, &jpc_mqstates[81], &jpc_mqstates[75]}, + {0x0049, 0, &jpc_mqstates[82], &jpc_mqstates[76]}, + {0x0049, 1, &jpc_mqstates[83], &jpc_mqstates[77]}, + {0x0025, 0, &jpc_mqstates[84], &jpc_mqstates[78]}, + {0x0025, 1, &jpc_mqstates[85], &jpc_mqstates[79]}, + {0x0015, 0, &jpc_mqstates[86], &jpc_mqstates[80]}, + {0x0015, 1, &jpc_mqstates[87], &jpc_mqstates[81]}, + {0x0009, 0, &jpc_mqstates[88], &jpc_mqstates[82]}, + {0x0009, 1, &jpc_mqstates[89], &jpc_mqstates[83]}, + {0x0005, 0, &jpc_mqstates[90], &jpc_mqstates[84]}, + {0x0005, 1, &jpc_mqstates[91], &jpc_mqstates[85]}, + {0x0001, 0, &jpc_mqstates[90], &jpc_mqstates[86]}, + {0x0001, 1, &jpc_mqstates[91], &jpc_mqstates[87]}, + {0x5601, 0, &jpc_mqstates[92], &jpc_mqstates[92]}, + {0x5601, 1, &jpc_mqstates[93], &jpc_mqstates[93]}, +}; diff --git a/src/libjasper/jpc/jpc_mqcod.h b/src/libjasper/jpc/jpc_mqcod.h new file mode 100644 index 0000000..ac32b8a --- /dev/null +++ b/src/libjasper/jpc/jpc_mqcod.h @@ -0,0 +1,124 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * MQ Arithmetic Coder + * + * $Id: jpc_mqcod.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_MQCOD_H +#define JPC_MQCOD_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_types.h" + +/******************************************************************************\ +* Types. +\******************************************************************************/ + +/* + * MQ coder context information. + */ + +typedef struct { + + /* The most probable symbol (MPS). */ + int mps; + + /* The state index. */ + int_fast16_t ind; + +} jpc_mqctx_t; + +/* + * MQ coder state table entry. + */ + +typedef struct jpc_mqstate_s { + + /* The Qe value. */ + uint_fast16_t qeval; + + /* The MPS. */ + int mps; + + /* The NMPS state. */ + struct jpc_mqstate_s *nmps; + + /* The NLPS state. */ + struct jpc_mqstate_s *nlps; + +} jpc_mqstate_t; + +/******************************************************************************\ +* Data. +\******************************************************************************/ + +/* The state table for the MQ coder. */ +extern jpc_mqstate_t jpc_mqstates[]; + +#endif diff --git a/src/libjasper/jpc/jpc_mqdec.c b/src/libjasper/jpc/jpc_mqdec.c new file mode 100644 index 0000000..74e6b33 --- /dev/null +++ b/src/libjasper/jpc/jpc_mqdec.c @@ -0,0 +1,306 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * MQ Arithmetic Decoder + * + * $Id: jpc_mqdec.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jasper/jas_types.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_math.h" +#include "jasper/jas_debug.h" + +#include "jpc_mqdec.h" + +/******************************************************************************\ +* Local macros. +\******************************************************************************/ + +#if defined(DEBUG) +#define MQDEC_CALL(n, x) \ + ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0)) +#else +#define MQDEC_CALL(n, x) +#endif + +/******************************************************************************\ +* Local function prototypes. +\******************************************************************************/ + +static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec); + +/******************************************************************************\ +* Code for creation and destruction of a MQ decoder. +\******************************************************************************/ + +/* Create a MQ decoder. */ +jpc_mqdec_t *jpc_mqdec_create(int maxctxs, jas_stream_t *in) +{ + jpc_mqdec_t *mqdec; + + /* There must be at least one context. */ + assert(maxctxs > 0); + + /* Allocate memory for the MQ decoder. */ + if (!(mqdec = jas_malloc(sizeof(jpc_mqdec_t)))) { + goto error; + } + mqdec->in = in; + mqdec->maxctxs = maxctxs; + /* Allocate memory for the per-context state information. */ + if (!(mqdec->ctxs = jas_malloc(mqdec->maxctxs * sizeof(jpc_mqstate_t *)))) { + goto error; + } + /* Set the current context to the first context. */ + mqdec->curctx = mqdec->ctxs; + + /* If an input stream has been associated with the MQ decoder, + initialize the decoder state from the stream. */ + if (mqdec->in) { + jpc_mqdec_init(mqdec); + } + /* Initialize the per-context state information. */ + jpc_mqdec_setctxs(mqdec, 0, 0); + + return mqdec; + +error: + /* Oops... Something has gone wrong. */ + if (mqdec) { + jpc_mqdec_destroy(mqdec); + } + return 0; +} + +/* Destroy a MQ decoder. */ +void jpc_mqdec_destroy(jpc_mqdec_t *mqdec) +{ + if (mqdec->ctxs) { + jas_free(mqdec->ctxs); + } + jas_free(mqdec); +} + +/******************************************************************************\ +* Code for initialization of a MQ decoder. +\******************************************************************************/ + +/* Initialize the state of a MQ decoder. */ + +void jpc_mqdec_init(jpc_mqdec_t *mqdec) +{ + int c; + + mqdec->eof = 0; + mqdec->creg = 0; + /* Get the next byte from the input stream. */ + if ((c = jas_stream_getc(mqdec->in)) == EOF) { + /* We have encountered an I/O error or EOF. */ + c = 0xff; + mqdec->eof = 1; + } + mqdec->inbuffer = c; + mqdec->creg += mqdec->inbuffer << 16; + jpc_mqdec_bytein(mqdec); + mqdec->creg <<= 7; + mqdec->ctreg -= 7; + mqdec->areg = 0x8000; +} + +/* Set the input stream for a MQ decoder. */ + +void jpc_mqdec_setinput(jpc_mqdec_t *mqdec, jas_stream_t *in) +{ + mqdec->in = in; +} + +/* Initialize one or more contexts. */ + +void jpc_mqdec_setctxs(jpc_mqdec_t *mqdec, int numctxs, jpc_mqctx_t *ctxs) +{ + jpc_mqstate_t **ctx; + int n; + + ctx = mqdec->ctxs; + n = JAS_MIN(mqdec->maxctxs, numctxs); + while (--n >= 0) { + *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps]; + ++ctx; + ++ctxs; + } + n = mqdec->maxctxs - numctxs; + while (--n >= 0) { + *ctx = &jpc_mqstates[0]; + ++ctx; + } +} + +/* Initialize a context. */ + +void jpc_mqdec_setctx(jpc_mqdec_t *mqdec, int ctxno, jpc_mqctx_t *ctx) +{ + jpc_mqstate_t **ctxi; + ctxi = &mqdec->ctxs[ctxno]; + *ctxi = &jpc_mqstates[2 * ctx->ind + ctx->mps]; +} + +/******************************************************************************\ +* Code for decoding a bit. +\******************************************************************************/ + +/* Decode a bit. */ + +int jpc_mqdec_getbit_func(register jpc_mqdec_t *mqdec) +{ + int bit; + JAS_DBGLOG(100, ("jpc_mqdec_getbit_func(%p)\n", mqdec)); + MQDEC_CALL(100, jpc_mqdec_dump(mqdec, stderr)); + bit = jpc_mqdec_getbit_macro(mqdec); + MQDEC_CALL(100, jpc_mqdec_dump(mqdec, stderr)); + JAS_DBGLOG(100, ("ctx = %d, decoded %d\n", mqdec->curctx - + mqdec->ctxs, bit)); + return bit; +} + +/* Apply MPS_EXCHANGE algorithm (with RENORMD). */ +int jpc_mqdec_mpsexchrenormd(register jpc_mqdec_t *mqdec) +{ + int ret; + register jpc_mqstate_t *state = *mqdec->curctx; + jpc_mqdec_mpsexchange(mqdec->areg, state->qeval, mqdec->curctx, ret); + jpc_mqdec_renormd(mqdec->areg, mqdec->creg, mqdec->ctreg, mqdec->in, + mqdec->eof, mqdec->inbuffer); + return ret; +} + +/* Apply LPS_EXCHANGE algorithm (with RENORMD). */ +int jpc_mqdec_lpsexchrenormd(register jpc_mqdec_t *mqdec) +{ + int ret; + register jpc_mqstate_t *state = *mqdec->curctx; + jpc_mqdec_lpsexchange(mqdec->areg, state->qeval, mqdec->curctx, ret); + jpc_mqdec_renormd(mqdec->areg, mqdec->creg, mqdec->ctreg, mqdec->in, + mqdec->eof, mqdec->inbuffer); + return ret; +} + +/******************************************************************************\ +* Support code. +\******************************************************************************/ + +/* Apply the BYTEIN algorithm. */ +static void jpc_mqdec_bytein(jpc_mqdec_t *mqdec) +{ + int c; + unsigned char prevbuf; + + if (!mqdec->eof) { + if ((c = jas_stream_getc(mqdec->in)) == EOF) { + mqdec->eof = 1; + c = 0xff; + } + prevbuf = mqdec->inbuffer; + mqdec->inbuffer = c; + if (prevbuf == 0xff) { + if (c > 0x8f) { + mqdec->creg += 0xff00; + mqdec->ctreg = 8; + } else { + mqdec->creg += c << 9; + mqdec->ctreg = 7; + } + } else { + mqdec->creg += c << 8; + mqdec->ctreg = 8; + } + } else { + mqdec->creg += 0xff00; + mqdec->ctreg = 8; + } +} + +/******************************************************************************\ +* Code for debugging. +\******************************************************************************/ + +/* Dump a MQ decoder to a stream for debugging. */ + +void jpc_mqdec_dump(jpc_mqdec_t *mqdec, FILE *out) +{ + fprintf(out, "MQDEC A = %08lx, C = %08lx, CT=%08lx, ", + (unsigned long) mqdec->areg, (unsigned long) mqdec->creg, + (unsigned long) mqdec->ctreg); + fprintf(out, "CTX = %d, ", mqdec->curctx - mqdec->ctxs); + fprintf(out, "IND %d, MPS %d, QEVAL %x\n", *mqdec->curctx - + jpc_mqstates, (*mqdec->curctx)->mps, (*mqdec->curctx)->qeval); +} diff --git a/src/libjasper/jpc/jpc_mqdec.h b/src/libjasper/jpc/jpc_mqdec.h new file mode 100644 index 0000000..bac7b51 --- /dev/null +++ b/src/libjasper/jpc/jpc_mqdec.h @@ -0,0 +1,271 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * MQ Arithmetic Decoder + * + * $Id: jpc_mqdec.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_MQDEC_H +#define JPC_MQDEC_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_types.h" +#include "jasper/jas_stream.h" + +#include "jpc_mqcod.h" + +/******************************************************************************\ +* Types. +\******************************************************************************/ + +/* MQ arithmetic decoder. */ + +typedef struct { + + /* The C register. */ + uint_fast32_t creg; + + /* The A register. */ + uint_fast32_t areg; + + /* The CT register. */ + uint_fast32_t ctreg; + + /* The current context. */ + jpc_mqstate_t **curctx; + + /* The per-context information. */ + jpc_mqstate_t **ctxs; + + /* The maximum number of contexts. */ + int maxctxs; + + /* The stream from which to read data. */ + jas_stream_t *in; + + /* The last character read. */ + uchar inbuffer; + + /* The EOF indicator. */ + int eof; + +} jpc_mqdec_t; + +/******************************************************************************\ +* Functions/macros for construction and destruction. +\******************************************************************************/ + +/* Create a MQ decoder. */ +jpc_mqdec_t *jpc_mqdec_create(int maxctxs, jas_stream_t *in); + +/* Destroy a MQ decoder. */ +void jpc_mqdec_destroy(jpc_mqdec_t *dec); + +/******************************************************************************\ +* Functions/macros for initialization. +\******************************************************************************/ + +/* Set the input stream associated with a MQ decoder. */ +void jpc_mqdec_setinput(jpc_mqdec_t *dec, jas_stream_t *in); + +/* Initialize a MQ decoder. */ +void jpc_mqdec_init(jpc_mqdec_t *dec); + +/******************************************************************************\ +* Functions/macros for manipulating contexts. +\******************************************************************************/ + +/* Set the current context for a MQ decoder. */ +#define jpc_mqdec_setcurctx(dec, ctxno) \ + ((mqdec)->curctx = &(mqdec)->ctxs[ctxno]); + +/* Set the state information for a particular context of a MQ decoder. */ +void jpc_mqdec_setctx(jpc_mqdec_t *dec, int ctxno, jpc_mqctx_t *ctx); + +/* Set the state information for all contexts of a MQ decoder. */ +void jpc_mqdec_setctxs(jpc_mqdec_t *dec, int numctxs, jpc_mqctx_t *ctxs); + +/******************************************************************************\ +* Functions/macros for decoding bits. +\******************************************************************************/ + +/* Decode a symbol. */ +#if !defined(DEBUG) +#define jpc_mqdec_getbit(dec) \ + jpc_mqdec_getbit_macro(dec) +#else +#define jpc_mqdec_getbit(dec) \ + jpc_mqdec_getbit_func(dec) +#endif + +/* Decode a symbol (assuming an unskewed probability distribution). */ +#if !defined(DEBUG) +#define jpc_mqdec_getbitnoskew(dec) \ + jpc_mqdec_getbit_macro(dec) +#else +#define jpc_mqdec_getbitnoskew(dec) \ + jpc_mqdec_getbit_func(dec) +#endif + +/******************************************************************************\ +* Functions/macros for debugging. +\******************************************************************************/ + +/* Dump the MQ decoder state for debugging. */ +void jpc_mqdec_dump(jpc_mqdec_t *dec, FILE *out); + +/******************************************************************************\ +* EVERYTHING BELOW THIS POINT IS IMPLEMENTATION SPECIFIC AND NOT PART OF THE +* APPLICATION INTERFACE. DO NOT RELY ON ANY OF THE INTERNAL FUNCTIONS/MACROS +* GIVEN BELOW. +\******************************************************************************/ + +#define jpc_mqdec_getbit_macro(dec) \ + ((((dec)->areg -= (*(dec)->curctx)->qeval), \ + (dec)->creg >> 16 >= (*(dec)->curctx)->qeval) ? \ + ((((dec)->creg -= (*(dec)->curctx)->qeval << 16), \ + (dec)->areg & 0x8000) ? (*(dec)->curctx)->mps : \ + jpc_mqdec_mpsexchrenormd(dec)) : \ + jpc_mqdec_lpsexchrenormd(dec)) + +#define jpc_mqdec_mpsexchange(areg, delta, curctx, bit) \ +{ \ + if ((areg) < (delta)) { \ + register jpc_mqstate_t *state = *(curctx); \ + /* LPS decoded. */ \ + (bit) = state->mps ^ 1; \ + *(curctx) = state->nlps; \ + } else { \ + register jpc_mqstate_t *state = *(curctx); \ + /* MPS decoded. */ \ + (bit) = state->mps; \ + *(curctx) = state->nmps; \ + } \ +} + +#define jpc_mqdec_lpsexchange(areg, delta, curctx, bit) \ +{ \ + if ((areg) >= (delta)) { \ + register jpc_mqstate_t *state = *(curctx); \ + (areg) = (delta); \ + (bit) = state->mps ^ 1; \ + *(curctx) = state->nlps; \ + } else { \ + register jpc_mqstate_t *state = *(curctx); \ + (areg) = (delta); \ + (bit) = state->mps; \ + *(curctx) = state->nmps; \ + } \ +} + +#define jpc_mqdec_renormd(areg, creg, ctreg, in, eof, inbuf) \ +{ \ + do { \ + if (!(ctreg)) { \ + jpc_mqdec_bytein2(creg, ctreg, in, eof, inbuf); \ + } \ + (areg) <<= 1; \ + (creg) <<= 1; \ + --(ctreg); \ + } while (!((areg) & 0x8000)); \ +} + +#define jpc_mqdec_bytein2(creg, ctreg, in, eof, inbuf) \ +{ \ + int c; \ + unsigned char prevbuf; \ + if (!(eof)) { \ + if ((c = jas_stream_getc(in)) == EOF) { \ + (eof) = 1; \ + c = 0xff; \ + } \ + prevbuf = (inbuf); \ + (inbuf) = c; \ + if (prevbuf == 0xff) { \ + if (c > 0x8f) { \ + (creg) += 0xff00; \ + (ctreg) = 8; \ + } else { \ + (creg) += c << 9; \ + (ctreg) = 7; \ + } \ + } else { \ + (creg) += c << 8; \ + (ctreg) = 8; \ + } \ + } else { \ + (creg) += 0xff00; \ + (ctreg) = 8; \ + } \ +} + +int jpc_mqdec_getbit_func(jpc_mqdec_t *dec); +int jpc_mqdec_mpsexchrenormd(jpc_mqdec_t *dec); +int jpc_mqdec_lpsexchrenormd(jpc_mqdec_t *dec); + +#endif diff --git a/src/libjasper/jpc/jpc_mqenc.c b/src/libjasper/jpc/jpc_mqenc.c new file mode 100644 index 0000000..e3f4bea --- /dev/null +++ b/src/libjasper/jpc/jpc_mqenc.c @@ -0,0 +1,392 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * MQ Arithmetic Encoder + * + * $Id: jpc_mqenc.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include + +#include "jasper/jas_stream.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_math.h" +#include "jasper/jas_debug.h" + +#include "jpc_mqenc.h" + +/******************************************************************************\ +* Macros +\******************************************************************************/ + +#if defined(DEBUG) +#define JPC_MQENC_CALL(n, x) \ + ((jas_getdbglevel() >= (n)) ? ((void)(x)) : ((void)0)) +#else +#define JPC_MQENC_CALL(n, x) +#endif + +#define jpc_mqenc_codemps9(areg, creg, ctreg, curctx, enc) \ +{ \ + jpc_mqstate_t *state = *(curctx); \ + (areg) -= state->qeval; \ + if (!((areg) & 0x8000)) { \ + if ((areg) < state->qeval) { \ + (areg) = state->qeval; \ + } else { \ + (creg) += state->qeval; \ + } \ + *(curctx) = state->nmps; \ + jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \ + } else { \ + (creg) += state->qeval; \ + } \ +} + +#define jpc_mqenc_codelps2(areg, creg, ctreg, curctx, enc) \ +{ \ + jpc_mqstate_t *state = *(curctx); \ + (areg) -= state->qeval; \ + if ((areg) < state->qeval) { \ + (creg) += state->qeval; \ + } else { \ + (areg) = state->qeval; \ + } \ + *(curctx) = state->nlps; \ + jpc_mqenc_renorme((areg), (creg), (ctreg), (enc)); \ +} + +#define jpc_mqenc_renorme(areg, creg, ctreg, enc) \ +{ \ + do { \ + (areg) <<= 1; \ + (creg) <<= 1; \ + if (!--(ctreg)) { \ + jpc_mqenc_byteout((areg), (creg), (ctreg), (enc)); \ + } \ + } while (!((areg) & 0x8000)); \ +} + +#define jpc_mqenc_byteout(areg, creg, ctreg, enc) \ +{ \ + if ((enc)->outbuf != 0xff) { \ + if ((creg) & 0x8000000) { \ + if (++((enc)->outbuf) == 0xff) { \ + (creg) &= 0x7ffffff; \ + jpc_mqenc_byteout2(enc); \ + enc->outbuf = ((creg) >> 20) & 0xff; \ + (creg) &= 0xfffff; \ + (ctreg) = 7; \ + } else { \ + jpc_mqenc_byteout2(enc); \ + enc->outbuf = ((creg) >> 19) & 0xff; \ + (creg) &= 0x7ffff; \ + (ctreg) = 8; \ + } \ + } else { \ + jpc_mqenc_byteout2(enc); \ + (enc)->outbuf = ((creg) >> 19) & 0xff; \ + (creg) &= 0x7ffff; \ + (ctreg) = 8; \ + } \ + } else { \ + jpc_mqenc_byteout2(enc); \ + (enc)->outbuf = ((creg) >> 20) & 0xff; \ + (creg) &= 0xfffff; \ + (ctreg) = 7; \ + } \ +} + +#define jpc_mqenc_byteout2(enc) \ +{ \ + if (enc->outbuf >= 0) { \ + if (jas_stream_putc(enc->out, (unsigned char)enc->outbuf) == EOF) { \ + enc->err |= 1; \ + } \ + } \ + enc->lastbyte = enc->outbuf; \ +} + +/******************************************************************************\ +* Local function protoypes. +\******************************************************************************/ + +static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc); + +/******************************************************************************\ +* Code for creation and destruction of encoder. +\******************************************************************************/ + +/* Create a MQ encoder. */ + +jpc_mqenc_t *jpc_mqenc_create(int maxctxs, jas_stream_t *out) +{ + jpc_mqenc_t *mqenc; + + /* Allocate memory for the MQ encoder. */ + if (!(mqenc = jas_malloc(sizeof(jpc_mqenc_t)))) { + goto error; + } + mqenc->out = out; + mqenc->maxctxs = maxctxs; + + /* Allocate memory for the per-context state information. */ + if (!(mqenc->ctxs = jas_malloc(mqenc->maxctxs * sizeof(jpc_mqstate_t *)))) { + goto error; + } + + /* Set the current context to the first one. */ + mqenc->curctx = mqenc->ctxs; + + jpc_mqenc_init(mqenc); + + /* Initialize the per-context state information to something sane. */ + jpc_mqenc_setctxs(mqenc, 0, 0); + + return mqenc; + +error: + if (mqenc) { + jpc_mqenc_destroy(mqenc); + } + return 0; +} + +/* Destroy a MQ encoder. */ + +void jpc_mqenc_destroy(jpc_mqenc_t *mqenc) +{ + if (mqenc->ctxs) { + jas_free(mqenc->ctxs); + } + jas_free(mqenc); +} + +/******************************************************************************\ +* State initialization code. +\******************************************************************************/ + +/* Initialize the coding state of a MQ encoder. */ + +void jpc_mqenc_init(jpc_mqenc_t *mqenc) +{ + mqenc->areg = 0x8000; + mqenc->outbuf = -1; + mqenc->creg = 0; + mqenc->ctreg = 12; + mqenc->lastbyte = -1; + mqenc->err = 0; +} + +/* Initialize one or more contexts. */ + +void jpc_mqenc_setctxs(jpc_mqenc_t *mqenc, int numctxs, jpc_mqctx_t *ctxs) +{ + jpc_mqstate_t **ctx; + int n; + + ctx = mqenc->ctxs; + n = JAS_MIN(mqenc->maxctxs, numctxs); + while (--n >= 0) { + *ctx = &jpc_mqstates[2 * ctxs->ind + ctxs->mps]; + ++ctx; + ++ctxs; + } + n = mqenc->maxctxs - numctxs; + while (--n >= 0) { + *ctx = &jpc_mqstates[0]; + ++ctx; + } + +} + +/* Get the coding state for a MQ encoder. */ + +void jpc_mqenc_getstate(jpc_mqenc_t *mqenc, jpc_mqencstate_t *state) +{ + state->areg = mqenc->areg; + state->creg = mqenc->creg; + state->ctreg = mqenc->ctreg; + state->lastbyte = mqenc->lastbyte; +} + +/******************************************************************************\ +* Code for coding symbols. +\******************************************************************************/ + +/* Encode a bit. */ + +int jpc_mqenc_putbit_func(jpc_mqenc_t *mqenc, int bit) +{ + const jpc_mqstate_t *state; + JAS_DBGLOG(100, ("jpc_mqenc_putbit(%p, %d)\n", mqenc, bit)); + JPC_MQENC_CALL(100, jpc_mqenc_dump(mqenc, stderr)); + + state = *(mqenc->curctx); + + if (state->mps == bit) { + /* Apply the CODEMPS algorithm as defined in the standard. */ + mqenc->areg -= state->qeval; + if (!(mqenc->areg & 0x8000)) { + jpc_mqenc_codemps2(mqenc); + } else { + mqenc->creg += state->qeval; + } + } else { + /* Apply the CODELPS algorithm as defined in the standard. */ + jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc); + } + + return jpc_mqenc_error(mqenc) ? (-1) : 0; +} + +int jpc_mqenc_codemps2(jpc_mqenc_t *mqenc) +{ + /* Note: This function only performs part of the work associated with + the CODEMPS algorithm from the standard. Some of the work is also + performed by the caller. */ + + jpc_mqstate_t *state = *(mqenc->curctx); + if (mqenc->areg < state->qeval) { + mqenc->areg = state->qeval; + } else { + mqenc->creg += state->qeval; + } + *mqenc->curctx = state->nmps; + jpc_mqenc_renorme(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + return jpc_mqenc_error(mqenc) ? (-1) : 0; +} + +int jpc_mqenc_codelps(jpc_mqenc_t *mqenc) +{ + jpc_mqenc_codelps2(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc->curctx, mqenc); + return jpc_mqenc_error(mqenc) ? (-1) : 0; +} + +/******************************************************************************\ +* Miscellaneous code. +\******************************************************************************/ + +/* Terminate the code word. */ + +int jpc_mqenc_flush(jpc_mqenc_t *mqenc, int termmode) +{ + int_fast16_t k; + + switch (termmode) { + case JPC_MQENC_PTERM: + k = 11 - mqenc->ctreg + 1; + while (k > 0) { + mqenc->creg <<= mqenc->ctreg; + mqenc->ctreg = 0; + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, + mqenc); + k -= mqenc->ctreg; + } + if (mqenc->outbuf != 0xff) { + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + } + break; + case JPC_MQENC_DEFTERM: + jpc_mqenc_setbits(mqenc); + mqenc->creg <<= mqenc->ctreg; + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + mqenc->creg <<= mqenc->ctreg; + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + if (mqenc->outbuf != 0xff) { + jpc_mqenc_byteout(mqenc->areg, mqenc->creg, mqenc->ctreg, mqenc); + } + break; + default: + abort(); + break; + } + return 0; +} + +static void jpc_mqenc_setbits(jpc_mqenc_t *mqenc) +{ + uint_fast32_t tmp = mqenc->creg + mqenc->areg; + mqenc->creg |= 0xffff; + if (mqenc->creg >= tmp) { + mqenc->creg -= 0x8000; + } +} + +/* Dump a MQ encoder to a stream for debugging. */ + +int jpc_mqenc_dump(jpc_mqenc_t *mqenc, FILE *out) +{ + fprintf(out, "AREG = %08x, CREG = %08x, CTREG = %d\n", + mqenc->areg, mqenc->creg, mqenc->ctreg); + fprintf(out, "IND = %02d, MPS = %d, QEVAL = %04x\n", + *mqenc->curctx - jpc_mqstates, (*mqenc->curctx)->mps, + (*mqenc->curctx)->qeval); + return 0; +} diff --git a/src/libjasper/jpc/jpc_mqenc.h b/src/libjasper/jpc/jpc_mqenc.h new file mode 100644 index 0000000..248ed43 --- /dev/null +++ b/src/libjasper/jpc/jpc_mqenc.h @@ -0,0 +1,236 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * MQ Arithmetic Encoder + * + * $Id: jpc_mqenc.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_MQENC_H +#define JPC_MQENC_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_types.h" +#include "jasper/jas_stream.h" + +#include "jpc_mqcod.h" + +/******************************************************************************\ +* Constants. +\******************************************************************************/ + +/* + * Termination modes. + */ + +#define JPC_MQENC_DEFTERM 0 /* default termination */ +#define JPC_MQENC_PTERM 1 /* predictable termination */ + +/******************************************************************************\ +* Types. +\******************************************************************************/ + +/* MQ arithmetic encoder class. */ + +typedef struct { + + /* The C register. */ + uint_fast32_t creg; + + /* The A register. */ + uint_fast32_t areg; + + /* The CT register. */ + uint_fast32_t ctreg; + + /* The maximum number of contexts. */ + int maxctxs; + + /* The per-context information. */ + jpc_mqstate_t **ctxs; + + /* The current context. */ + jpc_mqstate_t **curctx; + + /* The stream for encoder output. */ + jas_stream_t *out; + + /* The byte buffer (i.e., the B variable in the standard). */ + int_fast16_t outbuf; + + /* The last byte output. */ + int_fast16_t lastbyte; + + /* The error indicator. */ + int err; + +} jpc_mqenc_t; + +/* MQ arithmetic encoder state information. */ + +typedef struct { + + /* The A register. */ + unsigned areg; + + /* The C register. */ + unsigned creg; + + /* The CT register. */ + unsigned ctreg; + + /* The last byte output by the encoder. */ + int lastbyte; + +} jpc_mqencstate_t; + +/******************************************************************************\ +* Functions/macros for construction and destruction. +\******************************************************************************/ + +/* Create a MQ encoder. */ +jpc_mqenc_t *jpc_mqenc_create(int maxctxs, jas_stream_t *out); + +/* Destroy a MQ encoder. */ +void jpc_mqenc_destroy(jpc_mqenc_t *enc); + +/******************************************************************************\ +* Functions/macros for initialization. +\******************************************************************************/ + +/* Initialize a MQ encoder. */ +void jpc_mqenc_init(jpc_mqenc_t *enc); + +/******************************************************************************\ +* Functions/macros for context manipulation. +\******************************************************************************/ + +/* Set the current context. */ +#define jpc_mqenc_setcurctx(enc, ctxno) \ + ((enc)->curctx = &(enc)->ctxs[ctxno]); + +/* Set the state information for a particular context. */ +void jpc_mqenc_setctx(jpc_mqenc_t *enc, int ctxno, jpc_mqctx_t *ctx); + +/* Set the state information for multiple contexts. */ +void jpc_mqenc_setctxs(jpc_mqenc_t *enc, int numctxs, jpc_mqctx_t *ctxs); + +/******************************************************************************\ +* Miscellaneous functions/macros. +\******************************************************************************/ + +/* Get the error state of a MQ encoder. */ +#define jpc_mqenc_error(enc) \ + ((enc)->err) + +/* Get the current encoder state. */ +void jpc_mqenc_getstate(jpc_mqenc_t *enc, jpc_mqencstate_t *state); + +/* Terminate the code. */ +int jpc_mqenc_flush(jpc_mqenc_t *enc, int termmode); + +/******************************************************************************\ +* Functions/macros for encoding bits. +\******************************************************************************/ + +/* Encode a bit. */ +#if !defined(DEBUG) +#define jpc_mqenc_putbit(enc, bit) jpc_mqenc_putbit_macro(enc, bit) +#else +#define jpc_mqenc_putbit(enc, bit) jpc_mqenc_putbit_func(enc, bit) +#endif + +/******************************************************************************\ +* Functions/macros for debugging. +\******************************************************************************/ + +int jpc_mqenc_dump(jpc_mqenc_t *mqenc, FILE *out); + +/******************************************************************************\ +* Implementation-specific details. +\******************************************************************************/ + +/* Note: This macro is included only to satisfy the needs of + the mqenc_putbit macro. */ +#define jpc_mqenc_putbit_macro(enc, bit) \ + (((*((enc)->curctx))->mps == (bit)) ? \ + (((enc)->areg -= (*(enc)->curctx)->qeval), \ + ((!((enc)->areg & 0x8000)) ? (jpc_mqenc_codemps2(enc)) : \ + ((enc)->creg += (*(enc)->curctx)->qeval))) : \ + jpc_mqenc_codelps(enc)) + +/* Note: These function prototypes are included only to satisfy the + needs of the mqenc_putbit_macro macro. Do not call any of these + functions directly. */ +int jpc_mqenc_codemps2(jpc_mqenc_t *enc); +int jpc_mqenc_codelps(jpc_mqenc_t *enc); + +/* Note: This function prototype is included only to satisfy the needs of + the mqenc_putbit macro. */ +int jpc_mqenc_putbit_func(jpc_mqenc_t *enc, int bit); + +#endif diff --git a/src/libjasper/jpc/jpc_qmfb.c b/src/libjasper/jpc/jpc_qmfb.c new file mode 100644 index 0000000..75b8566 --- /dev/null +++ b/src/libjasper/jpc/jpc_qmfb.c @@ -0,0 +1,3152 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Quadrature Mirror-Image Filter Bank (QMFB) Library + * + * $Id: jpc_qmfb.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* +\******************************************************************************/ + +#undef WT_LENONE /* This is not needed due to normalization. */ +#define WT_DOSCALE + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include "jasper/jas_fix.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_math.h" + +#include "jpc_qmfb.h" +#include "jpc_tsfb.h" +#include "jpc_math.h" + +/******************************************************************************\ +* +\******************************************************************************/ + +#define QMFB_SPLITBUFSIZE 4096 +#define QMFB_JOINBUFSIZE 4096 + +int jpc_ft_analyze(jpc_fix_t *a, int xstart, int ystart, int width, int height, + int stride); +int jpc_ft_synthesize(int *a, int xstart, int ystart, int width, int height, + int stride); + +int jpc_ns_analyze(jpc_fix_t *a, int xstart, int ystart, int width, int height, + int stride); +int jpc_ns_synthesize(jpc_fix_t *a, int xstart, int ystart, int width, + int height, int stride); + +void jpc_ft_fwdlift_row(jpc_fix_t *a, int numcols, int parity); +void jpc_ft_fwdlift_col(jpc_fix_t *a, int numrows, int stride, + int parity); +void jpc_ft_fwdlift_colgrp(jpc_fix_t *a, int numrows, int stride, + int parity); +void jpc_ft_fwdlift_colres(jpc_fix_t *a, int numrows, int numcols, + int stride, int parity); + +void jpc_ft_invlift_row(jpc_fix_t *a, int numcols, int parity); +void jpc_ft_invlift_col(jpc_fix_t *a, int numrows, int stride, + int parity); +void jpc_ft_invlift_colgrp(jpc_fix_t *a, int numrows, int stride, + int parity); +void jpc_ft_invlift_colres(jpc_fix_t *a, int numrows, int numcols, + int stride, int parity); + +void jpc_ns_fwdlift_row(jpc_fix_t *a, int numcols, int parity); +void jpc_ns_fwdlift_colgrp(jpc_fix_t *a, int numrows, int stride, int parity); +void jpc_ns_fwdlift_colres(jpc_fix_t *a, int numrows, int numcols, int stride, + int parity); +void jpc_ns_invlift_row(jpc_fix_t *a, int numcols, int parity); +void jpc_ns_invlift_colgrp(jpc_fix_t *a, int numrows, int stride, int parity); +void jpc_ns_invlift_colres(jpc_fix_t *a, int numrows, int numcols, int stride, + int parity); + +void jpc_qmfb_split_row(jpc_fix_t *a, int numcols, int parity); +void jpc_qmfb_split_col(jpc_fix_t *a, int numrows, int stride, int parity); +void jpc_qmfb_split_colgrp(jpc_fix_t *a, int numrows, int stride, int parity); +void jpc_qmfb_split_colres(jpc_fix_t *a, int numrows, int numcols, int stride, + int parity); + +void jpc_qmfb_join_row(jpc_fix_t *a, int numcols, int parity); +void jpc_qmfb_join_col(jpc_fix_t *a, int numrows, int stride, int parity); +void jpc_qmfb_join_colgrp(jpc_fix_t *a, int numrows, int stride, int parity); +void jpc_qmfb_join_colres(jpc_fix_t *a, int numrows, int numcols, int stride, + int parity); + +double jpc_ft_lpenergywts[32] = { + 1.2247448713915889, + 1.6583123951776999, + 2.3184046238739260, + 3.2691742076555053, + 4.6199296531440819, + 6.5323713152269596, + 9.2377452606141937, + 13.0639951297449581, + 18.4752262333915667, + 26.1278968190610392, + 36.9504194305524791, + 52.2557819580462777, + 73.9008347315741645, + 104.5115624560829133, + 147.8016689469569656, + 209.0231247296646018, + 295.6033378293900000, + 418.0462494347059419, + 591.2066756503630813, + 836.0924988714708661, + /* approximations */ + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661, + 836.0924988714708661 +}; + +double jpc_ft_hpenergywts[32] = { + 0.8477912478906585, + 0.9601432184835760, + 1.2593401049756179, + 1.7444107171191079, + 2.4538713036750726, + 3.4656517695088755, + 4.8995276398597856, + 6.9283970402160842, + 9.7980274940131444, + 13.8564306871112652, + 19.5959265076535587, + 27.7128159494245487, + 39.1918369552045860, + 55.4256262207444053, + 78.3836719028959124, + 110.8512517317256822, + 156.7673435548526868, + 221.7025033739244293, + 313.5346870787551552, + 443.4050067351659550, + /* approximations */ + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550, + 443.4050067351659550 +}; + +double jpc_ns_lpenergywts[32] = { + 1.4021081679297411, + 2.0303718560817923, + 2.9011625562785555, + 4.1152851751758002, + 5.8245108637728071, + 8.2387599345725171, + 11.6519546479210838, + 16.4785606470644375, + 23.3042776444606794, + 32.9572515613740435, + 46.6086013487782793, + 65.9145194076860861, + 93.2172084551803977, + 131.8290408510004283, + 186.4344176300625691, + 263.6580819564562148, + 372.8688353500955373, + 527.3161639447193920, + 745.7376707114038936, + 1054.6323278917823245, + /* approximations follow */ + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245, + 1054.6323278917823245 +}; + +double jpc_ns_hpenergywts[32] = { + 1.4425227650161456, + 1.9669426082455688, + 2.8839248082788891, + 4.1475208393432981, + 5.8946497530677817, + 8.3471789178590949, + 11.8086046551047463, + 16.7012780415647804, + 23.6196657032246620, + 33.4034255108592362, + 47.2396388881632632, + 66.8069597416714061, + 94.4793162154500692, + 133.6139330736999113, + 188.9586372358249378, + 267.2278678461869390, + 377.9172750722391356, + 534.4557359047058753, + 755.8345502191498326, + 1068.9114718353569060, + /* approximations follow */ + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060, + 1068.9114718353569060 +}; + +jpc_qmfb2d_t jpc_ft_qmfb2d = { + jpc_ft_analyze, + jpc_ft_synthesize, + jpc_ft_lpenergywts, + jpc_ft_hpenergywts +}; + +jpc_qmfb2d_t jpc_ns_qmfb2d = { + jpc_ns_analyze, + jpc_ns_synthesize, + jpc_ns_lpenergywts, + jpc_ns_hpenergywts +}; + +/******************************************************************************\ +* generic +\******************************************************************************/ + +void jpc_qmfb_split_row(jpc_fix_t *a, int numcols, int parity) +{ + + int bufsize = JPC_CEILDIVPOW2(numcols, 1); +#if !defined(HAVE_VLA) + jpc_fix_t splitbuf[QMFB_SPLITBUFSIZE]; +#else + jpc_fix_t splitbuf[bufsize]; +#endif + jpc_fix_t *buf = splitbuf; + register jpc_fix_t *srcptr; + register jpc_fix_t *dstptr; + register int n; + register int m; + int hstartcol; + +#if !defined(HAVE_VLA) + /* Get a buffer. */ + if (bufsize > QMFB_SPLITBUFSIZE) { + if (!(buf = jas_malloc(bufsize * sizeof(jpc_fix_t)))) { + /* We have no choice but to commit suicide in this case. */ + abort(); + } + } +#endif + + if (numcols >= 2) { + hstartcol = (numcols + 1 - parity) >> 1; + m = (parity) ? hstartcol : (numcols - hstartcol); + /* Save the samples destined for the highpass channel. */ + n = m; + dstptr = buf; + srcptr = &a[1 - parity]; + while (n-- > 0) { + *dstptr = *srcptr; + ++dstptr; + srcptr += 2; + } + /* Copy the appropriate samples into the lowpass channel. */ + dstptr = &a[1 - parity]; + srcptr = &a[2 - parity]; + n = numcols - m - (!parity); + while (n-- > 0) { + *dstptr = *srcptr; + ++dstptr; + srcptr += 2; + } + /* Copy the saved samples into the highpass channel. */ + dstptr = &a[hstartcol]; + srcptr = buf; + n = m; + while (n-- > 0) { + *dstptr = *srcptr; + ++dstptr; + ++srcptr; + } + } + +#if !defined(HAVE_VLA) + /* If the split buffer was allocated on the heap, free this memory. */ + if (buf != splitbuf) { + jas_free(buf); + } +#endif + +} + +void jpc_qmfb_split_col(jpc_fix_t *a, int numrows, int stride, + int parity) +{ + + int bufsize = JPC_CEILDIVPOW2(numrows, 1); +#if !defined(HAVE_VLA) + jpc_fix_t splitbuf[QMFB_SPLITBUFSIZE]; +#else + jpc_fix_t splitbuf[bufsize]; +#endif + jpc_fix_t *buf = splitbuf; + register jpc_fix_t *srcptr; + register jpc_fix_t *dstptr; + register int n; + register int m; + int hstartcol; + +#if !defined(HAVE_VLA) + /* Get a buffer. */ + if (bufsize > QMFB_SPLITBUFSIZE) { + if (!(buf = jas_malloc(bufsize * sizeof(jpc_fix_t)))) { + /* We have no choice but to commit suicide in this case. */ + abort(); + } + } +#endif + + if (numrows >= 2) { + hstartcol = (numrows + 1 - parity) >> 1; + m = (parity) ? hstartcol : (numrows - hstartcol); + /* Save the samples destined for the highpass channel. */ + n = m; + dstptr = buf; + srcptr = &a[(1 - parity) * stride]; + while (n-- > 0) { + *dstptr = *srcptr; + ++dstptr; + srcptr += stride << 1; + } + /* Copy the appropriate samples into the lowpass channel. */ + dstptr = &a[(1 - parity) * stride]; + srcptr = &a[(2 - parity) * stride]; + n = numrows - m - (!parity); + while (n-- > 0) { + *dstptr = *srcptr; + dstptr += stride; + srcptr += stride << 1; + } + /* Copy the saved samples into the highpass channel. */ + dstptr = &a[hstartcol * stride]; + srcptr = buf; + n = m; + while (n-- > 0) { + *dstptr = *srcptr; + dstptr += stride; + ++srcptr; + } + } + +#if !defined(HAVE_VLA) + /* If the split buffer was allocated on the heap, free this memory. */ + if (buf != splitbuf) { + jas_free(buf); + } +#endif + +} + +void jpc_qmfb_split_colgrp(jpc_fix_t *a, int numrows, int stride, + int parity) +{ + + int bufsize = JPC_CEILDIVPOW2(numrows, 1); +#if !defined(HAVE_VLA) + jpc_fix_t splitbuf[QMFB_SPLITBUFSIZE * JPC_QMFB_COLGRPSIZE]; +#else + jpc_fix_t splitbuf[bufsize * JPC_QMFB_COLGRPSIZE]; +#endif + jpc_fix_t *buf = splitbuf; + jpc_fix_t *srcptr; + jpc_fix_t *dstptr; + register jpc_fix_t *srcptr2; + register jpc_fix_t *dstptr2; + register int n; + register int i; + int m; + int hstartcol; + +#if !defined(HAVE_VLA) + /* Get a buffer. */ + if (bufsize > QMFB_SPLITBUFSIZE) { + if (!(buf = jas_malloc(bufsize * sizeof(jpc_fix_t)))) { + /* We have no choice but to commit suicide in this case. */ + abort(); + } + } +#endif + + if (numrows >= 2) { + hstartcol = (numrows + 1 - parity) >> 1; + m = (parity) ? hstartcol : (numrows - hstartcol); + /* Save the samples destined for the highpass channel. */ + n = m; + dstptr = buf; + srcptr = &a[(1 - parity) * stride]; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += JPC_QMFB_COLGRPSIZE; + srcptr += stride << 1; + } + /* Copy the appropriate samples into the lowpass channel. */ + dstptr = &a[(1 - parity) * stride]; + srcptr = &a[(2 - parity) * stride]; + n = numrows - m - (!parity); + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += stride; + srcptr += stride << 1; + } + /* Copy the saved samples into the highpass channel. */ + dstptr = &a[hstartcol * stride]; + srcptr = buf; + n = m; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += stride; + srcptr += JPC_QMFB_COLGRPSIZE; + } + } + +#if !defined(HAVE_VLA) + /* If the split buffer was allocated on the heap, free this memory. */ + if (buf != splitbuf) { + jas_free(buf); + } +#endif + +} + +void jpc_qmfb_split_colres(jpc_fix_t *a, int numrows, int numcols, + int stride, int parity) +{ + + int bufsize = JPC_CEILDIVPOW2(numrows, 1); +#if !defined(HAVE_VLA) + jpc_fix_t splitbuf[QMFB_SPLITBUFSIZE * JPC_QMFB_COLGRPSIZE]; +#else + jpc_fix_t splitbuf[bufsize * numcols]; +#endif + jpc_fix_t *buf = splitbuf; + jpc_fix_t *srcptr; + jpc_fix_t *dstptr; + register jpc_fix_t *srcptr2; + register jpc_fix_t *dstptr2; + register int n; + register int i; + int m; + int hstartcol; + +#if !defined(HAVE_VLA) + /* Get a buffer. */ + if (bufsize > QMFB_SPLITBUFSIZE) { + if (!(buf = jas_malloc(bufsize * sizeof(jpc_fix_t)))) { + /* We have no choice but to commit suicide in this case. */ + abort(); + } + } +#endif + + if (numrows >= 2) { + hstartcol = (numrows + 1 - parity) >> 1; + m = (parity) ? hstartcol : (numrows - hstartcol); + /* Save the samples destined for the highpass channel. */ + n = m; + dstptr = buf; + srcptr = &a[(1 - parity) * stride]; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < numcols; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += numcols; + srcptr += stride << 1; + } + /* Copy the appropriate samples into the lowpass channel. */ + dstptr = &a[(1 - parity) * stride]; + srcptr = &a[(2 - parity) * stride]; + n = numrows - m - (!parity); + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < numcols; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += stride; + srcptr += stride << 1; + } + /* Copy the saved samples into the highpass channel. */ + dstptr = &a[hstartcol * stride]; + srcptr = buf; + n = m; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < numcols; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += stride; + srcptr += numcols; + } + } + +#if !defined(HAVE_VLA) + /* If the split buffer was allocated on the heap, free this memory. */ + if (buf != splitbuf) { + jas_free(buf); + } +#endif + +} + +void jpc_qmfb_join_row(jpc_fix_t *a, int numcols, int parity) +{ + + int bufsize = JPC_CEILDIVPOW2(numcols, 1); +#if !defined(HAVE_VLA) + jpc_fix_t joinbuf[QMFB_JOINBUFSIZE]; +#else + jpc_fix_t joinbuf[bufsize]; +#endif + jpc_fix_t *buf = joinbuf; + register jpc_fix_t *srcptr; + register jpc_fix_t *dstptr; + register int n; + int hstartcol; + +#if !defined(HAVE_VLA) + /* Allocate memory for the join buffer from the heap. */ + if (bufsize > QMFB_JOINBUFSIZE) { + if (!(buf = jas_malloc(bufsize * sizeof(jpc_fix_t)))) { + /* We have no choice but to commit suicide. */ + abort(); + } + } +#endif + + hstartcol = (numcols + 1 - parity) >> 1; + + /* Save the samples from the lowpass channel. */ + n = hstartcol; + srcptr = &a[0]; + dstptr = buf; + while (n-- > 0) { + *dstptr = *srcptr; + ++srcptr; + ++dstptr; + } + /* Copy the samples from the highpass channel into place. */ + srcptr = &a[hstartcol]; + dstptr = &a[1 - parity]; + n = numcols - hstartcol; + while (n-- > 0) { + *dstptr = *srcptr; + dstptr += 2; + ++srcptr; + } + /* Copy the samples from the lowpass channel into place. */ + srcptr = buf; + dstptr = &a[parity]; + n = hstartcol; + while (n-- > 0) { + *dstptr = *srcptr; + dstptr += 2; + ++srcptr; + } + +#if !defined(HAVE_VLA) + /* If the join buffer was allocated on the heap, free this memory. */ + if (buf != joinbuf) { + jas_free(buf); + } +#endif + +} + +void jpc_qmfb_join_col(jpc_fix_t *a, int numrows, int stride, + int parity) +{ + + int bufsize = JPC_CEILDIVPOW2(numrows, 1); +#if !defined(HAVE_VLA) + jpc_fix_t joinbuf[QMFB_JOINBUFSIZE]; +#else + jpc_fix_t joinbuf[bufsize]; +#endif + jpc_fix_t *buf = joinbuf; + register jpc_fix_t *srcptr; + register jpc_fix_t *dstptr; + register int n; + int hstartcol; + +#if !defined(HAVE_VLA) + /* Allocate memory for the join buffer from the heap. */ + if (bufsize > QMFB_JOINBUFSIZE) { + if (!(buf = jas_malloc(bufsize * sizeof(jpc_fix_t)))) { + /* We have no choice but to commit suicide. */ + abort(); + } + } +#endif + + hstartcol = (numrows + 1 - parity) >> 1; + + /* Save the samples from the lowpass channel. */ + n = hstartcol; + srcptr = &a[0]; + dstptr = buf; + while (n-- > 0) { + *dstptr = *srcptr; + srcptr += stride; + ++dstptr; + } + /* Copy the samples from the highpass channel into place. */ + srcptr = &a[hstartcol * stride]; + dstptr = &a[(1 - parity) * stride]; + n = numrows - hstartcol; + while (n-- > 0) { + *dstptr = *srcptr; + dstptr += 2 * stride; + srcptr += stride; + } + /* Copy the samples from the lowpass channel into place. */ + srcptr = buf; + dstptr = &a[parity * stride]; + n = hstartcol; + while (n-- > 0) { + *dstptr = *srcptr; + dstptr += 2 * stride; + ++srcptr; + } + +#if !defined(HAVE_VLA) + /* If the join buffer was allocated on the heap, free this memory. */ + if (buf != joinbuf) { + jas_free(buf); + } +#endif + +} + +void jpc_qmfb_join_colgrp(jpc_fix_t *a, int numrows, int stride, + int parity) +{ + + int bufsize = JPC_CEILDIVPOW2(numrows, 1); +#if !defined(HAVE_VLA) + jpc_fix_t joinbuf[QMFB_JOINBUFSIZE * JPC_QMFB_COLGRPSIZE]; +#else + jpc_fix_t joinbuf[bufsize * JPC_QMFB_COLGRPSIZE]; +#endif + jpc_fix_t *buf = joinbuf; + jpc_fix_t *srcptr; + jpc_fix_t *dstptr; + register jpc_fix_t *srcptr2; + register jpc_fix_t *dstptr2; + register int n; + register int i; + int hstartcol; + +#if !defined(HAVE_VLA) + /* Allocate memory for the join buffer from the heap. */ + if (bufsize > QMFB_JOINBUFSIZE) { + if (!(buf = jas_malloc(bufsize * JPC_QMFB_COLGRPSIZE * sizeof(jpc_fix_t)))) { + /* We have no choice but to commit suicide. */ + abort(); + } + } +#endif + + hstartcol = (numrows + 1 - parity) >> 1; + + /* Save the samples from the lowpass channel. */ + n = hstartcol; + srcptr = &a[0]; + dstptr = buf; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + srcptr += stride; + dstptr += JPC_QMFB_COLGRPSIZE; + } + /* Copy the samples from the highpass channel into place. */ + srcptr = &a[hstartcol * stride]; + dstptr = &a[(1 - parity) * stride]; + n = numrows - hstartcol; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += 2 * stride; + srcptr += stride; + } + /* Copy the samples from the lowpass channel into place. */ + srcptr = buf; + dstptr = &a[parity * stride]; + n = hstartcol; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += 2 * stride; + srcptr += JPC_QMFB_COLGRPSIZE; + } + +#if !defined(HAVE_VLA) + /* If the join buffer was allocated on the heap, free this memory. */ + if (buf != joinbuf) { + jas_free(buf); + } +#endif + +} + +void jpc_qmfb_join_colres(jpc_fix_t *a, int numrows, int numcols, + int stride, int parity) +{ + + int bufsize = JPC_CEILDIVPOW2(numrows, 1); +#if !defined(HAVE_VLA) + jpc_fix_t joinbuf[QMFB_JOINBUFSIZE * JPC_QMFB_COLGRPSIZE]; +#else + jpc_fix_t joinbuf[bufsize * numcols]; +#endif + jpc_fix_t *buf = joinbuf; + jpc_fix_t *srcptr; + jpc_fix_t *dstptr; + register jpc_fix_t *srcptr2; + register jpc_fix_t *dstptr2; + register int n; + register int i; + int hstartcol; + +#if !defined(HAVE_VLA) + /* Allocate memory for the join buffer from the heap. */ + if (bufsize > QMFB_JOINBUFSIZE) { + if (!(buf = jas_malloc(bufsize * numcols * sizeof(jpc_fix_t)))) { + /* We have no choice but to commit suicide. */ + abort(); + } + } +#endif + + hstartcol = (numrows + 1 - parity) >> 1; + + /* Save the samples from the lowpass channel. */ + n = hstartcol; + srcptr = &a[0]; + dstptr = buf; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < numcols; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + srcptr += stride; + dstptr += numcols; + } + /* Copy the samples from the highpass channel into place. */ + srcptr = &a[hstartcol * stride]; + dstptr = &a[(1 - parity) * stride]; + n = numrows - hstartcol; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < numcols; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += 2 * stride; + srcptr += stride; + } + /* Copy the samples from the lowpass channel into place. */ + srcptr = buf; + dstptr = &a[parity * stride]; + n = hstartcol; + while (n-- > 0) { + dstptr2 = dstptr; + srcptr2 = srcptr; + for (i = 0; i < numcols; ++i) { + *dstptr2 = *srcptr2; + ++dstptr2; + ++srcptr2; + } + dstptr += 2 * stride; + srcptr += numcols; + } + +#if !defined(HAVE_VLA) + /* If the join buffer was allocated on the heap, free this memory. */ + if (buf != joinbuf) { + jas_free(buf); + } +#endif + +} + +/******************************************************************************\ +* 5/3 transform +\******************************************************************************/ + +void jpc_ft_fwdlift_row(jpc_fix_t *a, int numcols, int parity) +{ + + register jpc_fix_t *lptr; + register jpc_fix_t *hptr; + register int n; + int llen; + + llen = (numcols + 1 - parity) >> 1; + + if (numcols > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (parity) { + hptr[0] -= lptr[0]; + ++hptr; + } + n = numcols - llen - parity - (parity == (numcols & 1)); + while (n-- > 0) { + hptr[0] -= (lptr[0] + lptr[1]) >> 1; + ++hptr; + ++lptr; + } + if (parity == (numcols & 1)) { + hptr[0] -= lptr[0]; + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (!parity) { + lptr[0] += (hptr[0] + 1) >> 1; + ++lptr; + } + n = llen - (!parity) - (parity != (numcols & 1)); + while (n-- > 0) { + lptr[0] += (hptr[0] + hptr[1] + 2) >> 2; + ++lptr; + ++hptr; + } + if (parity != (numcols & 1)) { + lptr[0] += (hptr[0] + 1) >> 1; + } + + } else { + + if (parity) { + lptr = &a[0]; + lptr[0] <<= 1; + } + + } + +} + +void jpc_ft_fwdlift_col(jpc_fix_t *a, int numrows, int stride, int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; +#if 0 + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int i; +#endif + register int n; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + hptr[0] -= lptr[0]; + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + hptr[0] -= (lptr[0] + lptr[stride]) >> 1; + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + hptr[0] -= lptr[0]; + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr[0] += (hptr[0] + 1) >> 1; + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr[0] += (hptr[0] + hptr[stride] + 2) >> 2; + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr[0] += (hptr[0] + 1) >> 1; + } + + } else { + + if (parity) { + lptr = &a[0]; + lptr[0] <<= 1; + } + + } + +} + +void jpc_ft_fwdlift_colgrp(jpc_fix_t *a, int numrows, int stride, int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + register int i; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + hptr2[0] -= lptr2[0]; + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + hptr2[0] -= (lptr2[0] + lptr2[stride]) >> 1; + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + hptr2[0] -= lptr2[0]; + ++lptr2; + ++hptr2; + } + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] += (hptr2[0] + 1) >> 1; + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] += (hptr2[0] + hptr2[stride] + 2) >> 2; + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] += (hptr2[0] + 1) >> 1; + ++lptr2; + ++hptr2; + } + } + + } else { + + if (parity) { + lptr2 = &a[0]; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] <<= 1; + ++lptr2; + } + } + + } + +} + +void jpc_ft_fwdlift_colres(jpc_fix_t *a, int numrows, int numcols, int stride, + int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + register int i; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + hptr2[0] -= lptr2[0]; + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + hptr2[0] -= (lptr2[0] + lptr2[stride]) >> 1; + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + hptr2[0] -= lptr2[0]; + ++lptr2; + ++hptr2; + } + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + lptr2[0] += (hptr2[0] + 1) >> 1; + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + lptr2[0] += (hptr2[0] + hptr2[stride] + 2) >> 2; + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + lptr2[0] += (hptr2[0] + 1) >> 1; + ++lptr2; + ++hptr2; + } + } + + } else { + + if (parity) { + lptr2 = &a[0]; + for (i = 0; i < numcols; ++i) { + lptr2[0] <<= 1; + ++lptr2; + } + } + + } + +} + +void jpc_ft_invlift_row(jpc_fix_t *a, int numcols, int parity) +{ + + register jpc_fix_t *lptr; + register jpc_fix_t *hptr; + register int n; + int llen; + + llen = (numcols + 1 - parity) >> 1; + + if (numcols > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (!parity) { + lptr[0] -= (hptr[0] + 1) >> 1; + ++lptr; + } + n = llen - (!parity) - (parity != (numcols & 1)); + while (n-- > 0) { + lptr[0] -= (hptr[0] + hptr[1] + 2) >> 2; + ++lptr; + ++hptr; + } + if (parity != (numcols & 1)) { + lptr[0] -= (hptr[0] + 1) >> 1; + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (parity) { + hptr[0] += lptr[0]; + ++hptr; + } + n = numcols - llen - parity - (parity == (numcols & 1)); + while (n-- > 0) { + hptr[0] += (lptr[0] + lptr[1]) >> 1; + ++hptr; + ++lptr; + } + if (parity == (numcols & 1)) { + hptr[0] += lptr[0]; + } + + } else { + + if (parity) { + lptr = &a[0]; + lptr[0] >>= 1; + } + + } + +} + +void jpc_ft_invlift_col(jpc_fix_t *a, int numrows, int stride, int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; +#if 0 + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int i; +#endif + register int n; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr[0] -= (hptr[0] + 1) >> 1; + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr[0] -= (hptr[0] + hptr[stride] + 2) >> 2; + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr[0] -= (hptr[0] + 1) >> 1; + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + hptr[0] += lptr[0]; + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + hptr[0] += (lptr[0] + lptr[stride]) >> 1; + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + hptr[0] += lptr[0]; + } + + } else { + + if (parity) { + lptr = &a[0]; + lptr[0] >>= 1; + } + + } + +} + +void jpc_ft_invlift_colgrp(jpc_fix_t *a, int numrows, int stride, int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + register int i; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] -= (hptr2[0] + 1) >> 1; + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] -= (hptr2[0] + hptr2[stride] + 2) >> 2; + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] -= (hptr2[0] + 1) >> 1; + ++lptr2; + ++hptr2; + } + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + hptr2[0] += lptr2[0]; + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + hptr2[0] += (lptr2[0] + lptr2[stride]) >> 1; + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + hptr2[0] += lptr2[0]; + ++lptr2; + ++hptr2; + } + } + + } else { + + if (parity) { + lptr2 = &a[0]; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] >>= 1; + ++lptr2; + } + } + + } + +} + +void jpc_ft_invlift_colres(jpc_fix_t *a, int numrows, int numcols, int stride, + int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + register int i; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + lptr2[0] -= (hptr2[0] + 1) >> 1; + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + lptr2[0] -= (hptr2[0] + hptr2[stride] + 2) >> 2; + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + lptr2[0] -= (hptr2[0] + 1) >> 1; + ++lptr2; + ++hptr2; + } + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + hptr2[0] += lptr2[0]; + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + hptr2[0] += (lptr2[0] + lptr2[stride]) >> 1; + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + hptr2[0] += lptr2[0]; + ++lptr2; + ++hptr2; + } + } + + } else { + + if (parity) { + lptr2 = &a[0]; + for (i = 0; i < numcols; ++i) { + lptr2[0] >>= 1; + ++lptr2; + } + } + + } + +} + +int jpc_ft_analyze(jpc_fix_t *a, int xstart, int ystart, int width, int height, + int stride) +{ + int numrows = height; + int numcols = width; + int rowparity = ystart & 1; + int colparity = xstart & 1; + int i; + jpc_fix_t *startptr; + int maxcols; + + maxcols = (numcols / JPC_QMFB_COLGRPSIZE) * JPC_QMFB_COLGRPSIZE; + startptr = &a[0]; + for (i = 0; i < maxcols; i += JPC_QMFB_COLGRPSIZE) { + jpc_qmfb_split_colgrp(startptr, numrows, stride, rowparity); + jpc_ft_fwdlift_colgrp(startptr, numrows, stride, rowparity); + startptr += JPC_QMFB_COLGRPSIZE; + } + if (maxcols < numcols) { + jpc_qmfb_split_colres(startptr, numrows, numcols - maxcols, stride, + rowparity); + jpc_ft_fwdlift_colres(startptr, numrows, numcols - maxcols, stride, + rowparity); + } + + startptr = &a[0]; + for (i = 0; i < numrows; ++i) { + jpc_qmfb_split_row(startptr, numcols, colparity); + jpc_ft_fwdlift_row(startptr, numcols, colparity); + startptr += stride; + } + + return 0; + +} + +int jpc_ft_synthesize(int *a, int xstart, int ystart, int width, int height, + int stride) +{ + int numrows = height; + int numcols = width; + int rowparity = ystart & 1; + int colparity = xstart & 1; + + int maxcols; + jpc_fix_t *startptr; + int i; + + startptr = &a[0]; + for (i = 0; i < numrows; ++i) { + jpc_ft_invlift_row(startptr, numcols, colparity); + jpc_qmfb_join_row(startptr, numcols, colparity); + startptr += stride; + } + + maxcols = (numcols / JPC_QMFB_COLGRPSIZE) * JPC_QMFB_COLGRPSIZE; + startptr = &a[0]; + for (i = 0; i < maxcols; i += JPC_QMFB_COLGRPSIZE) { + // GeoJasper: dima - progress + jas_do_progress( i, maxcols, "jpc: synthesize" ); // dima + if (jas_test_abort() == 1) return 0; + + jpc_ft_invlift_colgrp(startptr, numrows, stride, rowparity); + jpc_qmfb_join_colgrp(startptr, numrows, stride, rowparity); + startptr += JPC_QMFB_COLGRPSIZE; + } + if (maxcols < numcols) { + jpc_ft_invlift_colres(startptr, numrows, numcols - maxcols, stride, + rowparity); + jpc_qmfb_join_colres(startptr, numrows, numcols - maxcols, stride, + rowparity); + } + + return 0; + +} + +/******************************************************************************\ +* 9/7 transform +\******************************************************************************/ + +#define ALPHA (-1.586134342059924) +#define BETA (-0.052980118572961) +#define GAMMA (0.882911075530934) +#define DELTA (0.443506852043971) +#define LGAIN (1.0 / 1.23017410558578) +#define HGAIN (1.0 / 1.62578613134411) + +void jpc_ns_fwdlift_row(jpc_fix_t *a, int numcols, int parity) +{ + + register jpc_fix_t *lptr; + register jpc_fix_t *hptr; + register int n; + int llen; + + llen = (numcols + 1 - parity) >> 1; + + if (numcols > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (parity) { + jpc_fix_pluseq(hptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr[0])); + ++hptr; + } + n = numcols - llen - parity - (parity == (numcols & 1)); + while (n-- > 0) { + jpc_fix_pluseq(hptr[0], jpc_fix_mul(jpc_dbltofix(ALPHA), + jpc_fix_add(lptr[0], lptr[1]))); + ++hptr; + ++lptr; + } + if (parity == (numcols & 1)) { + jpc_fix_pluseq(hptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr[0])); + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (!parity) { + jpc_fix_pluseq(lptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr[0])); + ++lptr; + } + n = llen - (!parity) - (parity != (numcols & 1)); + while (n-- > 0) { + jpc_fix_pluseq(lptr[0], jpc_fix_mul(jpc_dbltofix(BETA), + jpc_fix_add(hptr[0], hptr[1]))); + ++lptr; + ++hptr; + } + if (parity != (numcols & 1)) { + jpc_fix_pluseq(lptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr[0])); + } + + /* Apply the third lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (parity) { + jpc_fix_pluseq(hptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr[0])); + ++hptr; + } + n = numcols - llen - parity - (parity == (numcols & 1)); + while (n-- > 0) { + jpc_fix_pluseq(hptr[0], jpc_fix_mul(jpc_dbltofix(GAMMA), + jpc_fix_add(lptr[0], lptr[1]))); + ++hptr; + ++lptr; + } + if (parity == (numcols & 1)) { + jpc_fix_pluseq(hptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr[0])); + } + + /* Apply the fourth lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (!parity) { + jpc_fix_pluseq(lptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr[0])); + ++lptr; + } + n = llen - (!parity) - (parity != (numcols & 1)); + while (n-- > 0) { + jpc_fix_pluseq(lptr[0], jpc_fix_mul(jpc_dbltofix(DELTA), + jpc_fix_add(hptr[0], hptr[1]))); + ++lptr; + ++hptr; + } + if (parity != (numcols & 1)) { + jpc_fix_pluseq(lptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr[0])); + } + + /* Apply the scaling step. */ +#if defined(WT_DOSCALE) + lptr = &a[0]; + n = llen; + while (n-- > 0) { + lptr[0] = jpc_fix_mul(lptr[0], jpc_dbltofix(LGAIN)); + ++lptr; + } + hptr = &a[llen]; + n = numcols - llen; + while (n-- > 0) { + hptr[0] = jpc_fix_mul(hptr[0], jpc_dbltofix(HGAIN)); + ++hptr; + } +#endif + + } else { + +#if defined(WT_LENONE) + if (parity) { + lptr = &a[0]; + lptr[0] <<= 1; + } +#endif + + } + +} + +void jpc_ns_fwdlift_colgrp(jpc_fix_t *a, int numrows, int stride, + int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + register int i; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr2[0])); + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(ALPHA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(BETA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the third lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr2[0])); + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(GAMMA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the fourth lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(DELTA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the scaling step. */ +#if defined(WT_DOSCALE) + lptr = &a[0]; + n = llen; + while (n-- > 0) { + lptr2 = lptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] = jpc_fix_mul(lptr2[0], jpc_dbltofix(LGAIN)); + ++lptr2; + } + lptr += stride; + } + hptr = &a[llen * stride]; + n = numrows - llen; + while (n-- > 0) { + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + hptr2[0] = jpc_fix_mul(hptr2[0], jpc_dbltofix(HGAIN)); + ++hptr2; + } + hptr += stride; + } +#endif + + } else { + +#if defined(WT_LENONE) + if (parity) { + lptr2 = &a[0]; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] <<= 1; + ++lptr2; + } + } +#endif + + } + +} + +void jpc_ns_fwdlift_colres(jpc_fix_t *a, int numrows, int numcols, + int stride, int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + register int i; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr2[0])); + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(ALPHA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(BETA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the third lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr2[0])); + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(GAMMA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the fourth lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(DELTA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the scaling step. */ +#if defined(WT_DOSCALE) + lptr = &a[0]; + n = llen; + while (n-- > 0) { + lptr2 = lptr; + for (i = 0; i < numcols; ++i) { + lptr2[0] = jpc_fix_mul(lptr2[0], jpc_dbltofix(LGAIN)); + ++lptr2; + } + lptr += stride; + } + hptr = &a[llen * stride]; + n = numrows - llen; + while (n-- > 0) { + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + hptr2[0] = jpc_fix_mul(hptr2[0], jpc_dbltofix(HGAIN)); + ++hptr2; + } + hptr += stride; + } +#endif + + } else { + +#if defined(WT_LENONE) + if (parity) { + lptr2 = &a[0]; + for (i = 0; i < numcols; ++i) { + lptr2[0] <<= 1; + ++lptr2; + } + } +#endif + + } + +} + +void jpc_ns_fwdlift_col(jpc_fix_t *a, int numrows, int stride, + int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr2[0])); + ++hptr2; + ++lptr2; + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(ALPHA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr2[0])); + ++lptr2; + ++hptr2; + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(BETA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + + /* Apply the third lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr2[0])); + ++hptr2; + ++lptr2; + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(GAMMA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr2[0])); + ++lptr2; + ++hptr2; + } + + /* Apply the fourth lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr2[0])); + ++lptr2; + ++hptr2; + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(DELTA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_pluseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + + /* Apply the scaling step. */ +#if defined(WT_DOSCALE) + lptr = &a[0]; + n = llen; + while (n-- > 0) { + lptr2 = lptr; + lptr2[0] = jpc_fix_mul(lptr2[0], jpc_dbltofix(LGAIN)); + ++lptr2; + lptr += stride; + } + hptr = &a[llen * stride]; + n = numrows - llen; + while (n-- > 0) { + hptr2 = hptr; + hptr2[0] = jpc_fix_mul(hptr2[0], jpc_dbltofix(HGAIN)); + ++hptr2; + hptr += stride; + } +#endif + + } else { + +#if defined(WT_LENONE) + if (parity) { + lptr2 = &a[0]; + lptr2[0] <<= 1; + ++lptr2; + } +#endif + + } + +} + +void jpc_ns_invlift_row(jpc_fix_t *a, int numcols, int parity) +{ + + register jpc_fix_t *lptr; + register jpc_fix_t *hptr; + register int n; + int llen; + + llen = (numcols + 1 - parity) >> 1; + + if (numcols > 1) { + + /* Apply the scaling step. */ +#if defined(WT_DOSCALE) + lptr = &a[0]; + n = llen; + while (n-- > 0) { + lptr[0] = jpc_fix_mul(lptr[0], jpc_dbltofix(1.0 / LGAIN)); + ++lptr; + } + hptr = &a[llen]; + n = numcols - llen; + while (n-- > 0) { + hptr[0] = jpc_fix_mul(hptr[0], jpc_dbltofix(1.0 / HGAIN)); + ++hptr; + } +#endif + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (!parity) { + jpc_fix_minuseq(lptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr[0])); + ++lptr; + } + n = llen - (!parity) - (parity != (numcols & 1)); + while (n-- > 0) { + jpc_fix_minuseq(lptr[0], jpc_fix_mul(jpc_dbltofix(DELTA), + jpc_fix_add(hptr[0], hptr[1]))); + ++lptr; + ++hptr; + } + if (parity != (numcols & 1)) { + jpc_fix_minuseq(lptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * DELTA), + hptr[0])); + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (parity) { + jpc_fix_minuseq(hptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr[0])); + ++hptr; + } + n = numcols - llen - parity - (parity == (numcols & 1)); + while (n-- > 0) { + jpc_fix_minuseq(hptr[0], jpc_fix_mul(jpc_dbltofix(GAMMA), + jpc_fix_add(lptr[0], lptr[1]))); + ++hptr; + ++lptr; + } + if (parity == (numcols & 1)) { + jpc_fix_minuseq(hptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * GAMMA), + lptr[0])); + } + + /* Apply the third lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (!parity) { + jpc_fix_minuseq(lptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr[0])); + ++lptr; + } + n = llen - (!parity) - (parity != (numcols & 1)); + while (n-- > 0) { + jpc_fix_minuseq(lptr[0], jpc_fix_mul(jpc_dbltofix(BETA), + jpc_fix_add(hptr[0], hptr[1]))); + ++lptr; + ++hptr; + } + if (parity != (numcols & 1)) { + jpc_fix_minuseq(lptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr[0])); + } + + /* Apply the fourth lifting step. */ + lptr = &a[0]; + hptr = &a[llen]; + if (parity) { + jpc_fix_minuseq(hptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr[0])); + ++hptr; + } + n = numcols - llen - parity - (parity == (numcols & 1)); + while (n-- > 0) { + jpc_fix_minuseq(hptr[0], jpc_fix_mul(jpc_dbltofix(ALPHA), + jpc_fix_add(lptr[0], lptr[1]))); + ++hptr; + ++lptr; + } + if (parity == (numcols & 1)) { + jpc_fix_minuseq(hptr[0], jpc_fix_mul(jpc_dbltofix(2.0 * ALPHA), + lptr[0])); + } + + } else { + +#if defined(WT_LENONE) + if (parity) { + lptr = &a[0]; + lptr[0] >>= 1; + } +#endif + + } + +} + +void jpc_ns_invlift_colgrp(jpc_fix_t *a, int numrows, int stride, + int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + register int i; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the scaling step. */ +#if defined(WT_DOSCALE) + lptr = &a[0]; + n = llen; + while (n-- > 0) { + lptr2 = lptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] = jpc_fix_mul(lptr2[0], jpc_dbltofix(1.0 / LGAIN)); + ++lptr2; + } + lptr += stride; + } + hptr = &a[llen * stride]; + n = numrows - llen; + while (n-- > 0) { + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + hptr2[0] = jpc_fix_mul(hptr2[0], jpc_dbltofix(1.0 / HGAIN)); + ++hptr2; + } + hptr += stride; + } +#endif + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + DELTA), hptr2[0])); + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(DELTA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + DELTA), hptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + GAMMA), lptr2[0])); + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(GAMMA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + GAMMA), lptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the third lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(BETA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the fourth lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + ALPHA), lptr2[0])); + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(ALPHA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + ALPHA), lptr2[0])); + ++lptr2; + ++hptr2; + } + } + + } else { + +#if defined(WT_LENONE) + if (parity) { + lptr2 = &a[0]; + for (i = 0; i < JPC_QMFB_COLGRPSIZE; ++i) { + lptr2[0] >>= 1; + ++lptr2; + } + } +#endif + + } + +} + +void jpc_ns_invlift_colres(jpc_fix_t *a, int numrows, int numcols, + int stride, int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + register int i; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the scaling step. */ +#if defined(WT_DOSCALE) + lptr = &a[0]; + n = llen; + while (n-- > 0) { + lptr2 = lptr; + for (i = 0; i < numcols; ++i) { + lptr2[0] = jpc_fix_mul(lptr2[0], jpc_dbltofix(1.0 / LGAIN)); + ++lptr2; + } + lptr += stride; + } + hptr = &a[llen * stride]; + n = numrows - llen; + while (n-- > 0) { + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + hptr2[0] = jpc_fix_mul(hptr2[0], jpc_dbltofix(1.0 / HGAIN)); + ++hptr2; + } + hptr += stride; + } +#endif + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + DELTA), hptr2[0])); + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(DELTA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + DELTA), hptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + GAMMA), lptr2[0])); + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(GAMMA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + GAMMA), lptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the third lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(BETA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + } + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + } + + /* Apply the fourth lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + ALPHA), lptr2[0])); + ++hptr2; + ++lptr2; + } + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(ALPHA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + } + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + for (i = 0; i < numcols; ++i) { + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + ALPHA), lptr2[0])); + ++lptr2; + ++hptr2; + } + } + + } else { + +#if defined(WT_LENONE) + if (parity) { + lptr2 = &a[0]; + for (i = 0; i < numcols; ++i) { + lptr2[0] >>= 1; + ++lptr2; + } + } +#endif + + } + +} + +void jpc_ns_invlift_col(jpc_fix_t *a, int numrows, int stride, + int parity) +{ + + jpc_fix_t *lptr; + jpc_fix_t *hptr; + register jpc_fix_t *lptr2; + register jpc_fix_t *hptr2; + register int n; + int llen; + + llen = (numrows + 1 - parity) >> 1; + + if (numrows > 1) { + + /* Apply the scaling step. */ +#if defined(WT_DOSCALE) + lptr = &a[0]; + n = llen; + while (n-- > 0) { + lptr2 = lptr; + lptr2[0] = jpc_fix_mul(lptr2[0], jpc_dbltofix(1.0 / LGAIN)); + ++lptr2; + lptr += stride; + } + hptr = &a[llen * stride]; + n = numrows - llen; + while (n-- > 0) { + hptr2 = hptr; + hptr2[0] = jpc_fix_mul(hptr2[0], jpc_dbltofix(1.0 / HGAIN)); + ++hptr2; + hptr += stride; + } +#endif + + /* Apply the first lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + DELTA), hptr2[0])); + ++lptr2; + ++hptr2; + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(DELTA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + DELTA), hptr2[0])); + ++lptr2; + ++hptr2; + } + + /* Apply the second lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + GAMMA), lptr2[0])); + ++hptr2; + ++lptr2; + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(GAMMA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + GAMMA), lptr2[0])); + ++lptr2; + ++hptr2; + } + + /* Apply the third lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (!parity) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + lptr += stride; + } + n = llen - (!parity) - (parity != (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(BETA), + jpc_fix_add(hptr2[0], hptr2[stride]))); + ++lptr2; + ++hptr2; + lptr += stride; + hptr += stride; + } + if (parity != (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(lptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * BETA), + hptr2[0])); + ++lptr2; + ++hptr2; + } + + /* Apply the fourth lifting step. */ + lptr = &a[0]; + hptr = &a[llen * stride]; + if (parity) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + ALPHA), lptr2[0])); + ++hptr2; + ++lptr2; + hptr += stride; + } + n = numrows - llen - parity - (parity == (numrows & 1)); + while (n-- > 0) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(ALPHA), + jpc_fix_add(lptr2[0], lptr2[stride]))); + ++lptr2; + ++hptr2; + hptr += stride; + lptr += stride; + } + if (parity == (numrows & 1)) { + lptr2 = lptr; + hptr2 = hptr; + jpc_fix_minuseq(hptr2[0], jpc_fix_mul(jpc_dbltofix(2.0 * + ALPHA), lptr2[0])); + ++lptr2; + ++hptr2; + } + + } else { + +#if defined(WT_LENONE) + if (parity) { + lptr2 = &a[0]; + lptr2[0] >>= 1; + ++lptr2; + } +#endif + + } + +} + +int jpc_ns_analyze(jpc_fix_t *a, int xstart, int ystart, int width, int height, + int stride) +{ + + int numrows = height; + int numcols = width; + int rowparity = ystart & 1; + int colparity = xstart & 1; + int i; + jpc_fix_t *startptr; + int maxcols; + + maxcols = (numcols / JPC_QMFB_COLGRPSIZE) * JPC_QMFB_COLGRPSIZE; + startptr = &a[0]; + for (i = 0; i < maxcols; i += JPC_QMFB_COLGRPSIZE) { + jpc_qmfb_split_colgrp(startptr, numrows, stride, rowparity); + jpc_ns_fwdlift_colgrp(startptr, numrows, stride, rowparity); + startptr += JPC_QMFB_COLGRPSIZE; + } + if (maxcols < numcols) { + jpc_qmfb_split_colres(startptr, numrows, numcols - maxcols, stride, + rowparity); + jpc_ns_fwdlift_colres(startptr, numrows, numcols - maxcols, stride, + rowparity); + } + + startptr = &a[0]; + for (i = 0; i < numrows; ++i) { + jpc_qmfb_split_row(startptr, numcols, colparity); + jpc_ns_fwdlift_row(startptr, numcols, colparity); + startptr += stride; + } + + return 0; + +} + +int jpc_ns_synthesize(jpc_fix_t *a, int xstart, int ystart, int width, + int height, int stride) +{ + + int numrows = height; + int numcols = width; + int rowparity = ystart & 1; + int colparity = xstart & 1; + int maxcols; + jpc_fix_t *startptr; + int i; + + startptr = &a[0]; + for (i = 0; i < numrows; ++i) { + jpc_ns_invlift_row(startptr, numcols, colparity); + jpc_qmfb_join_row(startptr, numcols, colparity); + startptr += stride; + } + + maxcols = (numcols / JPC_QMFB_COLGRPSIZE) * JPC_QMFB_COLGRPSIZE; + startptr = &a[0]; + for (i = 0; i < maxcols; i += JPC_QMFB_COLGRPSIZE) { + // GeoJasper: dima - progress + jas_do_progress( i, maxcols, "jpc: synthesize" ); // dima + if (jas_test_abort() == 1) return 0; + + jpc_ns_invlift_colgrp(startptr, numrows, stride, rowparity); + jpc_qmfb_join_colgrp(startptr, numrows, stride, rowparity); + startptr += JPC_QMFB_COLGRPSIZE; + } + if (maxcols < numcols) { + jpc_ns_invlift_colres(startptr, numrows, numcols - maxcols, stride, + rowparity); + jpc_qmfb_join_colres(startptr, numrows, numcols - maxcols, stride, + rowparity); + } + + return 0; + +} + diff --git a/src/libjasper/jpc/jpc_qmfb.h b/src/libjasper/jpc/jpc_qmfb.h new file mode 100644 index 0000000..2586d55 --- /dev/null +++ b/src/libjasper/jpc/jpc_qmfb.h @@ -0,0 +1,113 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2004 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Quadrature Mirror-Image Filter Bank (QMFB) Routines + * + * $Id: jpc_qmfb.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_QMFB_H +#define JPC_QMFB_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_seq.h" + +/******************************************************************************\ +* Constants. +\******************************************************************************/ + +/* QMFB IDs. */ +#define JPC_QMFB1D_FT 1 /* 5/3 */ +#define JPC_QMFB1D_NS 2 /* 9/7 */ + +/******************************************************************************\ +* Types. +\******************************************************************************/ + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +#if !defined(JPC_QMFB_COLGRPSIZE) +/* The number of columns to group together during the vertical processing +stage of the wavelet transform. */ +/* The default value for this parameter is probably not optimal for +any particular platform. Hopefully, it is not too unreasonable, however. */ +#define JPC_QMFB_COLGRPSIZE 16 +#endif + +typedef struct { + int (*analyze)(int *, int, int, int, int, int); + int (*synthesize)(int *, int, int, int, int, int); + double *lpenergywts; + double *hpenergywts; +} jpc_qmfb2d_t; + +extern jpc_qmfb2d_t jpc_ft_qmfb2d; +extern jpc_qmfb2d_t jpc_ns_qmfb2d; + +#endif diff --git a/src/libjasper/jpc/jpc_t1cod.c b/src/libjasper/jpc/jpc_t1cod.c new file mode 100644 index 0000000..ea4d2b1 --- /dev/null +++ b/src/libjasper/jpc/jpc_t1cod.c @@ -0,0 +1,497 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * $Id: jpc_t1cod.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include +#include + +#include "jasper/jas_types.h" +#include "jasper/jas_math.h" + +#include "jpc_bs.h" +#include "jpc_dec.h" +#include "jpc_cs.h" +#include "jpc_mqcod.h" +#include "jpc_t1cod.h" +#include "jpc_tsfb.h" + +double jpc_pow2i(int n); + +/******************************************************************************\ +* Global data. +\******************************************************************************/ + +int jpc_zcctxnolut[4 * 256]; +int jpc_spblut[256]; +int jpc_scctxnolut[256]; +int jpc_magctxnolut[4096]; + +jpc_fix_t jpc_signmsedec[1 << JPC_NMSEDEC_BITS]; +jpc_fix_t jpc_refnmsedec[1 << JPC_NMSEDEC_BITS]; +jpc_fix_t jpc_signmsedec0[1 << JPC_NMSEDEC_BITS]; +jpc_fix_t jpc_refnmsedec0[1 << JPC_NMSEDEC_BITS]; + +jpc_mqctx_t jpc_mqctxs[JPC_NUMCTXS]; + +/******************************************************************************\ +* +\******************************************************************************/ + +void jpc_initmqctxs(void); + +/******************************************************************************\ +* Code. +\******************************************************************************/ + +int JPC_PASSTYPE(int passno) +{ + int passtype; + switch (passno % 3) { + case 0: + passtype = JPC_CLNPASS; + break; + case 1: + passtype = JPC_SIGPASS; + break; + case 2: + passtype = JPC_REFPASS; + break; + default: + passtype = -1; + assert(0); + break; + } + return passtype; +} + +int JPC_NOMINALGAIN(int qmfbid, int numlvls, int lvlno, int orient) +{ + /* Avoid compiler warnings about unused parameters. */ + numlvls = 0; + +if (qmfbid == JPC_COX_INS) { + return 0; +} + assert(qmfbid == JPC_COX_RFT); + if (lvlno == 0) { + assert(orient == JPC_TSFB_LL); + return 0; + } else { + switch (orient) { + case JPC_TSFB_LH: + case JPC_TSFB_HL: + return 1; + break; + case JPC_TSFB_HH: + return 2; + break; + } + } + abort(); +} + +/******************************************************************************\ +* Coding pass related functions. +\******************************************************************************/ + +int JPC_SEGTYPE(int passno, int firstpassno, int bypass) +{ + int passtype; + if (bypass) { + passtype = JPC_PASSTYPE(passno); + if (passtype == JPC_CLNPASS) { + return JPC_SEG_MQ; + } + return ((passno < firstpassno + 10) ? JPC_SEG_MQ : JPC_SEG_RAW); + } else { + return JPC_SEG_MQ; + } +} + +int JPC_SEGPASSCNT(int passno, int firstpassno, int numpasses, int bypass, int termall) +{ + int ret; + int passtype; + + if (termall) { + ret = 1; + } else if (bypass) { + if (passno < firstpassno + 10) { + ret = 10 - (passno - firstpassno); + } else { + passtype = JPC_PASSTYPE(passno); + switch (passtype) { + case JPC_SIGPASS: + ret = 2; + break; + case JPC_REFPASS: + ret = 1; + break; + case JPC_CLNPASS: + ret = 1; + break; + default: + ret = -1; + assert(0); + break; + } + } + } else { + ret = JPC_PREC * 3 - 2; + } + ret = JAS_MIN(ret, numpasses - passno); + return ret; +} + +int JPC_ISTERMINATED(int passno, int firstpassno, int numpasses, int termall, + int lazy) +{ + int ret; + int n; + if (passno - firstpassno == numpasses - 1) { + ret = 1; + } else { + n = JPC_SEGPASSCNT(passno, firstpassno, numpasses, lazy, termall); + ret = (n <= 1) ? 1 : 0; + } + + return ret; +} + +/******************************************************************************\ +* Lookup table code. +\******************************************************************************/ + +void jpc_initluts() +{ + int i; + int orient; + int refine; + float u; + float v; + float t; + +/* XXX - hack */ +jpc_initmqctxs(); + + for (orient = 0; orient < 4; ++orient) { + for (i = 0; i < 256; ++i) { + jpc_zcctxnolut[(orient << 8) | i] = jpc_getzcctxno(i, orient); + } + } + + for (i = 0; i < 256; ++i) { + jpc_spblut[i] = jpc_getspb(i << 4); + } + + for (i = 0; i < 256; ++i) { + jpc_scctxnolut[i] = jpc_getscctxno(i << 4); + } + + for (refine = 0; refine < 2; ++refine) { + for (i = 0; i < 2048; ++i) { + jpc_magctxnolut[(refine << 11) + i] = jpc_getmagctxno((refine ? JPC_REFINE : 0) | i); + } + } + + for (i = 0; i < (1 << JPC_NMSEDEC_BITS); ++i) { + t = i * jpc_pow2i(-JPC_NMSEDEC_FRACBITS); + u = t; + v = t - 1.5; + jpc_signmsedec[i] = jpc_dbltofix(floor((u * u - v * v) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS)); +/* XXX - this calc is not correct */ + jpc_signmsedec0[i] = jpc_dbltofix(floor((u * u) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS)); + u = t - 1.0; + if (i & (1 << (JPC_NMSEDEC_BITS - 1))) { + v = t - 1.5; + } else { + v = t - 0.5; + } + jpc_refnmsedec[i] = jpc_dbltofix(floor((u * u - v * v) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS)); +/* XXX - this calc is not correct */ + jpc_refnmsedec0[i] = jpc_dbltofix(floor((u * u) * jpc_pow2i(JPC_NMSEDEC_FRACBITS) + 0.5) / jpc_pow2i(JPC_NMSEDEC_FRACBITS)); + } +} + +jpc_fix_t jpc_getsignmsedec_func(jpc_fix_t x, int bitpos) +{ + jpc_fix_t y; + assert(!(x & (~JAS_ONES(bitpos + 1)))); + y = jpc_getsignmsedec_macro(x, bitpos); + return y; +} + +int jpc_getzcctxno(int f, int orient) +{ + int h; + int v; + int d; + int n; + int t; + int hv; + + /* Avoid compiler warning. */ + n = 0; + + h = ((f & JPC_WSIG) != 0) + ((f & JPC_ESIG) != 0); + v = ((f & JPC_NSIG) != 0) + ((f & JPC_SSIG) != 0); + d = ((f & JPC_NWSIG) != 0) + ((f & JPC_NESIG) != 0) + ((f & JPC_SESIG) != 0) + ((f & JPC_SWSIG) != 0); + switch (orient) { + case JPC_TSFB_HL: + t = h; + h = v; + v = t; + case JPC_TSFB_LL: + case JPC_TSFB_LH: + if (!h) { + if (!v) { + if (!d) { + n = 0; + } else if (d == 1) { + n = 1; + } else { + n = 2; + } + } else if (v == 1) { + n = 3; + } else { + n = 4; + } + } else if (h == 1) { + if (!v) { + if (!d) { + n = 5; + } else { + n = 6; + } + } else { + n = 7; + } + } else { + n = 8; + } + break; + case JPC_TSFB_HH: + hv = h + v; + if (!d) { + if (!hv) { + n = 0; + } else if (hv == 1) { + n = 1; + } else { + n = 2; + } + } else if (d == 1) { + if (!hv) { + n = 3; + } else if (hv == 1) { + n = 4; + } else { + n = 5; + } + } else if (d == 2) { + if (!hv) { + n = 6; + } else { + n = 7; + } + } else { + n = 8; + } + break; + } + assert(n < JPC_NUMZCCTXS); + return JPC_ZCCTXNO + n; +} + +int jpc_getspb(int f) +{ + int hc; + int vc; + int n; + + hc = JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == JPC_ESIG) + ((f & (JPC_WSIG | JPC_WSGN)) == JPC_WSIG), 1) - + JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == (JPC_ESIG | JPC_ESGN)) + ((f & (JPC_WSIG | JPC_WSGN)) == (JPC_WSIG | JPC_WSGN)), 1); + vc = JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == JPC_NSIG) + ((f & (JPC_SSIG | JPC_SSGN)) == JPC_SSIG), 1) - + JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == (JPC_NSIG | JPC_NSGN)) + ((f & (JPC_SSIG | JPC_SSGN)) == (JPC_SSIG | JPC_SSGN)), 1); + if (!hc && !vc) { + n = 0; + } else { + n = (!(hc > 0 || (!hc && vc > 0))); + } + return n; +} + +int jpc_getscctxno(int f) +{ + int hc; + int vc; + int n; + + /* Avoid compiler warning. */ + n = 0; + + hc = JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == JPC_ESIG) + ((f & (JPC_WSIG | JPC_WSGN)) == JPC_WSIG), + 1) - JAS_MIN(((f & (JPC_ESIG | JPC_ESGN)) == (JPC_ESIG | JPC_ESGN)) + + ((f & (JPC_WSIG | JPC_WSGN)) == (JPC_WSIG | JPC_WSGN)), 1); + vc = JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == JPC_NSIG) + ((f & (JPC_SSIG | JPC_SSGN)) == JPC_SSIG), + 1) - JAS_MIN(((f & (JPC_NSIG | JPC_NSGN)) == (JPC_NSIG | JPC_NSGN)) + + ((f & (JPC_SSIG | JPC_SSGN)) == (JPC_SSIG | JPC_SSGN)), 1); + assert(hc >= -1 && hc <= 1 && vc >= -1 && vc <= 1); + if (hc < 0) { + hc = -hc; + vc = -vc; + } + if (!hc) { + if (vc == -1) { + n = 1; + } else if (!vc) { + n = 0; + } else { + n = 1; + } + } else if (hc == 1) { + if (vc == -1) { + n = 2; + } else if (!vc) { + n = 3; + } else { + n = 4; + } + } + assert(n < JPC_NUMSCCTXS); + return JPC_SCCTXNO + n; +} + +int jpc_getmagctxno(int f) +{ + int n; + + if (!(f & JPC_REFINE)) { + n = (f & (JPC_OTHSIGMSK)) ? 1 : 0; + } else { + n = 2; + } + + assert(n < JPC_NUMMAGCTXS); + return JPC_MAGCTXNO + n; +} + +void jpc_initctxs(jpc_mqctx_t *ctxs) +{ + jpc_mqctx_t *ctx; + int i; + + ctx = ctxs; + for (i = 0; i < JPC_NUMCTXS; ++i) { + ctx->mps = 0; + switch (i) { + case JPC_UCTXNO: + ctx->ind = 46; + break; + case JPC_ZCCTXNO: + ctx->ind = 4; + break; + case JPC_AGGCTXNO: + ctx->ind = 3; + break; + default: + ctx->ind = 0; + break; + } + ++ctx; + } +} + +void jpc_initmqctxs() +{ + jpc_initctxs(jpc_mqctxs); +} + +/* Calculate the real quantity exp2(n), where x is an integer. */ +double jpc_pow2i(int n) +{ + double x; + double a; + + x = 1.0; + if (n < 0) { + a = 0.5; + n = -n; + } else { + a = 2.0; + } + while (--n >= 0) { + x *= a; + } + return x; +} diff --git a/src/libjasper/jpc/jpc_t1cod.h b/src/libjasper/jpc/jpc_t1cod.h new file mode 100644 index 0000000..065c0af --- /dev/null +++ b/src/libjasper/jpc/jpc_t1cod.h @@ -0,0 +1,295 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * $Id: jpc_t1cod.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_T1COD_H +#define JPC_T1COD_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_fix.h" +#include "jasper/jas_math.h" + +#include "jpc_mqcod.h" +#include "jpc_tsfb.h" + +/******************************************************************************\ +* Constants. +\******************************************************************************/ + +/* The number of bits used to index into various lookup tables. */ +#define JPC_NMSEDEC_BITS 7 +#define JPC_NMSEDEC_FRACBITS (JPC_NMSEDEC_BITS - 1) + +/* + * Segment types. + */ + +/* Invalid. */ +#define JPC_SEG_INVALID 0 +/* MQ. */ +#define JPC_SEG_MQ 1 +/* Raw. */ +#define JPC_SEG_RAW 2 + +/* The nominal word size. */ +#define JPC_PREC 32 + +/* Tier-1 coding pass types. */ +#define JPC_SIGPASS 0 /* significance */ +#define JPC_REFPASS 1 /* refinement */ +#define JPC_CLNPASS 2 /* cleanup */ + +/* + * Per-sample state information for tier-1 coding. + */ + +/* The northeast neighbour has been found to be significant. */ +#define JPC_NESIG 0x0001 +/* The southeast neighbour has been found to be significant. */ +#define JPC_SESIG 0x0002 +/* The southwest neighbour has been found to be significant. */ +#define JPC_SWSIG 0x0004 +/* The northwest neighbour has been found to be significant. */ +#define JPC_NWSIG 0x0008 +/* The north neighbour has been found to be significant. */ +#define JPC_NSIG 0x0010 +/* The east neighbour has been found to be significant. */ +#define JPC_ESIG 0x0020 +/* The south neighbour has been found to be significant. */ +#define JPC_SSIG 0x0040 +/* The west neighbour has been found to be significant. */ +#define JPC_WSIG 0x0080 +/* The significance mask for 8-connected neighbours. */ +#define JPC_OTHSIGMSK \ + (JPC_NSIG | JPC_NESIG | JPC_ESIG | JPC_SESIG | JPC_SSIG | JPC_SWSIG | JPC_WSIG | JPC_NWSIG) +/* The significance mask for 4-connected neighbours. */ +#define JPC_PRIMSIGMSK (JPC_NSIG | JPC_ESIG | JPC_SSIG | JPC_WSIG) + +/* The north neighbour is negative in value. */ +#define JPC_NSGN 0x0100 +/* The east neighbour is negative in value. */ +#define JPC_ESGN 0x0200 +/* The south neighbour is negative in value. */ +#define JPC_SSGN 0x0400 +/* The west neighbour is negative in value. */ +#define JPC_WSGN 0x0800 +/* The sign mask for 4-connected neighbours. */ +#define JPC_SGNMSK (JPC_NSGN | JPC_ESGN | JPC_SSGN | JPC_WSGN) + +/* This sample has been found to be significant. */ +#define JPC_SIG 0x1000 +/* The sample has been refined. */ +#define JPC_REFINE 0x2000 +/* This sample has been processed during the significance pass. */ +#define JPC_VISIT 0x4000 + +/* The number of aggregation contexts. */ +#define JPC_NUMAGGCTXS 1 +/* The number of zero coding contexts. */ +#define JPC_NUMZCCTXS 9 +/* The number of magnitude contexts. */ +#define JPC_NUMMAGCTXS 3 +/* The number of sign coding contexts. */ +#define JPC_NUMSCCTXS 5 +/* The number of uniform contexts. */ +#define JPC_NUMUCTXS 1 + +/* The context ID for the first aggregation context. */ +#define JPC_AGGCTXNO 0 +/* The context ID for the first zero coding context. */ +#define JPC_ZCCTXNO (JPC_AGGCTXNO + JPC_NUMAGGCTXS) +/* The context ID for the first magnitude context. */ +#define JPC_MAGCTXNO (JPC_ZCCTXNO + JPC_NUMZCCTXS) +/* The context ID for the first sign coding context. */ +#define JPC_SCCTXNO (JPC_MAGCTXNO + JPC_NUMMAGCTXS) +/* The context ID for the first uniform context. */ +#define JPC_UCTXNO (JPC_SCCTXNO + JPC_NUMSCCTXS) +/* The total number of contexts. */ +#define JPC_NUMCTXS (JPC_UCTXNO + JPC_NUMUCTXS) + +/******************************************************************************\ +* External data. +\******************************************************************************/ + +/* These lookup tables are used by various macros/functions. */ +/* Do not access these lookup tables directly. */ +extern int jpc_zcctxnolut[]; +extern int jpc_spblut[]; +extern int jpc_scctxnolut[]; +extern int jpc_magctxnolut[]; +extern jpc_fix_t jpc_refnmsedec[]; +extern jpc_fix_t jpc_signmsedec[]; +extern jpc_fix_t jpc_refnmsedec0[]; +extern jpc_fix_t jpc_signmsedec0[]; + +/* The initial settings for the MQ contexts. */ +extern jpc_mqctx_t jpc_mqctxs[]; + +/******************************************************************************\ +* Functions and macros. +\******************************************************************************/ + +/* Initialize the MQ contexts. */ +void jpc_initctxs(jpc_mqctx_t *ctxs); + +/* Get the zero coding context. */ +int jpc_getzcctxno(int f, int orient); +#define JPC_GETZCCTXNO(f, orient) \ + (jpc_zcctxnolut[((orient) << 8) | ((f) & JPC_OTHSIGMSK)]) + +/* Get the sign prediction bit. */ +int jpc_getspb(int f); +#define JPC_GETSPB(f) \ + (jpc_spblut[((f) & (JPC_PRIMSIGMSK | JPC_SGNMSK)) >> 4]) + +/* Get the sign coding context. */ +int jpc_getscctxno(int f); +#define JPC_GETSCCTXNO(f) \ + (jpc_scctxnolut[((f) & (JPC_PRIMSIGMSK | JPC_SGNMSK)) >> 4]) + +/* Get the magnitude context. */ +int jpc_getmagctxno(int f); +#define JPC_GETMAGCTXNO(f) \ + (jpc_magctxnolut[((f) & JPC_OTHSIGMSK) | ((((f) & JPC_REFINE) != 0) << 11)]) + +/* Get the normalized MSE reduction for significance passes. */ +#define JPC_GETSIGNMSEDEC(x, bitpos) jpc_getsignmsedec_macro(x, bitpos) +jpc_fix_t jpc_getsignmsedec_func(jpc_fix_t x, int bitpos); +#define jpc_getsignmsedec_macro(x, bitpos) \ + ((bitpos > JPC_NMSEDEC_FRACBITS) ? jpc_signmsedec[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)] : \ + (jpc_signmsedec0[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)])) + +/* Get the normalized MSE reduction for refinement passes. */ +#define JPC_GETREFNMSEDEC(x, bitpos) jpc_getrefnmsedec_macro(x, bitpos) +jpc_fix_t jpc_refsignmsedec_func(jpc_fix_t x, int bitpos); +#define jpc_getrefnmsedec_macro(x, bitpos) \ + ((bitpos > JPC_NMSEDEC_FRACBITS) ? jpc_refnmsedec[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)] : \ + (jpc_refnmsedec0[JPC_ASR(x, bitpos - JPC_NMSEDEC_FRACBITS) & JAS_ONES(JPC_NMSEDEC_BITS)])) + +/* Arithmetic shift right (with ability to shift left also). */ +#define JPC_ASR(x, n) \ + (((n) >= 0) ? ((x) >> (n)) : ((x) << (-(n)))) + +/* Update the per-sample state information. */ +#define JPC_UPDATEFLAGS4(fp, rowstep, s, vcausalflag) \ +{ \ + register jpc_fix_t *np = (fp) - (rowstep); \ + register jpc_fix_t *sp = (fp) + (rowstep); \ + if ((vcausalflag)) { \ + sp[-1] |= JPC_NESIG; \ + sp[1] |= JPC_NWSIG; \ + if (s) { \ + *sp |= JPC_NSIG | JPC_NSGN; \ + (fp)[-1] |= JPC_ESIG | JPC_ESGN; \ + (fp)[1] |= JPC_WSIG | JPC_WSGN; \ + } else { \ + *sp |= JPC_NSIG; \ + (fp)[-1] |= JPC_ESIG; \ + (fp)[1] |= JPC_WSIG; \ + } \ + } else { \ + np[-1] |= JPC_SESIG; \ + np[1] |= JPC_SWSIG; \ + sp[-1] |= JPC_NESIG; \ + sp[1] |= JPC_NWSIG; \ + if (s) { \ + *np |= JPC_SSIG | JPC_SSGN; \ + *sp |= JPC_NSIG | JPC_NSGN; \ + (fp)[-1] |= JPC_ESIG | JPC_ESGN; \ + (fp)[1] |= JPC_WSIG | JPC_WSGN; \ + } else { \ + *np |= JPC_SSIG; \ + *sp |= JPC_NSIG; \ + (fp)[-1] |= JPC_ESIG; \ + (fp)[1] |= JPC_WSIG; \ + } \ + } \ +} + +/* Initialize the lookup tables used by the codec. */ +void jpc_initluts(void); + +/* Get the nominal gain associated with a particular band. */ +int JPC_NOMINALGAIN(int qmfbid, int numlvls, int lvlno, int orient); + +/* Get the coding pass type. */ +int JPC_PASSTYPE(int passno); + +/* Get the segment type. */ +int JPC_SEGTYPE(int passno, int firstpassno, int bypass); + +/* Get the number of coding passess in the segment. */ +int JPC_SEGPASSCNT(int passno, int firstpassno, int numpasses, int bypass, + int termall); + +/* Is the coding pass terminated? */ +int JPC_ISTERMINATED(int passno, int firstpassno, int numpasses, int termall, + int lazy); + +#endif diff --git a/src/libjasper/jpc/jpc_t1dec.c b/src/libjasper/jpc/jpc_t1dec.c new file mode 100644 index 0000000..50df081 --- /dev/null +++ b/src/libjasper/jpc/jpc_t1dec.c @@ -0,0 +1,927 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier 1 Decoder + * + * $Id: jpc_t1dec.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jasper/jas_fix.h" +#include "jasper/jas_stream.h" +#include "jasper/jas_math.h" + +#include "jpc_bs.h" +#include "jpc_mqdec.h" +#include "jpc_t1dec.h" +#include "jpc_t1cod.h" +#include "jpc_dec.h" + +/******************************************************************************\ +* +\******************************************************************************/ + +static int jpc_dec_decodecblk(jpc_dec_t *dec, jpc_dec_tile_t *tile, jpc_dec_tcomp_t *tcomp, jpc_dec_band_t *band, + jpc_dec_cblk_t *cblk, int dopartial, int maxlyrs); +static int dec_sigpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos, int orient, + int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data); +static int dec_rawsigpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, + int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data); +static int dec_refpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos, int vcausalflag, + jas_matrix_t *flags, jas_matrix_t *data); +static int dec_rawrefpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, + int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data); +static int dec_clnpass(jpc_dec_t *dec, jpc_mqdec_t *mqdec, int bitpos, int orient, + int vcausalflag, int segsymflag, jas_matrix_t *flags, jas_matrix_t *data); + +#if defined(DEBUG) +static long t1dec_cnt = 0; +#endif + +#if !defined(DEBUG) +#define JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename) \ + ((v) = jpc_mqdec_getbit(mqdec)) +#else +#define JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename) \ +{ \ + (v) = jpc_mqdec_getbit(mqdec); \ + if (jas_getdbglevel() >= 100) { \ + jas_eprintf("index = %ld; passtype = %s; symtype = %s; sym = %d\n", t1dec_cnt, passtypename, symtypename, v); \ + ++t1dec_cnt; \ + } \ +} +#endif +#define JPC_T1D_GETBITNOSKEW(mqdec, v, passtypename, symtypename) \ + JPC_T1D_GETBIT(mqdec, v, passtypename, symtypename) + +#if !defined(DEBUG) +#define JPC_T1D_RAWGETBIT(bitstream, v, passtypename, symtypename) \ + ((v) = jpc_bitstream_getbit(bitstream)) +#else +#define JPC_T1D_RAWGETBIT(bitstream, v, passtypename, symtypename) \ +{ \ + (v) = jpc_bitstream_getbit(bitstream); \ + if (jas_getdbglevel() >= 100) { \ + jas_eprintf("index = %ld; passtype = %s; symtype = %s; sym = %d\n", t1dec_cnt, passtypename, symtypename, v); \ + ++t1dec_cnt; \ + } \ +} +#endif + +/******************************************************************************\ +* Code. +\******************************************************************************/ + +int jpc_dec_decodecblks(jpc_dec_t *dec, jpc_dec_tile_t *tile) +{ + jpc_dec_tcomp_t *tcomp; + int compcnt; + jpc_dec_rlvl_t *rlvl; + int rlvlcnt; + jpc_dec_band_t *band; + int bandcnt; + jpc_dec_prc_t *prc; + int prccnt; + jpc_dec_cblk_t *cblk; + int cblkcnt; + + for (compcnt = dec->numcomps, tcomp = tile->tcomps; compcnt > 0; + --compcnt, ++tcomp) { + for (rlvlcnt = tcomp->numrlvls, rlvl = tcomp->rlvls; + rlvlcnt > 0; --rlvlcnt, ++rlvl) { + + // GeoJasper: dima - progress + jas_do_progress( (tcomp->numrlvls-rlvlcnt), (tcomp->numrlvls-1), "jpc: decode" ); + if (jas_test_abort() == 1) return -1; + + if (!rlvl->bands) { + continue; + } + for (bandcnt = rlvl->numbands, band = rlvl->bands; + bandcnt > 0; --bandcnt, ++band) { + if (!band->data) { + continue; + } + for (prccnt = rlvl->numprcs, prc = band->prcs; + prccnt > 0; --prccnt, ++prc) { + if (!prc->cblks) { + continue; + } + for (cblkcnt = prc->numcblks, + cblk = prc->cblks; cblkcnt > 0; + --cblkcnt, ++cblk) { + if (jpc_dec_decodecblk(dec, tile, tcomp, + band, cblk, 1, JPC_MAXLYRS)) { + return -1; + } + } + } + + } + } + } + + return 0; +} + +static int jpc_dec_decodecblk(jpc_dec_t *dec, jpc_dec_tile_t *tile, jpc_dec_tcomp_t *tcomp, jpc_dec_band_t *band, + jpc_dec_cblk_t *cblk, int dopartial, int maxlyrs) +{ + jpc_dec_seg_t *seg; + int i; + int bpno; + int passtype; + int ret; + int compno; + int filldata; + int fillmask; + jpc_dec_ccp_t *ccp; + + compno = tcomp - tile->tcomps; + + if (!cblk->flags) { + /* Note: matrix is assumed to be zeroed */ + if (!(cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + + 2, jas_matrix_numcols(cblk->data) + 2))) { + return -1; + } + } + + seg = cblk->segs.head; + while (seg && (seg != cblk->curseg || dopartial) && (maxlyrs < 0 || + seg->lyrno < maxlyrs)) { + assert(seg->numpasses >= seg->maxpasses || dopartial); + assert(seg->stream); + jas_stream_rewind(seg->stream); + jas_stream_setrwcount(seg->stream, 0); + if (seg->type == JPC_SEG_MQ) { + if (!cblk->mqdec) { + if (!(cblk->mqdec = jpc_mqdec_create(JPC_NUMCTXS, 0))) { + return -1; + } + jpc_mqdec_setctxs(cblk->mqdec, JPC_NUMCTXS, jpc_mqctxs); + } + jpc_mqdec_setinput(cblk->mqdec, seg->stream); + jpc_mqdec_init(cblk->mqdec); + } else { + assert(seg->type == JPC_SEG_RAW); + if (!cblk->nulldec) { + if (!(cblk->nulldec = jpc_bitstream_sopen(seg->stream, "r"))) { + assert(0); + } + } + } + + + for (i = 0; i < seg->numpasses; ++i) { + if (cblk->numimsbs > band->numbps) { + ccp = &tile->cp->ccps[compno]; + if (ccp->roishift <= 0) { + jas_eprintf("warning: corrupt code stream\n"); + } else { + if (cblk->numimsbs < ccp->roishift - band->numbps) { + jas_eprintf("warning: corrupt code stream\n"); + } + } + } + bpno = band->roishift + band->numbps - 1 - (cblk->numimsbs + + (seg->passno + i - cblk->firstpassno + 2) / 3); +if (bpno < 0) { + goto premature_exit; +} +#if 1 + passtype = (seg->passno + i + 2) % 3; +#else + passtype = JPC_PASSTYPE(seg->passno + i + 2); +#endif + assert(bpno >= 0 && bpno < 31); + switch (passtype) { + case JPC_SIGPASS: + ret = (seg->type == JPC_SEG_MQ) ? dec_sigpass(dec, + cblk->mqdec, bpno, band->orient, + (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, + cblk->flags, cblk->data) : + dec_rawsigpass(dec, cblk->nulldec, bpno, + (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, + cblk->flags, cblk->data); + break; + case JPC_REFPASS: + ret = (seg->type == JPC_SEG_MQ) ? + dec_refpass(dec, cblk->mqdec, bpno, + (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, + cblk->flags, cblk->data) : + dec_rawrefpass(dec, cblk->nulldec, bpno, + (tile->cp->ccps[compno].cblkctx & JPC_COX_VSC) != 0, + cblk->flags, cblk->data); + break; + case JPC_CLNPASS: + assert(seg->type == JPC_SEG_MQ); + ret = dec_clnpass(dec, cblk->mqdec, bpno, + band->orient, (tile->cp->ccps[compno].cblkctx & + JPC_COX_VSC) != 0, (tile->cp->ccps[compno].cblkctx & + JPC_COX_SEGSYM) != 0, cblk->flags, + cblk->data); + break; + default: + ret = -1; + break; + } + /* Do we need to reset after each coding pass? */ + if (tile->cp->ccps[compno].cblkctx & JPC_COX_RESET) { + jpc_mqdec_setctxs(cblk->mqdec, JPC_NUMCTXS, jpc_mqctxs); + } + + if (ret) { + jas_eprintf("coding pass failed passtype=%d segtype=%d\n", passtype, seg->type); + return -1; + } + + } + + if (seg->type == JPC_SEG_MQ) { +/* Note: dont destroy mq decoder because context info will be lost */ + } else { + assert(seg->type == JPC_SEG_RAW); + if (tile->cp->ccps[compno].cblkctx & JPC_COX_PTERM) { + fillmask = 0x7f; + filldata = 0x2a; + } else { + fillmask = 0; + filldata = 0; + } + if ((ret = jpc_bitstream_inalign(cblk->nulldec, fillmask, + filldata)) < 0) { + return -1; + } else if (ret > 0) { + jas_eprintf("warning: bad termination pattern detected\n"); + } + jpc_bitstream_close(cblk->nulldec); + cblk->nulldec = 0; + } + + cblk->curseg = seg->next; + jpc_seglist_remove(&cblk->segs, seg); + jpc_seg_destroy(seg); + seg = cblk->curseg; + } + + assert(dopartial ? (!cblk->curseg) : 1); + +premature_exit: + return 0; +} + +/******************************************************************************\ +* Code for significance pass. +\******************************************************************************/ + +#define jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf, orient, mqdec, vcausalflag) \ +{ \ + int f; \ + int v; \ + f = *(fp); \ + if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ + jpc_mqdec_setcurctx((mqdec), JPC_GETZCCTXNO(f, (orient))); \ + JPC_T1D_GETBIT((mqdec), v, "SIG", "ZC"); \ + if (v) { \ + jpc_mqdec_setcurctx((mqdec), JPC_GETSCCTXNO(f)); \ + JPC_T1D_GETBIT((mqdec), v, "SIG", "SC"); \ + v ^= JPC_GETSPB(f); \ + JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \ + *(fp) |= JPC_SIG; \ + *(dp) = (v) ? (-(oneplushalf)) : (oneplushalf); \ + } \ + *(fp) |= JPC_VISIT; \ + } \ +} + +static int dec_sigpass(jpc_dec_t *dec, register jpc_mqdec_t *mqdec, int bitpos, int orient, + int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data) +{ + int i; + int j; + int one; + int half; + int oneplushalf; + int vscanlen; + int width; + int height; + jpc_fix_t *fp; + int frowstep; + int fstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dp; + int drowstep; + int dstripestep; + jpc_fix_t *dstripestart; + jpc_fix_t *dvscanstart; + int k; + + /* Avoid compiler warning about unused parameters. */ + dec = 0; + + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << bitpos; + half = one >> 1; + oneplushalf = one | half; + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + /* Process first sample in vertical scan. */ + jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf, + orient, mqdec, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process second sample in vertical scan. */ + jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf, + orient, mqdec, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process third sample in vertical scan. */ + jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf, + orient, mqdec, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process fourth sample in vertical scan. */ + jpc_sigpass_step(fp, frowstep, dp, bitpos, oneplushalf, + orient, mqdec, 0); + } + } + return 0; +} + +#define jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, in, vcausalflag) \ +{ \ + jpc_fix_t f = *(fp); \ + jpc_fix_t v; \ + if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ + JPC_T1D_RAWGETBIT(in, v, "SIG", "ZC"); \ + if (v < 0) { \ + return -1; \ + } \ + if (v) { \ + JPC_T1D_RAWGETBIT(in, v, "SIG", "SC"); \ + if (v < 0) { \ + return -1; \ + } \ + JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \ + *(fp) |= JPC_SIG; \ + *(dp) = v ? (-oneplushalf) : (oneplushalf); \ + } \ + *(fp) |= JPC_VISIT; \ + } \ +} + +static int dec_rawsigpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, int vcausalflag, + jas_matrix_t *flags, jas_matrix_t *data) +{ + int i; + int j; + int k; + int one; + int half; + int oneplushalf; + int vscanlen; + int width; + int height; + jpc_fix_t *fp; + int frowstep; + int fstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dp; + int drowstep; + int dstripestep; + jpc_fix_t *dstripestart; + jpc_fix_t *dvscanstart; + + /* Avoid compiler warning about unused parameters. */ + dec = 0; + + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << bitpos; + half = one >> 1; + oneplushalf = one | half; + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + /* Process first sample in vertical scan. */ + jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, + in, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process second sample in vertical scan. */ + jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, + in, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process third sample in vertical scan. */ + jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, + in, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process fourth sample in vertical scan. */ + jpc_rawsigpass_step(fp, frowstep, dp, oneplushalf, + in, 0); + + } + } + return 0; +} + +/******************************************************************************\ +* Code for refinement pass. +\******************************************************************************/ + +#define jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, vcausalflag) \ +{ \ + int v; \ + int t; \ + if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ + jpc_mqdec_setcurctx((mqdec), JPC_GETMAGCTXNO(*(fp))); \ + JPC_T1D_GETBITNOSKEW((mqdec), v, "REF", "MR"); \ + t = (v ? (poshalf) : (neghalf)); \ + *(dp) += (*(dp) < 0) ? (-t) : t; \ + *(fp) |= JPC_REFINE; \ + } \ +} + +static int dec_refpass(jpc_dec_t *dec, register jpc_mqdec_t *mqdec, int bitpos, + int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data) +{ + int i; + int j; + int vscanlen; + int width; + int height; + int one; + int poshalf; + int neghalf; + jpc_fix_t *fp; + int frowstep; + int fstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dp; + int drowstep; + int dstripestep; + jpc_fix_t *dstripestart; + jpc_fix_t *dvscanstart; + int k; + + /* Avoid compiler warning about unused parameters. */ + dec = 0; + vcausalflag = 0; + + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << bitpos; + poshalf = one >> 1; + neghalf = (bitpos > 0) ? (-poshalf) : (-1); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + /* Process first sample in vertical scan. */ + jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, + vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process second sample in vertical scan. */ + jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process third sample in vertical scan. */ + jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process fourth sample in vertical scan. */ + jpc_refpass_step(fp, dp, poshalf, neghalf, mqdec, 0); + } + } + + return 0; +} + +#define jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, vcausalflag) \ +{ \ + jpc_fix_t v; \ + jpc_fix_t t; \ + if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ + JPC_T1D_RAWGETBIT(in, v, "REF", "MAGREF"); \ + if (v < 0) { \ + return -1; \ + } \ + t = (v ? poshalf : neghalf); \ + *(dp) += (*(dp) < 0) ? (-t) : t; \ + *(fp) |= JPC_REFINE; \ + } \ +} + +static int dec_rawrefpass(jpc_dec_t *dec, jpc_bitstream_t *in, int bitpos, int vcausalflag, + jas_matrix_t *flags, jas_matrix_t *data) +{ + int i; + int j; + int k; + int vscanlen; + int width; + int height; + int one; + int poshalf; + int neghalf; + jpc_fix_t *fp; + int frowstep; + int fstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dp; + int drowstep; + int dstripestep; + jpc_fix_t *dstripestart; + jpc_fix_t *dvscanstart; + + /* Avoid compiler warning about unused parameters. */ + dec = 0; + vcausalflag = 0; + + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << bitpos; + poshalf = one >> 1; + neghalf = (bitpos > 0) ? (-poshalf) : (-1); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + /* Process first sample in vertical scan. */ + jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, + vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process second sample in vertical scan. */ + jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process third sample in vertical scan. */ + jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process fourth sample in vertical scan. */ + jpc_rawrefpass_step(fp, dp, poshalf, neghalf, in, 0); + } + } + return 0; +} + +/******************************************************************************\ +* Code for cleanup pass. +\******************************************************************************/ + +#define jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, mqdec, flabel, plabel, vcausalflag) \ +{ \ + int v; \ +flabel \ + if (!((f) & (JPC_SIG | JPC_VISIT))) { \ + jpc_mqdec_setcurctx((mqdec), JPC_GETZCCTXNO((f), (orient))); \ + JPC_T1D_GETBIT((mqdec), v, "CLN", "ZC"); \ + if (v) { \ +plabel \ + /* Coefficient is significant. */ \ + jpc_mqdec_setcurctx((mqdec), JPC_GETSCCTXNO(f)); \ + JPC_T1D_GETBIT((mqdec), v, "CLN", "SC"); \ + v ^= JPC_GETSPB(f); \ + *(dp) = (v) ? (-(oneplushalf)) : (oneplushalf); \ + JPC_UPDATEFLAGS4((fp), (frowstep), v, (vcausalflag)); \ + *(fp) |= JPC_SIG; \ + } \ + } \ + /* XXX - Is this correct? Can aggregation cause some VISIT bits not to be reset? Check. */ \ + *(fp) &= ~JPC_VISIT; \ +} + +static int dec_clnpass(jpc_dec_t *dec, register jpc_mqdec_t *mqdec, int bitpos, int orient, + int vcausalflag, int segsymflag, jas_matrix_t *flags, jas_matrix_t *data) +{ + int i; + int j; + int k; + int vscanlen; + int v; + int half; + int runlen; + int f; + int width; + int height; + int one; + int oneplushalf; + + jpc_fix_t *fp; + int frowstep; + int fstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *fvscanstart; + + jpc_fix_t *dp; + int drowstep; + int dstripestep; + jpc_fix_t *dstripestart; + jpc_fix_t *dvscanstart; + + /* Avoid compiler warning about unused parameters. */ + dec = 0; + + one = 1 << bitpos; + half = one >> 1; + oneplushalf = one | half; + + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = 0; i < height; i += 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(4, height - i); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + if (vscanlen >= 4 && (!((*fp) & (JPC_SIG | JPC_VISIT | + JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & (JPC_SIG | + JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & + (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, + !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) { + + jpc_mqdec_setcurctx(mqdec, JPC_AGGCTXNO); + JPC_T1D_GETBIT(mqdec, v, "CLN", "AGG"); + if (!v) { + continue; + } + jpc_mqdec_setcurctx(mqdec, JPC_UCTXNO); + JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL"); + runlen = v; + JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "RL"); + runlen = (runlen << 1) | v; + f = *(fp = fvscanstart + frowstep * runlen); + dp = dvscanstart + drowstep * runlen; + k = vscanlen - runlen; + switch (runlen) { + case 0: + goto clnpass_partial0; + break; + case 1: + goto clnpass_partial1; + break; + case 2: + goto clnpass_partial2; + break; + case 3: + goto clnpass_partial3; + break; + } + } else { + f = *(fp = fvscanstart); + dp = dvscanstart; + k = vscanlen; + goto clnpass_full0; + } + + /* Process first sample in vertical scan. */ + jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, + mqdec, clnpass_full0:, clnpass_partial0:, + vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process second sample in vertical scan. */ + f = *fp; + jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, + mqdec, ;, clnpass_partial1:, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process third sample in vertical scan. */ + f = *fp; + jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, + mqdec, ;, clnpass_partial2:, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + /* Process fourth sample in vertical scan. */ + f = *fp; + jpc_clnpass_step(f, fp, frowstep, dp, oneplushalf, orient, + mqdec, ;, clnpass_partial3:, 0); + } + } + + if (segsymflag) { + int segsymval; + segsymval = 0; + jpc_mqdec_setcurctx(mqdec, JPC_UCTXNO); + JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM"); + segsymval = (segsymval << 1) | (v & 1); + JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM"); + segsymval = (segsymval << 1) | (v & 1); + JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM"); + segsymval = (segsymval << 1) | (v & 1); + JPC_T1D_GETBITNOSKEW(mqdec, v, "CLN", "SEGSYM"); + segsymval = (segsymval << 1) | (v & 1); + if (segsymval != 0xa) { + jas_eprintf("warning: bad segmentation symbol\n"); + } + } + + return 0; +} diff --git a/src/libjasper/jpc/jpc_t1dec.h b/src/libjasper/jpc/jpc_t1dec.h new file mode 100644 index 0000000..133dff8 --- /dev/null +++ b/src/libjasper/jpc/jpc_t1dec.h @@ -0,0 +1,88 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier 1 Decoder + * + * $Id: jpc_t1dec.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_T1DEC_H +#define JPC_T1DEC_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jpc_dec.h" +#include "jpc_mqdec.h" +#include "jpc_t1cod.h" + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Decode all of the code blocks for a particular tile. */ +int jpc_dec_decodecblks(jpc_dec_t *dec, jpc_dec_tile_t *tile); + +#endif diff --git a/src/libjasper/jpc/jpc_t1enc.c b/src/libjasper/jpc/jpc_t1enc.c new file mode 100644 index 0000000..b7cc171 --- /dev/null +++ b/src/libjasper/jpc/jpc_t1enc.c @@ -0,0 +1,964 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier 1 Encoder + * + * $Id: jpc_t1enc.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jasper/jas_fix.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_math.h" + +#include "jpc_t1enc.h" +#include "jpc_t1cod.h" +#include "jpc_enc.h" +#include "jpc_cod.h" +#include "jpc_math.h" + +static int jpc_encsigpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int, + jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec); + +static int jpc_encrefpass(jpc_mqenc_t *mqenc, int bitpos, int, jas_matrix_t *flags, + jas_matrix_t *data, int term, long *nmsedec); + +static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int, + int, jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec); + +static int jpc_encrawsigpass(jpc_bitstream_t *out, int bitpos, int, + jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec); + +static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int, + jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec); + +/******************************************************************************\ +* Code for encoding code blocks. +\******************************************************************************/ + +/* Encode all of the code blocks associated with the current tile. */ +int jpc_enc_enccblks(jpc_enc_t *enc) +{ + jpc_enc_tcmpt_t *tcmpt; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_rlvl_t *lvl; + jpc_enc_rlvl_t *endlvls; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_cblk_t *cblk; + jpc_enc_cblk_t *endcblks; + int i; + int j; + int mx; + int bmx; + int v; + jpc_enc_tile_t *tile; + uint_fast32_t prcno; + jpc_enc_prc_t *prc; + + tile = enc->curtile; + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (tcmpt = tile->tcmpts; tcmpt != endcomps; ++tcmpt) { + endlvls = &tcmpt->rlvls[tcmpt->numrlvls]; + for (lvl = tcmpt->rlvls; lvl != endlvls; ++lvl) { + + // GeoJasper: dima - progress + jas_do_progress( (int) lvl, (int) (endlvls-1), "jpc: encode" ); + if (jas_test_abort() == 1) return -1; + + if (!lvl->bands) { + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + bmx = 0; + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + mx = 0; + for (i = 0; i < jas_matrix_numrows(cblk->data); ++i) { + for (j = 0; j < jas_matrix_numcols(cblk->data); ++j) { + v = abs(jas_matrix_get(cblk->data, i, j)); + if (v > mx) { + mx = v; + } + } + } + if (mx > bmx) { + bmx = mx; + } + cblk->numbps = JAS_MAX(jpc_firstone(mx) + 1 - JPC_NUMEXTRABITS, 0); + } + + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + cblk->numimsbs = band->numbps - cblk->numbps; + assert(cblk->numimsbs >= 0); + } + + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + if (jpc_enc_enccblk(enc, cblk->stream, tcmpt, band, cblk)) { + return -1; + } + } + } + } + } + } + return 0; +} + +int getthebyte(jas_stream_t *in, long off) +{ + int c; + long oldpos; + oldpos = jas_stream_tell(in); + assert(oldpos >= 0); + jas_stream_seek(in, off, SEEK_SET); + c = jas_stream_peekc(in); + jas_stream_seek(in, oldpos, SEEK_SET); + return c; +} + +/* Encode a single code block. */ +int jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *tcmpt, jpc_enc_band_t *band, jpc_enc_cblk_t *cblk) +{ + jpc_enc_pass_t *pass; + jpc_enc_pass_t *endpasses; + int bitpos; + int n; + int adjust; + int ret; + int passtype; + int t; + jpc_bitstream_t *bout; + jpc_enc_pass_t *termpass; + jpc_enc_rlvl_t *rlvl; + int vcausal; + int segsym; + int termmode; + int c; + + bout = 0; + rlvl = band->rlvl; + + cblk->stream = jas_stream_memopen(0, 0); + assert(cblk->stream); + cblk->mqenc = jpc_mqenc_create(JPC_NUMCTXS, cblk->stream); + assert(cblk->mqenc); + jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); + + cblk->numpasses = (cblk->numbps > 0) ? (3 * cblk->numbps - 2) : 0; + if (cblk->numpasses > 0) { + cblk->passes = jas_malloc(cblk->numpasses * sizeof(jpc_enc_pass_t)); + assert(cblk->passes); + } else { + cblk->passes = 0; + } + endpasses = &cblk->passes[cblk->numpasses]; + for (pass = cblk->passes; pass != endpasses; ++pass) { + pass->start = 0; + pass->end = 0; + pass->term = JPC_ISTERMINATED(pass - cblk->passes, 0, cblk->numpasses, (tcmpt->cblksty & JPC_COX_TERMALL) != 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0); + pass->type = JPC_SEGTYPE(pass - cblk->passes, 0, (tcmpt->cblksty & JPC_COX_LAZY) != 0); + pass->lyrno = -1; +if (pass == endpasses - 1) { +assert(pass->term == 1); + pass->term = 1; +} + } + + cblk->flags = jas_matrix_create(jas_matrix_numrows(cblk->data) + 2, + jas_matrix_numcols(cblk->data) + 2); + assert(cblk->flags); + + + bitpos = cblk->numbps - 1; + pass = cblk->passes; + n = cblk->numpasses; + while (--n >= 0) { + + if (pass->type == JPC_SEG_MQ) { + /* NOP */ + } else { + assert(pass->type == JPC_SEG_RAW); + if (!bout) { + bout = jpc_bitstream_sopen(cblk->stream, "w"); + assert(bout); + } + } + +#if 1 + passtype = (pass - cblk->passes + 2) % 3; +#else + passtype = JPC_PASSTYPE(pass - cblk->passes + 2); +#endif + pass->start = jas_stream_tell(cblk->stream); +#if 0 +assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream)); +#endif + assert(bitpos >= 0); + vcausal = (tcmpt->cblksty & JPC_COX_VSC) != 0; + segsym = (tcmpt->cblksty & JPC_COX_SEGSYM) != 0; + if (pass->term) { + termmode = ((tcmpt->cblksty & JPC_COX_PTERM) ? + JPC_MQENC_PTERM : JPC_MQENC_DEFTERM) + 1; + } else { + termmode = 0; + } + switch (passtype) { + case JPC_SIGPASS: + ret = (pass->type == JPC_SEG_MQ) ? jpc_encsigpass(cblk->mqenc, + bitpos, band->orient, vcausal, cblk->flags, + cblk->data, termmode, &pass->nmsedec) : + jpc_encrawsigpass(bout, bitpos, vcausal, cblk->flags, + cblk->data, termmode, &pass->nmsedec); + break; + case JPC_REFPASS: + ret = (pass->type == JPC_SEG_MQ) ? jpc_encrefpass(cblk->mqenc, + bitpos, vcausal, cblk->flags, cblk->data, termmode, + &pass->nmsedec) : jpc_encrawrefpass(bout, bitpos, + vcausal, cblk->flags, cblk->data, termmode, + &pass->nmsedec); + break; + case JPC_CLNPASS: + assert(pass->type == JPC_SEG_MQ); + ret = jpc_encclnpass(cblk->mqenc, bitpos, band->orient, + vcausal, segsym, cblk->flags, cblk->data, termmode, + &pass->nmsedec); + break; + default: + assert(0); + break; + } + + if (pass->type == JPC_SEG_MQ) { + if (pass->term) { + jpc_mqenc_init(cblk->mqenc); + } + jpc_mqenc_getstate(cblk->mqenc, &pass->mqencstate); + pass->end = jas_stream_tell(cblk->stream); + if (tcmpt->cblksty & JPC_COX_RESET) { + jpc_mqenc_setctxs(cblk->mqenc, JPC_NUMCTXS, jpc_mqctxs); + } + } else { + if (pass->term) { + if (jpc_bitstream_pending(bout)) { + jpc_bitstream_outalign(bout, 0x2a); + } + jpc_bitstream_close(bout); + bout = 0; + pass->end = jas_stream_tell(cblk->stream); + } else { + pass->end = jas_stream_tell(cblk->stream) + + jpc_bitstream_pending(bout); +/* NOTE - This will not work. need to adjust by # of pending output bytes */ + } + } +#if 0 +/* XXX - This assertion fails sometimes when various coding modes are used. +This seems to be harmless, but why does it happen at all? */ +assert(jas_stream_tell(cblk->stream) == jas_stream_getrwcount(cblk->stream)); +#endif + + pass->wmsedec = jpc_fixtodbl(band->rlvl->tcmpt->synweight) * + jpc_fixtodbl(band->rlvl->tcmpt->synweight) * + jpc_fixtodbl(band->synweight) * + jpc_fixtodbl(band->synweight) * + jpc_fixtodbl(band->absstepsize) * jpc_fixtodbl(band->absstepsize) * + ((double) (1 << bitpos)) * ((double)(1 << bitpos)) * + jpc_fixtodbl(pass->nmsedec); + pass->cumwmsedec = pass->wmsedec; + if (pass != cblk->passes) { + pass->cumwmsedec += pass[-1].cumwmsedec; + } + if (passtype == JPC_CLNPASS) { + --bitpos; + } + ++pass; + } + +#if 0 +dump_passes(cblk->passes, cblk->numpasses, cblk); +#endif + + n = 0; + endpasses = &cblk->passes[cblk->numpasses]; + for (pass = cblk->passes; pass != endpasses; ++pass) { + if (pass->start < n) { + pass->start = n; + } + if (pass->end < n) { + pass->end = n; + } + if (!pass->term) { + termpass = pass; + while (termpass - pass < cblk->numpasses && + !termpass->term) { + ++termpass; + } + if (pass->type == JPC_SEG_MQ) { + t = (pass->mqencstate.lastbyte == 0xff) ? 1 : 0; + if (pass->mqencstate.ctreg >= 5) { + adjust = 4 + t; + } else { + adjust = 5 + t; + } + pass->end += adjust; + } + if (pass->end > termpass->end) { + pass->end = termpass->end; + } + if ((c = getthebyte(cblk->stream, pass->end - 1)) == EOF) { + abort(); + } + if (c == 0xff) { + ++pass->end; + } + n = JAS_MAX(n, pass->end); + } else { + n = JAS_MAX(n, pass->end); + } + } + +#if 0 +dump_passes(cblk->passes, cblk->numpasses, cblk); +#endif + + if (bout) { + jpc_bitstream_close(bout); + } + + return 0; +} + +/******************************************************************************\ +* Code for significance pass. +\******************************************************************************/ + +#define sigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, orient, mqenc, vcausalflag) \ +{ \ + int f; \ + int v; \ + f = *(fp); \ + if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ + v = (abs(*(dp)) & (one)) ? 1 : 0; \ + jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \ + jpc_mqenc_putbit(mqenc, v); \ + if (v) { \ + *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ + v = ((*(dp) < 0) ? 1 : 0); \ + jpc_mqenc_setcurctx(mqenc, JPC_GETSCCTXNO(f)); \ + jpc_mqenc_putbit(mqenc, v ^ JPC_GETSPB(f)); \ + JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \ + *(fp) |= JPC_SIG; \ + } \ + *(fp) |= JPC_VISIT; \ + } \ +} + +static int jpc_encsigpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, + jas_matrix_t *flags, jas_matrix_t *data, int term, long *nmsedec) +{ + int i; + int j; + int one; + int vscanlen; + int width; + int height; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fp; + jpc_fix_t *dp; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + int k; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + sigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, orient, mqenc, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + sigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, orient, mqenc, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + sigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, orient, mqenc, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + sigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, orient, mqenc, 0); + + } + } + + if (term) { + jpc_mqenc_flush(mqenc, term - 1); + } + + return jpc_mqenc_error(mqenc) ? (-1) : 0; +} + +#define rawsigpass_step(fp, frowstep, dp, bitpos, one, nmsedec, out, vcausalflag) \ +{ \ + jpc_fix_t f = *(fp); \ + jpc_fix_t v; \ + if ((f & JPC_OTHSIGMSK) && !(f & (JPC_SIG | JPC_VISIT))) { \ + v = (abs(*(dp)) & (one)) ? 1 : 0; \ + if ((jpc_bitstream_putbit((out), v)) == EOF) { \ + return -1; \ + } \ + if (v) { \ + *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ + v = ((*(dp) < 0) ? 1 : 0); \ + if (jpc_bitstream_putbit(out, v) == EOF) { \ + return -1; \ + } \ + JPC_UPDATEFLAGS4(fp, frowstep, v, vcausalflag); \ + *(fp) |= JPC_SIG; \ + } \ + *(fp) |= JPC_VISIT; \ + } \ +} + +static int jpc_encrawsigpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags, + jas_matrix_t *data, int term, long *nmsedec) +{ + int i; + int j; + int k; + int one; + int vscanlen; + int width; + int height; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fp; + jpc_fix_t *dp; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + rawsigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, out, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + rawsigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, out, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + rawsigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, out, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + rawsigpass_step(fp, frowstep, dp, bitpos, one, + nmsedec, out, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + + } + } + + if (term) { + jpc_bitstream_outalign(out, 0x2a); + } + + return 0; +} + +/******************************************************************************\ +* Code for refinement pass. +\******************************************************************************/ + +#define refpass_step(fp, dp, bitpos, one, nmsedec, mqenc, vcausalflag) \ +{ \ + int v; \ + if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ + (d) = *(dp); \ + *(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \ + jpc_mqenc_setcurctx((mqenc), JPC_GETMAGCTXNO(*(fp))); \ + v = (abs(d) & (one)) ? 1 : 0; \ + jpc_mqenc_putbit((mqenc), v); \ + *(fp) |= JPC_REFINE; \ + } \ +} + +static int jpc_encrefpass(jpc_mqenc_t *mqenc, int bitpos, int vcausalflag, jas_matrix_t *flags, jas_matrix_t *data, + int term, long *nmsedec) +{ + int i; + int j; + int one; + int vscanlen; + int d; + int width; + int height; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + jpc_fix_t *dp; + jpc_fix_t *fp; +int k; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + refpass_step(fp, dp, bitpos, one, nmsedec, + mqenc, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + refpass_step(fp, dp, bitpos, one, nmsedec, + mqenc, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + refpass_step(fp, dp, bitpos, one, nmsedec, + mqenc, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + refpass_step(fp, dp, bitpos, one, nmsedec, + mqenc, 0); + + } + } + + if (term) { + jpc_mqenc_flush(mqenc, term - 1); + } + + return jpc_mqenc_error(mqenc) ? (-1) : 0; +} + +#define rawrefpass_step(fp, dp, bitpos, one, nmsedec, out, vcausalflag) \ +{ \ + jpc_fix_t d; \ + jpc_fix_t v; \ + if (((*(fp)) & (JPC_SIG | JPC_VISIT)) == JPC_SIG) { \ + d = *(dp); \ + *(nmsedec) += JPC_GETREFNMSEDEC(abs(d), (bitpos) + JPC_NUMEXTRABITS); \ + v = (abs(d) & (one)) ? 1 : 0; \ + if (jpc_bitstream_putbit((out), v) == EOF) { \ + return -1; \ + } \ + *(fp) |= JPC_REFINE; \ + } \ +} + +static int jpc_encrawrefpass(jpc_bitstream_t *out, int bitpos, int vcausalflag, jas_matrix_t *flags, + jas_matrix_t *data, int term, long *nmsedec) +{ + int i; + int j; + int k; + int one; + int vscanlen; + int width; + int height; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + jpc_fix_t *dp; + jpc_fix_t *fp; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + + rawrefpass_step(fp, dp, bitpos, one, nmsedec, + out, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + rawrefpass_step(fp, dp, bitpos, one, nmsedec, + out, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + rawrefpass_step(fp, dp, bitpos, one, nmsedec, + out, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + rawrefpass_step(fp, dp, bitpos, one, nmsedec, + out, vcausalflag); + + } + } + + if (term) { + jpc_bitstream_outalign(out, 0x2a); + } + + return 0; +} + +/******************************************************************************\ +* Code for cleanup pass. +\******************************************************************************/ + +#define clnpass_step(fp, frowstep, dp, bitpos, one, orient, nmsedec, mqenc, label1, label2, vcausalflag) \ +{ \ + int f; \ + int v; \ +label1 \ + f = *(fp); \ + if (!(f & (JPC_SIG | JPC_VISIT))) { \ + jpc_mqenc_setcurctx(mqenc, JPC_GETZCCTXNO(f, (orient))); \ + v = (abs(*(dp)) & (one)) ? 1 : 0; \ + jpc_mqenc_putbit((mqenc), v); \ + if (v) { \ +label2 \ + f = *(fp); \ + /* Coefficient is significant. */ \ + *(nmsedec) += JPC_GETSIGNMSEDEC(abs(*(dp)), (bitpos) + JPC_NUMEXTRABITS); \ + jpc_mqenc_setcurctx((mqenc), JPC_GETSCCTXNO(f)); \ + v = ((*(dp) < 0) ? 1 : 0); \ + jpc_mqenc_putbit((mqenc), v ^ JPC_GETSPB(f)); \ + JPC_UPDATEFLAGS4((fp), (frowstep), v, vcausalflag); \ + *(fp) |= JPC_SIG; \ + } \ + } \ + *(fp) &= ~JPC_VISIT; \ +} + +static int jpc_encclnpass(jpc_mqenc_t *mqenc, int bitpos, int orient, int vcausalflag, int segsymflag, jas_matrix_t *flags, + jas_matrix_t *data, int term, long *nmsedec) +{ + int i; + int j; + int k; + int vscanlen; + int v; + int runlen; + jpc_fix_t *fp; + int width; + int height; + jpc_fix_t *dp; + int one; + int frowstep; + int drowstep; + int fstripestep; + int dstripestep; + jpc_fix_t *fstripestart; + jpc_fix_t *dstripestart; + jpc_fix_t *fvscanstart; + jpc_fix_t *dvscanstart; + + *nmsedec = 0; + width = jas_matrix_numcols(data); + height = jas_matrix_numrows(data); + frowstep = jas_matrix_rowstep(flags); + drowstep = jas_matrix_rowstep(data); + fstripestep = frowstep << 2; + dstripestep = drowstep << 2; + + one = 1 << (bitpos + JPC_NUMEXTRABITS); + + fstripestart = jas_matrix_getref(flags, 1, 1); + dstripestart = jas_matrix_getref(data, 0, 0); + for (i = height; i > 0; i -= 4, fstripestart += fstripestep, + dstripestart += dstripestep) { + fvscanstart = fstripestart; + dvscanstart = dstripestart; + vscanlen = JAS_MIN(i, 4); + for (j = width; j > 0; --j, ++fvscanstart, ++dvscanstart) { + + fp = fvscanstart; + if (vscanlen >= 4 && !((*fp) & (JPC_SIG | JPC_VISIT | + JPC_OTHSIGMSK)) && (fp += frowstep, !((*fp) & (JPC_SIG | + JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, !((*fp) & + (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK))) && (fp += frowstep, + !((*fp) & (JPC_SIG | JPC_VISIT | JPC_OTHSIGMSK)))) { + dp = dvscanstart; + for (k = 0; k < vscanlen; ++k) { + v = (abs(*dp) & one) ? 1 : 0; + if (v) { + break; + } + dp += drowstep; + } + runlen = k; + if (runlen >= 4) { + jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); + jpc_mqenc_putbit(mqenc, 0); + continue; + } + jpc_mqenc_setcurctx(mqenc, JPC_AGGCTXNO); + jpc_mqenc_putbit(mqenc, 1); + jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); + jpc_mqenc_putbit(mqenc, runlen >> 1); + jpc_mqenc_putbit(mqenc, runlen & 1); + fp = fvscanstart + frowstep * runlen; + dp = dvscanstart + drowstep * runlen; + k = vscanlen - runlen; + switch (runlen) { + case 0: + goto clnpass_partial0; + break; + case 1: + goto clnpass_partial1; + break; + case 2: + goto clnpass_partial2; + break; + case 3: + goto clnpass_partial3; + break; + } + } else { + runlen = 0; + fp = fvscanstart; + dp = dvscanstart; + k = vscanlen; + goto clnpass_full0; + } + clnpass_step(fp, frowstep, dp, bitpos, one, + orient, nmsedec, mqenc, clnpass_full0:, clnpass_partial0:, vcausalflag); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + clnpass_step(fp, frowstep, dp, bitpos, one, + orient, nmsedec, mqenc, ;, clnpass_partial1:, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + clnpass_step(fp, frowstep, dp, bitpos, one, + orient, nmsedec, mqenc, ;, clnpass_partial2:, 0); + if (--k <= 0) { + continue; + } + fp += frowstep; + dp += drowstep; + clnpass_step(fp, frowstep, dp, bitpos, one, + orient, nmsedec, mqenc, ;, clnpass_partial3:, 0); + } + } + + if (segsymflag) { + jpc_mqenc_setcurctx(mqenc, JPC_UCTXNO); + jpc_mqenc_putbit(mqenc, 1); + jpc_mqenc_putbit(mqenc, 0); + jpc_mqenc_putbit(mqenc, 1); + jpc_mqenc_putbit(mqenc, 0); + } + + if (term) { + jpc_mqenc_flush(mqenc, term - 1); + } + + return jpc_mqenc_error(mqenc) ? (-1) : 0; +} diff --git a/src/libjasper/jpc/jpc_t1enc.h b/src/libjasper/jpc/jpc_t1enc.h new file mode 100644 index 0000000..5cdf6fa --- /dev/null +++ b/src/libjasper/jpc/jpc_t1enc.h @@ -0,0 +1,93 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier 1 Encoder + * + * $Id: jpc_t1enc.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_T1ENC_H +#define JPC_T1ENC_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_seq.h" + +#include "jpc_enc.h" +#include "jpc_t1cod.h" + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Encode all of the code blocks. */ +int jpc_enc_enccblks(jpc_enc_t *enc); + +/* Encode a single code block. */ +int jpc_enc_enccblk(jpc_enc_t *enc, jas_stream_t *out, jpc_enc_tcmpt_t *comp, + jpc_enc_band_t *band, jpc_enc_cblk_t *cblk); + +#endif diff --git a/src/libjasper/jpc/jpc_t2cod.c b/src/libjasper/jpc/jpc_t2cod.c new file mode 100644 index 0000000..a186d35 --- /dev/null +++ b/src/libjasper/jpc/jpc_t2cod.c @@ -0,0 +1,684 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier-2 Coding Library + * + * $Id: jpc_t2cod.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#include "jasper/jas_math.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_math.h" + +#include "jpc_cs.h" +#include "jpc_t2cod.h" +#include "jpc_math.h" + +static int jpc_pi_nextlrcp(jpc_pi_t *pi); +static int jpc_pi_nextrlcp(jpc_pi_t *pi); +static int jpc_pi_nextrpcl(jpc_pi_t *pi); +static int jpc_pi_nextpcrl(jpc_pi_t *pi); +static int jpc_pi_nextcprl(jpc_pi_t *pi); + +int jpc_pi_next(jpc_pi_t *pi) +{ + jpc_pchg_t *pchg; + int ret; + + + for (;;) { + + pi->valid = false; + + if (!pi->pchg) { + ++pi->pchgno; + pi->compno = 0; + pi->rlvlno = 0; + pi->prcno = 0; + pi->lyrno = 0; + pi->prgvolfirst = true; + if (pi->pchgno < jpc_pchglist_numpchgs(pi->pchglist)) { + pi->pchg = jpc_pchglist_get(pi->pchglist, pi->pchgno); + } else if (pi->pchgno == jpc_pchglist_numpchgs(pi->pchglist)) { + pi->pchg = &pi->defaultpchg; + } else { + return 1; + } + } + + pchg = pi->pchg; + switch (pchg->prgord) { + case JPC_COD_LRCPPRG: + ret = jpc_pi_nextlrcp(pi); + break; + case JPC_COD_RLCPPRG: + ret = jpc_pi_nextrlcp(pi); + break; + case JPC_COD_RPCLPRG: + ret = jpc_pi_nextrpcl(pi); + break; + case JPC_COD_PCRLPRG: + ret = jpc_pi_nextpcrl(pi); + break; + case JPC_COD_CPRLPRG: + ret = jpc_pi_nextcprl(pi); + break; + default: + ret = -1; + break; + } + if (!ret) { + pi->valid = true; + ++pi->pktno; + return 0; + } + pi->pchg = 0; + } +} + +static int jpc_pi_nextlrcp(register jpc_pi_t *pi) +{ + jpc_pchg_t *pchg; + int *prclyrno; + + pchg = pi->pchg; + if (!pi->prgvolfirst) { + prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; + goto skip; + } else { + pi->prgvolfirst = false; + } + + for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno < + JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) { + for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls && + pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) { + for (pi->compno = pchg->compnostart, pi->picomp = + &pi->picomps[pi->compno]; pi->compno < pi->numcomps + && pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno, + ++pi->picomp) { + if (pi->rlvlno >= pi->picomp->numrlvls) { + continue; + } + pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; + for (pi->prcno = 0, prclyrno = + pi->pirlvl->prclyrnos; pi->prcno < + pi->pirlvl->numprcs; ++pi->prcno, + ++prclyrno) { + if (pi->lyrno >= *prclyrno) { + *prclyrno = pi->lyrno; + ++(*prclyrno); + return 0; + } +skip: + ; + } + } + } + } + return 1; +} + +static int jpc_pi_nextrlcp(register jpc_pi_t *pi) +{ + jpc_pchg_t *pchg; + int *prclyrno; + + pchg = pi->pchg; + if (!pi->prgvolfirst) { + assert(pi->prcno < pi->pirlvl->numprcs); + prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; + goto skip; + } else { + pi->prgvolfirst = 0; + } + + for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pi->maxrlvls && + pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno) { + for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && pi->lyrno < + JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) { + for (pi->compno = pchg->compnostart, pi->picomp = + &pi->picomps[pi->compno]; pi->compno < pi->numcomps && + pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno, ++pi->picomp) { + if (pi->rlvlno >= pi->picomp->numrlvls) { + continue; + } + pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; + for (pi->prcno = 0, prclyrno = pi->pirlvl->prclyrnos; + pi->prcno < pi->pirlvl->numprcs; ++pi->prcno, ++prclyrno) { + if (pi->lyrno >= *prclyrno) { + *prclyrno = pi->lyrno; + ++(*prclyrno); + return 0; + } +skip: + ; + } + } + } + } + return 1; +} + +static int jpc_pi_nextrpcl(register jpc_pi_t *pi) +{ + int rlvlno; + jpc_pirlvl_t *pirlvl; + jpc_pchg_t *pchg; + int prchind; + int prcvind; + int *prclyrno; + int compno; + jpc_picomp_t *picomp; + int xstep; + int ystep; + uint_fast32_t r; + uint_fast32_t rpx; + uint_fast32_t rpy; + uint_fast32_t trx0; + uint_fast32_t try0; + + pchg = pi->pchg; + if (!pi->prgvolfirst) { + goto skip; + } else { + pi->xstep = 0; + pi->ystep = 0; + for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; + ++compno, ++picomp) { + for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < + picomp->numrlvls; ++rlvlno, ++pirlvl) { + xstep = picomp->hsamp * (1 << (pirlvl->prcwidthexpn + + picomp->numrlvls - rlvlno - 1)); + ystep = picomp->vsamp * (1 << (pirlvl->prcheightexpn + + picomp->numrlvls - rlvlno - 1)); + pi->xstep = (!pi->xstep) ? xstep : JAS_MIN(pi->xstep, xstep); + pi->ystep = (!pi->ystep) ? ystep : JAS_MIN(pi->ystep, ystep); + } + } + pi->prgvolfirst = 0; + } + + for (pi->rlvlno = pchg->rlvlnostart; pi->rlvlno < pchg->rlvlnoend && + pi->rlvlno < pi->maxrlvls; ++pi->rlvlno) { + for (pi->y = pi->ystart; pi->y < pi->yend; pi->y += + pi->ystep - (pi->y % pi->ystep)) { + for (pi->x = pi->xstart; pi->x < pi->xend; pi->x += + pi->xstep - (pi->x % pi->xstep)) { + for (pi->compno = pchg->compnostart, + pi->picomp = &pi->picomps[pi->compno]; + pi->compno < JAS_CAST(int, pchg->compnoend) && pi->compno < + pi->numcomps; ++pi->compno, ++pi->picomp) { + if (pi->rlvlno >= pi->picomp->numrlvls) { + continue; + } + pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; + if (pi->pirlvl->numprcs == 0) { + continue; + } + r = pi->picomp->numrlvls - 1 - pi->rlvlno; + rpx = r + pi->pirlvl->prcwidthexpn; + rpy = r + pi->pirlvl->prcheightexpn; + trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r); + try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r); + if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) + || !(pi->x % (1 << rpx))) && + ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) + || !(pi->y % (1 << rpy)))) { + prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp + << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0, + pi->pirlvl->prcwidthexpn); + prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp + << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0, + pi->pirlvl->prcheightexpn); + pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind; + + assert(pi->prcno < pi->pirlvl->numprcs); + for (pi->lyrno = 0; pi->lyrno < + pi->numlyrs && pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) { + prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; + if (pi->lyrno >= *prclyrno) { + ++(*prclyrno); + return 0; + } +skip: + ; + } + } + } + } + } + } + return 1; +} + +static int jpc_pi_nextpcrl(register jpc_pi_t *pi) +{ + int rlvlno; + jpc_pirlvl_t *pirlvl; + jpc_pchg_t *pchg; + int prchind; + int prcvind; + int *prclyrno; + int compno; + jpc_picomp_t *picomp; + int xstep; + int ystep; + uint_fast32_t trx0; + uint_fast32_t try0; + uint_fast32_t r; + uint_fast32_t rpx; + uint_fast32_t rpy; + + pchg = pi->pchg; + if (!pi->prgvolfirst) { + goto skip; + } else { + pi->xstep = 0; + pi->ystep = 0; + for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; + ++compno, ++picomp) { + for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < + picomp->numrlvls; ++rlvlno, ++pirlvl) { + xstep = picomp->hsamp * (1 << + (pirlvl->prcwidthexpn + picomp->numrlvls - + rlvlno - 1)); + ystep = picomp->vsamp * (1 << + (pirlvl->prcheightexpn + picomp->numrlvls - + rlvlno - 1)); + pi->xstep = (!pi->xstep) ? xstep : + JAS_MIN(pi->xstep, xstep); + pi->ystep = (!pi->ystep) ? ystep : + JAS_MIN(pi->ystep, ystep); + } + } + pi->prgvolfirst = 0; + } + + for (pi->y = pi->ystart; pi->y < pi->yend; pi->y += pi->ystep - + (pi->y % pi->ystep)) { + for (pi->x = pi->xstart; pi->x < pi->xend; pi->x += pi->xstep - + (pi->x % pi->xstep)) { + for (pi->compno = pchg->compnostart, pi->picomp = + &pi->picomps[pi->compno]; pi->compno < pi->numcomps + && pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno, + ++pi->picomp) { + for (pi->rlvlno = pchg->rlvlnostart, + pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; + pi->rlvlno < pi->picomp->numrlvls && + pi->rlvlno < pchg->rlvlnoend; ++pi->rlvlno, + ++pi->pirlvl) { + if (pi->pirlvl->numprcs == 0) { + continue; + } + r = pi->picomp->numrlvls - 1 - pi->rlvlno; + trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r); + try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r); + rpx = r + pi->pirlvl->prcwidthexpn; + rpy = r + pi->pirlvl->prcheightexpn; + if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) || + !(pi->x % (pi->picomp->hsamp << rpx))) && + ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) || + !(pi->y % (pi->picomp->vsamp << rpy)))) { + prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp + << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0, + pi->pirlvl->prcwidthexpn); + prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp + << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0, + pi->pirlvl->prcheightexpn); + pi->prcno = prcvind * pi->pirlvl->numhprcs + prchind; + assert(pi->prcno < pi->pirlvl->numprcs); + for (pi->lyrno = 0; pi->lyrno < pi->numlyrs && + pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) { + prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; + if (pi->lyrno >= *prclyrno) { + ++(*prclyrno); + return 0; + } +skip: + ; + } + } + } + } + } + } + return 1; +} + +static int jpc_pi_nextcprl(register jpc_pi_t *pi) +{ + int rlvlno; + jpc_pirlvl_t *pirlvl; + jpc_pchg_t *pchg; + int prchind; + int prcvind; + int *prclyrno; + uint_fast32_t trx0; + uint_fast32_t try0; + uint_fast32_t r; + uint_fast32_t rpx; + uint_fast32_t rpy; + + pchg = pi->pchg; + if (!pi->prgvolfirst) { + goto skip; + } else { + pi->prgvolfirst = 0; + } + + for (pi->compno = pchg->compnostart, pi->picomp = + &pi->picomps[pi->compno]; pi->compno < JAS_CAST(int, pchg->compnoend); ++pi->compno, + ++pi->picomp) { + pirlvl = pi->picomp->pirlvls; + pi->xstep = pi->picomp->hsamp * (1 << (pirlvl->prcwidthexpn + + pi->picomp->numrlvls - 1)); + pi->ystep = pi->picomp->vsamp * (1 << (pirlvl->prcheightexpn + + pi->picomp->numrlvls - 1)); + for (rlvlno = 1, pirlvl = &pi->picomp->pirlvls[1]; + rlvlno < pi->picomp->numrlvls; ++rlvlno, ++pirlvl) { + pi->xstep = JAS_MIN(pi->xstep, pi->picomp->hsamp * (1 << + (pirlvl->prcwidthexpn + pi->picomp->numrlvls - + rlvlno - 1))); + pi->ystep = JAS_MIN(pi->ystep, pi->picomp->vsamp * (1 << + (pirlvl->prcheightexpn + pi->picomp->numrlvls - + rlvlno - 1))); + } + for (pi->y = pi->ystart; pi->y < pi->yend; + pi->y += pi->ystep - (pi->y % pi->ystep)) { + for (pi->x = pi->xstart; pi->x < pi->xend; + pi->x += pi->xstep - (pi->x % pi->xstep)) { + for (pi->rlvlno = pchg->rlvlnostart, + pi->pirlvl = &pi->picomp->pirlvls[pi->rlvlno]; + pi->rlvlno < pi->picomp->numrlvls && pi->rlvlno < + pchg->rlvlnoend; ++pi->rlvlno, ++pi->pirlvl) { + if (pi->pirlvl->numprcs == 0) { + continue; + } + r = pi->picomp->numrlvls - 1 - pi->rlvlno; + trx0 = JPC_CEILDIV(pi->xstart, pi->picomp->hsamp << r); + try0 = JPC_CEILDIV(pi->ystart, pi->picomp->vsamp << r); + rpx = r + pi->pirlvl->prcwidthexpn; + rpy = r + pi->pirlvl->prcheightexpn; + if (((pi->x == pi->xstart && ((trx0 << r) % (1 << rpx))) || + !(pi->x % (pi->picomp->hsamp << rpx))) && + ((pi->y == pi->ystart && ((try0 << r) % (1 << rpy))) || + !(pi->y % (pi->picomp->vsamp << rpy)))) { + prchind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->x, pi->picomp->hsamp + << r), pi->pirlvl->prcwidthexpn) - JPC_FLOORDIVPOW2(trx0, + pi->pirlvl->prcwidthexpn); + prcvind = JPC_FLOORDIVPOW2(JPC_CEILDIV(pi->y, pi->picomp->vsamp + << r), pi->pirlvl->prcheightexpn) - JPC_FLOORDIVPOW2(try0, + pi->pirlvl->prcheightexpn); + pi->prcno = prcvind * + pi->pirlvl->numhprcs + + prchind; + assert(pi->prcno < + pi->pirlvl->numprcs); + for (pi->lyrno = 0; pi->lyrno < + pi->numlyrs && pi->lyrno < JAS_CAST(int, pchg->lyrnoend); ++pi->lyrno) { + prclyrno = &pi->pirlvl->prclyrnos[pi->prcno]; + if (pi->lyrno >= *prclyrno) { + ++(*prclyrno); + return 0; + } +skip: + ; + } + } + } + } + } + } + return 1; +} + +static void pirlvl_destroy(jpc_pirlvl_t *rlvl) +{ + if (rlvl->prclyrnos) { + jas_free(rlvl->prclyrnos); + } +} + +static void jpc_picomp_destroy(jpc_picomp_t *picomp) +{ + int rlvlno; + jpc_pirlvl_t *pirlvl; + if (picomp->pirlvls) { + for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < + picomp->numrlvls; ++rlvlno, ++pirlvl) { + pirlvl_destroy(pirlvl); + } + jas_free(picomp->pirlvls); + } +} + +void jpc_pi_destroy(jpc_pi_t *pi) +{ + jpc_picomp_t *picomp; + int compno; + if (pi->picomps) { + for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; + ++compno, ++picomp) { + jpc_picomp_destroy(picomp); + } + jas_free(pi->picomps); + } + if (pi->pchglist) { + jpc_pchglist_destroy(pi->pchglist); + } + jas_free(pi); +} + +jpc_pi_t *jpc_pi_create0() +{ + jpc_pi_t *pi; + if (!(pi = jas_malloc(sizeof(jpc_pi_t)))) { + return 0; + } + pi->picomps = 0; + pi->pchgno = 0; + if (!(pi->pchglist = jpc_pchglist_create())) { + jas_free(pi); + return 0; + } + return pi; +} + +int jpc_pi_addpchg(jpc_pi_t *pi, jpc_pocpchg_t *pchg) +{ + return jpc_pchglist_insert(pi->pchglist, -1, pchg); +} + +jpc_pchglist_t *jpc_pchglist_create() +{ + jpc_pchglist_t *pchglist; + if (!(pchglist = jas_malloc(sizeof(jpc_pchglist_t)))) { + return 0; + } + pchglist->numpchgs = 0; + pchglist->maxpchgs = 0; + pchglist->pchgs = 0; + return pchglist; +} + +int jpc_pchglist_insert(jpc_pchglist_t *pchglist, int pchgno, jpc_pchg_t *pchg) +{ + int i; + int newmaxpchgs; + jpc_pchg_t **newpchgs; + if (pchgno < 0) { + pchgno = pchglist->numpchgs; + } + if (pchglist->numpchgs >= pchglist->maxpchgs) { + newmaxpchgs = pchglist->maxpchgs + 128; + if (!(newpchgs = jas_realloc(pchglist->pchgs, newmaxpchgs * sizeof(jpc_pchg_t *)))) { + return -1; + } + pchglist->maxpchgs = newmaxpchgs; + pchglist->pchgs = newpchgs; + } + for (i = pchglist->numpchgs; i > pchgno; --i) { + pchglist->pchgs[i] = pchglist->pchgs[i - 1]; + } + pchglist->pchgs[pchgno] = pchg; + ++pchglist->numpchgs; + return 0; +} + +jpc_pchg_t *jpc_pchglist_remove(jpc_pchglist_t *pchglist, int pchgno) +{ + int i; + jpc_pchg_t *pchg; + assert(pchgno < pchglist->numpchgs); + pchg = pchglist->pchgs[pchgno]; + for (i = pchgno + 1; i < pchglist->numpchgs; ++i) { + pchglist->pchgs[i - 1] = pchglist->pchgs[i]; + } + --pchglist->numpchgs; + return pchg; +} + +jpc_pchg_t *jpc_pchg_copy(jpc_pchg_t *pchg) +{ + jpc_pchg_t *newpchg; + if (!(newpchg = jas_malloc(sizeof(jpc_pchg_t)))) { + return 0; + } + *newpchg = *pchg; + return newpchg; +} + +jpc_pchglist_t *jpc_pchglist_copy(jpc_pchglist_t *pchglist) +{ + jpc_pchglist_t *newpchglist; + jpc_pchg_t *newpchg; + int pchgno; + if (!(newpchglist = jpc_pchglist_create())) { + return 0; + } + for (pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) { + if (!(newpchg = jpc_pchg_copy(pchglist->pchgs[pchgno])) || + jpc_pchglist_insert(newpchglist, -1, newpchg)) { + jpc_pchglist_destroy(newpchglist); + return 0; + } + } + return newpchglist; +} + +void jpc_pchglist_destroy(jpc_pchglist_t *pchglist) +{ + int pchgno; + if (pchglist->pchgs) { + for (pchgno = 0; pchgno < pchglist->numpchgs; ++pchgno) { + jpc_pchg_destroy(pchglist->pchgs[pchgno]); + } + jas_free(pchglist->pchgs); + } + jas_free(pchglist); +} + +void jpc_pchg_destroy(jpc_pchg_t *pchg) +{ + jas_free(pchg); +} + +jpc_pchg_t *jpc_pchglist_get(jpc_pchglist_t *pchglist, int pchgno) +{ + return pchglist->pchgs[pchgno]; +} + +int jpc_pchglist_numpchgs(jpc_pchglist_t *pchglist) +{ + return pchglist->numpchgs; +} + +int jpc_pi_init(jpc_pi_t *pi) +{ + int compno; + int rlvlno; + int prcno; + jpc_picomp_t *picomp; + jpc_pirlvl_t *pirlvl; + int *prclyrno; + + pi->prgvolfirst = 0; + pi->valid = 0; + pi->pktno = -1; + pi->pchgno = -1; + pi->pchg = 0; + + for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; + ++compno, ++picomp) { + for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < + picomp->numrlvls; ++rlvlno, ++pirlvl) { + for (prcno = 0, prclyrno = pirlvl->prclyrnos; + prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { + *prclyrno = 0; + } + } + } + return 0; +} diff --git a/src/libjasper/jpc/jpc_t2cod.h b/src/libjasper/jpc/jpc_t2cod.h new file mode 100644 index 0000000..5eefcc2 --- /dev/null +++ b/src/libjasper/jpc/jpc_t2cod.h @@ -0,0 +1,299 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier-2 Coding Library + * + * $Id: jpc_t2cod.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_T2COD_H +#define JPC_T2COD_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jpc_cs.h" + +/******************************************************************************\ +* Types. +\******************************************************************************/ + +/* Progression change list. */ + +typedef struct { + + /* The number of progression changes. */ + int numpchgs; + + /* The maximum number of progression changes that can be accomodated + without growing the progression change array. */ + int maxpchgs; + + /* The progression changes. */ + jpc_pchg_t **pchgs; + +} jpc_pchglist_t; + +/* Packet iterator per-resolution-level information. */ + +typedef struct { + + /* The number of precincts. */ + int numprcs; + + /* The last layer processed for each precinct. */ + int *prclyrnos; + + /* The precinct width exponent. */ + int prcwidthexpn; + + /* The precinct height exponent. */ + int prcheightexpn; + + /* The number of precincts spanning the resolution level in the horizontal + direction. */ + int numhprcs; + +} jpc_pirlvl_t; + +/* Packet iterator per-component information. */ + +typedef struct { + + /* The number of resolution levels. */ + int numrlvls; + + /* The per-resolution-level information. */ + jpc_pirlvl_t *pirlvls; + + /* The horizontal sampling period. */ + int hsamp; + + /* The vertical sampling period. */ + int vsamp; + +} jpc_picomp_t; + +/* Packet iterator class. */ + +typedef struct { + + /* The number of layers. */ + int numlyrs; + + /* The number of resolution levels. */ + int maxrlvls; + + /* The number of components. */ + int numcomps; + + /* The per-component information. */ + jpc_picomp_t *picomps; + + /* The current component. */ + jpc_picomp_t *picomp; + + /* The current resolution level. */ + jpc_pirlvl_t *pirlvl; + + /* The number of the current component. */ + int compno; + + /* The number of the current resolution level. */ + int rlvlno; + + /* The number of the current precinct. */ + int prcno; + + /* The number of the current layer. */ + int lyrno; + + /* The x-coordinate of the current position. */ + int x; + + /* The y-coordinate of the current position. */ + int y; + + /* The horizontal step size. */ + int xstep; + + /* The vertical step size. */ + int ystep; + + /* The x-coordinate of the top-left corner of the tile on the reference + grid. */ + int xstart; + + /* The y-coordinate of the top-left corner of the tile on the reference + grid. */ + int ystart; + + /* The x-coordinate of the bottom-right corner of the tile on the + reference grid (plus one). */ + int xend; + + /* The y-coordinate of the bottom-right corner of the tile on the + reference grid (plus one). */ + int yend; + + /* The current progression change. */ + jpc_pchg_t *pchg; + + /* The progression change list. */ + jpc_pchglist_t *pchglist; + + /* The progression to use in the absense of explicit specification. */ + jpc_pchg_t defaultpchg; + + /* The current progression change number. */ + int pchgno; + + /* Is this the first time in the current progression volume? */ + bool prgvolfirst; + + /* Is the current iterator value valid? */ + bool valid; + + /* The current packet number. */ + int pktno; + +} jpc_pi_t; + +/******************************************************************************\ +* Functions/macros for packet iterators. +\******************************************************************************/ + +/* Create a packet iterator. */ +jpc_pi_t *jpc_pi_create0(void); + +/* Destroy a packet iterator. */ +void jpc_pi_destroy(jpc_pi_t *pi); + +/* Add a progression change to a packet iterator. */ +int jpc_pi_addpchg(jpc_pi_t *pi, jpc_pocpchg_t *pchg); + +/* Prepare a packet iterator for iteration. */ +int jpc_pi_init(jpc_pi_t *pi); + +/* Set the iterator to the first packet. */ +int jpc_pi_begin(jpc_pi_t *pi); + +/* Proceed to the next packet in sequence. */ +int jpc_pi_next(jpc_pi_t *pi); + +/* Get the index of the current packet. */ +#define jpc_pi_getind(pi) ((pi)->pktno) + +/* Get the component number of the current packet. */ +#define jpc_pi_cmptno(pi) (assert(pi->valid), (pi)->compno) + +/* Get the resolution level of the current packet. */ +#define jpc_pi_rlvlno(pi) (assert(pi->valid), (pi)->rlvlno) + +/* Get the layer number of the current packet. */ +#define jpc_pi_lyrno(pi) (assert(pi->valid), (pi)->lyrno) + +/* Get the precinct number of the current packet. */ +#define jpc_pi_prcno(pi) (assert(pi->valid), (pi)->prcno) + +/* Get the progression order for the current packet. */ +#define jpc_pi_prg(pi) (assert(pi->valid), (pi)->pchg->prgord) + +/******************************************************************************\ +* Functions/macros for progression change lists. +\******************************************************************************/ + +/* Create a progression change list. */ +jpc_pchglist_t *jpc_pchglist_create(void); + +/* Destroy a progression change list. */ +void jpc_pchglist_destroy(jpc_pchglist_t *pchglist); + +/* Insert a new element into a progression change list. */ +int jpc_pchglist_insert(jpc_pchglist_t *pchglist, int pchgno, jpc_pchg_t *pchg); + +/* Remove an element from a progression change list. */ +jpc_pchg_t *jpc_pchglist_remove(jpc_pchglist_t *pchglist, int pchgno); + +/* Get an element from a progression change list. */ +jpc_pchg_t *jpc_pchglist_get(jpc_pchglist_t *pchglist, int pchgno); + +/* Copy a progression change list. */ +jpc_pchglist_t *jpc_pchglist_copy(jpc_pchglist_t *pchglist); + +/* Get the number of elements in a progression change list. */ +int jpc_pchglist_numpchgs(jpc_pchglist_t *pchglist); + +/******************************************************************************\ +* Functions/macros for progression changes. +\******************************************************************************/ + +/* Destroy a progression change. */ +void jpc_pchg_destroy(jpc_pchg_t *pchg); + +/* Copy a progression change. */ +jpc_pchg_t *jpc_pchg_copy(jpc_pchg_t *pchg); + +#endif diff --git a/src/libjasper/jpc/jpc_t2dec.c b/src/libjasper/jpc/jpc_t2dec.c new file mode 100644 index 0000000..0a79c88 --- /dev/null +++ b/src/libjasper/jpc/jpc_t2dec.c @@ -0,0 +1,581 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier 2 Decoder + * + * $Id: jpc_t2dec.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jasper/jas_types.h" +#include "jasper/jas_fix.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_math.h" +#include "jasper/jas_stream.h" +#include "jasper/jas_debug.h" + +#include "jpc_bs.h" +#include "jpc_dec.h" +#include "jpc_cs.h" +#include "jpc_mqdec.h" +#include "jpc_t2dec.h" +#include "jpc_t1cod.h" +#include "jpc_math.h" + +/******************************************************************************\ +* +\******************************************************************************/ + +long jpc_dec_lookahead(jas_stream_t *in); +static int jpc_getcommacode(jpc_bitstream_t *in); +static int jpc_getnumnewpasses(jpc_bitstream_t *in); +static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int lvlno, + int prcno, int lyrno); + +/******************************************************************************\ +* Code. +\******************************************************************************/ + +static int jpc_getcommacode(jpc_bitstream_t *in) +{ + int n; + int v; + + n = 0; + for (;;) { + if ((v = jpc_bitstream_getbit(in)) < 0) { + return -1; + } + if (jpc_bitstream_eof(in)) { + return -1; + } + if (!v) { + break; + } + ++n; + } + + return n; +} + +static int jpc_getnumnewpasses(jpc_bitstream_t *in) +{ + int n; + + if ((n = jpc_bitstream_getbit(in)) > 0) { + if ((n = jpc_bitstream_getbit(in)) > 0) { + if ((n = jpc_bitstream_getbits(in, 2)) == 3) { + if ((n = jpc_bitstream_getbits(in, 5)) == 31) { + if ((n = jpc_bitstream_getbits(in, 7)) >= 0) { + n += 36 + 1; + } + } else if (n >= 0) { + n += 5 + 1; + } + } else if (n >= 0) { + n += 2 + 1; + } + } else if (!n) { + n += 2; + } + } else if (!n) { + ++n; + } + + return n; +} + +static int jpc_dec_decodepkt(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in, int compno, int rlvlno, + int prcno, int lyrno) +{ + jpc_bitstream_t *inb; + jpc_dec_tcomp_t *tcomp; + jpc_dec_rlvl_t *rlvl; + jpc_dec_band_t *band; + jpc_dec_cblk_t *cblk; + int n; + int m; + int i; + jpc_tagtreenode_t *leaf; + int included; + int ret; + int numnewpasses; + jpc_dec_seg_t *seg; + int len; + int present; + int savenumnewpasses; + int mycounter; + jpc_ms_t *ms; + jpc_dec_tile_t *tile; + jpc_dec_ccp_t *ccp; + jpc_dec_cp_t *cp; + int bandno; + jpc_dec_prc_t *prc; + int usedcblkcnt; + int cblkno; + uint_fast32_t bodylen; + bool discard; + int passno; + int maxpasses; + int hdrlen; + int hdroffstart; + int hdroffend; + + /* Avoid compiler warning about possible use of uninitialized + variable. */ + bodylen = 0; + + discard = (lyrno >= dec->maxlyrs); + + tile = dec->curtile; + cp = tile->cp; + ccp = &cp->ccps[compno]; + + /* + * Decode the packet header. + */ + + /* Decode the SOP marker segment if present. */ + if (cp->csty & JPC_COD_SOP) { + if (jpc_dec_lookahead(in) == JPC_MS_SOP) { + if (!(ms = jpc_getms(in, dec->cstate))) { + return -1; + } + if (jpc_ms_gettype(ms) != JPC_MS_SOP) { + jpc_ms_destroy(ms); + jas_eprintf("missing SOP marker segment\n"); + return -1; + } + jpc_ms_destroy(ms); + } + } + +hdroffstart = jas_stream_getrwcount(pkthdrstream); + + if (!(inb = jpc_bitstream_sopen(pkthdrstream, "r"))) { + return -1; + } + + if ((present = jpc_bitstream_getbit(inb)) < 0) { + return 1; + } + JAS_DBGLOG(10, ("\n", present)); + JAS_DBGLOG(10, ("present=%d ", present)); + + /* Is the packet non-empty? */ + if (present) { + /* The packet is non-empty. */ + tcomp = &tile->tcomps[compno]; + rlvl = &tcomp->rlvls[rlvlno]; + bodylen = 0; + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band->data) { + continue; + } + prc = &band->prcs[prcno]; + if (!prc->cblks) { + continue; + } + usedcblkcnt = 0; + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + ++usedcblkcnt; + if (!cblk->numpasses) { + leaf = jpc_tagtree_getleaf(prc->incltagtree, usedcblkcnt - 1); + if ((included = jpc_tagtree_decode(prc->incltagtree, leaf, lyrno + 1, inb)) < 0) { + return -1; + } + } else { + if ((included = jpc_bitstream_getbit(inb)) < 0) { + return -1; + } + } + JAS_DBGLOG(10, ("\n")); + JAS_DBGLOG(10, ("included=%d ", included)); + if (!included) { + continue; + } + if (!cblk->numpasses) { + i = 1; + leaf = jpc_tagtree_getleaf(prc->numimsbstagtree, usedcblkcnt - 1); + for (;;) { + if ((ret = jpc_tagtree_decode(prc->numimsbstagtree, leaf, i, inb)) < 0) { + return -1; + } + if (ret) { + break; + } + ++i; + } + cblk->numimsbs = i - 1; + cblk->firstpassno = cblk->numimsbs * 3; + } + if ((numnewpasses = jpc_getnumnewpasses(inb)) < 0) { + return -1; + } + JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses)); + seg = cblk->curseg; + savenumnewpasses = numnewpasses; + mycounter = 0; + if (numnewpasses > 0) { + if ((m = jpc_getcommacode(inb)) < 0) { + return -1; + } + cblk->numlenbits += m; + JAS_DBGLOG(10, ("increment=%d ", m)); + while (numnewpasses > 0) { + passno = cblk->firstpassno + cblk->numpasses + mycounter; + /* XXX - the maxpasses is not set precisely but this doesn't matter... */ + maxpasses = JPC_SEGPASSCNT(passno, cblk->firstpassno, 10000, (ccp->cblkctx & JPC_COX_LAZY) != 0, (ccp->cblkctx & JPC_COX_TERMALL) != 0); + if (!discard && !seg) { + if (!(seg = jpc_seg_alloc())) { + return -1; + } + jpc_seglist_insert(&cblk->segs, cblk->segs.tail, seg); + if (!cblk->curseg) { + cblk->curseg = seg; + } + seg->passno = passno; + seg->type = JPC_SEGTYPE(seg->passno, cblk->firstpassno, (ccp->cblkctx & JPC_COX_LAZY) != 0); + seg->maxpasses = maxpasses; + } + n = JAS_MIN(numnewpasses, maxpasses); + mycounter += n; + numnewpasses -= n; + if ((len = jpc_bitstream_getbits(inb, cblk->numlenbits + jpc_floorlog2(n))) < 0) { + return -1; + } + JAS_DBGLOG(10, ("len=%d ", len)); + if (!discard) { + seg->lyrno = lyrno; + seg->numpasses += n; + seg->cnt = len; + seg = seg->next; + } + bodylen += len; + } + } + cblk->numpasses += savenumnewpasses; + } + } + + jpc_bitstream_inalign(inb, 0, 0); + + } else { + if (jpc_bitstream_inalign(inb, 0x7f, 0)) { + jas_eprintf("alignment failed\n"); + return -1; + } + } + jpc_bitstream_close(inb); + + hdroffend = jas_stream_getrwcount(pkthdrstream); + hdrlen = hdroffend - hdroffstart; + if (jas_getdbglevel() >= 5) { + jas_eprintf("hdrlen=%lu bodylen=%lu \n", (unsigned long) hdrlen, + (unsigned long) bodylen); + } + + if (cp->csty & JPC_COD_EPH) { + if (jpc_dec_lookahead(pkthdrstream) == JPC_MS_EPH) { + if (!(ms = jpc_getms(pkthdrstream, dec->cstate))) { + jas_eprintf("cannot get (EPH) marker segment\n"); + return -1; + } + if (jpc_ms_gettype(ms) != JPC_MS_EPH) { + jpc_ms_destroy(ms); + jas_eprintf("missing EPH marker segment\n"); + return -1; + } + jpc_ms_destroy(ms); + } + } + + /* decode the packet body. */ + + if (jas_getdbglevel() >= 1) { + jas_eprintf("packet body offset=%06ld\n", (long) jas_stream_getrwcount(in)); + } + + if (!discard) { + tcomp = &tile->tcomps[compno]; + rlvl = &tcomp->rlvls[rlvlno]; + for (bandno = 0, band = rlvl->bands; bandno < rlvl->numbands; + ++bandno, ++band) { + if (!band->data) { + continue; + } + prc = &band->prcs[prcno]; + if (!prc->cblks) { + continue; + } + for (cblkno = 0, cblk = prc->cblks; cblkno < prc->numcblks; + ++cblkno, ++cblk) { + seg = cblk->curseg; + while (seg) { + if (!seg->stream) { + if (!(seg->stream = jas_stream_memopen(0, 0))) { + return -1; + } + } +#if 0 +jas_eprintf("lyrno=%02d, compno=%02d, lvlno=%02d, prcno=%02d, bandno=%02d, cblkno=%02d, passno=%02d numpasses=%02d cnt=%d numbps=%d, numimsbs=%d\n", lyrno, compno, rlvlno, prcno, band - rlvl->bands, cblk - prc->cblks, seg->passno, seg->numpasses, seg->cnt, band->numbps, cblk->numimsbs); +#endif + if (seg->cnt > 0) { + if (jpc_getdata(in, seg->stream, seg->cnt) < 0) { + return -1; + } + seg->cnt = 0; + } + if (seg->numpasses >= seg->maxpasses) { + cblk->curseg = seg->next; + } + seg = seg->next; + } + } + } + } else { + if (jas_stream_gobble(in, bodylen) != JAS_CAST(int, bodylen)) { + return -1; + } + } + return 0; +} + +/********************************************************************************************/ +/********************************************************************************************/ + +int jpc_dec_decodepkts(jpc_dec_t *dec, jas_stream_t *pkthdrstream, jas_stream_t *in) +{ + jpc_dec_tile_t *tile; + jpc_pi_t *pi; + int ret; + + tile = dec->curtile; + pi = tile->pi; + for (;;) { +if (!tile->pkthdrstream || jas_stream_peekc(tile->pkthdrstream) == EOF) { + switch (jpc_dec_lookahead(in)) { + case JPC_MS_EOC: + case JPC_MS_SOT: + return 0; + break; + case JPC_MS_SOP: + case JPC_MS_EPH: + case 0: + break; + default: + return -1; + break; + } +} + if ((ret = jpc_pi_next(pi))) { + return ret; + } +if (dec->maxpkts >= 0 && dec->numpkts >= dec->maxpkts) { + jas_eprintf("warning: stopping decode prematurely as requested\n"); + return 0; +} + if (jas_getdbglevel() >= 1) { + jas_eprintf("packet offset=%08ld prg=%d cmptno=%02d " + "rlvlno=%02d prcno=%03d lyrno=%02d\n", (long) + jas_stream_getrwcount(in), jpc_pi_prg(pi), jpc_pi_cmptno(pi), + jpc_pi_rlvlno(pi), jpc_pi_prcno(pi), jpc_pi_lyrno(pi)); + } + if (jpc_dec_decodepkt(dec, pkthdrstream, in, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi), + jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) { + return -1; + } +++dec->numpkts; + } + + return 0; +} + +jpc_pi_t *jpc_dec_pi_create(jpc_dec_t *dec, jpc_dec_tile_t *tile) +{ + jpc_pi_t *pi; + int compno; + jpc_picomp_t *picomp; + jpc_pirlvl_t *pirlvl; + jpc_dec_tcomp_t *tcomp; + int rlvlno; + jpc_dec_rlvl_t *rlvl; + int prcno; + int *prclyrno; + jpc_dec_cmpt_t *cmpt; + + if (!(pi = jpc_pi_create0())) { + return 0; + } + pi->numcomps = dec->numcomps; + if (!(pi->picomps = jas_malloc(pi->numcomps * sizeof(jpc_picomp_t)))) { + jpc_pi_destroy(pi); + return 0; + } + for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno, + ++picomp) { + picomp->pirlvls = 0; + } + + for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps; + compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { + picomp->numrlvls = tcomp->numrlvls; + if (!(picomp->pirlvls = jas_malloc(picomp->numrlvls * + sizeof(jpc_pirlvl_t)))) { + jpc_pi_destroy(pi); + return 0; + } + for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < + picomp->numrlvls; ++rlvlno, ++pirlvl) { + pirlvl->prclyrnos = 0; + } + for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; + rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { +/* XXX sizeof(long) should be sizeof different type */ + pirlvl->numprcs = rlvl->numprcs; + if (!(pirlvl->prclyrnos = jas_malloc(pirlvl->numprcs * + sizeof(long)))) { + jpc_pi_destroy(pi); + return 0; + } + } + } + + pi->maxrlvls = 0; + for (compno = 0, tcomp = tile->tcomps, picomp = pi->picomps, cmpt = + dec->cmpts; compno < pi->numcomps; ++compno, ++tcomp, ++picomp, + ++cmpt) { + picomp->hsamp = cmpt->hstep; + picomp->vsamp = cmpt->vstep; + for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; + rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { + pirlvl->prcwidthexpn = rlvl->prcwidthexpn; + pirlvl->prcheightexpn = rlvl->prcheightexpn; + for (prcno = 0, prclyrno = pirlvl->prclyrnos; + prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { + *prclyrno = 0; + } + pirlvl->numhprcs = rlvl->numhprcs; + } + if (pi->maxrlvls < tcomp->numrlvls) { + pi->maxrlvls = tcomp->numrlvls; + } + } + + pi->numlyrs = tile->cp->numlyrs; + pi->xstart = tile->xstart; + pi->ystart = tile->ystart; + pi->xend = tile->xend; + pi->yend = tile->yend; + + pi->picomp = 0; + pi->pirlvl = 0; + pi->x = 0; + pi->y = 0; + pi->compno = 0; + pi->rlvlno = 0; + pi->prcno = 0; + pi->lyrno = 0; + pi->xstep = 0; + pi->ystep = 0; + + pi->pchgno = -1; + + pi->defaultpchg.prgord = tile->cp->prgord; + pi->defaultpchg.compnostart = 0; + pi->defaultpchg.compnoend = pi->numcomps; + pi->defaultpchg.rlvlnostart = 0; + pi->defaultpchg.rlvlnoend = pi->maxrlvls; + pi->defaultpchg.lyrnoend = pi->numlyrs; + pi->pchg = 0; + + pi->valid = 0; + + return pi; +} + +long jpc_dec_lookahead(jas_stream_t *in) +{ + uint_fast16_t x; + if (jpc_getuint16(in, &x)) { + return -1; + } + if (jas_stream_ungetc(in, x & 0xff) == EOF || + jas_stream_ungetc(in, x >> 8) == EOF) { + return -1; + } + if (x >= JPC_MS_INMIN && x <= JPC_MS_INMAX) { + return x; + } + return 0; +} diff --git a/src/libjasper/jpc/jpc_t2dec.h b/src/libjasper/jpc/jpc_t2dec.h new file mode 100644 index 0000000..6517252 --- /dev/null +++ b/src/libjasper/jpc/jpc_t2dec.h @@ -0,0 +1,95 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier 2 Decoder + * + * $Id: jpc_t2dec.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_T2DEC_H +#define JPC_T2DEC_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_fix.h" +#include "jasper/jas_stream.h" + +#include "jpc_bs.h" +#include "jpc_dec.h" +#include "jpc_mqdec.h" + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Decode the packets for a tile-part. */ +int jpc_dec_decodepkts(jpc_dec_t *dec, jas_stream_t *pkthdrstream, + jas_stream_t *in); + +/* Create a packet iterator for the decoder. */ +jpc_pi_t *jpc_dec_pi_create(jpc_dec_t *dec, jpc_dec_tile_t *tile); + +#endif diff --git a/src/libjasper/jpc/jpc_t2enc.c b/src/libjasper/jpc/jpc_t2enc.c new file mode 100644 index 0000000..3c807d9 --- /dev/null +++ b/src/libjasper/jpc/jpc_t2enc.c @@ -0,0 +1,655 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier 2 Encoder + * + * $Id: jpc_t2enc.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jasper/jas_fix.h" +#include "jasper/jas_malloc.h" +#include "jasper/jas_math.h" +#include "jasper/jas_debug.h" + +#include "jpc_flt.h" +#include "jpc_t2enc.h" +#include "jpc_t2cod.h" +#include "jpc_tagtree.h" +#include "jpc_enc.h" +#include "jpc_math.h" + +/******************************************************************************\ +* Code. +\******************************************************************************/ + +static int jpc_putcommacode(jpc_bitstream_t *out, int n) +{ + assert(n >= 0); + + while (--n >= 0) { + if (jpc_bitstream_putbit(out, 1) == EOF) { + return -1; + } + } + if (jpc_bitstream_putbit(out, 0) == EOF) { + return -1; + } + return 0; +} + +static int jpc_putnumnewpasses(jpc_bitstream_t *out, int n) +{ + int ret; + + if (n <= 0) { + return -1; + } else if (n == 1) { + ret = jpc_bitstream_putbit(out, 0); + } else if (n == 2) { + ret = jpc_bitstream_putbits(out, 2, 2); + } else if (n <= 5) { + ret = jpc_bitstream_putbits(out, 4, 0xc | (n - 3)); + } else if (n <= 36) { + ret = jpc_bitstream_putbits(out, 9, 0x1e0 | (n - 6)); + } else if (n <= 164) { + ret = jpc_bitstream_putbits(out, 16, 0xff80 | (n - 37)); + } else { + /* The standard has no provision for encoding a larger value. + In practice, however, it is highly unlikely that this + limitation will ever be encountered. */ + return -1; + } + + return (ret != EOF) ? 0 : (-1); +} + +int jpc_enc_encpkts(jpc_enc_t *enc, jas_stream_t *out) +{ + jpc_enc_tile_t *tile; + jpc_pi_t *pi; + + tile = enc->curtile; + + jpc_init_t2state(enc, 0); + pi = tile->pi; + jpc_pi_init(pi); + + if (!jpc_pi_next(pi)) { + for (;;) { + if (jpc_enc_encpkt(enc, out, jpc_pi_cmptno(pi), jpc_pi_rlvlno(pi), + jpc_pi_prcno(pi), jpc_pi_lyrno(pi))) { + return -1; + } + if (jpc_pi_next(pi)) { + break; + } + } + } + + return 0; +} + +int jpc_enc_encpkt(jpc_enc_t *enc, jas_stream_t *out, int compno, int lvlno, int prcno, int lyrno) +{ + jpc_enc_tcmpt_t *comp; + jpc_enc_rlvl_t *lvl; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_cblk_t *cblk; + jpc_enc_cblk_t *endcblks; + jpc_bitstream_t *outb; + jpc_enc_pass_t *pass; + jpc_enc_pass_t *startpass; + jpc_enc_pass_t *lastpass; + jpc_enc_pass_t *endpass; + jpc_enc_pass_t *endpasses; + int i; + int included; + int ret; + jpc_tagtreenode_t *leaf; + int n; + int t1; + int t2; + int adjust; + int maxadjust; + int datalen; + int numnewpasses; + int passcount; + jpc_enc_tile_t *tile; + jpc_enc_prc_t *prc; + jpc_enc_cp_t *cp; + jpc_ms_t *ms; + + tile = enc->curtile; + cp = enc->cp; + + if (cp->tcp.csty & JPC_COD_SOP) { + if (!(ms = jpc_ms_create(JPC_MS_SOP))) { + return -1; + } + ms->parms.sop.seqno = jpc_pi_getind(tile->pi); + if (jpc_putms(out, enc->cstate, ms)) { + return -1; + } + jpc_ms_destroy(ms); + } + + outb = jpc_bitstream_sopen(out, "w+"); + assert(outb); + + if (jpc_bitstream_putbit(outb, 1) == EOF) { + return -1; + } + JAS_DBGLOG(10, ("\n")); + JAS_DBGLOG(10, ("present. ")); + + comp = &tile->tcmpts[compno]; + lvl = &comp->rlvls[lvlno]; + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + prc = &band->prcs[prcno]; + if (!prc->cblks) { + continue; + } + + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + if (!lyrno) { + leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); + jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs); + } + pass = cblk->curpass; + included = (pass && pass->lyrno == lyrno); + if (included && (!cblk->numencpasses)) { + assert(pass->lyrno == lyrno); + leaf = jpc_tagtree_getleaf(prc->incltree, + cblk - prc->cblks); + jpc_tagtree_setvalue(prc->incltree, leaf, pass->lyrno); + } + } + + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + pass = cblk->curpass; + included = (pass && pass->lyrno == lyrno); + if (!cblk->numencpasses) { + leaf = jpc_tagtree_getleaf(prc->incltree, + cblk - prc->cblks); + if (jpc_tagtree_encode(prc->incltree, leaf, lyrno + + 1, outb) < 0) { + return -1; + } + } else { + if (jpc_bitstream_putbit(outb, included) == EOF) { + return -1; + } + } + JAS_DBGLOG(10, ("included=%d ", included)); + if (!included) { + continue; + } + if (!cblk->numencpasses) { + i = 1; + leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); + for (;;) { + if ((ret = jpc_tagtree_encode(prc->nlibtree, leaf, i, outb)) < 0) { + return -1; + } + if (ret) { + break; + } + ++i; + } + assert(leaf->known_ && i == leaf->value_ + 1); + } + + endpasses = &cblk->passes[cblk->numpasses]; + startpass = pass; + endpass = startpass; + while (endpass != endpasses && endpass->lyrno == lyrno){ + ++endpass; + } + numnewpasses = endpass - startpass; + if (jpc_putnumnewpasses(outb, numnewpasses)) { + return -1; + } + JAS_DBGLOG(10, ("numnewpasses=%d ", numnewpasses)); + + lastpass = endpass - 1; + n = startpass->start; + passcount = 1; + maxadjust = 0; + for (pass = startpass; pass != endpass; ++pass) { + if (pass->term || pass == lastpass) { + datalen = pass->end - n; + t1 = jpc_firstone(datalen) + 1; + t2 = cblk->numlenbits + jpc_floorlog2(passcount); + adjust = JAS_MAX(t1 - t2, 0); + maxadjust = JAS_MAX(adjust, maxadjust); + n += datalen; + passcount = 1; + } else { + ++passcount; + } + } + if (jpc_putcommacode(outb, maxadjust)) { + return -1; + } + cblk->numlenbits += maxadjust; + + lastpass = endpass - 1; + n = startpass->start; + passcount = 1; + for (pass = startpass; pass != endpass; ++pass) { + if (pass->term || pass == lastpass) { + datalen = pass->end - n; +assert(jpc_firstone(datalen) < cblk->numlenbits + jpc_floorlog2(passcount)); + if (jpc_bitstream_putbits(outb, cblk->numlenbits + jpc_floorlog2(passcount), datalen) == EOF) { + return -1; + } + n += datalen; + passcount = 1; + } else { + ++passcount; + } + } + } + } + + jpc_bitstream_outalign(outb, 0); + jpc_bitstream_close(outb); + + if (cp->tcp.csty & JPC_COD_EPH) { + if (!(ms = jpc_ms_create(JPC_MS_EPH))) { + return -1; + } + jpc_putms(out, enc->cstate, ms); + jpc_ms_destroy(ms); + } + + comp = &tile->tcmpts[compno]; + lvl = &comp->rlvls[lvlno]; + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + prc = &band->prcs[prcno]; + if (!prc->cblks) { + continue; + } + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + pass = cblk->curpass; + + if (!pass) { + continue; + } + if (pass->lyrno != lyrno) { + assert(pass->lyrno < 0 || pass->lyrno > lyrno); + continue; + } + + endpasses = &cblk->passes[cblk->numpasses]; + startpass = pass; + endpass = startpass; + while (endpass != endpasses && endpass->lyrno == lyrno){ + ++endpass; + } + lastpass = endpass - 1; + numnewpasses = endpass - startpass; + + jas_stream_seek(cblk->stream, startpass->start, SEEK_SET); + assert(jas_stream_tell(cblk->stream) == startpass->start); + if (jas_stream_copy(out, cblk->stream, lastpass->end - startpass->start)) { + return -1; + } + cblk->curpass = (endpass != endpasses) ? endpass : 0; + cblk->numencpasses += numnewpasses; + + } + } + + return 0; +} + +void jpc_save_t2state(jpc_enc_t *enc) +{ +/* stream pos in embedded T1 stream may be wrong since not saved/restored! */ + + jpc_enc_tcmpt_t *comp; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_rlvl_t *lvl; + jpc_enc_rlvl_t *endlvls; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_cblk_t *cblk; + jpc_enc_cblk_t *endcblks; + jpc_enc_tile_t *tile; + int prcno; + jpc_enc_prc_t *prc; + + tile = enc->curtile; + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + jpc_tagtree_copy(prc->savincltree, prc->incltree); + jpc_tagtree_copy(prc->savnlibtree, prc->nlibtree); + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + cblk->savedcurpass = cblk->curpass; + cblk->savednumencpasses = cblk->numencpasses; + cblk->savednumlenbits = cblk->numlenbits; + } + } + } + } + } + +} + +void jpc_restore_t2state(jpc_enc_t *enc) +{ + + jpc_enc_tcmpt_t *comp; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_rlvl_t *lvl; + jpc_enc_rlvl_t *endlvls; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_cblk_t *cblk; + jpc_enc_cblk_t *endcblks; + jpc_enc_tile_t *tile; + int prcno; + jpc_enc_prc_t *prc; + + tile = enc->curtile; + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + jpc_tagtree_copy(prc->incltree, prc->savincltree); + jpc_tagtree_copy(prc->nlibtree, prc->savnlibtree); + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + cblk->curpass = cblk->savedcurpass; + cblk->numencpasses = cblk->savednumencpasses; + cblk->numlenbits = cblk->savednumlenbits; + } + } + } + } + } +} + +void jpc_init_t2state(jpc_enc_t *enc, int raflag) +{ +/* It is assumed that band->numbps and cblk->numbps precomputed */ + + jpc_enc_tcmpt_t *comp; + jpc_enc_tcmpt_t *endcomps; + jpc_enc_rlvl_t *lvl; + jpc_enc_rlvl_t *endlvls; + jpc_enc_band_t *band; + jpc_enc_band_t *endbands; + jpc_enc_cblk_t *cblk; + jpc_enc_cblk_t *endcblks; + jpc_enc_pass_t *pass; + jpc_enc_pass_t *endpasses; + jpc_tagtreenode_t *leaf; + jpc_enc_tile_t *tile; + int prcno; + jpc_enc_prc_t *prc; + + tile = enc->curtile; + + endcomps = &tile->tcmpts[tile->numtcmpts]; + for (comp = tile->tcmpts; comp != endcomps; ++comp) { + endlvls = &comp->rlvls[comp->numrlvls]; + for (lvl = comp->rlvls; lvl != endlvls; ++lvl) { + if (!lvl->bands) { + continue; + } + endbands = &lvl->bands[lvl->numbands]; + for (band = lvl->bands; band != endbands; ++band) { + if (!band->data) { + continue; + } + for (prcno = 0, prc = band->prcs; prcno < lvl->numprcs; ++prcno, ++prc) { + if (!prc->cblks) { + continue; + } + jpc_tagtree_reset(prc->incltree); + jpc_tagtree_reset(prc->nlibtree); + endcblks = &prc->cblks[prc->numcblks]; + for (cblk = prc->cblks; cblk != endcblks; ++cblk) { + if (jas_stream_rewind(cblk->stream)) { + assert(0); + } + cblk->curpass = (cblk->numpasses > 0) ? cblk->passes : 0; + cblk->numencpasses = 0; + cblk->numlenbits = 3; + cblk->numimsbs = band->numbps - cblk->numbps; + assert(cblk->numimsbs >= 0); + leaf = jpc_tagtree_getleaf(prc->nlibtree, cblk - prc->cblks); + jpc_tagtree_setvalue(prc->nlibtree, leaf, cblk->numimsbs); + + if (raflag) { + endpasses = &cblk->passes[cblk->numpasses]; + for (pass = cblk->passes; pass != endpasses; ++pass) { + pass->lyrno = -1; + pass->lyrno = 0; + } + } + } + } + } + } + } + +} + +jpc_pi_t *jpc_enc_pi_create(jpc_enc_cp_t *cp, jpc_enc_tile_t *tile) +{ + jpc_pi_t *pi; + int compno; + jpc_picomp_t *picomp; + jpc_pirlvl_t *pirlvl; + jpc_enc_tcmpt_t *tcomp; + int rlvlno; + jpc_enc_rlvl_t *rlvl; + int prcno; + int *prclyrno; + + if (!(pi = jpc_pi_create0())) { + return 0; + } + pi->pktno = -1; + pi->numcomps = cp->numcmpts; + if (!(pi->picomps = jas_malloc(pi->numcomps * sizeof(jpc_picomp_t)))) { + jpc_pi_destroy(pi); + return 0; + } + for (compno = 0, picomp = pi->picomps; compno < pi->numcomps; ++compno, + ++picomp) { + picomp->pirlvls = 0; + } + + for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps; + compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { + picomp->numrlvls = tcomp->numrlvls; + if (!(picomp->pirlvls = jas_malloc(picomp->numrlvls * + sizeof(jpc_pirlvl_t)))) { + jpc_pi_destroy(pi); + return 0; + } + for (rlvlno = 0, pirlvl = picomp->pirlvls; rlvlno < + picomp->numrlvls; ++rlvlno, ++pirlvl) { + pirlvl->prclyrnos = 0; + } + for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; + rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { +/* XXX sizeof(long) should be sizeof different type */ + pirlvl->numprcs = rlvl->numprcs; + if (rlvl->numprcs) { + if (!(pirlvl->prclyrnos = jas_malloc(pirlvl->numprcs * + sizeof(long)))) { + jpc_pi_destroy(pi); + return 0; + } + } else { + pirlvl->prclyrnos = 0; + } + } + } + + pi->maxrlvls = 0; + for (compno = 0, tcomp = tile->tcmpts, picomp = pi->picomps; + compno < pi->numcomps; ++compno, ++tcomp, ++picomp) { + picomp->hsamp = cp->ccps[compno].sampgrdstepx; + picomp->vsamp = cp->ccps[compno].sampgrdstepy; + for (rlvlno = 0, pirlvl = picomp->pirlvls, rlvl = tcomp->rlvls; + rlvlno < picomp->numrlvls; ++rlvlno, ++pirlvl, ++rlvl) { + pirlvl->prcwidthexpn = rlvl->prcwidthexpn; + pirlvl->prcheightexpn = rlvl->prcheightexpn; + for (prcno = 0, prclyrno = pirlvl->prclyrnos; + prcno < pirlvl->numprcs; ++prcno, ++prclyrno) { + *prclyrno = 0; + } + pirlvl->numhprcs = rlvl->numhprcs; + } + if (pi->maxrlvls < tcomp->numrlvls) { + pi->maxrlvls = tcomp->numrlvls; + } + } + + pi->numlyrs = tile->numlyrs; + pi->xstart = tile->tlx; + pi->ystart = tile->tly; + pi->xend = tile->brx; + pi->yend = tile->bry; + + pi->picomp = 0; + pi->pirlvl = 0; + pi->x = 0; + pi->y = 0; + pi->compno = 0; + pi->rlvlno = 0; + pi->prcno = 0; + pi->lyrno = 0; + pi->xstep = 0; + pi->ystep = 0; + + pi->pchgno = -1; + + pi->defaultpchg.prgord = tile->prg; + pi->defaultpchg.compnostart = 0; + pi->defaultpchg.compnoend = pi->numcomps; + pi->defaultpchg.rlvlnostart = 0; + pi->defaultpchg.rlvlnoend = pi->maxrlvls; + pi->defaultpchg.lyrnoend = pi->numlyrs; + pi->pchg = 0; + + pi->valid = 0; + + return pi; +} diff --git a/src/libjasper/jpc/jpc_t2enc.h b/src/libjasper/jpc/jpc_t2enc.h new file mode 100644 index 0000000..f2ade16 --- /dev/null +++ b/src/libjasper/jpc/jpc_t2enc.h @@ -0,0 +1,106 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tier 2 Encoder + * + * $Id: jpc_t2enc.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_T2ENC_H +#define JPC_T2ENC_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include + +#include "jpc_enc.h" + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Encode the packets for a tile. */ +int jpc_enc_encpkts(jpc_enc_t *enc, jas_stream_t *out); + +/* Encode the specified packet. */ +int jpc_enc_encpkt(jpc_enc_t *enc, jas_stream_t *out, int compno, int lvlno, + int prcno, int lyrno); + +/* Save the tier-2 coding state. */ +void jpc_save_t2state(jpc_enc_t *enc); + +/* Restore the tier-2 coding state. */ +void jpc_restore_t2state(jpc_enc_t *enc); + +/* Initialize the tier-2 coding state. */ +void jpc_init_t2state(jpc_enc_t *enc, int raflag); + +/* Create a packet iterator for the encoder. */ +jpc_pi_t *jpc_enc_pi_create(jpc_enc_cp_t *cp, jpc_enc_tile_t *tile); + +#endif diff --git a/src/libjasper/jpc/jpc_tagtree.c b/src/libjasper/jpc/jpc_tagtree.c new file mode 100644 index 0000000..c4878d1 --- /dev/null +++ b/src/libjasper/jpc/jpc_tagtree.c @@ -0,0 +1,393 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tag Tree Library + * + * $Id: jpc_tagtree.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include +#include +#include +#include + +#include "jasper/jas_malloc.h" + +#include "jpc_tagtree.h" + +/******************************************************************************\ +* Prototypes. +\******************************************************************************/ + +static jpc_tagtree_t *jpc_tagtree_alloc(void); + +/******************************************************************************\ +* Code for creating and destroying tag trees. +\******************************************************************************/ + +/* Create a tag tree. */ + +jpc_tagtree_t *jpc_tagtree_create(int numleafsh, int numleafsv) +{ + int nplh[JPC_TAGTREE_MAXDEPTH]; + int nplv[JPC_TAGTREE_MAXDEPTH]; + jpc_tagtreenode_t *node; + jpc_tagtreenode_t *parentnode; + jpc_tagtreenode_t *parentnode0; + jpc_tagtree_t *tree; + int i; + int j; + int k; + int numlvls; + int n; + + assert(numleafsh > 0 && numleafsv > 0); + + if (!(tree = jpc_tagtree_alloc())) { + return 0; + } + tree->numleafsh_ = numleafsh; + tree->numleafsv_ = numleafsv; + + numlvls = 0; + nplh[0] = numleafsh; + nplv[0] = numleafsv; + do { + n = nplh[numlvls] * nplv[numlvls]; + nplh[numlvls + 1] = (nplh[numlvls] + 1) / 2; + nplv[numlvls + 1] = (nplv[numlvls] + 1) / 2; + tree->numnodes_ += n; + ++numlvls; + } while (n > 1); + + if (!(tree->nodes_ = jas_malloc(tree->numnodes_ * sizeof(jpc_tagtreenode_t)))) { + return 0; + } + + /* Initialize the parent links for all nodes in the tree. */ + + node = tree->nodes_; + parentnode = &tree->nodes_[tree->numleafsh_ * tree->numleafsv_]; + parentnode0 = parentnode; + + for (i = 0; i < numlvls - 1; ++i) { + for (j = 0; j < nplv[i]; ++j) { + k = nplh[i]; + while (--k >= 0) { + node->parent_ = parentnode; + ++node; + if (--k >= 0) { + node->parent_ = parentnode; + ++node; + } + ++parentnode; + } + if ((j & 1) || j == nplv[i] - 1) { + parentnode0 = parentnode; + } else { + parentnode = parentnode0; + parentnode0 += nplh[i]; + } + } + } + node->parent_ = 0; + + /* Initialize the data values to something sane. */ + + jpc_tagtree_reset(tree); + + return tree; +} + +/* Destroy a tag tree. */ + +void jpc_tagtree_destroy(jpc_tagtree_t *tree) +{ + if (tree->nodes_) { + jas_free(tree->nodes_); + } + jas_free(tree); +} + +static jpc_tagtree_t *jpc_tagtree_alloc() +{ + jpc_tagtree_t *tree; + + if (!(tree = jas_malloc(sizeof(jpc_tagtree_t)))) { + return 0; + } + tree->numleafsh_ = 0; + tree->numleafsv_ = 0; + tree->numnodes_ = 0; + tree->nodes_ = 0; + + return tree; +} + +/******************************************************************************\ +* Code. +\******************************************************************************/ + +/* Copy state information from one tag tree to another. */ + +void jpc_tagtree_copy(jpc_tagtree_t *dsttree, jpc_tagtree_t *srctree) +{ + int n; + jpc_tagtreenode_t *srcnode; + jpc_tagtreenode_t *dstnode; + + /* The two tag trees must have similar sizes. */ + assert(srctree->numleafsh_ == dsttree->numleafsh_ && + srctree->numleafsv_ == dsttree->numleafsv_); + + n = srctree->numnodes_; + srcnode = srctree->nodes_; + dstnode = dsttree->nodes_; + while (--n >= 0) { + dstnode->value_ = srcnode->value_; + dstnode->low_ = srcnode->low_; + dstnode->known_ = srcnode->known_; + ++dstnode; + ++srcnode; + } +} + +/* Reset all of the state information associated with a tag tree. */ + +void jpc_tagtree_reset(jpc_tagtree_t *tree) +{ + int n; + jpc_tagtreenode_t *node; + + n = tree->numnodes_; + node = tree->nodes_; + + while (--n >= 0) { + node->value_ = INT_MAX; + node->low_ = 0; + node->known_ = 0; + ++node; + } +} + +/* Set the value associated with the specified leaf node, updating +the other nodes as necessary. */ + +void jpc_tagtree_setvalue(jpc_tagtree_t *tree, jpc_tagtreenode_t *leaf, + int value) +{ + jpc_tagtreenode_t *node; + + /* Avoid compiler warnings about unused parameters. */ + tree = 0; + + assert(value >= 0); + + node = leaf; + while (node && node->value_ > value) { + node->value_ = value; + node = node->parent_; + } +} + +/* Get a particular leaf node. */ + +jpc_tagtreenode_t *jpc_tagtree_getleaf(jpc_tagtree_t *tree, int n) +{ + return &tree->nodes_[n]; +} + +/* Invoke the tag tree encoding procedure. */ + +int jpc_tagtree_encode(jpc_tagtree_t *tree, jpc_tagtreenode_t *leaf, + int threshold, jpc_bitstream_t *out) +{ + jpc_tagtreenode_t *stk[JPC_TAGTREE_MAXDEPTH - 1]; + jpc_tagtreenode_t **stkptr; + jpc_tagtreenode_t *node; + int low; + + /* Avoid compiler warnings about unused parameters. */ + tree = 0; + + assert(leaf); + assert(threshold >= 0); + + /* Traverse to the root of the tree, recording the path taken. */ + stkptr = stk; + node = leaf; + while (node->parent_) { + *stkptr++ = node; + node = node->parent_; + } + + low = 0; + for (;;) { + if (low > node->low_) { + /* Deferred propagation of the lower bound downward in + the tree. */ + node->low_ = low; + } else { + low = node->low_; + } + + while (low < threshold) { + if (low >= node->value_) { + if (!node->known_) { + if (jpc_bitstream_putbit(out, 1) == EOF) { + return -1; + } + node->known_ = 1; + } + break; + } + if (jpc_bitstream_putbit(out, 0) == EOF) { + return -1; + } + ++low; + } + node->low_ = low; + if (stkptr == stk) { + break; + } + node = *--stkptr; + + } + return (leaf->low_ < threshold) ? 1 : 0; + +} + +/* Invoke the tag tree decoding procedure. */ + +int jpc_tagtree_decode(jpc_tagtree_t *tree, jpc_tagtreenode_t *leaf, + int threshold, jpc_bitstream_t *in) +{ + jpc_tagtreenode_t *stk[JPC_TAGTREE_MAXDEPTH - 1]; + jpc_tagtreenode_t **stkptr; + jpc_tagtreenode_t *node; + int low; + int ret; + + /* Avoid compiler warnings about unused parameters. */ + tree = 0; + + assert(threshold >= 0); + + /* Traverse to the root of the tree, recording the path taken. */ + stkptr = stk; + node = leaf; + while (node->parent_) { + *stkptr++ = node; + node = node->parent_; + } + + low = 0; + for (;;) { + if (low > node->low_) { + node->low_ = low; + } else { + low = node->low_; + } + while (low < threshold && low < node->value_) { + if ((ret = jpc_bitstream_getbit(in)) < 0) { + return -1; + } + if (ret) { + node->value_ = low; + } else { + ++low; + } + } + node->low_ = low; + if (stkptr == stk) { + break; + } + node = *--stkptr; + } + + return (node->value_ < threshold) ? 1 : 0; +} + +/******************************************************************************\ +* Code for debugging. +\******************************************************************************/ + +void jpc_tagtree_dump(jpc_tagtree_t *tree, FILE *out) +{ + jpc_tagtreenode_t *node; + int n; + + node = tree->nodes_; + n = tree->numnodes_; + while (--n >= 0) { + fprintf(out, "node %p, parent %p, value %d, lower %d, known %d\n", + (void *) node, (void *) node->parent_, node->value_, node->low_, + node->known_); + ++node; + } +} diff --git a/src/libjasper/jpc/jpc_tagtree.h b/src/libjasper/jpc/jpc_tagtree.h new file mode 100644 index 0000000..22c4b84 --- /dev/null +++ b/src/libjasper/jpc/jpc_tagtree.h @@ -0,0 +1,167 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tag Tree Library + * + * $Id: jpc_tagtree.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_TAGTREE_H +#define JPC_TAGTREE_H + +/******************************************************************************\ +* Includes +\******************************************************************************/ + +#include +#include + +#include "jpc_bs.h" + +/******************************************************************************\ +* Constants +\******************************************************************************/ + +/* The maximum allowable depth for a tag tree. */ +#define JPC_TAGTREE_MAXDEPTH 32 + +/******************************************************************************\ +* Types +\******************************************************************************/ + +/* + * Tag tree node. + */ + +typedef struct jpc_tagtreenode_ { + + /* The parent of this node. */ + struct jpc_tagtreenode_ *parent_; + + /* The value associated with this node. */ + int value_; + + /* The lower bound on the value associated with this node. */ + int low_; + + /* A flag indicating if the value is known exactly. */ + int known_; + +} jpc_tagtreenode_t; + +/* + * Tag tree. + */ + +typedef struct { + + /* The number of leaves in the horizontal direction. */ + int numleafsh_; + + /* The number of leaves in the vertical direction. */ + int numleafsv_; + + /* The total number of nodes in the tree. */ + int numnodes_; + + /* The nodes. */ + jpc_tagtreenode_t *nodes_; + +} jpc_tagtree_t; + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Create a tag tree. */ +jpc_tagtree_t *jpc_tagtree_create(int numleafsh, int numleafsv); + +/* Destroy a tag tree. */ +void jpc_tagtree_destroy(jpc_tagtree_t *tree); + +/* Copy data from one tag tree to another. */ +void jpc_tagtree_copy(jpc_tagtree_t *dsttree, jpc_tagtree_t *srctree); + +/* Reset the tag tree state. */ +void jpc_tagtree_reset(jpc_tagtree_t *tree); + +/* Set the value associated with a particular leaf node of a tag tree. */ +void jpc_tagtree_setvalue(jpc_tagtree_t *tree, jpc_tagtreenode_t *leaf, + int value); + +/* Get a pointer to a particular leaf node. */ +jpc_tagtreenode_t *jpc_tagtree_getleaf(jpc_tagtree_t *tree, int n); + +/* Invoke the tag tree decoding procedure. */ +int jpc_tagtree_decode(jpc_tagtree_t *tree, jpc_tagtreenode_t *leaf, + int threshold, jpc_bitstream_t *in); + +/* Invoke the tag tree encoding procedure. */ +int jpc_tagtree_encode(jpc_tagtree_t *tree, jpc_tagtreenode_t *leaf, + int threshold, jpc_bitstream_t *out); + +/* Dump a tag tree (for debugging purposes). */ +void jpc_tagtree_dump(jpc_tagtree_t *tree, FILE *out); + +#endif diff --git a/src/libjasper/jpc/jpc_tsfb.c b/src/libjasper/jpc/jpc_tsfb.c new file mode 100644 index 0000000..6f332ff --- /dev/null +++ b/src/libjasper/jpc/jpc_tsfb.c @@ -0,0 +1,288 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2004 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tree-Structured Filter Bank (TSFB) Library + * + * $Id: jpc_tsfb.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include + +#include "jasper/jas_malloc.h" +#include "jasper/jas_seq.h" + +#include "jpc_tsfb.h" +#include "jpc_cod.h" +#include "jpc_cs.h" +#include "jpc_util.h" +#include "jpc_math.h" + +void jpc_tsfb_getbands2(jpc_tsfb_t *tsfb, int locxstart, int locystart, + int xstart, int ystart, int xend, int yend, jpc_tsfb_band_t **bands, + int numlvls); + +/******************************************************************************\ +* +\******************************************************************************/ + +jpc_tsfb_t *jpc_cod_gettsfb(int qmfbid, int numlvls) +{ + jpc_tsfb_t *tsfb; + + if (!(tsfb = malloc(sizeof(jpc_tsfb_t)))) + return 0; + + if (numlvls > 0) { + switch (qmfbid) { + case JPC_COX_INS: + tsfb->qmfb = &jpc_ns_qmfb2d; + break; + default: + case JPC_COX_RFT: + tsfb->qmfb = &jpc_ft_qmfb2d; + break; + } + } else { + tsfb->qmfb = 0; + } + tsfb->numlvls = numlvls; + return tsfb; +} + +void jpc_tsfb_destroy(jpc_tsfb_t *tsfb) +{ + free(tsfb); +} + +int jpc_tsfb_analyze(jpc_tsfb_t *tsfb, jas_seq2d_t *a) +{ + return (tsfb->numlvls > 0) ? jpc_tsfb_analyze2(tsfb, jas_seq2d_getref(a, + jas_seq2d_xstart(a), jas_seq2d_ystart(a)), jas_seq2d_xstart(a), + jas_seq2d_ystart(a), jas_seq2d_width(a), + jas_seq2d_height(a), jas_seq2d_rowstep(a), tsfb->numlvls - 1) : 0; +} + +int jpc_tsfb_analyze2(jpc_tsfb_t *tsfb, int *a, int xstart, int ystart, + int width, int height, int stride, int numlvls) +{ + if (width > 0 && height > 0) { + if ((*tsfb->qmfb->analyze)(a, xstart, ystart, width, height, stride)) + return -1; + if (numlvls > 0) { + if (jpc_tsfb_analyze2(tsfb, a, JPC_CEILDIVPOW2(xstart, + 1), JPC_CEILDIVPOW2(ystart, 1), JPC_CEILDIVPOW2( + xstart + width, 1) - JPC_CEILDIVPOW2(xstart, 1), + JPC_CEILDIVPOW2(ystart + height, 1) - + JPC_CEILDIVPOW2(ystart, 1), stride, numlvls - 1)) { + return -1; + } + } + } + return 0; +} + +int jpc_tsfb_synthesize(jpc_tsfb_t *tsfb, jas_seq2d_t *a) +{ + return (tsfb->numlvls > 0) ? jpc_tsfb_synthesize2(tsfb, + jas_seq2d_getref(a, jas_seq2d_xstart(a), jas_seq2d_ystart(a)), + jas_seq2d_xstart(a), jas_seq2d_ystart(a), jas_seq2d_width(a), + jas_seq2d_height(a), jas_seq2d_rowstep(a), tsfb->numlvls - 1) : 0; +} + +int jpc_tsfb_synthesize2(jpc_tsfb_t *tsfb, int *a, int xstart, int ystart, + int width, int height, int stride, int numlvls) +{ + if (numlvls > 0) { + if (jpc_tsfb_synthesize2(tsfb, a, JPC_CEILDIVPOW2(xstart, 1), + JPC_CEILDIVPOW2(ystart, 1), JPC_CEILDIVPOW2(xstart + width, + 1) - JPC_CEILDIVPOW2(xstart, 1), JPC_CEILDIVPOW2(ystart + + height, 1) - JPC_CEILDIVPOW2(ystart, 1), stride, numlvls - + 1)) { + return -1; + } + } + if (width > 0 && height > 0) { + if ((*tsfb->qmfb->synthesize)(a, xstart, ystart, width, height, stride)) { + return -1; + } + } + return 0; +} + +int jpc_tsfb_getbands(jpc_tsfb_t *tsfb, uint_fast32_t xstart, + uint_fast32_t ystart, uint_fast32_t xend, uint_fast32_t yend, + jpc_tsfb_band_t *bands) +{ + jpc_tsfb_band_t *band; + + band = bands; + if (tsfb->numlvls > 0) { + jpc_tsfb_getbands2(tsfb, xstart, ystart, xstart, ystart, xend, yend, + &band, tsfb->numlvls); + } else { + + band->xstart = xstart; + band->ystart = ystart; + band->xend = xend; + band->yend = yend; + band->locxstart = xstart; + band->locystart = ystart; + band->locxend = band->locxstart + band->xend - band->xstart; + band->locyend = band->locystart + band->yend - band->ystart; + band->orient = JPC_TSFB_LL; + band->synenergywt = JPC_FIX_ONE; + ++band; + } + return band - bands; +} + +void jpc_tsfb_getbands2(jpc_tsfb_t *tsfb, int locxstart, int locystart, + int xstart, int ystart, int xend, int yend, jpc_tsfb_band_t **bands, + int numlvls) +{ + int newxstart; + int newystart; + int newxend; + int newyend; + jpc_tsfb_band_t *band; + + newxstart = JPC_CEILDIVPOW2(xstart, 1); + newystart = JPC_CEILDIVPOW2(ystart, 1); + newxend = JPC_CEILDIVPOW2(xend, 1); + newyend = JPC_CEILDIVPOW2(yend, 1); + + if (numlvls > 0) { + + jpc_tsfb_getbands2(tsfb, locxstart, locystart, newxstart, newystart, + newxend, newyend, bands, numlvls - 1); + + band = *bands; + band->xstart = JPC_FLOORDIVPOW2(xstart, 1); + band->ystart = newystart; + band->xend = JPC_FLOORDIVPOW2(xend, 1); + band->yend = newyend; + band->locxstart = locxstart + newxend - newxstart; + band->locystart = locystart; + band->locxend = band->locxstart + band->xend - band->xstart; + band->locyend = band->locystart + band->yend - band->ystart; + band->orient = JPC_TSFB_HL; + band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[ + tsfb->numlvls - numlvls] * tsfb->qmfb->lpenergywts[ + tsfb->numlvls - numlvls]); + ++(*bands); + + band = *bands; + band->xstart = newxstart; + band->ystart = JPC_FLOORDIVPOW2(ystart, 1); + band->xend = newxend; + band->yend = JPC_FLOORDIVPOW2(yend, 1); + band->locxstart = locxstart; + band->locystart = locystart + newyend - newystart; + band->locxend = band->locxstart + band->xend - band->xstart; + band->locyend = band->locystart + band->yend - band->ystart; + band->orient = JPC_TSFB_LH; + band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[ + tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[ + tsfb->numlvls - numlvls]); + ++(*bands); + + band = *bands; + band->xstart = JPC_FLOORDIVPOW2(xstart, 1); + band->ystart = JPC_FLOORDIVPOW2(ystart, 1); + band->xend = JPC_FLOORDIVPOW2(xend, 1); + band->yend = JPC_FLOORDIVPOW2(yend, 1); + band->locxstart = locxstart + newxend - newxstart; + band->locystart = locystart + newyend - newystart; + band->locxend = band->locxstart + band->xend - band->xstart; + band->locyend = band->locystart + band->yend - band->ystart; + band->orient = JPC_TSFB_HH; + band->synenergywt = jpc_dbltofix(tsfb->qmfb->hpenergywts[ + tsfb->numlvls - numlvls] * tsfb->qmfb->hpenergywts[ + tsfb->numlvls - numlvls]); + ++(*bands); + + } else { + + band = *bands; + band->xstart = xstart; + band->ystart = ystart; + band->xend = xend; + band->yend = yend; + band->locxstart = locxstart; + band->locystart = locystart; + band->locxend = band->locxstart + band->xend - band->xstart; + band->locyend = band->locystart + band->yend - band->ystart; + band->orient = JPC_TSFB_LL; + band->synenergywt = jpc_dbltofix(tsfb->qmfb->lpenergywts[ + tsfb->numlvls - numlvls - 1] * tsfb->qmfb->lpenergywts[ + tsfb->numlvls - numlvls - 1]); + ++(*bands); + + } + +} diff --git a/src/libjasper/jpc/jpc_tsfb.h b/src/libjasper/jpc/jpc_tsfb.h new file mode 100644 index 0000000..3285e51 --- /dev/null +++ b/src/libjasper/jpc/jpc_tsfb.h @@ -0,0 +1,138 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2004 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * Tree-Structured Filter Bank (TSFB) Library + * + * $Id: jpc_tsfb.h,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +#ifndef JPC_TSFB_H +#define JPC_TSFB_H + +/******************************************************************************\ +* Includes. +\******************************************************************************/ + +#include "jasper/jas_seq.h" + +#include "jpc_fix.h" +#include "jpc_qmfb.h" + +/******************************************************************************\ +* Constants. +\******************************************************************************/ + +#define JPC_TSFB_MAXBANDS (JPC_TSFB_MAXDEPTH * 3 + 1) +#define JPC_TSFB_MAXDEPTH 32 +#define JPC_TSFB_RITIMODE JPC_QMFB1D_RITIMODE + +#define JPC_TSFB_LL 0 +#define JPC_TSFB_LH 1 +#define JPC_TSFB_HL 2 +#define JPC_TSFB_HH 3 + +/******************************************************************************\ +* Types. +\******************************************************************************/ + +typedef struct { + int xstart; + int ystart; + int xend; + int yend; + int orient; + int locxstart; + int locystart; + int locxend; + int locyend; + jpc_fix_t synenergywt; +} jpc_tsfb_band_t; + +typedef struct { + int numlvls; + jpc_qmfb2d_t *qmfb; +} jpc_tsfb_t; + +/******************************************************************************\ +* Functions. +\******************************************************************************/ + +/* Create a TSFB. */ +jpc_tsfb_t *jpc_cod_gettsfb(int qmfbid, int numlevels); + +/* Destroy a TSFB. */ +void jpc_tsfb_destroy(jpc_tsfb_t *tsfb); + +/* Perform analysis. */ +int jpc_tsfb_analyze(jpc_tsfb_t *tsfb, jas_seq2d_t *x); + +/* Perform synthesis. */ +int jpc_tsfb_synthesize(jpc_tsfb_t *tsfb, jas_seq2d_t *x); + +/* Get band information for a TSFB. */ +int jpc_tsfb_getbands(jpc_tsfb_t *tsfb, uint_fast32_t xstart, + uint_fast32_t ystart, uint_fast32_t xend, uint_fast32_t yend, + jpc_tsfb_band_t *bands); + +#endif diff --git a/src/libjasper/jpc/jpc_util.c b/src/libjasper/jpc/jpc_util.c new file mode 100644 index 0000000..7dfeb63 --- /dev/null +++ b/src/libjasper/jpc/jpc_util.c @@ -0,0 +1,194 @@ +/* + * Copyright (c) 1999-2000 Image Power, Inc. and the University of + * British Columbia. + * Copyright (c) 2001-2003 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +/* + * $Id: jpc_util.c,v 1.1 2008/10/17 06:15:00 scuri Exp $ + */ + +/******************************************************************************\ +* Includes +\******************************************************************************/ + +#include +#include +#include +#include +#include +#include + +#include "jasper/jas_math.h" +#include "jasper/jas_malloc.h" + +#include "jpc_fix.h" +#include "jpc_cs.h" +#include "jpc_flt.h" +#include "jpc_util.h" + +/******************************************************************************\ +* Miscellaneous Functions +\******************************************************************************/ + +int jpc_atoaf(char *s, int *numvalues, double **values) +{ + static char delim[] = ", \t\n"; + char buf[4096]; + int n; + double *vs; + char *cp; + + strncpy(buf, s, sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + n = 0; + if ((cp = strtok(buf, delim))) { + ++n; + while ((cp = strtok(0, delim))) { + if (cp != '\0') { + ++n; + } + } + } + + if (n) { + if (!(vs = jas_malloc(n * sizeof(double)))) { + return -1; + } + + strncpy(buf, s, sizeof(buf)); + buf[sizeof(buf) - 1] = '\0'; + n = 0; + if ((cp = strtok(buf, delim))) { + vs[n] = atof(cp); + ++n; + while ((cp = strtok(0, delim))) { + if (cp != '\0') { + vs[n] = atof(cp); + ++n; + } + } + } + } else { + vs = 0; + } + + *numvalues = n; + *values = vs; + + return 0; +} + +jas_seq_t *jpc_seq_upsample(jas_seq_t *x, int m) +{ + jas_seq_t *z; + int i; + + if (!(z = jas_seq_create(jas_seq_start(x) * m, (jas_seq_end(x) - 1) * m + 1))) + return 0; + for (i = jas_seq_start(z); i < jas_seq_end(z); i++) { + *jas_seq_getref(z, i) = (!JAS_MOD(i, m)) ? jas_seq_get(x, i / m) : + jpc_inttofix(0); + } + + return z; +} + +jpc_fix_t jpc_seq_norm(jas_seq_t *x) +{ + jpc_fix_t s; + int i; + + s = jpc_inttofix(0); + for (i = jas_seq_start(x); i < jas_seq_end(x); i++) { + s = jpc_fix_add(s, jpc_fix_mul(jas_seq_get(x, i), jas_seq_get(x, i))); + } + + return jpc_dbltofix(sqrt(jpc_fixtodbl(s))); +} + +jas_seq_t *jpc_seq_conv(jas_seq_t *x, jas_seq_t *y) +{ + int i; + int j; + int k; + jas_seq_t *z; + jpc_fix_t s; + jpc_fix_t v; + + z = jas_seq_create(jas_seq_start(x) + jas_seq_start(y), + jas_seq_end(x) + jas_seq_end(y) - 1); + assert(z); + for (i = jas_seq_start(z); i < jas_seq_end(z); i++) { + s = jpc_inttofix(0); + for (j = jas_seq_start(y); j < jas_seq_end(y); j++) { + k = i - j; + if (k < jas_seq_start(x) || k >= jas_seq_end(x)) { + v = JPC_FIX_ZERO; + } else { + v = jas_seq_get(x, k); + } + s = jpc_fix_add(s, jpc_fix_mul(jas_seq_get(y, j), v)); + } + *jas_seq_getref(z, i) = s; + } + + return z; +} diff --git a/src/libjasper/jpc/jpc_util.h b/src/libjasper/jpc/jpc_util.h new file mode 100644 index 0000000..526312a --- /dev/null +++ b/src/libjasper/jpc/jpc_util.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2001-2002 Michael David Adams. + * All rights reserved. + */ + +/* __START_OF_JASPER_LICENSE__ + * + * JasPer License Version 2.0 + * + * Copyright (c) 2001-2006 Michael David Adams + * Copyright (c) 1999-2000 Image Power, Inc. + * Copyright (c) 1999-2000 The University of British Columbia + * + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person (the + * "User") obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, + * publish, distribute, and/or sell copies of the Software, and to permit + * persons to whom the Software is furnished to do so, subject to the + * following conditions: + * + * 1. The above copyright notices and this permission notice (which + * includes the disclaimer below) shall be included in all copies or + * substantial portions of the Software. + * + * 2. The name of a copyright holder shall not be used to endorse or + * promote products derived from the Software without specific prior + * written permission. + * + * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS + * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER + * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE + * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE + * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY. + * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS + * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL + * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS + * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE + * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE + * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL + * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES, + * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL + * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH + * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH, + * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH + * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY + * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES. + * + * __END_OF_JASPER_LICENSE__ + */ + +#ifndef JPC_UTIL_H +#define JPC_UTIL_H + +/* Parse a comma separated list of real numbers into an array of doubles. */ +int jpc_atoaf(char *s, int *numvalues, double **values); + +/* Upsample a sequence. */ +jas_seq_t *jpc_seq_upsample(jas_seq_t *seq, int n); + +/* Convolve two sequences. */ +jas_seq_t *jpc_seq_conv(jas_seq_t *seq0, jas_seq_t *seq1); + +/* Compute the norm of a sequence. */ +jpc_fix_t jpc_seq_norm(jas_seq_t *x); + +#endif -- cgit v1.2.3