0

So after being stuck on this pset for overlooking tiny typos I have finally gotten it to compile but I received more errors then I would like to have. If anyone could tell me where I can find the issues so I can figure out a way to fix them that would be great. The only function that works properly is sepia. Here is my code:

#include "helpers.h"
#include <math.h>

// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    float gray;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float red = image[i][j].rgbtRed;
            float green = image[i][j].rgbtGreen;
            float blue = image[i][j].rgbtBlue;
            gray = round((red + green + blue) /3);

            image[i][j].rgbtRed = gray;
            image[i][j].rgbtGreen = gray;
            image[i][j].rgbtBlue = gray;
        }

    }
    return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j ++)
        {
            float red = image[i][j].rgbtRed;
            float green = image[i][j].rgbtGreen;
            float blue = image[i][j].rgbtBlue;
            float sepiaRed = round(.393 * red + .769 * green + .189 * blue);
            if (sepiaRed > 255)
            {
                sepiaRed = 255;
            }
            float sepiaGreen = round(.349 * red + .686 * green + .168 * blue);
            if (sepiaGreen > 255)
            {
                sepiaGreen = 255;
            }
            float sepiaBlue = round(.272 * red + .534 * green + .131 * blue);
            if (sepiaBlue > 255)
            {
                sepiaBlue = 255;
            }
            image[i][j].rgbtRed = sepiaRed;
            image[i][j].rgbtGreen = sepiaGreen;
            image[i][j].rgbtBlue =sepiaBlue;
        }
    }
    return;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    void swap(int *a, int *b);
    int temp[6];
    int s = 1;
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width / 2; j++)
        {
            temp[0] = image[i][j].rgbtRed;
            temp[1] = image[i][j].rgbtGreen;
            temp[2] = image[i][j].rgbtBlue;
            temp[3] = image[i][width - s].rgbtRed;
            temp[4] = image[i][width - s].rgbtGreen;
            temp[5] = image[i][width - s].rgbtBlue;
            swap(&temp[0], &temp[3]);
            swap(&temp[1], &temp[4]);
            swap(&temp[2], &temp[5]);
            s++;
        }

    }
    return;
}
void swap(int *a, int *b)
{
    int tempB = *a;
    *a = *b;
    *b = tempB;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    //initiazlizes a temporary variable to store original colors
    RGBTRIPLE temp[height][width];
    //iterates through the pixels
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            float counter = 0.00;
            int red = 0;
            int green = 0;
            int blue = 0;
            //iterates through the pixels surrounding the pixel your on in the i/j loop
            for (int k = -1; k < 2; k++)
            {
                for (int l = -1; l < 2; l++)
                {
                    //checks to see if the pixels around the current pixel exist and ups the counter for each one
                    if ((i + k >= 0) && (j + l >= 0) && (i + k <= height - 1) && (j + l <= width -1))
                    {
                        red += image[i + k][j + l].rgbtRed;
                        green += image[i + k][j + l].rgbtGreen;
                        blue += image[i + k][j + l].rgbtBlue;
                       counter++;
                    }
                }
            }
            temp[i][j].rgbtRed = round((red) / counter);
            temp[i][j].rgbtBlue = round((blue) / counter);
            temp[i][j].rgbtGreen = round((green) / counter);
        }
    }
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = temp[i][j];
        }
    }
    return;
}

The errors I am receiving are:

:( reflect correctly filters 1x2 image expected "0 0 255\n255 0...", not "255 0 0\n0 0 2..."

:( reflect correctly filters 1x3 image expected "0 0 255\n0 255...", not "255 0 0\n0 255..."

:( reflect correctly filters 3x3 image expected "70 80 90\n40 5...", not "10 20 30\n40 5..."

:( reflect correctly filters 4x4 image expected "100 110 120\n7...", not "10 20 30\n40 5..."

1

For grayscale you should take a look to operator precedence in C (google). The result 'a + b / c' is not the same than '(a + b) / c'

For reflect a question: How many swaps do you need to do to flip the image horizontaly? Think about it.

For blur you've found a way to go over the 3x3 pixels around the current pixel with your loops 'k' and 'l', and then reject the ones that out of boundaries with your if conditions, but once you've gotten the correct ones you're not adding them to average later. Instead you're adding all 9 of them outside those loops taking values from pixels out of boundaries. Also in C the arrays start with 0, so if your array length is 'l', the expresion array[l] is out of the limit of the array. Revise your 'if' conditions.

Edit to go over it with more detail:

Let's go over reflect step by step: Let's call a swap the act of getting one pixel from the right side of the image, store it in a temp variable, get one pixel from the left side and put it in the right side, and lastly put the one in the temp variable in the left side.

In the very first interation, being the width index 'j', we swap the first pixel in the left, that would be j = 0, with the one in the rigth, width - j(0) - 1 ( or in your code 'width - s'). The next iteration, j would be 1, and the one in the rigth side would be width -2. The next, j = 2, and the right side, width -3. And so on.... So now a question: What will happen if you pass the middle point?

In blur, when you are incrementing your 'counter' variable, you have the oportunity to add the value of the current pixel to a variable, and then, when the 3x3 loops are finished, you can average an round them. By doing that, if for example the current pixel is in the corner, your 'counter' variable would be 4, and you'll have only the 'good' 4 pixels to average, and not all of 9 pixels, as your code is doing now.

11
  • Isn't the counter incrementing for every correct pixel to use for the average in the blur function? I originally added the counter so that I can divide the sum by the amount of pixels used rather than dividing by 9 for each case. Also for the reflect function I was thinking you need to know about the amount of pixels in the picture to know how many swaps you would need to do but from other solutions it seems to only take three swaps from what I could see. I attempted to use debug50 when I couldn't figure your hints for blur and reflect, but it said it could not connect. Sorry I can't think rn May 27 '20 at 3:56
  • To debug your code: Click on the left of the line number you want your code to stop, to light a red dot. And in the terminal: 'debug50 ./filter -r images/tower.bmp images/reversetower.bmp
    – Tritum
    May 27 '20 at 12:18
  • Ok so basically when it hits the middle pixel in the reflect function according to my code the middle would "swap itself" then all the previous ones that had already been swapped would be swapped back which means the image is reflected then goes back to normal correct? Could I fix this with an if statement that stops the iteration once it hits the middle? For blur I used the floats i created for the colors to store the values of the pixels used and debugged and I think now my error is the values being stored aren't correct. I will post my updated code now. Sorry if i'm getting annoying. Jun 8 '20 at 2:35
  • Update: I have figured out the issue with blur. I changed the color assignments from being the pixels color values to 0 and also set them as ints rather than floats (unsure if that had any actual effect) then I had to change the color assignments under my if statement to an assignment by sum and now I get no error messages. Currently working on the reflect issue to see if I can solve it with an if statement. Again I appreciate all your help! Jun 9 '20 at 1:39
  • Yes you can stop a 'for' loop using an 'if' condition, followed by a break statement. You can also use the middle argument of he 'for' statement, ie. for(int i = 0; condition that is true until it hits the middle pixel; i++)
    – Tritum
    Jun 9 '20 at 2:54
0

So my swap function was switching the values of my variables but the variables themselves did not get switched (which I thought the swap function was doing) so I had to write out the code to swap the variables (from what I was told the swap function is basically useless in my code I guess) then make sure to initialize my s after because I originally added the variable swaps after the s and s was being incremented before the swap leading to the wrong pixels being swapped

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .