Skip to content

Commit a68f741

Browse files
authored
Merge pull request #2844 from Starbuck5/surface-colorkey
Always call HasColorKey before GetColorKey
2 parents b7e1aa3 + 73df3c8 commit a68f741

File tree

7 files changed

+36
-33
lines changed

7 files changed

+36
-33
lines changed

src_c/alphablit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ SoftBlitPyGame(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst,
114114
info.src = src->format;
115115
info.dst = dst->format;
116116
SDL_GetSurfaceAlphaMod(src, &info.src_blanket_alpha);
117-
info.src_has_colorkey = SDL_GetColorKey(src, &info.src_colorkey) == 0;
117+
if ((info.src_has_colorkey = SDL_HasColorKey(src))) {
118+
SDL_GetColorKey(src, &info.src_colorkey);
119+
}
118120
if (SDL_GetSurfaceBlendMode(src, &info.src_blend) ||
119121
SDL_GetSurfaceBlendMode(dst, &info.dst_blend)) {
120122
okay = 0;

src_c/image.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,9 @@ image_tobytes(PyObject *self, PyObject *arg, PyObject *kwarg)
548548
byte_width = surf->w * 3;
549549
}
550550
else if (!strcmp(format, "RGBA")) {
551-
hascolorkey = (SDL_GetColorKey(surf, &colorkey) == 0);
551+
if ((hascolorkey = SDL_HasColorKey(surf))) {
552+
SDL_GetColorKey(surf, &colorkey);
553+
}
552554
byte_width = surf->w * 4;
553555
}
554556
else if (!strcmp(format, "RGBX") || !strcmp(format, "ARGB") ||
@@ -1542,7 +1544,9 @@ SaveTGA_RW(SDL_Surface *surface, SDL_RWops *out, int rle)
15421544
}
15431545

15441546
SDL_GetSurfaceAlphaMod(surface, &surf_alpha);
1545-
have_surf_colorkey = (SDL_GetColorKey(surface, &surf_colorkey) == 0);
1547+
if ((have_surf_colorkey = SDL_HasColorKey(surface))) {
1548+
SDL_GetColorKey(surface, &surf_colorkey);
1549+
}
15461550

15471551
if (srcbpp == 8) {
15481552
h.has_cmap = 1;

src_c/mask.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,6 @@ mask_from_surface(PyObject *self, PyObject *args, PyObject *kwargs)
831831
pgMaskObject *maskobj = NULL;
832832
Uint32 colorkey;
833833
int threshold = 127; /* default value */
834-
int use_thresh = 1;
835834
static char *keywords[] = {"surface", "threshold", NULL};
836835

837836
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|i", keywords,
@@ -864,14 +863,13 @@ mask_from_surface(PyObject *self, PyObject *args, PyObject *kwargs)
864863

865864
Py_BEGIN_ALLOW_THREADS; /* Release the GIL. */
866865

867-
use_thresh = (SDL_GetColorKey(surf, &colorkey) == -1);
868-
869-
if (use_thresh) {
870-
set_from_threshold(surf, maskobj->mask, threshold);
871-
}
872-
else {
866+
if (SDL_HasColorKey(surf)) {
867+
SDL_GetColorKey(surf, &colorkey);
873868
set_from_colorkey(surf, maskobj->mask, colorkey);
874869
}
870+
else { // use threshold
871+
set_from_threshold(surf, maskobj->mask, threshold);
872+
}
875873

876874
Py_END_ALLOW_THREADS; /* Obtain the GIL. */
877875

src_c/pixelcopy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf,
322322
dz_dst = -1;
323323
}
324324
#endif
325-
if (view_kind == VIEWKIND_COLORKEY &&
326-
SDL_GetColorKey(surf, &colorkey) == 0) {
325+
if (view_kind == VIEWKIND_COLORKEY && SDL_HasColorKey(surf)) {
326+
SDL_GetColorKey(surf, &colorkey);
327327
for (x = 0; x < w; ++x) {
328328
for (y = 0; y < h; ++y) {
329329
for (z = 0; z < pixelsize; ++z) {

src_c/rotozoom.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
602602
* Target surface is 32bit with source RGBA/ABGR ordering
603603
*/
604604
rz_dst = PG_CreateSurface(dstwidth, dstheight, rz_src->format->format);
605-
if (SDL_GetColorKey(src, &colorkey) == 0) {
605+
if (SDL_HasColorKey(src)) {
606+
SDL_GetColorKey(src, &colorkey);
606607
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
607608
SDL_FreeSurface(rz_dst);
608609
return NULL;
@@ -660,7 +661,8 @@ rotozoomSurface(SDL_Surface *src, double angle, double zoom, int smooth)
660661
*/
661662

662663
rz_dst = PG_CreateSurface(dstwidth, dstheight, rz_src->format->format);
663-
if (SDL_GetColorKey(src, &colorkey) == 0) {
664+
if (SDL_HasColorKey(src)) {
665+
SDL_GetColorKey(src, &colorkey);
664666
if (SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey) != 0) {
665667
SDL_FreeSurface(rz_dst);
666668
return NULL;

src_c/surface.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,11 +1265,12 @@ surf_get_colorkey(pgSurfaceObject *self, PyObject *_null)
12651265

12661266
SURF_INIT_CHECK(surf)
12671267

1268-
if (SDL_GetColorKey(surf, &mapped_color) != 0) {
1269-
SDL_ClearError();
1268+
if (!SDL_HasColorKey(surf)) {
12701269
Py_RETURN_NONE;
12711270
}
12721271

1272+
SDL_GetColorKey(surf, &mapped_color);
1273+
12731274
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format))
12741275
SDL_GetRGBA(mapped_color, surf->format, &r, &g, &b, &a);
12751276
else
@@ -1430,8 +1431,8 @@ surf_convert(pgSurfaceObject *self, PyObject *args)
14301431

14311432
pgSurface_Prep(self);
14321433

1433-
if (SDL_GetColorKey(surf, &colorkey) == 0) {
1434-
has_colorkey = SDL_TRUE;
1434+
if ((has_colorkey = SDL_HasColorKey(surf))) {
1435+
SDL_GetColorKey(surf, &colorkey);
14351436
if (SDL_ISPIXELFORMAT_ALPHA(surf->format->format))
14361437
SDL_GetRGBA(colorkey, surf->format, &key_r, &key_g, &key_b,
14371438
&key_a);
@@ -2434,7 +2435,7 @@ surf_get_flags(PyObject *self, PyObject *_null)
24342435
if (is_alpha) {
24352436
flags |= PGS_SRCALPHA;
24362437
}
2437-
if (SDL_GetColorKey(surf, NULL) == 0)
2438+
if (SDL_HasColorKey(surf))
24382439
flags |= PGS_SRCCOLORKEY;
24392440
if (sdl_flags & SDL_PREALLOC)
24402441
flags |= PGS_PREALLOC;
@@ -2615,7 +2616,6 @@ surf_subsurface(PyObject *self, PyObject *args)
26152616
struct pgSubSurface_Data *data;
26162617
Uint8 alpha;
26172618
Uint32 colorkey;
2618-
int ecode;
26192619

26202620
SURF_INIT_CHECK(surf)
26212621

@@ -2679,21 +2679,14 @@ surf_subsurface(PyObject *self, PyObject *args)
26792679
return NULL;
26802680
}
26812681
}
2682-
ecode = SDL_GetColorKey(surf, &colorkey);
2683-
if (ecode == 0) {
2682+
if (SDL_HasColorKey(surf)) {
2683+
SDL_GetColorKey(surf, &colorkey);
26842684
if (SDL_SetColorKey(sub, SDL_TRUE, colorkey) != 0) {
26852685
PyErr_SetString(pgExc_SDLError, SDL_GetError());
26862686
SDL_FreeSurface(sub);
26872687
return NULL;
26882688
}
26892689
}
2690-
else if (ecode == -1)
2691-
SDL_ClearError();
2692-
else {
2693-
PyErr_SetString(pgExc_SDLError, SDL_GetError());
2694-
SDL_FreeSurface(sub);
2695-
return NULL;
2696-
}
26972690

26982691
data = PyMem_New(struct pgSubSurface_Data, 1);
26992692
if (!data)
@@ -2839,8 +2832,8 @@ surf_get_bounding_rect(PyObject *self, PyObject *args, PyObject *kwargs)
28392832

28402833
format = surf->format;
28412834

2842-
if (SDL_GetColorKey(surf, &colorkey) == 0) {
2843-
has_colorkey = 1;
2835+
if ((has_colorkey = SDL_HasColorKey(surf))) {
2836+
SDL_GetColorKey(surf, &colorkey);
28442837
SDL_GetRGBA(colorkey, surf->format, &keyr, &keyg, &keyb, &a);
28452838
}
28462839

src_c/transform.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ newsurf_fromsurf(SDL_Surface *surf, int width, int height)
178178
}
179179
}
180180

181-
if (SDL_GetColorKey(surf, &colorkey) == 0) {
181+
if (SDL_HasColorKey(surf)) {
182+
SDL_GetColorKey(surf, &colorkey);
182183
if (SDL_SetColorKey(newsurf, SDL_TRUE, colorkey) != 0) {
183184
PyErr_SetString(pgExc_SDLError, SDL_GetError());
184185
SDL_FreeSurface(newsurf);
@@ -672,7 +673,7 @@ surf_rotate(PyObject *self, PyObject *args, PyObject *kwargs)
672673
return NULL;
673674

674675
/* get the background color */
675-
if (SDL_GetColorKey(surf, &bgcolor) != 0) {
676+
if (!SDL_HasColorKey(surf)) {
676677
SDL_LockSurface(surf);
677678
switch (PG_SURF_BytesPerPixel(surf)) {
678679
case 1:
@@ -698,6 +699,9 @@ surf_rotate(PyObject *self, PyObject *args, PyObject *kwargs)
698699
SDL_UnlockSurface(surf);
699700
bgcolor &= ~surf->format->Amask;
700701
}
702+
else {
703+
SDL_GetColorKey(surf, &bgcolor);
704+
}
701705

702706
SDL_LockSurface(newsurf);
703707
pgSurface_Lock(surfobj);

0 commit comments

Comments
 (0)