1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
49 const int BS_DEF_BLOCK_SIZE = 1<<15;
51 bool bsIsBigEndian( void )
53 return (((const int*)"\0\x1\x2\x3\x4\x5\x6\x7")[0] & 255) != 0;
56 ///////////////////////// RBaseStream ////////////////////////////
58 bool RBaseStream::isOpened()
63 void RBaseStream::allocate()
67 m_start = new uchar[m_block_size];
68 m_end = m_start + m_block_size;
75 RBaseStream::RBaseStream()
77 m_start = m_end = m_current = 0;
79 m_block_size = BS_DEF_BLOCK_SIZE;
85 RBaseStream::~RBaseStream()
87 close(); // Close files
88 release(); // free buffers
92 void RBaseStream::readBlock()
94 setPos( getPos() ); // normalize position
98 if( m_block_pos == 0 && m_current < m_end )
103 fseek( m_file, m_block_pos, SEEK_SET );
104 size_t readed = fread( m_start, 1, m_block_size, m_file );
105 m_end = m_start + readed;
108 if( readed == 0 || m_current >= m_end )
113 bool RBaseStream::open( const string& filename )
118 m_file = fopen( filename.c_str(), "rb" );
128 bool RBaseStream::open( const Mat& buf )
133 CV_Assert(buf.isContinuous());
135 m_end = m_start + buf.cols*buf.rows*buf.elemSize();
143 void RBaseStream::close()
152 m_start = m_end = m_current = 0;
156 void RBaseStream::release()
160 m_start = m_end = m_current = 0;
165 void RBaseStream::setPos( int pos )
167 assert( isOpened() && pos >= 0 );
171 m_current = m_start + pos;
176 int offset = pos % m_block_size;
177 m_block_pos = pos - offset;
178 m_current = m_start + offset;
182 int RBaseStream::getPos()
184 assert( isOpened() );
185 return m_block_pos + (int)(m_current - m_start);
188 void RBaseStream::skip( int bytes )
190 assert( bytes >= 0 );
194 ///////////////////////// RLByteStream ////////////////////////////
196 RLByteStream::~RLByteStream()
200 int RLByteStream::getByte()
202 uchar *current = m_current;
205 if( current >= m_end )
211 val = *((uchar*)current);
212 m_current = current + 1;
217 int RLByteStream::getBytes( void* buffer, int count )
219 uchar* data = (uchar*)buffer;
221 assert( count >= 0 );
229 l = (int)(m_end - m_current);
230 if( l > count ) l = count;
234 memcpy( data, m_current, l );
244 //////////// RLByteStream & RMByteStream <Get[d]word>s ////////////////
246 RMByteStream::~RMByteStream()
251 int RLByteStream::getWord()
253 uchar *current = m_current;
256 if( current+1 < m_end )
258 val = current[0] + (current[1] << 8);
259 m_current = current + 2;
264 val|= getByte() << 8;
270 int RLByteStream::getDWord()
272 uchar *current = m_current;
275 if( current+3 < m_end )
277 val = current[0] + (current[1] << 8) +
278 (current[2] << 16) + (current[3] << 24);
279 m_current = current + 4;
284 val |= getByte() << 8;
285 val |= getByte() << 16;
286 val |= getByte() << 24;
292 int RMByteStream::getWord()
294 uchar *current = m_current;
297 if( current+1 < m_end )
299 val = (current[0] << 8) + current[1];
300 m_current = current + 2;
304 val = getByte() << 8;
311 int RMByteStream::getDWord()
313 uchar *current = m_current;
316 if( current+3 < m_end )
318 val = (current[0] << 24) + (current[1] << 16) +
319 (current[2] << 8) + current[3];
320 m_current = current + 4;
324 val = getByte() << 24;
325 val |= getByte() << 16;
326 val |= getByte() << 8;
332 /////////////////////////// WBaseStream /////////////////////////////////
334 // WBaseStream - base class for output streams
335 WBaseStream::WBaseStream()
337 m_start = m_end = m_current = 0;
339 m_block_size = BS_DEF_BLOCK_SIZE;
345 WBaseStream::~WBaseStream()
352 bool WBaseStream::isOpened()
358 void WBaseStream::allocate()
361 m_start = new uchar[m_block_size];
363 m_end = m_start + m_block_size;
368 void WBaseStream::writeBlock()
370 int size = (int)(m_current - m_start);
372 assert( isOpened() );
378 size_t sz = m_buf->size();
379 m_buf->resize( sz + size );
380 memcpy( &(*m_buf)[sz], m_start, size );
384 fwrite( m_start, 1, size, m_file );
391 bool WBaseStream::open( const string& filename )
396 m_file = fopen( filename.c_str(), "wb" );
406 bool WBaseStream::open( vector<uchar>& buf )
419 void WBaseStream::close()
433 void WBaseStream::release()
437 m_start = m_end = m_current = 0;
441 int WBaseStream::getPos()
443 assert( isOpened() );
444 return m_block_pos + (int)(m_current - m_start);
448 ///////////////////////////// WLByteStream ///////////////////////////////////
450 WLByteStream::~WLByteStream()
454 void WLByteStream::putByte( int val )
456 *m_current++ = (uchar)val;
457 if( m_current >= m_end )
462 void WLByteStream::putBytes( const void* buffer, int count )
464 uchar* data = (uchar*)buffer;
466 assert( data && m_current && count >= 0 );
470 int l = (int)(m_end - m_current);
477 memcpy( m_current, data, l );
482 if( m_current == m_end )
488 void WLByteStream::putWord( int val )
490 uchar *current = m_current;
492 if( current+1 < m_end )
494 current[0] = (uchar)val;
495 current[1] = (uchar)(val >> 8);
496 m_current = current + 2;
497 if( m_current == m_end )
508 void WLByteStream::putDWord( int val )
510 uchar *current = m_current;
512 if( current+3 < m_end )
514 current[0] = (uchar)val;
515 current[1] = (uchar)(val >> 8);
516 current[2] = (uchar)(val >> 16);
517 current[3] = (uchar)(val >> 24);
518 m_current = current + 4;
519 if( m_current == m_end )
532 ///////////////////////////// WMByteStream ///////////////////////////////////
534 WMByteStream::~WMByteStream()
539 void WMByteStream::putWord( int val )
541 uchar *current = m_current;
543 if( current+1 < m_end )
545 current[0] = (uchar)(val >> 8);
546 current[1] = (uchar)val;
547 m_current = current + 2;
548 if( m_current == m_end )
559 void WMByteStream::putDWord( int val )
561 uchar *current = m_current;
563 if( current+3 < m_end )
565 current[0] = (uchar)(val >> 24);
566 current[1] = (uchar)(val >> 16);
567 current[2] = (uchar)(val >> 8);
568 current[3] = (uchar)val;
569 m_current = current + 4;
570 if( m_current == m_end )