我试图使用SDL加载PNG图像,但程序不工作,这个错误出现在控制台中
libpng警告:iCCP:已知错误的sRGB配置文件
为什么会出现这个警告?我该怎么解决这个问题呢?
我试图使用SDL加载PNG图像,但程序不工作,这个错误出现在控制台中
libpng警告:iCCP:已知错误的sRGB配置文件
为什么会出现这个警告?我该怎么解决这个问题呢?
当前回答
下面是一个荒谬粗暴的回答:
我修改了gradlew脚本。下面是我的新exec命令在文件的末尾
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" **| grep -v "libpng warning:"**
其他回答
一些背景信息:
libpng 1.6+版本中的一些更改导致它发出警告或 甚至不能与原始HP/MS sRGB配置文件正确工作,领先 到以下stderr: libpng警告:iCCP:已知错误的sRGB 旧的配置文件使用D50白点,其中D65是标准的。 这个配置文件并不少见,虽然Adobe Photoshop正在使用 默认情况下,它没有嵌入到图像中。
(来源: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.
(来源:https://forum.qt.io/topic/58638/solved-libpng-warning-iccp-known-incorrect-srgb-profile-drive-me-nuts/16)
解决方案
不正确的配置文件可以通过以下方法修复:
使用QPixmap::load打开带有错误配置文件的图像 使用QPixmap::save将图像保存回磁盘(已经具有正确的配置文件)
注意:此解决方案使用Qt库。
例子
下面是我用c++写的一个最小示例,以演示如何实现建议的解决方案:
QPixmap pixmap;
pixmap.load("badProfileImage.png");
QFile file("goodProfileImage.png");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "PNG");
基于此示例的GUI应用程序的完整源代码可在GitHub上获得。
2019年12月5日更新:答案过去是有效的,现在仍然有效,但是我在GitHub上分享的GUI应用程序中有一个错误,导致输出图像为空。我刚修好,给您带来的不便深表歉意!
在尝试了本页上的几个建议后,我最终使用了pngcrush解决方案。您可以使用下面的bash脚本递归地检测和修复错误的png配置文件。只需要将完整路径传递给你想要搜索png文件的目录。
fixpng "/path/to/png/folder"
脚本:
#!/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"
下面是一个荒谬粗暴的回答:
我修改了gradlew脚本。下面是我的新exec命令在文件的末尾
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" **| grep -v "libpng warning:"**
使用pngcrush从png文件中删除不正确的sRGB配置文件:
pngcrush -ow -rem allb -reduce file.png
-ow将覆盖输入文件 -rem allb将删除除tRNS和gAMA之外的所有辅助块 -reduce用于无损颜色类型或位深度缩减
在控制台输出中,您应该看到Removed the sRGB块,可能还有更多关于块移除的消息。你将得到一个更小的、优化过的PNG文件。由于该命令将覆盖原始文件,请确保创建备份或使用版本控制。