Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RGBTRIPLE add(RGBTRIPLE x, RGBTRIPLE y)
- {
- RGBTRIPLE ans;
- ans.rgbtRed = x.rgbtRed + y.rgbtRed;
- ans.rgbtBlue = x.rgbtBlue + y.rgbtBlue;
- ans.rgbtGreen = x.rgbtGreen + y.rgbtGreen;
- return ans;
- }
- RGBTRIPLE minu(RGBTRIPLE x, RGBTRIPLE y)
- {
- RGBTRIPLE ans;
- ans.rgbtRed = x.rgbtRed - y.rgbtRed;
- ans.rgbtBlue = x.rgbtBlue - y.rgbtBlue;
- ans.rgbtGreen = x.rgbtGreen - y.rgbtGreen;
- return ans;
- }
- RGBTRIPLE divide(RGBTRIPLE x, int y)
- {
- RGBTRIPLE ans;
- ans.rgbtRed = round((double)x.rgbtRed / y);
- ans.rgbtBlue = round((double)x.rgbtBlue / y);
- ans.rgbtGreen = round((double)x.rgbtGreen / y);
- return ans;
- }
- // Blur image
- void blur(int height, int width, RGBTRIPLE image[height + 5][width + 5])
- {
- RGBTRIPLE average[height + 5][width + 5], sum[height + 5][width + 5];
- sum[0][0] = image[0][0];
- for (int i = 1; i < width; i++)
- {
- sum[0][i] = add(sum[0][i - 1], image[0][i]);
- }
- for (int i = 1; i < height; i++)
- {
- sum[i][0] = add(sum[i - 1][0], image[i][0]);
- }
- for (int i = 1; i < height; i++)
- {
- for (int j = 1; j < width; j++)
- {
- sum[i][j] = add(minu(add(sum[i - 1][j], sum[i][j - 1]), sum[i - 1][j - 1]), image[i][j]);
- }
- }
- for (int i = 0; i < width; i++)
- {
- sum[height][i] = sum[height - 1][i];
- }
- for (int i = 0; i < height; i++)
- {
- sum[i][width] = sum[i][width - 1];
- }
- sum[height][width] = sum[height - 1][width - 1];
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- if (i - 2 < 0 && j - 2 < 0)
- {
- image[i][j] = sum[i + 1][j + 1];
- }
- else if (i - 2 < 0 && j - 2 >= 0)
- {
- image[i][j] = minu(sum[i + 1][j + 1], sum[i + 1][j - 2]);
- }
- else if (i - 2 >= 0 && j - 2 < 0)
- {
- image[i][j] = minu(sum[i + 1][j + 1], sum[i - 2][j + 1]);
- }
- else
- {
- image[i][j] = add(minu(minu(sum[i + 1][j + 1], sum[i - 2][j + 1]), sum[i + 1][j - 2]), sum[i - 2][j - 2]);
- }
- if ((i == 0 || i == height - 1) && (j == 0 || j == width - 1))
- {
- image[i][j] = divide(image[i][j], 4);
- }
- else if (((i == 0 || i == height - 1) && j > 0 && j < width - 1) || ((j == 0 || j == width - 1) && i > 0 && i < height - 1))
- {
- image[i][j] = divide(image[i][j], 6);
- }
- else //if (i > 0 && i < height - 1 && j > 0 && j < width - 1)
- {
- image[i][j] = divide(image[i][j], 9);
- }
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement