Apply maemo2 patch
[opencv] / otherlibs / highgui / grfmt_base.cpp
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 #include "_highgui.h"
43
44 #include "grfmt_base.h"
45 #include "bitstrm.h"
46
47
48 GrFmtReader::GrFmtReader( const char* filename )
49 {
50     strncpy( m_filename, filename, sizeof(m_filename) - 1 );
51     m_filename[sizeof(m_filename)-1] = '\0';
52     m_width = m_height = 0;
53     m_iscolor = false;
54     m_bit_depth = 8;
55     m_native_depth = false;
56     m_isfloat = false;
57 }
58
59
60 GrFmtReader::~GrFmtReader()
61 {
62     Close();
63 }
64
65
66 void GrFmtReader::Close()
67 {
68     m_width = m_height = 0;
69     m_iscolor = false;
70 }
71
72
73 GrFmtWriter::GrFmtWriter( const char* filename )
74 {
75     strncpy( m_filename, filename, sizeof(m_filename) - 1 );
76     m_filename[sizeof(m_filename)-1] = '\0';
77 }
78
79
80 bool  GrFmtWriter::IsFormatSupported( int depth )
81 {
82     return depth == IPL_DEPTH_8U;
83 }
84
85
86 GrFmtFilterFactory::GrFmtFilterFactory()
87 {
88     m_description = m_signature = 0;
89     m_sign_len = 0;
90 }
91
92
93 bool  GrFmtFilterFactory::CheckFile( const char* filename )
94 {
95         FILE* f = 0;
96         char signature[4096];
97         int sign_len = 0;
98         int cur_sign_len = GetSignatureLength();
99         
100         if( !filename ) return false;
101         
102         assert( cur_sign_len <= (int)sizeof( signature ) );
103         f = fopen( filename, "rb" );
104         
105         if( f )
106         {
107                 sign_len = (int)fread( signature, 1, cur_sign_len, f );
108                 fclose( f );
109                 
110                 if( cur_sign_len <= sign_len && CheckSignature( signature ) )
111             return true;
112         }
113         
114         return false;
115 }
116
117
118 bool GrFmtFilterFactory::CheckSignature( const char* signature )
119 {
120     return m_sign_len > 0 && signature != 0 &&
121            memcmp( signature, m_signature, m_sign_len ) == 0;
122 }
123
124
125 static int GetExtensionLength( const char* buffer )
126 {
127     int len = 0;
128
129     if( buffer )
130     {
131         const char* ext = strchr( buffer, '.');
132         if( ext++ )
133             while( isalnum(ext[len]) && len < _MAX_PATH )
134                 len++;
135     }
136
137     return len;
138 }
139
140
141 bool GrFmtFilterFactory::CheckExtension( const char* format )
142 {
143     const char* descr = 0;
144     int len = 0;
145         
146     if( !format || !m_description )
147         return false;
148
149     // find the right-most extension of the passed format string
150     for(;;)
151     {
152         const char* ext = strchr( format + 1, '.' );
153         if( !ext ) break;
154         format = ext;
155     }
156
157     len = GetExtensionLength( format );
158
159     if( format[0] != '.' || len == 0 )
160         return false;
161
162     descr = strchr( m_description, '(' );
163
164     while( descr )
165     {
166         descr = strchr( descr + 1, '.' );
167         int i, len2 = GetExtensionLength( descr ); 
168
169         if( len2 == 0 )
170             break;
171
172         if( len2 == len )
173         {
174             for( i = 0; i < len; i++ )
175             {
176                 int c1 = tolower(format[i+1]);
177                 int c2 = tolower(descr[i+1]);
178
179                 if( c1 != c2 )
180                     break;
181             }
182             if( i == len )
183                 return true;
184         }
185     }
186
187     return false;
188 }
189
190
191
192 ///////////////////// GrFmtFilterList //////////////////////////
193
194 GrFmtFactoriesList::GrFmtFactoriesList()
195 {
196     m_factories = 0;
197     RemoveAll();
198 }
199
200
201 GrFmtFactoriesList::~GrFmtFactoriesList()
202 {
203     RemoveAll();
204 }
205
206
207 void  GrFmtFactoriesList::RemoveAll()
208 {
209     if( m_factories )
210     {
211         for( int i = 0; i < m_curFactories; i++ ) delete m_factories[i];
212         delete[] m_factories;
213     }
214     m_factories = 0;
215     m_maxFactories = m_curFactories = 0;
216 }
217
218
219 bool  GrFmtFactoriesList::AddFactory( GrFmtFilterFactory* factory )
220 {
221     assert( factory != 0 );
222     if( m_curFactories == m_maxFactories )
223     {
224         // reallocate the factorys pointers storage
225         int newMaxFactories = 2*m_maxFactories;
226         if( newMaxFactories < 16 ) newMaxFactories = 16;
227
228         GrFmtFilterFactory** newFactories = new GrFmtFilterFactory*[newMaxFactories];
229
230         for( int i = 0; i < m_curFactories; i++ ) newFactories[i] = m_factories[i];
231
232         delete[] m_factories;
233         m_factories = newFactories;
234         m_maxFactories = newMaxFactories;
235     }
236
237     m_factories[m_curFactories++] = factory;
238     return true;
239 }
240
241
242 ListPosition  GrFmtFactoriesList::GetFirstFactoryPos()
243 {
244     return (ListPosition)m_factories;
245 }
246
247
248 GrFmtFilterFactory* GrFmtFactoriesList::GetNextFactory( ListPosition& pos )
249 {
250     GrFmtFilterFactory* factory = 0;
251     GrFmtFilterFactory** temp = (GrFmtFilterFactory**)pos;
252
253     assert( temp == 0 || (m_factories <= temp && temp < m_factories + m_curFactories));
254     if( temp )
255     {
256         factory = *temp++;
257         pos = (ListPosition)(temp < m_factories + m_curFactories ? temp : 0);
258     }
259     return factory;
260 }
261
262
263 GrFmtReader* GrFmtFactoriesList::FindReader( const char* filename )
264 {
265         if( !filename ) return 0;
266         
267         GrFmtReader* reader = 0;
268         ListPosition pos = GetFirstFactoryPos();
269         
270         while( pos )
271         {
272                 GrFmtFilterFactory* tempFactory = GetNextFactory( pos );
273                 if( tempFactory->CheckFile( filename ) )
274                 {
275                         reader = tempFactory->NewReader( filename );
276                         break;
277                 }
278         }
279         
280     return reader;
281 }
282
283
284 GrFmtWriter* GrFmtFactoriesList::FindWriter( const char* filename )
285 {
286     GrFmtWriter* writer = 0;
287     ListPosition pos = GetFirstFactoryPos();
288
289     if( !filename ) return 0;
290
291     while( pos )
292     {
293         GrFmtFilterFactory* tempFactory = GetNextFactory(pos);
294         if( tempFactory->CheckExtension( filename ))
295         {
296             writer = tempFactory->NewWriter( filename );
297             break;
298         }
299     }
300
301     return writer;
302 }
303
304 /* End of file. */
305