nesdev.com
https://forums.nesdev.com/

Lossy compression for NES screens
https://forums.nesdev.com/viewtopic.php?f=21&t=14807
Page 1 of 1

Author:  JRoatch [ Fri Sep 09, 2016 8:30 pm ]
Post subject:  Lossy compression for NES screens

So I took a couple days to tackle a problem I had to deal with a couple of times: Fitting complex images into the 256 tile limit.
What I basically tried out was an automated version of the process I end up do manually. Randomly search for 2 similar tiles that are unique in the nametable and merge them in some way.

In this program similar tiles are pairs of tiles that have the least number of pixels changed, and merging is randomly selecting the pixels from both tiles

Example image NES screen sized image with way too many distinct tiles (471)
Image
Source: https://twitter.com/azumakiyohiko/status/495548119048679426

After merging tiles together until there's only 256 unique ones.
Image

Results vary due to random pixel selections between tiles. Not the best of quality, but looks on par with the worst JPEG compression. In any case I figured this hack of a tool might be useful to someone. It's input is the output of tepple's pilbmp2nes.py

Attachments:
compress-nes-image.py [2.06 KiB]
Downloaded 73 times

Author:  rainwarrior [ Fri Sep 09, 2016 9:22 pm ]
Post subject:  Re: Lossy compression for NES screens

Two ideas that might improve results:

1. Instead of just "number of changed pixels", multiply this number by the number of times that either tile is being used in the image. That will give you the total number of pixels changed in the whole image, rather than just the number changed in the individual tiles. (Also might guide the merging process; i.e. where pixels differ, take more from the one that is used more.)

2. Use a 3x3 neighbourhood to weight the importance of pixel changes. e.g. if you're about to place a black pixel into a 3x3 region of white pixels, consider this a greater change than placing a black pixel next to a group of black pixels just extending an edge, etc. This might let you favour changes that don't stand out as much? (Also gives you a way to choose which pixels to keep from the two merging tiles.)

Author:  JRoatch [ Sat Sep 10, 2016 10:03 am ]
Post subject:  Re: Lossy compression for NES screens

Suggestion 1 about counting the pixel changes of the whole image helped a lot. What is measured now better matches what's changed. Combined with the suggestion to bias the merge changes, this now does subtle changes to more tiles rather then big changes to less.

Image

I not sure how to work in suggestion 2. I did try something with a 3x3 region, but I must of severely messed up the weights because the result was garbage.

The way I see the problem is that tiles with opposite edges and corners are matched together, but every time I try to fix the random edge dots, the overall picture gets worse. I'm starting to think the random dots are actually a feature and the best compromise for reducing the tileset.

A thing that I might try another day is to factor in a manually made mask that prevents select pixels from changing. So that some crucial details, like faces, are preserved.

Attachments:
compress-nes-image-2.py [2.41 KiB]
Downloaded 75 times

Author:  Alp [ Sat Sep 10, 2016 12:38 pm ]
Post subject:  Re: Lossy compression for NES screens

JRoatch wrote:
So I took a couple days to tackle a problem I had to deal with a couple of times: Fitting complex images into the 256 tile limit.

Example image NES screen sized image with way too many distinct tiles (471)


471? That's honestly not too bad. With fullscreen scenes like this, I say screw the limitations!

I tend to limit my RPG's *ahem* "cutscenes" to 448 tiles, with a timed $00/$10 background split, to use more on-screen tiles. The rest is used for sprite overlays, to enhance the scene. It's all done by hand, on my end, though.

Author:  Macbee [ Mon Nov 14, 2016 12:32 pm ]
Post subject:  Re: Lossy compression for NES screens

Thanks JRoatch for creating (and publishing) this!
It's fantastic and is already helping me a lot with a NES project.

User NESRocks is also using this - and even created a .BAT to make it easier to use (just drop the BMP file to convert.bat).
I'm attaching it here.

Are you planning to release a version that also removes the duplicated tiles?
It can be done on NES Screen Tool, just asking for curiosity.

Thanks! =)

Attachments:
bmp2nescompress.zip [9.17 KiB]
Downloaded 62 times

Author:  Bregalad [ Mon Nov 14, 2016 12:45 pm ]
Post subject:  Re: Lossy compression for NES screens

Looks like this really rocks ! Drawing such a scene limited to 256 tiles should be hard, a bit of automation can always help.

Also, for those pointing to the possibility of doing raster effects trickery to bypass the limitation, I'd add that there's a million of reason why you'd want to stick to vanialla 256 tiles and not want to deal with PPU register writes midframe at all.

Author:  FrankenGraphics [ Mon Nov 14, 2016 2:30 pm ]
Post subject:  Re: Lossy compression for NES screens

A masking feature sounds like a very sensible option to be able to use.

Currently, i'm doing this completely manually.

Author:  tepples [ Tue Nov 14, 2017 10:06 pm ]
Post subject:  Re: Lossy compression for NES screens

Would you be willing to make compress-nes-image-2.py available under a free software license so that I can incorporate it into my NES background tools?

Author:  JRoatch [ Wed Nov 15, 2017 7:00 am ]
Post subject:  Re: Lossy compression for NES screens

Sure, it's now under the "Expat License".

Attachments:
lossy-nes-image.py [3.58 KiB]
Downloaded 19 times

Author:  tepples [ Wed Nov 15, 2017 9:53 am ]
Post subject:  Re: Lossy compression for NES screens

Many thanks.

Author:  bazza [ Sat Nov 25, 2017 4:00 pm ]
Post subject:  Re: Lossy compression for NES screens

Macbee wrote:
Thanks JRoatch for creating (and publishing) this!
It's fantastic and is already helping me a lot with a NES project.

User NESRocks is also using this - and even created a .BAT to make it easier to use (just drop the BMP file to convert.bat).
I'm attaching it here.

Are you planning to release a version that also removes the duplicated tiles?
It can be done on NES Screen Tool, just asking for curiosity.

Thanks! =)


Some example of as use the output_result.chr and input_tiles.chr In a project?

Author:  zzo38 [ Sat Nov 25, 2017 7:55 pm ]
Post subject:  Re: Lossy compression for NES screens

Macbee wrote:
Are you planning to release a version that also removes the duplicated tiles?
It can be done on NES Screen Tool, just asking for curiosity.
My ff-uniq program will remove duplicated tiles from a tileset. However, it does not use the Famicom format directly (but ffbit and bitff can be used to convert in either direction).

Page 1 of 1 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/