]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/bitio.c
COMP: Tons of warnings fix...cannot believe how poorly written this was
[gdcm.git] / src / gdcmjpegls / Decoder / bitio.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 /*
43  *
44  * Initial code by Alex Jakulin,  Aug. 1995
45  *
46  * Modified and optimized: Gadiel Seroussi, October 1995
47  *
48  * Modified and added Restart marker and input tables by:
49  * David Cheng-Hsiu Chu, and Ismail R. Ismail march 1999
50  */
51
52 #include "global.h"
53 #include "bitio.h"
54
55
56 extern int zeroLUT[];     /* lookup table to find number of leading zeroes */
57
58 extern FILE *in, *out;
59
60 byte negbuff[BUFSIZE+4];    /* byte I/O buffer, allowing for 4 "negative" 
61                    locations  */
62
63 /*
64  'buff' is defined as 'rawbuff+4' in bitio.h, so that buff[-4]..buff[-1]
65  are well defined. Those locations are used to "return" data to
66  the byte buffer when flushing the input bit buffer .
67  */
68
69
70 int fp;          /* index into byte buffer */
71 int truebufsize;       /* true size of byte buffer ( <= BUFSIZE) */
72 int foundeof;
73
74 int zeroLUT[256];      /* table to find out number of leading zeros */
75
76
77 /* BIT I/O variables */
78 dword reg; /* BIT buffer for input/output */
79 int bits;  /* number of bits free in bit buffer (on output) */
80      /* (number of bits free)-8 in bit buffer (on input)*/
81
82
83 /****************************************************************************
84  *  INPUT ROUTINES
85  *  note: some routines are implemented as preprocessor macros. See bitio.h.
86  ****************************************************************************/
87
88
89 void bufiinit(FILE *fil) {
90     /* argument is ignored */
91     (void)fil;
92     fp = BUFSIZE;
93     truebufsize = 0;
94     foundeof = 0;
95 }
96
97
98 byte fillinbuff(FILE *fil)
99 {
100   int i;
101
102   /* remember 4 last bytes of current buffer (for "undo") */
103   for ( i=-4; i<0; i++ )
104     buff[i] = buff[fp+i];
105         truebufsize = fread(buff, 1, BUFSIZE, fil);
106   if ( truebufsize < BUFSIZE ) 
107   {
108       if ( truebufsize <= 0 ) 
109     {
110       if ( foundeof ) 
111       {  /* second attempt to read past EOF */
112         fprintf(stderr,"*** Premature EOF in compressed file\n");
113         exit(10);
114       }
115       else 
116       {
117         /* One attempt to read past EOF is OK  */
118         foundeof = 1;
119       }
120       }
121       /* fill buffer with zeros */
122       memset(buff+truebufsize, 0, (BUFSIZE-truebufsize)*sizeof(*buff));
123   }
124
125   fp = 1;
126     return buff[0];
127 }
128
129
130 /* Initializes the bit input routines */
131 void bitiinit() {
132         bits = reg = 0;
133         fillbuffer(24);
134 }
135
136
137 /* 
138    Flush the input bit buffer TO A BYTE BOUNDARY. Return unused whole
139    bytes to the byte buffer
140  */
141 void bitiflush()  {
142     int filled,
143   discard, 
144   dbytes,
145   i, k;
146   unsigned int treg;
147     byte *bp;
148
149     filled = 24 - bits;    /* how many bits at the MS part of reg 
150             have unused data. These correspond to 
151             at most filled+2 bits from the input
152             stream, as at most 2 '0' bits might have
153             been dropped by marker processing */
154
155     dbytes = (filled+2)/8;  /* the coorrect number of bytes we need to
156         "unget" is either dbytes or dbytes-1 */
157     /* this solution is more general than what is required here: it 
158        will work correctly even if the end of a scan is not followed
159        by a marker, as will necessarily be the case with the standard JPEG
160        format */
161     for ( ; ; dbytes-- ) 
162   {
163     bp = buff + fp - dbytes;  /* back-in the buffer */
164     treg = k = 0;
165     for ( i=0; i<dbytes; i++ ) 
166     {
167       if ( bp[i-1]==0xff && (bp[i]&0x80)==0 ) 
168       {
169         treg |= bp[i] << (BITBUFSIZE-7-k);
170         k += 7;
171       }
172       else 
173       {
174         treg |= bp[i] << (BITBUFSIZE-8-k);
175         k += 8;
176       }
177     }
178
179     if ( k <= filled )
180     break;
181     }
182     /* check consistency */
183     if ( filled-k > 7 ) {
184       fprintf(stderr,"bitiflush: inconsistent bits=%d filled=%d k=%d\n",bits,filled,k);
185       exit(10);
186     }
187     discard = filled-k;
188     if ( treg != (reg<<discard) ) {
189       fprintf(stderr,"bitiflush: inconsistent bits=%d discard=%d reg=%08x treg=%08x\n",bits, discard, reg, treg);
190       exit(10);
191     }
192     if ( reg & (((1<<discard)-1)<<(BITBUFSIZE-discard)) )
193       fprintf(stderr,"bitiflush: Warning: discarding nonzero bits; reg=%08x bits=%d discard=%d\n",reg,bits,discard);
194
195     fp -= dbytes;  /* do the unget */
196     if ( buff[fp-1]==0xff && buff[fp]==0 ) fp++;
197
198     bits = 0;
199     reg = 0;
200 }
201
202
203
204
205 /* Unpad zeros from byte length */
206 void unpadzeros()
207 {
208   int z;
209
210   z = bits % 8;
211   
212   if (bits!=24)
213   {
214     reg = (reg << z);
215     bits = bits + z;
216   }
217
218
219 }
220   
221   
222
223
224 /* creates the bit counting look-up table. */
225 void createzeroLUT()
226 {
227         int i, j, k, l;
228
229         j = k = 1; l = 8;
230         for (i = 0; i < 256; ++i) {
231                 zeroLUT[i] = l;
232                 --k;
233                 if (k == 0) {
234                         k = j;
235                         --l;
236                         j *= 2;
237                 }
238         }
239 }
240
241
242
243 #ifdef IOFXNS
244 /* if using functions instead of macros */
245 dword
246 getbits(int no) 
247 {
248         register dword temp;
249
250         assert(no > 0 && no <= 18);
251         temp = reg >> (32 - no);
252         fillbuffer(no);
253         return temp;
254 }
255 #  define GETBITS(n)  getbits(n)
256 #else
257 /* see GETBITS(x,n) in bitio.h */
258 #endif