Created
May 11, 2020 11:22
-
-
Save wx5162839/a2fadd60d6d7aa14b1ffbafd4ed6d647 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // when use round and do dividing, attention: int/int return int, | |
| //as for int/int, | |
| // float x = 55; // stored as 54.999999... | |
| // x = x + 0.5 (- 0.5) (x<0); // x is now 55.499999... | |
| // int y = (int)x; // truncated to 55 | |
| //float a; | |
| //int b = (int)(a + 0.5); | |
| //+0.5是四舍五入的关键 | |
| #include "helpers.h" | |
| #include <math.h> | |
| // Convert image to grayscale | |
| void grayscale(int height, int width, RGBTRIPLE image[height][width]) | |
| { | |
| for (int i = 0; i < height; i++) | |
| { | |
| for (int j = 0; j < width; j++) | |
| { | |
| // dividing an integer by an integer. This is integer division, and will produce an integer again, ignoring the fractional part. | |
| int blue = image[i][j].rgbtBlue; | |
| int green = image[i][j].rgbtGreen; | |
| int red = image[i][j].rgbtRed; | |
| int ave = round((float)(blue + green + red) / (float)(3)); | |
| image[i][j].rgbtBlue = ave; | |
| image[i][j].rgbtGreen = ave; | |
| image[i][j].rgbtRed = ave; | |
| } | |
| } | |
| return; | |
| } | |
| // Convert image to sepia | |
| int limit(int n) | |
| { | |
| if (n > 255) | |
| { | |
| return n = 255; | |
| } | |
| else | |
| { | |
| return n; | |
| } | |
| } | |
| void sepia(int height, int width, RGBTRIPLE image[height][width]) | |
| { | |
| int sepiaRed; | |
| int sepiaGreen; | |
| int sepiaBlue; | |
| for (int i = 0; i < height; i++) | |
| { | |
| for (int j = 0; j < width; j++) | |
| { | |
| sepiaRed = round(.393 * image[i][j].rgbtRed + .769 * image[i][j].rgbtGreen + .189 * image[i][j].rgbtBlue); | |
| sepiaGreen = round(.349 * image[i][j].rgbtRed + .686 * image[i][j].rgbtGreen + .168 * image[i][j].rgbtBlue); | |
| sepiaBlue = round(.272 * image[i][j].rgbtRed + .534 * image[i][j].rgbtGreen + .131 * image[i][j].rgbtBlue); | |
| image[i][j].rgbtBlue = limit(sepiaBlue); | |
| image[i][j].rgbtGreen = limit(sepiaGreen); | |
| image[i][j].rgbtRed = limit(sepiaRed); | |
| } | |
| } | |
| return; | |
| } | |
| // Reflect image horizontally | |
| void reflect(int height, int width, RGBTRIPLE image[height][width]) | |
| { | |
| for (int i = 0; i < height; i++) | |
| { | |
| int tmp[3]; | |
| // don't need to use pointer here, because you do not copy a value or someting, you can assign it directly. | |
| for (int j = 0; j < width / 2; j++) | |
| { | |
| tmp[0] = image[i][j].rgbtRed; | |
| tmp[1] = image[i][j].rgbtGreen; | |
| tmp[2] = image[i][j].rgbtBlue; | |
| image[i][j].rgbtRed = image[i][width - 1 - j].rgbtRed; | |
| image[i][j].rgbtGreen = image[i][width- 1 - j].rgbtGreen; | |
| image[i][j].rgbtBlue = image[i][width - 1 - j].rgbtBlue; | |
| image[i][width - 1 - j].rgbtRed = tmp[0]; | |
| image[i][width - 1 - j].rgbtGreen = tmp[1]; | |
| image[i][width - 1 - j].rgbtBlue = tmp[2]; | |
| } | |
| } | |
| return; | |
| } | |
| // Blur image | |
| void blur(int height, int width, RGBTRIPLE image[height][width]) | |
| { | |
| RGBTRIPLE tmp[height][width]; | |
| for (int i = 0; i < height; i++) | |
| { | |
| for (int j = 0; j < width; j++) | |
| { | |
| int red = 0, green = 0, blue = 0, counter = 0; | |
| for (int k = -1; k < 2; k++) | |
| { | |
| if (k + j < 0 || k + j > width - 1) | |
| { | |
| continue; | |
| } | |
| for (int h = -1; h < 2; h++) | |
| { | |
| if (h + i < 0 || h + i > height - 1) | |
| { | |
| // the way of using continue statement: https://beginnersbook.com/2014/01/c-continue-statement/ | |
| continue; | |
| } | |
| red += image[i + h][j + k].rgbtRed; | |
| green += image[i + h][j + k].rgbtGreen; | |
| blue += image[i + h][j + k].rgbtBlue; | |
| counter++; | |
| } | |
| } | |
| tmp[i][j].rgbtBlue = round((float)blue / (float)counter); | |
| tmp[i][j].rgbtGreen = round((float)green / (float)counter); | |
| tmp[i][j].rgbtRed = round((float)red / (float)counter); | |
| } | |
| } | |
| for (int i = 0; i < height; i++) | |
| { | |
| for (int j = 0; j < width; j++) | |
| { | |
| image[i][j].rgbtBlue = tmp[i][j].rgbtBlue; | |
| image[i][j].rgbtGreen = tmp[i][j].rgbtGreen; | |
| image[i][j].rgbtRed = tmp[i][j].rgbtRed; | |
| } | |
| } | |
| return; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment