annotate src/pixelarray_methods.c @ 3194:9fcb7abf3185 default tip

Remove some gcc 4.7.2 compiler warnings
author Lenard Lindstrom <len-l@telus.net>
date Tue, 23 Apr 2013 19:53:36 -0700
parents 5985bf3a6235
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
1 /*
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
2 pygame - Python Game Library
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
3 Copyright (C) 2007-2008 Marcus von Appen
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
4
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
6 modify it under the terms of the GNU Library General Public
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
7 License as published by the Free Software Foundation; either
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
8 version 2 of the License, or (at your option) any later version.
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
9
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
13 Library General Public License for more details.
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
14
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
15 You should have received a copy of the GNU Library General Public
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
16 License along with this library; if not, write to the Free
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
18
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
19 */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
20
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
21 /* Simple weighted euclidian distance, which tries to get near to the
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
22 * human eye reception using the weights.
993
78ab04b73064 Changed distance range to 0-1.
marcus
parents: 992
diff changeset
23 * It receives RGB values in the range 0-255 and returns a distance
78ab04b73064 Changed distance range to 0-1.
marcus
parents: 992
diff changeset
24 * value between 0.0 and 1.0.
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
25 */
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
26 #define COLOR_DIFF_RGB(wr,wg,wb,r1,g1,b1,r2,g2,b2) \
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
27 (sqrt (wr * (r1 - r2) * (r1 - r2) + \
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
28 wg * (g1 - g2) * (g1 - g2) + \
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
29 wb * (b1 - b2) * (b1 - b2)) / 255.0)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
30
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
31 #define WR_NTSC 0.299
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
32 #define WG_NTSC 0.587
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
33 #define WB_NTSC 0.114
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
34
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
35 /* Modified RGBAFromColorObj that only accepts pygame.Color or tuple objects.
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
36 */
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
37 static int
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
38 _RGBAFromColorObj (PyObject *obj, Uint8 rgba[4]) {
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
39 if (PyObject_IsInstance (obj, &PyColor_Type) || PyTuple_Check (obj)) {
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
40 return RGBAFromColorObj (obj, rgba);
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
41 }
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
42 PyErr_SetString(PyExc_ValueError, "invalid color argument");
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
43 return 0;
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
44 }
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
45
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
46 /**
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
47 * Tries to retrieve a valid color for a Surface.
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
48 */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
49 static int
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
50 _get_color_from_object(PyObject *val, SDL_PixelFormat *format, Uint32 *color)
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
51 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
52 Uint8 rgba[4];
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
53
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
54 if (!val) {
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
55 return 0;
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
56 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
57
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
58 if (PyInt_Check(val)) {
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
59 long intval = PyInt_AsLong (val);
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
60 if (intval == -1 && PyErr_Occurred()) {
1011
61a09a2965b0 _Really_ fixed 24bpp access for pygame.surfarray.pixels3d().
marcus
parents: 1007
diff changeset
61 PyErr_SetString (PyExc_ValueError, "invalid color argument");
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
62 return 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
63 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
64 *color = (Uint32) intval;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
65 return 1;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
66 }
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
67 else if (PyLong_Check(val)) {
1011
61a09a2965b0 _Really_ fixed 24bpp access for pygame.surfarray.pixels3d().
marcus
parents: 1007
diff changeset
68 unsigned long longval = PyLong_AsUnsignedLong (val);
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
69 if (PyErr_Occurred()) {
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
70 PyErr_SetString(PyExc_ValueError, "invalid color argument");
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
71 return 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
72 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
73 *color = (Uint32) longval;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
74 return 1;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
75 }
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
76 else if (_RGBAFromColorObj(val, rgba)) {
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
77 *color = (Uint32) SDL_MapRGBA(format, rgba[0], rgba[1], rgba[2], rgba[3]);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
78 return 1;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
79 }
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
80 else {
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
81 PyErr_SetString(PyExc_ValueError, "invalid color argument");
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
82 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
83 return 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
84 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
85
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
86
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
87 /**
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
88 * Retrieves a single pixel located at index from the surface pixel
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
89 * array.
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
90 */
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
91 static PyObject *
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
92 _get_single_pixel(PyPixelArray *array, Uint32 x, Uint32 y)
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
93 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
94 Uint8 *pixel_p = array->pixels + x * array->stride0 + y * array->stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
95 SDL_Surface *surf = PySurface_AsSurface(array->surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
96 int bpp;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
97 Uint32 pixel;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
98
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
99 bpp = surf->format->BytesPerPixel;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
100
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
101 switch (bpp) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
102
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
103 case 1:
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
104 pixel = (Uint32)*pixel_p;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
105 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
106 case 2:
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
107 pixel = (Uint32)*((Uint16 *)pixel_p);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
108 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
109 case 3:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
110 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
111 pixel = (pixel_p[0]) + (pixel_p[1] << 8) + (pixel_p[2] << 16);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
112 #else
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
113 pixel = (pixel_p[2]) + (pixel_p[1] << 8) + (pixel_p[0] << 16);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
114 #endif
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
115 break;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
116 case 4:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
117 pixel = *((Uint32 *)pixel_p);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
118 break;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
119 default: /* Should not get here. */
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
120 PyErr_Format(PyExc_SystemError,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
121 "Pygame internal error in _get_single_pixel: "
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
122 "unexpected pixel size %i", bpp);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
123 return 0;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
124 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
125
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
126 return PyInt_FromLong((long)pixel);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
127 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
128
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
129 /**
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
130 * Creates a new surface using the currently applied dimensions, step
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
131 * size, etc.
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
132 */
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
133 static PyObject *
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
134 _make_surface(PyPixelArray *array)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
135 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
136 SDL_Surface *surf = PySurface_AsSurface(array->surface);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
137 int bpp;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
138 Py_ssize_t dim0 = array->dim0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
139 Py_ssize_t dim1 = array->dim1 ? array->dim1 : 1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
140 Py_ssize_t stride0 = array->stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
141 Py_ssize_t stride1 = array->stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
142 PyObject *new_surface;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
143 SDL_Surface *temp_surf;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
144 SDL_Surface *new_surf;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
145 Py_ssize_t new_stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
146 Py_ssize_t new_stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
147 Uint8 *pixels = array->pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
148 Uint8 *new_pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
149 Py_ssize_t x;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
150 Py_ssize_t y;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
151 Uint8 *pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
152 Uint8 *pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
153 Uint8 *new_pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
154 Uint8 *new_pixel_p;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
155
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
156 bpp = surf->format->BytesPerPixel;
2202
30fac6cc07ae fix PixelArray bug involving slices and subarray size
Lenard Lindstrom <len-l@telus.net>
parents: 1505
diff changeset
157
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
158 /* Create the second surface. */
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
159
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
160 temp_surf = SDL_CreateRGBSurface(surf->flags,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
161 (int)dim0,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
162 (int)dim1,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
163 bpp,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
164 surf->format->Rmask,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
165 surf->format->Gmask,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
166 surf->format->Bmask,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
167 surf->format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
168 if (!temp_surf) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
169 return RAISE(PyExc_SDLError, SDL_GetError());
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
170 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
171
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
172 /* Guarantee an identical format. */
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
173 new_surf = SDL_ConvertSurface(temp_surf, surf->format, surf->flags);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
174 SDL_FreeSurface(temp_surf);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
175 if (!new_surf) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
176 return RAISE(PyExc_SDLError, SDL_GetError());
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
177 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
178
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
179 new_surface = PySurface_New(new_surf);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
180 if (!new_surface) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
181 SDL_FreeSurface(new_surf);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
182 return 0;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
183 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
184
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
185 /* Acquire a temporary lock. */
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
186 if (SDL_MUSTLOCK(new_surf) == 0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
187 SDL_LockSurface(new_surf);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
188 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
189
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
190 new_pixels = (Uint8 *)new_surf->pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
191 new_stride0 = new_surf->format->BytesPerPixel;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
192 new_stride1 = new_surf->pitch;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
193 pixelrow = pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
194 new_pixelrow = new_pixels;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
195
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
196 Py_BEGIN_ALLOW_THREADS;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
197 switch (bpp) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
198
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
199 case 1:
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
200 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
201 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
202 new_pixel_p = new_pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
203 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
204 *new_pixel_p = *pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
205 pixel_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
206 new_pixel_p += new_stride0;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
207 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
208 pixelrow += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
209 new_pixelrow += new_stride1;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
210 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
211 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
212 case 2:
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
213 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
214 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
215 new_pixel_p = new_pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
216 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
217 *((Uint16 *)new_pixel_p) = *((Uint16 *)pixel_p);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
218 pixel_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
219 new_pixel_p += new_stride0;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
220 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
221 pixelrow += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
222 new_pixelrow += new_stride1;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
223 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
224 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
225 case 3:
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
226 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
227 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
228 new_pixel_p = new_pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
229 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
230 new_pixel_p[0] = pixel_p[0];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
231 new_pixel_p[1] = pixel_p[1];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
232 new_pixel_p[2] = pixel_p[2];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
233 pixel_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
234 new_pixel_p += new_stride0;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
235 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
236 pixelrow += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
237 new_pixelrow += new_stride1;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
238 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
239 break;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
240 default: /* case: 4 */
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
241 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
242 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
243 new_pixel_p = new_pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
244 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
245 *((Uint32 *)new_pixel_p) = *((Uint32 *)pixel_p);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
246 pixel_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
247 new_pixel_p += new_stride0;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
248 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
249 pixelrow += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
250 new_pixelrow += new_stride1;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
251 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
252 }
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
253 Py_END_ALLOW_THREADS;
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
254
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
255 if (SDL_MUSTLOCK(new_surf) == 0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
256 SDL_UnlockSurface(new_surf);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
257 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
258 return new_surface;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
259 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
260
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
261 static int
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
262 _get_weights(PyObject *weights, float *wr, float *wg, float *wb)
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
263 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
264 int success = 1;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
265 float rgb[3] = { 0 };
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
266
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
267 if (!weights) {
2248
5985bf3a6235 Fix VC compiler errors and warings for pixelarray and pixelcopy (and remove some spaces on empty lines as well)
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
268 *wr = (float)WR_NTSC;
5985bf3a6235 Fix VC compiler errors and warings for pixelarray and pixelcopy (and remove some spaces on empty lines as well)
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
269 *wg = (float)WG_NTSC;
5985bf3a6235 Fix VC compiler errors and warings for pixelarray and pixelcopy (and remove some spaces on empty lines as well)
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
270 *wb = (float)WB_NTSC;
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
271 return 1;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
272 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
273
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
274 if (!PySequence_Check(weights)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
275 PyErr_SetString(PyExc_TypeError, "weights must be a sequence");
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
276 success = 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
277 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
278 else if (PySequence_Size(weights) < 3) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
279 PyErr_SetString(PyExc_TypeError,
2248
5985bf3a6235 Fix VC compiler errors and warings for pixelarray and pixelcopy (and remove some spaces on empty lines as well)
Lenard Lindstrom <len-l@telus.net>
parents: 2240
diff changeset
280 "weights must contain at least 3 values");
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
281 success = 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
282 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
283 else {
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
284 PyObject *item;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
285 int i;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
286
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
287 for (i = 0; i < 3; ++i) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
288 item = PySequence_GetItem(weights, i);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
289 if (PyNumber_Check(item)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
290 PyObject *num;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
291
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
292 if ((num = PyNumber_Float(item))) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
293 rgb[i] = (float)PyFloat_AsDouble(num);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
294 Py_DECREF(num);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
295 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
296 else if (PyErr_Clear(), (num = PyNumber_Int(item))) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
297 rgb[i] = (float)PyInt_AsLong (num);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
298 success = rgb[i] != -1 || !PyErr_Occurred();
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
299 Py_DECREF(num);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
300 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
301 else if (PyErr_Clear(), (num = PyNumber_Long(item))) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
302 rgb[i] = (float)PyLong_AsLong(num);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
303 success = (!PyErr_Occurred() ||
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
304 !PyErr_ExceptionMatches(PyExc_OverflowError));
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
305 Py_DECREF(num);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
306 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
307 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
308 PyErr_Clear();
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
309 PyErr_Format(PyExc_TypeError,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
310 "Unrecognized number type %s",
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
311 Py_TYPE(item)->tp_name);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
312 success = 0;
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
313 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
314 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
315 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
316 PyErr_SetString(PyExc_TypeError, "invalid weights");
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
317 success = 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
318 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
319 Py_XDECREF(item);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
320 if (!success) {
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
321 break;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
322 }
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
323 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
324 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
325
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
326 if (success)
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
327 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
328 float sum = 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
329
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
330 *wr = rgb[0];
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
331 *wg = rgb[1];
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
332 *wb = rgb[2];
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
333 if ((*wr < 0 || *wg < 0 || *wb < 0) ||
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
334 (*wr == 0 && *wg == 0 && *wb == 0))
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
335 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
336 PyErr_SetString(PyExc_ValueError,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
337 "weights must be positive and greater than 0");
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
338 return 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
339 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
340 /* Build the average weight values. */
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
341 sum = *wr + *wg + *wb;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
342 *wr = *wr / sum;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
343 *wg = *wg / sum;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
344 *wb = *wb / sum;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
345
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
346 return success;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
347 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
348 return 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
349 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
350
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
351 static PyObject *
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
352 _replace_color(PyPixelArray *array, PyObject *args, PyObject *kwds)
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
353 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
354 PyObject *weights = 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
355 PyObject *delcolor = 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
356 PyObject *replcolor = 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
357 SDL_Surface *surf = PySurface_AsSurface(array->surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
358 SDL_PixelFormat *format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
359 Py_ssize_t dim0 = array->dim0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
360 Py_ssize_t dim1 = array->dim1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
361 Py_ssize_t stride0 = array->stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
362 Py_ssize_t stride1 = array->stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
363 Uint8 *pixels = array->pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
364 int bpp;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
365 Uint32 dcolor;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
366 Uint32 rcolor;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
367 Uint8 r1, g1, b1, r2, g2, b2, a2;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
368 Uint8 *pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
369 Uint8 *pixel_p;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
370 float distance = 0;
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
371 float wr, wg, wb;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
372 Py_ssize_t x;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
373 Py_ssize_t y;
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
374 static char *keys[] = { "color", "repcolor", "distance", "weights", NULL };
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
375
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
376 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO|fO", keys, &delcolor,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
377 &replcolor, &distance, &weights)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
378 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
379 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
380
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
381 if (distance < 0 || distance > 1) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
382 return RAISE(PyExc_ValueError,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
383 "distance must be in the range from 0.0 to 1.0");
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
384 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
385
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
386 format = surf->format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
387 bpp = surf->format->BytesPerPixel;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
388
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
389 if (!_get_color_from_object(delcolor, format, &dcolor) ||
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
390 !_get_color_from_object(replcolor, format, &rcolor) ) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
391 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
392 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
393
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
394 if (!_get_weights(weights, &wr, &wg, &wb)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
395 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
396 }
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
397
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
398 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
399 SDL_GetRGB(dcolor, format, &r1, &g1, &b1);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
400 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
401
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
402 if (!dim1) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
403 dim1 = 1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
404 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
405 pixelrow = pixels;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
406
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
407 Py_BEGIN_ALLOW_THREADS;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
408 switch (bpp) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
409
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
410 case 1:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
411 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
412 Uint8 *px_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
413
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
414 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
415 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
416 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
417 px_p = pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
418 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
419 GET_PIXELVALS_1(r2, g2, b2, a2, px_p, format);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
420 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
421 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
422 *px_p = (Uint8)rcolor;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
423 }
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
424 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
425 else if (*px_p == dcolor) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
426 *px_p = (Uint8)rcolor;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
427 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
428 pixel_p += stride0;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
429 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
430 pixelrow += stride1;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
431 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
432 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
433 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
434 case 2:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
435 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
436 Uint16 *px_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
437 int ppa = (surf->flags & SDL_SRCALPHA && format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
438
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
439 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
440 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
441 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
442 px_p = (Uint16 *)pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
443 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
444 GET_PIXELVALS(r2, g2, b2, a2, (Uint32)*px_p, format, ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
445 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
446 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
447 *px_p = (Uint16)rcolor;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
448 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
449 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
450 else if (*px_p == dcolor) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
451 *px_p = (Uint16)rcolor;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
452 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
453 pixel_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
454 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
455 pixelrow += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
456 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
457 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
458 break;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
459 case 3:
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
460 {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
461 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
462 Uint32 Roffset = format->Rshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
463 Uint32 Goffset = format->Gshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
464 Uint32 Boffset = format->Bshift >> 3;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
465 #else
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
466 Uint32 Roffset = 2 - (format->Rshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
467 Uint32 Goffset = 2 - (format->Gshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
468 Uint32 Boffset = 2 - (format->Bshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
469 #endif
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
470 Uint32 pxcolor;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
471 int ppa = (surf->flags & SDL_SRCALPHA && format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
472
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
473 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
474 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
475 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
476 pxcolor = (((Uint32)pixel_p[Roffset] << 16) +
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
477 ((Uint32)pixel_p[Goffset] << 8) +
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
478 ((Uint32)pixel_p[Boffset]) );
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
479 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
480 GET_PIXELVALS(r2, g2, b2, a2, pxcolor, format, ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
481 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
482 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
483 pixel_p[Roffset] = (Uint8)(rcolor >> 16);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
484 pixel_p[Goffset] = (Uint8)(rcolor >> 8);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
485 pixel_p[Boffset] = (Uint8)rcolor;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
486 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
487 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
488 else if (pxcolor == dcolor) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
489 pixel_p[Roffset] = (Uint8)(rcolor >> 16);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
490 pixel_p[Goffset] = (Uint8)(rcolor >> 8);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
491 pixel_p[Boffset] = (Uint8)rcolor;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
492 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
493 pixel_p += stride0;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
494 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
495 pixelrow += stride1;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
496 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
497 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
498 break;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
499 default: /* case 4: */
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
500 {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
501 Uint32 *px_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
502 int ppa = (surf->flags & SDL_SRCALPHA && surf->format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
503
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
504 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
505 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
506 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
507 px_p = (Uint32 *)pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
508 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
509 GET_PIXELVALS(r2, g2, b2, a2, *px_p, format, ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
510 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
511 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
512 *px_p = rcolor;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
513 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
514 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
515 else if (*px_p == dcolor) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
516 *px_p = rcolor;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
517 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
518 pixel_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
519 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
520 pixelrow += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
521 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
522 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
523 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
524 }
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
525 Py_END_ALLOW_THREADS;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
526
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
527 Py_RETURN_NONE;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
528 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
529
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
530 static PyObject*
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
531 _extract_color (PyPixelArray *array, PyObject *args, PyObject *kwds)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
532 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
533 PyObject *weights = 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
534 PyObject *excolor = 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
535 int bpp;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
536 Uint32 black;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
537 Uint32 white;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
538 Uint32 color;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
539 Uint8 r1, g1, b1, r2, g2, b2, a2;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
540 Uint8 *pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
541 Uint8 *pixel_p;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
542 float distance = 0;
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
543 float wr, wg, wb;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
544 Py_ssize_t x;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
545 Py_ssize_t y;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
546 PyObject *surface;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
547 SDL_Surface *surf;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
548 SDL_PixelFormat *format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
549 PyPixelArray *new_array;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
550 Py_ssize_t dim0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
551 Py_ssize_t dim1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
552 Py_ssize_t stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
553 Py_ssize_t stride1;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
554 Uint8 *pixels;
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
555 static char *keys[] = { "color", "distance", "weights", NULL };
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
556
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
557 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|fO", keys, &excolor,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
558 &distance, &weights)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
559 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
560 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
561
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
562 if (distance < 0 || distance > 1) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
563 return RAISE(PyExc_ValueError,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
564 "distance must be in the range from 0.0 to 1.0");
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
565 }
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
566
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
567 if (!_get_weights(weights, &wr, &wg, &wb)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
568 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
569 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
570
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
571 /* Create the b/w mask surface. */
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
572 surface = _make_surface(array);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
573 if (!surface) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
574 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
575 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
576
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
577 new_array = (PyPixelArray *)PyPixelArray_New(surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
578 if (!new_array) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
579 Py_DECREF(surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
580 return 0;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
581 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
582
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
583 surf = PySurface_AsSurface(surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
584 format = surf->format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
585 bpp = surf->format->BytesPerPixel;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
586 dim0 = new_array->dim0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
587 dim1 = new_array->dim1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
588 stride0 = new_array->stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
589 stride1 = new_array->stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
590 pixels = new_array->pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
591
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
592 black = SDL_MapRGBA(format, 0, 0, 0, 255);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
593 white = SDL_MapRGBA(format, 255, 255, 255, 255);
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
594
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
595 if (!_get_color_from_object(excolor, format, &color)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
596 Py_DECREF(new_array);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
597 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
598 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
599
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
600 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
601 SDL_GetRGB(color, format, &r1, &g1, &b1);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
602 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
603
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
604 if (!dim1) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
605 dim1 = 1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
606 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
607 pixelrow = pixels;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
608
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
609 Py_BEGIN_ALLOW_THREADS;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
610 switch (bpp) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
611
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
612 case 1:
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
613 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
614 Uint8 *px_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
615
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
616 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
617 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
618 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
619 px_p = pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
620 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
621 GET_PIXELVALS_1(r2, g2, b2, a2, px_p, format);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
622 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
623 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
624 *px_p = (Uint8)white;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
625 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
626 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
627 *px_p = (Uint8)black;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
628 }
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
629 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
630 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
631 *px_p = (Uint8)(*px_p == color ? white : black);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
632 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
633 pixel_p += stride0;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
634 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
635 pixelrow += stride1;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
636 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
637 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
638 break;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
639 case 2:
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
640 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
641 Uint16 *px_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
642 int ppa = (surf->flags & SDL_SRCALPHA && format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
643
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
644 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
645 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
646 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
647 px_p = (Uint16 *)pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
648 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
649 GET_PIXELVALS(r2, g2, b2, a2, (Uint32)*px_p, format, ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
650 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
651 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
652 *px_p = (Uint16)white;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
653 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
654 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
655 *px_p = (Uint16)black;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
656 }
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
657 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
658 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
659 *px_p = (Uint16)(*px_p == color ? white : black);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
660 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
661 pixel_p += stride0;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
662 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
663 pixelrow += stride1;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
664 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
665 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
666 break;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
667 case 3:
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
668 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
669 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
670 Uint32 Roffset = format->Rshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
671 Uint32 Goffset = format->Gshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
672 Uint32 Boffset = format->Bshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
673 #else
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
674 Uint32 Roffset = 2 - (format->Rshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
675 Uint32 Goffset = 2 - (format->Gshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
676 Uint32 Boffset = 2 - (format->Bshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
677 #endif
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
678 Uint8 white_r = (Uint8)(white >> 16);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
679 Uint8 white_g = (Uint8)(white >> 8);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
680 Uint8 white_b = (Uint8)white;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
681 Uint8 black_r = (Uint8)(black >> 16);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
682 Uint8 black_g = (Uint8)(black >> 8);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
683 Uint8 black_b = (Uint8)black;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
684 Uint32 pxcolor;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
685 int ppa = (surf->flags & SDL_SRCALPHA && format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
686
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
687 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
688 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
689 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
690 pxcolor = (((Uint32)pixel_p[Roffset] << 16) +
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
691 ((Uint32)pixel_p[Goffset] << 8) +
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
692 ((Uint32)pixel_p[Boffset]) );
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
693 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
694 GET_PIXELVALS(r2, g2, b2, a2, pxcolor, format, ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
695 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
696 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
697 pixel_p[Roffset] = white_r;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
698 pixel_p[Goffset] = white_g;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
699 pixel_p[Boffset] = white_b;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
700 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
701 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
702 pixel_p[Roffset] = black_r;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
703 pixel_p[Goffset] = black_g;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
704 pixel_p[Boffset] = black_b;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
705 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
706 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
707 else if (pxcolor == color) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
708 pixel_p[Roffset] = white_r;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
709 pixel_p[Goffset] = white_g;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
710 pixel_p[Boffset] = white_b;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
711 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
712 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
713 pixel_p[Roffset] = black_r;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
714 pixel_p[Goffset] = black_g;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
715 pixel_p[Boffset] = black_b;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
716 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
717 pixel_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
718 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
719 pixelrow += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
720 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
721 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
722 break;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
723 default: /* case 4: */
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
724 {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
725 Uint32 *px_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
726 int ppa = (surf->flags & SDL_SRCALPHA && surf->format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
727
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
728 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
729 pixel_p = pixelrow;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
730 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
731 px_p = (Uint32 *)pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
732 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
733 GET_PIXELVALS(r2, g2, b2, a2, *px_p, format, ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
734 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
735 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
736 *px_p = white;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
737 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
738 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
739 *px_p = black;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
740 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
741 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
742 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
743 *px_p = *px_p == color ? white : black;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
744 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
745 pixel_p += stride0;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
746 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
747 pixelrow += stride1;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
748 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
749 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
750 break;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
751 }
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
752 Py_END_ALLOW_THREADS;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
753
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
754 return (PyObject *)new_array;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
755 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
756
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
757 static PyObject *
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
758 _compare(PyPixelArray *array, PyObject *args, PyObject *kwds)
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
759 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
760 Py_ssize_t dim0 = array->dim0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
761 Py_ssize_t dim1 = array->dim1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
762 SDL_Surface *surf = PySurface_AsSurface(array->surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
763 SDL_PixelFormat *format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
764 PyPixelArray *other_array;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
765 PyObject *weights = 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
766 SDL_Surface *other_surf;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
767 SDL_PixelFormat *other_format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
768 Py_ssize_t other_stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
769 Py_ssize_t other_stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
770 Uint8 *other_pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
771 int bpp;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
772 Uint32 black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
773 Uint32 white;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
774 Uint8 r1, g1, b1, a1, r2, g2, b2, a2;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
775 Uint8 *row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
776 Uint8 *byte_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
777 Uint8 *other_row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
778 Uint8 *other_byte_p;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
779 float distance = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
780 float wr, wg, wb;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
781 Py_ssize_t x;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
782 Py_ssize_t y;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
783 PyPixelArray *new_array;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
784 PyObject *new_surface;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
785 SDL_PixelFormat *new_format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
786 Py_ssize_t stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
787 Py_ssize_t stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
788 Uint8 *pixels;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
789
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
790 static char *keys[] = { "array", "distance", "weights", NULL };
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
791
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
792 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|fO", keys,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
793 &PyPixelArray_Type, &other_array,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
794 &distance, &weights)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
795 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
796 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
797
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
798
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
799 if (distance < 0.0 || distance > 1.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
800 return RAISE(PyExc_ValueError,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
801 "distance must be in the range from 0.0 to 1.0");
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
802 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
803
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
804 if (!_get_weights(weights, &wr, &wg, &wb)) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
805 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
806 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
807
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
808 if (other_array->dim0 != dim0 || other_array->dim1 != dim1) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
809 /* Bounds do not match. */
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
810 PyErr_SetString(PyExc_ValueError, "array sizes do not match");
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
811 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
812 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
813
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
814 format = surf->format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
815 bpp = surf->format->BytesPerPixel;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
816 other_surf = PySurface_AsSurface(other_array->surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
817 other_format = other_surf->format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
818
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
819 if (other_format->BytesPerPixel != bpp) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
820 /* bpp do not match. We cannot guarantee that the padding and co
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
821 * would be set correctly. */
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
822 PyErr_SetString(PyExc_ValueError, "bit depths do not match");
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
823 return 0;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
824 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
825
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
826 other_stride0 = other_array->stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
827 other_stride1 = other_array->stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
828 other_pixels = other_array->pixels;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
829
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
830 /* Create the b/w mask surface. */
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
831 new_surface = _make_surface(array);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
832 if (!new_surface) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
833 return 0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
834 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
835
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
836 new_array = (PyPixelArray *)PyPixelArray_New(new_surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
837 if (!new_array) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
838 Py_DECREF(new_surface);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
839 return 0;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
840 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
841
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
842 new_format = surf->format;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
843 stride0 = new_array->stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
844 stride1 = new_array->stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
845 pixels = new_array->pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
846
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
847 black = SDL_MapRGBA(format, 0, 0, 0, 255);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
848 white = SDL_MapRGBA(format, 255, 255, 255, 255);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
849
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
850 Py_BEGIN_ALLOW_THREADS;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
851 if (!dim1) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
852 dim1 = 1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
853 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
854 row_p = pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
855 other_row_p = other_pixels;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
856
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
857 switch (bpp) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
858
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
859 case 1:
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
860 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
861 Uint8 *pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
862 Uint8 *other_pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
863
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
864 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
865 byte_p = row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
866 other_byte_p = other_row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
867 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
868 pixel_p = byte_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
869 other_pixel_p = other_byte_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
870 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
871 GET_PIXELVALS_1(r1, g1, b1, a1, pixel_p, new_format);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
872 GET_PIXELVALS_1(r2, g2, b2, a2, other_pixel_p,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
873 other_format);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
874 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
875 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
876 *pixel_p = (Uint8)white;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
877 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
878 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
879 *pixel_p = (Uint8)black;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
880 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
881 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
882 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
883 *pixel_p = (Uint8)(*pixel_p == *other_pixel_p ?
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
884 white : black);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
885 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
886 byte_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
887 other_byte_p += other_stride0;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
888 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
889 row_p += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
890 other_row_p += other_stride1;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
891 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
892 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
893 break;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
894 case 2:
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
895 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
896 Uint16 *pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
897 Uint16 *other_pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
898 int ppa = (surf->flags & SDL_SRCALPHA && format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
899 int other_ppa = (other_surf->flags & SDL_SRCALPHA &&
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
900 other_format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
901
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
902 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
903 byte_p = row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
904 other_byte_p = other_row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
905 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
906 pixel_p = (Uint16 *)byte_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
907 other_pixel_p = (Uint16 *)other_byte_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
908 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
909 GET_PIXELVALS(r1, g1, b1, a1,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
910 (Uint32)*pixel_p, format, ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
911 GET_PIXELVALS(r2, g2, b2, a2,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
912 (Uint32)*pixel_p, other_format, other_ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
913 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
914 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
915 *pixel_p = (Uint16)white;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
916 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
917 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
918 *pixel_p = (Uint16)black;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
919 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
920 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
921 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
922 *pixel_p = (Uint16)(*pixel_p == *other_pixel_p ?
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
923 white : black);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
924 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
925 byte_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
926 other_byte_p += other_stride0;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
927 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
928 row_p += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
929 other_row_p += other_stride1;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
930 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
931 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
932 break;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
933 case 3:
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
934 {
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
935 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
936 Uint32 Roffset = format->Rshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
937 Uint32 Goffset = format->Gshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
938 Uint32 Boffset = format->Bshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
939 Uint32 oRoffset = other_format->Rshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
940 Uint32 oGoffset = other_format->Gshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
941 Uint32 oBoffset = other_format->Bshift >> 3;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
942 #else
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
943 Uint32 Roffset = 2 - (format->Rshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
944 Uint32 Goffset = 2 - (format->Gshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
945 Uint32 Boffset = 2 - (format->Bshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
946 Uint32 oRoffset = 2 - (other_format->Rshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
947 Uint32 oGoffset = 2 - (other_format->Gshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
948 Uint32 oBoffset = 2 - (other_format->Bshift >> 3);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
949 #endif
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
950 Uint8 white_r = (Uint8)(white >> 16);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
951 Uint8 white_g = (Uint8)(white >> 8);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
952 Uint8 white_b = (Uint8)white;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
953 Uint8 black_r = (Uint8)(black >> 16);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
954 Uint8 black_g = (Uint8)(black >> 8);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
955 Uint8 black_b = (Uint8)black;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
956
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
957 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
958 byte_p = row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
959 other_byte_p = other_row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
960 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
961 r1 = byte_p[Roffset];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
962 g1 = byte_p[Goffset];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
963 b1 = byte_p[Boffset];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
964 r2 = other_byte_p[oRoffset];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
965 g2 = other_byte_p[oGoffset];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
966 b2 = other_byte_p[oBoffset];
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
967 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
968 if (COLOR_DIFF_RGB(wr, wg, wb,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
969 r1, g1, b1,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
970 r2, g2, b2) <= distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
971 byte_p[Roffset] = white_r;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
972 byte_p[Goffset] = white_g;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
973 byte_p[Boffset] = white_b;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
974 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
975 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
976 byte_p[Roffset] = black_r;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
977 byte_p[Goffset] = black_g;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
978 byte_p[Boffset] = black_b;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
979 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
980 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
981 else if (r1 == r2 && g1 == g2 && b1 == b2) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
982 byte_p[Roffset] = white_r;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
983 byte_p[Goffset] = white_g;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
984 byte_p[Boffset] = white_b;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
985 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
986 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
987 byte_p[Roffset] = black_r;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
988 byte_p[Goffset] = black_g;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
989 byte_p[Boffset] = black_b;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
990 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
991 byte_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
992 other_byte_p += other_stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
993 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
994 row_p += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
995 other_row_p += other_stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
996 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
997 }
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
998 break;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
999 default: /* case 4: */
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1000 {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1001 Uint32 *pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1002 Uint32 *other_pixel_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1003 int ppa = (surf->flags & SDL_SRCALPHA && surf->format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1004 int other_ppa = (other_surf->flags & SDL_SRCALPHA &&
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1005 other_format->Amask);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1006
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1007 for (y = 0; y < dim1; ++y) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1008 byte_p = row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1009 other_byte_p = other_row_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1010 for (x = 0; x < dim0; ++x) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1011 pixel_p = (Uint32 *)byte_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1012 other_pixel_p = (Uint32 *)other_byte_p;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1013 if (distance != 0.0) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1014 GET_PIXELVALS(r1, g1, b1, a1, *pixel_p, format, ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1015 GET_PIXELVALS(r2, g2, b2, a2,
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1016 *pixel_p, other_format, other_ppa);
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1017 if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1018 distance) {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1019 *pixel_p = white;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1020 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1021 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1022 *pixel_p = black;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1023 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1024 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1025 else {
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1026 *pixel_p = *pixel_p == *other_pixel_p ? white : black;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1027 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1028 byte_p += stride0;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1029 other_byte_p += other_stride0;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1030 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1031 row_p += stride1;
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1032 other_row_p += other_stride1;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1033 }
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1034 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1035 break;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1036 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1037 Py_END_ALLOW_THREADS;
2218
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1038
7b81f1162a96 Add an array struct interface and the ndim, itemsize, shape, strides, and _pixels_address properties.
Lenard Lindstrom <len-l@telus.net>
parents: 2202
diff changeset
1039 return (PyObject *)new_array;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1040 }
2240
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1041
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1042 static PyObject *
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1043 _transpose(PyPixelArray *array)
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1044 {
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1045 SDL_Surface *surf = PySurface_AsSurface(array->surface);
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1046 Py_ssize_t dim0 = array->dim1 ? array->dim1 : 1;
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1047 Py_ssize_t dim1 = array->dim0;
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1048 Py_ssize_t stride0;
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1049 Py_ssize_t stride1 = array->stride0;
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1050
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1051 stride0 = array->dim1 ?
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1052 array->stride1 : array->dim0 * surf->format->BytesPerPixel;
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1053
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1054 return (PyObject *)_pxarray_new_internal(&PyPixelArray_Type,
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1055 0, array, array->pixels,
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1056 dim0, dim1, stride0, stride1);
be3228f63883 Make PixelArray more internally consistent
Lenard Lindstrom <len-l@telus.net>
parents: 2218
diff changeset
1057 }