jpc_bs.h 8.6 KB

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