Skip to content

Commit 2a368a7

Browse files
Final touchups
1 parent 7b5bfea commit 2a368a7

File tree

4 files changed

+61
-73
lines changed

4 files changed

+61
-73
lines changed

Image-Converter.h

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,6 @@
1010

1111
using namespace std;
1212

13-
// function declaration
14-
std::vector<std::vector<png_byte>> read_png_file(const char* filename, int& width, int& height);
15-
16-
void get_image_data(const char* image_name) {
17-
int width, height;
18-
std::vector<std::vector<png_byte>> pixel_data = read_png_file(image_name, width, height);
19-
20-
for (int y = 0; y < height; y++) {
21-
png_bytep row = pixel_data[y].data();
22-
for (int x = 0; x < width; x++) {
23-
24-
// this checks if we have gone past the limits of the canvas
25-
if (x > WIDTH || y > HEIGHT) {
26-
break;
27-
}
28-
29-
png_bytep px = &(row[x * 4]);
30-
// px[0] = red, px[1] = green, px[2] = blue, px[3] = alpha
31-
if ((int)px[3] == 0) {
32-
continue;
33-
}
34-
Color pixel_colour = {(int)px[0], (int)px[1], (int)px[2]};
35-
pixel_matrix[y][x] = pixel_colour;
36-
}
37-
}
38-
}
39-
40-
4113
// Function to read PNG file and get pixel data
4214
std::vector<std::vector<png_byte>> read_png_file(const char* filename, int& width, int& height) {
4315
FILE* fp = fopen(filename, "rb");
@@ -113,7 +85,29 @@ std::vector<std::vector<png_byte>> read_png_file(const char* filename, int& widt
11385
return pixel_data;
11486
}
11587

88+
void get_image_data(const char* image_name) {
89+
int width, height;
90+
std::vector<std::vector<png_byte>> pixel_data = read_png_file(image_name, width, height);
91+
92+
resize_pixel_matrix(height, width);
93+
WIDTH = width;
94+
HEIGHT = height;
95+
96+
for (int y = 0; y < height; y++) {
97+
png_bytep row = pixel_data[y].data();
98+
for (int x = 0; x < width; x++) {
11699

100+
png_bytep px = &(row[x * 4]);
101+
//* px[0] = red, px[1] = green, px[2] = blue, px[3] = alpha
102+
if ((int)px[3] < 5) { // this makes sure pixels that are basically transparent are left transparent
103+
pixel_matrix[y][x] = NULL_COLOR;
104+
continue;
105+
}
106+
Color pixel_colour = {(int)px[0], (int)px[1], (int)px[2]};
107+
pixel_matrix[y][x] = pixel_colour;
108+
}
109+
}
110+
}
117111

118112

119113

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
11
# Pixel-Renderer-In-Terminal
2-
A engine to render images to the terminal
2+
A simple renderer that converts PNG images to unicode characters displayed in the terminal
3+
4+
## Slightly important things:
5+
1) You will need the "png.h" library to compile. Just google how to install
6+
7+
## Install and Compile
8+
```
9+
git clone https://github.com/InterstellarDragonDev/Pixel-Renderer-In-Terminal.git
10+
cd Pixel-Renderer-In-Terminal/
11+
mkdir build
12+
make
13+
```
14+
this will compile a executable file
15+
*you might have to give it execute permissions with `chmod +x pixel_engine`*
16+
17+
## How to use
18+
run the executable with the first parameter being the image file
19+
e.g) `./pixel_engine test_image.png`
20+
> [!IMPORTANT]
21+
> Only works with png's no other file format is supported
22+
23+
> [!NOTE]
24+
> 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
25+
26+
27+

Render-Engine.h

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,40 @@
1212
#include <string>
1313

1414

15-
1615
struct Color {
1716
int r, g, b;
1817
};
1918

20-
const int WIDTH = 200;
21-
const int HEIGHT = 200;
19+
int WIDTH = 0;
20+
int HEIGHT = 0;
2221

2322
const Color NULL_COLOR = {-1, -1, -1};
2423

25-
std::unordered_map<char, Color> colour_map = {
26-
{'R', {255, 0, 0}}, // Bright red
27-
{'G', {0, 255, 0}}, // Bright green
28-
{'B', {0, 0, 255}}, // Bright blue
29-
{'Y', {255, 255, 0}}, // Yellow
30-
{'M', {255, 0, 255}}, // Magenta
31-
{'C', {0, 255, 255}}, // Cyan
32-
{'g', {100, 100, 100}}, // Grey
33-
};
34-
35-
Color pixel_matrix[WIDTH][HEIGHT];
24+
std::vector<std::vector<Color>> pixel_matrix;
3625

3726

3827
//* ----------------------------------------------------------------------------------------------------------------------------- Functions start here
3928

29+
// a little function that randomly generates a colour (used for testing purposes)
4030
Color random_color(){
4131
return {(rand() % 255) + 1, (rand() % 255) + 1, (rand() % 255) + 1} ;
4232
}
4333

34+
// this resizes the pixel matrix when loading the image
35+
void resize_pixel_matrix(int num_rows, int num_cols) {
36+
pixel_matrix.resize(num_rows);
37+
38+
for (auto& row : pixel_matrix) {
39+
row.resize(num_cols);
40+
}
41+
}
42+
4443
// Function to compare two Color structs
4544
bool operator==(const Color& c1, const Color& c2) {
4645
return (c1.r == c2.r && c1.g == c2.g && c1.b == c2.b);
4746
}
4847

4948
void initialize() {
50-
// this sets a new seed for the random number generation
51-
srand((unsigned) time(0));
5249

5350
for (int y = 0; y < HEIGHT; y++){
5451
for (int x = 0; x < WIDTH; x++){
@@ -75,17 +72,5 @@ void render() {
7572
}
7673
}
7774

78-
// prints out the test to make sure pixel rendering is working
79-
void test_pixels() {
80-
std::cout << "rendering test: ";
81-
for(auto i:colour_map) {
82-
std::cout << i.first << " : ";
83-
print_single_pixel(i.second);
84-
std::cout << " ";
85-
}
86-
std::cout << "\n";
87-
}
88-
89-
9075

9176
#endif

main.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
#include <iostream>
21
#include "Render-Engine.h"
32
#include "Image-Converter.h"
43

5-
//* compile command: g++ -o build/pixel_engine main.cpp Render-Engine.h Image-Converter.h -lm -lpthread -lX11 -lpng -Wall -Wextra
6-
74
int main(int argc, char* argv[]) {
85

96
if (argc != 2) {
@@ -12,22 +9,9 @@ int main(int argc, char* argv[]) {
129
}
1310
const char* image_name = argv[1];
1411

15-
initialize();
16-
1712
get_image_data(image_name);
1813

1914
render();
20-
//std::vector<std::vector<png_byte>> pixel_data = read_png_file(image_name, width, height);
21-
22-
// Process the pixel data
23-
// for (int y = 0; y < height; y++) {
24-
// png_bytep row = pixel_data[y].data();
25-
// for (int x = 0; x < width; x++) {
26-
// png_bytep px = &(row[x * 4]);
27-
// // px[0] = red, px[1] = green, px[2] = blue, px[3] = alpha
28-
// std::cout << "Pixel at (" << x << ", " << y << "): R=" << (int)px[0] << " G=" << (int)px[1] << " B=" << (int)px[2] << " A=" << (int)px[3] << std::endl;
29-
// }
30-
// }
3115

3216
return 0;
3317
}

0 commit comments

Comments
 (0)