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