ImageMapper.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: ImageMapper.cpp
37 // Author: Pedram Azad
38 // Date: 04.10.2008
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "ImageMapper.h"
49 
50 #include "Image/ByteImage.h"
51 #include "Image/ImageProcessor.h"
53 #include "Helpers/helpers.h"
54 
55 #include <stdio.h>
56 #include <math.h>
57 
58 
59 
60 // ****************************************************************************
61 // Constructor / Destructor
62 // ****************************************************************************
63 
64 CImageMapper::CImageMapper(bool bInterpolate)
65 {
66  m_bInterpolate = bInterpolate;
67 
68  m_pOffsetMap = 0;
69  m_pCoordinateMap = 0;
70 
71  width = height = 0;
72 
73  m_bMapComputed = false;
74 }
75 
77 {
78  if (m_pOffsetMap)
79  delete [] m_pOffsetMap;
80 
81  if (m_pCoordinateMap)
82  delete [] m_pCoordinateMap;
83 }
84 
85 
86 // ****************************************************************************
87 // Methods
88 // ****************************************************************************
89 
91 {
92  if (width != this->width || height != this->height)
93  {
94  this->width = width;
95  this->height = height;
96 
97  if (m_pOffsetMap)
98  delete [] m_pOffsetMap;
99 
100  m_pOffsetMap = new int[width * height];
101 
102  if (m_bInterpolate)
103  {
104  if (m_pCoordinateMap)
105  delete [] m_pCoordinateMap;
106 
107  m_pCoordinateMap = new MapCoordinates[width * height];
108  }
109  }
110 
111  // compute map
112  for (int i = 0, offset = 0; i < height; i++)
113  {
114  for (int j = 0; j < width; j++, offset++)
115  {
116  const Vec2d newCoordinates = { float(j), float(i) };
117 
118  // call virtual method
119  Vec2d originalCoordinates;
120  ComputeOriginalCoordinates(newCoordinates, originalCoordinates);
121 
122  const float u = originalCoordinates.x;
123  const float v = originalCoordinates.y;
124 
125  const int u_int = m_bInterpolate ? int(floor(u)) : my_round(u);
126  const int v_int = m_bInterpolate ? int(floor(v)) : my_round(v);
127 
128  if (u_int >= 0 && u_int < width - 1 && v_int >= 0 && v_int < height - 1)
129  {
130  m_pOffsetMap[offset] = v_int * width + u_int;
131 
132  if (m_bInterpolate)
133  {
134  const int u1 = int(floor(u));
135  const int v1 = int(floor(v));
136  const float x = u - u1;
137  const float y = v - v1;
138 
139  const float f00 = (1 - x) * (1 - y);
140  const float f10 = x * (1 - y);
141  const float f01 = (1 - x) * y;
142  const float f11 = x * y;
143 
144  const float sum = f00 + f10 + f01 + f11;
145 
146  // 2^22 = 4194304
147  m_pCoordinateMap[offset].f00 = int((f00 / sum) * 4194304);
148  m_pCoordinateMap[offset].f10 = int((f10 / sum) * 4194304);
149  m_pCoordinateMap[offset].f01 = int((f01 / sum) * 4194304);
150  m_pCoordinateMap[offset].f11 = int((f11 / sum) * 4194304);
151  }
152  }
153  else
154  {
155  m_pOffsetMap[offset] = 0;
156 
157  if (m_bInterpolate)
158  {
163  }
164  }
165  }
166  }
167 
168  m_bMapComputed = true;
169 }
170 
171 void CImageMapper::PerformMapping(const CByteImage *pInputImage, CByteImage *pOutputImage)
172 {
173  if (!m_bMapComputed)
174  {
175  printf("error: map has not been computed yet. call CImageMapper::ComputeMap\n");
176  return;
177  }
178 
179  if (pInputImage->type != pOutputImage->type)
180  {
181  printf("error: input and output image must be of same type for CImageMapper::PerformMapping\n");
182  return;
183  }
184 
185  if (pInputImage->width != width || pInputImage->height != height)
186  {
187  printf("error: input image does not match calibration file for CImageMapper::PerformMapping\n");
188  return;
189  }
190 
191  if (pOutputImage->width != width || pOutputImage->height != height)
192  {
193  printf("error: output image does not match calibration file for CImageMapper::PerformMapping\n");
194  return;
195  }
196 
197  CByteImage *pSaveOutputImage = 0;
198  if (pInputImage->pixels == pOutputImage->pixels)
199  {
200  pSaveOutputImage = pOutputImage;
201  pOutputImage = new CByteImage(pInputImage);
202  }
203 
204  unsigned char *input = pInputImage->pixels;
205  unsigned char *output = pOutputImage->pixels;
206 
207  const int nPixels = width * height;
208 
209  if (pInputImage->type == CByteImage::eGrayScale)
210  {
211  const unsigned char g = input[0];
212  input[0] = 0;
213 
214  if (m_bInterpolate)
215  {
216  for (int i = 0; i < nPixels; i++)
217  {
218  const int input_offset = m_pOffsetMap[i];
219  const MapCoordinates &m = m_pCoordinateMap[i];
220  output[i] = (unsigned char) ((input[input_offset] * m.f00 + input[input_offset + width] * m.f01 + input[input_offset + 1] * m.f10 + input[input_offset + width + 1] * m.f11 + 2097152) >> 22);
221  }
222  }
223  else
224  {
225  for (int i = 0; i < nPixels; i++)
226  output[i] = input[m_pOffsetMap[i]];
227  }
228 
229  input[0] = g;
230  }
231  else if (pInputImage->type == CByteImage::eRGB24)
232  {
233  const unsigned char r = input[0];
234  const unsigned char g = input[1];
235  const unsigned char b = input[2];
236  input[0] = input[1] = input[2] = 0;
237 
238  if (m_bInterpolate)
239  {
240  for (int i = 0, output_offset = 0; i < nPixels; i++, output_offset += 3)
241  {
242  const int input_offset = 3 * m_pOffsetMap[i];
243  const int width3 = 3 * width;
244 
245  const MapCoordinates &m = m_pCoordinateMap[i];
246 
247  output[output_offset] = (unsigned char) ((input[input_offset] * m.f00 + input[input_offset + width3] * m.f01 + input[input_offset + 3] * m.f10 + input[input_offset + width3 + 3] * m.f11 + 2097152) >> 22);
248  output[output_offset + 1] = (unsigned char) ((input[input_offset + 1] * m.f00 + input[input_offset + width3 + 1] * m.f01 + input[input_offset + 4] * m.f10 + input[input_offset + width3 + 4] * m.f11 + 2097152) >> 22);
249  output[output_offset + 2] = (unsigned char) ((input[input_offset + 2] * m.f00 + input[input_offset + width3 + 2] * m.f01 + input[input_offset + 5] * m.f10 + input[input_offset + width3 + 5] * m.f11 + 2097152) >> 22);
250  }
251  }
252  else
253  {
254  for (int i = 0, output_offset = 0; i < nPixels; i++, output_offset += 3)
255  {
256  const int input_offset = 3 * m_pOffsetMap[i];
257  output[output_offset] = input[input_offset];
258  output[output_offset + 1] = input[input_offset + 1];
259  output[output_offset + 2] = input[input_offset + 2];
260  }
261  }
262 
263  input[0] = r;
264  input[1] = g;
265  input[2] = b;
266  }
267 
268  if (pSaveOutputImage)
269  {
270  ImageProcessor::CopyImage(pOutputImage, pSaveOutputImage);
271  delete pOutputImage;
272  }
273 }
void ComputeMap(int width, int height)
This method initializes the instance for mapping of images of a specific size.
Definition: ImageMapper.cpp:90
GLubyte g
Definition: glext.h:5166
CImageMapper(bool bInterpolate=true)
The only constructor of this class.
Definition: ImageMapper.cpp:64
GLdouble u1
Definition: glext.h:4584
float y
Definition: Math2d.h:84
MapCoordinates * m_pCoordinateMap
Definition: ImageMapper.h:140
int width
The width of the image in pixels.
Definition: ByteImage.h:257
bool m_bInterpolate
Definition: ImageMapper.h:143
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
GLintptr offset
Definition: glext.h:3389
unsigned char * pixels
The pointer to the the pixels.
Definition: ByteImage.h:283
virtual ~CImageMapper()
The destructor.
Definition: ImageMapper.cpp:76
virtual void ComputeOriginalCoordinates(const Vec2d &newCoordinates, Vec2d &originalCoordinates)=0
GLenum GLint x
Definition: glext.h:3125
float x
Definition: Math2d.h:84
bool CopyImage(const CByteImage *pInputImage, CByteImage *pOutputImage, const MyRegion *pROI=0, bool bUseSameSize=false)
Copies one CByteImage to another.
int * m_pOffsetMap
Definition: ImageMapper.h:139
bool m_bMapComputed
Definition: ImageMapper.h:144
int height
The height of the image in pixels.
Definition: ByteImage.h:264
int my_round(double x)
Definition: helpers.cpp:325
GLubyte GLubyte b
Definition: glext.h:5166
void PerformMapping(const CByteImage *pInputImage, CByteImage *pOutputImage)
This method performs the mapping.
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
GLdouble GLdouble GLdouble r
Definition: glext.h:3227
GLenum GLint GLint y
Definition: glext.h:3125
const GLdouble * v
Definition: glext.h:3212
Data structure for the representation of a 2D vector.
Definition: Math2d.h:82
GLfloat GLfloat v1
Definition: glext.h:3531


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