I'm trying to load a PNG file using SDL but the program doesn't work and this error appears in the console
"libpng warning: iCCP: known incorrect sRGB profile"
Why this warning appears? what should I do to solve this problem?
I'm trying to load a PNG file using SDL but the program doesn't work and this error appears in the console
"libpng warning: iCCP: known incorrect sRGB profile"
Why this warning appears? what should I do to solve this problem?
Libpng-1.6 is more stringent about checking ICC profiles than previous versions. You can ignore the warning. To get rid of it, remove the iCCP chunk from the PNG image.
Some applications treat warnings as errors; if you are using such an application you do have to remove the chunk. You can do that with any of a variety of PNG editors such as ImageMagick's convert in.png out.png
.
To remove the invalid iCCP chunk from all of the PNG files in a folder (directory), you can use ImageMagick's mogrify *.png
, provided that your ImageMagick was built with libpng16 (run convert -list format | grep PNG
to be sure of that).
If you'd like to find out which files need to be fixed instead of blindly processing all of them, you can run my pngcrush -n -q *.png
where the "-n
" means don't rewrite the files and "-q
" means suppress most of the output except for warnings. Sorry, there's no option yet in pngcrush to suppress everything but the warnings.
Binary Releases of ImageMagick are here
For Android Projects (Android Studio) navigate into res folder.
For example: C:\AndroidProjects\{your_project_folder}\app\src\main\res\drawable-hdpi\mogrify *.png
Use pngcrush
to remove the incorrect sRGB profile from the png file:
pngcrush -ow -rem allb -reduce file.png
-ow
will overwrite the input file-rem allb
will remove all ancillary chunks except tRNS and gAMA-reduce
does lossless color-type or bit-depth reductionIn the console output you should see Removed the sRGB chunk.
, and possibly more messages about chunk removals. You will end up with a smaller, optimized png file. As the command will overwrite the original file, make sure to create a backup or use version control.
Thanks to the fantastic answer from Glenn, I used ImageMagik's "mogrify *.png" functionality. However, I had images buried in sub-folders, so I used this simple Python script to apply this to all images in all sub-folders and thought it might help others:
import os import subprocess def system_call(args, cwd="."): print("Running '{}' in '{}'".format(str(args), cwd)) subprocess.call(args, cwd=cwd) pass def fix_image_files(root=os.curdir): for path, dirs, files in os.walk(os.path.abspath(root)): # sys.stdout.write('.') for dir in dirs: system_call("mogrify *.png", "{}".format(os.path.join(path, dir))) fix_image_files(os.curdir)
To add to Glenn's great answer, here's what I did to find which files were faulty:
find . -name "*.png" -type f -print0 | xargs -0 pngcrush_1_8_8_w64.exe -n -q > pngError.txt 2>&1
I used the find and xargs because pngcrush could not handle lots of arguments (which were returned by **/*.png
). The -print0
and -0
is required to handle file names containing spaces.
Then search in the output for these lines: iCCP: Not recognizing known sRGB profile that has been edited
.
./Installer/Images/installer_background.png:
Total length of data found in critical chunks = 11286
pngcrush: iCCP: Not recognizing known sRGB profile that has been edited
And for each of those, run mogrify on it to fix them.
mogrify ./Installer/Images/installer_background.png
Doing this prevents having a commit changing every single png file in the repository when only a few have actually been modified. Plus it has the advantage to show exactly which files were faulty.
I tested this on Windows with a Cygwin console and a zsh shell. Thanks again to Glenn who put most of the above, I'm just adding an answer as it's usually easier to find than comments :)
Using IrfanView image viewer in Windows, I simply resaved the PNG image and that corrected the problem.
You can also just fix this in photoshop... I've got CC2015 but I'm sure this is the same for all versions.
There is an easier way to fix this issue with Mac OS with Homebrew:
-> install homebrew if it is not installed yet $brew install libpng $pngfix --strip=color --out=file2.png file.png
or to do it with every file in the current directory:
mkdir tmp; for f in ./*.png; do pngfix --strip=color --out=tmp/"$f" "$f"; done
It will create a fixed copy for each png file in the current directory and put it in the the tmp subdirectory. After that, if everything is OK, you just need to override the original files.
Another tip is to use the Keynote and Preview applications to create the icons. I draw them using Keynote, in the size of about 120x120 pixels, over a slide with a white background (the option to make polygons editable is great!). Before exporting to Preview, I draw a rectangle around the icon (without any fill or shadow, just the outline, with the size of about 135x135) and copy everything to the clipboard. After that, you just need to open it with the Preview tool using "New from Clipboard", select a 128x128 pixels area around the icon, copy, use "New from Clipboard" again, and export it to PNG. You won't need to run the pngfix tool.
After trying a couple of the suggestions on this page I ended up using the pngcrush solution. You can use the bash script below to recursively detect and fix bad png profiles. Just pass it the full path to the directory you want to search for png files.
fixpng "/path/to/png/folder"
The script:
#!/bin/bash FILES=$(find "$1" -type f -iname '*.png') FIXED=0 for f in $FILES; do WARN=$(pngcrush -n -warn "$f" 2>&1) if [[ "$WARN" == *"PCS illuminant is not D50"* ]] || [[ "$WARN" == *"known incorrect sRGB profile"* ]]; then pngcrush -s -ow -rem allb -reduce "$f" FIXED=$((FIXED + 1)) fi done echo "$FIXED errors fixed"
some background info on this:
Some changes in libpng version 1.6+ cause it to issue a warning or even not work correctly with the original HP/MS sRGB profile, leading to the following stderr: libpng warning: iCCP: known incorrect sRGB profile The old profile uses a D50 whitepoint, where D65 is standard. This profile is not uncommon, being used by Adobe Photoshop, although it was not embedded into images by default.
(source: https://wiki.archlinux.org/index.php/Libpng_errors)
Error detection in some chunks has improved; in particular the iCCP chunk reader now does pretty complete validation of the basic format. Some bad profiles that were previously accepted are now rejected, in particular the very old broken Microsoft/HP sRGB profile. The PNG spec requirement that only grayscale profiles may appear in images with color type 0 or 4 and that even if the image only contains gray pixels, only RGB profiles may appear in images with color type 2, 3, or 6, is now enforced. The sRGB chunk is allowed to appear in images with any color type.
Here is a ridiculously brute force answer:
I modified the gradlew script. Here is my new exec command at the end of the file in the
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" **| grep -v "libpng warning:"**