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