GraphicsMagick: HEIF: Support reading ICC color profile.

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.20024.1668957947.1350.graphicsmagick-commit@lists.sourceforge.net>
changeset ceb64ec13c98 in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=ceb64ec13c98
summary: HEIF: Support reading ICC color profile.

diffstat:

 ChangeLog                              |   5 ++
 VisualMagick/installer/inc/version.isx |   4 +-
 coders/heif.c                          |  81 ++++++++++++++++++++++++++++++++++
 magick/version.h                       |   4 +-
 www/Changelog.html                     |  11 ++++
 5 files changed, 101 insertions(+), 4 deletions(-)

diffs (169 lines):

diff -r 1976d2fdce7d -r ceb64ec13c98 ChangeLog
--- a/ChangeLog	Sun Nov 20 12:25:33 2022 +0100
+++ b/ChangeLog	Sun Nov 20 09:25:30 2022 -0600
@@ -1,3 +1,8 @@
+2022-11-20  Bob Friesenhahn  <[email protected]>
+
+	* coders/heif.c (ReadColorProfile): Support reading ICC color
+	profile.
+
 2022-11-20 Fojtik Jaroslav  <[email protected]>
 
         coders/wpg.c Reveal more internal info to optional log.
diff -r 1976d2fdce7d -r ceb64ec13c98 VisualMagick/installer/inc/version.isx
--- a/VisualMagick/installer/inc/version.isx	Sun Nov 20 12:25:33 2022 +0100
+++ b/VisualMagick/installer/inc/version.isx	Sun Nov 20 09:25:30 2022 -0600
@@ -10,5 +10,5 @@
 
 #define public MagickPackageName "GraphicsMagick"
 #define public MagickPackageVersion "1.4"
-#define public MagickPackageVersionAddendum ".020221119"
-#define public MagickPackageReleaseDate "snapshot-20221119"
+#define public MagickPackageVersionAddendum ".020221120"
+#define public MagickPackageReleaseDate "snapshot-20221120"
diff -r 1976d2fdce7d -r ceb64ec13c98 coders/heif.c
--- a/coders/heif.c	Sun Nov 20 12:25:33 2022 +0100
+++ b/coders/heif.c	Sun Nov 20 09:25:30 2022 -0600
@@ -133,6 +133,9 @@
       ThrowReaderException(code_,reason_,image_);      \
     } while (0);
 
+/*
+  Read metadata (Exif and XMP)
+*/
 static Image *ReadMetadata(struct heif_image_handle *heif_image_handle,
                            Image *image, ExceptionInfo *exception)
 {
@@ -237,6 +240,76 @@
   return image;
 }
 
+
+/*
+  Read Color Profile
+*/
+static Image *ReadColorProfile(struct heif_image_handle *heif_image_handle,
+                               Image *image, ExceptionInfo *exception)
+{
+  struct heif_error
+    err;
+
+  enum heif_color_profile_type
+    profile_type; /* 4 chars encoded into enum by 'heif_fourcc()' */
+
+  unsigned char
+    *profile;
+
+  profile_type = heif_image_handle_get_color_profile_type(heif_image_handle);
+
+  if (heif_color_profile_type_not_present == profile_type)
+    return image;
+
+  if (image->logging && (heif_color_profile_type_not_present !=profile_type))
+    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+                          "Found color profile of type \"%c%c%c%c\")",
+                          ((char) ((unsigned int) profile_type >> 24) & 0xff),
+                          ((char) ((unsigned int) profile_type >> 16) & 0xff),
+                          ((char) ((unsigned int) profile_type >> 8) & 0xff),
+                          ((char) ((unsigned int) profile_type) & 0xff));
+
+  if (heif_color_profile_type_prof == profile_type)
+    {
+      size_t profile_size;
+
+      profile_size = heif_image_handle_get_raw_color_profile_size(heif_image_handle);
+
+      if (image->logging)
+        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+                              "Reading ICC profile with size %" MAGICK_SIZE_T_F "u bytes",
+                              (MAGICK_SIZE_T) profile_size);
+
+      if (profile_size > 0)
+        {
+          /* Allocate 'profile' buffer for profile */
+          profile=MagickAllocateResourceLimitedArray(unsigned char*,profile_size,
+                                                     sizeof(*profile));
+
+          if (profile == (unsigned char*) NULL)
+            ThrowReaderException(ResourceLimitError,MemoryAllocationFailed,image);
+
+          /* Copy ICC profile to 'profile' buffer */
+          err = heif_image_handle_get_raw_color_profile(heif_image_handle,
+                                                        profile);
+          if (err.code != heif_error_Ok)
+            {
+              if (image->logging)
+                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
+                                      "heif_image_handle_get_raw_color_profile() reports error \"%s\"",
+                                      err.message);
+              MagickFreeResourceLimitedMemory(profile);
+              ThrowReaderException(CorruptImageError,
+                                   AnErrorHasOccurredReadingFromFile,image);
+
+            }
+          SetImageProfile(image,"ICM",profile,profile_size);
+          MagickFreeResourceLimitedMemory(profile);
+        }
+    }
+  return image;
+}
+
 /*
   This progress monitor implementation is tentative since it is not invoked
 
@@ -416,12 +489,20 @@
                             "Matte: %s", image->matte ? "True" : "False");
     }
 
+  /* Read EXIF and XMP profile */
   if (!ReadMetadata(heif_image_handle, image, exception))
     {
       HEIFReadCleanup();
       return NULL;
     }
 
+  /* Read ICC profile */
+  if (!ReadColorProfile(heif_image_handle, image, exception))
+    {
+      HEIFReadCleanup();
+      return NULL;
+    }
+
   if (image_info->ping)
     {
       image->depth = 8;
diff -r 1976d2fdce7d -r ceb64ec13c98 magick/version.h
--- a/magick/version.h	Sun Nov 20 12:25:33 2022 +0100
+++ b/magick/version.h	Sun Nov 20 09:25:30 2022 -0600
@@ -38,8 +38,8 @@
 #define MagickLibVersion  0x272400
 #define MagickLibVersionText  "1.4"
 #define MagickLibVersionNumber 27,24,0
-#define MagickChangeDate   "20221119"
-#define MagickReleaseDate  "snapshot-20221119"
+#define MagickChangeDate   "20221120"
+#define MagickReleaseDate  "snapshot-20221120"
 
 /*
   The MagickLibInterfaceNewest and MagickLibInterfaceOldest defines
diff -r 1976d2fdce7d -r ceb64ec13c98 www/Changelog.html
--- a/www/Changelog.html	Sun Nov 20 12:25:33 2022 +0100
+++ b/www/Changelog.html	Sun Nov 20 09:25:30 2022 -0600
@@ -37,6 +37,17 @@
 </div>
 
 <div class="document">
+<p>2022-11-20  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
+<blockquote>
+<ul class="simple">
+<li><p>coders/heif.c (ReadColorProfile): Support reading ICC color
+profile.</p></li>
+</ul>
+</blockquote>
+<p>2022-11-20 Fojtik Jaroslav  &lt;<a class="reference external" href="mailto:JaFojtik&#37;&#52;&#48;yandex&#46;com">JaFojtik<span>&#64;</span>yandex<span>&#46;</span>com</a>&gt;</p>
+<blockquote>
+<p>coders/wpg.c Reveal more internal info to optional log.</p>
+</blockquote>
 <p>2022-11-19  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
 <blockquote>
 <ul class="simple">
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.