diff --git a/decode.c b/decode.c index 58ba396a4..7bb88d94e 100644 --- a/decode.c +++ b/decode.c @@ -850,7 +850,6 @@ PyImaging_Jpeg2KDecoderNew(PyObject* self, PyObject* args) int layers = 0; int fd = -1; PY_LONG_LONG length = -1; - PyObject * py_fd; if (!PyArg_ParseTuple(args, "ss|iiiL", &mode, &format, &reduce, &layers, &fd, &length)) @@ -886,60 +885,3 @@ PyImaging_Jpeg2KDecoderNew(PyObject* self, PyObject* args) } #endif /* HAVE_OPENJPEG */ -Py_ssize_t -_imaging_read_pyFd(PyObject *fd, char* dest, Py_ssize_t bytes) -{ - /* dest should be a buffer bytes long, returns length of read - -1 on error */ - - PyObject *result; - char *buffer; - Py_ssize_t length; - int bytes_result; - - result = PyObject_CallMethod(fd, "read", "n", bytes); - - bytes_result = PyBytes_AsStringAndSize(result, &buffer, &length); - if (bytes_result == -1) { - goto err; - } - - if (length > bytes) { - goto err; - } - - memcpy(dest, buffer, length); - - Py_DECREF(result); - return length; - - err: - Py_DECREF(result); - return -1; - -} - -int -_imaging_seek_pyFd(PyObject *fd, Py_ssize_t offset, int whence) -{ - PyObject *result; - - result = PyObject_CallMethod(fd, "seek", "ni", offset, whence); - - Py_DECREF(result); - return 0; - -} - -Py_ssize_t -_imaging_tell_pyFd(PyObject *fd) -{ - PyObject *result; - Py_ssize_t location; - - result = PyObject_CallMethod(fd, "tell", NULL); - location = PyInt_AsSsize_t(result); - - Py_DECREF(result); - return location; -} diff --git a/encode.c b/encode.c index 62faf86ff..b08a9aa57 100644 --- a/encode.c +++ b/encode.c @@ -1032,25 +1032,6 @@ PyImaging_Jpeg2KEncoderNew(PyObject *self, PyObject *args) #endif -Py_ssize_t -_imaging_write_pyFd(PyObject *fd, char* src, Py_ssize_t bytes) -{ - - PyObject *result; - PyObject *byteObj; - - byteObj = PyBytes_FromStringAndSize(src, bytes); - result = PyObject_CallMethod(fd, "write", "O", byteObj); - - Py_DECREF(byteObj); - Py_DECREF(result); - - return bytes; - -} - - - /* * Local Variables: * c-basic-offset: 4 diff --git a/libImaging/codec_fd.c b/libImaging/codec_fd.c new file mode 100644 index 000000000..33954ece0 --- /dev/null +++ b/libImaging/codec_fd.c @@ -0,0 +1,79 @@ +#include "Python.h" +#include "Imaging.h" +#include "../py3.h" + + +Py_ssize_t +_imaging_read_pyFd(PyObject *fd, char* dest, Py_ssize_t bytes) +{ + /* dest should be a buffer bytes long, returns length of read + -1 on error */ + + PyObject *result; + char *buffer; + Py_ssize_t length; + int bytes_result; + + result = PyObject_CallMethod(fd, "read", "n", bytes); + + bytes_result = PyBytes_AsStringAndSize(result, &buffer, &length); + if (bytes_result == -1) { + goto err; + } + + if (length > bytes) { + goto err; + } + + memcpy(dest, buffer, length); + + Py_DECREF(result); + return length; + + err: + Py_DECREF(result); + return -1; + +} + +Py_ssize_t +_imaging_write_pyFd(PyObject *fd, char* src, Py_ssize_t bytes) +{ + + PyObject *result; + PyObject *byteObj; + + byteObj = PyBytes_FromStringAndSize(src, bytes); + result = PyObject_CallMethod(fd, "write", "O", byteObj); + + Py_DECREF(byteObj); + Py_DECREF(result); + + return bytes; + +} + +int +_imaging_seek_pyFd(PyObject *fd, Py_ssize_t offset, int whence) +{ + PyObject *result; + + result = PyObject_CallMethod(fd, "seek", "ni", offset, whence); + + Py_DECREF(result); + return 0; + +} + +Py_ssize_t +_imaging_tell_pyFd(PyObject *fd) +{ + PyObject *result; + Py_ssize_t location; + + result = PyObject_CallMethod(fd, "tell", NULL); + location = PyInt_AsSsize_t(result); + + Py_DECREF(result); + return location; +} diff --git a/setup.py b/setup.py index cf1407282..bf9adb718 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ _LIB_IMAGING = ( "RankFilter", "RawDecode", "RawEncode", "Storage", "SunRleDecode", "TgaRleDecode", "Unpack", "UnpackYCC", "UnsharpMask", "XbmDecode", "XbmEncode", "ZipDecode", "ZipEncode", "TiffDecode", "Jpeg2KDecode", - "Jpeg2KEncode", "BoxBlur", "QuantPngQuant") + "Jpeg2KEncode", "BoxBlur", "QuantPngQuant", "codec_fd") DEBUG = False