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 # define mygetc(in) (getc(in))
63 # define myfeof(in) (feof(in))
67 check_range(int param, char *name, int low, int high)
69 if ( param < low || param > high ) {
70 fprintf(stderr,"Allowed range for %s is [%d..%d]: got %d\n",
71 name, low, high, param);
78 * Marker output functions
83 write_n_bytes(FILE *out, int value, int n)
90 fprintf(stderr,"write_n_bytes: Only 32 bits variables supported.\n");
96 for (l=n-1;l>=0;l--) {
97 if ( putc((value>>(8*l))&0x000000FF,out) == EOF )
100 #else /* little endian */
102 if ( putc((value&0x000000FF),out) == EOF )
112 write_2_bytes(FILE *out, int value)
114 return write_n_bytes(out,value,2);
118 write_marker(FILE *out, int marker)
119 /* Write a two-byte marker (just the marker identifier) */
121 write_n_bytes(out, marker, 2);
126 write_jpegls_frame(FILE *out, jpeg_ls_header *jp)
131 ct += write_marker(out, SOF_LS); /* write JPEG-LS frame marker */
133 check_range(jp->comp, "frame components", 1, 255);
134 marker_len = 8 + 3*jp->comp;
136 ct += write_n_bytes(out, marker_len, 2); /* write marker length */
138 for ( bpp=1; (1L<<bpp)<jp->alp; bpp++ );
140 ct += write_n_bytes(out, bpp, 1); /* write bits/sample */
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);
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 */
150 ct += write_n_bytes(out, jp->comp, 1);
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];
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 */
167 write_jpegls_scan(FILE *out, jpeg_ls_header *jp)
169 int i, marker_len, ct=0;
171 ct += write_marker(out, SOS); /* write JPEG-LS scan marker */
173 check_range(jp->comp, "scan components", 1, 4);
175 if ( jp->comp == 1 && jp->color_mode != PLANE_INT) {
176 fprintf(stderr,"Interleave for 1 component must be PLANE_INT: got %d\n",
181 if ( jp->comp >1 && jp->color_mode == 0 ) {
182 fprintf(stderr,"Interleave for multi-component scan must be nonzero: got %d\n",
187 marker_len = 6 + 2*jp->comp;
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 */
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 */
198 check_range(jp->NEAR, "NEAR", 0, 255);
199 ct += write_n_bytes(out, jp->NEAR, 1);
201 check_range(jp->color_mode,"INTERLEAVE", 0, 2);
202 ct += write_n_bytes(out, jp->color_mode, 1);
204 check_range(jp->shift, "SHIFT", 0, 15);
205 ct += write_n_bytes(out, jp->shift, 1);
212 write_jpegls_extmarker(FILE *out, jpeg_ls_header *jp)
214 int marker_len, ct=0;
216 ct += write_marker(out, LSE); /* write JPEG-LS extended marker id */
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);
231 * Marker input functions
236 seek_marker(FILE *in, int *mkp )
237 /* Seeks a marker in the input stream. Returns the marker head, or EOF */
240 while ( (c=mygetc(in)) != EOF ) {
243 if ( (c2=mygetc(in)) == EOF )
257 read_n_bytes(FILE *in, int n)
258 /* reads n bytes (0 <= n <= 4) from the input stream */
263 for ( i=0; i<n; i++ )
264 m = (m << 8) | mygetc(in);
270 read_marker(FILE *in, int *mkp)
271 /* reads a marker from the next two bytes in the input stream */
273 unsigned int m, ct=0;
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);
286 read_jpegls_frame(FILE *in, jpeg_ls_header *jp)
287 /* reads the JPEG-LS frame marker (not including marker head) */
296 /* Read Marker Length */
297 marker_len = read_n_bytes(in, 2);
300 /* Read the bits per pixel */
301 bpp = read_n_bytes(in, 1);
304 check_range(bpp,"bpp",2,16);
307 /* Read the rows and columns */
308 jp->rows = read_n_bytes(in, 2);
310 jp->columns = read_n_bytes(in, 2);
313 /* Read component information */
314 comp = read_n_bytes(in, 1);
316 check_range(comp,"COMP",1,255);
319 for ( i=0; i<comp; i++ )
323 cid = read_n_bytes(in, 1);
325 sx = read_n_bytes(in, 1);
327 tq = read_n_bytes(in, 1);
329 check_range(tq,"Tq",0,0);
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;
339 /* Check for errors */
342 fprintf(stderr,"read_jpegls_frame: EOF while reading frame marker\n");
345 if ( marker_len != 8 + 3*comp )
347 fprintf(stderr,"read_jpegls_frame: inconsistent marker length: expected %d, got %d\n",marker_len, 8 + 3*comp);
356 /* reads the JPEG-LS scan marker (not including marker head) */
357 int read_jpegls_scan(FILE *in, jpeg_ls_header *jp)
363 marker_len = read_n_bytes(in, 2);
366 comp = read_n_bytes(in, 1);
368 check_range(comp, "scan components", 1, 4);
372 /* read 2 bytes per component */
373 for ( i=0; i<comp; i++ ) {
376 cid = read_n_bytes(in, 1); /* component identifier */
378 tm = read_n_bytes(in, 1); /* table identifier */
382 fprintf(stderr,"read_jpegls_scan: found nonzero table identifier, not supported\n");
385 jp->comp_ids[i] = cid;
388 jp->NEAR = read_n_bytes(in, 1);
390 check_range(jp->NEAR,"NEAR", 0, 255);
392 jp->color_mode = read_n_bytes(in, 1);
394 check_range(jp->color_mode, "INTERLEAVE", 0, 2);
396 if ( jp->comp == 1 && jp->color_mode != 0 ) {
398 fprintf(stderr,"Interleave for 1 component must be 0: got %d\n",
402 /* ignore interleave value, set to 0 */
406 if ( jp->comp >1 && jp->color_mode == 0 ) {
407 fprintf(stderr,"Interleave for multi-component scan must be nonzero: got %d\n",
413 jp->shift = read_n_bytes(in, 1);
415 check_range(jp->shift, "SHIFT", 0, 15);
418 fprintf(stderr,"read_jpegls_scan: EOF while reading frame marker\n");
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);
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)
435 int marker_len, /* marker length */
436 maxval, /* max value */
437 T1, T2, T3, /* thresholds */
439 int IDtype; /* LSE type */
440 int TID; /* table ID */
441 int Wt; /* width of each table entry */
442 int MAXTAB; /* maximum table index */
446 /* Read marker length */
447 marker_len = read_n_bytes(in, 2); /* marker length */
451 IDtype = read_n_bytes(in, 1);
455 /* For Type 1 - non default parameters */
456 if (IDtype == LSE_PARAMS)
458 if ( marker_len != 13 )
460 fprintf(stderr,"read_jpegls_extmarker: bad marker length %d\n",marker_len);
465 maxval = read_n_bytes(in, 2);
469 /* read thresholds and reset */
470 jp->T1 = read_n_bytes(in, 2);
472 jp->T2 = read_n_bytes(in, 2);
473 jp->T3 = read_n_bytes(in, 2);
474 jp->RES = read_n_bytes(in, 2);
478 fprintf(stderr,"read_jpegls_extmarker: EOF while reading frame marker\n");
485 /* For Type 2 - mapping table */
486 if (IDtype == LSE_MAPTABLE)
489 /* Indicate table used */
493 jp->TID = TID = read_n_bytes(in, 1);
496 /* Read width of table entry */
497 jp->Wt = Wt = read_n_bytes(in, 1);
500 fprintf(stderr, "Width of mapping table entries must be either 1,2 or 3 in this implementation. Sorry!\n");
505 /* Calculate value of MAXTAB */
506 jp->MAXTAB = MAXTAB = ((marker_len - 5) / Wt) - 1;
508 /* Get table entries */
509 jp->TABLE[TID] = (unsigned int *)safecalloc((MAXTAB+1)*sizeof(int), 1);
510 for (i=0; i<=MAXTAB; i++)
512 jp->TABLE[TID][i] = read_n_bytes(in, Wt);
514 ct += ((MAXTAB+1) * Wt);
519 /* Non supported types */
521 fprintf(stderr, "LSE marker type %i not supported in this implementation.\n", IDtype);
530 /* Read DRI restart marker */
531 int read_jpegls_restartmarker(FILE *in, jpeg_ls_header *jp)
534 int marker_len; /* the marker length */
535 int Ri; /* the restart interval */
537 /* Read marker length */
538 marker_len = read_n_bytes(in, 2);
541 /* Read the restart interval */
542 Ri = read_n_bytes(in, marker_len - 2);
543 ct += (marker_len - 2);
545 jp->restart_interval = Ri;