Home | History | Annotate | Download | only in src
      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 = NULL;
     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 
    213 /////////////////////////////// CvMatrix implementation //////////////////////////////////
    214 
    215 CvMatrix::CvMatrix( int rows, int cols, int type, CvMemStorage* storage, bool alloc_data )
    216 {
    217     if( storage )
    218     {
    219         matrix = (CvMat*)cvMemStorageAlloc( storage, sizeof(*matrix) );
    220         cvInitMatHeader( matrix, rows, cols, type, alloc_data ?
    221             cvMemStorageAlloc( storage, rows*cols*CV_ELEM_SIZE(type) ) : 0 );
    222     }
    223     else
    224         matrix = 0;
    225 }
    226 
    227 static CvMat*
    228 icvRetrieveMatrix( void* obj )
    229 {
    230     CvMat* m = 0;
    231 
    232     CV_FUNCNAME( "icvRetrieveMatrix" );
    233 
    234     __BEGIN__;
    235 
    236     if( CV_IS_MAT(obj) )
    237         m = (CvMat*)obj;
    238     else if( CV_IS_IMAGE(obj) )
    239     {
    240         IplImage* img = (IplImage*)obj;
    241         CvMat hdr, *src = cvGetMat( img, &hdr );
    242         CV_CALL( m = cvCreateMat( src->rows, src->cols, src->type ));
    243         CV_CALL( cvCopy( src, m ));
    244         cvReleaseImage( &img );
    245     }
    246     else if( obj )
    247     {
    248         cvRelease( &obj );
    249         CV_ERROR( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
    250     }
    251 
    252     __END__;
    253 
    254     return m;
    255 }
    256 
    257 
    258 bool CvMatrix::load( const char* filename, const char* matname, int color )
    259 {
    260     CvMat* m = 0;
    261 
    262     CV_FUNCNAME( "CvMatrix::read" );
    263 
    264     __BEGIN__;
    265 
    266     if( icvIsXmlOrYaml(filename) )
    267     {
    268         m = icvRetrieveMatrix(cvLoad(filename,0,matname));
    269 
    270         if( (CV_MAT_CN(m->type) > 1) != (color == 0) )
    271             CV_ERROR( CV_StsNotImplemented,
    272             "RGB<->Grayscale conversion is not implemented for matrices stored in XML/YAML" );
    273         /*{
    274             CvMat* temp_mat;
    275             CV_CALL( temp_mat = cvCreateMat( m->rows, m->cols,
    276                 CV_MAKETYPE(CV_MAT_DEPTH(m->type), color > 0 ? 3 : 1 )));
    277             cvCvtColor( m, temp_mat, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
    278             cvReleaseMat( &m );
    279             m = temp_mat;
    280         }*/
    281     }
    282     else
    283     {
    284         if( load_image_m )
    285             m = load_image_m( filename, color );
    286         else
    287             CV_ERROR( CV_StsNotImplemented,
    288             "Loading an image stored in such a format requires HigGUI.\n"
    289             "Link it to your program and call any function from it\n" );
    290     }
    291 
    292     set( m, false );
    293 
    294     __END__;
    295 
    296     return m != 0;
    297 }
    298 
    299 
    300 bool CvMatrix::read( CvFileStorage* fs, const char* mapname, const char* matname )
    301 {
    302     void* obj = 0;
    303     CvMat* m = 0;
    304 
    305     if( mapname )
    306     {
    307         CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
    308         if( !mapnode )
    309             obj = cvReadByName( fs, mapnode, matname );
    310     }
    311     else
    312         obj = cvReadByName( fs, 0, matname );
    313 
    314     m = icvRetrieveMatrix(obj);
    315     set( m, false );
    316     return m != 0;
    317 }
    318 
    319 
    320 bool CvMatrix::read( CvFileStorage* fs, const char* seqname, int idx )
    321 {
    322     void* obj = 0;
    323     CvMat* m = 0;
    324     CvFileNode* seqnode = seqname ?
    325         cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);
    326 
    327     if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
    328         obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
    329     m = icvRetrieveMatrix(obj);
    330     set( m, false );
    331     return m != 0;
    332 }
    333 
    334 
    335 void CvMatrix::save( const char* filename, const char* matname )
    336 {
    337     CV_FUNCNAME( "CvMatrix::write" );
    338     __BEGIN__;
    339 
    340     if( !matrix )
    341         return;
    342     if( icvIsXmlOrYaml( filename ) )
    343         cvSave( filename, matrix, matname );
    344     else
    345     {
    346         if( save_image )
    347             save_image( filename, matrix );
    348         else
    349             CV_ERROR( CV_StsNotImplemented,
    350             "Saving a matrixe in such a format requires HigGUI.\n"
    351             "Link it to your program and call any function from it\n" );
    352     }
    353 
    354     __END__;
    355 }
    356 
    357 
    358 void CvMatrix::write( CvFileStorage* fs, const char* matname )
    359 {
    360     if( matrix )
    361         cvWrite( fs, matname, matrix );
    362 }
    363 
    364 
    365 
    366 CV_IMPL int
    367 cvSetImageIOFunctions( CvLoadImageFunc _load_image, CvLoadImageMFunc _load_image_m,
    368                        CvSaveImageFunc _save_image, CvShowImageFunc _show_image=NULL )
    369 {
    370     load_image = _load_image;
    371     load_image_m = _load_image_m;
    372     save_image = _save_image;
    373     show_image = _show_image;
    374     return 1;
    375 }
    376 
    377 
    378 /*void main(void)
    379 {
    380     CvImage a(cvSize(300,200),8,3), b(cvSize(300,200),8,3);
    381     CvRNG rng = cvRNG(-1);
    382 
    383     CV_SET_IMAGE_IO_FUNCTIONS();
    384 
    385     cvNamedWindow( "test", 1 );
    386     //cvZero( a );
    387     cvZero( b );
    388     cvRandArr( &rng, a, CV_RAND_UNI, cvScalarAll(0), cvScalarAll(100) );
    389     cvCircle( b, cvPoint(100,100), 70, cvScalar(0,255,0), -1, CV_AA, 0 );
    390     cvAdd( a, b, a );
    391     a.show( "test" );
    392 
    393     cvWaitKey();
    394 }*/
    395 
    396 /* End of file. */
    397 
    398