32bpp_optimized.cpp

00001 /* $Id: 32bpp_optimized.cpp 11828 2008-01-13 01:21:35Z rubidium $ */
00002 
00003 #include "../stdafx.h"
00004 #include "../zoom_func.h"
00005 #include "../gfx_func.h"
00006 #include "../debug.h"
00007 #include "32bpp_optimized.hpp"
00008 
00009 static FBlitter_32bppOptimized iFBlitter_32bppOptimized;
00010 
00011 void Blitter_32bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
00012 {
00013   const SpriteLoader::CommonPixel *src, *src_line;
00014   uint32 *dst, *dst_line;
00015 
00016   /* Find where to start reading in the source sprite */
00017   src_line = (const SpriteLoader::CommonPixel *)bp->sprite + (bp->skip_top * bp->sprite_width + bp->skip_left) * ScaleByZoom(1, zoom);
00018   dst_line = (uint32 *)bp->dst + bp->top * bp->pitch + bp->left;
00019 
00020   for (int y = 0; y < bp->height; y++) {
00021     dst = dst_line;
00022     dst_line += bp->pitch;
00023 
00024     src = src_line;
00025     src_line += bp->sprite_width * ScaleByZoom(1, zoom);
00026 
00027     for (int x = 0; x < bp->width; x++) {
00028       if (src->a == 0) {
00029         /* src->r is 'misused' here to indicate how much more pixels are following with an alpha of 0 */
00030         int skip = UnScaleByZoom(src->r, zoom);
00031 
00032         dst += skip;
00033         x   += skip - 1;
00034         src += ScaleByZoom(1, zoom) * skip;
00035         continue;
00036       }
00037 
00038       switch (mode) {
00039         case BM_COLOUR_REMAP:
00040           /* In case the m-channel is zero, do not remap this pixel in any way */
00041           if (src->m == 0) {
00042             *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
00043           } else {
00044             if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst);
00045           }
00046           break;
00047 
00048         case BM_TRANSPARENT:
00049           /* TODO -- We make an assumption here that the remap in fact is transparency, not some color.
00050            *  This is never a problem with the code we produce, but newgrfs can make it fail... or at least:
00051            *  we produce a result the newgrf maker didn't expect ;) */
00052 
00053           /* Make the current color a bit more black, so it looks like this image is transparent */
00054           *dst = MakeTransparent(*dst, 192);
00055           break;
00056 
00057         default:
00058           *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
00059           break;
00060       }
00061       dst++;
00062       src += ScaleByZoom(1, zoom);
00063     }
00064   }
00065 }
00066 
00067 Sprite *Blitter_32bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
00068 {
00069   Sprite *dest_sprite;
00070   SpriteLoader::CommonPixel *dst;
00071   dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
00072 
00073   dest_sprite->height = sprite->height;
00074   dest_sprite->width  = sprite->width;
00075   dest_sprite->x_offs = sprite->x_offs;
00076   dest_sprite->y_offs = sprite->y_offs;
00077 
00078   dst = (SpriteLoader::CommonPixel *)dest_sprite->data;
00079 
00080   memcpy(dst, sprite->data, sprite->height * sprite->width * sizeof(SpriteLoader::CommonPixel));
00081   /* Skip to the end of the array, and work backwards to find transparent blocks */
00082   dst = dst + sprite->height * sprite->width - 1;
00083 
00084   for (uint y = sprite->height; y > 0; y--) {
00085     int trans = 0;
00086     /* Process sprite line backwards, to compute lengths of transparent blocks */
00087     for (uint x = sprite->width; x > 0; x--) {
00088       if (dst->a == 0) {
00089         /* Save transparent block length in red channel; max value is 255 the red channel can contain */
00090         if (trans < 255) trans++;
00091         dst->r = trans;
00092         dst->g = 0;
00093         dst->b = 0;
00094         dst->m = 0;
00095       } else {
00096         trans = 0;
00097         if (dst->m != 0) {
00098           /* Pre-convert the mapping channel to a RGB value */
00099           uint color = this->LookupColourInPalette(dst->m);
00100           dst->r = GB(color, 16, 8);
00101           dst->g = GB(color, 8,  8);
00102           dst->b = GB(color, 0,  8);
00103         }
00104       }
00105       dst--;
00106     }
00107   }
00108   return dest_sprite;
00109 }

Generated on Wed Oct 1 17:03:20 2008 for openttd by  doxygen 1.5.6