From 91b08b7daa9b8bbdb951a16ff3f73c1e39879a37 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 12 Aug 2017 19:08:07 +0300 Subject: [PATCH] noop core.merge --- PIL/Image.py | 8 +++----- _imaging.c | 24 ++++++++++++++++++++++++ libImaging/Bands.c | 39 +++++++++++++++++++++++++++++++++++++++ libImaging/Imaging.h | 1 + 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 49c9f6ab2..5fdf7e99b 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -2612,11 +2612,9 @@ def merge(mode, bands): raise ValueError("mode mismatch") if im.size != bands[0].size: raise ValueError("size mismatch") - im = core.new(mode, bands[0].size) - for i in range(getmodebands(mode)): - bands[i].load() - im.putband(bands[i].im, i) - return bands[0]._new(im) + for band in bands: + band.load() + return bands[0]._new(core.merge(mode, *[b.im for b in bands])) # -------------------------------------------------------------------- diff --git a/_imaging.c b/_imaging.c index 7b3380b40..1dc803fc9 100644 --- a/_imaging.c +++ b/_imaging.c @@ -1905,6 +1905,29 @@ _putband(ImagingObject* self, PyObject* args) return Py_None; } +static PyObject* +_merge(PyObject* self, PyObject* args) +{ + char* mode; + ImagingObject *band0 = NULL; + ImagingObject *band1 = NULL; + ImagingObject *band2 = NULL; + ImagingObject *band3 = NULL; + Imaging bands[4] = {NULL, NULL, NULL, NULL}; + + if (!PyArg_ParseTuple(args, "sO!|O!O!O!", &mode, + &Imaging_Type, &band0, &Imaging_Type, &band1, + &Imaging_Type, &band2, &Imaging_Type, &band3)) + return NULL; + + if (band0) bands[0] = band0->image; + if (band1) bands[1] = band1->image; + if (band2) bands[2] = band2->image; + if (band3) bands[3] = band3->image; + + return PyImagingNew(ImagingMerge(mode, bands)); +} + /* -------------------------------------------------------------------- */ #ifdef WITH_IMAGECHOPS @@ -3316,6 +3339,7 @@ static PyMethodDef functions[] = { {"blend", (PyCFunction)_blend, 1}, {"fill", (PyCFunction)_fill, 1}, {"new", (PyCFunction)_new, 1}, + {"merge", (PyCFunction)_merge, 1}, {"getcount", (PyCFunction)_getcount, 1}, diff --git a/libImaging/Bands.c b/libImaging/Bands.c index d7424ab25..9dac17b68 100644 --- a/libImaging/Bands.c +++ b/libImaging/Bands.c @@ -127,3 +127,42 @@ ImagingFillBand(Imaging imOut, int band, int color) return imOut; } + +Imaging +ImagingMerge(const char* mode, Imaging bands[4]) +{ + int i; + int bandsCount = 0; + Imaging imOut; + Imaging firstBand; + + firstBand = bands[0]; + if ( ! firstBand) { + return (Imaging) ImagingError_ValueError("At least one band required"); + } + + for (i = 0; i < 4; ++i) { + if ( ! bands[i]) { + break; + } + if (bands[i]->bands != 1 || bands[i]->type != IMAGING_TYPE_UINT8) { + return (Imaging) ImagingError_ModeError(); + } + if (bands[i]->xsize != firstBand->xsize + || bands[i]->ysize != firstBand->ysize) { + return (Imaging) ImagingError_Mismatch(); + } + } + bandsCount = i; + + imOut = ImagingNew(mode, firstBand->xsize, firstBand->ysize); + if ( ! imOut) + return NULL; + + if (imOut->bands != bandsCount) { + ImagingDelete(imOut); + return (Imaging) ImagingError_ValueError("wrong number of bands"); + } + + return imOut; +} diff --git a/libImaging/Imaging.h b/libImaging/Imaging.h index 99fff7f67..f4ff3c345 100644 --- a/libImaging/Imaging.h +++ b/libImaging/Imaging.h @@ -267,6 +267,7 @@ extern Imaging ImagingFlipTopBottom(Imaging imOut, Imaging imIn); extern Imaging ImagingGaussianBlur(Imaging imOut, Imaging imIn, float radius, int passes); extern Imaging ImagingGetBand(Imaging im, int band); +extern Imaging ImagingMerge(const char* mode, Imaging bands[4]); extern int ImagingGetBBox(Imaging im, int bbox[4]); typedef struct { int x, y; INT32 count; INT32 pixel; } ImagingColorItem; extern ImagingColorItem* ImagingGetColors(Imaging im, int maxcolors,