0

I've tried doing this problem a few ways (without success).

This time, I wrote the following code:

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

int cap (int x);
RGBTRIPLE corner (RGBTRIPLE x, RGBTRIPLE y, RGBTRIPLE z, RGBTRIPLE a);


// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++) //gets each row of the image
    {
        for (int j = 0; j < width; j++) //gets each pixel in each row
        {
            int k = round((image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3.0); //divides by float to get float value, which it then rounds
            image[i][j].rgbtBlue = k;
            image[i][j].rgbtGreen = k;
            image[i][j].rgbtRed = k;
        }
    }
    return;
}

// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
    for (int i = 0; i < height; i++) //gets each row of the image
    {
        for (int j = 0; j < width; j++) //gets each pixel in each row
        {
            //applies formula, but saves new value in a separate variable. Rounds and caps at 255.
            int blue = cap(round(.272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue));
            int green = cap(round(.349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue));
            int red = cap(round(.393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue));

            //moves value from temp variable back to array
            image[i][j].rgbtBlue = blue;
            image[i][j].rgbtGreen = green;
            image[i][j].rgbtRed = red;
        }
    }
    return;
}

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE temp[height][width]; //new temp array to store backwards values

    for (int i = 0; i < height; i++) //gets each row of the image
    {
        for (int j = 0; j < width; j++) //gets each pixel in each row
        {
            temp[i][j] = image[i][width - (j + 1)]; //sets temp array to 'reflected' image array
        }
    }

    for (int i = 0; i < height; i++) //gets each row of the image
    {
        for (int j = 0; j < width; j++) //gets each pixel in each row
        {
            image[i][j] = temp[i][j]; //sets image values as temp array values
        }
    }
    return;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{

    RGBTRIPLE blurred[height][width];

    //middle pixels
    if (height > 2 && width > 2) //checks that there ARE middle pixels
    {
        for (int i = 1; i < (height - 1); i++) //gets each middle pixel row
        {
            for (int j = 1; j < (width - 1); j++) //gets each middle pixel column
            {
                for (int l = (i-1); l < (i+2); i++) //for loop through immediate surrounding pixels
                {
                    for (int m = (j-1); m < (j+2); m++)
                    {
                        blurred[i][j].rgbtBlue += image[l][m].rgbtBlue; //adds up all blue values
                        blurred[i][j].rgbtGreen += image[l][m].rgbtGreen; //adds up all green values
                        blurred[i][j].rgbtRed += image[l][m].rgbtRed; //adds up all red values
                    }
                }
                //after we've looped through and added all those values. we divide by 9 to get averages
                blurred[i][j].rgbtBlue = round(blurred[i][j].rgbtBlue / 9.0);
                blurred[i][j].rgbtGreen = round(blurred[i][j].rgbtGreen / 9.0);
                blurred[i][j].rgbtRed = round(blurred[i][j].rgbtRed / 9.0);
            }
        }
    }

    //edge pixels

    if (height > 2 && width > 1) //checks that there's a vertical edge
    {
        for (int i = 1; i < (height - 1); i++) //for each row
        {
            for (int l = (i-1); l < (i+2); i++) //for left edge
            {
                for (int m = 0; m < 2; m++)
                {
                    blurred[i][0].rgbtBlue += image[l][m].rgbtBlue; //adds up all blue values
                    blurred[i][0].rgbtGreen += image[l][m].rgbtGreen; //adds up all green values
                    blurred[i][0].rgbtRed += image[l][m].rgbtRed; //adds up all red values
                }
                for (int m = (width - 1); m > (width - 3); m--)
                {
                    blurred[i][width - 1].rgbtBlue += image[l][m].rgbtBlue; //adds up all blue values
                    blurred[i][width - 1].rgbtGreen += image[l][m].rgbtGreen; //adds up all green values
                    blurred[i][width - 1].rgbtRed += image[l][m].rgbtRed; //adds up all red values
                }
            }
            //after we've looped through and added all those values. we divide by 6 to get averages
            blurred[i][0].rgbtBlue = round(blurred[i][0].rgbtBlue / 6.0);
            blurred[i][0].rgbtGreen = round(blurred[i][0].rgbtGreen / 6.0);
            blurred[i][0].rgbtRed = round(blurred[i][0].rgbtRed / 6.0);
            blurred[i][width - 1].rgbtBlue = round(blurred[i][width - 1].rgbtBlue / 6.0);
            blurred[i][width - 1].rgbtGreen = round(blurred[i][width - 1].rgbtGreen / 6.0);
            blurred[i][width - 1].rgbtRed = round(blurred[i][width - 1].rgbtRed / 6.0);
        }
    }

     if (width > 2 && height > 1) //checks that there's a horizontal edge
    {
        for (int i = 1; i < (width - 1); i++) //for each row
        {
            for (int l = (i-1); l < (i+2); l++) //for left edge
            {
                for (int m = 0; m < 2; m++)
                {
                    blurred[0][i].rgbtBlue += image[m][l].rgbtBlue; //adds up all blue values
                    blurred[0][i].rgbtGreen += image[m][l].rgbtGreen; //adds up all green values
                    blurred[0][i].rgbtRed += image[m][l].rgbtRed; //adds up all red values
                }
                for (int m = (height - 1); m > (height - 3); m--)
                {
                    blurred[height - 1][i].rgbtBlue += image[m][l].rgbtBlue; //adds up all blue values
                    blurred[height - 1][i].rgbtGreen += image[m][l].rgbtGreen; //adds up all green values
                    blurred[height - 1][i].rgbtRed += image[m][l].rgbtRed; //adds up all red values
                }
            }
            //after we've looped through and added all those values. we divide by 6 to get averages
            blurred[0][i].rgbtBlue =  round(blurred[0][i].rgbtBlue / 6.0);
            blurred[0][i].rgbtGreen =  round(blurred[0][i].rgbtGreen / 6.0);
            blurred[0][i].rgbtRed =  round(blurred[0][i].rgbtRed / 6.0);
            blurred[height - 1][i].rgbtBlue =  round(blurred[height - 1][i].rgbtBlue / 6.0);
            blurred[height - 1][i].rgbtGreen =  round(blurred[height - 1][i].rgbtGreen / 6.0);
            blurred[height - 1][i].rgbtRed = round(blurred[height - 1][i].rgbtRed / 6.0);
        }
    }

    if (height == 1 && width > 2) //for a single row
    {
        for (int i = 1; i < (width - 2); i++)
        {
            for (int j = (i - 1); j < (i + 2); j++)
            {
            blurred[0][i].rgbtBlue += image[0][j].rgbtBlue;
            blurred[0][i].rgbtGreen += image[0][j].rgbtGreen;
            blurred[0][i].rgbtRed += image[0][j].rgbtRed;
            }
            blurred[0][i].rgbtBlue = round(blurred[0][i].rgbtBlue / 3.0);
            blurred[0][i].rgbtGreen = round(blurred[0][i].rgbtGreen / 3.0);
            blurred[0][i].rgbtRed = round(blurred[0][i].rgbtRed / 3.0);
        }
    }

    if (width == 1 && height > 2) //for a single column
    {
        for (int i = 1; i < (height - 2); i++)
        {
            for (int j = (i - 1); j < (i + 2); j++)
            {
            blurred[i][0].rgbtBlue += image[j][0].rgbtBlue;
            blurred[i][0].rgbtGreen += image[j][0].rgbtGreen;
            blurred[i][0].rgbtRed += image[j][0].rgbtRed;
            }
            blurred[i][0].rgbtBlue = round(blurred[i][0].rgbtBlue / 3.0);
            blurred[i][0].rgbtGreen = round(blurred[i][0].rgbtGreen / 3.0);
            blurred[i][0].rgbtRed = round(blurred[i][0].rgbtRed / 3.0);
        }
    }

    if (height == 1)
    {
        blurred[0][0].rgbtBlue = round((image[0][0].rgbtBlue + image[0][1].rgbtBlue) / 2);
        blurred[0][0].rgbtGreen = round((image[0][0].rgbtGreen + image[0][1].rgbtGreen) / 2);
        blurred[0][0].rgbtRed = round((image[0][0].rgbtRed + image[0][1].rgbtRed) / 2);
        blurred[0][width - 1].rgbtBlue = round((image[0][width - 1].rgbtBlue + image[0][width - 2].rgbtBlue) / 2);
        blurred[0][width - 1].rgbtGreen = round((image[0][width - 1].rgbtGreen + image[0][width - 2].rgbtGreen) / 2);
        blurred[0][width - 1].rgbtRed = round((image[0][width - 1].rgbtRed + image[0][width - 2].rgbtRed) / 2);
    }

    if (width ==1)
    {
        blurred[0][0].rgbtBlue = round((image[0][0].rgbtBlue + image[1][0].rgbtBlue) / 2.0);
        blurred[0][0].rgbtGreen = round((image[0][0].rgbtGreen + image[1][0].rgbtGreen) / 2.0);
        blurred[0][0].rgbtRed = round((image[0][0].rgbtRed + image[1][0].rgbtRed) / 2.0);
        blurred[height - 1][0].rgbtBlue = round((image[height - 1][0].rgbtBlue + image[height - 2][0].rgbtBlue) / 2.0);
        blurred[height - 1][0].rgbtGreen = round((image[height - 1][0].rgbtGreen + image[height - 2][0].rgbtGreen) / 2.0);
        blurred[height - 1][0].rgbtRed = round((image[height - 1][0].rgbtRed + image[height - 2][0].rgbtRed) / 2.0);
    }

    //corners
    if (height > 2 && width > 2)
    {
        blurred[0][0] = corner(image[0][0], image[0][1], image[1][0], image[1][1]);
        blurred[0][width - 1] = corner(image[0][width - 1], image[0][width - 2], image[1][width - 1], image[1][width - 2]);
        blurred[height - 1][0] = corner(image[height - 1][0], image[height - 1][1], image[height - 2][0], image[height - 2][1]);
        blurred[height - 1][width - 1] = corner(image[height - 1][width - 1], image[height - 1][width - 1], image[height - 2][width - 2], image[height - 2][width - 2]);
    }

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j] = blurred[i][j];
        }
    }

    return;

}

//caps sepia values at 255
int cap (int x) //takes an x value as input
{
    if (x > 255) //if x is higher than 255, returns 255
    {
        return 255;
    }
    else //if x isn't higher than 255, returns x
    {
        return x;
    }
}


//gets new RGB values for corner pixels
RGBTRIPLE corner (RGBTRIPLE x, RGBTRIPLE y, RGBTRIPLE z, RGBTRIPLE a)
{
    RGBTRIPLE f;
    f.rgbtBlue = round((x.rgbtBlue + y.rgbtBlue + z.rgbtBlue + a.rgbtBlue) / 4.0);
    f.rgbtGreen = round((x.rgbtGreen + y.rgbtGreen + z.rgbtGreen + a.rgbtGreen) / 4.0);
    f.rgbtRed = round((x.rgbtRed + y.rgbtRed + z.rgbtRed + a.rgbtRed) / 4.0);
    return f;
}

When I ran it, I got this message:

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1674==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7ffd99ed76ab (pc 0x00000042743e bp 0x7ffd99ed6220 sp 0x7ffd99e22cd0 T1674)
==1674==The signal is caused by a READ memory access.
    #0 0x42743d  (/home/ubuntu/pset4/filter/filter+0x42743d)
    #1 0x4232b1  (/home/ubuntu/pset4/filter/filter+0x4232b1)
    #2 0x7f016c810b96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #3 0x402dd9  (/home/ubuntu/pset4/filter/filter+0x402dd9)

UndefinedBehaviorSanitizer can not provide additional info.
==1674==ABORTING

I know what I've done is super inefficient and messy - the previous iteration was better but only worked part of the time. I'm not sure what I've done wrong this time. Any help/pointers (no pun intended) would be hugely appreciated!

You must log in to answer this question.

Browse other questions tagged .