1. ## Box blur implementation

Having trouble with this - I know I've written way too much code but I can't figure out what different to do...

RGB is a
Code:
`struct`
containing the red green and blue

Code:
```void blur(int height, int width, RGB image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
//0,0
float red1 = image[i][j].rgbtRed;
float green1 = image[i][j].rgbtGreen;
float blue1 = image[i][j].rgbtBlue;

//0,1
float red2 = image[i][j + 1].rgbtRed;
float green2 = image[i][j + 1].rgbtGreen;
float blue2 = image[i][j + 1].rgbtBlue;

//0,2
float red3 = image[i][j + 2].rgbtRed;
float green3 = image[i][j + 2].rgbtGreen;
float blue3 = image[i][j + 2].rgbtBlue;

//1,0
float red4 = image[i + 1][j].rgbtRed;
float green4 = image[i + 1][j].rgbtGreen;
float blue4 = image[i + 1][j].rgbtBlue;

//1,1
float red5 = image[i + 1][j + 1].rgbtRed;
float green5 = image[i + 1][j + 1].rgbtGreen;
float blue5 = image[i + 1][j + 1].rgbtBlue;

//1,2
float red6 = image[i + 1][j + 2].rgbtRed;
float green6 = image[i + 1][j + 2].rgbtGreen;
float blue6 = image[i + 1][j + 2].rgbtBlue;

//2,0
float red7 = image[i + 2][j].rgbtRed;
float green7 = image[i + 2][j].rgbtGreen;
float blue7 = image[i + 2][j].rgbtBlue;

//2,1
float red8 = image[i + 2][j + 1].rgbtRed;
float green8 = image[i + 2][j + 1].rgbtGreen;
float blue8 = image[i + 2][j + 1].rgbtBlue;

//2,2
float red9 = image[i + 2][j + 2].rgbtRed;
float green9 = image[i + 2][j + 2].rgbtGreen;
float blue9 = image[i + 2][j + 2].rgbtBlue;

float averageRed = red1 + red2 + red3 + red4 + red5 + red6 + red7 + red8 + red9;
float averageGreen = green1 + green2 + green3 + green4 + green5 + green6 + green7 + green8 + green9;
float averageBlue = blue1 + blue2 + blue3 + blue4 + blue5 + blue6 + blue7 + blue8 + blue9;

averageRed = averageRed / 9;
averageGreen = averageGreen / 9;
averageBlue = averageBlue / 9;

image[i][j].rgbtRed = roundf(averageRed);
image[i][j].rgbtGreen = roundf(averageGreen);
image[i][j].rgbtBlue = roundf(averageBlue);```

2. > float red2 = image[i][j + 1].rgbtRed;
All these +1 and +2 are going to overflow your array bounds at the limits of your loops.

Code:
```float red = 0;
float green = 0;
float blue = 0;
for ( int x = 0 ; x < 2 ; x++ ) {
for ( int y = 0 ; y < 2 ; y++ ) {
red += image[i+x][j+y].rgbtRed;
}
}```
Saves you copy/pasting 40 lines of code.

3. Originally Posted by Salem
> float red2 = image[i][j + 1].rgbtRed;
All these +1 and +2 are going to overflow your array bounds at the limits of your loops.

Code:
```float red = 0;
float green = 0;
float blue = 0;
for ( int x = 0 ; x < 2 ; x++ ) {
for ( int y = 0 ; y < 2 ; y++ ) {
red += image[i+x][j+y].rgbtRed;
}
}```
Saves you copy/pasting 40 lines of code.
Yes you're correct that's exactly what happened when I ran the program.

4. Originally Posted by Salem
> float red2 = image[i][j + 1].rgbtRed;
All these +1 and +2 are going to overflow your array bounds at the limits of your loops.

Code:
```float red = 0;
float green = 0;
float blue = 0;
for ( int x = 0 ; x < 2 ; x++ ) {
for ( int y = 0 ; y < 2 ; y++ ) {
red += image[i+x][j+y].rgbtRed;
}
}```
Saves you copy/pasting 40 lines of code.
This is probably what you want for a box filter. a 3x3 box
centred on the target pixel.
You can be a bit more sophisticated in handling the edges.

You can't write directly to your source as you corrupt the image
for the next pixel.

Code:
```float red = 0;
float green = 0;
float blue = 0;
int N = 0;
for ( int x = -1 ; x < 2 ; x++ ) {
for ( int y = -1 ; y < 2 ; y++ ) {
if (i + x >= 0 && i + x < width && j + y >= 0 && j + y < height){
red += image[i+x][j+y].rgbtRed;
N++;
}
}
}
outputimage[j][i].rgbtRed = red / N;```

5. Originally Posted by Malcolm McLean
This is probably what you want for a box filter. a 3x3 box
centred on the target pixel.
You can be a bit more sophisticated in handling the edges.

You can't write directly to your source as you corrupt the image
for the next pixel.

Code:
```float red = 0;
float green = 0;
float blue = 0;
int N = 0;
for ( int x = -1 ; x < 2 ; x++ ) {
for ( int y = -1 ; y < 2 ; y++ ) {
if (i + x >= 0 && i + x < width && j + y >= 0 && j + y < height){
red += image[i+x][j+y].rgbtRed;
N++;
}
}
}
outputimage[j][i].rgbtRed = red / N;```
Thanks. I'm not going to lie and pretend that I understand how/where to implement the changes you've offered! What are the underlying concepts behind this box blur implementation in C?
Specifically, what areas of C should I learn more about to deal with this? The problem came alongside teaching about memory, so I assume it's that? I try not to look at other people's code but in the little snippets I have seen, I've seen people using malloc.

At the moment, I'm having fundamental problems understanding what I can and cannot do with the syntax e.g. how do I access the surrounding pixels in order to calculate their averages - hence the +1/-1 type approach I took.

The code I wrote was my naive way of implementing the following:

1) Assign each color within each pixel to a variable (float red1, green1 etc)

2) Work out the averages

3) Assign the averages to the current pixel (i.e. image[i][j])