Merged _MODE_CONV typ into ImageMode as typestr

This commit is contained in:
Andrew Murray 2022-02-15 18:01:02 +11:00
parent 95c17a8334
commit a2313be894
3 changed files with 54 additions and 70 deletions

View File

@ -21,6 +21,7 @@ def test_sanity():
assert m.bands == ("1",) assert m.bands == ("1",)
assert m.basemode == "L" assert m.basemode == "L"
assert m.basetype == "L" assert m.basetype == "L"
assert m.typestr == "|b1"
for mode in ( for mode in (
"I;16", "I;16",
@ -45,6 +46,7 @@ def test_sanity():
assert m.bands == ("R", "G", "B") assert m.bands == ("R", "G", "B")
assert m.basemode == "RGB" assert m.basemode == "RGB"
assert m.basetype == "L" assert m.basetype == "L"
assert m.typestr == "|u1"
def test_properties(): def test_properties():

View File

@ -208,44 +208,14 @@ if sys.byteorder == "little":
else: else:
_ENDIAN = ">" _ENDIAN = ">"
_MODE_CONV = {
# official modes
"1": "|b1", # Bits need to be extended to bytes
"L": "|u1",
"LA": "|u1",
"I": _ENDIAN + "i4",
"F": _ENDIAN + "f4",
"P": "|u1",
"RGB": "|u1",
"RGBX": "|u1",
"RGBA": "|u1",
"CMYK": "|u1",
"YCbCr": "|u1",
"LAB": "|u1", # UNDONE - unsigned |u1i1i1
"HSV": "|u1",
# I;16 == I;16L, and I;32 == I;32L
"I;16": "<u2",
"I;16B": ">u2",
"I;16L": "<u2",
"I;16S": "<i2",
"I;16BS": ">i2",
"I;16LS": "<i2",
"I;32": "<u4",
"I;32B": ">u4",
"I;32L": "<u4",
"I;32S": "<i4",
"I;32BS": ">i4",
"I;32LS": "<i4",
}
def _conv_type_shape(im): def _conv_type_shape(im):
typ = _MODE_CONV[im.mode] m = ImageMode.getmode(im.mode)
extra = len(im.getbands()) shape = (im.size[1], im.size[0])
if extra == 1: extra = len(m.bands)
return (im.size[1], im.size[0]), typ if extra != 1:
else: shape += (extra,)
return (im.size[1], im.size[0], extra), typ return shape, m.typestr
MODES = ["1", "CMYK", "F", "HSV", "I", "L", "LAB", "P", "RGB", "RGBA", "RGBX", "YCbCr"] MODES = ["1", "CMYK", "F", "HSV", "I", "L", "LAB", "P", "RGB", "RGBA", "RGBX", "YCbCr"]

View File

@ -13,6 +13,8 @@
# See the README file for information on usage and redistribution. # See the README file for information on usage and redistribution.
# #
from PIL import Image
# mode descriptor cache # mode descriptor cache
_modes = None _modes = None
@ -20,11 +22,12 @@ _modes = None
class ModeDescriptor: class ModeDescriptor:
"""Wrapper for mode strings.""" """Wrapper for mode strings."""
def __init__(self, mode, bands, basemode, basetype): def __init__(self, mode, bands, basemode, basetype, typestr):
self.mode = mode self.mode = mode
self.bands = bands self.bands = bands
self.basemode = basemode self.basemode = basemode
self.basetype = basetype self.basetype = basetype
self.typestr = typestr
def __str__(self): def __str__(self):
return self.mode return self.mode
@ -36,43 +39,52 @@ def getmode(mode):
if not _modes: if not _modes:
# initialize mode cache # initialize mode cache
modes = {} modes = {}
for m, (basemode, basetype, bands) in { for m, (basemode, basetype, bands, typestr) in {
# core modes # core modes
"1": ("L", "L", ("1",)), # Bits need to be extended to bytes
"L": ("L", "L", ("L",)), "1": ("L", "L", ("1",), "|b1"),
"I": ("L", "I", ("I",)), "L": ("L", "L", ("L",), "|u1"),
"F": ("L", "F", ("F",)), "I": ("L", "I", ("I",), Image._ENDIAN + "i4"),
"P": ("P", "L", ("P",)), "F": ("L", "F", ("F",), Image._ENDIAN + "f4"),
"RGB": ("RGB", "L", ("R", "G", "B")), "P": ("P", "L", ("P",), "|u1"),
"RGBX": ("RGB", "L", ("R", "G", "B", "X")), "RGB": ("RGB", "L", ("R", "G", "B"), "|u1"),
"RGBA": ("RGB", "L", ("R", "G", "B", "A")), "RGBX": ("RGB", "L", ("R", "G", "B", "X"), "|u1"),
"CMYK": ("RGB", "L", ("C", "M", "Y", "K")), "RGBA": ("RGB", "L", ("R", "G", "B", "A"), "|u1"),
"YCbCr": ("RGB", "L", ("Y", "Cb", "Cr")), "CMYK": ("RGB", "L", ("C", "M", "Y", "K"), "|u1"),
"LAB": ("RGB", "L", ("L", "A", "B")), "YCbCr": ("RGB", "L", ("Y", "Cb", "Cr"), "|u1"),
"HSV": ("RGB", "L", ("H", "S", "V")), # UNDONE - unsigned |u1i1i1
"LAB": ("RGB", "L", ("L", "A", "B"), "|u1"),
"HSV": ("RGB", "L", ("H", "S", "V"), "|u1"),
# extra experimental modes # extra experimental modes
"RGBa": ("RGB", "L", ("R", "G", "B", "a")), "RGBa": ("RGB", "L", ("R", "G", "B", "a"), None),
"BGR;15": ("RGB", "L", ("B", "G", "R")), "BGR;15": ("RGB", "L", ("B", "G", "R"), None),
"BGR;16": ("RGB", "L", ("B", "G", "R")), "BGR;16": ("RGB", "L", ("B", "G", "R"), None),
"BGR;24": ("RGB", "L", ("B", "G", "R")), "BGR;24": ("RGB", "L", ("B", "G", "R"), None),
"BGR;32": ("RGB", "L", ("B", "G", "R")), "BGR;32": ("RGB", "L", ("B", "G", "R"), None),
"LA": ("L", "L", ("L", "A")), "LA": ("L", "L", ("L", "A"), "|u1"),
"La": ("L", "L", ("L", "a")), "La": ("L", "L", ("L", "a"), None),
"PA": ("RGB", "L", ("P", "A")), "PA": ("RGB", "L", ("P", "A"), "|u1"),
}.items(): }.items():
modes[m] = ModeDescriptor(m, bands, basemode, basetype) modes[m] = ModeDescriptor(m, bands, basemode, basetype, typestr)
# mapping modes # mapping modes
for i16mode in ( for i16mode, typestr in {
"I;16", # I;16 == I;16L, and I;32 == I;32L
"I;16S", "I;16": "<u2",
"I;16L", "I;16S": "<i2",
"I;16LS", "I;16L": "<u2",
"I;16B", "I;16LS": "<i2",
"I;16BS", "I;16B": ">u2",
"I;16N", "I;16BS": ">i2",
"I;16NS", "I;16N": Image._ENDIAN + "u2",
): "I;16NS": Image._ENDIAN + "i2",
modes[i16mode] = ModeDescriptor(i16mode, ("I",), "L", "L") "I;32": "<u4",
"I;32B": ">u4",
"I;32L": "<u4",
"I;32S": "<i4",
"I;32BS": ">i4",
"I;32LS": "<i4",
}.items():
modes[i16mode] = ModeDescriptor(i16mode, ("I",), "L", "L", typestr)
# set global mode cache atomically # set global mode cache atomically
_modes = modes _modes = modes
return _modes[mode] return _modes[mode]