]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/global.h
9277ea5469c08947aa42bfee2625116f755c0ebd
[gdcm.git] / src / gdcmjpegls / Decoder / global.h
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.
9
10    =========================================================
11    THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls:
12    =========================================================
13
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    -------------------------------------------------------------------------------
40 */
41
42 /* global.h --- prototypes for functions and global variables 
43  *
44  * Initial code by Alex Jakulin,  Aug. 1995
45  *
46  * Modified and optimized: Gadiel Seroussi, October 1995
47  *
48  * Modified and added Restart marker and input tables by:
49  * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
50  */
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <assert.h>
55
56 #ifndef GLOBAL_H
57 #define GLOBAL_H
58
59 #ifndef CLOCKS_PER_SEC
60 #define CLOCKS_PER_SEC 10e6
61 #endif
62
63 /*#define NDEBUG*/
64 #define POW2
65 #define FIXAPLHA
66 #define FIXRESET
67
68
69 /* TRUE and FALSE values */
70 #define TRUE 1
71 #define FALSE 0
72
73
74 /* Version number */
75 #define JPEGLSVERSION   "V.2.1"
76
77 /* Maximal number of components in the implementation*/
78 #define MAX_COMPONENTS  6
79 #define MAX_SCANS  MAX_COMPONENTS
80   
81
82 /* For 1st component of plane interl. mode */
83 #define FIRST    1
84
85
86 /* Different colour modes */
87 #define PLANE_INT  0
88 #define LINE_INT  1
89 #define PIXEL_INT  2
90
91 #define DEFAULT_COLOR_MODE LINE_INT
92
93 extern char  *plane_int_string,
94       *line_int_string,
95       *pixel_int_string;
96
97
98 #ifndef BIG_ENDIAN
99 #define BIG_ENDIAN  1
100 #endif
101
102 typedef struct  jpeg_ls {
103
104   int  columns,          /* The number of columns */
105     rows,            /* Number of rows */
106     alp,            /* alphabet size (Max+1) , 2 bytes*/
107     comp,            /* number of components, 1 byte  */
108     NEAR,            /* near-lossless error, 1 byte  */
109     color_mode,          /* indicates the color mode , 1 byte */
110     need_lse,          /* Indicates non-default parameters */
111     need_table,          /* Indicates use of mapping table */
112     need_restart,        /* Indicates use of restart markers */
113     restart_interval,      /* The number of MCU's between restart markers */
114     shift,            /* for sparse images, 1 byte */
115     T1,T2,T3,          /* Thresholds, 2 bytes each */
116     RES,            /* reset value for counters, 2 bytes */
117     samplingx[MAX_COMPONENTS],  /* col. sampling rates 1 byte each*/
118     samplingy[MAX_COMPONENTS],  /* row sampling rates */
119     comp_ids[MAX_COMPONENTS],  /* component id's */
120     acc_size,          /* 1 byte */
121     adds[MAX_COMPONENTS];    /* size given by acc_size */
122   unsigned int  TID,      /* Table ID, 1 byte */
123             MAXTAB,      /* Maximum table index value */
124             Wt,      /* Width of each table entry, 1 byte */
125                   *TABLE[MAX_COMPONENTS];  /* The table(s) for each component */
126
127 } jpeg_ls_header;
128
129 extern int  components;
130 extern int  sampling[MAX_COMPONENTS];
131
132 #define NAME_LENGTH  40
133
134 /* Output file names */
135 #define OUTFILE "outfile"
136 #define COMPSUFFIX ".out"
137
138
139 /* Define max and min macros */
140 #ifndef max
141 #define max(a,b)  (((a)>=(b))?(a):(b))
142 #define min(a,b)  (((a)<=(b))?(a):(b))
143 #endif
144
145
146 /****** Constants */
147
148 /* margins for scan lines */
149 #define  LEFTMARGIN  2
150 #define RIGHTMARGIN  1
151
152
153 extern char *disclaimer;
154
155
156 /* alphabet size */
157 #define MAXA8 (256)
158 #define MAXA16 (65536)
159 #define LUTMAX8 (256)
160 #define LUTMAX16 (4501)
161
162
163 #ifdef FIXALPHA
164 #  ifndef alpha
165 #    define  alpha  256
166 #  endif
167 #  define   highmask (-(alpha))
168 #  ifndef POW2
169 #  define POW2
170 #  endif
171 #  if (alpha!=2) && (alpha!=4) && (alpha!=8) && (alpha!=16) && (alpha!=32) &&\
172      (alpha!=64) && ( alpha!=128) && (alpha!=256) && (alpha!=512) &&\
173      (alpha!=1024) && ( alpha!=2048) && (alpha!=4096) && (alpha!=8192) &&\
174      (alpha!=16384) && ( alpha!=32768) && (alpha!=65536)
175 #     error "Fixed alpha must be a power of 2"
176 #  endif
177 #  define    ceil_half_alpha (alpha/2)
178 #else
179 extern int      alpha;     /* alphabet size */
180 extern int      ceil_half_alpha; /* ceil(alpha/2) */
181 extern int      highmask;  /* for powers of 2, a mask for high bits */
182 #endif
183
184
185
186 extern int  bpp,    /* bits per sample */
187       qbpp,   /* bits per sample for quantized prediction errors */
188       limit,  /* limit for unary part of Golomb code */
189       limit_reduce;  /* reduction on above for EOR states */
190
191
192 #define DEF_NEAR  0
193
194 /* for LOSSY mode */
195 extern  int  quant, 
196       beta, 
197       qbeta,
198       ceil_half_qbeta,
199       negNEAR,
200       alpha1eps;
201
202 /* loss tolerance */
203 extern int NEAR;
204
205
206 /* Quantization threshold basic defaults */
207 /* These are the defaults for LOSSLESS, 8 bpp. Defaults for other
208    cases are derived from these basic values */
209 #define  BASIC_T1  3
210 #define  BASIC_T2  7
211 #define  BASIC_T3  21
212 #define  BASIC_Ta  5
213
214 #define CREGIONS (9)    /* quantization regions for d-a, a-c, c-b */
215
216 /* run-length treshold */
217 #ifndef MAXRUN
218 #  define MAXRUN (64)
219 #endif
220
221 #define EOLINE   1
222 #define NOEOLINE 0
223
224 /* number of different contexts */
225 #define CONTEXTS1 (CREGIONS*CREGIONS*CREGIONS)
226
227 #define CONTEXTS   ((CONTEXTS1+1)/2) /* all regions, with symmetric merging */
228
229
230 /* Mandatory for JPEG-LS: */
231 #define CLAMP
232 #define CLAMPB
233 #define CLAMPC
234
235
236 #define MAX_C 127
237 #define MIN_C -128
238
239
240 #define MAXCODE (N_R_L_ERROR)
241
242
243 /* Number of end-of-run contexts */
244 #define EOR_CONTEXTS 2
245
246
247 /* Total number of contexts */
248 #define TOT_CONTEXTS (CONTEXTS +  EOR_CONTEXTS)
249
250
251 /* index of first end-of-run context */
252 #define EOR_0  (CONTEXTS)
253
254
255 /* index of run state */
256 #define RUNSTATE 0
257
258
259
260 /*** offsets */
261
262 /* The longest code the bit IO can facilitate */
263 #define MAXCODELEN 24
264
265 /* The stat initialization values */
266 #define INITNSTAT 1
267 #define MIN_INITABSTAT 2    /* min init value for abstat[] */
268 #define INITABSLACK 6       /* init value for abstat is roughly 
269                2^(bpp-INITABSLACK) but not less than above */
270 #define INITBIASTAT 0
271
272 /* Limit for unary code */
273 #define LIMIT 23
274
275 /* reset values */
276 #define DEFAULT_RESET 64
277 #define MINRESET 3
278
279 #ifdef FIXRESET
280 #   ifndef RESET
281 #    define RESET     DEFAULT_RESET
282 #   endif
283 #else
284 extern int  RESET;
285 #endif
286
287 #define  reset  RESET          /* reset threshold */
288
289 #define RESRUN    256
290
291
292 /****** Type prototypes */
293
294 /* Portability types */
295 typedef unsigned char byte;
296 typedef unsigned short word;
297 typedef unsigned long dword;
298
299 typedef unsigned short pixel;
300
301
302
303 /****** Global variables prototypes */
304
305 extern FILE *in, *out, *msgfile;
306 extern FILE  *c_in[MAX_COMPONENTS];
307 extern FILE  *c_out[MAX_COMPONENTS];
308 extern int  inhandle;
309 extern int  T1, T2, T3, Ta;
310 extern int  verbose,
311       nopause,
312       nolegal;
313
314 extern int bpp16;
315 extern int lossy;
316
317
318 /* for look-up tables */
319 extern int alpha;
320 extern int vLUT[3][2 * LUTMAX16];
321 extern int lutmax;
322 extern int classmap[CONTEXTS1];
323 extern int *qdiv0, *qdiv,        /* quantization table (division via look-up) */
324      *qmul0, *qmul;        /* dequantization table */
325
326 /* statistics tables */
327 extern int  N[TOT_CONTEXTS], 
328     A[TOT_CONTEXTS], 
329     B[TOT_CONTEXTS],
330     C[TOT_CONTEXTS];
331
332
333 /****** Function prototypes */
334
335 /* global.c */
336 void error(char *msg);
337 void *safealloc(size_t size);
338 void *safecalloc(size_t numels, size_t size);
339 double get_utime();
340 int set_thresholds(int alfa, int NEAR, int *T1p, int *T2p, int *T3p);
341 void check_compatibility(jpeg_ls_header *head_frame, jpeg_ls_header *head_scan, int n_s);
342
343 /* scanline.c */
344 void prepareLUTs();
345 void init_stats(int);
346 void doscanline(pixel *psl, pixel *sl, int no, int color);
347 int undoscanline(pixel *psl, pixel *sl, int no, int color);
348 void doscanline_pixel(pixel *psl, pixel *sl, int no);
349 int undoscanline_pixel(pixel *psl, pixel *sl, int no);
350
351 /* bitio.c */
352 void bitiinit();
353 void bitflush();
354 void createzeroLUT();
355 void buffinit(FILE *);
356 void bufiinit(FILE *fil);
357 void bitiflush();
358
359 /*  melcode.c */
360 void init_process_run(int);
361 void close_process_run();
362 int  process_run_dec(int,int);
363
364 /* initialize.c */
365 void prepareLUTs();
366 void prepare_qtables(int, int);
367 void init_stats(int);
368
369 /* lossless_d.c */
370 int lossless_undoscanline(  pixel *psl, pixel *sl, int no, int color);
371 int lossless_undoscanline_pixel(pixel *psl, pixel *sl, int no);
372
373 /* lossy_d.c */
374 int lossy_undoscanline(  pixel *psl, pixel *sl, int no, int color);
375 int lossy_undoscanline_pixel(  pixel *psl, pixel *sl, int no);
376
377
378 #ifdef BIG_ENDIAN
379 #    define ENDIAN8(x)   (x)
380 #    define ENDIAN16(x)   (x)
381 #else
382 #    define ENDIAN8(x) (x&0x000000ff)
383 #    define ENDIAN16(x) ( ((x>>8)|(x<<8)) & 0x0000ffff)
384 #endif
385
386 /* ENDIAN function to fix endian of PCs (for 8 bit pixels) 
387 #define ENDIAN8(x) (x&0x000000ff)*/
388
389
390 /* ENDIAN function to fix endian of PCs (for 16 bit pixels) 
391 #define ENDIAN16(x) ( ((x>>8)|(x<<8)) & 0x0000ffff )*/
392
393
394
395
396 /* clipping macro */
397 #ifdef POW2
398 #  define clip(x,alpha) \
399       if ( x & highmask ) {\
400         if(x < 0) \
401       x = 0;\
402         else \
403       x = alpha - 1;\
404       }
405 #else
406 #  define clip(x,alpha) \
407     if(x < 0)  \
408       x = 0; \
409     else if (x >= alpha) \
410       x = alpha - 1;
411 #endif  /* POW2 */ 
412
413
414
415 /* macro to predict Px */
416 #define predict(Rb, Ra, Rc)  \
417 {  \
418   register pixel minx;  \
419   register pixel maxx;  \
420   \
421   if (Rb > Ra) {  \
422     minx = Ra;  \
423     maxx = Rb;  \
424   } else {  \
425     maxx = Ra;  \
426     minx = Rb;  \
427   }  \
428   if (Rc >= maxx)  \
429     Px = minx;  \
430   else if (Rc <= minx)  \
431     Px = maxx;  \
432   else  \
433     Px = Ra + Rb - Rc;  \
434 }
435
436
437 #endif