Skip to content

Commit 9642192

Browse files
committed
use mode enums in _imagingcms.c
1 parent c3c968e commit 9642192

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ def debug_build() -> bool:
10131013
ext_modules = [
10141014
Extension("PIL._imaging", files, libraries=["pil_imaging_mode"]),
10151015
Extension("PIL._imagingft", ["src/_imagingft.c"], libraries=["pil_imaging_mode"]),
1016-
Extension("PIL._imagingcms", ["src/_imagingcms.c"]),
1016+
Extension("PIL._imagingcms", ["src/_imagingcms.c"], libraries=["pil_imaging_mode"]),
10171017
Extension("PIL._webp", ["src/_webp.c"], libraries=["pil_imaging_mode"]),
10181018
Extension("PIL._imagingtk", ["src/_imagingtk.c", "src/Tk/tkImaging.c"]),
10191019
Extension("PIL._imagingmath", ["src/_imagingmath.c"]),

src/_imagingcms.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -212,32 +212,44 @@ cms_transform_dealloc(CmsTransformObject *self) {
212212
/* internal functions */
213213

214214
static cmsUInt32Number
215-
findLCMStype(char *PILmode) {
216-
if (strcmp(PILmode, "RGB") == 0 || strcmp(PILmode, "RGBA") == 0 ||
217-
strcmp(PILmode, "RGBX") == 0) {
218-
return TYPE_RGBA_8;
215+
findLCMStype(const char *const mode_name) {
216+
const ModeID mode = findModeID(mode_name);
217+
switch (mode) {
218+
case IMAGING_MODE_RGB:
219+
case IMAGING_MODE_RGBA:
220+
case IMAGING_MODE_RGBX:
221+
return TYPE_RGBA_8;
222+
case IMAGING_MODE_CMYK:
223+
return TYPE_CMYK_8;
224+
case IMAGING_MODE_I_16:
225+
case IMAGING_MODE_I_16L:
226+
return TYPE_GRAY_16;
227+
case IMAGING_MODE_I_16B:
228+
return TYPE_GRAY_16_SE;
229+
case IMAGING_MODE_YCbCr:
230+
return TYPE_YCbCr_8;
231+
case IMAGING_MODE_LAB:
232+
// LabX equivalent like ALab, but not reversed -- no #define in lcms2
233+
return (
234+
COLORSPACE_SH(PT_LabV2) | CHANNELS_SH(3) | BYTES_SH(1) | EXTRA_SH(1)
235+
);
236+
default:
237+
// This function only accepts a subset of the imaging modes Pillow has.
238+
break;
219239
}
220-
if (strcmp(PILmode, "RGBA;16B") == 0) {
240+
// The following modes are not valid PIL Image modes.
241+
if (strcmp(mode_name, "RGBA;16B") == 0) {
221242
return TYPE_RGBA_16;
222243
}
223-
if (strcmp(PILmode, "CMYK") == 0) {
224-
return TYPE_CMYK_8;
225-
}
226-
if (strcmp(PILmode, "I;16") == 0 || strcmp(PILmode, "I;16L") == 0 ||
227-
strcmp(PILmode, "L;16") == 0) {
244+
if (strcmp(mode_name, "L;16") == 0) {
228245
return TYPE_GRAY_16;
229246
}
230-
if (strcmp(PILmode, "I;16B") == 0 || strcmp(PILmode, "L;16B") == 0) {
247+
if (strcmp(mode_name, "L;16B") == 0) {
231248
return TYPE_GRAY_16_SE;
232249
}
233-
if (strcmp(PILmode, "YCbCr") == 0 || strcmp(PILmode, "YCCA") == 0 ||
234-
strcmp(PILmode, "YCC") == 0) {
250+
if (strcmp(mode_name, "YCCA") == 0 || strcmp(mode_name, "YCC") == 0) {
235251
return TYPE_YCbCr_8;
236252
}
237-
if (strcmp(PILmode, "LAB") == 0) {
238-
// LabX equivalent like ALab, but not reversed -- no #define in lcms2
239-
return (COLORSPACE_SH(PT_LabV2) | CHANNELS_SH(3) | BYTES_SH(1) | EXTRA_SH(1));
240-
}
241253
/* presume "1" or "L" by default */
242254
return TYPE_GRAY_8;
243255
}

0 commit comments

Comments
 (0)