Index: ImageMagick-6.4.3/magick/quantum.h
===================================================================
--- ImageMagick-6.4.3.orig/magick/quantum.h	2008-06-14 03:03:16.000000000 +0200
+++ ImageMagick-6.4.3/magick/quantum.h	2020-12-11 16:34:49.401469782 +0100
@@ -22,8 +22,11 @@
 extern "C" {
 #endif
 
+#include <float.h>
 #include "magick/semaphore.h"
 
+#define isNaN(a) ((a) != (a) ? MagickTrue : MagickFalse)
+
 typedef enum
 {
   UndefinedEndian,
@@ -93,7 +96,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(0UL);
   if (quantum >= 255.0)
     return(255);
@@ -106,7 +109,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);
@@ -120,7 +123,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);
Index: ImageMagick-6.4.3/magick/quantum-import.c
===================================================================
--- ImageMagick-6.4.3.orig/magick/quantum-import.c	2008-06-14 03:03:16.000000000 +0200
+++ ImageMagick-6.4.3/magick/quantum-import.c	2020-12-11 16:22:57.509053703 +0100
@@ -115,12 +115,27 @@ static inline IndexPacket PushColormapIn
 }
 
 static inline const unsigned char *PushDoublePixel(
-  const QuantumState *quantum_state,const unsigned char *pixels,double *pixel)
+  const QuantumState *quantum_state,const unsigned char *pixels,
+  double *pixel)
 {
+  double
+    *p;
+
   unsigned char
     quantum[8];
 
-  if (quantum_state->endian != LSBEndian)
+  if (quantum_state->endian == LSBEndian)
+    {
+      quantum[0]=(*pixels++);
+      quantum[1]=(*pixels++);
+      quantum[2]=(*pixels++);
+      quantum[3]=(*pixels++);
+      quantum[4]=(*pixels++);
+      quantum[5]=(*pixels++);
+      quantum[6]=(*pixels++);
+      quantum[7]=(*pixels++);
+    }
+  else
     {
       quantum[7]=(*pixels++);
       quantum[6]=(*pixels++);
@@ -130,49 +145,47 @@ static inline const unsigned char *PushD
       quantum[2]=(*pixels++);
       quantum[1]=(*pixels++);
       quantum[0]=(*pixels++);
-      *pixel=(*((double *) quantum));
-      *pixel-=quantum_state->minimum;
-      *pixel*=quantum_state->scale;
-      return(pixels);
     }
-  quantum[0]=(*pixels++);
-  quantum[1]=(*pixels++);
-  quantum[2]=(*pixels++);
-  quantum[3]=(*pixels++);
-  quantum[4]=(*pixels++);
-  quantum[5]=(*pixels++);
-  quantum[6]=(*pixels++);
-  quantum[7]=(*pixels++);
-  *pixel=(*((double *) quantum));
+  p=(double *) quantum;
+  *pixel=(*p);
   *pixel-=quantum_state->minimum;
   *pixel*=quantum_state->scale;
   return(pixels);
 }
 
 static inline const unsigned char *PushFloatPixel(
-  const QuantumState *quantum_state,const unsigned char *pixels,float *pixel)
+  const QuantumState *quantum_state,const unsigned char *restrict pixels,
+  float *pixel)
 {
+  float
+    *p;
+
   unsigned char
     quantum[4];
 
-  if (quantum_state->endian != LSBEndian)
+  if (quantum_state->endian == LSBEndian)
     {
-      quantum[3]=(*pixels++);
-      quantum[2]=(*pixels++);
-      quantum[1]=(*pixels++);
       quantum[0]=(*pixels++);
-      *pixel=(*((float *) quantum));
-      *pixel-=quantum_state->minimum;
-      *pixel*=quantum_state->scale;
-      return(pixels);
-    }
-  quantum[0]=(*pixels++);
-  quantum[1]=(*pixels++);
-  quantum[2]=(*pixels++);
-  quantum[3]=(*pixels++);
-  *pixel=(*((float *) quantum));
+      quantum[1]=(*pixels++);
+      quantum[2]=(*pixels++);
+      quantum[3]=(*pixels++);
+     }
+   else
+     {
+       quantum[3]=(*pixels++);
+       quantum[2]=(*pixels++);
+       quantum[1]=(*pixels++);
+       quantum[0]=(*pixels++);
+     }
+  p=(float *) quantum;
+  *pixel=(*p);
   *pixel-=quantum_state->minimum;
   *pixel*=quantum_state->scale;
+  if (*pixel < FLT_MIN)
+    *pixel=FLT_MIN;
+  else
+    if (*pixel > FLT_MAX)
+      *pixel=FLT_MAX;
   return(pixels);
 }
 
Index: ImageMagick-6.4.3/magick/quantum-private.h
===================================================================
--- ImageMagick-6.4.3.orig/magick/quantum-private.h	2008-08-28 23:27:06.000000000 +0200
+++ ImageMagick-6.4.3/magick/quantum-private.h	2020-12-11 16:35:31.105728494 +0100
@@ -22,6 +22,8 @@
 extern "C" {
 #endif
 
+#define IsNaN(a) ((a) != (a) ? MagickTrue : MagickFalse)
+
 typedef struct _QuantumState
 {
   EndianType
@@ -250,8 +252,8 @@ static inline unsigned long ScaleQuantum
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned long) (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 long) (16843009.0*quantum+0.5));
@@ -260,13 +262,13 @@ static inline unsigned long ScaleQuantum
 
 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
 {
-  if (quantum <= 0)
-    return(0UL);
   if (quantum >= (Quantum) MaxMap)
     return((unsigned long) MaxMap);
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned long) quantum);
 #else
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+    return(0UL);
   return((unsigned long) (quantum+0.5));
 #endif
 }
@@ -276,7 +278,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);
@@ -330,7 +332,7 @@ static inline unsigned long ScaleQuantum
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned long) (65537UL*quantum));
 #else
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0UL);
   if ((65537.0*quantum) >= 4294967295.0)
     return(4294967295UL);
@@ -340,13 +342,13 @@ static inline unsigned long ScaleQuantum
 
 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
 {
-  if (quantum <= 0)
-    return(0);
   if (quantum >= (Quantum) MaxMap)
     return((unsigned long) MaxMap);
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   return((unsigned long) quantum);
 #else
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
+    return(0UL);
   return((unsigned long) (quantum+0.5));
 #endif
 }
@@ -356,7 +358,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);
@@ -403,7 +405,7 @@ static inline unsigned long ScaleQuantum
 
 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
 {
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if ((quantum/65537.0) >= MaxMap)
     return((unsigned long) MaxMap);
@@ -421,7 +423,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);
@@ -480,7 +482,7 @@ static inline unsigned long ScaleQuantum
 
 static inline unsigned long ScaleQuantumToMap(const Quantum quantum)
 {
-  if (quantum <= 0.0)
+  if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
     return(0);
   if ((quantum/281479271612415.0) >= MaxMap)
     return((unsigned long) MaxMap);
