66 m_bInterpolate = bInterpolate;
73 m_bMapComputed =
false;
79 delete [] m_pOffsetMap;
82 delete [] m_pCoordinateMap;
92 if (width != this->width || height != this->height)
95 this->height = height;
98 delete [] m_pOffsetMap;
100 m_pOffsetMap =
new int[width * height];
104 if (m_pCoordinateMap)
105 delete [] m_pCoordinateMap;
107 m_pCoordinateMap =
new MapCoordinates[width * height];
112 for (
int i = 0, offset = 0; i < height; i++)
114 for (
int j = 0; j < width; j++, offset++)
116 const Vec2d newCoordinates = { float(j), float(i) };
119 Vec2d originalCoordinates;
120 ComputeOriginalCoordinates(newCoordinates, originalCoordinates);
122 const float u = originalCoordinates.
x;
123 const float v = originalCoordinates.
y;
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);
128 if (u_int >= 0 && u_int < width - 1 && v_int >= 0 && v_int < height - 1)
130 m_pOffsetMap[offset] = v_int * width + u_int;
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;
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;
144 const float sum = f00 + f10 + f01 + f11;
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);
155 m_pOffsetMap[offset] = 0;
159 m_pCoordinateMap[offset].f00 = 0;
160 m_pCoordinateMap[offset].f10 = 0;
161 m_pCoordinateMap[offset].f01 = 0;
162 m_pCoordinateMap[offset].f11 = 0;
168 m_bMapComputed =
true;
175 printf(
"error: map has not been computed yet. call CImageMapper::ComputeMap\n");
179 if (pInputImage->
type != pOutputImage->
type)
181 printf(
"error: input and output image must be of same type for CImageMapper::PerformMapping\n");
185 if (pInputImage->
width != width || pInputImage->
height != height)
187 printf(
"error: input image does not match calibration file for CImageMapper::PerformMapping\n");
191 if (pOutputImage->
width != width || pOutputImage->
height != height)
193 printf(
"error: output image does not match calibration file for CImageMapper::PerformMapping\n");
200 pSaveOutputImage = pOutputImage;
204 unsigned char *input = pInputImage->
pixels;
205 unsigned char *output = pOutputImage->
pixels;
207 const int nPixels = width * height;
211 const unsigned char g = input[0];
216 for (
int i = 0; i < nPixels; i++)
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);
225 for (
int i = 0; i < nPixels; i++)
226 output[i] = input[m_pOffsetMap[i]];
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;
240 for (
int i = 0, output_offset = 0; i < nPixels; i++, output_offset += 3)
242 const int input_offset = 3 * m_pOffsetMap[i];
243 const int width3 = 3 * width;
245 const MapCoordinates &m = m_pCoordinateMap[i];
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);
254 for (
int i = 0, output_offset = 0; i < nPixels; i++, output_offset += 3)
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];
268 if (pSaveOutputImage)
bool CopyImage(const CByteImage *pInputImage, CByteImage *pOutputImage, const MyRegion *pROI=0, bool bUseSameSize=false)
Copies one CByteImage to another.
Data structure for the representation of a 2D vector.
void PerformMapping(const CByteImage *pInputImage, CByteImage *pOutputImage)
This method performs the mapping.
virtual ~CImageMapper()
The destructor.
ImageType type
The type of the image.
int width
The width of the image in pixels.
int height
The height of the image in pixels.
unsigned char * pixels
The pointer to the the pixels.
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
CImageMapper(bool bInterpolate=true)
The only constructor of this class.
void ComputeMap(int width, int height)
This method initializes the instance for mapping of images of a specific size.