Merge branch 'master' of https://github.com/plane000/examples
This commit is contained in:
28
C++/Floyd-Steinberg diffusion/.vscode/settings.json
vendored
Normal file
28
C++/Floyd-Steinberg diffusion/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"typeinfo": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
C++/Floyd-Steinberg diffusion/a.out
Normal file
BIN
C++/Floyd-Steinberg diffusion/a.out
Normal file
Binary file not shown.
93
C++/Floyd-Steinberg diffusion/fixed_floyd_steinberg.cpp
Normal file
93
C++/Floyd-Steinberg diffusion/fixed_floyd_steinberg.cpp
Normal 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);
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// fixed by CobaltXII cuz Ben is a furry
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
@@ -13,6 +14,12 @@ int index(int x, int y, int w) {
|
|||||||
return x+y*w;
|
return x+y*w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int floorColour(float col) {
|
||||||
|
//yeah do NOT diss me FUCK OFF
|
||||||
|
#include <cmath>
|
||||||
|
return std::max(0.0f,std::min(255.0f,col));
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
std::cout << "Incorrect usage, use like ./output.o <imagepath>" << std::endl;
|
std::cout << "Incorrect usage, use like ./output.o <imagepath>" << std::endl;
|
||||||
@@ -26,7 +33,7 @@ int main(int argc, char** argv) {
|
|||||||
return 0;
|
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 y = 1; y < h - 1; y++) {
|
||||||
for (int x = 1; x < w - 1; x++) {
|
for (int x = 1; x < w - 1; x++) {
|
||||||
@@ -35,7 +42,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;
|
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};
|
image[i] = {(unsigned char)gray, (unsigned char)gray, (unsigned char)gray, (unsigned char)255};
|
||||||
// Initalize new image
|
// 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,9 +53,10 @@ int main(int argc, char** argv) {
|
|||||||
int oldR = image[index(x, y, w)].r;
|
int oldR = image[index(x, y, w)].r;
|
||||||
int oldG = image[index(x, y, w)].g;
|
int oldG = image[index(x, y, w)].g;
|
||||||
int oldB = image[index(x, y, w)].b;
|
int oldB = image[index(x, y, w)].b;
|
||||||
int newR = round(colComplexity * image[index(x, y, w)].r / 255) * (255 / colComplexity);
|
// CXII: this is just rounding to black or white i assume
|
||||||
int newG = round(colComplexity * image[index(x, y, w)].g / 255) * (255 / colComplexity);
|
int newR = image[index(x, y, w)].r < 127?0:255;
|
||||||
int newB = round(colComplexity * image[index(x, y, w)].b / 255) * (255 / colComplexity);
|
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 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 errorG = oldG - newG; //image[index(x, y, w)].g - image[index(x, y, w)].g;
|
||||||
@@ -56,24 +64,29 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
// Perform the diffusion
|
// Perform the diffusion
|
||||||
int i = index(x+1, y, w);
|
int i = index(x+1, y, w);
|
||||||
image[i].r = (float)image[i].r + errorR * (7.0f / 16.0f);
|
image[i].r = floorColour((float)image[i].r + errorR * (7.0f / 16.0f));
|
||||||
image[i].g = (float)image[i].g + errorG * (7.0f / 16.0f);
|
image[i].g = floorColour((float)image[i].g + errorG * (7.0f / 16.0f));
|
||||||
image[i].b = (float)image[i].b + errorB * (7.0f / 16.0f);
|
image[i].b = floorColour((float)image[i].b + errorB * (7.0f / 16.0f));
|
||||||
|
|
||||||
i = index(x-1, y+1, w);
|
i = index(x-1, y+1, w);
|
||||||
image[i].r = (float)image[i].r + errorR * (3.0f / 16.0f);
|
image[i].r = floorColour((float)image[i].r + errorR * (3.0f / 16.0f));
|
||||||
image[i].g = (float)image[i].g + errorG * (3.0f / 16.0f);
|
image[i].g = floorColour((float)image[i].g + errorG * (3.0f / 16.0f));
|
||||||
image[i].b = (float)image[i].b + errorB * (3.0f / 16.0f);
|
image[i].b = floorColour((float)image[i].b + errorB * (3.0f / 16.0f));
|
||||||
|
|
||||||
i = index(x, y+1, w);
|
i = index(x, y+1, w);
|
||||||
image[i].r = (float)image[i].r + errorR * (5.0f / 16.0f);
|
image[i].r = floorColour((float)image[i].r + errorR * (5.0f / 16.0f));
|
||||||
image[i].g = (float)image[i].g + errorG * (5.0f / 16.0f);
|
image[i].g = floorColour((float)image[i].g + errorG * (5.0f / 16.0f));
|
||||||
image[i].b = (float)image[i].b + errorB * (5.0f / 16.0f);
|
image[i].b = floorColour((float)image[i].b + errorB * (5.0f / 16.0f));
|
||||||
|
|
||||||
i = index(x+1, y+1, w);
|
i = index(x+1, y+1, w);
|
||||||
image[i].r = (float)image[i].r + errorR * (1.0f / 16.0f);
|
image[i].r = floorColour((float)image[i].r + errorR * (1.0f / 16.0f));
|
||||||
image[i].g = (float)image[i].g + errorG * (1.0f / 16.0f);
|
image[i].g = floorColour((float)image[i].g + errorG * (1.0f / 16.0f));
|
||||||
image[i].b = (float)image[i].b + errorB * (1.0f / 16.0f);
|
image[i].b = floorColour((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 ] := pixel[x + 1][y ] + quant_error * 7 / 16
|
||||||
// pixel[x - 1][y + 1] := pixel[x - 1][y + 1] + quant_error * 3 / 16
|
// pixel[x - 1][y + 1] := pixel[x - 1][y + 1] + quant_error * 3 / 16
|
||||||
@@ -82,5 +95,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.
Binary file not shown.
|
Before Width: | Height: | Size: 608 KiB After Width: | Height: | Size: 148 KiB |
Reference in New Issue
Block a user