1 /* SPMG/JPEG-LS IMPLEMENTATION V.2.1
2 =====================================
3 These programs are Copyright (c) University of British Columbia. All rights reserved.
4 They may be freely redistributed in their entirety provided that this copyright
5 notice is not removed. THEY MAY NOT BE SOLD FOR PROFIT OR INCORPORATED IN
6 COMMERCIAL PROGRAMS WITHOUT THE WRITTEN PERMISSION OF THE COPYRIGHT HOLDER.
7 Each program is provided as is, without any express or implied warranty,
8 without even the warranty of fitness for a particular purpose.
10 =========================================================
11 THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls:
12 =========================================================
14 LOCO-I/JPEG-LS IMPLEMENTATION V.0.90
15 -------------------------------------------------------------------------------
16 (c) COPYRIGHT HEWLETT-PACKARD COMPANY, 1995-1999.
17 HEWLETT-PACKARD COMPANY ("HP") DOES NOT WARRANT THE ACCURACY OR
18 COMPLETENESS OF THE INFORMATION GIVEN HERE. ANY USE MADE OF, OR
19 RELIANCE ON, SUCH INFORMATION IS ENTIRELY AT USER'S OWN RISK.
20 BY DOWNLOADING THE LOCO-I/JPEG-LS COMPRESSORS/DECOMPRESSORS
21 ("THE SOFTWARE") YOU AGREE TO BE BOUND BY THE TERMS AND CONDITIONS
22 OF THIS LICENSING AGREEMENT.
23 YOU MAY DOWNLOAD AND USE THE SOFTWARE FOR NON-COMMERCIAL PURPOSES
24 FREE OF CHARGE OR FURTHER OBLIGATION. YOU MAY NOT, DIRECTLY OR
25 INDIRECTLY, DISTRIBUTE THE SOFTWARE FOR A FEE, INCORPORATE THIS
26 SOFTWARE INTO ANY PRODUCT OFFERED FOR SALE, OR USE THE SOFTWARE
27 TO PROVIDE A SERVICE FOR WHICH A FEE IS CHARGED.
28 YOU MAY MAKE COPIES OF THE SOFTWARE AND DISTRIBUTE SUCH COPIES TO
29 OTHER PERSONS PROVIDED THAT SUCH COPIES ARE ACCOMPANIED BY
30 HEWLETT-PACKARD'S COPYRIGHT NOTICE AND THIS AGREEMENT AND THAT
31 SUCH OTHER PERSONS AGREE TO BE BOUND BY THE TERMS OF THIS AGREEMENT.
32 THE SOFTWARE IS NOT OF PRODUCT QUALITY AND MAY HAVE ERRORS OR DEFECTS.
33 THE JPEG-LS STANDARD IS STILL UNDER DEVELOPMENT. THE SOFTWARE IS NOT A
34 FINAL OR FULL IMPLEMENTATION OF THE STANDARD. HP GIVES NO EXPRESS OR
35 IMPLIED WARRANTY OF ANY KIND AND ANY IMPLIED WARRANTIES OF
36 MERCHANTABILITY AND FITNESS FOR PURPOSE ARE DISCLAIMED.
37 HP SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
38 OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE.
39 -------------------------------------------------------------------------------
42 /* initialize.c --- functions to initialize look up tables
43 * and statistics tables
45 * Initial code by Alex Jakulin, Aug. 1995
47 * Modified and optimized: Gadiel Seroussi, October 1995
49 * Modified and added Restart marker and input tables by:
50 * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
60 int vLUT[3][2 * LUTMAX16];
62 /*byte getk[65][3000];*/
65 int classmap[CONTEXTS1];
67 int *qdiv0, *qdiv, /* quantization table (division via look-up) */
68 *qmul0, *qmul; /* dequantization table */
81 /* Setup Look Up Tables for quantized gradient merging */
87 lmax = min(alpha,lutmax);
89 /* implementation limitation: */
91 fprintf(stderr,"Sorry, current implementation does not support threshold T3 > %d, got %d\n",lmax-1,T3);
96 /* Build classification tables (lossless or lossy) */
100 for (i = -lmax + 1; i < lmax; i++) {
102 if ( i <= -T3 ) /* ...... -T3 */
104 else if ( i <= -T2 ) /* -(T3-1) ... -T2 */
106 else if ( i <= -T1 ) /* -(T2-1) ... -T1 */
109 else if ( i <= -1 ) /* -(T1-1) ... -1 */
111 else if ( i == 0 ) /* just 0 */
114 else if ( i < T1 ) /* 1 ... T1-1 */
116 else if ( i < T2 ) /* T1 ... T2-1 */
118 else if ( i < T3 ) /* T2 ... T3-1 */
123 vLUT[0][i + lutmax] = CREGIONS * CREGIONS * idx;
124 vLUT[1][i + lutmax] = CREGIONS * idx;
125 vLUT[2][i + lutmax] = idx;
130 for (i = -lmax + 1; i < lmax; i++) {
132 if ( NEAR >= (alpha-1) )
133 idx = 0; /* degenerate case, regardless of thresholds */
136 if ( i <= -T3 ) /* ...... -T3 */
138 else if ( i <= -T2 ) /* -(T3-1) ... -T2 */
140 else if ( i <= -T1 ) /* -(T2-1) ... -T1 */
143 else if ( i <= -NEAR-1 ) /* -(T1-1) ... -NEAR-1 */
145 else if ( i <= NEAR ) /* within NEAR of 0 */
148 else if ( i < T1 ) /* 1 ... T1-1 */
150 else if ( i < T2 ) /* T1 ... T2-1 */
152 else if ( i < T3 ) /* T2 ... T3-1 */
157 vLUT[0][i + lutmax] = CREGIONS * CREGIONS * idx;
158 vLUT[1][i + lutmax] = CREGIONS * idx;
159 vLUT[2][i + lutmax] = idx;
165 /* prepare context mapping table (symmetric context merging) */
167 for ( i=1, j=0; i<CONTEXTS1; i++) {
168 int q1, q2, q3, n1=0, n2=0, n3=0, ineg, sgn;
173 q1 = i/(CREGIONS*CREGIONS); /* first digit */
174 q2 = (i/CREGIONS)%CREGIONS; /* second digit */
175 q3 = i%CREGIONS; /* third digit */
177 if((q1%2)||(q1==0 && (q2%2))||(q1==0 && q2==0 && (q3%2)))
182 /* compute negative context */
183 if(q1) n1 = q1 + ((q1%2) ? 1 : -1);
184 if(q2) n2 = q2 + ((q2%2) ? 1 : -1);
185 if(q3) n3 = q3 + ((q3%2) ? 1 : -1);
187 ineg = (n1*CREGIONS+n2)*CREGIONS+n3;
188 j++ ; /* next class number */
190 classmap[ineg] = -sgn*j;
194 /* prepare table to find k */
197 /* for (i=1; i< 65; i++) /* value of N[] */
198 /* for (j=1; j<2500; j++) /* value of A[] */
201 for(k=0; nst < j; nst<<=1, k++);
205 /* prepare table to find clip of Px with 8-bit */
206 /* for (i=0; i<127; i++)
208 for (i=127; i<382; i++)
210 for (i=382; i<510; i++)
219 /* prepare quantization tables for near-lossless quantization */
220 void prepare_qtables(int absize, int NEAR)
228 if ( (qdiv0 = (int *)calloc(2*absize-1,sizeof(int)))==NULL ) {
229 perror("qdiv table");
232 qdiv = qdiv0+absize-1;
234 if ( (qmul0 = (int *)calloc(2*beta-1,sizeof(int)))==NULL ) {
235 perror("qmul table");
240 for ( dif = -(absize-1); dif<absize; dif++ ) {
242 qdiff = - ( (NEAR-dif)/quant );
244 qdiff = ( NEAR + dif )/quant;
247 for ( qdiff = -(beta-1); qdiff<beta; qdiff++ ) {
255 /* Initialize A[], B[], C[], and N[] arrays */
256 void init_stats(int absize)
258 int i, initabstat, slack;
260 slack = 1<<INITABSLACK;
261 initabstat = (absize + slack/2)/slack;
262 if ( initabstat < MIN_INITABSTAT ) initabstat = MIN_INITABSTAT;
264 /* do the statistics initialization */
265 for (i = 0; i < TOT_CONTEXTS; ++i) {