-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
mask.c
2604 lines (2242 loc) · 76.7 KB
/
mask.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2002-2007 Ulf Ekstrom except for the bitcount function.
This wrapper code was originally written by Danny van Bruggen(?) for
the SCAM library, it was then converted by Ulf Ekstrom to wrap the
bitmask library, a spinoff from SCAM.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* a couple of print debugging helpers */
/*
#define CALLLOG2(x,y) fprintf(stderr, (x), (y));
#define CALLLOG(x) fprintf(stderr, (x));
*/
#define PYGAMEAPI_MASK_INTERNAL 1
#include "mask.h"
#include "pygame.h"
#include "pgcompat.h"
#include "doc/mask_doc.h"
#include "structmember.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* Macro to create mask objects. This will call the type's tp_new and tp_init.
* Params:
* w: width of mask
* h: height of mask
* f: fill, 1 is used to set all the bits (to 1) and 0 is used to clear
* all the bits (to 0)
*/
#define CREATE_MASK_OBJ(w, h, f) \
(pgMaskObject *)PyObject_CallFunction((PyObject *)&pgMask_Type, "(ii)i", \
(w), (h), (f))
/* Prototypes */
static PyTypeObject pgMask_Type;
static PG_INLINE pgMaskObject *
create_mask_using_bitmask(bitmask_t *bitmask);
static PG_INLINE pgMaskObject *
create_mask_using_bitmask_and_type(bitmask_t *bitmask, PyTypeObject *ob_type);
/********** mask helper functions **********/
/* Calculate the absolute difference between 2 Uint32s. */
static PG_INLINE Uint32
abs_diff_uint32(Uint32 a, Uint32 b)
{
return (a > b) ? a - b : b - a;
}
/********** mask object methods **********/
/* Copies the given mask. */
static PyObject *
mask_copy(PyObject *self, PyObject *args)
{
bitmask_t *new_bitmask = bitmask_copy(pgMask_AsBitmap(self));
if (NULL == new_bitmask) {
return RAISE(PyExc_MemoryError, "cannot allocate memory for bitmask");
}
return (PyObject *)create_mask_using_bitmask_and_type(new_bitmask,
self->ob_type);
}
/* Redirects mask.copy() to mask.__copy__(). This is done to allow
* subclasses that override the __copy__() method to also override the copy()
* method automatically. */
static PyObject *
mask_call_copy(PyObject *self, PyObject *args)
{
return PyObject_CallMethodObjArgs(self, Text_FromUTF8("__copy__"), args);
}
static PyObject *
mask_get_size(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
if (!PyArg_ParseTuple(args, ""))
return NULL;
return Py_BuildValue("(ii)", mask->w, mask->h);
}
/* Creates a Rect object based on the given mask's size. The rect's
* attributes can be altered via the kwargs.
*
* Returns:
* Rect object or NULL to indicate a fail
*
* Ref: src_c/surface.c surf_get_rect()
*/
static PyObject *
mask_get_rect(PyObject *self, PyObject *args, PyObject *kwargs)
{
PyObject *rect = NULL;
bitmask_t *bitmask = pgMask_AsBitmap(self);
if (0 != PyTuple_GET_SIZE(args)) {
return RAISE(PyExc_TypeError,
"get_rect only supports keyword arguments");
}
rect = pgRect_New4(0, 0, bitmask->w, bitmask->h);
if (NULL == rect) {
return RAISE(PyExc_MemoryError, "cannot allocate memory for rect");
}
if (NULL != kwargs) {
PyObject *key = NULL, *value = NULL;
Py_ssize_t pos = 0;
while (PyDict_Next(kwargs, &pos, &key, &value)) {
if ((-1 == PyObject_SetAttr(rect, key, value))) {
Py_DECREF(rect);
return NULL;
}
}
}
return rect;
}
static PyObject *
mask_get_at(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
int x, y, val;
if (!PyArg_ParseTuple(args, "(ii)", &x, &y))
return NULL;
if (x >= 0 && x < mask->w && y >= 0 && y < mask->h) {
val = bitmask_getbit(mask, x, y);
}
else {
PyErr_Format(PyExc_IndexError, "%d, %d is out of bounds", x, y);
return NULL;
}
return PyInt_FromLong(val);
}
static PyObject *
mask_set_at(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
int x, y, value = 1;
if (!PyArg_ParseTuple(args, "(ii)|i", &x, &y, &value))
return NULL;
if (x >= 0 && x < mask->w && y >= 0 && y < mask->h) {
if (value) {
bitmask_setbit(mask, x, y);
}
else {
bitmask_clearbit(mask, x, y);
}
}
else {
PyErr_Format(PyExc_IndexError, "%d, %d is out of bounds", x, y);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
mask_overlap(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
bitmask_t *othermask;
PyObject *maskobj;
int x, y, val;
int xp, yp;
if (!PyArg_ParseTuple(args, "O!(ii)", &pgMask_Type, &maskobj, &x, &y))
return NULL;
othermask = pgMask_AsBitmap(maskobj);
val = bitmask_overlap_pos(mask, othermask, x, y, &xp, &yp);
if (val) {
return Py_BuildValue("(ii)", xp, yp);
}
else {
Py_INCREF(Py_None);
return Py_None;
}
}
static PyObject *
mask_overlap_area(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
bitmask_t *othermask;
PyObject *maskobj;
int x, y, val;
if (!PyArg_ParseTuple(args, "O!(ii)", &pgMask_Type, &maskobj, &x, &y)) {
return NULL;
}
othermask = pgMask_AsBitmap(maskobj);
val = bitmask_overlap_area(mask, othermask, x, y);
return PyInt_FromLong(val);
}
static PyObject *
mask_overlap_mask(PyObject *self, PyObject *args)
{
int x, y;
bitmask_t *bitmask = pgMask_AsBitmap(self);
PyObject *maskobj = NULL;
pgMaskObject *output_maskobj = NULL;
if (!PyArg_ParseTuple(args, "O!(ii)", &pgMask_Type, &maskobj, &x, &y)) {
return NULL; /* Exception already set. */
}
output_maskobj = CREATE_MASK_OBJ(bitmask->w, bitmask->h, 0);
if (NULL == output_maskobj) {
return NULL; /* Exception already set. */
}
bitmask_overlap_mask(bitmask, pgMask_AsBitmap(maskobj),
output_maskobj->mask, x, y);
return (PyObject *)output_maskobj;
}
static PyObject *
mask_fill(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
bitmask_fill(mask);
Py_RETURN_NONE;
}
static PyObject *
mask_clear(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
bitmask_clear(mask);
Py_RETURN_NONE;
}
static PyObject *
mask_invert(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
bitmask_invert(mask);
Py_RETURN_NONE;
}
static PyObject *
mask_scale(PyObject *self, PyObject *args)
{
int x, y;
bitmask_t *bitmask = NULL;
if (!PyArg_ParseTuple(args, "(ii)", &x, &y)) {
return NULL; /* Exception already set. */
}
if (x < 0 || y < 0) {
return RAISE(PyExc_ValueError, "cannot scale mask to negative size");
}
bitmask = bitmask_scale(pgMask_AsBitmap(self), x, y);
if (NULL == bitmask) {
return RAISE(PyExc_MemoryError, "cannot allocate memory for bitmask");
}
return (PyObject *)create_mask_using_bitmask(bitmask);
}
static PyObject *
mask_draw(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
bitmask_t *othermask;
PyObject *maskobj;
int x, y;
if (!PyArg_ParseTuple(args, "O!(ii)", &pgMask_Type, &maskobj, &x, &y)) {
return NULL;
}
othermask = pgMask_AsBitmap(maskobj);
bitmask_draw(mask, othermask, x, y);
Py_RETURN_NONE;
}
static PyObject *
mask_erase(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
bitmask_t *othermask;
PyObject *maskobj;
int x, y;
if (!PyArg_ParseTuple(args, "O!(ii)", &pgMask_Type, &maskobj, &x, &y)) {
return NULL;
}
othermask = pgMask_AsBitmap(maskobj);
bitmask_erase(mask, othermask, x, y);
Py_RETURN_NONE;
}
static PyObject *
mask_count(PyObject *self, PyObject *args)
{
bitmask_t *m = pgMask_AsBitmap(self);
return PyInt_FromLong(bitmask_count(m));
}
static PyObject *
mask_centroid(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
int x, y;
long int m10, m01, m00;
PyObject *xobj, *yobj;
m10 = m01 = m00 = 0;
for (x = 0; x < mask->w; x++) {
for (y = 0; y < mask->h; y++) {
if (bitmask_getbit(mask, x, y)) {
m10 += x;
m01 += y;
m00++;
}
}
}
if (m00) {
xobj = PyInt_FromLong(m10 / m00);
yobj = PyInt_FromLong(m01 / m00);
}
else {
xobj = PyInt_FromLong(0);
yobj = PyInt_FromLong(0);
}
return Py_BuildValue("(NN)", xobj, yobj);
}
static PyObject *
mask_angle(PyObject *self, PyObject *args)
{
bitmask_t *mask = pgMask_AsBitmap(self);
int x, y;
long int m10, m01, m00, m20, m02, m11;
m10 = m01 = m00 = m20 = m02 = m11 = 0;
for (x = 0; x < mask->w; x++) {
for (y = 0; y < mask->h; y++) {
if (bitmask_getbit(mask, x, y)) {
m10 += x;
m20 += (long)x * x;
m11 += (long)x * y;
m02 += (long)y * y;
m01 += y;
m00++;
}
}
}
if (m00) {
int xc = m10 / m00;
int yc = m01 / m00;
double theta = -90.0 *
atan2(2 * (m11 / m00 - (long)xc * yc),
(m20 / m00 - (long)xc * xc) - (m02 / m00 - (long)yc * yc)) /
M_PI;
return PyFloat_FromDouble(theta);
}
else {
return PyFloat_FromDouble(0);
}
}
static PyObject *
mask_outline(PyObject *self, PyObject *args)
{
bitmask_t *c = pgMask_AsBitmap(self);
bitmask_t *m = NULL;
PyObject *plist = NULL;
PyObject *value = NULL;
int x, y, firstx, firsty, secx, secy, currx, curry, nextx, nexty, n;
int e, every = 1;
int a[] = {1, 1, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1, -1, -1};
int b[] = {0, 1, 1, 1, 0, -1, -1, -1, 0, 1, 1, 1, 0, -1};
n = firstx = firsty = secx = x = 0;
if (!PyArg_ParseTuple(args, "|i", &every)) {
return NULL;
}
plist = PyList_New(0);
if (!plist) {
return RAISE(PyExc_MemoryError,
"outline cannot allocate memory for list");
}
if (!c->w || !c->h) {
return plist;
}
/* Copying to a larger mask to avoid border checking. */
m = bitmask_create(c->w + 2, c->h + 2);
if (!m) {
Py_DECREF(plist);
return RAISE(PyExc_MemoryError,
"outline cannot allocate memory for mask");
}
bitmask_draw(m, c, 1, 1);
/* find the first set pixel in the mask */
for (y = 1; y < m->h - 1; y++) {
for (x = 1; x < m->w - 1; x++) {
if (bitmask_getbit(m, x, y)) {
firstx = x;
firsty = y;
value = Py_BuildValue("(ii)", x - 1, y - 1);
if (NULL == value) {
Py_DECREF(plist);
bitmask_free(m);
return NULL; /* Exception already set. */
}
if (0 != PyList_Append(plist, value)) {
Py_DECREF(value);
Py_DECREF(plist);
bitmask_free(m);
return NULL; /* Exception already set. */
}
Py_DECREF(value);
break;
}
}
if (bitmask_getbit(m, x, y))
break;
}
/* covers the mask having zero pixels set or only the final pixel */
if ((x == m->w - 1) && (y == m->h - 1)) {
bitmask_free(m);
return plist;
}
e = every;
/* check just the first pixel for neighbors */
for (n = 0; n < 8; n++) {
if (bitmask_getbit(m, x + a[n], y + b[n])) {
currx = secx = x + a[n];
curry = secy = y + b[n];
e--;
if (!e) {
e = every;
value = Py_BuildValue("(ii)", secx - 1, secy - 1);
if (NULL == value) {
Py_DECREF(plist);
bitmask_free(m);
return NULL; /* Exception already set. */
}
if (0 != PyList_Append(plist, value)) {
Py_DECREF(value);
Py_DECREF(plist);
bitmask_free(m);
return NULL; /* Exception already set. */
}
Py_DECREF(value);
}
break;
}
}
/* if there are no neighbors, return */
if (!secx) {
bitmask_free(m);
return plist;
}
/* the outline tracing loop */
for (;;) {
/* look around the pixel, it has to have a neighbor */
for (n = (n + 6) & 7;; n++) {
if (bitmask_getbit(m, currx + a[n], curry + b[n])) {
nextx = currx + a[n];
nexty = curry + b[n];
e--;
if (!e) {
e = every;
if ((curry == firsty && currx == firstx) &&
(secx == nextx && secy == nexty)) {
break;
}
value = Py_BuildValue("(ii)", nextx - 1, nexty - 1);
if (NULL == value) {
Py_DECREF(plist);
bitmask_free(m);
return NULL; /* Exception already set. */
}
if (0 != PyList_Append(plist, value)) {
Py_DECREF(value);
Py_DECREF(plist);
bitmask_free(m);
return NULL; /* Exception already set. */
}
Py_DECREF(value);
}
break;
}
}
/* if we are back at the first pixel, and the next one will be the
second one we visited, we are done */
if ((curry == firsty && currx == firstx) &&
(secx == nextx && secy == nexty)) {
break;
}
curry = nexty;
currx = nextx;
}
bitmask_free(m);
return plist;
}
static PyObject *
mask_convolve(PyObject *aobj, PyObject *args)
{
PyObject *bobj = NULL;
PyObject *oobj = Py_None;
bitmask_t *a = NULL, *b = NULL;
int xoffset = 0, yoffset = 0;
if (!PyArg_ParseTuple(args, "O!|O(ii)", &pgMask_Type, &bobj, &oobj,
&xoffset, &yoffset)) {
return NULL; /* Exception already set. */
}
a = pgMask_AsBitmap(aobj);
b = pgMask_AsBitmap(bobj);
if (oobj != Py_None) {
/* Use this mask for the output. */
Py_INCREF(oobj);
}
else {
pgMaskObject *maskobj = CREATE_MASK_OBJ(MAX(0, a->w + b->w - 1),
MAX(0, a->h + b->h - 1), 0);
if (NULL == maskobj) {
return NULL; /* Exception already set. */
}
oobj = (PyObject *)maskobj;
}
bitmask_convolve(a, b, pgMask_AsBitmap(oobj), xoffset, yoffset);
return oobj;
}
/* Gets the color of a given pixel.
*
* Params:
* pixel: pixel to get the color of
* bpp: bytes per pixel
*
* Returns:
* pixel color
*/
static PG_INLINE Uint32
get_pixel_color(Uint8 *pixel, Uint8 bpp)
{
switch (bpp) {
case 1:
return *((Uint8 *)pixel);
case 2:
return *((Uint16 *)pixel);
case 3:
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
return (pixel[0]) + (pixel[1] << 8) + (pixel[2] << 16);
#else /* SDL_BIG_ENDIAN */
return (pixel[2]) + (pixel[1] << 8) + (pixel[0] << 16);
#endif /* SDL_BIG_ENDIAN */
default: /* case 4: */
return *((Uint32 *)pixel);
}
}
/* Sets the color of a given pixel.
*
* Params:
* pixel: pixel to set the color of
* bpp: bytes per pixel
* color: color to set
*
* Ref: src_c/draw.c set_pixel_32()
*/
static void
set_pixel_color(Uint8 *pixel, Uint8 bpp, Uint32 color)
{
switch (bpp) {
case 1:
*pixel = color;
break;
case 2:
*(Uint16 *)pixel = color;
break;
case 3:
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
*(Uint16 *)pixel = color;
pixel[2] = color >> 16;
#else /* != SDL_LIL_ENDIAN */
pixel[2] = color;
pixel[1] = color >> 8;
pixel[0] = color >> 16;
#endif /* SDL_LIL_ENDIAN */
break;
default: /* case 4: */
*(Uint32 *)pixel = color;
break;
}
}
/* For each surface pixel's alpha that is greater than the threshold,
* the corresponding bitmask bit is set.
*
* Params:
* surf: surface
* bitmask: bitmask to alter
* threshold: threshold used check surface pixels (alpha) against
*
* Returns:
* void
*/
static void
set_from_threshold(SDL_Surface *surf, bitmask_t *bitmask, int threshold)
{
SDL_PixelFormat *format = surf->format;
Uint8 bpp = format->BytesPerPixel;
Uint8 *pixel = NULL;
Uint8 rgba[4];
int x, y;
for (y = 0; y < surf->h; ++y) {
pixel = (Uint8 *)surf->pixels + y * surf->pitch;
for (x = 0; x < surf->w; ++x, pixel += bpp) {
SDL_GetRGBA(get_pixel_color(pixel, bpp), format, rgba, rgba + 1,
rgba + 2, rgba + 3);
if (rgba[3] > threshold) {
bitmask_setbit(bitmask, x, y);
}
}
}
}
/* For each surface pixel's color that is not equal to the colorkey, the
* corresponding bitmask bit is set.
*
* Params:
* surf: surface
* bitmask: bitmask to alter
* colorkey: color used to check surface pixels against
*
* Returns:
* void
*/
static void
set_from_colorkey(SDL_Surface *surf, bitmask_t *bitmask, Uint32 colorkey)
{
Uint8 bpp = surf->format->BytesPerPixel;
Uint8 *pixel = NULL;
int x, y;
for (y = 0; y < surf->h; ++y) {
pixel = (Uint8 *)surf->pixels + y * surf->pitch;
for (x = 0; x < surf->w; ++x, pixel += bpp) {
if (get_pixel_color(pixel, bpp) != colorkey) {
bitmask_setbit(bitmask, x, y);
}
}
}
}
/* Creates a mask from a given surface.
*
* Returns:
* Mask object or NULL to indicate a fail
*/
static PyObject *
mask_from_surface(PyObject *self, PyObject *args)
{
SDL_Surface *surf = NULL;
pgSurfaceObject *surfobj = NULL;
pgMaskObject *maskobj = NULL;
Uint32 colorkey;
int threshold = 127; /* default value */
int use_thresh = 1;
if (!PyArg_ParseTuple(args, "O!|i", &pgSurface_Type, &surfobj,
&threshold)) {
return NULL; /* Exception already set. */
}
surf = pgSurface_AsSurface(surfobj);
if (surf->w < 0 || surf->h < 0) {
return RAISE(PyExc_ValueError,
"cannot create mask with negative size");
}
maskobj = CREATE_MASK_OBJ(surf->w, surf->h, 0);
if (NULL == maskobj) {
return NULL; /* Exception already set. */
}
if (surf->w == 0 || surf->h == 0) {
/* Nothing left to do for 0 sized surfaces. */
return (PyObject *)maskobj;
}
if (!pgSurface_Lock(surfobj)) {
Py_DECREF((PyObject *)maskobj);
return RAISE(PyExc_RuntimeError, "cannot lock surface");
}
Py_BEGIN_ALLOW_THREADS; /* Release the GIL. */
#if IS_SDLv1
if (surf->flags & SDL_SRCCOLORKEY) {
colorkey = surf->format->colorkey;
use_thresh = 0;
}
#else /* IS_SDLv2 */
use_thresh = (SDL_GetColorKey(surf, &colorkey) == -1);
#endif /* IS_SDLv2 */
if (use_thresh) {
set_from_threshold(surf, maskobj->mask, threshold);
}
else {
set_from_colorkey(surf, maskobj->mask, colorkey);
}
Py_END_ALLOW_THREADS; /* Obtain the GIL. */
if (!pgSurface_Unlock(surfobj)) {
Py_DECREF((PyObject *)maskobj);
return RAISE(PyExc_RuntimeError, "cannot unlock surface");
}
return (PyObject *)maskobj;
}
/*
palette_colors - this only affects surfaces with a palette
if true we look at the colors from the palette,
otherwise we threshold the pixel values. This is useful if
the surface is actually greyscale colors, and not palette colors.
*/
void
bitmask_threshold(bitmask_t *m, SDL_Surface *surf, SDL_Surface *surf2,
Uint32 color, Uint32 threshold, int palette_colors)
{
int x, y, rshift, gshift, bshift, rshift2, gshift2, bshift2;
int rloss, gloss, bloss, rloss2, gloss2, bloss2;
Uint8 *pixels, *pixels2;
SDL_PixelFormat *format, *format2;
Uint32 the_color, the_color2, rmask, gmask, bmask, rmask2, gmask2, bmask2;
Uint8 *pix;
Uint8 r, g, b, a;
Uint8 tr, tg, tb, ta;
int bpp1, bpp2;
format = surf->format;
rmask = format->Rmask;
gmask = format->Gmask;
bmask = format->Bmask;
rshift = format->Rshift;
gshift = format->Gshift;
bshift = format->Bshift;
rloss = format->Rloss;
gloss = format->Gloss;
bloss = format->Bloss;
bpp1 = surf->format->BytesPerPixel;
if (surf2) {
format2 = surf2->format;
rmask2 = format2->Rmask;
gmask2 = format2->Gmask;
bmask2 = format2->Bmask;
rshift2 = format2->Rshift;
gshift2 = format2->Gshift;
bshift2 = format2->Bshift;
rloss2 = format2->Rloss;
gloss2 = format2->Gloss;
bloss2 = format2->Bloss;
pixels2 = (Uint8 *)surf2->pixels;
bpp2 = surf->format->BytesPerPixel;
}
else { /* make gcc stop complaining */
rmask2 = gmask2 = bmask2 = 0;
rshift2 = gshift2 = bshift2 = 0;
rloss2 = gloss2 = bloss2 = 0;
format2 = NULL;
pixels2 = NULL;
bpp2 = 0;
}
SDL_GetRGBA(color, format, &r, &g, &b, &a);
SDL_GetRGBA(threshold, format, &tr, &tg, &tb, &ta);
for (y = 0; y < surf->h; y++) {
pixels = (Uint8 *)surf->pixels + y * surf->pitch;
if (surf2) {
pixels2 = (Uint8 *)surf2->pixels + y * surf2->pitch;
}
for (x = 0; x < surf->w; x++) {
/* the_color = surf->get_at(x,y) */
switch (bpp1) {
case 1:
the_color = (Uint32) * ((Uint8 *)pixels);
pixels++;
break;
case 2:
the_color = (Uint32) * ((Uint16 *)pixels);
pixels += 2;
break;
case 3:
pix = ((Uint8 *)pixels);
pixels += 3;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
the_color = (pix[0]) + (pix[1] << 8) + (pix[2] << 16);
#else
the_color = (pix[2]) + (pix[1] << 8) + (pix[0] << 16);
#endif
break;
default: /* case 4: */
the_color = *((Uint32 *)pixels);
pixels += 4;
break;
}
if (surf2) {
switch (bpp2) {
case 1:
the_color2 = (Uint32) * ((Uint8 *)pixels2);
pixels2++;
break;
case 2:
the_color2 = (Uint32) * ((Uint16 *)pixels2);
pixels2 += 2;
break;
case 3:
pix = ((Uint8 *)pixels2);
pixels2 += 3;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
the_color2 = (pix[0]) + (pix[1] << 8) + (pix[2] << 16);
#else
the_color2 = (pix[2]) + (pix[1] << 8) + (pix[0] << 16);
#endif
break;
default: /* case 4: */
the_color2 = *((Uint32 *)pixels2);
pixels2 += 4;
break;
}
/* TODO: will need to handle surfaces with palette colors.
*/
if ((bpp2 == 1) && (bpp1 == 1) && (!palette_colors)) {
/* Don't look at the color of the surface, just use the
value. This is useful for 8bit images that aren't
actually using the palette.
*/
if (abs_diff_uint32(the_color2, the_color) < tr) {
/* this pixel is within the threshold of othersurface.
*/
bitmask_setbit(m, x, y);
}
}
else if ((abs_diff_uint32(
(((the_color2 & rmask2) >> rshift2) << rloss2),
(((the_color & rmask) >> rshift) << rloss)) <
tr) &&
(abs_diff_uint32(
(((the_color2 & gmask2) >> gshift2) << gloss2),
(((the_color & gmask) >> gshift) << gloss)) <
tg) &&
(abs_diff_uint32(
(((the_color2 & bmask2) >> bshift2) << bloss2),
(((the_color & bmask) >> bshift) << bloss)) <
tb)) {
/* this pixel is within the threshold of othersurface. */
bitmask_setbit(m, x, y);
}
/* TODO: will need to handle surfaces with palette colors.
TODO: will need to handle the case where palette_colors == 0
*/
}
else if ((abs_diff_uint32(
(((the_color & rmask) >> rshift) << rloss), r) <
tr) &&
(abs_diff_uint32(
(((the_color & gmask) >> gshift) << gloss), g) <
tg) &&
(abs_diff_uint32(
(((the_color & bmask) >> bshift) << bloss), b) <
tb)) {
/* this pixel is within the threshold of the color. */
bitmask_setbit(m, x, y);
}
}
}
}
static PyObject *
mask_from_threshold(PyObject *self, PyObject *args)
{
pgSurfaceObject *surfobj = NULL;
pgSurfaceObject *surfobj2 = NULL;
pgMaskObject *maskobj = NULL;
SDL_Surface *surf = NULL, *surf2 = NULL;
PyObject *rgba_obj_color, *rgba_obj_threshold = NULL;
Uint8 rgba_color[4];
Uint8 rgba_threshold[4] = {0, 0, 0, 255};