0

instead of all if-else statements, there is another way for all conditions to minimize my code.

void blur(int height, int width, RGBTRIPLE image[height][width])
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                int r = 0, b = 0, g = 0;
                float spr = 0, spg = 0, spb = 0;
                if (j == 0 && i == 0)
                {
                    for (int k = i; k <= i + 1; k++ )
                    {
                        for (int p = j; p <= j + 1; p++)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/4);
                    spg = (spg/4);
                    spb = (spb/4);
                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
                else if (j == width - 1 && i == 0)
                {
                    for (int k = i; k <= i + 1; k++ )
                    {
                        for (int p = j; p >= j - 1; p--)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/4);
                    spg = (spg/4);
                    spb = (spb/4);
                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
                else if (i == height - 1 && j == 0)
                {
                    for (int k = i; k >= i - 1; k--)
                    {
                        for (int p = j; p <= j + 1; p++)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/4);
                    spg = (spg/4);
                    spb = (spb/4);

                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
                else if (i == height - 1 && j == width -1)
                {
                    for (int k = i; k >= i - 1; k--)
                    {
                        for (int p = j; p >= j - 1; p--)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/4);
                    spg = (spg/4);
                    spb = (spb/4);

                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
                else if (j == 0)
                {
                   for (int k = i - 1; k <= i + 1; k++ )
                    {
                        for (int p = j; p <= j + 1; p++)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/6);
                    spg = (spg/6);
                    spb = (spb/6);

                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
                else if (j == width - 1)
                {
                    for (int k = i - 1; k <= i + 1; k++ )
                    {
                        for (int p = j; p >= j - 1; p--)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/6);
                    spg = (spg/6);
                    spb = (spb/6);

                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
                else if (i == 0)
                {
                    for (int k = i; k <= i + 1; k++ )
                    {
                        for (int p = j - 1; p <= j + 1; p++)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/6);
                    spg = (spg/6);
                    spb = (spb/6);

                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
                else if (i == height - 1)
                {
                    for (int k = i; k >= i - 1; k-- )
                    {
                        for (int p = j - 1; p <= j + 1; p++)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/6);
                    spg = (spg/6);
                    spb = (spb/6);

                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
                else
                {
                    for (int k = i - 1; k <= i + 1; k++ )
                    {
                        for (int p = j - 1; p <= j + 1; p++)
                        {
                            r = image[k][p].rgbtRed;
                            b = image[k][p].rgbtBlue;
                            g = image[k][p].rgbtGreen;
                            spr = spr + r;
                            spb = spb + b;
                            spg = spg + g;
                        }
                    }
                    spr = (spr/9);
                    spg = (spg/9);
                    spb = (spb/9);

                    spr = round(spr);
                    spg = round(spg);
                    spb = round(spb);
                    image[i][j].rgbtRed = spr;
                    image[i][j].rgbtBlue = spb;
                    image[i][j].rgbtGreen = spg;
                }
            }
        }
        return;
    }
0

You could determine the minimum and maximum value before your loop. Or always run from k = i - 1 to i + 1, and then check whether k >= 0 && k < height.

Either way, in the innermost loop, count the occurrences so you can divide by the correct number.

You must log in to answer this question.

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