So the problem is called "Filters" PSET4 and I was assigned to code a "blur" function that blurs an image. My code blurs the sample pictures but when I use the in-website tool to check the code it says that the program was "unable to blur a 3x3 and 4x4 pictures correctly". This is the code:
Here's what the tool says:
4x4
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE image0[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image0[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0 && j == 0) //topleft
{
float red00 = round((float)(image0[0][0].rgbtRed + image0[0][1].rgbtRed + image0[1][1].rgbtRed + image[1][0].rgbtRed) / 4);
float green00 = round((image0[0][0].rgbtGreen + image0[0][1].rgbtGreen + image0[1][1].rgbtGreen + image[1][0].rgbtGreen) / 4);
float blue00 = round((image0[0][0].rgbtBlue + image0[0][1].rgbtBlue + image0[1][1].rgbtBlue + image[1][0].rgbtBlue) / 4);
image[0][0].rgbtRed = red00;
image[0][0].rgbtGreen = green00;
image[0][0].rgbtBlue = blue00;
}
else if (i == 0 && j == width - 1) //topright
{
float red01 = round((float)(image0[0][width - 1].rgbtRed + image0[0][width - 2].rgbtRed + image0[1][width - 2].rgbtRed + image0[1][width - 1].rgbtRed)
/ 4);
float green01 = round((float)(image0[0][width - 1].rgbtGreen + image0[0][width - 2].rgbtGreen + image0[1][width - 2].rgbtGreen + image0[1][width - 1].rgbtGreen)
/ 4);
float blue01 = round(((float)image0[0][width - 1].rgbtBlue + image0[0][width - 2].rgbtBlue + image0[1][width - 2].rgbtBlue + image0[1][width - 1].rgbtBlue)
/ 4);
image[0][width - 1].rgbtRed = red01;
image[0][width - 1].rgbtGreen = green01;
image[0][width - 1].rgbtBlue = blue01;
}
else if (i == height - 1 && j == 0) //bottomleft
{
float red10 = round((float)(image0[height - 1][0].rgbtRed + image0[height - 2][0].rgbtRed + image0[height - 2][1].rgbtRed + image0[height - 1][1].rgbtRed)
/ 4);
float green10 = round((float)(image0[height - 1][0].rgbtGreen + image0[height - 2][0].rgbtGreen + image0[height - 2][1].rgbtGreen + image0[height - 1][1].rgbtGreen)
/ 4);
float blue10 = round((float)(image0[height - 1][0].rgbtBlue + image0[height - 2][0].rgbtBlue + image0[height - 2][1].rgbtBlue + image0[height - 1][1].rgbtBlue)
/ 4);
image[height - 1][0].rgbtRed = red10;
image[height - 1][0].rgbtGreen = green10;
image[height - 1][0].rgbtBlue = blue10;
}
else if (i == height - 1 && j == width - 1) //bottomright
{
float red11 = round((float)(image0[height - 1][width - 1].rgbtRed + image0[height - 2][width - 1].rgbtRed + image0[height - 2][width - 2].rgbtRed
+ image0[height - 1][width - 2].rgbtRed) / 4);
float green11 = round((float)(image0[height - 1][width - 1].rgbtGreen + image0[height - 2][width - 1].rgbtGreen + image0[height - 2][width - 2].rgbtGreen
+ image0[height - 1][width - 2].rgbtGreen) / 4);
float blue11 = round((float)(image0[height - 1][width - 1].rgbtBlue + image0[height - 2][width - 1].rgbtBlue + image0[height - 2][width - 2].rgbtBlue
+ image0[height - 1][width - 2].rgbtBlue) / 4);
image[height - 1][width - 1].rgbtRed = red11;
image[height - 1][width - 1].rgbtGreen = green11;
image[height - 1][width - 1].rgbtBlue = blue11;
}
else if (i == 0 && j != 0 && j != width - 1) //top
{
float redtop = round((float)(image0[i][j].rgbtRed + image0[i][j - 1].rgbtRed + image0[i + 1][j - 1].rgbtRed
+ image0[i + 1][j].rgbtRed + image0[i + 1][j + 1].rgbtRed + image0[i][j + 1].rgbtRed) / 6);
float greentop = round((float)(image0[i][j].rgbtGreen + image0[i][j - 1].rgbtGreen + image0[i + 1][j - 1].rgbtGreen
+ image0[i + 1][j].rgbtGreen + image0[i + 1][j + 1].rgbtGreen + image0[i][j + 1].rgbtGreen) / 6);
float bluetop = round((float)(image0[i][j].rgbtBlue + image0[i][j - 1].rgbtBlue + image0[i + 1][j - 1].rgbtBlue
+ image0[i + 1][j].rgbtBlue + image0[i + 1][j + 1].rgbtBlue + image0[i][j + 1].rgbtBlue) / 6);
image[i][j].rgbtRed = redtop;
image[i][j].rgbtGreen = greentop;
image[i][j].rgbtBlue = bluetop;
}
else if (i != 0 && i != height - 1 && j == 0) //left
{
float redleft = round((float)(image0[i][j].rgbtRed + image0[i - 1][j].rgbtRed + image0[i - 1][j + 1].rgbtRed
+ image0[i][j + 1].rgbtRed + image0[i + 1][j + 1].rgbtRed + image0[i + 1][j].rgbtRed) / 6);
float greenleft = round((float)(image0[i][j].rgbtGreen + image0[i - 1][j].rgbtGreen + image0[i - 1][j + 1].rgbtGreen
+ image0[i][j + 1].rgbtGreen + image0[i + 1][j + 1].rgbtGreen + image0[i + 1][j].rgbtGreen) / 6);
float blueleft = round((float)(image0[i][j].rgbtBlue + image0[i - 1][j].rgbtBlue + image0[i - 1][j + 1].rgbtBlue
+ image0[i][j + 1].rgbtBlue + image0[i + 1][j + 1].rgbtBlue + image0[i + 1][j].rgbtBlue) / 6);
image[i][j].rgbtRed = redleft;
image[i][j].rgbtGreen = greenleft;
image[i][j].rgbtBlue = blueleft;
}
else if (i != 0 && i != height - 1 && j == width - 1) //right
{
float redright = round((float)(image0[i][j].rgbtRed + image[i - 1][j].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i][j - 1].rgbtRed
+ image0[i + 1][j - 1].rgbtRed + image0[i + 1][j].rgbtRed) / 6);
float greenright = round((float)(image0[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i][j - 1].rgbtGreen
+ image0[i + 1][j - 1].rgbtGreen + image0[i + 1][j].rgbtGreen) / 6);
float blueright = round((float)(image0[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i][j - 1].rgbtBlue
+ image0[i + 1][j - 1].rgbtBlue + image0[i + 1][j].rgbtBlue) / 6);
image[i][j].rgbtRed = redright;
image[i][j].rgbtGreen = greenright;
image[i][j].rgbtBlue = blueright;
}
else if (i == height - 1 && j != 0 && j != width - 1) //bottom
{
float redbottom = round((float)(image0[i][j].rgbtRed + image0[i][j - 1].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i - 1][j].rgbtRed
+ image0[i - 1][j + 1].rgbtRed + image0[i][j + 1].rgbtRed) / 6);
float greenbottom = round((float)(image0[i][j].rgbtGreen + image0[i][j - 1].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i - 1][j].rgbtGreen
+ image0[i - 1][j + 1].rgbtGreen + image0[i][j + 1].rgbtGreen) / 6);
float bluebottom = round((float)(image0[i][j].rgbtBlue + image0[i][j - 1].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i - 1][j].rgbtBlue
+ image0[i - 1][j + 1].rgbtBlue + image0[i][j + 1].rgbtBlue) / 6);
image[i][j].rgbtRed = redbottom;
image[i][j].rgbtGreen = greenbottom;
image[i][j].rgbtBlue = bluebottom;
}
else
{
float red = round((float)(image0[i][j].rgbtRed + image0[i - 1][j - 1].rgbtRed + image0[i - 1][j].rgbtRed + image0[i - 1][j + 1].rgbtRed
+ image0[i][j - 1].rgbtRed + image0[i][j + 1].rgbtRed + image0[i + 1][j - 1].rgbtRed + image0[i + 1][j].rgbtRed + image0[i + 1][j + 1].rgbtRed)
/ 9);
float green = round((float)(image0[i][j].rgbtGreen + image0[i - 1][j - 1].rgbtGreen + image0[i - 1][j].rgbtGreen + image0[i - 1][j + 1].rgbtGreen
+ image0[i][j - 1].rgbtGreen + image0[i][j + 1].rgbtGreen + image0[i + 1][j - 1].rgbtGreen + image0[i + 1][j].rgbtGreen + image0[i + 1][j + 1].rgbtGreen)
/ 9);
float blue = round((float)(image0[i][j].rgbtBlue + image0[i - 1][j - 1].rgbtBlue + image0[i - 1][j].rgbtBlue + image0[i - 1][j + 1].rgbtBlue
+ image0[i][j - 1].rgbtBlue + image0[i][j + 1].rgbtBlue + image0[i + 1][j - 1].rgbtBlue + image0[i + 1][j].rgbtBlue + image0[i + 1][j + 1].rgbtBlue)
/ 9);
image[i][j].rgbtRed = red;
image[i][j].rgbtGreen = green;
image[i][j].rgbtBlue = blue;
}
}
}
return;
}
In this piece of code block, you use image in round function for three times by mistake. It should be image0.
if (i == 0 && j == 0) //topleft
{
float red00 = round((float)(image0[0][0].rgbtRed + image0[0][1].rgbtRed + image0[1][1].rgbtRed + image[1][0].rgbtRed) / 4);
float green00 = round((image0[0][0].rgbtGreen + image0[0][1].rgbtGreen + image0[1][1].rgbtGreen + image[1][0].rgbtGreen) / 4);
float blue00 = round((image0[0][0].rgbtBlue + image0[0][1].rgbtBlue + image0[1][1].rgbtBlue + image[1][0].rgbtBlue) / 4);
Related
I am trying to write I could that can edit images according to Sobel filter algorithm but it still gives me segmentation error when I try to use edges function but I couldn't know the reason can any one tell me what is the error ?
the target of such project is to turn any picture to be edged by multiplying each pixel colors values with a corresponding matrix but when I do so I got segmentation fault error (core dump) what shall I do ?
#include <stdio.h>
#include <math.h>
#include "helpers.h"
//#include "bmp.h"
//variables for
//int Gx_sum_red, Gx_sum_blue, Gx_sum_green, Gy_sum_red, Gy_sum_blue, Gy_sum_green;
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
//int height_edited = 0;
//int width_edited = 0;
//printf("height is %i, width is %i and RGBTRIPLE IS %i for green, %i for blue and %i for red\n", height, width,
// image[height_edited][width_edited].rgbtBlue, image[height_edited][width_edited].rgbtGreen, image[height_edited][width_edited].rgbtRed);
//RGBTRIPLE temp
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
image[i][j].rgbtGreen = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
image[i][j].rgbtRed = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
//int height_edited = height / 2;
//int width_edited = width / 2;
//printf("height is %i, width is %i and RGBTRIPLE IS %i for green, %i for blue and %i for red\n", height, width,
// image[height_edited][width_edited].rgbtBlue, image[height_edited][width_edited].rgbtGreen, image[height_edited][width_edited].rgbtRed);
RGBTRIPLE temp;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width / 2; j++)
{
temp.rgbtBlue = image[i][j].rgbtBlue;
temp.rgbtGreen = image[i][j].rgbtGreen;
temp.rgbtRed = image[i][j].rgbtRed;
image[i][j].rgbtBlue = image[i][width - j].rgbtBlue;
image[i][j].rgbtGreen = image[i][width - j].rgbtGreen;
image[i][j].rgbtRed = image[i][width - j].rgbtRed;
image[i][width - j].rgbtBlue = temp.rgbtBlue;
image[i][width - j].rgbtGreen = temp.rgbtGreen;
image[i][width - j].rgbtRed = temp.rgbtRed;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0 && j == 0)
{
image[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 4;
image[i][j].rgbtGreen = (image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 4;
image[i][j].rgbtRed = (image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 4;
}
else if (i == 0 && j == width - 1)
{
image[i][j].rgbtBlue = (image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4;
image[i][j].rgbtGreen = (image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4;
image[i][j].rgbtRed = (image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 4;
}
else if (i == height - 1 && j == 0)
{
image[i][j].rgbtBlue = (image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue) / 4;
image[i][j].rgbtGreen = (image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen) / 4;
image[i][j].rgbtRed = (image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed) / 4;
}
else if (i == height - 1 && j == width - 1)
{
image[i][j].rgbtBlue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue) / 4;
image[i][j].rgbtGreen = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen) / 4;
image[i][j].rgbtRed = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j].rgbtRed) / 4;
}
else if (i == 0 && (j != 0 || j != width - 1))
{
image[i][j].rgbtBlue = (image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6;
image[i][j].rgbtGreen = (image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6;
image[i][j].rgbtRed = (image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6;
}
else if (i == height - 1 && (j != 0 || j != width - 1))
{
image[i][j].rgbtBlue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue +
image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue) / 6;
image[i][j].rgbtGreen = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen +
image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen) / 6;
image[i][j].rgbtRed = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed +
image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed) / 6;
}
else if (j == 0 && (i != 0 || i != height - 1))
{
image[i][j].rgbtBlue = (image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6;
image[i][j].rgbtGreen = (image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6;
image[i][j].rgbtRed = (image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6;
}
else if (j == width - 1 && (i != 0 || i != height - 1))
{
image[i][j].rgbtBlue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6;
image[i][j].rgbtGreen = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6;
image[i][j].rgbtRed = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 6;
}
else
{
image[i][j].rgbtBlue = (image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue +
image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue +
image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 9;
image[i][j].rgbtGreen = (image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen +
image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen +
image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9;
image[i][j].rgbtRed = (image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed +
image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed +
image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 9;
}
}
}
return;
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int red_avg, blue_avg, green_avg;
int fGx_sum_red, fGx_sum_blue, fGx_sum_green, fGy_sum_red, fGy_sum_blue, fGy_sum_green;
int red_sum, blue_sum, green_sum;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//calculating the Gx sum of red values
if (i == 0 && j == 0)
{
fGx_sum_red = (image[i][j + 1].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);
fGx_sum_blue = (image[i][j + 1].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);
fGx_sum_green = (image[i][j + 1].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
fGy_sum_red = (image[i + 1][j].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);//
fGy_sum_blue = (image[i + 1][j].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);//
fGy_sum_green = (image[i + 1][j].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
}
else if (i == 0 && j == width - 1)
{
fGx_sum_red = (image[i][j - 1].rgbtRed * -2) + (image[i + 1][j - 1].rgbtRed * -1);
fGx_sum_blue = (image[i][j - 1].rgbtBlue * -2) + (image[i + 1][j - 1].rgbtBlue * -1);
fGx_sum_green = (image[i][j - 1].rgbtGreen * -2) + (image[i + 1][j - 1].rgbtGreen * -1);//
fGy_sum_red = (image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtRed * 2);//
fGy_sum_blue = (image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtBlue * 2);//
fGy_sum_green = (image[i + 1][j - 1].rgbtGreen * 1) + (image[i + 1][j].rgbtGreen * 2);//
}
else if (i == height - 1 && j == 0)
{
fGx_sum_red = (image[i - 1][j + 1].rgbtRed * 1) + (image[i][j + 1].rgbtRed * 2);
fGx_sum_blue = (image[i - 1][j + 1].rgbtBlue * 1) + (image[i][j + 1].rgbtBlue * 2);
fGx_sum_green = (image[i - 1][j + 1].rgbtGreen * 1) + (image[i][j + 1].rgbtGreen * 2);//
fGy_sum_red = (image[i - 1][j].rgbtRed * -2) + (image[i - 1][j + 1].rgbtRed * -1);//
fGy_sum_blue = (image[i - 1][j].rgbtBlue * -2) + (image[i - 1][j + 1].rgbtBlue * -1);//
fGy_sum_green = (image[i - 1][j].rgbtGreen * -2) + (image[i - 1][j + 1].rgbtGreen * -1);//
}
else if (i == height - 1 && j == width - 1)
{
fGx_sum_red = (image[i - 1][j - 1].rgbtRed * -1)+ (image[i][j - 1].rgbtRed * -2);
fGx_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i][j - 1].rgbtBlue * -2);
fGx_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i][j - 1].rgbtGreen * -2);//
fGy_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j].rgbtRed * -2);//
fGy_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j].rgbtBlue * -2);//
fGy_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j].rgbtGreen * -2);//
}
else if (i == 0 && (j != 0 || j != width - 1))
{
fGx_sum_red = (image[i][j - 1].rgbtRed * -2) + (image[i][j + 1].rgbtRed * 2) +
(image[i + 1][j - 1].rgbtRed * -1) + (image[i + 1][j + 1].rgbtRed * 1);
fGx_sum_blue = (image[i][j - 1].rgbtBlue * -2) + (image[i][j + 1].rgbtBlue * 2) +
(image[i + 1][j - 1].rgbtBlue * -1) + (image[i + 1][j + 1].rgbtBlue * 1);
fGx_sum_green = (image[i][j - 1].rgbtGreen * -2) + (image[i][j + 1].rgbtGreen * 2) +
(image[i + 1][j - 1].rgbtGreen * -1) + (image[i + 1][j + 1].rgbtGreen * 1);//
fGy_sum_red = (image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);//
fGy_sum_blue = (image[i + 1][j - 1].rgbtBlue * 1) + (image[i + 1][j].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);//
fGy_sum_green = (image[i + 1][j - 1].rgbtGreen * 1) + (image[i + 1][j].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
}
else if (i == height - 1 && (j != 0 || j != width - 1))
{
fGx_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j + 1].rgbtRed * 1) +
(image[i][j - 1].rgbtRed * -2) + (image[i][j + 1].rgbtRed * 2);
fGx_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j + 1].rgbtBlue * 1) +
(image[i][j - 1].rgbtBlue * -2) + (image[i][j + 1].rgbtBlue * 2);
fGx_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j + 1].rgbtGreen * 1) +
(image[i][j - 1].rgbtGreen * -2) + (image[i][j + 1].rgbtGreen * 2);//
fGy_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j].rgbtRed * -2) + (image[i - 1][j + 1].rgbtRed * -1);//
fGy_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j].rgbtBlue * -2) + (image[i - 1][j + 1].rgbtBlue * -1);//
fGy_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j].rgbtGreen * -2) + (image[i - 1][j + 1].rgbtGreen * -1);//
}
else if (j == 0 && (i != 0 || i != height - 1))
{
fGx_sum_red = (image[i - 1][j + 1].rgbtRed * 1) + (image[i][j + 1].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);
fGx_sum_blue = (image[i - 1][j + 1].rgbtBlue * 1) + (image[i][j + 1].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);
fGx_sum_green = (image[i - 1][j + 1].rgbtGreen * 1) + (image[i][j + 1].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
fGy_sum_red = (image[i - 1][j].rgbtRed * -2) + (image[i - 1][j + 1].rgbtRed * -1) +
(image[i + 1][j].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);//
fGy_sum_blue = (image[i - 1][j].rgbtBlue * -2) + (image[i - 1][j + 1].rgbtBlue * -1) +
(image[i + 1][j].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);//
fGy_sum_green = (image[i - 1][j].rgbtGreen * -2) + (image[i - 1][j + 1].rgbtGreen * -1) +
(image[i + 1][j].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
}
else if (j == width - 1 && (i != 0 || i != height - 1))
{
fGx_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i][j - 1].rgbtRed * -2) + (image[i + 1][j - 1].rgbtRed * -1);
fGx_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i][j - 1].rgbtBlue * -2) + (image[i + 1][j - 1].rgbtBlue * -1);
fGx_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i][j - 1].rgbtGreen * -2) + (image[i + 1][j - 1].rgbtGreen * -1);//
fGy_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j].rgbtRed * -2) +
(image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtRed * 2);//
fGy_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j].rgbtBlue * -2) +
(image[i + 1][j - 1].rgbtBlue * 1) + (image[i + 1][j].rgbtBlue * 2);//
fGy_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j].rgbtGreen * -2) +
(image[i + 1][j - 1].rgbtGreen * 1) + (image[i + 1][j].rgbtGreen * 2);//
}
else
{
fGx_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j + 1].rgbtRed * 1) +
(image[i][j - 1].rgbtRed * -2) + (image[i][j + 1].rgbtRed * 2) +
(image[i + 1][j - 1].rgbtRed * -1) + (image[i + 1][j + 1].rgbtRed * 1);
fGx_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j + 1].rgbtBlue * 1) +
(image[i][j - 1].rgbtBlue * -2) + (image[i][j + 1].rgbtBlue * 2) +
(image[i + 1][j - 1].rgbtBlue * -1) + (image[i + 1][j + 1].rgbtBlue * 1);
fGx_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j + 1].rgbtGreen * 1) +
(image[i][j - 1].rgbtGreen * -2) + (image[i][j + 1].rgbtGreen * 2) +
(image[i + 1][j - 1].rgbtGreen * -1) + (image[i + 1][j + 1].rgbtGreen * 1);//
fGy_sum_red = (image[i - 1][j - 1].rgbtRed * -1) + (image[i - 1][j].rgbtRed * -2) + (image[i - 1][j + 1].rgbtRed * -1) +
(image[i + 1][j - 1].rgbtRed * 1) + (image[i + 1][j].rgbtRed * 2) + (image[i + 1][j + 1].rgbtRed * 1);//
fGy_sum_blue = (image[i - 1][j - 1].rgbtBlue * -1) + (image[i - 1][j].rgbtBlue * -2) + (image[i - 1][j + 1].rgbtBlue * -1) +
(image[i + 1][j - 1].rgbtBlue * 1) + (image[i + 1][j].rgbtBlue * 2) + (image[i + 1][j + 1].rgbtBlue * 1);//
fGy_sum_green = (image[i - 1][j - 1].rgbtGreen * -1) + (image[i - 1][j].rgbtGreen * -2) + (image[i - 1][j + 1].rgbtGreen * -1) +
(image[i + 1][j - 1].rgbtGreen * 1) + (image[i + 1][j].rgbtGreen * 2) + (image[i + 1][j + 1].rgbtGreen * 1);//
}
printf("red_sum is %i and %i, blue_sum is %i and %i and green_sum is %i and %i\n", fGx_sum_red, fGy_sum_red, fGx_sum_blue, fGy_sum_blue,
fGx_sum_green, fGy_sum_green);
//calculating sum of Gx and Gy for each color
red_sum = (fGx_sum_red * fGx_sum_red) + (fGy_sum_red * fGy_sum_red);
blue_sum = (fGx_sum_blue * fGx_sum_blue) + (fGy_sum_blue * fGy_sum_blue);
green_sum = (fGx_sum_green * fGx_sum_green) + (fGy_sum_green * fGy_sum_green);
printf("red_sum is %i, blue_sum is %i and green_sum is %i\n", red_sum, blue_sum, green_sum);
red_avg = sqrt(red_sum);
blue_avg = sqrt(blue_sum);
green_avg = sqrt(green_sum);
printf("red_avg is %i, blue_avg is %i and green_avg is %i\n", red_avg, blue_avg, green_avg);
//updating colors values
image[i][j].rgbtRed = red_avg > 255 ? 255 : red_avg;
image[i][j].rgbtBlue = blue_avg > 255 ? 255 : blue_avg;
image[i][j].rgbtGreen = green_avg > 255 ? 255 : green_avg;
printf("image[i][j].rgbtRed is %i, image[i][j].rgbtBlue is %i and image[i][j].rgbtGreen is %i, i and j are %i and %i \n",
image[i][j].rgbtRed, image[i][j].rgbtBlue, image[i][j].rgbtGreen, i, j);
//continue;
}
}
return;
}
When i == 0 you try to deference image[i - 1][j] in this block of code. This is out of bound access to the image array and trigger a segfault for me:
} else if (i == 0 && (j != 0 || j != width - 1)) {
fGx_sum_red = (image[i - 1][j].rgbtRed * 0) ...
By analogy (in other words I have no idea if that is correct algorithm) replace i - 1 with i in that block.
So in this project , I need to take an image.bmp file and blur it using "box blur".
The instructions for Box Blurring as follows:
For this problem, we’ll use the “box blur,” which works by taking each pixel and, for each color value, giving it a new value by averaging the color values of neighboring pixels.
(example) The new value of each pixel would be the average of the values of all of the pixels that are within 1 row and column of the original pixel (forming a 3x3 box). For example, each of the color values for pixel 6 would be obtained by averaging the original color values of pixels 1, 2, 3, 5, 6, 7, 9, 10, and 11 (note that pixel 6 itself is included in the average). Likewise, the color values for pixel 11 would be be obtained by averaging the color values of pixels 6, 7, 8, 10, 11, 12, 14, 15 and 16.
For a pixel along the edge or corner, like pixel 15, we would still look for all pixels within 1 row and column: in this case, pixels 10, 11, 12, 14, 15, and 16.
Now I know my code is a bit long and amateur,but it is only because I am an amateur but I have come this far without any help:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// iterate through every row
for (int i = 0; i < height; i++)
{
//for every column
for (int j = 0; j < width; j++)
{
int avRed = 0;
int avGreen = 0;
int avBlue = 0;
// check if the current index is an index of an edge
if ((i == 0 && (j >= 0 && j <= width - 1))
|| ((i >= 0 && i <= height - 1) && j == 0)
|| (i == height - 1 && (j >= 0 && j <= width - 1))
|| ((i >= 0 && i <= height -1) && j == width - 1))
{
// if it is : check if it is a corner
if ((i == 0 && j == 0)
|| (i == 0 && j == width - 1)
|| (i == height - 1 && j == 0)
|| (i == height - 1 && j == width - 1))
{
if (i == 0 && j == 0)
{
avRed = round((image[i][j].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 4.0);
avGreen = round((image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 4.0);
avBlue = round((image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 4.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if (i == 0 && j == width - 1)
{
avRed = round((image[i][j].rgbtRed + image[i +1 ][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed) / 4.0);
avGreen = round((image[i][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen) / 4.0);
avBlue = round((image[i][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue) / 4.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if(i == height - 1 && j == 0)
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 4.0);
avGreen= round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 4.0);
avRed = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 4.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if (i == height - 1 && j == width - 1)
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed) / 4.0);
avGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen) / 4.0);
avBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue) / 4.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
}
else // if it is not a corner index
{
if (i == 0 && (j >= 1 && j <= width - 2))
{
avRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6.0);
avGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6.0);
avBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if ((i >= 1 && i <= height - 2) && j == 0)
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i + 1][j].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6.0);
avGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6.0);
avBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if (i == height - 1 && (j >= 1 && j <= width - 2))
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i - 1][j - 1].rgbtRed) / 6.0);
avGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen) / 6.0);
avBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue) / 6.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
if ((i >= 1 && i <= height -2) && j == width - 1)
{
avRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i + 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed) / 6.0);
avGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i + 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / 6.0);
avBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i + 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / 6.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
}
}
else // if it is not an index of an edge at all
{
avRed = round(((image[i - 1][j - 1].rgbtRed) + (image[i - 1][j].rgbtRed) + (image[i - 1][j + 1].rgbtRed) + (image[i][j - 1].rgbtRed) + (image[i][j].rgbtRed) + (image[i][j + 1].rgbtRed) + (image[i + 1][j - 1].rgbtRed) + (image[i + 1][j].rgbtRed) + (image[i + 1][j + 1].rgbtRed)) / 9.0);
avGreen = round(((image[i - 1][j - 1].rgbtGreen) + (image[i - 1][j].rgbtGreen) + (image[i - 1][j + 1].rgbtGreen) + (image[i][j - 1].rgbtGreen) + (image[i][j].rgbtGreen) + (image[i][j + 1].rgbtGreen) + (image[i + 1][j - 1].rgbtGreen) + (image[i + 1][j].rgbtGreen) + (image[i + 1][j + 1].rgbtGreen)) / 9.0);
avBlue = round(((image[i - 1][j - 1].rgbtBlue) + (image[i - 1][j].rgbtBlue) + (image[i - 1][j + 1].rgbtBlue) + (image[i][j - 1].rgbtBlue) + (image[i][j].rgbtBlue) + (image[i][j + 1].rgbtBlue) + (image[i + 1][j - 1].rgbtBlue) + (image[i + 1][j].rgbtBlue) + (image[i + 1][j + 1].rgbtBlue)) / 9.0);
image[i][j].rgbtRed = avRed;
image[i][j].rgbtGreen = avGreen;
image[i][j].rgbtBlue = avBlue;
}
}
}
return;
}
Now at this point, program somewhat blurs the image, but not for the right amount. When I test it via cs50's pre-set test cases, my RGB ratios are correct for corner indexes, but off for all the others. Why that might be happening?
first make shure to copy the orignal image's pixels into another varial of type RGBTRIPLE pixel by pixel using forloop if not the first blured pixel would affect the next pixel to abe blured then do you math.
this works for me!
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
copy[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
// check for an edge
if ((i == 0 && (j >= 0 && j <= width - 1))
|| ((i >= 0 && i <= height - 1) && j == 0)
|| (i == height - 1 && (j >= 0 && j <= width - 1))
|| ((i >= 0 && i <= height -1) && j == width - 1))
{
//check if it is a corner
if ((i == 0 && j == 0)
|| (i == 0 && j == width - 1)
|| (i == height - 1 && j == 0)
|| (i == height - 1 && j == width - 1))
{
if (i == 0 && j == 0)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j + 1].rgbtRed + copy[i][j + 1].rgbtRed) / 4.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j + 1].rgbtGreen + copy[i][j + 1].rgbtGreen) / 4.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j + 1].rgbtBlue + copy[i][j + 1].rgbtBlue) / 4.0);
}
if (i == 0 && j == width - 1)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i +1 ][j].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i][j - 1].rgbtRed) / 4.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i][j - 1].rgbtGreen) / 4.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i][j - 1].rgbtBlue) / 4.0);
}
if (i == height - 1 && j == 0)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i][j + 1].rgbtRed) / 4.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i][j + 1].rgbtGreen) / 4.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i][j + 1].rgbtBlue) / 4.0);
}
if (i == height - 1 && j == width - 1)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i][j - 1].rgbtRed) / 4.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i][j - 1].rgbtGreen) / 4.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i][j - 1].rgbtBlue) / 4.0);
}
}
// if not a corner index
else
{
if (i == 0 && (j >= 1 && j <= width - 2))
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i][j - 1].rgbtRed + copy[i][j + 1].rgbtRed + copy[i + 1][j].rgbtRed + copy[i + 1][j - 1].rgbtRed + copy[i + 1][j + 1].rgbtRed) / 6.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i + 1][j - 1].rgbtGreen + copy[i + 1][j + 1].rgbtGreen) / 6.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i + 1][j - 1].rgbtBlue + copy[i + 1][j + 1].rgbtBlue) / 6.0);
}
if ((i >= 1 && i <= height - 2) && j == 0)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j].rgbtRed + copy[i][j + 1].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i + 1][j + 1].rgbtRed) / 6.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i + 1][j + 1].rgbtGreen) / 6.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i + 1][j + 1].rgbtBlue) / 6.0);
}
if (i == height - 1 && (j >= 1 && j <= width - 2))
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i][j - 1].rgbtRed + copy[i][j + 1].rgbtRed + copy[i - 1][j + 1].rgbtRed + copy[i - 1][j - 1].rgbtRed) / 6.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i][j + 1].rgbtGreen + copy[i - 1][j + 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen) / 6.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i][j + 1].rgbtBlue + copy[i - 1][j + 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue) / 6.0);
}
if ((i >= 1 && i <= height -2) && j == width - 1)
{
image[i][j].rgbtRed = (int) round((copy[i][j].rgbtRed + copy[i - 1][j].rgbtRed + copy[i + 1][j].rgbtRed + copy[i][j - 1].rgbtRed + copy[i - 1][j - 1].rgbtRed + copy[i + 1][j - 1].rgbtRed) / 6.0);
image[i][j].rgbtGreen = (int) round((copy[i][j].rgbtGreen + copy[i - 1][j].rgbtGreen + copy[i + 1][j].rgbtGreen + copy[i][j - 1].rgbtGreen + copy[i - 1][j - 1].rgbtGreen + copy[i + 1][j - 1].rgbtGreen) / 6.0);
image[i][j].rgbtBlue = (int) round((copy[i][j].rgbtBlue + copy[i - 1][j].rgbtBlue + copy[i + 1][j].rgbtBlue + copy[i][j - 1].rgbtBlue + copy[i - 1][j - 1].rgbtBlue + copy[i + 1][j - 1].rgbtBlue) / 6.0);
}
}
}
// for normal pixel no edge at all
else
{
image[i][j].rgbtRed = (int) round(((copy[i - 1][j - 1].rgbtRed) + (copy[i - 1][j].rgbtRed) + (copy[i - 1][j + 1].rgbtRed) + (copy[i][j - 1].rgbtRed) + (copy[i][j].rgbtRed) + (copy[i][j + 1].rgbtRed) + (copy[i + 1][j - 1].rgbtRed) + (copy[i + 1][j].rgbtRed) + (copy[i + 1][j + 1].rgbtRed)) / 9.0);
image[i][j].rgbtGreen = (int) round(((copy[i - 1][j - 1].rgbtGreen) + (copy[i - 1][j].rgbtGreen) + (copy[i - 1][j + 1].rgbtGreen) + (copy[i][j - 1].rgbtGreen) + (copy[i][j].rgbtGreen) + (copy[i][j + 1].rgbtGreen) + (copy[i + 1][j - 1].rgbtGreen) + (copy[i + 1][j].rgbtGreen) + (copy[i + 1][j + 1].rgbtGreen)) / 9.0);
image[i][j].rgbtBlue = (int) round(((copy[i - 1][j - 1].rgbtBlue) + (copy[i - 1][j].rgbtBlue) + (copy[i - 1][j + 1].rgbtBlue) + (copy[i][j - 1].rgbtBlue) + (copy[i][j].rgbtBlue) + (copy[i][j + 1].rgbtBlue) + (copy[i + 1][j - 1].rgbtBlue) + (copy[i + 1][j].rgbtBlue) + (copy[i + 1][j + 1].rgbtBlue)) / 9.0);
}
}
}
return;
}
I am doing a cs50 assignment filter(blur). i didnt do it really well but it is working. bluring all the pixels except the first row(north) and the final row (south) in the pic. and the corners of the picture are blured but the pixels after the corners the colors are really unusual. i dont know its the conditions or the the color changing that messed this up. so i can use another pair of eyes for finding the problem thank you
[click here to see the issues in detail]
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE changed[height][width];
for(int i=0 ; i<=height-1 ;i++)
{
for(int j=0 ; j<=width-1 ; j++)
{
if(i==0) //main if north corners
{
if (j==0)
{
changed[i][j].rgbtRed =round( (image[i][j].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed) /4);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen) /4);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue) /4);
}
else if(j==width-1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed) /4);
changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen) /4);
changed[i][j].rgbtBlue = round( (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue) /4);
}
}
else if(i==height-1) //main if south corners
{
if (j==0)
{
changed[i][j].rgbtRed = round( (image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed) /4);
changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen) /4);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue) /4);
}
else if(j==width-1)
{
changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed + image[i][j-1].rgbtRed) /4);
changed[i][j].rgbtGreen = round ((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i][j-1].rgbtGreen) /4);
changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i][j-1].rgbtBlue) /4);
}
}
else if(i>0 && i<height-1 && j==0) //main if left side
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed) /6);
changed[i][j].rgbtGreen = round( (image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen) /6);
changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue) /6);
}
else if(i>0 && i<height-1 && j==width-1) //main if right side
{
changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j-1].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed) /6);
changed[i][j].rgbtGreen = round ((image[i][j].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen) /6);
changed[i][j].rgbtBlue = round ((image[i][j].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue) /6);
}
else if(i==0 && j>0 && j<width-1) //main if north side
{
changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i+1][j-1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j+1].rgbtRed + image[i][j+1].rgbtRed) /6);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i+1][j-1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i][j+1].rgbtGreen) /6);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i+1][j-1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i][j+1].rgbtBlue) /6);
}
else if(i==height-1 && j>0 && j>width-1) //main if south side
{
changed[i][j].rgbtRed = round( (image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed) /6);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen) /6);
changed[i][j].rgbtBlue = round( (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue) /6);
}
else //other pixels in the middle
{
changed[i][j].rgbtRed = round ((image[i][j].rgbtRed + image[i][j-1].rgbtRed + image[i-1][j-1].rgbtRed + image[i-1][j].rgbtRed + image[i-1][j+1].rgbtRed + image[i][j+1].rgbtRed + image[i+1][j+1].rgbtRed + image[i+1][j].rgbtRed + image[i+1][j-1].rgbtRed) /9);
changed[i][j].rgbtGreen = round ((image[i][j].rgbtGreen + image[i][j-1].rgbtGreen + image[i-1][j-1].rgbtGreen + image[i-1][j].rgbtGreen + image[i-1][j+1].rgbtGreen + image[i][j+1].rgbtGreen + image[i+1][j+1].rgbtGreen + image[i+1][j].rgbtGreen + image[i+1][j-1].rgbtGreen) /9);
changed[i][j].rgbtBlue = round( (image[i][j].rgbtBlue + image[i][j-1].rgbtBlue + image[i-1][j-1].rgbtBlue + image[i-1][j].rgbtBlue + image[i-1][j+1].rgbtBlue + image[i][j+1].rgbtBlue + image[i+1][j+1].rgbtBlue + image[i+1][j].rgbtBlue + image[i+1][j-1].rgbtBlue) /9);
}
}
}
for (int i = 0 ;i<=height-1 ; i++)
{
for (int j = 0 ; j<=width-1 ; j++)
{
image[i][j]=changed[i][j];
}
}
return;
}
bluring all the pixels except the first row(north) and the final row (south) in the pic
If you go through the case discriminations with all those ifs and elses, you'll find that the code blocks that should handle the north and south edges between the corners are not entered after the preceding if(i==0) and if(i==height-1); indeed with that many cases that was easy to miss. You could move the code blocks or you could make the whole operation more concise, e. g.:
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
{
int r = 0, g = 0, b = 0, c = 0; // surrounding pixel color sums and count
for (int y = i-1; y <= i+1; ++y) if (0 <= y && y < height)
for (int x = j-1; x <= j+1; ++x) if (0 <= x && x < width)
r += image[y][x].rgbtRed,
g += image[y][x].rgbtGreen,
b += image[y][x].rgbtBlue,
++c;
// now compute the rounded averages
changed[i][j].rgbtRed = (r+c/2)/c;
changed[i][j].rgbtGreen = (g+c/2)/c;
changed[i][j].rgbtBlue = (b+c/2)/c;
}
This is the changed code
RGBTRIPLE changed[height][width];
for (int i = 0 ; i <= height - 1 ;i++)
{
for (int j = 0 ; j <= width - 1 ; j++)
{
//main if north side
if (i == 0 && j > 0 && j < width - 1 )
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / (6.0));
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / (6.0));
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / (6.0));
}
//main if south side
else if (i==height - 1 && j > 0 && j > width - 1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / (6.0));
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / (6.0));
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / (6.0));
}
//main if left side
else if (i > 0 && i < height - 1 && j == 0)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed) / 6.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6.0);
}
//main if right side
else if (i > 0 && i < height - 1 && j == width - 1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 6.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6.0);
}
//main if north corners
if (i == 0)
{
//north left corner
if (j == 0)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed) / 4.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4.0);
}
//north left corner
else if (j == width - 1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 4.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4.0);
}
}
//main if south corners
else if (i == height - 1)
{
//south right corner
if (j == 0)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 4.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 4.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 4.0);
}
//south left corner
else if (j == width - 1)
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed) / 4.0);
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen) / 4.0);
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue) / 4.0);
}
}
else //other pixels in the middle
{
changed[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j - 1].rgbtRed) / (9.0));
changed[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j - 1].rgbtGreen) / (9.0));
changed[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j - 1].rgbtBlue) / (9.0));
}
}
}
//putting the changed color value in original image(applying the blue filter)
for (int i = 0 ; i <= height - 1 ; i++)
{
for (int j = 0 ; j <= width - 1 ; j++)
{
image[i][j] = changed[i][j];
}
}
return;
I wrote a code for the blur function, but it returns a wrong output for reason which is not known for me (it just looks fine for me), here is an example (always same):
:( blur correctly filters middle pixel
expected "127 140 149\n", not "126 140 149\n"
:) blur correctly filters pixel on edge
:) blur correctly filters pixel in corner
:( blur correctly filters 3x3 image
expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
:( blur correctly filters 4x4 image
expected "70 85 95\n80 9...", not "70 85 95\n80 9..."
and the code is here:
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE original[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
original[i][j] = image[i][j];
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0)
{
if (j == 0)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i][j + 1].rgbtRed + original[i + 1][j + 1].rgbtRed) / 4);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i][j + 1].rgbtGreen + original[i + 1][j + 1].rgbtGreen) / 4);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i][j + 1].rgbtBlue + original[i + 1][j + 1].rgbtBlue) / 4);
}
else if (j == width - 1)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i][j - 1].rgbtRed + original[i + 1][j - 1].rgbtRed) / 4);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i + 1][j - 1].rgbtGreen) / 4);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i + 1][j - 1].rgbtBlue) / 4);
}
else
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i][j - 1].rgbtRed + original[i + 1][j - 1].rgbtRed + original[i][j + 1].rgbtRed + original[i + 1][j + 1].rgbtRed) / 6);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i + 1][j - 1].rgbtGreen + original[i][j + 1].rgbtGreen + original[i + 1][j + 1].rgbtGreen) / 6);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i + 1][j - 1].rgbtBlue + original[i][j + 1].rgbtBlue + original[i + 1][j + 1].rgbtBlue) / 6);
}
}
else if (i > 0 && i < height - 1)
{
if (j == 0)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i - 1][j].rgbtRed + original[i + 1][j + 1].rgbtRed + original[i][j + 1].rgbtRed + original[i - 1][j + 1].rgbtRed) / 6);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i- 1][j].rgbtGreen + original[i + 1][j + 1].rgbtGreen + original[i][j + 1].rgbtGreen + original[i - 1][j + 1].rgbtGreen) / 6);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i + 1][j + 1].rgbtBlue + original[i][j + 1].rgbtBlue + original[i - 1][j + 1].rgbtBlue) / 6);
}
else if (j == width - 1)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i - 1][j].rgbtRed + original[i + 1][j - 1].rgbtRed + original[i][j - 1].rgbtRed + original[i - 1][j - 1].rgbtRed) / 6);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i + 1][j].rgbtGreen + original[i- 1][j].rgbtGreen + original[i + 1][j - 1].rgbtGreen + original[i][j - 1].rgbtGreen + original[i - 1][j - 1].rgbtGreen) / 6);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i + 1][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i + 1][j - 1].rgbtBlue + original[i][j - 1].rgbtBlue + original[i - 1][j - 1].rgbtBlue) / 6);
}
else
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i][j - 1].rgbtRed + original[i][j + 1].rgbtRed + original[i + 1][j - 1].rgbtRed + original[i + 1][j + 1].rgbtRed + original[i + 1][j].rgbtRed + original[i - 1][j - 1].rgbtRed + original[i - 1][j + 1].rgbtRed + original[i - 1][j].rgbtRed) / 9);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i][j + 1].rgbtGreen + original[i + 1][j - 1].rgbtGreen + original[i + 1][j + 1].rgbtGreen+ original[i + 1][j].rgbtGreen + original[i - 1][j - 1].rgbtGreen + original[i - 1][j + 1].rgbtGreen + original[i - 1][j].rgbtGreen) / 9);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i][j + 1].rgbtBlue + original[i + 1][j - 1].rgbtBlue + original[i + 1][j + 1].rgbtBlue + original[i + 1][j].rgbtBlue + original[i - 1][j - 1].rgbtBlue + original[i - 1][j + 1].rgbtBlue + original[i - 1][j].rgbtBlue) / 9);
}
}
else if (i == height - 1)
{
if (j == 0)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i - 1][j].rgbtRed + original[i][j + 1].rgbtRed + original[i - 1][j + 1].rgbtRed) / 4);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i - 1][j].rgbtGreen + original[i][j + 1].rgbtGreen + original[i - 1][j + 1].rgbtGreen) / 4);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i][j + 1].rgbtBlue + original[i - 1][j + 1].rgbtBlue) / 4);
}
else if (j == width - 1)
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i - 1][j].rgbtRed + original[i][j - 1].rgbtRed + original[i - 1][j - 1].rgbtRed) / 4);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i - 1][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i - 1][j - 1].rgbtGreen) / 4);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i - 1][j - 1].rgbtBlue) / 4);
}
else
{
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i - 1][j].rgbtRed + original[i][j - 1].rgbtRed + original[i - 1][j - 1].rgbtRed + original[i][j + 1].rgbtRed + original[i - 1][j + 1].rgbtRed) / 6);
image[i][j].rgbtGreen = round((original[i][j].rgbtGreen + original[i - 1][j].rgbtGreen + original[i][j - 1].rgbtGreen + original[i - 1][j - 1].rgbtGreen + original[i][j + 1].rgbtGreen + original[i - 1][j + 1].rgbtGreen) / 6);
image[i][j].rgbtBlue = round((original[i][j].rgbtBlue + original[i - 1][j].rgbtBlue + original[i][j - 1].rgbtBlue + original[i - 1][j - 1].rgbtBlue + original[i][j + 1].rgbtBlue + original[i - 1][j + 1].rgbtBlue) / 6);
}
}
}
}
}
if the members of the structure RGBTRIPLE has integer types, integer division happens in the lines like
image[i][j].rgbtRed = round((original[i][j].rgbtRed + original[i + 1][j].rgbtRed + original[i][j + 1].rgbtRed + original[i + 1][j + 1].rgbtRed) / 4);
and the results are truncated to integers before passed to the round() function.
The dividers should be floating-point numbers like 4.0 instead of integers like 4.
If the judge still rejects your answer after applying this change, it should be due to the calculation errors in floating-point numbers. In this case, you should avoid using floating-point number calculation and think how to calculate the desired values using only calculation in integers.
My solution is much short I guess
void blur(int height, int width, RGBTRIPLE image[height][width])
{
//Iterates through all the pixels
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
//Stores average of all the pixels around the certain color
int avgrgb[3] = {0,0,0}, n = 0;
// Matrix “a” and “b” represent the nine pixels address around the pixel of address i and j
int a[3][3] = { {i-1, i-1, i-1}, {i, i, i}, {i+1, i+1,i+1} }, b[3][3] = { {j-1, j, j+1}, {j-1, j, j+1}, {j-1, j, j+1} };
//Iterating through the pixels around the middle pixel
for(int x = 0; x < 3; x++)
{
for(int y = 0; y < 3; y++)
{
// Checking if the index contains any negative integer(negative integer represents a pixel outside the image for corner or edge pixel)
if (a[x][y] >= 0 && a[x][y] >= 0)
{
// Calculates the sum of the valid pixel colours
avgrgb[0] += image[a[x][y]][b[x][y]].rgbtRed;
avgrgb[1] += image[a[x][y]][b[x][y]].rgbtGreen;
avgrgb[2] += image[a[x][y]][b[x][y]].rgbtBlue;
n++;
}
}
}
// Calculates the actual average
avgrgb[0] /= n;
avgrgb[1] /= n;
avgrgb[2] /= n;
// Assigning new pixel values
image[i][j].rgbtRed = avgrgb[0];
image[i][j].rgbtGreen = avgrgb[1];
image[i][j].rgbtBlue = avgrgb[2];
}
}
return;
}
MikeCAT's answer is surely right. Just as an alternative to his suggestion, you could avoid the conversions to and from floating point by adding half the divisor before the integer division, e. g.
image[i][j].rgbtRed = (original[i][j].rgbtRed + … + original[i+1][j+1].rgbtRed + 2) / 4;
…
image[i][j].rgbtRed = (original[i][j].rgbtRed + … + original[i-1][j ].rgbtRed + 4) / 9;
Terminal photo HERE!I'm currently having a runtime error and have tried modifying my code but it will not pass.
If there is any specific pointers I could get (no pun intended) to help me approach this issue.
I'm also seeking some feedback on the length of my code - I wrote it as it came to my head and I'm aware that I could make this more short and coherent.
I'm currently working on filtering each independent pixel within the image grid.
Would one suggest writing code as it comes to our head or spend more time trying to decipher an algorithm much simpler.
The code would be the following:
any feedback is welcomed!
// Blur image
void
blur(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int redAvg, greenAvg, blueAvg;
if (i == 0) {
if (j == 0) {
redAvg = image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed / 4.0;
greenAvg = image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen / 4.0;
blueAvg = image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue / 4.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
if (j == width - 1) {
redAvg = image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed / 4.0;
greenAvg = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen / 4.0;
blueAvg = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue / 4.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
else {
redAvg = image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed / 6.0;
greenAvg = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i][j - +1].rgbtGreen / 6.0;
blueAvg = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue / 6.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
}
if (i > 0 && i < i - 2) {
if (j == 0) {
redAvg = image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed / 6.0;
greenAvg = image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen / 6.0;
blueAvg = image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue / 6.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
if (j == width - 1) {
redAvg = image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed / 6.0;
greenAvg = image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen / 6.0;
blueAvg = image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue / 6.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
}
else if (i == i - 1) {
if (j == 0) {
redAvg = image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed / 4.0;
greenAvg = image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen / 4.0;
blueAvg = image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue / 4.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
if (j == width - 1) {
redAvg = image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed / 4.0;
greenAvg = image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen / 4.0;
blueAvg = image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue / 4.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
else {
redAvg = image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed / 6.0;
greenAvg = image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen / 6.0;
blueAvg = image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue / 6.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
}
else {
redAvg = image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i - 1][j + 1].rgbtRed / 9.0;
greenAvg = image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i - 1][j + 1].rgbtGreen / 9.0;
blueAvg = image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i - 1][j + 1].rgbtBlue / 9.0;
image[i][j].rgbtRed = redAvg;
image[i][j].rgbtGreen = greenAvg;
image[i][j].rgbtBlue = blueAvg;
}
}
}
}
As others have mentioned, you need a separate output matrix to prevent average of averages.
Using relative offset values and some limit checks, you can simplify the function.
Also, you may need to do "saturation math" (e.g. prevent a 256 value from wrapping to 1 because it gets stored into a byte). This may not be necessary if avg / count is always <= 255, but I've included the code to show this
Here's a refactored version:
// Blur image
void
blur(int height, int width,
RGBTRIPLE image[height][width],
RGBTRIPLE imgout[height][width])
{
//RGBTRIPLE *src;
RGBTRIPLE *dst;
for (int i = 0; i < height; i++) {
dst = &imgout[i][0];
for (int j = 0; j < width; j++, dst++) {
int redAvg = 0, greenAvg = 0, blueAvg = 0;
int curcnt = 0;
for (int yoff = -1; yoff <= 1; ++yoff) {
int ycur = i + yoff;
// row index out of range
if (ycur < 0)
continue;
if (ycur >= height)
continue;
RGBTRIPLE *yptr = &image[ycur][0];
for (int xoff = -1; xoff <= 1; ++xoff) {
int xcur = j + xoff;
// index into row (i.e. column index) out of range
if (xcur < 0)
continue;
if (xcur >= width)
continue;
RGBTRIPLE *xptr = &yptr[xcur];
redAvg += xptr->rgbtRed;
greenAvg += xptr->rgbtGreen;
blueAvg += xptr->rgbtBlue;
++curcnt;
}
}
redAvg /= curcnt;
blueAvg /= curcnt;
greenAvg /= curcnt;
// prevent (e.g.) 256 from wrapping to 1 -- clip to max of byte
// NOTE: _may_ not be necessary
if (redAvg > 255)
redAvg = 255;
if (greenAvg > 255)
greenAvg = 255;
if (blueAvg > 255)
blueAvg = 255;
dst->rgbtRed = redAvg;
dst->rgbtBlue = blueAvg;
dst->rgbtGreen = greenAvg;
}
}
}
Note this could be made faster if the border pixels were handled outside the main loop
UPDATE:
Here's a version that might be slightly faster:
// Blur image
void
blur(int height, int width,
RGBTRIPLE image[height][width],
RGBTRIPLE imgout[height][width])
{
int ylim = height - 1;
int xlim = width - 1;
//RGBTRIPLE *src;
RGBTRIPLE *dst;
for (int i = 0; i < height; i++) {
dst = &imgout[i][0];
for (int j = 0; j < width; j++, dst++) {
int redAvg = 0, greenAvg = 0, blueAvg = 0;
int curcnt = 0;
int ylo = i - 1;
if (ylo < 0)
ylo = 0;
int yhi = i + 1;
if (yhi > ylim)
yhi = ylim;
for (int ycur = ylo; ycur <= yhi; ++ycur) {
RGBTRIPLE *yptr = &image[ycur][0];
int xlo = j - 1;
if (xlo < 0)
xlo = 0;
int xhi = j + 1;
if (xhi > xlim)
xhi = xlim;
for (int xcur = xlo; xcur <= xhi; ++xcur) {
RGBTRIPLE *xptr = &yptr[xcur];
redAvg += xptr->rgbtRed;
greenAvg += xptr->rgbtGreen;
blueAvg += xptr->rgbtBlue;
++curcnt;
}
}
redAvg /= curcnt;
blueAvg /= curcnt;
greenAvg /= curcnt;
// prevent (e.g.) 256 from wrapping to 1 -- clip to max of byte
// NOTE: _may_ not be necessary
if (redAvg > 255)
redAvg = 255;
if (greenAvg > 255)
greenAvg = 255;
if (blueAvg > 255)
blueAvg = 255;
dst->rgbtRed = redAvg;
dst->rgbtBlue = blueAvg;
dst->rgbtGreen = greenAvg;
}
}
}
UPDATE #2:
Here's a version that moves more of the limit checks out of the inner loops:
// Blur image
void
blur(int height, int width,
RGBTRIPLE image[height][width],
RGBTRIPLE imgout[height][width])
{
int ylim = height - 1;
int xlim = width - 1;
//RGBTRIPLE *src;
RGBTRIPLE *dst;
for (int i = 0; i < height; i++) {
dst = &imgout[i][0];
int ylo = i - 1;
if (ylo < 0)
ylo = 0;
int yhi = i + 1;
if (yhi > ylim)
yhi = ylim;
for (int j = 0; j < width; j++, dst++) {
int redAvg = 0, greenAvg = 0, blueAvg = 0;
int curcnt = 0;
int xlo = j - 1;
if (xlo < 0)
xlo = 0;
int xhi = j + 1;
if (xhi > xlim)
xhi = xlim;
for (int ycur = ylo; ycur <= yhi; ++ycur) {
RGBTRIPLE *yptr = &image[ycur][0];
for (int xcur = xlo; xcur <= xhi; ++xcur) {
RGBTRIPLE *xptr = &yptr[xcur];
redAvg += xptr->rgbtRed;
greenAvg += xptr->rgbtGreen;
blueAvg += xptr->rgbtBlue;
++curcnt;
}
}
redAvg /= curcnt;
blueAvg /= curcnt;
greenAvg /= curcnt;
// prevent (e.g.) 256 from wrapping to 1 -- clip to max of byte
// NOTE: _may_ not be necessary
if (redAvg > 255)
redAvg = 255;
if (greenAvg > 255)
greenAvg = 255;
if (blueAvg > 255)
blueAvg = 255;
dst->rgbtRed = redAvg;
dst->rgbtBlue = blueAvg;
dst->rgbtGreen = greenAvg;
}
}
}