Home | History | Annotate | Download | only in libjasper
      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: jpc_bs.h,v 1.2 2008-05-26 09:40:52 vp153 Exp $
     68  */
     69 
     70 #ifndef JPC_BS_H
     71 #define JPC_BS_H
     72 
     73 /******************************************************************************\
     74 * Includes.
     75 \******************************************************************************/
     76 
     77 #include <stdio.h>
     78 
     79 #include "jasper/jas_types.h"
     80 #include "jasper/jas_stream.h"
     81 
     82 /******************************************************************************\
     83 * Constants.
     84 \******************************************************************************/
     85 
     86 /*
     87  * Bit stream open mode flags.
     88  */
     89 
     90 /* Bit stream open for reading. */
     91 #define	JPC_BITSTREAM_READ	0x01
     92 /* Bit stream open for writing. */
     93 #define	JPC_BITSTREAM_WRITE	0x02
     94 
     95 /*
     96  * Bit stream flags.
     97  */
     98 
     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
    105 
    106 /******************************************************************************\
    107 * Types.
    108 \******************************************************************************/
    109 
    110 /* Bit stream class. */
    111 
    112 typedef struct {
    113 
    114     /* Some miscellaneous flags. */
    115     int flags_;
    116 
    117     /* The input/output buffer. */
    118     uint_fast16_t buf_;
    119 
    120     /* The number of bits remaining in the byte being read/written. */
    121     int cnt_;
    122 
    123     /* The underlying stream associated with this bit stream. */
    124     jas_stream_t *stream_;
    125 
    126     /* The mode in which this bit stream was opened. */
    127     int openmode_;
    128 
    129 } jpc_bitstream_t;
    130 
    131 /******************************************************************************\
    132 * Functions/macros for opening and closing bit streams..
    133 \******************************************************************************/
    134 
    135 /* Open a stream as a bit stream. */
    136 jpc_bitstream_t *jpc_bitstream_sopen(jas_stream_t *stream, char *mode);
    137 
    138 /* Close a bit stream. */
    139 int jpc_bitstream_close(jpc_bitstream_t *bitstream);
    140 
    141 /******************************************************************************\
    142 * Functions/macros for reading from and writing to bit streams..
    143 \******************************************************************************/
    144 
    145 /* Read a bit from a bit stream. */
    146 #if defined(DEBUG)
    147 #define	jpc_bitstream_getbit(bitstream) \
    148     jpc_bitstream_getbit_func(bitstream)
    149 #else
    150 #define jpc_bitstream_getbit(bitstream) \
    151     jpc_bitstream_getbit_macro(bitstream)
    152 #endif
    153 
    154 /* Write a bit to a bit stream. */
    155 #if defined(DEBUG)
    156 #define	jpc_bitstream_putbit(bitstream, v) \
    157     jpc_bitstream_putbit_func(bitstream, v)
    158 #else
    159 #define	jpc_bitstream_putbit(bitstream, v) \
    160     jpc_bitstream_putbit_macro(bitstream, v)
    161 #endif
    162 
    163 /* Read one or more bits from a bit stream. */
    164 long jpc_bitstream_getbits(jpc_bitstream_t *bitstream, int n);
    165 
    166 /* Write one or more bits to a bit stream. */
    167 int jpc_bitstream_putbits(jpc_bitstream_t *bitstream, int n, long v);
    168 
    169 /******************************************************************************\
    170 * Functions/macros for flushing and aligning bit streams.
    171 \******************************************************************************/
    172 
    173 /* Align the current position within the bit stream to the next byte
    174   boundary. */
    175 int jpc_bitstream_align(jpc_bitstream_t *bitstream);
    176 
    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
    179   pattern. */
    180 int jpc_bitstream_inalign(jpc_bitstream_t *bitstream, int fillmask,
    181   int filldata);
    182 
    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);
    186 
    187 /* Check if a bit stream needs alignment. */
    188 int jpc_bitstream_needalign(jpc_bitstream_t *bitstream);
    189 
    190 /* How many additional bytes would be output if the bit stream was aligned? */
    191 int jpc_bitstream_pending(jpc_bitstream_t *bitstream);
    192 
    193 /******************************************************************************\
    194 * Functions/macros for querying state information for bit streams.
    195 \******************************************************************************/
    196 
    197 /* Has EOF been encountered on a bit stream? */
    198 #define jpc_bitstream_eof(bitstream) \
    199     ((bitstream)->flags_ & JPC_BITSTREAM_EOF)
    200 
    201 /******************************************************************************\
    202 * Internals.
    203 \******************************************************************************/
    204 
    205 /* DO NOT DIRECTLY INVOKE ANY OF THE MACROS OR FUNCTIONS BELOW.  THEY ARE
    206   FOR INTERNAL USE ONLY. */
    207 
    208 int jpc_bitstream_getbit_func(jpc_bitstream_t *bitstream);
    209 
    210 int jpc_bitstream_putbit_func(jpc_bitstream_t *bitstream, int v);
    211 
    212 int jpc_bitstream_fillbuf(jpc_bitstream_t *bitstream);
    213 
    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))
    219 
    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_, \
    229       (bit) & 1))
    230 
    231 #endif
    232