Skip to content

Instantly share code, notes, and snippets.

@emmahyde
Last active July 8, 2021 03:23
Show Gist options
  • Save emmahyde/3912313d69f706985e8eb4da45308a4f to your computer and use it in GitHub Desktop.
Save emmahyde/3912313d69f706985e8eb4da45308a4f to your computer and use it in GitHub Desktop.
maybe!
#include "helpers.h"
#include <math.h>
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// instantiate a new array with `height` number of rows and `width` number of columns
RGBTRIPLE blurred_image[height][width];
// Loop over each row in `image`
// `row` is some number from 0 to `height`
// `row` increments once every iteration
for (int row = 0; row < height; row++)
{
//Loop over each column in `row`
// `col` is some number from 0 to `width`
// `col` increments once every iteration
for (int col = 0; col < width; col++)
{
// Every iteration we have values `row` (0->height) and `column` (0->width),
// which associates with a particular pixel that has up to 8 adjacent pixels.
// Lets say that a pixels own self also counts as adjacent, so 9 total.
// The goal is to take each immediately adjacent pixel IF IT EXISTS
// (i.e. within 1 row or column),
// and average their RGB values together. In a perfect scenario,
// (i.e. the pixel has 9 pixels, including itself, to average):
//// [example]:
// c0 c1 c2
// * | * | * r0
//-------------
// * | X | * r1
//-------------
// * | * | * r2
// NOTE: c0 - c2 and r0 - r2 are just names I gave the columns for the purpose
// of illustration, we can assume this image is a 2x2 square and the happy
// path is that we're currently evaluating image[1][1].
// where * == adjacent pixel and X == current pixel.
// X is at image[row][column]
// when x is in column c1,
// adjacent columns are c0 through c2
// when x is in row r1,
// adjacent rows are r0 through r2
// using the above logic, we know that for every pixel,
// we want to iterate from the row before its row and the
// row after its row,
// i.e. row - 1 to row + 1.
// we also know that for every pixel, we want to iterate
// from the column before its column and the column after
// its column,
// i.e. col - 1 to col + 1.
// using this above logic, we implement the two loops required to pass over
// all immediately adjacent pixels to the current pixel at image[row][col].
// in order to average a particular pixel,
// we need to:
//// 1. collect the red, green, blue values of each adjacent pixel to average
//// 2. know how many pixels were added to these values, assuming that we will
//// NOT always have 9 pixels to average, if it is on any side or in a corner:
//// [examples]:
//// 4 adjacent pixels:
// c0 c1 c2
// * | * | * r0
//-------------
// * | * | * r1
//-------------
// * | * | X r2
//// 6 adjacent pixels:
// c0 c1 c2
// * | * | * r0
//-------------
// * | * | X r1
//-------------
// * | * | * r2
// so lets create temporary variables `counter`, `red`, `green`, `blue`
// for each iteration.
// instantiate counters for X, these will only be averaged by counter when we've
// looked at all adjacent values and added them to the below tracking variables
int counter = 0;
float red = 0;
float green = 0;
float blue = 0;
//For each adjacent row to `row`
for (int adj_row = row - 1; adj_row < row + 1; adj_row++)
{
// given that the adjacent row we've picked 'exists',
// we'll want to iterate through the columns for that row
if (adj_row < height && adj_row >= 0)
{
// for each adjacent column to `col`
for (int adj_col = col - 1; adj_col < col + 1; adj_col++)
{
// given that the adjacent column we've picked 'exists',
// we'll want to add the RGB values and increment the counter
if (adj_col < width && adj_col >= 0)
{
counter++;
red += image[adj_row][adj_col].rgbtRed;
green += image[adj_row][adj_col].rgbtGreen;
blue += image[adj_row][adj_col].rgbtBlue;
}
}
}
}
// we've passed over and counted all valid adjacent pixels now
// average X and copy new blurred value as a rounded int to blurred_image
blurred_image[row][col].rgbtRed = round(red / counter)
blurred_image[row][col].rgbtRed = round(green / counter)
blurred_image[row][col].rgbtRed = round(blue / counter)
}
}
// can you assign something to something else like this?
image = blurred_image;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment