Packing a PNG image into a GB ROM (and vice versa)

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Packing a PNG image into a GB ROM (and vice versa)
by on (#226755)
I've managed to produce a file that's a valid GB ROM (tested on my GB Pocket) and a valid PNG image.

Attachment:
File comment: This is a ROM.
gbpng.gb.png
gbpng.gb.png [ 32 KiB | Viewed 4080 times ]


Rules: Only RST $28, $30, and $38 may be used, not $00-$20, and $28 must start with LD L,L. There must also be enough free space at the end of the ROM to hold the PNG.
Re: Packing a PNG image into a GB ROM
by on (#226765)
Impressive, but the 32Kbyte PNG file is larger than a 160x144pix BMP file? Does it support compressed PNG's, too? Ah, no, got it! It is compressed, with the 32kbyte "png" file containing the decompressor code in first some bytes, and the main part of the image at offset 4000h..4603h? I always thought .gif would compress better at low color depths, but that PNG is actually smaller than the same image stored as 4bpp GIF.

Now I am wondering what happens if you open the image in your gameboy's email client... no, no, never ever never open email attachments on your gameboy ; )
Re: Packing a PNG image into a GB ROM
by on (#226767)
nocash wrote:
Impressive, but the 32Kbyte PNG file is larger than a 160x144pix BMP file?

Yes. The PNG file contains the ROM in a prGm chunk.

nocash wrote:
It is compressed, with the 32kbyte "png" file containing the decompressor code in first some bytes, and the main part of the image at offset 4000h..4603h?

It actually stores two copies of the image: one for the GB to display, compressed with a less space-efficient but more time-efficient codec, and one for PNG viewers to display. The GB ignores the PNG, and PNG viewers ignore the program. The PNG and the pic inside the ROM can look different and usually will in practice. It can be used, for example, to embed a screenshot into the last 4K of a game. Or it can be used to upload (say) yet another port of Magic Floor to forums whose file type whitelist is authoritarian, but not so much that they recompress all uploaded images. I just made them the same because I was lazy time constrained.

Speaking of which, I've got a couple questions about Magic Floor that I'll take to private messages.