2010-07-31 06:52:47 +04:00
|
|
|
/*
|
|
|
|
* The Python Imaging Library.
|
|
|
|
*
|
|
|
|
* standard memory mapping interface for the Imaging library
|
|
|
|
*
|
|
|
|
* history:
|
|
|
|
* 1998-03-05 fl added Win32 read mapping
|
|
|
|
* 1999-02-06 fl added "I;16" support
|
|
|
|
* 2003-04-21 fl added PyImaging_MapBuffer primitive
|
|
|
|
*
|
|
|
|
* Copyright (c) 1998-2003 by Secret Labs AB.
|
|
|
|
* Copyright (c) 2003 by Fredrik Lundh.
|
|
|
|
*
|
|
|
|
* See the README file for information on usage and redistribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: should move the memory mapping primitives into libImaging!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
#include "Imaging.h"
|
|
|
|
|
|
|
|
/* compatibility wrappers (defined in _imaging.c) */
|
|
|
|
extern int PyImaging_CheckBuffer(PyObject* buffer);
|
2012-10-21 01:13:12 +04:00
|
|
|
extern int PyImaging_GetBuffer(PyObject* buffer, Py_buffer *view);
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Standard mapper */
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
char* base;
|
|
|
|
int size;
|
|
|
|
int offset;
|
2014-05-09 23:05:30 +04:00
|
|
|
#ifdef _WIN32
|
2010-07-31 06:52:47 +04:00
|
|
|
HANDLE hFile;
|
|
|
|
HANDLE hMap;
|
|
|
|
#endif
|
|
|
|
} ImagingMapperObject;
|
|
|
|
|
2012-10-13 20:53:07 +04:00
|
|
|
static PyTypeObject ImagingMapperType;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
ImagingMapperObject*
|
|
|
|
PyImaging_MapperNew(const char* filename, int readonly)
|
|
|
|
{
|
|
|
|
ImagingMapperObject *mapper;
|
|
|
|
|
2012-10-13 20:53:07 +04:00
|
|
|
if (PyType_Ready(&ImagingMapperType) < 0)
|
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
mapper = PyObject_New(ImagingMapperObject, &ImagingMapperType);
|
|
|
|
if (mapper == NULL)
|
2013-12-14 03:13:43 +04:00
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
mapper->base = NULL;
|
|
|
|
mapper->size = mapper->offset = 0;
|
|
|
|
|
2014-05-09 23:05:30 +04:00
|
|
|
#ifdef _WIN32
|
2010-07-31 06:52:47 +04:00
|
|
|
mapper->hFile = (HANDLE)-1;
|
|
|
|
mapper->hMap = (HANDLE)-1;
|
|
|
|
|
|
|
|
/* FIXME: currently supports readonly mappings only */
|
|
|
|
mapper->hFile = CreateFile(
|
|
|
|
filename,
|
|
|
|
GENERIC_READ,
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
if (mapper->hFile == (HANDLE)-1) {
|
2020-04-07 09:58:21 +03:00
|
|
|
PyErr_SetString(PyExc_OSError, "cannot open file");
|
2016-05-30 13:16:16 +03:00
|
|
|
Py_DECREF(mapper);
|
2010-07-31 06:52:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapper->hMap = CreateFileMapping(
|
|
|
|
mapper->hFile, NULL,
|
|
|
|
PAGE_READONLY,
|
|
|
|
0, 0, NULL);
|
|
|
|
if (mapper->hMap == (HANDLE)-1) {
|
2013-12-14 03:13:43 +04:00
|
|
|
CloseHandle(mapper->hFile);
|
2020-04-07 09:58:21 +03:00
|
|
|
PyErr_SetString(PyExc_OSError, "cannot map file");
|
2016-05-30 13:16:16 +03:00
|
|
|
Py_DECREF(mapper);
|
2010-07-31 06:52:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapper->base = (char*) MapViewOfFile(
|
|
|
|
mapper->hMap,
|
|
|
|
FILE_MAP_READ,
|
|
|
|
0, 0, 0);
|
|
|
|
|
|
|
|
mapper->size = GetFileSize(mapper->hFile, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mapping_dealloc(ImagingMapperObject* mapper)
|
|
|
|
{
|
2014-05-09 23:05:30 +04:00
|
|
|
#ifdef _WIN32
|
2010-07-31 06:52:47 +04:00
|
|
|
if (mapper->base != 0)
|
2013-12-14 03:13:43 +04:00
|
|
|
UnmapViewOfFile(mapper->base);
|
2010-07-31 06:52:47 +04:00
|
|
|
if (mapper->hMap != (HANDLE)-1)
|
2013-12-14 03:13:43 +04:00
|
|
|
CloseHandle(mapper->hMap);
|
2010-07-31 06:52:47 +04:00
|
|
|
if (mapper->hFile != (HANDLE)-1)
|
2013-12-14 03:13:43 +04:00
|
|
|
CloseHandle(mapper->hFile);
|
2010-07-31 06:52:47 +04:00
|
|
|
mapper->base = 0;
|
|
|
|
mapper->hMap = mapper->hFile = (HANDLE)-1;
|
|
|
|
#endif
|
|
|
|
PyObject_Del(mapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* standard file operations */
|
|
|
|
|
2013-07-01 02:42:19 +04:00
|
|
|
static PyObject*
|
2010-07-31 06:52:47 +04:00
|
|
|
mapping_read(ImagingMapperObject* mapper, PyObject* args)
|
|
|
|
{
|
|
|
|
PyObject* buf;
|
|
|
|
|
|
|
|
int size = -1;
|
|
|
|
if (!PyArg_ParseTuple(args, "|i", &size))
|
2013-12-14 03:13:43 +04:00
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* check size */
|
|
|
|
if (size < 0 || mapper->offset + size > mapper->size)
|
|
|
|
size = mapper->size - mapper->offset;
|
|
|
|
if (size < 0)
|
|
|
|
size = 0;
|
|
|
|
|
2012-10-14 08:47:30 +04:00
|
|
|
buf = PyBytes_FromStringAndSize(NULL, size);
|
2010-07-31 06:52:47 +04:00
|
|
|
if (!buf)
|
2013-12-14 03:13:43 +04:00
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if (size > 0) {
|
2012-10-14 08:47:30 +04:00
|
|
|
memcpy(PyBytes_AsString(buf), mapper->base + mapper->offset, size);
|
2010-07-31 06:52:47 +04:00
|
|
|
mapper->offset += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-07-01 02:42:19 +04:00
|
|
|
static PyObject*
|
2010-07-31 06:52:47 +04:00
|
|
|
mapping_seek(ImagingMapperObject* mapper, PyObject* args)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
int whence = 0;
|
|
|
|
if (!PyArg_ParseTuple(args, "i|i", &offset, &whence))
|
2013-12-14 03:13:43 +04:00
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
switch (whence) {
|
|
|
|
case 0: /* SEEK_SET */
|
|
|
|
mapper->offset = offset;
|
|
|
|
break;
|
|
|
|
case 1: /* SEEK_CUR */
|
|
|
|
mapper->offset += offset;
|
|
|
|
break;
|
|
|
|
case 2: /* SEEK_END */
|
|
|
|
mapper->offset = mapper->size + offset;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* FIXME: raise ValueError? */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* map entire image */
|
|
|
|
|
|
|
|
extern PyObject*PyImagingNew(Imaging im);
|
|
|
|
|
|
|
|
static void
|
|
|
|
ImagingDestroyMap(Imaging im)
|
|
|
|
{
|
|
|
|
return; /* nothing to do! */
|
|
|
|
}
|
|
|
|
|
2013-07-01 02:42:19 +04:00
|
|
|
static PyObject*
|
2010-07-31 06:52:47 +04:00
|
|
|
mapping_readimage(ImagingMapperObject* mapper, PyObject* args)
|
|
|
|
{
|
|
|
|
int y, size;
|
|
|
|
Imaging im;
|
|
|
|
|
|
|
|
char* mode;
|
|
|
|
int xsize;
|
|
|
|
int ysize;
|
|
|
|
int stride;
|
|
|
|
int orientation;
|
|
|
|
if (!PyArg_ParseTuple(args, "s(ii)ii", &mode, &xsize, &ysize,
|
|
|
|
&stride, &orientation))
|
2013-12-14 03:13:43 +04:00
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if (stride <= 0) {
|
|
|
|
/* FIXME: maybe we should call ImagingNewPrologue instead */
|
|
|
|
if (!strcmp(mode, "L") || !strcmp(mode, "P"))
|
|
|
|
stride = xsize;
|
|
|
|
else if (!strcmp(mode, "I;16") || !strcmp(mode, "I;16B"))
|
|
|
|
stride = xsize * 2;
|
|
|
|
else
|
|
|
|
stride = xsize * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = ysize * stride;
|
|
|
|
|
|
|
|
if (mapper->offset + size > mapper->size) {
|
2020-04-07 09:58:21 +03:00
|
|
|
PyErr_SetString(PyExc_OSError, "image file truncated");
|
2010-07-31 06:52:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
im = ImagingNewPrologue(mode, xsize, ysize);
|
|
|
|
if (!im)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* setup file pointers */
|
|
|
|
if (orientation > 0)
|
|
|
|
for (y = 0; y < ysize; y++)
|
|
|
|
im->image[y] = mapper->base + mapper->offset + y * stride;
|
|
|
|
else
|
|
|
|
for (y = 0; y < ysize; y++)
|
|
|
|
im->image[ysize-y-1] = mapper->base + mapper->offset + y * stride;
|
|
|
|
|
|
|
|
im->destroy = ImagingDestroyMap;
|
|
|
|
|
2013-07-01 02:42:19 +04:00
|
|
|
mapper->offset += size;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return PyImagingNew(im);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct PyMethodDef methods[] = {
|
|
|
|
/* standard file interface */
|
|
|
|
{"read", (PyCFunction)mapping_read, 1},
|
|
|
|
{"seek", (PyCFunction)mapping_seek, 1},
|
|
|
|
/* extensions */
|
|
|
|
{"readimage", (PyCFunction)mapping_readimage, 1},
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2012-10-13 20:53:07 +04:00
|
|
|
static PyTypeObject ImagingMapperType = {
|
2013-12-14 03:13:43 +04:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"ImagingMapper", /*tp_name*/
|
|
|
|
sizeof(ImagingMapperObject), /*tp_size*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
/* methods */
|
|
|
|
(destructor)mapping_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
2012-10-13 20:53:07 +04:00
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number */
|
|
|
|
0, /*tp_as_sequence */
|
|
|
|
0, /*tp_as_mapping */
|
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /*tp_as_buffer*/
|
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
|
|
|
0, /*tp_doc*/
|
|
|
|
0, /*tp_traverse*/
|
|
|
|
0, /*tp_clear*/
|
|
|
|
0, /*tp_richcompare*/
|
|
|
|
0, /*tp_weaklistoffset*/
|
|
|
|
0, /*tp_iter*/
|
|
|
|
0, /*tp_iternext*/
|
|
|
|
methods, /*tp_methods*/
|
|
|
|
0, /*tp_members*/
|
|
|
|
0, /*tp_getset*/
|
2010-07-31 06:52:47 +04:00
|
|
|
};
|
|
|
|
|
2013-07-01 02:42:19 +04:00
|
|
|
PyObject*
|
2010-07-31 06:52:47 +04:00
|
|
|
PyImaging_Mapper(PyObject* self, PyObject* args)
|
|
|
|
{
|
|
|
|
char* filename;
|
|
|
|
if (!PyArg_ParseTuple(args, "s", &filename))
|
2013-12-14 03:13:43 +04:00
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return (PyObject*) PyImaging_MapperNew(filename, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Buffer mapper */
|
|
|
|
|
|
|
|
typedef struct ImagingBufferInstance {
|
|
|
|
struct ImagingMemoryInstance im;
|
|
|
|
PyObject* target;
|
2012-10-21 01:13:12 +04:00
|
|
|
Py_buffer view;
|
2010-07-31 06:52:47 +04:00
|
|
|
} ImagingBufferInstance;
|
|
|
|
|
|
|
|
static void
|
|
|
|
mapping_destroy_buffer(Imaging im)
|
|
|
|
{
|
|
|
|
ImagingBufferInstance* buffer = (ImagingBufferInstance*) im;
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2012-10-21 01:13:12 +04:00
|
|
|
PyBuffer_Release(&buffer->view);
|
2010-07-31 06:52:47 +04:00
|
|
|
Py_XDECREF(buffer->target);
|
|
|
|
}
|
|
|
|
|
2013-07-01 02:42:19 +04:00
|
|
|
PyObject*
|
2010-07-31 06:52:47 +04:00
|
|
|
PyImaging_MapBuffer(PyObject* self, PyObject* args)
|
|
|
|
{
|
2013-12-14 03:13:19 +04:00
|
|
|
Py_ssize_t y, size;
|
2010-07-31 06:52:47 +04:00
|
|
|
Imaging im;
|
|
|
|
|
|
|
|
PyObject* target;
|
2012-10-21 01:13:12 +04:00
|
|
|
Py_buffer view;
|
2010-07-31 06:52:47 +04:00
|
|
|
char* mode;
|
|
|
|
char* codec;
|
2013-12-14 03:13:19 +04:00
|
|
|
Py_ssize_t offset;
|
2010-07-31 06:52:47 +04:00
|
|
|
int xsize, ysize;
|
|
|
|
int stride;
|
|
|
|
int ystep;
|
|
|
|
|
2020-01-25 14:37:26 +03:00
|
|
|
if (!PyArg_ParseTuple(args, "O(ii)sn(sii)", &target, &xsize, &ysize,
|
|
|
|
&codec, &offset, &mode, &stride, &ystep))
|
2013-12-14 03:13:43 +04:00
|
|
|
return NULL;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if (!PyImaging_CheckBuffer(target)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "expected string or buffer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stride <= 0) {
|
|
|
|
if (!strcmp(mode, "L") || !strcmp(mode, "P"))
|
|
|
|
stride = xsize;
|
|
|
|
else if (!strncmp(mode, "I;16", 4))
|
|
|
|
stride = xsize * 2;
|
|
|
|
else
|
|
|
|
stride = xsize * 4;
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:12:36 +03:00
|
|
|
if (stride > 0 && ysize > PY_SSIZE_T_MAX / stride) {
|
2016-09-29 17:05:00 +03:00
|
|
|
PyErr_SetString(PyExc_MemoryError, "Integer overflow in ysize");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-14 03:13:19 +04:00
|
|
|
size = (Py_ssize_t) ysize * stride;
|
2016-09-29 17:05:00 +03:00
|
|
|
|
2016-10-04 16:40:22 +03:00
|
|
|
if (offset > PY_SSIZE_T_MAX - size) {
|
2016-09-29 17:05:00 +03:00
|
|
|
PyErr_SetString(PyExc_MemoryError, "Integer overflow in offset");
|
|
|
|
return NULL;
|
2017-02-22 09:28:20 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* check buffer size */
|
2012-10-21 01:13:12 +04:00
|
|
|
if (PyImaging_GetBuffer(target, &view) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (view.len < 0) {
|
2010-07-31 06:52:47 +04:00
|
|
|
PyErr_SetString(PyExc_ValueError, "buffer has negative size");
|
2020-01-25 15:22:03 +03:00
|
|
|
PyBuffer_Release(&view);
|
2010-07-31 06:52:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-10-21 01:13:12 +04:00
|
|
|
if (offset + size > view.len) {
|
2010-07-31 06:52:47 +04:00
|
|
|
PyErr_SetString(PyExc_ValueError, "buffer is not large enough");
|
2020-01-25 15:22:03 +03:00
|
|
|
PyBuffer_Release(&view);
|
2010-07-31 06:52:47 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
im = ImagingNewPrologueSubtype(
|
2017-08-05 20:39:14 +03:00
|
|
|
mode, xsize, ysize, sizeof(ImagingBufferInstance));
|
2020-01-25 15:22:03 +03:00
|
|
|
if (!im) {
|
|
|
|
PyBuffer_Release(&view);
|
2010-07-31 06:52:47 +04:00
|
|
|
return NULL;
|
2020-01-25 15:22:03 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
/* setup file pointers */
|
|
|
|
if (ystep > 0)
|
|
|
|
for (y = 0; y < ysize; y++)
|
2013-01-10 09:30:33 +04:00
|
|
|
im->image[y] = (char*)view.buf + offset + y * stride;
|
2010-07-31 06:52:47 +04:00
|
|
|
else
|
|
|
|
for (y = 0; y < ysize; y++)
|
2013-01-10 09:30:33 +04:00
|
|
|
im->image[ysize-y-1] = (char*)view.buf + offset + y * stride;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
im->destroy = mapping_destroy_buffer;
|
|
|
|
|
|
|
|
Py_INCREF(target);
|
|
|
|
((ImagingBufferInstance*) im)->target = target;
|
2012-10-21 01:13:12 +04:00
|
|
|
((ImagingBufferInstance*) im)->view = view;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
return PyImagingNew(im);
|
|
|
|
}
|
|
|
|
|