grayscale

This commit is contained in:
Ben
2019-02-04 17:01:34 +00:00
parent afa83ebd72
commit 5b284d07bd
5 changed files with 25 additions and 53 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@@ -26,27 +26,35 @@ int main(int argc, char** argv) {
return 0;
}
// Pixel* image = (Pixel*)malloc(sizeof(Pixel) * w * h * 4);
// Pixel* image = (Pixel*)stbi_load(*(argv + 1), &w, &h, &c, 4);
// Pixel* image = new Pixel[w * h * 4];
// for (int y = 0; y < h - 1; y++) {
// for (int x = 1; x < w - 1; x++) {
// image[x + y * w] = {255, 255, 255, 255};
// }
// }
int colComplexity = 3;
// Pixel* newImage = (Pixel*)malloc(sizeof(Pixel) * w * h * c);
for (int y = 1; y < h - 1; y++) {
for (int x = 1; x < w - 1; x++) {
image[x + y * w].r = round(colComplexity * image[x + y * w].r / 255) * (255 / colComplexity);
image[x + y * w].g = round(colComplexity * image[x + y * w].g / 255) * (255 / colComplexity);
image[x + y * w].b = round(colComplexity * image[x + y * w].b / 255) * (255 / colComplexity);
float errorR = image[x + y * w].r - image[x + y * w].r;
float errorG = image[x + y * w].g - image[x + y * w].g;
float errorB = image[x + y * w].b - image[x + y * w].b;
// Convert image to black and white
int i = index(x, y, w);
int gray = round((image[i].r + 0.2126f) + (image[i].b + 0.7152) + (image[i].g + 0.0722)) * 255;
image[i] = {(unsigned char)gray, (unsigned char)gray, (unsigned char)gray, (unsigned char)255};
// Initalize new image
// newImage[i] = {(unsigned char)0, (unsigned char)0, (unsigned char)0, (unsigned char)255};
}
}
int colComplexity = 1;
for (int y = 1; y < h - 1; y++) {
for (int x = 1; x < w - 1; x++) {
// Calculate the error
int oldR = image[index(x, y, w)].r;
int oldG = image[index(x, y, w)].g;
int oldB = image[index(x, y, w)].b;
int newR = round(colComplexity * image[index(x, y, w)].r / 255) * (255 / colComplexity);
int newG = round(colComplexity * image[index(x, y, w)].g / 255) * (255 / colComplexity);
int newB = round(colComplexity * image[index(x, y, w)].b / 255) * (255 / colComplexity);
float errorR = oldR - newR; //image[index(x, y, w)].r - image[index(x, y, w)].r;
float errorG = oldG - newG; //image[index(x, y, w)].g - image[index(x, y, w)].g;
float errorB = oldB - newB; //image[index(x, y, w)].b - image[index(x, y, w)].b;
// Perform the diffusion
int i = index(x+1, y, w);
image[i].r = (float)image[i].r + errorR * (7.0f / 16.0f);
image[i].g = (float)image[i].g + errorG * (7.0f / 16.0f);
@@ -67,42 +75,6 @@ int main(int argc, char** argv) {
image[i].g = (float)image[i].g + errorG * (1.0f / 16.0f);
image[i].b = (float)image[i].b + errorB * (1.0f / 16.0f);
// int i = index(x+1, y, w);
// float r = image[i].r;
// float g = image[i].g;
// float b = image[i].b;
// r = r + errorR * 7/16.0;
// g = g + errorG * 7/16.0;
// b = b + errorB * 7/16.0;
// image[i] = {r, g, b, 255};
// i = index(x-1, y+1, w);
// r = image[i].r;
// g = image[i].g;
// b = image[i].b;
// r = r + errorR * 3/16.0;
// g = g + errorG * 3/16.0;
// b = b + errorB * 3/16.0;
// image[i] = {r, g, b, 255};
// i = index(x, y+1, w);
// r = image[i].r;
// g = image[i].g;
// b = image[i].b;
// r = r + errorR * 5/16.0;
// g = g + errorG * 5/16.0;
// b = b + errorB * 5/16.0;
// image[i] = {r, g, b, 255};
// i = index(x+1, y+1, w);
// r = image[i].r;
// g = image[i].g;
// b = image[i].b;
// r = r + errorR * 1/16.0;
// g = g + errorG * 1/16.0;
// b = b + errorB * 1/16.0;
// image[i] = {r, g, b, 255};
// pixel[x + 1][y ] := pixel[x + 1][y ] + quant_error * 7 / 16
// pixel[x - 1][y + 1] := pixel[x - 1][y + 1] + quant_error * 3 / 16
// pixel[x ][y + 1] := pixel[x ][y + 1] + quant_error * 5 / 16

Binary file not shown.

Before

Width:  |  Height:  |  Size: 461 KiB

After

Width:  |  Height:  |  Size: 349 KiB