diff --git a/Image-Converter.h b/Image-Converter.h index 9a406f7..9745bcf 100644 --- a/Image-Converter.h +++ b/Image-Converter.h @@ -10,34 +10,6 @@ using namespace std; -// function declaration -std::vector> read_png_file(const char* filename, int& width, int& height); - -void get_image_data(const char* image_name) { - int width, height; - std::vector> pixel_data = read_png_file(image_name, width, height); - - for (int y = 0; y < height; y++) { - png_bytep row = pixel_data[y].data(); - for (int x = 0; x < width; x++) { - - // this checks if we have gone past the limits of the canvas - if (x > WIDTH || y > HEIGHT) { - break; - } - - png_bytep px = &(row[x * 4]); - // px[0] = red, px[1] = green, px[2] = blue, px[3] = alpha - if ((int)px[3] == 0) { - continue; - } - Color pixel_colour = {(int)px[0], (int)px[1], (int)px[2]}; - pixel_matrix[y][x] = pixel_colour; - } - } -} - - // Function to read PNG file and get pixel data std::vector> read_png_file(const char* filename, int& width, int& height) { FILE* fp = fopen(filename, "rb"); @@ -113,7 +85,29 @@ std::vector> read_png_file(const char* filename, int& widt return pixel_data; } +void get_image_data(const char* image_name) { + int width, height; + std::vector> pixel_data = read_png_file(image_name, width, height); + + resize_pixel_matrix(height, width); + WIDTH = width; + HEIGHT = height; + + for (int y = 0; y < height; y++) { + png_bytep row = pixel_data[y].data(); + for (int x = 0; x < width; x++) { + png_bytep px = &(row[x * 4]); + //* px[0] = red, px[1] = green, px[2] = blue, px[3] = alpha + if ((int)px[3] < 5) { // this makes sure pixels that are basically transparent are left transparent + pixel_matrix[y][x] = NULL_COLOR; + continue; + } + Color pixel_colour = {(int)px[0], (int)px[1], (int)px[2]}; + pixel_matrix[y][x] = pixel_colour; + } + } +} diff --git a/README.md b/README.md index 3885b4d..a7941e6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,27 @@ # Pixel-Renderer-In-Terminal -A engine to render images to the terminal +A simple renderer that converts PNG images to unicode characters displayed in the terminal + +## Slightly important things: +1) You will need the "png.h" library to compile. Just google how to install + +## Install and Compile +``` +git clone https://github.com/InterstellarDragonDev/Pixel-Renderer-In-Terminal.git +cd Pixel-Renderer-In-Terminal/ +mkdir build +make +``` +this will compile a executable file +*you might have to give it execute permissions with `chmod +x pixel_engine`* + +## How to use +run the executable with the first parameter being the image file +e.g) `./pixel_engine test_image.png` +> [!IMPORTANT] +> Only works with png's no other file format is supported + +> [!NOTE] +> You may want to set your terminal font to something low (I tested with it set to 1). there is technically no limit to the size that can be rendered but that doesn't mean all images will render properly if they can't fit into the window without warping + + + diff --git a/Render-Engine.h b/Render-Engine.h index db7e3f0..105c6e8 100644 --- a/Render-Engine.h +++ b/Render-Engine.h @@ -12,43 +12,40 @@ #include - struct Color { int r, g, b; }; -const int WIDTH = 200; -const int HEIGHT = 200; +int WIDTH = 0; +int HEIGHT = 0; const Color NULL_COLOR = {-1, -1, -1}; -std::unordered_map colour_map = { - {'R', {255, 0, 0}}, // Bright red - {'G', {0, 255, 0}}, // Bright green - {'B', {0, 0, 255}}, // Bright blue - {'Y', {255, 255, 0}}, // Yellow - {'M', {255, 0, 255}}, // Magenta - {'C', {0, 255, 255}}, // Cyan - {'g', {100, 100, 100}}, // Grey -}; - -Color pixel_matrix[WIDTH][HEIGHT]; +std::vector> pixel_matrix; //* ----------------------------------------------------------------------------------------------------------------------------- Functions start here +// a little function that randomly generates a colour (used for testing purposes) Color random_color(){ return {(rand() % 255) + 1, (rand() % 255) + 1, (rand() % 255) + 1} ; } +// this resizes the pixel matrix when loading the image +void resize_pixel_matrix(int num_rows, int num_cols) { + pixel_matrix.resize(num_rows); + + for (auto& row : pixel_matrix) { + row.resize(num_cols); + } +} + // Function to compare two Color structs bool operator==(const Color& c1, const Color& c2) { return (c1.r == c2.r && c1.g == c2.g && c1.b == c2.b); } void initialize() { - // this sets a new seed for the random number generation - srand((unsigned) time(0)); for (int y = 0; y < HEIGHT; y++){ for (int x = 0; x < WIDTH; x++){ @@ -75,17 +72,5 @@ void render() { } } -// prints out the test to make sure pixel rendering is working -void test_pixels() { - std::cout << "rendering test: "; - for(auto i:colour_map) { - std::cout << i.first << " : "; - print_single_pixel(i.second); - std::cout << " "; - } - std::cout << "\n"; -} - - #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 7e82fa3..acca5fb 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,6 @@ -#include #include "Render-Engine.h" #include "Image-Converter.h" -//* compile command: g++ -o build/pixel_engine main.cpp Render-Engine.h Image-Converter.h -lm -lpthread -lX11 -lpng -Wall -Wextra - int main(int argc, char* argv[]) { if (argc != 2) { @@ -12,22 +9,9 @@ int main(int argc, char* argv[]) { } const char* image_name = argv[1]; - initialize(); - get_image_data(image_name); render(); - //std::vector> pixel_data = read_png_file(image_name, width, height); - - // Process the pixel data - // for (int y = 0; y < height; y++) { - // png_bytep row = pixel_data[y].data(); - // for (int x = 0; x < width; x++) { - // png_bytep px = &(row[x * 4]); - // // px[0] = red, px[1] = green, px[2] = blue, px[3] = alpha - // std::cout << "Pixel at (" << x << ", " << y << "): R=" << (int)px[0] << " G=" << (int)px[1] << " B=" << (int)px[2] << " A=" << (int)px[3] << std::endl; - // } - // } return 0; } \ No newline at end of file