1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
#pragma once
#include <stdint.h>
#include <algorithm>
namespace Etc
{
enum FilterEnums
{
MaxFilterSize = 32
};
enum WrapFlags
{
FILTER_WRAP_NONE = 0,
FILTER_WRAP_X = 0x1,
FILTER_WRAP_Y = 0x2
};
typedef struct tagFilterWeights
{
int first;
int numWeights;
double weight[MaxFilterSize * 2 + 1];
} FilterWeights;
typedef struct tagRGBCOLOR
{
union
{
uint32_t ulColor;
uint8_t rgba[4];
};
} RGBCOLOR;
double FilterBox( double t );
double FilterLinear( double t );
double FilterLanczos3( double t );
int FilterTwoPass( RGBCOLOR *pSrcImage, int srcWidth, int srcHeight,
RGBCOLOR *pDestImage, int destWidth, int destHeight, unsigned int wrapFlags, double (*FilterProc)(double) );
void FilterResample( RGBCOLOR *pSrcImage, int srcWidth, int srcHeight,
RGBCOLOR *pDstImage, int dstWidth, int dstHeight );
void CalcContributions(int srcSize, int destSize, double filterSize, bool wrap, double(*FilterProc)(double), FilterWeights contrib[]);
template <typename T>
void FilterResample(T *pSrcImage, int srcWidth, int srcHeight, T *pDstImage, int dstWidth, int dstHeight)
{
float xScale;
float yScale;
T *pSrcPixel;
T *pDstPixel;
xScale = (float)srcWidth / dstWidth;
yScale = (float)srcHeight / dstHeight;
for (int iRow = 0; iRow < dstHeight; iRow++)
{
for (int iCol = 0; iCol < dstWidth; iCol++)
{
int samples;
int iFirstSampleRow;
int iFirstSampleCol;
int iLastSampleRow;
int iLastSampleCol;
float red;
float green;
float blue;
float alpha;
iFirstSampleRow = (int)(iRow * yScale);
iLastSampleRow = (int)ceil(iFirstSampleRow + yScale - 1);
if (iLastSampleRow >= srcHeight)
{
iLastSampleRow = srcHeight - 1;
}
iFirstSampleCol = (int)(iCol * xScale);
iLastSampleCol = (int)ceil(iFirstSampleCol + xScale - 1);
if (iLastSampleCol >= srcWidth)
{
iLastSampleCol = srcWidth - 1;
}
samples = 0;
red = 0.f;
green = 0.f;
blue = 0.f;
alpha = 0.f;
for (int iSampleRow = iFirstSampleRow; iSampleRow <= iLastSampleRow; iSampleRow++)
{
for (int iSampleCol = iFirstSampleCol; iSampleCol <= iLastSampleCol; iSampleCol++)
{
pSrcPixel = pSrcImage + (iSampleRow * srcWidth + iSampleCol) * 4;
red += static_cast<float>(pSrcPixel[0]);
green += static_cast<float>(pSrcPixel[1]);
blue += static_cast<float>(pSrcPixel[2]);
alpha += static_cast<float>(pSrcPixel[3]);
samples++;
}
}
pDstPixel = pDstImage + (iRow * dstWidth + iCol) * 4;
if (samples > 0)
{
pDstPixel[0] = static_cast<T>(red / samples);
pDstPixel[1] = static_cast<T>(green / samples);
pDstPixel[2] = static_cast<T>(blue / samples);
pDstPixel[3] = static_cast<T>(alpha / samples);
}
else
{
pDstPixel[0] = static_cast<T>(red);
pDstPixel[1] = static_cast<T>(green);
pDstPixel[2] = static_cast<T>(blue);
pDstPixel[3] = static_cast<T>(alpha);
}
}
}
}
//**-------------------------------------------------------------------------
//** Name: Filter_TwoPass( RGBCOLOR *pSrcImage,
//** int srcWidth, int srcHeight,
//** RGBCOLOR *pDestImage,
//** int destWidth, int destHeight,
//** double (*FilterProc)(double) )
//** Returns: 0 on failure and 1 on success
//** Description: Filters a 2d image with a two pass filter by averaging the
//** weighted contributions of the pixels within the filter region. The
//** contributions are determined by a weighting function parameter.
//**-------------------------------------------------------------------------
template <typename T>
int FilterTwoPass(T *pSrcImage, int srcWidth, int srcHeight,
T *pDestImage, int destWidth, int destHeight, unsigned int wrapFlags, double(*FilterProc)(double))
{
const int numComponents = 4;
FilterWeights *contrib;
T *pPixel;
T *pTempImage;
double dRed;
double dGreen;
double dBlue;
double dAlpha;
double filterSize = 3.0;
int maxDim = (srcWidth>srcHeight) ? srcWidth : srcHeight;
contrib = new FilterWeights[maxDim];
//**------------------------------------------------------------------------
//** Need to create a temporary image to stuff the horizontally scaled image
//**------------------------------------------------------------------------
pTempImage = new T[destWidth * srcHeight * numComponents];
if (pTempImage == NULL)
{
return 0;
}
//**-------------------------------------------------------
//** Horizontally filter the image into the temporary image
//**-------------------------------------------------------
bool bWrapHorizontal = !!(wrapFlags&FILTER_WRAP_X);
CalcContributions(srcWidth, destWidth, filterSize, bWrapHorizontal, FilterProc, contrib);
for (int iRow = 0; iRow < srcHeight; iRow++)
{
for (int iCol = 0; iCol < destWidth; iCol++)
{
dRed = 0;
dGreen = 0;
dBlue = 0;
dAlpha = 0;
for (int iWeight = 0; iWeight < contrib[iCol].numWeights; iWeight++)
{
int iSrcCol = iWeight + contrib[iCol].first;
if(bWrapHorizontal)
{
iSrcCol = (iSrcCol < 0)?(srcWidth+iSrcCol):(iSrcCol >= srcWidth)?(iSrcCol-srcWidth):iSrcCol;
}
T* pSrcPixel = pSrcImage + ((iRow * srcWidth) + iSrcCol)*numComponents;
dRed += contrib[iCol].weight[iWeight] * pSrcPixel[0];
dGreen += contrib[iCol].weight[iWeight] * pSrcPixel[1];
dBlue += contrib[iCol].weight[iWeight] * pSrcPixel[2];
dAlpha += contrib[iCol].weight[iWeight] * pSrcPixel[3];
}
pPixel = pTempImage + ((iRow * destWidth) + iCol)*numComponents;
pPixel[0] = static_cast<T>(std::max(0.0, std::min(255.0, dRed)));
pPixel[1] = static_cast<T>(std::max(0.0, std::min(255.0, dGreen)));
pPixel[2] = static_cast<T>(std::max(0.0, std::min(255.0, dBlue)));
pPixel[3] = static_cast<T>(std::max(0.0, std::min(255.0, dAlpha)));
}
}
//**-------------------------------------------------------
//** Vertically filter the image into the destination image
//**-------------------------------------------------------
bool bWrapVertical = !!(wrapFlags&FILTER_WRAP_Y);
CalcContributions(srcHeight, destHeight, filterSize, bWrapVertical, FilterProc, contrib);
for (int iCol = 0; iCol < destWidth; iCol++)
{
for (int iRow = 0; iRow < destHeight; iRow++)
{
dRed = 0;
dGreen = 0;
dBlue = 0;
dAlpha = 0;
for (int iWeight = 0; iWeight < contrib[iRow].numWeights; iWeight++)
{
int iSrcRow = iWeight + contrib[iRow].first;
if (bWrapVertical)
{
iSrcRow = (iSrcRow < 0) ? (srcHeight + iSrcRow) : (iSrcRow >= srcHeight) ? (iSrcRow - srcHeight) : iSrcRow;
}
T* pSrcPixel = pTempImage + ((iSrcRow * destWidth) + iCol)*numComponents;
dRed += contrib[iRow].weight[iWeight] * pSrcPixel[0];
dGreen += contrib[iRow].weight[iWeight] * pSrcPixel[1];
dBlue += contrib[iRow].weight[iWeight] * pSrcPixel[2];
dAlpha += contrib[iRow].weight[iWeight] * pSrcPixel[3];
}
pPixel = pDestImage + ((iRow * destWidth) + iCol)*numComponents;
pPixel[0] = static_cast<T>(std::max(0.0, std::min(255.0, dRed)));
pPixel[1] = static_cast<T>(std::max(0.0, std::min(255.0, dGreen)));
pPixel[2] = static_cast<T>(std::max(0.0, std::min(255.0, dBlue)));
pPixel[3] = static_cast<T>(std::max(0.0, std::min(255.0, dAlpha)));
}
}
delete[] pTempImage;
delete[] contrib;
return 1;
}
}
|