IVT
ImageAccessCV.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: ImageAccessCV.cpp
37 // Author: Pedram Azad
38 // Date: 2005
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "ImageAccessCV.h"
49 #include "IplImageAdaptor.h"
50 #include "ByteImage.h"
51 
52 #include <highgui.h>
53 #include <stdio.h>
54 
55 
56 
57 bool ImageAccessCV::LoadFromFile(CByteImage *pImage, const char *pFilePath)
58 {
59  IplImage *pIplImage = cvLoadImage(pFilePath);
60  if (!pIplImage)
61  return false;
62 
63  if (pIplImage->nChannels != 3 && pIplImage->nChannels != 1)
64  {
65  cvReleaseImage(&pIplImage);
66  return false;
67  }
68 
69  if (pImage->pixels && pImage->m_bOwnMemory)
70  delete [] pImage->pixels;
71 
72  pImage->type = pIplImage->nChannels == 1 ? CByteImage::eGrayScale : CByteImage::eRGB24;
73  pImage->width = pIplImage->width;
74  pImage->height = pIplImage->height;
75  pImage->bytesPerPixel = pIplImage->nChannels;
76  pImage->pixels = new unsigned char[pImage->width * pImage->height * pImage->bytesPerPixel];
77  pImage->m_bOwnMemory = true;
78 
79  const int nPaddingBytes = pIplImage->widthStep - pImage->bytesPerPixel * pIplImage->width;
80  const unsigned char *input = (unsigned char *) pIplImage->imageData;
81  unsigned char *output = pImage->pixels;
82  const int width = pImage->width;
83  const int height = pImage->height;
84 
85  if (pImage->type == CByteImage::eGrayScale)
86  {
87  for (int y = 0, i_input = 0, i_output = 0; y < height; y++, i_input += nPaddingBytes)
88  {
89  for (int x = 0; x < width; x++, i_input++, i_output++)
90  output[i_output] = input[i_input];
91  }
92  }
93  else if (pImage->type == CByteImage::eRGB24)
94  {
95  for (int y = 0, i_input = 0, i_output = 0; y < height; y++, i_input += nPaddingBytes)
96  {
97  for (int x = 0; x < width; x++, i_input += 3, i_output += 3)
98  {
99  output[i_output] = input[i_input + 2];
100  output[i_output + 1] = input[i_input + 1];
101  output[i_output + 2] = input[i_input];
102  }
103  }
104  }
105  else
106  {
107  printf("error: CByteImage::eRGB24Split not supported by ImageAccessCV::LoadFromFile\n");
108  }
109 
110  cvReleaseImage(&pIplImage);
111 
112  return true;
113 }
114 
115 
116 bool ImageAccessCV::SaveToFile(const CByteImage *pImage, const char *pFilePath)
117 {
118  int nRet;
119 
120  if (pImage->type == CByteImage::eGrayScale)
121  {
122  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
123  nRet = cvSaveImage(pFilePath, pIplImage);
124  cvReleaseImageHeader(&pIplImage);
125  }
126  else if (pImage->type == CByteImage::eRGB24)
127  {
128  const int width = pImage->width;
129  const int height = pImage->height;
130  const int nBytes = width * height * 3;
131 
132  CByteImage tempImage(width, height, pImage->type);
133 
134  const unsigned char *input = pImage->pixels;
135  unsigned char *output = tempImage.pixels;
136 
137  for (int i = 0; i < nBytes; i += 3)
138  {
139  output[i] = input[i + 2];
140  output[i + 1] = input[i + 1];
141  output[i + 2] = input[i];
142  }
143 
144  IplImage *pIplImage = IplImageAdaptor::Adapt(&tempImage);
145  nRet = cvSaveImage(pFilePath, pIplImage);
146  cvReleaseImageHeader(&pIplImage);
147  }
148  else
149  {
150  printf("error: CByteImage::eRGB24Split not supported by ImageAccessCV::SaveToFile\n");
151  }
152 
153 
154  return nRet != 0;
155 }
bool m_bOwnMemory
Flag signaling if memory is to be freed or not.
Definition: ByteImage.h:301
bool LoadFromFile(CByteImage *pImage, const char *pFilePath)
Loads an image from a file.
IplImage * Adapt(const CByteImage *pImage, bool bAllocateMemory=false)
Converts a CByteImage to an IplImage.
ImageType type
The type of the image.
Definition: ByteImage.h:292
int width
The width of the image in pixels.
Definition: ByteImage.h:257
int height
The height of the image in pixels.
Definition: ByteImage.h:264
bool SaveToFile(const CByteImage *pImage, const char *pFilePath)
Saves an image to a file.
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
int bytesPerPixel
The number of bytes used for encoding one pixel.
Definition: ByteImage.h:273