Fixed bug 4188 - Software renderer SDL_RenderCopyEx blits corrupt image under certain cases
Sylvain
Re-opening this issue.
It fixes the test-case, but it introduces a regression with another bug (bug #4313).
So here's a new patch that activate cropping of the source surface to solve the issue.
It also reverts the wrong changeset.
It prevents unneeded colorkey error message.
3 SDL_rotate.c: rotates 32bit or 8bit surfaces
5 Shamelessly stolen from SDL_gfx by Andreas Schiffler. Original copyright follows:
7 Copyright (C) 2001-2011 Andreas Schiffler
9 This software is provided 'as-is', without any express or implied
10 warranty. In no event will the authors be held liable for any damages
11 arising from the use of this software.
13 Permission is granted to anyone to use this software for any purpose,
14 including commercial applications, and to alter it and redistribute it
15 freely, subject to the following restrictions:
17 1. The origin of this software must not be misrepresented; you must not
18 claim that you wrote the original software. If you use this software
19 in a product, an acknowledgment in the product documentation would be
20 appreciated but is not required.
22 2. Altered source versions must be plainly marked as such, and must not be
23 misrepresented as being the original software.
25 3. This notice may not be removed or altered from any source
28 Andreas Schiffler -- aschiffler at ferzkopp dot net
31 #include "../../SDL_internal.h"
33 #if defined(__WIN32__)
34 #include "../../core/windows/SDL_windows.h"
41 #include "SDL_rotate.h"
43 /* ---- Internally used structures */
46 \brief A 32 bit RGBA pixel.
48 typedef struct tColorRGBA {
56 \brief A 8bit Y/palette pixel.
58 typedef struct tColorY {
63 \brief Returns maximum of two numbers a and b.
65 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
68 \brief Number of guard rows added to destination surfaces.
70 This is a simple but effective workaround for observed issues.
71 These rows allocate extra memory and are then hidden from the surface.
72 Rows are added to the end of destination surfaces when they are allocated.
73 This catches any potential overflows which seem to happen with
74 just the right src image dimensions and scale/rotation and can lead
75 to a situation where the program can segfault.
77 #define GUARD_ROWS (2)
80 \brief Returns colorkey info for a surface
83 _colorkey(SDL_Surface *src)
86 if (SDL_HasColorKey(src)) {
87 SDL_GetColorKey(src, &key);
94 \brief Internal target surface sizing function for rotations with trig result return.
96 \param width The source surface width.
97 \param height The source surface height.
98 \param angle The angle to rotate in degrees.
99 \param dstwidth The calculated width of the destination surface.
100 \param dstheight The calculated height of the destination surface.
101 \param cangle The sine of the angle
102 \param sangle The cosine of the angle
106 SDLgfx_rotozoomSurfaceSizeTrig(int width, int height, double angle,
107 int *dstwidth, int *dstheight,
108 double *cangle, double *sangle)
110 /* The trig code below gets the wrong size (due to FP inaccuracy?) when angle is a multiple of 90 degrees */
111 int angle90 = (int)(angle/90);
112 if(angle90 == angle/90) { /* if the angle is a multiple of 90 degrees */
114 if(angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
119 *sangle = angle90 == 1 ? -1 : 1; /* reversed because our rotations are clockwise */
123 *cangle = angle90 == 0 ? 1 : -1;
127 double x, y, cx, cy, sx, sy;
129 int dstwidthhalf, dstheighthalf;
131 * Determine destination width and height by rotating a centered source box
133 radangle = angle * (M_PI / -180.0); /* reverse the angle because our rotations are clockwise */
134 *sangle = SDL_sin(radangle);
135 *cangle = SDL_cos(radangle);
136 x = (double)(width / 2);
137 y = (double)(height / 2);
143 dstwidthhalf = MAX((int)
144 SDL_ceil(MAX(MAX(MAX(SDL_fabs(cx + sy), SDL_fabs(cx - sy)), SDL_fabs(-cx + sy)), SDL_fabs(-cx - sy))), 1);
145 dstheighthalf = MAX((int)
146 SDL_ceil(MAX(MAX(MAX(SDL_fabs(sx + cy), SDL_fabs(sx - cy)), SDL_fabs(-sx + cy)), SDL_fabs(-sx - cy))), 1);
147 *dstwidth = 2 * dstwidthhalf;
148 *dstheight = 2 * dstheighthalf;
152 /* Computes source pointer X/Y increments for a rotation that's a multiple of 90 degrees. */
154 computeSourceIncrements90(SDL_Surface * src, int bpp, int angle, int flipx, int flipy,
155 int *sincx, int *sincy, int *signx, int *signy)
157 int pitch = flipy ? -src->pitch : src->pitch;
161 switch (angle) { /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
162 case 0: *sincx = bpp; *sincy = pitch - src->w * *sincx; *signx = *signy = 1; break;
163 case 1: *sincx = -pitch; *sincy = bpp - *sincx * src->h; *signx = 1; *signy = -1; break;
164 case 2: *sincx = -bpp; *sincy = -src->w * *sincx - pitch; *signx = *signy = -1; break;
165 case 3: default: *sincx = pitch; *sincy = -*sincx * src->h - bpp; *signx = -1; *signy = 1; break;
175 /* Performs a relatively fast rotation/flip when the angle is a multiple of 90 degrees. */
176 #define TRANSFORM_SURFACE_90(pixelType) \
177 int dy, dincy = dst->pitch - dst->w*sizeof(pixelType), sincx, sincy, signx, signy; \
178 Uint8 *sp = (Uint8*)src->pixels, *dp = (Uint8*)dst->pixels, *de; \
180 computeSourceIncrements90(src, sizeof(pixelType), angle, flipx, flipy, &sincx, &sincy, &signx, &signy); \
181 if (signx < 0) sp += (src->w-1)*sizeof(pixelType); \
182 if (signy < 0) sp += (src->h-1)*src->pitch; \
184 for (dy = 0; dy < dst->h; sp += sincy, dp += dincy, dy++) { \
185 if (sincx == sizeof(pixelType)) { /* if advancing src and dest equally, use memcpy */ \
186 SDL_memcpy(dp, sp, dst->w*sizeof(pixelType)); \
187 sp += dst->w*sizeof(pixelType); \
188 dp += dst->w*sizeof(pixelType); \
190 for (de = dp + dst->w*sizeof(pixelType); dp != de; sp += sincx, dp += sizeof(pixelType)) { \
191 *(pixelType*)dp = *(pixelType*)sp; \
197 transformSurfaceRGBA90(SDL_Surface * src, SDL_Surface * dst, int angle, int flipx, int flipy)
199 TRANSFORM_SURFACE_90(tColorRGBA);
203 transformSurfaceY90(SDL_Surface * src, SDL_Surface * dst, int angle, int flipx, int flipy)
205 TRANSFORM_SURFACE_90(tColorY);
208 #undef TRANSFORM_SURFACE_90
211 \brief Internal 32 bit rotozoomer with optional anti-aliasing.
213 Rotates and zooms 32 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
214 parameters by scanning the destination surface and applying optionally anti-aliasing
215 by bilinear interpolation.
216 Assumes src and dst surfaces are of 32 bit depth.
217 Assumes dst surface was allocated with the correct dimensions.
219 \param src Source surface.
220 \param dst Destination surface.
221 \param cx Horizontal center coordinate.
222 \param cy Vertical center coordinate.
223 \param isin Integer version of sine of angle.
224 \param icos Integer version of cosine of angle.
225 \param flipx Flag indicating horizontal mirroring should be applied.
226 \param flipy Flag indicating vertical mirroring should be applied.
227 \param smooth Flag indicating anti-aliasing should be used.
230 _transformSurfaceRGBA(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy, int smooth)
232 int x, y, t1, t2, dx, dy, xd, yd, sdx, sdy, ax, ay, ex, ey, sw, sh;
233 tColorRGBA c00, c01, c10, c11, cswap;
240 xd = ((src->w - dst->w) << 15);
241 yd = ((src->h - dst->h) << 15);
242 ax = (cx << 16) - (icos * cx);
243 ay = (cy << 16) - (isin * cx);
246 pc = (tColorRGBA*) dst->pixels;
247 gap = dst->pitch - dst->w * 4;
250 * Switch between interpolating and non-interpolating code
253 for (y = 0; y < dst->h; y++) {
255 sdx = (ax + (isin * dy)) + xd;
256 sdy = (ay - (icos * dy)) + yd;
257 for (x = 0; x < dst->w; x++) {
260 if (flipx) dx = sw - dx;
261 if (flipy) dy = sh - dy;
262 if ((dx > -1) && (dy > -1) && (dx < (src->w-1)) && (dy < (src->h-1))) {
263 sp = (tColorRGBA *) ((Uint8 *) src->pixels + src->pitch * dy) + dx;
267 sp += (src->pitch/4);
272 cswap = c00; c00=c01; c01=cswap;
273 cswap = c10; c10=c11; c11=cswap;
276 cswap = c00; c00=c10; c10=cswap;
277 cswap = c01; c01=c11; c11=cswap;
284 t1 = ((((c01.r - c00.r) * ex) >> 16) + c00.r) & 0xff;
285 t2 = ((((c11.r - c10.r) * ex) >> 16) + c10.r) & 0xff;
286 pc->r = (((t2 - t1) * ey) >> 16) + t1;
287 t1 = ((((c01.g - c00.g) * ex) >> 16) + c00.g) & 0xff;
288 t2 = ((((c11.g - c10.g) * ex) >> 16) + c10.g) & 0xff;
289 pc->g = (((t2 - t1) * ey) >> 16) + t1;
290 t1 = ((((c01.b - c00.b) * ex) >> 16) + c00.b) & 0xff;
291 t2 = ((((c11.b - c10.b) * ex) >> 16) + c10.b) & 0xff;
292 pc->b = (((t2 - t1) * ey) >> 16) + t1;
293 t1 = ((((c01.a - c00.a) * ex) >> 16) + c00.a) & 0xff;
294 t2 = ((((c11.a - c10.a) * ex) >> 16) + c10.a) & 0xff;
295 pc->a = (((t2 - t1) * ey) >> 16) + t1;
301 pc = (tColorRGBA *) ((Uint8 *) pc + gap);
304 for (y = 0; y < dst->h; y++) {
306 sdx = (ax + (isin * dy)) + xd;
307 sdy = (ay - (icos * dy)) + yd;
308 for (x = 0; x < dst->w; x++) {
311 if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
312 if(flipx) dx = sw - dx;
313 if(flipy) dy = sh - dy;
314 *pc = *((tColorRGBA *)((Uint8 *)src->pixels + src->pitch * dy) + dx);
320 pc = (tColorRGBA *) ((Uint8 *) pc + gap);
327 \brief Rotates and zooms 8 bit palette/Y 'src' surface to 'dst' surface without smoothing.
329 Rotates and zooms 8 bit RGBA/ABGR 'src' surface to 'dst' surface based on the control
330 parameters by scanning the destination surface.
331 Assumes src and dst surfaces are of 8 bit depth.
332 Assumes dst surface was allocated with the correct dimensions.
334 \param src Source surface.
335 \param dst Destination surface.
336 \param cx Horizontal center coordinate.
337 \param cy Vertical center coordinate.
338 \param isin Integer version of sine of angle.
339 \param icos Integer version of cosine of angle.
340 \param flipx Flag indicating horizontal mirroring should be applied.
341 \param flipy Flag indicating vertical mirroring should be applied.
344 transformSurfaceY(SDL_Surface * src, SDL_Surface * dst, int cx, int cy, int isin, int icos, int flipx, int flipy)
346 int x, y, dx, dy, xd, yd, sdx, sdy, ax, ay;
353 xd = ((src->w - dst->w) << 15);
354 yd = ((src->h - dst->h) << 15);
355 ax = (cx << 16) - (icos * cx);
356 ay = (cy << 16) - (isin * cx);
357 pc = (tColorY*) dst->pixels;
358 gap = dst->pitch - dst->w;
360 * Clear surface to colorkey
362 SDL_memset(pc, (int)(_colorkey(src) & 0xff), dst->pitch * dst->h);
364 * Iterate through destination surface
366 for (y = 0; y < dst->h; y++) {
368 sdx = (ax + (isin * dy)) + xd;
369 sdy = (ay - (icos * dy)) + yd;
370 for (x = 0; x < dst->w; x++) {
373 if ((unsigned)dx < (unsigned)src->w && (unsigned)dy < (unsigned)src->h) {
374 if (flipx) dx = (src->w-1)-dx;
375 if (flipy) dy = (src->h-1)-dy;
376 *pc = *((tColorY *)src->pixels + src->pitch * dy + dx);
388 \brief Rotates and zooms a surface with different horizontal and vertival scaling factors and optional anti-aliasing.
390 Rotates a 32-bit or 8-bit 'src' surface to newly created 'dst' surface.
391 'angle' is the rotation in degrees, 'centerx' and 'centery' the rotation center. If 'smooth' is set
392 then the destination 32-bit surface is anti-aliased. 8-bit surfaces must have a colorkey. 32-bit
393 surfaces must have a 8888 layout with red, green, blue and alpha masks (any ordering goes).
394 The blend mode of the 'src' surface has some effects on generation of the 'dst' surface: The NONE
395 mode will set the BLEND mode on the 'dst' surface. The MOD mode either generates a white 'dst'
396 surface and sets the colorkey or fills the it with the colorkey before copying the pixels.
397 When using the NONE and MOD modes, color and alpha modulation must be applied before using this function.
399 \param src The surface to rotozoom.
400 \param angle The angle to rotate in degrees.
401 \param centerx The horizontal coordinate of the center of rotation
402 \param zoomy The vertical coordinate of the center of rotation
403 \param smooth Antialiasing flag; set to SMOOTHING_ON to enable.
404 \param flipx Set to 1 to flip the image horizontally
405 \param flipy Set to 1 to flip the image vertically
406 \param dstwidth The destination surface width
407 \param dstheight The destination surface height
408 \param cangle The angle cosine
409 \param sangle The angle sine
410 \return The new rotated surface.
415 SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, int smooth, int flipx, int flipy, int dstwidth, int dstheight, double cangle, double sangle)
420 SDL_BlendMode blendmode;
422 int colorKeyAvailable = SDL_FALSE;
423 double sangleinv, cangleinv;
429 if (SDL_HasColorKey(src)) {
430 if (SDL_GetColorKey(src, &colorkey) == 0) {
431 colorKeyAvailable = SDL_TRUE;
435 /* This function requires a 32-bit surface or 8-bit surface with a colorkey */
436 is8bit = src->format->BitsPerPixel == 8 && colorKeyAvailable;
437 if (!(is8bit || (src->format->BitsPerPixel == 32 && src->format->Amask)))
440 /* Calculate target factors from sin/cos and zoom */
441 sangleinv = sangle*65536.0;
442 cangleinv = cangle*65536.0;
444 /* Alloc space to completely contain the rotated surface */
447 /* Target surface is 8 bit */
448 rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 8, 0, 0, 0, 0);
449 if (rz_dst != NULL) {
450 for (i = 0; i < src->format->palette->ncolors; i++) {
451 rz_dst->format->palette->colors[i] = src->format->palette->colors[i];
453 rz_dst->format->palette->ncolors = src->format->palette->ncolors;
456 /* Target surface is 32 bit with source RGBA ordering */
457 rz_dst = SDL_CreateRGBSurface(0, dstwidth, dstheight + GUARD_ROWS, 32,
458 src->format->Rmask, src->format->Gmask,
459 src->format->Bmask, src->format->Amask);
466 /* Adjust for guard rows */
467 rz_dst->h = dstheight;
469 SDL_GetSurfaceBlendMode(src, &blendmode);
471 if (colorKeyAvailable == SDL_TRUE) {
472 /* If available, the colorkey will be used to discard the pixels that are outside of the rotated area. */
473 SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
474 SDL_FillRect(rz_dst, NULL, colorkey);
475 } else if (blendmode == SDL_BLENDMODE_NONE) {
476 blendmode = SDL_BLENDMODE_BLEND;
477 } else if (blendmode == SDL_BLENDMODE_MOD) {
478 /* Without a colorkey, the target texture has to be white for the MOD blend mode so
479 * that the pixels outside the rotated area don't affect the destination surface.
481 colorkey = SDL_MapRGBA(rz_dst->format, 255, 255, 255, 0);
482 SDL_FillRect(rz_dst, NULL, colorkey);
483 /* Setting a white colorkey for the destination surface makes the final blit discard
484 * all pixels outside of the rotated area. This doesn't interfere with anything because
485 * white pixels are already a no-op and the MOD blend mode does not interact with alpha.
487 SDL_SetColorKey(rz_dst, SDL_TRUE, colorkey);
490 SDL_SetSurfaceBlendMode(rz_dst, blendmode);
492 /* Lock source surface */
493 if (SDL_MUSTLOCK(src)) {
494 SDL_LockSurface(src);
497 /* check if the rotation is a multiple of 90 degrees so we can take a fast path and also somewhat reduce
498 * the off-by-one problem in _transformSurfaceRGBA that expresses itself when the rotation is near
499 * multiples of 90 degrees.
501 angle90 = (int)(angle/90);
502 if (angle90 == angle/90) {
504 if (angle90 < 0) angle90 += 4; /* 0:0 deg, 1:90 deg, 2:180 deg, 3:270 deg */
510 /* Call the 8-bit transformation routine to do the rotation */
512 transformSurfaceY90(src, rz_dst, angle90, flipx, flipy);
514 transformSurfaceY(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
518 /* Call the 32-bit transformation routine to do the rotation */
520 transformSurfaceRGBA90(src, rz_dst, angle90, flipx, flipy);
522 _transformSurfaceRGBA(src, rz_dst, centerx, centery, (int)sangleinv, (int)cangleinv,
523 flipx, flipy, smooth);
527 /* Unlock source surface */
528 if (SDL_MUSTLOCK(src)) {
529 SDL_UnlockSurface(src);
532 /* Return rotated surface */