smatz@8091: /* $Id$ */ smatz@8091: rubidium@9111: /** @file 32bpp_optimized.cpp Implementation of the optimized 32 bpp blitter. */ rubidium@9111: truelight@7553: #include "../stdafx.h" rubidium@8123: #include "../zoom_func.h" rubidium@8123: #include "../gfx_func.h" truelight@7553: #include "../debug.h" smatz@9597: #include "../core/math_func.hpp" smatz@9597: #include "../core/alloc_func.hpp" truelight@7553: #include "32bpp_optimized.hpp" truelight@7553: truelight@7553: static FBlitter_32bppOptimized iFBlitter_32bppOptimized; truelight@7553: smatz@9597: /** smatz@9597: * Draws a sprite to a (screen) buffer. It is templated to allow faster operation. smatz@9597: * smatz@9597: * @param mode blitter mode smatz@9597: * @param bp further blitting parameters smatz@9597: * @param zoom zoom level at which we are drawing smatz@9597: */ smatz@9597: template smatz@9597: inline void Blitter_32bppOptimized::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom) truelight@7553: { smatz@9597: const SpriteData *src = (const SpriteData *)bp->sprite; truelight@7553: smatz@9597: /* src_px : each line begins with uint32 n = 'number of bytes in this line', smatz@9597: * then n times is the Colour struct for this line */ smatz@9597: const Colour *src_px = (const Colour *)(src->data + src->offset[zoom][0]); smatz@9597: /* src_n : each line begins with uint32 n = 'number of bytes in this line', smatz@9597: * then interleaved stream of 'm' and 'n' channels. 'm' is remap, smatz@9597: * 'n' is number of bytes with the same alpha channel class */ smatz@9597: const uint8 *src_n = (const uint8 *)(src->data + src->offset[zoom][1]); smatz@9597: smatz@9597: /* skip upper lines in src_px and src_n */ smatz@9597: for (uint i = bp->skip_top; i != 0; i--) { smatz@9597: src_px = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px); smatz@9597: src_n += *(uint32 *)src_n; smatz@9597: } smatz@9597: smatz@9597: /* skip lines in dst */ smatz@9597: uint32 *dst = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left; smatz@9597: smatz@9597: /* store so we don't have to access it via bp everytime (compiler assumes pointer aliasing) */ smatz@9597: const byte *remap = bp->remap; truelight@7553: truelight@7553: for (int y = 0; y < bp->height; y++) { smatz@9597: /* next dst line begins here */ smatz@9597: uint32 *dst_ln = dst + bp->pitch; truelight@7553: smatz@9597: /* next src line begins here */ smatz@9597: const Colour *src_px_ln = (const Colour *)((const byte *)src_px + *(const uint32 *)src_px); smatz@9597: src_px++; truelight@7553: smatz@9597: /* next src_n line begins here */ smatz@9597: const uint8 *src_n_ln = src_n + *(uint32 *)src_n; smatz@9597: src_n += 4; smatz@9597: smatz@9597: /* we will end this line when we reach this point */ smatz@9597: uint32 *dst_end = dst + bp->skip_left; smatz@9597: smatz@9597: /* number of pixels with the same aplha channel class */ smatz@9597: uint n; smatz@9597: smatz@9597: while (dst < dst_end) { smatz@9597: n = *src_n++; smatz@9597: smatz@9597: if (src_px->a == 0) { smatz@9597: dst += n; smatz@9597: src_px ++; smatz@9597: src_n++; smatz@9597: } else { smatz@9597: if (dst + n > dst_end) { smatz@9597: uint d = dst_end - dst; smatz@9597: src_px += d; smatz@9597: src_n += d; smatz@9597: smatz@9597: dst = dst_end - bp->skip_left; smatz@9597: dst_end = dst + bp->width; smatz@9597: smatz@9597: n = min(n - d, (uint)bp->width); smatz@9597: goto draw; smatz@9597: } smatz@9597: dst += n; smatz@9597: src_px += n; smatz@9597: src_n += n; smatz@9597: } smatz@9597: } smatz@9597: smatz@9597: dst -= bp->skip_left; smatz@9597: dst_end -= bp->skip_left; smatz@9597: smatz@9597: dst_end += bp->width; smatz@9597: smatz@9597: while (dst < dst_end) { smatz@9597: n = min(*src_n++, (uint)(dst_end - dst)); smatz@9597: smatz@9597: if (src_px->a == 0) { smatz@9597: dst += n; smatz@9597: src_px++; smatz@9597: src_n++; truelight@7553: continue; truelight@7553: } truelight@7553: smatz@9597: draw:; smatz@9597: truelight@7553: switch (mode) { truelight@7553: case BM_COLOUR_REMAP: smatz@9597: if (src_px->a == 255) { smatz@9597: do { smatz@9597: uint m = *src_n; smatz@9597: /* In case the m-channel is zero, do not remap this pixel in any way */ smatz@9597: if (m == 0) { smatz@9597: *dst = *src_px; smatz@9597: } else { smatz@9597: uint r = remap[m]; smatz@9597: if (r != 0) *dst = this->LookupColourInPalette(r); smatz@9597: } smatz@9597: dst++; smatz@9597: src_px++; smatz@9597: src_n++; smatz@9597: } while (--n != 0); truelight@7553: } else { smatz@9597: do { smatz@9597: uint m = *src_n; smatz@9597: if (m == 0) { smatz@9597: *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst); smatz@9597: } else { smatz@9597: uint r = remap[m]; smatz@9597: if (r != 0) *dst = ComposeColourPANoCheck(this->LookupColourInPalette(r), src_px->a, *dst); smatz@9597: } smatz@9597: dst++; smatz@9597: src_px++; smatz@9597: src_n++; smatz@9597: } while (--n != 0); truelight@7553: } truelight@7553: break; truelight@7553: truelight@7553: case BM_TRANSPARENT: truelight@7553: /* TODO -- We make an assumption here that the remap in fact is transparency, not some color. truelight@7553: * This is never a problem with the code we produce, but newgrfs can make it fail... or at least: truelight@7553: * we produce a result the newgrf maker didn't expect ;) */ truelight@7553: truelight@7553: /* Make the current color a bit more black, so it looks like this image is transparent */ smatz@9597: src_n += n; smatz@9598: if (src_px->a == 255) { smatz@9598: src_px += n; smatz@9598: do { smatz@9598: *dst = MakeTransparent(*dst, 3, 4); smatz@9598: dst++; smatz@9598: } while (--n != 0); smatz@9598: } else { smatz@9598: do { smatz@9598: *dst = MakeTransparent(*dst, (256 * 4 - src_px->a), 256 * 4); smatz@9598: dst++; smatz@9598: src_px++; smatz@9598: } while (--n != 0); smatz@9598: } truelight@7553: break; truelight@7553: truelight@7553: default: smatz@9597: if (src_px->a == 255) { smatz@9597: /* faster than memcpy(), n is usually low */ smatz@9597: src_n += n; smatz@9597: do { smatz@9597: *dst++ = *src_px++; smatz@9597: } while (--n != 0); smatz@9597: } else { smatz@9597: src_n += n; smatz@9597: do { smatz@9597: *dst = ComposeColourRGBANoCheck(src_px->r, src_px->g, src_px->b, src_px->a, *dst); smatz@9597: dst++; smatz@9597: src_px++; smatz@9597: } while (--n != 0); smatz@9597: } truelight@7553: break; truelight@7553: } truelight@7553: } smatz@9597: smatz@9597: dst = dst_ln; smatz@9597: src_px = src_px_ln; smatz@9597: src_n = src_n_ln; truelight@7553: } truelight@7553: } truelight@7553: smatz@9597: /** smatz@9597: * Draws a sprite to a (screen) buffer. Calls adequate templated function. smatz@9597: * smatz@9597: * @param bp further blitting parameters smatz@9597: * @param mode blitter mode smatz@9597: * @param zoom zoom level at which we are drawing smatz@9597: */ smatz@9486: void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) smatz@9486: { smatz@9486: switch (mode) { smatz@9486: default: NOT_REACHED(); smatz@9486: case BM_NORMAL: Draw (bp, zoom); return; smatz@9486: case BM_COLOUR_REMAP: Draw(bp, zoom); return; smatz@9486: case BM_TRANSPARENT: Draw (bp, zoom); return; smatz@9486: } smatz@9486: } smatz@9486: smatz@9597: /** smatz@9597: * Resizes the sprite in a very simple way, takes every n-th pixel and every n-th row smatz@9597: * smatz@9597: * @param sprite_src sprite to resize smatz@9597: * @param zoom resizing scale smatz@9597: * @return resized sprite smatz@9597: */ smatz@9597: static const SpriteLoader::Sprite *ResizeSprite(const SpriteLoader::Sprite *sprite_src, ZoomLevel zoom) smatz@9597: { smatz@9597: SpriteLoader::Sprite *sprite = MallocT(1); smatz@9597: smatz@9597: if (zoom == ZOOM_LVL_NORMAL) { smatz@9597: memcpy(sprite, sprite_src, sizeof(*sprite)); smatz@9597: uint size = sprite_src->height * sprite_src->width; smatz@9597: sprite->data = MallocT(size); smatz@9597: memcpy(sprite->data, sprite_src->data, size * sizeof(SpriteLoader::CommonPixel)); smatz@9597: return sprite; smatz@9597: } smatz@9597: smatz@9597: sprite->height = UnScaleByZoom(sprite_src->height, zoom); smatz@9597: sprite->width = UnScaleByZoom(sprite_src->width, zoom); smatz@9597: sprite->x_offs = UnScaleByZoom(sprite_src->x_offs, zoom); smatz@9597: sprite->y_offs = UnScaleByZoom(sprite_src->y_offs, zoom); smatz@9597: smatz@9597: uint size = sprite->height * sprite->width; smatz@9597: SpriteLoader::CommonPixel *dst = sprite->data = CallocT(size); smatz@9597: smatz@9597: const SpriteLoader::CommonPixel *src = (SpriteLoader::CommonPixel *)sprite_src->data; smatz@9597: const SpriteLoader::CommonPixel *src_end = src + sprite_src->height * sprite_src->width; smatz@9597: smatz@9597: uint scaled_1 = ScaleByZoom(1, zoom); smatz@9597: smatz@9597: for (uint y = 0; y < sprite->height; y++) { smatz@9597: if (src >= src_end) src = src_end - sprite_src->width; smatz@9597: smatz@9597: const SpriteLoader::CommonPixel *src_ln = src + sprite_src->width * scaled_1; smatz@9597: for (uint x = 0; x < sprite->width; x++) { smatz@9597: if (src >= src_ln) src = src_ln - 1; smatz@9597: *dst = *src; smatz@9597: dst++; smatz@9597: src += scaled_1; smatz@9597: } smatz@9597: smatz@9597: src = src_ln; smatz@9597: } smatz@9597: smatz@9597: return sprite; smatz@9597: } smatz@9597: truelight@7553: Sprite *Blitter_32bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator) truelight@7553: { smatz@9597: /* streams of pixels (a, r, g, b channels) smatz@9597: * smatz@9597: * stored in separated stream so data are always aligned on 4B boundary */ smatz@9597: Colour *dst_px_orig[ZOOM_LVL_COUNT]; smatz@9597: smatz@9597: /* interleaved stream of 'm' channel and 'n' channel smatz@9597: * 'n' is number if following pixels with the same alpha channel class smatz@9597: * there are 3 classes: 0, 255, others smatz@9597: * smatz@9597: * it has to be stored in one stream so fewer registers are used - smatz@9597: * x86 has problems with register allocation even with this solution */ smatz@9597: uint8 *dst_n_orig[ZOOM_LVL_COUNT]; smatz@9597: smatz@9597: /* lengths of streams */ smatz@9597: uint32 lengths[ZOOM_LVL_COUNT][2]; smatz@9597: smatz@9597: for (ZoomLevel z = ZOOM_LVL_BEGIN; z < ZOOM_LVL_END; z++) { smatz@9597: const SpriteLoader::Sprite *src_orig = ResizeSprite(sprite, z); smatz@9597: smatz@9597: uint size = src_orig->height * src_orig->width; smatz@9597: smatz@9597: dst_px_orig[z] = CallocT(size + src_orig->height * 2); smatz@9597: dst_n_orig[z] = CallocT(size * 2 + src_orig->height * 4 * 2); smatz@9597: smatz@9597: uint32 *dst_px_ln = (uint32 *)dst_px_orig[z]; smatz@9597: uint32 *dst_n_ln = (uint32 *)dst_n_orig[z]; smatz@9597: smatz@9597: const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data; smatz@9597: smatz@9597: for (uint y = src_orig->height; y > 0; y--) { smatz@9597: Colour *dst_px = (Colour *)(dst_px_ln + 1); smatz@9597: uint8 *dst_n = (uint8 *)(dst_n_ln + 1); smatz@9597: smatz@9597: uint8 *dst_len = dst_n++; smatz@9597: smatz@9597: uint last = 3; smatz@9597: int len = 0; smatz@9597: smatz@9597: for (uint x = src_orig->width; x > 0; x--) { smatz@9597: uint8 a = src->a; smatz@9597: uint t = a > 0 && a < 255 ? 1 : a; smatz@9597: smatz@9597: if (last != t || len == 255) { smatz@9597: if (last != 3) { smatz@9597: *dst_len = len; smatz@9597: dst_len = dst_n++; smatz@9597: } smatz@9597: len = 0; smatz@9597: } smatz@9597: smatz@9597: last = t; smatz@9597: len++; smatz@9597: smatz@9597: if (a != 0) { smatz@9597: dst_px->a = a; smatz@9597: *dst_n = src->m; smatz@9597: if (src->m != 0) { smatz@9597: /* Pre-convert the mapping channel to a RGB value */ smatz@9597: uint32 colour = this->LookupColourInPalette(src->m); smatz@9597: dst_px->r = GB(colour, 16, 8); smatz@9597: dst_px->g = GB(colour, 8, 8); smatz@9597: dst_px->b = GB(colour, 0, 8); smatz@9597: } else { smatz@9597: dst_px->r = src->r; smatz@9597: dst_px->g = src->g; smatz@9597: dst_px->b = src->b; smatz@9597: } smatz@9597: dst_px++; smatz@9597: dst_n++; smatz@9597: } else if (len == 1) { smatz@9597: dst_px++; smatz@9597: *dst_n = src->m; smatz@9597: dst_n++; smatz@9597: } smatz@9597: smatz@9597: src++; smatz@9597: } smatz@9597: smatz@9597: if (last != 3) { smatz@9597: *dst_len = len; smatz@9597: } smatz@9597: smatz@9597: dst_px = (Colour *)AlignPtr(dst_px, 4); smatz@9597: dst_n = (uint8 *)AlignPtr(dst_n, 4); smatz@9597: smatz@9597: *dst_px_ln = (uint8 *)dst_px - (uint8 *)dst_px_ln; smatz@9597: *dst_n_ln = (uint8 *)dst_n - (uint8 *)dst_n_ln; smatz@9597: smatz@9597: dst_px_ln = (uint32 *)dst_px; smatz@9597: dst_n_ln = (uint32 *)dst_n; smatz@9597: } smatz@9597: smatz@9597: lengths[z][0] = (byte *)dst_px_ln - (byte *)dst_px_orig[z]; // all are aligned to 4B boundary smatz@9597: lengths[z][1] = (byte *)dst_n_ln - (byte *)dst_n_orig[z]; smatz@9597: smatz@9597: free(src_orig->data); smatz@9597: free((void *)src_orig); smatz@9597: } smatz@9597: smatz@9597: uint len = 0; // total length of data smatz@9597: for (ZoomLevel z = ZOOM_LVL_BEGIN; z < ZOOM_LVL_END; z++) { smatz@9597: len += lengths[z][0] + lengths[z][1]; smatz@9597: } smatz@9597: smatz@9597: Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sizeof(SpriteData) + len); truelight@7553: truelight@7553: dest_sprite->height = sprite->height; truelight@7553: dest_sprite->width = sprite->width; truelight@7553: dest_sprite->x_offs = sprite->x_offs; truelight@7553: dest_sprite->y_offs = sprite->y_offs; truelight@7553: smatz@9597: SpriteData *dst = (SpriteData *)dest_sprite->data; truelight@7553: smatz@9597: for (ZoomLevel z = ZOOM_LVL_BEGIN; z < ZOOM_LVL_END; z++) { smatz@9597: dst->offset[z][0] = z == ZOOM_LVL_BEGIN ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1]; smatz@9597: dst->offset[z][1] = lengths[z][0] + dst->offset[z][0]; smatz@9597: smatz@9597: memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]); smatz@9597: memcpy(dst->data + dst->offset[z][1], dst_n_orig[z], lengths[z][1]); smatz@9597: smatz@9597: free(dst_px_orig[z]); smatz@9597: free(dst_n_orig[z]); truelight@7553: } smatz@9597: truelight@7553: return dest_sprite; truelight@7553: }