]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/global.c
COMP: Remove warning with -pedantic
[gdcm.git] / src / gdcmjpegls / Decoder / global.c
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.c --- support and portability routines: error handling, safe memory
43  *                              management, etc.
44  *
45  * Initial code by Alex Jakulin,  Aug. 1995
46  *
47  * Modified and optimized: Gadiel Seroussi, October 1995 - ...
48  *
49  * Modified and added Restart marker and input tables by:
50  * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
51  */
52
53 #include <time.h>
54 #include "global.h"
55 #ifdef HAVE_UNISTD_H
56 #include <unistd.h>
57 #else
58 #include <io.h>
59 #endif
60
61
62
63 char *disclaimer = "\
64 This program is Copyright (c) University of British Columbia.\n\
65 All rights reserved. It may be freely redistributed in its\n\
66 entirety provided that this copyright notice is not removed.\n\
67 It may not be sold for profit or incorporated in commercial programs\n\
68 without the written permission of the copyright holder.\n\
69 ";
70
71
72
73 /* I/O files */
74 FILE *in, *out;
75 FILE *c_in[MAX_COMPONENTS];
76 FILE *c_out[MAX_COMPONENTS];
77 FILE *msgfile = NULL; /* = stdout;*/
78
79 /* Context quantization thresholds  - initially unset */
80 int     T3 = -1,
81         T2 = -1,
82         T1 = -1,
83     Ta = -1;
84
85
86 int verbose = 1;   /* verbosity level */
87 int nopause = 0;   /* whether to pause the legal notice or not */
88 int nolegal = 0;   /* whether to print the legal notice or not */
89
90
91 /* parameters for LOSSY images */
92 int  quant,          /* quantization = 2*NEAR+1 */
93   beta,    /* size of extended alphabet */
94   qbeta,          /* size of quantized alphabet */
95   ceil_half_qbeta, /* ceil(qbeta/2) */
96   negNEAR,            /* -NEAR */
97   alpha1eps;       /* alpha-1+NEAR */
98
99 int  NEAR = DEF_NEAR;   /* loss tolerance per symbol, fixed at 0 for lossless */
100 int bpp,    /* bits per sample */
101   qbpp,   /* bits per sample for quantized prediction errors */
102     limit,  /* limit for unary part of Golomb code */
103     limit_reduce;  /* reduction on above for EOR states */
104
105
106 /* define color mode strings */
107 char *plane_int_string = "plane by plane",
108    *line_int_string = "line intlv",
109    *pixel_int_string = "sample intlv";
110
111
112 /* function to print out error messages */
113 void error(char *msg) {
114   fprintf(stderr, msg);
115   exit(-1);
116 }
117
118
119 /* function to safely call malloc */
120 void *safealloc(size_t size) {
121   void *temp;
122
123   temp = malloc(size);
124     if (temp == NULL)
125     error("\nsafealloc: Out of memory. Aborting...\n");
126   return temp;
127 }
128
129
130 /* function to safely call calloc **/
131 void *safecalloc(size_t numels, size_t size) {
132   void *temp;
133
134   temp = calloc(numels, size);
135     if (temp == NULL)
136     error("\nsafecalloc: Out of memory. Aborting...\n");
137   return temp;
138 }
139
140
141 /*
142  * TIMING ROUTINES
143  */
144
145
146 double get_utime()
147 {
148   clock_t c;
149   (void)c;
150
151   return (double)clock()/CLOCKS_PER_SEC;
152 }
153
154
155 /* Set thresholds to default unless specified by header: */
156 int set_thresholds(int alfa, int NEAR, int *T1p, int *T2p, int *T3p)
157 {
158   int lambda,
159       ilambda = 256/alfa,
160       quant = 2*NEAR+1,
161       T1 = *T1p, 
162       T2 = *T2p, 
163       T3 = *T3p;
164   /* Unused */
165   (void)quant;
166   
167   if (alfa<4096)
168      lambda = (alfa+127)/256;
169         else
170      lambda = (4096+127)/256;
171
172
173
174   if ( T1 <= 0 )  {
175     /* compute lossless default */
176     if ( lambda ) 
177       T1 = lambda*(BASIC_T1 - 2) + 2;
178     else {  /* alphabet < 8 bits */
179       T1 = BASIC_T1/ilambda;
180       if ( T1 < 2 ) T1 = 2;
181     }
182     /* adjust for lossy */
183     T1 += 3*NEAR;
184
185     /* check that the default threshold is in bounds */
186     if ( T1 < NEAR+1 || T1 > (alfa-1) ) 
187          T1 = NEAR+1;         /* eliminates the threshold */
188   }
189   if ( T2 <= 0 )  {
190     /* compute lossless default */
191     if ( lambda ) 
192       T2 = lambda*(BASIC_T2 - 3) + 3;
193     else {
194       T2 = BASIC_T2/ilambda;
195       if ( T2 < 3 ) T2 = 3;
196     }
197     /* adjust for lossy */
198     T2 += 5*NEAR;
199
200     /* check that the default threshold is in bounds */
201     if ( T2 < T1 || T2 > (alfa-1) ) 
202          T2 = T1;         /* eliminates the threshold */
203   }
204   if ( T3 <= 0 )  {
205     /* compute lossless default */
206     if ( lambda ) 
207       T3 = lambda*(BASIC_T3 - 4) + 4;
208     else {
209       T3 = BASIC_T3/ilambda;
210       if ( T3 < 4 ) T3 = 4;
211     }
212     /* adjust for lossy */
213     T3 += 7*NEAR;
214
215     /* check that the default threshold is in bounds */
216     if ( T3 < T2 || T3 > (alfa-1) ) 
217          T3 = T2;         /* eliminates the threshold */
218   }
219
220   *T1p = T1;
221   *T2p = T2;
222   *T3p = T3;
223   return 0;
224 }
225
226
227
228
229 /* We first check compatibility with JPEG-LS, then with this implementation */
230
231 void check_compatibility(jpeg_ls_header *head_frame, jpeg_ls_header *head_scan, int n_s)
232 {
233
234     int  number_of_scans,i;  
235     int maxreset;
236
237 /* Check implemented color modes */
238     if ((head_scan->color_mode>PIXEL_INT)) {
239   fprintf(stderr,"Color mode %d not supported\n",head_scan->color_mode);
240   exit(10);
241     }
242
243     if (head_scan->color_mode==PLANE_INT) 
244   number_of_scans=head_frame->comp;
245     else 
246   number_of_scans=1;
247     
248
249 /* Test standard compatibility */
250
251     if (head_frame->columns<=0 || head_frame->rows <=0) {
252   fprintf(stderr,"Image size must be positive for this implementation.\n");
253   exit(10);
254     }
255
256     if (head_frame->alp<4) {
257   fprintf(stderr,"Alphabet size must be >= 4, got %d\n",head_frame->alp);
258   exit(10);
259     }
260
261
262     if (head_scan->T1>head_scan->T2 || head_scan->T2>head_scan->T3 ||
263   head_scan->T1<head_scan->NEAR+1 || head_scan->T3>=head_scan->alp ) {
264   fprintf(stderr,"Bad thresholds: must be %d <= Ta <= Tb <= Tc <= %d\n",
265       head_scan->NEAR+1,head_scan->alp-1);
266   exit(10);
267     }
268
269     if (head_frame->comp>255) {
270   fprintf(stderr,"Too many components (must be less than 255)\n");
271   exit(10);
272     }
273
274     if (head_scan->NEAR>=head_scan->alp) {
275   fprintf(stderr,"Error for near-lossless must be smaller than alphabet (%d), got %d",head_scan->alp,head_scan->NEAR);
276   exit(10);
277     }
278
279     /*
280     if (head_scan->RES < MINRESET || head_scan->RES >= head_scan->alp ) {
281   fprintf(stderr,"Reset parameter must be between %d and %d\n",
282           MINRESET, head_scan->alp-1);
283   exit(10);
284     }
285     */
286
287     maxreset = (head_scan->alp >= 256)? (head_scan->alp-1):255;
288
289     if (head_scan->RES < MINRESET || head_scan->RES > maxreset ) {
290   fprintf(stderr,"Reset parameter must be between %d and %d\n",
291           MINRESET, head_scan->alp-1);
292   exit(10);
293     }
294
295     for (i=0;i<head_frame->comp;i++)
296   if (head_frame->comp_ids[i] != (i+1)) {
297      fprintf(stderr,"Components id in frame not compatible with this implementation.\n");
298      exit(10);
299         }
300
301     if (number_of_scans == 1) {
302   if (head_frame->comp != head_scan->comp) {
303      fprintf(stderr,"In this implementation, when single scan, all components must be in the scan.\n");
304      exit(10);
305         }
306         for (i=0;i<head_frame->comp;i++)
307     if (head_scan->comp_ids[i] != (i+1)) {
308        fprintf(stderr,"Components id in single scan not compatible with this implementation.\n");
309        exit(10);
310           }
311
312     }
313     else {
314   if (head_scan->comp != 1) {
315      fprintf(stderr,"Only 1 component per scan for plane interleaved mode\n");
316      exit(10);
317         }
318         if (head_scan->comp_ids[0] != (n_s+1)) {
319      fprintf(stderr,"Components id in multiple scan not compatible with this implementation.\n");
320      exit(10);
321         }
322
323     }
324 }
325
326
327 /* for writing disclaimer to command line in DOS */
328
329 char *ttyfilename = "CON";
330
331 #define PAUSE  20
332
333 void fprint_disclaimer(FILE *fp, int nopause)
334 {
335   char *p0, *p1;
336   FILE *ttyf;
337   int  i; /*, c;*/
338
339   nopause = nopause | !isatty(fileno(fp));
340
341   if ( !nopause && (ttyf=fopen(ttyfilename,"r"))==NULL ) {
342     nopause = 1;
343   }
344
345   for ( i=1, p0=disclaimer; ; i++ ) {
346     if ( !(*p0)  ) break;
347     if ( !nopause && i%PAUSE==0 ) {
348       fflush(fp);
349       fprintf(stderr, "--- (press RETURN to continue) ---"); 
350       fflush(stderr);
351 /*      c = getc(ttyf);*/
352     }
353     for ( p1=p0; (*p1 != '\n') && (*p1 != 0); p1++ );
354     *p1 = 0;
355     fprintf(fp,"%s\n",p0);
356     p0 = p1+1;
357   }
358   fprintf(fp,"\n"); fflush(fp);
359   if ( !nopause) fclose(ttyf);
360 }