image.cpp 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/* ////////////////////////////////////////////////////////////////////
//
//  C++ classes for image and matrices
//
// */

#include "precomp.hpp"
49 50 51 52
#include "opencv2/opencv_modules.hpp"
#ifdef HAVE_OPENCV_HIGHGUI
#  include "opencv2/highgui/highgui_c.h"
#endif
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

/////////////////////////////// CvImage implementation //////////////////////////////////

static bool
icvIsXmlOrYaml( const char* filename )
{
    const char* suffix = strrchr( filename, '.' );
    return suffix &&
        (strcmp( suffix, ".xml" ) == 0 ||
        strcmp( suffix, ".Xml" ) == 0 ||
        strcmp( suffix, ".XML" ) == 0 ||
        strcmp( suffix, ".yml" ) == 0 ||
        strcmp( suffix, ".Yml" ) == 0 ||
        strcmp( suffix, ".YML" ) == 0 ||
        strcmp( suffix, ".yaml" ) == 0 ||
        strcmp( suffix, ".Yaml" ) == 0 ||
        strcmp( suffix, ".YAML" ) == 0);
}


static IplImage*
icvRetrieveImage( void* obj )
{
    IplImage* img = 0;

    if( CV_IS_IMAGE(obj) )
        img = (IplImage*)obj;
    else if( CV_IS_MAT(obj) )
    {
        CvMat* m = (CvMat*)obj;
        img = cvCreateImageHeader( cvSize(m->cols,m->rows),
                        CV_MAT_DEPTH(m->type), CV_MAT_CN(m->type) );
        cvSetData( img, m->data.ptr, m->step );
        img->imageDataOrigin = (char*)m->refcount;
        m->data.ptr = 0; m->step = 0;
        cvReleaseMat( &m );
    }
    else if( obj )
    {
        cvRelease( &obj );
        CV_Error( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
    }

    return img;
}


bool CvImage::load( const char* filename, const char* imgname, int color )
{
    IplImage* img = 0;

    if( icvIsXmlOrYaml(filename) )
    {
        img = icvRetrieveImage(cvLoad(filename,0,imgname));
        if( (img->nChannels > 1) != (color == 0) )
            CV_Error( CV_StsNotImplemented,
            "RGB<->Grayscale conversion is not implemented for images stored in XML/YAML" );
        /*{
            IplImage* temp_img = 0;
            temp_img = cvCreateImage( cvGetSize(img), img->depth, color > 0 ? 3 : 1 ));
            cvCvtColor( img, temp_img, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
            cvReleaseImage( &img );
            img = temp_img;
        }*/
    }
118
#ifdef HAVE_OPENCV_HIGHGUI
119 120
    else
        img = cvLoadImage( filename, color );
121
#endif
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    attach( img );
    return img != 0;
}


bool CvImage::read( CvFileStorage* fs, const char* mapname, const char* imgname )
{
    void* obj = 0;
    IplImage* img = 0;

    if( mapname )
    {
        CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
        if( !mapnode )
            obj = cvReadByName( fs, mapnode, imgname );
    }
    else
        obj = cvReadByName( fs, 0, imgname );

    img = icvRetrieveImage(obj);
    attach( img );
    return img != 0;
}


bool CvImage::read( CvFileStorage* fs, const char* seqname, int idx )
{
    void* obj = 0;
    IplImage* img = 0;
    CvFileNode* seqnode = seqname ?
        cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);

    if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
        obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
    img = icvRetrieveImage(obj);
    attach( img );
    return img != 0;
}


void CvImage::save( const char* filename, const char* imgname, const int* params )
{
    if( !image )
        return;
    if( icvIsXmlOrYaml( filename ) )
        cvSave( filename, image, imgname );
169
#ifdef HAVE_OPENCV_HIGHGUI
170 171
    else
        cvSaveImage( filename, image, params );
172 173 174
#else
    (void)params;
#endif
175 176 177 178 179 180 181 182 183 184 185 186
}


void CvImage::write( CvFileStorage* fs, const char* imgname )
{
    if( image )
        cvWrite( fs, imgname, image );
}


void CvImage::show( const char* window_name )
{
187
#ifdef HAVE_OPENCV_HIGHGUI
188 189
    if( image )
        cvShowImage( window_name, image );
190 191 192
#else
    (void)window_name;
#endif
193 194 195 196 197
}


/////////////////////////////// CvMatrix implementation //////////////////////////////////

198
CvMatrix::CvMatrix( int _rows, int _cols, int _type, CvMemStorage* storage, bool alloc_data )
199 200 201 202
{
    if( storage )
    {
        matrix = (CvMat*)cvMemStorageAlloc( storage, sizeof(*matrix) );
203 204
        cvInitMatHeader( matrix, _rows, _cols, _type, alloc_data ?
            cvMemStorageAlloc( storage, _rows*_cols*CV_ELEM_SIZE(_type) ) : 0 );
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    }
    else
        matrix = 0;
}

static CvMat*
icvRetrieveMatrix( void* obj )
{
    CvMat* m = 0;

    if( CV_IS_MAT(obj) )
        m = (CvMat*)obj;
    else if( CV_IS_IMAGE(obj) )
    {
        IplImage* img = (IplImage*)obj;
        CvMat hdr, *src = cvGetMat( img, &hdr );
        m = cvCreateMat( src->rows, src->cols, src->type );
        cvCopy( src, m );
        cvReleaseImage( &img );
    }
    else if( obj )
    {
        cvRelease( &obj );
        CV_Error( CV_StsUnsupportedFormat, "The object is neither an image, nor a matrix" );
    }

    return m;
}


bool CvMatrix::load( const char* filename, const char* matname, int color )
{
    CvMat* m = 0;
    if( icvIsXmlOrYaml(filename) )
    {
        m = icvRetrieveMatrix(cvLoad(filename,0,matname));

        if( (CV_MAT_CN(m->type) > 1) != (color == 0) )
            CV_Error( CV_StsNotImplemented,
            "RGB<->Grayscale conversion is not implemented for matrices stored in XML/YAML" );
        /*{
            CvMat* temp_mat;
            temp_mat = cvCreateMat( m->rows, m->cols,
                CV_MAKETYPE(CV_MAT_DEPTH(m->type), color > 0 ? 3 : 1 )));
            cvCvtColor( m, temp_mat, color > 0 ? CV_GRAY2BGR : CV_BGR2GRAY );
            cvReleaseMat( &m );
            m = temp_mat;
        }*/
    }
254
#ifdef HAVE_OPENCV_HIGHGUI
255 256
    else
        m = cvLoadImageM( filename, color );
257
#endif
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

    set( m, false );
    return m != 0;
}


bool CvMatrix::read( CvFileStorage* fs, const char* mapname, const char* matname )
{
    void* obj = 0;
    CvMat* m = 0;

    if( mapname )
    {
        CvFileNode* mapnode = cvGetFileNodeByName( fs, 0, mapname );
        if( !mapnode )
            obj = cvReadByName( fs, mapnode, matname );
    }
    else
        obj = cvReadByName( fs, 0, matname );

    m = icvRetrieveMatrix(obj);
    set( m, false );
    return m != 0;
}


bool CvMatrix::read( CvFileStorage* fs, const char* seqname, int idx )
{
    void* obj = 0;
    CvMat* m = 0;
    CvFileNode* seqnode = seqname ?
        cvGetFileNodeByName( fs, 0, seqname ) : cvGetRootFileNode(fs,0);

    if( seqnode && CV_NODE_IS_SEQ(seqnode->tag) )
        obj = cvRead( fs, (CvFileNode*)cvGetSeqElem( seqnode->data.seq, idx ));
    m = icvRetrieveMatrix(obj);
    set( m, false );
    return m != 0;
}


void CvMatrix::save( const char* filename, const char* matname, const int* params )
{
    if( !matrix )
        return;
    if( icvIsXmlOrYaml( filename ) )
        cvSave( filename, matrix, matname );
305
#ifdef HAVE_OPENCV_HIGHGUI
306 307
    else
        cvSaveImage( filename, matrix, params );
308 309 310
#else
    (void)params;
#endif
311 312 313 314 315 316 317 318 319 320 321 322
}


void CvMatrix::write( CvFileStorage* fs, const char* matname )
{
    if( matrix )
        cvWrite( fs, matname, matrix );
}


void CvMatrix::show( const char* window_name )
{
323
#ifdef HAVE_OPENCV_HIGHGUI
324 325
    if( matrix )
        cvShowImage( window_name, matrix );
326 327 328
#else
    (void)window_name;
#endif
329 330 331 332
}


/* End of file. */