From ed188fdfe8534cf35d80fada4814d82bf42873f0 Mon Sep 17 00:00:00 2001
From: David Manthey <david.manthey@kitware.com>
Date: Thu, 2 Feb 2023 08:44:22 -0500
Subject: [PATCH] Fix an issue in applying icc color profiles.

---
 CHANGELOG.md                   |  2 +-
 large_image/tilesource/base.py | 11 ++++++-----
 test/test_source_tiff.py       |  3 ++-
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 472b9d68f..8b172e994 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,7 +3,7 @@
 ## 1.20.0
 
 ### Features
-- ICC color profile support ([#1037](../../pull/1037), [#1043](../../pull/1043))
+- ICC color profile support ([#1037](../../pull/1037), [#1043](../../pull/1043), [#1046](../../pull/1046))
 
 ### Improvements
 - Speed up generating tiles for some multi source files ([#1035](../../pull/1035))
diff --git a/large_image/tilesource/base.py b/large_image/tilesource/base.py
index 9b327b3ce..49fa112c1 100644
--- a/large_image/tilesource/base.py
+++ b/large_image/tilesource/base.py
@@ -1463,15 +1463,16 @@ def _outputTile(self, tile, tileEncoding, x, y, z, pilImageAllowed=False,
             maxX = (x + 1) * self.tileWidth
             maxY = (y + 1) * self.tileHeight
             isEdge = maxX > sizeX or maxY > sizeY
+        hasStyle = (
+            (getattr(self, 'style', None) or hasattr(self, '_iccprofiles')) and
+            getattr(self, 'style', None) != {'icc': False})
         if (tileEncoding not in (TILE_FORMAT_PIL, TILE_FORMAT_NUMPY) and
                 numpyAllowed != 'always' and tileEncoding == self.encoding and
-                not isEdge and (not applyStyle or not getattr(self, 'style', None))):
+                not isEdge and (not applyStyle or not hasStyle)):
             return tile
         mode = None
-        if (numpyAllowed == 'always' or tileEncoding == TILE_FORMAT_NUMPY or (applyStyle and (
-                (getattr(self, 'style', None) or hasattr(self, '_iccprofiles')) and
-                getattr(self, 'style', None) != {'icc': False})) or
-                isEdge):
+        if (numpyAllowed == 'always' or tileEncoding == TILE_FORMAT_NUMPY or
+                (applyStyle and hasStyle) or isEdge):
             tile, mode = self._outputTileNumpyStyle(
                 tile, applyStyle, x, y, z, self._getFrame(**kwargs))
         if isEdge:
diff --git a/test/test_source_tiff.py b/test/test_source_tiff.py
index d469e195a..4aa4f6084 100644
--- a/test/test_source_tiff.py
+++ b/test/test_source_tiff.py
@@ -769,7 +769,8 @@ def testFromTiffRGBJPEG():
         'TCGA-AA-A02O-11A-01-BS1.8b76f05c-4a8b-44ba-b581-6b8b4f437367.svs')
     source = large_image_source_tiff.open(imagePath)
     tile = source.getSingleTile()
-    assert list(tile['tile'][0, 0]) == [243, 243, 243]
+    # Handle ICC Profiles
+    assert list(tile['tile'][0, 0]) == [243, 243, 243] or list(tile['tile'][0, 0]) == [242, 243, 242]
 
 
 def testTilesFromMultiFrameTiff():