diff --git a/src/_imaging.c b/src/_imaging.c index 3542fcdc5..0351250e9 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -580,11 +580,7 @@ getink(PyObject *color, Imaging im, char *ink) { memcpy(ink, &ftmp, sizeof(ftmp)); return ink; case IMAGING_TYPE_SPECIAL: - if (im->mode == IMAGING_MODE_I_16 - || im->mode == IMAGING_MODE_I_16L - || im->mode == IMAGING_MODE_I_16B - || im->mode == IMAGING_MODE_I_16N - ) { + if (isModeI16(im->mode)) { ink[0] = (UINT8)r; ink[1] = (UINT8)(r >> 8); ink[2] = ink[3] = 0; diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index e391fd4bb..0b4fc4d2b 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -68,7 +68,7 @@ typedef void (*hline_handler)(Imaging, int, int, int, int); static inline void point8(Imaging im, int x, int y, int ink) { if (x >= 0 && x < im->xsize && y >= 0 && y < im->ysize) { - if (strncmp(im->mode->name, "I;16", 4) == 0) { + if (isModeI16(im->mode)) { #ifdef WORDS_BIGENDIAN im->image8[y][x * 2] = (UINT8)(ink >> 8); im->image8[y][x * 2 + 1] = (UINT8)ink; @@ -118,7 +118,7 @@ hline8(Imaging im, int x0, int y0, int x1, int ink) { x1 = im->xsize - 1; } if (x0 <= x1) { - pixelwidth = strncmp(im->mode->name, "I;16", 4) == 0 ? 2 : 1; + pixelwidth = isModeI16(im->mode) ? 2 : 1; memset( im->image8[y0] + x0 * pixelwidth, (UINT8)ink, (x1 - x0 + 1) * pixelwidth ); @@ -659,17 +659,17 @@ DRAW draw32rgba = {point32rgba, hline32rgba, line32rgba, polygon32rgba}; /* Interface */ /* -------------------------------------------------------------------- */ -#define DRAWINIT() \ - if (im->image8) { \ - draw = &draw8; \ - if (strncmp(im->mode->name, "I;16", 4) == 0) { \ - ink = INK16(ink_); \ - } else { \ - ink = INK8(ink_); \ - } \ - } else { \ - draw = (op) ? &draw32rgba : &draw32; \ - memcpy(&ink, ink_, sizeof(ink)); \ +#define DRAWINIT() \ + if (im->image8) { \ + draw = &draw8; \ + if (isModeI16(im->mode)) { \ + ink = INK16(ink_); \ + } else { \ + ink = INK8(ink_); \ + } \ + } else { \ + draw = (op) ? &draw32rgba : &draw32; \ + memcpy(&ink, ink_, sizeof(ink)); \ } int diff --git a/src/libImaging/Geometry.c b/src/libImaging/Geometry.c index c141ad2a1..80ecd7cb6 100644 --- a/src/libImaging/Geometry.c +++ b/src/libImaging/Geometry.c @@ -41,7 +41,7 @@ ImagingFlipLeftRight(Imaging imOut, Imaging imIn) { ImagingSectionEnter(&cookie); if (imIn->image8) { - if (strncmp(imIn->mode->name, "I;16", 4) == 0) { + if (isModeI16(imIn->mode)) { FLIP_LEFT_RIGHT(UINT16, image8) } else { FLIP_LEFT_RIGHT(UINT8, image8) @@ -127,7 +127,7 @@ ImagingRotate90(Imaging imOut, Imaging imIn) { ImagingSectionEnter(&cookie); if (imIn->image8) { - if (strncmp(imIn->mode->name, "I;16", 4) == 0) { + if (isModeI16(imIn->mode)) { ROTATE_90(UINT16, image8); } else { ROTATE_90(UINT8, image8); @@ -186,7 +186,7 @@ ImagingTranspose(Imaging imOut, Imaging imIn) { ImagingSectionEnter(&cookie); if (imIn->image8) { - if (strncmp(imIn->mode->name, "I;16", 4) == 0) { + if (isModeI16(imIn->mode)) { TRANSPOSE(UINT16, image8); } else { TRANSPOSE(UINT8, image8); @@ -247,7 +247,7 @@ ImagingTransverse(Imaging imOut, Imaging imIn) { ImagingSectionEnter(&cookie); if (imIn->image8) { - if (strncmp(imIn->mode->name, "I;16", 4) == 0) { + if (isModeI16(imIn->mode)) { TRANSVERSE(UINT16, image8); } else { TRANSVERSE(UINT8, image8); @@ -291,7 +291,7 @@ ImagingRotate180(Imaging imOut, Imaging imIn) { yr = imIn->ysize - 1; if (imIn->image8) { - if (strncmp(imIn->mode->name, "I;16", 4) == 0) { + if (isModeI16(imIn->mode)) { ROTATE_180(UINT16, image8) } else { ROTATE_180(UINT8, image8) @@ -351,7 +351,7 @@ ImagingRotate270(Imaging imOut, Imaging imIn) { ImagingSectionEnter(&cookie); if (imIn->image8) { - if (strncmp(imIn->mode->name, "I;16", 4) == 0) { + if (isModeI16(imIn->mode)) { ROTATE_270(UINT16, image8); } else { ROTATE_270(UINT8, image8); diff --git a/src/libImaging/Mode.c b/src/libImaging/Mode.c index 85ba50e3f..a11b65905 100644 --- a/src/libImaging/Mode.c +++ b/src/libImaging/Mode.c @@ -152,3 +152,10 @@ const RawMode * findRawMode(const char * const name) { } return NULL; } + +int isModeI16(const Mode * const mode) { + return mode == IMAGING_MODE_I_16 + || mode == IMAGING_MODE_I_16L + || mode == IMAGING_MODE_I_16B + || mode == IMAGING_MODE_I_16N; +} diff --git a/src/libImaging/Mode.h b/src/libImaging/Mode.h index 36deddd02..d1035efe8 100644 --- a/src/libImaging/Mode.h +++ b/src/libImaging/Mode.h @@ -86,4 +86,6 @@ extern const RawMode * const IMAGING_RAWMODE_YCbCrK; const RawMode * findRawMode(const char * const name); +int isModeI16(const Mode * const mode); + #endif // __MODE_H__ diff --git a/src/libImaging/Paste.c b/src/libImaging/Paste.c index 86085942a..5d745d0c1 100644 --- a/src/libImaging/Paste.c +++ b/src/libImaging/Paste.c @@ -67,8 +67,8 @@ paste_mask_1( int x, y; if (imOut->image8) { - int in_i16 = strncmp(imIn->mode, "I;16", 4) == 0; - int out_i16 = strncmp(imOut->mode, "I;16", 4) == 0; + int in_i16 = isModeI16(imIn->mode); + int out_i16 = isModeI16(imOut->mode); for (y = 0; y < ysize; y++) { UINT8 *out = imOut->image8[y + dy] + dx; if (out_i16) { @@ -437,7 +437,7 @@ fill_mask_L( unsigned int tmp1; if (imOut->image8) { - int i16 = strncmp(imOut->mode, "I;16", 4) == 0; + int i16 = isModeI16(imOut->mode); for (y = 0; y < ysize; y++) { UINT8 *out = imOut->image8[y + dy] + dx; if (i16) { diff --git a/src/map.c b/src/map.c index c66702981..0e4cf6040 100644 --- a/src/map.c +++ b/src/map.c @@ -85,7 +85,7 @@ PyImaging_MapBuffer(PyObject *self, PyObject *args) { if (stride <= 0) { if (!strcmp(mode, "L") || !strcmp(mode, "P")) { stride = xsize; - } else if (!strncmp(mode, "I;16", 4)) { + } else if (isModeI16(mode)) { stride = xsize * 2; } else { stride = xsize * 4;