]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/global.h
BUG: map<>::mapped_type is not part of the STL. Should fix issue on VS* compiler
[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
95 #ifndef BIG_ENDIAN
96 #define BIG_ENDIAN  1
97 #endif
98
99 typedef struct  jpeg_ls {
100
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 */
123
124 } jpeg_ls_header;
125
126 extern int  components;
127 extern int  sampling[MAX_COMPONENTS];
128
129 #define NAME_LENGTH  40
130
131 /* Output file names */
132 #define OUTFILE "outfile"
133 #define COMPSUFFIX ".out"
134
135
136 /* Define max and min macros */
137 #ifndef max
138 #define max(a,b)  (((a)>=(b))?(a):(b))
139 #define min(a,b)  (((a)<=(b))?(a):(b))
140 #endif
141
142
143 /****** Constants */
144
145 /* margins for scan lines */
146 #define  LEFTMARGIN  2
147 #define RIGHTMARGIN  1
148
149
150
151 /* alphabet size */
152 #define MAXA8 (256)
153 #define MAXA16 (65536)
154 #define LUTMAX8 (256)
155 #define LUTMAX16 (4501)
156
157
158 #ifdef FIXALPHA
159 #  ifndef alpha
160 #    define  alpha  256
161 #  endif
162 #  define   highmask (-(alpha))
163 #  ifndef POW2
164 #  define POW2
165 #  endif
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"
171 #  endif
172 #  define    ceil_half_alpha (alpha/2)
173 #else
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 */
177 #endif
178
179
180
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 */
185
186
187 #define DEF_NEAR  0
188
189 /* for LOSSY mode */
190 extern  int  quant, 
191       beta, 
192       qbeta,
193       ceil_half_qbeta,
194       negNEAR,
195       alpha1eps;
196
197 /* loss tolerance */
198 extern int NEAR;
199
200
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 */
204 #define  BASIC_T1  3
205 #define  BASIC_T2  7
206 #define  BASIC_T3  21
207 #define  BASIC_Ta  5
208
209 #define CREGIONS (9)    /* quantization regions for d-a, a-c, c-b */
210
211 /* run-length treshold */
212 #ifndef MAXRUN
213 #  define MAXRUN (64)
214 #endif
215
216 #define EOLINE   1
217 #define NOEOLINE 0
218
219 /* number of different contexts */
220 #define CONTEXTS1 (CREGIONS*CREGIONS*CREGIONS)
221
222 #define CONTEXTS   ((CONTEXTS1+1)/2) /* all regions, with symmetric merging */
223
224
225 /* Mandatory for JPEG-LS: */
226 #define CLAMP
227 #define CLAMPB
228 #define CLAMPC
229
230
231 #define MAX_C 127
232 #define MIN_C -128
233
234
235 #define MAXCODE (N_R_L_ERROR)
236
237
238 /* Number of end-of-run contexts */
239 #define EOR_CONTEXTS 2
240
241
242 /* Total number of contexts */
243 #define TOT_CONTEXTS (CONTEXTS +  EOR_CONTEXTS)
244
245
246 /* index of first end-of-run context */
247 #define EOR_0  (CONTEXTS)
248
249
250 /* index of run state */
251 #define RUNSTATE 0
252
253
254
255 /*** offsets */
256
257 /* The longest code the bit IO can facilitate */
258 #define MAXCODELEN 24
259
260 /* The stat initialization values */
261 #define INITNSTAT 1
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
266
267 /* Limit for unary code */
268 #define LIMIT 23
269
270 /* reset values */
271 #define DEFAULT_RESET 64
272 #define MINRESET 3
273
274 #ifdef FIXRESET
275 #   ifndef RESET
276 #    define RESET     DEFAULT_RESET
277 #   endif
278 #else
279 extern int  RESET;
280 #endif
281
282 #define  reset  RESET          /* reset threshold */
283
284 #define RESRUN    256
285
286
287 /****** Type prototypes */
288
289 /* Portability types */
290 typedef unsigned char byte;
291 typedef unsigned short word;
292 typedef unsigned long dword;
293
294 typedef unsigned short pixel;
295
296
297
298 /****** Global variables prototypes */
299
300 extern FILE *in, *out, *msgfile;
301 extern FILE  *c_in[MAX_COMPONENTS];
302 extern FILE  *c_out[MAX_COMPONENTS];
303 extern int  inhandle;
304 extern int  T1, T2, T3, Ta;
305 extern int  verbose,
306       nopause,
307       nolegal;
308
309 extern int bpp16;
310 extern int lossy;
311
312
313 /* for look-up tables */
314 extern int alpha;
315 extern int vLUT[3][2 * LUTMAX16];
316 extern int lutmax;
317 extern int classmap[CONTEXTS1];
318 extern int *qdiv0, *qdiv,        /* quantization table (division via look-up) */
319      *qmul0, *qmul;        /* dequantization table */
320
321 /* statistics tables */
322 extern int  N[TOT_CONTEXTS], 
323     A[TOT_CONTEXTS], 
324     B[TOT_CONTEXTS],
325     C[TOT_CONTEXTS];
326
327
328 /****** Function prototypes */
329
330 /* global.c */
331 void error(const char *msg);
332 void *safealloc(size_t size);
333 void *safecalloc(size_t numels, size_t size);
334 double get_utime();
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);
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 void bufiinit(FILE *fil);
352 void bitiflush();
353
354 /*  melcode.c */
355 void init_process_run(int);
356 void close_process_run();
357 int  process_run_dec(int,int);
358
359 /* initialize.c */
360 void prepareLUTs();
361 void prepare_qtables(int, int);
362 void init_stats(int);
363
364 /* lossless_d.c */
365 int lossless_undoscanline(  pixel *psl, pixel *sl, int no, int color);
366 int lossless_undoscanline_pixel(pixel *psl, pixel *sl, int no);
367
368 /* lossy_d.c */
369 int lossy_undoscanline(  pixel *psl, pixel *sl, int no, int color);
370 int lossy_undoscanline_pixel(  pixel *psl, pixel *sl, int no);
371
372
373 #ifdef BIG_ENDIAN
374 #    define ENDIAN8(x)   (x)
375 #    define ENDIAN16(x)   (x)
376 #else
377 #    define ENDIAN8(x) (x&0x000000ff)
378 #    define ENDIAN16(x) ( ((x>>8)|(x<<8)) & 0x0000ffff)
379 #endif
380
381 /* ENDIAN function to fix endian of PCs (for 8 bit pixels) 
382 #define ENDIAN8(x) (x&0x000000ff)*/
383
384
385 /* ENDIAN function to fix endian of PCs (for 16 bit pixels) 
386 #define ENDIAN16(x) ( ((x>>8)|(x<<8)) & 0x0000ffff )*/
387
388
389
390
391 /* clipping macro */
392 #ifdef POW2
393 #  define clip(x,alpha) \
394       if ( x & highmask ) {\
395         if(x < 0) \
396       x = 0;\
397         else \
398       x = alpha - 1;\
399       }
400 #else
401 #  define clip(x,alpha) \
402     if(x < 0)  \
403       x = 0; \
404     else if (x >= alpha) \
405       x = alpha - 1;
406 #endif  /* POW2 */ 
407
408
409
410 /* macro to predict Px */
411 #define predict(Rb, Ra, Rc)  \
412 {  \
413   register pixel minx;  \
414   register pixel maxx;  \
415   \
416   if (Rb > Ra) {  \
417     minx = Ra;  \
418     maxx = Rb;  \
419   } else {  \
420     maxx = Ra;  \
421     minx = Rb;  \
422   }  \
423   if (Rc >= maxx)  \
424     Px = minx;  \
425   else if (Rc <= minx)  \
426     Px = maxx;  \
427   else  \
428     Px = Ra + Rb - Rc;  \
429 }
430
431
432 #endif