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 /* global.h --- prototypes for functions and global variables
44 * Initial code by Alex Jakulin, Aug. 1995
46 * Modified and optimized: Gadiel Seroussi, October 1995
48 * Modified and added Restart marker and input tables by:
49 * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
55 #include "jpegls_config.h"
60 #ifndef CLOCKS_PER_SEC
61 #define CLOCKS_PER_SEC 10e6
70 /* TRUE and FALSE values */
76 #define JPEGLSVERSION "V.2.1"
78 /* Maximal number of components in the implementation*/
79 #define MAX_COMPONENTS 6
80 #define MAX_SCANS MAX_COMPONENTS
83 /* For 1st component of plane interl. mode */
87 /* Different colour modes */
92 #define DEFAULT_COLOR_MODE LINE_INT
99 typedef struct jpeg_ls {
101 int columns, /* The number of columns */
102 rows, /* Number of rows */
103 alp, /* alphabet size (Max+1) , 2 bytes*/
104 comp, /* number of components, 1 byte */
105 NEAR, /* near-lossless error, 1 byte */
106 color_mode, /* indicates the color mode , 1 byte */
107 need_lse, /* Indicates non-default parameters */
108 need_table, /* Indicates use of mapping table */
109 need_restart, /* Indicates use of restart markers */
110 restart_interval, /* The number of MCU's between restart markers */
111 shift, /* for sparse images, 1 byte */
112 T1,T2,T3, /* Thresholds, 2 bytes each */
113 RES, /* reset value for counters, 2 bytes */
114 samplingx[MAX_COMPONENTS], /* col. sampling rates 1 byte each*/
115 samplingy[MAX_COMPONENTS], /* row sampling rates */
116 comp_ids[MAX_COMPONENTS], /* component id's */
117 acc_size, /* 1 byte */
118 adds[MAX_COMPONENTS]; /* size given by acc_size */
119 unsigned int TID, /* Table ID, 1 byte */
120 MAXTAB, /* Maximum table index value */
121 Wt, /* Width of each table entry, 1 byte */
122 *TABLE[MAX_COMPONENTS]; /* The table(s) for each component */
126 extern int components;
127 extern int sampling[MAX_COMPONENTS];
129 #define NAME_LENGTH 40
131 /* Output file names */
132 #define OUTFILE "outfile"
133 #define COMPSUFFIX ".out"
136 /* Define max and min macros */
138 #define max(a,b) (((a)>=(b))?(a):(b))
139 #define min(a,b) (((a)<=(b))?(a):(b))
145 /* margins for scan lines */
147 #define RIGHTMARGIN 1
153 #define MAXA16 (65536)
154 #define LUTMAX8 (256)
155 #define LUTMAX16 (4501)
162 # define highmask (-(alpha))
166 # if (alpha!=2) && (alpha!=4) && (alpha!=8) && (alpha!=16) && (alpha!=32) &&\
167 (alpha!=64) && ( alpha!=128) && (alpha!=256) && (alpha!=512) &&\
168 (alpha!=1024) && ( alpha!=2048) && (alpha!=4096) && (alpha!=8192) &&\
169 (alpha!=16384) && ( alpha!=32768) && (alpha!=65536)
170 # error "Fixed alpha must be a power of 2"
172 # define ceil_half_alpha (alpha/2)
174 extern int alpha; /* alphabet size */
175 extern int ceil_half_alpha; /* ceil(alpha/2) */
176 extern int highmask; /* for powers of 2, a mask for high bits */
181 extern int bpp, /* bits per sample */
182 qbpp, /* bits per sample for quantized prediction errors */
183 limit, /* limit for unary part of Golomb code */
184 limit_reduce; /* reduction on above for EOR states */
201 /* Quantization threshold basic defaults */
202 /* These are the defaults for LOSSLESS, 8 bpp. Defaults for other
203 cases are derived from these basic values */
209 #define CREGIONS (9) /* quantization regions for d-a, a-c, c-b */
211 /* run-length treshold */
219 /* number of different contexts */
220 #define CONTEXTS1 (CREGIONS*CREGIONS*CREGIONS)
222 #define CONTEXTS ((CONTEXTS1+1)/2) /* all regions, with symmetric merging */
225 /* Mandatory for JPEG-LS: */
235 #define MAXCODE (N_R_L_ERROR)
238 /* Number of end-of-run contexts */
239 #define EOR_CONTEXTS 2
242 /* Total number of contexts */
243 #define TOT_CONTEXTS (CONTEXTS + EOR_CONTEXTS)
246 /* index of first end-of-run context */
247 #define EOR_0 (CONTEXTS)
250 /* index of run state */
257 /* The longest code the bit IO can facilitate */
258 #define MAXCODELEN 24
260 /* The stat initialization values */
262 #define MIN_INITABSTAT 2 /* min init value for abstat[] */
263 #define INITABSLACK 6 /* init value for abstat is roughly
264 2^(bpp-INITABSLACK) but not less than above */
265 #define INITBIASTAT 0
267 /* Limit for unary code */
271 #define DEFAULT_RESET 64
276 # define RESET DEFAULT_RESET
282 #define reset RESET /* reset threshold */
287 /****** Type prototypes */
289 /* Portability types */
290 typedef unsigned char byte;
291 typedef unsigned short word;
292 typedef unsigned long dword;
294 typedef unsigned short pixel;
298 /****** Global variables prototypes */
300 extern FILE *in, *out, *msgfile;
301 extern FILE *c_in[MAX_COMPONENTS];
302 extern FILE *c_out[MAX_COMPONENTS];
304 extern int T1, T2, T3, Ta;
313 /* for look-up tables */
315 extern int vLUT[3][2 * LUTMAX16];
317 extern int classmap[CONTEXTS1];
318 extern int *qdiv0, *qdiv, /* quantization table (division via look-up) */
319 *qmul0, *qmul; /* dequantization table */
321 /* statistics tables */
322 extern int N[TOT_CONTEXTS],
328 /****** Function prototypes */
331 void error(const char *msg);
332 void *safealloc(size_t size);
333 void *safecalloc(size_t numels, size_t size);
335 int set_thresholds(int alfa, int NEAR, int *T1p, int *T2p, int *T3p);
336 void check_compatibility(jpeg_ls_header *head_frame, jpeg_ls_header *head_scan, int n_s);
340 void init_stats(int);
341 void doscanline(pixel *psl, pixel *sl, int no, int color);
342 int undoscanline(pixel *psl, pixel *sl, int no, int color);
343 void doscanline_pixel(pixel *psl, pixel *sl, int no);
344 int undoscanline_pixel(pixel *psl, pixel *sl, int no);
349 void createzeroLUT();
350 void buffinit(FILE *);
351 void bufiinit(FILE *fil);
355 void init_process_run(int);
356 void close_process_run();
357 int process_run_dec(int,int);
361 void prepare_qtables(int, int);
362 void init_stats(int);
365 int lossless_undoscanline( pixel *psl, pixel *sl, int no, int color);
366 int lossless_undoscanline_pixel(pixel *psl, pixel *sl, int no);
369 int lossy_undoscanline( pixel *psl, pixel *sl, int no, int color);
370 int lossy_undoscanline_pixel( pixel *psl, pixel *sl, int no);
374 # define ENDIAN8(x) (x)
375 # define ENDIAN16(x) (x)
377 # define ENDIAN8(x) (x&0x000000ff)
378 # define ENDIAN16(x) ( ((x>>8)|(x<<8)) & 0x0000ffff)
381 /* ENDIAN function to fix endian of PCs (for 8 bit pixels)
382 #define ENDIAN8(x) (x&0x000000ff)*/
385 /* ENDIAN function to fix endian of PCs (for 16 bit pixels)
386 #define ENDIAN16(x) ( ((x>>8)|(x<<8)) & 0x0000ffff )*/
393 # define clip(x,alpha) \
394 if ( x & highmask ) {\
401 # define clip(x,alpha) \
404 else if (x >= alpha) \
410 /* macro to predict Px */
411 #define predict(Rb, Ra, Rc) \
413 register pixel minx; \
414 register pixel maxx; \
425 else if (Rc <= minx) \