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/qmath.h>
7 
8 // ACF includes
9 #include "istd/istd.h"
10 
11 // IACF includes
13 
14 
15 namespace iimg
16 {
17  class IBitmap;
18 }
19 
20 
21 namespace iipr
22 {
23 
24 
25 template <typename PixelComponentType>
27 {
28 public:
29  typedef PixelComponentType PixelComponent;
30 
31  TImagePixelInterpolator(const iimg::IBitmap& image, int interpolationMode);
32 
33  PixelComponentType GetInterpolatedValue(double x, double y, int componentIndex) const;
34 
35 private:
36  PixelComponentType GetBilinearInterpolated(double x, double y, int componentIndex) const;
37  PixelComponentType GetBitmapPixelValue(int x, int y, int componentIndex) const;
38 
39 private:
40  PixelComponentType* m_imageBufferPtr;
41  int m_imageLineDifference;
42  int m_imageHeight;
43  int m_imageWidth;
44  int m_interpolationMode;
45  int m_pixelBytesCount;
46  int m_maxValue;
47 };
48 
49 
50 // public methods
51 
52 template <typename PixelComponentType>
54 {
55  m_imageBufferPtr = (PixelComponentType*)bitmap.GetLinePtr(0);
56  m_imageLineDifference = bitmap.GetLinesDifference();
57  m_imageHeight = bitmap.GetImageSize().GetY();
58  m_imageWidth = bitmap.GetImageSize().GetX();
59  m_pixelBytesCount = bitmap.GetPixelBitsCount() / 8;
60  m_interpolationMode = interpolationMode;
61  m_maxValue = int(qPow(2.0, double(sizeof(PixelComponentType) * 8)) - 1);
62 }
63 
64 
65 template <typename PixelComponentType>
67 {
68  switch (m_interpolationMode){
70  return GetBitmapPixelValue(int(x), int(y), componentIndex);
71 
73  return GetBilinearInterpolated(x, y, componentIndex);
74 
75  default:
76  I_CRITICAL();
77  }
78 
79  return GetBitmapPixelValue(int(x), int(y), componentIndex);
80 }
81 
82 
83 // private methods
84 
85 template <typename PixelComponentType>
87 {
88  int cx = int(qCeil(x));
89  int cy = int(qCeil(y));
90 
91  int fx = int(qFloor(x));
92  int fy = int(qFloor(y));
93 
94  double dx = cx - x;
95  double dy = cy - y;
96 
97  double v1 = (1.0 - dx) * (1.0 - dy) * GetBitmapPixelValue(fx, fy, componentIndex);
98  double v2 = (dx) * (1.0 - dy) * GetBitmapPixelValue(cx, fy, componentIndex);
99  double v3 = (dy) * (1.0 - dx) * GetBitmapPixelValue(fx, cy, componentIndex);
100  double v4 = (dy) * (dx) * GetBitmapPixelValue(cx, cy, componentIndex);
101 
102  double retVal = v1 + v2 + v3 + v4;
103  if (retVal > m_maxValue){
104  retVal = m_maxValue;
105  }
106 
107  return PixelComponentType(retVal);
108 }
109 
110 
111 template <typename PixelComponentType>
112 typename TImagePixelInterpolator<PixelComponentType>::PixelComponent TImagePixelInterpolator<PixelComponentType>::GetBitmapPixelValue(int x, int y, int componentIndex) const
113 {
114  if (x < 0){
115  x = 0;
116  }
117 
118  if (x >= m_imageWidth){
119  x = m_imageWidth - 1;
120  }
121 
122  if (y < 0){
123  y = 0;
124  }
125 
126  if (y >= m_imageHeight){
127  y = m_imageHeight - 1;
128  }
129 
130  return *(m_imageBufferPtr + x * m_pixelBytesCount + componentIndex + m_imageLineDifference * y);
131 }
132 
133 
134 } // namespace iipr
135 
136 
137 #endif // !iipr_TImagePixelInterpolator_included
138 
139 
#define I_CRITICAL()
TImagePixelInterpolator(const iimg::IBitmap &image, int interpolationMode)
PixelComponentType GetInterpolatedValue(double x, double y, int componentIndex) const

© 2007-2011 Witold Gantzke and Kirill Lepskiy