0

All of reflect works except reflected a 1 x 2 and a 4 x 4 (3x3 works fine) None of blur works except for when a point is in the corner, which makes little sense. I've sluthed around other answers and I seem to be doing the right thing, but nothing works

// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
    // establishes temporary struct
    RGBTRIPLE temp[height][width];
    
    // iterates through rows
    for (int i = 0; i < height; i++)
    {
        // iterates through columns
        for (int j = 0; j < width; j++)
        {
            // for left side
            if (j < width / 2)
            {
                // stores colors of left side in temp before reflecting
                temp[i][j].rgbtRed = image[i][j].rgbtRed;
                temp[i][j].rgbtGreen = image[i][j].rgbtGreen;
                temp[i][j].rgbtBlue = image[i][j].rgbtBlue;
                
                // changes colors on left side to that of right side 
                image[i][j].rgbtRed = image[i][width - (j + 1)].rgbtRed;
                image[i][j].rgbtGreen = image[i][width - (j + 1)].rgbtGreen;
                image[i][j].rgbtBlue = image[i][width - (j + 1)].rgbtBlue;
            }
            // for right side
            if (j > width / 2)
            {
                // changes color on right side to stored colors of left side
                image[i][j].rgbtRed = temp[i][width - (j + 1)].rgbtRed;
                image[i][j].rgbtGreen = temp[i][width - (j + 1)].rgbtGreen;
                image[i][j].rgbtBlue = temp[i][width - (j + 1)].rgbtBlue;
            }
        }
    }
    return;
}

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // making a copy matrix to modify
    RGBTRIPLE copy[height][width];
    for (int a = 0; a < height; a++)
    {
        for (int b = 0; b < width; b++)
        {
            copy[a][b] = image[a][b];
        }
    }
    
    // to have total RGB amount for later
    double red_float = 0.0;
    double green_float = 0.0;
    double blue_float = 0.0;
    // iterate through rows of image
    for (int i = 0; i < height; i++)
    {
        // iterate through columns of image
        for (int j = 0; j < width; j++)
        {
            // will be 9 if in middle, less if against side. number to divide to average
            int terms_to_blur = 0;
            // iterate through the rows that are within 1 above or below of [i]
            for (int h = -1; h < 2; h++)
            {
                // iterate through the columns (within the rows) that are within 1 left or right of [j]
                for (int w = -1; w < 2; w++)
                {
                    // only adds number to color average if within pictures (i.e not beyond the bounds of picture)
                    if ((h + i) >= 0 && (h + i) <= (height - 1) && (w + j) >= 0 && (w + j) <= (width - 1))
                    {
                        // each iteration should modify total amount of each color
                        red_float += copy[i + h][j + w].rgbtRed;
                        green_float += copy[i + h][j + w].rgbtGreen;
                        blue_float += copy[i + h][j + w].rgbtBlue;
                        terms_to_blur++;
                    }
                }
            }
            // red blur through total amount of red averaged
            red_float /= terms_to_blur;
            int red_blur = round(red_float);
            image[i][j].rgbtRed = red_blur;
            
            // green blur through total amount of green averaged
            green_float /= terms_to_blur;
            int green_blur = round(green_float);
            image[i][j].rgbtGreen = green_blur;
            
            // blue blur through total amount of blue averaged
            blue_float /= terms_to_blur;
            int blue_blur = round(blue_float);
            image[i][j].rgbtBlue = blue_blur;
        }
    }
    return;
}
1

For reflect, you have to re-think this expresion: [width - (j + 1)].

For Blur, you need to reset your variables before every i/j iteration.

| improve this answer | |
  • blur worked, but what's the issue with reflect? It's the same as (width - 1 - j) mathematically (I also tried it, and the same thing). (width - 1 - j) is definitely what it needs to be? – Matthew Jul 3 at 16:11
  • You're right. I didn't see that. You can do the complete swap in the same if block. You can assign the temp values to the pixel on the right side, and by the time it reaches the midle point the swap will be complete. – Tritum Jul 3 at 21:10
  • I got rid of the "if" block entirely and just had the "for" loop going to j < width / 2, and having image[i][width - j - 1].color = temp[i][j].color and it worked, so thanks, although I'm not really sure why it worked? – Matthew Jul 3 at 21:57

You must log in to answer this question.

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