|
1 /* $Id$ */ |
|
2 |
|
3 /** @file 8bpp_optimized.cpp */ |
|
4 |
|
5 #include "../stdafx.h" |
|
6 #include "../zoom.hpp" |
|
7 #include "../gfx.h" |
|
8 #include "../debug.h" |
|
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 { |
|
15 const uint8 *src, *src_next; |
|
16 uint8 *dst, *dst_line; |
|
17 uint offset = 0; |
|
18 |
|
19 /* Find the offset of this zoom-level */ |
|
20 offset = ((const uint8 *)bp->sprite)[(int)zoom * 2] | ((const byte *)bp->sprite)[(int)zoom * 2 + 1] << 8; |
|
21 |
|
22 /* Find where to start reading in the source sprite */ |
|
23 src = (const uint8 *)bp->sprite + offset; |
|
24 dst_line = (uint8 *)bp->dst + bp->top * bp->pitch + bp->left; |
|
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 */ |
|
113 memory += (int)ZOOM_LVL_END * sizeof(uint16); |
|
114 for (int i = 0; i < (int)ZOOM_LVL_END; i++) { |
|
115 memory += UnScaleByZoom(sprite->height, (ZoomLevel)i) * UnScaleByZoom(sprite->width, (ZoomLevel)i); |
|
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 */ |
|
124 for (int i = 0; i < (int)ZOOM_LVL_END; i++) { |
|
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 |
|
134 for (int y = 0; y < UnScaleByZoom(sprite->height, (ZoomLevel)i); y++) { |
|
135 uint trans = 0; |
|
136 uint pixels = 0; |
|
137 uint last_color = 0; |
|
138 uint count_index = 0; |
|
139 uint rx = 0; |
|
140 src = &sprite->data[ScaleByZoom(y, (ZoomLevel)i) * sprite->width]; |
|
141 |
|
142 for (int x = 0; x < UnScaleByZoom(sprite->width, (ZoomLevel)i); x++) { |
|
143 uint color = 0; |
|
144 |
|
145 /* Get the color keeping in mind the zoom-level */ |
|
146 for (int j = 0; j < ScaleByZoom(1, (ZoomLevel)i); j++) { |
|
147 if (src->m != 0) color = src->m; |
|
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 |
|
154 if (last_color == 0 || color == 0 || pixels == 255) { |
|
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); |
|
200 free(temp_dst); |
|
201 |
|
202 return dest_sprite; |
|
203 } |