]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/jpegmark.c
use GDCM_NAME_SPACE:: instead of gdcm::, even in Examples ...
[gdcm.git] / src / gdcmjpegls / Decoder / jpegmark.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 /* jpegmark.c --- for JPEG markers
43  *
44  * Initial code by Alex Jakulin,  Aug. 1995
45  *
46  * Modified and optimized: Gadiel Seroussi, October 1995
47  *
48  * Color Enhancement: Guillermo Sapiro, August 1996
49  *
50  * Modified and added Restart marker and input tables by:
51  * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
52  */
53
54
55 #include <stdio.h>
56 #include "global.h"
57 #include "jpegmark.h"
58
59 #ifdef BUFINPUT
60 #   include "bitio.h"
61 #else
62 #   define  mygetc(in)  (getc(in))     
63 #   define   myfeof(in)  (feof(in))
64 #endif
65
66 void
67 check_range(int param, const char *name, int low, int high)
68 {
69     if ( param < low || param > high ) {
70   fprintf(stderr,"Allowed range for %s is [%d..%d]: got %d\n",
71   name, low, high, param);
72   exit(10);
73     }
74 }
75
76 /*
77  *
78  *   Marker output functions
79  *
80  */
81
82 int
83 write_n_bytes(FILE *out, int value, int n)
84 {
85
86   int  l;
87
88
89   if (n>4)  {
90     fprintf(stderr,"write_n_bytes: Only 32 bits variables supported.\n");
91     exit(10);
92   }
93     
94
95 #ifdef BIG_ENDIAN
96         for (l=n-1;l>=0;l--)  {
97       if ( putc((value>>(8*l))&0x000000FF,out) == EOF )
98     return EOF;
99         }
100 #else  /* little endian */
101   for (l=0;l<n;l++) {
102       if ( putc((value&0x000000FF),out) == EOF )
103     return EOF;
104       value >>= 8;
105   }
106 #endif
107   return n;
108
109 }
110
111 int
112 write_2_bytes(FILE *out, int value)
113 {
114   return write_n_bytes(out,value,2);
115 }
116
117 int
118 write_marker(FILE *out, int marker)
119 /* Write a two-byte marker (just the marker identifier) */
120 {
121   write_n_bytes(out, marker, 2);
122   return 2;
123 }
124
125 int
126 write_jpegls_frame(FILE *out, jpeg_ls_header *jp)
127 {
128     int i, marker_len,
129   bpp, ct = 0;
130      
131     ct += write_marker(out, SOF_LS);   /* write JPEG-LS frame marker */
132
133     check_range(jp->comp, "frame components", 1, 255);
134     marker_len = 8 + 3*jp->comp;
135
136     ct += write_n_bytes(out, marker_len, 2); /* write marker length */
137
138     for ( bpp=1; (1L<<bpp)<jp->alp; bpp++ );
139
140     ct += write_n_bytes(out, bpp, 1);        /* write bits/sample */
141
142     /* current implementation only supports up to 64K samples in 
143        either direction. Also, they must be specified in the frame header  */
144     check_range(jp->rows, "rows", 1, 65535);
145     check_range(jp->columns, "columns", 1, 65535);
146
147     ct += write_n_bytes(out, jp->rows, 2);     /* write number of rows */
148     ct += write_n_bytes(out, jp->columns, 2);  /* write number of cols */
149
150     ct += write_n_bytes(out, jp->comp, 1);
151
152     /* now write a triplet of bytes per component */
153     for ( i=0; i<jp->comp; i++ ) {
154   int sx = jp->samplingx[i], 
155       sy = jp->samplingy[i];
156
157   check_range(sx,"sampling(x)",1,4);
158   check_range(sy,"sampling(y)",1,4);
159   ct += write_n_bytes(out, jp->comp_ids[i], 1); /* component identifier */
160   ct += write_n_bytes(out, (sx<<4)|sy, 1);  /* sampling rates */
161   ct += write_n_bytes(out, 0, 1);    /* Tq unused */
162     }
163     return ct;
164 }
165
166 int
167 write_jpegls_scan(FILE *out, jpeg_ls_header *jp)
168 {
169     int i, marker_len, ct=0;
170      
171     ct += write_marker(out, SOS);   /* write JPEG-LS scan marker */
172
173     check_range(jp->comp, "scan components", 1, 4);
174
175     if ( jp->comp == 1 && jp->color_mode != PLANE_INT) {
176   fprintf(stderr,"Interleave for 1 component must be PLANE_INT: got %d\n",
177     jp->color_mode);
178   exit(10);
179     }
180
181     if ( jp->comp >1 && jp->color_mode == 0 ) {
182   fprintf(stderr,"Interleave for multi-component scan must be nonzero: got %d\n",
183     jp->color_mode);
184   exit(10);
185     }
186
187     marker_len = 6 + 2*jp->comp;
188
189     ct += write_n_bytes(out, marker_len, 2); /* write marker length */
190     ct += write_n_bytes(out, jp->comp, 1);   /* # of components for the scan */
191
192     /* write 2 bytes per component */
193     for ( i=0; i<jp->comp; i++ ) {
194   ct += write_n_bytes(out, jp->comp_ids[i], 1); /* component identifier */
195   ct += write_n_bytes(out, 0, 1);   /* no tables in this implementation */
196     }
197
198     check_range(jp->NEAR, "NEAR", 0, 255);
199     ct += write_n_bytes(out, jp->NEAR, 1);
200
201     check_range(jp->color_mode,"INTERLEAVE", 0, 2);
202     ct += write_n_bytes(out, jp->color_mode, 1);
203
204     check_range(jp->shift, "SHIFT", 0, 15);
205     ct += write_n_bytes(out, jp->shift, 1);
206
207     return ct;
208 }
209
210
211 int
212 write_jpegls_extmarker(FILE *out, jpeg_ls_header *jp)
213 {
214     int ct=0;
215      
216     ct += write_marker(out, LSE);   /* write JPEG-LS extended marker id */
217
218     ct += write_n_bytes(out, 13, 2); /* marker length */
219     ct += write_n_bytes(out,  LSE_PARAMS, 1); /* ext marker id */
220     ct += write_n_bytes(out, jp->alp-1, 2);  /* MAXVAL */
221     ct += write_n_bytes(out, jp->T1, 2); 
222     ct += write_n_bytes(out, jp->T2, 2); 
223     ct += write_n_bytes(out, jp->T3, 2); 
224     ct += write_n_bytes(out, jp->RES, 2);
225     return ct;
226 }
227
228
229 /*
230  *
231  *   Marker input functions
232  *
233  */
234
235 int
236 seek_marker(FILE *in, int *mkp )
237 /* Seeks a marker in the input stream. Returns the marker head, or EOF */
238 {
239     int c, c2, ct=0;
240     while ( (c=mygetc(in)) != EOF ) {
241   ct ++;
242   if ( c == 0xFF ) {
243       if ( (c2=mygetc(in)) == EOF )
244       return EOF;
245       ct ++;
246       if ( c2 & 0x80 ) {
247       *mkp = (c<<8)|c2;
248     return ct;
249       }
250   }
251     }
252     return EOF;
253 }
254   
255
256 unsigned int
257 read_n_bytes(FILE *in, int n)
258 /* reads n bytes (0 <= n <= 4) from the input stream */
259 {
260     unsigned int m = 0;
261     int i;
262
263     for ( i=0; i<n; i++ )
264   m = (m << 8) | mygetc(in);
265     return m;
266
267 }
268
269 int
270 read_marker(FILE *in, int *mkp)
271 /* reads a marker from the next two bytes in the input stream */
272 {
273   unsigned int m;
274
275   m = read_n_bytes(in, 2);
276   if ( feof(in) ) return EOF;
277   if ( (m & 0xFF00) != 0xFF00 )  {
278       fprintf(stderr,"read_marker: Expected marker, got %04x\n",m);
279       exit(10);
280   }
281   *mkp = m;
282   return 2;
283 }
284
285 int
286 read_jpegls_frame(FILE *in, jpeg_ls_header *jp)
287 /* reads the JPEG-LS frame marker (not including marker head) */
288 {
289     int i,
290   marker_len,
291   bpp,
292   tq,
293   comp,
294   ct = 0;
295
296     /* Read Marker Length */
297     marker_len = read_n_bytes(in, 2);
298     ct += 2;
299
300   /* Read the bits per pixel */
301     bpp = read_n_bytes(in, 1);
302     ct ++;
303
304     check_range(bpp,"bpp",2,16);
305     jp->alp = 1<<bpp;
306
307   /* Read the rows and columns */
308     jp->rows = read_n_bytes(in, 2);
309     ct += 2;
310     jp->columns = read_n_bytes(in, 2);
311     ct += 2;
312     
313   /* Read component information */
314   comp = read_n_bytes(in, 1);
315     ct += 1;
316     check_range(comp,"COMP",1,255);
317     jp->comp = comp;
318
319     for ( i=0; i<comp; i++ ) 
320   {
321     int sx, sy, cid;
322
323     cid = read_n_bytes(in, 1);
324     ct += 1;
325     sx = read_n_bytes(in, 1);
326     ct += 1;
327     tq = read_n_bytes(in, 1);
328     ct += 1;
329     check_range(tq,"Tq",0,0);
330     sy = sx & 0x0f;
331     sx >>= 4;
332     check_range(sx, "sampling(x)", 1, 4);
333     check_range(sy, "sampling(y)", 1, 4);
334     jp->samplingx[i] = sx;
335     jp->samplingy[i] = sy;
336     jp->comp_ids[i] = cid;
337     }
338
339   /* Check for errors */
340     if ( myfeof(in) ) 
341   {
342     fprintf(stderr,"read_jpegls_frame: EOF while reading frame marker\n");
343     return EOF;
344     }
345     if ( marker_len != 8 + 3*comp ) 
346   {
347     fprintf(stderr,"read_jpegls_frame: inconsistent marker length: expected %d, got %d\n",marker_len, 8 + 3*comp);
348     exit(10);
349     }
350
351     return ct;
352 }
353
354
355
356 /* reads the JPEG-LS scan marker (not including marker head) */
357 int read_jpegls_scan(FILE *in, jpeg_ls_header *jp)
358 {
359     int i, marker_len,
360   comp, ct=0;
361      
362
363     marker_len = read_n_bytes(in, 2);
364     ct += 2;
365
366     comp = read_n_bytes(in, 1);
367     ct += 1;
368     check_range(comp, "scan components", 1, 4);
369
370     jp->comp = comp;
371
372     /* read 2 bytes per component */
373     for ( i=0; i<comp; i++ ) {
374   int cid, tm;
375
376   cid = read_n_bytes(in, 1); /* component identifier */
377   ct += 1;
378   tm = read_n_bytes(in, 1);  /* table identifier */
379   ct += 1;
380
381   if ( tm ) {
382       fprintf(stderr,"read_jpegls_scan: found nonzero table identifier, not supported\n");
383       exit(10);
384   }
385   jp->comp_ids[i] = cid;
386     }
387
388     jp->NEAR = read_n_bytes(in, 1);
389     ct += 1;
390     check_range(jp->NEAR,"NEAR", 0, 255);
391
392     jp->color_mode = read_n_bytes(in, 1);
393     ct += 1;
394     check_range(jp->color_mode, "INTERLEAVE", 0, 2);
395
396     if ( jp->comp == 1 && jp->color_mode != 0 ) {
397   /*
398   fprintf(stderr,"Interleave for 1 component must be 0: got %d\n",
399     jp->color_mode);
400   */
401
402   /* ignore interleave value, set to 0 */
403   jp->color_mode = 0;
404     }
405
406     if ( jp->comp >1 && jp->color_mode == 0 ) {
407   fprintf(stderr,"Interleave for multi-component scan must be nonzero: got %d\n",
408     jp->color_mode);
409   exit(10);
410     }
411
412
413     jp->shift = read_n_bytes(in, 1);
414     ct += 1;
415     check_range(jp->shift, "SHIFT", 0, 15);
416
417     if ( myfeof(in) ) {
418   fprintf(stderr,"read_jpegls_scan: EOF while reading frame marker\n");
419   return EOF;
420     }
421     if ( marker_len != 6 + 2*comp ) {
422   fprintf(stderr,"read_jpegls_scan: inconsistent marker length: expected %d, got %d\n",marker_len, 6 + 2*comp);
423   exit(10);
424     }
425     return ct;
426 }
427
428
429
430 /* reads the JPEG-LS extension marker (not including marker head) */
431 /* Supports non-default type (1) and mapping table type (2) */
432 int read_jpegls_extmarker(FILE *in, jpeg_ls_header *jp)
433
434 {
435     int marker_len,    /* marker length */
436     maxval,      /* max value */
437     ct = 0;      
438   int IDtype;      /* LSE type */
439   int TID;      /* table ID */
440   int Wt;        /* width of each table entry */
441   int MAXTAB;      /* maximum table index */
442   int i;
443
444
445   /* Read marker length */
446   marker_len = read_n_bytes(in, 2); /* marker length */
447   ct += 2;
448
449   /* Read id type */
450   IDtype = read_n_bytes(in,  1);
451   ct += 1;
452   
453
454   /* For Type 1 - non default parameters */
455   if (IDtype == LSE_PARAMS)
456   {
457     if ( marker_len != 13 ) 
458     {
459       fprintf(stderr,"read_jpegls_extmarker: bad marker length %d\n",marker_len);
460       exit(10);
461     }
462
463     /* read maxval */
464     maxval = read_n_bytes(in, 2);
465     ct += 2;
466     jp->alp = maxval +1;
467
468     /* read thresholds and reset */
469     jp->T1 = read_n_bytes(in, 2);
470     ct += 2;
471     jp->T2 = read_n_bytes(in, 2);
472     jp->T3 = read_n_bytes(in, 2);
473     jp->RES = read_n_bytes(in, 2);
474     ct += 6;
475
476     if ( myfeof(in) ) {
477       fprintf(stderr,"read_jpegls_extmarker: EOF while reading frame marker\n");
478       return EOF;
479     }
480
481     return ct;
482   }
483
484   /* For Type 2 - mapping table */
485   if (IDtype == LSE_MAPTABLE)
486   {
487
488     /* Indicate table used */
489     jp->need_table = 1;
490
491     /* Read table ID */
492     jp->TID = TID = read_n_bytes(in, 1);
493     ct += 1;
494
495     /* Read width of table entry */
496     jp->Wt = Wt = read_n_bytes(in, 1);
497     if (Wt<=0 || Wt>3)
498     {  
499       fprintf(stderr, "Width of mapping table entries must be either 1,2 or 3 in this implementation. Sorry!\n");
500       exit(0);
501     }
502     ct += 1;
503
504     /* Calculate value of MAXTAB */
505     jp->MAXTAB = MAXTAB = ((marker_len - 5) / Wt) - 1;
506
507     /* Get table entries */
508     jp->TABLE[TID] = (unsigned int *)safecalloc((MAXTAB+1)*sizeof(int), 1);
509     for (i=0; i<=MAXTAB; i++)
510     {
511       jp->TABLE[TID][i] = read_n_bytes(in, Wt);
512     }
513     ct += ((MAXTAB+1) * Wt);
514
515     return ct;
516   }
517
518   /* Non supported types */
519
520   fprintf(stderr, "LSE marker type %i not supported in this implementation.\n", IDtype);
521   exit(0);
522
523 }
524
525
526
527
528
529 /* Read DRI restart marker */
530 int read_jpegls_restartmarker(FILE *in, jpeg_ls_header *jp)
531 {
532   int ct = 0;
533   int marker_len;    /* the marker length */
534   int Ri;        /* the restart interval */
535
536   /* Read marker length */
537     marker_len = read_n_bytes(in, 2);
538     ct += 2;
539
540   /* Read the restart interval */
541   Ri = read_n_bytes(in, marker_len - 2);
542   ct += (marker_len - 2);
543
544   jp->restart_interval = Ri;
545
546   return ct;
547   
548 }