Commit ab2232d0f for imagemagick.org

commit ab2232d0fe25085168db5b1f1e8017a8de624e91
Author: Dirk Lemstra <dirk@lemstra.org>
Date:   Thu Jan 1 15:49:56 2026 +0100

    Check if the aspect ratio is a known ratio when density_unit is set to zero and otherwise read it as an undefined density in the jpeg coder (#8496)

diff --git a/coders/jpeg.c b/coders/jpeg.c
index 45726236b..01626242a 100644
--- a/coders/jpeg.c
+++ b/coders/jpeg.c
@@ -312,6 +312,23 @@ static void InitializeSource(j_decompress_ptr compress_info)
   source->start_of_blob=TRUE;
 }

+static MagickBooleanType IsAspectRatio(const j_decompress_ptr compress_info)
+{
+  if (compress_info->density_unit != 0)
+    return(MagickFalse);
+  if ((compress_info->X_density == 1) && (compress_info->Y_density == 1))
+    return(MagickTrue);
+  if ((compress_info->X_density == 3) && (compress_info->Y_density == 2))
+    return(MagickTrue);
+  if ((compress_info->X_density == 4) && (compress_info->Y_density == 3))
+    return(MagickTrue);
+  if ((compress_info->X_density == 16) && (compress_info->Y_density == 9))
+    return(MagickTrue);
+  if ((compress_info->X_density == 16) && (compress_info->Y_density == 10))
+    return(MagickTrue);
+  return(MagickFalse);
+}
+
 static MagickBooleanType IsITUFaxImage(const Image *image)
 {
   const StringInfo
@@ -1188,16 +1205,19 @@ static Image *ReadOneJPEGImage(const ImageInfo *image_info,
   /*
     Set image resolution.
   */
-  if ((jpeg_info->saw_JFIF_marker != 0) && (jpeg_info->density_unit != 0))
+  if (jpeg_info->saw_JFIF_marker != 0)
     {
       if (jpeg_info->density_unit == 1)
         image->units=PixelsPerInchResolution;
       else if (jpeg_info->density_unit == 2)
         image->units = PixelsPerCentimeterResolution;
-      if (jpeg_info->X_density != 0)
-        image->resolution.x=(double) jpeg_info->X_density;
-      if (jpeg_info->Y_density != 0)
-        image->resolution.y=(double) jpeg_info->Y_density;
+      if (IsAspectRatio(jpeg_info) == MagickFalse)
+        {
+          if (jpeg_info->X_density != 0)
+            image->resolution.x=(double) jpeg_info->X_density;
+          if (jpeg_info->Y_density != 0)
+            image->resolution.y=(double) jpeg_info->Y_density;
+        }
     }
   number_pixels=(MagickSizeType) image->columns*image->rows;
   option=GetImageOption(image_info,"jpeg:size");