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
| char desired_output[] = "";
#define COLOR_PIXEL_FMT "\x1b[38;2;%03d;%03d;%03dm%c\x1b[0m"
typedef struct { union { char data[24]; struct term_str_st { char color_set[7]; char r[3]; char s1; char g[3]; char s2; char b[3]; char m; char c; char color_reset[4]; } str; }; } term_pixel_t;
struct cimg { struct cimg_header header; unsigned num_pixels; term_pixel_t *framebuffer; };
#define CIMG_NUM_PIXELS(cimg) ((cimg)->header.width * (cimg)->header.height) #define CIMG_DATA_SIZE(cimg) (CIMG_NUM_PIXELS(cimg) * sizeof(pixel_t)) #define CIMG_FRAMEBUFFER_PIXELS(cimg) ((cimg)->header.width * (cimg)->header.height) #define CIMG_FRAMEBUFFER_SIZE(cimg) (CIMG_FRAMEBUFFER_PIXELS(cimg) * sizeof(term_pixel_t))
void display(struct cimg *cimg, pixel_t *data) { int idx = 0; for (int y = 0; y < cimg->header.height; y++) { for (int x = 0; x < cimg->header.width; x++) { idx = (0+y)*((cimg)->header.width) + ((0+x)%((cimg)->header.width)); char emit_tmp[24+1]; snprintf(emit_tmp, sizeof(emit_tmp), "\x1b[38;2;%03d;%03d;%03dm%c\x1b[0m", data[y * cimg->header.width + x].r, data[y * cimg->header.width + x].g, data[y * cimg->header.width + x].b, data[y * cimg->header.width + x].ascii); memcpy((cimg)->framebuffer[idx%(cimg)->num_pixels].data, emit_tmp, 24);
} }
for (int i = 0; i < cimg->header.height; i++) { write(1, cimg->framebuffer+i*cimg->header.width, sizeof(term_pixel_t)*cimg->header.width); write(1, "\x1b[38;2;000;000;000m\n\x1b[0m", 24); } }
struct cimg *initialize_framebuffer(struct cimg *cimg) { cimg->num_pixels = CIMG_FRAMEBUFFER_PIXELS(cimg); cimg->framebuffer = malloc(CIMG_FRAMEBUFFER_SIZE(cimg)+1); if (cimg->framebuffer == NULL) { puts("ERROR: Failed to allocate memory for the framebuffer!"); exit(-1); } for (int idx = 0; idx < cimg->num_pixels; idx += 1) { char emit_tmp[24+1]; snprintf(emit_tmp, sizeof(emit_tmp), "\x1b[38;2;%03d;%03d;%03dm%c\x1b[0m", 255, 255, 255, ' '); memcpy(cimg->framebuffer[idx].data, emit_tmp, 24);
}
return cimg; }
void __attribute__ ((constructor)) disable_buffering() { }
int main(int argc, char **argv, char **envp) {
struct cimg cimg = { 0 }; cimg.framebuffer = NULL; int won = 1;
if (argc > 1) { if (strcmp(argv[1]+strlen(argv[1])-5, ".cimg")) { printf("ERROR: Invalid file extension!"); exit(-1); } dup2(open(argv[1], O_RDONLY), 0); }
read_exact(0, &cimg.header, sizeof(cimg.header), "ERROR: Failed to read header!", -1);
if (cimg.header.magic_number[0] != 'c' || cimg.header.magic_number[1] != 'I' || cimg.header.magic_number[2] != 'M' || cimg.header.magic_number[3] != 'G') { puts("ERROR: Invalid magic number!"); exit(-1); }
if (cimg.header.version != 2) { puts("ERROR: Unsupported version!"); exit(-1); }
initialize_framebuffer(&cimg);
unsigned long data_size = cimg.header.width * cimg.header.height * sizeof(pixel_t); pixel_t *data = malloc(data_size); if (data == NULL) { puts("ERROR: Failed to allocate memory for the image data!"); exit(-1); } read_exact(0, data, data_size, "ERROR: Failed to read data!", -1);
for (int i = 0; i < cimg.header.width * cimg.header.height; i++) { if (data[i].ascii < 0x20 || data[i].ascii > 0x7e) { fprintf(stderr, "ERROR: Invalid character 0x%x in the image data!\n", data[i].ascii); exit(-1); } }
display(&cimg, data);
if (cimg.num_pixels != sizeof(desired_output)/sizeof(term_pixel_t)) { won = 0; } for (int i = 0; i < cimg.num_pixels && i < sizeof(desired_output)/sizeof(term_pixel_t); i++) { if (cimg.framebuffer[i].str.c != ((term_pixel_t*)&desired_output)[i].str.c) { won = 0; } if ( cimg.framebuffer[i].str.c != ' ' && cimg.framebuffer[i].str.c != '\n' && memcmp(cimg.framebuffer[i].data, ((term_pixel_t*)&desired_output)[i].data, sizeof(term_pixel_t)) ) { won = 0; } }
if (won) win(); }
|