41 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include <memory>
 | 
						|
#include <libscterm.hpp>
 | 
						|
#include <netpbm/pam.h>
 | 
						|
#include "pixels.hpp"
 | 
						|
 | 
						|
void write_image(std::ostream& os, FILE* in) {
 | 
						|
    struct pam info;
 | 
						|
    pnm_readpaminit(in, &info, PAM_STRUCT_SIZE(tuple_type));
 | 
						|
    std::unique_ptr<tuple, void(*)(void*)> row1 {pnm_allocpamrow(&info), pm_freerow};
 | 
						|
    std::unique_ptr<tuple, void(*)(void*)> row2 {pnm_allocpamrow(&info), pm_freerow};
 | 
						|
    for (int y = 0; y < info.height; y += 2) {
 | 
						|
        tuple* t1 {row1.get()};
 | 
						|
        tuple* t2 {row2.get()};
 | 
						|
        pnm_readpamrow(&info, row1.get());
 | 
						|
        if (y < info.height - 1) {
 | 
						|
            pnm_readpamrow(&info, row2.get());
 | 
						|
        }
 | 
						|
        for (int x = 0; x < info.width; ++x) {
 | 
						|
            if (info.depth == 3) { // assume RGB, 1 byte per sample
 | 
						|
                os << sc::io::truecolorf(t1[x][0], t1[x][1], t1[x][2]);
 | 
						|
                if (y < info.height - 1) {
 | 
						|
                    os << sc::io::truecolorb(t2[x][0], t2[x][1], t2[x][2]);
 | 
						|
                } else {
 | 
						|
                    os << sc::io::defaultb;
 | 
						|
                }
 | 
						|
                os << u8"\u2580"; // unicode upper half block
 | 
						|
            }
 | 
						|
        }
 | 
						|
        os << sc::io::reset << '\n';
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
void write_image(std::ostream& os) {
 | 
						|
    write_image(os, stdin);
 | 
						|
}
 | 
						|
 | 
						|
void write_image(std::ostream& os, const std::string& path) {
 | 
						|
    std::unique_ptr<FILE, void(*)(FILE*)> infile {pm_openr(path.c_str()), pm_close};
 | 
						|
    write_image(os, infile.get());
 | 
						|
}
 |