Skip to content

Commit

Permalink
Optimize png files by reducing amount of colors to 255 + use oxipng m…
Browse files Browse the repository at this point in the history
…ax compressions

Godot head have only 4 colors (counting transparent) but in non quantized form our
files use dozens of colors. Usually 500 and up to 1000 in one of them. All additional
colors are colors used to anti-alias the image. Lots of them are used in file only once
or twice. They are mostly indistingusahble between each other so they are safe to remove
When we reduce count of colors used by PNG file to 256, we can change it is mode to
"Indexed". In this mode palette is created (up to 256 colors) and all the pixels are
described not by using RGB(A) 3/4 bytes but by in index in palette 1 byte

Signed-off-by: Yevhen Babiichuk (DustDFG) <dfgdust@gmail.com>
  • Loading branch information
dustdfg committed Dec 4, 2024
1 parent 47bc374 commit f106e9f
Show file tree
Hide file tree
Showing 36 changed files with 54 additions and 2 deletions.
Binary file modified icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified icon_outlined.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified logo_outlined.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified main/app_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified main/splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions misc/scripts/optimize_pngs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Requires ImageMagic 7+
# Requires oxipng 9.1.3+

import multiprocessing
import subprocess
import sys


def quantize_image(filename, desired_colors):
"""
Godot head have only 4 colors (counting transparent) but in non quantized form our
files use dozens of colors. Usually 500 and up to 1000 in one of them. All additional
colors are colors used to anti-alias the image. Lots of them are used in file only once
or twice. They are mostly indistingusahble between each other so they are safe to remove
When we reduce count of colors used by PNG file to 256, we can change it is mode to
"Indexed". In this mode palette is created (up to 256 colors) and all the pixels are
described not by using RGB(A) 3/4 bytes but by in index in palette 1 byte
"""

# Retrieve amount of colors in the source image
command = ["magick", filename, "-format", "%k", "info:-"]
colors_count = int(subprocess.check_output(command))

# Reduce amount of colors in image 1 by 1 for each step
# It is much slower then reducing to desired amount of colors in one call
# and gives slightly worse results but produces output without any artifacts
for i in range(colors_count, desired_colors, -1):
command = ["magick", filename, "-dither", "None", "-colors", str(i), filename]
subprocess.run(command)


def optimize_image(filename):
"""
Uses `oxipng's` "max" compression to compress image even further
"""
command = ["oxipng", "-t1", "-omax", "-q", "--zopfli", "--zi", "255", "--strip", "all", filename]
subprocess.run(command)


def process_image(filename):
print(f"quantize_image: {filename}")
quantize_image(filename, 255)
print(f"optimize_image: {filename}")
optimize_image(filename)


for i in range(1, len(sys.argv)):
process = multiprocessing.Process(target=process_image, args=[sys.argv[i]])
process.start()
Binary file modified platform/android/java/editor/src/horizonos/assets/vr_splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-hdpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-hdpi/icon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-hdpi/icon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-mdpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-mdpi/icon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-mdpi/icon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-xhdpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-xhdpi/icon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-xhdpi/icon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-xxhdpi/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-xxhdpi/icon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified platform/android/java/lib/res/mipmap-xxhdpi/icon_foreground.png
Binary file modified platform/android/java/lib/res/mipmap-xxxhdpi/icon.png
Binary file modified platform/android/java/lib/res/mipmap-xxxhdpi/icon_background.png
Binary file modified platform/android/java/lib/res/mipmap/icon.png
Binary file modified platform/android/java/lib/res/mipmap/icon_background.png
Binary file modified platform/android/java/lib/res/mipmap/icon_foreground.png
Binary file modified platform/android/java/lib/res/mipmap/icon_monochrome.png
4 changes: 2 additions & 2 deletions tests/core/io/test_pck_packer.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ TEST_CASE("[PCKPacker] Pack a PCK file with some files and directories") {
err == OK,
"The generated non-empty PCK file should be opened successfully.");
CHECK_MESSAGE(
f->get_length() >= 18000,
f->get_length() >= 7000,
"The generated non-empty PCK file should be large enough to actually hold the contents specified above.");
CHECK_MESSAGE(
f->get_length() <= 27000,
f->get_length() <= 16000,
"The generated non-empty PCK file shouldn't be too large.");
}
} // namespace TestPCKPacker
Expand Down
Binary file modified tests/data/images/icon.png

0 comments on commit f106e9f

Please sign in to comment.