-
Notifications
You must be signed in to change notification settings - Fork 0
/
stipje.hh
396 lines (337 loc) · 14.8 KB
/
stipje.hh
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* Copyright (c) 2018, Chris Smeele
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <cstdint>
#include <algorithm>
#include <tuple>
#include <mpl/mpl.hpp>
using pixel_t = uint32_t;
using namespace kvasir::mpl;
template<typename T, T V> struct value_ { static constexpr T value = V; };
// Lots of endianness assumptions below.
static_assert(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__);
// Convert 8 hex characters to one 32-bit pixel value, byte-order ABGR.
static constexpr uint32_t hex_to_abgr(char c7, char c6, char c5, char c4,
char c3, char c2, char c1, char c0) {
constexpr auto f = [] (char c) constexpr -> uint32_t {
return (c >= '0' && c <= '9'
? c - '0'
: c >= 'a' && c <= 'f'
? c - 'a' + 10
: c - 'A' + 10);
};
return ((f(c7) << 28) |
(f(c6) << 24) |
(f(c5) << 20) |
(f(c4) << 16) |
(f(c3) << 12) |
(f(c2) << 8) |
(f(c1) << 4) |
(f(c0)));
}
namespace stipje::pixel_format {
// `format` functions convert (byte-ordered) ABGR to another format
// (denoted by byte-ordered name).
// `unformat` convert back to ABGR, this is used to allow the image
// formatters to be agnostic of the pixel format of the internal buffer.
// All formats must currently assume pixels are 32-bit wide.
struct abgr {
// No-ops.
static constexpr pixel_t format(uint32_t x) { return x; }
static constexpr pixel_t unformat(uint32_t x) { return x; }
};
struct rgba {
static constexpr pixel_t format(uint32_t x) {
return (((x << 24)) |
((x << 8) & 0xff0000) |
((x >> 8) & 0x00ff00) |
((x >> 24) & 0x0000ff));
}
static constexpr pixel_t unformat(uint32_t x) {
return (((x << 24)) |
((x << 8) & 0xff0000) |
((x >> 8) & 0x00ff00) |
((x >> 24) & 0x0000ff));
}
};
}
template<typename PixelFormat_>
class ImageProxy {
// Interface to an image with only the pixel format as a template parameter.
const pixel_t *pixels_;
int width_;
int height_;
public:
using PixelFormat = PixelFormat_;
auto width() const { return width_; }
auto height() const { return height_; }
auto pixels() const { return pixels_; }
template<typename I>
ImageProxy(I &img)
: pixels_(img.pixels),
width_(img.width()),
height_(img.height())
{ }
};
template<typename T> ImageProxy(T &img) -> ImageProxy<typename T::PixelFormat>;
namespace stipje::image_format {
constexpr auto ptorgba = [] (pixel_t p) constexpr {
return std::tuple((p >> 24) & 0xff,
(p >> 16) & 0xff,
(p >> 8) & 0xff,
p & 0xff);
};
// PAM format: http://netpbm.sourceforge.net/doc/pam.html
struct pam {
template<typename Img, typename S>
static S &format(S &s, const Img &img) {
s << "P7\n";
s << "WIDTH " << img.width() << "\n";
s << "HEIGHT " << img.height() << "\n";
s << "DEPTH " << 4 << "\n";
s << "MAXVAL " << 255 << "\n";
s << "TUPLTYPE " << "RGB_ALPHA" << "\n";
s << "ENDHDR" << "\n";
if constexpr (std::is_same<typename Img::PixelFormat, pixel_format::rgba>::value) {
// Pixel format is already RGBA, just dump the pixel buffer.
s.write((const char*)img.pixels(), img.width() * img.height() * 4);
} else {
// Pixel format is different. Convert and write.
//
// XXX: Let's hope we have enough stack space.
// Don't want to depend on std::array/vector types here,
// because we don't want to throw.
pixel_t buf[img.width() * img.height()];
for (int i = 0; i < img.width() * img.height(); ++i)
buf[i] = pixel_format::rgba::format(Img::PixelFormat::unformat(img.pixels()[i]));
s.write((const char*)buf, img.width() * img.height() * 4);
}
return s;
}
};
// Similar to PAM, but simpler and more widely supported (no alpha channel).
struct pnm {
template<typename Img, typename S>
static S &format(S &s, const Img &img) {
s << "P6\n" << img.width() << "\n" << img.height() << "\n";
s << 255 << "\n";
for (int i = 0; i < img.width() * img.height(); ++i) {
// We can never write the buffer directly because the PNM
// format has this weird packed 24-bit pixel format.
auto p = Img::PixelFormat::unformat(img.pixels()[i]);
s << (char)(p >> 24);
s << (char)(p >> 16);
s << (char)(p >> 8);
}
return s;
}
};
// Console output, using 16 color escape codes that should work on anything xterm-like.
// Each pixel is a single unicode full block character.
struct console16 {
template<typename Img, typename S>
static S &format(S &s, const Img &img) {
for (int i = 0; i < img.width() * img.height(); ++i) {
if (i && i % img.width() == 0)
s << "\x1b[0m\n";
auto p = Img::PixelFormat::unformat(img.pixels()[i]);
auto [r,g,b,a] = ptorgba(p);
constexpr auto thres = 256/3;
int c =
((r >= thres)
| ((g >= thres) << 1)
| ((b >= thres) << 2));
if (a < 128) {
s << "\x1b[0m ";
} else {
s << "\x1b[" << (c + 30);
if (std::max({r, g, b}) > thres*2)
s << ";1";
s << "m█";
}
}
s << "\x1b[0m";
return s;
}
};
// Console output, using 16 color escape codes that should work on anything xterm-like.
// This uses unicode half-block characters and background colors to create square pixels.
struct console16_squared {
template<typename Img, typename S>
static S &format(S &s, const Img &img) {
for (int i = 0;; ++i) {
if (i && i % img.width() == 0) {
s << "\x1b[0m\n";
i += img.width(); // Skip even rows.
if (i >= img.width() * img.height())
break;
}
auto tp = Img::PixelFormat::unformat(img.pixels()[i]);
auto bp = i + img.width() >= img.width() * img.height()
? 0x0000000 // Special case for uneven-height images.
: Img::PixelFormat::unformat(img.pixels()[i + img.width()]);
auto [tr,tg,tb,ta] = ptorgba(tp);
auto [br,bg,bb,ba] = ptorgba(bp);
constexpr auto thres = 256/3;
int tc = ((tr >= thres)
| ((tg >= thres) << 1)
| ((tb >= thres) << 2));
int bc = ((br >= thres)
| ((bg >= thres) << 1)
| ((bb >= thres) << 2));
if (ta < 128 && ba < 128) {
s << "\x1b[0m "; // Transparency case.
} else if (ta < 128) {
s << "\x1b[0m\x1b[" << (bc + 30);
s << "m▄"; // Transparent top half case.
} else if (ba < 128) {
s << "\x1b[0m\x1b[" << (tc + 30);
s << "m▀"; // Transparent bottom half case.
} else {
s << "\x1b[" << (tc + 30) << ";" << (bc + 40);
s << "m▀"; // Full opacity case.
}
}
s << "\x1b[0m";
return s;
}
};
// Console output, using 24-bit color escape codes. This works on modern terminal emulators.
// This uses unicode half-block characters and background colors to create square pixels.
struct console256_squared {
template<typename Img, typename S>
static S &format(S &s, const Img &img) {
for (int i = 0;; ++i) {
if (i && i % img.width() == 0) {
s << "\x1b[0m\n";
i += img.width(); // Skip even rows.
if (i >= img.width() * img.height())
break;
}
auto tp = Img::PixelFormat::unformat(img.pixels()[i]);
auto bp = i + img.width() >= img.width() * img.height()
? 0x0000000 // Special case for uneven-height images.
: Img::PixelFormat::unformat(img.pixels()[i + img.width()]);
auto [tr,tg,tb,ta] = ptorgba(tp);
auto [br,bg,bb,ba] = ptorgba(bp);
if (ta < 128 && ba < 128) {
s << "\x1b[0m "; // Transparency case.
} else if (ta < 128) {
s << "\x1b[0m\x1b[38;2;" << br << ";" << bg << ";" << bb;
s << "m▄"; // Transparent top half case.
} else if (ba < 128) {
s << "\x1b[0m\x1b[38;2;" << tr << ";" << tg << ";" << tb;
s << "m▀"; // Transparent bottom half case.
} else {
s << "\x1b[38;2;" << tr << ";" << tg << ";" << tb;
s << ";48;2;" << br << ";" << bg << ";" << bb;
s << "m▀"; // Full opacity case.
}
}
s << "\x1b[0m";
return s;
}
};
}
namespace stipje {
template<typename S, typename I, typename F>
void format(S &s, I &img, F) {
F::format(s, ImageProxy {img});
}
}
namespace impl {
template<typename M, typename... Rest>
struct do_extract_palette;
template<typename... Ms,
typename Ch,
typename ChDiscard, // A space or tab, or whatever.
typename H7, typename H6, typename H5, typename H4,
typename H3, typename H2, typename H1, typename H0,
typename... Rest>
struct do_extract_palette<list<Ms...>,
Ch, ChDiscard,
H7, H6, H5, H4,
H3, H2, H1, H0,
Rest...> {
// Take a single palette color text (e.g. "# ffddaaff") and turn it into an key/value pair (as a mpl list).
using type = typename
do_extract_palette<list<Ms..., list<Ch, uint_<hex_to_abgr(H7::value,
H6::value,
H5::value,
H4::value,
H3::value,
H2::value,
H1::value,
H0::value)>>>,
Rest...>::type;
};
template<typename... Ms>
struct do_extract_palette<list<Ms...>> {
using type = list<Ms...>;
};
}
template<typename... Rest>
using extract_palette = impl::do_extract_palette<list<>, Rest...>;
template<typename T, T... Cs>
constexpr auto operator ""_stipje_palette() {
// Return a dummy value to contain the palette type.
struct X {
using Palette [[maybe_unused]] = typename extract_palette<value_<T, Cs>...>::type;
};
return X { };
}
// Get the value corresponding to a key in a kvlist.
// Kvs... is: list<K,V>...
template<typename K, typename... Kvs>
using assoc_get = call<find_if<unpack<at0<same_as<K>>>,
at0<unpack<at1<>>>,
always<void>>,
Kvs...>;
template<typename T, T... Cs>
constexpr auto operator ""_stipje() {
// Return a function that will receive the palette, width, height and pixel
// format, to create an image.
// C++20 template lambda syntax.
return [] <typename PT, typename W, typename H, typename Fmt>
(PT, W, H, Fmt) constexpr {
// Passing these as lambda parameters purely to get to the type feels dirty.
// Unfortunately I couldn't explicitly pass template parameters to a lambda...
static_assert(W::value*H::value == sizeof...(Cs), "incorrect dimensions");
struct Image {
// GCC complains when an alias isn't used locally, even though we
// reference it elsewhere.
using PixelFormat [[maybe_unused]] = Fmt;
static constexpr int width () { return W::value; }
static constexpr int height() { return H::value; }
pixel_t pixels[sizeof...(Cs)];
};
// In one go, transform the list of characters to a pixel buffer, with
// the given pixel format and palette.
return Image { Fmt::format(call<unpack<push_front<value_<T, Cs>, cfe<assoc_get>>>,
typename PT::Palette>::value)... };
};
}