Created
May 19, 2022 19:12
-
-
Save stolk/67990004d8a36323ec58efe6cf6f084b to your computer and use it in GitHub Desktop.
Converts 24 bit PPM file into a 32 bit raw data blob that can be pasted to /dev/fb0 as is.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <assert.h> | |
#include <stdio.h> | |
#include <inttypes.h> | |
int main(int argc, char* argv[]) | |
{ | |
FILE* f = stdin; | |
if (argc!=1 && argc!=2) | |
{ | |
fprintf(stderr,"Usage: %s file.ppm > out.rgba\n", argv[0]); | |
exit(1); | |
} | |
if (argc==2) | |
{ | |
f = fopen(argv[1],"rb"); | |
assert(f); | |
} | |
char newl=0; | |
int imw=0, imh=0; | |
const int numscanned = fscanf(f, "P6 %d %d 255%c", &imw, &imh, &newl); | |
if (numscanned != 3) | |
{ | |
fprintf(stderr,"Input is not a valid 24-bit raw PPM image.\n"); | |
exit(2); | |
} | |
assert(imw); | |
assert(imh); | |
uint8_t* im_src = (uint8_t*) malloc(imw*imh*3); | |
uint8_t* im_dst = (uint8_t*) malloc(imw*imh*4); | |
assert(im_src && im_dst); | |
const size_t numr = fread(im_src, imw*imh, 3, f); | |
assert(numr == 3); | |
fclose(f); | |
uint8_t* reader = im_src; | |
uint8_t* writer = im_dst; | |
for (int i=0; i<imw*imh; ++i) | |
{ | |
*writer++ = reader[2]; // R | |
*writer++ = reader[1]; // G | |
*writer++ = reader[0]; // B | |
*writer++ = 255; // A | |
reader += 3; | |
} | |
fwrite(im_dst, imw*imh, 4, stdout); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment