Home | History | Annotate | Download | only in highgui
      1 /*M///////////////////////////////////////////////////////////////////////////////////////
      2 //
      3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
      4 //
      5 //  By downloading, copying, installing or using the software you agree to this license.
      6 //  If you do not agree to this license, do not download, install,
      7 //  copy or use the software.
      8 //
      9 //
     10 //                        Intel License Agreement
     11 //                For Open Source Computer Vision Library
     12 //
     13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
     14 // Third party copyrights are property of their respective owners.
     15 //
     16 // Redistribution and use in source and binary forms, with or without modification,
     17 // are permitted provided that the following conditions are met:
     18 //
     19 //   * Redistribution's of source code must retain the above copyright notice,
     20 //     this list of conditions and the following disclaimer.
     21 //
     22 //   * Redistribution's in binary form must reproduce the above copyright notice,
     23 //     this list of conditions and the following disclaimer in the documentation
     24 //     and/or other materials provided with the distribution.
     25 //
     26 //   * The name of Intel Corporation may not be used to endorse or promote products
     27 //     derived from this software without specific prior written permission.
     28 //
     29 // This software is provided by the copyright holders and contributors "as is" and
     30 // any express or implied warranties, including, but not limited to, the implied
     31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     32 // In no event shall the Intel Corporation or contributors be liable for any direct,
     33 // indirect, incidental, special, exemplary, or consequential damages
     34 // (including, but not limited to, procurement of substitute goods or services;
     35 // loss of use, data, or profits; or business interruption) however caused
     36 // and on any theory of liability, whether in contract, strict liability,
     37 // or tort (including negligence or otherwise) arising in any way out of
     38 // the use of this software, even if advised of the possibility of such damage.
     39 //
     40 //M*/
     41 
     42 #ifndef _BITSTRM_H_
     43 #define _BITSTRM_H_
     44 
     45 #include <stdio.h>
     46 #include <setjmp.h>
     47 
     48 #if _MSC_VER >= 1200
     49     #pragma warning( disable: 4711 4324 )
     50 #endif
     51 
     52 #define  RBS_THROW_EOS    -123  /* <end of stream> exception code */
     53 #define  RBS_THROW_FORB   -124  /* <forrbidden huffman code> exception code */
     54 #define  RBS_HUFF_FORB    2047  /* forrbidden huffman code "value" */
     55 
     56 typedef unsigned char uchar;
     57 typedef unsigned long ulong;
     58 
     59 // class RBaseStream - base class for other reading streams.
     60 class RBaseStream
     61 {
     62 public:
     63     //methods
     64     RBaseStream();
     65     virtual ~RBaseStream();
     66 
     67     virtual bool  Open( const char* filename );
     68     virtual void  Close();
     69     void          SetBlockSize( int block_size, int unGetsize = 4 );
     70     bool          IsOpened();
     71     void          SetPos( int pos );
     72     int           GetPos();
     73     void          Skip( int bytes );
     74     jmp_buf&      JmpBuf();
     75 
     76 protected:
     77 
     78     jmp_buf m_jmp_buf;
     79     uchar*  m_start;
     80     uchar*  m_end;
     81     uchar*  m_current;
     82     FILE*   m_file;
     83     int     m_unGetsize;
     84     int     m_block_size;
     85     int     m_block_pos;
     86     bool    m_jmp_set;
     87     bool    m_is_opened;
     88 
     89     virtual void  ReadBlock();
     90     virtual void  Release();
     91     virtual void  Allocate();
     92 };
     93 
     94 
     95 // class RLByteStream - uchar-oriented stream.
     96 // l in prefix means that the least significant uchar of a multi-uchar value goes first
     97 class RLByteStream : public RBaseStream
     98 {
     99 public:
    100     virtual ~RLByteStream();
    101 
    102     int     GetByte();
    103     void    GetBytes( void* buffer, int count, int* readed = 0 );
    104     int     GetWord();
    105     int     GetDWord();
    106 };
    107 
    108 // class RMBitStream - uchar-oriented stream.
    109 // m in prefix means that the most significant uchar of a multi-uchar value go first
    110 class RMByteStream : public RLByteStream
    111 {
    112 public:
    113     virtual ~RMByteStream();
    114 
    115     int     GetWord();
    116     int     GetDWord();
    117 };
    118 
    119 // class RLBitStream - bit-oriented stream.
    120 // l in prefix means that the least significant bit of a multi-bit value goes first
    121 class RLBitStream : public RBaseStream
    122 {
    123 public:
    124     virtual ~RLBitStream();
    125 
    126     void    SetPos( int pos );
    127     int     GetPos();
    128     int     Get( int bits );
    129     int     Show( int bits );
    130     int     GetHuff( const short* table );
    131     void    Move( int shift );
    132     void    Skip( int bytes );
    133 
    134 protected:
    135     int     m_bit_idx;
    136     virtual void  ReadBlock();
    137 };
    138 
    139 // class RMBitStream - bit-oriented stream.
    140 // m in prefix means that the most significant bit of a multi-bit value goes first
    141 class RMBitStream : public RLBitStream
    142 {
    143 public:
    144     virtual ~RMBitStream();
    145 
    146     void    SetPos( int pos );
    147     int     GetPos();
    148     int     Get( int bits );
    149     int     Show( int bits );
    150     int     GetHuff( const short* table );
    151     void    Move( int shift );
    152     void    Skip( int bytes );
    153 
    154 protected:
    155     virtual void  ReadBlock();
    156 };
    157 
    158 
    159 // WBaseStream - base class for output streams
    160 class WBaseStream
    161 {
    162 public:
    163     //methods
    164     WBaseStream();
    165     virtual ~WBaseStream();
    166 
    167     virtual bool  Open( const char* filename );
    168     virtual void  Close();
    169     void          SetBlockSize( int block_size );
    170     bool          IsOpened();
    171     int           GetPos();
    172 
    173 protected:
    174 
    175     uchar*  m_start;
    176     uchar*  m_end;
    177     uchar*  m_current;
    178     int     m_block_size;
    179     int     m_block_pos;
    180     FILE*   m_file;
    181     bool    m_is_opened;
    182 
    183     virtual void  WriteBlock();
    184     virtual void  Release();
    185     virtual void  Allocate();
    186 };
    187 
    188 
    189 // class WLByteStream - uchar-oriented stream.
    190 // l in prefix means that the least significant uchar of a multi-byte value goes first
    191 class WLByteStream : public WBaseStream
    192 {
    193 public:
    194     virtual ~WLByteStream();
    195 
    196     void    PutByte( int val );
    197     void    PutBytes( const void* buffer, int count );
    198     void    PutWord( int val );
    199     void    PutDWord( int val );
    200 };
    201 
    202 
    203 // class WLByteStream - uchar-oriented stream.
    204 // m in prefix means that the least significant uchar of a multi-byte value goes last
    205 class WMByteStream : public WLByteStream
    206 {
    207 public:
    208     virtual ~WMByteStream();
    209 
    210     void    PutWord( int val );
    211     void    PutDWord( int val );
    212 };
    213 
    214 
    215 // class WLBitStream - bit-oriented stream.
    216 // l in prefix means that the least significant bit of a multi-bit value goes first
    217 class WLBitStream : public WBaseStream
    218 {
    219 public:
    220     virtual ~WLBitStream();
    221 
    222     int     GetPos();
    223     void    Put( int val, int bits );
    224     void    PutHuff( int val, const int* table );
    225 
    226 protected:
    227     int     m_bit_idx;
    228     int     m_val;
    229     virtual void  WriteBlock();
    230 };
    231 
    232 
    233 // class WMBitStream - bit-oriented stream.
    234 // l in prefix means that the least significant bit of a multi-bit value goes first
    235 class WMBitStream : public WBaseStream
    236 {
    237 public:
    238     WMBitStream();
    239     virtual ~WMBitStream();
    240 
    241     bool    Open( const char* filename );
    242     void    Close();
    243     virtual void  Flush();
    244 
    245     int     GetPos();
    246     void    Put( int val, int bits );
    247     void    PutHuff( int val, const ulong* table );
    248 
    249 protected:
    250     int     m_bit_idx;
    251     ulong   m_pad_val;
    252     ulong   m_val;
    253     virtual void  WriteBlock();
    254     void    ResetBuffer();
    255 };
    256 
    257 
    258 
    259 #define BSWAP(v)    (((v)<<24)|(((v)&0xff00)<<8)| \
    260                     (((v)>>8)&0xff00)|((unsigned)(v)>>24))
    261 
    262 int* bsCreateSourceHuffmanTable( const uchar* src, int* dst,
    263                                  int max_bits, int first_bits );
    264 bool bsCreateDecodeHuffmanTable( const int* src, short* dst, int max_size );
    265 bool bsCreateEncodeHuffmanTable( const int* src, ulong* dst, int max_size );
    266 
    267 void bsBSwapBlock( uchar *start, uchar *end );
    268 bool bsIsBigEndian( void );
    269 
    270 extern const ulong bs_bit_mask[];
    271 
    272 #endif/*_BITSTRM_H_*/
    273