TImagePixelInterpolator.h
Go to the documentation of this file.
1 #ifndef iipr_TImagePixelInterpolator_included
2 #define iipr_TImagePixelInterpolator_included
3 
4 
5 // Qt includes
6 #include <QtCore/QtGlobal>
7 #if QT_VERSION >= 0x050000
8 #include <QtCore/QtMath>
9 #else
10 #include <QtCore/qmath.h>
11 #endif
12 
13 // ACF includes
14 #include <istd/istd.h>
15 
16 // IACF includes
18 
19 
20 namespace iimg
21 {
22  class IBitmap;
23 }
24 
25 
26 namespace iipr
27 {
28 
29 
30 template <typename PixelComponentType>
32 {
33 public:
34  typedef PixelComponentType PixelComponent;
35 
36  TImagePixelInterpolator(const iimg::IBitmap& image, int interpolationMode);
37 
38  PixelComponentType GetInterpolatedValue(float x, float y, int componentIndex) const;
39 
40 private:
41  PixelComponentType GetBilinearInterpolated(float x, float y, int componentIndex) const;
42  PixelComponentType GetBitmapPixelValue(int x, int y, int componentIndex) const;
43 
44 private:
45  PixelComponentType* m_imageBufferPtr;
46  int m_imageLineDifference;
47  int m_imageHeight;
48  int m_imageWidth;
49  int m_interpolationMode;
50  int m_pixelBytesCount;
51  int m_maxValue;
52 };
53 
54 
55 // public methods
56 
57 template <typename PixelComponentType>
59 {
60  m_imageBufferPtr = (PixelComponentType*)bitmap.GetLinePtr(0);
61  m_imageLineDifference = bitmap.GetLinesDifference();
62  m_imageHeight = bitmap.GetImageSize().GetY();
63  m_imageWidth = bitmap.GetImageSize().GetX();
64  m_pixelBytesCount = bitmap.GetPixelBitsCount() / 8;
65  m_interpolationMode = interpolationMode;
66  m_maxValue = int(qPow(2.0, double(sizeof(PixelComponentType) * 8)) - 1);
67 }
68 
69 
70 template <typename PixelComponentType>
72 {
73  switch (m_interpolationMode){
75  return GetBitmapPixelValue(int(x), int(y), componentIndex);
76 
78  return GetBilinearInterpolated(x, y, componentIndex);
79 
80  default:
81  I_CRITICAL();
82  }
83 
84  return GetBitmapPixelValue(int(x), int(y), componentIndex);
85 }
86 
87 
88 // private methods
89 
90 template <typename PixelComponentType>
92 {
93  int px = qFloor(x);
94  int py = qFloor(y);
95 
96  // Calculate the weights for each pixel:
97  float fx = x - px;
98  float fy = y - py;
99  float fx1 = 1.0f - fx;
100  float fy1 = 1.0f - fy;
101 
102  float w1 = fx1 * fy1;
103  float w2 = fx * fy1;
104  float w3 = fx1 * fy;
105  float w4 = fx * fy;
106 
107  float v1 = w1 * GetBitmapPixelValue(px, py, componentIndex);
108  float v2 = w2 * GetBitmapPixelValue(px + 1, py, componentIndex);
109  float v3 = w3 * GetBitmapPixelValue(px, py + 1, componentIndex);
110  float v4 = w4 * GetBitmapPixelValue(px + 1, py + 1, componentIndex);
111 
112  float retVal = v1 + v2 + v3 + v4;
113  if (retVal > m_maxValue){
114  retVal = m_maxValue;
115  }
116 
117  return PixelComponentType(retVal);
118 }
119 
120 
121 template <typename PixelComponentType>
122 typename TImagePixelInterpolator<PixelComponentType>::PixelComponent TImagePixelInterpolator<PixelComponentType>::GetBitmapPixelValue(int x, int y, int componentIndex) const
123 {
124  if (x < 0 || x >= m_imageWidth || y < 0 || y >= m_imageHeight){
125  return 0;
126  }
127 
128  return *(m_imageBufferPtr + x * m_pixelBytesCount + componentIndex + m_imageLineDifference * y);
129 }
130 
131 
132 } // namespace iipr
133 
134 
135 #endif // !iipr_TImagePixelInterpolator_included
136 
137 
PixelComponentType GetInterpolatedValue(float x, float y, int componentIndex) const
#define I_CRITICAL()
TImagePixelInterpolator(const iimg::IBitmap &image, int interpolationMode)

© 2007-2017 Witold Gantzke and Kirill Lepskiy