Qt QImage 加载 BMP 图像的一个BUG

这个问题源于水木社区的一个帖子:https://www.mysmth.net/nForum/#!article/KDE_Qt/27466

经过测试 QImage 加载像素数大于 16384*16384 的图像会失败。

查看Qt 源代码可以看到:

static bool read_dib_infoheader(QDataStream &s, BMP_INFOHDR &bi)
{
     s >> bi;                                        // read BMP info header
     if (s.status() != QDataStream::Ok)
         return false;
  
     int nbits = bi.biBitCount;
     int comp = bi.biCompression;
     if (!(nbits == 1 || nbits == 4 || nbits == 8 || nbits == 16 || nbits == 24 || nbits == 32) ||
         bi.biPlanes != 1 || comp > BMP_BITFIELDS)
         return false;                                        // weird BMP image
     if (!(comp == BMP_RGB || (nbits == 4 && comp == BMP_RLE4) ||
         (nbits == 8 && comp == BMP_RLE8) || ((nbits == 16 || nbits == 32) && comp == BMP_BITFIELDS)))
          return false;                                // weird compression type
     if (bi.biWidth <= 0 || !bi.biHeight || quint64(bi.biWidth) * qAbs(bi.biHeight) > 16384 * 16384)
         return false;
  
     return true;
} 

确实是 quint64(bi.biWidth) * qAbs(bi.biHeight) > 16384 * 16384 时直接返回 false。
非常愚蠢和粗暴的代码。

去查找这个代码是谁提交的,可以看到 Sep 11, 2018 一条提交记录。
https://github.com/qt/qtbase/commit/621ab8ab59901cc3f9bd98be709929c9eac997a8
给出了如下的解释:

bmp image handler: check for out of range image size

Make the decoder fail early to avoid spending time and memory on
attempting to decode a corrupt image file.

Change-Id: I874e04f3b43122d73f8e58c7a5bcc4a741b68264
Reviewed-by: Lars Knoll <lars.knoll@qt.io>

所以在 Qt里,如果你用 QImage 打开 BMP图像,像素数要小于 16384 * 16384 。这个可以说是个 Bug ,也可以说是 QImage 的Feature吧。

如果要加载超过 16384 * 16384 的图像怎么办呢?可以把图像存为 png 格式。这个格式是无损压缩的。QImage 可以成功打开很大的 png 图像。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值