]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/melcode.c
UserDefinedFileIdentifier is now more human readable
[gdcm.git] / src / gdcmjpegls / Decoder / melcode.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-1997.
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 /* melcode.c --- for processing in run mode
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 "bitio.h"
57
58 #define MELCSTATES  32  /* number of melcode states */
59
60 static int J[MELCSTATES] = {
61   0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,5,5,6,6,7,
62   7,8,9,10,11,12,13,14,15 
63 };
64
65
66 static int  melcstate[MAX_COMPONENTS],    /* index to the state array */
67     melclen[MAX_COMPONENTS];      /* contents of the state array location
68                     indexed by melcstate: the "expected"
69              run length is 2^melclen, shorter runs are
70              encoded by a 1 followed by the run length
71              in binary representation, wit a fixed length
72              of melclen bits */
73
74 static unsigned long melcorder[MAX_COMPONENTS];  /* 2^ melclen */
75
76
77 void init_process_run(int maxrun)    /* maxrun is ignoreed when using MELCODE,
78           kept here for function compatibility */            
79 {
80   int  n_c;
81   (void)maxrun;
82
83   for (n_c=0;n_c<components;n_c++)
84   {
85     melcstate[n_c] = 0;
86     melclen[n_c] = J[0];
87     melcorder[n_c] = 1<<melclen[n_c];
88   }
89 }
90
91
92
93
94 /* decoding routine: reads bits from the input and returns a run length. */
95 /* argument is the number of pixels left to  end-of-line (bound on run length) */
96 int process_run_dec(int lineleft, int color)  
97 {
98   int runlen = 0;
99     
100   do {
101     register int temp, hits;
102     temp = zeroLUT[(byte)(~(reg >> 24))];   /* number of leading ones in the
103                  input stream, up to 8 */
104     for ( hits = 1; hits<=temp; hits++ ) 
105     {
106       runlen += melcorder[color];
107       if ( runlen >= lineleft )
108       { /* reached end-of-line */
109         if ( runlen==lineleft && melcstate[color] < MELCSTATES ) 
110         {
111           melclen[color] = J[++melcstate[color]];
112           melcorder[color] = (1L<<melclen[color]);
113         }
114         FILLBUFFER(hits); /* actual # of 1's consumed */
115         return lineleft; 
116       }
117       if ( melcstate[color] < MELCSTATES ) 
118       {
119         melclen[color] = J[++melcstate[color]];
120         melcorder[color] = (1L<<melclen[color]);
121       }
122     }  
123     if (temp != 8) 
124     {
125       FILLBUFFER(temp + 1);  /* consume the leading
126               0 of the remainder encoding */
127       break;
128         }
129         FILLBUFFER(8);
130   } while ( 1 );
131
132   /* read the length of the remainder */
133   if ( melclen[color] ) 
134   {
135     register int temp;
136     GETBITS(temp, melclen[color]);  /*** GETBITS is a macro, not a function */
137     runlen += temp;
138   }
139   limit_reduce = melclen[color]+1;
140
141   /* adjust melcoder parameters */
142   if ( melcstate[color] ) 
143   {
144     melclen[color] = J[--melcstate[color]];
145     melcorder[color] = (1L<<melclen[color]);
146   }
147
148   return runlen;
149 }
150
151
152
153 void
154 close_process_run()
155 {
156 /* retained for compatibility with ranked runs */
157 }