author | rubidium |
Wed, 09 Jul 2008 13:32:13 +0000 | |
branch | noai |
changeset 11126 | 72d4c9314c72 |
parent 11044 | 097ea3e7ec56 |
permissions | -rw-r--r-- |
9627 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
9723
diff
changeset
|
3 |
/** @file 8bpp_optimized.cpp Implementation of the optimized 8 bpp blitter. */ |
9627 | 4 |
|
9626 | 5 |
#include "../stdafx.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9631
diff
changeset
|
6 |
#include "../zoom_func.h" |
9626 | 7 |
#include "../debug.h" |
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9631
diff
changeset
|
8 |
#include "../core/alloc_func.hpp" |
10829 | 9 |
#include "../core/math_func.hpp" |
9626 | 10 |
#include "8bpp_optimized.hpp" |
11 |
||
12 |
static FBlitter_8bppOptimized iFBlitter_8bppOptimized; |
|
13 |
||
14 |
void Blitter_8bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) |
|
15 |
{ |
|
16 |
/* Find the offset of this zoom-level */ |
|
11044
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
17 |
const SpriteData *sprite_src = (const SpriteData *)bp->sprite; |
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
18 |
uint offset = sprite_src->offset[zoom]; |
9626 | 19 |
|
20 |
/* Find where to start reading in the source sprite */ |
|
11044
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
21 |
const uint8 *src = sprite_src->data + offset; |
10829 | 22 |
uint8 *dst_line = (uint8 *)bp->dst + bp->top * bp->pitch + bp->left; |
9626 | 23 |
|
24 |
/* Skip over the top lines in the source image */ |
|
25 |
for (int y = 0; y < bp->skip_top; y++) { |
|
26 |
for (;;) { |
|
10829 | 27 |
uint trans = *src++; |
28 |
uint pixels = *src++; |
|
9626 | 29 |
if (trans == 0 && pixels == 0) break; |
30 |
src += pixels; |
|
31 |
} |
|
32 |
} |
|
33 |
||
10829 | 34 |
const uint8 *src_next = src; |
9626 | 35 |
|
36 |
for (int y = 0; y < bp->height; y++) { |
|
10829 | 37 |
uint8 *dst = dst_line; |
9626 | 38 |
dst_line += bp->pitch; |
39 |
||
40 |
uint skip_left = bp->skip_left; |
|
41 |
int width = bp->width; |
|
42 |
||
43 |
for (;;) { |
|
44 |
src = src_next; |
|
10829 | 45 |
uint trans = *src++; |
46 |
uint pixels = *src++; |
|
9626 | 47 |
src_next = src + pixels; |
48 |
if (trans == 0 && pixels == 0) break; |
|
49 |
if (width <= 0) continue; |
|
50 |
||
51 |
if (skip_left != 0) { |
|
52 |
if (skip_left < trans) { |
|
53 |
trans -= skip_left; |
|
54 |
skip_left = 0; |
|
55 |
} else { |
|
56 |
skip_left -= trans; |
|
57 |
trans = 0; |
|
58 |
} |
|
59 |
if (skip_left < pixels) { |
|
60 |
src += skip_left; |
|
61 |
pixels -= skip_left; |
|
62 |
skip_left = 0; |
|
63 |
} else { |
|
64 |
src += pixels; |
|
65 |
skip_left -= pixels; |
|
66 |
pixels = 0; |
|
67 |
} |
|
68 |
} |
|
69 |
if (skip_left != 0) continue; |
|
70 |
||
71 |
/* Skip transparent pixels */ |
|
72 |
dst += trans; |
|
73 |
width -= trans; |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
74 |
if (width <= 0 || pixels == 0) continue; |
10829 | 75 |
pixels = min<uint>(pixels, (uint)width); |
9626 | 76 |
width -= pixels; |
77 |
||
78 |
switch (mode) { |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
79 |
case BM_COLOUR_REMAP: { |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
80 |
const uint8 *remap = bp->remap; |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
81 |
do { |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
82 |
uint m = remap[*src]; |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
83 |
if (m != 0) *dst = m; |
9626 | 84 |
dst++; src++; |
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
85 |
} while (--pixels != 0); |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
86 |
} break; |
9626 | 87 |
|
11126
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
88 |
case BM_TRANSPARENT: { |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
89 |
const uint8 *remap = bp->remap; |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
90 |
src += pixels; |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
91 |
do { |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
92 |
*dst = remap[*dst]; |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
93 |
dst++; |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
94 |
} while (--pixels != 0); |
72d4c9314c72
(svn r13684) [NoAI] -Sync: with trunk r13599:13683.
rubidium
parents:
11044
diff
changeset
|
95 |
} break; |
9626 | 96 |
|
97 |
default: |
|
98 |
memcpy(dst, src, pixels); |
|
99 |
dst += pixels; src += pixels; |
|
100 |
break; |
|
101 |
} |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
Sprite *Blitter_8bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator) |
|
107 |
{ |
|
10829 | 108 |
/* Make memory for all zoom-levels */ |
11044
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
109 |
uint memory = sizeof(SpriteData); |
9626 | 110 |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9631
diff
changeset
|
111 |
for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) { |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9631
diff
changeset
|
112 |
memory += UnScaleByZoom(sprite->height, i) * UnScaleByZoom(sprite->width, i); |
9626 | 113 |
} |
114 |
||
115 |
/* We have no idea how much memory we really need, so just guess something */ |
|
116 |
memory *= 5; |
|
11044
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
117 |
SpriteData *temp_dst = (SpriteData *)MallocT<byte>(memory); |
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
118 |
byte *dst = temp_dst->data; |
9626 | 119 |
|
120 |
/* Make the sprites per zoom-level */ |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9631
diff
changeset
|
121 |
for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) { |
9626 | 122 |
/* Store the index table */ |
11044
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
123 |
uint offset = dst - temp_dst->data; |
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
124 |
temp_dst->offset[i] = offset; |
9626 | 125 |
|
10829 | 126 |
/* cache values, because compiler can't cache it */ |
127 |
int scaled_height = UnScaleByZoom(sprite->height, i); |
|
128 |
int scaled_width = UnScaleByZoom(sprite->width, i); |
|
129 |
int scaled_1 = ScaleByZoom(1, i); |
|
9626 | 130 |
|
10829 | 131 |
for (int y = 0; y < scaled_height; y++) { |
9626 | 132 |
uint trans = 0; |
133 |
uint pixels = 0; |
|
10829 | 134 |
uint last_colour = 0; |
135 |
byte *count_dst = NULL; |
|
9626 | 136 |
|
10829 | 137 |
/* Store the scaled image */ |
138 |
const SpriteLoader::CommonPixel *src = &sprite->data[ScaleByZoom(y, i) * sprite->width]; |
|
139 |
const SpriteLoader::CommonPixel *src_end = &src[sprite->width]; |
|
140 |
||
141 |
for (int x = 0; x < scaled_width; x++) { |
|
142 |
uint colour = 0; |
|
9626 | 143 |
|
144 |
/* Get the color keeping in mind the zoom-level */ |
|
10829 | 145 |
for (int j = 0; j < scaled_1; j++) { |
146 |
if (src->m != 0) colour = src->m; |
|
9626 | 147 |
/* Because of the scaling it might happen we read outside the buffer. Avoid that. */ |
10829 | 148 |
if (++src == src_end) break; |
9626 | 149 |
} |
150 |
||
10829 | 151 |
if (last_colour == 0 || colour == 0 || pixels == 255) { |
152 |
if (count_dst != NULL) { |
|
9626 | 153 |
/* Write how many non-transparent bytes we get */ |
10829 | 154 |
*count_dst = pixels; |
9626 | 155 |
pixels = 0; |
10829 | 156 |
count_dst = NULL; |
9626 | 157 |
} |
158 |
/* As long as we find transparency bytes, keep counting */ |
|
10829 | 159 |
if (colour == 0) { |
160 |
last_colour = 0; |
|
9626 | 161 |
trans++; |
162 |
continue; |
|
163 |
} |
|
164 |
/* No longer transparency, so write the amount of transparent bytes */ |
|
165 |
*dst = trans; |
|
10829 | 166 |
dst++; |
9626 | 167 |
trans = 0; |
168 |
/* Reserve a byte for the pixel counter */ |
|
10829 | 169 |
count_dst = dst; |
170 |
dst++; |
|
9626 | 171 |
} |
10829 | 172 |
last_colour = colour; |
9626 | 173 |
pixels++; |
10829 | 174 |
*dst = colour; |
175 |
dst++; |
|
9626 | 176 |
} |
177 |
||
10829 | 178 |
if (count_dst != NULL) *count_dst = pixels; |
9626 | 179 |
|
180 |
/* Write line-ending */ |
|
10829 | 181 |
*dst = 0; dst++; |
182 |
*dst = 0; dst++; |
|
9626 | 183 |
} |
184 |
} |
|
185 |
||
11044
097ea3e7ec56
(svn r13600) [NoAI] -Sync: with trunk r13508:13599.
rubidium
parents:
10829
diff
changeset
|
186 |
uint size = dst - (byte *)temp_dst; |
10829 | 187 |
|
9626 | 188 |
/* Safety check, to make sure we guessed the size correctly */ |
10829 | 189 |
assert(size < memory); |
9626 | 190 |
|
191 |
/* Allocate the exact amount of memory we need */ |
|
10829 | 192 |
Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + size); |
9626 | 193 |
|
194 |
dest_sprite->height = sprite->height; |
|
195 |
dest_sprite->width = sprite->width; |
|
196 |
dest_sprite->x_offs = sprite->x_offs; |
|
197 |
dest_sprite->y_offs = sprite->y_offs; |
|
10829 | 198 |
memcpy(dest_sprite->data, temp_dst, size); |
9631
8a2d1c2ceb88
(svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents:
9629
diff
changeset
|
199 |
free(temp_dst); |
9626 | 200 |
|
201 |
return dest_sprite; |
|
202 |
} |