Commit ecd5a6d2c for imagemagick.org

commit ecd5a6d2c0cc3127bdc8b9f9275408ee09be29c7
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 18:46:36 2026 -0500

    ReadSF3Image now picks the quantum type and alpha trait from the SF3 channel layout

diff --git a/coders/sf3.c b/coders/sf3.c
index ec5459991..21e5ffc75 100644
--- a/coders/sf3.c
+++ b/coders/sf3.c
@@ -147,7 +147,7 @@ static const unsigned int CRC32Table[] = {
     0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
 };

-static unsigned int crc32(unsigned int crc, const void *buf, size_t size)
+static unsigned int SF3CRC32(unsigned int crc, const void *buf, size_t size)
 {
   const unsigned char *p = (const unsigned char*)buf;

@@ -220,8 +220,45 @@ static unsigned int IsSF3(const unsigned char *magick,const size_t length)
 %    o exception: return any errors or warnings in this structure.
 %
 */
+/*
+  Channel permutations that map SF3's alpha-first / reversed layouts onto
+  the orders ImportQuantumPixels() understands: output[k] = input[map[k]].
+*/
+static const unsigned char
+  SwizzleAV[4] = { 1, 0, 0, 0 },          /* A,V     -> V,A     */
+  SwizzleAlphaFirst[4] = { 1, 2, 3, 0 },  /* A,x,y,z -> x,y,z,A */
+  SwizzleKYMC[4] = { 3, 2, 1, 0 };        /* K,Y,M,C -> C,M,Y,K */
+
+static void SwizzleSF3Row(unsigned char *pixels,const size_t columns,
+  const size_t sample_size,const size_t channels,const unsigned char *map)
+{
+  size_t
+    k,
+    pixel_size,
+    x;
+
+  unsigned char
+    sample[4*8];
+
+  pixel_size=sample_size*channels;
+  for (x=0; x < columns; x++)
+  {
+    (void) memcpy(sample,pixels,pixel_size);
+    for (k=0; k < channels; k++)
+      (void) memcpy(pixels+k*sample_size,sample+map[k]*sample_size,
+        sample_size);
+    pixels+=pixel_size;
+  }
+}
+
 static Image *ReadSF3Image(const ImageInfo *image_info,ExceptionInfo *exception)
 {
+  ColorspaceType
+    colorspace;
+
+  const unsigned char
+    *swizzle;
+
   MagickBooleanType
     status;

@@ -231,9 +268,22 @@ static Image *ReadSF3Image(const ImageInfo *image_info,ExceptionInfo *exception)
   Image
     *image;

+  PixelTrait
+    alpha_trait;
+
+  QuantumFormatType
+    quantum_format;
+
+  QuantumType
+    quantum_type;
+
+  size_t
+    pixel_size,
+    sample_size;
+
   ssize_t
     count = 0;
-
+
   unsigned char
     channels,
     format;
@@ -275,14 +325,90 @@ static Image *ReadSF3Image(const ImageInfo *image_info,ExceptionInfo *exception)
     ThrowReaderException(ResourceLimitError,"ListLengthExceedsLimit");
   channels=(unsigned char) ReadBlobByte(image);
   format=(unsigned char) ReadBlobByte(image);
+  /*
+    Validate the pixel format and channel layout once, up front.  The quantum
+    type is chosen explicitly from the layout: GetQuantumType() ignores BGR
+    ordering and, without alpha_trait set, drops the alpha channel entirely.
+  */
+  switch (format)
+  {
+    case SF3_PIXEL_INT8: case SF3_PIXEL_INT16: case SF3_PIXEL_INT32:
+    case SF3_PIXEL_INT64:
+      quantum_format=SignedQuantumFormat;
+      break;
+    case SF3_PIXEL_UINT8: case SF3_PIXEL_UINT16: case SF3_PIXEL_UINT32:
+    case SF3_PIXEL_UINT64:
+      quantum_format=UnsignedQuantumFormat;
+      break;
+    case SF3_PIXEL_FLOAT16: case SF3_PIXEL_FLOAT32: case SF3_PIXEL_FLOAT64:
+      quantum_format=FloatingPointQuantumFormat;
+      break;
+    default:
+      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  }
+  alpha_trait=UndefinedPixelTrait;
+  colorspace=sRGBColorspace;
+  swizzle=(const unsigned char *) NULL;
+  switch (channels)
+  {
+    case SF3_PIXEL_V:
+      colorspace=GRAYColorspace;
+      quantum_type=GrayQuantum;
+      break;
+    case SF3_PIXEL_VA:
+      colorspace=GRAYColorspace;
+      alpha_trait=BlendPixelTrait;
+      quantum_type=GrayAlphaQuantum;
+      break;
+    case SF3_PIXEL_AV:
+      colorspace=GRAYColorspace;
+      alpha_trait=BlendPixelTrait;
+      quantum_type=GrayAlphaQuantum;
+      swizzle=SwizzleAV;
+      break;
+    case SF3_PIXEL_RGB:
+      quantum_type=RGBQuantum;
+      break;
+    case SF3_PIXEL_BGR:
+      quantum_type=BGRQuantum;
+      break;
+    case SF3_PIXEL_RGBA:
+      alpha_trait=BlendPixelTrait;
+      quantum_type=RGBAQuantum;
+      break;
+    case SF3_PIXEL_ARGB:
+      alpha_trait=BlendPixelTrait;
+      quantum_type=RGBAQuantum;
+      swizzle=SwizzleAlphaFirst;
+      break;
+    case SF3_PIXEL_BGRA:
+      alpha_trait=BlendPixelTrait;
+      quantum_type=BGRAQuantum;
+      break;
+    case SF3_PIXEL_ABGR:
+      alpha_trait=BlendPixelTrait;
+      quantum_type=BGRAQuantum;
+      swizzle=SwizzleAlphaFirst;
+      break;
+    case SF3_PIXEL_CMYK:
+      colorspace=CMYKColorspace;
+      quantum_type=CMYKQuantum;
+      break;
+    case SF3_PIXEL_KYMC:
+      colorspace=CMYKColorspace;
+      quantum_type=CMYKQuantum;
+      swizzle=SwizzleKYMC;
+      break;
+    default:
+      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  }
+  sample_size=(size_t) (format & 0xF);
+  pixel_size=sample_size*(size_t) (channels & 0xF);
   for (unsigned int z=0; z<layers; ++z)
   {
     QuantumInfo
       *quantum_info;

-    QuantumType
-      quantum_type;
-
     size_t
       length = 0;

@@ -297,113 +423,50 @@ static Image *ReadSF3Image(const ImageInfo *image_info,ExceptionInfo *exception)
     image->orientation=TopLeftOrientation;
     image->columns=(size_t) width;
     image->rows=(size_t) height;
-    image->depth=(size_t)((format & 0xF)*8);
+    image->depth=(size_t) (sample_size*8);
+    image->alpha_trait=alpha_trait;
     if (image_info->ping != MagickFalse)
       break;
-    if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
-      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
-        break;
     number_pixels=(MagickSizeType) image->columns*image->rows;
-    if (number_pixels > GetBlobSize(image))
+    if ((number_pixels > (GetBlobSize(image)/pixel_size)) ||
+        (image->columns > (MAGICK_SIZE_MAX/pixel_size)))
       ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
     status=SetImageExtent(image,image->columns,image->rows,exception);
     if (status == MagickFalse)
       return(DestroyImageList(image));
-    switch(channels)
-    {
-      case SF3_PIXEL_V:
-        (void) SetImageColorspace(image,GRAYColorspace,exception);
-        SetQuantumImageType(image,GrayQuantum);
-        break;
-      case SF3_PIXEL_VA:
-        (void) SetImageColorspace(image,GRAYColorspace,exception);
-        SetQuantumImageType(image,GrayAlphaQuantum);
-        break;
-      case SF3_PIXEL_RGB:
-        (void) SetImageColorspace(image,RGBColorspace,exception);
-        SetQuantumImageType(image,RGBQuantum);
-        break;
-      case SF3_PIXEL_RGBA:
-        (void) SetImageColorspace(image,RGBColorspace,exception);
-        SetQuantumImageType(image,RGBAQuantum);
-        break;
-      case SF3_PIXEL_AV:
-        (void) SetImageColorspace(image,GRAYColorspace,exception);
-        SetQuantumImageType(image,GrayAlphaQuantum);
-        break;
-      case SF3_PIXEL_BGR:
-        (void) SetImageColorspace(image,RGBColorspace,exception);
-        SetQuantumImageType(image,BGRQuantum);
-        break;
-      case SF3_PIXEL_ABGR:
-        (void) SetImageColorspace(image,RGBColorspace,exception);
-        SetQuantumImageType(image,BGRAQuantum);
-        break;
-      case SF3_PIXEL_ARGB:
-        (void) SetImageColorspace(image,RGBColorspace,exception);
-        SetQuantumImageType(image,RGBAQuantum);
-        break;
-      case SF3_PIXEL_BGRA:
-        (void) SetImageColorspace(image,RGBColorspace,exception);
-        SetQuantumImageType(image,BGRAQuantum);
-        break;
-      case SF3_PIXEL_CMYK:
-        (void) SetImageColorspace(image,CMYKColorspace,exception);
-        SetQuantumImageType(image,CMYKQuantum);
-        break;
-      case SF3_PIXEL_KYMC:
-        (void) SetImageColorspace(image,CMYKColorspace,exception);
-        SetQuantumImageType(image,CMYKQuantum);
-        break;
-      default:
-        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-    }
-    quantum_type=GetQuantumType(image,exception);
+    (void) SetImageColorspace(image,colorspace,exception);
+    SetQuantumImageType(image,quantum_type);
     quantum_info=AcquireQuantumInfo(image_info,image);
     if (quantum_info == (QuantumInfo *) NULL)
       ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-    switch(format)
-    {
-      case SF3_PIXEL_INT8:
-      case SF3_PIXEL_INT16:
-      case SF3_PIXEL_INT32:
-      case SF3_PIXEL_INT64:
-        status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
-        break;
-      case SF3_PIXEL_UINT8:
-      case SF3_PIXEL_UINT16:
-      case SF3_PIXEL_UINT32:
-      case SF3_PIXEL_UINT64:
-        status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
-        break;
-      case SF3_PIXEL_FLOAT16:
-      case SF3_PIXEL_FLOAT32:
-      case SF3_PIXEL_FLOAT64:
-        status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
-        break;
-      default:
-        quantum_info=DestroyQuantumInfo(quantum_info);
-        ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-    }
+    status=SetQuantumFormat(image,quantum_info,quantum_format);
+    if (status != MagickFalse)
+      status=SetQuantumEndian(image,quantum_info,LSBEndian);
     if (status == MagickFalse)
       {
         quantum_info=DestroyQuantumInfo(quantum_info);
         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
       }
-    length=image->columns*(format & 0xF)*(channels & 0xF);
+    length=image->columns*pixel_size;
     pixels=GetQuantumPixels(quantum_info);
     for (y=0; y < (ssize_t) image->rows; ++y)
     {
+      Quantum
+        *magick_restrict q;
+
       count=ReadBlob(image,length,pixels);
       if (count != (ssize_t) length)
         break;
-      (void) GetAuthenticPixels(image,0,y,image->columns,1,exception);
+      if (swizzle != (const unsigned char *) NULL)
+        SwizzleSF3Row(pixels,image->columns,sample_size,
+          (size_t) (channels & 0xF),swizzle);
+      q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
+      if (q == (Quantum *) NULL)
+        break;
       (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
         quantum_type,pixels,exception);
       if (SyncAuthenticPixels(image,exception) == MagickFalse)
         break;
-      if (EOFBlob(image) != MagickFalse)
-        break;
     }
     quantum_info=DestroyQuantumInfo(quantum_info);
     if (y < (ssize_t) image->rows)
@@ -429,6 +492,8 @@ static Image *ReadSF3Image(const ImageInfo *image_info,ExceptionInfo *exception)
     if (status == MagickFalse)
       break;
   }
+  if (CloseBlob(image) == MagickFalse)
+    status=MagickFalse;
   if (status == MagickFalse)
     return(DestroyImageList(image));
   return(GetFirstImageInList(image));
@@ -591,6 +656,8 @@ static MagickBooleanType WriteSF3Image(const ImageInfo *image_info,Image *image,
   (void) SetQuantumEndian(image,quantum_info,LSBEndian);
   (void) SetQuantumPad(image,quantum_info,0);
   number_scenes=GetImageListLength(image);
+  if (image_info->adjoin == MagickFalse)
+    number_scenes=1;  /* only the first frame is written; say so in the header */
   quantum_type=GetQuantumType(image,exception);
   quantum_format=GetQuantumFormat(quantum_info);
   switch (quantum_type)
@@ -721,20 +788,34 @@ static MagickBooleanType WriteSF3Image(const ImageInfo *image_info,Image *image,
   /*
     Write SF3 header.
   */
-  (void) WriteBlob(image,11,"\x81SF3\x00\xE0\xD0\r\n\n\x03");
-  (void) WriteBlobLSBLong(image,(unsigned int) 0); // Zero CRC32 for now
+  (void) WriteBlob(image,11,(const unsigned char *)
+    "\x81SF3\x00\xE0\xD0\r\n\n\x03");
+  (void) WriteBlobLSBLong(image,(unsigned int) 0);  /* CRC32 patched below */
   (void) WriteBlobByte(image,(unsigned char) 0);
-  checksum=0xFFFFFFFF;
-  (void) WriteBlobLSBLong(image,(unsigned int) image->columns);
-  (void) WriteBlobLSBLong(image,(unsigned int) image->rows);
-  (void) WriteBlobLSBLong(image,(unsigned int) number_scenes);
-  (void) WriteBlobByte(image,channels);
-  (void) WriteBlobByte(image,format);
-  if(IsBlobSeekable(image)){
-    (void) SeekBlob(image,16,SEEK_SET);
-    (void) ReadBlob(image,14,header);
-    checksum=crc32(checksum,header,14);
-  }
+  /*
+    Build the 14-byte image header in memory and checksum it directly rather
+    than seeking back and re-reading it from a blob opened for writing.
+  */
+  header[0]=(unsigned char) (image->columns & 0xff);
+  header[1]=(unsigned char) ((image->columns >> 8) & 0xff);
+  header[2]=(unsigned char) ((image->columns >> 16) & 0xff);
+  header[3]=(unsigned char) ((image->columns >> 24) & 0xff);
+  header[4]=(unsigned char) (image->rows & 0xff);
+  header[5]=(unsigned char) ((image->rows >> 8) & 0xff);
+  header[6]=(unsigned char) ((image->rows >> 16) & 0xff);
+  header[7]=(unsigned char) ((image->rows >> 24) & 0xff);
+  header[8]=(unsigned char) (number_scenes & 0xff);
+  header[9]=(unsigned char) ((number_scenes >> 8) & 0xff);
+  header[10]=(unsigned char) ((number_scenes >> 16) & 0xff);
+  header[11]=(unsigned char) ((number_scenes >> 24) & 0xff);
+  header[12]=channels;
+  header[13]=format;
+  if (WriteBlob(image,14,header) != 14)
+    {
+      quantum_info=DestroyQuantumInfo(quantum_info);
+      ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
+    }
+  checksum=SF3CRC32(0xFFFFFFFF,header,14);
   /*
     Write pixels.
   */
@@ -753,10 +834,13 @@ static MagickBooleanType WriteSF3Image(const ImageInfo *image_info,Image *image,
       {
         p=GetVirtualPixels(image,0,y,image->columns,1,exception);
         if (p == (const Quantum *) NULL)
-          break;
+          {
+            status=MagickFalse;
+            break;
+          }
         length=ExportQuantumPixels(image,(CacheView *)NULL,quantum_info,
            quantum_type,pixels,exception);
-        checksum=crc32(checksum,pixels,length);
+        checksum=SF3CRC32(checksum,pixels,length);
         count=WriteBlob(image,length,pixels);
         if (count != (ssize_t) length)
           {