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.
10 =========================================================
11 THIS SOFTWARE IS BASED ON HP's implementation of jpeg-ls:
12 =========================================================
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 -------------------------------------------------------------------------------
42 /* jpegmark.c --- for JPEG markers
44 * Initial code by Alex Jakulin, Aug. 1995
46 * Modified and optimized: Gadiel Seroussi, October 1995
48 * Color Enhancement: Guillermo Sapiro, August 1996
50 * Modified and added Restart marker and input tables by:
51 * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
62 /* Makes sure a parameter falls within its allowed range */
63 void check_range(int param, char *name, int low, int high)
65 if ( param < low || param > high )
67 fprintf(stderr,"Allowed range for %s is [%d..%d]: got %d\n",
68 name, low, high, param);
73 /* reads n bytes (0 <= n <= 4) from the input stream */
75 unsigned int read_n_bytes(FILE *in, unsigned int n)
81 m = (m << 8) | ((unsigned char)getc(in));
86 * Marker output functions
89 int write_n_bytes(FILE *out, int value, int n)
96 fprintf(stderr,"write_n_bytes: Only 32 bits variables supported.\n");
102 for (l=n-1;l>=0;l--) {
103 if ( putc((value>>(8*l))&0x000000FF,out) == EOF )
106 #else /* little endian */
108 if ( putc((value&0x000000FF),out) == EOF )
119 int write_2_bytes(FILE *out, int value)
121 return write_n_bytes(out,value,2);
126 int write_marker(FILE *out, int marker)
127 /* Write a two-byte marker (just the marker identifier) */
129 write_n_bytes(out, marker, 2);
134 /* Write the frame header to the JLS file */
135 int write_jpegls_frame(FILE *out, jpeg_ls_header *jp)
140 ct += write_marker(out, SOF_LS); /* write JPEG-LS frame marker */
142 check_range(jp->comp, "frame components", 1, 255);
143 marker_len = 8 + 3*jp->comp;
145 ct += write_n_bytes(out, marker_len, 2); /* write marker length */
147 for ( bpp=1; (1L<<bpp)<jp->alp; bpp++ );
149 ct += write_n_bytes(out, bpp, 1); /* write bits/sample */
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);
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 */
159 ct += write_n_bytes(out, jp->comp, 1);
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];
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 */
176 /* Write the Scan header to JLS file */
177 int write_jpegls_scan(FILE *out, jpeg_ls_header *jp)
179 int i, marker_len, ct=0;
181 ct += write_marker(out, SOS); /* write JPEG-LS scan marker */
183 check_range(jp->comp, "scan components", 1, 4);
185 if ( jp->comp == 1 && jp->color_mode != PLANE_INT) {
186 fprintf(stderr,"Interleave for 1 component must be PLANE_INT: got %d\n",
191 if ( jp->comp >1 && jp->color_mode == 0 ) {
192 fprintf(stderr,"Interleave for multi-component scan must be nonzero: got %d\n",
197 marker_len = 6 + 2*jp->comp;
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 */
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 */
208 check_range(jp->NEAR, "NEAR", 0, 255);
209 ct += write_n_bytes(out, jp->NEAR, 1);
211 check_range(jp->color_mode,"INTERLEAVE", 0, 2);
212 ct += write_n_bytes(out, jp->color_mode, 1);
214 check_range(jp->shift, "SHIFT", 0, 15);
215 ct += write_n_bytes(out, jp->shift, 1);
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)
227 /* For Type 1 - non default parameters */
228 if (IDtype == LSE_PARAMS)
230 ct += write_marker(out, LSE); /* write JPEG-LS extended marker id */
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);
242 /* For Type 2 - Mapping Table */
243 if (IDtype == LSE_MAPTABLE)
245 unsigned int TID, /* Table ID */
246 Wt, /* Width of table entries */
247 MAXTAB, /* Maximum index of table */
248 length ; /* Marker length */
252 /* Is only implemented for 8 bpp images in this implementation */
255 fprintf(stderr, "Sorry, mapping tables are only supported for 8 bpp images in this implementation.\n");
259 /* Open the table file */
260 if ( mapfilename == NULL )
265 if ( (tablefile=fopen(mapfilename,"rb")) == NULL )
271 /* Assign values to jpeg header */
272 /* Read the table ID */
273 jp->TID = TID = read_n_bytes(tablefile, 1);
275 /* Read the width of each entry */
276 jp->Wt = Wt = read_n_bytes(tablefile, 1);
278 /* Read the max table index value */
279 MAXTAB = read_n_bytes(tablefile, 4);
281 /* Create the table */
282 jp->TABLE[TID] = (unsigned int *) safecalloc ((MAXTAB+1)*sizeof(unsigned int), 1);
284 for (i=0; i <= MAXTAB; i++)
289 jp->TABLE[TID][i] = read_n_bytes(tablefile, Wt);
292 fprintf(stderr,"Error Reading Table File - Premature EOF found.\n");
298 length = 5 + (Wt*(MAXTAB+1));
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 */
307 for (i=0; i<=MAXTAB; i++)
308 ct += write_n_bytes(out, jp->TABLE[TID][i], Wt);
315 fprintf(stderr, "LSE Parameter %i not defined in this implementation.\n",IDtype);
325 /* Writes the DRI header to the JLS file */
326 int write_jpegls_restartmarker(FILE *out, jpeg_ls_header *jp)
329 int Ri; /* the restart interval (# of MCU's between markers) */
330 int length; /* the length of the DRI header */
332 Ri = jp->restart_interval;
339 ct += write_marker(out, DRI);
340 ct += write_n_bytes(out, length, 2);
341 ct += write_n_bytes(out, Ri, 2);