/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., 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 documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// 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 damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#ifndef _BITSTRM_H_43#define _BITSTRM_H_4445#include <stdio.h>4647namespace cv48{4950#define DECLARE_RBS_EXCEPTION(name) \51class RBS_ ## name ## _Exception : public cv::Exception \52{ \53public: \54RBS_ ## name ## _Exception(int code_, const String& err_, const String& func_, const String& file_, int line_) : \55cv::Exception(code_, err_, func_, file_, line_) \56{} \57};58DECLARE_RBS_EXCEPTION(THROW_EOS)59#define RBS_THROW_EOS RBS_THROW_EOS_Exception(cv::Error::StsError, "Unexpected end of input stream", CV_Func, __FILE__, __LINE__)60DECLARE_RBS_EXCEPTION(THROW_FORB)61#define RBS_THROW_FORB RBS_THROW_FORB_Exception(cv::Error::StsError, "Forrbidden huffman code", CV_Func, __FILE__, __LINE__)62DECLARE_RBS_EXCEPTION(BAD_HEADER)63#define RBS_BAD_HEADER RBS_BAD_HEADER_Exception(cv::Error::StsError, "Invalid header", CV_Func, __FILE__, __LINE__)6465typedef unsigned long ulong;6667// class RBaseStream - base class for other reading streams.68class RBaseStream69{70public:71//methods72RBaseStream();73virtual ~RBaseStream();7475virtual bool open( const String& filename );76virtual bool open( const Mat& buf );77virtual void close();78bool isOpened();79void setPos( int pos );80int getPos();81void skip( int bytes );8283protected:8485bool m_allocated;86uchar* m_start;87uchar* m_end;88uchar* m_current;89FILE* m_file;90int m_block_size;91int m_block_pos;92bool m_is_opened;9394virtual void readBlock();95virtual void release();96virtual void allocate();97};9899100// class RLByteStream - uchar-oriented stream.101// l in prefix means that the least significant uchar of a multi-uchar value goes first102class RLByteStream : public RBaseStream103{104public:105virtual ~RLByteStream();106107int getByte();108int getBytes( void* buffer, int count );109int getWord();110int getDWord();111};112113// class RMBitStream - uchar-oriented stream.114// m in prefix means that the most significant uchar of a multi-uchar value go first115class RMByteStream : public RLByteStream116{117public:118virtual ~RMByteStream();119120int getWord();121int getDWord();122};123124// WBaseStream - base class for output streams125class WBaseStream126{127public:128//methods129WBaseStream();130virtual ~WBaseStream();131132virtual bool open( const String& filename );133virtual bool open( std::vector<uchar>& buf );134virtual void close();135bool isOpened();136int getPos();137138protected:139140uchar* m_start;141uchar* m_end;142uchar* m_current;143int m_block_size;144int m_block_pos;145FILE* m_file;146bool m_is_opened;147std::vector<uchar>* m_buf;148149virtual void writeBlock();150virtual void release();151virtual void allocate();152};153154155// class WLByteStream - uchar-oriented stream.156// l in prefix means that the least significant uchar of a multi-byte value goes first157class WLByteStream : public WBaseStream158{159public:160virtual ~WLByteStream();161162void putByte( int val );163void putBytes( const void* buffer, int count );164void putWord( int val );165void putDWord( int val );166};167168169// class WLByteStream - uchar-oriented stream.170// m in prefix means that the least significant uchar of a multi-byte value goes last171class WMByteStream : public WLByteStream172{173public:174virtual ~WMByteStream();175void putWord( int val );176void putDWord( int val );177};178179inline unsigned BSWAP(unsigned v)180{181return (v<<24)|((v&0xff00)<<8)|((v>>8)&0xff00)|((unsigned)v>>24);182}183184bool bsIsBigEndian( void );185186}187188#endif/*_BITSTRM_H_*/189190191