ImageAccessCV.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 
00036 
00037 // ****************************************************************************
00038 // Includes
00039 // ****************************************************************************
00040 
00041 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00042 
00043 #include "ImageAccessCV.h"
00044 #include "IplImageAdaptor.h"
00045 #include "ByteImage.h"
00046 
00047 #include <stdio.h>
00048 
00049 
00050 
00051 bool ImageAccessCV::LoadFromFile(CByteImage *pImage, const char *pFilePath)
00052 {
00053         IplImage *pIplImage = cvLoadImage(pFilePath);
00054         if (!pIplImage)
00055                 return false;
00056                 
00057         if (pIplImage->nChannels != 3 && pIplImage->nChannels != 1)
00058         {
00059                 cvReleaseImage(&pIplImage);
00060                 return false;
00061         }
00062                 
00063         if (pImage->pixels && pImage->m_bOwnMemory)
00064                 delete [] pImage->pixels;
00065         
00066         pImage->type = pIplImage->nChannels == 1 ? CByteImage::eGrayScale : CByteImage::eRGB24;
00067         pImage->width = pIplImage->width;
00068         pImage->height = pIplImage->height;
00069         pImage->bytesPerPixel = pIplImage->nChannels;
00070         pImage->pixels = new unsigned char[pImage->width * pImage->height * pImage->bytesPerPixel];
00071         pImage->m_bOwnMemory = true;
00072 
00073         const int nPaddingBytes = pIplImage->widthStep - pImage->bytesPerPixel * pIplImage->width;
00074         const unsigned char *input = (unsigned char *) pIplImage->imageData;
00075         unsigned char *output = pImage->pixels;
00076         const int width = pImage->width;
00077         const int height = pImage->height;
00078         
00079         if (pImage->type == CByteImage::eGrayScale)
00080         {
00081                 for (int y = 0, i_input = 0, i_output = 0; y < height; y++, i_input += nPaddingBytes)
00082                 {
00083                         for (int x = 0; x < width; x++, i_input++, i_output++)
00084                                 output[i_output] = input[i_input];
00085                 }
00086         }
00087         else if (pImage->type == CByteImage::eRGB24)
00088         {
00089                 for (int y = 0, i_input = 0, i_output = 0; y < height; y++, i_input += nPaddingBytes)
00090                 {
00091                         for (int x = 0; x < width; x++, i_input += 3, i_output += 3)
00092                         {
00093                                 output[i_output] = input[i_input + 2];
00094                                 output[i_output + 1] = input[i_input + 1];
00095                                 output[i_output + 2] = input[i_input];
00096                         }
00097                 }
00098         }
00099         else
00100         {
00101                 printf("error: CByteImage::eRGB24Split not supported by ImageAccessCV::LoadFromFile\n");
00102         }
00103         
00104         cvReleaseImage(&pIplImage);
00105                 
00106         return true;
00107 }
00108 
00109 
00110 bool ImageAccessCV::SaveToFile(const CByteImage *pImage, const char *pFilePath)
00111 {
00112         int nRet;
00113         
00114         if (pImage->type == CByteImage::eGrayScale)
00115         {
00116                 IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
00117                 nRet = cvSaveImage(pFilePath, pIplImage);
00118                 cvReleaseImageHeader(&pIplImage);
00119         }
00120         else if (pImage->type == CByteImage::eRGB24)
00121         {
00122                 const int width = pImage->width;
00123                 const int height = pImage->height;
00124                 const int nBytes = width * height * 3;
00125                 
00126                 CByteImage tempImage(width, height, pImage->type);
00127                 
00128                 const unsigned char *input = pImage->pixels;
00129                 unsigned char *output = tempImage.pixels;
00130                 
00131                 for (int i = 0; i < nBytes; i += 3)
00132                 {
00133                         output[i] = input[i + 2];
00134                         output[i + 1] = input[i + 1];
00135                         output[i + 2] = input[i];
00136                 }
00137                 
00138                 IplImage *pIplImage = IplImageAdaptor::Adapt(&tempImage);
00139                 nRet = cvSaveImage(pFilePath, pIplImage);
00140                 cvReleaseImageHeader(&pIplImage);
00141         }
00142         else
00143         {
00144                 printf("error: CByteImage::eRGB24Split not supported by ImageAccessCV::SaveToFile\n");
00145         }
00146         
00147         
00148         return nRet != 0;
00149 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57