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