4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file 32bpp_optimized.cpp Implementation of the optimized 32 bpp blitter. */
12 #include "../stdafx.h"
13 #include "../zoom_func.h"
14 #include "../settings_type.h"
15 #include "32bpp_optimized.hpp"
17 #include "../safeguards.h"
19 /** Instantiation of the optimized 32bpp blitter factory. */
20 static FBlitter_32bppOptimized iFBlitter_32bppOptimized
;
23 * Draws a sprite to a (screen) buffer. It is templated to allow faster operation.
25 * @tparam mode blitter mode
26 * @param bp further blitting parameters
27 * @param zoom zoom level at which we are drawing
29 template <BlitterMode mode
>
30 inline void Blitter_32bppOptimized::Draw(const Blitter::BlitterParams
*bp
, ZoomLevel zoom
)
32 const SpriteData
*src
= (const SpriteData
*)bp
->sprite
;
34 /* src_px : each line begins with uint32 n = 'number of bytes in this line',
35 * then n times is the Colour struct for this line */
36 const Colour
*src_px
= (const Colour
*)(src
->data
+ src
->offset
[zoom
][0]);
37 /* src_n : each line begins with uint32 n = 'number of bytes in this line',
38 * then interleaved stream of 'm' and 'n' channels. 'm' is remap,
39 * 'n' is number of bytes with the same alpha channel class */
40 const uint16
*src_n
= (const uint16
*)(src
->data
+ src
->offset
[zoom
][1]);
42 /* skip upper lines in src_px and src_n */
43 for (uint i
= bp
->skip_top
; i
!= 0; i
--) {
44 src_px
= (const Colour
*)((const byte
*)src_px
+ *(const uint32
*)src_px
);
45 src_n
= (const uint16
*)((const byte
*)src_n
+ *(const uint32
*)src_n
);
48 /* skip lines in dst */
49 Colour
*dst
= (Colour
*)bp
->dst
+ bp
->top
* bp
->pitch
+ bp
->left
;
51 /* store so we don't have to access it via bp everytime (compiler assumes pointer aliasing) */
52 const byte
*remap
= bp
->remap
;
54 for (int y
= 0; y
< bp
->height
; y
++) {
55 /* next dst line begins here */
56 Colour
*dst_ln
= dst
+ bp
->pitch
;
58 /* next src line begins here */
59 const Colour
*src_px_ln
= (const Colour
*)((const byte
*)src_px
+ *(const uint32
*)src_px
);
62 /* next src_n line begins here */
63 const uint16
*src_n_ln
= (const uint16
*)((const byte
*)src_n
+ *(const uint32
*)src_n
);
66 /* we will end this line when we reach this point */
67 Colour
*dst_end
= dst
+ bp
->skip_left
;
69 /* number of pixels with the same aplha channel class */
72 while (dst
< dst_end
) {
80 if (dst
+ n
> dst_end
) {
81 uint d
= dst_end
- dst
;
85 dst
= dst_end
- bp
->skip_left
;
86 dst_end
= dst
+ bp
->width
;
88 n
= min
<uint
>(n
- d
, (uint
)bp
->width
);
98 dst_end
-= bp
->skip_left
;
100 dst_end
+= bp
->width
;
102 while (dst
< dst_end
) {
103 n
= min
<uint
>(*src_n
++, (uint
)(dst_end
- dst
));
105 if (src_px
->a
== 0) {
115 case BM_COLOUR_REMAP
:
116 if (src_px
->a
== 255) {
119 /* In case the m-channel is zero, do not remap this pixel in any way */
123 uint r
= remap
[GB(m
, 0, 8)];
124 if (r
!= 0) *dst
= this->AdjustBrightness(this->LookupColourInPalette(r
), GB(m
, 8, 8));
134 *dst
= ComposeColourRGBANoCheck(src_px
->r
, src_px
->g
, src_px
->b
, src_px
->a
, *dst
);
136 uint r
= remap
[GB(m
, 0, 8)];
137 if (r
!= 0) *dst
= ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r
), GB(m
, 8, 8)), src_px
->a
, *dst
);
147 if (src_px
->a
== 255) {
151 uint8 g
= MakeDark(src_px
->r
, src_px
->g
, src_px
->b
);
152 *dst
= ComposeColourRGBA(g
, g
, g
, src_px
->a
, *dst
);
154 uint r
= remap
[GB(m
, 0, 8)];
155 if (r
!= 0) *dst
= this->AdjustBrightness(this->LookupColourInPalette(r
), GB(m
, 8, 8));
165 if (src_px
->a
!= 0) {
166 uint8 g
= MakeDark(src_px
->r
, src_px
->g
, src_px
->b
);
167 *dst
= ComposeColourRGBA(g
, g
, g
, src_px
->a
, *dst
);
170 uint r
= remap
[GB(m
, 0, 8)];
171 if (r
!= 0) *dst
= ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r
), GB(m
, 8, 8)), src_px
->a
, *dst
);
182 *dst
= Colour(0, 0, 0);
190 /* TODO -- We make an assumption here that the remap in fact is transparency, not some colour.
191 * This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
192 * we produce a result the newgrf maker didn't expect ;) */
194 /* Make the current colour a bit more black, so it looks like this image is transparent */
196 if (src_px
->a
== 255) {
199 *dst
= MakeTransparent(*dst
, 3, 4);
204 *dst
= MakeTransparent(*dst
, (256 * 4 - src_px
->a
), 256 * 4);
212 if (src_px
->a
== 255) {
213 /* faster than memcpy(), n is usually low */
223 *dst
= ComposeColourRGBANoCheck(src_px
->r
, src_px
->g
, src_px
->b
, src_px
->a
, *dst
);
239 * Draws a sprite to a (screen) buffer. Calls adequate templated function.
241 * @param bp further blitting parameters
242 * @param mode blitter mode
243 * @param zoom zoom level at which we are drawing
245 void Blitter_32bppOptimized::Draw(Blitter::BlitterParams
*bp
, BlitterMode mode
, ZoomLevel zoom
)
248 default: NOT_REACHED();
249 case BM_NORMAL
: Draw
<BM_NORMAL
> (bp
, zoom
); return;
250 case BM_COLOUR_REMAP
: Draw
<BM_COLOUR_REMAP
>(bp
, zoom
); return;
251 case BM_TRANSPARENT
: Draw
<BM_TRANSPARENT
> (bp
, zoom
); return;
252 case BM_CRASH_REMAP
: Draw
<BM_CRASH_REMAP
> (bp
, zoom
); return;
253 case BM_BLACK_REMAP
: Draw
<BM_BLACK_REMAP
> (bp
, zoom
); return;
257 Sprite
*Blitter_32bppOptimized::Encode(const SpriteLoader::Sprite
*sprite
, AllocatorProc
*allocator
)
259 /* streams of pixels (a, r, g, b channels)
261 * stored in separated stream so data are always aligned on 4B boundary */
262 Colour
*dst_px_orig
[ZOOM_LVL_COUNT
];
264 /* interleaved stream of 'm' channel and 'n' channel
265 * 'n' is number of following pixels with the same alpha channel class
266 * there are 3 classes: 0, 255, others
268 * it has to be stored in one stream so fewer registers are used -
269 * x86 has problems with register allocation even with this solution */
270 uint16
*dst_n_orig
[ZOOM_LVL_COUNT
];
272 /* lengths of streams */
273 uint32 lengths
[ZOOM_LVL_COUNT
][2];
278 if (sprite
->type
== ST_FONT
) {
279 zoom_min
= ZOOM_LVL_NORMAL
;
280 zoom_max
= ZOOM_LVL_NORMAL
;
282 zoom_min
= _settings_client
.gui
.zoom_min
;
283 zoom_max
= _settings_client
.gui
.zoom_max
;
284 if (zoom_max
== zoom_min
) zoom_max
= ZOOM_LVL_MAX
;
287 for (ZoomLevel z
= zoom_min
; z
<= zoom_max
; z
++) {
288 const SpriteLoader::Sprite
*src_orig
= &sprite
[z
];
290 uint size
= src_orig
->height
* src_orig
->width
;
292 dst_px_orig
[z
] = CallocT
<Colour
>(size
+ src_orig
->height
* 2);
293 dst_n_orig
[z
] = CallocT
<uint16
>(size
* 2 + src_orig
->height
* 4 * 2);
295 uint32
*dst_px_ln
= (uint32
*)dst_px_orig
[z
];
296 uint32
*dst_n_ln
= (uint32
*)dst_n_orig
[z
];
298 const SpriteLoader::CommonPixel
*src
= (const SpriteLoader::CommonPixel
*)src_orig
->data
;
300 for (uint y
= src_orig
->height
; y
> 0; y
--) {
301 Colour
*dst_px
= (Colour
*)(dst_px_ln
+ 1);
302 uint16
*dst_n
= (uint16
*)(dst_n_ln
+ 1);
304 uint16
*dst_len
= dst_n
++;
309 for (uint x
= src_orig
->width
; x
> 0; x
--) {
311 uint t
= a
> 0 && a
< 255 ? 1 : a
;
313 if (last
!= t
|| len
== 65535) {
328 /* Get brightest value */
329 uint8 rgb_max
= max(src
->r
, max(src
->g
, src
->b
));
331 /* Black pixel (8bpp or old 32bpp image), so use default value */
332 if (rgb_max
== 0) rgb_max
= DEFAULT_BRIGHTNESS
;
333 *dst_n
|= rgb_max
<< 8;
335 /* Pre-convert the mapping channel to a RGB value */
336 Colour colour
= this->AdjustBrightness(this->LookupColourInPalette(src
->m
), rgb_max
);
337 dst_px
->r
= colour
.r
;
338 dst_px
->g
= colour
.g
;
339 dst_px
->b
= colour
.b
;
347 } else if (len
== 1) {
360 dst_px
= (Colour
*)AlignPtr(dst_px
, 4);
361 dst_n
= (uint16
*)AlignPtr(dst_n
, 4);
363 *dst_px_ln
= (uint8
*)dst_px
- (uint8
*)dst_px_ln
;
364 *dst_n_ln
= (uint8
*)dst_n
- (uint8
*)dst_n_ln
;
366 dst_px_ln
= (uint32
*)dst_px
;
367 dst_n_ln
= (uint32
*)dst_n
;
370 lengths
[z
][0] = (byte
*)dst_px_ln
- (byte
*)dst_px_orig
[z
]; // all are aligned to 4B boundary
371 lengths
[z
][1] = (byte
*)dst_n_ln
- (byte
*)dst_n_orig
[z
];
374 uint len
= 0; // total length of data
375 for (ZoomLevel z
= zoom_min
; z
<= zoom_max
; z
++) {
376 len
+= lengths
[z
][0] + lengths
[z
][1];
379 Sprite
*dest_sprite
= (Sprite
*)allocator(sizeof(*dest_sprite
) + sizeof(SpriteData
) + len
);
381 dest_sprite
->height
= sprite
->height
;
382 dest_sprite
->width
= sprite
->width
;
383 dest_sprite
->x_offs
= sprite
->x_offs
;
384 dest_sprite
->y_offs
= sprite
->y_offs
;
386 SpriteData
*dst
= (SpriteData
*)dest_sprite
->data
;
387 memset(dst
, 0, sizeof(*dst
));
389 for (ZoomLevel z
= zoom_min
; z
<= zoom_max
; z
++) {
390 dst
->offset
[z
][0] = z
== zoom_min
? 0 : lengths
[z
- 1][1] + dst
->offset
[z
- 1][1];
391 dst
->offset
[z
][1] = lengths
[z
][0] + dst
->offset
[z
][0];
393 memcpy(dst
->data
+ dst
->offset
[z
][0], dst_px_orig
[z
], lengths
[z
][0]);
394 memcpy(dst
->data
+ dst
->offset
[z
][1], dst_n_orig
[z
], lengths
[z
][1]);
396 free(dst_px_orig
[z
]);