]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Encoder/jpegmark.c
change
[gdcm.git] / src / gdcmjpegls / Encoder / 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 #include <stdio.h>
55 #include "global.h"
56 #include "jpegmark.h"
57
58 #ifdef BUFINPUT
59 #   include "bitio.h"
60 #endif
61
62 /* Makes sure a parameter falls within its allowed range */
63 void check_range(int param, char *name, int low, int high)
64 {
65     if ( param < low || param > high )
66   {
67     fprintf(stderr,"Allowed range for %s is [%d..%d]: got %d\n",
68       name, low, high, param);
69     exit(10);
70     }
71 }
72
73 /* reads n bytes (0 <= n <= 4) from the input stream */
74
75 unsigned int read_n_bytes(FILE *in, unsigned int n)
76 {
77     unsigned int m = 0;
78     int i;
79
80     for ( i=0; i<n; i++ )
81         m = (m << 8) | ((unsigned char)getc(in));
82     return m;
83 }
84
85 /*
86  *   Marker output functions
87  */
88
89 int write_n_bytes(FILE *out, int value, int n)
90 {
91
92   int  l;
93
94
95   if (n>4)  {
96     fprintf(stderr,"write_n_bytes: Only 32 bits variables supported.\n");
97     exit(10);
98   }
99     
100
101 #ifdef BIG_ENDIAN
102         for (l=n-1;l>=0;l--)  {
103       if ( putc((value>>(8*l))&0x000000FF,out) == EOF )
104     return EOF;
105         }
106 #else  /* little endian */
107   for (l=0;l<n;l++) {
108       if ( putc((value&0x000000FF),out) == EOF )
109     return EOF;
110       value >>= 8;
111   }
112 #endif
113   return n;
114
115 }
116
117
118
119 int write_2_bytes(FILE *out, int value)
120 {
121   return write_n_bytes(out,value,2);
122 }
123
124
125
126 int write_marker(FILE *out, int marker)
127 /* Write a two-byte marker (just the marker identifier) */
128 {
129   write_n_bytes(out, marker, 2);
130   return 2;
131 }
132
133
134 /* Write the frame header to the JLS file */
135 int write_jpegls_frame(FILE *out, jpeg_ls_header *jp)
136 {
137     int i, marker_len,
138   bpp, ct = 0;
139      
140     ct += write_marker(out, SOF_LS);   /* write JPEG-LS frame marker */
141
142     check_range(jp->comp, "frame components", 1, 255);
143     marker_len = 8 + 3*jp->comp;
144
145     ct += write_n_bytes(out, marker_len, 2); /* write marker length */
146
147     for ( bpp=1; (1L<<bpp)<jp->alp; bpp++ );
148
149     ct += write_n_bytes(out, bpp, 1);        /* write bits/sample */
150
151     /* current implementation only supports up to 64K samples in 
152        either direction. Also, they must be specified in the frame header  */
153     check_range(jp->rows, "rows", 1, 65535);
154     check_range(jp->columns, "columns", 1, 65535);
155
156     ct += write_n_bytes(out, jp->rows, 2);     /* write number of rows */
157     ct += write_n_bytes(out, jp->columns, 2);  /* write number of cols */
158
159     ct += write_n_bytes(out, jp->comp, 1);
160
161     /* now write a triplet of bytes per component */
162     for ( i=0; i<jp->comp; i++ ) {
163   int sx = jp->samplingx[i], 
164       sy = jp->samplingy[i];
165
166   check_range(sx,"sampling(x)",1,4);
167   check_range(sy,"sampling(y)",1,4);
168   ct += write_n_bytes(out, jp->comp_ids[i], 1); /* component identifier */
169   ct += write_n_bytes(out, (sx<<4)|sy, 1);  /* sampling rates */
170   ct += write_n_bytes(out, 0, 1);    /* Tq unused */
171     }
172     return ct;
173 }
174
175
176 /* Write the Scan header to JLS file */
177 int write_jpegls_scan(FILE *out, jpeg_ls_header *jp)
178 {
179     int i, marker_len, ct=0;
180      
181     ct += write_marker(out, SOS);   /* write JPEG-LS scan marker */
182
183     check_range(jp->comp, "scan components", 1, 4);
184
185     if ( jp->comp == 1 && jp->color_mode != PLANE_INT) {
186   fprintf(stderr,"Interleave for 1 component must be PLANE_INT: got %d\n",
187     jp->color_mode);
188   exit(10);
189     }
190
191     if ( jp->comp >1 && jp->color_mode == 0 ) {
192   fprintf(stderr,"Interleave for multi-component scan must be nonzero: got %d\n",
193     jp->color_mode);
194   exit(10);
195     }
196
197     marker_len = 6 + 2*jp->comp;
198
199     ct += write_n_bytes(out, marker_len, 2); /* write marker length */
200     ct += write_n_bytes(out, jp->comp, 1);   /* # of components for the scan */
201
202     /* write 2 bytes per component */
203     for ( i=0; i<jp->comp; i++ ) {
204   ct += write_n_bytes(out, jp->comp_ids[i], 1); /* component identifier */
205   ct += write_n_bytes(out, 0, 1);   /* no tables in this implementation */
206     }
207
208     check_range(jp->NEAR, "NEAR", 0, 255);
209     ct += write_n_bytes(out, jp->NEAR, 1);
210
211     check_range(jp->color_mode,"INTERLEAVE", 0, 2);
212     ct += write_n_bytes(out, jp->color_mode, 1);
213
214     check_range(jp->shift, "SHIFT", 0, 15);
215     ct += write_n_bytes(out, jp->shift, 1);
216
217     return ct;
218 }
219
220
221
222 /* Write out LSE header to JLS file - only supports type 1 and 2 currently */
223 int write_jpegls_extmarker(FILE *out, jpeg_ls_header *jp, int IDtype, char *mapfilename)
224 {
225     int ct=0;
226
227   /* For Type 1 - non default parameters */
228   if (IDtype == LSE_PARAMS)
229   {
230     ct += write_marker(out, LSE);      /* write JPEG-LS extended marker id */
231
232     ct += write_n_bytes(out, 13, 2);      /* marker length */
233     ct += write_n_bytes(out, LSE_PARAMS, 1);  /* ext marker id */
234     ct += write_n_bytes(out, jp->alp-1, 2);    /* MAXVAL */
235     ct += write_n_bytes(out, jp->T1, 2);
236     ct += write_n_bytes(out, jp->T2, 2);
237     ct += write_n_bytes(out, jp->T3, 2);
238     ct += write_n_bytes(out, jp->RES, 2);
239     return ct;
240   }
241
242   /* For Type 2 - Mapping Table */
243   if (IDtype == LSE_MAPTABLE)
244   {
245     unsigned int TID,    /* Table ID */
246            Wt,    /* Width of table entries */
247            MAXTAB,    /* Maximum index of table */
248            length  ;  /* Marker length */
249     int i;
250     FILE* tablefile;
251
252     /* Is only implemented for 8 bpp images in this implementation */
253     if (bpp16==TRUE)
254     {
255       fprintf(stderr, "Sorry, mapping tables are only supported for 8 bpp images in this implementation.\n");
256       exit(1);
257     }
258     
259     /* Open the table file */
260     if ( mapfilename == NULL )
261     {
262       usage();
263       exit(1);
264     }
265     if ( (tablefile=fopen(mapfilename,"rb")) == NULL )
266     {
267       perror(mapfilename);
268       exit(10);
269     }
270
271     /* Assign values to jpeg header */
272     /* Read the table ID */
273     jp->TID = TID = read_n_bytes(tablefile, 1);
274
275     /* Read the width of each entry */
276     jp->Wt = Wt = read_n_bytes(tablefile, 1);
277
278     /* Read the max table index value */
279     MAXTAB = read_n_bytes(tablefile, 4);
280     
281     /* Create the table */
282     jp->TABLE[TID] = (unsigned int *) safecalloc ((MAXTAB+1)*sizeof(unsigned int), 1);
283   
284     for (i=0; i <= MAXTAB; i++)
285     {
286       if (i==200)
287         i=200;
288
289       jp->TABLE[TID][i] = read_n_bytes(tablefile, Wt);
290       if feof(tablefile)
291       {
292         fprintf(stderr,"Error Reading Table File - Premature EOF found.\n");
293         exit(1);
294       }
295     }
296
297
298     length = 5 + (Wt*(MAXTAB+1));
299     
300     /* Write out the header */
301     ct += write_marker(out, LSE);
302     ct += write_n_bytes(out, length, 2);    /* marker length */
303     ct += write_n_bytes(out, LSE_MAPTABLE, 1);  /* LSE marker id */
304     ct += write_n_bytes(out, TID, 1);      /* table id */
305     ct += write_n_bytes(out, Wt, 1);      /* width of table entries in bytes */
306
307     for (i=0; i<=MAXTAB; i++)
308       ct += write_n_bytes(out, jp->TABLE[TID][i], Wt);
309
310
311     return ct;
312   }
313   else
314   {
315     fprintf(stderr, "LSE Parameter %i not defined in this implementation.\n",IDtype);
316     exit(1);
317   }
318
319 }
320
321
322
323
324
325 /* Writes the DRI header to the JLS file */
326 int write_jpegls_restartmarker(FILE *out, jpeg_ls_header *jp)
327 {
328   int ct=0;
329   int Ri;    /* the restart interval (# of MCU's between markers) */
330   int length;  /* the length of the DRI header */
331
332   Ri = jp->restart_interval;
333
334   if (Ri <= 65535)
335     length = 4;
336   else
337     length = 6;
338
339   ct += write_marker(out, DRI);
340   ct += write_n_bytes(out, length, 2);
341   ct += write_n_bytes(out, Ri, 2);
342
343   return ct;
344 }