From 3288ae60b96efa404fbeb40598a8567bc8c75761 Mon Sep 17 00:00:00 2001
From: Markus Diem <diemmarkus@gmail.com>
Date: Mon, 22 Jun 2020 11:12:12 +0200
Subject: [PATCH] maps color format from HEIF to Qt (i.e. RGB88)

- this patch is needed to load RGB888 heif images with libheif 1.7.0
- maps heif_chroma_interleaved_RGB to QImage::Format_RGB888
- other conversions are not added because I don't have test images
---
 src/qheifhandler.cpp | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/qheifhandler.cpp b/src/qheifhandler.cpp
index e1b9933..822c45d 100644
--- a/src/qheifhandler.cpp
+++ b/src/qheifhandler.cpp
@@ -288,13 +288,31 @@ bool QHeifHandler::read(QImage* destImage)
         qWarning("QHeifHandler::read() invalid stride: %d", stride);
         return false;
     }
+    
+    // map image format
+    heif_chroma heifFormat = heif_image_get_chroma_format(srcImage.get());
+    QImage::Format qtFormat;
 
+    switch (heifFormat) {
+    case heif_chroma_interleaved_RGB: {
+        qtFormat = QImage::Format_RGB888;
+        break;
+    }
+    case heif_chroma_interleaved_RGBA: {
+        qtFormat = QImage::Format_RGBA8888;
+        break;
+    }
+    // TODO: add other formats i.e. heif_chroma_monochrome  here
+    default:
+        qtFormat = QImage::Format_RGBA8888;
+    }
+    
     // move data ownership to QImage
     heif_image* dataImage = srcImage.release();
 
     *destImage = QImage(
         data, imgSize.width(), imgSize.height(),
-        stride, QImage::Format_RGBA8888,
+        stride, qtFormat,
         [](void* img) { heif_image_release(static_cast<heif_image*>(img)); },
         dataImage
     );
