Update the changelog
[opencv] / cxcore / src / cximage.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 /* ////////////////////////////////////////////////////////////////////
43 //
44 //  C++ classes for image and matrices
45 //
46 // */
47
48 #include "_cxcore.h"
49
50 /////////////////////////////// CvImage implementation //////////////////////////////////
51
52 static CvLoadImageFunc load_image = 0;
53 static CvLoadImageMFunc load_image_m = 0;
54 static CvSaveImageFunc save_image = 0;
55 static CvShowImageFunc show_image = 0;
56
57 static bool
58 icvIsXmlOrYaml( const char* filename )
59 {
60     const char* suffix = strrchr( filename, '.' );
61     return suffix &&
62         strcmp( suffix, ".xml" ) == 0 ||
63         strcmp( suffix, ".Xml" ) == 0 ||
64         strcmp( suffix, ".XML" ) == 0 ||
65         strcmp( suffix, ".yml" ) == 0 ||
66         strcmp( suffix, ".Yml" ) == 0 ||
67         strcmp( suffix, ".YML" ) == 0 ||
68         strcmp( suffix, ".yaml" ) == 0 ||
69         strcmp( suffix, ".Yaml" ) == 0 ||
70         strcmp( suffix, ".YAML" ) == 0;
71 }
72
73
74 static IplImage*
75 icvRetrieveImage( void* obj )
76 {
77     IplImage* img = 0;
78
79     CV_FUNCNAME( "icvRetrieveImage" );
80
81     __BEGIN__;
82
83     if( CV_IS_IMAGE(obj) )
84         img = (IplImage*)obj;
85     else if( CV_IS_MAT(obj) )
86     {
87         CvMat* m = (CvMat*)obj;
88         CV_CALL( img = cvCreateImageHeader( cvSize(m->cols,m->rows),
89                         CV_MAT_DEPTH(m->type), CV_MAT_CN(m->type) ));
90         cvSetData( img, m->data.ptr, m->step );
91         img->imageDataOrigin = (char*)m->refcount;
92         m->data.ptr = 0; m->step = 0;
93         cvReleaseMat( &m );
94     }
95     else if( obj )
96     {
97         cvRelease( &obj );
98         CV_ERROR( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
99     }
100
101     __END__;
102
103     return img;
104 }
105
106
107 bool CvImage::load( const char* filename, const char* imgname, int color )
108 {
109     IplImage* img = 0;
110
111     CV_FUNCNAME( "CvImage::read" );
112
113     __BEGIN__;
114
115     if( icvIsXmlOrYaml(filename) )
116     {
117         img = icvRetrieveImage(cvLoad(filename,0,imgname));
118         if( (img->nChannels > 1) != (color == 0) )
119             CV_ERROR( CV_StsNotImplemented,
120             "RGB<->Grayscale conversion is not implemented for images stored in XML/YAML" );
121         /*{
122             IplImage* temp_img = 0;
123             CV_CALL( temp_img = cvCreateImage( cvGetSize(img), img->depth, color > 0 ? 3 : 1 ));
124             cvCvtColor( img, temp_img, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
125             cvReleaseImage( &img );
126             img = temp_img;
127         }*/
128     }
129     else
130     {
131         if( load_image )
132             img = load_image( filename, color );
133         else
134             CV_ERROR( CV_StsNotImplemented,
135             "Loading an image stored in such a format requires HigGUI.\n"
136             "Link it to your program and call any function from it\n" );
137     }
138
139     attach( img );
140
141     __END__;
142
143     return img != 0;
144 }
145
146
147 bool CvImage::read( CvFileStorage* fs, const char* mapname, const char* imgname )
148 {
149     void* obj = 0;
150     IplImage* img = 0;
151
152     if( mapname )
153     {
154         CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
155         if( !mapnode )
156             obj = cvReadByName( fs, mapnode, imgname );
157     }
158     else
159         obj = cvReadByName( fs, 0, imgname );
160
161     img = icvRetrieveImage(obj);
162     attach( img );
163     return img != 0;
164 }
165
166
167 bool CvImage::read( CvFileStorage* fs, const char* seqname, int idx )
168 {
169     void* obj = 0;
170     IplImage* img = 0;
171     CvFileNode* seqnode = seqname ?
172         cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
173
174     if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
175         obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
176     img = icvRetrieveImage(obj);
177     attach( img );
178     return img != 0;
179 }
180
181
182 void CvImage::save( const char* filename, const char* imgname )
183 {
184     CV_FUNCNAME( "CvImage::write" );
185     __BEGIN__;
186
187     if( !image )
188         return;
189     if( icvIsXmlOrYaml( filename ) )
190         cvSave( filename, image, imgname );
191     else
192     {
193         if( save_image )
194             save_image( filename, image );
195         else
196             CV_ERROR( CV_StsNotImplemented,
197             "Saving an image in such a format requires HigGUI.\n"
198             "Link it to your program and call any function from it\n" );
199     }
200
201     __END__;
202 }
203
204
205 void CvImage::write( CvFileStorage* fs, const char* imgname )
206 {
207     if( image )
208         cvWrite( fs, imgname, image );
209 }
210
211
212 void CvImage::show( const char* window_name )
213 {
214     CV_FUNCNAME( "CvMatrix::show" );
215
216     __BEGIN__;
217
218     if( image )
219     {
220         if( !show_image )
221             CV_ERROR( CV_StsNotImplemented,
222             "CvImage::show method requires HighGUI.\n"
223             "Link it to your program and call any function from it\n" );
224         show_image( window_name, image );
225     }
226
227     __END__;
228 }
229
230
231 /////////////////////////////// CvMatrix implementation //////////////////////////////////
232
233 CvMatrix::CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data )
234 {
235     if( storage )
236     {
237         matrix = (CvMat*)cvMemStorageAlloc( storage, sizeof(*matrix) );
238         cvInitMatHeader( matrix, rows, cols, type, alloc_data ?
239             cvMemStorageAlloc( storage, rows*cols*CV_ELEM_SIZE(type) ) : 0 );
240     }
241     else
242         matrix = 0;
243 }
244
245 static CvMat*
246 icvRetrieveMatrix( void* obj )
247 {
248     CvMat* m = 0;
249
250     CV_FUNCNAME( "icvRetrieveMatrix" );
251
252     __BEGIN__;
253
254     if( CV_IS_MAT(obj) )
255         m = (CvMat*)obj;
256     else if( CV_IS_IMAGE(obj) )
257     {
258         IplImage* img = (IplImage*)obj;
259         CvMat hdr, *src = cvGetMat( img, &hdr );
260         CV_CALL( m = cvCreateMat( src->rows, src->cols, src->type ));
261         CV_CALL( cvCopy( src, m ));
262         cvReleaseImage( &img );
263     }
264     else if( obj )
265     {
266         cvRelease( &obj );
267         CV_ERROR( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
268     }
269
270     __END__;
271
272     return m;
273 }
274
275
276 bool CvMatrix::load( const char* filename, const char* matname, int color )
277 {
278     CvMat* m = 0;
279
280     CV_FUNCNAME( "CvMatrix::read" );
281
282     __BEGIN__;
283
284     if( icvIsXmlOrYaml(filename) )
285     {
286         m = icvRetrieveMatrix(cvLoad(filename,0,matname));
287
288         if( (CV_MAT_CN(m->type) > 1) != (color == 0) )
289             CV_ERROR( CV_StsNotImplemented,
290             "RGB<->Grayscale conversion is not implemented for matrices stored in XML/YAML" );
291         /*{
292             CvMat* temp_mat;
293             CV_CALL( temp_mat = cvCreateMat( m->rows, m->cols,
294                 CV_MAKETYPE(CV_MAT_DEPTH(m->type), color > 0 ? 3 : 1 )));
295             cvCvtColor( m, temp_mat, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
296             cvReleaseMat( &m );
297             m = temp_mat;
298         }*/
299     }
300     else
301     {
302         if( load_image_m )
303             m = load_image_m( filename, color );
304         else
305             CV_ERROR( CV_StsNotImplemented,
306             "Loading an image stored in such a format requires HigGUI.\n"
307             "Link it to your program and call any function from it\n" );
308     }
309
310     set( m, false );
311
312     __END__;
313
314     return m != 0;
315 }
316
317
318 bool CvMatrix::read( CvFileStorage* fs, const char* mapname, const char* matname )
319 {
320     void* obj = 0;
321     CvMat* m = 0;
322
323     if( mapname )
324     {
325         CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
326         if( !mapnode )
327             obj = cvReadByName( fs, mapnode, matname );
328     }
329     else
330         obj = cvReadByName( fs, 0, matname );
331
332     m = icvRetrieveMatrix(obj);
333     set( m, false );
334     return m != 0;
335 }
336
337
338 bool CvMatrix::read( CvFileStorage* fs, const char* seqname, int idx )
339 {
340     void* obj = 0;
341     CvMat* m = 0;
342     CvFileNode* seqnode = seqname ?
343         cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
344
345     if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
346         obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
347     m = icvRetrieveMatrix(obj);
348     set( m, false );
349     return m != 0;
350 }
351
352
353 void CvMatrix::save( const char* filename, const char* matname )
354 {
355     CV_FUNCNAME( "CvMatrix::write" );
356     __BEGIN__;
357
358     if( !matrix )
359         return;
360     if( icvIsXmlOrYaml( filename ) )
361         cvSave( filename, matrix, matname );
362     else
363     {
364         if( save_image )
365             save_image( filename, matrix );
366         else
367             CV_ERROR( CV_StsNotImplemented,
368             "Saving a matrixe in such a format requires HigGUI.\n"
369             "Link it to your program and call any function from it\n" );
370     }
371
372     __END__;
373 }
374
375
376 void CvMatrix::write( CvFileStorage* fs, const char* matname )
377 {
378     if( matrix )
379         cvWrite( fs, matname, matrix );
380 }
381
382
383 void CvMatrix::show( const char* window_name )
384 {
385     CV_FUNCNAME( "CvMatrix::show" );
386
387     __BEGIN__;
388
389     if( matrix )
390     {
391         if( !show_image )
392             CV_ERROR( CV_StsNotImplemented,
393             "CvMatrix::show method requires HighGUI.\n"
394             "Link it to your program and call any function from it\n" );
395         show_image( window_name, matrix );
396     }
397
398     __END__;
399 }
400
401
402 CV_IMPL int
403 cvSetImageIOFunctions( CvLoadImageFunc _load_image, CvLoadImageMFunc _load_image_m,
404                        CvSaveImageFunc _save_image, CvShowImageFunc _show_image )
405 {
406     load_image = _load_image;
407     load_image_m = _load_image_m;
408     save_image = _save_image;
409     show_image = _show_image;
410     return 1;
411 }
412
413
414 /*void main(void)
415 {
416     CvImage a(cvSize(300,200),8,3), b(cvSize(300,200),8,3);
417     CvRNG rng = cvRNG(-1);
418
419     CV_SET_IMAGE_IO_FUNCTIONS();
420
421     cvNamedWindow( "test", 1 );
422     //cvZero( a );
423     cvZero( b );
424     cvRandArr( &rng, a, CV_RAND_UNI, cvScalarAll(0), cvScalarAll(100) );
425     cvCircle( b, cvPoint(100,100), 70, cvScalar(0,255,0), -1, CV_AA, 0 );
426     cvAdd( a, b, a );
427     a.show( "test" );
428
429     cvWaitKey();
430 }*/
431
432 /* End of file. */
433