From 066e349b8e20f6e0400683ff988ac72eea24c049 Mon Sep 17 00:00:00 2001
From: Andrew Murray <radarhere@users.noreply.github.com>
Date: Sat, 20 Jun 2026 19:58:05 +1000
Subject: [PATCH 1/3] Simplify code by calling apply() from apply_in_place()

---
 src/PIL/ImageCms.py | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Index: pillow-11.3.0/src/PIL/ImageCms.py
===================================================================
--- pillow-11.3.0.orig/src/PIL/ImageCms.py
+++ pillow-11.3.0/src/PIL/ImageCms.py
@@ -351,19 +351,21 @@ class ImageCmsTransform(Image.ImagePoint
         return self.apply(im)
 
     def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image:
-        if imOut is None:
+        if im.mode != self.input_mode:
+            msg = "mode mismatch"
+            raise ValueError(msg)
+        if imOut is not None:
+            if imOut.mode != self.output_mode:
+                msg = "mode mismatch"
+                raise ValueError(msg)
+        else:
             imOut = Image.new(self.output_mode, im.size, None)
         self.transform.apply(im.getim(), imOut.getim())
         imOut.info["icc_profile"] = self.output_profile.tobytes()
         return imOut
 
     def apply_in_place(self, im: Image.Image) -> Image.Image:
-        if im.mode != self.output_mode:
-            msg = "mode mismatch"
-            raise ValueError(msg)  # wrong output mode
-        self.transform.apply(im.getim(), im.getim())
-        im.info["icc_profile"] = self.output_profile.tobytes()
-        return im
+        return self.apply(im, im)
 
 
 def get_display_profile(handle: SupportsInt | None = None) -> ImageCmsProfile | None:
Index: pillow-11.3.0/Tests/test_imagecms.py
===================================================================
--- pillow-11.3.0.orig/Tests/test_imagecms.py
+++ pillow-11.3.0/Tests/test_imagecms.py
@@ -198,6 +198,10 @@ def test_exceptions() -> None:
     pLab = ImageCms.createProfile("LAB")
     t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB")
     with pytest.raises(ValueError, match="mode mismatch"):
+        t.apply(hopper("RGBA"))
+    with pytest.raises(ValueError, match="mode mismatch"):
+        t.apply(hopper("LAB"), hopper("RGBA"))
+    with pytest.raises(ValueError, match="mode mismatch"):
         t.apply_in_place(hopper("RGBA"))
 
     # the procedural pyCMS API uses PyCMSError for all sorts of errors
