Commit 5da42355a for imagemagick.org

commit 5da42355a609d88e28c496f4d65ed16d38793dfd
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 10:54:49 2026 -0500

    pure-black pixels divided by zero, they now keep their value

diff --git a/MagickCore/accelerate-kernels-private.h b/MagickCore/accelerate-kernels-private.h
index d71bd2e7f..09ec0db7d 100644
--- a/MagickCore/accelerate-kernels-private.h
+++ b/MagickCore/accelerate-kernels-private.h
@@ -28,6 +28,12 @@ extern "C" {
 /*
   Define declarations.
 */
+#if defined(__GNUC__) || defined(__clang__)
+#  define MAGICK_KERNEL_SOURCE_UNUSED  __attribute__((unused))
+#else
+#  define MAGICK_KERNEL_SOURCE_UNUSED
+#endif
+
 #define OPENCL_DEFINE(VAR,...)	"\n #""define " #VAR " " #__VA_ARGS__ " \n"
 #define OPENCL_ELIF(...)	"\n #""elif " #__VA_ARGS__ " \n"
 #define OPENCL_ELSE()		"\n #""else " " \n"
@@ -35,7 +41,7 @@ extern "C" {
 #define OPENCL_IF(...)		"\n #""if " #__VA_ARGS__ " \n"
 #define STRINGIFY(...) #__VA_ARGS__ "\n"

-const char *accelerateKernels =
+static const char *const accelerateKernels MAGICK_KERNEL_SOURCE_UNUSED =

 /*
   Define declarations.
@@ -309,7 +315,7 @@ OPENCL_IF((MAGICKCORE_HDRI_SUPPORT == 1))
   STRINGIFY(
     static inline CLQuantum ClampToQuantum(const float value)
       {
-        return (CLQuantum) clamp(value, 0.0f, QuantumRange);
+        return (CLQuantum) fmin(fmax(value, 0.0f), (float) QuantumRange);
       }
   )

@@ -318,7 +324,7 @@ OPENCL_ELSE()
   STRINGIFY(
     static inline CLQuantum ClampToQuantum(const float value)
       {
-        return (CLQuantum) (clamp(value, 0.0f, QuantumRange) + 0.5f);
+        return (CLQuantum) (fmin(fmax(value, 0.0f), (float) QuantumRange) + 0.5f);
       }
   )

@@ -412,7 +418,7 @@ OPENCL_ENDIF()
   {
     const __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);

-    float4 pixel;
+    float4 pixel=(float4) 0.0f;

     pixel.x=getPixelRed(p);

@@ -498,7 +504,7 @@ OPENCL_ENDIF()
     {
       case AveragePixelIntensityMethod:
         {
-          intensity=(red+green+blue)/3.0;
+          intensity=(red+green+blue)/3.0f;
           break;
         }
       case BrightnessPixelIntensityMethod:
@@ -509,13 +515,13 @@ OPENCL_ENDIF()
       case LightnessPixelIntensityMethod:
         {
           intensity=(MagickMin(MagickMin(red,green),blue)+
-              MagickMax(MagickMax(red,green),blue))/2.0;
+              MagickMax(MagickMax(red,green),blue))/2.0f;
           break;
         }
       case MSPixelIntensityMethod:
         {
           intensity=(float) (((float) red*red+green*green+blue*blue)/
-              (3.0*QuantumRange));
+              (3.0f*QuantumRange));
           break;
         }
       case Rec601LumaPixelIntensityMethod:
@@ -531,7 +537,7 @@ OPENCL_ENDIF()
           if ((red == green) && (green == blue))
             intensity=red;
           else
-            intensity=0.298839*red+0.586811*green+0.114350*blue;
+            intensity=0.298839f*red+0.586811f*green+0.114350f*blue;
           break;
         }
       case Rec601LuminancePixelIntensityMethod:
@@ -547,7 +553,7 @@ OPENCL_ENDIF()
           if ((red == green) && (green == blue))
             intensity=red;
           else
-            intensity=0.298839*red+0.586811*green+0.114350*blue;
+            intensity=0.298839f*red+0.586811f*green+0.114350f*blue;
           break;
         }
       case Rec709LumaPixelIntensityMethod:
@@ -564,7 +570,7 @@ OPENCL_ENDIF()
           if ((red == green) && (green == blue))
             intensity=red;
           else
-            intensity=0.212656*red+0.715158*green+0.072186*blue;
+            intensity=0.212656f*red+0.715158f*green+0.072186f*blue;
           break;
         }
       case Rec709LuminancePixelIntensityMethod:
@@ -580,13 +586,13 @@ OPENCL_ENDIF()
           if ((red == green) && (green == blue))
             intensity=red;
           else
-            intensity=0.212656*red+0.715158*green+0.072186*blue;
+            intensity=0.212656f*red+0.715158f*green+0.072186f*blue;
           break;
         }
       case RMSPixelIntensityMethod:
         {
           intensity=(float) (sqrt((float) red*red+green*green+blue*blue)/
-              sqrt(3.0));
+              sqrt(3.0f));
           break;
         }
     }
@@ -684,7 +690,7 @@ OPENCL_ENDIF()
     // being a bit poor in ones - once you've decided that, one constant is as
     // good as any another. There is no deep mathematical reason for it, I just
     // generated a random number.
-    enum{ MWC_BASEID = 4077358422479273989UL };
+    const ulong MWC_BASEID = 4077358422479273989UL;

     ulong dist=streamBase + (get_global_id(0)*vecSize+vecOffset)*streamGap;
     ulong m=MWC_PowMod64(A, dist, M);
@@ -1466,9 +1472,13 @@ OPENCL_ENDIF()
         bias   = ( number_parameters >= 4 ) ? parameters[3] : 0.5f;

         result = 2.0f/width*(QuantumScale*pixel - center);
-        result = range/MagickPI*asin(result)+bias;
-        result = ( result <= -1.0f ) ? bias - range/2.0f : result;
-        result = ( result >= 1.0f ) ? bias + range/2.0f : result;
+        if (result <= -1.0f)
+          result = bias - range/2.0f;
+        else
+          if (result >= 1.0f)
+            result = bias + range/2.0f;
+          else
+            result = range/MagickPI*asin(result)+bias;
         result *= QuantumRange;
         break;
       }
@@ -1507,10 +1517,10 @@ OPENCL_ENDIF()
     const unsigned int columns = get_global_size(0);
     __global CLQuantum *p = image + getPixelIndex(number_channels, columns, x, y);

-    float red;
-    float green;
-    float blue;
-    float alpha;
+    float red = 0.0f;
+    float green = 0.0f;
+    float blue = 0.0f;
+    float alpha = 0.0f;

     ReadChannels(p, number_channels, channel, &red, &green, &blue, &alpha);

@@ -1560,6 +1570,10 @@ OPENCL_ENDIF()
       green,
       red;

+    // already single channel: green/blue would be out of bounds
+    if (number_channels < 3)
+      return;
+
     red=getPixelRed(p);
     green=getPixelGreen(p);
     blue=getPixelBlue(p);
@@ -1683,7 +1697,7 @@ OPENCL_ENDIF()
         CLPixelType pixel = srcImage[x + y * imageWidth];
         float srcVal = dot(RGB, convert_float4(pixel));
         float mult = (srcVal - (sum / ((radius + 1) * (radius + 1)))) * (strength / 100.0f);
-        mult = (srcVal + mult) / srcVal;
+        mult = (srcVal > MagickEpsilon) ? ((srcVal + mult) / srcVal) : 1.0f;

         pixel.x = ClampToQuantum(pixel.x * mult);
         pixel.y = ClampToQuantum(pixel.y * mult);
@@ -1723,11 +1737,11 @@ OPENCL_ENDIF()

   c=tmax-tmin;

-  *lightness=(tmax+tmin)/2.0;
-  if (c <= 0.0)
+  *lightness=(tmax+tmin)/2.0f;
+  if (c <= 0.0f)
   {
-    *hue=0.0;
-    *saturation=0.0;
+    *hue=0.0f;
+    *saturation=0.0f;
     return;
   }

@@ -1735,19 +1749,19 @@ OPENCL_ENDIF()
   {
     *hue=(QuantumScale*green-QuantumScale*blue)/c;
     if ((QuantumScale*green) < (QuantumScale*blue))
-      *hue+=6.0;
+      *hue+=6.0f;
   }
   else
     if (tmax == (QuantumScale*green))
-      *hue=2.0+(QuantumScale*blue-QuantumScale*red)/c;
+      *hue=2.0f+(QuantumScale*blue-QuantumScale*red)/c;
     else
-      *hue=4.0+(QuantumScale*red-QuantumScale*green)/c;
+      *hue=4.0f+(QuantumScale*red-QuantumScale*green)/c;

-  *hue*=60.0/360.0;
-  if (*lightness <= 0.5)
-    *saturation=c/(2.0*(*lightness));
+  *hue*=60.0f/360.0f;
+  if (*lightness <= 0.5f)
+    *saturation=c/(2.0f*(*lightness));
   else
-    *saturation=c/(2.0-2.0*(*lightness));
+    *saturation=c/(2.0f-2.0f*(*lightness));
   }

   static inline void ConvertHSLToRGB(const float hue,const float saturation, const float lightness,
@@ -1765,15 +1779,15 @@ OPENCL_ENDIF()
     /*
        Convert HSL to RGB colorspace.
        */
-    h=hue*360.0;
-    if (lightness <= 0.5)
-      c=2.0*lightness*saturation;
+    h=hue*360.0f;
+    if (lightness <= 0.5f)
+      c=2.0f*lightness*saturation;
     else
-      c=(2.0-2.0*lightness)*saturation;
-    tmin=lightness-0.5*c;
-    h-=360.0*floor(h/360.0);
-    h/=60.0;
-    x=c*(1.0-fabs(h-2.0*floor(h/2.0)-1.0));
+      c=(2.0f-2.0f*lightness)*saturation;
+    tmin=lightness-0.5f*c;
+    h-=360.0f*floor(h/360.0f);
+    h/=60.0f;
+    x=c*(1.0f-fabs(h-2.0f*floor(h/2.0f)-1.0f));
     switch ((int) floor(h) % 6)
     {
       case 0:
@@ -1837,13 +1851,13 @@ OPENCL_ENDIF()
     Increase or decrease color lightness, saturation, or hue.
     */
     ConvertRGBToHSL(*red,*green,*blue,&hue,&saturation,&lightness);
-    hue+=0.5*(0.01*percent_hue-1.0);
-    while (hue < 0.0)
-      hue+=1.0;
-    while (hue >= 1.0)
-      hue-=1.0;
-    saturation*=0.01*percent_saturation;
-    lightness*=0.01*percent_lightness;
+    hue+=0.5f*(0.01f*percent_hue-1.0f);
+    while (hue < 0.0f)
+      hue+=1.0f;
+    while (hue >= 1.0f)
+      hue-=1.0f;
+    saturation*=0.01f*percent_saturation;
+    lightness*=0.01f*percent_lightness;
     ConvertHSLToRGB(hue,saturation,lightness,red,green,blue);
   }

@@ -2037,13 +2051,13 @@ OPENCL_ENDIF()

     which ensures function is continuous in value and derivative (slope).
     */
-    if (x < 1.0)
+    if (x < 1.0f)
       return(resizeFilterCoefficients[0]+x*(x*
       (resizeFilterCoefficients[1]+x*resizeFilterCoefficients[2])));
-    if (x < 2.0)
+    if (x < 2.0f)
       return(resizeFilterCoefficients[3]+x*(resizeFilterCoefficients[4]+x*
       (resizeFilterCoefficients[5]+x*resizeFilterCoefficients[6])));
-    return(0.0);
+    return(0.0f);
   }
   )

@@ -2165,12 +2179,12 @@ OPENCL_ENDIF()
   )

   ;
-  const char *accelerateKernels2 =
+  static const char *const accelerateKernels2 MAGICK_KERNEL_SOURCE_UNUSED =

   STRINGIFY(

   static inline unsigned int getNumWorkItemsPerPixel(const unsigned int pixelPerWorkgroup, const unsigned int numWorkItems) {
-    return (numWorkItems/pixelPerWorkgroup);
+    return MagickMax((unsigned int) 1, numWorkItems/pixelPerWorkgroup);
   }

   // returns the index of the pixel for the current workitem to compute.
@@ -2239,7 +2253,7 @@ OPENCL_ENDIF()
         const int x = chunkStartX + pixelIndex;

         // calculate how many steps required for this pixel
-        const float bisect = (x+0.5)/xFactor+MagickEpsilon;
+        const float bisect = (x+0.5f)/xFactor+MagickEpsilon;
         const unsigned int start = (unsigned int)MagickMax(bisect-support+0.5f,0.0f);
         const unsigned int stop  = (unsigned int)MagickMin(bisect+support+0.5f,(float)inputColumns);
         const unsigned int n = stop - start;
@@ -2260,7 +2274,7 @@ OPENCL_ENDIF()
               (ResizeWeightingFunctionType) resizeFilterType,
               (ResizeWeightingFunctionType) resizeWindowType,
               resizeFilterScale, resizeFilterWindowSupport,
-              resizeFilterBlur, scale*(start + i - bisect + 0.5));
+              resizeFilterBlur, scale*(start + i - bisect + 0.5f));

             float4 cp = (float4)0.0f;

@@ -2405,7 +2419,7 @@ OPENCL_ENDIF()
         const int y = chunkStartY + pixelIndex;

         // calculate how many steps required for this pixel
-        const float bisect = (y+0.5)/yFactor+MagickEpsilon;
+        const float bisect = (y+0.5f)/yFactor+MagickEpsilon;
         const unsigned int start = (unsigned int)MagickMax(bisect-support+0.5f,0.0f);
         const unsigned int stop  = (unsigned int)MagickMin(bisect+support+0.5f,(float)inputRows);
         const unsigned int n = stop - start;
@@ -2426,7 +2440,7 @@ OPENCL_ENDIF()
               (ResizeWeightingFunctionType) resizeFilterType,
               (ResizeWeightingFunctionType) resizeWindowType,
               resizeFilterScale, resizeFilterWindowSupport,
-              resizeFilterBlur, scale*(start + i - bisect + 0.5));
+              resizeFilterBlur, scale*(start + i - bisect + 0.5f));

             float4 cp = (float4)0.0f;

@@ -2771,9 +2785,10 @@ OPENCL_ENDIF()
       const unsigned int imageHeight)
   {
     const int pad = (1 << (passes - 1));
+    const unsigned int numChannels = min(max_channels, (unsigned int) 3); // stage[] holds 3 channels
     const int tileSize = 64;
     const int tileRowPixels = 64;
-    const float noise[] = { 0.8002, 0.2735, 0.1202, 0.0585, 0.0291, 0.0152, 0.0080, 0.0044 };
+    const float noise[] = { 0.8002f, 0.2735f, 0.1202f, 0.0585f, 0.0291f, 0.0152f, 0.0080f, 0.0044f };

     CLQuantum stage[48]; // 16 * 3 (we only need 3 channels)

@@ -2786,11 +2801,11 @@ OPENCL_ENDIF()
       int pos = (mirrorTop(mirrorBottom(srcx), imageWidth) * number_channels) +
                 (mirrorTop(mirrorBottom(srcy + i), imageHeight)) * imageWidth * number_channels;

-      for (int channel = 0; channel < max_channels; ++channel)
+      for (int channel = 0; channel < numChannels; ++channel)
         stage[(i / 4) + (16 * channel)] = srcImage[pos + channel];
     }

-    for (int channel = 0; channel < max_channels; ++channel) {
+    for (int channel = 0; channel < numChannels; ++channel) {
       // Load LDS
       for (int i = get_local_id(1); i < tileSize; i += get_local_size(1))
         buffer[get_local_id(0) + i * tileRowPixels] = convert_float(stage[(i / 4) + (16 * channel)]);
@@ -2814,7 +2829,7 @@ OPENCL_ENDIF()
           const int offset = i * tileRowPixels;
           if (pass == 0)
             tmp[i / 4] = buffer[x + offset]; // snapshot input on first pass
-          pixel = 0.5f * tmp[i / 4] + 0.25 * (buffer[mirrorBottom(x - radius) + offset] + buffer[mirrorTop(x + radius, tileSize) + offset]);
+          pixel = 0.5f * tmp[i / 4] + 0.25f * (buffer[mirrorBottom(x - radius) + offset] + buffer[mirrorTop(x + radius, tileSize) + offset]);
           barrier(CLK_LOCAL_MEM_FENCE);
           buffer[x + offset] = pixel;
         }
@@ -2822,7 +2837,7 @@ OPENCL_ENDIF()

         // Apply vertical hat
         for (int i = get_local_id(1); i < tileSize; i += get_local_size(1)) {
-          pixel = 0.5f * buffer[x + i * tileRowPixels] + 0.25 * (buffer[x + mirrorBottom(i - radius) * tileRowPixels] + buffer[x + mirrorTop(i + radius, tileRowPixels) * tileRowPixels]);
+          pixel = 0.5f * buffer[x + i * tileRowPixels] + 0.25f * (buffer[x + mirrorBottom(i - radius) * tileRowPixels] + buffer[x + mirrorTop(i + radius, tileRowPixels) * tileRowPixels]);
           float delta = tmp[i / 4] - pixel;
           tmp[i / 4] = pixel; // hold output in tmp until all workitems are done
           if (delta < -thresh)
@@ -2856,7 +2871,7 @@ OPENCL_ENDIF()
       for (int i = get_local_id(1); i < tileSize; i += get_local_size(1)) {
         if ((i >= pad) && (i < tileSize - pad) && (srcy + i >= 0) && (srcy + i < imageHeight)) {
           int pos = (srcx * number_channels) + ((srcy + i) * (imageWidth * number_channels));
-          for (int channel = 0; channel < max_channels; ++channel) {
+          for (int channel = 0; channel < numChannels; ++channel) {
             dstImage[pos + channel] = stage[(i / 4) + (16 * channel)];
           }
         }