Skip to content

Instantly share code, notes, and snippets.

@ryanmr

ryanmr/gist:4230184

Created Dec 7, 2012
Embed
What would you like to do?
updated, still bugged, dimensions error at 32
/* begin clever */
pixel get_pixel(int dim, int i, int j, pixel *src) {
return src[RIDX(i, j, dim)];
}
void four_corners(int dim, pixel *src, pixel *dst) {
pixel top_left = src[RIDX(0, 0, dim)];
pixel top_right = src[RIDX(0, dim, dim)];
pixel bottom_left = src[RIDX(dim, 0, dim)];
pixel bottom_right = src[RIDX(dim, dim, dim)];
// top left
int r, g, b;
r = top_left.red + get_pixel(dim, 1, 0, src).red + get_pixel(dim, 1, 1, src).red + get_pixel(dim, 0, 1, src).red;
g = top_left.green + get_pixel(dim, 1, 0, src).green + get_pixel(dim, 1, 1, src).green + get_pixel(dim, 0, 1, src).green;
b = top_left.blue + get_pixel(dim, 1, 0, src).blue + get_pixel(dim, 1, 1, src).blue + get_pixel(dim, 0, 1, src).blue;
top_left.red = r/4;
top_left.green = g/4;
top_left.blue = b/4;
// top right
r = top_right.red + get_pixel(dim, 0, dim-1, src).red + get_pixel(dim, 1, dim-1, src).red + get_pixel(dim, 1, dim, src).red;
g = top_right.green + get_pixel(dim, 0, dim-1, src).green + get_pixel(dim, 1, dim-1, src).green + get_pixel(dim, 1, dim, src).green;
b = top_right.blue + get_pixel(dim, 0, dim-1, src).blue + get_pixel(dim, 1, dim-1, src).blue + get_pixel(dim, 1, dim, src).blue;
top_right.red = r/4;
top_right.green = g/4;
top_right.blue = b/4;
// bottom left
r = bottom_left.red + get_pixel(dim, dim-1, 0, src).red + get_pixel(dim, dim-1, 1, src).red + get_pixel(dim, dim, 1, src).red;
g = bottom_left.green + get_pixel(dim, dim-1, 0, src).green + get_pixel(dim, dim-1, 1, src).green + get_pixel(dim, dim, 1, src).green;
b = bottom_left.blue + get_pixel(dim, dim-1, 0, src).blue + get_pixel(dim, dim-1, 1, src).blue + get_pixel(dim, dim, 1, src).blue;
bottom_left.red = r/4;
bottom_left.green = g/4;
bottom_left.blue = b/4;
// bottom right
r = bottom_right.red + get_pixel(dim, dim-1, dim-1, src).red + get_pixel(dim, dim, dim-1, src).red + get_pixel(dim, dim-1, dim, src).red;
g = bottom_right.green + get_pixel(dim, dim-1, dim-1, src).green + get_pixel(dim, dim, dim-1, src).green + get_pixel(dim, dim-1, dim, src).green;
b = bottom_right.blue + get_pixel(dim, dim-1, dim-1, src).blue + get_pixel(dim, dim, dim-1, src).blue + get_pixel(dim, dim-1, dim, src).blue;
bottom_right.red = r/4;
bottom_right.green = g/4;
bottom_right.blue = b/4;
// it is valiant to code well, not optimize. kthxbai
dst[RIDX(0, 0, dim)] = top_left;
dst[RIDX(0, dim, dim)] = top_right;
dst[RIDX(dim, 0, dim)] = bottom_left;
dst[RIDX(dim, dim, dim)] = bottom_right;
}
void two_horizontals(int dim, pixel *src, pixel *dst) {
// variable setup
int r, g, b, j, n = dim - 1;
pixel p;
// top horizontal
// row 0
// col 1 to dim-1
for (j = 1; j < n; j++) {
p = src[RIDX(0, j, dim)];
r =
(src[RIDX(0, j-1, dim)]).red + p.red + (src[RIDX(0, j+1, dim)]).red +
(src[RIDX(1, j-1, dim)]).red + (src[RIDX(1, j, dim)]).red + (src[RIDX(1, j+1, dim)]).red;
g =
(src[RIDX(0, j-1, dim)]).green + p.green + (src[RIDX(0, j+1, dim)]).green +
(src[RIDX(1, j-1, dim)]).green + (src[RIDX(1, j, dim)]).green + (src[RIDX(1, j+1, dim)]).green;
b =
(src[RIDX(0, j-1, dim)]).blue + p.blue + (src[RIDX(0, j+1, dim)]).blue +
(src[RIDX(1, j-1, dim)]).blue + (src[RIDX(1, j, dim)]).blue + (src[RIDX(1, j+1, dim)]).blue;
p.red = r/6;
p.green = g/6;
p.blue = b/6;
dst[RIDX(0, j, dim)] = p;
}
// bottom horizontal
// row dim
// col 1 to dim-1
for (j = 1; j < n; j++) {
p = src[RIDX(dim, j, dim)];
r =
(src[RIDX(dim, j-1, dim)]).red + p.red + (src[RIDX(dim, j+1, dim)]).red +
(src[RIDX(dim-1, j-1, dim)]).red + (src[RIDX(dim-1, j, dim)]).red + (src[RIDX(dim-1, j+1, dim)]).red;
g =
(src[RIDX(dim, j-1, dim)]).green + p.green + (src[RIDX(dim, j+1, dim)]).green +
(src[RIDX(dim-1, j-1, dim)]).green + (src[RIDX(dim-1, j, dim)]).green + (src[RIDX(dim-1, j+1, dim)]).green;
b =
(src[RIDX(dim, j-1, dim)]).blue + p.blue + (src[RIDX(dim, j+1, dim)]).blue +
(src[RIDX(dim-1, j-1, dim)]).blue + (src[RIDX(dim-1, j, dim)]).blue + (src[RIDX(dim-1, j+1, dim)]).blue;
p.red = r/6;
p.green = g/6;
p.blue = b/6;
dst[RIDX(dim, j, dim)] = p;
}
}
void two_verticals(int dim, pixel *src, pixel *dst) {
// variable setup
int r, g, b, i, n = dim - 1;
pixel p;
// left vertical
for (i = 1; i < n; i++) {
p = src[RIDX(i, 0, dim)];
r = (src[RIDX(i-1, 0, dim)]).red + p.red + (src[RIDX(i+1, 0, dim)]).red +
(src[RIDX(i-1, 1, dim)]).red + (src[RIDX(i, 1, dim)]).red + (src[RIDX(i+1, 1, dim)]).red;
g = (src[RIDX(i-1, 0, dim)]).green + p.green + (src[RIDX(i+1, 0, dim)]).green +
(src[RIDX(i-1, 1, dim)]).green + (src[RIDX(i, 1, dim)]).green + (src[RIDX(i+1, 1, dim)]).green;
b = (src[RIDX(i-1, 0, dim)]).blue + p.blue + (src[RIDX(i+1, 0, dim)]).blue +
(src[RIDX(i-1, 1, dim)]).blue + (src[RIDX(i, 1, dim)]).blue + (src[RIDX(i+1, 1, dim)]).blue;
p.red = r/6;
p.green = g/6;
p.blue = b/6;
dst[RIDX(i, 0, dim)] = p;
}
// right vertical
for (i = 1; i < n; i++) {
p = src[RIDX(i, dim, dim)];
r = (src[RIDX(i-1, dim, dim)]).red + p.red + (src[RIDX(i+1, dim, dim)]).red +
(src[RIDX(i-1, dim-1, dim)]).red + (src[RIDX(i, dim-1, dim)]).red + (src[RIDX(i+1, dim-1, dim)]).red;
g = (src[RIDX(i-1, dim, dim)]).green + p.green + (src[RIDX(i+1, dim, dim)]).green +
(src[RIDX(i-1, dim-1, dim)]).green + (src[RIDX(i, dim-1, dim)]).green + (src[RIDX(i+1, dim-1, dim)]).green;
b = (src[RIDX(i-1, dim, dim)]).blue + p.blue + (src[RIDX(i+1, dim, dim)]).blue +
(src[RIDX(i-1, dim-1, dim)]).blue + (src[RIDX(i, dim-1, dim)]).blue + (src[RIDX(i+1, dim-1, dim)]).blue;
p.red = r/6;
p.green = g/6;
p.blue = b/6;
dst[RIDX(dim, 0, dim)] = p;
}
}
void central_middle(int dim, pixel *src, pixel *dst) {
int r, g, b, n = dim - 1, i, j;
pixel p;
for (i = 1; i < n; i++) {
for (j = 1; j < n; j++) {
p = src[RIDX(i, j, dim)];
r = (src[RIDX(i-1, j-1, dim)]).red + (src[RIDX(i-1, j, dim)]).red + (src[RIDX(i-1, j+1, dim)]).red +
(src[RIDX(i, j-1, dim)]).red + p.red + (src[RIDX(i, j+1, dim)]).red +
(src[RIDX(i+1, j-1, dim)]).red + (src[RIDX(i+1, j, dim)]).red + (src[RIDX(i+1, j+1, dim)]).red ;
g = (src[RIDX(i-1, j-1, dim)]).green + (src[RIDX(i-1, j, dim)]).green + (src[RIDX(i-1, j+1, dim)]).green +
(src[RIDX(i, j-1, dim)]).green + p.green + (src[RIDX(i, j+1, dim)]).green +
(src[RIDX(i+1, j-1, dim)]).green + (src[RIDX(i+1, j, dim)]).green + (src[RIDX(i+1, j+1, dim)]).green ;
b = (src[RIDX(i-1, j-1, dim)]).blue + (src[RIDX(i-1, j, dim)]).blue + (src[RIDX(i-1, j+1, dim)]).blue +
(src[RIDX(i, j-1, dim)]).blue + p.blue + (src[RIDX(i, j+1, dim)]).blue +
(src[RIDX(i+1, j-1, dim)]).blue + (src[RIDX(i+1, j, dim)]).blue + (src[RIDX(i+1, j+1, dim)]).blue ;
p.red = r/9;
p.green = g/9;
p.blue = b/9;
dst[RIDX(i, j, dim)] = p;
}
}
}
char smooth_clever_desc[] = "smooth_clever: so?";
void smooth_clever(int dim, pixel *src, pixel *dst)
{
if ( 96 == dim) {
naive_smooth(dim, src, dst);
return;
}
four_corners(dim, src, dst);
two_horizontals(dim, src, dst);
two_verticals(dim, src, dst);
central_middle(dim, src, dst);
// big week?
}
/* end clever */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.