smatz@8587: /* $Id$ */ smatz@8587: rubidium@10429: /** @file 32bpp_optimized.cpp Implementation of the optimized 32 bpp blitter. */ rubidium@10429: truelight@8049: #include "../stdafx.h" rubidium@8619: #include "../zoom_func.h" rubidium@8619: #include "../gfx_func.h" truelight@8049: #include "../debug.h" truelight@8049: #include "32bpp_optimized.hpp" truelight@8049: truelight@8049: static FBlitter_32bppOptimized iFBlitter_32bppOptimized; truelight@8049: truelight@8049: void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) truelight@8049: { truelight@8049: const SpriteLoader::CommonPixel *src, *src_line; truelight@8049: uint32 *dst, *dst_line; truelight@8049: truelight@8049: /* Find where to start reading in the source sprite */ truelight@8049: src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom); truelight@8049: dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left; truelight@8049: truelight@8049: for (int y = 0; y < bp->height; y++) { truelight@8049: dst = dst_line; truelight@8049: dst_line += bp->pitch; truelight@8049: truelight@8049: src = src_line; truelight@8049: src_line += bp->sprite_width * ScaleByZoom(1, zoom); truelight@8049: truelight@8049: for (int x = 0; x < bp->width; x++) { truelight@8049: if (src->a == 0) { truelight@8049: /* src->r is 'misused' here to indicate how much more pixels are following with an alpha of 0 */ truelight@8049: int skip = UnScaleByZoom(src->r, zoom); truelight@8049: truelight@8049: dst += skip; truelight@8049: x += skip - 1; truelight@8049: src += ScaleByZoom(1, zoom) * skip; truelight@8049: continue; truelight@8049: } truelight@8049: truelight@8049: switch (mode) { truelight@8049: case BM_COLOUR_REMAP: truelight@8049: /* In case the m-channel is zero, do not remap this pixel in any way */ truelight@8049: if (src->m == 0) { truelight@8049: *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst); truelight@8049: } else { truelight@8049: if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst); truelight@8049: } truelight@8049: break; truelight@8049: truelight@8049: case BM_TRANSPARENT: truelight@8049: /* TODO -- We make an assumption here that the remap in fact is transparency, not some color. truelight@8049: * This is never a problem with the code we produce, but newgrfs can make it fail... or at least: truelight@8049: * we produce a result the newgrf maker didn't expect ;) */ truelight@8049: truelight@8049: /* Make the current color a bit more black, so it looks like this image is transparent */ truelight@8049: *dst = MakeTransparent(*dst, 192); truelight@8049: break; truelight@8049: truelight@8049: default: truelight@8049: *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst); truelight@8049: break; truelight@8049: } truelight@8049: dst++; truelight@8049: src += ScaleByZoom(1, zoom); truelight@8049: } truelight@8049: } truelight@8049: } truelight@8049: truelight@8049: Sprite *Blitter_32bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator) truelight@8049: { truelight@8049: Sprite *dest_sprite; truelight@8049: SpriteLoader::CommonPixel *dst; truelight@8049: dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel)); truelight@8049: truelight@8049: dest_sprite->height = sprite->height; truelight@8049: dest_sprite->width = sprite->width; truelight@8049: dest_sprite->x_offs = sprite->x_offs; truelight@8049: dest_sprite->y_offs = sprite->y_offs; truelight@8049: truelight@8049: dst = (SpriteLoader::CommonPixel *)dest_sprite->data; truelight@8049: truelight@8049: memcpy(dst, sprite->data, sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel)); truelight@8049: /* Skip to the end of the array, and work backwards to find transparent blocks */ truelight@8049: dst = dst + sprite->height * sprite->width - 1; truelight@8049: truelight@8049: for (uint y = sprite->height; y > 0; y--) { truelight@8049: int trans = 0; truelight@8049: /* Process sprite line backwards, to compute lengths of transparent blocks */ truelight@8049: for (uint x = sprite->width; x > 0; x--) { truelight@8049: if (dst->a == 0) { truelight@8049: /* Save transparent block length in red channel; max value is 255 the red channel can contain */ truelight@8049: if (trans < 255) trans++; truelight@8049: dst->r = trans; truelight@8049: dst->g = 0; truelight@8049: dst->b = 0; truelight@8049: dst->m = 0; truelight@8049: } else { truelight@8049: trans = 0; truelight@8049: if (dst->m != 0) { truelight@8049: /* Pre-convert the mapping channel to a RGB value */ truelight@8049: uint color = this->LookupColourInPalette(dst->m); truelight@8049: dst->r = GB(color, 16, 8); truelight@8049: dst->g = GB(color, 8, 8); truelight@8049: dst->b = GB(color, 0, 8); truelight@8049: } truelight@8049: } truelight@8049: dst--; truelight@8049: } truelight@8049: } truelight@8049: return dest_sprite; truelight@8049: }