2 * Copyright (c) 1999-2000 Image Power, Inc. and the University of
4 * Copyright (c) 2001-2002 Michael David Adams.
8 /* __START_OF_JASPER_LICENSE__
10 * JasPer License Version 2.0
12 * Copyright (c) 1999-2000 Image Power, Inc.
13 * Copyright (c) 1999-2000 The University of British Columbia
14 * Copyright (c) 2001-2003 Michael David Adams
16 * All rights reserved.
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:
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.
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
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.
61 * __END_OF_JASPER_LICENSE__
67 * $Id: jpc_bs.h,v 1.1 2005/05/22 18:33:03 malaterre Exp $
73 /******************************************************************************\
75 \******************************************************************************/
79 #include "jasper/jas_types.h"
80 #include "jasper/jas_stream.h"
82 /******************************************************************************\
84 \******************************************************************************/
87 * Bit stream open mode flags.
90 /* Bit stream open for reading. */
91 #define JPC_BITSTREAM_READ 0x01
92 /* Bit stream open for writing. */
93 #define JPC_BITSTREAM_WRITE 0x02
99 /* Do not close underlying character stream. */
100 #define JPC_BITSTREAM_NOCLOSE 0x01
101 /* End of file has been reached while reading. */
102 #define JPC_BITSTREAM_EOF 0x02
103 /* An I/O error has occured. */
104 #define JPC_BITSTREAM_ERR 0x04
106 /******************************************************************************\
108 \******************************************************************************/
110 /* Bit stream class. */
114 /* Some miscellaneous flags. */
117 /* The input/output buffer. */
120 /* The number of bits remaining in the byte being read/written. */
123 /* The underlying stream associated with this bit stream. */
124 jas_stream_t *stream_;
126 /* The mode in which this bit stream was opened. */
131 /******************************************************************************\
132 * Functions/macros for opening and closing bit streams..
133 \******************************************************************************/
135 /* Open a stream as a bit stream. */
136 jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, char *mode);
138 /* Close a bit stream. */
139 int jpc_bitstream_close(jpc_bitstream_t *bitstream);
141 /******************************************************************************\
142 * Functions/macros for reading from and writing to bit streams..
143 \******************************************************************************/
145 /* Read a bit from a bit stream. */
147 #define jpc_bitstream_getbit(bitstream) \
148 jpc_bitstream_getbit_func(bitstream)
150 #define jpc_bitstream_getbit(bitstream) \
151 jpc_bitstream_getbit_macro(bitstream)
154 /* Write a bit to a bit stream. */
156 #define jpc_bitstream_putbit(bitstream, v) \
157 jpc_bitstream_putbit_func(bitstream, v)
159 #define jpc_bitstream_putbit(bitstream, v) \
160 jpc_bitstream_putbit_macro(bitstream, v)
163 /* Read one or more bits from a bit stream. */
164 long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n);
166 /* Write one or more bits to a bit stream. */
167 int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v);
169 /******************************************************************************\
170 * Functions/macros for flushing and aligning bit streams.
171 \******************************************************************************/
173 /* Align the current position within the bit stream to the next byte
175 int jpc_bitstream_align(jpc_bitstream_t *bitstream);
177 /* Align the current position in the bit stream with the next byte boundary,
178 ensuring that certain bits consumed in the process match a particular
180 int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask,
183 /* Align the current position in the bit stream with the next byte boundary,
184 writing bits from the specified pattern (if necessary) in the process. */
185 int jpc_bitstream_outalign(jpc_bitstream_t *bitstream, int filldata);
187 /* Check if a bit stream needs alignment. */
188 int jpc_bitstream_needalign(jpc_bitstream_t *bitstream);
190 /* How many additional bytes would be output if the bit stream was aligned? */
191 int jpc_bitstream_pending(jpc_bitstream_t *bitstream);
193 /******************************************************************************\
194 * Functions/macros for querying state information for bit streams.
195 \******************************************************************************/
197 /* Has EOF been encountered on a bit stream? */
198 #define jpc_bitstream_eof(bitstream) \
199 ((bitstream)->flags_ & JPC_BITSTREAM_EOF)
201 /******************************************************************************\
203 \******************************************************************************/
205 /* DO NOT DIRECTLY INVOKE ANY OF THE MACROS OR FUNCTIONS BELOW. THEY ARE
206 FOR INTERNAL USE ONLY. */
208 int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream);
210 int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int v);
212 int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream);
214 #define jpc_bitstream_getbit_macro(bitstream) \
215 (assert((bitstream)->openmode_ & JPC_BITSTREAM_READ), \
216 (--(bitstream)->cnt_ >= 0) ? \
217 ((int)(((bitstream)->buf_ >> (bitstream)->cnt_) & 1)) : \
218 jpc_bitstream_fillbuf(bitstream))
220 #define jpc_bitstream_putbit_macro(bitstream, bit) \
221 (assert((bitstream)->openmode_ & JPC_BITSTREAM_WRITE), \
222 (--(bitstream)->cnt_ < 0) ? \
223 ((bitstream)->buf_ = ((bitstream)->buf_ << 8) & 0xffff, \
224 (bitstream)->cnt_ = ((bitstream)->buf_ == 0xff00) ? 6 : 7, \
225 (bitstream)->buf_ |= ((bit) & 1) << (bitstream)->cnt_, \
226 (jas_stream_putc((bitstream)->stream_, (bitstream)->buf_ >> 8) == EOF) \
227 ? (EOF) : ((bit) & 1)) : \
228 ((bitstream)->buf_ |= ((bit) & 1) << (bitstream)->cnt_, \