Studying Papyrix's support library for loading PNG images, by 1st converting them into BMPs on the SD card...
It has some serialization support routines (redundant?), a util for writing a 2bit BMP header, & a struct for tracking all the image metadata as its being serialized.
There's a callback which validates its data, at start of row checks if it should abort, converts the RGBA (on white) colour into gray possibly saving it into a row buffer, & checks if its the last pixel of the row.
1/2?
Upon finishing a row it might zero out a final buffer, zero out the temporary buffer, iterates over each pixel using helpers to colour-correct & either dither or quantize each pixel before bitpacking them, tells the ditherer if any that we're on a new row, & writes it out to the file. Or a variant might average adjacent pixels before applying similar logic to output the downsampled image.
There's an init callback which initializes its buffers & metadata from the image's metadata.
2/3!