Move the sources to trunk
[opencv] / otherlibs / VlGrFmts / bitstrm.h
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
47 #define  LITTLE_ENDIAN  /* intel-like uchar order */
48 #ifndef  LITTLE_ENDIAN
49     #define BIG_ENDIAN  /* motorolla-like uchar order */
50 #endif
51
52 #pragma warning( disable: 4711 )
53
54 #define  RBS_THROW_EOS    -123  /* <end of stream> exception code */
55 #define  RBS_THROW_FORB   -124  /* <forrbidden huffman code> exception code */
56 #define  RBS_HUFF_FORB    2047  /* forrbidden huffman code "value" */
57
58 typedef unsigned char uchar;
59
60 // class RBaseStream - base class for other reading streams.
61 class RBaseStream
62 {
63 public:
64     //methods
65     RBaseStream();
66     ~RBaseStream();
67     
68     virtual bool  Open( const char* filename );
69     virtual void  Close();
70     void          SetBlockSize( int block_size, int unGetsize = 4 );
71     bool          IsOpened();
72     void          SetPos( int pos );
73     int           GetPos();
74     void          Skip( int bytes );
75     
76 protected:
77     
78     uchar*   m_start;
79     uchar*   m_end;
80     uchar*   m_current;
81     int     m_unGetsize;
82     int     m_block_size;
83     int     m_block_pos;
84     FILE*   m_file;
85     bool    m_is_opened;
86     
87     virtual void  ReadBlock();
88     virtual void  Release();
89     virtual void  Allocate();
90 };
91
92
93 // class RLByteStream - uchar-oriented stream.
94 // l in prefix means that the least significant uchar of a multi-uchar value goes first
95 class RLByteStream : public RBaseStream
96 {
97 public:
98     int     GetByte();
99     void    GetBytes( void* buffer, int length, int* readed = 0 );
100     int     GetWord();
101     int     GetDWord(); 
102 };
103
104 // class RMBitStream - uchar-oriented stream.
105 // m in prefix means that the most significant uchar of a multi-uchar value go first
106 class RMByteStream : public RLByteStream
107 {
108 public:
109     int     GetWord();
110     int     GetDWord(); 
111 };
112
113 // class RLBitStream - bit-oriented stream.
114 // l in prefix means that the least significant bit of a multi-bit value goes first
115 class RLBitStream : public RBaseStream
116 {
117 public:
118     void    SetPos( int pos );
119     int     GetPos();
120     int     Get( int bits );
121     int     Show( int bits );
122     int     GetHuff( const short* table );
123     void    Move( int shift );
124     void    Skip( int bytes );
125         
126 protected:
127     int     m_bit_idx;
128     virtual void  ReadBlock();
129 };
130
131 // class RMBitStream - bit-oriented stream.
132 // m in prefix means that the most significant bit of a multi-bit value goes first
133 class RMBitStream : public RLBitStream
134 {
135 public:
136     void    SetPos( int pos );
137     int     GetPos();
138     int     Get( int bits );
139     int     Show( int bits );
140     int     GetHuff( const short* table );
141     void    Move( int shift );
142     void    Skip( int bytes );
143
144 protected:
145     virtual void  ReadBlock();
146 };
147
148
149 int* bs_create_source_huffman_table( const uchar* src, int* dst, 
150                                      int max_bits, int first_bits );
151 bool bs_create_decode_huffman_table( const int* src, short* dst, int max_size );
152 void bs_bswap_block( uchar *start, uchar *end );
153
154 extern unsigned long bs_bits_masks[];
155
156 #endif/*_BITSTRM_H_*/