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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
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.
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.
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.
44 #include "grfmt_base.h"
48 GrFmtReader::GrFmtReader( const char* filename )
50 strncpy( m_filename, filename, sizeof(m_filename) - 1 );
51 m_filename[sizeof(m_filename)-1] = '\0';
52 m_width = m_height = 0;
55 m_native_depth = false;
60 GrFmtReader::~GrFmtReader()
66 void GrFmtReader::Close()
68 m_width = m_height = 0;
73 GrFmtWriter::GrFmtWriter( const char* filename )
75 strncpy( m_filename, filename, sizeof(m_filename) - 1 );
76 m_filename[sizeof(m_filename)-1] = '\0';
80 bool GrFmtWriter::IsFormatSupported( int depth )
82 return depth == IPL_DEPTH_8U;
86 GrFmtFilterFactory::GrFmtFilterFactory()
88 m_description = m_signature = 0;
93 bool GrFmtFilterFactory::CheckFile( const char* filename )
98 int cur_sign_len = GetSignatureLength();
100 if( !filename ) return false;
102 assert( cur_sign_len <= (int)sizeof( signature ) );
103 f = fopen( filename, "rb" );
107 sign_len = (int)fread( signature, 1, cur_sign_len, f );
110 if( cur_sign_len <= sign_len && CheckSignature( signature ) )
118 bool GrFmtFilterFactory::CheckSignature( const char* signature )
120 return m_sign_len > 0 && signature != 0 &&
121 memcmp( signature, m_signature, m_sign_len ) == 0;
125 static int GetExtensionLength( const char* buffer )
131 const char* ext = strchr( buffer, '.');
133 while( isalnum(ext[len]) && len < _MAX_PATH )
141 bool GrFmtFilterFactory::CheckExtension( const char* format )
143 const char* descr = 0;
146 if( !format || !m_description )
149 // find the right-most extension of the passed format string
152 const char* ext = strchr( format + 1, '.' );
157 len = GetExtensionLength( format );
159 if( format[0] != '.' || len == 0 )
162 descr = strchr( m_description, '(' );
166 descr = strchr( descr + 1, '.' );
167 int i, len2 = GetExtensionLength( descr );
174 for( i = 0; i < len; i++ )
176 int c1 = tolower(format[i+1]);
177 int c2 = tolower(descr[i+1]);
192 ///////////////////// GrFmtFilterList //////////////////////////
194 GrFmtFactoriesList::GrFmtFactoriesList()
201 GrFmtFactoriesList::~GrFmtFactoriesList()
207 void GrFmtFactoriesList::RemoveAll()
211 for( int i = 0; i < m_curFactories; i++ ) delete m_factories[i];
212 delete[] m_factories;
215 m_maxFactories = m_curFactories = 0;
219 bool GrFmtFactoriesList::AddFactory( GrFmtFilterFactory* factory )
221 assert( factory != 0 );
222 if( m_curFactories == m_maxFactories )
224 // reallocate the factorys pointers storage
225 int newMaxFactories = 2*m_maxFactories;
226 if( newMaxFactories < 16 ) newMaxFactories = 16;
228 GrFmtFilterFactory** newFactories = new GrFmtFilterFactory*[newMaxFactories];
230 for( int i = 0; i < m_curFactories; i++ ) newFactories[i] = m_factories[i];
232 delete[] m_factories;
233 m_factories = newFactories;
234 m_maxFactories = newMaxFactories;
237 m_factories[m_curFactories++] = factory;
242 ListPosition GrFmtFactoriesList::GetFirstFactoryPos()
244 return (ListPosition)m_factories;
248 GrFmtFilterFactory* GrFmtFactoriesList::GetNextFactory( ListPosition& pos )
250 GrFmtFilterFactory* factory = 0;
251 GrFmtFilterFactory** temp = (GrFmtFilterFactory**)pos;
253 assert( temp == 0 || (m_factories <= temp && temp < m_factories + m_curFactories));
257 pos = (ListPosition)(temp < m_factories + m_curFactories ? temp : 0);
263 GrFmtReader* GrFmtFactoriesList::FindReader( const char* filename )
265 if( !filename ) return 0;
267 GrFmtReader* reader = 0;
268 ListPosition pos = GetFirstFactoryPos();
272 GrFmtFilterFactory* tempFactory = GetNextFactory( pos );
273 if( tempFactory->CheckFile( filename ) )
275 reader = tempFactory->NewReader( filename );
284 GrFmtWriter* GrFmtFactoriesList::FindWriter( const char* filename )
286 GrFmtWriter* writer = 0;
287 ListPosition pos = GetFirstFactoryPos();
289 if( !filename ) return 0;
293 GrFmtFilterFactory* tempFactory = GetNextFactory(pos);
294 if( tempFactory->CheckExtension( filename ))
296 writer = tempFactory->NewWriter( filename );