28
28
29
29
#include "doc/pixelcopy_doc.h"
30
30
31
- #include <SDL_endian.h>
32
-
33
31
typedef enum {
34
32
PXC_VIEWKIND_RED ,
35
33
PXC_VIEWKIND_GREEN ,
@@ -256,7 +254,6 @@ static int
256
254
_copy_colorplane (Py_buffer * view_p , SDL_Surface * surf ,
257
255
_pc_view_kind_t view_kind , Uint8 opaque , Uint8 clear )
258
256
{
259
- SDL_PixelFormat * format = surf -> format ;
260
257
int pixelsize = PG_SURF_BytesPerPixel (surf );
261
258
SDL_BlendMode mode ;
262
259
int intsize = (int )view_p -> itemsize ;
@@ -275,6 +272,8 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf,
275
272
Uint8 * element = 0 ;
276
273
_pc_pixel_t pixel = {0 };
277
274
Uint32 colorkey ;
275
+ PG_PixelFormat * format ;
276
+ SDL_Palette * palette ;
278
277
279
278
if (view_p -> shape [0 ] != w || view_p -> shape [1 ] != h ) {
280
279
PyErr_Format (PyExc_ValueError ,
@@ -288,10 +287,21 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf,
288
287
intsize );
289
288
return -1 ;
290
289
}
291
- if (SDL_GetSurfaceBlendMode (surf , & mode ) < 0 ) {
290
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
291
+ if (!SDL_GetSurfaceBlendMode (surf , & mode ))
292
+ #else
293
+ if (SDL_GetSurfaceBlendMode (surf , & mode ) < 0 )
294
+ #endif
295
+ {
296
+ PyErr_SetString (pgExc_SDLError , SDL_GetError ());
297
+ return -1 ;
298
+ }
299
+
300
+ if (!PG_GetSurfaceDetails (surf , & format , & palette )) {
292
301
PyErr_SetString (pgExc_SDLError , SDL_GetError ());
293
302
return -1 ;
294
303
}
304
+
295
305
/* Select appropriate color plane element within the pixel */
296
306
switch (view_kind ) {
297
307
case PXC_VIEWKIND_RED :
@@ -344,7 +354,7 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf,
344
354
for (z = 0 ; z < pixelsize ; ++ z ) {
345
355
pixel .bytes [dz_pix + z ] = src [dx_src * x + dy_src * y + z ];
346
356
}
347
- SDL_GetRGBA (pixel .value , format , & r , & g , & b , & a );
357
+ PG_GetRGBA (pixel .value , format , palette , & r , & g , & b , & a );
348
358
dst [dx_dst * x + dy_dst * y ] = * element ;
349
359
for (z = 1 ; z < intsize ; ++ z ) {
350
360
dst [dx_dst * x + dy_dst * y + dz_dst * z ] = 0 ;
@@ -369,7 +379,6 @@ _copy_colorplane(Py_buffer *view_p, SDL_Surface *surf,
369
379
static int
370
380
_copy_unmapped (Py_buffer * view_p , SDL_Surface * surf )
371
381
{
372
- SDL_PixelFormat * format = surf -> format ;
373
382
int pixelsize = PG_SURF_BytesPerPixel (surf );
374
383
int intsize = (int )view_p -> itemsize ;
375
384
char * src = (char * )surf -> pixels ;
@@ -414,12 +423,21 @@ _copy_unmapped(Py_buffer *view_p, SDL_Surface *surf)
414
423
dz_dst = -1 ;
415
424
}
416
425
#endif
426
+
427
+ PG_PixelFormat * format ;
428
+ SDL_Palette * palette ;
429
+
430
+ if (!PG_GetSurfaceDetails (surf , & format , & palette )) {
431
+ PyErr_SetString (pgExc_SDLError , SDL_GetError ());
432
+ return -1 ;
433
+ }
434
+
417
435
for (x = 0 ; x < w ; ++ x ) {
418
436
for (y = 0 ; y < h ; ++ y ) {
419
437
for (z = 0 ; z < pixelsize ; ++ z ) {
420
438
pixel .bytes [dz_pix + z ] = src [dx_src * x + dy_src * y + z ];
421
439
}
422
- SDL_GetRGB (pixel .value , format , & r , & g , & b );
440
+ PG_GetRGB (pixel .value , format , palette , & r , & g , & b );
423
441
dst [dx_dst * x + dy_dst * y ] = r ;
424
442
for (z = 1 ; z < intsize ; ++ z ) {
425
443
dst [dx_dst * x + dy_dst * y + dz_dst * z ] = 0 ;
@@ -469,16 +487,14 @@ array_to_surface(PyObject *self, PyObject *arg)
469
487
Py_buffer * view_p = (Py_buffer * )& pg_view ;
470
488
char * array_data ;
471
489
SDL_Surface * surf ;
472
- SDL_PixelFormat * format ;
473
490
int loopx , loopy ;
474
491
Py_ssize_t stridex , stridey , stridez = 0 , stridez2 = 0 , sizex , sizey ;
475
- int Rloss , Gloss , Bloss , Rshift , Gshift , Bshift ;
492
+ int Rloss , Gloss , Bloss , Aloss , Rshift , Gshift , Bshift , Ashift ;
476
493
477
494
if (!PyArg_ParseTuple (arg , "O!O" , & pgSurface_Type , & surfobj , & arrayobj )) {
478
495
return NULL ;
479
496
}
480
497
surf = pgSurface_AsSurface (surfobj );
481
- format = surf -> format ;
482
498
483
499
if (pgObject_GetBuffer (arrayobj , & pg_view , PyBUF_RECORDS_RO )) {
484
500
return 0 ;
@@ -507,12 +523,21 @@ array_to_surface(PyObject *self, PyObject *arg)
507
523
}
508
524
sizex = view_p -> shape [0 ];
509
525
sizey = view_p -> shape [1 ];
510
- Rloss = format -> Rloss ;
511
- Gloss = format -> Gloss ;
512
- Bloss = format -> Bloss ;
526
+
527
+ PG_PixelFormat * format ;
528
+ SDL_Palette * palette ;
529
+ if (!PG_GetSurfaceDetails (surf , & format , & palette )) {
530
+ return RAISE (pgExc_SDLError , SDL_GetError ());
531
+ }
532
+
513
533
Rshift = format -> Rshift ;
514
534
Gshift = format -> Gshift ;
515
535
Bshift = format -> Bshift ;
536
+ Ashift = format -> Ashift ;
537
+ Rloss = PG_FORMAT_R_LOSS (format );
538
+ Gloss = PG_FORMAT_G_LOSS (format );
539
+ Bloss = PG_FORMAT_B_LOSS (format );
540
+ Aloss = PG_FORMAT_A_LOSS (format );
516
541
517
542
/* Do any required broadcasting. */
518
543
if (sizex == 1 ) {
@@ -593,7 +618,7 @@ array_to_surface(PyObject *self, PyObject *arg)
593
618
else {
594
619
Uint16 alpha = 0 ;
595
620
if (format -> Amask ) {
596
- alpha = 255 >> format -> Aloss << format -> Ashift ;
621
+ alpha = 255 >> Aloss << Ashift ;
597
622
}
598
623
switch (view_p -> itemsize ) {
599
624
case sizeof (Uint8 ):
@@ -713,7 +738,7 @@ array_to_surface(PyObject *self, PyObject *arg)
713
738
else {
714
739
Uint32 alpha = 0 ;
715
740
if (format -> Amask ) {
716
- alpha = 255 >> format -> Aloss << format -> Ashift ;
741
+ alpha = 255 >> Aloss << Ashift ;
717
742
}
718
743
switch (view_p -> itemsize ) {
719
744
case sizeof (Uint8 ):
@@ -839,7 +864,6 @@ map_array(PyObject *self, PyObject *args)
839
864
PyObject * src_array ;
840
865
PyObject * tar_array ;
841
866
pgSurfaceObject * format_surf ;
842
- SDL_PixelFormat * format ;
843
867
pg_buffer src_pg_view ;
844
868
Py_buffer * src_view_p ;
845
869
Uint8 is_src_alloc = 0 ;
@@ -962,7 +986,14 @@ map_array(PyObject *self, PyObject *args)
962
986
963
987
/* Determine source and destination pixel formats
964
988
*/
965
- format = pgSurface_AsSurface (format_surf )-> format ;
989
+
990
+ PG_PixelFormat * format ;
991
+ SDL_Palette * palette ;
992
+ if (!PG_GetSurfaceDetails (pgSurface_AsSurface (format_surf ), & format ,
993
+ & palette )) {
994
+ return RAISE (pgExc_SDLError , SDL_GetError ());
995
+ }
996
+
966
997
pix_bytesize = PG_FORMAT_BytesPerPixel (format );
967
998
if (tar_itemsize < pix_bytesize ) {
968
999
PyErr_SetString (PyExc_ValueError ,
@@ -1078,8 +1109,8 @@ map_array(PyObject *self, PyObject *args)
1078
1109
else if (dim == topdim ) {
1079
1110
/* Next iteration of inner most loop: copy pixel
1080
1111
*/
1081
- pixel .value = SDL_MapRGB (format , src [ src_red ] , src [src_green ],
1082
- src [src_blue ]);
1112
+ pixel .value = PG_MapRGB (format , palette , src [src_red ],
1113
+ src [ src_green ], src [src_blue ]);
1083
1114
/* Bytes are copied from the pixel in most to least significant
1084
1115
* byte order. If destination bytes get overwritten, when the
1085
1116
* destination size is less than 4 bytes, only zero pad bytes
@@ -1170,11 +1201,16 @@ make_surface(PyObject *self, PyObject *arg)
1170
1201
pgBuffer_Release (& pg_view );
1171
1202
return RAISE (pgExc_SDLError , SDL_GetError ());
1172
1203
}
1204
+ #if SDL_VERSION_ATLEAST (3 , 0 , 0 )
1205
+ SDL_Palette * palette = SDL_GetSurfacePalette (surf );
1206
+ #else
1207
+ SDL_Palette * palette = surf -> format -> palette ;
1208
+ #endif
1173
1209
if (SDL_ISPIXELFORMAT_INDEXED (PG_SURF_FORMATENUM (surf ))) {
1174
1210
/* Give the surface something other than an all white palette.
1175
1211
* */
1176
- if (SDL_SetPaletteColors (surf -> format -> palette , default_palette_colors ,
1177
- 0 , default_palette_size - 1 ) != 0 ) {
1212
+ if (SDL_SetPaletteColors (palette , default_palette_colors , 0 ,
1213
+ default_palette_size - 1 ) != 0 ) {
1178
1214
PyErr_SetString (pgExc_SDLError , SDL_GetError ());
1179
1215
SDL_FreeSurface (surf );
1180
1216
return 0 ;
0 commit comments