Add a non-vignetting blur function

This commit is contained in:
Kevin Lange 2014-04-01 21:23:22 -07:00
parent 3140c8e4b2
commit 091ab50a3f
2 changed files with 73 additions and 0 deletions

View File

@ -270,6 +270,78 @@ void blur_context(gfx_context_t * _dst, gfx_context_t * _src, double amount) {
}
}
void blur_context_no_vignette(gfx_context_t * _dst, gfx_context_t * _src, double amount) {
int width, height;
int x, y, z, w, i, j, k;
uint32_t *s, *d, a, p;
uint8_t * src, * dst;
uint8_t kernel[17];
const int size = ARRAY_LENGTH(kernel);
const int half = size / 2;
width = _src->width;
height = _src->height;
src = _src->backbuffer;
dst = _dst->backbuffer;
a = 0;
for (i = 0; i < size; ++i) {
double f = i - half;
a += kernel[i] = exp (- f * f / amount) * 80;
}
for (i = 0; i < height; ++i) {
s = (uint32_t *) (src + i * (_src->width * 4));
d = (uint32_t *) (dst + i * (_dst->width * 4));
for (j = 0; j < width; ++j) {
x = y = z = w = 0;
for (k = 0; k < size; ++k) {
int j_ = j;
if (j_ - half + k < 0) {
j_ = half - k;
} else if (j_ - half + k >= width) {
j_ = width - k + half - 1;
}
p = s[j_ - half + k];
x += ((p >> 24) & 0xFF) * kernel[k];
y += ((p >> 16) & 0xFF) * kernel[k];
z += ((p >> 8) & 0xFF) * kernel[k];
w += ((p >> 0) & 0xFF) * kernel[k];
}
d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
}
}
for (i = 0; i < height; ++i) {
s = (uint32_t *) (src + i * (_src->width * 4));
d = (uint32_t *) (dst + i * (_dst->width * 4));
for (j = 0; j < width; ++j) {
x = y = z = w = 0;
for (k = 0; k < size; ++k) {
int i_ = i;
if (i_ - half + k < 0) {
i_ = half - k;
} else if (i_ - half + k >= height) {
i_ = height - k + half - 1;
}
s = (uint32_t *) (dst + (i_ - half + k) * (_dst->width * 4));
p = s[j];
x += ((p >> 24) & 0xFF) * kernel[k];
y += ((p >> 16) & 0xFF) * kernel[k];
z += ((p >> 8) & 0xFF) * kernel[k];
w += ((p >> 0) & 0xFF) * kernel[k];
}
d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
}
}
}
void load_sprite(sprite_t * sprite, char * filename) {
/* Open the requested binary */
FILE * image = fopen(filename, "r");

View File

@ -64,6 +64,7 @@ gfx_context_t * init_graphics_sprite(sprite_t * sprite);
sprite_t * create_sprite(size_t width, size_t height, int alpha);
void blur_context(gfx_context_t * _dst, gfx_context_t * _src, double amount);
void blur_context_no_vignette(gfx_context_t * _dst, gfx_context_t * _src, double amount);
void sprite_free(sprite_t * sprite);
void load_sprite(sprite_t * sprite, char * filename);