Skip to content

Commit 8c8632a

Browse files
authored
Fix cache-related bug in apply_mask for masks with position-dependent colors
- Resolved cache issues in `apply_mask` by setting `use_cache` to `False` by default, preventing errors in masks where pixel position affects color (e.g., RadialGradientColorMask). - Enabled `use_cache` for `SolidFillColorMask`, as pixel position is not relevant, preserving performance where applicable.
1 parent c4b9bd8 commit 8c8632a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

qrcode/image/styles/colormasks.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ class QRColorMask:
2727
def initialize(self, styledPilImage, image):
2828
self.paint_color = styledPilImage.paint_color
2929

30-
def apply_mask(self, image):
30+
def apply_mask(self, image, use_cache=False):
3131
width, height = image.size
3232
pixels = image.load()
33-
fg_color_cache = {}
33+
fg_color_cache = {} if use_cache else None
3434
for x in range(width):
3535
for y in range(height):
3636
current_color = pixels[x, y]
3737
if current_color == self.back_color:
3838
continue
39-
if current_color in fg_color_cache:
39+
if use_cache and current_color in fg_color_cache:
4040
pixels[x, y] = fg_color_cache[current_color]
4141
continue
4242
norm = self.extrap_color(self.back_color, self.paint_color, current_color)
@@ -47,7 +47,9 @@ def apply_mask(self, image):
4747
norm
4848
)
4949
pixels[x, y] = new_color
50-
fg_color_cache[current_color] = new_color
50+
51+
if use_cache:
52+
fg_color_cache[current_color] = new_color
5153
else:
5254
pixels[x, y] = self.get_bg_pixel(image, x, y)
5355

@@ -108,7 +110,7 @@ def apply_mask(self, image):
108110
# the individual pixel comparisons that the base class uses, which
109111
# would be a lot faster. (In fact doing this would probably remove
110112
# the need for the B&W optimization above.)
111-
QRColorMask.apply_mask(self, image)
113+
QRColorMask.apply_mask(self, image, use_cache=True)
112114

113115
def get_fg_pixel(self, image, x, y):
114116
return self.front_color

0 commit comments

Comments
 (0)