| 1 |
#include <stdlib.h> |
| 2 |
#include <math.h> |
| 3 |
#include "EtcFilter.h" |
| 4 |
|
| 5 |
|
| 6 |
namespace Etc |
| 7 |
{ |
| 8 |
|
| 9 |
static const double PiConst = 3.14159265358979323846; |
| 10 |
|
| 11 |
inline double sinc(double x) |
| 12 |
{ |
| 13 |
if ( x == 0.0 ) |
| 14 |
{ |
| 15 |
return 1.0; |
| 16 |
} |
| 17 |
|
| 18 |
return sin(PiConst * x) / (PiConst * x); |
| 19 |
} |
| 20 |
|
| 21 |
//inline float sincf( float x ) |
| 22 |
//{ |
| 23 |
// x *= F_PI; |
| 24 |
// if (x < 0.01f && x > -0.01f) |
| 25 |
// { |
| 26 |
// return 1.0f + x*x*(-1.0f/6.0f + x*x*1.0f/120.0f); |
| 27 |
// } |
| 28 |
// |
| 29 |
// return sinf(x)/x; |
| 30 |
//} |
| 31 |
// |
| 32 |
//double bessel0(double x) |
| 33 |
//{ |
| 34 |
// const double EPSILON_RATIO = 1E-16; |
| 35 |
// double xh, sum, pow, ds; |
| 36 |
// int k; |
| 37 |
// |
| 38 |
// xh = 0.5 * x; |
| 39 |
// sum = 1.0; |
| 40 |
// pow = 1.0; |
| 41 |
// k = 0; |
| 42 |
// ds = 1.0; |
| 43 |
// while (ds > sum * EPSILON_RATIO) |
| 44 |
// { |
| 45 |
// ++k; |
| 46 |
// pow = pow * (xh / k); |
| 47 |
// ds = pow * pow; |
| 48 |
// sum = sum + ds; |
| 49 |
// } |
| 50 |
// |
| 51 |
// return sum; |
| 52 |
//} |
| 53 |
|
| 54 |
//**-------------------------------------------------------------------------- |
| 55 |
//** Name: kaiser(double alpha, double half_width, double x) |
| 56 |
//** Returns: |
| 57 |
//** Description: Alpha controls shape of filter. We are using 4. |
| 58 |
//**-------------------------------------------------------------------------- |
| 59 |
//inline double kaiser(double alpha, double half_width, double x) |
| 60 |
//{ |
| 61 |
// double ratio = (x / half_width); |
| 62 |
// return bessel0(alpha * sqrt(1 - ratio * ratio)) / bessel0(alpha); |
| 63 |
//} |
| 64 |
// |
| 65 |
//float Filter_Lanczos4Sinc(float x) |
| 66 |
//{ |
| 67 |
// if (x <= -4.0f || x >= 4.0f) // half-width of 4 |
| 68 |
// { |
| 69 |
// return 0.0; |
| 70 |
// } |
| 71 |
// |
| 72 |
// return sinc(0.875f * x) * sinc(0.25f * x); |
| 73 |
//} |
| 74 |
// |
| 75 |
//double Filter_Kaiser4( double t ) |
| 76 |
//{ |
| 77 |
// return kaiser( 4.0, 3.0, t); |
| 78 |
//} |
| 79 |
// |
| 80 |
//double Filter_KaiserOptimal( double t ) |
| 81 |
//{ |
| 82 |
// return kaiser( 8.93, 3.0f, t); |
| 83 |
//} |
| 84 |
|
| 85 |
double FilterLanczos3( double t ) |
| 86 |
{ |
| 87 |
if ( t <= -3.0 || t >= 3.0 ) |
| 88 |
{ |
| 89 |
return 0.0; |
| 90 |
} |
| 91 |
|
| 92 |
return sinc( t ) * sinc( t / 3.0 ); |
| 93 |
} |
| 94 |
|
| 95 |
double FilterBox( double t ) |
| 96 |
{ |
| 97 |
return ( t > -0.5 && t < 0.5) ? 1.0 : 0.0; |
| 98 |
} |
| 99 |
|
| 100 |
double FilterLinear( double t ) |
| 101 |
{ |
| 102 |
if (t < 0.0) t = -t; |
| 103 |
|
| 104 |
return (t < 1.0) ? (1.0 - t) : 0.0; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
//**-------------------------------------------------------------------------- |
| 109 |
//** Name: CalcContributions( int srcSize, |
| 110 |
//** int destSize, |
| 111 |
//** double filterSize, |
| 112 |
//** bool wrap, |
| 113 |
//** double (*FilterProc)(double), |
| 114 |
//** FilterWeights contrib[] ) |
| 115 |
//** Returns: void |
| 116 |
//** Description: |
| 117 |
//**-------------------------------------------------------------------------- |
| 118 |
void CalcContributions( int srcSize, int destSize, double filterSize, bool wrap, double (*FilterProc)(double), FilterWeights contrib[] ) |
| 119 |
{ |
| 120 |
double scale; |
| 121 |
double filterScale; |
| 122 |
double center; |
| 123 |
double totalWeight; |
| 124 |
double weight; |
| 125 |
int iRight; |
| 126 |
int iLeft; |
| 127 |
int iDest; |
| 128 |
|
| 129 |
scale = (double)destSize / srcSize; |
| 130 |
if ( scale < 1.0 ) |
| 131 |
{ |
| 132 |
filterSize = filterSize / scale; |
| 133 |
filterScale = scale; |
| 134 |
} |
| 135 |
else |
| 136 |
{ |
| 137 |
filterScale = 1.0; |
| 138 |
} |
| 139 |
|
| 140 |
if ( filterSize > (double)MaxFilterSize ) |
| 141 |
{ |
| 142 |
filterSize = (double)MaxFilterSize; |
| 143 |
} |
| 144 |
|
| 145 |
for ( iDest = 0; iDest < destSize; ++iDest ) |
| 146 |
{ |
| 147 |
center = (double)iDest / scale; |
| 148 |
|
| 149 |
iLeft = (int)ceil(center - filterSize); |
| 150 |
iRight = (int)floor(center + filterSize); |
| 151 |
|
| 152 |
if ( !wrap ) |
| 153 |
{ |
| 154 |
if ( iLeft < 0 ) |
| 155 |
{ |
| 156 |
iLeft = 0; |
| 157 |
} |
| 158 |
|
| 159 |
if ( iRight >= srcSize ) |
| 160 |
{ |
| 161 |
iRight = srcSize - 1; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
int numWeights = iRight - iLeft + 1; |
| 166 |
|
| 167 |
contrib[iDest].first = iLeft; |
| 168 |
contrib[iDest].numWeights = numWeights; |
| 169 |
|
| 170 |
totalWeight = 0; |
| 171 |
double t = ((double)iLeft - center) * filterScale; |
| 172 |
for (int i = 0; i < numWeights; i++) |
| 173 |
{ |
| 174 |
weight = (*FilterProc)(t) * filterScale; |
| 175 |
totalWeight += weight; |
| 176 |
contrib[iDest].weight[i] = weight; |
| 177 |
t += filterScale; |
| 178 |
} |
| 179 |
|
| 180 |
//**-------------------------------------------------------- |
| 181 |
//** Normalize weights by dividing by the sum of the weights |
| 182 |
//**-------------------------------------------------------- |
| 183 |
if ( totalWeight > 0.0 ) |
| 184 |
{ |
| 185 |
for ( int i = 0; i < numWeights; i++) |
| 186 |
{ |
| 187 |
contrib[iDest].weight[i] /= totalWeight; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
//**------------------------------------------------------------------------- |
| 194 |
//** Name: Filter_TwoPass( RGBCOLOR *pSrcImage, |
| 195 |
//** int srcWidth, int srcHeight, |
| 196 |
//** RGBCOLOR *pDestImage, |
| 197 |
//** int destWidth, int destHeight, |
| 198 |
//** double (*FilterProc)(double) ) |
| 199 |
//** Returns: 0 on failure and 1 on success |
| 200 |
//** Description: Filters a 2d image with a two pass filter by averaging the |
| 201 |
//** weighted contributions of the pixels within the filter region. The |
| 202 |
//** contributions are determined by a weighting function parameter. |
| 203 |
//**------------------------------------------------------------------------- |
| 204 |
int FilterTwoPass( RGBCOLOR *pSrcImage, int srcWidth, int srcHeight, |
| 205 |
RGBCOLOR *pDestImage, int destWidth, int destHeight, unsigned int wrapFlags, double (*FilterProc)(double) ) |
| 206 |
{ |
| 207 |
FilterWeights *contrib; |
| 208 |
RGBCOLOR *pPixel; |
| 209 |
RGBCOLOR *pSrcPixel; |
| 210 |
RGBCOLOR *pTempImage; |
| 211 |
int iRow; |
| 212 |
int iCol; |
| 213 |
int iSrcCol; |
| 214 |
int iSrcRow; |
| 215 |
int iWeight; |
| 216 |
double dRed; |
| 217 |
double dGreen; |
| 218 |
double dBlue; |
| 219 |
double dAlpha; |
| 220 |
double filterSize = 3.0; |
| 221 |
|
| 222 |
int maxDim = (srcWidth>srcHeight)?srcWidth:srcHeight; |
| 223 |
contrib = (FilterWeights*)malloc(maxDim * sizeof(FilterWeights)); |
| 224 |
|
| 225 |
//**------------------------------------------------------------------------ |
| 226 |
//** Need to create a temporary image to stuff the horizontally scaled image |
| 227 |
//**------------------------------------------------------------------------ |
| 228 |
pTempImage = (RGBCOLOR *)malloc( destWidth * srcHeight * sizeof(RGBCOLOR) ); |
| 229 |
if ( pTempImage == NULL ) |
| 230 |
{ |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
//**------------------------------------------------------- |
| 235 |
//** Horizontally filter the image into the temporary image |
| 236 |
//**------------------------------------------------------- |
| 237 |
bool bWrapHorizontal = !!(wrapFlags&FILTER_WRAP_X); |
| 238 |
CalcContributions( srcWidth, destWidth, filterSize, bWrapHorizontal, FilterProc, contrib ); |
| 239 |
for ( iRow = 0; iRow < srcHeight; iRow++ ) |
| 240 |
{ |
| 241 |
for ( iCol = 0; iCol < destWidth; iCol++ ) |
| 242 |
{ |
| 243 |
dRed = 0; |
| 244 |
dGreen = 0; |
| 245 |
dBlue = 0; |
| 246 |
dAlpha = 0; |
| 247 |
|
| 248 |
for ( iWeight = 0; iWeight < contrib[iCol].numWeights; iWeight++ ) |
| 249 |
{ |
| 250 |
iSrcCol = iWeight + contrib[iCol].first; |
| 251 |
if (bWrapHorizontal) |
| 252 |
{ |
| 253 |
iSrcCol = (iSrcCol < 0) ? (srcWidth + iSrcCol) : (iSrcCol >= srcWidth) ? (iSrcCol - srcWidth) : iSrcCol; |
| 254 |
} |
| 255 |
pSrcPixel = pSrcImage + (iRow * srcWidth) + iSrcCol; |
| 256 |
dRed += contrib[iCol].weight[iWeight] * pSrcPixel->rgba[0]; |
| 257 |
dGreen += contrib[iCol].weight[iWeight] * pSrcPixel->rgba[1]; |
| 258 |
dBlue += contrib[iCol].weight[iWeight] * pSrcPixel->rgba[2]; |
| 259 |
dAlpha += contrib[iCol].weight[iWeight] * pSrcPixel->rgba[3]; |
| 260 |
} |
| 261 |
|
| 262 |
pPixel = pTempImage + (iRow * destWidth) + iCol; |
| 263 |
pPixel->rgba[0] = static_cast<unsigned char>(std::max(0.0, std::min(255.0, dRed))); |
| 264 |
pPixel->rgba[1] = static_cast<unsigned char>(std::max(0.0, std::min(255.0, dGreen))); |
| 265 |
pPixel->rgba[2] = static_cast<unsigned char>(std::max(0.0, std::min(255.0, dBlue))); |
| 266 |
pPixel->rgba[3] = static_cast<unsigned char>(std::max(0.0, std::min(255.0, dAlpha))); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
//**------------------------------------------------------- |
| 271 |
//** Vertically filter the image into the destination image |
| 272 |
//**------------------------------------------------------- |
| 273 |
bool bWrapVertical = !!(wrapFlags&FILTER_WRAP_Y); |
| 274 |
CalcContributions(srcHeight, destHeight, filterSize, bWrapVertical, FilterProc, contrib); |
| 275 |
for ( iCol = 0; iCol < destWidth; iCol++ ) |
| 276 |
{ |
| 277 |
for ( iRow = 0; iRow < destHeight; iRow++ ) |
| 278 |
{ |
| 279 |
dRed = 0; |
| 280 |
dGreen = 0; |
| 281 |
dBlue = 0; |
| 282 |
dAlpha = 0; |
| 283 |
|
| 284 |
for ( iWeight = 0; iWeight < contrib[iRow].numWeights; iWeight++ ) |
| 285 |
{ |
| 286 |
iSrcRow = iWeight + contrib[iRow].first; |
| 287 |
if (bWrapVertical) |
| 288 |
{ |
| 289 |
iSrcRow = (iSrcRow < 0) ? (srcHeight + iSrcRow) : (iSrcRow >= srcHeight) ? (iSrcRow - srcHeight) : iSrcRow; |
| 290 |
} |
| 291 |
pSrcPixel = pTempImage + (iSrcRow * destWidth) + iCol; |
| 292 |
dRed += contrib[iRow].weight[iWeight] * pSrcPixel->rgba[0]; |
| 293 |
dGreen += contrib[iRow].weight[iWeight] * pSrcPixel->rgba[1]; |
| 294 |
dBlue += contrib[iRow].weight[iWeight] * pSrcPixel->rgba[2]; |
| 295 |
dAlpha += contrib[iRow].weight[iWeight] * pSrcPixel->rgba[3]; |
| 296 |
} |
| 297 |
|
| 298 |
pPixel = pDestImage + (iRow * destWidth) + iCol; |
| 299 |
pPixel->rgba[0] = (unsigned char)(std::max( 0.0, std::min( 255.0, dRed))); |
| 300 |
pPixel->rgba[1] = (unsigned char)(std::max( 0.0, std::min( 255.0, dGreen))); |
| 301 |
pPixel->rgba[2] = (unsigned char)(std::max( 0.0, std::min( 255.0, dBlue))); |
| 302 |
pPixel->rgba[3] = (unsigned char)(std::max( 0.0, std::min( 255.0, dAlpha))); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
free( pTempImage ); |
| 307 |
free( contrib ); |
| 308 |
|
| 309 |
return 1; |
| 310 |
} |
| 311 |
|
| 312 |
//**------------------------------------------------------------------------- |
| 313 |
//** Name: FilterResample(RGBCOLOR *pSrcImage, int srcWidth, int srcHeight, |
| 314 |
//** RGBCOLOR *pDstImage, int dstWidth, int dstHeight) |
| 315 |
//** Returns: 1 |
| 316 |
//** Description: This function runs a 2d box filter over the srouce image |
| 317 |
//** to produce the destination image. |
| 318 |
//**------------------------------------------------------------------------- |
| 319 |
void FilterResample( RGBCOLOR *pSrcImage, int srcWidth, int srcHeight, |
| 320 |
RGBCOLOR *pDstImage, int dstWidth, int dstHeight ) |
| 321 |
{ |
| 322 |
int iRow; |
| 323 |
int iCol; |
| 324 |
int iSampleRow; |
| 325 |
int iSampleCol; |
| 326 |
int iFirstSampleRow; |
| 327 |
int iFirstSampleCol; |
| 328 |
int iLastSampleRow; |
| 329 |
int iLastSampleCol; |
| 330 |
int red; |
| 331 |
int green; |
| 332 |
int blue; |
| 333 |
int alpha; |
| 334 |
int samples; |
| 335 |
float xScale; |
| 336 |
float yScale; |
| 337 |
|
| 338 |
RGBCOLOR *pSrcPixel; |
| 339 |
RGBCOLOR *pDstPixel; |
| 340 |
|
| 341 |
xScale = (float)srcWidth / dstWidth; |
| 342 |
yScale = (float)srcHeight / dstHeight; |
| 343 |
|
| 344 |
for ( iRow = 0; iRow < dstHeight; iRow++ ) |
| 345 |
{ |
| 346 |
for ( iCol = 0; iCol < dstWidth; iCol++ ) |
| 347 |
{ |
| 348 |
iFirstSampleRow = (int)(iRow * yScale); |
| 349 |
iLastSampleRow = (int)ceil(iFirstSampleRow + yScale - 1); |
| 350 |
if ( iLastSampleRow >= srcHeight ) |
| 351 |
{ |
| 352 |
iLastSampleRow = srcHeight - 1; |
| 353 |
} |
| 354 |
|
| 355 |
iFirstSampleCol = (int)(iCol * xScale); |
| 356 |
iLastSampleCol = (int)ceil(iFirstSampleCol + xScale - 1); |
| 357 |
if ( iLastSampleCol >= srcWidth ) |
| 358 |
{ |
| 359 |
iLastSampleCol = srcWidth - 1; |
| 360 |
} |
| 361 |
|
| 362 |
samples = 0; |
| 363 |
red = 0; |
| 364 |
green = 0; |
| 365 |
blue = 0; |
| 366 |
alpha = 0; |
| 367 |
for ( iSampleRow = iFirstSampleRow; iSampleRow <= iLastSampleRow; iSampleRow++ ) |
| 368 |
{ |
| 369 |
for ( iSampleCol = iFirstSampleCol; iSampleCol <= iLastSampleCol; iSampleCol++ ) |
| 370 |
{ |
| 371 |
pSrcPixel = pSrcImage + iSampleRow * srcWidth + iSampleCol; |
| 372 |
red += pSrcPixel->rgba[0]; |
| 373 |
green += pSrcPixel->rgba[1]; |
| 374 |
blue += pSrcPixel->rgba[2]; |
| 375 |
alpha += pSrcPixel->rgba[3]; |
| 376 |
|
| 377 |
samples++; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
pDstPixel = pDstImage + iRow * dstWidth + iCol; |
| 382 |
if ( samples > 0 ) |
| 383 |
{ |
| 384 |
pDstPixel->rgba[0] = static_cast<uint8_t>(red / samples); |
| 385 |
pDstPixel->rgba[1] = static_cast<uint8_t>(green / samples); |
| 386 |
pDstPixel->rgba[2] = static_cast<uint8_t>(blue / samples); |
| 387 |
pDstPixel->rgba[3] = static_cast<uint8_t>(alpha / samples); |
| 388 |
} |
| 389 |
else |
| 390 |
{ |
| 391 |
pDstPixel->rgba[0] = static_cast<uint8_t>(red); |
| 392 |
pDstPixel->rgba[1] = static_cast<uint8_t>(green); |
| 393 |
pDstPixel->rgba[2] = static_cast<uint8_t>(blue); |
| 394 |
pDstPixel->rgba[3] = static_cast<uint8_t>(alpha); |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
} |