Skip to content

Commit 42c10c8

Browse files
committed
GET_PIXELVALS SDL3 compat
1 parent e84b4be commit 42c10c8

File tree

6 files changed

+347
-241
lines changed

6 files changed

+347
-241
lines changed

src_c/alphablit.c

Lines changed: 242 additions & 146 deletions
Large diffs are not rendered by default.

src_c/pixelarray_methods.c

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,6 @@
2828
wb * (b1 - b2) * (b1 - b2)) / \
2929
255.0)
3030

31-
// TODO: once things are fully ported, consolidate PXM_GET_PIXELVALS
32-
// and PXM_GET_PIXELVALS_1 back into GET_PIXELVALS and GET_PIXELVALS_1
33-
// from surface.h
34-
#define PXM_GET_PIXELVALS(_sR, _sG, _sB, _sA, px, fmt, palette, ppa) \
35-
PG_GetRGBA(px, fmt, palette, &(_sR), &(_sG), &(_sB), &(_sA)); \
36-
if (!ppa) { \
37-
_sA = 255; \
38-
}
39-
40-
#define PXM_GET_PIXELVALS_1(sr, sg, sb, sa, _src, _palette) \
41-
sr = _palette->colors[*((Uint8 *)(_src))].r; \
42-
sg = _palette->colors[*((Uint8 *)(_src))].g; \
43-
sb = _palette->colors[*((Uint8 *)(_src))].b; \
44-
sa = 255;
45-
4631
#define WR_NTSC 0.299
4732
#define WG_NTSC 0.587
4833
#define WB_NTSC 0.114
@@ -422,7 +407,7 @@ _replace_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
422407
for (x = 0; x < dim0; ++x) {
423408
px_p = pixel_p;
424409
if (distance != 0.0) {
425-
PXM_GET_PIXELVALS_1(r2, g2, b2, a2, px_p, palette);
410+
GET_PIXELVALS_1(r2, g2, b2, a2, px_p, palette);
426411
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
427412
b2) <= distance) {
428413
*px_p = (Uint8)rcolor;
@@ -446,8 +431,8 @@ _replace_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
446431
for (x = 0; x < dim0; ++x) {
447432
px_p = (Uint16 *)pixel_p;
448433
if (distance != 0.0) {
449-
PXM_GET_PIXELVALS(r2, g2, b2, a2, (Uint32)*px_p,
450-
format, palette, ppa);
434+
GET_PIXELVALS(r2, g2, b2, a2, (Uint32)*px_p, format,
435+
palette, ppa);
451436
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
452437
b2) <= distance) {
453438
*px_p = (Uint16)rcolor;
@@ -482,8 +467,8 @@ _replace_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
482467
((Uint32)pixel_p[Goffset] << 8) +
483468
((Uint32)pixel_p[Boffset]));
484469
if (distance != 0.0) {
485-
PXM_GET_PIXELVALS(r2, g2, b2, a2, pxcolor, format,
486-
palette, ppa);
470+
GET_PIXELVALS(r2, g2, b2, a2, pxcolor, format, palette,
471+
ppa);
487472
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
488473
b2) <= distance) {
489474
pixel_p[Roffset] = (Uint8)(rcolor >> 16);
@@ -512,8 +497,8 @@ _replace_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
512497
for (x = 0; x < dim0; ++x) {
513498
px_p = (Uint32 *)pixel_p;
514499
if (distance != 0.0) {
515-
PXM_GET_PIXELVALS(r2, g2, b2, a2, *px_p, format,
516-
palette, ppa);
500+
GET_PIXELVALS(r2, g2, b2, a2, *px_p, format, palette,
501+
ppa);
517502
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
518503
b2) <= distance) {
519504
*px_p = rcolor;
@@ -632,7 +617,7 @@ _extract_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
632617
for (x = 0; x < dim0; ++x) {
633618
px_p = pixel_p;
634619
if (distance != 0.0) {
635-
PXM_GET_PIXELVALS_1(r2, g2, b2, a2, px_p, palette);
620+
GET_PIXELVALS_1(r2, g2, b2, a2, px_p, palette);
636621
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
637622
b2) <= distance) {
638623
*px_p = (Uint8)white;
@@ -659,8 +644,8 @@ _extract_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
659644
for (x = 0; x < dim0; ++x) {
660645
px_p = (Uint16 *)pixel_p;
661646
if (distance != 0.0) {
662-
PXM_GET_PIXELVALS(r2, g2, b2, a2, (Uint32)*px_p,
663-
format, palette, ppa);
647+
GET_PIXELVALS(r2, g2, b2, a2, (Uint32)*px_p, format,
648+
palette, ppa);
664649
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
665650
b2) <= distance) {
666651
*px_p = (Uint16)white;
@@ -704,8 +689,8 @@ _extract_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
704689
((Uint32)pixel_p[Goffset] << 8) +
705690
((Uint32)pixel_p[Boffset]));
706691
if (distance != 0.0) {
707-
PXM_GET_PIXELVALS(r2, g2, b2, a2, pxcolor, format,
708-
palette, ppa);
692+
GET_PIXELVALS(r2, g2, b2, a2, pxcolor, format, palette,
693+
ppa);
709694
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
710695
b2) <= distance) {
711696
pixel_p[Roffset] = white_r;
@@ -744,8 +729,8 @@ _extract_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
744729
for (x = 0; x < dim0; ++x) {
745730
px_p = (Uint32 *)pixel_p;
746731
if (distance != 0.0) {
747-
PXM_GET_PIXELVALS(r2, g2, b2, a2, *px_p, format,
748-
palette, ppa);
732+
GET_PIXELVALS(r2, g2, b2, a2, *px_p, format, palette,
733+
ppa);
749734
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
750735
b2) <= distance) {
751736
*px_p = white;
@@ -888,9 +873,9 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
888873
pixel_p = byte_p;
889874
other_pixel_p = other_byte_p;
890875
if (distance != 0.0) {
891-
PXM_GET_PIXELVALS_1(r1, g1, b1, a1, pixel_p, palette);
892-
PXM_GET_PIXELVALS_1(r2, g2, b2, a2, other_pixel_p,
893-
other_palette);
876+
GET_PIXELVALS_1(r1, g1, b1, a1, pixel_p, palette);
877+
GET_PIXELVALS_1(r2, g2, b2, a2, other_pixel_p,
878+
other_palette);
894879
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
895880
b2) <= distance) {
896881
*pixel_p = (Uint8)white;
@@ -925,11 +910,10 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
925910
pixel_p = (Uint16 *)byte_p;
926911
other_pixel_p = (Uint16 *)other_byte_p;
927912
if (distance != 0.0) {
928-
PXM_GET_PIXELVALS(r1, g1, b1, a1, (Uint32)*pixel_p,
929-
format, palette, ppa);
930-
PXM_GET_PIXELVALS(r2, g2, b2, a2,
931-
(Uint32)*other_pixel_p, other_format,
932-
other_palette, other_ppa);
913+
GET_PIXELVALS(r1, g1, b1, a1, (Uint32)*pixel_p, format,
914+
palette, ppa);
915+
GET_PIXELVALS(r2, g2, b2, a2, (Uint32)*other_pixel_p,
916+
other_format, other_palette, other_ppa);
933917
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
934918
b2) <= distance) {
935919
*pixel_p = (Uint16)white;
@@ -1029,11 +1013,10 @@ _compare(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
10291013
pixel_p = (Uint32 *)byte_p;
10301014
other_pixel_p = (Uint32 *)other_byte_p;
10311015
if (distance != 0.0) {
1032-
PXM_GET_PIXELVALS(r1, g1, b1, a1, *pixel_p, format,
1033-
palette, ppa);
1034-
PXM_GET_PIXELVALS(r2, g2, b2, a2, *other_pixel_p,
1035-
other_format, other_palette,
1036-
other_ppa);
1016+
GET_PIXELVALS(r1, g1, b1, a1, *pixel_p, format,
1017+
palette, ppa);
1018+
GET_PIXELVALS(r2, g2, b2, a2, *other_pixel_p,
1019+
other_format, other_palette, other_ppa);
10371020
if (COLOR_DIFF_RGB(wr, wg, wb, r1, g1, b1, r2, g2,
10381021
b2) <= distance) {
10391022
*pixel_p = white;

src_c/simd_blitters.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ blit_blend_premultiplied_sse2(SDL_BlitInfo *info);
4949
generic like surface_ops*/
5050

5151
void
52-
premul_surf_color_by_alpha_non_simd(SDL_Surface *src, SDL_Surface *dst);
52+
premul_surf_color_by_alpha_non_simd(SDL_Surface *src,
53+
PG_PixelFormat *src_format,
54+
SDL_Palette *src_palette, SDL_Surface *dst,
55+
PG_PixelFormat *dst_format,
56+
SDL_Palette *dst_palette);
5357
void
5458
premul_surf_color_by_alpha_sse2(SDL_Surface *src, SDL_Surface *dst);
5559

src_c/surface.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3336,11 +3336,15 @@ surf_premul_alpha(pgSurfaceObject *self, PyObject *_null)
33363336
if ((surf->w > 0 && surf->h > 0)) {
33373337
// If the surface has no pixels we don't need to premul
33383338
// just return the copy.
3339-
if (premul_surf_color_by_alpha(surf, newsurf) != 0) {
3339+
int result = premul_surf_color_by_alpha(surf, newsurf);
3340+
if (result == -1) {
33403341
return RAISE(PyExc_ValueError,
33413342
"source surface to be alpha pre-multiplied must have "
33423343
"alpha channel");
33433344
}
3345+
else if (result == -2) {
3346+
return RAISE(pgExc_SDLError, SDL_GetError());
3347+
}
33443348
}
33453349
pgSurface_Unprep(self);
33463350

@@ -3363,11 +3367,15 @@ surf_premul_alpha_ip(pgSurfaceObject *self, PyObject *_null)
33633367

33643368
pgSurface_Prep(self);
33653369

3366-
if (premul_surf_color_by_alpha(surf, surf) != 0) {
3370+
int result = premul_surf_color_by_alpha(surf, surf);
3371+
if (result == -1) {
33673372
return RAISE(PyExc_ValueError,
33683373
"source surface to be alpha pre-multiplied must have "
33693374
"alpha channel");
33703375
}
3376+
else if (result == -2) {
3377+
return RAISE(pgExc_SDLError, SDL_GetError());
3378+
}
33713379

33723380
pgSurface_Unprep(self);
33733381

src_c/surface.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@
7878
} break; \
7979
}
8080

81-
#define GET_PIXELVALS(_sR, _sG, _sB, _sA, px, fmt, ppa) \
82-
SDL_GetRGBA(px, fmt, &(_sR), &(_sG), &(_sB), &(_sA)); \
83-
if (!ppa) { \
84-
_sA = 255; \
81+
#define GET_PIXELVALS(_sR, _sG, _sB, _sA, px, fmt, palette, ppa) \
82+
PG_GetRGBA(px, fmt, palette, &(_sR), &(_sG), &(_sB), &(_sA)); \
83+
if (!ppa) { \
84+
_sA = 255; \
8585
}
8686

87-
#define GET_PIXELVALS_1(sr, sg, sb, sa, _src, _fmt) \
88-
sr = _fmt->palette->colors[*((Uint8 *)(_src))].r; \
89-
sg = _fmt->palette->colors[*((Uint8 *)(_src))].g; \
90-
sb = _fmt->palette->colors[*((Uint8 *)(_src))].b; \
87+
#define GET_PIXELVALS_1(sr, sg, sb, sa, _src, _palette) \
88+
sr = _palette->colors[*((Uint8 *)(_src))].r; \
89+
sg = _palette->colors[*((Uint8 *)(_src))].g; \
90+
sb = _palette->colors[*((Uint8 *)(_src))].b; \
9191
sa = 255;
9292

9393
/* For 1 byte palette pixels */
94-
#define SET_PIXELVAL(px, fmt, _dR, _dG, _dB, _dA) \
95-
*(px) = (Uint8)SDL_MapRGBA(fmt, _dR, _dG, _dB, _dA)
94+
#define SET_PIXELVAL(px, fmt, palette, _dR, _dG, _dB, _dA) \
95+
*(px) = (Uint8)PG_MapRGBA(fmt, palette, _dR, _dG, _dB, _dA)
9696

9797
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
9898
#define SET_OFFSETS_24(or, og, ob, fmt) \

0 commit comments

Comments
 (0)