1 /*
2 OpenCV for Android NDK
3 Copyright (c) 2006-2009 SIProp Project http://www.siprop.org/
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 #include "WLNonFileByteStream.h"
16 
17 ///////////////////////////// WLNonFileByteStream ///////////////////////////////////
18 
WLNonFileByteStream()19 WLNonFileByteStream::WLNonFileByteStream()
20 {
21     m_start = m_end = m_current = 0;
22     _size = 0;
23     m_is_opened = false;
24 }
25 
26 
~WLNonFileByteStream()27 WLNonFileByteStream::~WLNonFileByteStream()
28 {
29 	Deallocate();
30 }
31 
32 
Allocate(int data_size)33 void  WLNonFileByteStream::Allocate(int data_size)
34 {
35 	if(!m_start)
36     	m_start = new uchar[data_size];
37 
38     m_end = m_start + data_size;
39     m_current = m_start;
40 }
41 
Deallocate()42 void  WLNonFileByteStream::Deallocate()
43 {
44 	if(m_start)
45 	{
46 		delete [] m_start;
47 		m_start = 0;
48 	}
49 }
50 
Open(int data_size)51 bool  WLNonFileByteStream::Open(int data_size)
52 {
53     Close();
54     Allocate(data_size);
55 
56     m_is_opened = true;
57     m_current = m_start;
58     _size = data_size;
59 
60     return true;
61 }
62 
63 
Close()64 void  WLNonFileByteStream::Close()
65 {
66     m_is_opened = false;
67 	Deallocate();
68 }
69 
70 
PutByte(int val)71 void WLNonFileByteStream::PutByte( int val )
72 {
73     *m_current++ = (uchar)val;
74 }
75 
76 
PutBytes(const void * buffer,int count)77 void WLNonFileByteStream::PutBytes( const void* buffer, int count )
78 {
79     uchar* data = (uchar*)buffer;
80 
81     assert( data && m_current && count >= 0 );
82 
83     while( count )
84     {
85         int l = (int)(m_end - m_current);
86 
87         if( l > count )
88             l = count;
89 
90         if( l > 0 )
91         {
92             memcpy( m_current, data, l );
93             m_current += l;
94             data += l;
95             count -= l;
96         }
97     }
98 }
99 
100 
PutWord(int val)101 void WLNonFileByteStream::PutWord( int val )
102 {
103     uchar *current = m_current;
104 
105     if( current+1 < m_end )
106     {
107         current[0] = (uchar)val;
108         current[1] = (uchar)(val >> 8);
109         m_current = current + 2;
110     }
111     else
112     {
113         PutByte(val);
114         PutByte(val >> 8);
115     }
116 }
117 
118 
PutDWord(int val)119 void WLNonFileByteStream::PutDWord( int val )
120 {
121     uchar *current = m_current;
122 
123     if( current+3 < m_end )
124     {
125         current[0] = (uchar)val;
126         current[1] = (uchar)(val >> 8);
127         current[2] = (uchar)(val >> 16);
128         current[3] = (uchar)(val >> 24);
129         m_current = current + 4;
130     }
131     else
132     {
133         PutByte(val);
134         PutByte(val >> 8);
135         PutByte(val >> 16);
136         PutByte(val >> 24);
137     }
138 }
139 
140 
GetByte()141 uchar* WLNonFileByteStream::GetByte()
142 {
143 	return m_start;
144 }
145 
GetSize()146 int WLNonFileByteStream::GetSize()
147 {
148 	return _size;
149 }
150 
151