dansanderson / picotool

Tools and Python libraries for manipulating Pico-8 game files. http://www.lexaloffle.com/pico-8.php
MIT License
371 stars 45 forks source link

Different data read by picotool and PICO-8 from old p8.png files #108

Open anas-purpurata opened 1 year ago

anas-purpurata commented 1 year ago

Hello Dan,

I have stumbled over a strange thing affecting .p8.png files created by older versions of PICO-8 (file version 8 and lower). It appears that the memory image a modern version of PICO-8 (tested 0.2.4c, 0.2.5c) reads from those files differs from what it should be according to the encoding scheme described in https://pico-8.fandom.com/wiki/P8PNGFileFormat and implemented in picotool.

Example: https://www.lexaloffle.com/bbs/get_cart.php?cat=7&play_src=2&lid=13010

When I read that PNG and look at the pixel data, the bytes starting at 0xc400 look like this:

0c400 fc f0 e9 fc fc f0 ea fc

Decoding them according to the usual scheme gives

fc f0 e9 fc fc f0 ea fc 

extract lowest 2 bits (& 3):
00 00 01 00 00 00 02 00 

merge (alpha << 6 + red << 4 + green << 2 + blue) :
01 02

Since 0xc400 / 4 == 0x3100, these are the first bytes of the music section. So the first music pattern should have SFX 1 in the first channel and SFX 2 in the second channel. But when I open the file with PICO-8, I instead get SFX 2 in the first channel and SFX 1 in the second channel:

Screenshot_20221129_204134

This is confirmed when I re-export the file using PICO-8, i.e. pico8 -export test.p8.png 13010.png. When I look at the pixels of the re-exported file at the same offset, I see

0c400 fc f0 ea fc fc f0 e9 fc

extract lowest 2 bits (& 3):
00 00 02 00 00 00 01 00 

merge (alpha << 6 + red << 4 + green << 2 + blue) :
02 01

Those are not the only differences between the original and the re-exported version. There are also changes in the SFX section:

(this is memory image data, not pixels)
-03100 02 01 03 44 41 42 43 44 41 42 43 44 41 42 43 44 
+03100 01 02 03 44 41 42 43 44 41 42 43 44 41 42 43 44 
-03200 8c 03 93 05 9b 07 9f 09 a4 0b a8 0c aa 0b ad 0e 
+03200 8c 03 93 05 9b 07 9f 09 a4 0b a8 0c aa 0f ad 0e 
-03210 bb 0b bb 0b bb 0b bb 0b bb 0b bb 0b a9 0e a6 0b 
+03210 bb 0f bb 0f bb 0f bb 0f bb 0f bb 0f a9 0e a6 0f

In all the carts I tested, this seems to affect only the SFX and music sections.

Do you have any idea what PICO-8 is doing here?

Have a nice day!