Move the sources to trunk
[opencv] / otherlibs / VlGrFmts / 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 <assert.h>
43 #include <string.h>
44
45 #include "grfmt_base.h"
46 #include "bitstrm.h"
47
48 ///////////////////// GrFmtReader //////////////////////////
49
50 GrFmtReader::GrFmtReader()
51 {
52     m_width = m_height = -1;
53     m_sign_len    = 0;
54     m_signature   = 0;
55     m_description = 0;
56     m_filename[0] ='\0';
57 }
58
59
60 void  GrFmtReader::SetFile( const char* filename )
61 {
62     assert( filename != 0 && strlen(filename) < sizeof(m_filename) );
63     strcpy( m_filename, filename );
64 }
65
66
67 bool  GrFmtReader::CheckFormat( const char* signature )
68 {
69     assert( signature != 0 );
70     return memcmp( m_signature, signature, m_sign_len ) == 0;
71 }
72
73
74 ///////////////////// GrFmtReader_list //////////////////////////
75
76 GrFmtReadersList::GrFmtReadersList()
77 {
78     m_readers = 0;
79     RemoveAllReaders();
80 }
81
82
83 GrFmtReadersList::~GrFmtReadersList()
84 {
85     RemoveAllReaders();
86 }
87
88
89 void  GrFmtReadersList::RemoveAllReaders()
90 {
91     if( m_readers )
92     {
93         for( int i = 0; i < m_curReaders; i++ ) delete m_readers[i];
94         delete m_readers;
95     }
96     m_readers = 0;
97     m_maxReaders = m_curReaders = 0;
98 }
99
100
101 void  GrFmtReadersList::AddReader( GrFmtReader* reader )
102 {
103     assert( reader != 0 );
104     if( m_curReaders == m_maxReaders )
105     {
106         // reallocate the readers pointers storage
107         int new_maxReaders = 2*m_maxReaders;
108         if( new_maxReaders < 16 ) new_maxReaders = 16;
109
110         GrFmtReader** newReaders = 
111             new GrFmtReader*[new_maxReaders];
112
113         for( int i = 0; i < m_curReaders; i++ ) newReaders[i] = m_readers[i];
114
115         delete m_readers;
116         m_readers = newReaders;
117         m_maxReaders = new_maxReaders;
118     }
119
120     m_readers[m_curReaders++] = reader;
121 }
122
123
124 ListPosition*  GrFmtReadersList::GetFirstReaderPos()
125 {
126     return (ListPosition*)m_readers;
127 }
128
129
130 GrFmtReader* GrFmtReadersList::
131     GetNextReader( ListPosition*& pos )
132 {
133     GrFmtReader* reader = 0;
134     GrFmtReader** temp = (GrFmtReader**)pos;
135
136     assert( temp == 0 || (m_readers <= temp && temp < m_readers + m_curReaders));
137     if( temp )
138     {
139         reader = *temp++;
140         pos = (ListPosition*)(temp < m_readers + m_curReaders ? temp : 0);
141     }
142     return reader;
143 }
144
145
146 GrFmtReader* GrFmtReadersList::
147     FindReader( const char* filename )
148 {
149     int    i;
150     FILE*  f = fopen( filename, "rb" );
151     char   signature[4096];
152     int    sign_len = 0;
153     GrFmtReader* reader = 0;
154
155     if( f )
156     {
157         for( i = 0; i < m_curReaders; i++ )
158         {
159             int temp = m_readers[i]->GetSignatureLength();
160             if( temp > sign_len ) sign_len = temp;
161         }
162
163         assert( sign_len <= sizeof(signature) );
164
165         sign_len = fread( signature, 1, sign_len, f );
166         fclose(f);
167
168         for( i = 0; i < m_curReaders; i++ )
169         {
170             int temp = m_readers[i]->GetSignatureLength();
171             if( temp <= sign_len && m_readers[i]->CheckFormat(signature))
172             {
173                 reader = m_readers[i];
174                 break;
175             }
176         }
177     }
178
179     return reader;
180 }
181
182 int  GrFmtReadersList::GetFiltersString( char* buffer, int maxlen )
183 {
184     buffer, maxlen;
185     return 0;
186 }
187
188