From 805dc447076dc580816d730d360de08917d86a9e Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 30 Mar 2018 11:29:59 +0300 Subject: [PATCH] improve color cube parser --- Tests/test_color_lut.py | 18 +++++++++++------- src/PIL/ImageFilter.py | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index fffd3341b..4aedd5b43 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -300,7 +300,6 @@ class TestColorLut3DFilter(PillowTestCase): def test_from_cube_file_minimal(self): lut = ImageFilter.Color3DLUT.from_cube_file([ "LUT_3D_SIZE 2", - "", "0 0 0.031", "0.96 0 0.031", "0 1 0.031", @@ -321,6 +320,7 @@ class TestColorLut3DFilter(PillowTestCase): 'TITLE "LUT name from file"', " LUT_3D_SIZE 2 3 4", " SKIP THIS", + "", " # Comment", "CHANNELS 4", "", @@ -343,25 +343,30 @@ class TestColorLut3DFilter(PillowTestCase): with self.assertRaisesRegexp(ValueError, "No size found"): lut = ImageFilter.Color3DLUT.from_cube_file([ 'TITLE "LUT name from file"', - "", ] + [ "0 0 0.031", "0.96 0 0.031", ] * 3) - with self.assertRaisesRegexp(ValueError, "number of colors on line 4"): + with self.assertRaisesRegexp(ValueError, "number of colors on line 3"): lut = ImageFilter.Color3DLUT.from_cube_file([ 'LUT_3D_SIZE 2', - "", ] + [ "0 0 0.031", "0.96 0 0.031 1", ] * 3) - with self.assertRaisesRegexp(ValueError, "Not a number on line 3"): + with self.assertRaisesRegexp(ValueError, "1D LUT cube files"): + lut = ImageFilter.Color3DLUT.from_cube_file([ + 'LUT_1D_SIZE 2', + ] + [ + "0 0 0.031", + "0.96 0 0.031 1", + ]) + + with self.assertRaisesRegexp(ValueError, "Not a number on line 2"): lut = ImageFilter.Color3DLUT.from_cube_file([ 'LUT_3D_SIZE 2', - "", ] + [ "0 green 0.031", "0.96 0 0.031", @@ -371,7 +376,6 @@ class TestColorLut3DFilter(PillowTestCase): with NamedTemporaryFile('w+t', delete=False) as f: f.write( "LUT_3D_SIZE 2\n" - "\n" "0 0 0.031\n" "0.96 0 0.031\n" "0 1 0.031\n" diff --git a/src/PIL/ImageFilter.py b/src/PIL/ImageFilter.py index 4fb3db381..8e193798d 100644 --- a/src/PIL/ImageFilter.py +++ b/src/PIL/ImageFilter.py @@ -16,6 +16,7 @@ # import functools +from itertools import chain from ._util import isPath @@ -402,8 +403,6 @@ class Color3DLUT(MultibandFilter): for i, line in enumerate(iterator, 1): line = line.strip() - if not line: - break if line.startswith('TITLE "'): name = line.split('"')[1] continue @@ -414,12 +413,22 @@ class Color3DLUT(MultibandFilter): continue if line.startswith('CHANNELS '): channels = int(line.split()[1]) + if line.startswith('LUT_1D_SIZE '): + raise ValueError("1D LUT cube files aren't supported.") + + try: + float(line.partition(' ')[0]) + except ValueError: + pass + else: + # Data starts + break if size is None: raise ValueError('No size found in the file') table = [] - for i, line in enumerate(iterator, i + 1): + for i, line in enumerate(chain([line], iterator), i): line = line.strip() if not line or line.startswith('#'): continue