Returns (bitdepth, rescale) pair.
(palette, bitdepth, transparent, alpha, greyscale)
| 1024 | |
| 1025 | |
| 1026 | def check_bitdepth_rescale(palette, bitdepth, transparent, alpha, greyscale): |
| 1027 | """ |
| 1028 | Returns (bitdepth, rescale) pair. |
| 1029 | """ |
| 1030 | |
| 1031 | if palette: |
| 1032 | if len(bitdepth) != 1: |
| 1033 | raise ProtocolError("with palette, only a single bitdepth may be used") |
| 1034 | (bitdepth,) = bitdepth |
| 1035 | if bitdepth not in (1, 2, 4, 8): |
| 1036 | raise ProtocolError("with palette, bitdepth must be 1, 2, 4, or 8") |
| 1037 | if transparent is not None: |
| 1038 | raise ProtocolError("transparent and palette not compatible") |
| 1039 | if alpha: |
| 1040 | raise ProtocolError("alpha and palette not compatible") |
| 1041 | if greyscale: |
| 1042 | raise ProtocolError("greyscale and palette not compatible") |
| 1043 | return bitdepth, None |
| 1044 | |
| 1045 | # No palette, check for sBIT chunk generation. |
| 1046 | |
| 1047 | if greyscale and not alpha: |
| 1048 | # Single channel, L. |
| 1049 | (bitdepth,) = bitdepth |
| 1050 | if bitdepth in (1, 2, 4, 8, 16): |
| 1051 | return bitdepth, None |
| 1052 | if bitdepth > 8: |
| 1053 | targetbitdepth = 16 |
| 1054 | elif bitdepth == 3: |
| 1055 | targetbitdepth = 4 |
| 1056 | else: |
| 1057 | assert bitdepth in (5, 6, 7) |
| 1058 | targetbitdepth = 8 |
| 1059 | return targetbitdepth, [(bitdepth, targetbitdepth)] |
| 1060 | |
| 1061 | assert alpha or not greyscale |
| 1062 | |
| 1063 | depth_set = tuple(set(bitdepth)) |
| 1064 | if depth_set in [(8,), (16,)]: |
| 1065 | # No sBIT required. |
| 1066 | (bitdepth,) = depth_set |
| 1067 | return bitdepth, None |
| 1068 | |
| 1069 | targetbitdepth = (8, 16)[max(bitdepth) > 8] |
| 1070 | return targetbitdepth, [(b, targetbitdepth) for b in bitdepth] |
| 1071 | |
| 1072 | |
| 1073 | # Regex for decoding mode string |