~ubuntu-branches/ubuntu/maverick/pygame/maverick

« back to all changes in this revision

Viewing changes to src/pixelarray.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-01-14 17:02:11 UTC
  • mfrom: (1.3.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100114170211-21eop2ja7mr9vdcr
Tags: 1.9.1release-0ubuntu1
* New upstream version (lp: #433304)
* debian/control:
  - build-depends on libportmidi-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#define PYGAMEAPI_PIXELARRAY_INTERNAL
22
22
 
23
23
#include "pygame.h"
 
24
#include "pgcompat.h"
24
25
#include "pygamedocs.h"
25
26
#include "surface.h"
26
27
 
63
64
/* Sequence methods */
64
65
static Py_ssize_t _pxarray_length (PyPixelArray *array);
65
66
static PyObject* _pxarray_item (PyPixelArray *array, Py_ssize_t _index);
 
67
#if !PY3
66
68
static PyObject* _pxarray_slice (PyPixelArray *array, Py_ssize_t low,
67
69
    Py_ssize_t high);
 
70
#endif
68
71
static int _array_assign_array (PyPixelArray *array, Py_ssize_t low,
69
72
    Py_ssize_t high, PyPixelArray *val);
70
73
static int _array_assign_sequence (PyPixelArray *array, Py_ssize_t low,
91
94
/* Incomplete forward declaration so we can use it in the methods included
92
95
 * below.
93
96
 */
94
 
staticforward PyTypeObject PyPixelArray_Type;
 
97
static PyTypeObject PyPixelArray_Type;
95
98
#define PyPixelArray_Check(o) \
96
 
    ((o)->ob_type == (PyTypeObject *) &PyPixelArray_Type)
 
99
    (Py_TYPE (o) == (PyTypeObject *) &PyPixelArray_Type)
97
100
 
98
101
#define SURFACE_EQUALS(x,y) \
99
102
    (((PyPixelArray *)x)->surface == ((PyPixelArray *)y)->surface)
105
108
 */
106
109
static PyMethodDef _pxarray_methods[] =
107
110
{
108
 
    { "compare", (PyCFunction) _compare, METH_KEYWORDS,
 
111
    { "compare", (PyCFunction) _compare, METH_VARARGS | METH_KEYWORDS,
109
112
      DOC_PIXELARRAYCOMPARE },
110
 
    { "extract", (PyCFunction) _extract_color, METH_KEYWORDS,
 
113
    { "extract", (PyCFunction) _extract_color, METH_VARARGS | METH_KEYWORDS,
111
114
      DOC_PIXELARRAYEXTRACT },
112
 
    { "make_surface", (PyCFunction) _make_surface, METH_NOARGS,
 
115
    { "make_surface", (PyCFunction) _make_surface, METH_VARARGS | METH_NOARGS,
113
116
      DOC_PIXELARRAYMAKESURFACE },
114
 
    { "replace", (PyCFunction) _replace_color, METH_KEYWORDS,
 
117
    { "replace", (PyCFunction) _replace_color, METH_VARARGS | METH_KEYWORDS,
115
118
      DOC_PIXELARRAYREPLACE },
116
119
    { NULL, NULL, 0, NULL }
117
120
};
118
121
 
 
122
#if PY3
 
123
static void
 
124
Text_ConcatAndDel (PyObject **string, PyObject *newpart)
 
125
{
 
126
    PyObject *result = NULL;
 
127
    if (*string != NULL && newpart != NULL) {
 
128
        PyUnicode_Concat (*string, newpart);
 
129
        Py_DECREF (*string);
 
130
        Py_DECREF (newpart);
 
131
    }
 
132
    else {
 
133
        Py_XDECREF (*string);
 
134
        Py_XDECREF (newpart);
 
135
    }
 
136
    *string = result;
 
137
}
 
138
#else
 
139
#define Text_ConcatAndDel PyString_ConcatAndDel
 
140
#endif
 
141
 
119
142
/**
120
143
 * Getters and setters for the PyPixelArray.
121
144
 */
138
161
    NULL, /*sq_concat*/
139
162
    NULL, /*sq_repeat*/
140
163
    (ssizeargfunc) _pxarray_item,               /*sq_item*/
 
164
#if PY3
 
165
    NULL,
 
166
#else
141
167
    (ssizessizeargfunc) _pxarray_slice,         /*sq_slice*/
 
168
#endif
142
169
    (ssizeobjargproc) _pxarray_ass_item,        /*sq_ass_item*/
 
170
#if PY3
 
171
    NULL,
 
172
#else
143
173
    (ssizessizeobjargproc) _pxarray_ass_slice,  /*sq_ass_slice*/
 
174
#endif
144
175
    (objobjproc) _pxarray_contains,             /*sq_contains*/
145
176
    NULL, /*sq_inplace_concat*/
146
177
    NULL, /*sq_inplace_repeat*/
158
189
 
159
190
static PyTypeObject PyPixelArray_Type =
160
191
{
161
 
    PyObject_HEAD_INIT(NULL)
162
 
    0,
 
192
    TYPE_HEAD (NULL, 0)
163
193
    "pygame.PixelArray",        /* tp_name */
164
194
    sizeof (PyPixelArray),      /* tp_basicsize */
165
195
    0,                          /* tp_itemsize */
197
227
    0,                          /* tp_init */
198
228
    0,                          /* tp_alloc */
199
229
    _pxarray_new,               /* tp_new */
 
230
#ifndef __SYMBIAN32__
200
231
    0,                          /* tp_free */
201
232
    0,                          /* tp_is_gc */
202
233
    0,                          /* tp_bases */
205
236
    0,                          /* tp_subclasses */
206
237
    0,                          /* tp_weaklist */
207
238
    0                           /* tp_del */
 
239
#endif    
208
240
};
209
241
 
210
242
static PyPixelArray*
230
262
            if (!self->lock)
231
263
            {
232
264
                Py_DECREF (surface);
233
 
                self->ob_type->tp_free ((PyObject *) self);
 
265
                Py_TYPE (self)->tp_free ((PyObject *) self);
234
266
                return NULL;
235
267
            }
236
268
        }
290
322
    Py_XDECREF (self->parent);
291
323
    Py_XDECREF (self->dict);
292
324
    Py_DECREF (self->surface);
293
 
    self->ob_type->tp_free ((PyObject *) self);
 
325
    Py_TYPE (self)->tp_free ((PyObject *) self);
294
326
}
295
327
 
296
328
/**** Getter and setter access ****/
353
385
        array->xstart, array->xlen, array->xstep, array->ystart, 
354
386
        array->ylen, array->ystep, array->padding);
355
387
*/
356
 
    string = PyString_FromString ("PixelArray(");
 
388
    string = Text_FromUTF8 ("PixelArray(");
357
389
 
358
390
    absxstep = ABS (array->xstep);
359
391
    absystep = ABS (array->ystep);
367
399
        while (posy < array->ylen)
368
400
        {
369
401
            /* Construct the rows */
370
 
            PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
 
402
            Text_ConcatAndDel (&string, Text_FromUTF8 ("\n  ["));
 
403
            if (string == NULL)
 
404
            {
 
405
                return NULL;
 
406
            }
371
407
            posx = 0;
372
408
            x = array->xstart;
373
409
            while (posx < (Uint32)xlen)
374
410
            {
375
411
                /* Construct the columns */
376
412
                pixel = (Uint32) *((Uint8 *) pixels + x + y * array->padding);
377
 
                PyString_ConcatAndDel (&string, PyString_FromFormat
378
 
                    ("%ld, ", (long)pixel));
 
413
                Text_ConcatAndDel (&string,
 
414
                                   Text_FromFormat ("%ld, ", (long)pixel));
 
415
                if (string == NULL)
 
416
                {
 
417
                    return NULL;
 
418
                }
379
419
                x += array->xstep;
380
420
                posx += absxstep;
381
421
            }
382
422
            pixel = (Uint32) *((Uint8 *) pixels + x + y * array->padding);
383
 
            PyString_ConcatAndDel (&string,
384
 
                PyString_FromFormat ("%ld]", (long)pixel));
 
423
            Text_ConcatAndDel (&string,
 
424
                               Text_FromFormat ("%ld]", (long)pixel));
 
425
            if (string == NULL)
 
426
            {
 
427
                return NULL;
 
428
            }
385
429
            y += array->ystep;
386
430
            posy += absystep;
387
431
        }
390
434
        while (posy < array->ylen)
391
435
        {
392
436
            /* Construct the rows */
393
 
            PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
 
437
            Text_ConcatAndDel (&string, Text_FromUTF8 ("\n  ["));
 
438
            if (string == NULL)
 
439
            {
 
440
                return NULL;
 
441
            }
394
442
            posx = 0;
395
443
            x = array->xstart;
396
444
            while (posx < (Uint32)xlen)
398
446
                /* Construct the columns */
399
447
                pixel = (Uint32)
400
448
                    *((Uint16 *) (pixels + y * array->padding) + x);
401
 
                PyString_ConcatAndDel (&string, PyString_FromFormat
402
 
                    ("%ld, ", (long)pixel));
 
449
                Text_ConcatAndDel (&string,
 
450
                                   Text_FromFormat ("%ld, ", (long)pixel));
 
451
                if (string == NULL)
 
452
                {
 
453
                    return NULL;
 
454
                }
403
455
                x += array->xstep;
404
456
                posx += absxstep;
405
457
            }
406
458
            pixel = (Uint32) *((Uint16 *) (pixels + y * array->padding) + x);
407
 
            PyString_ConcatAndDel (&string,
408
 
                PyString_FromFormat ("%ld]", (long)pixel));
 
459
            Text_ConcatAndDel (&string,
 
460
                               Text_FromFormat ("%ld]", (long)pixel));
 
461
            if (string == NULL)
 
462
            {
 
463
                return NULL;
 
464
            }
409
465
            y += array->ystep;
410
466
            posy += absystep;
411
467
        }
414
470
        while (posy < array->ylen)
415
471
        {
416
472
            /* Construct the rows */
417
 
            PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
 
473
            Text_ConcatAndDel (&string, Text_FromUTF8 ("\n  ["));
 
474
            if (string == NULL)
 
475
            {
 
476
                return NULL;
 
477
            }
418
478
            posx = 0;
419
479
            x = array->xstart;
420
480
            while (posx < (Uint32)xlen)
426
486
#else
427
487
                pixel = (px24[2]) + (px24[1] << 8) + (px24[0] << 16);
428
488
#endif
429
 
                PyString_ConcatAndDel (&string, PyString_FromFormat
430
 
                    ("%ld, ", (long)pixel));
 
489
                Text_ConcatAndDel (&string,
 
490
                                   Text_FromFormat ("%ld, ", (long)pixel));
 
491
                if (string == NULL)
 
492
                {
 
493
                    return NULL;
 
494
                }
431
495
                x += array->xstep;
432
496
                posx += absxstep;
433
497
            }
437
501
#else
438
502
            pixel = (px24[2]) + (px24[1] << 8) + (px24[0] << 16);
439
503
#endif
440
 
            PyString_ConcatAndDel (&string,
441
 
                PyString_FromFormat ("%ld]", (long)pixel));
 
504
            Text_ConcatAndDel (&string,
 
505
                               Text_FromFormat ("%ld]", (long)pixel));
 
506
            if (string == NULL)
 
507
            {
 
508
                return NULL;
 
509
            }
442
510
            y += array->ystep;
443
511
            posy += absystep;
444
512
        }
447
515
        while (posy < array->ylen)
448
516
        {
449
517
            /* Construct the rows */
450
 
            PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
 
518
            Text_ConcatAndDel (&string, Text_FromUTF8 ("\n  ["));
 
519
            if (string == NULL)
 
520
            {
 
521
                return NULL;
 
522
            }
451
523
            posx = 0;
452
524
            x = array->xstart;
453
525
            while (posx < (Uint32)xlen)
454
526
            {
455
527
                /* Construct the columns */
456
528
                pixel = *((Uint32 *) (pixels + y * array->padding) + x);
457
 
                PyString_ConcatAndDel (&string, PyString_FromFormat
458
 
                    ("%ld, ", (long)pixel));
 
529
                Text_ConcatAndDel (&string,
 
530
                                   Text_FromFormat ("%ld, ", (long)pixel));
 
531
                if (string == NULL)
 
532
                {
 
533
                    return NULL;
 
534
                }
459
535
                x += array->xstep;
460
536
                posx += absxstep;
461
537
            }
462
538
            pixel = *((Uint32 *) (pixels + y * array->padding) + x);
463
 
            PyString_ConcatAndDel (&string,
464
 
                PyString_FromFormat ("%ld]", (long)pixel));
 
539
            Text_ConcatAndDel (&string,
 
540
                               Text_FromFormat ("%ld]", (long)pixel));
 
541
            if (string == NULL)
 
542
            {
 
543
                return NULL;
 
544
            }
465
545
            y += array->ystep;
466
546
            posy += absystep;
467
547
        }
468
548
        break;
469
549
    }
470
 
    PyString_ConcatAndDel (&string, PyString_FromString ("\n)"));
 
550
    Text_ConcatAndDel (&string, Text_FromUTF8 ("\n)"));
471
551
    return string;
472
552
}
473
553
 
579
659
    return _array_slice_internal (array, _index, _index + 1, 1);
580
660
}
581
661
 
 
662
#if !PY3
582
663
/**
583
664
 * array[x:y]
584
665
 */
600
681
 
601
682
    return _array_slice_internal (array, low, high, 1);
602
683
}
 
684
#endif
603
685
 
604
686
static int
605
687
_array_assign_array (PyPixelArray *array, Py_ssize_t low, Py_ssize_t high,
2027
2109
            (Uint32) surface->pitch, NULL);
2028
2110
}
2029
2111
 
2030
 
PYGAME_EXPORT
2031
 
void initpixelarray (void)
 
2112
MODINIT_DEFINE (pixelarray)
2032
2113
{
2033
2114
    PyObject *module;
2034
2115
    PyObject *dict;
2035
2116
    PyObject *apiobj;
 
2117
    int ecode;
2036
2118
    static void* c_api[PYGAMEAPI_PIXELARRAY_NUMSLOTS];
2037
2119
 
 
2120
#if PY3
 
2121
    static struct PyModuleDef _module = {
 
2122
        PyModuleDef_HEAD_INIT,
 
2123
        "pixelarray",
 
2124
        NULL,
 
2125
        -1,
 
2126
        NULL,
 
2127
        NULL, NULL, NULL, NULL
 
2128
    };
 
2129
#endif
 
2130
 
 
2131
    /* imported needed apis; Do this first so if there is an error
 
2132
       the module is not loaded.
 
2133
    */
 
2134
    import_pygame_base ();
 
2135
    if (PyErr_Occurred ()) {
 
2136
        MODINIT_ERROR;
 
2137
    }
 
2138
    import_pygame_color();
 
2139
    if (PyErr_Occurred ()) {
 
2140
        MODINIT_ERROR;
 
2141
    }
 
2142
    import_pygame_surface ();
 
2143
    if (PyErr_Occurred ()) {
 
2144
        MODINIT_ERROR;
 
2145
    }
 
2146
 
 
2147
    /* type preparation */
2038
2148
    if (PyType_Ready (&PyPixelArray_Type) < 0)
2039
 
        return;
 
2149
    {
 
2150
        MODINIT_ERROR;
 
2151
    }
2040
2152
    
2041
2153
    /* create the module */
2042
 
    module = Py_InitModule3 ("pixelarray", NULL, NULL);
 
2154
#if PY3
 
2155
    module = PyModule_Create (&_module);
 
2156
#else
 
2157
    module = Py_InitModule3 (MODPREFIX "pixelarray", NULL, NULL);
 
2158
#endif
 
2159
    if (module == NULL)
 
2160
    {
 
2161
        MODINIT_ERROR;
 
2162
    }
2043
2163
    Py_INCREF (&PyPixelArray_Type);
2044
 
    PyModule_AddObject (module, "PixelArray", (PyObject *) &PyPixelArray_Type);
 
2164
    if (PyModule_AddObject (module, "PixelArray",
 
2165
                            (PyObject *) &PyPixelArray_Type) == -1)
 
2166
    {
 
2167
        Py_DECREF ((PyObject *) &PyPixelArray_Type);
 
2168
        DECREF_MOD (module);
 
2169
        MODINIT_ERROR;
 
2170
    }
2045
2171
    PyPixelArray_Type.tp_getattro = PyObject_GenericGetAttr;
2046
2172
    dict = PyModule_GetDict (module);
2047
2173
 
2048
2174
    c_api[0] = &PyPixelArray_Type;
2049
2175
    c_api[1] = PyPixelArray_New;
2050
2176
    apiobj = PyCObject_FromVoidPtr (c_api, NULL);
2051
 
    PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
 
2177
    if (apiobj == NULL)
 
2178
    {
 
2179
        DECREF_MOD (module);
 
2180
        MODINIT_ERROR;
 
2181
    }
 
2182
    ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
2052
2183
    Py_DECREF (apiobj);
2053
 
 
2054
 
    /*imported needed apis*/
2055
 
    import_pygame_base ();
2056
 
    import_pygame_color();
2057
 
    import_pygame_surface ();
 
2184
    if (ecode == -1)
 
2185
    {
 
2186
        DECREF_MOD (module);
 
2187
        MODINIT_ERROR;
 
2188
    }
 
2189
    MODINIT_RETURN (module);
2058
2190
}