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);
66
68
static PyObject* _pxarray_slice (PyPixelArray *array, Py_ssize_t low,
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
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)
98
101
#define SURFACE_EQUALS(x,y) \
99
102
(((PyPixelArray *)x)->surface == ((PyPixelArray *)y)->surface)
106
109
static PyMethodDef _pxarray_methods[] =
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 }
124
Text_ConcatAndDel (PyObject **string, PyObject *newpart)
126
PyObject *result = NULL;
127
if (*string != NULL && newpart != NULL) {
128
PyUnicode_Concat (*string, newpart);
133
Py_XDECREF (*string);
134
Py_XDECREF (newpart);
139
#define Text_ConcatAndDel PyString_ConcatAndDel
120
143
* Getters and setters for the PyPixelArray.
138
161
NULL, /*sq_concat*/
139
162
NULL, /*sq_repeat*/
140
163
(ssizeargfunc) _pxarray_item, /*sq_item*/
141
167
(ssizessizeargfunc) _pxarray_slice, /*sq_slice*/
142
169
(ssizeobjargproc) _pxarray_ass_item, /*sq_ass_item*/
143
173
(ssizessizeobjargproc) _pxarray_ass_slice, /*sq_ass_slice*/
144
175
(objobjproc) _pxarray_contains, /*sq_contains*/
145
176
NULL, /*sq_inplace_concat*/
146
177
NULL, /*sq_inplace_repeat*/
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);
296
328
/**** Getter and setter access ****/
353
385
array->xstart, array->xlen, array->xstep, array->ystart,
354
386
array->ylen, array->ystep, array->padding);
356
string = PyString_FromString ("PixelArray(");
388
string = Text_FromUTF8 ("PixelArray(");
358
390
absxstep = ABS (array->xstep);
359
391
absystep = ABS (array->ystep);
367
399
while (posy < array->ylen)
369
401
/* Construct the rows */
370
PyString_ConcatAndDel (&string, PyString_FromString ("\n ["));
402
Text_ConcatAndDel (&string, Text_FromUTF8 ("\n ["));
372
408
x = array->xstart;
373
409
while (posx < (Uint32)xlen)
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));
379
419
x += array->xstep;
380
420
posx += absxstep;
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));
385
429
y += array->ystep;
386
430
posy += absystep;
390
434
while (posy < array->ylen)
392
436
/* Construct the rows */
393
PyString_ConcatAndDel (&string, PyString_FromString ("\n ["));
437
Text_ConcatAndDel (&string, Text_FromUTF8 ("\n ["));
395
443
x = array->xstart;
396
444
while (posx < (Uint32)xlen)
398
446
/* Construct the columns */
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));
403
455
x += array->xstep;
404
456
posx += absxstep;
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));
409
465
y += array->ystep;
410
466
posy += absystep;
414
470
while (posy < array->ylen)
416
472
/* Construct the rows */
417
PyString_ConcatAndDel (&string, PyString_FromString ("\n ["));
473
Text_ConcatAndDel (&string, Text_FromUTF8 ("\n ["));
419
479
x = array->xstart;
420
480
while (posx < (Uint32)xlen)
427
487
pixel = (px24[2]) + (px24[1] << 8) + (px24[0] << 16);
429
PyString_ConcatAndDel (&string, PyString_FromFormat
430
("%ld, ", (long)pixel));
489
Text_ConcatAndDel (&string,
490
Text_FromFormat ("%ld, ", (long)pixel));
431
495
x += array->xstep;
432
496
posx += absxstep;
438
502
pixel = (px24[2]) + (px24[1] << 8) + (px24[0] << 16);
440
PyString_ConcatAndDel (&string,
441
PyString_FromFormat ("%ld]", (long)pixel));
504
Text_ConcatAndDel (&string,
505
Text_FromFormat ("%ld]", (long)pixel));
442
510
y += array->ystep;
443
511
posy += absystep;
447
515
while (posy < array->ylen)
449
517
/* Construct the rows */
450
PyString_ConcatAndDel (&string, PyString_FromString ("\n ["));
518
Text_ConcatAndDel (&string, Text_FromUTF8 ("\n ["));
452
524
x = array->xstart;
453
525
while (posx < (Uint32)xlen)
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));
459
535
x += array->xstep;
460
536
posx += absxstep;
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));
465
545
y += array->ystep;
466
546
posy += absystep;
470
PyString_ConcatAndDel (&string, PyString_FromString ("\n)"));
550
Text_ConcatAndDel (&string, Text_FromUTF8 ("\n)"));
601
682
return _array_slice_internal (array, low, high, 1);
605
687
_array_assign_array (PyPixelArray *array, Py_ssize_t low, Py_ssize_t high,
2027
2109
(Uint32) surface->pitch, NULL);
2031
void initpixelarray (void)
2112
MODINIT_DEFINE (pixelarray)
2033
2114
PyObject *module;
2034
2115
PyObject *dict;
2035
2116
PyObject *apiobj;
2036
2118
static void* c_api[PYGAMEAPI_PIXELARRAY_NUMSLOTS];
2121
static struct PyModuleDef _module = {
2122
PyModuleDef_HEAD_INIT,
2127
NULL, NULL, NULL, NULL
2131
/* imported needed apis; Do this first so if there is an error
2132
the module is not loaded.
2134
import_pygame_base ();
2135
if (PyErr_Occurred ()) {
2138
import_pygame_color();
2139
if (PyErr_Occurred ()) {
2142
import_pygame_surface ();
2143
if (PyErr_Occurred ()) {
2147
/* type preparation */
2038
2148
if (PyType_Ready (&PyPixelArray_Type) < 0)
2041
2153
/* create the module */
2042
module = Py_InitModule3 ("pixelarray", NULL, NULL);
2155
module = PyModule_Create (&_module);
2157
module = Py_InitModule3 (MODPREFIX "pixelarray", NULL, NULL);
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)
2167
Py_DECREF ((PyObject *) &PyPixelArray_Type);
2168
DECREF_MOD (module);
2045
2171
PyPixelArray_Type.tp_getattro = PyObject_GenericGetAttr;
2046
2172
dict = PyModule_GetDict (module);
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);
2179
DECREF_MOD (module);
2182
ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
2052
2183
Py_DECREF (apiobj);
2054
/*imported needed apis*/
2055
import_pygame_base ();
2056
import_pygame_color();
2057
import_pygame_surface ();
2186
DECREF_MOD (module);
2189
MODINIT_RETURN (module);