improve color cube parser

This commit is contained in:
Alexander 2018-03-30 11:29:59 +03:00
parent c1b956e3c8
commit 805dc44707
2 changed files with 23 additions and 10 deletions

View File

@ -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"

View File

@ -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