Slluxx / IconGrabber

GNU General Public License v3.0
30 stars 8 forks source link

Downloaded images are not sanitized for extra metadata #6

Open rlaphoenix opened 1 year ago

rlaphoenix commented 1 year ago

If you download an image, already JPEG or otherwise, it will not be immediately compatible with Horizon OS. Any single extra piece of metadata that is not strictly required for viewing the image, will cause Horizon OS to display a "?" icon rather than the icon selected. It's extremely annoying.

This is happening in some images from SteamGridDB. Ideally adding a function that strips downloaded metadata from any and all extra metadata would be ideal.

Example:

2023080711180000-57B4628D2267231D57E0FC1078C0596D 2023080711180400-57B4628D2267231D57E0FC1078C0596D

The following SteamGridDB Images for SONIC FORCES (600x900) all fail to display:

Whereas the Image 305235 for the same Title works flawlessly.

2023080711335700-57B4628D2267231D57E0FC1078C0596D

You can strip the unnecessary metadata using ImageMagick or Python Pillow/PIL. I'm sure there are also ways in C++.

Via ImageMagick (overwrites input):

magick mogrify -format jpg -resize 256x256 -strip downloaded_icon.png

Via Python Pillow (overwrites input):

icon_file = Path("downloaded_icon.png")
# ...
im = Image.open(icon_file)
if im.size != (256, 256):
    im = im.resize((256, 256))
if im.mode != "RGB":
    im = im.convert("RGB")
clean_im = Image.new(im.mode, im.size)
clean_im.putdata(list(im.getdata()))
clean_im.save(icon_file, format="JPEG")
clean_im.close()
im.close()

Note that it's resizing to 256x256 as that's normally a required restriction for Icon files. However, with sys-tweak, I'm not sure if that's still the case.

I'm using a self-compiled build of sys-tweak as of the latest commit https://github.com/p-sam/switch-sys-tweak/commit/fe4cf8582023901afaacb0ac22cfbc39bd928445

Slluxx commented 1 year ago

Good research!

Can you upload a non-working image and a stripped one to this issue? I knew this issue existed but didn't know the reason. Now that you have proposed this, i was reading about that different image formats (JFIF/Exif) can exist for a single extension (JPEG).

Apparently JFIF is not supported by stb_image so i wonder if the images from steamgriddb that dont work are JFIF and your converting doesnt just strip the metadata but also converts it to Exif or if its, like you said, "just" the metadata that fucks up the image for the switch.

rlaphoenix commented 1 year ago

Hi,

I've updated the original post with links to the exact images on SteamGridDB. All three of them are PNG images, and so is the 4th image that works fine.

There doesn't seem to be any "extra" metadata that may affect it (though I haven't fully checked that), but one thing I have noticed is all three of the broken images are 32-bit RGB, while the working one is 24-bit RGB. All images are 8bpp, but the three broken images are RGBA (therefore a 32-bit image) while the working image is 24-bit.

I guess just see if you can convert from RGBA to RGB explicitly (remove the alpha channel). However, I still believe you should look into stripping metadata if possible as extra metadata WILL also cause this. I can confirm this from another project I have that deals with NSPs.

Slluxx commented 1 year ago

Sounds good, i will leave this open until i can fix it. I am actually working on a new version that is not yet on github, so this is perfect timing.

sodasoba1 commented 1 year ago

It's definitely the 32bit rgba images that are causing they grey "?" @rlaphoenix if you open the image on a pc the image displayed has a weird corrupt look like it's not processed correctly.

Metadata stripping should help towards filesize especially when 120kb is a good size to aim for (128kb being the maximum)