CS50-Blur

Created Diff never expires
7 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
176 lines
13 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
176 lines
void blur(int height, int width, RGBTRIPLE image[height][width])
void blur(int height, int width, RGBTRIPLE image[height][width])
{
{
RGBTRIPLE imgcpy2[height][width];
RGBTRIPLE imgcpy2[height][width];
for (int hi = 0; hi < height; hi++)
for (int hi = 0; hi < height; hi++)
{
{
for (int wj = 0; wj < width; wj++)
for (int wj = 0; wj < width; wj++)
{
{
//set original pixel info into a new array:
//set original pixel info into a new array:
imgcpy2[hi][wj] = image[hi][wj];
imgcpy2[hi][wj] = image[hi][wj];
}
}
}
}
for (int i = 0; i < height; i++)
for (int i = 0; i < height; i++)
{
{
for (int j = 0; j < width; j++)
for (int j = 0; j < width; j++)
{
{
// Variables to sum pixels
// Variables to sum pixels
int avpixr = 0;
int avpixr = 0;
int avpixg = 0;
int avpixg = 0;
int avpixb = 0;
int avpixb = 0;
// Counter to use in averages
// Counter to use in averages
float n = 0.00;
float n = 0.00;


//Top Row pixels
//Top Row pixels
//Top left corner
//Top left corner
if (i == 0 && j == 0)
if (i == 0 && j == 0)
{
{
for (int h = i; h < i + 2; h++)
for (int h = i; h < i + 2; h++)
{
{
// the pixel right
// the pixel right
for (int w = j; w < j + 2; w++)
for (int w = j; w < j + 2; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;
}
}
}
}
}
}
//Top row middle pixels
//Top row middle pixels
if (i == 0 && j > 0 && j < width)
if (i == 0 && j > 0 && j < width - 1)
{
{
for (int h = i; h < i + 2; h++)
for (int h = i; h < i + 2; h++)
{
{
// the pixel left and right
// the pixel left and right
for (int w = j - 1; w < j + 2; w++)
for (int w = j - 1; w < j + 2; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;
}
}
}
}
}
}
//Top right corner pixel
//Top right corner pixel
if (i == 0 && j == width)
if (i == 0 && j == width - 1)
{
{
for (int h = i; h < i + 2; h++)
for (int h = i; h < i + 2; h++)
{
{
// the pixel left
// the pixel left
for (int w = j - 1; w == j; w++)
for (int w = j - 1; w < j + 1; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;
}
}
}
}
}
}


//Middle rows:
//Middle rows:
//Middle leftmost pixel
//Middle leftmost pixel
if (i > 0 && i < height && j == 0)
if (i > 0 && i < height - 1 && j == 0)
{
{
// the pixels above through below
// the pixels above through below
for (int h = i - 1; h < i + 2; h++)
for (int h = i - 1; h < i + 2; h++)
{
{
// the pixels right
// the pixels right
for (int w = j; w < j + 2; w++)
for (int w = j; w < j + 2; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;
}
}
}
}
}
}
//Middle row middle pixels
//Middle row middle pixels
if (i > 0 && i < height && j > 0 && j < width)
if (i > 0 && i < height - 1 && j > 0 && j < width - 1)
{
{
// the pixels above through below
// the pixels above through below
for (int h = i - 1; h < i + 2; h++)
for (int h = i - 1; h < i + 2; h++)
{
{
// the pixels right and left
// the pixels right and left
for (int w = j - 1; w < j + 2; w++)
for (int w = j - 1; w < j + 2; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;
}
}
}
}
}
}
// Middle rightmost pixel
// Middle rightmost pixel
if (i > 0 && i < height && j == width)
if (i > 0 && i < height - 1 && j == width - 1)
{
{
// the pixels above through below
// the pixels above through below
for (int h = i - 1; h < i + 2; h++)
for (int h = i - 1; h < i + 2; h++)
{
{
// the pixels left
// the pixels left
for (int w = j - 1; w < j + 1; w++)
for (int w = j - 1; w < j + 1; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;
}
}
}
}
}
}


//Bottom rows:
//Bottom rows:
//Bottom left corner
//Bottom left corner
if (i == height && j == 0)
if (i == height - 1 && j == 0)
{
{
for (int h = i - 1; h < i + 1; h++)
for (int h = i - 1; h < i + 1; h++)
{
{
// the pixel right
// the pixel right
for (int w = j; w < j + 2; w++)
for (int w = j; w < j + 2; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;
}
}
}
}
}
}
//Pixels bottom middle
//Pixels bottom middle
if (i == height && j > 0 && j < width)
if (i == height - 1 && j > 0 && j < width - 1)
{
{
// the pixels above through itself
// the pixels above through itself
for (int h = i - 1; h < i + 1; h++)
for (int h = i - 1; h < i + 1; h++)
{
{
// the pixels right and left
// the pixels right and left
for (int w = j - 1; w < j + 2; w++)
for (int w = j - 1; w < j + 2; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;
}
}
}
}
}
}
//Pixels right corner
//Pixels right corner
if (i == height && j == width)
if (i == height - 1 && j == width - 1)
{
{
for (int h = i - 1; h < i + 1; h++)
for (int h = i - 1; h < i + 1; h++)
{
{


for (int w = j - 1; w < j + 1; w++)
for (int w = j - 1; w < j + 1; w++)
{
{
avpixr += imgcpy2[h][w].rgbtRed;
avpixr += imgcpy2[h][w].rgbtRed;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixg += imgcpy2[h][w].rgbtGreen;
avpixb += imgcpy2[h][w].rgbtBlue;
avpixb += imgcpy2[h][w].rgbtBlue;
n++;
n++;


}
}
}
}
}
}
image[i][j].rgbtBlue = roundf(avpixb / (float)n);
image[i][j].rgbtBlue = roundf(avpixb / (float)n);
image[i][j].rgbtGreen = roundf(avpixg / (float)n);
image[i][j].rgbtGreen = roundf(avpixg / (float)n);
image[i][j].rgbtRed = roundf(avpixr / (float)n);
image[i][j].rgbtRed = roundf(avpixr / (float)n);


}
}
}
}


return;
return;
}
}