From 4fee09f91cf87e3655ea1cbf2f1581d1a5e6df03 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 6 Jun 2022 18:50:36 -0700
Subject: [PATCH] Avoid copying the pixel data in the QOI image loader
Fixes https://github.com/libsdl-org/SDL_image/pull/278
---
IMG_qoi.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/IMG_qoi.c b/IMG_qoi.c
index 6533bab7..e109445a 100644
--- a/IMG_qoi.c
+++ b/IMG_qoi.c
@@ -88,19 +88,20 @@ SDL_Surface *IMG_LoadQOI_RW(SDL_RWops *src)
return NULL;
}
- surface = SDL_CreateRGBSurfaceWithFormat(0,
- image_info.width,
- image_info.height,
- 32,
- SDL_PIXELFORMAT_RGBA32);
-
+ surface = SDL_CreateRGBSurfaceWithFormatFrom(pixel_data,
+ image_info.width,
+ image_info.height,
+ 32,
+ (image_info.width * 4),
+ SDL_PIXELFORMAT_RGBA32);
if ( !surface ) {
SDL_free(pixel_data);
IMG_SetError("Couldn't create SDL_Surface");
return NULL;
}
- SDL_memcpy(surface->pixels, pixel_data, image_info.width*image_info.height*4);
+ /* Let SDL manage the memory now */
+ surface->flags &= ~SDL_PREALLOC;
return surface;
}