1616# below for the original description.
1717from __future__ import annotations
1818
19+ import operator
1920import sys
20- from enum import IntEnum
21+ from enum import IntEnum , IntFlag
22+ from functools import reduce
2123
2224from . import Image
2325
@@ -119,6 +121,69 @@ class Direction(IntEnum):
119121#
120122# flags
121123
124+
125+ class Flags (IntFlag ):
126+ """Flags and documentation are taken from ``lcms2.h``."""
127+
128+ NONE = 0
129+ NOCACHE = 0x0040
130+ """Inhibit 1-pixel cache"""
131+ NOOPTIMIZE = 0x0100
132+ """Inhibit optimizations"""
133+ NULLTRANSFORM = 0x0200
134+ """Don't transform anyway"""
135+ GAMUTCHECK = 0x1000
136+ """Out of Gamut alarm"""
137+ SOFTPROOFING = 0x4000
138+ """Do softproofing"""
139+ BLACKPOINTCOMPENSATION = 0x2000
140+ NOWHITEONWHITEFIXUP = 0x0004
141+ """Don't fix scum dot"""
142+ HIGHRESPRECALC = 0x0400
143+ """Use more memory to give better accuracy"""
144+ LOWRESPRECALC = 0x0800
145+ """Use less memory to minimize resources"""
146+ # this should be 8BITS_DEVICELINK, but that is not a valid name in Python:
147+ USE_8BITS_DEVICELINK = 0x0008
148+ """Create 8 bits devicelinks"""
149+ GUESSDEVICECLASS = 0x0020
150+ """Guess device class (for ``transform2devicelink``)"""
151+ KEEP_SEQUENCE = 0x0080
152+ """Keep profile sequence for devicelink creation"""
153+ FORCE_CLUT = 0x0002
154+ """Force CLUT optimization"""
155+ CLUT_POST_LINEARIZATION = 0x0001
156+ """create postlinearization tables if possible"""
157+ CLUT_PRE_LINEARIZATION = 0x0010
158+ """create prelinearization tables if possible"""
159+ NONEGATIVES = 0x8000
160+ """Prevent negative numbers in floating point transforms"""
161+ COPY_ALPHA = 0x04000000
162+ """Alpha channels are copied on ``cmsDoTransform()``"""
163+ NODEFAULTRESOURCEDEF = 0x01000000
164+
165+ _GRIDPOINTS_1 = 1 << 16
166+ _GRIDPOINTS_2 = 2 << 16
167+ _GRIDPOINTS_4 = 4 << 16
168+ _GRIDPOINTS_8 = 8 << 16
169+ _GRIDPOINTS_16 = 16 << 16
170+ _GRIDPOINTS_32 = 32 << 16
171+ _GRIDPOINTS_64 = 64 << 16
172+ _GRIDPOINTS_128 = 128 << 16
173+
174+ @staticmethod
175+ def GRIDPOINTS (n : int ) -> Flags :
176+ """
177+ Fine-tune control over number of gridpoints
178+
179+ :param n: :py:class:`int` in range ``0 <= n <= 255``
180+ """
181+ return Flags .NONE | ((n & 0xFF ) << 16 )
182+
183+
184+ _MAX_FLAG = reduce (operator .or_ , Flags )
185+
186+
122187FLAGS = {
123188 "MATRIXINPUT" : 1 ,
124189 "MATRIXOUTPUT" : 2 ,
@@ -142,11 +207,6 @@ class Direction(IntEnum):
142207 "GRIDPOINTS" : lambda n : (n & 0xFF ) << 16 , # Gridpoints
143208}
144209
145- _MAX_FLAG = 0
146- for flag in FLAGS .values ():
147- if isinstance (flag , int ):
148- _MAX_FLAG = _MAX_FLAG | flag
149-
150210
151211# --------------------------------------------------------------------.
152212# Experimental PIL-level API
@@ -218,7 +278,7 @@ def __init__(
218278 intent = Intent .PERCEPTUAL ,
219279 proof = None ,
220280 proof_intent = Intent .ABSOLUTE_COLORIMETRIC ,
221- flags = 0 ,
281+ flags = Flags . NONE ,
222282 ):
223283 if proof is None :
224284 self .transform = core .buildTransform (
@@ -303,7 +363,7 @@ def profileToProfile(
303363 renderingIntent = Intent .PERCEPTUAL ,
304364 outputMode = None ,
305365 inPlace = False ,
306- flags = 0 ,
366+ flags = Flags . NONE ,
307367):
308368 """
309369 (pyCMS) Applies an ICC transformation to a given image, mapping from
@@ -420,7 +480,7 @@ def buildTransform(
420480 inMode ,
421481 outMode ,
422482 renderingIntent = Intent .PERCEPTUAL ,
423- flags = 0 ,
483+ flags = Flags . NONE ,
424484):
425485 """
426486 (pyCMS) Builds an ICC transform mapping from the ``inputProfile`` to the
@@ -482,7 +542,7 @@ def buildTransform(
482542 raise PyCMSError (msg )
483543
484544 if not isinstance (flags , int ) or not (0 <= flags <= _MAX_FLAG ):
485- msg = "flags must be an integer between 0 and %s" + _MAX_FLAG
545+ msg = f "flags must be an integer between 0 and { _MAX_FLAG } "
486546 raise PyCMSError (msg )
487547
488548 try :
@@ -505,7 +565,7 @@ def buildProofTransform(
505565 outMode ,
506566 renderingIntent = Intent .PERCEPTUAL ,
507567 proofRenderingIntent = Intent .ABSOLUTE_COLORIMETRIC ,
508- flags = FLAGS [ " SOFTPROOFING" ] ,
568+ flags = Flags . SOFTPROOFING ,
509569):
510570 """
511571 (pyCMS) Builds an ICC transform mapping from the ``inputProfile`` to the
@@ -586,7 +646,7 @@ def buildProofTransform(
586646 raise PyCMSError (msg )
587647
588648 if not isinstance (flags , int ) or not (0 <= flags <= _MAX_FLAG ):
589- msg = "flags must be an integer between 0 and %s" + _MAX_FLAG
649+ msg = f "flags must be an integer between 0 and { _MAX_FLAG } "
590650 raise PyCMSError (msg )
591651
592652 try :
0 commit comments