|
1 | 1 | #include <iostream> |
2 | 2 | #include <fstream> |
| 3 | +#include <cmath> |
3 | 4 | #include <string.h> |
4 | 5 | #include <time.h> |
5 | 6 | #include <math.h> |
@@ -354,3 +355,50 @@ bool TGAImage::scale(int w, int h) { |
354 | 355 | return true; |
355 | 356 | } |
356 | 357 |
|
| 358 | +float* gaussian_kernel(const int radius) { |
| 359 | + int size = (radius*2)+1; |
| 360 | + float norm = 1.f/(std::sqrt(2.f*M_PI)*radius); |
| 361 | + float* gaussian_kernel = new float[size]; |
| 362 | + float coeff = -1.f/(2.f*radius*radius); |
| 363 | + |
| 364 | + float sum = 0.f; |
| 365 | + for (int i=0; i<size; i++) { |
| 366 | + gaussian_kernel[i] = norm * exp(powl(i-radius,2)*coeff); |
| 367 | + sum += gaussian_kernel[i]; |
| 368 | + } |
| 369 | + for (int i=size; i--; gaussian_kernel[i] /= sum); |
| 370 | + return gaussian_kernel; |
| 371 | +} |
| 372 | + |
| 373 | +void TGAImage::gaussian_blur(const int radius) { |
| 374 | + float *kernel = gaussian_kernel(radius); |
| 375 | + TGAImage tmp(*this); |
| 376 | + int size = (radius*2)+1; |
| 377 | + for (int j=size; j<get_height(); j++) { |
| 378 | + for (int i=0; i<get_width(); i++) { |
| 379 | + float BGRA[4] = {0,0,0,0}; |
| 380 | + for (int k=0; k<size; k++){ |
| 381 | + TGAColor c = get(i, j-size+k); |
| 382 | + for (int d=0; d<bytespp; d++) BGRA[d] += float(c[d])*kernel[k]; |
| 383 | + } |
| 384 | + unsigned char cBGRA[4]; |
| 385 | + for (int i=4; i--; cBGRA[i] = (unsigned char)BGRA[i]); |
| 386 | + tmp.set(i, j, TGAColor(cBGRA, bytespp)); |
| 387 | + } |
| 388 | + } |
| 389 | + for (int j=0; j<get_height(); j++) { |
| 390 | + for (int i=size; i<get_width(); i++) { |
| 391 | + float BGRA[4] = {0,0,0,0}; |
| 392 | + for (int k=0; k<size; k++){ |
| 393 | + TGAColor c = tmp.get(i-size+k, j); |
| 394 | + for (int d=0; d<bytespp; d++) BGRA[d] += float(c[d])*kernel[k]; |
| 395 | + } |
| 396 | + unsigned char cBGRA[4]; |
| 397 | + for (int i=4; i--; cBGRA[i] = (unsigned char)BGRA[i]); |
| 398 | + set(i, j, TGAColor(cBGRA, bytespp)); |
| 399 | + } |
| 400 | + } |
| 401 | + delete [] kernel; |
| 402 | +} |
| 403 | + |
| 404 | + |
0 commit comments