1

I'm testing my Gy function separately, and it looks like the values I put in the test array are not correctly showing up in the function. I printf() the main function and they are recording correctly, but when I 'printf()' within the Gy function they are all wrong after the first row (See "image [][] is being worked on (which equals x)" in the image of print statements vs. the array I manually inputted in the main function of the test file). Any ideas as to how the rgbtRed values can be getting so messed up? Also please excuse messy code - I'm a beginner. Thanks!

int main(void)
{
    int Red = 1;
    int Blue = 2;
    int Green = 3;

    RGBTRIPLE image[3][3];
        image[0][0].rgbtRed = 0;
        image[0][1].rgbtRed = 0;
        image[0][2].rgbtRed = 0;
        image[1][0].rgbtRed = 128;
        image[1][1].rgbtRed = 128;
        image[1][2].rgbtRed = 128;
        image[2][0].rgbtRed = 255;
        image[2][1].rgbtRed = 255;
        image[2][2].rgbtRed = 255;
        
        //printf("image[1][0] = %i\n", image[1][0].rgbtRed); 
        //printf("image[1][1] = %i\n", image[1][1].rgbtRed); 
        //printf("image[1][2] = %i\n", image[1][2].rgbtRed); [![enter image description here][1]][1]

    int finalValue = sobelGy(1, 1, Red, 3, 3, image);
    printf("finalValue = %i\n", finalValue);
}


int sobelGy(int i, int j, int color, int height, int width, RGBTRIPLE image[i][j])
{
    int total = 0;
    int i_row_counter = 0;
    int j_row_counter = 0;
    int multiplier;


    //for each pixel of the 3x3 grid
    for (int l = (i - 1); l <= (i + 1); l++)
        {
            i_row_counter++;
            //printf("i row counter = %i\n", i_row_counter);
            j_row_counter = 0;
            //printf("j row counter = %i\n", j_row_counter);

            for (int m = (j - 1); m <= (j + 1); m++)
                {

                    printf("image[%i][%i] is being worked on (which equals %i).\n", l, m, image[l][m].rgbtRed);
                    printf("l = %i\n", l);
                    printf("m = %i\n", m);


                    if (i_row_counter != 2)
                    {
                        //printf("i row counter does not equal 2.\n");

                        //set multiplier
                        if (j_row_counter != 1)
                            multiplier = 1;

                        else
                            multiplier = 2;

                        if (i_row_counter == 1)
                            multiplier = multiplier * -1;

                            printf("multiplier = %i\n", multiplier);

                        //add value to pixel Gy count
                        if (l < height && l >= 0 && m < width && m >= 0)
                        {
                            //printf("pixel is within bounds.\n");

                            if (color == 1)
                                {
                                    total += image[l][m].rgbtRed * multiplier;
                                    printf("************Red total (%i) += image[%i][%i].red (%i) * multiplier (%i)\n", total, l, m, image[l][m].rgbtRed, multiplier);
                                }

                            else if (color == 2)
                                {
                                    total += image[l][m].rgbtBlue * multiplier;
                                    printf("Blue total (%i) = image[%i][%i].blue (%i) * multiplier (%i)\n", total, l, m, image[l][m].rgbtBlue, multiplier);
                                }

                            else if (color == 3)
                                {
                                    total += image[l][m].rgbtGreen * multiplier;
                                    printf("Green total (%i) = image[%i][%i].green (%i) * multiplier (%i)\n", total, l, m, image[l][m].rgbtGreen, multiplier);
                                }
                        }
                    }
                    j_row_counter++;
                    //printf("j row counter = %i\n\n", j_row_counter);
                }

        }
    return total;
}

1 Answer 1

0

I love how detailed your printf statements are!

Your program seems to work as intended if you change the last parameter in sobelGy's signature from RGBTRIPLE image[i][j] to RGBTRIPLE image[height][width]. The rest of your logic seems sound.

It's a very easy mistake to make. You're doing well!

If this answers your question, please click on the check mark to accept. Don't be shy to ask another question the next time you have an issue!

You must log in to answer this question.

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