]> Creatis software - gdcm.git/blob - src/gdcmjpegls/Decoder/bitio.h
BUG: map<>::mapped_type is not part of the STL. Should fix issue on VS* compiler
[gdcm.git] / src / gdcmjpegls / Decoder / bitio.h
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 /* bitio.h --- for I/O routines
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 #ifndef BITIO_H
55 #define BITIO_H
56
57 #include "global.h"
58
59 /* BYTE I/O variables */
60 #define BUFSIZE ((16*1024)-4) /* Size of input BYTE buffer */
61 extern int fp;                /* index into byte  buffer */
62 extern int truebufsize;       /* true size of byte buffer ( <= BUFSIZE) */
63 extern byte negbuff[];        /* the buffer */
64 #define buff (negbuff+4)
65
66
67 /* BIT I/O variables */
68 extern dword reg;         /* BIT buffer for input/output */
69 extern int bits;          /* number of bits free in bit buffer (on output) */
70                           /* (number of bits free)-8 in bit buffer (on input)*/
71 #define BITBUFSIZE (8*sizeof(reg))
72
73 extern int zeroLUT[];     /* lookup table to find number of leading zeroes */
74
75 extern FILE *in, *out;
76
77
78 #define mygetc(fil) ((fp >= BUFSIZE) ? (fillinbuff(fil)) : (buff[fp++]))
79 #define myungetc(x,fil) (buff[--fp]=x)
80 #define myfeof(fil) ((fp >= truebufsize) && feof(fil))
81
82
83 extern void bufiinit(FILE *fil);
84
85 extern byte fillinbuff(FILE *fil);
86
87
88
89 /* loads more data in the input buffer (inline code )*/
90 #define fillbuffer(no) {                        \
91         byte x;                                 \
92                                                 \
93   assert(no+bits <= 24);      \
94         reg <<= no;                             \
95         bits += no;                             \
96         while (bits >= 0) {                     \
97                 x = mygetc(in);                 \
98     if ( x == 0xff ) {              \
99         if ( bits < 8 ) {           \
100       myungetc(0xff, in);     \
101       break;                  \
102         }        \
103         else {      \
104       x = mygetc(in);    \
105       if ( !(x & 0x80) )  { /* non-marker: drop 0 */  \
106           reg |= (0xff << bits) | ((x & 0x7f)<<(bits-7));\
107           bits -= 15;    \
108       }      \
109       else {      \
110           /* marker: hope we know what we're doing */\
111           /* the "1" bit following ff is NOT dropped */\
112           reg |= (0xff << bits) | (x <<(bits-8));\
113           bits -= 16;    \
114       }      \
115       continue;    \
116         }        \
117     }        \
118                 reg |= x << bits;               \
119                 bits -= 8;                      \
120         }                                       \
121 }
122
123 #define FILLBUFFER(n)  fillbuffer(n)
124
125
126 /* Initializes the bit input routines */
127 extern void bitiinit();
128
129
130 /* creates the bit counting look-up table. */
131 extern void createzeroLUT();
132
133
134 #ifdef IOFXNS
135 extern dword getbits(int no);
136 #  define GETBITS(n)  getbits(n)
137 #else
138 #  define GETBITS(x, n)\
139  { x = reg >> (32 - (n));\
140    FILLBUFFER(n);\
141  }
142 #endif
143
144
145
146 #endif /* BITIO_H */