Fixed bug 2687 - SDL_BlitScaled does not handle clipping correctly Patch from Benoit Pierre: video: fix clipping handling in SDL_UpperBlitScaled - honor destination clipping rectangle - update both destination and source rectangles when clipping source rectangle to source surface and destination rectangle to destination clip rectangle - don't change scaling factors when clipping N.B.: - when no scaling is involved (source and destination width/height are the same), SDL_UpperBlit is used (so SDL_BlitScaled behaves like SDL_BlitSurface) - the final destination rectangle after all clipping is performed is saved back to dstrect (like for SDL_UpperBlit)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
diff --git a/src/video/SDL_surface.c b/src/video/SDL_surface.c
index 421e2b8..3f4244b 100644
--- a/src/video/SDL_surface.c
+++ b/src/video/SDL_surface.c
@@ -26,7 +26,6 @@
#include "SDL_RLEaccel_c.h"
#include "SDL_pixels_c.h"
-
/* Public routines */
/*
* Create an empty RGB surface of the appropriate depth
@@ -623,7 +622,12 @@ int
SDL_UpperBlitScaled(SDL_Surface * src, const SDL_Rect * srcrect,
SDL_Surface * dst, SDL_Rect * dstrect)
{
- SDL_Rect final_src, final_dst, fulldst;
+ double src_x0, src_y0, src_x1, src_y1;
+ double dst_x0, dst_y0, dst_x1, dst_y1;
+ SDL_Rect final_src, final_dst;
+ double scaling_w, scaling_h;
+ int src_w, src_h;
+ int dst_w, dst_h;
/* Make sure the surfaces aren't locked */
if (!src || !dst) {
@@ -633,78 +637,135 @@ SDL_UpperBlitScaled(SDL_Surface * src, const SDL_Rect * srcrect,
return SDL_SetError("Surfaces must not be locked during blit");
}
- /* If the destination rectangle is NULL, use the entire dest surface */
- if (dstrect == NULL) {
- fulldst.x = fulldst.y = 0;
- fulldst.w = dst->w;
- fulldst.h = dst->h;
- dstrect = &fulldst;
+ if (NULL == srcrect) {
+ src_w = src->w;
+ src_h = src->h;
+ } else {
+ src_w = srcrect->w;
+ src_h = srcrect->h;
}
- /* clip the source rectangle to the source surface */
- if (srcrect) {
- int maxw, maxh;
+ if (NULL == dstrect) {
+ dst_w = dst->w;
+ dst_h = dst->h;
+ } else {
+ dst_w = dstrect->w;
+ dst_h = dstrect->h;
+ }
- final_src.x = srcrect->x;
- final_src.w = srcrect->w;
- if (final_src.x < 0) {
- final_src.w += final_src.x;
- final_src.x = 0;
- }
- maxw = src->w - final_src.x;
- if (maxw < final_src.w)
- final_src.w = maxw;
-
- final_src.y = srcrect->y;
- final_src.h = srcrect->h;
- if (final_src.y < 0) {
- final_src.h += final_src.y;
- final_src.y = 0;
- }
- maxh = src->h - final_src.y;
- if (maxh < final_src.h)
- final_src.h = maxh;
+ if (dst_w == src_w && dst_h == src_h) {
+ /* No scaling, defer to regular blit */
+ return SDL_BlitSurface(src, srcrect, dst, dstrect);
+ }
+
+ scaling_w = (double)dst_w / src_w;
+ scaling_h = (double)dst_h / src_h;
+ if (NULL == dstrect) {
+ dst_x0 = 0;
+ dst_y0 = 0;
+ dst_x1 = dst_w - 1;
+ dst_y1 = dst_h - 1;
} else {
- final_src.x = final_src.y = 0;
- final_src.w = src->w;
- final_src.h = src->h;
+ dst_x0 = dstrect->x;
+ dst_y0 = dstrect->y;
+ dst_x1 = dst_x0 + dst_w - 1;
+ dst_y1 = dst_y0 + dst_h - 1;
}
- /* clip the destination rectangle against the clip rectangle */
- if (dstrect) {
- int maxw, maxh;
+ if (NULL == srcrect) {
+ src_x0 = 0;
+ src_y0 = 0;
+ src_x1 = src_w - 1;
+ src_y1 = src_h - 1;
+ } else {
+ src_x0 = srcrect->x;
+ src_y0 = srcrect->y;
+ src_x1 = src_x0 + src_w - 1;
+ src_y1 = src_y0 + src_h - 1;
+
+ /* Clip source rectangle to the source surface */
- final_dst.x = dstrect->x;
- final_dst.w = dstrect->w;
- if (final_dst.x < 0) {
- final_dst.w += final_dst.x;
- final_dst.x = 0;
+ if (src_x0 < 0) {
+ dst_x0 -= src_x0 * scaling_w;
+ src_x0 = 0;
}
- maxw = dst->w - final_dst.x;
- if (maxw < final_dst.w)
- final_dst.w = maxw;
-
- final_dst.y = dstrect->y;
- final_dst.h = dstrect->h;
- if (final_dst.y < 0) {
- final_dst.h += final_dst.y;
- final_dst.y = 0;
+
+ if (src_x1 >= src->w) {
+ dst_x1 -= (src_x1 - src->w + 1) * scaling_w;
+ src_x1 = src->w - 1;
+ }
+
+ if (src_y0 < 0) {
+ dst_y0 -= src_y0 * scaling_h;
+ src_y0 = 0;
+ }
+
+ if (src_y1 >= src->h) {
+ dst_y1 -= (src_y1 - src->h + 1) * scaling_h;
+ src_y1 = src->h - 1;
}
- maxh = dst->h - final_dst.y;
- if (maxh < final_dst.h)
- final_dst.h = maxh;
- } else {
- final_dst.x = final_dst.y = 0;
- final_dst.w = dst->w;
- final_dst.h = dst->h;
}
- if (final_dst.w > 0 && final_dst.h > 0) {
- return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
+ /* Clip destination rectangle to the clip rectangle */
+
+ /* Translate to clip space for easier calculations */
+ dst_x0 -= dst->clip_rect.x;
+ dst_x1 -= dst->clip_rect.x;
+ dst_y0 -= dst->clip_rect.y;
+ dst_y1 -= dst->clip_rect.y;
+
+ if (dst_x0 < 0) {
+ src_x0 -= dst_x0 / scaling_w;
+ dst_x0 = 0;
}
- return 0;
+ if (dst_x1 >= dst->clip_rect.w) {
+ src_x1 -= (dst_x1 - dst->clip_rect.w + 1) / scaling_w;
+ dst_x1 = dst->clip_rect.w - 1;
+ }
+
+ if (dst_y0 < 0) {
+ src_y0 -= dst_y0 / scaling_h;
+ dst_y0 = 0;
+ }
+
+ if (dst_y1 >= dst->clip_rect.h) {
+ src_y1 -= (dst_y1 - dst->clip_rect.h + 1) / scaling_h;
+ dst_y1 = dst->clip_rect.h - 1;
+ }
+
+ /* Translate back to surface coordinates */
+ dst_x0 += dst->clip_rect.x;
+ dst_x1 += dst->clip_rect.x;
+ dst_y0 += dst->clip_rect.y;
+ dst_y1 += dst->clip_rect.y;
+
+ final_src.x = SDL_round(src_x0);
+ final_src.y = SDL_round(src_y0);
+ final_src.w = SDL_round(src_x1 - src_x0 + 1);
+ final_src.h = SDL_round(src_y1 - src_y0 + 1);
+
+ final_dst.x = SDL_round(dst_x0);
+ final_dst.y = SDL_round(dst_y0);
+ final_dst.w = SDL_round(dst_x1 - dst_x0 + 1);
+ final_dst.h = SDL_round(dst_y1 - dst_y0 + 1);
+
+ if (final_dst.w < 0)
+ final_dst.w = 0;
+ if (final_dst.h < 0)
+ final_dst.h = 0;
+
+ if (dstrect)
+ *dstrect = final_dst;
+
+ if (final_dst.w == 0 || final_dst.h == 0 ||
+ final_src.w <= 0 || final_src.h <= 0) {
+ /* No-op. */
+ return 0;
+ }
+
+ return SDL_LowerBlitScaled(src, &final_src, dst, &final_dst);
}
/**
@@ -721,43 +782,6 @@ SDL_LowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect,
SDL_COPY_COLORKEY
);
- /* Save off the original dst width, height */
- int dstW = dstrect->w;
- int dstH = dstrect->h;
- SDL_Rect full_rect;
- SDL_Rect final_dst = *dstrect;
- SDL_Rect final_src = *srcrect;
-
- /* Clip the dst surface to the dstrect */
- full_rect.x = 0;
- full_rect.y = 0;
- full_rect.w = dst->w;
- full_rect.h = dst->h;
- if (!SDL_IntersectRect(&final_dst, &full_rect, &final_dst)) {
- return 0;
- }
-
- /* Did the dst width change? */
- if ( dstW != final_dst.w ) {
- /* scale the src width appropriately */
- final_src.w = final_src.w * dst->clip_rect.w / dstW;
- }
-
- /* Did the dst height change? */
- if ( dstH != final_dst.h ) {
- /* scale the src width appropriately */
- final_src.h = final_src.h * dst->clip_rect.h / dstH;
- }
-
- /* Clip the src surface to the srcrect */
- full_rect.x = 0;
- full_rect.y = 0;
- full_rect.w = src->w;
- full_rect.h = src->h;
- if (!SDL_IntersectRect(&final_src, &full_rect, &final_src)) {
- return 0;
- }
-
if (!(src->map->info.flags & SDL_COPY_NEAREST)) {
src->map->info.flags |= SDL_COPY_NEAREST;
SDL_InvalidateMap(src->map);
@@ -766,9 +790,9 @@ SDL_LowerBlitScaled(SDL_Surface * src, SDL_Rect * srcrect,
if ( !(src->map->info.flags & complex_copy_flags) &&
src->format->format == dst->format->format &&
!SDL_ISPIXELFORMAT_INDEXED(src->format->format) ) {
- return SDL_SoftStretch( src, &final_src, dst, &final_dst );
+ return SDL_SoftStretch( src, srcrect, dst, dstrect );
} else {
- return SDL_LowerBlit( src, &final_src, dst, &final_dst );
+ return SDL_LowerBlit( src, srcrect, dst, dstrect );
}
}