Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize PNG files #13509

Merged
merged 8 commits into from
Oct 9, 2023
Merged

Optimize PNG files #13509

merged 8 commits into from
Oct 9, 2023

Conversation

Bituvo
Copy link
Contributor

@Bituvo Bituvo commented May 14, 2023

Yet another trivial PR. I ran all of the PNG files through the minetest_game optimizer. I'm not sure how many kilobytes I saved, but I know it's more than one.

@Zughy Zughy added Maintenance Tasks to keep the codebase and related parts in order, including architectural improvements Trivial The change is a trivial bug fix, documentation or maintenance change, as per the Git Guidelines labels May 14, 2023
@Bituvo Bituvo changed the title Optimize base PNG files Optimize PNG files May 15, 2023
@oong819
Copy link
Contributor

oong819 commented May 16, 2023

what's the different between optimized PNG and lossless compressed PNG?

@fluxionary
Copy link
Contributor

what's the different between optimized PNG and lossless compressed PNG?

the resulting image is identical, but the files are smaller. see https://linux.die.net/man/1/optipng

@Montandalar
Copy link
Contributor

@Bituvo It looks like you have merged master into this branch. This project prefers the rebase workflow, where if a branch needs to be updated, it should be rebased and force pushed instead of merging master. This keeps the history cleaner. See: .github/CONTRIBUTING.md

@sfan5
Copy link
Member

sfan5 commented May 24, 2023

FWIW it doesn't really matter what the contributor does to their branch since changes are squashed upon merging anyway.

@HybridDog
Copy link
Contributor

HybridDog commented Jun 3, 2023

The texture changes increase the repository size because both the old and new versions are saved in the history, and the texture sizes are probably not optimal, so a future recompression may further decrease texture sizes and increase the repository size.

I suggest to keep only file changes where the size reduction is at least 70% and to recompress them more aggressively with oxipng and advpng.

changed_files=$(git diff --name-only master...)
for f in $changed_files; do
	size_old=$(git cat-file -s "master:$f")
	size_new=$(git cat-file -s "@:$f")
	if ((size_old * 7 > size_new * 10)); then
		echo "$f"
	fi
done
# Requires imagemagick, oxipng and advpng

png_shrink() {
	file=$1
	size_initial=$(stat --printf='%s' "$file")
	size_prev=$size_initial
	while true; do
		if [[ $(convert "$file" -format "%[opaque]" info:) == "False" ]]; then
			# There is an alpha channel. Keep colour in transparent pixels.
			oxipng --nc --opt max --zopfli "$file"
			oxipng --nc --opt max "$file"
		else
			advpng --shrink-insane --recompress "$file"
			oxipng --opt max --zopfli "$file"
			oxipng --opt max "$file"
		fi
		size=$(stat --printf='%s' "$file")
		if [[ "$size" -eq "$size_prev" ]]; then
			break
		fi
		size_prev=$size
	done
	if [[ "$size_initial" -eq "$size_prev" ]]; then
		echo "No size reduction"
	else
		size_initial_ie=$(numfmt --to=iec-i "$size_initial")B
		size_final_ie=$(numfmt --to=iec-i "$size_prev")B
		echo "Size reduced from $size_initial_ie to $size_final_ie"
	fi
}

# Shrink only images which are small enough
pngs_shrink() {
	threshold=$1
	shift
	for f in $@; do
		num_pixels=$(identify -format '%[fx:w*h]' "$f")
		if ((num_pixels < threshold)); then
			echo "$f"
			png_shrink "$f"
		fi
	done
}

changed_files=$(git diff --name-only master...)
pngs_shrink 1000 $changed_files

@Emojigit
Copy link
Contributor

Even if the git repository size is larger, the release sizes will be smaller. From an end-user point of view, I think it is good to have the textures compressed.

@grorp
Copy link
Member

grorp commented Oct 3, 2023

Since you wrote "minetest_game optimizer", I assume you used utils/optimize_textures.sh from the Minetest Game repo for your PR.

If I do the same, I get slightly different results. Here's a commit comparing my results with yours: d714c09

OptiPNG version 0.7.7
Using libpng version 1.6.37 and zlib version 1.2.12

Also, this PR probably needs an update because new PNG files have been added to the repo in the meantime.

@Bituvo
Copy link
Contributor Author

Bituvo commented Oct 4, 2023

Will do.

@sfan5 sfan5 merged commit 352a403 into minetest:master Oct 9, 2023
@Bituvo Bituvo deleted the optimized_images branch October 9, 2023 18:03
@ExeVirus
Copy link
Contributor

hate to bring this up, I wrote the optimize script a while back - just wanted to clarify a few things:

  1. gorp: that should be exhaustive search, not heuristic based, so the only reason that you would get different results is possibly optipng version
  2. HybridDog: careful - I had to modify the optipng (tried advpng, got same results) and how I do my script to ignore certain kinds of optimizations that break rendering of certain textures on AMD built-in GPUs. Really painful bug for some, really hard for us to test so we just avoid it by trusting that script haha.

kawogi pushed a commit to kawogi/minetest that referenced this pull request Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Maintenance Tasks to keep the codebase and related parts in order, including architectural improvements One approval ✅ ◻️ Textures Trivial The change is a trivial bug fix, documentation or maintenance change, as per the Git Guidelines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet