#include #include #include #include #include #include #include #include #include #include void bitmap_header(FILE* file, const uint32_t w, const uint32_t h){ const size_t ims = sizeof(uint8_t[h][w][4]); const uint8_t header[54] = { 'B','M', (sizeof(header)+ims),(sizeof(header)+ims)>>8,(sizeof(header)+ims)>>16,(sizeof(header)+ims)>>24, 0,0,0,0, sizeof(header),sizeof(header)>>8,sizeof(header)>>16,sizeof(header)>>24, 40,0,0,0, w,w>>8,w>>16,w>>24, h,h>>8,h>>16,h>>24, 1,0, 32,0, 0,0,0,0, ims,ims>>8,ims>>16,ims>>24, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0 }; fwrite(header, 1, sizeof(header), file); } bool qoi_to_bmp(const unsigned char* a){ if(!qoi_check_magic(a)) return false; uint32_t width = qoi_get_width(a); uint32_t height = qoi_get_height(a); struct qoi_decoder qoi = QOI_DECODER_INIT(a); bitmap_header(stdout, width, height); unsigned char buf[16*4] = {0}; size_t length = (size_t)width*height*4; while(true){ qoi_decode(&qoi, sizeof(buf)/4, buf); if(length < sizeof(buf)) break; fwrite(buf, 1, sizeof(buf), stdout); length -= sizeof(buf); } fwrite(buf, 1, length, stdout); fclose(stdout); return true; } int main(int argc, char *argv[]){ if(argc != 1){ fprintf(stderr, "usage: %s image.bmp\n", argv[0]); return 1; } struct stat sb; if(fstat(0, &sb) == -1){ perror("fstat"); return 1; } const unsigned char* a = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, 0, 0); if(a == MAP_FAILED){ perror("mmap"); return 1; } if(!qoi_to_bmp(a)){ fprintf(stderr, "Failed to parse qoi\n"); return 1; } munmap((void*)a, sb.st_size); }