Commit a09a75a39 for imagemagick.org

commit a09a75a39aef31b6c9966cd6a27f522630c60d78
Author: Cristy <urban-warrior@imagemagick.org>
Date:   Fri Dec 4 18:47:12 2026 -0500

    WriteMATImage compared each exported column's length against image->columns instead of image->rows

diff --git a/coders/mat.c b/coders/mat.c
index fd16ad89f..56b1458ef 100644
--- a/coders/mat.c
+++ b/coders/mat.c
@@ -238,7 +238,7 @@ static void InsertComplexDoubleRow(Image *image,double *p,int y,double MinVal,
           }
       }
     p++;
-    q++;
+    q+=(ptrdiff_t) GetPixelChannels(image);
   }
   if (!SyncAuthenticPixels(image,exception))
     return;
@@ -286,7 +286,7 @@ static void InsertComplexFloatRow(Image *image,float *p,int y,double MinVal,
       }
     if (*p < 0)
       {
-        f=(*p/MaxVal)*(Quantum) (QuantumRange-GetPixelBlue(image,q));
+        f=((double) *p/MinVal)*(Quantum) (QuantumRange-GetPixelBlue(image,q));
         if (IsNaN(f) != 0)
           f=0.0;
         if ((f+GetPixelBlue(image,q)) < QuantumRange)
@@ -306,7 +306,7 @@ static void InsertComplexFloatRow(Image *image,float *p,int y,double MinVal,
           }
       }
     p++;
-    q++;
+    q+=(ptrdiff_t) GetPixelChannels(image);
   }
   if (!SyncAuthenticPixels(image,exception))
     return;
@@ -435,7 +435,7 @@ static void FixSignedValues(const Image *image,Quantum *q, ssize_t y)
     SetPixelRed(image,GetPixelRed(image,q)+QuantumRange/2+1,q);
     SetPixelGreen(image,GetPixelGreen(image,q)+QuantumRange/2+1,q);
     SetPixelBlue(image,GetPixelBlue(image,q)+QuantumRange/2+1,q);
-    q++;
+    q+=(ptrdiff_t) GetPixelChannels(image);
   }
 }

@@ -1032,6 +1032,7 @@ MATLAB_KO:
     {
       image2 = decompress_block(image,&MATLAB_HDR.ObjectSize,clone_info,exception);
       if(image2==NULL) continue;
+      image2->endian = image->endian;  /* honor the header's byte order */
       MATLAB_HDR.DataType = ReadBlobXXXLong(image2); /* replace compressed object type. */
     }
 #endif
@@ -1308,7 +1309,7 @@ RestoreMSCWarning
     MaxVal = 0;
     if (CellType==miDOUBLE || CellType==miSINGLE)        /* Find Min and Max Values for floats */
       {
-        CalcMinMax(image2,(int) image_info->endian,MATLAB_HDR.SizeX,
+        CalcMinMax(image2,(int) image->endian,MATLAB_HDR.SizeX,
           MATLAB_HDR.SizeY,CellType,(unsigned int) ldblk,BImgBuff,
           &quantum_info->minimum,&quantum_info->maximum);
       }
@@ -1382,7 +1383,7 @@ ExitLoop:

       if (CellType==miDOUBLE || CellType==miSINGLE)
       {
-        CalcMinMax(image2,(int) image_info->endian,MATLAB_HDR.SizeX,
+        CalcMinMax(image2,(int) image->endian,MATLAB_HDR.SizeX,
           MATLAB_HDR.SizeY,CellType,(unsigned int) ldblk,BImgBuff,&MinVal,&MaxVal);
       }

@@ -1390,7 +1391,7 @@ ExitLoop:
         for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
         {
           ReadBlobDoublesXXX(image2, (size_t) ldblk, (double *)BImgBuff);
-          if (EOFBlob(image) != MagickFalse)
+          if (EOFBlob(image2) != MagickFalse)
             break;
           InsertComplexDoubleRow(image,(double *)BImgBuff,(int) i,MinVal,MaxVal,
             exception);
@@ -1400,7 +1401,7 @@ ExitLoop:
         for (i = 0; i < (ssize_t) MATLAB_HDR.SizeY; i++)
         {
           ReadBlobFloatsXXX(image2, (size_t) ldblk, (float *)BImgBuff);
-          if (EOFBlob(image) != MagickFalse)
+          if (EOFBlob(image2) != MagickFalse)
             break;
           InsertComplexFloatRow(image,(float *)BImgBuff,(int) i,MinVal,MaxVal,
             exception);
@@ -1762,23 +1763,30 @@ static MagickBooleanType WriteMATImage(const ImageInfo *image_info,Image *image,
         size_t
           length;

+        /*
+          MATLAB stores column-major: each exported "row" is one image column
+          of image->rows samples, so compare against rows, not columns.
+        */
         p=GetVirtualPixels(image,y,0,1,image->rows,exception);
         if (p == (const Quantum *) NULL)
           break;
         length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
           z2qtype[z],pixels,exception);
-        if (length != image->columns)
+        if (length != image->rows)
           break;
         if (WriteBlob(image,image->rows,pixels) != (ssize_t) image->rows)
           break;
       }
       if (y < (ssize_t) image->columns)
-        break;
-      if (SyncAuthenticPixels(image,exception) == MagickFalse)
-        break;
+        {
+          status=MagickFalse;
+          break;
+        }
     } while (z-- >= 2);
     (void) WriteBlobPadding(image,padding,'\0');
     quantum_info=DestroyQuantumInfo(quantum_info);
+    if (status == MagickFalse)
+      break;
     if (GetNextImageInList(image) == (Image *) NULL)
       break;
     image=SyncNextImageInList(image);