If no truecolor support, degrade to 216-colors

This commit is contained in:
Bob Polis 2024-01-27 16:25:23 +01:00
parent 7a555f6102
commit 17b32097fe

View File

@ -3,7 +3,15 @@
#include <netpbm/pam.h> #include <netpbm/pam.h>
#include "pixels.hpp" #include "pixels.hpp"
// truecolor to palette color (216-color)
static inline int t2p(int val) {
return .5 + 5 * val / 255.0;
}
void write_image(std::ostream& os, const std::string& path) { void write_image(std::ostream& os, const std::string& path) {
char* tc = getenv("TRUECOLOR");
std::string truecolor {tc ? tc : ""};
bool has_truecolor = truecolor.size() > 0;
std::unique_ptr<FILE, void(*)(FILE*)> infile {pm_openr(path.c_str()), pm_close}; std::unique_ptr<FILE, void(*)(FILE*)> infile {pm_openr(path.c_str()), pm_close};
struct pam info; struct pam info;
pnm_readpaminit(infile.get(), &info, PAM_STRUCT_SIZE(tuple_type)); pnm_readpaminit(infile.get(), &info, PAM_STRUCT_SIZE(tuple_type));
@ -15,9 +23,17 @@ void write_image(std::ostream& os, const std::string& path) {
for (int x = 0; x < info.width; ++x) { for (int x = 0; x < info.width; ++x) {
if (info.depth == 3) { // assume RGB, 1 byte per sample if (info.depth == 3) { // assume RGB, 1 byte per sample
tuple* t1 {row1.get()}; tuple* t1 {row1.get()};
os << sc::io::truecolorb(t1[x][0], t1[x][1], t1[x][2]); if (has_truecolor) {
os << sc::io::truecolorb(t1[x][0], t1[x][1], t1[x][2]);
} else {
os << sc::io::rgbb(t2p(t1[x][0]), t2p(t1[x][1]), t2p(t1[x][2]));
}
tuple* t2 {row2.get()}; tuple* t2 {row2.get()};
os << sc::io::truecolorf(t2[x][0], t2[x][1], t2[x][2]); if (has_truecolor) {
os << sc::io::truecolorf(t2[x][0], t2[x][1], t2[x][2]);
} else {
os << sc::io::rgbf(t2p(t2[x][0]), t2p(t2[x][1]), t2p(t2[x][2]));
}
os << u8"\u2584"; // unicode lower half block os << u8"\u2584"; // unicode lower half block
} }
} }