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