]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Encoder/global.h
Fix writting VR = OL
[gdcm.git] / src / gdcmjpegls / Encoder / 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
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <assert.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
79 /* Maximal number of components in the implementation*/
80 #define MAX_COMPONENTS  6   
81 #define MAX_SCANS  MAX_COMPONENTS 
82
83
84 /* For 1st component of plane interl. mode */
85 #define FIRST    1  
86
87
88 /* Different colour modes */
89 #define PLANE_INT  0
90 #define LINE_INT  1
91 #define PIXEL_INT  2
92
93 #define DEFAULT_COLOR_MODE LINE_INT
94
95 extern char  *plane_int_string,
96     *line_int_string,
97     *pixel_int_string;
98
99
100 #define BIG_ENDIAN  1
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             Wt,        /* Width of each table entry, 1 byte */
124                   *TABLE[MAX_COMPONENTS];    /* The table(s) for each component */
125
126 } jpeg_ls_header;
127
128 extern int  components;
129 extern int  sampling[MAX_COMPONENTS];
130
131 #define NAME_LENGTH  40
132
133 /* Output file names */
134 #define OUTFILE "outfile"
135 #define COMPSUFFIX ".jls"
136
137
138 /* Define max and min macros */
139 #ifndef max
140 #  define max(a,b)  (((a)>=(b))?(a):(b))
141 #  define min(a,b)  (((a)<=(b))?(a):(b))
142 #endif
143
144
145 /****** Constants */
146
147 /* margins for scan lines */
148 #define  LEFTMARGIN  2
149 #define RIGHTMARGIN  1
150
151
152 extern char *disclaimer;
153
154
155 /* alphabet size */
156 #define MAXA8 (256)
157 #define MAXA16 (65536)
158 #define LUTMAX8 (256)
159 #define LUTMAX16 (4501)
160
161
162 #ifdef FIXALPHA
163 #  ifndef alpha
164 #    define  alpha  256
165 #  endif
166 #  define   highmask (-(alpha))
167 #  ifndef POW2
168 #  define POW2
169 #  endif
170 #  if (alpha!=2) && (alpha!=4) && (alpha!=8) && (alpha!=16) && (alpha!=32) &&\
171      (alpha!=64) && ( alpha!=128) && (alpha!=256) && (alpha!=512) &&\
172      (alpha!=1024) && ( alpha!=2048) && (alpha!=4096) && (alpha!=8192) &&\
173      (alpha!=16384) && ( alpha!=32768) && (alpha!=65536)\
174 #      error "Fixed alpha must be a power of 2"
175 #  endif
176 #  define    ceil_half_alpha (alpha/2)
177 #else
178 extern int      alpha;     /* alphabet size */
179 extern int      ceil_half_alpha; /* ceil(alpha/2) */
180 extern int      highmask;  /* for powers of 2, a mask for high bits */
181 #endif
182
183
184
185 extern int bpp,      /* bits per sample */
186      qbpp,    /* bits per sample for quantized prediction errors */
187            limit,    /* limit for unary part of Golomb code */
188      limit_reduce;  /* reduction on above for EOR states */
189
190
191 #define DEF_NEAR  0
192
193 /* for LOSSY mode */
194 extern  int  quant, 
195       beta, 
196       qbeta,
197       ceil_half_qbeta,
198       negNEAR,
199       alpha1eps;
200
201 /* loss tolerance */
202 extern int NEAR;
203
204
205 /* Quantization threshold basic defaults */
206 /* These are the defaults for LOSSLESS, 8 bpp. Defaults for other
207    cases are derived from these basic values */
208 #define  BASIC_T1  3
209 #define  BASIC_T2  7
210 #define  BASIC_T3  21
211 #define  BASIC_Ta  5
212
213 #define CREGIONS (9)    /* quantization regions for d-b, b-c, c-a */
214
215 /* run-length treshold */
216 #ifndef MAXRUN
217 #  define MAXRUN (64)
218 #endif
219
220 #define EOLINE   1
221 #define NOEOLINE 0
222
223 /* number of different contexts */
224 #define CONTEXTS1 (CREGIONS*CREGIONS*CREGIONS)
225
226 #define CONTEXTS   ((CONTEXTS1+1)/2) /* all regions, with symmetric merging */
227
228
229 /* Mandatory for JPEG-LS: */
230 #define CLAMP
231 #define CLAMPB
232 #define CLAMPC
233
234
235 #define MAX_C 127
236 #define MIN_C -128
237
238
239 #define MAXCODE (N_R_L_ERROR)
240
241
242 /* Number of end-of-run contexts */
243 #define EOR_CONTEXTS 2
244
245
246 /* Total number of contexts */
247 #define TOT_CONTEXTS (CONTEXTS +  EOR_CONTEXTS)
248
249
250 /* index of first end-of-run context */
251 #define EOR_0  (CONTEXTS)
252
253
254 /* index of run state */
255 #define RUNSTATE 0
256
257
258
259 /*** offsets */
260
261 /* The longest code the bit IO can facilitate */
262 #define MAXCODELEN 24
263
264 /* The stat initialization values */
265 #define INITNSTAT 1      /* init value for N[] */
266 #define MIN_INITABSTAT 2    /* min init value for A[] */
267 #define INITABSLACK 6       /* init value for A is roughly 
268                  2^(bpp-INITABSLACK) but not less than above */
269 #define INITBIASTAT 0    /* init value for B[] */
270
271 /* Limit for unary code */
272 #define LIMIT 23
273
274 /* reset values */
275 #define DEFAULT_RESET 64
276 #define MINRESET 3
277
278 #ifdef FIXRESET
279 #   ifndef RESET
280 #    define RESET     DEFAULT_RESET
281 #   endif
282 #else
283 extern int  RESET;
284 #endif
285
286 #define  reset  RESET          /* reset threshold */
287
288 #define RESRUN    256
289
290
291 /****** Type prototypes */
292
293 /* Portability types */
294 typedef unsigned char byte;
295 typedef unsigned short word;
296 typedef unsigned long dword;
297
298 typedef unsigned short pixel;
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;        /* Indicates if 16 bits per pixel mode or not */
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 /*extern byte getk[65][3000];*/
332 /*extern int clipPx[510];*/
333
334
335 /****** Function prototypes */
336
337 /* global.c */
338 void error(char *msg);
339 void *safealloc(size_t size);
340 void *safecalloc(size_t numels, size_t size);
341
342 /* lossless.c */
343 void lossless_doscanline_pixel(pixel *psl, pixel *sl, int no);
344 void lossless_doscanline(pixel *psl, pixel *sl, int no, int color);
345
346 /* lossy.c */
347 void lossy_doscanline_pixel(pixel *psl, pixel *sl, int no);
348 void lossy_doscanline(pixel *psl, pixel *sl, int no, int color);
349
350 /* bitio.c */
351 void bitoflush();
352 void bitoinit();
353 void buffinit(FILE *);
354
355 /*  melcode.c */
356 void init_process_run(int);
357 void close_process_run();
358 int  process_run(int,int,int);
359
360
361 /* initialize.c */
362 void prepareLUTs();
363 void prepare_qtables(int, int);
364 void init_stats(int);
365
366
367
368 #ifdef BIG_ENDIAN
369 #    define ENDIAN8(x)   (x)
370 #    define ENDIAN16(x)   (x)
371 #else
372 #    define ENDIAN8(x) (x&0x000000ff)
373 #    define ENDIAN16(x) ( ((x>>8)|(x<<8)) & 0x0000ffff)
374 #endif
375
376 /* ENDIAN function to fix endian of PCs (for 8 bit pixels)
377 #define ENDIAN8(x) (x&0x000000ff)*/
378
379
380 /* ENDIAN function to fix endian of PCs (for 16 bit pixels)
381 #define ENDIAN16(x) ( ((x>>8)|(x<<8)) & 0x0000ffff )*/
382
383
384
385 /* clipping macro */
386 #ifdef POW2
387 #  define clip(x,alpha) \
388       if ( x & highmask ) {\
389         if(x < 0) \
390       x = 0;\
391         else \
392       x = alpha - 1;\
393       }
394 #else
395 #  define clip(x,alpha) \
396     if(x < 0)  \
397       x = 0; \
398     else if (x >= alpha) \
399       x = alpha - 1;
400 #endif  /* POW2 */ 
401
402
403
404 /* macro to predict Px */
405 #define predict(Rb, Ra, Rc)  \
406 {  \
407   register pixel minx;  \
408   register pixel maxx;  \
409   \
410   if (Rb > Ra) {  \
411     minx = Ra;  \
412     maxx = Rb;  \
413   } else {  \
414     maxx = Ra;  \
415     minx = Rb;  \
416   }  \
417   if (Rc >= maxx)  \
418     Px = minx;  \
419   else if (Rc <= minx)  \
420     Px = maxx;  \
421   else  \
422     Px = Ra + Rb - Rc;  \
423 }
424
425 #endif