-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasciimage.cpp
More file actions
260 lines (220 loc) · 6.95 KB
/
asciimage.cpp
File metadata and controls
260 lines (220 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <stdio.h>
#include <stdint.h>
#include <string>
#include <vector>
#include "winsole/winsole.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb/stb_image_write.h"
typedef unsigned char byte;
struct Pixel
{
char ch;
COLORS colors;
};
struct RGBA
{
byte r, g, b, a = 255;
byte max_value() const { return (r > g ? (r > b ? r : b) : (g > b ? g : b)); }
byte min_value() const { return (r < g ? (r < b ? r : b) : (g < b ? g : b)); }
float average() const { return (r + g + b) / 3.0f; }
};
struct Image
{
std::string path;
int width, height, bpp, channels;
byte *data = nullptr;
Image() = default;
Image(std::string path, int channels = 3) : path(path), channels(channels) {}
size_t size() const { return width * height * channels; }
size_t image_size() const { return width * height; }
bool read(std::string rpath = "", int rchannels = 0)
{
if (rchannels < 3 || rchannels > 4)
rchannels = channels;
if (rpath.empty())
rpath = path;
if (data)
stbi_image_free(data);
data = stbi_load(rpath.c_str(), &width, &height, &bpp, rchannels);
if (!data)
return false;
channels = rchannels;
return true;
}
void get_color_array(RGBA *&color_array) const
{
if (color_array)
delete[] color_array;
color_array = new RGBA[image_size()];
for (size_t b = 0, color = 0; b < size(); b += channels)
color_array[color++] = {data[b], data[b + 1], data[b + 2], (byte)((channels == 4) ? data[b + 3] : 255)};
}
~Image()
{
if (data)
stbi_image_free(data);
}
};
float map(float input, float x1, float x2, float y1, float y2)
{
return y1 + (input - x1) * (y2 - y1) / (x2 - x1);
}
void put_line(Winsole &winsole, int x, int y, const Pixel *line, size_t length)
{
for (size_t i = 0; i < length; ++i)
{
const Pixel &p = line[i];
winsole.set_cursor(x + i, y);
winsole.set_color(p.colors.fg, p.colors.bg);
winsole.print(p.ch);
}
}
std::string ascii_image(const Image &image, RGBA *&colors, const std::string &ascii_map)
{
std::string ascii_output;
ascii_output.reserve(image.image_size() + image.height); // '\n's
for (size_t ci = 0, row = 1; ci < image.image_size(); ci++)
{
byte grey = (colors[ci].max_value() + colors[ci].min_value()) / 2;
byte index = map(grey, 0, 255, 0, ascii_map.length() - 1);
if (ci >= (row * image.width))
{
ascii_output += '\n';
row++;
}
ascii_output += ascii_map[index];
}
return ascii_output;
}
void print_color_image_fast(Winsole &winsole, const Image &image, RGBA *&colors, const std::vector<Color> &colormap)
{
size_t w = image.width, h = image.height;
std::vector<Pixel> pixels;
pixels.reserve(w * h);
size_t map_size = colormap.size();
for (size_t i = 0; i < image.image_size(); ++i)
{
byte grey = (colors[i].max_value() + colors[i].min_value()) / 2;
Color color = colormap[static_cast<size_t>(map(grey, 0, 255, 0, map_size - 1))];
pixels.push_back({' ', {AUTO, color}});
}
for (size_t y = 0; y < h; ++y)
winsole.put_line(0, y, &pixels[y * w], w);
}
void print_color_ascii_fast(Winsole &winsole, const std::string &ascii_image, RGBA *&colors, const Image &image, const std::vector<Color> &colormap)
{
size_t w = image.width, h = image.height;
std::vector<Pixel> pixels;
pixels.reserve(w * h);
size_t ci = 0;
size_t map_size = colormap.size();
for (char ch : ascii_image)
{
if (ch == '\n')
continue;
byte grey = (colors[ci].max_value() + colors[ci].min_value()) / 2;
Color color = colormap[static_cast<size_t>(map(grey, 0, 255, 0, map_size - 1))];
pixels.push_back({ch, {color, AUTO}});
ci++;
}
for (size_t y = 0; y < h; ++y)
winsole.put_line(0, y, &pixels[y * w], w);
}
void fast_print(const Winsole &console, const std::string &buffer)
{
WriteConsole(console.get_handle(), buffer.c_str(), buffer.length(), nullptr, nullptr);
}
#define version_message "AsciiMage v1.1 (May 2025)\n\n"
#define DEFAULT_ASCII " ._-3#@"
#define DEFAULT_COLOR_MAP {BLACK, BLACK, GREY, GREY, BLUE, LIGHT_BLUE, AQUA, LIGHT_AQUA, WHITE, WHITE}
void print_help()
{
printf(version_message);
printf("[USAGE]\n");
printf(" asciimage [--help] Display this message.\n");
printf(" asciimage <input> <mode> [map] Prints an image in the selected mode.\n");
printf("\n[MODES]\n");
printf(" ASCII Prints ASCII version fast.\n");
printf(" COLOR Colored image (optimized).\n");
printf(" ASCOL ASCII+color (optimized).\n");
printf("\n[MAPS]\n");
printf(" ASCII mode: single string map.\n");
printf(" COLOR/ASCOL: ASCII map + color palette string (e.g., \"0193BF\").\n");
}
int main(int argc, char *argv[])
{
argc -= 1;
if (!argc)
{
print_help();
return 0;
}
std::string str_args[argc];
for (size_t i = 0; i < argc; i++)
{
str_args[i] = argv[i + 1];
if (str_args[i] == "-h" || str_args[i] == "--help")
{
print_help();
return 0;
}
}
Winsole console;
if (!console.init())
{
fprintf(stderr, "[!] Failed to init console.\n");
return 1;
}
std::string input_path = str_args[0];
std::string ascii_map, color_map;
Image input_image(input_path.c_str());
if (!input_image.read())
{
fprintf(stderr, "[!] Failed to read image.\n");
return 1;
}
RGBA *colors = nullptr;
input_image.get_color_array(colors);
if (!colors)
{
fprintf(stderr, "[!] Failed to get color array.\n");
return 1;
}
if (argc == 1)
str_args[1] = "ASCII";
if (str_args[1] == "ASCII")
{
ascii_map = (argc == 2) ? DEFAULT_ASCII : str_args[2];
std::string ascii_output = ascii_image(input_image, colors, ascii_map);
fast_print(console, ascii_output);
delete[] colors;
return 0;
}
std::vector<Color> colormap;
if (argc == 2)
{
colormap = DEFAULT_COLOR_MAP;
}
else
{
for (char ch : str_args[2])
{
Color color = (ch >= 'A' && ch <= 'F') ? static_cast<Color>((ch - 'A') + 10) : static_cast<Color>(ch - '0');
colormap.push_back(color);
}
}
if (str_args[1] == "COLOR")
{
print_color_image_fast(console, input_image, colors, colormap);
}
else if (str_args[1] == "ASCOL")
{
std::string ascii_map = (argc == 4) ? str_args[3] : DEFAULT_ASCII;
std::string ascii_output = ascii_image(input_image, colors, ascii_map);
print_color_ascii_fast(console, ascii_output, colors, input_image, colormap);
}
delete[] colors;
return 0;
}