JasPer  2.0.33
jpc_bs.h
1 /*
2  * Copyright (c) 1999-2000 Image Power, Inc. and the University of
3  * British Columbia.
4  * Copyright (c) 2001-2002 Michael David Adams.
5  * All rights reserved.
6  */
7 
8 /* __START_OF_JASPER_LICENSE__
9  *
10  * JasPer License Version 2.0
11  *
12  * Copyright (c) 2001-2006 Michael David Adams
13  * Copyright (c) 1999-2000 Image Power, Inc.
14  * Copyright (c) 1999-2000 The University of British Columbia
15  *
16  * All rights reserved.
17  *
18  * Permission is hereby granted, free of charge, to any person (the
19  * "User") obtaining a copy of this software and associated documentation
20  * files (the "Software"), to deal in the Software without restriction,
21  * including without limitation the rights to use, copy, modify, merge,
22  * publish, distribute, and/or sell copies of the Software, and to permit
23  * persons to whom the Software is furnished to do so, subject to the
24  * following conditions:
25  *
26  * 1. The above copyright notices and this permission notice (which
27  * includes the disclaimer below) shall be included in all copies or
28  * substantial portions of the Software.
29  *
30  * 2. The name of a copyright holder shall not be used to endorse or
31  * promote products derived from the Software without specific prior
32  * written permission.
33  *
34  * THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS
35  * LICENSE. NO USE OF THE SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER
36  * THIS DISCLAIMER. THE SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
37  * "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
38  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
39  * PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO
40  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
41  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
42  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
43  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
44  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. NO ASSURANCES ARE
45  * PROVIDED BY THE COPYRIGHT HOLDERS THAT THE SOFTWARE DOES NOT INFRINGE
46  * THE PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OF ANY OTHER ENTITY.
47  * EACH COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY TO THE USER FOR CLAIMS
48  * BROUGHT BY ANY OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL
49  * PROPERTY RIGHTS OR OTHERWISE. AS A CONDITION TO EXERCISING THE RIGHTS
50  * GRANTED HEREUNDER, EACH USER HEREBY ASSUMES SOLE RESPONSIBILITY TO SECURE
51  * ANY OTHER INTELLECTUAL PROPERTY RIGHTS NEEDED, IF ANY. THE SOFTWARE
52  * IS NOT FAULT-TOLERANT AND IS NOT INTENDED FOR USE IN MISSION-CRITICAL
53  * SYSTEMS, SUCH AS THOSE USED IN THE OPERATION OF NUCLEAR FACILITIES,
54  * AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL
55  * SYSTEMS, DIRECT LIFE SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH
56  * THE FAILURE OF THE SOFTWARE OR SYSTEM COULD LEAD DIRECTLY TO DEATH,
57  * PERSONAL INJURY, OR SEVERE PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH
58  * RISK ACTIVITIES"). THE COPYRIGHT HOLDERS SPECIFICALLY DISCLAIM ANY
59  * EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR HIGH RISK ACTIVITIES.
60  *
61  * __END_OF_JASPER_LICENSE__
62  */
63 
64 /*
65  * Bit Stream Class
66  *
67  * $Id$
68  */
69 
70 #ifndef JPC_BS_H
71 #define JPC_BS_H
72 
73 /******************************************************************************\
74 * Includes.
75 \******************************************************************************/
76 
77 #include "jasper/jas_types.h"
78 #include "jasper/jas_stream.h"
79 
80 #include <assert.h>
81 #include <stdio.h>
82 
83 /******************************************************************************\
84 * Constants.
85 \******************************************************************************/
86 
87 /*
88  * Bit stream open mode flags.
89  */
90 
91 /* Bit stream open for reading. */
92 #define JPC_BITSTREAM_READ 0x01
93 /* Bit stream open for writing. */
94 #define JPC_BITSTREAM_WRITE 0x02
95 
96 /*
97  * Bit stream flags.
98  */
99 
100 /* Do not close underlying character stream. */
101 #define JPC_BITSTREAM_NOCLOSE 0x01
102 /* End of file has been reached while reading. */
103 #define JPC_BITSTREAM_EOF 0x02
104 /* An I/O error has occured. */
105 #define JPC_BITSTREAM_ERR 0x04
106 
107 /******************************************************************************\
108 * Types.
109 \******************************************************************************/
110 
111 /* Bit stream class. */
112 
113 typedef struct {
114 
115  /* Some miscellaneous flags. */
116  int flags_;
117 
118  /* The input/output buffer. */
119  uint_fast16_t buf_;
120 
121  /* The number of bits remaining in the byte being read/written. */
122  int cnt_;
123 
124  /* The underlying stream associated with this bit stream. */
125  jas_stream_t *stream_;
126 
127  /* The mode in which this bit stream was opened. */
128  int openmode_;
129 
130 } jpc_bitstream_t;
131 
132 /******************************************************************************\
133 * Functions/macros for opening and closing bit streams..
134 \******************************************************************************/
135 
136 /* Open a stream as a bit stream. */
137 jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, const char *mode);
138 
139 /* Close a bit stream. */
140 int jpc_bitstream_close(jpc_bitstream_t *bitstream);
141 
142 /******************************************************************************\
143 * Functions/macros for reading from and writing to bit streams..
144 \******************************************************************************/
145 
146 /* Read a bit from a bit stream. */
147 #ifndef NDEBUG
148 #define jpc_bitstream_getbit(bitstream) \
149  jpc_bitstream_getbit_func(bitstream)
150 #else
151 #define jpc_bitstream_getbit(bitstream) \
152  jpc_bitstream_getbit_macro(bitstream)
153 #endif
154 
155 /* Write a bit to a bit stream. */
156 #ifndef NDEBUG
157 #define jpc_bitstream_putbit(bitstream, v) \
158  jpc_bitstream_putbit_func(bitstream, v)
159 #else
160 #define jpc_bitstream_putbit(bitstream, v) \
161  jpc_bitstream_putbit_macro(bitstream, v)
162 #endif
163 
164 /* Read one or more bits from a bit stream. */
165 long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n);
166 
167 /* Write one or more bits to a bit stream. */
168 int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v);
169 
170 /******************************************************************************\
171 * Functions/macros for flushing and aligning bit streams.
172 \******************************************************************************/
173 
174 /* Align the current position within the bit stream to the next byte
175  boundary. */
176 int jpc_bitstream_align(jpc_bitstream_t *bitstream);
177 
178 /* Align the current position in the bit stream with the next byte boundary,
179  ensuring that certain bits consumed in the process match a particular
180  pattern. */
181 int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask,
182  int filldata);
183 
184 /* Align the current position in the bit stream with the next byte boundary,
185  writing bits from the specified pattern (if necessary) in the process. */
186 int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata);
187 
188 /* Check if a bit stream needs alignment. */
189 JAS_ATTRIBUTE_PURE
190 int jpc_bitstream_needalign(const jpc_bitstream_t *bitstream);
191 
192 /* How many additional bytes would be output if the bit stream was aligned? */
193 JAS_ATTRIBUTE_PURE
194 int jpc_bitstream_pending(const jpc_bitstream_t *bitstream);
195 
196 /******************************************************************************\
197 * Functions/macros for querying state information for bit streams.
198 \******************************************************************************/
199 
200 /* Has EOF been encountered on a bit stream? */
201 #define jpc_bitstream_eof(bitstream) \
202  ((bitstream)->flags_ & JPC_BITSTREAM_EOF)
203 
204 /******************************************************************************\
205 * Internals.
206 \******************************************************************************/
207 
208 /* DO NOT DIRECTLY INVOKE ANY OF THE MACROS OR FUNCTIONS BELOW. THEY ARE
209  FOR INTERNAL USE ONLY. */
210 
211 int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream);
212 
213 int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int v);
214 
215 int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream);
216 
217 #define jpc_bitstream_getbit_macro(bitstream) \
218  (assert((bitstream)->openmode_ & JPC_BITSTREAM_READ), \
219  (--(bitstream)->cnt_ >= 0) ? \
220  ((int)(((bitstream)->buf_ >> (bitstream)->cnt_) & 1)) : \
221  jpc_bitstream_fillbuf(bitstream))
222 
223 #define jpc_bitstream_putbit_macro(bitstream, bit) \
224  (assert((bitstream)->openmode_ & JPC_BITSTREAM_WRITE), \
225  (--(bitstream)->cnt_ < 0) ? \
226  ((bitstream)->buf_ = ((bitstream)->buf_ << 8) & 0xffff, \
227  (bitstream)->cnt_ = ((bitstream)->buf_ == 0xff00) ? 6 : 7, \
228  (bitstream)->buf_ |= ((bit) & 1) << (bitstream)->cnt_, \
229  (jas_stream_putc((bitstream)->stream_, (bitstream)->buf_ >> 8) == EOF) \
230  ? (EOF) : ((bit) & 1)) : \
231  ((bitstream)->buf_ |= ((bit) & 1) << (bitstream)->cnt_, \
232  (bit) & 1))
233 
234 #endif
Primitive Types.
I/O Stream Class.