Skip to content

Commit

Permalink
Final touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
InterstellarDragonDev committed May 20, 2024
1 parent 7b5bfea commit 2a368a7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 73 deletions.
50 changes: 22 additions & 28 deletions Image-Converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,6 @@

using namespace std;

// function declaration
std::vector<std::vector<png_byte>> read_png_file(const char* filename, int& width, int& height);

void get_image_data(const char* image_name) {
int width, height;
std::vector<std::vector<png_byte>> 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<std::vector<png_byte>> read_png_file(const char* filename, int& width, int& height) {
FILE* fp = fopen(filename, "rb");
Expand Down Expand Up @@ -113,7 +85,29 @@ std::vector<std::vector<png_byte>> read_png_file(const char* filename, int& widt
return pixel_data;
}

void get_image_data(const char* image_name) {
int width, height;
std::vector<std::vector<png_byte>> 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;
}
}
}



Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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


41 changes: 13 additions & 28 deletions Render-Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,40 @@
#include <string>



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<char, Color> 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<std::vector<Color>> 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++){
Expand All @@ -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
16 changes: 0 additions & 16 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include <iostream>
#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) {
Expand All @@ -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<std::vector<png_byte>> 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;
}

0 comments on commit 2a368a7

Please sign in to comment.