annotate src/pixelarray_methods.c @ 1849:301bc61643a9 math_module

merged changes from trunk
author lorenz
date Mon, 16 Nov 2009 18:31:40 +0000
parents 902b98865c6c
children 30fac6cc07ae
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
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
35 /**
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
36 * 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
37 */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
38 static int
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
39 _get_color_from_object (PyObject *val, SDL_PixelFormat *format, Uint32 *color)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
40 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
41 Uint8 rgba[4];
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
42
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
43 if (!val)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
44 return 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
45
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
46 if (PyInt_Check (val))
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
47 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
48 long intval = PyInt_AsLong (val);
1011
61a09a2965b0 _Really_ fixed 24bpp access for pygame.surfarray.pixels3d().
marcus
parents: 1007
diff changeset
49 if (intval == -1 && PyErr_Occurred ())
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
50 {
1011
61a09a2965b0 _Really_ fixed 24bpp access for pygame.surfarray.pixels3d().
marcus
parents: 1007
diff changeset
51 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
52 return 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
53 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
54 *color = (Uint32) intval;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
55 return 1;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
56 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
57 else if (PyLong_Check (val))
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
58 {
1011
61a09a2965b0 _Really_ fixed 24bpp access for pygame.surfarray.pixels3d().
marcus
parents: 1007
diff changeset
59 unsigned long longval = PyLong_AsUnsignedLong (val);
61a09a2965b0 _Really_ fixed 24bpp access for pygame.surfarray.pixels3d().
marcus
parents: 1007
diff changeset
60 if (PyErr_Occurred ())
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
61 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
62 PyErr_SetString(PyExc_ValueError, "invalid color argument");
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
63 return 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
64 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
65 *color = (Uint32) longval;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
66 return 1;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
67 }
1037
c4c38b97db3b Added Color class.
marcus
parents: 1023
diff changeset
68 else if (RGBAFromColorObj (val, rgba))
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
69 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
70 *color = (Uint32) SDL_MapRGBA
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
71 (format, rgba[0], rgba[1], rgba[2], rgba[3]);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
72 return 1;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
73 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
74 else
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
75 PyErr_SetString (PyExc_ValueError, "invalid color argument");
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
76 return 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
77 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
78
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
79 /**
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
80 * 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
81 * array.
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
82 */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
83 static PyObject*
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
84 _get_single_pixel (Uint8 *pixels, int bpp, Uint32 _index, Uint32 row)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
85 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
86 Uint32 pixel;
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 switch (bpp)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
89 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
90 case 1:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
91 pixel = (Uint32)*((Uint8 *) pixels + row + _index);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
92 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
93 case 2:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
94 pixel = (Uint32)*((Uint16 *) (pixels + row) + _index);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
95 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
96 case 3:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
97 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
98 Uint8 *px = ((Uint8 *) (pixels + row) + _index * 3);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
99 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
100 pixel = (px[0]) + (px[1] << 8) + (px[2] << 16);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
101 #else
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
102 pixel = (px[2]) + (px[1] << 8) + (px[0] << 16);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
103 #endif
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
104 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
105 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
106 default: /* 4 bpp */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
107 pixel = *((Uint32 *) (pixels + row) + _index);
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 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
110
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
111 return PyInt_FromLong ((long)pixel);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
112 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
113
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
114 /**
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
115 * Sets a single pixel located at index from the surface pixel array.
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
116 */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
117 static void
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
118 _set_single_pixel (Uint8 *pixels, int bpp, Uint32 _index, Uint32 row,
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
119 SDL_PixelFormat *format, Uint32 color)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
120 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
121 switch (bpp)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
122 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
123 case 1:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
124 *((Uint8 *) pixels + row + _index) = (Uint8) color;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
125 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
126 case 2:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
127 *((Uint16 *) (pixels + row) + _index) = (Uint16) color;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
128 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
129 case 3:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
130 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
131 *((Uint8 *) (pixels + row) + _index * 3 + (format->Rshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
132 (Uint8) (color >> 16);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
133 *((Uint8 *) (pixels + row) + _index * 3 + (format->Gshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
134 (Uint8) (color >> 8);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
135 *((Uint8 *) (pixels + row) + _index * 3 + (format->Bshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
136 (Uint8) color;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
137 #else
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
138 *((Uint8 *) (pixels + row) + _index * 3 + 2 - (format->Rshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
139 (Uint8) (color >> 16);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
140 *((Uint8 *) (pixels + row) + _index * 3 + 2 - (format->Gshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
141 (Uint8) (color >> 8);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
142 *((Uint8 *) (pixels + row) + _index * 3 + 2 - (format->Bshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
143 (Uint8) color;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
144 #endif
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
145 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
146 default: /* 4 bpp */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
147 *((Uint32 *) (pixels + row) + _index) = color;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
148 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
149 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
150 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
151
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
152 /**
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
153 * 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
154 * size, etc.
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
155 */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
156 static PyObject*
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
157 _make_surface(PyPixelArray *array)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
158 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
159 PyObject *newsf;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
160 SDL_Surface *tmpsf;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
161 SDL_Surface *newsurf;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
162 Uint8 *pixels;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
163 Uint8 *origpixels;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
164
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
165 SDL_Surface *surface;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
166 int bpp;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
167 Uint32 x = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
168 Uint32 y = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
169 Uint32 vx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
170 Uint32 vy = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
171 Uint32 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
172 Uint32 posy = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
173 Uint32 absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
174 Uint32 absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
175
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
176 surface = PySurface_AsSurface (array->surface);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
177 bpp = surface->format->BytesPerPixel;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
178
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
179 /* Create the second surface. */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
180 tmpsf = SDL_CreateRGBSurface (surface->flags,
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
181 (int) (array->xlen / ABS (array->xstep)),
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
182 (int) (array->ylen / ABS (array->ystep)), bpp, surface->format->Rmask,
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
183 surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
184 if (!tmpsf)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
185 return RAISE (PyExc_SDLError, SDL_GetError ());
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
186
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
187 /* Guarantee an identical format. */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
188 newsurf = SDL_ConvertSurface (tmpsf, surface->format, surface->flags);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
189 if (!newsurf)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
190 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
191 SDL_FreeSurface (tmpsf);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
192 return RAISE (PyExc_SDLError, SDL_GetError ());
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
193 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
194 SDL_FreeSurface (tmpsf);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
195
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
196 newsf = PySurface_New (newsurf);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
197 if (!newsf)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
198 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
199 SDL_FreeSurface (newsurf);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
200 return NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
201 }
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
202
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
203 /* Acquire a temporary lock. */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
204 if (SDL_MUSTLOCK (newsurf) == 0)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
205 SDL_LockSurface (newsurf);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
206
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
207 pixels = (Uint8 *) newsurf->pixels;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
208 origpixels = (Uint8 *) surface->pixels;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
209
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
210 absxstep = ABS (array->xstep);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
211 absystep = ABS (array->ystep);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
212 y = array->ystart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
213
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
214 Py_BEGIN_ALLOW_THREADS;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
215 /* Single value assignment. */
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
216 switch (bpp)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
217 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
218 case 1:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
219 while (posy < array->ylen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
220 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
221 vx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
222 x = array->xstart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
223 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
224 while (posx < array->xlen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
225 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
226 *((Uint8 *) pixels + vy * newsurf->pitch + vx) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
227 (Uint8)*((Uint8 *) origpixels + y * array->padding + x);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
228 vx++;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
229 x += array->xstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
230 posx += absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
231 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
232 vy++;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
233 y += array->ystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
234 posy += absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
235 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
236 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
237 case 2:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
238 while (posy < array->ylen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
239 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
240 vx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
241 x = array->xstart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
242 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
243 while (posx < array->xlen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
244 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
245 *((Uint16 *) (pixels + vy * newsurf->pitch) + vx) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
246 (Uint16)*((Uint16 *) (origpixels + y * array->padding) + x);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
247 vx++;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
248 x += array->xstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
249 posx += absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
250 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
251 vy++;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
252 y += array->ystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
253 posy += absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
254 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
255 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
256 case 3:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
257 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
258 Uint8 *px;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
259 Uint8 *vpx;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
260 SDL_PixelFormat *format = newsurf->format;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
261 SDL_PixelFormat *vformat = surface->format;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
262
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
263 while (posy < array->ylen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
264 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
265 vx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
266 x = array->xstart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
267 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
268 while (posx < array->xlen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
269 {
1023
1a7e9d62146c Minor 24bpp changes for the PixelArray class. The should not change anything.
marcus
parents: 1011
diff changeset
270 px = ((Uint8 *) (pixels + vy * newsurf->pitch) + vx * 3);
1a7e9d62146c Minor 24bpp changes for the PixelArray class. The should not change anything.
marcus
parents: 1011
diff changeset
271 vpx = ((Uint8 *) (origpixels + y * array->padding) + x * 3);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
272
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
273 #if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
274 *(px + (format->Rshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
275 *(vpx + (vformat->Rshift >> 3));
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
276 *(px + (format->Gshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
277 *(vpx + (vformat->Gshift >> 3));
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
278 *(px + (format->Bshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
279 *(vpx + (vformat->Bshift >> 3));
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
280 #else
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
281 *(px + 2 - (format->Rshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
282 *(vpx + 2 - (vformat->Rshift >> 3));
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
283 *(px + 2 - (format->Gshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
284 *(vpx + 2 - (vformat->Gshift >> 3));
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
285 *(px + 2 - (format->Bshift >> 3)) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
286 *(vpx + 2 - (vformat->Bshift >> 3));
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
287 #endif
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
288 vx++;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
289 x += array->xstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
290 posx += absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
291 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
292 vy++;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
293 y += array->ystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
294 posy += absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
295 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
296 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
297 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
298 default:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
299 while (posy < array->ylen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
300 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
301 vx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
302 x = array->xstart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
303 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
304 while (posx < array->xlen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
305 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
306 *((Uint32 *) (pixels + vy * newsurf->pitch) + vx) =
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
307 (Uint32)*((Uint32 *) (origpixels + y * array->padding) + x);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
308 vx++;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
309 x += array->xstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
310 posx += absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
311 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
312 vy++;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
313 y += array->ystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
314 posy += absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
315 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
316 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
317 }
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
318 Py_END_ALLOW_THREADS;
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
319
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
320 if (SDL_MUSTLOCK (newsurf) == 0)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
321 SDL_UnlockSurface (newsurf);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
322 return newsf;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
323 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
324
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
325 static int
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
326 _get_weights (PyObject *weights, float *wr, float *wg, float *wb)
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 int success = 1;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
329 float rgb[3] = { 0 };
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
330
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
331 if (!weights)
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
332 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
333 *wr = WR_NTSC;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
334 *wg = WG_NTSC;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
335 *wb = WB_NTSC;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
336 return 1;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
337 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
338
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
339 if (!PySequence_Check (weights))
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
340 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
341 PyErr_SetString (PyExc_TypeError, "weights must be a sequence");
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
342 success = 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
343 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
344 else if (PySequence_Size (weights) < 3)
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 PyErr_SetString (PyExc_TypeError,
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
347 "weights must contain at least 3 values");
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
348 success = 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 else
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
351 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
352 PyObject *item;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
353 int i;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
354
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
355 for (i = 0; i < 3; i++)
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
356 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
357 item = PySequence_GetItem (weights, i);
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
358 if (PyNumber_Check (item))
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
359 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
360 PyObject *num = NULL;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
361 if ((num = PyNumber_Float (item)) != NULL)
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
362 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
363 rgb[i] = (float) PyFloat_AsDouble (num);
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
364 Py_DECREF (num);
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
365 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
366 else if ((num = PyNumber_Int (item)) != NULL)
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
367 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
368 rgb[i] = (float) PyInt_AsLong (num);
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
369 if (rgb[i] == -1 && PyErr_Occurred ())
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
370 success = 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
371 Py_DECREF (num);
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
372 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
373 else if ((num = PyNumber_Long (item)) != NULL)
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
374 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
375 rgb[i] = (float) PyLong_AsLong (num);
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
376 if (PyErr_Occurred () &&
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
377 PyErr_ExceptionMatches (PyExc_OverflowError))
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
378 success = 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
379 Py_DECREF (num);
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
380 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
381 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
382 else
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
383 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
384 PyErr_SetString (PyExc_TypeError, "invalid weights");
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
385 success = 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
386 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
387 Py_XDECREF (item);
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
388 if (!success)
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
389 break;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
390 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
391 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
392
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
393 if (success)
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
394 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
395 float sum = 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
396
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
397 *wr = rgb[0];
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
398 *wg = rgb[1];
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
399 *wb = rgb[2];
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
400 if ((*wr < 0 || *wg < 0 || *wb < 0) ||
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
401 (*wr == 0 && *wg == 0 && *wb == 0))
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
402 {
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
403 PyErr_SetString (PyExc_ValueError,
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
404 "weights must be positive and greater than 0");
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
405 return 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
406 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
407 /* Build the average weight values. */
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
408 sum = *wr + *wg + *wb;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
409 *wr = *wr / sum;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
410 *wg = *wg / sum;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
411 *wb = *wb / sum;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
412
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
413 return success;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
414 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
415 return 0;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
416 }
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
417
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
418 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
419 _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
420 {
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
421 PyObject *weights = NULL;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
422 PyObject *delcolor = NULL;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
423 PyObject *replcolor = NULL;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
424 Uint32 dcolor;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
425 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
426 Uint8 r1, g1, b1, r2, g2, b2, a2;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
427 SDL_Surface *surface;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
428 float distance = 0;
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
429 float wr, wg, wb;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
430
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
431 Uint32 x = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
432 Uint32 y = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
433 Uint32 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
434 Uint32 posy = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
435 Sint32 absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
436 Sint32 absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
437 Uint8 *pixels;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
438
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
439 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
440
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
441 if (!PyArg_ParseTupleAndKeywords (args, kwds, "OO|fO", keys, &delcolor,
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
442 &replcolor, &distance, &weights))
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
443 return NULL;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
444
993
78ab04b73064 Changed distance range to 0-1.
marcus
parents: 992
diff changeset
445 if (distance < 0 || distance > 1)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
446 return RAISE (PyExc_ValueError,
993
78ab04b73064 Changed distance range to 0-1.
marcus
parents: 992
diff changeset
447 "distance must be in the range from 0.0 to 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
448
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
449 surface = PySurface_AsSurface (array->surface);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
450 if (!_get_color_from_object (delcolor, surface->format, &dcolor) ||
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
451 !_get_color_from_object (replcolor, surface->format, &rcolor))
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
452 return NULL;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
453
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
454 if (!_get_weights (weights, &wr, &wg, &wb))
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
455 return NULL;
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
456
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
457 surface = PySurface_AsSurface (array->surface);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
458 pixels = surface->pixels;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
459 absxstep = ABS (array->xstep);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
460 absystep = ABS (array->ystep);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
461 y = array->ystart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
462
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
463 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
464 SDL_GetRGB (dcolor, surface->format, &r1, &g1, &b1);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
465
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
466 Py_BEGIN_ALLOW_THREADS;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
467 switch (surface->format->BytesPerPixel)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
468 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
469 case 1:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
470 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
471 Uint8 *pixel;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
472 while (posy < array->ylen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
473 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
474 x = array->xstart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
475 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
476 while (posx < array->xlen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
477 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
478 pixel = ((Uint8 *) pixels + y * surface->pitch + x);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
479 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
480 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
481 GET_PIXELVALS_1 (r2, g2, b2, a2, pixel, surface->format);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
482 if (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
483 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
484 *pixel = (Uint8) rcolor;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
485 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
486 else if (*pixel == dcolor)
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
487 *pixel = (Uint8) rcolor;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
488 x += array->xstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
489 posx += absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
490 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
491 y += array->ystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
492 posy += absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
493 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
494 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
495 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
496 case 2:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
497 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
498 Uint16 *pixel;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
499 int ppa = (surface->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
500 surface->format->Amask);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
501 while (posy < array->ylen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
502 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
503 x = array->xstart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
504 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
505 while (posx < array->xlen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
506 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
507 pixel = ((Uint16 *) (pixels + y * surface->pitch) + x);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
508 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
509 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
510 GET_PIXELVALS (r2, g2, b2, a2, (Uint32) *pixel,
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
511 surface->format, ppa);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
512 if (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
513 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
514 *pixel = (Uint16) rcolor;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
515 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
516 else if (*pixel == dcolor)
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
517 *pixel = (Uint16) rcolor;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
518 x += array->xstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
519 posx += absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
520 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
521 y += array->ystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
522 posy += absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
523 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
524 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
525 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
526 case 3:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
527 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
528 Uint8 *px;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
529 Uint32 pxcolor;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
530 SDL_PixelFormat *format = surface->format;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
531 int ppa = (surface->flags & SDL_SRCALPHA && format->Amask);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
532 while (posy < array->ylen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
533 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
534 x = array->xstart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
535 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
536 while (posx < array->xlen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
537 {
1023
1a7e9d62146c Minor 24bpp changes for the PixelArray class. The should not change anything.
marcus
parents: 1011
diff changeset
538 px = ((Uint8 *) (pixels + y * surface->pitch) + x * 3);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
539 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
540 pxcolor = (px[0]) + (px[1] << 8) + (px[2] << 16);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
541 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
542 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
543 GET_PIXELVALS (r2, g2, b2, a2, pxcolor, format, ppa);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
544 if (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
545 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
546 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
547 *(px + (format->Rshift >> 3)) = (Uint8) (rcolor >> 16);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
548 *(px + (format->Gshift >> 3)) = (Uint8) (rcolor >> 8);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
549 *(px + (format->Bshift >> 3)) = (Uint8) rcolor;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
550 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
551 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
552 else if (pxcolor == dcolor)
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
553 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
554 *(px + (format->Rshift >> 3)) = (Uint8) (rcolor >> 16);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
555 *(px + (format->Gshift >> 3)) = (Uint8) (rcolor >> 8);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
556 *(px + (format->Bshift >> 3)) = (Uint8) rcolor;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
557 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
558 #else
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
559 pxcolor = (px[2]) + (px[1] << 8) + (px[0] << 16);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
560 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
561 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
562 GET_PIXELVALS (r2, g2, b2, a2, pxcolor, format, ppa);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
563 if (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
564 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
565 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
566 *(px + 2 - (format->Rshift >> 3)) =
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
567 (Uint8) (rcolor >> 16);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
568 *(px + 2 - (format->Gshift >> 3)) =
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
569 (Uint8) (rcolor >> 8);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
570 *(px + 2 - (format->Bshift >> 3)) = (Uint8) rcolor;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
571 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
572 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
573 else if (pxcolor == dcolor)
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
574 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
575 *(px + 2 - (format->Rshift >> 3)) = (Uint8) (rcolor >> 16);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
576 *(px + 2 - (format->Gshift >> 3)) = (Uint8) (rcolor >> 8);
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
577 *(px + 2 - (format->Bshift >> 3)) = (Uint8) rcolor;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
578 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
579 #endif
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
580 x += array->xstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
581 posx += absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
582 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
583 y += array->ystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
584 posy += absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
585 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
586 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
587 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
588 default:
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
589 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
590 Uint32 *pixel;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
591 int ppa = (surface->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
592 surface->format->Amask);
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
593 while (posy < array->ylen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
594 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
595 x = array->xstart;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
596 posx = 0;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
597 while (posx < array->xlen)
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
598 {
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
599 pixel = ((Uint32 *) (pixels + y * surface->pitch) + x);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
600 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
601 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
602 GET_PIXELVALS (r2, g2, b2, a2, *pixel,
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
603 surface->format, ppa);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
604 if (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
605 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
606 *pixel = rcolor;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
607 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
608 else if (*pixel == dcolor)
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
609 *pixel = rcolor;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
610 x += array->xstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
611 posx += absxstep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
612 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
613 y += array->ystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
614 posy += absystep;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
615 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
616 break;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
617 }
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
618 }
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
619 Py_END_ALLOW_THREADS;
989
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
620 Py_RETURN_NONE;
1bbdc02af359 Added PixelArray.replace() method. Moved pixelarray methods into an own source file.
marcus
parents:
diff changeset
621 }
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
622
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
623 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
624 _extract_color (PyPixelArray *array, PyObject *args, PyObject *kwds)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
625 {
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
626 PyObject *weights = NULL;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
627 PyObject *sf = NULL;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
628 PyObject *excolor = NULL;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
629 PyPixelArray *newarray = NULL;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
630 Uint32 color;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
631 Uint32 white;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
632 Uint32 black;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
633 SDL_Surface *surface;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
634 float distance = 0;
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
635 float wr, wg, wb;
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
636 Uint8 r1, g1, b1, r2, g2, b2, a2;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
637
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
638 Uint32 x = 0;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
639 Uint32 y = 0;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
640 Uint32 posx = 0;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
641 Uint32 posy = 0;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
642 Sint32 absxstep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
643 Sint32 absystep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
644 Uint8 *pixels;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
645
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
646 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
647
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
648 if (!PyArg_ParseTupleAndKeywords (args, kwds, "O|fO", keys, &excolor,
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
649 &distance, &weights))
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
650 return NULL;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
651
993
78ab04b73064 Changed distance range to 0-1.
marcus
parents: 992
diff changeset
652 if (distance < 0 || distance > 1)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
653 return RAISE (PyExc_ValueError,
993
78ab04b73064 Changed distance range to 0-1.
marcus
parents: 992
diff changeset
654 "distance must be in the range from 0.0 to 1.0");
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
655 if (!_get_weights (weights, &wr, &wg, &wb))
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
656 return 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
657
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
658 surface = PySurface_AsSurface (array->surface);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
659 if (!_get_color_from_object (excolor, surface->format, &color))
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
660 return NULL;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
661
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
662 /* Create the b/w mask surface. */
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
663 sf = _make_surface (array);
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
664 if (!sf)
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
665 return NULL;
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
666 newarray = (PyPixelArray *) PyPixelArray_New (sf);
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
667 if (!newarray)
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
668 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
669 Py_DECREF (sf);
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
670 return NULL;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
671 }
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
672 surface = PySurface_AsSurface (newarray->surface);
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
673
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
674 black = SDL_MapRGBA (surface->format, 0, 0, 0, 255);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
675 white = SDL_MapRGBA (surface->format, 255, 255, 255, 255);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
676 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
677 SDL_GetRGB (color, surface->format, &r1, &g1, &b1);
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
678
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
679 pixels = surface->pixels;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
680 absxstep = ABS (newarray->xstep);
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
681 absystep = ABS (newarray->ystep);
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
682 y = newarray->ystart;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
683
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
684 Py_BEGIN_ALLOW_THREADS;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
685 switch (surface->format->BytesPerPixel)
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
686 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
687 case 1:
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
688 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
689 Uint8 *pixel;
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
690 while (posy < newarray->ylen)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
691 {
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
692 x = newarray->xstart;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
693 posx = 0;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
694 while (posx < newarray->xlen)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
695 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
696 pixel = ((Uint8 *) pixels + y * surface->pitch + x);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
697 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
698 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
699 GET_PIXELVALS_1 (r2, g2, b2, a2, pixel, surface->format);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
700 if (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
701 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
702 *pixel = (Uint8) white;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
703 else
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
704 *pixel = (Uint8) black;
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 else
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
707 *pixel = (*pixel == color) ? (Uint8) white : (Uint8) black;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
708 x += newarray->xstep;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
709 posx += absxstep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
710 }
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
711 y += newarray->ystep;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
712 posy += absystep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
713 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
714 break;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
715 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
716 case 2:
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
717 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
718 Uint16 *pixel;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
719 int ppa = (surface->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
720 surface->format->Amask);
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
721 while (posy < newarray->ylen)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
722 {
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
723 x = newarray->xstart;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
724 posx = 0;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
725 while (posx < newarray->xlen)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
726 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
727 pixel = ((Uint16 *) (pixels + y * surface->pitch) + x);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
728 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
729 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
730 GET_PIXELVALS (r2, g2, b2, a2, (Uint32) *pixel,
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
731 surface->format, ppa);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
732 if (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
733 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
734 *pixel = (Uint16) white;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
735 else
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
736 *pixel = (Uint16) black;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
737 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
738 else
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
739 *pixel = (*pixel == color) ? (Uint16) white :
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
740 (Uint16) black;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
741 x += newarray->xstep;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
742 posx += absxstep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
743 }
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
744 y += newarray->ystep;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
745 posy += absystep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
746 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
747 break;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
748 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
749 case 3:
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
750 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
751 Uint8 *px;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
752 Uint32 pxcolor;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
753 SDL_PixelFormat *format = surface->format;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
754 int ppa = (surface->flags & SDL_SRCALPHA && format->Amask);
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
755 while (posy < newarray->ylen)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
756 {
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
757 x = newarray->xstart;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
758 posx = 0;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
759 while (posx < newarray->xlen)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
760 {
1023
1a7e9d62146c Minor 24bpp changes for the PixelArray class. The should not change anything.
marcus
parents: 1011
diff changeset
761 px = ((Uint8 *) (pixels + y * surface->pitch) + x * 3);
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
762 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
763 pxcolor = (px[0]) + (px[1] << 8) + (px[2] << 16);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
764 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
765 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
766 GET_PIXELVALS (r2, g2, b2, a2, pxcolor, format, ppa);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
767 if (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
768 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
769 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
770 *(px + (format->Rshift >> 3)) = (Uint8) (white >> 16);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
771 *(px + (format->Gshift >> 3)) = (Uint8) (white >> 8);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
772 *(px + (format->Bshift >> 3)) = (Uint8) white;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
773 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
774 else
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
775 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
776 *(px + (format->Rshift >> 3)) = (Uint8) (black >> 16);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
777 *(px + (format->Gshift >> 3)) = (Uint8) (black >> 8);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
778 *(px + (format->Bshift >> 3)) = (Uint8) black;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
779 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
780 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
781 else if (pxcolor == color)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
782 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
783 *(px + (format->Rshift >> 3)) = (Uint8) (white >> 16);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
784 *(px + (format->Gshift >> 3)) = (Uint8) (white >> 8);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
785 *(px + (format->Bshift >> 3)) = (Uint8) white;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
786 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
787 else
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
788 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
789 *(px + (format->Rshift >> 3)) = (Uint8) (black >> 16);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
790 *(px + (format->Gshift >> 3)) = (Uint8) (black >> 8);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
791 *(px + (format->Bshift >> 3)) = (Uint8) black;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
792 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
793 #else
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
794 pxcolor = (px[2]) + (px[1] << 8) + (px[0] << 16);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
795 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
796 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
797 GET_PIXELVALS (r2, g2, b2, a2, pxcolor, format, ppa);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
798 if (COLOR_DIFF_RGB (wr, wg, wb, r1, g1, b1, r2, g2, b2) <=
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
799 distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
800 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
801 *(px + 2 - (format->Rshift >> 3)) =
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
802 (Uint8) (white >> 16);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
803 *(px + 2 - (format->Gshift >> 3)) =
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
804 (Uint8) (white >> 8);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
805 *(px + 2 - (format->Bshift >> 3)) = (Uint8) white;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
806 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
807 else
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
808 {
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
809 *(px + 2 - (format->Rshift >> 3)) =
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
810 (Uint8) (black >> 16);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
811 *(px + 2 - (format->Gshift >> 3)) =
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
812 (Uint8) (black >> 8);
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
813 *(px + 2 - (format->Bshift >> 3)) = (Uint8) black;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
814 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
815 }
996
123fae0cfc2d Unbreak build for big endian platforms.
marcus
parents: 993
diff changeset
816 else if (pxcolor == color)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
817 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
818 *(px + 2 - (format->Rshift >> 3)) = (Uint8) (white >> 16);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
819 *(px + 2 - (format->Gshift >> 3)) = (Uint8) (white >> 8);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
820 *(px + 2 - (format->Bshift >> 3)) = (Uint8) white;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
821 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
822 else
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
823 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
824 *(px + 2 - (format->Rshift >> 3)) = (Uint8) (black >> 16);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
825 *(px + 2 - (format->Gshift >> 3)) = (Uint8) (black >> 8);
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
826 *(px + 2 - (format->Bshift >> 3)) = (Uint8) black;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
827 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
828 #endif
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
829 x += newarray->xstep;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
830 posx += absxstep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
831 }
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
832 y += newarray->ystep;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
833 posy += absystep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
834 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
835 break;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
836 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
837 default:
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
838 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
839 Uint32 *pixel;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
840 int ppa = (surface->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
841 surface->format->Amask);
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
842 while (posy < newarray->ylen)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
843 {
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
844 x = newarray->xstart;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
845 posx = 0;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
846 while (posx < newarray->xlen)
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
847 {
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
848 pixel = ((Uint32 *) (pixels + y * surface->pitch) + x);
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
849 if (distance)
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
850 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
851 GET_PIXELVALS (r2, g2, b2, a2, *pixel,
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
852 surface->format, ppa);
997
46a786465642 Added weight support to PixelArray.extract() and PixelArray.replace().
marcus
parents: 996
diff changeset
853 if (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
854 distance)
992
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
855 *pixel = white;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
856 else
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
857 *pixel = black;
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
858 }
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
859 else
1577bcf02ed5 Added threshold recognition using weighted euclideance distances to PixelArray.replace() and PixelArray.extract(). Fixed slice assignment issue.
marcus
parents: 991
diff changeset
860 *pixel = (*pixel == color) ? white : black;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
861 x += newarray->xstep;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
862 posx += absxstep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
863 }
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
864 y += newarray->ystep;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
865 posy += absystep;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
866 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
867 break;
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
868 }
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
869 }
1001
7355da1a6df6 Release GIL where possible in PixelArray.
marcus
parents: 997
diff changeset
870 Py_END_ALLOW_THREADS;
991
684d96c73d0d Changed PixelArray.extract() to return a new PixelArray mask instead of performing an in place operation.
marcus
parents: 990
diff changeset
871 return (PyObject *) newarray;
990
404e3a0be4f8 Added PixelArray.extract() method.
marcus
parents: 989
diff changeset
872 }
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
873
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
874 static PyObject*
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
875 _compare (PyPixelArray *array, PyObject *args, PyObject *kwds)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
876 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
877 PyPixelArray *array2 = NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
878 PyPixelArray *newarray = NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
879 PyObject *weights = NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
880 PyObject *sf = NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
881 SDL_Surface *surface1 = NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
882 SDL_Surface *surface2 = NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
883 Uint32 black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
884 Uint32 white;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
885 float distance = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
886 Uint8 r1, g1, b1, a1, r2, g2, b2, a2;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
887 float wr, wg, wb;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
888
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
889 Uint32 x = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
890 Uint32 y = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
891 Uint32 vx = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
892 Uint32 vy = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
893 Uint32 posx = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
894 Uint32 posy = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
895 Sint32 absxstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
896 Sint32 absystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
897 Uint8 *pixels1;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
898 Uint8 *pixels2;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
899
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
900 static char *keys[] = { "array", "distance", "weights", NULL };
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
901
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
902 if (!PyArg_ParseTupleAndKeywords (args, kwds, "O|fO", keys, &newarray,
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
903 &distance, &weights))
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
904 return NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
905
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
906 if (!PyPixelArray_Check (newarray))
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
907 return RAISE (PyExc_TypeError, "invalid array type");
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
908 array2 = (PyPixelArray *) newarray;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
909 newarray = NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
910 if (distance < 0 || distance > 1)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
911 return RAISE (PyExc_ValueError,
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
912 "distance must be in the range from 0.0 to 1.0");
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
913 if (!_get_weights (weights, &wr, &wg, &wb))
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
914 return NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
915
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
916 if (array->ylen / ABS (array->ystep) != array2->ylen / ABS (array2->ystep)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
917 || array->xlen / ABS (array->xstep) != array2->xlen / ABS (array2->xstep))
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
918 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
919 /* Bounds do not match. */
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
920 PyErr_SetString (PyExc_ValueError, "array sizes do not match");
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
921 return NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
922 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
923
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
924 surface1 = PySurface_AsSurface (array->surface);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
925 surface2 = PySurface_AsSurface (array2->surface);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
926 if (surface2->format->BytesPerPixel != surface1->format->BytesPerPixel)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
927 return RAISE (PyExc_ValueError, "bit depths do not match");
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
928
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
929 /* Create the b/w mask surface. */
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
930 sf = _make_surface (array);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
931 if (!sf)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
932 return NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
933 newarray = (PyPixelArray *) PyPixelArray_New (sf);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
934 if (!newarray)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
935 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
936 Py_DECREF (sf);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
937 return NULL;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
938 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
939 surface1 = PySurface_AsSurface (newarray->surface);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
940
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
941 black = SDL_MapRGBA (surface1->format, 0, 0, 0, 255);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
942 white = SDL_MapRGBA (surface1->format, 255, 255, 255, 255);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
943
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
944 pixels1 = surface1->pixels;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
945 pixels2 = surface2->pixels;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
946 absxstep = ABS (array2->xstep);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
947 absystep = ABS (array2->ystep);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
948 y = array2->ystart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
949
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
950 Py_BEGIN_ALLOW_THREADS;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
951 switch (surface1->format->BytesPerPixel)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
952 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
953 case 1:
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
954 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
955 Uint8 *pixel1, *pixel2;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
956 while (posy < newarray->ylen)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
957 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
958 vx = array2->xstart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
959 x = newarray->xstart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
960 posx = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
961 while (posx < newarray->xlen)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
962 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
963 pixel1 = ((Uint8 *) pixels1 + y * surface1->pitch + x);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
964 pixel2 = ((Uint8 *) pixels2 + vy * surface2->pitch + vx);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
965 if (distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
966 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
967 GET_PIXELVALS_1 (r1, g1, b1, a1, pixel1, surface1->format);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
968 GET_PIXELVALS_1 (r2, g2, b2, a2, pixel2, surface2->format);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
969 if (COLOR_DIFF_RGB (wr, wg, wb, r1, g1, b1, r2, g2, b2) >
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
970 distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
971 *pixel1 = (Uint8) white;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
972 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
973 *pixel1 = (Uint8) black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
974 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
975 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
976 *pixel1 = (*pixel1 == *pixel2) ? (Uint8) white :
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
977 (Uint8) black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
978 vx += array2->xstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
979 x += newarray->xstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
980 posx += absxstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
981 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
982 vy += array2->ystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
983 y += newarray->ystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
984 posy += absystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
985 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
986 break;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
987 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
988 case 2:
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
989 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
990 Uint16 *pixel1, *pixel2;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
991 int ppa = (surface1->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
992 surface1->format->Amask);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
993 while (posy < newarray->ylen)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
994 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
995 vx = array2->xstart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
996 x = array->xstart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
997 posx = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
998 while (posx < newarray->xlen)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
999 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1000 pixel1 = ((Uint16 *) (pixels1 + y * surface1->pitch) + x);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1001 pixel2 = ((Uint16 *) (pixels2 + vy * surface2->pitch) + vx);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1002 if (distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1003 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1004 GET_PIXELVALS (r1, g1, b1, a1, (Uint32) *pixel1,
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1005 surface1->format, ppa);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1006 GET_PIXELVALS (r2, g2, b2, a2, (Uint32) *pixel2,
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1007 surface1->format, ppa);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1008 if (COLOR_DIFF_RGB (wr, wg, wb, r1, g1, b1, r2, g2, b2) >
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1009 distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1010 *pixel1 = (Uint16) white;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1011 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1012 *pixel1 = (Uint16) black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1013 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1014 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1015 *pixel1 = (*pixel1 == *pixel2) ? (Uint16) white :
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1016 (Uint16) black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1017 vx += array2->xstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1018 x += newarray->xstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1019 posx += absxstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1020 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1021 vy += array2->ystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1022 y += newarray->ystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1023 posy += absystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1024 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1025 break;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1026 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1027 case 3:
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1028 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1029 Uint8 *px1, *px2;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1030 Uint32 pxcolor1, pxcolor2;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1031 SDL_PixelFormat *format = surface1->format;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1032 int ppa1 = (surface1->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1033 surface1->format->Amask);
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1034 int ppa2 = (surface2->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1035 surface2->format->Amask);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1036 while (posy < newarray->ylen)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1037 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1038 vx = array2->xstart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1039 x = newarray->xstart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1040 posx = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1041 while (posx < newarray->xlen)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1042 {
1023
1a7e9d62146c Minor 24bpp changes for the PixelArray class. The should not change anything.
marcus
parents: 1011
diff changeset
1043 px1 = ((Uint8 *) (pixels1 + y * surface1->pitch) + x * 3);
1a7e9d62146c Minor 24bpp changes for the PixelArray class. The should not change anything.
marcus
parents: 1011
diff changeset
1044 px2 = ((Uint8 *) (pixels2 + vy * surface2->pitch) + vx * 3);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1045 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1046 pxcolor1 = (px1[0]) + (px1[1] << 8) + (px1[2] << 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1047 pxcolor2 = (px2[0]) + (px2[1] << 8) + (px2[2] << 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1048 if (distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1049 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1050 GET_PIXELVALS (r1, g1, b1, a1, pxcolor1,
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1051 surface1->format, ppa1);
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1052 GET_PIXELVALS (r2, g2, b2, a2, pxcolor2,
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1053 surface2->format, ppa2);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1054 if (COLOR_DIFF_RGB (wr, wg, wb, r1, g1, b1, r2, g2, b2) >
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1055 distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1056 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1057 *(px1 + (format->Rshift >> 3)) = (Uint8) (white >> 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1058 *(px1 + (format->Gshift >> 3)) = (Uint8) (white >> 8);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1059 *(px1 + (format->Bshift >> 3)) = (Uint8) white;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1060 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1061 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1062 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1063 *(px1 + (format->Rshift >> 3)) = (Uint8) (black >> 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1064 *(px1 + (format->Gshift >> 3)) = (Uint8) (black >> 8);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1065 *(px1 + (format->Bshift >> 3)) = (Uint8) black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1066 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1067 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1068 else if (pxcolor1 != pxcolor2)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1069 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1070 *(px1 + (format->Rshift >> 3)) = (Uint8) (white >> 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1071 *(px1 + (format->Gshift >> 3)) = (Uint8) (white >> 8);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1072 *(px1 + (format->Bshift >> 3)) = (Uint8) white;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1073 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1074 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1075 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1076 *(px1 + (format->Rshift >> 3)) = (Uint8) (black >> 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1077 *(px1 + (format->Gshift >> 3)) = (Uint8) (black >> 8);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1078 *(px1 + (format->Bshift >> 3)) = (Uint8) black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1079 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1080 #else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1081 pxcolor1 = (px1[2]) + (px1[1] << 8) + (px1[0] << 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1082 pxcolor2 = (px2[2]) + (px2[1] << 8) + (px2[0] << 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1083 if (distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1084 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1085 GET_PIXELVALS (r1, g1, b1, a1, pxcolor1,
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1086 surface1->format, ppa1);
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1087 GET_PIXELVALS (r2, g2, b2, a2, pxcolor2,
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1088 surface2->format, ppa2);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1089 if (COLOR_DIFF_RGB (wr, wg, wb, r1, g1, b1, r2, g2, b2) >
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1090 distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1091 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1092 *(px1 + 2 - (format->Rshift >> 3)) =
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1093 (Uint8) (white >> 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1094 *(px1 + 2 - (format->Gshift >> 3)) =
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1095 (Uint8) (white >> 8);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1096 *(px1 + 2 - (format->Bshift >> 3)) = (Uint8) white;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1097 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1098 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1099 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1100 *(px1 + 2 - (format->Rshift >> 3)) =
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1101 (Uint8) (black >> 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1102 *(px1 + 2 - (format->Gshift >> 3)) =
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1103 (Uint8) (black >> 8);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1104 *(px1 + 2 - (format->Bshift >> 3)) = (Uint8) black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1105 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1106 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1107 else if (pxcolor1 != pxcolor2)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1108 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1109 *(px1 + 2 - (format->Rshift >> 3)) = (Uint8) (white >> 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1110 *(px1 + 2 - (format->Gshift >> 3)) = (Uint8) (white >> 8);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1111 *(px1 + 2 - (format->Bshift >> 3)) = (Uint8) white;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1112 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1113 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1114 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1115 *(px1 + 2 - (format->Rshift >> 3)) = (Uint8) (black >> 16);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1116 *(px1 + 2 - (format->Gshift >> 3)) = (Uint8) (black >> 8);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1117 *(px1 + 2 - (format->Bshift >> 3)) = (Uint8) black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1118 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1119 #endif
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1120 vx += array2->xstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1121 x += newarray->xstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1122 posx += absxstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1123 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1124 vy += array2->ystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1125 y += newarray->ystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1126 posy += absystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1127 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1128 break;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1129 }
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1130 default:
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1131 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1132 Uint32 *pixel1, *pixel2;
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1133 int ppa1 = (surface1->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1134 surface1->format->Amask);
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1135 int ppa2 = (surface2->flags & SDL_SRCALPHA &&
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1136 surface2->format->Amask);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1137 while (posy < newarray->ylen)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1138 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1139 vx = array2->xstart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1140 x = newarray->xstart;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1141 posx = 0;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1142 while (posx < newarray->xlen)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1143 {
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1144 pixel1 = ((Uint32 *) (pixels1 + y * surface1->pitch) + x);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1145 pixel2 = ((Uint32 *) (pixels2 + vy * surface2->pitch) + vx);
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1146 if (distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1147 {
1505
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1148 GET_PIXELVALS (r1, g1, b1, a1, *pixel1,
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1149 surface1->format, ppa1);
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1150 GET_PIXELVALS (r2, g2, b2, a2, *pixel2,
902b98865c6c fix per-pixel alpha bug involving pixel unpacking
Lenard Lindstrom <len-l@telus.net>
parents: 1037
diff changeset
1151 surface2->format, ppa2);
1007
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1152 if (COLOR_DIFF_RGB (wr, wg, wb, r1, g1, b1, r2, g2, b2) >
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1153 distance)
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1154 *pixel1 = white;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1155 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1156 *pixel1 = black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1157 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1158 else
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1159 *pixel1 = (*pixel1 == *pixel2) ? white : black;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1160 vx += array2->xstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1161 x += newarray->xstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1162 posx += absxstep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1163 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1164 vy += array2->ystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1165 y += newarray->ystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1166 posy += absystep;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1167 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1168 break;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1169 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1170 }
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1171 Py_END_ALLOW_THREADS;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1172 return (PyObject *) newarray;
9e42170b0f2e Added PixelArray.compare().
marcus
parents: 1001
diff changeset
1173 }