Commit a8955f26405b9957074f4b9193068a261cb174d9

Sam Lantinga 2014-06-25T01:35:17

Made the RLE code (semi) readable again

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
diff --git a/src/video/SDL_RLEaccel.c b/src/video/SDL_RLEaccel.c
index 5829367..1f0f750 100644
--- a/src/video/SDL_RLEaccel.c
+++ b/src/video/SDL_RLEaccel.c
@@ -115,22 +115,22 @@
  * This can be used for any RGB permutation of course.
  */
 #define ALPHA_BLIT32_888(to, from, length, bpp, alpha)      \
-    do {                            \
-        int i;                          \
-    Uint32 *src = (Uint32 *)(from);             \
-    Uint32 *dst = (Uint32 *)(to);               \
-    for(i = 0; i < (int)(length); i++) {            \
-        Uint32 s = *src++;                  \
-        Uint32 d = *dst;                    \
-        Uint32 s1 = s & 0xff00ff;               \
-        Uint32 d1 = d & 0xff00ff;               \
-        d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;    \
-        s &= 0xff00;                    \
-        d &= 0xff00;                    \
-        d = (d + ((s - d) * alpha >> 8)) & 0xff00;      \
-        *dst++ = d1 | d;                    \
-    }                           \
-    } while(0)
+    do {                                                    \
+        int i;                                              \
+        Uint32 *src = (Uint32 *)(from);                     \
+        Uint32 *dst = (Uint32 *)(to);                       \
+        for (i = 0; i < (int)(length); i++) {               \
+            Uint32 s = *src++;                              \
+            Uint32 d = *dst;                                \
+            Uint32 s1 = s & 0xff00ff;                       \
+            Uint32 d1 = d & 0xff00ff;                       \
+            d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
+            s &= 0xff00;                                    \
+            d &= 0xff00;                                    \
+            d = (d + ((s - d) * alpha >> 8)) & 0xff00;      \
+            *dst++ = d1 | d;                                \
+        }                                                   \
+    } while (0)
 
 /*
  * For 16bpp pixels we can go a step further: put the middle component
@@ -139,97 +139,97 @@
  * 5 bits, we have to scale alpha down to 5 bits as well.
  */
 #define ALPHA_BLIT16_565(to, from, length, bpp, alpha)  \
-    do {                        \
-        int i;                      \
-    Uint16 *src = (Uint16 *)(from);         \
-    Uint16 *dst = (Uint16 *)(to);           \
-    Uint32 ALPHA = alpha >> 3;          \
-    for(i = 0; i < (int)(length); i++) {        \
-        Uint32 s = *src++;              \
-        Uint32 d = *dst;                \
-        s = (s | s << 16) & 0x07e0f81f;     \
-        d = (d | d << 16) & 0x07e0f81f;     \
-        d += (s - d) * ALPHA >> 5;          \
-        d &= 0x07e0f81f;                \
-        *dst++ = (Uint16)(d | d >> 16);         \
-    }                       \
+    do {                                                \
+        int i;                                          \
+        Uint16 *src = (Uint16 *)(from);                 \
+        Uint16 *dst = (Uint16 *)(to);                   \
+        Uint32 ALPHA = alpha >> 3;                      \
+        for(i = 0; i < (int)(length); i++) {            \
+            Uint32 s = *src++;                          \
+            Uint32 d = *dst;                            \
+            s = (s | s << 16) & 0x07e0f81f;             \
+            d = (d | d << 16) & 0x07e0f81f;             \
+            d += (s - d) * ALPHA >> 5;                  \
+            d &= 0x07e0f81f;                            \
+            *dst++ = (Uint16)(d | d >> 16);             \
+        }                                               \
     } while(0)
 
 #define ALPHA_BLIT16_555(to, from, length, bpp, alpha)  \
-    do {                        \
-        int i;                      \
-    Uint16 *src = (Uint16 *)(from);         \
-    Uint16 *dst = (Uint16 *)(to);           \
-    Uint32 ALPHA = alpha >> 3;          \
-    for(i = 0; i < (int)(length); i++) {        \
-        Uint32 s = *src++;              \
-        Uint32 d = *dst;                \
-        s = (s | s << 16) & 0x03e07c1f;     \
-        d = (d | d << 16) & 0x03e07c1f;     \
-        d += (s - d) * ALPHA >> 5;          \
-        d &= 0x03e07c1f;                \
-        *dst++ = (Uint16)(d | d >> 16);         \
-    }                       \
+    do {                                                \
+        int i;                                          \
+        Uint16 *src = (Uint16 *)(from);                 \
+        Uint16 *dst = (Uint16 *)(to);                   \
+        Uint32 ALPHA = alpha >> 3;                      \
+        for(i = 0; i < (int)(length); i++) {            \
+            Uint32 s = *src++;                          \
+            Uint32 d = *dst;                            \
+            s = (s | s << 16) & 0x03e07c1f;             \
+            d = (d | d << 16) & 0x03e07c1f;             \
+            d += (s - d) * ALPHA >> 5;                  \
+            d &= 0x03e07c1f;                            \
+            *dst++ = (Uint16)(d | d >> 16);             \
+        }                                               \
     } while(0)
 
 /*
  * The general slow catch-all function, for remaining depths and formats
  */
 #define ALPHA_BLIT_ANY(to, from, length, bpp, alpha)            \
-    do {                                \
-        int i;                              \
-    Uint8 *src = from;                      \
-    Uint8 *dst = to;                        \
-    for(i = 0; i < (int)(length); i++) {                \
-        Uint32 s, d;                        \
-        unsigned rs, gs, bs, rd, gd, bd;                \
-        switch(bpp) {                       \
-        case 2:                         \
-        s = *(Uint16 *)src;                 \
-        d = *(Uint16 *)dst;                 \
-        break;                          \
-        case 3:                         \
-        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {           \
-            s = (src[0] << 16) | (src[1] << 8) | src[2];    \
-            d = (dst[0] << 16) | (dst[1] << 8) | dst[2];    \
-        } else {                        \
-            s = (src[2] << 16) | (src[1] << 8) | src[0];    \
-            d = (dst[2] << 16) | (dst[1] << 8) | dst[0];    \
-        }                           \
-        break;                          \
-        case 4:                         \
-        s = *(Uint32 *)src;                 \
-        d = *(Uint32 *)dst;                 \
-        break;                          \
-        }                               \
-        RGB_FROM_PIXEL(s, fmt, rs, gs, bs);             \
-        RGB_FROM_PIXEL(d, fmt, rd, gd, bd);             \
-        rd += (rs - rd) * alpha >> 8;               \
-        gd += (gs - gd) * alpha >> 8;               \
-        bd += (bs - bd) * alpha >> 8;               \
-        PIXEL_FROM_RGB(d, fmt, rd, gd, bd);             \
-        switch(bpp) {                       \
-        case 2:                         \
-        *(Uint16 *)dst = (Uint16)d;                 \
-        break;                          \
-        case 3:                         \
-        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {           \
-            dst[0] = (Uint8)(d >> 16);                  \
-            dst[1] = (Uint8)(d >> 8);                   \
-            dst[2] = (Uint8)(d);                        \
-        } else {                        \
-            dst[0] = (Uint8)d;                      \
-            dst[1] = (Uint8)(d >> 8);                   \
-            dst[2] = (Uint8)(d >> 16);                  \
-        }                           \
-        break;                          \
-        case 4:                         \
-        *(Uint32 *)dst = d;                 \
-        break;                          \
-        }                               \
-        src += bpp;                         \
-        dst += bpp;                         \
-    }                               \
+    do {                                                        \
+        int i;                                                  \
+        Uint8 *src = from;                                      \
+        Uint8 *dst = to;                                        \
+        for (i = 0; i < (int)(length); i++) {                   \
+            Uint32 s, d;                                        \
+            unsigned rs, gs, bs, rd, gd, bd;                    \
+            switch (bpp) {                                      \
+            case 2:                                             \
+                s = *(Uint16 *)src;                             \
+                d = *(Uint16 *)dst;                             \
+                break;                                          \
+            case 3:                                             \
+                if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {          \
+                    s = (src[0] << 16) | (src[1] << 8) | src[2]; \
+                    d = (dst[0] << 16) | (dst[1] << 8) | dst[2]; \
+                } else {                                        \
+                    s = (src[2] << 16) | (src[1] << 8) | src[0]; \
+                    d = (dst[2] << 16) | (dst[1] << 8) | dst[0]; \
+                }                                               \
+                break;                                          \
+            case 4:                                             \
+                s = *(Uint32 *)src;                             \
+                d = *(Uint32 *)dst;                             \
+                break;                                          \
+            }                                                   \
+            RGB_FROM_PIXEL(s, fmt, rs, gs, bs);                 \
+            RGB_FROM_PIXEL(d, fmt, rd, gd, bd);                 \
+            rd += (rs - rd) * alpha >> 8;                       \
+            gd += (gs - gd) * alpha >> 8;                       \
+            bd += (bs - bd) * alpha >> 8;                       \
+            PIXEL_FROM_RGB(d, fmt, rd, gd, bd);                 \
+            switch (bpp) {                                      \
+            case 2:                                             \
+                *(Uint16 *)dst = (Uint16)d;                     \
+                break;                                          \
+            case 3:                                             \
+                if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {          \
+                    dst[0] = (Uint8)(d >> 16);                  \
+                    dst[1] = (Uint8)(d >> 8);                   \
+                    dst[2] = (Uint8)(d);                        \
+                } else {                                        \
+                    dst[0] = (Uint8)d;                          \
+                    dst[1] = (Uint8)(d >> 8);                   \
+                    dst[2] = (Uint8)(d >> 16);                  \
+                }                                               \
+                break;                                          \
+            case 4:                                             \
+                *(Uint32 *)dst = d;                             \
+                break;                                          \
+            }                                                   \
+            src += bpp;                                         \
+            dst += bpp;                                         \
+        }                                                       \
     } while(0)
 
 /*
@@ -241,16 +241,16 @@
  * add them. Then shift right and add the sum of the lowest bits.
  */
 #define ALPHA_BLIT32_888_50(to, from, length, bpp, alpha)       \
-    do {                                \
-        int i;                              \
-    Uint32 *src = (Uint32 *)(from);                 \
-    Uint32 *dst = (Uint32 *)(to);                   \
-    for(i = 0; i < (int)(length); i++) {                \
-        Uint32 s = *src++;                      \
-        Uint32 d = *dst;                        \
-        *dst++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)   \
-             + (s & d & 0x00010101);                \
-    }                               \
+    do {                                                        \
+        int i;                                                  \
+        Uint32 *src = (Uint32 *)(from);                         \
+        Uint32 *dst = (Uint32 *)(to);                           \
+        for(i = 0; i < (int)(length); i++) {                    \
+            Uint32 s = *src++;                                  \
+            Uint32 d = *dst;                                    \
+            *dst++ = (((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1) \
+                 + (s & d & 0x00010101);                        \
+        }                                                       \
     } while(0)
 
 /*
@@ -259,116 +259,118 @@
  */
 
 /* helper: blend a single 16 bit pixel at 50% */
-#define BLEND16_50(dst, src, mask)          \
-    do {                        \
-    Uint32 s = *src++;              \
-    Uint32 d = *dst;                \
-    *dst++ = (Uint16)((((s & mask) + (d & mask)) >> 1) +    \
-                      (s & d & (~mask & 0xffff)));      \
+#define BLEND16_50(dst, src, mask)                              \
+    do {                                                        \
+        Uint32 s = *src++;                                      \
+        Uint32 d = *dst;                                        \
+        *dst++ = (Uint16)((((s & mask) + (d & mask)) >> 1) +    \
+                          (s & d & (~mask & 0xffff)));          \
     } while(0)
 
 /* basic 16bpp blender. mask is the pixels to keep when adding. */
 #define ALPHA_BLIT16_50(to, from, length, bpp, alpha, mask)     \
-    do {                                \
-    unsigned n = (length);                      \
-    Uint16 *src = (Uint16 *)(from);                 \
-    Uint16 *dst = (Uint16 *)(to);                   \
-    if(((uintptr_t)src ^ (uintptr_t)dst) & 3) {         \
-        /* source and destination not in phase, blit one by one */  \
-        while(n--)                          \
-        BLEND16_50(dst, src, mask);             \
-    } else {                            \
-        if((uintptr_t)src & 3) {                    \
-        /* first odd pixel */                   \
-        BLEND16_50(dst, src, mask);             \
-        n--;                            \
-        }                               \
-        for(; n > 1; n -= 2) {                  \
-        Uint32 s = *(Uint32 *)src;              \
-        Uint32 d = *(Uint32 *)dst;              \
-        *(Uint32 *)dst = ((s & (mask | mask << 16)) >> 1)   \
-                       + ((d & (mask | mask << 16)) >> 1)   \
-                       + (s & d & (~(mask | mask << 16)));  \
-        src += 2;                       \
-        dst += 2;                       \
-        }                               \
-        if(n)                           \
-        BLEND16_50(dst, src, mask); /* last odd pixel */    \
-    }                               \
+    do {                                                        \
+        unsigned n = (length);                                  \
+        Uint16 *src = (Uint16 *)(from);                         \
+        Uint16 *dst = (Uint16 *)(to);                           \
+        if (((uintptr_t)src ^ (uintptr_t)dst) & 3) {            \
+            /* source and destination not in phase, blit one by one */ \
+            while (n--)                                         \
+                BLEND16_50(dst, src, mask);                     \
+        } else {                                                \
+            if ((uintptr_t)src & 3) {                           \
+                /* first odd pixel */                           \
+                BLEND16_50(dst, src, mask);                     \
+                n--;                                            \
+            }                                                   \
+            for (; n > 1; n -= 2) {                             \
+                Uint32 s = *(Uint32 *)src;                      \
+                Uint32 d = *(Uint32 *)dst;                      \
+                *(Uint32 *)dst = ((s & (mask | mask << 16)) >> 1) \
+                    + ((d & (mask | mask << 16)) >> 1)          \
+                    + (s & d & (~(mask | mask << 16)));         \
+                src += 2;                                       \
+                dst += 2;                                       \
+            }                                                   \
+            if (n)                                              \
+                BLEND16_50(dst, src, mask); /* last odd pixel */ \
+        }                                                       \
     } while(0)
 
-#define ALPHA_BLIT16_565_50(to, from, length, bpp, alpha)   \
+#define ALPHA_BLIT16_565_50(to, from, length, bpp, alpha)       \
     ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xf7de)
 
-#define ALPHA_BLIT16_555_50(to, from, length, bpp, alpha)   \
+#define ALPHA_BLIT16_555_50(to, from, length, bpp, alpha)       \
     ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xfbde)
 
-#define CHOOSE_BLIT(blitter, alpha, fmt)                \
-    do {                                \
-        if(alpha == 255) {                      \
-        switch(fmt->BytesPerPixel) {                \
-        case 1: blitter(1, Uint8, OPAQUE_BLIT); break;      \
-        case 2: blitter(2, Uint8, OPAQUE_BLIT); break;      \
-        case 3: blitter(3, Uint8, OPAQUE_BLIT); break;      \
-        case 4: blitter(4, Uint16, OPAQUE_BLIT); break;     \
-        }                               \
-    } else {                            \
-        switch(fmt->BytesPerPixel) {                \
-        case 1:                         \
-        /* No 8bpp alpha blitting */                \
-        break;                          \
-                                    \
-        case 2:                         \
-        switch(fmt->Rmask | fmt->Gmask | fmt->Bmask) {      \
-        case 0xffff:                        \
-            if(fmt->Gmask == 0x07e0             \
-               || fmt->Rmask == 0x07e0              \
-               || fmt->Bmask == 0x07e0) {           \
-            if(alpha == 128)                \
-                blitter(2, Uint8, ALPHA_BLIT16_565_50); \
-            else {                      \
-                blitter(2, Uint8, ALPHA_BLIT16_565);    \
-            }                       \
-            } else                      \
-            goto general16;                 \
-            break;                      \
-                                    \
-        case 0x7fff:                        \
-            if(fmt->Gmask == 0x03e0             \
-               || fmt->Rmask == 0x03e0              \
-               || fmt->Bmask == 0x03e0) {           \
-            if(alpha == 128)                \
-                blitter(2, Uint8, ALPHA_BLIT16_555_50); \
-            else {                      \
-                blitter(2, Uint8, ALPHA_BLIT16_555);    \
-            }                       \
-            break;                      \
-            }                           \
-            /* fallthrough */                   \
-                                    \
-        default:                        \
-        general16:                      \
-            blitter(2, Uint8, ALPHA_BLIT_ANY);          \
-        }                           \
-        break;                          \
-                                    \
-        case 3:                         \
-        blitter(3, Uint8, ALPHA_BLIT_ANY);          \
-        break;                          \
-                                    \
-        case 4:                         \
-        if((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff \
-           && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00 \
-               || fmt->Bmask == 0xff00)) {          \
-            if(alpha == 128)                    \
-            blitter(4, Uint16, ALPHA_BLIT32_888_50);    \
-            else                        \
-            blitter(4, Uint16, ALPHA_BLIT32_888);       \
-        } else                          \
-            blitter(4, Uint16, ALPHA_BLIT_ANY);         \
-        break;                          \
-        }                               \
-    }                               \
+#define CHOOSE_BLIT(blitter, alpha, fmt)                        \
+    do {                                                        \
+        if (alpha == 255) {                                     \
+            switch (fmt->BytesPerPixel) {                       \
+            case 1: blitter(1, Uint8, OPAQUE_BLIT); break;      \
+            case 2: blitter(2, Uint8, OPAQUE_BLIT); break;      \
+            case 3: blitter(3, Uint8, OPAQUE_BLIT); break;      \
+            case 4: blitter(4, Uint16, OPAQUE_BLIT); break;     \
+            }                                                   \
+        } else {                                                \
+            switch (fmt->BytesPerPixel) {                       \
+            case 1:                                             \
+                /* No 8bpp alpha blitting */                    \
+                break;                                          \
+                                                                \
+            case 2:                                             \
+                switch (fmt->Rmask | fmt->Gmask | fmt->Bmask) { \
+                case 0xffff:                                    \
+                    if (fmt->Gmask == 0x07e0                    \
+                        || fmt->Rmask == 0x07e0                 \
+                        || fmt->Bmask == 0x07e0) {              \
+                        if (alpha == 128) {                     \
+                            blitter(2, Uint8, ALPHA_BLIT16_565_50); \
+                        } else {                                \
+                            blitter(2, Uint8, ALPHA_BLIT16_565); \
+                        }                                       \
+                    } else                                      \
+                        goto general16;                         \
+                    break;                                      \
+                                                                \
+                case 0x7fff:                                    \
+                    if (fmt->Gmask == 0x03e0                    \
+                        || fmt->Rmask == 0x03e0                 \
+                        || fmt->Bmask == 0x03e0) {              \
+                        if (alpha == 128) {                     \
+                            blitter(2, Uint8, ALPHA_BLIT16_555_50); \
+                        } else {                                \
+                            blitter(2, Uint8, ALPHA_BLIT16_555); \
+                        }                                       \
+                        break;                                  \
+                    } else                                      \
+                        goto general16;                         \
+                    break;                                      \
+                                                                \
+                default:                                        \
+    general16:                                                  \
+                    blitter(2, Uint8, ALPHA_BLIT_ANY);          \
+                }                                               \
+                break;                                          \
+                                                                \
+            case 3:                                             \
+                blitter(3, Uint8, ALPHA_BLIT_ANY);              \
+                break;                                          \
+                                                                \
+            case 4:                                             \
+                if ((fmt->Rmask | fmt->Gmask | fmt->Bmask) == 0x00ffffff \
+                    && (fmt->Gmask == 0xff00 || fmt->Rmask == 0xff00 \
+                    || fmt->Bmask == 0xff00)) {                 \
+                    if (alpha == 128) {                         \
+                        blitter(4, Uint16, ALPHA_BLIT32_888_50); \
+                    } else {                                    \
+                        blitter(4, Uint16, ALPHA_BLIT32_888);   \
+                    }                                           \
+                } else                                          \
+                    blitter(4, Uint16, ALPHA_BLIT_ANY);         \
+                break;                                          \
+            }                                                   \
+        }                                                       \
     } while(0)
 
 /*
@@ -381,48 +383,49 @@ RLEClipBlit(int w, Uint8 * srcbuf, SDL_Surface * surf_dst,
 {
     SDL_PixelFormat *fmt = surf_dst->format;
 
-#define RLECLIPBLIT(bpp, Type, do_blit)                    \
-    do {                                   \
-    int linecount = srcrect->h;                    \
-    int ofs = 0;                               \
-    int left = srcrect->x;                         \
-    int right = left + srcrect->w;                     \
-    dstbuf -= left * bpp;                          \
-    for(;;) {                              \
-        int run;                               \
-        ofs += *(Type *)srcbuf;                    \
-        run = ((Type *)srcbuf)[1];                     \
-        srcbuf += 2 * sizeof(Type);                    \
-        if(run) {                              \
-        /* clip to left and right borders */               \
-        if(ofs < right) {                      \
-            int start = 0;                     \
-            int len = run;                     \
-            int startcol;                      \
-            if(left - ofs > 0) {                   \
-            start = left - ofs;                \
-            len -= start;                      \
-            if(len <= 0)                       \
-                goto nocopy ## bpp ## do_blit;         \
-            }                              \
-            startcol = ofs + start;                \
-            if(len > right - startcol)                 \
-            len = right - startcol;                \
-            do_blit(dstbuf + startcol * bpp, srcbuf + start * bpp, \
-                len, bpp, alpha);                  \
-        }                              \
-        nocopy ## bpp ## do_blit:                      \
-        srcbuf += run * bpp;                       \
-        ofs += run;                        \
-        } else if(!ofs)                        \
-        break;                             \
-        if(ofs == w) {                         \
-        ofs = 0;                           \
-        dstbuf += surf_dst->pitch;                      \
-        if(!--linecount)                       \
-            break;                         \
-        }                                  \
-    }                                  \
+#define RLECLIPBLIT(bpp, Type, do_blit)                         \
+    do {                                                        \
+        int linecount = srcrect->h;                             \
+        int ofs = 0;                                            \
+        int left = srcrect->x;                                  \
+        int right = left + srcrect->w;                          \
+        dstbuf -= left * bpp;                                   \
+        for (;;) {                                              \
+            int run;                                            \
+            ofs += *(Type *)srcbuf;                             \
+            run = ((Type *)srcbuf)[1];                          \
+            srcbuf += 2 * sizeof(Type);                         \
+            if (run) {                                          \
+                /* clip to left and right borders */            \
+                if (ofs < right) {                              \
+                    int start = 0;                              \
+                    int len = run;                              \
+                    int startcol;                               \
+                    if (left - ofs > 0) {                       \
+                        start = left - ofs;                     \
+                        len -= start;                           \
+                        if (len <= 0)                           \
+                            goto nocopy ## bpp ## do_blit;      \
+                    }                                           \
+                    startcol = ofs + start;                     \
+                    if (len > right - startcol)                 \
+                        len = right - startcol;                 \
+                    do_blit(dstbuf + startcol * bpp, srcbuf + start * bpp, \
+                        len, bpp, alpha);                       \
+                }                                               \
+    nocopy ## bpp ## do_blit:                                   \
+                srcbuf += run * bpp;                            \
+                ofs += run;                                     \
+            } else if (!ofs)                                    \
+                break;                                          \
+                                                                \
+            if (ofs == w) {                                     \
+                ofs = 0;                                        \
+                dstbuf += surf_dst->pitch;                      \
+                if (!--linecount)                               \
+                    break;                                      \
+            }                                                   \
+        }                                                       \
     } while(0)
 
     CHOOSE_BLIT(RLECLIPBLIT, alpha, fmt);