Index: ImageMagick-6.8.8-1/magick/quantum.h
===================================================================
--- ImageMagick-6.8.8-1.orig/magick/quantum.h	2013-11-27 20:02:59.000000000 +0100
+++ ImageMagick-6.8.8-1/magick/quantum.h	2020-12-11 15:10:21.982208775 +0100
@@ -18,8 +18,10 @@
 #ifndef _MAGICKCORE_QUANTUM_H
 #define _MAGICKCORE_QUANTUM_H
 
+#include <float.h>
 #include "magick/image.h"
 #include "magick/semaphore.h"
+#include "magick/image-private.h"
 
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
@@ -84,16 +86,16 @@ typedef enum
 typedef struct _QuantumInfo
   QuantumInfo;
 
-static inline Quantum ClampToQuantum(const MagickRealType value)
+static inline Quantum ClampToQuantum(const MagickRealType quantum)
 {
 #if defined(MAGICKCORE_HDRI_SUPPORT)
-  return((Quantum) value);
+  return((Quantum) quantum);
 #else
-  if (value <= 0.0f)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return((Quantum) 0);
-  if (value >= (MagickRealType) QuantumRange)
+  if (quantum >= (MagickRealType) QuantumRange)
     return(QuantumRange);
-  return((Quantum) (value+0.5f));
+  return((Quantum) (quantum+0.5));
 #endif
 }
 
@@ -103,7 +105,7 @@ static inline unsigned char ScaleQuantum
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned char) quantum);
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if (quantum >= 255.0)
     return(255);
@@ -116,7 +118,7 @@ static inline unsigned char ScaleQuantum
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned char) (((quantum+128UL)-((quantum+128UL) >> 8)) >> 8));
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if ((quantum/257.0) >= 255.0)
     return(255);
@@ -130,7 +132,7 @@ static inline unsigned char ScaleQuantum
   return((unsigned char) ((quantum+MagickULLConstant(8421504))/
     MagickULLConstant(16843009)));
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if ((quantum/16843009.0) >= 255.0)
     return(255);
@@ -143,7 +145,7 @@ static inline unsigned char ScaleQuantum
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned char) (quantum/72340172838076673.0+0.5));
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if ((quantum/72340172838076673.0) >= 255.0)
     return(255);
Index: ImageMagick-6.8.8-1/magick/quantum-import.c
===================================================================
--- ImageMagick-6.8.8-1.orig/magick/quantum-import.c	2020-12-11 15:10:18.502187351 +0100
+++ ImageMagick-6.8.8-1/magick/quantum-import.c	2020-12-11 15:13:33.855389987 +0100
@@ -139,20 +139,18 @@ static inline const unsigned char *PushD
       quantum[5]=(*pixels++);
       quantum[6]=(*pixels++);
       quantum[7]=(*pixels++);
-      p=(double *) quantum;
-      *pixel=(*p);
-      *pixel-=quantum_info->minimum;
-      *pixel*=quantum_info->scale;
-      return(pixels);
     }
-  quantum[7]=(*pixels++);
-  quantum[6]=(*pixels++);
-  quantum[5]=(*pixels++);
-  quantum[5]=(*pixels++);
-  quantum[3]=(*pixels++);
-  quantum[2]=(*pixels++);
-  quantum[1]=(*pixels++);
-  quantum[0]=(*pixels++);
+  else
+    {
+      quantum[7]=(*pixels++);
+      quantum[6]=(*pixels++);
+      quantum[5]=(*pixels++);
+      quantum[5]=(*pixels++);
+      quantum[3]=(*pixels++);
+      quantum[2]=(*pixels++);
+      quantum[1]=(*pixels++);
+      quantum[0]=(*pixels++);
+    }
   p=(double *) quantum;
   *pixel=(*p);
   *pixel-=quantum_info->minimum;
@@ -176,20 +174,23 @@ static inline const unsigned char *PushF
       quantum[1]=(*pixels++);
       quantum[2]=(*pixels++);
       quantum[3]=(*pixels++);
-      p=(float *) quantum;
-      *pixel=(*p);
-      *pixel-=quantum_info->minimum;
-      *pixel*=quantum_info->scale;
-      return(pixels);
-    }
-  quantum[3]=(*pixels++);
-  quantum[2]=(*pixels++);
-  quantum[1]=(*pixels++);
-  quantum[0]=(*pixels++);
+     }
+   else
+     {
+       quantum[3]=(*pixels++);
+       quantum[2]=(*pixels++);
+       quantum[1]=(*pixels++);
+       quantum[0]=(*pixels++);
+     }
   p=(float *) quantum;
   *pixel=(*p);
   *pixel-=quantum_info->minimum;
   *pixel*=quantum_info->scale;
+  if (*pixel < FLT_MIN)
+    *pixel=FLT_MIN;
+  else
+    if (*pixel > FLT_MAX)
+      *pixel=FLT_MAX;
   return(pixels);
 }
 
Index: ImageMagick-6.8.8-1/magick/quantum-private.h
===================================================================
--- ImageMagick-6.8.8-1.orig/magick/quantum-private.h	2013-11-27 20:02:59.000000000 +0100
+++ ImageMagick-6.8.8-1/magick/quantum-private.h	2020-12-11 15:10:21.986208800 +0100
@@ -19,6 +19,7 @@
 #define _MAGICKCORE_QUANTUM_PRIVATE_H
 
 #include "magick/cache.h"
+#include "magick/image-private.h"
 
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
@@ -268,20 +269,42 @@ static inline const unsigned char *PushS
   return(pixels);
 }
 
+static inline double PerceptibleReciprocal(const double x)
+{
+  double
+    sign;
+
+  /*
+    Return 1/x where x is perceptible (not unlimited or infinitesimal).
+  */
+  sign=x < 0.0 ? -1.0 : 1.0;
+  if ((sign*x) >= MagickEpsilon)
+    return(1.0/x);
+  return(sign/MagickEpsilon);
+}
+
 static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum,
   const QuantumAny range)
 {
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
-  return((Quantum) (((MagickRealType) QuantumRange*quantum)/range+0.5));
+  return((Quantum) ((double) QuantumRange*(quantum*PerceptibleReciprocal((double) range))+0.5));
 #else
-  return((Quantum) (((MagickRealType) QuantumRange*quantum)/range));
+  return((Quantum) ((double) QuantumRange*(quantum*PerceptibleReciprocal((double) range))));
 #endif
 }
 
 static inline QuantumAny ScaleQuantumToAny(const Quantum quantum,
   const QuantumAny range)
 {
-  return((QuantumAny) (((MagickRealType) range*quantum)/QuantumRange+0.5));
+#if !defined(MAGICKCORE_HDRI_SUPPORT)
+  return((QuantumAny) ((double) range*quantum/QuantumRange));
+#else
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+    return((QuantumAny) 0UL);
+  if (((double) range*quantum/QuantumRange) >= 18446744073709551615.0)
+    return((QuantumAny) MagickULLConstant(18446744073709551615));
+  return((QuantumAny) ((double) range*quantum/QuantumRange+0.5));
+#endif
 }
 
 #if (MAGICKCORE_QUANTUM_DEPTH == 8)
@@ -317,8 +340,8 @@ static inline unsigned int ScaleQuantumT
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned int) (16843009UL*quantum));
 #else
-  if (quantum <= 0.0)
-    return(0UL);
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+    return(0U);
   if ((16843009.0*quantum) >= 4294967295.0)
     return(4294967295UL);
   return((unsigned int) (16843009.0*quantum+0.5));
@@ -332,8 +355,8 @@ static inline unsigned int ScaleQuantumT
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned int) quantum);
 #else
-  if (quantum < 0.0)
-    return(0UL);
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+    return(0U);
   return((unsigned int) (quantum+0.5));
 #endif
 }
@@ -343,7 +366,7 @@ static inline unsigned short ScaleQuantu
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned short) (257UL*quantum));
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if ((257.0*quantum) >= 65535.0)
     return(65535);
@@ -397,7 +420,7 @@ static inline unsigned int ScaleQuantumT
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned int) (65537UL*quantum));
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0UL);
   if ((65537.0*quantum) >= 4294967295.0)
     return(4294967295U);
@@ -412,8 +435,8 @@ static inline unsigned int ScaleQuantumT
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned int) quantum);
 #else
-  if (quantum < 0.0)
-    return(0UL);
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+    return(0U);
   return((unsigned int) (quantum+0.5));
 #endif
 }
@@ -423,7 +446,7 @@ static inline unsigned short ScaleQuantu
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned short) quantum);
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if (quantum >= 65535.0)
     return(65535);
@@ -474,14 +497,14 @@ static inline unsigned int ScaleQuantumT
 
 static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
 {
-  if (quantum < 0.0)
-    return(0UL);
   if ((quantum/65537) >= (Quantum) MaxMap)
     return((unsigned int) MaxMap);
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned int) ((quantum+MagickULLConstant(32768))/
     MagickULLConstant(65537)));
 #else
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+    return(0U);
   return((unsigned int) (quantum/65537.0+0.5));
 #endif
 }
@@ -492,7 +515,7 @@ static inline unsigned short ScaleQuantu
   return((unsigned short) ((quantum+MagickULLConstant(32768))/
     MagickULLConstant(65537)));
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if ((quantum/65537.0) >= 65535.0)
     return(65535);
@@ -535,8 +558,8 @@ static inline unsigned int ScaleQuantumT
 
 static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
 {
-  if (quantum <= 0.0)
-    return(0UL);
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+    return(0U);
   if ((quantum/281479271743489.0) >= MaxMap)
     return((unsigned int) MaxMap);
   return((unsigned int) (quantum/281479271743489.0+0.5));
@@ -544,7 +567,7 @@ static inline unsigned int ScaleQuantumT
 
 static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
 {
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if ((quantum/281479271743489.0) >= 65535.0)
     return(65535);
Index: ImageMagick-6.8.8-1/magick/pixel-private.h
===================================================================
--- ImageMagick-6.8.8-1.orig/magick/pixel-private.h	2013-11-27 20:02:59.000000000 +0100
+++ ImageMagick-6.8.8-1/magick/pixel-private.h	2020-12-11 15:11:14.974535007 +0100
@@ -75,20 +75,6 @@ static inline MagickBooleanType IsMonoch
   return(MagickFalse);
 }
 
-static inline double PerceptibleReciprocal(const double x)
-{
-  double
-    sign;
-
-  /*
-    Return 1/x where x is perceptible (not unlimited or infinitesimal).
-  */
-  sign=x < 0.0 ? -1.0 : 1.0;
-  if ((sign*x) >= MagickEpsilon)
-    return(1.0/x);
-  return(sign/MagickEpsilon);
-}
-
 static inline void SetMagickPixelPacket(const Image *image,
   const PixelPacket *color,const IndexPacket *index,MagickPixelPacket *pixel)
 {
