Skip to content

Correctly detect if image format is supported by QImageWriter

David Edmundson requested to merge davidedmundson/qtwayland:kde/5.15 into kde/5.15

The code queries potential image formats by stripping a mimetype of its 'image/' prefix and making the rest of the mimetype capitalised, such as 'image/png' -> 'PNG'. The problem is that this is then searched for in QImageWriter::supportedImageFormats() by simple equality. The method returns a list of lowercase byte arrays, not uppercase. As the codepath can never match due to checking for an uppercase word in an array of lowercase words, this means that images are effectively always sent as BMP format, even if they should be sent in other formats, such as PNG or JPEG.

A simple inspection with GDB (or a qDebug) reveals this:

(gdb) p QImageWriter::supportedImageFormats()
$31 = {"bmp" = {...}, "bw" = {...}, "cur" = {...}, "eps" = {...},
  "epsf" = {...}, "epsi" = {...}, "icns" = {...},
  "ico" = {...}, "jp2" = {...}, "jpeg" = {...}, "jpg" = {...},
  "pbm" = {...}, "pcx" = {...}, "pgm" = {...},
  "pic" = {...}, "png" = {...}, "ppm" = {...},
  "rgb" = {...}, "rgba" = {...}, "sgi" = {...},
  "tga" = {...}, "tif" = {...}, "tiff" = {...},
  "wbmp" = {...}, "webp" = {...}, "xbm" = {...}, "xpm" = {...}}
(gdb) p QImageWriter::supportedImageFormats().contains("PNG")
$32 = false
(gdb) p QImageWriter::supportedImageFormats().contains("png")
$33 = true

The fix for this is simple: lowercase the remainder of the mimetype, instead of uppercasing it, and we can start hitting the codepath that's supposed to write non-BMP formats.

Change-Id: Id3e9b730b7edcabcb2f1b04d8ef0a4c1fb9c9159 Reviewed-by: David Edmundson davidedmundson@kde.org Reviewed-by: Qt CI Bot qt_ci_bot@qt-project.org (cherry picked from commit 6072c1dc)

Merge request reports