diff --git a/Tests/test_image_access.py b/Tests/test_image_access.py index bb30b462d2f..4e3435cfeec 100644 --- a/Tests/test_image_access.py +++ b/Tests/test_image_access.py @@ -282,7 +282,7 @@ def test_embeddable(self) -> None: home = sys.prefix.replace("\\", "\\\\") fh.write( f""" -#include "Python.h" +#include int main(int argc, char* argv[]) {{ diff --git a/setup.py b/setup.py index a85731db936..afe4738b501 100644 --- a/setup.py +++ b/setup.py @@ -347,7 +347,6 @@ def __iter__(self) -> Iterator[str]: ("disable-platform-guessing", None, "Disable platform guessing"), ("debug", None, "Debug logging"), ] - + [("add-imaging-libs=", None, "Add libs to _imaging build")] ) @staticmethod @@ -358,7 +357,6 @@ def initialize_options(self) -> None: self.disable_platform_guessing = self.check_configuration( "platform-guessing", "disable" ) - self.add_imaging_libs = "" build_ext.initialize_options(self) for x in self.feature: setattr(self, f"disable_{x}", self.check_configuration(x, "disable")) @@ -855,7 +853,6 @@ def build_extensions(self) -> None: # core library libs: list[str | bool | None] = [] - libs.extend(self.add_imaging_libs.split()) defs: list[tuple[str, str | None]] = [] if feature.get("tiff"): libs.append(feature.get("tiff")) diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index bf29fdba54c..b095b589cb8 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -181,7 +181,7 @@ def paste(self, im: Image.Image) -> None: image = im.im if not image.isblock() or im.mode != self.__mode: block = Image.core.new_block(self.__mode, im.size) - image.convert2(block, image) # convert directly between buffers + image.convert2(block) # convert directly between buffers ptr = block.ptr _pyimagingtkcall("PyImagingPhoto", self.__photo, ptr) diff --git a/src/Tk/tkImaging.c b/src/Tk/tkImaging.c index a36c3e0bdc4..0234d2c78f9 100644 --- a/src/Tk/tkImaging.c +++ b/src/Tk/tkImaging.c @@ -243,13 +243,9 @@ _dfunc(HMODULE lib_handle, const char *func_name) { * Set Python exception if we can't find `func_name` in `lib_handle`. * Returns function pointer or NULL if not present. */ - - char message[100]; - FARPROC func = GetProcAddress(lib_handle, func_name); if (func == NULL) { - sprintf(message, "Cannot load function %s", func_name); - PyErr_SetString(PyExc_RuntimeError, message); + PyErr_Format(PyExc_RuntimeError, "Cannot load function %s", func_name); } return func; } diff --git a/src/_imaging.c b/src/_imaging.c index 5d6d97bedab..ea533f94833 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -71,29 +71,20 @@ * See the README file for information on usage and redistribution. */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" - #ifdef HAVE_LIBJPEG -#include "jconfig.h" +#include #endif #ifdef HAVE_LIBZ -#include "zlib.h" +#include #endif #ifdef HAVE_LIBTIFF -#ifndef _TIFFIO_ #include #endif -#endif #include "libImaging/Imaging.h" -#define _USE_MATH_DEFINES -#include -#include - #undef VERBOSE #define B16(p, i) ((((int)p[(i)]) << 8) + p[(i) + 1]) @@ -101,7 +92,7 @@ #define S16(v) ((v) < 32768 ? (v) : ((v) - 65536)) /* -------------------------------------------------------------------- */ -/* OBJECT ADMINISTRATION */ +/* OBJECT ADMINISTRATION */ /* -------------------------------------------------------------------- */ typedef struct { @@ -207,22 +198,6 @@ ImagingSectionLeave(ImagingSectionCookie *cookie) { PyEval_RestoreThread((PyThreadState *)*cookie); } -/* -------------------------------------------------------------------- */ -/* BUFFER HANDLING */ -/* -------------------------------------------------------------------- */ -/* Python compatibility API */ - -int -PyImaging_CheckBuffer(PyObject *buffer) { - return PyObject_CheckBuffer(buffer); -} - -int -PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view) { - /* must call check_buffer first! */ - return PyObject_GetBuffer(buffer, view, PyBUF_SIMPLE); -} - /* -------------------------------------------------------------------- */ /* EXCEPTION REROUTING */ /* -------------------------------------------------------------------- */ @@ -244,12 +219,6 @@ static const char *no_palette = "image has no palette"; static const char *readonly = "image is readonly"; /* static const char* no_content = "image has no content"; */ -void * -ImagingError_OSError(void) { - PyErr_SetString(PyExc_OSError, "error when accessing file"); - return NULL; -} - void * ImagingError_MemoryError(void) { return PyErr_NoMemory(); @@ -275,13 +244,8 @@ ImagingError_ValueError(const char *message) { return NULL; } -void -ImagingError_Clear(void) { - PyErr_Clear(); -} - /* -------------------------------------------------------------------- */ -/* HELPERS */ +/* HELPERS */ /* -------------------------------------------------------------------- */ static int @@ -466,8 +430,7 @@ getpixel(Imaging im, ImagingAccess access, int x, int y) { } /* unknown type */ - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static char * @@ -631,7 +594,7 @@ getink(PyObject *color, Imaging im, char *ink) { } /* -------------------------------------------------------------------- */ -/* FACTORIES */ +/* FACTORIES */ /* -------------------------------------------------------------------- */ static PyObject * @@ -714,7 +677,7 @@ _radial_gradient(PyObject *self, PyObject *args) { } static PyObject * -_alpha_composite(ImagingObject *self, PyObject *args) { +_alpha_composite(PyObject *self, PyObject *args) { ImagingObject *imagep1; ImagingObject *imagep2; @@ -728,7 +691,7 @@ _alpha_composite(ImagingObject *self, PyObject *args) { } static PyObject * -_blend(ImagingObject *self, PyObject *args) { +_blend(PyObject *self, PyObject *args) { ImagingObject *imagep1; ImagingObject *imagep2; double alpha; @@ -922,17 +885,12 @@ _convert(ImagingObject *self, PyObject *args) { int dither = 0; ImagingObject *paletteimage = NULL; - if (!PyArg_ParseTuple(args, "s|iO", &mode, &dither, &paletteimage)) { + if (!PyArg_ParseTuple( + args, "s|iO!", &mode, &dither, &Imaging_Type, &paletteimage + )) { return NULL; } if (paletteimage != NULL) { - if (!PyImaging_Check(paletteimage)) { - PyObject_Print((PyObject *)paletteimage, stderr, 0); - PyErr_SetString( - PyExc_ValueError, "palette argument must be image with mode 'P'" - ); - return NULL; - } if (paletteimage->image->palette == NULL) { PyErr_SetString(PyExc_ValueError, "null palette"); return NULL; @@ -946,20 +904,16 @@ _convert(ImagingObject *self, PyObject *args) { static PyObject * _convert2(ImagingObject *self, PyObject *args) { - ImagingObject *imagep1; - ImagingObject *imagep2; - if (!PyArg_ParseTuple( - args, "O!O!", &Imaging_Type, &imagep1, &Imaging_Type, &imagep2 - )) { + ImagingObject *imagep; + if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; } - if (!ImagingConvert2(imagep1->image, imagep2->image)) { + if (!ImagingConvert2(imagep->image, self->image)) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -1207,8 +1161,7 @@ _getpixel(ImagingObject *self, PyObject *args) { } if (self->access == NULL) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return getpixel(self->image, self->access, x, y); @@ -1410,8 +1363,7 @@ _paste(ImagingObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -1571,7 +1523,6 @@ _putdata(ImagingObject *self, PyObject *args) { } else { seq = PySequence_Fast(data, must_be_sequence); if (!seq) { - PyErr_SetString(PyExc_TypeError, must_be_sequence); return NULL; } double value; @@ -1630,7 +1581,6 @@ _putdata(ImagingObject *self, PyObject *args) { /* 32-bit images */ seq = PySequence_Fast(data, must_be_sequence); if (!seq) { - PyErr_SetString(PyExc_TypeError, must_be_sequence); return NULL; } switch (image->type) { @@ -1684,8 +1634,7 @@ _putdata(ImagingObject *self, PyObject *args) { Py_XDECREF(seq); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -1745,8 +1694,7 @@ _putpalette(ImagingObject *self, PyObject *args) { self->image->palette->size = palettesize * 8 / bits; unpack(self->image->palette->palette, palette, self->image->palette->size); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -1770,8 +1718,7 @@ _putpalettealpha(ImagingObject *self, PyObject *args) { strcpy(self->image->palette->mode, "RGBA"); self->image->palette->palette[index * 4 + 3] = (UINT8)alpha; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -1798,8 +1745,7 @@ _putpalettealphas(ImagingObject *self, PyObject *args) { self->image->palette->palette[i * 4 + 3] = (UINT8)values[i]; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -1835,8 +1781,7 @@ _putpixel(ImagingObject *self, PyObject *args) { self->access->put_pixel(im, x, y, ink); } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -2003,8 +1948,7 @@ im_setmode(ImagingObject *self, PyObject *args) { } self->access = ImagingAccessNew(im); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -2067,8 +2011,7 @@ _transform(ImagingObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -2195,8 +2138,7 @@ _getbbox(ImagingObject *self, PyObject *args) { } if (!ImagingGetBBox(self->image, bbox, alpha_only)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return Py_BuildValue("iiii", bbox[0], bbox[1], bbox[2], bbox[3]); @@ -2276,8 +2218,7 @@ _getextrema(ImagingObject *self) { } } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -2340,8 +2281,7 @@ _fillband(ImagingObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -2356,8 +2296,7 @@ _putband(ImagingObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -2943,8 +2882,7 @@ _draw_arc(ImagingDrawObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -2981,8 +2919,7 @@ _draw_bitmap(ImagingDrawObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -3038,8 +2975,7 @@ _draw_chord(ImagingDrawObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -3093,8 +3029,7 @@ _draw_ellipse(ImagingDrawObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -3157,8 +3092,7 @@ _draw_lines(ImagingDrawObject *self, PyObject *args) { free(xy); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -3189,8 +3123,7 @@ _draw_points(ImagingDrawObject *self, PyObject *args) { free(xy); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } /* from outline.c */ @@ -3218,8 +3151,7 @@ _draw_outline(ImagingDrawObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -3275,8 +3207,7 @@ _draw_pieslice(ImagingDrawObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -3327,8 +3258,7 @@ _draw_polygon(ImagingDrawObject *self, PyObject *args) { free(ixy); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -3382,8 +3312,7 @@ _draw_rectangle(ImagingDrawObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static struct PyMethodDef _draw_methods[] = { @@ -3537,7 +3466,7 @@ _effect_spread(ImagingObject *self, PyObject *args) { } /* -------------------------------------------------------------------- */ -/* UTILITIES */ +/* UTILITIES */ /* -------------------------------------------------------------------- */ static PyObject * @@ -3573,7 +3502,7 @@ _getcodecstatus(PyObject *self, PyObject *args) { } /* -------------------------------------------------------------------- */ -/* DEBUGGING HELPERS */ +/* DEBUGGING HELPERS */ /* -------------------------------------------------------------------- */ static PyObject * @@ -3588,8 +3517,7 @@ _save_ppm(ImagingObject *self, PyObject *args) { return NULL; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } /* -------------------------------------------------------------------- */ @@ -3977,8 +3905,7 @@ _reset_stats(PyObject *self, PyObject *args) { arena->stats_freed_blocks = 0; MUTEX_UNLOCK(&ImagingDefaultArena.mutex); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -4038,8 +3965,7 @@ _set_alignment(PyObject *self, PyObject *args) { ImagingDefaultArena.alignment = alignment; MUTEX_UNLOCK(&ImagingDefaultArena.mutex); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -4063,8 +3989,7 @@ _set_block_size(PyObject *self, PyObject *args) { ImagingDefaultArena.block_size = block_size; MUTEX_UNLOCK(&ImagingDefaultArena.mutex); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -4092,8 +4017,7 @@ _set_blocks_max(PyObject *self, PyObject *args) { return ImagingError_MemoryError(); } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -4108,8 +4032,7 @@ _clear_cache(PyObject *self, PyObject *args) { ImagingMemoryClearCache(&ImagingDefaultArena, i); MUTEX_UNLOCK(&ImagingDefaultArena.mutex); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } /* -------------------------------------------------------------------- */ @@ -4197,19 +4120,19 @@ extern PyObject * PyImaging_GrabScreenX11(PyObject *self, PyObject *args); #endif -/* Experimental path stuff (in path.c) */ +/* Path object (in path.c) */ extern PyObject * PyPath_Create(ImagingObject *self, PyObject *args); -/* Experimental outline stuff (in outline.c) */ +/* Outline object (in outline.c) */ extern PyObject * PyOutline_Create(ImagingObject *self, PyObject *args); +/* MapBuffer implementation (in map.c) */ extern PyObject * PyImaging_MapBuffer(PyObject *self, PyObject *args); static PyMethodDef functions[] = { - /* Object factories */ {"alpha_composite", (PyCFunction)_alpha_composite, METH_VARARGS}, {"blend", (PyCFunction)_blend, METH_VARARGS}, @@ -4218,9 +4141,6 @@ static PyMethodDef functions[] = { {"new_block", (PyCFunction)_new_block, METH_VARARGS}, {"merge", (PyCFunction)_merge, METH_VARARGS}, - /* Functions */ - {"convert", (PyCFunction)_convert2, METH_VARARGS}, - /* Codecs */ {"bcn_decoder", (PyCFunction)PyImaging_BcnDecoderNew, METH_VARARGS}, {"bit_decoder", (PyCFunction)PyImaging_BitDecoderNew, METH_VARARGS}, diff --git a/src/_imagingcms.c b/src/_imagingcms.c index 1805ebde17f..e1cf627c3d5 100644 --- a/src/_imagingcms.c +++ b/src/_imagingcms.c @@ -26,15 +26,11 @@ kevin@cazabon.com\n\ https://www.cazabon.com\n\ " -#define PY_SSIZE_T_CLEAN -#include "Python.h" // Include before wchar.h so _GNU_SOURCE is set -#include "wchar.h" -#include "datetime.h" +#include "libImaging/Imaging.h" // Set _GNU_SOURCE in Python.h before wchar.h -#include "lcms2.h" -#include "libImaging/Imaging.h" - -#define PYCMSVERSION "1.0.0 pil" +#include +#include +#include /* version history */ @@ -620,12 +616,6 @@ cms_profile_is_intent_supported(CmsProfileObject *self, PyObject *args) { #ifdef _WIN32 -#ifdef _WIN64 -#define F_HANDLE "K" -#else -#define F_HANDLE "k" -#endif - static PyObject * cms_get_display_profile_win32(PyObject *self, PyObject *args) { char filename[MAX_PATH]; @@ -654,8 +644,7 @@ cms_get_display_profile_win32(PyObject *self, PyObject *args) { return PyUnicode_FromStringAndSize(filename, filename_size - 1); } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } #endif @@ -672,20 +661,17 @@ _profile_read_mlu(CmsProfileObject *self, cmsTagSignature info) { wchar_t *buf; if (!cmsIsTag(self->profile, info)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } mlu = cmsReadTag(self->profile, info); if (!mlu) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } len = cmsMLUgetWide(mlu, lc, cc, NULL, 0); if (len == 0) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } buf = malloc(len); @@ -723,14 +709,12 @@ _profile_read_signature(CmsProfileObject *self, cmsTagSignature info) { unsigned int *sig; if (!cmsIsTag(self->profile, info)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } sig = (unsigned int *)cmsReadTag(self->profile, info); if (!sig) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return _profile_read_int_as_string(*sig); @@ -780,14 +764,12 @@ _profile_read_ciexyz(CmsProfileObject *self, cmsTagSignature info, int multi) { cmsCIEXYZ *XYZ; if (!cmsIsTag(self->profile, info)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } XYZ = (cmsCIEXYZ *)cmsReadTag(self->profile, info); if (!XYZ) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } if (multi) { return _xyz3_py(XYZ); @@ -801,14 +783,12 @@ _profile_read_ciexyy_triple(CmsProfileObject *self, cmsTagSignature info) { cmsCIExyYTRIPLE *triple; if (!cmsIsTag(self->profile, info)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } triple = (cmsCIExyYTRIPLE *)cmsReadTag(self->profile, info); if (!triple) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } /* Note: lcms does all the heavy lifting and error checking (nr of @@ -835,21 +815,18 @@ _profile_read_named_color_list(CmsProfileObject *self, cmsTagSignature info) { PyObject *result; if (!cmsIsTag(self->profile, info)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } ncl = (cmsNAMEDCOLORLIST *)cmsReadTag(self->profile, info); if (ncl == NULL) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } n = cmsNamedColorCount(ncl); result = PyList_New(n); if (!result) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } for (i = 0; i < n; i++) { @@ -858,8 +835,7 @@ _profile_read_named_color_list(CmsProfileObject *self, cmsTagSignature info) { str = PyUnicode_FromString(name); if (str == NULL) { Py_DECREF(result); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } PyList_SET_ITEM(result, i, str); } @@ -926,8 +902,7 @@ _is_intent_supported(CmsProfileObject *self, int clut) { result = PyDict_New(); if (result == NULL) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } n = cmsGetSupportedIntents(INTENTS, intent_ids, intent_descs); @@ -957,8 +932,7 @@ _is_intent_supported(CmsProfileObject *self, int clut) { Py_XDECREF(id); Py_XDECREF(entry); Py_XDECREF(result); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } PyDict_SetItem(result, id, entry); Py_DECREF(id); @@ -1042,8 +1016,7 @@ cms_profile_getattr_creation_date(CmsProfileObject *self, void *closure) { result = cmsGetHeaderCreationDateTime(self->profile, &ct); if (!result) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return PyDateTime_FromDateAndTime( @@ -1141,8 +1114,7 @@ cms_profile_getattr_saturation_rendering_intent_gamut( static PyObject * cms_profile_getattr_red_colorant(CmsProfileObject *self, void *closure) { if (!cmsIsMatrixShaper(self->profile)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return _profile_read_ciexyz(self, cmsSigRedColorantTag, 0); } @@ -1150,8 +1122,7 @@ cms_profile_getattr_red_colorant(CmsProfileObject *self, void *closure) { static PyObject * cms_profile_getattr_green_colorant(CmsProfileObject *self, void *closure) { if (!cmsIsMatrixShaper(self->profile)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return _profile_read_ciexyz(self, cmsSigGreenColorantTag, 0); } @@ -1159,8 +1130,7 @@ cms_profile_getattr_green_colorant(CmsProfileObject *self, void *closure) { static PyObject * cms_profile_getattr_blue_colorant(CmsProfileObject *self, void *closure) { if (!cmsIsMatrixShaper(self->profile)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return _profile_read_ciexyz(self, cmsSigBlueColorantTag, 0); } @@ -1176,21 +1146,18 @@ cms_profile_getattr_media_white_point_temperature( cmsBool result; if (!cmsIsTag(self->profile, info)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } XYZ = (cmsCIEXYZ *)cmsReadTag(self->profile, info); if (XYZ == NULL || XYZ->X == 0) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } cmsXYZ2xyY(&xyY, XYZ); result = cmsTempFromWhitePoint(&tempK, &xyY); if (!result) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return PyFloat_FromDouble(tempK); } @@ -1229,8 +1196,7 @@ cms_profile_getattr_red_primary(CmsProfileObject *self, void *closure) { result = _calculate_rgb_primaries(self, &primaries); } if (!result) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return _xyz_py(&primaries.Red); @@ -1245,8 +1211,7 @@ cms_profile_getattr_green_primary(CmsProfileObject *self, void *closure) { result = _calculate_rgb_primaries(self, &primaries); } if (!result) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return _xyz_py(&primaries.Green); @@ -1261,8 +1226,7 @@ cms_profile_getattr_blue_primary(CmsProfileObject *self, void *closure) { result = _calculate_rgb_primaries(self, &primaries); } if (!result) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return _xyz_py(&primaries.Blue); @@ -1321,14 +1285,12 @@ cms_profile_getattr_icc_measurement_condition(CmsProfileObject *self, void *clos const char *geo; if (!cmsIsTag(self->profile, info)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } mc = (cmsICCMeasurementConditions *)cmsReadTag(self->profile, info); if (!mc) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } if (mc->Geometry == 1) { @@ -1362,14 +1324,12 @@ cms_profile_getattr_icc_viewing_condition(CmsProfileObject *self, void *closure) cmsTagSignature info = cmsSigViewingConditionsTag; if (!cmsIsTag(self->profile, info)) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } vc = (cmsICCViewingConditions *)cmsReadTag(self->profile, info); if (!vc) { - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } return Py_BuildValue( diff --git a/src/_imagingft.c b/src/_imagingft.c index d38279f3e4b..dcff54b6c5c 100644 --- a/src/_imagingft.c +++ b/src/_imagingft.c @@ -18,10 +18,8 @@ * Copyright (c) 1998-2007 by Secret Labs AB */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" -#include "thirdparty/pythoncapi_compat.h" #include "libImaging/Imaging.h" +#include "thirdparty/pythoncapi_compat.h" #include #include FT_FREETYPE_H @@ -1377,8 +1375,7 @@ font_setvarname(FontObject *self, PyObject *args) { return geterror(error); } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -1432,8 +1429,7 @@ font_setvaraxes(FontObject *self, PyObject *args) { return geterror(error); } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } #endif diff --git a/src/_imagingmath.c b/src/_imagingmath.c index dbe636707f4..d54c6db56c3 100644 --- a/src/_imagingmath.c +++ b/src/_imagingmath.c @@ -13,12 +13,9 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" - #include "libImaging/Imaging.h" -#include "math.h" -#include "float.h" +#include #define MAX_INT32 2147483647.0 #define MIN_INT32 -2147483648.0 @@ -63,8 +60,8 @@ #define SUB(type, v1, v2) (v1) - (v2) #define MUL(type, v1, v2) (v1) * (v2) -#define MIN(type, v1, v2) ((v1) < (v2)) ? (v1) : (v2) -#define MAX(type, v1, v2) ((v1) > (v2)) ? (v1) : (v2) +#define MINOP(type, v1, v2) ((v1) < (v2)) ? (v1) : (v2) +#define MAXOP(type, v1, v2) ((v1) > (v2)) ? (v1) : (v2) #define AND(type, v1, v2) (v1) & (v2) #define OR(type, v1, v2) (v1) | (v2) @@ -134,8 +131,8 @@ BINOP(xor_I, XOR, INT32) BINOP(lshift_I, LSHIFT, INT32) BINOP(rshift_I, RSHIFT, INT32) -BINOP(min_I, MIN, INT32) -BINOP(max_I, MAX, INT32) +BINOP(min_I, MINOP, INT32) +BINOP(max_I, MAXOP, INT32) BINOP(eq_I, EQ, INT32) BINOP(ne_I, NE, INT32) @@ -155,8 +152,8 @@ BINOP(mod_F, MOD_F, FLOAT32) BINOP(pow_F, POW_F, FLOAT32) BINOP(diff_F, DIFF_F, FLOAT32) -BINOP(min_F, MIN, FLOAT32) -BINOP(max_F, MAX, FLOAT32) +BINOP(min_F, MINOP, FLOAT32) +BINOP(max_F, MAXOP, FLOAT32) BINOP(eq_F, EQ, FLOAT32) BINOP(ne_F, NE, FLOAT32) @@ -192,8 +189,7 @@ _unop(PyObject *self, PyObject *args) { unop(out, im1); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -226,8 +222,7 @@ _binop(PyObject *self, PyObject *args) { binop(out, im1, im2); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyMethodDef _functions[] = { diff --git a/src/_imagingtk.c b/src/_imagingtk.c index c70d044bb86..03bda916378 100644 --- a/src/_imagingtk.c +++ b/src/_imagingtk.c @@ -12,7 +12,6 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" #include "libImaging/Imaging.h" #include "Tk/_tkmini.h" @@ -37,8 +36,7 @@ _tkinit(PyObject *self, PyObject *args) { /* This will bomb if interp is invalid... */ TkImaging_Init(interp); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyMethodDef functions[] = { diff --git a/src/_webp.c b/src/_webp.c index dfda7048de4..cf6e999b1d6 100644 --- a/src/_webp.c +++ b/src/_webp.c @@ -1,6 +1,5 @@ -#define PY_SSIZE_T_CLEAN -#include #include "libImaging/Imaging.h" + #include #include #include diff --git a/src/decode.c b/src/decode.c index 51d0aced2bd..1660741d383 100644 --- a/src/decode.c +++ b/src/decode.c @@ -29,11 +29,7 @@ /* FIXME: make these pluggable! */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" - #include "libImaging/Imaging.h" - #include "libImaging/Bit.h" #include "libImaging/Bcn.h" #include "libImaging/Gif.h" @@ -213,8 +209,7 @@ _setimage(ImagingDecoderObject *decoder, PyObject *args) { Py_XDECREF(decoder->lock); decoder->lock = op; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -231,8 +226,7 @@ _setfd(ImagingDecoderObject *decoder, PyObject *args) { Py_XINCREF(fd); state->fd = fd; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -800,74 +794,6 @@ PyImaging_ZipDecoderNew(PyObject *self, PyObject *args) { } #endif -/* -------------------------------------------------------------------- */ -/* JPEG */ -/* -------------------------------------------------------------------- */ - -#ifdef HAVE_LIBJPEG - -/* We better define this decoder last in this file, so the following - undef's won't mess things up for the Imaging library proper. */ - -#undef HAVE_PROTOTYPES -#undef HAVE_STDDEF_H -#undef HAVE_STDLIB_H -#undef UINT8 -#undef UINT16 -#undef UINT32 -#undef INT8 -#undef INT16 -#undef INT32 - -#include "libImaging/Jpeg.h" - -PyObject * -PyImaging_JpegDecoderNew(PyObject *self, PyObject *args) { - ImagingDecoderObject *decoder; - - char *mode; - char *rawmode; /* what we want from the decoder */ - char *jpegmode; /* what's in the file */ - int scale = 1; - int draft = 0; - - if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode, &scale, &draft)) { - return NULL; - } - - if (!jpegmode) { - jpegmode = ""; - } - - decoder = PyImaging_DecoderNew(sizeof(JPEGSTATE)); - if (decoder == NULL) { - return NULL; - } - - // libjpeg-turbo supports different output formats. - // We are choosing Pillow's native format (3 color bytes + 1 padding) - // to avoid extra conversion in Unpack.c. - if (ImagingJpegUseJCSExtensions() && strcmp(rawmode, "RGB") == 0) { - rawmode = "RGBX"; - } - - if (get_unpacker(decoder, mode, rawmode) < 0) { - return NULL; - } - - decoder->decode = ImagingJpegDecode; - decoder->cleanup = ImagingJpegDecodeCleanup; - - strncpy(((JPEGSTATE *)decoder->state.context)->rawmode, rawmode, 8); - strncpy(((JPEGSTATE *)decoder->state.context)->jpegmode, jpegmode, 8); - - ((JPEGSTATE *)decoder->state.context)->scale = scale; - ((JPEGSTATE *)decoder->state.context)->draft = draft; - - return (PyObject *)decoder; -} -#endif - /* -------------------------------------------------------------------- */ /* JPEG 2000 */ /* -------------------------------------------------------------------- */ @@ -925,3 +851,61 @@ PyImaging_Jpeg2KDecoderNew(PyObject *self, PyObject *args) { return (PyObject *)decoder; } #endif /* HAVE_OPENJPEG */ + +/* -------------------------------------------------------------------- */ +/* JPEG */ +/* -------------------------------------------------------------------- */ + +#ifdef HAVE_LIBJPEG + +/* We better define this decoder last in this file, so the undef's + in Jpeg.h won't mess things up for the Imaging library proper. */ + +#include "libImaging/Jpeg.h" + +PyObject * +PyImaging_JpegDecoderNew(PyObject *self, PyObject *args) { + ImagingDecoderObject *decoder; + + char *mode; + char *rawmode; /* what we want from the decoder */ + char *jpegmode; /* what's in the file */ + int scale = 1; + int draft = 0; + + if (!PyArg_ParseTuple(args, "ssz|ii", &mode, &rawmode, &jpegmode, &scale, &draft)) { + return NULL; + } + + if (!jpegmode) { + jpegmode = ""; + } + + decoder = PyImaging_DecoderNew(sizeof(JPEGSTATE)); + if (decoder == NULL) { + return NULL; + } + + // libjpeg-turbo supports different output formats. + // We are choosing Pillow's native format (3 color bytes + 1 padding) + // to avoid extra conversion in Unpack.c. + if (ImagingJpegUseJCSExtensions() && strcmp(rawmode, "RGB") == 0) { + rawmode = "RGBX"; + } + + if (get_unpacker(decoder, mode, rawmode) < 0) { + return NULL; + } + + decoder->decode = ImagingJpegDecode; + decoder->cleanup = ImagingJpegDecodeCleanup; + + strncpy(((JPEGSTATE *)decoder->state.context)->rawmode, rawmode, 8); + strncpy(((JPEGSTATE *)decoder->state.context)->jpegmode, jpegmode, 8); + + ((JPEGSTATE *)decoder->state.context)->scale = scale; + ((JPEGSTATE *)decoder->state.context)->draft = draft; + + return (PyObject *)decoder; +} +#endif diff --git a/src/display.c b/src/display.c index eed75975d71..cf133ca86ae 100644 --- a/src/display.c +++ b/src/display.c @@ -22,9 +22,6 @@ * See the README file for information on usage and redistribution. */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" - #include "libImaging/Imaging.h" /* -------------------------------------------------------------------- */ @@ -34,12 +31,6 @@ #include "libImaging/ImDib.h" -#if SIZEOF_VOID_P == 8 -#define F_HANDLE "K" -#else -#define F_HANDLE "k" -#endif - typedef struct { PyObject_HEAD ImagingDIB dib; } ImagingDisplayObject; @@ -85,8 +76,7 @@ _expose(ImagingDisplayObject *display, PyObject *args) { ImagingExposeDIB(display->dib, hdc); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -112,8 +102,7 @@ _draw(ImagingDisplayObject *display, PyObject *args) { ImagingDrawDIB(display->dib, hdc, dst, src); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } extern Imaging @@ -143,8 +132,7 @@ _paste(ImagingDisplayObject *display, PyObject *args) { ImagingPasteDIB(display->dib, im, xy); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -190,8 +178,7 @@ _releasedc(ImagingDisplayObject *display, PyObject *args) { ReleaseDC(window, dc); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -211,8 +198,7 @@ _frombytes(ImagingDisplayObject *display, PyObject *args) { memcpy(display->dib->bits, buffer.buf, buffer.len); PyBuffer_Release(&buffer); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -709,8 +695,7 @@ PyImaging_EventLoopWin32(PyObject *self, PyObject *args) { } Py_END_ALLOW_THREADS; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } /* -------------------------------------------------------------------- */ diff --git a/src/encode.c b/src/encode.c index d369a1b4598..244e218c4d8 100644 --- a/src/encode.c +++ b/src/encode.c @@ -22,11 +22,8 @@ /* FIXME: make these pluggable! */ -#define PY_SSIZE_T_CLEAN -#include "Python.h" - -#include "thirdparty/pythoncapi_compat.h" #include "libImaging/Imaging.h" +#include "thirdparty/pythoncapi_compat.h" #include "libImaging/Gif.h" #ifdef HAVE_UNISTD_H @@ -278,8 +275,7 @@ _setimage(ImagingEncoderObject *encoder, PyObject *args) { Py_XDECREF(encoder->lock); encoder->lock = op; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -296,8 +292,7 @@ _setfd(ImagingEncoderObject *encoder, PyObject *args) { Py_XINCREF(fd); state->fd = fd; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -986,24 +981,213 @@ PyImaging_LibTiffEncoderNew(PyObject *self, PyObject *args) { #endif +/* -------------------------------------------------------------------- */ +/* JPEG 2000 */ +/* -------------------------------------------------------------------- */ + +#ifdef HAVE_OPENJPEG + +#include "libImaging/Jpeg2K.h" + +static void +j2k_decode_coord_tuple(PyObject *tuple, int *x, int *y) { + *x = *y = 0; + + if (tuple && PyTuple_Check(tuple) && PyTuple_GET_SIZE(tuple) == 2) { + *x = (int)PyLong_AsLong(PyTuple_GET_ITEM(tuple, 0)); + *y = (int)PyLong_AsLong(PyTuple_GET_ITEM(tuple, 1)); + + if (*x < 0) { + *x = 0; + } + if (*y < 0) { + *y = 0; + } + } +} + +PyObject * +PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) { + ImagingEncoderObject *encoder; + JPEG2KENCODESTATE *context; + + char *mode; + char *format; + OPJ_CODEC_FORMAT codec_format; + PyObject *offset = NULL, *tile_offset = NULL, *tile_size = NULL; + char *quality_mode = "rates"; + PyObject *quality_layers = NULL; + Py_ssize_t num_resolutions = 0; + PyObject *cblk_size = NULL, *precinct_size = NULL; + PyObject *irreversible = NULL; + char *progression = "LRCP"; + OPJ_PROG_ORDER prog_order; + char *cinema_mode = "no"; + OPJ_CINEMA_MODE cine_mode; + char mct = 0; + int sgnd = 0; + Py_ssize_t fd = -1; + char *comment; + Py_ssize_t comment_size; + int plt = 0; + + if (!PyArg_ParseTuple( + args, + "ss|OOOsOnOOOssbbnz#p", + &mode, + &format, + &offset, + &tile_offset, + &tile_size, + &quality_mode, + &quality_layers, + &num_resolutions, + &cblk_size, + &precinct_size, + &irreversible, + &progression, + &cinema_mode, + &mct, + &sgnd, + &fd, + &comment, + &comment_size, + &plt + )) { + return NULL; + } + + if (strcmp(format, "j2k") == 0) { + codec_format = OPJ_CODEC_J2K; + } else if (strcmp(format, "jpt") == 0) { + codec_format = OPJ_CODEC_JPT; + } else if (strcmp(format, "jp2") == 0) { + codec_format = OPJ_CODEC_JP2; + } else { + return NULL; + } + + if (strcmp(progression, "LRCP") == 0) { + prog_order = OPJ_LRCP; + } else if (strcmp(progression, "RLCP") == 0) { + prog_order = OPJ_RLCP; + } else if (strcmp(progression, "RPCL") == 0) { + prog_order = OPJ_RPCL; + } else if (strcmp(progression, "PCRL") == 0) { + prog_order = OPJ_PCRL; + } else if (strcmp(progression, "CPRL") == 0) { + prog_order = OPJ_CPRL; + } else { + return NULL; + } + + if (strcmp(cinema_mode, "no") == 0) { + cine_mode = OPJ_OFF; + } else if (strcmp(cinema_mode, "cinema2k-24") == 0) { + cine_mode = OPJ_CINEMA2K_24; + } else if (strcmp(cinema_mode, "cinema2k-48") == 0) { + cine_mode = OPJ_CINEMA2K_48; + } else if (strcmp(cinema_mode, "cinema4k-24") == 0) { + cine_mode = OPJ_CINEMA4K_24; + } else { + return NULL; + } + + encoder = PyImaging_EncoderNew(sizeof(JPEG2KENCODESTATE)); + if (!encoder) { + return NULL; + } + + encoder->encode = ImagingJpeg2KEncode; + encoder->cleanup = ImagingJpeg2KEncodeCleanup; + encoder->pushes_fd = 1; + + context = (JPEG2KENCODESTATE *)encoder->state.context; + + context->fd = fd; + context->format = codec_format; + context->offset_x = context->offset_y = 0; + + j2k_decode_coord_tuple(offset, &context->offset_x, &context->offset_y); + j2k_decode_coord_tuple( + tile_offset, &context->tile_offset_x, &context->tile_offset_y + ); + j2k_decode_coord_tuple(tile_size, &context->tile_size_x, &context->tile_size_y); + + /* Error on illegal tile offsets */ + if (context->tile_size_x && context->tile_size_y) { + if (context->tile_offset_x <= context->offset_x - context->tile_size_x || + context->tile_offset_y <= context->offset_y - context->tile_size_y) { + PyErr_SetString( + PyExc_ValueError, + "JPEG 2000 tile offset too small; top left tile must " + "intersect image area" + ); + Py_DECREF(encoder); + return NULL; + } + + if (context->tile_offset_x > context->offset_x || + context->tile_offset_y > context->offset_y) { + PyErr_SetString( + PyExc_ValueError, "JPEG 2000 tile offset too large to cover image area" + ); + Py_DECREF(encoder); + return NULL; + } + } + + if (comment && comment_size > 0) { + /* Size is stored as as an uint16, subtract 4 bytes for the header */ + if (comment_size >= 65532) { + PyErr_SetString(PyExc_ValueError, "JPEG 2000 comment is too long"); + Py_DECREF(encoder); + return NULL; + } + + char *p = malloc(comment_size + 1); + if (!p) { + Py_DECREF(encoder); + return ImagingError_MemoryError(); + } + memcpy(p, comment, comment_size); + p[comment_size] = '\0'; + context->comment = p; + } + + if (quality_layers && PySequence_Check(quality_layers)) { + context->quality_is_in_db = strcmp(quality_mode, "dB") == 0; + context->quality_layers = quality_layers; + Py_INCREF(quality_layers); + } + + context->num_resolutions = num_resolutions; + + j2k_decode_coord_tuple(cblk_size, &context->cblk_width, &context->cblk_height); + j2k_decode_coord_tuple( + precinct_size, &context->precinct_width, &context->precinct_height + ); + + context->irreversible = PyObject_IsTrue(irreversible); + context->progression = prog_order; + context->cinema_mode = cine_mode; + context->mct = mct; + context->sgnd = sgnd; + context->plt = plt; + + return (PyObject *)encoder; +} + +#endif + /* -------------------------------------------------------------------- */ /* JPEG */ /* -------------------------------------------------------------------- */ #ifdef HAVE_LIBJPEG -/* We better define this encoder last in this file, so the following - undef's won't mess things up for the Imaging library proper. */ - -#undef HAVE_PROTOTYPES -#undef HAVE_STDDEF_H -#undef HAVE_STDLIB_H -#undef UINT8 -#undef UINT16 -#undef UINT32 -#undef INT8 -#undef INT16 -#undef INT32 +/* We better define this decoder last in this file, so the undef's + in Jpeg.h won't mess things up for the Imaging library proper. */ #include "libImaging/Jpeg.h" @@ -1216,202 +1400,3 @@ PyImaging_JpegEncoderNew(PyObject *self, PyObject *args) { } #endif - -/* -------------------------------------------------------------------- */ -/* JPEG 2000 */ -/* -------------------------------------------------------------------- */ - -#ifdef HAVE_OPENJPEG - -#include "libImaging/Jpeg2K.h" - -static void -j2k_decode_coord_tuple(PyObject *tuple, int *x, int *y) { - *x = *y = 0; - - if (tuple && PyTuple_Check(tuple) && PyTuple_GET_SIZE(tuple) == 2) { - *x = (int)PyLong_AsLong(PyTuple_GET_ITEM(tuple, 0)); - *y = (int)PyLong_AsLong(PyTuple_GET_ITEM(tuple, 1)); - - if (*x < 0) { - *x = 0; - } - if (*y < 0) { - *y = 0; - } - } -} - -PyObject * -PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) { - ImagingEncoderObject *encoder; - JPEG2KENCODESTATE *context; - - char *mode; - char *format; - OPJ_CODEC_FORMAT codec_format; - PyObject *offset = NULL, *tile_offset = NULL, *tile_size = NULL; - char *quality_mode = "rates"; - PyObject *quality_layers = NULL; - Py_ssize_t num_resolutions = 0; - PyObject *cblk_size = NULL, *precinct_size = NULL; - PyObject *irreversible = NULL; - char *progression = "LRCP"; - OPJ_PROG_ORDER prog_order; - char *cinema_mode = "no"; - OPJ_CINEMA_MODE cine_mode; - char mct = 0; - int sgnd = 0; - Py_ssize_t fd = -1; - char *comment; - Py_ssize_t comment_size; - int plt = 0; - - if (!PyArg_ParseTuple( - args, - "ss|OOOsOnOOOssbbnz#p", - &mode, - &format, - &offset, - &tile_offset, - &tile_size, - &quality_mode, - &quality_layers, - &num_resolutions, - &cblk_size, - &precinct_size, - &irreversible, - &progression, - &cinema_mode, - &mct, - &sgnd, - &fd, - &comment, - &comment_size, - &plt - )) { - return NULL; - } - - if (strcmp(format, "j2k") == 0) { - codec_format = OPJ_CODEC_J2K; - } else if (strcmp(format, "jpt") == 0) { - codec_format = OPJ_CODEC_JPT; - } else if (strcmp(format, "jp2") == 0) { - codec_format = OPJ_CODEC_JP2; - } else { - return NULL; - } - - if (strcmp(progression, "LRCP") == 0) { - prog_order = OPJ_LRCP; - } else if (strcmp(progression, "RLCP") == 0) { - prog_order = OPJ_RLCP; - } else if (strcmp(progression, "RPCL") == 0) { - prog_order = OPJ_RPCL; - } else if (strcmp(progression, "PCRL") == 0) { - prog_order = OPJ_PCRL; - } else if (strcmp(progression, "CPRL") == 0) { - prog_order = OPJ_CPRL; - } else { - return NULL; - } - - if (strcmp(cinema_mode, "no") == 0) { - cine_mode = OPJ_OFF; - } else if (strcmp(cinema_mode, "cinema2k-24") == 0) { - cine_mode = OPJ_CINEMA2K_24; - } else if (strcmp(cinema_mode, "cinema2k-48") == 0) { - cine_mode = OPJ_CINEMA2K_48; - } else if (strcmp(cinema_mode, "cinema4k-24") == 0) { - cine_mode = OPJ_CINEMA4K_24; - } else { - return NULL; - } - - encoder = PyImaging_EncoderNew(sizeof(JPEG2KENCODESTATE)); - if (!encoder) { - return NULL; - } - - encoder->encode = ImagingJpeg2KEncode; - encoder->cleanup = ImagingJpeg2KEncodeCleanup; - encoder->pushes_fd = 1; - - context = (JPEG2KENCODESTATE *)encoder->state.context; - - context->fd = fd; - context->format = codec_format; - context->offset_x = context->offset_y = 0; - - j2k_decode_coord_tuple(offset, &context->offset_x, &context->offset_y); - j2k_decode_coord_tuple( - tile_offset, &context->tile_offset_x, &context->tile_offset_y - ); - j2k_decode_coord_tuple(tile_size, &context->tile_size_x, &context->tile_size_y); - - /* Error on illegal tile offsets */ - if (context->tile_size_x && context->tile_size_y) { - if (context->tile_offset_x <= context->offset_x - context->tile_size_x || - context->tile_offset_y <= context->offset_y - context->tile_size_y) { - PyErr_SetString( - PyExc_ValueError, - "JPEG 2000 tile offset too small; top left tile must " - "intersect image area" - ); - Py_DECREF(encoder); - return NULL; - } - - if (context->tile_offset_x > context->offset_x || - context->tile_offset_y > context->offset_y) { - PyErr_SetString( - PyExc_ValueError, "JPEG 2000 tile offset too large to cover image area" - ); - Py_DECREF(encoder); - return NULL; - } - } - - if (comment && comment_size > 0) { - /* Size is stored as as an uint16, subtract 4 bytes for the header */ - if (comment_size >= 65532) { - PyErr_SetString(PyExc_ValueError, "JPEG 2000 comment is too long"); - Py_DECREF(encoder); - return NULL; - } - - char *p = malloc(comment_size + 1); - if (!p) { - Py_DECREF(encoder); - return ImagingError_MemoryError(); - } - memcpy(p, comment, comment_size); - p[comment_size] = '\0'; - context->comment = p; - } - - if (quality_layers && PySequence_Check(quality_layers)) { - context->quality_is_in_db = strcmp(quality_mode, "dB") == 0; - context->quality_layers = quality_layers; - Py_INCREF(quality_layers); - } - - context->num_resolutions = num_resolutions; - - j2k_decode_coord_tuple(cblk_size, &context->cblk_width, &context->cblk_height); - j2k_decode_coord_tuple( - precinct_size, &context->precinct_width, &context->precinct_height - ); - - context->irreversible = PyObject_IsTrue(irreversible); - context->progression = prog_order; - context->cinema_mode = cine_mode; - context->mct = mct; - context->sgnd = sgnd; - context->plt = plt; - - return (PyObject *)encoder; -} - -#endif diff --git a/src/libImaging/BoxBlur.c b/src/libImaging/BoxBlur.c index ed91541fed4..41fb5f0d90f 100644 --- a/src/libImaging/BoxBlur.c +++ b/src/libImaging/BoxBlur.c @@ -1,8 +1,5 @@ #include "Imaging.h" -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) -#define MIN(x, y) (((x) < (y)) ? (x) : (y)) - typedef UINT8 pixel[4]; void static inline ImagingLineBoxBlur32( diff --git a/src/libImaging/ColorLUT.c b/src/libImaging/ColorLUT.c index 5559de689b2..33633f555d1 100644 --- a/src/libImaging/ColorLUT.c +++ b/src/libImaging/ColorLUT.c @@ -1,5 +1,4 @@ #include "Imaging.h" -#include /* 8 bits for result. Table can overflow [0, 1.0] range, so we need extra bits for overflow and negative values. diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index c8f23426105..a5b2b89a7bc 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -34,9 +34,6 @@ #include "Imaging.h" -#define MAX(a, b) (a) > (b) ? (a) : (b) -#define MIN(a, b) (a) < (b) ? (a) : (b) - #define CLIP16(v) ((v) <= 0 ? 0 : (v) >= 65535 ? 65535 : (v)) /* ITU-R Recommendation 601-2 (assuming nonlinear RGB) */ @@ -1672,15 +1669,9 @@ convert( } if (!convert) { -#ifdef notdef - return (Imaging)ImagingError_ValueError("conversion not supported"); -#else - static char buf[100]; - snprintf( - buf, 100, "conversion from %.10s to %.10s not supported", imIn->mode, mode + return (Imaging)PyErr_Format( + PyExc_ValueError, "conversion from %s to %s not supported", imIn->mode, mode ); - return (Imaging)ImagingError_ValueError(buf); -#endif } imOut = ImagingNew2Dirty(mode, imOut, imIn); @@ -1749,15 +1740,12 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) { } g = b = r; } else { - static char buf[100]; - snprintf( - buf, - 100, - "conversion from %.10s to %.10s not supported in convert_transparent", + return (Imaging)PyErr_Format( + PyExc_ValueError, + "conversion from %s to %s not supported in convert_transparent", imIn->mode, mode ); - return (Imaging)ImagingError_ValueError(buf); } imOut = ImagingNew2Dirty(mode, imOut, imIn); diff --git a/src/libImaging/Dib.c b/src/libImaging/Dib.c index c69e9e552ae..a34fba85aa2 100644 --- a/src/libImaging/Dib.c +++ b/src/libImaging/Dib.c @@ -19,10 +19,9 @@ * See the README file for information on usage and redistribution. */ -#include "Imaging.h" - #ifdef _WIN32 +#include "Imaging.h" #include "ImDib.h" char * diff --git a/src/libImaging/Draw.c b/src/libImaging/Draw.c index ea6f8805efb..943638028a3 100644 --- a/src/libImaging/Draw.c +++ b/src/libImaging/Draw.c @@ -34,9 +34,6 @@ #include "Imaging.h" -#include -#include - #define CEIL(v) (int)ceil(v) #define FLOOR(v) ((v) >= 0.0 ? (int)(v) : (int)floor(v)) diff --git a/src/libImaging/Effects.c b/src/libImaging/Effects.c index 93e7af0bce9..64d6188d917 100644 --- a/src/libImaging/Effects.c +++ b/src/libImaging/Effects.c @@ -17,8 +17,6 @@ #include "Imaging.h" -#include - Imaging ImagingEffectMandelbrot(int xsize, int ysize, double extent[4], int quality) { /* Generate a Mandelbrot set covering the given extent */ diff --git a/src/libImaging/Except.c b/src/libImaging/Except.c deleted file mode 100644 index f42ff9aec9e..00000000000 --- a/src/libImaging/Except.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * The Python Imaging Library - * $Id$ - * - * default exception handling - * - * This module is usually overridden by application code (e.g. - * _imaging.c for PIL's standard Python bindings). If you get - * linking errors, remove this file from your project/library. - * - * history: - * 1995-06-15 fl Created - * 1998-12-29 fl Minor tweaks - * 2003-09-13 fl Added ImagingEnter/LeaveSection() - * - * Copyright (c) 1997-2003 by Secret Labs AB. - * Copyright (c) 1995-2003 by Fredrik Lundh. - * - * See the README file for information on usage and redistribution. - */ - -#include "Imaging.h" - -/* exception state */ - -void * -ImagingError_OSError(void) { - fprintf(stderr, "*** exception: file access error\n"); - return NULL; -} - -void * -ImagingError_MemoryError(void) { - fprintf(stderr, "*** exception: out of memory\n"); - return NULL; -} - -void * -ImagingError_ModeError(void) { - return ImagingError_ValueError("bad image mode"); -} - -void * -ImagingError_Mismatch(void) { - return ImagingError_ValueError("images don't match"); -} - -void * -ImagingError_ValueError(const char *message) { - if (!message) { - message = "exception: bad argument to function"; - } - fprintf(stderr, "*** %s\n", message); - return NULL; -} - -void -ImagingError_Clear(void) { - /* nop */; -} - -/* thread state */ - -void -ImagingSectionEnter(ImagingSectionCookie *cookie) { - /* pass */ -} - -void -ImagingSectionLeave(ImagingSectionCookie *cookie) { - /* pass */ -} diff --git a/src/libImaging/File.c b/src/libImaging/File.c index 76d0abccc4f..901fe83ad27 100644 --- a/src/libImaging/File.c +++ b/src/libImaging/File.c @@ -54,7 +54,7 @@ ImagingSavePPM(Imaging im, const char *outfile) { fp = fopen(outfile, "wb"); if (!fp) { - (void)ImagingError_OSError(); + PyErr_SetString(PyExc_OSError, "error when accessing file"); return 0; } diff --git a/src/libImaging/Fill.c b/src/libImaging/Fill.c index 8fb481e7e4d..1e4bb9ba0b5 100644 --- a/src/libImaging/Fill.c +++ b/src/libImaging/Fill.c @@ -17,8 +17,6 @@ #include "Imaging.h" -#include "math.h" - Imaging ImagingFill(Imaging im, const void *colour) { int x, y; diff --git a/src/libImaging/ImDib.h b/src/libImaging/ImDib.h index 91ff3f322ff..a0c2348fd08 100644 --- a/src/libImaging/ImDib.h +++ b/src/libImaging/ImDib.h @@ -12,8 +12,6 @@ #ifdef _WIN32 -#include "ImPlatform.h" - #if defined(__cplusplus) extern "C" { #endif diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index 31052c68a97..945d861a5c5 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -10,16 +10,12 @@ * See the README file for information on usage and redistribution. */ -#include "ImPlatform.h" +#include "ImagingPlatform.h" #if defined(__cplusplus) extern "C" { #endif -#ifndef M_PI -#define M_PI 3.1415926535897932384626433832795 -#endif - /* -------------------------------------------------------------------- */ /* @@ -241,8 +237,6 @@ ImagingSectionLeave(ImagingSectionCookie *cookie); /* Exceptions */ /* ---------- */ -extern void * -ImagingError_OSError(void); extern void * ImagingError_MemoryError(void); extern void * @@ -251,8 +245,6 @@ extern void * ImagingError_Mismatch(void); /* maps to ValueError by default */ extern void * ImagingError_ValueError(const char *message); -extern void -ImagingError_Clear(void); /* Transform callbacks */ /* ------------------- */ @@ -609,10 +601,6 @@ ImagingLibTiffDecode( extern int ImagingLibTiffEncode(Imaging im, ImagingCodecState state, UINT8 *buffer, int bytes); #endif -#ifdef HAVE_LIBMPEG -extern int -ImagingMpegDecode(Imaging im, ImagingCodecState state, UINT8 *buffer, Py_ssize_t bytes); -#endif extern int ImagingMspDecode(Imaging im, ImagingCodecState state, UINT8 *buffer, Py_ssize_t bytes); extern int diff --git a/src/libImaging/ImPlatform.h b/src/libImaging/ImagingPlatform.h similarity index 52% rename from src/libImaging/ImPlatform.h rename to src/libImaging/ImagingPlatform.h index c9b7e43b425..635ade21c88 100644 --- a/src/libImaging/ImPlatform.h +++ b/src/libImaging/ImagingPlatform.h @@ -8,7 +8,10 @@ */ #define PY_SSIZE_T_CLEAN -#include "Python.h" +#include + +#define _USE_MATH_DEFINES +#include /* Check that we have an ANSI compliant compiler */ #ifndef HAVE_PROTOTYPES @@ -18,36 +21,25 @@ #error Sorry, this library requires ANSI header files. #endif -#if defined(PIL_NO_INLINE) -#define inline -#else #if defined(_MSC_VER) && !defined(__GNUC__) #define inline __inline #endif -#endif - -#if defined(_WIN32) || defined(__CYGWIN__) /* WIN */ +#ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include - -#ifdef __CYGWIN__ -#undef _WIN64 -#undef _WIN32 -#undef __WIN32__ -#undef WIN32 +#ifdef _WIN64 +#define F_HANDLE "K" +#else +#define F_HANDLE "k" #endif - -#else /* not WIN */ -/* For System that are not Windows, we'll need to define these. */ -/* We have to define them instead of using typedef because the JPEG lib also - defines their own types with the same names, so we need to be able to undef - ours before including the JPEG code. */ - -#if __STDC_VERSION__ >= 199901L /* C99+ */ +#endif /* _WIN32 */ #include +/* We have to define types instead of using typedef because the JPEG lib also + defines their own types with the same names, so we need to be able to undef + ours before including the JPEG code. */ #define INT8 int8_t #define UINT8 uint8_t #define INT16 int16_t @@ -55,45 +47,11 @@ #define INT32 int32_t #define UINT32 uint32_t -#else /* < C99 */ - -#define INT8 signed char - -#if SIZEOF_SHORT == 2 -#define INT16 short -#elif SIZEOF_INT == 2 -#define INT16 int -#else -#error Cannot find required 16-bit integer type -#endif - -#if SIZEOF_SHORT == 4 -#define INT32 short -#elif SIZEOF_INT == 4 -#define INT32 int -#elif SIZEOF_LONG == 4 -#define INT32 long -#else -#error Cannot find required 32-bit integer type -#endif - -#define UINT8 unsigned char -#define UINT16 unsigned INT16 -#define UINT32 unsigned INT32 - -#endif /* < C99 */ - -#endif /* not WIN */ - /* assume IEEE; tweak if necessary (patches are welcome) */ #define FLOAT16 UINT16 #define FLOAT32 float #define FLOAT64 double -#ifdef _MSC_VER -typedef signed __int64 int64_t; -#endif - #ifdef __GNUC__ #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) #endif diff --git a/src/libImaging/ImagingUtils.h b/src/libImaging/ImagingUtils.h index 714458ad02a..e8045506980 100644 --- a/src/libImaging/ImagingUtils.h +++ b/src/libImaging/ImagingUtils.h @@ -14,6 +14,9 @@ #define MASK_UINT32_CHANNEL_3 0xff000000 #endif +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) + #define SHIFTFORDIV255(a) ((((a) >> 8) + a) >> 8) /* like (a * b + 127) / 255), but much faster on most platforms */ diff --git a/src/libImaging/Jpeg.h b/src/libImaging/Jpeg.h index 7cdba902281..f59f7909e9a 100644 --- a/src/libImaging/Jpeg.h +++ b/src/libImaging/Jpeg.h @@ -8,7 +8,16 @@ * Copyright (c) 1995-1996 by Fredrik Lundh */ -#include "jpeglib.h" +/* These undefs are required for libjpeg v8 and below */ + +#undef UINT8 +#undef UINT16 +#undef UINT32 +#undef INT8 +#undef INT16 +#undef INT32 + +#include #include diff --git a/src/libImaging/Jpeg2KDecode.c b/src/libImaging/Jpeg2KDecode.c index 4f185b529ac..7b0564c9bb8 100644 --- a/src/libImaging/Jpeg2KDecode.c +++ b/src/libImaging/Jpeg2KDecode.c @@ -17,7 +17,6 @@ #ifdef HAVE_OPENJPEG -#include #include "Jpeg2K.h" typedef struct { diff --git a/src/libImaging/JpegDecode.c b/src/libImaging/JpegDecode.c index 2970f56d1af..838a7483cdd 100644 --- a/src/libImaging/JpegDecode.c +++ b/src/libImaging/JpegDecode.c @@ -25,15 +25,6 @@ #ifdef HAVE_LIBJPEG -#undef HAVE_PROTOTYPES -#undef HAVE_STDLIB_H -#undef HAVE_STDDEF_H -#undef UINT8 -#undef UINT16 -#undef UINT32 -#undef INT16 -#undef INT32 - #include "Jpeg.h" #define STRINGIFY(x) #x diff --git a/src/libImaging/JpegEncode.c b/src/libImaging/JpegEncode.c index 4372d51d5c3..880a3111794 100644 --- a/src/libImaging/JpegEncode.c +++ b/src/libImaging/JpegEncode.c @@ -23,15 +23,6 @@ #ifdef HAVE_LIBJPEG -#undef HAVE_PROTOTYPES -#undef HAVE_STDLIB_H -#undef HAVE_STDDEF_H -#undef UINT8 -#undef UINT16 -#undef UINT32 -#undef INT16 -#undef INT32 - #include "Jpeg.h" /* -------------------------------------------------------------------- */ diff --git a/src/libImaging/Palette.c b/src/libImaging/Palette.c index 78916bca52b..032ce8ab26a 100644 --- a/src/libImaging/Palette.c +++ b/src/libImaging/Palette.c @@ -18,8 +18,6 @@ #include "Imaging.h" -#include - ImagingPalette ImagingPaletteNew(const char *mode) { /* Create a palette object */ diff --git a/src/libImaging/Quant.c b/src/libImaging/Quant.c index a489a882db2..147d8ae52cb 100644 --- a/src/libImaging/Quant.c +++ b/src/libImaging/Quant.c @@ -1428,7 +1428,6 @@ quantize( } #ifdef TEST_NEAREST_NEIGHBOUR -#include { uint32_t bestmatch, bestdist, dist; HashTable *h2; diff --git a/src/libImaging/QuantOctree.c b/src/libImaging/QuantOctree.c index ec60b214ef1..be1b239a26f 100644 --- a/src/libImaging/QuantOctree.c +++ b/src/libImaging/QuantOctree.c @@ -28,7 +28,7 @@ #include #include -#include "ImagingUtils.h" +#include "Imaging.h" #include "QuantOctree.h" typedef struct _ColorBucket { @@ -49,8 +49,6 @@ typedef struct _ColorCube { ColorBucket buckets; } *ColorCube; -#define MAX(a, b) (a) > (b) ? (a) : (b) - static ColorCube new_color_cube(int r, int g, int b, int a) { ColorCube cube; diff --git a/src/libImaging/QuantPngQuant.c b/src/libImaging/QuantPngQuant.c index a2258c3a289..52f12751532 100644 --- a/src/libImaging/QuantPngQuant.c +++ b/src/libImaging/QuantPngQuant.c @@ -8,15 +8,15 @@ * */ +#ifdef HAVE_LIBIMAGEQUANT + #include #include #include +#include #include "QuantPngQuant.h" -#ifdef HAVE_LIBIMAGEQUANT -#include "libimagequant.h" - int quantize_pngquant( Pixel *pixelData, diff --git a/src/libImaging/QuantTypes.h b/src/libImaging/QuantTypes.h index 986b70806dc..e01023de7e1 100644 --- a/src/libImaging/QuantTypes.h +++ b/src/libImaging/QuantTypes.h @@ -9,15 +9,10 @@ * See the README file for information on usage and redistribution. */ -#ifndef __TYPES_H__ -#define __TYPES_H__ +#ifndef __QUANTTYPES_H__ +#define __QUANTTYPES_H__ -#ifdef _MSC_VER -typedef unsigned __int32 uint32_t; -typedef unsigned __int64 uint64_t; -#else #include -#endif typedef union { struct { diff --git a/src/libImaging/Reduce.c b/src/libImaging/Reduce.c index 022daa0003f..6da18d773d5 100644 --- a/src/libImaging/Reduce.c +++ b/src/libImaging/Reduce.c @@ -1,7 +1,5 @@ #include "Imaging.h" -#include - #define ROUND_UP(f) ((int)((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F)) UINT32 diff --git a/src/libImaging/Resample.c b/src/libImaging/Resample.c index f5e386dc2ba..978bf74c750 100644 --- a/src/libImaging/Resample.c +++ b/src/libImaging/Resample.c @@ -1,7 +1,5 @@ #include "Imaging.h" -#include - #define ROUND_UP(f) ((int)((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F)) struct filter { diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index 522e9f37557..634ad71bfc4 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -513,7 +513,7 @@ ImagingNewInternal(const char *mode, int xsize, int ysize, int dirty) { return im; } - ImagingError_Clear(); + PyErr_Clear(); // Try to allocate the image once more with smallest possible block size MUTEX_LOCK(&ImagingDefaultArena.mutex); diff --git a/src/libImaging/TiffDecode.c b/src/libImaging/TiffDecode.c index e4da9162dc9..a4ce042eb41 100644 --- a/src/libImaging/TiffDecode.c +++ b/src/libImaging/TiffDecode.c @@ -69,7 +69,7 @@ _tiffReadProc(thandle_t hdata, tdata_t buf, tsize_t size) { ); return 0; } - to_read = min(size, min(state->size, (tsize_t)state->eof) - (tsize_t)state->loc); + to_read = MIN(size, MIN(state->size, (tsize_t)state->eof) - (tsize_t)state->loc); TRACE(("to_read: %d\n", (int)to_read)); _TIFFmemcpy(buf, (UINT8 *)state->data + state->loc, to_read); @@ -87,7 +87,7 @@ _tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) { TRACE(("_tiffWriteProc: %d \n", (int)size)); dump_state(state); - to_write = min(size, state->size - (tsize_t)state->loc); + to_write = MIN(size, state->size - (tsize_t)state->loc); if (state->flrealloc && size > to_write) { tdata_t new_data; tsize_t newsize = state->size; @@ -114,7 +114,7 @@ _tiffWriteProc(thandle_t hdata, tdata_t buf, tsize_t size) { _TIFFmemcpy((UINT8 *)state->data + state->loc, buf, to_write); state->loc += (toff_t)to_write; - state->eof = max(state->loc, state->eof); + state->eof = MAX(state->loc, state->eof); dump_state(state); return to_write; @@ -333,7 +333,7 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { for (; state->y < state->ysize; state->y += rows_per_block) { img.row_offset = state->y; - rows_to_read = min(rows_per_block, img.height - state->y); + rows_to_read = MIN(rows_per_block, img.height - state->y); if (!TIFFRGBAImageGet(&img, (UINT32 *)state->buffer, img.width, rows_to_read)) { TRACE(("Decode Error, y: %d\n", state->y)); @@ -349,7 +349,7 @@ _decodeAsRGBA(Imaging im, ImagingCodecState state, TIFF *tiff) { // iterate over each row in the strip and stuff data into image for (current_row = 0; - current_row < min((INT32)rows_per_block, state->ysize - state->y); + current_row < MIN((INT32)rows_per_block, state->ysize - state->y); current_row++) { TRACE(("Writing data into line %d ; \n", state->y + current_row)); @@ -452,8 +452,8 @@ _decodeTile( TRACE(("Read tile at %dx%d; \n\n", x, y)); - current_tile_width = min((INT32)tile_width, state->xsize - x); - current_tile_length = min((INT32)tile_length, state->ysize - y); + current_tile_width = MIN((INT32)tile_width, state->xsize - x); + current_tile_length = MIN((INT32)tile_length, state->ysize - y); // iterate over each line in the tile and stuff data into image for (tile_y = 0; tile_y < current_tile_length; tile_y++) { TRACE( @@ -566,7 +566,7 @@ _decodeStrip( // iterate over each row in the strip and stuff data into image for (strip_row = 0; - strip_row < min((INT32)rows_per_strip, state->ysize - state->y); + strip_row < MIN((INT32)rows_per_strip, state->ysize - state->y); strip_row++) { TRACE(("Writing data into line %d ; \n", state->y + strip_row)); diff --git a/src/libImaging/TiffDecode.h b/src/libImaging/TiffDecode.h index 22361210dcb..0768fb67496 100644 --- a/src/libImaging/TiffDecode.h +++ b/src/libImaging/TiffDecode.h @@ -6,20 +6,8 @@ * */ -#ifndef _TIFFIO_ #include -#endif -#ifndef _TIFF_ #include -#endif - -#ifndef min -#define min(x, y) ((x > y) ? y : x) -#define max(x, y) ((x < y) ? y : x) -#endif - -#ifndef _PIL_LIBTIFF_ -#define _PIL_LIBTIFF_ typedef struct { tdata_t data; /* tdata_t == void* */ @@ -57,5 +45,3 @@ ImagingLibTiffSetField(ImagingCodecState state, ttag_t tag, ...); */ #define TRACE(args) - -#endif diff --git a/src/libImaging/ZipCodecs.h b/src/libImaging/ZipCodecs.h index 50218b6c69a..42f1a710150 100644 --- a/src/libImaging/ZipCodecs.h +++ b/src/libImaging/ZipCodecs.h @@ -7,7 +7,7 @@ * Copyright (c) Fredrik Lundh 1996. */ -#include "zlib.h" +#include /* modes */ #define ZIP_PNG 0 /* continuous, filtered image data */ diff --git a/src/libImaging/codec_fd.c b/src/libImaging/codec_fd.c index 5261681107b..f5541c332f2 100644 --- a/src/libImaging/codec_fd.c +++ b/src/libImaging/codec_fd.c @@ -1,4 +1,3 @@ -#include "Python.h" #include "Imaging.h" Py_ssize_t diff --git a/src/map.c b/src/map.c index c66702981d3..ee0848cfb6c 100644 --- a/src/map.c +++ b/src/map.c @@ -18,16 +18,8 @@ * FIXME: should move the memory mapping primitives into libImaging! */ -#include "Python.h" - #include "libImaging/Imaging.h" -/* compatibility wrappers (defined in _imaging.c) */ -extern int -PyImaging_CheckBuffer(PyObject *buffer); -extern int -PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view); - extern PyObject * PyImagingNew(Imaging im); @@ -77,7 +69,7 @@ PyImaging_MapBuffer(PyObject *self, PyObject *args) { return NULL; } - if (!PyImaging_CheckBuffer(target)) { + if (!PyObject_CheckBuffer(target)) { PyErr_SetString(PyExc_TypeError, "expected string or buffer"); return NULL; } @@ -105,7 +97,7 @@ PyImaging_MapBuffer(PyObject *self, PyObject *args) { } /* check buffer size */ - if (PyImaging_GetBuffer(target, &view) < 0) { + if (PyObject_GetBuffer(target, &view, PyBUF_SIMPLE) < 0) { return NULL; } diff --git a/src/outline.c b/src/outline.c index 27cc255cf84..d5e47caf79f 100644 --- a/src/outline.c +++ b/src/outline.c @@ -17,8 +17,6 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" - #include "libImaging/Imaging.h" /* -------------------------------------------------------------------- */ @@ -89,8 +87,7 @@ _outline_move(OutlineObject *self, PyObject *args) { ImagingOutlineMove(self->outline, x0, y0); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -102,8 +99,7 @@ _outline_line(OutlineObject *self, PyObject *args) { ImagingOutlineLine(self->outline, x1, y1); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -115,8 +111,7 @@ _outline_curve(OutlineObject *self, PyObject *args) { ImagingOutlineCurve(self->outline, x1, y1, x2, y2, x3, y3); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -127,8 +122,7 @@ _outline_close(OutlineObject *self, PyObject *args) { ImagingOutlineClose(self->outline); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static PyObject * @@ -140,8 +134,7 @@ _outline_transform(OutlineObject *self, PyObject *args) { ImagingOutlineTransform(self->outline, a); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static struct PyMethodDef _outline_methods[] = { diff --git a/src/path.c b/src/path.c index 067f42f62de..e83e35d2a90 100644 --- a/src/path.c +++ b/src/path.c @@ -25,17 +25,8 @@ * See the README file for information on usage and redistribution. */ -#include "Python.h" -#include "thirdparty/pythoncapi_compat.h" #include "libImaging/Imaging.h" - -#include - -/* compatibility wrappers (defined in _imaging.c) */ -extern int -PyImaging_CheckBuffer(PyObject *buffer); -extern int -PyImaging_GetBuffer(PyObject *buffer, Py_buffer *view); +#include "thirdparty/pythoncapi_compat.h" /* -------------------------------------------------------------------- */ /* Class */ @@ -126,10 +117,10 @@ PyPath_Flatten(PyObject *data, double **pxy) { return path->count; } - if (PyImaging_CheckBuffer(data)) { + if (PyObject_CheckBuffer(data)) { /* Assume the buffer contains floats */ Py_buffer buffer; - if (PyImaging_GetBuffer(data, &buffer) == 0) { + if (PyObject_GetBuffer(data, &buffer, PyBUF_SIMPLE) == 0) { float *ptr = (float *)buffer.buf; n = buffer.len / (2 * sizeof(float)); xy = alloc_array(n); @@ -415,8 +406,7 @@ path_map(PyPathObject *self, PyObject *args) { } self->mapping = 0; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static int @@ -528,8 +518,7 @@ path_transform(PyPathObject *self, PyObject *args) { } } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static struct PyMethodDef methods[] = {