Commit d6771f33c for imagemagick.org

commit d6771f33cb07b426dea3f156a70c8c5bb1eaf93e
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Tue Dec 1 18:30:37 2026 -0500

    the alpha plane was also imported as BlueQuantum

diff --git a/coders/bgr.c b/coders/bgr.c
index ba7a547a4..7f9df5894 100644
--- a/coders/bgr.c
+++ b/coders/bgr.c
@@ -81,7 +81,7 @@ static MagickBooleanType
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  ReadBGRImage() reads an image of raw BGR, or BGRA samples and returns
+%  ReadBGRImage() reads an image of raw BGR, BGRA, or BGRO samples and returns
 %  it.  It allocates the memory necessary for the new Image structure and
 %  returns a pointer to the new image.
 %
@@ -154,8 +154,11 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
       if (status == MagickFalse)
         return(DestroyImageList(image));
       if (DiscardBlobBytes(image,(MagickSizeType) image->offset) == MagickFalse)
-        ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
-          image->filename);
+        {
+          ThrowFileException(exception,CorruptImageError,
+            "UnexpectedEndOfFile",image->filename);
+          return(DestroyImageList(image));
+        }
     }
   /*
     Create virtual canvas to support cropping (i.e. image.rgb[100x100+10+20]).
@@ -302,6 +305,9 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
             AlphaQuantum
           };

+        QuantumType
+          component;
+
         /*
           Line interlacing:  BBB...GGG...RRR...RRR...GGG...BBB...
         */
@@ -330,13 +336,13 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   "UnexpectedEndOfFile",image->filename);
                 break;
               }
-            quantum_type=quantum_types[i];
+            component=quantum_types[i];
             q=GetAuthenticPixels(canvas_image,0,0,canvas_image->columns,1,
               exception);
             if (q == (Quantum *) NULL)
               break;
             length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
-              quantum_info,quantum_type,(unsigned char *) stream,exception);
+              quantum_info,component,(unsigned char *) stream,exception);
             if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
               break;
             if (((y-image->extract_info.y) >= 0) &&
@@ -351,7 +357,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   break;
                 for (x=0; x < columns; x++)
                 {
-                  switch (quantum_type)
+                  switch (component)
                   {
                     case RedQuantum:
                     {
@@ -389,6 +395,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
               }
             stream=ReadBlobStream(image,length,pixels,&count);
           }
+          if (status == MagickFalse)
+            break;
           if (image->previous == (Image *) NULL)
             {
               status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
@@ -456,6 +464,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
             }
           stream=ReadBlobStream(image,length,pixels,&count);
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,1,6);
@@ -509,6 +519,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
            }
           stream=ReadBlobStream(image,length,pixels,&count);
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,2,6);
@@ -562,12 +574,16 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
             }
           stream=ReadBlobStream(image,length,pixels,&count);
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,3,6);
             if (status == MagickFalse)
               break;
           }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,4,6);
@@ -624,6 +640,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 }
               stream=ReadBlobStream(image,length,pixels,&count);
             }
+            if (status == MagickFalse)
+              break;
             if (image->previous == (Image *) NULL)
               {
                 status=SetImageProgress(image,LoadImageTag,5,6);
@@ -631,6 +649,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
                   break;
               }
           }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,6,6);
@@ -706,7 +726,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 break;
               for (x=0; x < columns; x++)
               {
-                SetPixelRed(image,GetPixelRed(canvas_image,p),q);
+                SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
                 p+=(ptrdiff_t) GetPixelChannels(canvas_image);
                 q+=(ptrdiff_t) GetPixelChannels(image);
               }
@@ -715,6 +735,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
             }
           stream=ReadBlobStream(image,length,pixels,&count);
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,1,5);
@@ -787,6 +809,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
            }
           stream=ReadBlobStream(image,length,pixels,&count);
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,2,5);
@@ -850,7 +874,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
                 break;
               for (x=0; x < columns; x++)
               {
-                SetPixelBlue(image,GetPixelBlue(canvas_image,p),q);
+                SetPixelRed(image,GetPixelRed(canvas_image,p),q);
                 p+=(ptrdiff_t) GetPixelChannels(canvas_image);
                 q+=(ptrdiff_t) GetPixelChannels(image);
               }
@@ -859,6 +883,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
            }
           stream=ReadBlobStream(image,length,pixels,&count);
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,3,5);
@@ -909,7 +935,7 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
               if (q == (Quantum *) NULL)
                 break;
               length=ImportQuantumPixels(canvas_image,(CacheView *) NULL,
-                quantum_info,BlueQuantum,(unsigned char *) stream,exception);
+                quantum_info,AlphaQuantum,(unsigned char *) stream,exception);
               if (SyncAuthenticPixels(canvas_image,exception) == MagickFalse)
                 break;
               if (((y-image->extract_info.y) >= 0) &&
@@ -933,6 +959,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
                }
               stream=ReadBlobStream(image,length,pixels,&count);
             }
+            if (status == MagickFalse)
+              break;
             if (image->previous == (Image *) NULL)
               {
                 status=SetImageProgress(image,LoadImageTag,4,5);
@@ -941,6 +969,8 @@ static Image *ReadBGRImage(const ImageInfo *image_info,ExceptionInfo *exception)
               }
           }
         (void) CloseBlob(image);
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,LoadImageTag,5,5);
@@ -1061,6 +1091,7 @@ ModuleExport size_t RegisterBGRImage(void)
 */
 ModuleExport void UnregisterBGRImage(void)
 {
+  (void) UnregisterMagickInfo("BGRO");
   (void) UnregisterMagickInfo("BGRA");
   (void) UnregisterMagickInfo("BGR");
 }
@@ -1076,8 +1107,8 @@ ModuleExport void UnregisterBGRImage(void)
 %                                                                             %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
-%  WriteBGRImage() writes an image to a file in the BGR or BGRA
-%  rasterfile format.
+%  WriteBGRImage() writes an image to a file in the BGR, BGRA, or BGRO
+%  raw rasterfile format.
 %
 %  The format of the WriteBGRImage method is:
 %
@@ -1097,6 +1128,7 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
   ExceptionInfo *exception)
 {
   MagickBooleanType
+    has_alpha,
     status = MagickTrue;

   MagickOffsetType
@@ -1126,6 +1158,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
   assert(image_info->signature == MagickCoreSignature);
   assert(image != (Image *) NULL);
   assert(image->signature == MagickCoreSignature);
+  assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickCoreSignature);
   if (IsEventLogging() != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
   if (image_info->interlace != PartitionInterlace)
@@ -1133,18 +1167,16 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
       /*
         Open output image file.
       */
-      assert(exception != (ExceptionInfo *) NULL);
-      assert(exception->signature == MagickCoreSignature);
       status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
       if (status == MagickFalse)
         return(status);
     }
   quantum_type=BGRQuantum;
   if (LocaleCompare(image_info->magick,"BGRA") == 0)
-    {
-      quantum_type=BGRAQuantum;
-      image->alpha_trait=BlendPixelTrait;
-    }
+    quantum_type=BGRAQuantum;
+  if (LocaleCompare(image_info->magick,"BGRO") == 0)
+    quantum_type=BGROQuantum;
+  has_alpha=(MagickBooleanType) (quantum_type != BGRQuantum);
   scene=0;
   number_scenes=GetImageListLength(image);
   do
@@ -1154,9 +1186,13 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
     */
     if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
       (void) TransformImageColorspace(image,sRGBColorspace,exception);
-    if ((LocaleCompare(image_info->magick,"BGRA") == 0) &&
+    if ((has_alpha != MagickFalse) &&
         ((image->alpha_trait & BlendPixelTrait) == 0))
-      (void) SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
+      {
+        status=SetImageAlphaChannel(image,OpaqueAlphaChannel,exception);
+        if (status == MagickFalse)
+          break;
+      }
     quantum_info=AcquireQuantumInfo(image_info,image);
     if (quantum_info == (QuantumInfo *) NULL)
       ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
@@ -1232,7 +1268,7 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
               status=MagickFalse;
               break;
             }
-          if (quantum_type == BGRAQuantum)
+          if (has_alpha != MagickFalse)
             {
               length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
                 AlphaQuantum,pixels,exception);
@@ -1275,6 +1311,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
               break;
             }
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,SaveImageTag,1,6);
@@ -1298,6 +1336,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
               break;
             }
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,SaveImageTag,2,6);
@@ -1321,13 +1361,15 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
               break;
             }
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,SaveImageTag,3,6);
             if (status == MagickFalse)
               break;
           }
-        if (quantum_type == BGRAQuantum)
+        if (has_alpha != MagickFalse)
           {
             for (y=0; y < (ssize_t) image->rows; y++)
             {
@@ -1346,6 +1388,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
                   break;
                 }
             }
+            if (status == MagickFalse)
+              break;
             if (image->previous == (Image *) NULL)
               {
                 status=SetImageProgress(image,SaveImageTag,5,6);
@@ -1353,9 +1397,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
                   break;
               }
           }
-        if (image_info->interlace == PartitionInterlace)
-          (void) CopyMagickString(image->filename,image_info->filename,
-            MagickPathExtent);
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,SaveImageTag,6,6);
@@ -1374,8 +1417,7 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
           AppendBinaryBlobMode,exception);
         if (status == MagickFalse)
           {
-            quantum_info=DestroyQuantumInfo(quantum_info);
-            return(status);
+            break;
           }
         for (y=0; y < (ssize_t) image->rows; y++)
         {
@@ -1394,6 +1436,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
               break;
             }
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,SaveImageTag,1,6);
@@ -1407,8 +1451,7 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
           AppendBinaryBlobMode,exception);
         if (status == MagickFalse)
           {
-            quantum_info=DestroyQuantumInfo(quantum_info);
-            return(status);
+            break;
           }
         for (y=0; y < (ssize_t) image->rows; y++)
         {
@@ -1427,6 +1470,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
               break;
             }
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,SaveImageTag,2,6);
@@ -1440,8 +1485,7 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
           AppendBinaryBlobMode,exception);
         if (status == MagickFalse)
           {
-            quantum_info=DestroyQuantumInfo(quantum_info);
-            return(status);
+            break;
           }
         for (y=0; y < (ssize_t) image->rows; y++)
         {
@@ -1460,14 +1504,15 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
               break;
             }
         }
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,SaveImageTag,3,6);
             if (status == MagickFalse)
               break;
           }
-        (void) CloseBlob(image);
-        if (quantum_type == BGRAQuantum)
+        if (has_alpha != MagickFalse)
           {
             if (CloseBlob(image) == MagickFalse)
               break;
@@ -1476,8 +1521,7 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
               AppendBinaryBlobMode,exception);
             if (status == MagickFalse)
               {
-                quantum_info=DestroyQuantumInfo(quantum_info);
-                return(status);
+                break;
               }
             for (y=0; y < (ssize_t) image->rows; y++)
             {
@@ -1496,6 +1540,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
                   break;
                 }
             }
+            if (status == MagickFalse)
+              break;
             if (image->previous == (Image *) NULL)
               {
                 status=SetImageProgress(image,SaveImageTag,5,6);
@@ -1506,6 +1552,8 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
         (void) CloseBlob(image);
         (void) CopyMagickString(image->filename,image_info->filename,
           MagickPathExtent);
+        if (status == MagickFalse)
+          break;
         if (image->previous == (Image *) NULL)
           {
             status=SetImageProgress(image,SaveImageTag,6,6);
@@ -1516,6 +1564,13 @@ static MagickBooleanType WriteBGRImage(const ImageInfo *image_info,Image *image,
       }
     }
     quantum_info=DestroyQuantumInfo(quantum_info);
+    if ((status == MagickFalse) &&
+        (image_info->interlace == PartitionInterlace))
+      {
+        (void) CloseBlob(image);
+        (void) CopyMagickString(image->filename,image_info->filename,
+          MagickPathExtent);
+      }
     if (status == MagickFalse)
       break;
     if (GetNextImageInList(image) == (Image *) NULL)