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 
37 // ****************************************************************************
38 // Includes
39 // ****************************************************************************
40 
41 #include <new> // for explicitly using correct new/delete operators on VC DSPs
42 
43 #include "ImageAccessCV.h"
44 #include "IplImageAdaptor.h"
45 #include "ByteImage.h"
46 
47 #include <stdio.h>
48 
49 
50 
51 bool ImageAccessCV::LoadFromFile(CByteImage *pImage, const char *pFilePath)
52 {
53  IplImage *pIplImage = cvLoadImage(pFilePath);
54  if (!pIplImage)
55  return false;
56 
57  if (pIplImage->nChannels != 3 && pIplImage->nChannels != 1)
58  {
59  cvReleaseImage(&pIplImage);
60  return false;
61  }
62 
63  if (pImage->pixels && pImage->m_bOwnMemory)
64  delete [] pImage->pixels;
65 
66  pImage->type = pIplImage->nChannels == 1 ? CByteImage::eGrayScale : CByteImage::eRGB24;
67  pImage->width = pIplImage->width;
68  pImage->height = pIplImage->height;
69  pImage->bytesPerPixel = pIplImage->nChannels;
70  pImage->pixels = new unsigned char[pImage->width * pImage->height * pImage->bytesPerPixel];
71  pImage->m_bOwnMemory = true;
72 
73  const int nPaddingBytes = pIplImage->widthStep - pImage->bytesPerPixel * pIplImage->width;
74  const unsigned char *input = (unsigned char *) pIplImage->imageData;
75  unsigned char *output = pImage->pixels;
76  const int width = pImage->width;
77  const int height = pImage->height;
78 
79  if (pImage->type == CByteImage::eGrayScale)
80  {
81  for (int y = 0, i_input = 0, i_output = 0; y < height; y++, i_input += nPaddingBytes)
82  {
83  for (int x = 0; x < width; x++, i_input++, i_output++)
84  output[i_output] = input[i_input];
85  }
86  }
87  else if (pImage->type == CByteImage::eRGB24)
88  {
89  for (int y = 0, i_input = 0, i_output = 0; y < height; y++, i_input += nPaddingBytes)
90  {
91  for (int x = 0; x < width; x++, i_input += 3, i_output += 3)
92  {
93  output[i_output] = input[i_input + 2];
94  output[i_output + 1] = input[i_input + 1];
95  output[i_output + 2] = input[i_input];
96  }
97  }
98  }
99  else
100  {
101  printf("error: CByteImage::eRGB24Split not supported by ImageAccessCV::LoadFromFile\n");
102  }
103 
104  cvReleaseImage(&pIplImage);
105 
106  return true;
107 }
108 
109 
110 bool ImageAccessCV::SaveToFile(const CByteImage *pImage, const char *pFilePath)
111 {
112  int nRet;
113 
114  if (pImage->type == CByteImage::eGrayScale)
115  {
116  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
117  nRet = cvSaveImage(pFilePath, pIplImage);
118  cvReleaseImageHeader(&pIplImage);
119  }
120  else if (pImage->type == CByteImage::eRGB24)
121  {
122  const int width = pImage->width;
123  const int height = pImage->height;
124  const int nBytes = width * height * 3;
125 
126  CByteImage tempImage(width, height, pImage->type);
127 
128  const unsigned char *input = pImage->pixels;
129  unsigned char *output = tempImage.pixels;
130 
131  for (int i = 0; i < nBytes; i += 3)
132  {
133  output[i] = input[i + 2];
134  output[i + 1] = input[i + 1];
135  output[i + 2] = input[i];
136  }
137 
138  IplImage *pIplImage = IplImageAdaptor::Adapt(&tempImage);
139  nRet = cvSaveImage(pFilePath, pIplImage);
140  cvReleaseImageHeader(&pIplImage);
141  }
142  else
143  {
144  printf("error: CByteImage::eRGB24Split not supported by ImageAccessCV::SaveToFile\n");
145  }
146 
147 
148  return nRet != 0;
149 }
int width
The width of the image in pixels.
Definition: ByteImage.h:257
bool SaveToFile(const CByteImage *pImage, const char *pFilePath)
Saves an image to a file.
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
bool m_bOwnMemory
Flag signaling if memory is to be freed or not.
Definition: ByteImage.h:301
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
GLenum GLint x
Definition: glext.h:3125
bool LoadFromFile(CByteImage *pImage, const char *pFilePath)
Loads an image from a file.
int height
The height of the image in pixels.
Definition: ByteImage.h:264
int bytesPerPixel
The number of bytes used for encoding one pixel.
Definition: ByteImage.h:273
GLenum GLsizei width
Definition: glext.h:3122
GLenum GLsizei GLsizei height
Definition: glext.h:3132
GLenum GLenum GLenum input
Definition: glext.h:5307
ImageType type
The type of the image.
Definition: ByteImage.h:292
GLenum GLint GLint y
Definition: glext.h:3125
IplImage * Adapt(const CByteImage *pImage, bool bAllocateMemory=false)
Converts a CByteImage to an IplImage.


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28