From 4c82b5843e15665b609a34d8403c1b55943a64ad Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 5 Mar 2025 23:19:41 -0800 Subject: [PATCH] Added support for SDL_PIXELFORMAT_MJPG to SDL_DuplicateSurface() Fixes https://github.com/libsdl-org/SDL/issues/12472 --- src/video/SDL_surface.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c index 945fda606d..62dc5f04a0 100644 --- a/src/video/SDL_surface.c +++ b/src/video/SDL_surface.c @@ -104,6 +104,11 @@ bool SDL_CalculateSurfaceSize(SDL_PixelFormat format, int width, int height, siz } if (SDL_ISPIXELFORMAT_FOURCC(format)) { + if (format == SDL_PIXELFORMAT_MJPG) { + // We don't know in advance what it will be, we'll figure it out later. + return true; + } + if (!SDL_CalculateYUVSize(format, width, height, &sz, &p)) { // Overflow... return false; @@ -214,7 +219,7 @@ SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format) return NULL; } - if (surface->w && surface->h) { + if (surface->w && surface->h && format != SDL_PIXELFORMAT_MJPG) { surface->flags &= ~SDL_SURFACE_PREALLOCATED; surface->pixels = SDL_aligned_alloc(SDL_GetSIMDAlignment(), size); if (!surface->pixels) { @@ -1917,7 +1922,17 @@ SDL_Surface *SDL_ConvertSurfaceAndColorspace(SDL_Surface *surface, SDL_PixelForm SDL_SetSurfaceColorspace(convert, colorspace); if (SDL_ISPIXELFORMAT_FOURCC(format) || SDL_ISPIXELFORMAT_FOURCC(surface->format)) { - if (!SDL_ConvertPixelsAndColorspace(surface->w, surface->h, surface->format, src_colorspace, src_properties, surface->pixels, surface->pitch, convert->format, colorspace, props, convert->pixels, convert->pitch)) { + if (surface->format == SDL_PIXELFORMAT_MJPG && format == SDL_PIXELFORMAT_MJPG) { + // Just do a straight pixel copy of the JPEG image + size_t size = (size_t)surface->pitch; + convert->pixels = SDL_malloc(size); + if (!convert->pixels) { + goto error; + } + convert->pitch = surface->pitch; + SDL_memcpy(convert->pixels, surface->pixels, size); + + } else if (!SDL_ConvertPixelsAndColorspace(surface->w, surface->h, surface->format, src_colorspace, src_properties, surface->pixels, surface->pitch, convert->format, colorspace, props, convert->pixels, convert->pitch)) { goto error; }