aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py3/map.c
blob: c298bd1482a6eb2583e431ff1a9ed9a344c8cfe0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * 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 "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);

/* -------------------------------------------------------------------- */
/* Buffer mapper */

typedef struct ImagingBufferInstance {
    struct ImagingMemoryInstance im;
    PyObject *target;
    Py_buffer view;
} ImagingBufferInstance;

static void
mapping_destroy_buffer(Imaging im) {
    ImagingBufferInstance *buffer = (ImagingBufferInstance *)im;

    PyBuffer_Release(&buffer->view);
    Py_XDECREF(buffer->target);
}

PyObject *
PyImaging_MapBuffer(PyObject *self, PyObject *args) {
    Py_ssize_t y, size;
    Imaging im;

    PyObject *target;
    Py_buffer view;
    char *mode;
    char *codec;
    Py_ssize_t offset;
    int xsize, ysize;
    int stride;
    int ystep;

    if (!PyArg_ParseTuple(
            args,
            "O(ii)sn(sii)",
            &target,
            &xsize,
            &ysize,
            &codec,
            &offset,
            &mode,
            &stride,
            &ystep)) {
        return NULL;
    }

    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;
        }
    }

    if (stride > 0 && ysize > PY_SSIZE_T_MAX / stride) {
        PyErr_SetString(PyExc_MemoryError, "Integer overflow in ysize");
        return NULL;
    }

    size = (Py_ssize_t)ysize * stride;

    if (offset > PY_SSIZE_T_MAX - size) {
        PyErr_SetString(PyExc_MemoryError, "Integer overflow in offset");
        return NULL;
    }

    /* check buffer size */
    if (PyImaging_GetBuffer(target, &view) < 0) {
        return NULL;
    }

    if (view.len < 0) {
        PyErr_SetString(PyExc_ValueError, "buffer has negative size");
        PyBuffer_Release(&view);
        return NULL;
    }
    if (offset + size > view.len) {
        PyErr_SetString(PyExc_ValueError, "buffer is not large enough");
        PyBuffer_Release(&view);
        return NULL;
    }

    im = ImagingNewPrologueSubtype(mode, xsize, ysize, sizeof(ImagingBufferInstance));
    if (!im) {
        PyBuffer_Release(&view);
        return NULL;
    }

    /* setup file pointers */
    if (ystep > 0) {
        for (y = 0; y < ysize; y++) {
            im->image[y] = (char *)view.buf + offset + y * stride;
        }
    } else {
        for (y = 0; y < ysize; y++) {
            im->image[ysize - y - 1] = (char *)view.buf + offset + y * stride;
        }
    }

    im->destroy = mapping_destroy_buffer;

    Py_INCREF(target);
    ((ImagingBufferInstance *)im)->target = target;
    ((ImagingBufferInstance *)im)->view = view;

    return PyImagingNew(im);
}