Commit 348f4d994 for imagemagick.org

commit 348f4d994c807a40255677a546f874906251fb23
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Sun Nov 29 07:56:31 2026 -0500

    initialize the output channels for contrast stretch and equalize

diff --git a/MagickCore/accelerate-kernels-private.h b/MagickCore/accelerate-kernels-private.h
index d7f2f6224..d71bd2e7f 100644
--- a/MagickCore/accelerate-kernels-private.h
+++ b/MagickCore/accelerate-kernels-private.h
@@ -663,7 +663,7 @@ OPENCL_ENDIF()
     while(e!=0){
       if(e&1)
         acc=MWC_MulMod64(acc,sqr,M);
-        sqr=MWC_MulMod64(sqr,sqr,M);
+      sqr=MWC_MulMod64(sqr,sqr,M);
       e=e>>1;
     }
     return acc;
@@ -992,7 +992,7 @@ OPENCL_ENDIF()
         result.x+=((pixel.x == tmax) ? (pixel.y-pixel.z) : ((pixel.y == tmax) ?
           (pixel.z-pixel.x) : (pixel.x-pixel.y)))/delta;
         result.x/=6.0f;
-        result.x+=(result.x < 0.0f) ? 0.0f : 1.0f;
+        result.x+=(result.x < 0.0f) ? 1.0f : 0.0f;
       }
     }
     return(result);
@@ -1143,6 +1143,10 @@ OPENCL_ENDIF()

         //read from global
         oValue=im[c];
+        red=getRed(oValue);
+        green=getGreen(oValue);
+        blue=getBlue(oValue);
+        alpha=getAlpha(oValue);

         if ((channel & RedChannel) != 0)
         {
@@ -1378,6 +1382,10 @@ OPENCL_ENDIF()

         //read from global
         oValue=im[c];
+        red=getRed(oValue);
+        green=getGreen(oValue);
+        blue=getBlue(oValue);
+        alpha=getAlpha(oValue);

         if ((channel & SyncChannels) != 0)
         {
@@ -2643,8 +2651,9 @@ OPENCL_ENDIF()

       for ( ; i+7 < width; )
       {
-        for (int j=0; j < 8; j++, i++)
+        for (int j=0; j < 8; j++)
           blurredPixel+=cachedFilter[i+j]*cachedData[i+j+get_local_id(1)];
+        i+=8;
       }

       for ( ; i < width; i++)
@@ -2720,7 +2729,7 @@ OPENCL_ENDIF()
     int i = 0;
     while (i + 7 < width) {
       for (int j = 0; j < 8; ++j) // unrolled
-        value += (float4)(filter[i]) * pixels[px + (py + i + j) * prp];
+        value += (float4)(filter[i + j]) * pixels[px + (py + i + j) * prp];
       i += 8;
     }
     while (i < width) {
diff --git a/MagickCore/accelerate.c b/MagickCore/accelerate.c
index 01d768d23..b3a4950f8 100644
--- a/MagickCore/accelerate.c
+++ b/MagickCore/accelerate.c
@@ -134,7 +134,7 @@ static MagickBooleanType checkAccelerateCondition(const Image* image)
   if (image->number_channels > 4)
     return(MagickFalse);

-  /* check if */
+  /* check that the channel mask fits in 32 bits */
   if ((image->channel_mask != AllChannels) &&
       (image->channel_mask > 0x7ffffff))
     return(MagickFalse);
@@ -852,7 +852,11 @@ static MagickBooleanType ComputeContrastStretchImage(Image *image,
   histogram=(cl_uint4 *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*histogram));

   if (histogram == (cl_uint4 *) NULL)
-    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", image->filename);
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
+        "MemoryAllocationFailed","`%s'",image->filename);
+      goto cleanup;
+    }

   /* reset histogram */
   (void) memset(histogram,0,(MaxMap+1)*sizeof(*histogram));
@@ -1052,8 +1056,11 @@ static MagickBooleanType ComputeContrastStretchImage(Image *image,
     sizeof(*stretch_map));

   if (stretch_map == (PixelPacket *) NULL)
-    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
-      image->filename);
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
+        "MemoryAllocationFailed","`%s'",image->filename);
+      goto cleanup;
+    }

   /*
     Stretch the histogram to create the stretched image mapping.
@@ -1258,7 +1265,8 @@ static MagickBooleanType ComputeContrastStretchImage(Image *image,

 cleanup:

-  image_view=DestroyCacheView(image_view);
+  if (image_view != (CacheView *) NULL)
+    image_view=DestroyCacheView(image_view);

   if (imageBuffer!=NULL)
     clEnv->library->clReleaseMemObject(imageBuffer);
@@ -1442,7 +1450,8 @@ static Image *ComputeDespeckleImage(const Image *image,MagickCLEnv clEnv,
   }

   filteredImage = CloneImage(image,0,0,MagickTrue,exception);
-  assert(filteredImage != NULL);
+  if (filteredImage == (Image *) NULL)
+    goto cleanup;
   if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
   {
     (void) OpenCLThrowMagickException(device,exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", ".");
@@ -1477,6 +1486,12 @@ static Image *ComputeDespeckleImage(const Image *image,MagickCLEnv clEnv,

   hullPass1 = AcquireOpenCLKernel(device,"HullPass1");
   hullPass2 = AcquireOpenCLKernel(device,"HullPass2");
+  if ((hullPass1 == NULL) || (hullPass2 == NULL))
+  {
+    (void) OpenCLThrowMagickException(device,exception,GetMagickModule(),
+      ResourceLimitWarning,"AcquireOpenCLKernel failed.",".");
+    goto cleanup;
+  }

   clStatus =clEnv->library->clSetKernelArg(hullPass1,0,sizeof(cl_mem),(void *)&imageBuffer);
   clStatus |=clEnv->library->clSetKernelArg(hullPass1,1,sizeof(cl_mem),(void *)(tempImageBuffer+1));
@@ -1664,7 +1679,8 @@ static Image *ComputeDespeckleImage(const Image *image,MagickCLEnv clEnv,

 cleanup:

-  image_view=DestroyCacheView(image_view);
+  if (image_view != (CacheView *) NULL)
+    image_view=DestroyCacheView(image_view);
   if (filteredImage_view != NULL)
     filteredImage_view=DestroyCacheView(filteredImage_view);

@@ -1819,7 +1835,11 @@ static MagickBooleanType ComputeEqualizeImage(Image *image,MagickCLEnv clEnv,
   */
   histogram=(cl_uint4 *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*histogram));
   if (histogram == (cl_uint4 *) NULL)
-      ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
+      {
+      (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitWarning,
+        "MemoryAllocationFailed","`%s'",image->filename);
+      goto cleanup;
+    }

   /* reset histogram */
   (void) memset(histogram,0,(MaxMap+1)*sizeof(*histogram));
@@ -1918,11 +1938,19 @@ static MagickBooleanType ComputeEqualizeImage(Image *image,MagickCLEnv clEnv,
   /* CPU stuff */
   equalize_map=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL, sizeof(*equalize_map));
   if (equalize_map == (PixelPacket *) NULL)
-    ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitWarning,
+        "MemoryAllocationFailed","`%s'",image->filename);
+      goto cleanup;
+    }

   map=(cl_float4 *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*map));
   if (map == (cl_float4 *) NULL)
-    ThrowBinaryException(ResourceLimitWarning,"MemoryAllocationFailed", image->filename);
+    {
+      (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitWarning,
+        "MemoryAllocationFailed","`%s'",image->filename);
+      goto cleanup;
+    }

   /*
     Integrate the histogram to get the equalization map.
@@ -2115,7 +2143,8 @@ static MagickBooleanType ComputeEqualizeImage(Image *image,MagickCLEnv clEnv,

 cleanup:

-  image_view=DestroyCacheView(image_view);
+  if (image_view != (CacheView *) NULL)
+    image_view=DestroyCacheView(image_view);

   if (imageBuffer!=NULL)
     clEnv->library->clReleaseMemObject(imageBuffer);
@@ -2587,7 +2616,8 @@ static Image *ComputeLocalContrastImage(const Image *image,MagickCLEnv clEnv,
   /* create output */
   {
     filteredImage = CloneImage(image,0,0,MagickTrue,exception);
-    assert(filteredImage != NULL);
+    if (filteredImage == (Image *) NULL)
+      goto cleanup;
     if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
     {
       (void) OpenCLThrowMagickException(device,exception, GetMagickModule(), ResourceLimitWarning, "CloneImage failed.", ".");
@@ -2768,7 +2798,8 @@ static Image *ComputeLocalContrastImage(const Image *image,MagickCLEnv clEnv,

 cleanup:

-  image_view=DestroyCacheView(image_view);
+  if (image_view != (CacheView *) NULL)
+    image_view=DestroyCacheView(image_view);
   if (filteredImage_view != NULL)
     filteredImage_view=DestroyCacheView(filteredImage_view);

@@ -2955,7 +2986,7 @@ static MagickBooleanType ComputeModulateImage(Image *image,MagickCLEnv clEnv,
   clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&bright);
   clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&hue);
   clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&saturation);
-  clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_float),&color);
+  clStatus|=clEnv->library->clSetKernelArg(modulateKernel,i++,sizeof(cl_int),&color);
   if (clStatus != CL_SUCCESS)
   {
     (void) OpenCLThrowMagickException(device,exception, GetMagickModule(), ResourceLimitWarning, "clEnv->library->clSetKernelArg failed.", ".");
@@ -2996,7 +3027,8 @@ static MagickBooleanType ComputeModulateImage(Image *image,MagickCLEnv clEnv,

 cleanup:

-  image_view=DestroyCacheView(image_view);
+  if (image_view != (CacheView *) NULL)
+    image_view=DestroyCacheView(image_view);

   if (imageBuffer!=NULL)
     clEnv->library->clReleaseMemObject(imageBuffer);
@@ -3135,6 +3167,7 @@ static Image* ComputeMotionBlurImage(const Image *image,MagickCLEnv clEnv,
   imageBuffer=NULL;
   filteredImageBuffer=NULL;
   imageKernelBuffer=NULL;
+  offsetBuffer=NULL;
   motionBlurKernel=NULL;
   outputReady=MagickFalse;

@@ -3183,7 +3216,8 @@ static Image* ComputeMotionBlurImage(const Image *image,MagickCLEnv clEnv,

   filteredImage = CloneImage(image,image->columns,image->rows,
     MagickTrue,exception);
-  assert(filteredImage != NULL);
+  if (filteredImage == (Image *) NULL)
+    goto cleanup;
   if (SetImageStorageClass(filteredImage,DirectClass,exception) != MagickTrue)
   {
     (void) ThrowMagickException(exception, GetMagickModule(),
@@ -3382,7 +3416,8 @@ static Image* ComputeMotionBlurImage(const Image *image,MagickCLEnv clEnv,

 cleanup:

-  image_view=DestroyCacheView(image_view);
+  if (image_view != (CacheView *) NULL)
+    image_view=DestroyCacheView(image_view);
   if (filteredImage_view != NULL)
     filteredImage_view=DestroyCacheView(filteredImage_view);

@@ -3392,6 +3427,8 @@ cleanup:
     clEnv->library->clReleaseMemObject(imageBuffer);
   if (imageKernelBuffer!=NULL)
     clEnv->library->clReleaseMemObject(imageKernelBuffer);
+  if (offsetBuffer!=NULL)
+    clEnv->library->clReleaseMemObject(offsetBuffer);
   if (motionBlurKernel!=NULL)
     ReleaseOpenCLKernel(motionBlurKernel);
   if (queue != NULL)
@@ -4302,6 +4339,8 @@ static Image *ComputeUnsharpMaskImage(const Image *image,MagickCLEnv clEnv,

   imageKernelBuffer=createKernelInfo(device,radius,sigma,&kernelWidth,
     exception);
+  if (imageKernelBuffer == (cl_mem) NULL)
+    goto cleanup;

   length=image->columns*image->rows;
   tempImageBuffer=CreateOpenCLBuffer(device,CL_MEM_READ_WRITE,length*
@@ -4360,6 +4399,8 @@ static Image *ComputeUnsharpMaskImage(const Image *image,MagickCLEnv clEnv,
   outputReady=EnqueueOpenCLKernel(queue,blurRowKernel,2,
     (const size_t *) NULL,gsize,lsize,image,filteredImage,MagickFalse,
     exception);
+  if (outputReady == MagickFalse)
+    goto cleanup;

   chunkSize=256;
   fGain=(float) gain;
@@ -4489,6 +4530,8 @@ static Image *ComputeUnsharpMaskImageSingle(const Image *image,

   imageKernelBuffer=createKernelInfo(device,radius,sigma,&kernelWidth,
     exception);
+  if (imageKernelBuffer == (cl_mem) NULL)
+    goto cleanup;

   unsharpMaskKernel=AcquireOpenCLKernel(device,"UnsharpMask");
   if (unsharpMaskKernel == NULL)