Merge pull request #8341 from uploadcare/use-ptr

Use ImagingCore.ptr instead of ImagingCore.id
This commit is contained in:
Hugo van Kemenade 2024-10-07 13:37:44 +03:00 committed by GitHub
commit 535bf234c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 264 additions and 193 deletions

View File

@ -1,11 +1,19 @@
from __future__ import annotations from __future__ import annotations
import pytest
from .helper import hopper from .helper import hopper
def test_sanity() -> None: def test_sanity() -> None:
im = hopper() im = hopper()
type_repr = repr(type(im.getim()))
type_repr = repr(type(im.getim()))
assert "PyCapsule" in type_repr assert "PyCapsule" in type_repr
assert isinstance(im.im.id, int)
with pytest.warns(DeprecationWarning):
assert isinstance(im.im.id, int)
with pytest.warns(DeprecationWarning):
ptrs = dict(im.im.unsafe_ptrs)
assert ptrs.keys() == {"image8", "image32", "image"}

View File

@ -324,17 +324,17 @@ def test_set_lut() -> None:
def test_wrong_mode() -> None: def test_wrong_mode() -> None:
lut = ImageMorph.LutBuilder(op_name="corner").build_lut() lut = ImageMorph.LutBuilder(op_name="corner").build_lut()
imrgb = Image.new("RGB", (10, 10)) imrgb_ptr = Image.new("RGB", (10, 10)).getim()
iml = Image.new("L", (10, 10)) iml_ptr = Image.new("L", (10, 10)).getim()
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
_imagingmorph.apply(bytes(lut), imrgb.im.id, iml.im.id) _imagingmorph.apply(bytes(lut), imrgb_ptr, iml_ptr)
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
_imagingmorph.apply(bytes(lut), iml.im.id, imrgb.im.id) _imagingmorph.apply(bytes(lut), iml_ptr, imrgb_ptr)
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
_imagingmorph.match(bytes(lut), imrgb.im.id) _imagingmorph.match(bytes(lut), imrgb_ptr)
# Should not raise # Should not raise
_imagingmorph.match(bytes(lut), iml.im.id) _imagingmorph.match(bytes(lut), iml_ptr)

View File

@ -165,6 +165,16 @@ Specific WebP Feature Checks
``True`` if the WebP module is installed, until they are removed in Pillow ``True`` if the WebP module is installed, until they are removed in Pillow
12.0.0 (2025-10-15). 12.0.0 (2025-10-15).
Get internal pointers to objects
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. deprecated:: 11.0.0
``Image.core.ImagingCore.id`` and ``Image.core.ImagingCore.unsafe_ptrs`` have been
deprecated and will be removed in Pillow 12 (2025-10-15). They were used for obtaining
raw pointers to ``ImagingCore`` internals. To interact with C code, you can use
``Image.Image.getim()``, which returns a ``Capsule`` object.
Removed features Removed features
---------------- ----------------

View File

@ -73,6 +73,16 @@ vulnerability introduced in FreeType 2.6 (:cve:`2020-15999`).
.. _2.10.4: https://sourceforge.net/projects/freetype/files/freetype2/2.10.4/ .. _2.10.4: https://sourceforge.net/projects/freetype/files/freetype2/2.10.4/
Get internal pointers to objects
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. deprecated:: 11.0.0
``Image.core.ImagingCore.id`` and ``Image.core.ImagingCore.unsafe_ptrs`` have been
deprecated and will be removed in Pillow 12 (2025-10-15). They were used for obtaining
raw pointers to ``ImagingCore`` internals. To interact with C code, you can use
``Image.Image.getim()``, which returns a ``Capsule`` object.
ICNS (width, height, scale) sizes ICNS (width, height, scale) sizes
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -225,12 +225,7 @@ if TYPE_CHECKING:
from IPython.lib.pretty import PrettyPrinter from IPython.lib.pretty import PrettyPrinter
from . import ImageFile, ImageFilter, ImagePalette, ImageQt, TiffImagePlugin from . import ImageFile, ImageFilter, ImagePalette, ImageQt, TiffImagePlugin
from ._typing import NumpyArray, StrOrBytesPath, TypeGuard from ._typing import CapsuleType, NumpyArray, StrOrBytesPath, TypeGuard
if sys.version_info >= (3, 13):
from types import CapsuleType
else:
CapsuleType = object
ID: list[str] = [] ID: list[str] = []
OPEN: dict[ OPEN: dict[
str, str,

View File

@ -349,19 +349,17 @@ class ImageCmsTransform(Image.ImagePointHandler):
return self.apply(im) return self.apply(im)
def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image: def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Image:
im.load()
if imOut is None: if imOut is None:
imOut = Image.new(self.output_mode, im.size, None) imOut = Image.new(self.output_mode, im.size, None)
self.transform.apply(im.im.id, imOut.im.id) self.transform.apply(im.getim(), imOut.getim())
imOut.info["icc_profile"] = self.output_profile.tobytes() imOut.info["icc_profile"] = self.output_profile.tobytes()
return imOut return imOut
def apply_in_place(self, im: Image.Image) -> Image.Image: def apply_in_place(self, im: Image.Image) -> Image.Image:
im.load()
if im.mode != self.output_mode: if im.mode != self.output_mode:
msg = "mode mismatch" msg = "mode mismatch"
raise ValueError(msg) # wrong output mode raise ValueError(msg) # wrong output mode
self.transform.apply(im.im.id, im.im.id) self.transform.apply(im.getim(), im.getim())
im.info["icc_profile"] = self.output_profile.tobytes() im.info["icc_profile"] = self.output_profile.tobytes()
return im return im

View File

@ -59,13 +59,12 @@ class _Operand:
if im2 is None: if im2 is None:
# unary operation # unary operation
out = Image.new(mode or im_1.mode, im_1.size, None) out = Image.new(mode or im_1.mode, im_1.size, None)
im_1.load()
try: try:
op = getattr(_imagingmath, f"{op}_{im_1.mode}") op = getattr(_imagingmath, f"{op}_{im_1.mode}")
except AttributeError as e: except AttributeError as e:
msg = f"bad operand type for '{op}'" msg = f"bad operand type for '{op}'"
raise TypeError(msg) from e raise TypeError(msg) from e
_imagingmath.unop(op, out.im.id, im_1.im.id) _imagingmath.unop(op, out.getim(), im_1.getim())
else: else:
# binary operation # binary operation
im_2 = self.__fixup(im2) im_2 = self.__fixup(im2)
@ -86,14 +85,12 @@ class _Operand:
if im_2.size != size: if im_2.size != size:
im_2 = im_2.crop((0, 0) + size) im_2 = im_2.crop((0, 0) + size)
out = Image.new(mode or im_1.mode, im_1.size, None) out = Image.new(mode or im_1.mode, im_1.size, None)
im_1.load()
im_2.load()
try: try:
op = getattr(_imagingmath, f"{op}_{im_1.mode}") op = getattr(_imagingmath, f"{op}_{im_1.mode}")
except AttributeError as e: except AttributeError as e:
msg = f"bad operand type for '{op}'" msg = f"bad operand type for '{op}'"
raise TypeError(msg) from e raise TypeError(msg) from e
_imagingmath.binop(op, out.im.id, im_1.im.id, im_2.im.id) _imagingmath.binop(op, out.getim(), im_1.getim(), im_2.getim())
return _Operand(out) return _Operand(out)
# unary operators # unary operators

View File

@ -213,7 +213,7 @@ class MorphOp:
msg = "Image mode must be L" msg = "Image mode must be L"
raise ValueError(msg) raise ValueError(msg)
outimage = Image.new(image.mode, image.size, None) outimage = Image.new(image.mode, image.size, None)
count = _imagingmorph.apply(bytes(self.lut), image.im.id, outimage.im.id) count = _imagingmorph.apply(bytes(self.lut), image.getim(), outimage.getim())
return count, outimage return count, outimage
def match(self, image: Image.Image) -> list[tuple[int, int]]: def match(self, image: Image.Image) -> list[tuple[int, int]]:
@ -229,7 +229,7 @@ class MorphOp:
if image.mode != "L": if image.mode != "L":
msg = "Image mode must be L" msg = "Image mode must be L"
raise ValueError(msg) raise ValueError(msg)
return _imagingmorph.match(bytes(self.lut), image.im.id) return _imagingmorph.match(bytes(self.lut), image.getim())
def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]: def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]:
"""Get a list of all turned on pixels in a binary image """Get a list of all turned on pixels in a binary image
@ -240,7 +240,7 @@ class MorphOp:
if image.mode != "L": if image.mode != "L":
msg = "Image mode must be L" msg = "Image mode must be L"
raise ValueError(msg) raise ValueError(msg)
return _imagingmorph.get_on_pixels(image.im.id) return _imagingmorph.get_on_pixels(image.getim())
def load_lut(self, filename: str) -> None: def load_lut(self, filename: str) -> None:
"""Load an operator from an mrl file""" """Load an operator from an mrl file"""

View File

@ -32,23 +32,12 @@ from typing import TYPE_CHECKING, Any, cast
from . import Image, ImageFile from . import Image, ImageFile
if TYPE_CHECKING:
from ._typing import CapsuleType
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# Check for Tkinter interface hooks # Check for Tkinter interface hooks
_pilbitmap_ok = None
def _pilbitmap_check() -> int:
global _pilbitmap_ok
if _pilbitmap_ok is None:
try:
im = Image.new("1", (1, 1))
tkinter.BitmapImage(data=f"PIL:{im.im.id}")
_pilbitmap_ok = 1
except tkinter.TclError:
_pilbitmap_ok = 0
return _pilbitmap_ok
def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None: def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None:
source = None source = None
@ -62,18 +51,18 @@ def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None:
def _pyimagingtkcall( def _pyimagingtkcall(
command: str, photo: PhotoImage | tkinter.PhotoImage, id: int command: str, photo: PhotoImage | tkinter.PhotoImage, ptr: CapsuleType
) -> None: ) -> None:
tk = photo.tk tk = photo.tk
try: try:
tk.call(command, photo, id) tk.call(command, photo, repr(ptr))
except tkinter.TclError: except tkinter.TclError:
# activate Tkinter hook # activate Tkinter hook
# may raise an error if it cannot attach to Tkinter # may raise an error if it cannot attach to Tkinter
from . import _imagingtk from . import _imagingtk
_imagingtk.tkinit(tk.interpaddr()) _imagingtk.tkinit(tk.interpaddr())
tk.call(command, photo, id) tk.call(command, photo, repr(ptr))
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -142,7 +131,10 @@ class PhotoImage:
self.paste(image) self.paste(image)
def __del__(self) -> None: def __del__(self) -> None:
name = self.__photo.name try:
name = self.__photo.name
except AttributeError:
return
self.__photo.name = None self.__photo.name = None
try: try:
self.__photo.tk.call("image", "delete", name) self.__photo.tk.call("image", "delete", name)
@ -185,15 +177,14 @@ class PhotoImage:
the bitmap image. the bitmap image.
""" """
# convert to blittable # convert to blittable
im.load() ptr = im.getim()
image = im.im image = im.im
if image.isblock() and im.mode == self.__mode: if not image.isblock() or im.mode != self.__mode:
block = image block = Image.core.new_block(self.__mode, im.size)
else:
block = image.new_block(self.__mode, im.size)
image.convert2(block, image) # convert directly between buffers image.convert2(block, image) # convert directly between buffers
ptr = block.ptr
_pyimagingtkcall("PyImagingPhoto", self.__photo, block.id) _pyimagingtkcall("PyImagingPhoto", self.__photo, ptr)
# -------------------------------------------------------------------- # --------------------------------------------------------------------
@ -225,18 +216,13 @@ class BitmapImage:
self.__mode = image.mode self.__mode = image.mode
self.__size = image.size self.__size = image.size
if _pilbitmap_check(): self.__photo = tkinter.BitmapImage(data=image.tobitmap(), **kw)
# fast way (requires the pilbitmap booster patch)
image.load()
kw["data"] = f"PIL:{image.im.id}"
self.__im = image # must keep a reference
else:
# slow but safe way
kw["data"] = image.tobitmap()
self.__photo = tkinter.BitmapImage(**kw)
def __del__(self) -> None: def __del__(self) -> None:
name = self.__photo.name try:
name = self.__photo.name
except AttributeError:
return
self.__photo.name = None self.__photo.name = None
try: try:
self.__photo.tk.call("image", "delete", name) self.__photo.tk.call("image", "delete", name)
@ -273,9 +259,8 @@ class BitmapImage:
def getimage(photo: PhotoImage) -> Image.Image: def getimage(photo: PhotoImage) -> Image.Image:
"""Copies the contents of a PhotoImage to a PIL image memory.""" """Copies the contents of a PhotoImage to a PIL image memory."""
im = Image.new("RGBA", (photo.width(), photo.height())) im = Image.new("RGBA", (photo.width(), photo.height()))
block = im.im
_pyimagingtkcall("PyImagingPhotoGet", photo, block.id) _pyimagingtkcall("PyImagingPhotoGet", photo, im.getim())
return im return im

View File

@ -2,6 +2,8 @@ import datetime
import sys import sys
from typing import Literal, SupportsFloat, TypedDict from typing import Literal, SupportsFloat, TypedDict
from ._typing import CapsuleType
littlecms_version: str | None littlecms_version: str | None
_Tuple3f = tuple[float, float, float] _Tuple3f = tuple[float, float, float]
@ -108,7 +110,7 @@ class CmsProfile:
def is_intent_supported(self, intent: int, direction: int, /) -> int: ... def is_intent_supported(self, intent: int, direction: int, /) -> int: ...
class CmsTransform: class CmsTransform:
def apply(self, id_in: int, id_out: int) -> int: ... def apply(self, id_in: CapsuleType, id_out: CapsuleType) -> int: ...
def profile_open(profile: str, /) -> CmsProfile: ... def profile_open(profile: str, /) -> CmsProfile: ...
def profile_frombytes(profile: bytes, /) -> CmsProfile: ... def profile_frombytes(profile: bytes, /) -> CmsProfile: ...

View File

@ -15,6 +15,11 @@ if TYPE_CHECKING:
except (ImportError, AttributeError): except (ImportError, AttributeError):
pass pass
if sys.version_info >= (3, 13):
from types import CapsuleType
else:
CapsuleType = object
if sys.version_info >= (3, 12): if sys.version_info >= (3, 12):
from collections.abc import Buffer from collections.abc import Buffer
else: else:

View File

@ -56,19 +56,34 @@ static Tk_PhotoPutBlock_t TK_PHOTO_PUT_BLOCK;
static Imaging static Imaging
ImagingFind(const char *name) { ImagingFind(const char *name) {
Py_ssize_t id; PyObject *capsule;
int direct_pointer = 0;
const char *expected = "capsule object \"" IMAGING_MAGIC "\" at 0x";
/* FIXME: use CObject instead? */ if (name[0] == '<') {
#if defined(_WIN64) name++;
id = _atoi64(name); } else {
#else // Special case for PyPy, where the string representation of a Capsule
id = atol(name); // refers directly to the pointer itself, not to the PyCapsule object.
#endif direct_pointer = 1;
if (!id) { }
if (strncmp(name, expected, strlen(expected))) {
return NULL; return NULL;
} }
return (Imaging)id; capsule = (PyObject *)strtoull(name + strlen(expected), NULL, 16);
if (direct_pointer) {
return (Imaging)capsule;
}
if (!PyCapsule_IsValid(capsule, IMAGING_MAGIC)) {
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
return NULL;
}
return (Imaging)PyCapsule_GetPointer(capsule, IMAGING_MAGIC);
} }
static int static int

View File

@ -3670,15 +3670,12 @@ static struct PyMethodDef methods[] = {
/* Unsharpmask extension */ /* Unsharpmask extension */
{"gaussian_blur", (PyCFunction)_gaussian_blur, METH_VARARGS}, {"gaussian_blur", (PyCFunction)_gaussian_blur, METH_VARARGS},
{"unsharp_mask", (PyCFunction)_unsharp_mask, METH_VARARGS}, {"unsharp_mask", (PyCFunction)_unsharp_mask, METH_VARARGS},
{"box_blur", (PyCFunction)_box_blur, METH_VARARGS}, {"box_blur", (PyCFunction)_box_blur, METH_VARARGS},
/* Special effects */ /* Special effects */
{"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS}, {"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS},
/* Misc. */ /* Misc. */
{"new_block", (PyCFunction)_new_block, METH_VARARGS},
{"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS}, {"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
@ -3703,16 +3700,40 @@ _getattr_bands(ImagingObject *self, void *closure) {
static PyObject * static PyObject *
_getattr_id(ImagingObject *self, void *closure) { _getattr_id(ImagingObject *self, void *closure) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"id property is deprecated and will be removed in Pillow 12 (2025-10-15)",
1
) < 0) {
return NULL;
}
return PyLong_FromSsize_t((Py_ssize_t)self->image); return PyLong_FromSsize_t((Py_ssize_t)self->image);
} }
static void
_ptr_destructor(PyObject *capsule) {
PyObject *self = (PyObject *)PyCapsule_GetContext(capsule);
Py_DECREF(self);
}
static PyObject * static PyObject *
_getattr_ptr(ImagingObject *self, void *closure) { _getattr_ptr(ImagingObject *self, void *closure) {
return PyCapsule_New(self->image, IMAGING_MAGIC, NULL); PyObject *capsule = PyCapsule_New(self->image, IMAGING_MAGIC, _ptr_destructor);
Py_INCREF(self);
PyCapsule_SetContext(capsule, self);
return capsule;
} }
static PyObject * static PyObject *
_getattr_unsafe_ptrs(ImagingObject *self, void *closure) { _getattr_unsafe_ptrs(ImagingObject *self, void *closure) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"unsafe_ptrs property is deprecated and will be removed in Pillow 12 "
"(2025-10-15)",
1
) < 0) {
return NULL;
}
return Py_BuildValue( return Py_BuildValue(
"(sn)(sn)(sn)", "(sn)(sn)(sn)",
"image8", "image8",
@ -4194,6 +4215,7 @@ static PyMethodDef functions[] = {
{"blend", (PyCFunction)_blend, METH_VARARGS}, {"blend", (PyCFunction)_blend, METH_VARARGS},
{"fill", (PyCFunction)_fill, METH_VARARGS}, {"fill", (PyCFunction)_fill, METH_VARARGS},
{"new", (PyCFunction)_new, METH_VARARGS}, {"new", (PyCFunction)_new, METH_VARARGS},
{"new_block", (PyCFunction)_new_block, METH_VARARGS},
{"merge", (PyCFunction)_merge, METH_VARARGS}, {"merge", (PyCFunction)_merge, METH_VARARGS},
/* Functions */ /* Functions */

View File

@ -531,23 +531,24 @@ buildProofTransform(PyObject *self, PyObject *args) {
static PyObject * static PyObject *
cms_transform_apply(CmsTransformObject *self, PyObject *args) { cms_transform_apply(CmsTransformObject *self, PyObject *args) {
Py_ssize_t idIn; PyObject *i0, *i1;
Py_ssize_t idOut;
Imaging im; Imaging im;
Imaging imOut; Imaging imOut;
int result; if (!PyArg_ParseTuple(args, "OO:apply", &i0, &i1)) {
if (!PyArg_ParseTuple(args, "nn:apply", &idIn, &idOut)) {
return NULL; return NULL;
} }
im = (Imaging)idIn; if (!PyCapsule_IsValid(i0, IMAGING_MAGIC) ||
imOut = (Imaging)idOut; !PyCapsule_IsValid(i1, IMAGING_MAGIC)) {
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
return NULL;
}
result = pyCMSdoTransform(im, imOut, self->transform); im = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
imOut = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);
return Py_BuildValue("i", result); return Py_BuildValue("i", pyCMSdoTransform(im, imOut, self->transform));
} }
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */

View File

@ -830,7 +830,6 @@ font_render(FontObject *self, PyObject *args) {
unsigned char convert_scale; /* scale factor for non-8bpp bitmaps */ unsigned char convert_scale; /* scale factor for non-8bpp bitmaps */
PyObject *image; PyObject *image;
Imaging im; Imaging im;
Py_ssize_t id;
int mask = 0; /* is FT_LOAD_TARGET_MONO enabled? */ int mask = 0; /* is FT_LOAD_TARGET_MONO enabled? */
int color = 0; /* is FT_LOAD_COLOR enabled? */ int color = 0; /* is FT_LOAD_COLOR enabled? */
float stroke_width = 0; float stroke_width = 0;
@ -926,10 +925,9 @@ font_render(FontObject *self, PyObject *args) {
PyMem_Del(glyph_info); PyMem_Del(glyph_info);
return NULL; return NULL;
} }
PyObject *imageId = PyObject_GetAttrString(image, "id"); PyObject *imagePtr = PyObject_GetAttrString(image, "ptr");
id = PyLong_AsSsize_t(imageId); im = (Imaging)PyCapsule_GetPointer(imagePtr, IMAGING_MAGIC);
Py_XDECREF(imageId); Py_XDECREF(imagePtr);
im = (Imaging)id;
x_offset = round(x_offset - stroke_width); x_offset = round(x_offset - stroke_width);
y_offset = round(y_offset - stroke_width); y_offset = round(y_offset - stroke_width);

View File

@ -23,6 +23,9 @@
#define MAX_INT32 2147483647.0 #define MAX_INT32 2147483647.0
#define MIN_INT32 -2147483648.0 #define MIN_INT32 -2147483648.0
#define MATH_FUNC_UNOP_MAGIC "Pillow Math unary func"
#define MATH_FUNC_BINOP_MAGIC "Pillow Math binary func"
#define UNOP(name, op, type) \ #define UNOP(name, op, type) \
void name(Imaging out, Imaging im1) { \ void name(Imaging out, Imaging im1) { \
int x, y; \ int x, y; \
@ -168,15 +171,24 @@ _unop(PyObject *self, PyObject *args) {
Imaging im1; Imaging im1;
void (*unop)(Imaging, Imaging); void (*unop)(Imaging, Imaging);
Py_ssize_t op, i0, i1; PyObject *op, *i0, *i1;
if (!PyArg_ParseTuple(args, "nnn", &op, &i0, &i1)) { if (!PyArg_ParseTuple(args, "OOO", &op, &i0, &i1)) {
return NULL; return NULL;
} }
out = (Imaging)i0; if (!PyCapsule_IsValid(op, MATH_FUNC_UNOP_MAGIC)) {
im1 = (Imaging)i1; PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", MATH_FUNC_UNOP_MAGIC);
return NULL;
}
if (!PyCapsule_IsValid(i0, IMAGING_MAGIC) ||
!PyCapsule_IsValid(i1, IMAGING_MAGIC)) {
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
return NULL;
}
unop = (void *)op; unop = (void *)PyCapsule_GetPointer(op, MATH_FUNC_UNOP_MAGIC);
out = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
im1 = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);
unop(out, im1); unop(out, im1);
@ -191,16 +203,26 @@ _binop(PyObject *self, PyObject *args) {
Imaging im2; Imaging im2;
void (*binop)(Imaging, Imaging, Imaging); void (*binop)(Imaging, Imaging, Imaging);
Py_ssize_t op, i0, i1, i2; PyObject *op, *i0, *i1, *i2;
if (!PyArg_ParseTuple(args, "nnnn", &op, &i0, &i1, &i2)) { if (!PyArg_ParseTuple(args, "OOOO", &op, &i0, &i1, &i2)) {
return NULL; return NULL;
} }
out = (Imaging)i0; if (!PyCapsule_IsValid(op, MATH_FUNC_BINOP_MAGIC)) {
im1 = (Imaging)i1; PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", MATH_FUNC_BINOP_MAGIC);
im2 = (Imaging)i2; return NULL;
}
if (!PyCapsule_IsValid(i0, IMAGING_MAGIC) ||
!PyCapsule_IsValid(i1, IMAGING_MAGIC) ||
!PyCapsule_IsValid(i2, IMAGING_MAGIC)) {
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
return NULL;
}
binop = (void *)op; binop = (void *)PyCapsule_GetPointer(op, MATH_FUNC_BINOP_MAGIC);
out = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
im1 = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);
im2 = (Imaging)PyCapsule_GetPointer(i2, IMAGING_MAGIC);
binop(out, im1, im2); binop(out, im1, im2);
@ -213,8 +235,17 @@ static PyMethodDef _functions[] = {
}; };
static void static void
install(PyObject *d, char *name, void *value) { install_unary(PyObject *d, char *name, void *func) {
PyObject *v = PyLong_FromSsize_t((Py_ssize_t)value); PyObject *v = PyCapsule_New(func, MATH_FUNC_UNOP_MAGIC, NULL);
if (!v || PyDict_SetItemString(d, name, v)) {
PyErr_Clear();
}
Py_XDECREF(v);
}
static void
install_binary(PyObject *d, char *name, void *func) {
PyObject *v = PyCapsule_New(func, MATH_FUNC_BINOP_MAGIC, NULL);
if (!v || PyDict_SetItemString(d, name, v)) { if (!v || PyDict_SetItemString(d, name, v)) {
PyErr_Clear(); PyErr_Clear();
} }
@ -225,50 +256,50 @@ static int
setup_module(PyObject *m) { setup_module(PyObject *m) {
PyObject *d = PyModule_GetDict(m); PyObject *d = PyModule_GetDict(m);
install(d, "abs_I", abs_I); install_unary(d, "abs_I", abs_I);
install(d, "neg_I", neg_I); install_unary(d, "neg_I", neg_I);
install(d, "add_I", add_I); install_binary(d, "add_I", add_I);
install(d, "sub_I", sub_I); install_binary(d, "sub_I", sub_I);
install(d, "diff_I", diff_I); install_binary(d, "diff_I", diff_I);
install(d, "mul_I", mul_I); install_binary(d, "mul_I", mul_I);
install(d, "div_I", div_I); install_binary(d, "div_I", div_I);
install(d, "mod_I", mod_I); install_binary(d, "mod_I", mod_I);
install(d, "min_I", min_I); install_binary(d, "min_I", min_I);
install(d, "max_I", max_I); install_binary(d, "max_I", max_I);
install(d, "pow_I", pow_I); install_binary(d, "pow_I", pow_I);
install(d, "invert_I", invert_I); install_unary(d, "invert_I", invert_I);
install(d, "and_I", and_I); install_binary(d, "and_I", and_I);
install(d, "or_I", or_I); install_binary(d, "or_I", or_I);
install(d, "xor_I", xor_I); install_binary(d, "xor_I", xor_I);
install(d, "lshift_I", lshift_I); install_binary(d, "lshift_I", lshift_I);
install(d, "rshift_I", rshift_I); install_binary(d, "rshift_I", rshift_I);
install(d, "eq_I", eq_I); install_binary(d, "eq_I", eq_I);
install(d, "ne_I", ne_I); install_binary(d, "ne_I", ne_I);
install(d, "lt_I", lt_I); install_binary(d, "lt_I", lt_I);
install(d, "le_I", le_I); install_binary(d, "le_I", le_I);
install(d, "gt_I", gt_I); install_binary(d, "gt_I", gt_I);
install(d, "ge_I", ge_I); install_binary(d, "ge_I", ge_I);
install(d, "abs_F", abs_F); install_unary(d, "abs_F", abs_F);
install(d, "neg_F", neg_F); install_unary(d, "neg_F", neg_F);
install(d, "add_F", add_F); install_binary(d, "add_F", add_F);
install(d, "sub_F", sub_F); install_binary(d, "sub_F", sub_F);
install(d, "diff_F", diff_F); install_binary(d, "diff_F", diff_F);
install(d, "mul_F", mul_F); install_binary(d, "mul_F", mul_F);
install(d, "div_F", div_F); install_binary(d, "div_F", div_F);
install(d, "mod_F", mod_F); install_binary(d, "mod_F", mod_F);
install(d, "min_F", min_F); install_binary(d, "min_F", min_F);
install(d, "max_F", max_F); install_binary(d, "max_F", max_F);
install(d, "pow_F", pow_F); install_binary(d, "pow_F", pow_F);
install(d, "eq_F", eq_F); install_binary(d, "eq_F", eq_F);
install(d, "ne_F", ne_F); install_binary(d, "ne_F", ne_F);
install(d, "lt_F", lt_F); install_binary(d, "lt_F", lt_F);
install(d, "le_F", le_F); install_binary(d, "le_F", le_F);
install(d, "gt_F", gt_F); install_binary(d, "gt_F", gt_F);
install(d, "ge_F", ge_F); install_binary(d, "ge_F", ge_F);
return 0; return 0;
} }

View File

@ -11,7 +11,6 @@
* See the README file for information on usage and redistribution. * See the README file for information on usage and redistribution.
*/ */
#include "Python.h"
#include "libImaging/Imaging.h" #include "libImaging/Imaging.h"
#define LUT_SIZE (1 << 9) #define LUT_SIZE (1 << 9)
@ -30,35 +29,31 @@
static PyObject * static PyObject *
apply(PyObject *self, PyObject *args) { apply(PyObject *self, PyObject *args) {
const char *lut; const char *lut;
PyObject *py_lut; Py_ssize_t lut_len;
Py_ssize_t lut_len, i0, i1; PyObject *i0, *i1;
Imaging imgin, imgout; Imaging imgin, imgout;
int width, height; int width, height;
int row_idx, col_idx; int row_idx, col_idx;
UINT8 **inrows, **outrows; UINT8 **inrows, **outrows;
int num_changed_pixels = 0; int num_changed_pixels = 0;
if (!PyArg_ParseTuple(args, "Onn", &py_lut, &i0, &i1)) { if (!PyArg_ParseTuple(args, "s#OO", &lut, &lut_len, &i0, &i1)) {
PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
return NULL; return NULL;
} }
if (!PyBytes_Check(py_lut)) {
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a bytes object");
return NULL;
}
lut_len = PyBytes_Size(py_lut);
if (lut_len < LUT_SIZE) { if (lut_len < LUT_SIZE) {
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size"); PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
return NULL; return NULL;
} }
lut = PyBytes_AsString(py_lut); if (!PyCapsule_IsValid(i0, IMAGING_MAGIC) ||
!PyCapsule_IsValid(i1, IMAGING_MAGIC)) {
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
return NULL;
}
imgin = (Imaging)i0; imgin = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
imgout = (Imaging)i1; imgout = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);
width = imgin->xsize; width = imgin->xsize;
height = imgin->ysize; height = imgin->ysize;
@ -126,46 +121,39 @@ apply(PyObject *self, PyObject *args) {
static PyObject * static PyObject *
match(PyObject *self, PyObject *args) { match(PyObject *self, PyObject *args) {
const char *lut; const char *lut;
PyObject *py_lut; Py_ssize_t lut_len;
Py_ssize_t lut_len, i0; PyObject *i0;
Imaging imgin; Imaging imgin;
int width, height; int width, height;
int row_idx, col_idx; int row_idx, col_idx;
UINT8 **inrows; UINT8 **inrows;
PyObject *ret = PyList_New(0);
if (ret == NULL) { if (!PyArg_ParseTuple(args, "s#O", &lut, &lut_len, &i0)) {
return NULL; return NULL;
} }
if (!PyArg_ParseTuple(args, "On", &py_lut, &i0)) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
return NULL;
}
if (!PyBytes_Check(py_lut)) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a bytes object");
return NULL;
}
lut_len = PyBytes_Size(py_lut);
if (lut_len < LUT_SIZE) { if (lut_len < LUT_SIZE) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size"); PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
return NULL; return NULL;
} }
lut = PyBytes_AsString(py_lut); if (!PyCapsule_IsValid(i0, IMAGING_MAGIC)) {
imgin = (Imaging)i0; PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
return NULL;
}
imgin = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
if (imgin->type != IMAGING_TYPE_UINT8 || imgin->bands != 1) { if (imgin->type != IMAGING_TYPE_UINT8 || imgin->bands != 1) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "Unsupported image type"); PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
return NULL; return NULL;
} }
PyObject *ret = PyList_New(0);
if (ret == NULL) {
return NULL;
}
inrows = imgin->image8; inrows = imgin->image8;
width = imgin->xsize; width = imgin->xsize;
height = imgin->ysize; height = imgin->ysize;
@ -212,26 +200,31 @@ match(PyObject *self, PyObject *args) {
*/ */
static PyObject * static PyObject *
get_on_pixels(PyObject *self, PyObject *args) { get_on_pixels(PyObject *self, PyObject *args) {
Py_ssize_t i0; PyObject *i0;
Imaging img; Imaging img;
UINT8 **rows; UINT8 **rows;
int row_idx, col_idx; int row_idx, col_idx;
int width, height; int width, height;
if (!PyArg_ParseTuple(args, "O", &i0)) {
return NULL;
}
if (!PyCapsule_IsValid(i0, IMAGING_MAGIC)) {
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
return NULL;
}
img = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
rows = img->image8;
width = img->xsize;
height = img->ysize;
PyObject *ret = PyList_New(0); PyObject *ret = PyList_New(0);
if (ret == NULL) { if (ret == NULL) {
return NULL; return NULL;
} }
if (!PyArg_ParseTuple(args, "n", &i0)) {
Py_DECREF(ret);
PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
return NULL;
}
img = (Imaging)i0;
rows = img->image8;
width = img->xsize;
height = img->ysize;
for (row_idx = 0; row_idx < height; row_idx++) { for (row_idx = 0; row_idx < height; row_idx++) {
UINT8 *row = rows[row_idx]; UINT8 *row = rows[row_idx];
for (col_idx = 0; col_idx < width; col_idx++) { for (col_idx = 0; col_idx < width; col_idx++) {

View File

@ -7,6 +7,7 @@
* Copyright (c) Fredrik Lundh 1995-2003. * Copyright (c) Fredrik Lundh 1995-2003.
*/ */
#define PY_SSIZE_T_CLEAN
#include "Python.h" #include "Python.h"
/* Check that we have an ANSI compliant compiler */ /* Check that we have an ANSI compliant compiler */

View File

@ -60,8 +60,8 @@ typedef struct ImagingHistogramInstance *ImagingHistogram;
typedef struct ImagingOutlineInstance *ImagingOutline; typedef struct ImagingOutlineInstance *ImagingOutline;
typedef struct ImagingPaletteInstance *ImagingPalette; typedef struct ImagingPaletteInstance *ImagingPalette;
/* handle magics (used with PyCObject). */ /* handle magics (used with PyCapsule). */
#define IMAGING_MAGIC "PIL Imaging" #define IMAGING_MAGIC "Pillow Imaging"
/* pixel types */ /* pixel types */
#define IMAGING_TYPE_UINT8 0 #define IMAGING_TYPE_UINT8 0