fixed dithering

This commit is contained in:
Ben
2019-02-17 21:49:44 +00:00
parent d2775830d5
commit 3eb7691619
5 changed files with 108 additions and 8 deletions

Binary file not shown.

View File

@@ -0,0 +1,93 @@
// fixed by CobaltXII cuz Ben is a furry
#include <iostream>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
struct Pixel {
unsigned char r, g, b, a;
};
int index(int x, int y, int w) {
return x+y*w;
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cout << "Incorrect usage, use like ./output.o <imagepath>" << std::endl;
return 0;
}
int w, h, c;
Pixel* image = (Pixel*)stbi_load(*(argv + 1), &w, &h, &c, 4);
if (image == NULL){
std::cout << "Invalid image: " << stbi_failure_reason() << std::endl;
return 0;
}
Pixel* newImage = (Pixel*)malloc(sizeof(Pixel) * w * h);
for (int y = 1; y < h - 1; y++) {
for (int x = 1; x < w - 1; x++) {
// 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;
// CXII: this is just rounding to black or white i assume
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);
image[i].b = (float)image[i].b + errorB * (7.0f / 16.0f);
i = index(x-1, y+1, w);
image[i].r = (float)image[i].r + errorR * (3.0f / 16.0f);
image[i].g = (float)image[i].g + errorG * (3.0f / 16.0f);
image[i].b = (float)image[i].b + errorB * (3.0f / 16.0f);
i = index(x, y+1, w);
image[i].r = (float)image[i].r + errorR * (5.0f / 16.0f);
image[i].g = (float)image[i].g + errorG * (5.0f / 16.0f);
image[i].b = (float)image[i].b + errorB * (5.0f / 16.0f);
i = index(x+1, y+1, w);
image[i].r = (float)image[i].r + errorR * (1.0f / 16.0f);
image[i].g = (float)image[i].g + errorG * (1.0f / 16.0f);
image[i].b = (float)image[i].b + errorB * (1.0f / 16.0f);
// CXII: now this is where u went wrong buddy
newImage[index(x, y, w)].r = 255;
newImage[index(x, y, w)].g = 0;
newImage[index(x, y, w)].b = 0;
// 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
// pixel[x + 1][y + 1] := pixel[x + 1][y + 1] + quant_error * 1 / 16
}
}
stbi_write_png("output.png", w, h, 4, (unsigned char*)newImage, 0);
}

View File

@@ -1,3 +1,4 @@
// fixed by CobaltXII cuz Ben is a furry
#include <iostream>
#define STB_IMAGE_WRITE_IMPLEMENTATION
@@ -26,7 +27,7 @@ int main(int argc, char** argv) {
return 0;
}
// Pixel* newImage = (Pixel*)malloc(sizeof(Pixel) * w * h * c);
Pixel* newImage = (Pixel*)malloc(sizeof(Pixel) * w * h);
for (int y = 1; y < h - 1; y++) {
for (int x = 1; x < w - 1; x++) {
@@ -35,7 +36,7 @@ int main(int argc, char** argv) {
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};
newImage[i] = {(unsigned char)0, (unsigned char)0, (unsigned char)0, (unsigned char)255};
}
}
@@ -46,15 +47,16 @@ int main(int argc, char** argv) {
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);
// CXII: this is just rounding to black or white i assume
int newR = image[index(x, y, w)].r < 127 ?0:255;
int newG = image[index(x, y, w)].g < 127 ? 0:255;
int newB = image[index(x, y, w)].b < 127 ? 0:255;
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
// // 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);
@@ -75,6 +77,11 @@ 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);
// CXII: now this is where u went wrong buddy
newImage[index(x, y, w)].r = newR;
newImage[index(x, y, w)].g = newG;
newImage[index(x, y, w)].b = newB;
// 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
@@ -82,5 +89,5 @@ int main(int argc, char** argv) {
}
}
stbi_write_png("output.png", w, h, 4, (unsigned char*)image, 0);
}
stbi_write_png("output.png", w, h, 4, (unsigned char*)newImage, 0);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 608 KiB

After

Width:  |  Height:  |  Size: 83 KiB