-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
surface.c
3886 lines (3475 loc) · 116 KB
/
surface.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
/*
pygame - Python Game Library
Copyright (C) 2000-2001 Pete Shinners
Copyright (C) 2007 Marcus von Appen
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
Pete Shinners
pete@shinners.org
*/
#define PYGAMEAPI_SURFACE_INTERNAL
#include "surface.h"
#include "palette.h"
#include "structmember.h"
#include "pgcompat.h"
#include "doc/surface_doc.h"
#include "pgbufferproxy.h"
/* stdint.h is missing from some versions of MSVC. */
#ifdef _MSC_VER
#ifndef UINT32_MAX
#define UINT32_MAX 0xFFFFFFFF
#endif
#else
#include <stdint.h>
#endif /* _MSC_VER */
typedef enum {
VIEWKIND_0D = 0,
VIEWKIND_1D = 1,
VIEWKIND_2D = 2,
VIEWKIND_3D = 3,
VIEWKIND_RED,
VIEWKIND_GREEN,
VIEWKIND_BLUE,
VIEWKIND_ALPHA
} SurfViewKind;
/* To avoid problems with non-const Py_buffer format field */
static char FormatUint8[] = "B";
static char FormatUint16[] = "=H";
static char FormatUint24[] = "3x";
static char FormatUint32[] = "=I";
typedef struct pg_bufferinternal_s {
PyObject *consumer_ref; /* A weak reference to a bufferproxy object */
Py_ssize_t mem[6]; /* Enough memory for dim 3 shape and strides */
} pg_bufferinternal;
/* copy of SDL Blit mapping definitions to enable pointer casting hack
for checking state of the SDL_COPY_RLE_DESIRED flag */
#define PGS_COPY_RLE_DESIRED 0x00001000
typedef struct {
Uint8 *src;
int src_w, src_h;
int src_pitch;
int src_skip;
Uint8 *dst;
int dst_w, dst_h;
int dst_pitch;
int dst_skip;
SDL_PixelFormat *src_fmt;
SDL_PixelFormat *dst_fmt;
Uint8 *table;
int flags;
Uint32 colorkey;
Uint8 r, g, b, a;
} pg_BlitInfo;
typedef struct pg_BlitMap {
SDL_Surface *dst;
int identity;
SDL_blit blit;
void *data;
pg_BlitInfo info;
/* the version count matches the destination; mismatch indicates
an invalid mapping */
Uint32 dst_palette_version;
Uint32 src_palette_version;
} pg_BlitMap;
/* end PGS_COPY_RLE_DESIRED hack definitions */
int
pgSurface_Blit(pgSurfaceObject *dstobj, pgSurfaceObject *srcobj,
SDL_Rect *dstrect, SDL_Rect *srcrect, int the_args);
/* statics */
static pgSurfaceObject *
pgSurface_New2(SDL_Surface *info, int owner);
static PyObject *
surf_subtype_new(PyTypeObject *type, SDL_Surface *s, int owner);
static PyObject *
surface_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static intptr_t
surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds);
static PyObject *
surface_str(PyObject *self);
static void
surface_dealloc(PyObject *self);
static void
surface_cleanup(pgSurfaceObject *self);
static void
surface_move(Uint8 *src, Uint8 *dst, int h, int span, int srcpitch,
int dstpitch);
static PyObject *
surf_get_at(PyObject *self, PyObject *args);
static PyObject *
surf_set_at(PyObject *self, PyObject *args);
static PyObject *
surf_get_at_mapped(PyObject *self, PyObject *args);
static PyObject *
surf_map_rgb(PyObject *self, PyObject *args);
static PyObject *
surf_unmap_rgb(PyObject *self, PyObject *arg);
static PyObject *
surf_lock(PyObject *self, PyObject *args);
static PyObject *
surf_unlock(PyObject *self, PyObject *args);
static PyObject *
surf_mustlock(PyObject *self, PyObject *args);
static PyObject *
surf_get_locked(PyObject *self, PyObject *args);
static PyObject *
surf_get_locks(PyObject *self, PyObject *args);
static PyObject *
surf_get_palette(PyObject *self, PyObject *args);
static PyObject *
surf_get_palette_at(PyObject *self, PyObject *args);
static PyObject *
surf_set_palette(PyObject *self, PyObject *seq);
static PyObject *
surf_set_palette_at(PyObject *self, PyObject *args);
static PyObject *
surf_set_colorkey(pgSurfaceObject *self, PyObject *args);
static PyObject *
surf_get_colorkey(pgSurfaceObject *self, PyObject *args);
static PyObject *
surf_set_alpha(pgSurfaceObject *self, PyObject *args);
static PyObject *
surf_get_alpha(pgSurfaceObject *self, PyObject *args);
static PyObject *
surf_get_blendmode(PyObject *self, PyObject *args);
static PyObject *
surf_copy(pgSurfaceObject *self, PyObject *args);
static PyObject *
surf_convert(pgSurfaceObject *self, PyObject *args);
static PyObject *
surf_convert_alpha(pgSurfaceObject *self, PyObject *args);
static PyObject *
surf_set_clip(PyObject *self, PyObject *args);
static PyObject *
surf_get_clip(PyObject *self, PyObject *args);
static PyObject *
surf_blit(pgSurfaceObject *self, PyObject *args, PyObject *keywds);
static PyObject *
surf_blits(pgSurfaceObject *self, PyObject *args, PyObject *keywds);
static PyObject *
surf_fill(pgSurfaceObject *self, PyObject *args, PyObject *keywds);
static PyObject *
surf_scroll(PyObject *self, PyObject *args, PyObject *keywds);
static PyObject *
surf_get_abs_offset(PyObject *self, PyObject *args);
static PyObject *
surf_get_abs_parent(PyObject *self, PyObject *args);
static PyObject *
surf_get_bitsize(PyObject *self, PyObject *args);
static PyObject *
surf_get_bytesize(PyObject *self, PyObject *args);
static PyObject *
surf_get_flags(PyObject *self, PyObject *args);
static PyObject *
surf_get_height(PyObject *self, PyObject *args);
static PyObject *
surf_get_pitch(PyObject *self, PyObject *args);
static PyObject *
surf_get_rect(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *
surf_get_width(PyObject *self, PyObject *args);
static PyObject *
surf_get_shifts(PyObject *self, PyObject *args);
static PyObject *
surf_set_shifts(PyObject *self, PyObject *args);
static PyObject *
surf_get_size(PyObject *self, PyObject *args);
static PyObject *
surf_get_losses(PyObject *self, PyObject *args);
static PyObject *
surf_get_masks(PyObject *self, PyObject *args);
static PyObject *
surf_set_masks(PyObject *self, PyObject *args);
static PyObject *
surf_get_offset(PyObject *self, PyObject *args);
static PyObject *
surf_get_parent(PyObject *self, PyObject *args);
static PyObject *
surf_subsurface(PyObject *self, PyObject *args);
static PyObject *
surf_get_view(PyObject *self, PyObject *args);
static PyObject *
surf_get_buffer(PyObject *self, PyObject *args);
static PyObject *
surf_get_bounding_rect(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject *
surf_get_pixels_address(PyObject *self, PyObject *closure);
static PyObject *
surf_premul_alpha(pgSurfaceObject *self, PyObject *args);
static int
_view_kind(PyObject *obj, void *view_kind_vptr);
static int
_get_buffer_0D(PyObject *obj, Py_buffer *view_p, int flags);
static int
_get_buffer_1D(PyObject *obj, Py_buffer *view_p, int flags);
static int
_get_buffer_2D(PyObject *obj, Py_buffer *view_p, int flags);
static int
_get_buffer_3D(PyObject *obj, Py_buffer *view_p, int flags);
static int
_get_buffer_red(PyObject *obj, Py_buffer *view_p, int flags);
static int
_get_buffer_green(PyObject *obj, Py_buffer *view_p, int flags);
static int
_get_buffer_blue(PyObject *obj, Py_buffer *view_p, int flags);
static int
_get_buffer_alpha(PyObject *obj, Py_buffer *view_p, int flags);
static int
_get_buffer_colorplane(PyObject *obj, Py_buffer *view_p, int flags, char *name,
Uint32 mask);
static int
_init_buffer(PyObject *surf, Py_buffer *view_p, int flags);
static void
_release_buffer(Py_buffer *view_p);
static PyObject *
_raise_get_view_ndim_error(int bitsize, SurfViewKind kind);
static PyObject *
_raise_create_surface_error(void);
static SDL_Surface *
pg_DisplayFormatAlpha(SDL_Surface *surface);
static SDL_Surface *
pg_DisplayFormat(SDL_Surface *surface);
static int
_PgSurface_SrcAlpha(SDL_Surface *surf);
#if !SDL_VERSION_ATLEAST(2, 0, 10)
static Uint32
pg_map_rgb(SDL_Surface *surf, Uint8 r, Uint8 g, Uint8 b)
{
/* SDL_MapRGB() returns wrong values for color keys
for indexed formats since since alpha = 0 */
Uint32 key;
if (!surf->format->palette)
return SDL_MapRGB(surf->format, r, g, b);
if (!SDL_GetColorKey(surf, &key)) {
Uint8 keyr, keyg, keyb;
SDL_GetRGB(key, surf->format, &keyr, &keyg, &keyb);
if (r == keyr && g == keyg && b == keyb)
return key;
}
else
SDL_ClearError();
return SDL_MapRGBA(surf->format, r, g, b, SDL_ALPHA_OPAQUE);
}
static Uint32
pg_map_rgba(SDL_Surface *surf, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
{
if (!surf->format->palette)
return SDL_MapRGBA(surf->format, r, g, b, a);
return pg_map_rgb(surf, r, g, b);
}
#else /* SDL_VERSION_ATLEAST(2, 0, 10) */
#define pg_map_rgb(surf, r, g, b) SDL_MapRGB((surf)->format, (r), (g), (b))
#define pg_map_rgba(surf, r, g, b, a) \
SDL_MapRGBA((surf)->format, (r), (g), (b), (a))
#endif /* SDL_VERSION_ATLEAST(2, 0, 10) */
static PyGetSetDef surface_getsets[] = {
{"_pixels_address", (getter)surf_get_pixels_address, NULL,
"pixel buffer address (readonly)", NULL},
{NULL, NULL, NULL, NULL, NULL}};
static struct PyMethodDef surface_methods[] = {
{"get_at", surf_get_at, METH_VARARGS, DOC_SURFACEGETAT},
{"set_at", surf_set_at, METH_VARARGS, DOC_SURFACESETAT},
{"get_at_mapped", surf_get_at_mapped, METH_VARARGS,
DOC_SURFACEGETATMAPPED},
{"map_rgb", surf_map_rgb, METH_VARARGS, DOC_SURFACEMAPRGB},
{"unmap_rgb", surf_unmap_rgb, METH_O, DOC_SURFACEUNMAPRGB},
{"get_palette", surf_get_palette, METH_NOARGS, DOC_SURFACEGETPALETTE},
{"get_palette_at", surf_get_palette_at, METH_VARARGS,
DOC_SURFACEGETPALETTEAT},
{"set_palette", surf_set_palette, METH_O, DOC_SURFACESETPALETTE},
{"set_palette_at", surf_set_palette_at, METH_VARARGS,
DOC_SURFACESETPALETTEAT},
{"lock", surf_lock, METH_NOARGS, DOC_SURFACELOCK},
{"unlock", surf_unlock, METH_NOARGS, DOC_SURFACEUNLOCK},
{"mustlock", surf_mustlock, METH_NOARGS, DOC_SURFACEMUSTLOCK},
{"get_locked", surf_get_locked, METH_NOARGS, DOC_SURFACEGETLOCKED},
{"get_locks", surf_get_locks, METH_NOARGS, DOC_SURFACEGETLOCKS},
{"set_colorkey", (PyCFunction)surf_set_colorkey, METH_VARARGS,
DOC_SURFACESETCOLORKEY},
{"get_colorkey", (PyCFunction)surf_get_colorkey, METH_NOARGS,
DOC_SURFACEGETCOLORKEY},
{"set_alpha", (PyCFunction)surf_set_alpha, METH_VARARGS,
DOC_SURFACESETALPHA},
{"get_alpha", (PyCFunction)surf_get_alpha, METH_NOARGS,
DOC_SURFACEGETALPHA},
{"get_blendmode", surf_get_blendmode, METH_NOARGS,
"Return the surface's SDL 2 blend mode"},
{"copy", (PyCFunction)surf_copy, METH_NOARGS, DOC_SURFACECOPY},
{"__copy__", (PyCFunction)surf_copy, METH_NOARGS, DOC_SURFACECOPY},
{"convert", (PyCFunction)surf_convert, METH_VARARGS, DOC_SURFACECONVERT},
{"convert_alpha", (PyCFunction)surf_convert_alpha, METH_VARARGS,
DOC_SURFACECONVERTALPHA},
{"set_clip", surf_set_clip, METH_VARARGS, DOC_SURFACESETCLIP},
{"get_clip", surf_get_clip, METH_NOARGS, DOC_SURFACEGETCLIP},
{"fill", (PyCFunction)surf_fill, METH_VARARGS | METH_KEYWORDS,
DOC_SURFACEFILL},
{"blit", (PyCFunction)surf_blit, METH_VARARGS | METH_KEYWORDS,
DOC_SURFACEBLIT},
{"blits", (PyCFunction)surf_blits, METH_VARARGS | METH_KEYWORDS,
DOC_SURFACEBLITS},
{"scroll", (PyCFunction)surf_scroll, METH_VARARGS | METH_KEYWORDS,
DOC_SURFACESCROLL},
{"get_flags", surf_get_flags, METH_NOARGS, DOC_SURFACEGETFLAGS},
{"get_size", surf_get_size, METH_NOARGS, DOC_SURFACEGETSIZE},
{"get_width", surf_get_width, METH_NOARGS, DOC_SURFACEGETWIDTH},
{"get_height", surf_get_height, METH_NOARGS, DOC_SURFACEGETHEIGHT},
{"get_rect", (PyCFunction)surf_get_rect, METH_VARARGS | METH_KEYWORDS,
DOC_SURFACEGETRECT},
{"get_pitch", surf_get_pitch, METH_NOARGS, DOC_SURFACEGETPITCH},
{"get_bitsize", surf_get_bitsize, METH_NOARGS, DOC_SURFACEGETBITSIZE},
{"get_bytesize", surf_get_bytesize, METH_NOARGS, DOC_SURFACEGETBYTESIZE},
{"get_masks", surf_get_masks, METH_NOARGS, DOC_SURFACEGETMASKS},
{"get_shifts", surf_get_shifts, METH_NOARGS, DOC_SURFACEGETSHIFTS},
{"set_masks", surf_set_masks, METH_VARARGS, DOC_SURFACESETMASKS},
{"set_shifts", surf_set_shifts, METH_VARARGS, DOC_SURFACESETSHIFTS},
{"get_losses", surf_get_losses, METH_NOARGS, DOC_SURFACEGETLOSSES},
{"subsurface", surf_subsurface, METH_VARARGS, DOC_SURFACESUBSURFACE},
{"get_offset", surf_get_offset, METH_NOARGS, DOC_SURFACEGETOFFSET},
{"get_abs_offset", surf_get_abs_offset, METH_NOARGS,
DOC_SURFACEGETABSOFFSET},
{"get_parent", surf_get_parent, METH_NOARGS, DOC_SURFACEGETPARENT},
{"get_abs_parent", surf_get_abs_parent, METH_NOARGS,
DOC_SURFACEGETABSPARENT},
{"get_bounding_rect", (PyCFunction)surf_get_bounding_rect,
METH_VARARGS | METH_KEYWORDS, DOC_SURFACEGETBOUNDINGRECT},
{"get_view", surf_get_view, METH_VARARGS, DOC_SURFACEGETVIEW},
{"get_buffer", surf_get_buffer, METH_NOARGS, DOC_SURFACEGETBUFFER},
{"premul_alpha", (PyCFunction)surf_premul_alpha, METH_NOARGS,
DOC_SURFACEPREMULALPHA},
{NULL, NULL, 0, NULL}};
static PyTypeObject pgSurface_Type = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.surface.Surface",
.tp_basicsize = sizeof(pgSurfaceObject),
.tp_dealloc = surface_dealloc,
.tp_repr = surface_str,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = DOC_PYGAMESURFACE,
.tp_weaklistoffset = offsetof(pgSurfaceObject, weakreflist),
.tp_methods = surface_methods,
.tp_getset = surface_getsets,
.tp_init = (initproc)surface_init,
.tp_new = surface_new,
};
#define pgSurface_Check(x) \
(PyObject_IsInstance((x), (PyObject *)&pgSurface_Type))
static pgSurfaceObject *
pgSurface_New2(SDL_Surface *s, int owner)
{
return (pgSurfaceObject *)surf_subtype_new(&pgSurface_Type, s, owner);
}
static int
pgSurface_SetSurface(pgSurfaceObject *self, SDL_Surface *s, int owner)
{
if (!s) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}
if (s == self->surf) {
self->owner = owner;
return 0;
}
surface_cleanup(self);
self->surf = s;
self->owner = owner;
return 0;
}
static PyObject *
surf_subtype_new(PyTypeObject *type, SDL_Surface *s, int owner)
{
pgSurfaceObject *self;
if (!s)
return RAISE(pgExc_SDLError, SDL_GetError());
self = (pgSurfaceObject *)pgSurface_Type.tp_new(type, NULL, NULL);
if (pgSurface_SetSurface(self, s, owner))
return NULL;
return (PyObject *)self;
}
static PyObject *
surface_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
pgSurfaceObject *self;
self = (pgSurfaceObject *)type->tp_alloc(type, 0);
if (self) {
self->surf = NULL;
self->owner = 0;
self->subsurface = NULL;
self->weakreflist = NULL;
self->dependency = NULL;
self->locklist = NULL;
}
return (PyObject *)self;
}
/* surface object internals */
static void
surface_cleanup(pgSurfaceObject *self)
{
if (self->surf && self->owner) {
SDL_FreeSurface(self->surf);
self->surf = NULL;
}
if (self->subsurface) {
Py_XDECREF(self->subsurface->owner);
PyMem_Free(self->subsurface);
self->subsurface = NULL;
}
if (self->dependency) {
Py_DECREF(self->dependency);
self->dependency = NULL;
}
if (self->locklist) {
Py_DECREF(self->locklist);
self->locklist = NULL;
}
self->owner = 0;
}
static void
surface_dealloc(PyObject *self)
{
if (((pgSurfaceObject *)self)->weakreflist)
PyObject_ClearWeakRefs(self);
surface_cleanup((pgSurfaceObject *)self);
Py_TYPE(self)->tp_free(self);
}
static PyObject *
surface_str(PyObject *self)
{
SDL_Surface *surf = pgSurface_AsSurface(self);
if (!surf) {
return PyUnicode_FromString("<Surface(Dead Display)>");
}
return PyUnicode_FromFormat("<Surface(%dx%dx%d SW)>", surf->w, surf->h,
surf->format->BitsPerPixel);
}
static intptr_t
surface_init(pgSurfaceObject *self, PyObject *args, PyObject *kwds)
{
Uint32 flags = 0;
int width, height;
PyObject *depth = NULL, *masks = NULL, *size = NULL;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
SDL_Surface *surface;
SDL_PixelFormat default_format;
char *kwids[] = {"size", "flags", "depth", "masks", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iOO", kwids, &size, &flags,
&depth, &masks))
return -1;
if (PySequence_Check(size) && PySequence_Length(size) == 2) {
if ((!pg_IntFromObjIndex(size, 0, &width)) ||
(!pg_IntFromObjIndex(size, 1, &height))) {
PyErr_SetString(PyExc_ValueError,
"size needs to be (number width, number height)");
return -1;
}
}
else {
PyErr_SetString(PyExc_ValueError,
"size needs to be (number width, number height)");
return -1;
}
if (width < 0 || height < 0) {
PyErr_SetString(pgExc_SDLError, "Invalid resolution for Surface");
return -1;
}
default_format.palette = NULL;
surface_cleanup(self);
if (depth && masks) { /* all info supplied, most errorchecking
* needed */
if (pgSurface_Check(depth)) {
PyErr_SetString(PyExc_ValueError,
"cannot pass surface for depth and color masks");
return -1;
}
if (!pg_IntFromObj(depth, &bpp)) {
PyErr_SetString(PyExc_ValueError,
"invalid bits per pixel depth argument");
return -1;
}
if (!PySequence_Check(masks) || PySequence_Length(masks) != 4) {
PyErr_SetString(PyExc_ValueError,
"masks argument must be sequence of four numbers");
return -1;
}
if (!pg_UintFromObjIndex(masks, 0, &Rmask) ||
!pg_UintFromObjIndex(masks, 1, &Gmask) ||
!pg_UintFromObjIndex(masks, 2, &Bmask) ||
!pg_UintFromObjIndex(masks, 3, &Amask)) {
PyErr_SetString(PyExc_ValueError,
"invalid mask values in masks sequence");
return -1;
}
}
else if (depth && PyNumber_Check(depth)) { /* use default masks */
if (!pg_IntFromObj(depth, &bpp)) {
PyErr_SetString(PyExc_ValueError,
"invalid bits per pixel depth argument");
return -1;
}
if (flags & PGS_SRCALPHA) {
switch (bpp) {
case 16:
Rmask = 0xF << 8;
Gmask = 0xF << 4;
Bmask = 0xF;
Amask = 0xF << 12;
break;
case 32:
Rmask = 0xFF << 16;
Gmask = 0xFF << 8;
Bmask = 0xFF;
Amask = 0xFF << 24;
break;
default:
PyErr_SetString(
PyExc_ValueError,
"no standard masks exist for given bitdepth with "
"alpha");
return -1;
}
}
else {
Amask = 0;
switch (bpp) {
case 8:
Rmask = 0;
Gmask = 0;
Bmask = 0;
break;
case 12:
Rmask = 0xFF >> 4 << 8;
Gmask = 0xFF >> 4 << 4;
Bmask = 0xFF >> 4;
break;
case 15:
Rmask = 0xFF >> 3 << 10;
Gmask = 0xFF >> 3 << 5;
Bmask = 0xFF >> 3;
break;
case 16:
Rmask = 0xFF >> 3 << 11;
Gmask = 0xFF >> 2 << 5;
Bmask = 0xFF >> 3;
break;
case 24:
case 32:
Rmask = 0xFF << 16;
Gmask = 0xFF << 8;
Bmask = 0xFF;
break;
default:
PyErr_SetString(PyExc_ValueError,
"nonstandard bit depth given");
return -1;
}
}
}
else { /* no depth or surface */
SDL_PixelFormat *pix;
if (depth && pgSurface_Check(depth))
pix = ((pgSurfaceObject *)depth)->surf->format;
else if (pg_GetDefaultWindowSurface())
pix = pgSurface_AsSurface(pg_GetDefaultWindowSurface())->format;
else {
pix = &default_format;
pix->BitsPerPixel = 32;
pix->Amask = 0;
pix->Rmask = 0xFF0000;
pix->Gmask = 0xFF00;
pix->Bmask = 0xFF;
}
bpp = pix->BitsPerPixel;
if (flags & PGS_SRCALPHA) {
switch (bpp) {
case 16:
Rmask = 0xF << 8;
Gmask = 0xF << 4;
Bmask = 0xF;
Amask = 0xF << 12;
break;
case 24:
bpp = 32;
// we automatically step up to 32 if video is 24, fall
// through to case below
case 32:
Rmask = 0xFF << 16;
Gmask = 0xFF << 8;
Bmask = 0xFF;
Amask = 0xFF << 24;
break;
default:
PyErr_SetString(
PyExc_ValueError,
"no standard masks exist for given bitdepth with "
"alpha");
return -1;
}
}
else {
Rmask = pix->Rmask;
Gmask = pix->Gmask;
Bmask = pix->Bmask;
Amask = pix->Amask;
}
}
surface = SDL_CreateRGBSurface(0, width, height, bpp, Rmask, Gmask, Bmask,
Amask);
if (!surface) {
_raise_create_surface_error();
return -1;
}
if (!(flags & PGS_SRCALPHA)) {
/* We ignore the error if any. */
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
/* When the display format has a full alpha channel (MacOS right now),
* Surfaces may be created with an unreqested alpha channel, which
* could cause issues.
* pygame Surfaces are supposed to be (0, 0, 0, 255) by default.
* This is a simple fix to fill it with (0, 0, 0, 255) if necessary.
* See Github issue: https://github.com/pygame/pygame/issues/1395
*/
if (Amask != 0) {
SDL_FillRect(surface, NULL,
SDL_MapRGBA(surface->format, 0, 0, 0, 255));
}
}
if (SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
/* Give the surface something other than an all white palette.
* */
if (SDL_SetPaletteColors(surface->format->palette,
default_palette_colors, 0,
default_palette_size - 1) != 0) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
SDL_FreeSurface(surface);
return -1;
}
}
if (surface) {
self->surf = surface;
self->owner = 1;
self->subsurface = NULL;
}
return 0;
}
static PyObject *
_raise_create_surface_error(void)
{
const char *msg = SDL_GetError();
if (strcmp(msg, "Unknown pixel format") == 0)
return RAISE(PyExc_ValueError, "Invalid mask values");
return RAISE(pgExc_SDLError, msg);
}
/* surface object methods */
static PyObject *
surf_get_at(PyObject *self, PyObject *args)
{
SDL_Surface *surf = pgSurface_AsSurface(self);
SDL_PixelFormat *format = NULL;
Uint8 *pixels = NULL;
int x, y;
Uint32 color;
Uint8 *pix;
Uint8 rgba[4] = {0, 0, 0, 255};
if (!PyArg_ParseTuple(args, "(ii)", &x, &y))
return NULL;
if (!surf)
return RAISE(pgExc_SDLError, "display Surface quit");
if (x < 0 || x >= surf->w || y < 0 || y >= surf->h)
return RAISE(PyExc_IndexError, "pixel index out of range");
format = surf->format;
if (format->BytesPerPixel < 1 || format->BytesPerPixel > 4)
return RAISE(PyExc_RuntimeError, "invalid color depth for surface");
if (!pgSurface_Lock((pgSurfaceObject *)self))
return NULL;
pixels = (Uint8 *)surf->pixels;
switch (format->BytesPerPixel) {
case 1:
color = (Uint32) * ((Uint8 *)pixels + y * surf->pitch + x);
SDL_GetRGB(color, format, rgba, rgba + 1, rgba + 2);
break;
case 2:
color = (Uint32) * ((Uint16 *)(pixels + y * surf->pitch) + x);
SDL_GetRGBA(color, format, rgba, rgba + 1, rgba + 2, rgba + 3);
break;
case 3:
pix = ((Uint8 *)(pixels + y * surf->pitch) + x * 3);
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
color = (pix[0]) + (pix[1] << 8) + (pix[2] << 16);
#else
color = (pix[2]) + (pix[1] << 8) + (pix[0] << 16);
#endif
SDL_GetRGB(color, format, rgba, rgba + 1, rgba + 2);
break;
default: /* case 4: */
assert(format->BytesPerPixel == 4);
color = *((Uint32 *)(pixels + y * surf->pitch) + x);
SDL_GetRGBA(color, format, rgba, rgba + 1, rgba + 2, rgba + 3);
break;
}
if (!pgSurface_Unlock((pgSurfaceObject *)self))
return NULL;
return pgColor_New(rgba);
}
static PyObject *
surf_set_at(PyObject *self, PyObject *args)
{
SDL_Surface *surf = pgSurface_AsSurface(self);
SDL_PixelFormat *format = NULL;
Uint8 *pixels;
int x, y;
Uint32 color;
Uint8 rgba[4] = {0, 0, 0, 0};
PyObject *rgba_obj;
Uint8 *byte_buf;
if (!PyArg_ParseTuple(args, "(ii)O", &x, &y, &rgba_obj))
return NULL;
if (!surf)
return RAISE(pgExc_SDLError, "display Surface quit");
format = surf->format;
if (format->BytesPerPixel < 1 || format->BytesPerPixel > 4)
return RAISE(PyExc_RuntimeError, "invalid color depth for surface");
if (x < surf->clip_rect.x || x >= surf->clip_rect.x + surf->clip_rect.w ||
y < surf->clip_rect.y || y >= surf->clip_rect.y + surf->clip_rect.h) {
/* out of clip area */
Py_RETURN_NONE;
}
if (PyLong_Check(rgba_obj)) {
color = (Uint32)PyLong_AsLong(rgba_obj);
if (PyErr_Occurred() && (Sint32)color == -1)
return RAISE(PyExc_TypeError, "invalid color argument");
}
else if (PyLong_Check(rgba_obj)) {
color = (Uint32)PyLong_AsUnsignedLong(rgba_obj);
if (PyErr_Occurred() && (Sint32)color == -1)
return RAISE(PyExc_TypeError, "invalid color argument");
}
else if (pg_RGBAFromFuzzyColorObj(rgba_obj, rgba))
color = pg_map_rgba(surf, rgba[0], rgba[1], rgba[2], rgba[3]);
else
return NULL; /* pg_RGBAFromFuzzyColorObj set an except for us */
if (!pgSurface_Lock((pgSurfaceObject *)self))
return NULL;
pixels = (Uint8 *)surf->pixels;
switch (format->BytesPerPixel) {
case 1:
*((Uint8 *)pixels + y * surf->pitch + x) = (Uint8)color;
break;
case 2:
*((Uint16 *)(pixels + y * surf->pitch) + x) = (Uint16)color;
break;
case 3:
byte_buf = (Uint8 *)(pixels + y * surf->pitch) + x * 3;
#if (SDL_BYTEORDER == SDL_LIL_ENDIAN)
*(byte_buf + (format->Rshift >> 3)) =
(Uint8)(color >> format->Rshift);
*(byte_buf + (format->Gshift >> 3)) =
(Uint8)(color >> format->Gshift);
*(byte_buf + (format->Bshift >> 3)) =
(Uint8)(color >> format->Bshift);
#else
*(byte_buf + 2 - (format->Rshift >> 3)) =
(Uint8)(color >> format->Rshift);
*(byte_buf + 2 - (format->Gshift >> 3)) =
(Uint8)(color >> format->Gshift);
*(byte_buf + 2 - (format->Bshift >> 3)) =
(Uint8)(color >> format->Bshift);
#endif
break;
default: /* case 4: */
*((Uint32 *)(pixels + y * surf->pitch) + x) = color;
break;
}
if (!pgSurface_Unlock((pgSurfaceObject *)self))
return NULL;
Py_RETURN_NONE;
}
static PyObject *
surf_get_at_mapped(PyObject *self, PyObject *args)
{
SDL_Surface *surf = pgSurface_AsSurface(self);
SDL_PixelFormat *format = NULL;
Uint8 *pixels = NULL;
int x, y;
Sint32 color;
Uint8 *pix;
if (!PyArg_ParseTuple(args, "(ii)", &x, &y))
return NULL;
if (!surf)
return RAISE(pgExc_SDLError, "display Surface quit");
if (x < 0 || x >= surf->w || y < 0 || y >= surf->h)
return RAISE(PyExc_IndexError, "pixel index out of range");
format = surf->format;
if (format->BytesPerPixel < 1 || format->BytesPerPixel > 4)
return RAISE(PyExc_RuntimeError, "invalid color depth for surface");
if (!pgSurface_Lock((pgSurfaceObject *)self))
return NULL;
pixels = (Uint8 *)surf->pixels;
switch (format->BytesPerPixel) {
case 1:
color = (Uint32) * ((Uint8 *)pixels + y * surf->pitch + x);
break;
case 2:
color = (Uint32) * ((Uint16 *)(pixels + y * surf->pitch) + x);
break;
case 3:
pix = ((Uint8 *)(pixels + y * surf->pitch) + x * 3);
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
color = (pix[0]) + (pix[1] << 8) + (pix[2] << 16);
#else
color = (pix[2]) + (pix[1] << 8) + (pix[0] << 16);
#endif
break;
default: /* case 4: */
color = *((Uint32 *)(pixels + y * surf->pitch) + x);
break;
}
if (!pgSurface_Unlock((pgSurfaceObject *)self))
return NULL;
return PyLong_FromLong((long)color);
}
static PyObject *
surf_map_rgb(PyObject *self, PyObject *args)
{
SDL_Surface *surf = pgSurface_AsSurface(self);
Uint8 rgba[4];
int color;
if (!pg_RGBAFromFuzzyColorObj(args, rgba))
return NULL; /* Exception already set for us */
if (!surf)
return RAISE(pgExc_SDLError, "display Surface quit");
color = pg_map_rgba(surf, rgba[0], rgba[1], rgba[2], rgba[3]);
return PyLong_FromLong(color);
}
static PyObject *
surf_unmap_rgb(PyObject *self, PyObject *arg)
{
SDL_Surface *surf = pgSurface_AsSurface(self);
Uint32 col;
Uint8 rgba[4];
col = (Uint32)PyLong_AsLong(arg);
if (col == (Uint32)-1 && PyErr_Occurred()) {
PyErr_Clear();
return RAISE(PyExc_TypeError, "unmap_rgb expects 1 number argument");
}
if (!surf)
return RAISE(pgExc_SDLError, "display Surface quit");
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format))
SDL_GetRGBA(col, surf->format, rgba, rgba + 1, rgba + 2, rgba + 3);
else {
SDL_GetRGB(col, surf->format, rgba, rgba + 1, rgba + 2);
rgba[3] = 255;
}
return pgColor_New(rgba);
}
static PyObject *
surf_lock(PyObject *self, PyObject *_null)
{
if (!pgSurface_Lock((pgSurfaceObject *)self))
return NULL;
Py_RETURN_NONE;
}
static PyObject *
surf_unlock(PyObject *self, PyObject *_null)
{
pgSurface_Unlock((pgSurfaceObject *)self);
Py_RETURN_NONE;
}
static PyObject *
surf_mustlock(PyObject *self, PyObject *_null)
{
SDL_Surface *surf = pgSurface_AsSurface(self);
if (!surf)
return RAISE(pgExc_SDLError, "display Surface quit");
return PyBool_FromLong(SDL_MUSTLOCK(surf) ||
((pgSurfaceObject *)self)->subsurface);
}
static PyObject *
surf_get_locked(PyObject *self, PyObject *_null)
{