This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_blit_0.c
483 lines (440 loc) · 12 KB
1
/*
2
3
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
8
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
12
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
*/
21
#include "SDL_config.h"
22
23
24
25
26
27
#include "SDL_video.h"
#include "SDL_blit.h"
/* Functions to blit from bitmaps to other surfaces */
28
29
static void
BlitBto1(SDL_BlitInfo * info)
30
{
31
32
33
34
35
36
int c;
int width, height;
Uint8 *src, *map, *dst;
int srcskip, dstskip;
/* Set up some basic variables */
37
38
39
width = info->dst_w;
height = info->dst_h;
src = info->src;
40
srcskip = info->src_skip;
41
dst = info->dst;
42
dstskip = info->dst_skip;
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
map = info->table;
srcskip += width - (width + 7) / 8;
if (map) {
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (1) {
*dst = map[bit];
}
dst++;
byte <<= 1;
}
src += srcskip;
dst += dstskip;
}
} else {
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (1) {
*dst = bit;
}
dst++;
byte <<= 1;
}
src += srcskip;
dst += dstskip;
}
}
81
}
82
83
84
static void
BlitBto2(SDL_BlitInfo * info)
85
{
86
87
88
89
90
91
92
int c;
int width, height;
Uint8 *src;
Uint16 *map, *dst;
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 = (Uint16 *) info->dst;
98
dstskip = info->dst_skip / 2;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
map = (Uint16 *) info->table;
srcskip += width - (width + 7) / 8;
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (1) {
*dst = map[bit];
}
byte <<= 1;
dst++;
}
src += srcskip;
dst += dstskip;
}
118
}
119
120
121
static void
BlitBto3(SDL_BlitInfo * info)
122
{
123
124
125
126
127
128
int c, o;
int width, height;
Uint8 *src, *map, *dst;
int srcskip, dstskip;
/* Set up some basic variables */
129
130
131
width = info->dst_w;
height = info->dst_h;
src = info->src;
132
srcskip = info->src_skip;
133
dst = info->dst;
134
dstskip = info->dst_skip;
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
map = info->table;
srcskip += width - (width + 7) / 8;
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (1) {
o = bit * 4;
dst[0] = map[o++];
dst[1] = map[o++];
dst[2] = map[o++];
}
byte <<= 1;
dst += 3;
}
src += srcskip;
dst += dstskip;
}
157
}
158
159
160
static void
BlitBto4(SDL_BlitInfo * info)
161
{
162
163
164
165
166
167
168
int width, height;
Uint8 *src;
Uint32 *map, *dst;
int srcskip, dstskip;
int c;
/* Set up some basic variables */
169
170
171
width = info->dst_w;
height = info->dst_h;
src = info->src;
172
srcskip = info->src_skip;
173
dst = (Uint32 *) info->dst;
174
dstskip = info->dst_skip / 4;
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
map = (Uint32 *) info->table;
srcskip += width - (width + 7) / 8;
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (1) {
*dst = map[bit];
}
byte <<= 1;
dst++;
}
src += srcskip;
dst += dstskip;
}
194
195
}
196
197
static void
BlitBto1Key(SDL_BlitInfo * info)
198
{
199
200
201
202
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
Uint8 *dst = info->dst;
203
204
205
int srcskip = info->src_skip;
int dstskip = info->dst_skip;
Uint32 ckey = info->colorkey;
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
Uint8 *palmap = info->table;
int c;
/* Set up some basic variables */
srcskip += width - (width + 7) / 8;
if (palmap) {
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (bit != ckey) {
*dst = palmap[bit];
}
dst++;
byte <<= 1;
}
src += srcskip;
dst += dstskip;
}
} else {
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (bit != ckey) {
*dst = bit;
}
dst++;
byte <<= 1;
}
src += srcskip;
dst += dstskip;
}
}
247
248
}
249
250
static void
BlitBto2Key(SDL_BlitInfo * info)
251
{
252
253
254
255
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
Uint16 *dstp = (Uint16 *) info->dst;
256
257
258
int srcskip = info->src_skip;
int dstskip = info->dst_skip;
Uint32 ckey = info->colorkey;
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
Uint8 *palmap = info->table;
int c;
/* Set up some basic variables */
srcskip += width - (width + 7) / 8;
dstskip /= 2;
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (bit != ckey) {
*dstp = ((Uint16 *) palmap)[bit];
}
byte <<= 1;
dstp++;
}
src += srcskip;
dstp += dstskip;
}
282
283
}
284
285
static void
BlitBto3Key(SDL_BlitInfo * info)
286
{
287
288
289
290
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
Uint8 *dst = info->dst;
291
292
293
int srcskip = info->src_skip;
int dstskip = info->dst_skip;
Uint32 ckey = info->colorkey;
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
Uint8 *palmap = info->table;
int c;
/* Set up some basic variables */
srcskip += width - (width + 7) / 8;
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (bit != ckey) {
SDL_memcpy(dst, &palmap[bit * 4], 3);
}
byte <<= 1;
dst += 3;
}
src += srcskip;
dst += dstskip;
}
316
317
}
318
319
static void
BlitBto4Key(SDL_BlitInfo * info)
320
{
321
322
323
324
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
Uint32 *dstp = (Uint32 *) info->dst;
325
326
327
int srcskip = info->src_skip;
int dstskip = info->dst_skip;
Uint32 ckey = info->colorkey;
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
Uint8 *palmap = info->table;
int c;
/* Set up some basic variables */
srcskip += width - (width + 7) / 8;
dstskip /= 4;
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (bit != ckey) {
*dstp = ((Uint32 *) palmap)[bit];
}
byte <<= 1;
dstp++;
}
src += srcskip;
dstp += dstskip;
}
351
352
}
353
354
static void
BlitBtoNAlpha(SDL_BlitInfo * info)
355
{
356
357
358
359
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
Uint8 *dst = info->dst;
360
361
362
363
int srcskip = info->src_skip;
int dstskip = info->dst_skip;
const SDL_Color *srcpal = info->src_fmt->palette->colors;
SDL_PixelFormat *dstfmt = info->dst_fmt;
364
365
int dstbpp;
int c;
366
const int A = info->a;
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
/* Set up some basic variables */
dstbpp = dstfmt->BytesPerPixel;
srcskip += width - (width + 7) / 8;
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (1) {
Uint32 pixel;
unsigned sR, sG, sB;
unsigned dR, dG, dB;
sR = srcpal[bit].r;
sG = srcpal[bit].g;
sB = srcpal[bit].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);
}
byte <<= 1;
dst += dstbpp;
}
src += srcskip;
dst += dstskip;
}
396
397
}
398
399
static void
BlitBtoNAlphaKey(SDL_BlitInfo * info)
400
{
401
402
403
404
int width = info->dst_w;
int height = info->dst_h;
Uint8 *src = info->src;
Uint8 *dst = info->dst;
405
406
407
408
int srcskip = info->src_skip;
int dstskip = info->dst_skip;
SDL_PixelFormat *srcfmt = info->src_fmt;
SDL_PixelFormat *dstfmt = info->dst_fmt;
409
410
411
const SDL_Color *srcpal = srcfmt->palette->colors;
int dstbpp;
int c;
412
413
const int A = info->a;
Uint32 ckey = info->colorkey;
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
/* Set up some basic variables */
dstbpp = dstfmt->BytesPerPixel;
srcskip += width - (width + 7) / 8;
while (height--) {
Uint8 byte = 0, bit;
for (c = 0; c < width; ++c) {
if ((c & 7) == 0) {
byte = *src++;
}
bit = (byte & 0x80) >> 7;
if (bit != ckey) {
int sR, sG, sB;
int dR, dG, dB;
Uint32 pixel;
sR = srcpal[bit].r;
sG = srcpal[bit].g;
sB = srcpal[bit].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);
}
byte <<= 1;
dst += dstbpp;
}
src += srcskip;
dst += dstskip;
}
443
444
}
445
static const SDL_BlitFunc bitmap_blit[] = {
446
NULL, BlitBto1, BlitBto2, BlitBto3, BlitBto4
447
448
};
449
static const SDL_BlitFunc colorkey_blit[] = {
450
451
452
NULL, BlitBto1Key, BlitBto2Key, BlitBto3Key, BlitBto4Key
};
453
454
SDL_BlitFunc
SDL_CalculateBlit0(SDL_Surface * surface)
455
{
456
457
458
459
460
461
462
463
464
465
466
int which;
if (surface->format->BitsPerPixel != 1) {
/* We don't support sub 8-bit packed pixel modes */
return NULL;
}
if (surface->map->dst->format->BitsPerPixel < 8) {
which = 0;
} else {
which = surface->map->dst->format->BytesPerPixel;
}
467
switch (surface->map->info.flags & ~SDL_COPY_RLE_MASK) {
468
case 0:
469
470
return bitmap_blit[which];
471
case SDL_COPY_COLORKEY:
472
473
return colorkey_blit[which];
474
case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
475
476
return which >= 2 ? BlitBtoNAlpha : NULL;
477
case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
478
479
480
return which >= 2 ? BlitBtoNAlphaKey : NULL;
}
return NULL;
481
482
}
483
/* vi: set ts=4 sw=4 expandtab: */