Skip to content

Commit a05d084

Browse files
authored
Merge pull request #3341 from pygame-community/ankith26-camera-sdl3
Port camera to SDL3
2 parents 0d8d379 + 0da1eaa commit a05d084

File tree

4 files changed

+100
-102
lines changed

4 files changed

+100
-102
lines changed

src_c/_camera.c

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,16 @@ surf_colorspace(PyObject *self, PyObject *arg)
127127
if (PG_SURF_BitsPerPixel(surf) != PG_SURF_BitsPerPixel(newsurf))
128128
return RAISE(PyExc_ValueError, "Surfaces not the same depth");
129129

130+
PG_PixelFormat *src_fmt = PG_GetSurfaceFormat(surf);
131+
if (!src_fmt) {
132+
return RAISE(pgExc_SDLError, SDL_GetError());
133+
}
134+
130135
SDL_LockSurface(newsurf);
131136
pgSurface_Lock(surfobj);
132137

133138
Py_BEGIN_ALLOW_THREADS;
134-
colorspace(surf, newsurf, cspace);
139+
colorspace(surf, src_fmt, newsurf, cspace);
135140
Py_END_ALLOW_THREADS;
136141

137142
pgSurface_Unlock(surfobj);
@@ -473,23 +478,22 @@ camera_get_raw(pgCameraObject *self, PyObject *_null)
473478
/* converts from rgb Surface to yuv or hsv */
474479
/* TODO: Allow for conversion from yuv and hsv to all */
475480
void
476-
colorspace(SDL_Surface *src, SDL_Surface *dst, int cspace)
481+
colorspace(SDL_Surface *src, PG_PixelFormat *src_fmt, SDL_Surface *dst,
482+
int cspace)
477483
{
478484
switch (cspace) {
479485
case YUV_OUT:
480-
rgb_to_yuv(src->pixels, dst->pixels, src->h * src->w, 0,
481-
src->format);
486+
rgb_to_yuv(src->pixels, dst->pixels, src->h * src->w, 0, src_fmt);
482487
break;
483488
case HSV_OUT:
484-
rgb_to_hsv(src->pixels, dst->pixels, src->h * src->w, 0,
485-
src->format);
489+
rgb_to_hsv(src->pixels, dst->pixels, src->h * src->w, 0, src_fmt);
486490
break;
487491
}
488492
}
489493

490494
/* converts pretty directly if its already RGB24 */
491495
void
492-
rgb24_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
496+
rgb24_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format)
493497
{
494498
Uint8 *s = (Uint8 *)src;
495499
Uint8 *d8;
@@ -501,9 +505,9 @@ rgb24_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
501505
rshift = format->Rshift;
502506
gshift = format->Gshift;
503507
bshift = format->Bshift;
504-
rloss = format->Rloss;
505-
gloss = format->Gloss;
506-
bloss = format->Bloss;
508+
rloss = PG_FORMAT_R_LOSS(format);
509+
gloss = PG_FORMAT_G_LOSS(format);
510+
bloss = PG_FORMAT_B_LOSS(format);
507511

508512
switch (PG_FORMAT_BytesPerPixel(format)) {
509513
case 1:
@@ -551,7 +555,7 @@ rgb24_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
551555
/* slight variation on rgb24_to_rgb, just drops the 4th byte of each pixel,
552556
* changes the R, G, B ordering. */
553557
void
554-
bgr32_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
558+
bgr32_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format)
555559
{
556560
Uint8 *s = (Uint8 *)src;
557561
Uint8 *d8;
@@ -563,9 +567,9 @@ bgr32_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
563567
rshift = format->Rshift;
564568
gshift = format->Gshift;
565569
bshift = format->Bshift;
566-
rloss = format->Rloss;
567-
gloss = format->Gloss;
568-
bloss = format->Bloss;
570+
rloss = PG_FORMAT_R_LOSS(format);
571+
gloss = PG_FORMAT_G_LOSS(format);
572+
bloss = PG_FORMAT_B_LOSS(format);
569573

570574
switch (PG_FORMAT_BytesPerPixel(format)) {
571575
case 1:
@@ -616,7 +620,7 @@ bgr32_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
616620
/* converts packed rgb to packed hsv. formulas modified from wikipedia */
617621
void
618622
rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
619-
SDL_PixelFormat *format)
623+
PG_PixelFormat *format)
620624
{
621625
Uint8 *s8, *d8;
622626
Uint16 *s16, *d16;
@@ -633,9 +637,9 @@ rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
633637
rshift = format->Rshift;
634638
gshift = format->Gshift;
635639
bshift = format->Bshift;
636-
rloss = format->Rloss;
637-
gloss = format->Gloss;
638-
bloss = format->Bloss;
640+
rloss = PG_FORMAT_R_LOSS(format);
641+
gloss = PG_FORMAT_G_LOSS(format);
642+
bloss = PG_FORMAT_B_LOSS(format);
639643

640644
/* you could stick the if statement inside the loop, but I'm sacrificing a
641645
a few hundred bytes for a little performance */
@@ -779,7 +783,7 @@ rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
779783
this has a full range of 0-255 for Y, not 16-235. Formulas from wikipedia */
780784
void
781785
rgb_to_yuv(const void *src, void *dst, int length, unsigned long source,
782-
SDL_PixelFormat *format)
786+
PG_PixelFormat *format)
783787
{
784788
Uint8 *s8, *d8;
785789
Uint16 *s16, *d16;
@@ -797,9 +801,9 @@ rgb_to_yuv(const void *src, void *dst, int length, unsigned long source,
797801
rshift = format->Rshift;
798802
gshift = format->Gshift;
799803
bshift = format->Bshift;
800-
rloss = format->Rloss;
801-
gloss = format->Gloss;
802-
bloss = format->Bloss;
804+
rloss = PG_FORMAT_R_LOSS(format);
805+
gloss = PG_FORMAT_G_LOSS(format);
806+
bloss = PG_FORMAT_B_LOSS(format);
803807

804808
if (source == V4L2_PIX_FMT_RGB444 || source == V4L2_PIX_FMT_RGB24 ||
805809
source == V4L2_PIX_FMT_XBGR32) {
@@ -916,7 +920,7 @@ rgb_to_yuv(const void *src, void *dst, int length, unsigned long source,
916920

917921
/* Converts from rgb444 (R444) to rgb24 (RGB3) */
918922
void
919-
rgb444_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
923+
rgb444_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format)
920924
{
921925
Uint8 *s, *d8;
922926
Uint16 *d16;
@@ -928,9 +932,9 @@ rgb444_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
928932
rshift = format->Rshift;
929933
gshift = format->Gshift;
930934
bshift = format->Bshift;
931-
rloss = format->Rloss;
932-
gloss = format->Gloss;
933-
bloss = format->Bloss;
935+
rloss = PG_FORMAT_R_LOSS(format);
936+
gloss = PG_FORMAT_G_LOSS(format);
937+
bloss = PG_FORMAT_B_LOSS(format);
934938

935939
switch (PG_FORMAT_BytesPerPixel(format)) {
936940
case 1:
@@ -980,7 +984,7 @@ rgb444_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
980984
/* colorspace conversion routine from libv4l. Licensed LGPL 2.1
981985
(C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl> */
982986
void
983-
yuyv_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
987+
yuyv_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format)
984988
{
985989
Uint8 *s, *d8;
986990
Uint16 *d16;
@@ -992,9 +996,9 @@ yuyv_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
992996
rshift = format->Rshift;
993997
gshift = format->Gshift;
994998
bshift = format->Bshift;
995-
rloss = format->Rloss;
996-
gloss = format->Gloss;
997-
bloss = format->Bloss;
999+
rloss = PG_FORMAT_R_LOSS(format);
1000+
gloss = PG_FORMAT_G_LOSS(format);
1001+
bloss = PG_FORMAT_B_LOSS(format);
9981002

9991003
d8 = (Uint8 *)dst;
10001004
d16 = (Uint16 *)dst;
@@ -1059,7 +1063,7 @@ yuyv_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
10591063

10601064
/* turn yuyv into packed yuv. */
10611065
void
1062-
yuyv_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format)
1066+
yuyv_to_yuv(const void *src, void *dst, int length, PG_PixelFormat *format)
10631067
{
10641068
Uint8 *s, *d8;
10651069
Uint8 y1, u, y2, v;
@@ -1071,9 +1075,9 @@ yuyv_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format)
10711075
rshift = format->Rshift;
10721076
gshift = format->Gshift;
10731077
bshift = format->Bshift;
1074-
rloss = format->Rloss;
1075-
gloss = format->Gloss;
1076-
bloss = format->Bloss;
1078+
rloss = PG_FORMAT_R_LOSS(format);
1079+
gloss = PG_FORMAT_G_LOSS(format);
1080+
bloss = PG_FORMAT_B_LOSS(format);
10771081
s = (Uint8 *)src;
10781082

10791083
switch (PG_FORMAT_BytesPerPixel(format)) {
@@ -1133,7 +1137,7 @@ yuyv_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format)
11331137

11341138
/* cribbed from above, but modified for uyvy ordering */
11351139
void
1136-
uyvy_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
1140+
uyvy_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format)
11371141
{
11381142
Uint8 *s, *d8;
11391143
Uint16 *d16;
@@ -1145,9 +1149,9 @@ uyvy_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
11451149
rshift = format->Rshift;
11461150
gshift = format->Gshift;
11471151
bshift = format->Bshift;
1148-
rloss = format->Rloss;
1149-
gloss = format->Gloss;
1150-
bloss = format->Bloss;
1152+
rloss = PG_FORMAT_R_LOSS(format);
1153+
gloss = PG_FORMAT_G_LOSS(format);
1154+
bloss = PG_FORMAT_B_LOSS(format);
11511155

11521156
d8 = (Uint8 *)dst;
11531157
d16 = (Uint16 *)dst;
@@ -1212,7 +1216,7 @@ uyvy_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format)
12121216
}
12131217
/* turn uyvy into packed yuv. */
12141218
void
1215-
uyvy_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format)
1219+
uyvy_to_yuv(const void *src, void *dst, int length, PG_PixelFormat *format)
12161220
{
12171221
Uint8 *s, *d8;
12181222
Uint8 y1, u, y2, v;
@@ -1224,9 +1228,9 @@ uyvy_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format)
12241228
rshift = format->Rshift;
12251229
gshift = format->Gshift;
12261230
bshift = format->Bshift;
1227-
rloss = format->Rloss;
1228-
gloss = format->Gloss;
1229-
bloss = format->Bloss;
1231+
rloss = PG_FORMAT_R_LOSS(format);
1232+
gloss = PG_FORMAT_G_LOSS(format);
1233+
bloss = PG_FORMAT_B_LOSS(format);
12301234
s = (Uint8 *)src;
12311235

12321236
switch (PG_FORMAT_BytesPerPixel(format)) {
@@ -1312,7 +1316,7 @@ uyvy_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format)
13121316
/* TODO: Certainly not the most efficient way of doing this conversion. */
13131317
void
13141318
sbggr8_to_rgb(const void *src, void *dst, int width, int height,
1315-
SDL_PixelFormat *format)
1319+
PG_PixelFormat *format)
13161320
{
13171321
Uint8 *rawpt, *d8;
13181322
Uint16 *d16;
@@ -1324,9 +1328,9 @@ sbggr8_to_rgb(const void *src, void *dst, int width, int height,
13241328
rshift = format->Rshift;
13251329
gshift = format->Gshift;
13261330
bshift = format->Bshift;
1327-
rloss = format->Rloss;
1328-
gloss = format->Gloss;
1329-
bloss = format->Bloss;
1331+
rloss = PG_FORMAT_R_LOSS(format);
1332+
gloss = PG_FORMAT_G_LOSS(format);
1333+
bloss = PG_FORMAT_B_LOSS(format);
13301334

13311335
d8 = (Uint8 *)dst;
13321336
d16 = (Uint16 *)dst;
@@ -1432,7 +1436,7 @@ sbggr8_to_rgb(const void *src, void *dst, int width, int height,
14321436
*/
14331437
void
14341438
yuv420_to_rgb(const void *src, void *dst, int width, int height,
1435-
SDL_PixelFormat *format)
1439+
PG_PixelFormat *format)
14361440
{
14371441
int rshift, gshift, bshift, rloss, gloss, bloss, i, j, u1, v1, rg, y;
14381442
const Uint8 *y1, *y2, *u, *v;
@@ -1443,9 +1447,9 @@ yuv420_to_rgb(const void *src, void *dst, int width, int height,
14431447
rshift = format->Rshift;
14441448
gshift = format->Gshift;
14451449
bshift = format->Bshift;
1446-
rloss = format->Rloss;
1447-
gloss = format->Gloss;
1448-
bloss = format->Bloss;
1450+
rloss = PG_FORMAT_R_LOSS(format);
1451+
gloss = PG_FORMAT_G_LOSS(format);
1452+
bloss = PG_FORMAT_B_LOSS(format);
14491453

14501454
/* see http://en.wikipedia.org/wiki/YUV for an explanation of YUV420 */
14511455
y1 = (Uint8 *)src;
@@ -1632,7 +1636,7 @@ yuv420_to_rgb(const void *src, void *dst, int width, int height,
16321636
/* turn yuv420 into packed yuv. */
16331637
void
16341638
yuv420_to_yuv(const void *src, void *dst, int width, int height,
1635-
SDL_PixelFormat *format)
1639+
PG_PixelFormat *format)
16361640
{
16371641
const Uint8 *y1, *y2, *u, *v;
16381642
Uint8 *d8_1, *d8_2;
@@ -1643,9 +1647,9 @@ yuv420_to_yuv(const void *src, void *dst, int width, int height,
16431647
rshift = format->Rshift;
16441648
gshift = format->Gshift;
16451649
bshift = format->Bshift;
1646-
rloss = format->Rloss;
1647-
gloss = format->Gloss;
1648-
bloss = format->Bloss;
1650+
rloss = PG_FORMAT_R_LOSS(format);
1651+
gloss = PG_FORMAT_G_LOSS(format);
1652+
bloss = PG_FORMAT_B_LOSS(format);
16491653

16501654
d8_1 = (Uint8 *)dst;
16511655
d8_2 = d8_1 + (PG_FORMAT_BytesPerPixel(format) == 3 ? width * 3 : 3);

src_c/camera.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,36 +166,37 @@ typedef struct pgCameraObject {
166166

167167
/* internal functions for colorspace conversion */
168168
void
169-
colorspace(SDL_Surface *src, SDL_Surface *dst, int cspace);
169+
colorspace(SDL_Surface *src, PG_PixelFormat *src_fmt, SDL_Surface *dst,
170+
int cspace);
170171
void
171-
rgb24_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format);
172+
rgb24_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format);
172173
void
173-
bgr32_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format);
174+
bgr32_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format);
174175
void
175-
rgb444_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format);
176+
rgb444_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format);
176177
void
177178
rgb_to_yuv(const void *src, void *dst, int length, unsigned long source,
178-
SDL_PixelFormat *format);
179+
PG_PixelFormat *format);
179180
void
180181
rgb_to_hsv(const void *src, void *dst, int length, unsigned long source,
181-
SDL_PixelFormat *format);
182+
PG_PixelFormat *format);
182183
void
183-
yuyv_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format);
184+
yuyv_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format);
184185
void
185-
yuyv_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format);
186+
yuyv_to_yuv(const void *src, void *dst, int length, PG_PixelFormat *format);
186187
void
187-
uyvy_to_rgb(const void *src, void *dst, int length, SDL_PixelFormat *format);
188+
uyvy_to_rgb(const void *src, void *dst, int length, PG_PixelFormat *format);
188189
void
189-
uyvy_to_yuv(const void *src, void *dst, int length, SDL_PixelFormat *format);
190+
uyvy_to_yuv(const void *src, void *dst, int length, PG_PixelFormat *format);
190191
void
191192
sbggr8_to_rgb(const void *src, void *dst, int width, int height,
192-
SDL_PixelFormat *format);
193+
PG_PixelFormat *format);
193194
void
194195
yuv420_to_rgb(const void *src, void *dst, int width, int height,
195-
SDL_PixelFormat *format);
196+
PG_PixelFormat *format);
196197
void
197198
yuv420_to_yuv(const void *src, void *dst, int width, int height,
198-
SDL_PixelFormat *format);
199+
PG_PixelFormat *format);
199200

200201
#if defined(__unix__)
201202
/* internal functions specific to v4l2 */

0 commit comments

Comments
 (0)