From 89cd11f012ee5322746e307efd3089dcfc9fa129 Mon Sep 17 00:00:00 2001
From: wang_yue111 <648774160@qq.com>
Date: Wed, 3 Mar 2021 10:38:53 +0800
Subject: [PATCH] https://github.com/ImageMagick/ImageMagick/issues/1727

---
 magick/quantum-private.h | 12 +++++++++---
 magick/quantum.c         |  2 +-
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/magick/quantum-private.h b/magick/quantum-private.h
index 6654c1d..7d51397 100644
--- a/magick/quantum-private.h
+++ b/magick/quantum-private.h
@@ -324,9 +324,15 @@ static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum,
 static inline QuantumAny ScaleQuantumToAny(const Quantum quantum,
   const QuantumAny range)
 {
-  if (quantum < 0)
-    return((QuantumAny) 0);
-  return((QuantumAny) (((MagickRealType) range*quantum)/QuantumRange+0.5));
+#if !defined(MAGICKCORE_HDRI_SUPPORT)
+  return((QuantumAny) ((MagickRealType) range*quantum/QuantumRange));
+#else
+  if (quantum <= 0.0)
+    return((QuantumAny) 0UL);
+  if (((MagickRealType) range*quantum/QuantumRange) >= 18446744073709551615.0)
+    return((QuantumAny) MagickULLConstant(18446744073709551615));
+  return((QuantumAny) ((MagickRealType) range*quantum/QuantumRange+0.5));
+#endif
 }
 
 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
diff --git a/magick/quantum.c b/magick/quantum.c
index 8edd7e3..37ae6db 100644
--- a/magick/quantum.c
+++ b/magick/quantum.c
@@ -674,7 +674,7 @@ MagickExport MagickBooleanType SetQuantumDepth(const Image *image,
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   assert(quantum_info != (QuantumInfo *) NULL);
   assert(quantum_info->signature == MagickCoreSignature);
-  quantum_info->depth=depth;
+  quantum_info->depth=MagickMin(depth,64);
   if (quantum_info->format == FloatingPointQuantumFormat)
     {
       if (quantum_info->depth > 32)
-- 
2.23.0

