001: /*
002: * $RCSfile: PktHeaderBitReader.java,v $
003: * $Revision: 1.1 $
004: * $Date: 2005/02/11 05:02:01 $
005: * $State: Exp $
006: *
007: * Class: PktHeaderBitReader
008: *
009: * Description: Bit based reader for packet headers
010: *
011: *
012: *
013: * COPYRIGHT:
014: *
015: * This software module was originally developed by Raphaël Grosbois and
016: * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
017: * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
018: * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
019: * Centre France S.A) in the course of development of the JPEG2000
020: * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
021: * software module is an implementation of a part of the JPEG 2000
022: * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
023: * Systems AB and Canon Research Centre France S.A (collectively JJ2000
024: * Partners) agree not to assert against ISO/IEC and users of the JPEG
025: * 2000 Standard (Users) any of their rights under the copyright, not
026: * including other intellectual property rights, for this software module
027: * with respect to the usage by ISO/IEC and Users of this software module
028: * or modifications thereof for use in hardware or software products
029: * claiming conformance to the JPEG 2000 Standard. Those intending to use
030: * this software module in hardware or software products are advised that
031: * their use may infringe existing patents. The original developers of
032: * this software module, JJ2000 Partners and ISO/IEC assume no liability
033: * for use of this software module or modifications thereof. No license
034: * or right to this software module is granted for non JPEG 2000 Standard
035: * conforming products. JJ2000 Partners have full right to use this
036: * software module for his/her own purpose, assign or donate this
037: * software module to any third party and to inhibit third parties from
038: * using this software module for non JPEG 2000 Standard conforming
039: * products. This copyright notice must be included in all copies or
040: * derivative works of this software module.
041: *
042: * Copyright (c) 1999/2000 JJ2000 Partners.
043: *
044: *
045: *
046: */
047:
048: package jj2000.j2k.codestream.reader;
049:
050: import jj2000.j2k.io.*;
051: import java.io.*;
052:
053: /**
054: * This class provides a bit based reading facility from a byte based one,
055: * applying the bit unstuffing procedure as required by the packet headers.
056: *
057: */
058: class PktHeaderBitReader {
059:
060: /** The byte based source of data */
061: RandomAccessIO in;
062:
063: /** The byte array that is the source of data if the PktHeaderBitReader
064: * is instantiated with a buffer instead of a RandomAccessIO*/
065: ByteArrayInputStream bais;
066:
067: /** Flag indicating whether the data should be read from the buffer */
068: boolean usebais;
069:
070: /** The current bit buffer */
071: int bbuf;
072:
073: /** The position of the next bit to read in the bit buffer (0 means
074: * empty, 8 full) */
075: int bpos;
076:
077: /** The next bit buffer, if bit stuffing occurred (i.e. current bit
078: * buffer holds 0xFF) */
079: int nextbbuf;
080:
081: /**
082: * Instantiates a 'PktHeaderBitReader' that gets the byte data from the
083: * given source.
084: *
085: * @param in The source of byte data
086: *
087: * */
088: PktHeaderBitReader(RandomAccessIO in) {
089: this .in = in;
090: usebais = false;
091: }
092:
093: /**
094: * Instantiates a 'PktHeaderBitReader' that gets the byte data from the
095: * given source.
096: *
097: * @param bais The source of byte data
098: *
099: * */
100: PktHeaderBitReader(ByteArrayInputStream bais) {
101: this .bais = bais;
102: usebais = true;
103: }
104:
105: /**
106: * Reads a single bit from the input.
107: *
108: * @return The read bit (0 or 1)
109: *
110: * @exception IOException If an I/O error occurred
111: *
112: * @exception EOFException If teh end of file has been reached
113: *
114: * */
115: final int readBit() throws IOException {
116: if (bpos == 0) { // Is bit buffer empty?
117: if (bbuf != 0xFF) { // No bit stuffing
118: if (usebais)
119: bbuf = bais.read();
120: else
121: bbuf = in.read();
122: bpos = 8;
123: if (bbuf == 0xFF) { // If new bit stuffing get next byte
124: if (usebais)
125: nextbbuf = bais.read();
126: else
127: nextbbuf = in.read();
128: }
129: } else { // We had bit stuffing, nextbuf can not be 0xFF
130: bbuf = nextbbuf;
131: bpos = 7;
132: }
133: }
134: return (bbuf >> --bpos) & 0x01;
135: }
136:
137: /**
138: * Reads a specified number of bits and returns them in a single
139: * integer. The bits are returned in the 'n' least significant bits of the
140: * returned integer. The maximum number of bits that can be read is 31.
141: *
142: * @param n The number of bits to read
143: *
144: * @return The read bits, packed in the 'n' LSBs.
145: * @exception IOException If an I/O error occurred
146: *
147: * @exception EOFException If teh end of file has been reached
148: *
149: * */
150: final int readBits(int n) throws IOException {
151: int bits; // The read bits
152:
153: // Can we get all bits from the bit buffer?
154: if (n <= bpos) {
155: return (bbuf >> (bpos -= n)) & ((1 << n) - 1);
156: } else {
157: // NOTE: The implementation need not be recursive but the not
158: // recursive one exploits a bug in the IBM x86 JIT and caused
159: // incorrect decoding (Diego Santa Cruz).
160: bits = 0;
161: do {
162: // Get all the bits we can from the bit buffer
163: bits <<= bpos;
164: n -= bpos;
165: bits |= readBits(bpos);
166: // Get an extra bit to load next byte (here bpos is 0)
167: if (bbuf != 0xFF) { // No bit stuffing
168: if (usebais)
169: bbuf = bais.read();
170: else
171: bbuf = in.read();
172:
173: bpos = 8;
174: if (bbuf == 0xFF) { // If new bit stuffing get next byte
175: if (usebais)
176: nextbbuf = bais.read();
177: else
178: nextbbuf = in.read();
179: }
180: } else { // We had bit stuffing, nextbuf can not be 0xFF
181: bbuf = nextbbuf;
182: bpos = 7;
183: }
184: } while (n > bpos);
185: // Get the last bits, if any
186: bits <<= n;
187: bits |= (bbuf >> (bpos -= n)) & ((1 << n) - 1);
188: // Return result
189: return bits;
190: }
191: }
192:
193: /**
194: * Synchronizes this object with the underlying byte based input. It
195: * discards and buffered bits and gets ready to read bits from the current
196: * position in the underlying byte based input.
197: *
198: * <P>This method should always be called when some data has been read
199: * directly from the underlying byte based input since the last call to
200: * 'readBits()' or 'readBit()' before a new call to any of those methods.
201: *
202: * */
203: void sync() {
204: bbuf = 0;
205: bpos = 0;
206: }
207:
208: /**
209: * Sets the underlying byte based input to the given object. This method
210: * discards any currently buffered bits and gets ready to start reading
211: * bits from 'in'.
212: *
213: * <P>This method is equivalent to creating a new 'PktHeaderBitReader'
214: * object.
215: *
216: * @param in The source of byte data
217: *
218: * */
219: void setInput(RandomAccessIO in) {
220: this .in = in;
221: bbuf = 0;
222: bpos = 0;
223: }
224:
225: /**
226: * Sets the underlying byte based input to the given object. This method
227: * discards any currently buffered bits and gets ready to start reading
228: * bits from 'in'.
229: *
230: * <P>This method is equivalent to creating a new 'PktHeaderBitReader'
231: * object.
232: *
233: * @param bais The source of byte data
234: *
235: * */
236: void setInput(ByteArrayInputStream bais) {
237: this .bais = bais;
238: bbuf = 0;
239: bpos = 0;
240: }
241: }
|