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