This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_blit_1.c
553 lines (509 loc) · 12.2 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
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
13
Lesser General Public License for more details.
14
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
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
26
27
28
29
30
#include "SDL_video.h"
#include "SDL_blit.h"
#include "SDL_sysvideo.h"
#include "SDL_endian.h"
/* Functions to blit from 8-bit surfaces to other surfaces */
31
32
static void
Blit1to1(SDL_BlitInfo * info)
33
34
{
#ifndef USE_DUFFS_LOOP
35
int c;
36
#endif
37
38
39
40
41
int width, height;
Uint8 *src, *map, *dst;
int srcskip, dstskip;
/* Set up some basic variables */
42
43
44
width = info->dst_w;
height = info->dst_h;
src = info->src;
45
srcskip = info->src_skip;
46
dst = info->dst;
47
dstskip = info->dst_skip;
48
49
50
map = info->table;
while (height--) {
51
#ifdef USE_DUFFS_LOOP
52
/* *INDENT-OFF* */
53
54
55
56
57
58
59
DUFFS_LOOP(
{
*dst = map[*src];
}
dst++;
src++;
, width);
60
/* *INDENT-ON* */
61
#else
62
63
64
65
66
for (c = width; c; --c) {
*dst = map[*src];
dst++;
src++;
}
67
#endif
68
69
70
src += srcskip;
dst += dstskip;
}
71
}
72
73
74
75
76
77
78
79
80
/* This is now endian dependent */
#if ( SDL_BYTEORDER == SDL_LIL_ENDIAN )
#define HI 1
#define LO 0
#else /* ( SDL_BYTEORDER == SDL_BIG_ENDIAN ) */
#define HI 0
#define LO 1
#endif
81
82
static void
Blit1to2(SDL_BlitInfo * info)
83
84
{
#ifndef USE_DUFFS_LOOP
85
int c;
86
#endif
87
88
89
90
91
92
int width, height;
Uint8 *src, *dst;
Uint16 *map;
int srcskip, dstskip;
/* Set up some basic variables */
93
94
95
width = info->dst_w;
height = info->dst_h;
src = info->src;
96
srcskip = info->src_skip;
97
dst = info->dst;
98
dstskip = info->dst_skip;
99
map = (Uint16 *) info->table;
100
101
#ifdef USE_DUFFS_LOOP
102
103
while (height--) {
/* *INDENT-OFF* */
104
105
106
107
108
109
DUFFS_LOOP(
{
*(Uint16 *)dst = map[*src++];
dst += 2;
},
width);
110
111
112
113
/* *INDENT-ON* */
src += srcskip;
dst += dstskip;
}
114
#else
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
/* Memory align at 4-byte boundary, if necessary */
if ((long) dst & 0x03) {
/* Don't do anything if width is 0 */
if (width == 0) {
return;
}
--width;
while (height--) {
/* Perform copy alignment */
*(Uint16 *) dst = map[*src++];
dst += 2;
/* Copy in 4 pixel chunks */
for (c = width / 4; c; --c) {
*(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
src += 2;
dst += 4;
*(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
src += 2;
dst += 4;
}
/* Get any leftovers */
switch (width & 3) {
case 3:
*(Uint16 *) dst = map[*src++];
dst += 2;
case 2:
*(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
src += 2;
dst += 4;
break;
case 1:
*(Uint16 *) dst = map[*src++];
dst += 2;
break;
}
src += srcskip;
dst += dstskip;
}
} else {
while (height--) {
/* Copy in 4 pixel chunks */
for (c = width / 4; c; --c) {
*(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
src += 2;
dst += 4;
*(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
src += 2;
dst += 4;
}
/* Get any leftovers */
switch (width & 3) {
case 3:
*(Uint16 *) dst = map[*src++];
dst += 2;
case 2:
*(Uint32 *) dst = (map[src[HI]] << 16) | (map[src[LO]]);
src += 2;
dst += 4;
break;
case 1:
*(Uint16 *) dst = map[*src++];
dst += 2;
break;
}
src += srcskip;
dst += dstskip;
}
}
185
186
#endif /* USE_DUFFS_LOOP */
}
187
188
189
static void
Blit1to3(SDL_BlitInfo * info)
190
191
{
#ifndef USE_DUFFS_LOOP
192
int c;
193
#endif
194
195
196
197
198
199
int o;
int width, height;
Uint8 *src, *map, *dst;
int srcskip, dstskip;
/* Set up some basic variables */
200
201
202
width = info->dst_w;
height = info->dst_h;
src = info->src;
203
srcskip = info->src_skip;
204
dst = info->dst;
205
dstskip = info->dst_skip;
206
207
208
map = info->table;
while (height--) {
209
#ifdef USE_DUFFS_LOOP
210
/* *INDENT-OFF* */
211
212
213
214
215
216
217
218
219
220
DUFFS_LOOP(
{
o = *src * 4;
dst[0] = map[o++];
dst[1] = map[o++];
dst[2] = map[o++];
}
src++;
dst += 3;
, width);
221
/* *INDENT-ON* */
222
#else
223
224
225
226
227
228
229
230
for (c = width; c; --c) {
o = *src * 4;
dst[0] = map[o++];
dst[1] = map[o++];
dst[2] = map[o++];
src++;
dst += 3;
}
231
#endif /* USE_DUFFS_LOOP */
232
233
234
src += srcskip;
dst += dstskip;
}
235
}
236
237
238
static void
Blit1to4(SDL_BlitInfo * info)
239
240
{
#ifndef USE_DUFFS_LOOP
241
int c;
242
#endif
243
244
245
246
247
248
int width, height;
Uint8 *src;
Uint32 *map, *dst;
int srcskip, dstskip;
/* Set up some basic variables */
249
250
251
width = info->dst_w;
height = info->dst_h;
src = info->src;
252
srcskip = info->src_skip;
253
dst = (Uint32 *) info->dst;
254
dstskip = info->dst_skip / 4;
255
256
257
map = (Uint32 *) info->table;
while (height--) {
258
#ifdef USE_DUFFS_LOOP
259
/* *INDENT-OFF* */
260
261
262
DUFFS_LOOP(
*dst++ = map[*src++];
, width);
263
/* *INDENT-ON* */
264
#else
265
266
267
268
269
270
271
272
273
274
275
276
277
278
for (c = width / 4; c; --c) {
*dst++ = map[*src++];
*dst++ = map[*src++];
*dst++ = map[*src++];
*dst++ = map[*src++];
}
switch (width & 3) {
case 3:
*dst++ = map[*src++];
case 2:
*dst++ = map[*src++];
case 1:
*dst++ = map[*src++];
}
279
#endif /* USE_DUFFS_LOOP */
280
281
282
src += srcskip;
dst += dstskip;
}
283
284
}
285
286
static void
Blit1to1Key(SDL_BlitInfo * info)
287
{
288
289
290
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
291
int srcskip = info->src_skip;
292
Uint8 *dst = info->dst;
293
int dstskip = info->dst_skip;
294
Uint8 *palmap = info->table;
295
Uint32 ckey = info->colorkey;
296
297
298
299
if (palmap) {
while (height--) {
/* *INDENT-OFF* */
300
301
302
303
304
305
306
307
308
DUFFS_LOOP(
{
if ( *src != ckey ) {
*dst = palmap[*src];
}
dst++;
src++;
},
width);
309
310
311
312
313
314
315
/* *INDENT-ON* */
src += srcskip;
dst += dstskip;
}
} else {
while (height--) {
/* *INDENT-OFF* */
316
317
318
319
320
321
322
323
324
DUFFS_LOOP(
{
if ( *src != ckey ) {
*dst = *src;
}
dst++;
src++;
},
width);
325
326
327
328
329
/* *INDENT-ON* */
src += srcskip;
dst += dstskip;
}
}
330
331
}
332
333
static void
Blit1to2Key(SDL_BlitInfo * info)
334
{
335
336
337
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
338
int srcskip = info->src_skip;
339
Uint16 *dstp = (Uint16 *) info->dst;
340
int dstskip = info->dst_skip;
341
Uint16 *palmap = (Uint16 *) info->table;
342
Uint32 ckey = info->colorkey;
343
344
345
346
347
348
/* Set up some basic variables */
dstskip /= 2;
while (height--) {
/* *INDENT-OFF* */
349
350
351
352
353
354
355
356
357
DUFFS_LOOP(
{
if ( *src != ckey ) {
*dstp=palmap[*src];
}
src++;
dstp++;
},
width);
358
359
360
361
/* *INDENT-ON* */
src += srcskip;
dstp += dstskip;
}
362
363
}
364
365
static void
Blit1to3Key(SDL_BlitInfo * info)
366
{
367
368
369
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
370
int srcskip = info->src_skip;
371
Uint8 *dst = info->dst;
372
int dstskip = info->dst_skip;
373
Uint8 *palmap = info->table;
374
Uint32 ckey = info->colorkey;
375
376
377
378
int o;
while (height--) {
/* *INDENT-OFF* */
379
380
381
382
383
384
385
386
387
388
389
390
DUFFS_LOOP(
{
if ( *src != ckey ) {
o = *src * 4;
dst[0] = palmap[o++];
dst[1] = palmap[o++];
dst[2] = palmap[o++];
}
src++;
dst += 3;
},
width);
391
392
393
394
/* *INDENT-ON* */
src += srcskip;
dst += dstskip;
}
395
396
}
397
398
static void
Blit1to4Key(SDL_BlitInfo * info)
399
{
400
401
402
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
403
int srcskip = info->src_skip;
404
Uint32 *dstp = (Uint32 *) info->dst;
405
int dstskip = info->dst_skip;
406
Uint32 *palmap = (Uint32 *) info->table;
407
Uint32 ckey = info->colorkey;
408
409
410
411
412
413
/* Set up some basic variables */
dstskip /= 4;
while (height--) {
/* *INDENT-OFF* */
414
415
416
417
418
419
420
421
422
DUFFS_LOOP(
{
if ( *src != ckey ) {
*dstp = palmap[*src];
}
src++;
dstp++;
},
width);
423
424
425
426
/* *INDENT-ON* */
src += srcskip;
dstp += dstskip;
}
427
428
}
429
430
static void
Blit1toNAlpha(SDL_BlitInfo * info)
431
{
432
433
434
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
435
int srcskip = info->src_skip;
436
Uint8 *dst = info->dst;
437
438
439
int dstskip = info->dst_skip;
SDL_PixelFormat *dstfmt = info->dst_fmt;
const SDL_Color *srcpal = info->src_fmt->palette->colors;
440
int dstbpp;
441
const int A = info->a;
442
443
444
445
446
447
448
449
/* Set up some basic variables */
dstbpp = dstfmt->BytesPerPixel;
while (height--) {
int sR, sG, sB;
int dR, dG, dB;
/* *INDENT-OFF* */
450
451
452
453
454
455
456
457
458
459
460
461
462
463
DUFFS_LOOP4(
{
Uint32 pixel;
sR = srcpal[*src].r;
sG = srcpal[*src].g;
sB = srcpal[*src].b;
DISEMBLE_RGB(dst, dstbpp, dstfmt,
pixel, dR, dG, dB);
ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
src++;
dst += dstbpp;
},
width);
464
465
466
467
/* *INDENT-ON* */
src += srcskip;
dst += dstskip;
}
468
469
}
470
471
static void
Blit1toNAlphaKey(SDL_BlitInfo * info)
472
{
473
474
475
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
476
int srcskip = info->src_skip;
477
Uint8 *dst = info->dst;
478
479
480
481
int dstskip = info->dst_skip;
SDL_PixelFormat *dstfmt = info->dst_fmt;
const SDL_Color *srcpal = info->src_fmt->palette->colors;
Uint32 ckey = info->colorkey;
482
int dstbpp;
483
const int A = info->a;
484
485
486
487
488
489
490
491
/* Set up some basic variables */
dstbpp = dstfmt->BytesPerPixel;
while (height--) {
int sR, sG, sB;
int dR, dG, dB;
/* *INDENT-OFF* */
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
DUFFS_LOOP(
{
if ( *src != ckey ) {
Uint32 pixel;
sR = srcpal[*src].r;
sG = srcpal[*src].g;
sB = srcpal[*src].b;
DISEMBLE_RGB(dst, dstbpp, dstfmt,
pixel, dR, dG, dB);
ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
ASSEMBLE_RGB(dst, dstbpp, dstfmt, dR, dG, dB);
}
src++;
dst += dstbpp;
},
width);
508
509
510
511
/* *INDENT-ON* */
src += srcskip;
dst += dstskip;
}
512
513
}
514
static const SDL_BlitFunc one_blit[] = {
515
NULL, Blit1to1, Blit1to2, Blit1to3, Blit1to4
516
517
};
518
static const SDL_BlitFunc one_blitkey[] = {
519
NULL, Blit1to1Key, Blit1to2Key, Blit1to3Key, Blit1to4Key
520
521
};
522
523
SDL_BlitFunc
SDL_CalculateBlit1(SDL_Surface * surface)
524
{
525
526
527
528
529
530
531
532
533
int which;
SDL_PixelFormat *dstfmt;
dstfmt = surface->map->dst->format;
if (dstfmt->BitsPerPixel < 8) {
which = 0;
} else {
which = dstfmt->BytesPerPixel;
}
534
switch (surface->map->info.flags & ~SDL_COPY_RLE_MASK) {
535
case 0:
536
537
return one_blit[which];
538
case SDL_COPY_COLORKEY:
539
540
return one_blitkey[which];
541
case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
542
543
544
545
546
/* Supporting 8bpp->8bpp alpha is doable but requires lots of
tables which consume space and takes time to precompute,
so is better left to the user */
return which >= 2 ? Blit1toNAlpha : NULL;
547
case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
548
549
550
return which >= 2 ? Blit1toNAlphaKey : NULL;
}
return NULL;
551
}
552
553
/* vi: set ts=4 sw=4 expandtab: */