16acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/*M///////////////////////////////////////////////////////////////////////////////////////
26acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
36acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
46acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
56acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  By downloading, copying, installing or using the software you agree to this license.
66acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  If you do not agree to this license, do not download, install,
76acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  copy or use the software.
86acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
96acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                        Intel License Agreement
116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//                For Open Source Computer Vision Library
126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Copyright (C) 2000, Intel Corporation, all rights reserved.
146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Third party copyrights are property of their respective owners.
156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// Redistribution and use in source and binary forms, with or without modification,
176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// are permitted provided that the following conditions are met:
186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's of source code must retain the above copyright notice,
206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer.
216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * Redistribution's in binary form must reproduce the above copyright notice,
236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     this list of conditions and the following disclaimer in the documentation
246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     and/or other materials provided with the distribution.
256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//   * The name of Intel Corporation may not be used to endorse or promote products
276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//     derived from this software without specific prior written permission.
286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// This software is provided by the copyright holders and contributors "as is" and
306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// any express or implied warranties, including, but not limited to, the implied
316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// warranties of merchantability and fitness for a particular purpose are disclaimed.
326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// In no event shall the Intel Corporation or contributors be liable for any direct,
336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// indirect, incidental, special, exemplary, or consequential damages
346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// (including, but not limited to, procurement of substitute goods or services;
356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// loss of use, data, or profits; or business interruption) however caused
366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// and on any theory of liability, whether in contract, strict liability,
376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// or tort (including negligence or otherwise) arising in any way out of
386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// the use of this software, even if advised of the possibility of such damage.
396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//M*/
416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//  Loading and saving IPL images.
446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn//
456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "_highgui.h"
476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#include "grfmts.h"
486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#if 0
506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************\
516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn*                              Path class (list of search folders)                       *
526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn\****************************************************************************************/
536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennclass  CvFilePath
556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennpublic:
576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvFilePath();
586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ~CvFilePath();
596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // preprocess folder or file name - calculate its length,
616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // check for invalid symbols in the name and substitute
626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // all backslashes with simple slashes.
636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // the result is put into the specified buffer
646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    static int Preprocess( const char* filename, char* buffer );
656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // add folder to the path
676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    bool Add( const char* path );
686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // clear the path
706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    void Clear();
716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // return the path - string, where folders are separated by ';'
736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const char* Get() const { return m_path; };
746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // find the file in the path
766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const char* Find( const char* filename, char* buffer ) const;
776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // return the first folder from the path
796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // the returned string is not terminated by '\0'!!!
806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // its length is returned via len parameter
816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const char* First( int& len ) const;
826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // return the folder, next in the path after the specified folder.
846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    // see also note to First() method
856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const char* Next( const char* folder, int& len ) const;
866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennprotected:
886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    char* m_path;
906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int m_maxsize;
916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int m_len;
926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn};
936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennvoid CvFilePath::Clear()
966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    delete[] m_path;
986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_maxsize = m_len = 0;
996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvFilePath::CvFilePath()
1036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_path = 0;
1056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_maxsize = m_len = 0;
1066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvFilePath::~CvFilePath()
1106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    Clear();
1126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennbool  CvFilePath::Add( const char* path )
1166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    char buffer[_MAX_PATH + 1];
1186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int len = Preprocess( path, buffer );
1196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( len < 0 )
1216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return false;
1226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( m_len + len + 3 // +1 for one more ';',
1246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        // another +1 for possible additional '/',
1256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                        // and the last +1 is for '\0'
1266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                      > m_maxsize )
1276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        int new_size = (m_len + len + 3 + 1023) & -1024;
1296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        char* new_path = new char[new_size];
1306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( m_path )
1326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
1336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            memcpy( new_path, m_path, m_len );
1346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            delete[] m_path;
1356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
1366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        m_path = new_path;
1386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        m_maxsize = new_size;
1396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_path[m_len++] = ';';
1426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    memcpy( m_path + m_len, buffer, len );
1436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_len += len;
1446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( m_path[m_len] != '/' )
1466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        m_path[m_len++] = '/';
1476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_path[m_len] = '\0'; // '\0' is not counted in m_len.
1496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return true;
1516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennconst char* CvFilePath::First( int& len ) const
1556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const char* path = (const char*)(m_path ? m_path : "");
1576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const char* path_end = path;
1586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    while( *path_end && *path_end != ';' )
1606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        path_end++;
1616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    len = path_end - path;
1636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return path;
1646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennconst char* CvFilePath::Next( const char* folder, int& len ) const
1686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !folder || folder < m_path || folder >= m_path + m_len )
1706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return 0;
1716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    folder = strchr( folder, ';' );
1736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( folder )
1746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
1756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        const char* folder_end = ++folder;
1766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        while( *folder_end && *folder_end != ';' )
1776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            folder_end++;
1786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        len = folder_end - folder;
1806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
1816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return folder;
1836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
1846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennconst char* CvFilePath::Find( const char* filename, char* buffer ) const
1876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
1886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    char path0[_MAX_PATH + 1];
1896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int len = Preprocess( filename, path0 );
1906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int folder_len = 0;
1916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    const char* folder = First( folder_len );
1926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    char* name_only = 0;
1936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    char* name = path0;
1946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    FILE* f = 0;
1956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( len < 0 )
1976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return 0;
1986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
1996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    do
2006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( folder_len + len <= _MAX_PATH )
2026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            memcpy( buffer, folder, folder_len );
2046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            strcpy( buffer + folder_len, name );
2056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            f = fopen( buffer, "rb" );
2076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( f )
2086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                break;
2096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( name != name_only )
2126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
2136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            name_only = strrchr( path0, '/' );
2146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            if( !name_only )
2156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                name_only = path0;
2166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            else
2176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                name_only++;
2186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            len = strlen( name_only );
2196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            name = name_only;
2206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
2216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    while( (folder = Next( folder, folder_len )) != 0 );
2236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    filename = 0;
2256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( f )
2276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        filename = (const char*)buffer;
2296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        fclose(f);
2306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return filename;
2336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennint CvFilePath::Preprocess( const char* str, char* buffer )
2376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
2386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int i;
2396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !str || !buffer )
2416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return -1;
2426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    for( i = 0; i <= _MAX_PATH; i++ )
2446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
2456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        buffer[i] = str[i];
2466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( isalnum(str[i])) // fast check to skip most of characters
2486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            continue;
2496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( str[i] == '\0' )
2516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            break;
2526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( str[i] == '\\' ) // convert back slashes to simple slashes
2546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                             // (for Win32-*NIX compatibility)
2556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            buffer[i] = '/';
2566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if (str[i] == '*' || str[i] == '?' || str[i] == '\"' ||
2586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            str[i] == '>' || str[i] == '<' ||
2596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            str[i] == ';' || /* used as a separator in the path */
2606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        #ifndef WIN32
2616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            str[i] == ',' || str[i] == '%' ||
2626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        #endif
2636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            str[i] == '|')
2646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            return -1;
2656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
2666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return i <= _MAX_PATH ? i : -1;
2686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
2696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
2706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************\
2726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn*                              Image Readers & Writers Class                             *
2736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn\****************************************************************************************/
2746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennclass  CvImageFilters
2766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
2776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennpublic:
2786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvImageFilters();
2806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ~CvImageFilters();
2816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    GrFmtReader* FindReader( const char* filename ) const;
2836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    GrFmtWriter* FindWriter( const char* filename ) const;
2846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    //const CvFilePath& Path() const { return (const CvFilePath&)m_path; };
2866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    //CvFilePath& Path() { return m_path; };
2876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennprotected:
2896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    GrFmtFactoriesList*  m_factories;
2916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn};
2926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvImageFilters::CvImageFilters()
2956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
2966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories = new GrFmtFactoriesList;
2976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
2986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#ifdef HAVE_IMAGEIO
2996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtImageIO() );
3006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
3016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtBmp() );
3026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtJpeg() );
3036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtSunRaster() );
3046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtPxM() );
3056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtTiff() );
3066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#ifdef HAVE_PNG
3076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtPng() );
3086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
3096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#ifdef HAVE_JASPER
3106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtJpeg2000() );
3116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
3126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#ifdef HAVE_ILMIMF
3136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    m_factories->AddFactory( new GrFmtExr() );
3146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
3156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCvImageFilters::~CvImageFilters()
3196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    delete m_factories;
3216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennGrFmtReader* CvImageFilters::FindReader( const char* filename ) const
3256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return m_factories->FindReader( filename );
3276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennGrFmtWriter* CvImageFilters::FindWriter( const char* filename ) const
3316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return m_factories->FindWriter( filename );
3336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/****************************************************************************************\
3366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn*                         HighGUI loading & saving function implementation               *
3376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn\****************************************************************************************/
3386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennstatic int icvSetCXCOREBindings(void)
3406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return CV_SET_IMAGE_IO_FUNCTIONS();
3426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennint cxcore_bindings_initialized = icvSetCXCOREBindings();
3456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn// global image I/O filters
3476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennstatic CvImageFilters  g_Filters;
3486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#if 0
3506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL void
3516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvAddSearchPath( const char* path )
3526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_FUNCNAME( "cvAddSearchPath" );
3546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __BEGIN__;
3566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !path || strlen(path) == 0 )
3586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsNullPtr, "Null path" );
3596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    g_Filters.AddPath( path );
3616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __END__;
3636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn#endif
3656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL int
3676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvHaveImageReader( const char* filename )
3686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    GrFmtReader* reader = g_Filters.FindReader( filename );
3706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( reader ) {
3716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        delete reader;
3726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return 1;
3736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return 0;
3756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL int cvHaveImageWriter( const char* filename )
3786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    GrFmtWriter* writer = g_Filters.FindWriter( filename );
3806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( writer ) {
3816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        delete writer;
3826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        return 1;
3836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
3846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return 0;
3856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
3866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Rennstatic void*
3886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennicvLoadImage( const char* filename, int flags, bool load_as_matrix )
3896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
3906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    GrFmtReader* reader = 0;
3916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    IplImage* image = 0;
3926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMat hdr, *matrix = 0;
3936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int depth = 8;
3946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_FUNCNAME( "cvLoadImage" );
3966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __BEGIN__;
3986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
3996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvSize size;
4006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int iscolor;
4016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int cn;
4026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !filename || strlen(filename) == 0 )
4046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsNullPtr, "null filename" );
4056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    reader = g_Filters.FindReader( filename );
4076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !reader )
4086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        EXIT;
4096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !reader->ReadHeader() )
4116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        EXIT;
4126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    size.width = reader->GetWidth();
4146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    size.height = reader->GetHeight();
4156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( flags == -1 )
4176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        iscolor = reader->IsColor();
4186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
4196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
4206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( (flags & CV_LOAD_IMAGE_COLOR) != 0 ||
4216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn           ((flags & CV_LOAD_IMAGE_ANYCOLOR) != 0 && reader->IsColor()) )
4226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            iscolor = 1;
4236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
4246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            iscolor = 0;
4256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( (flags & CV_LOAD_IMAGE_ANYDEPTH) != 0 )
4276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        {
4286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            reader->UseNativeDepth(true);
4296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            depth = reader->GetDepth();
4306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        }
4316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
4326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cn = iscolor ? 3 : 1;
4346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( load_as_matrix )
4366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
4376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        int type;
4386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if(reader->IsFloat() && depth != 8)
4396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            type = CV_32F;
4406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
4416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            type = ( depth <= 8 ) ? CV_8U : ( depth <= 16 ) ? CV_16U : CV_32S;
4426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL( matrix = cvCreateMat( size.height, size.width, CV_MAKETYPE(type, cn) ));
4436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
4446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    else
4456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
4466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        int type;
4476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if(reader->IsFloat() && depth != 8)
4486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            type = IPL_DEPTH_32F;
4496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
4506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            type = ( depth <= 8 ) ? IPL_DEPTH_8U : ( depth <= 16 ) ? IPL_DEPTH_16U : IPL_DEPTH_32S;
4516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL( image = cvCreateImage( size, type, cn ));
4526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        matrix = cvGetMat( image, &hdr );
4536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
4546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !reader->ReadData( matrix->data.ptr, matrix->step, iscolor ))
4566acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
4576acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( load_as_matrix )
4586acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            cvReleaseMat( &matrix );
4596acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
4606acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            cvReleaseImage( &image );
4616acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        EXIT;
4626acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
4636acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4646acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __END__;
4656acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4666acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    delete reader;
4676acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4686acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( cvGetErrStatus() < 0 )
4696acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
4706acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        if( load_as_matrix )
4716acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            cvReleaseMat( &matrix );
4726acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        else
4736acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            cvReleaseImage( &image );
4746acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
4756acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4766acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return load_as_matrix ? (void*)matrix : (void*)image;
4776acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
4786acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4796acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4806acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL IplImage*
4816acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvLoadImage( const char* filename, int iscolor )
4826acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
4836acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return (IplImage*)icvLoadImage( filename, iscolor, false );
4846acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
4856acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4866acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL CvMat*
4876acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvLoadImageM( const char* filename, int iscolor )
4886acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
4896acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return (CvMat*)icvLoadImage( filename, iscolor, true );
4906acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
4916acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4926acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
4936acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RennCV_IMPL int
4946acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius RenncvSaveImage( const char* filename, const CvArr* arr )
4956acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn{
4966acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int origin = 0;
4976acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    GrFmtWriter* writer = 0;
4986acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMat *temp = 0, *temp2 = 0;
4996acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5006acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_FUNCNAME( "cvSaveImage" );
5016acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5026acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __BEGIN__;
5036acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5046acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CvMat stub, *image;
5056acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    int channels, ipl_depth;
5066acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5076acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !filename || strlen(filename) == 0 )
5086acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsNullPtr, "null filename" );
5096acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5106acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    CV_CALL( image = cvGetMat( arr, &stub ));
5116acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5126acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( CV_IS_IMAGE( arr ))
5136acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        origin = ((IplImage*)arr)->origin;
5146acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5156acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    channels = CV_MAT_CN( image->type );
5166acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( channels != 1 && channels != 3 && channels != 4 )
5176acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_BadNumChannels, "" );
5186acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5196acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    writer = g_Filters.FindWriter( filename );
5206acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !writer )
5216acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsError, "could not find a filter for the specified extension" );
5226acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5236acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( origin )
5246acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
5256acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL( temp = cvCreateMat(image->rows, image->cols, image->type) );
5266acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL( cvFlip( image, temp, 0 ));
5276acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        image = temp;
5286acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
5296acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5306acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    ipl_depth = cvCvToIplDepth(image->type);
5316acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5326acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !writer->IsFormatSupported(ipl_depth) )
5336acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    {
5346acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        assert( writer->IsFormatSupported(IPL_DEPTH_8U) );
5356acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL( temp2 = cvCreateMat(image->rows,
5366acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn            image->cols, CV_MAKETYPE(CV_8U,channels)) );
5376acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_CALL( cvConvertImage( image, temp2 ));
5386acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        image = temp2;
5396acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        ipl_depth = IPL_DEPTH_8U;
5406acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    }
5416acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5426acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    if( !writer->WriteImage( image->data.ptr, image->step, image->width,
5436acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn                             image->height, ipl_depth, channels ))
5446acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn        CV_ERROR( CV_StsError, "could not save the image" );
5456acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5466acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    __END__;
5476acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5486acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    delete writer;
5496acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvReleaseMat( &temp );
5506acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    cvReleaseMat( &temp2 );
5516acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5526acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn    return cvGetErrStatus() >= 0;
5536acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn}
5546acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn
5556acb9a7ea3d7564944e12cbc73a857b88c1301eeMarius Renn/* End of file. */
556