Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1578 lines (1449 loc) · 45.9 KB

SDL_RLEaccel.c

File metadata and controls

1578 lines (1449 loc) · 45.9 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
3
Copyright (C) 1997-2011 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
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
/*
* RLE encoding for software colorkey and alpha-channel acceleration
*
* Original version by Sam Lantinga
*
* Mattias Engdegård (Yorick): Rewrite. New encoding format, encoder and
* decoder. Added per-surface alpha blitter. Added per-pixel alpha
* format, encoder and blitter.
*
* Many thanks to Xark and johns for hints, benchmarks and useful comments
* leading to this code.
*
* Welcome to Macro Mayhem.
*/
/*
* The encoding translates the image data to a stream of segments of the form
*
* <skip> <run> <data>
*
* where <skip> is the number of transparent pixels to skip,
* <run> is the number of opaque pixels to blit,
* and <data> are the pixels themselves.
*
* This basic structure is used both for colorkeyed surfaces, used for simple
* binary transparency and for per-surface alpha blending, and for surfaces
* with per-pixel alpha. The details differ, however:
*
* Encoding of colorkeyed surfaces:
*
* Encoded pixels always have the same format as the target surface.
* <skip> and <run> are unsigned 8 bit integers, except for 32 bit depth
* where they are 16 bit. This makes the pixel data aligned at all times.
* Segments never wrap around from one scan line to the next.
*
* The end of the sequence is marked by a zero <skip>,<run> pair at the *
* beginning of a line.
*
* Encoding of surfaces with per-pixel alpha:
*
* The sequence begins with a struct RLEDestFormat describing the target
* pixel format, to provide reliable un-encoding.
*
* Each scan line is encoded twice: First all completely opaque pixels,
* encoded in the target format as described above, and then all
* partially transparent (translucent) pixels (where 1 <= alpha <= 254),
* in the following 32-bit format:
*
* For 32-bit targets, each pixel has the target RGB format but with
* the alpha value occupying the highest 8 bits. The <skip> and <run>
* counts are 16 bit.
*
* For 16-bit targets, each pixel has the target RGB format, but with
* the middle component (usually green) shifted 16 steps to the left,
* and the hole filled with the 5 most significant bits of the alpha value.
* i.e. if the target has the format rrrrrggggggbbbbb,
* the encoded pixel will be 00000gggggg00000rrrrr0aaaaabbbbb.
* The <skip> and <run> counts are 8 bit for the opaque lines, 16 bit
* for the translucent lines. Two padding bytes may be inserted
* before each translucent line to keep them 32-bit aligned.
*
* The end of the sequence is marked by a zero <skip>,<run> pair at the
* beginning of an opaque line.
*/
Feb 7, 2006
Feb 7, 2006
89
#include "SDL_video.h"
Apr 26, 2001
Apr 26, 2001
90
91
92
93
94
95
96
97
98
99
100
#include "SDL_sysvideo.h"
#include "SDL_blit.h"
#include "SDL_RLEaccel_c.h"
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
Apr 26, 2001
Apr 26, 2001
101
102
103
#define PIXEL_COPY(to, from, len, bpp) \
do { \
if(bpp == 4) { \
Feb 24, 2006
Feb 24, 2006
104
SDL_memcpy4(to, from, (size_t)(len)); \
Apr 26, 2001
Apr 26, 2001
105
} else { \
Feb 24, 2006
Feb 24, 2006
106
SDL_memcpy(to, from, (size_t)(len) * (bpp)); \
Apr 26, 2001
Apr 26, 2001
107
108
109
} \
} while(0)
Apr 26, 2001
Apr 26, 2001
110
111
112
113
114
/*
* Various colorkey blit methods, for opaque and per-surface alpha
*/
#define OPAQUE_BLIT(to, from, length, bpp, alpha) \
Apr 26, 2001
Apr 26, 2001
115
PIXEL_COPY(to, from, length, bpp)
Apr 26, 2001
Apr 26, 2001
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
/*
* For 32bpp pixels on the form 0x00rrggbb:
* If we treat the middle component separately, we can process the two
* remaining in parallel. This is safe to do because of the gap to the left
* of each component, so the bits from the multiplication don't collide.
* 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)
/*
* For 16bpp pixels we can go a step further: put the middle component
* in the high 16 bits of a 32 bit word, and process all three RGB
* components at the same time. Since the smallest gap is here just
* 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); \
Aug 22, 2003
Aug 22, 2003
153
Uint32 ALPHA = alpha >> 3; \
Apr 26, 2001
Apr 26, 2001
154
155
156
157
158
for(i = 0; i < (int)(length); i++) { \
Uint32 s = *src++; \
Uint32 d = *dst; \
s = (s | s << 16) & 0x07e0f81f; \
d = (d | d << 16) & 0x07e0f81f; \
Aug 22, 2003
Aug 22, 2003
159
d += (s - d) * ALPHA >> 5; \
Apr 26, 2001
Apr 26, 2001
160
d &= 0x07e0f81f; \
Feb 24, 2006
Feb 24, 2006
161
*dst++ = (Uint16)(d | d >> 16); \
Apr 26, 2001
Apr 26, 2001
162
163
164
165
166
167
168
169
} \
} while(0)
#define ALPHA_BLIT16_555(to, from, length, bpp, alpha) \
do { \
int i; \
Uint16 *src = (Uint16 *)(from); \
Uint16 *dst = (Uint16 *)(to); \
Aug 22, 2003
Aug 22, 2003
170
Uint32 ALPHA = alpha >> 3; \
Apr 26, 2001
Apr 26, 2001
171
172
173
174
175
for(i = 0; i < (int)(length); i++) { \
Uint32 s = *src++; \
Uint32 d = *dst; \
s = (s | s << 16) & 0x03e07c1f; \
d = (d | d << 16) & 0x03e07c1f; \
Aug 22, 2003
Aug 22, 2003
176
d += (s - d) * ALPHA >> 5; \
Apr 26, 2001
Apr 26, 2001
177
d &= 0x03e07c1f; \
Feb 24, 2006
Feb 24, 2006
178
*dst++ = (Uint16)(d | d >> 16); \
Apr 26, 2001
Apr 26, 2001
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
} \
} 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: \
Feb 24, 2006
Feb 24, 2006
220
*(Uint16 *)dst = (Uint16)d; \
Apr 26, 2001
Apr 26, 2001
221
222
223
break; \
case 3: \
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { \
Feb 24, 2006
Feb 24, 2006
224
225
226
dst[0] = (Uint8)(d >> 16); \
dst[1] = (Uint8)(d >> 8); \
dst[2] = (Uint8)(d); \
Apr 26, 2001
Apr 26, 2001
227
} else { \
Feb 24, 2006
Feb 24, 2006
228
229
230
dst[0] = (Uint8)d; \
dst[1] = (Uint8)(d >> 8); \
dst[2] = (Uint8)(d >> 16); \
Apr 26, 2001
Apr 26, 2001
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
} \
break; \
case 4: \
*(Uint32 *)dst = d; \
break; \
} \
src += bpp; \
dst += bpp; \
} \
} while(0)
/*
* Special case: 50% alpha (alpha=128)
* This is treated specially because it can be optimized very well, and
* since it is good for many cases of semi-translucency.
* The theory is to do all three components at the same time:
* First zero the lowest bit of each component, which gives us room to
* 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); \
} \
} while(0)
/*
* For 16bpp, we can actually blend two pixels in parallel, if we take
* care to shift before we add, not after.
*/
/* helper: blend a single 16 bit pixel at 50% */
#define BLEND16_50(dst, src, mask) \
do { \
Feb 24, 2006
Feb 24, 2006
271
Uint32 s = *src++; \
Apr 26, 2001
Apr 26, 2001
272
Uint32 d = *dst; \
Feb 24, 2006
Feb 24, 2006
273
274
*dst++ = (Uint16)((((s & mask) + (d & mask)) >> 1) + \
(s & d & (~mask & 0xffff))); \
Apr 26, 2001
Apr 26, 2001
275
276
277
278
279
280
281
282
} 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); \
Mar 1, 2006
Mar 1, 2006
283
if(((uintptr_t)src ^ (uintptr_t)dst) & 3) { \
Apr 26, 2001
Apr 26, 2001
284
285
286
287
/* source and destination not in phase, blit one by one */ \
while(n--) \
BLEND16_50(dst, src, mask); \
} else { \
Mar 1, 2006
Mar 1, 2006
288
if((uintptr_t)src & 3) { \
Apr 26, 2001
Apr 26, 2001
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
/* 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) \
ALPHA_BLIT16_50(to, from, length, bpp, alpha, 0xf7de)
#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; \
} \
} \
} while(0)
/*
* This takes care of the case when the surface is clipped on the left and/or
* right. Top clipping has already been taken care of.
*/
Jul 10, 2006
Jul 10, 2006
385
386
387
static void
RLEClipBlit(int w, Uint8 * srcbuf, SDL_Surface * dst,
Uint8 * dstbuf, SDL_Rect * srcrect, unsigned alpha)
Apr 26, 2001
Apr 26, 2001
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
{
SDL_PixelFormat *fmt = 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 += dst->pitch; \
if(!--linecount) \
break; \
} \
} \
} while(0)
CHOOSE_BLIT(RLECLIPBLIT, alpha, fmt);
#undef RLECLIPBLIT
}
/* blit a colorkeyed RLE surface */
Jul 10, 2006
Jul 10, 2006
443
444
445
int
SDL_RLEBlit(SDL_Surface * src, SDL_Rect * srcrect,
SDL_Surface * dst, SDL_Rect * dstrect)
Apr 26, 2001
Apr 26, 2001
446
{
Jul 10, 2006
Jul 10, 2006
447
448
449
450
451
Uint8 *dstbuf;
Uint8 *srcbuf;
int x, y;
int w = src->w;
unsigned alpha;
Apr 26, 2001
Apr 26, 2001
452
Jul 10, 2006
Jul 10, 2006
453
454
455
456
457
458
/* Lock the destination if necessary */
if (SDL_MUSTLOCK(dst)) {
if (SDL_LockSurface(dst) < 0) {
return (-1);
}
}
Apr 26, 2001
Apr 26, 2001
459
Jul 10, 2006
Jul 10, 2006
460
461
462
463
464
/* Set up the source and destination pointers */
x = dstrect->x;
y = dstrect->y;
dstbuf = (Uint8 *) dst->pixels
+ y * dst->pitch + x * src->format->BytesPerPixel;
Aug 17, 2007
Aug 17, 2007
465
srcbuf = (Uint8 *) src->map->data;
Jul 10, 2006
Jul 10, 2006
466
467
468
469
470
471
{
/* skip lines at the top if neccessary */
int vskip = srcrect->y;
int ofs = 0;
if (vskip) {
Apr 26, 2001
Apr 26, 2001
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#define RLESKIP(bpp, Type) \
for(;;) { \
int run; \
ofs += *(Type *)srcbuf; \
run = ((Type *)srcbuf)[1]; \
srcbuf += sizeof(Type) * 2; \
if(run) { \
srcbuf += run * bpp; \
ofs += run; \
} else if(!ofs) \
goto done; \
if(ofs == w) { \
ofs = 0; \
if(!--vskip) \
break; \
} \
}
Jul 10, 2006
Jul 10, 2006
491
492
493
494
495
496
497
498
499
500
501
502
503
504
switch (src->format->BytesPerPixel) {
case 1:
RLESKIP(1, Uint8);
break;
case 2:
RLESKIP(2, Uint8);
break;
case 3:
RLESKIP(3, Uint8);
break;
case 4:
RLESKIP(4, Uint16);
break;
}
Apr 26, 2001
Apr 26, 2001
505
506
507
#undef RLESKIP
Jul 10, 2006
Jul 10, 2006
508
509
}
}
Apr 26, 2001
Apr 26, 2001
510
Aug 18, 2007
Aug 18, 2007
511
alpha = src->map->info.a;
Jul 10, 2006
Jul 10, 2006
512
513
514
515
516
/* if left or right edge clipping needed, call clip blit */
if (srcrect->x || srcrect->w != src->w) {
RLEClipBlit(w, srcbuf, dst, dstbuf, srcrect, alpha);
} else {
SDL_PixelFormat *fmt = src->format;
Apr 26, 2001
Apr 26, 2001
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
#define RLEBLIT(bpp, Type, do_blit) \
do { \
int linecount = srcrect->h; \
int ofs = 0; \
for(;;) { \
unsigned run; \
ofs += *(Type *)srcbuf; \
run = ((Type *)srcbuf)[1]; \
srcbuf += 2 * sizeof(Type); \
if(run) { \
do_blit(dstbuf + ofs * bpp, srcbuf, run, bpp, alpha); \
srcbuf += run * bpp; \
ofs += run; \
} else if(!ofs) \
break; \
if(ofs == w) { \
ofs = 0; \
dstbuf += dst->pitch; \
if(!--linecount) \
break; \
} \
} \
} while(0)
Jul 10, 2006
Jul 10, 2006
542
CHOOSE_BLIT(RLEBLIT, alpha, fmt);
Apr 26, 2001
Apr 26, 2001
543
544
#undef RLEBLIT
Jul 10, 2006
Jul 10, 2006
545
}
Apr 26, 2001
Apr 26, 2001
546
Jul 10, 2006
Jul 10, 2006
547
548
549
550
551
552
done:
/* Unlock the destination if necessary */
if (SDL_MUSTLOCK(dst)) {
SDL_UnlockSurface(dst);
}
return (0);
Apr 26, 2001
Apr 26, 2001
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
}
#undef OPAQUE_BLIT
/*
* Per-pixel blitting macros for translucent pixels:
* These use the same techniques as the per-surface blitting macros
*/
/*
* For 32bpp pixels, we have made sure the alpha is stored in the top
* 8 bits, so proceed as usual
*/
#define BLIT_TRANSL_888(src, dst) \
do { \
Uint32 s = src; \
Uint32 d = dst; \
unsigned alpha = s >> 24; \
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; \
Dec 1, 2008
Dec 1, 2008
577
dst = d1 | d | 0xff000000; \
Apr 26, 2001
Apr 26, 2001
578
579
580
581
582
583
584
585
} while(0)
/*
* For 16bpp pixels, we have stored the 5 most significant alpha bits in
* bits 5-10. As before, we can process all 3 RGB components at the same time.
*/
#define BLIT_TRANSL_565(src, dst) \
do { \
Feb 24, 2006
Feb 24, 2006
586
Uint32 s = src; \
Apr 26, 2001
Apr 26, 2001
587
588
589
590
591
592
Uint32 d = dst; \
unsigned alpha = (s & 0x3e0) >> 5; \
s &= 0x07e0f81f; \
d = (d | d << 16) & 0x07e0f81f; \
d += (s - d) * alpha >> 5; \
d &= 0x07e0f81f; \
Feb 24, 2006
Feb 24, 2006
593
dst = (Uint16)(d | d >> 16); \
Apr 26, 2001
Apr 26, 2001
594
595
596
597
} while(0)
#define BLIT_TRANSL_555(src, dst) \
do { \
Feb 24, 2006
Feb 24, 2006
598
Uint32 s = src; \
Apr 26, 2001
Apr 26, 2001
599
600
601
602
603
604
Uint32 d = dst; \
unsigned alpha = (s & 0x3e0) >> 5; \
s &= 0x03e07c1f; \
d = (d | d << 16) & 0x03e07c1f; \
d += (s - d) * alpha >> 5; \
d &= 0x03e07c1f; \
Feb 24, 2006
Feb 24, 2006
605
dst = (Uint16)(d | d >> 16); \
Apr 26, 2001
Apr 26, 2001
606
607
608
609
} while(0)
/* used to save the destination format in the encoding. Designed to be
macro-compatible with SDL_PixelFormat but without the unneeded fields */
Jul 10, 2006
Jul 10, 2006
610
611
612
613
614
615
616
617
618
619
620
621
622
623
typedef struct
{
Uint8 BytesPerPixel;
Uint8 Rloss;
Uint8 Gloss;
Uint8 Bloss;
Uint8 Rshift;
Uint8 Gshift;
Uint8 Bshift;
Uint8 Ashift;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
Apr 26, 2001
Apr 26, 2001
624
625
626
} RLEDestFormat;
/* blit a pixel-alpha RLE surface clipped at the right and/or left edges */
Jul 10, 2006
Jul 10, 2006
627
628
629
static void
RLEAlphaClipBlit(int w, Uint8 * srcbuf, SDL_Surface * dst,
Uint8 * dstbuf, SDL_Rect * srcrect)
Apr 26, 2001
Apr 26, 2001
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
{
SDL_PixelFormat *df = dst->format;
/*
* clipped blitter: Ptype is the destination pixel type,
* Ctype the translucent count type, and do_blend the macro
* to blend one pixel.
*/
#define RLEALPHACLIPBLIT(Ptype, Ctype, do_blend) \
do { \
int linecount = srcrect->h; \
int left = srcrect->x; \
int right = left + srcrect->w; \
dstbuf -= left * sizeof(Ptype); \
do { \
int ofs = 0; \
/* blit opaque pixels on one line */ \
do { \
unsigned run; \
ofs += ((Ctype *)srcbuf)[0]; \
run = ((Ctype *)srcbuf)[1]; \
srcbuf += 2 * sizeof(Ctype); \
if(run) { \
/* clip to left and right borders */ \
int cofs = ofs; \
int crun = run; \
if(left - cofs > 0) { \
crun -= left - cofs; \
cofs = left; \
} \
if(crun > right - cofs) \
crun = right - cofs; \
if(crun > 0) \
Apr 26, 2001
Apr 26, 2001
662
PIXEL_COPY(dstbuf + cofs * sizeof(Ptype), \
Apr 26, 2001
Apr 26, 2001
663
srcbuf + (cofs - ofs) * sizeof(Ptype), \
Apr 26, 2001
Apr 26, 2001
664
(unsigned)crun, sizeof(Ptype)); \
Apr 26, 2001
Apr 26, 2001
665
666
667
668
669
670
671
srcbuf += run * sizeof(Ptype); \
ofs += run; \
} else if(!ofs) \
return; \
} while(ofs < w); \
/* skip padding if necessary */ \
if(sizeof(Ptype) == 2) \
Mar 1, 2006
Mar 1, 2006
672
srcbuf += (uintptr_t)srcbuf & 2; \
Apr 26, 2001
Apr 26, 2001
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/* blit translucent pixels on the same line */ \
ofs = 0; \
do { \
unsigned run; \
ofs += ((Uint16 *)srcbuf)[0]; \
run = ((Uint16 *)srcbuf)[1]; \
srcbuf += 4; \
if(run) { \
/* clip to left and right borders */ \
int cofs = ofs; \
int crun = run; \
if(left - cofs > 0) { \
crun -= left - cofs; \
cofs = left; \
} \
if(crun > right - cofs) \
crun = right - cofs; \
if(crun > 0) { \
Ptype *dst = (Ptype *)dstbuf + cofs; \
Uint32 *src = (Uint32 *)srcbuf + (cofs - ofs); \
int i; \
for(i = 0; i < crun; i++) \
do_blend(src[i], dst[i]); \
} \
srcbuf += run * 4; \
ofs += run; \
} \
} while(ofs < w); \
dstbuf += dst->pitch; \
} while(--linecount); \
} while(0)
Jul 10, 2006
Jul 10, 2006
705
switch (df->BytesPerPixel) {
Apr 26, 2001
Apr 26, 2001
706
case 2:
Jul 10, 2006
Jul 10, 2006
707
708
709
710
711
if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0 || df->Bmask == 0x07e0)
RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_565);
else
RLEALPHACLIPBLIT(Uint16, Uint8, BLIT_TRANSL_555);
break;
Apr 26, 2001
Apr 26, 2001
712
case 4:
Jul 10, 2006
Jul 10, 2006
713
714
RLEALPHACLIPBLIT(Uint32, Uint16, BLIT_TRANSL_888);
break;
Apr 26, 2001
Apr 26, 2001
715
716
717
718
}
}
/* blit a pixel-alpha RLE surface */
Jul 10, 2006
Jul 10, 2006
719
720
721
int
SDL_RLEAlphaBlit(SDL_Surface * src, SDL_Rect * srcrect,
SDL_Surface * dst, SDL_Rect * dstrect)
Apr 26, 2001
Apr 26, 2001
722
723
724
725
726
727
728
{
int x, y;
int w = src->w;
Uint8 *srcbuf, *dstbuf;
SDL_PixelFormat *df = dst->format;
/* Lock the destination if necessary */
Jul 10, 2006
Jul 10, 2006
729
730
731
732
if (SDL_MUSTLOCK(dst)) {
if (SDL_LockSurface(dst) < 0) {
return -1;
}
Apr 26, 2001
Apr 26, 2001
733
734
735
736
}
x = dstrect->x;
y = dstrect->y;
Jul 10, 2006
Jul 10, 2006
737
dstbuf = (Uint8 *) dst->pixels + y * dst->pitch + x * df->BytesPerPixel;
Aug 17, 2007
Aug 17, 2007
738
srcbuf = (Uint8 *) src->map->data + sizeof(RLEDestFormat);
Apr 26, 2001
Apr 26, 2001
739
740
{
Jul 10, 2006
Jul 10, 2006
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/* skip lines at the top if necessary */
int vskip = srcrect->y;
if (vskip) {
int ofs;
if (df->BytesPerPixel == 2) {
/* the 16/32 interleaved format */
do {
/* skip opaque line */
ofs = 0;
do {
int run;
ofs += srcbuf[0];
run = srcbuf[1];
srcbuf += 2;
if (run) {
srcbuf += 2 * run;
ofs += run;
} else if (!ofs)
goto done;
Aug 27, 2008
Aug 27, 2008
760
} while (ofs < w);
Jul 10, 2006
Jul 10, 2006
761
762
763
764
765
766
767
768
769
770
771
772
/* skip padding */
srcbuf += (uintptr_t) srcbuf & 2;
/* skip translucent line */
ofs = 0;
do {
int run;
ofs += ((Uint16 *) srcbuf)[0];
run = ((Uint16 *) srcbuf)[1];
srcbuf += 4 * (run + 1);
ofs += run;
Aug 27, 2008
Aug 27, 2008
773
774
} while (ofs < w);
} while (--vskip);
Jul 10, 2006
Jul 10, 2006
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
} else {
/* the 32/32 interleaved format */
vskip <<= 1; /* opaque and translucent have same format */
do {
ofs = 0;
do {
int run;
ofs += ((Uint16 *) srcbuf)[0];
run = ((Uint16 *) srcbuf)[1];
srcbuf += 4;
if (run) {
srcbuf += 4 * run;
ofs += run;
} else if (!ofs)
goto done;
Aug 27, 2008
Aug 27, 2008
790
791
} while (ofs < w);
} while (--vskip);
Jul 10, 2006
Jul 10, 2006
792
793
}
}
Apr 26, 2001
Apr 26, 2001
794
795
796
}
/* if left or right edge clipping needed, call clip blit */
Jul 10, 2006
Jul 10, 2006
797
798
if (srcrect->x || srcrect->w != src->w) {
RLEAlphaClipBlit(w, srcbuf, dst, dstbuf, srcrect);
Apr 26, 2001
Apr 26, 2001
799
800
} else {
Jul 10, 2006
Jul 10, 2006
801
802
803
804
805
/*
* non-clipped blitter. Ptype is the destination pixel type,
* Ctype the translucent count type, and do_blend the
* macro to blend one pixel.
*/
Apr 26, 2001
Apr 26, 2001
806
807
808
809
810
811
812
813
814
815
816
817
#define RLEALPHABLIT(Ptype, Ctype, do_blend) \
do { \
int linecount = srcrect->h; \
do { \
int ofs = 0; \
/* blit opaque pixels on one line */ \
do { \
unsigned run; \
ofs += ((Ctype *)srcbuf)[0]; \
run = ((Ctype *)srcbuf)[1]; \
srcbuf += 2 * sizeof(Ctype); \
if(run) { \
Apr 26, 2001
Apr 26, 2001
818
819
PIXEL_COPY(dstbuf + ofs * sizeof(Ptype), srcbuf, \
run, sizeof(Ptype)); \
Apr 26, 2001
Apr 26, 2001
820
821
822
823
824
825
826
srcbuf += run * sizeof(Ptype); \
ofs += run; \
} else if(!ofs) \
goto done; \
} while(ofs < w); \
/* skip padding if necessary */ \
if(sizeof(Ptype) == 2) \
Mar 1, 2006
Mar 1, 2006
827
srcbuf += (uintptr_t)srcbuf & 2; \
Apr 26, 2001
Apr 26, 2001
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
/* blit translucent pixels on the same line */ \
ofs = 0; \
do { \
unsigned run; \
ofs += ((Uint16 *)srcbuf)[0]; \
run = ((Uint16 *)srcbuf)[1]; \
srcbuf += 4; \
if(run) { \
Ptype *dst = (Ptype *)dstbuf + ofs; \
unsigned i; \
for(i = 0; i < run; i++) { \
Uint32 src = *(Uint32 *)srcbuf; \
do_blend(src, *dst); \
srcbuf += 4; \
dst++; \
} \
ofs += run; \
} \
} while(ofs < w); \
dstbuf += dst->pitch; \
} while(--linecount); \
} while(0)
Jul 10, 2006
Jul 10, 2006
851
852
853
854
855
856
857
858
859
860
861
862
switch (df->BytesPerPixel) {
case 2:
if (df->Gmask == 0x07e0 || df->Rmask == 0x07e0
|| df->Bmask == 0x07e0)
RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_565);
else
RLEALPHABLIT(Uint16, Uint8, BLIT_TRANSL_555);
break;
case 4:
RLEALPHABLIT(Uint32, Uint16, BLIT_TRANSL_888);
break;
}
Apr 26, 2001
Apr 26, 2001
863
864
}
Jul 10, 2006
Jul 10, 2006
865
done:
Apr 26, 2001
Apr 26, 2001
866
/* Unlock the destination if necessary */
Jul 10, 2006
Jul 10, 2006
867
868
if (SDL_MUSTLOCK(dst)) {
SDL_UnlockSurface(dst);
Apr 26, 2001
Apr 26, 2001
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
}
return 0;
}
/*
* Auxiliary functions:
* The encoding functions take 32bpp rgb + a, and
* return the number of bytes copied to the destination.
* The decoding functions copy to 32bpp rgb + a, and
* return the number of bytes copied from the source.
* These are only used in the encoder and un-RLE code and are therefore not
* highly optimised.
*/
/* encode 32bpp rgb + a into 16bpp rgb, losing alpha */
Jul 10, 2006
Jul 10, 2006
884
885
886
static int
copy_opaque_16(void *dst, Uint32 * src, int n,
SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
Apr 26, 2001
Apr 26, 2001
887
888
889
{
int i;
Uint16 *d = dst;
Jul 10, 2006
Jul 10, 2006
890
891
892
893
894
895
for (i = 0; i < n; i++) {
unsigned r, g, b;
RGB_FROM_PIXEL(*src, sfmt, r, g, b);
PIXEL_FROM_RGB(*d, dfmt, r, g, b);
src++;
d++;
Apr 26, 2001
Apr 26, 2001
896
897
898
899
900
}
return n * 2;
}
/* decode opaque pixels from 16bpp to 32bpp rgb + a */
Jul 10, 2006
Jul 10, 2006
901
902
903
static int
uncopy_opaque_16(Uint32 * dst, void *src, int n,
RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
Apr 26, 2001
Apr 26, 2001
904
905
906
907
{
int i;
Uint16 *s = src;
unsigned alpha = dfmt->Amask ? 255 : 0;
Jul 10, 2006
Jul 10, 2006
908
909
910
911
912
913
for (i = 0; i < n; i++) {
unsigned r, g, b;
RGB_FROM_PIXEL(*s, sfmt, r, g, b);
PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, alpha);
s++;
dst++;
Apr 26, 2001
Apr 26, 2001
914
915
916
917
918
919
920
}
return n * 2;
}
/* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 565 */
Jul 10, 2006
Jul 10, 2006
921
922
923
static int
copy_transl_565(void *dst, Uint32 * src, int n,
SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
Apr 26, 2001
Apr 26, 2001
924
925
926
{
int i;
Uint32 *d = dst;
Jul 10, 2006
Jul 10, 2006
927
928
929
930
931
932
933
934
for (i = 0; i < n; i++) {
unsigned r, g, b, a;
Uint16 pix;
RGBA_FROM_8888(*src, sfmt, r, g, b, a);
PIXEL_FROM_RGB(pix, dfmt, r, g, b);
*d = ((pix & 0x7e0) << 16) | (pix & 0xf81f) | ((a << 2) & 0x7e0);
src++;
d++;
Apr 26, 2001
Apr 26, 2001
935
936
937
938
939
}
return n * 4;
}
/* encode 32bpp rgb + a into 32bpp G0RAB format for blitting into 555 */
Jul 10, 2006
Jul 10, 2006
940
941
942
static int
copy_transl_555(void *dst, Uint32 * src, int n,
SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
Apr 26, 2001
Apr 26, 2001
943
944
945
{
int i;
Uint32 *d = dst;
Jul 10, 2006
Jul 10, 2006
946
947
948
949
950
951
952
953
for (i = 0; i < n; i++) {
unsigned r, g, b, a;
Uint16 pix;
RGBA_FROM_8888(*src, sfmt, r, g, b, a);
PIXEL_FROM_RGB(pix, dfmt, r, g, b);
*d = ((pix & 0x3e0) << 16) | (pix & 0xfc1f) | ((a << 2) & 0x3e0);
src++;
d++;
Apr 26, 2001
Apr 26, 2001
954
955
956
957
958
}
return n * 4;
}
/* decode translucent pixels from 32bpp GORAB to 32bpp rgb + a */
Jul 10, 2006
Jul 10, 2006
959
960
961
static int
uncopy_transl_16(Uint32 * dst, void *src, int n,
RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
Apr 26, 2001
Apr 26, 2001
962
963
964
{
int i;
Uint32 *s = src;
Jul 10, 2006
Jul 10, 2006
965
966
967
968
969
970
971
972
for (i = 0; i < n; i++) {
unsigned r, g, b, a;
Uint32 pix = *s++;
a = (pix & 0x3e0) >> 2;
pix = (pix & ~0x3e0) | pix >> 16;
RGB_FROM_PIXEL(pix, sfmt, r, g, b);
PIXEL_FROM_RGBA(*dst, dfmt, r, g, b, a);
dst++;
Apr 26, 2001
Apr 26, 2001
973
974
975
976
977
}
return n * 4;
}
/* encode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
Jul 10, 2006
Jul 10, 2006
978
979
980
static int
copy_32(void *dst, Uint32 * src, int n,
SDL_PixelFormat * sfmt, SDL_PixelFormat * dfmt)
Apr 26, 2001
Apr 26, 2001
981
982
983
{
int i;
Uint32 *d = dst;
Jul 10, 2006
Jul 10, 2006
984
985
986
987
988
989
990
for (i = 0; i < n; i++) {
unsigned r, g, b, a;
Uint32 pixel;
RGBA_FROM_8888(*src, sfmt, r, g, b, a);
PIXEL_FROM_RGB(pixel, dfmt, r, g, b);
*d++ = pixel | a << 24;
src++;
Apr 26, 2001
Apr 26, 2001
991
992
993
994
995
}
return n * 4;
}
/* decode 32bpp rgba into 32bpp rgba, keeping alpha (dual purpose) */
Jul 10, 2006
Jul 10, 2006
996
997
998
static int
uncopy_32(Uint32 * dst, void *src, int n,
RLEDestFormat * sfmt, SDL_PixelFormat * dfmt)
Apr 26, 2001
Apr 26, 2001
999
1000
{
int i;