Skip to content

Commit cae6b44

Browse files
committed
- simplication and factorization around CalculateSize and Pitch, RGB/YUV
- update SDL_CalculateYUVSize pitch to size_t
1 parent c82cca0 commit cae6b44

File tree

4 files changed

+67
-62
lines changed

4 files changed

+67
-62
lines changed

src/video/SDL_pixels_c.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
/* Pixel format functions */
3232
extern int SDL_InitFormat(SDL_PixelFormat *format, Uint32 pixel_format);
33+
extern int SDL_CalculateSize(Uint32 format, int width, int height, size_t *size, size_t *pitch, SDL_bool minimalPitch);
3334

3435
/* Blit mapping functions */
3536
extern SDL_BlitMap *SDL_AllocBlitMap(void);

src/video/SDL_surface.c

+63-59
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,71 @@ SDL_COMPILE_TIME_ASSERT(can_indicate_overflow, SDL_SIZE_MAX > SDL_MAX_SINT32);
4242
*
4343
* for FOURCC, use SDL_CalculateYUVSize()
4444
*/
45-
static size_t
46-
SDL_CalculatePitch(Uint32 format, size_t width, SDL_bool minimal)
45+
static int
46+
SDL_CalculateRGBSize(Uint32 format, size_t width, size_t height, size_t *size, size_t *pitch, SDL_bool minimal)
4747
{
48-
size_t pitch;
49-
5048
if (SDL_BITSPERPIXEL(format) >= 8) {
51-
if (SDL_size_mul_overflow(width, SDL_BYTESPERPIXEL(format), &pitch)) {
52-
return SDL_SIZE_MAX;
49+
if (SDL_size_mul_overflow(width, SDL_BYTESPERPIXEL(format), pitch)) {
50+
return -1;
5351
}
5452
} else {
55-
if (SDL_size_mul_overflow(width, SDL_BITSPERPIXEL(format), &pitch)) {
56-
return SDL_SIZE_MAX;
53+
if (SDL_size_mul_overflow(width, SDL_BITSPERPIXEL(format), pitch)) {
54+
return -1;
5755
}
58-
if (SDL_size_add_overflow(pitch, 7, &pitch)) {
59-
return SDL_SIZE_MAX;
56+
if (SDL_size_add_overflow(*pitch, 7, pitch)) {
57+
return -1;
6058
}
61-
pitch /= 8;
59+
*pitch /= 8;
6260
}
6361
if (!minimal) {
6462
/* 4-byte aligning for speed */
65-
if (SDL_size_add_overflow(pitch, 3, &pitch)) {
66-
return SDL_SIZE_MAX;
63+
if (SDL_size_add_overflow(*pitch, 3, pitch)) {
64+
return -1;
6765
}
68-
pitch &= ~3;
66+
*pitch &= ~3;
67+
}
68+
69+
if (SDL_size_mul_overflow(height, *pitch, size)) {
70+
/* Overflow... */
71+
return -1;
6972
}
70-
return pitch;
73+
74+
return 0;
75+
}
76+
77+
int SDL_CalculateSize(Uint32 format, int width, int height, size_t *size, size_t *pitch, SDL_bool minimalPitch)
78+
{
79+
size_t p = 0, sz = 0;
80+
81+
if (size) {
82+
*size = 0;
83+
}
84+
85+
if (pitch) {
86+
*pitch = 0;
87+
}
88+
89+
if (SDL_ISPIXELFORMAT_FOURCC(format)) {
90+
if (SDL_CalculateYUVSize(format, width, height, &sz, &p) < 0) {
91+
/* Overflow... */
92+
return -1;
93+
}
94+
} else {
95+
if (SDL_CalculateRGBSize(format, width, height, &sz, &p, minimalPitch) < 0) {
96+
/* Overflow... */
97+
return -1;
98+
}
99+
}
100+
101+
if (size) {
102+
*size = sz;
103+
}
104+
105+
if (pitch) {
106+
*pitch = p;
107+
}
108+
109+
return 0;
71110
}
72111

73112
/*
@@ -77,7 +116,7 @@ SDL_CalculatePitch(Uint32 format, size_t width, SDL_bool minimal)
77116
SDL_Surface *
78117
SDL_CreateSurface(int width, int height, Uint32 format)
79118
{
80-
size_t pitch;
119+
size_t pitch, size;
81120
SDL_Surface *surface;
82121

83122
if (width < 0) {
@@ -90,21 +129,10 @@ SDL_CreateSurface(int width, int height, Uint32 format)
90129
return NULL;
91130
}
92131

93-
if (SDL_ISPIXELFORMAT_FOURCC(format)) {
94-
int p;
95-
if (SDL_CalculateYUVSize(format, width, height, NULL, &p) < 0) {
96-
/* Overflow... */
97-
SDL_OutOfMemory();
98-
return NULL;
99-
}
100-
pitch = p;
101-
} else {
102-
pitch = SDL_CalculatePitch(format, width, SDL_FALSE);
103-
if (pitch > SDL_MAX_SINT32) {
104-
/* Overflow... */
105-
SDL_OutOfMemory();
106-
return NULL;
107-
}
132+
if (SDL_CalculateSize(format, width, height, &size, &pitch, SDL_FALSE /* not minimal pitch */) < 0) {
133+
/* Overflow... */
134+
SDL_OutOfMemory();
135+
return NULL;
108136
}
109137

110138
/* Allocate the surface */
@@ -146,25 +174,6 @@ SDL_CreateSurface(int width, int height, Uint32 format)
146174

147175
/* Get the pixels */
148176
if (surface->w && surface->h) {
149-
size_t size;
150-
if (SDL_ISPIXELFORMAT_FOURCC(surface->format->format)) {
151-
/* Get correct size and pitch for YUV formats */
152-
if (SDL_CalculateYUVSize(surface->format->format, surface->w, surface->h, &size, &surface->pitch) < 0) {
153-
/* Overflow... */
154-
SDL_DestroySurface(surface);
155-
SDL_OutOfMemory();
156-
return NULL;
157-
}
158-
} else {
159-
/* Assumptions checked in surface_size_assumptions assert above */
160-
if (SDL_size_mul_overflow(surface->h, surface->pitch, &size)) {
161-
/* Overflow... */
162-
SDL_DestroySurface(surface);
163-
SDL_OutOfMemory();
164-
return NULL;
165-
}
166-
}
167-
168177
surface->pixels = SDL_aligned_alloc(SDL_SIMDGetAlignment(), size);
169178
if (!surface->pixels) {
170179
SDL_DestroySurface(surface);
@@ -219,15 +228,10 @@ SDL_CreateSurfaceFrom(void *pixels,
219228
} else {
220229
size_t minimalPitch;
221230

222-
if (SDL_ISPIXELFORMAT_FOURCC(format)) {
223-
int p;
224-
if (SDL_CalculateYUVSize(format, width, height, NULL, &p) < 0) {
225-
SDL_InvalidParamError("pitch");
226-
return NULL;
227-
}
228-
minimalPitch = p;
229-
} else {
230-
minimalPitch = SDL_CalculatePitch(format, width, SDL_TRUE);
231+
if (SDL_CalculateSize(format, width, height, NULL, &minimalPitch, SDL_TRUE /* minimal pitch */) < 0) {
232+
/* Overflow... */
233+
SDL_OutOfMemory();
234+
return NULL;
231235
}
232236

233237
if (pitch < 0 || (size_t)pitch < minimalPitch) {

src/video/SDL_yuv.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ SDL_YUV_CONVERSION_MODE SDL_GetYUVConversionModeForResolution(int width, int hei
6262
*
6363
* return 0 on success, -1 on error
6464
*/
65-
int SDL_CalculateYUVSize(Uint32 format, int w, int h, size_t *size, int *pitch)
65+
int SDL_CalculateYUVSize(Uint32 format, int w, int h, size_t *size, size_t *pitch)
6666
{
6767
#if SDL_HAVE_YUV
6868
int sz_plane = 0, sz_plane_chroma = 0, sz_plane_packed = 0;
@@ -141,7 +141,7 @@ int SDL_CalculateYUVSize(Uint32 format, int w, int h, size_t *size, int *pitch)
141141
if (SDL_size_mul_overflow(p1, 4, &p2) < 0) {
142142
return -1;
143143
}
144-
*pitch = (int) p2;
144+
*pitch = p2;
145145
}
146146

147147
if (size) {

src/video/SDL_yuv_c.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ extern int SDL_ConvertPixels_RGB_to_YUV(int width, int height, Uint32 src_format
3131
extern int SDL_ConvertPixels_YUV_to_YUV(int width, int height, Uint32 src_format, const void *src, int src_pitch, Uint32 dst_format, void *dst, int dst_pitch);
3232

3333

34-
extern int SDL_CalculateYUVSize(Uint32 format, int w, int h, size_t *size, int *pitch);
34+
extern int SDL_CalculateYUVSize(Uint32 format, int w, int h, size_t *size, size_t *pitch);
3535

3636
#endif /* SDL_yuv_c_h_ */

0 commit comments

Comments
 (0)