~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to map.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
#include "Python.h"
22
22
 
23
 
#if PY_VERSION_HEX < 0x01060000
24
 
#define PyObject_New PyObject_NEW
25
 
#define PyObject_Del PyMem_DEL
26
 
#endif
27
 
 
28
23
#include "Imaging.h"
29
24
 
30
25
#ifdef WIN32
31
26
#define WIN32_LEAN_AND_MEAN
 
27
#undef INT8
 
28
#undef UINT8
 
29
#undef INT16
 
30
#undef UINT16
32
31
#undef INT32
33
32
#undef INT64
34
33
#undef UINT32
35
34
#include "windows.h"
36
35
#endif
37
36
 
 
37
#include "py3.h"
 
38
 
38
39
/* compatibility wrappers (defined in _imaging.c) */
39
40
extern int PyImaging_CheckBuffer(PyObject* buffer);
40
 
extern int PyImaging_ReadBuffer(PyObject* buffer, const void** ptr);
 
41
extern int PyImaging_GetBuffer(PyObject* buffer, Py_buffer *view);
41
42
 
42
43
/* -------------------------------------------------------------------- */
43
44
/* Standard mapper */
53
54
#endif
54
55
} ImagingMapperObject;
55
56
 
56
 
staticforward PyTypeObject ImagingMapperType;
 
57
static PyTypeObject ImagingMapperType;
57
58
 
58
59
ImagingMapperObject*
59
60
PyImaging_MapperNew(const char* filename, int readonly)
60
61
{
61
62
    ImagingMapperObject *mapper;
62
63
 
63
 
    ImagingMapperType.ob_type = &PyType_Type;
 
64
    if (PyType_Ready(&ImagingMapperType) < 0)
 
65
        return NULL;
64
66
 
65
67
    mapper = PyObject_New(ImagingMapperObject, &ImagingMapperType);
66
68
    if (mapper == NULL)
143
145
    if (size < 0)
144
146
        size = 0;
145
147
 
146
 
    buf = PyString_FromStringAndSize(NULL, size);
 
148
    buf = PyBytes_FromStringAndSize(NULL, size);
147
149
    if (!buf)
148
150
        return NULL;
149
151
 
150
152
    if (size > 0) {
151
 
        memcpy(PyString_AsString(buf), mapper->base + mapper->offset, size);
 
153
        memcpy(PyBytes_AsString(buf), mapper->base + mapper->offset, size);
152
154
        mapper->offset += size;
153
155
    }
154
156
 
256
258
    {NULL, NULL} /* sentinel */
257
259
};
258
260
 
259
 
static PyObject*  
260
 
mapping_getattr(ImagingMapperObject* self, char* name)
261
 
{
262
 
    return Py_FindMethod(methods, (PyObject*) self, name);
263
 
}
264
 
 
265
 
statichere PyTypeObject ImagingMapperType = {
266
 
        PyObject_HEAD_INIT(NULL)
267
 
        0,                              /*ob_size*/
 
261
static PyTypeObject ImagingMapperType = {
 
262
        PyVarObject_HEAD_INIT(NULL, 0)
268
263
        "ImagingMapper",                /*tp_name*/
269
264
        sizeof(ImagingMapperObject),    /*tp_size*/
270
265
        0,                              /*tp_itemsize*/
271
266
        /* methods */
272
267
        (destructor)mapping_dealloc,    /*tp_dealloc*/
273
268
        0,                              /*tp_print*/
274
 
        (getattrfunc)mapping_getattr,   /*tp_getattr*/
275
 
        0,                              /*tp_setattr*/
276
 
        0,                              /*tp_compare*/
277
 
        0,                              /*tp_repr*/
278
 
        0,                              /*tp_hash*/
 
269
    0,                          /*tp_getattr*/
 
270
    0,                          /*tp_setattr*/
 
271
    0,                          /*tp_compare*/
 
272
    0,                          /*tp_repr*/
 
273
    0,                          /*tp_as_number */
 
274
    0,                          /*tp_as_sequence */
 
275
    0,                          /*tp_as_mapping */
 
276
    0,                          /*tp_hash*/
 
277
    0,                          /*tp_call*/
 
278
    0,                          /*tp_str*/
 
279
    0,                          /*tp_getattro*/
 
280
    0,                          /*tp_setattro*/
 
281
    0,                          /*tp_as_buffer*/
 
282
    Py_TPFLAGS_DEFAULT,         /*tp_flags*/
 
283
    0,                          /*tp_doc*/
 
284
    0,                          /*tp_traverse*/
 
285
    0,                          /*tp_clear*/
 
286
    0,                          /*tp_richcompare*/
 
287
    0,                          /*tp_weaklistoffset*/
 
288
    0,                          /*tp_iter*/
 
289
    0,                          /*tp_iternext*/
 
290
    methods,                    /*tp_methods*/
 
291
    0,                          /*tp_members*/
 
292
    0,                          /*tp_getset*/
279
293
};
280
294
 
281
295
PyObject* 
294
308
typedef struct ImagingBufferInstance {
295
309
    struct ImagingMemoryInstance im;
296
310
    PyObject* target;
 
311
    Py_buffer view;
297
312
} ImagingBufferInstance;
298
313
 
299
314
static void
301
316
{
302
317
    ImagingBufferInstance* buffer = (ImagingBufferInstance*) im;
303
318
    
 
319
    PyBuffer_Release(&buffer->view);
304
320
    Py_XDECREF(buffer->target);
305
321
}
306
322
 
309
325
{
310
326
    int y, size;
311
327
    Imaging im;
312
 
    char* ptr;
313
 
    int bytes;
314
328
 
315
329
    PyObject* target;
 
330
    Py_buffer view;
316
331
    char* mode;
317
332
    char* codec;
318
333
    PyObject* bbox;
342
357
    size = ysize * stride;
343
358
 
344
359
    /* check buffer size */
345
 
    bytes = PyImaging_ReadBuffer(target, (const void**) &ptr);
346
 
    if (bytes < 0) {
 
360
    if (PyImaging_GetBuffer(target, &view) < 0)
 
361
        return NULL;
 
362
 
 
363
    if (view.len < 0) {
347
364
        PyErr_SetString(PyExc_ValueError, "buffer has negative size");
348
365
        return NULL;
349
366
    }
350
 
    if (offset + size > bytes) {
 
367
    if (offset + size > view.len) {
351
368
        PyErr_SetString(PyExc_ValueError, "buffer is not large enough");
352
369
        return NULL;
353
370
    }
361
378
    /* setup file pointers */
362
379
    if (ystep > 0)
363
380
        for (y = 0; y < ysize; y++)
364
 
            im->image[y] = ptr + offset + y * stride;
 
381
            im->image[y] = (char*)view.buf + offset + y * stride;
365
382
    else
366
383
        for (y = 0; y < ysize; y++)
367
 
            im->image[ysize-y-1] = ptr + offset + y * stride;
 
384
            im->image[ysize-y-1] = (char*)view.buf + offset + y * stride;
368
385
 
369
386
    im->destroy = mapping_destroy_buffer;
370
387
 
371
388
    Py_INCREF(target);
372
389
    ((ImagingBufferInstance*) im)->target = target;
 
390
    ((ImagingBufferInstance*) im)->view = view;
373
391
 
374
392
    if (!ImagingNewEpilogue(im))
375
393
        return NULL;