8bpp_optimized.cpp

Go to the documentation of this file.
00001 /* $Id: 8bpp_optimized.cpp 15556 2009-02-23 17:54:02Z rubidium $ */
00002 
00005 #include "../stdafx.h"
00006 #include "../zoom_func.h"
00007 #include "../core/alloc_func.hpp"
00008 #include "../core/math_func.hpp"
00009 #include "8bpp_optimized.hpp"
00010 
00011 static FBlitter_8bppOptimized iFBlitter_8bppOptimized;
00012 
00013 void Blitter_8bppOptimized::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
00014 {
00015   /* Find the offset of this zoom-level */
00016   const SpriteData *sprite_src = (const SpriteData *)bp->sprite;
00017   uint offset = sprite_src->offset[zoom];
00018 
00019   /* Find where to start reading in the source sprite */
00020   const uint8 *src = sprite_src->data + offset;
00021   uint8 *dst_line = (uint8 *)bp->dst + bp->top * bp->pitch + bp->left;
00022 
00023   /* Skip over the top lines in the source image */
00024   for (int y = 0; y < bp->skip_top; y++) {
00025     for (;;) {
00026       uint trans = *src++;
00027       uint pixels = *src++;
00028       if (trans == 0 && pixels == 0) break;
00029       src += pixels;
00030     }
00031   }
00032 
00033   const uint8 *src_next = src;
00034 
00035   for (int y = 0; y < bp->height; y++) {
00036     uint8 *dst = dst_line;
00037     dst_line += bp->pitch;
00038 
00039     uint skip_left = bp->skip_left;
00040     int width = bp->width;
00041 
00042     for (;;) {
00043       src = src_next;
00044       uint trans = *src++;
00045       uint pixels = *src++;
00046       src_next = src + pixels;
00047       if (trans == 0 && pixels == 0) break;
00048       if (width <= 0) continue;
00049 
00050       if (skip_left != 0) {
00051         if (skip_left < trans) {
00052           trans -= skip_left;
00053           skip_left = 0;
00054         } else {
00055           skip_left -= trans;
00056           trans = 0;
00057         }
00058         if (skip_left < pixels) {
00059           src += skip_left;
00060           pixels -= skip_left;
00061           skip_left = 0;
00062         } else {
00063           src += pixels;
00064           skip_left -= pixels;
00065           pixels = 0;
00066         }
00067       }
00068       if (skip_left != 0) continue;
00069 
00070       /* Skip transparent pixels */
00071       dst += trans;
00072       width -= trans;
00073       if (width <= 0 || pixels == 0) continue;
00074       pixels = min<uint>(pixels, (uint)width);
00075       width -= pixels;
00076 
00077       switch (mode) {
00078         case BM_COLOUR_REMAP: {
00079           const uint8 *remap = bp->remap;
00080           do {
00081             uint m = remap[*src];
00082             if (m != 0) *dst = m;
00083             dst++; src++;
00084           } while (--pixels != 0);
00085         } break;
00086 
00087         case BM_TRANSPARENT: {
00088           const uint8 *remap = bp->remap;
00089           src += pixels;
00090           do {
00091             *dst = remap[*dst];
00092             dst++;
00093           } while (--pixels != 0);
00094         } break;
00095 
00096         default:
00097           memcpy(dst, src, pixels);
00098           dst += pixels; src += pixels;
00099           break;
00100       }
00101     }
00102   }
00103 }
00104 
00105 Sprite *Blitter_8bppOptimized::Encode(SpriteLoader::Sprite *sprite, Blitter::AllocatorProc *allocator)
00106 {
00107   /* Make memory for all zoom-levels */
00108   uint memory = sizeof(SpriteData);
00109 
00110   for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) {
00111     memory += UnScaleByZoom(sprite->height, i) * UnScaleByZoom(sprite->width, i);
00112   }
00113 
00114   /* We have no idea how much memory we really need, so just guess something */
00115   memory *= 5;
00116 
00117   /* Don't allocate memory each time, but just keep some
00118    * memory around as this function is called quite often
00119    * and the memory usage is quite low. */
00120   static ReusableBuffer<byte> temp_buffer;
00121   SpriteData *temp_dst = (SpriteData *)temp_buffer.Allocate(memory);
00122   byte *dst = temp_dst->data;
00123 
00124   /* Make the sprites per zoom-level */
00125   for (ZoomLevel i = ZOOM_LVL_BEGIN; i < ZOOM_LVL_END; i++) {
00126     /* Store the index table */
00127     uint offset = dst - temp_dst->data;
00128     temp_dst->offset[i] = offset;
00129 
00130     /* cache values, because compiler can't cache it */
00131     int scaled_height = UnScaleByZoom(sprite->height, i);
00132     int scaled_width  = UnScaleByZoom(sprite->width,  i);
00133     int scaled_1      =   ScaleByZoom(1,              i);
00134 
00135     for (int y = 0; y < scaled_height; y++) {
00136       uint trans = 0;
00137       uint pixels = 0;
00138       uint last_colour = 0;
00139       byte *count_dst = NULL;
00140 
00141       /* Store the scaled image */
00142       const SpriteLoader::CommonPixel *src = &sprite->data[ScaleByZoom(y, i) * sprite->width];
00143       const SpriteLoader::CommonPixel *src_end = &src[sprite->width];
00144 
00145       for (int x = 0; x < scaled_width; x++) {
00146         uint colour = 0;
00147 
00148         /* Get the colour keeping in mind the zoom-level */
00149         for (int j = 0; j < scaled_1; j++) {
00150           if (src->m != 0) colour = src->m;
00151           /* Because of the scaling it might happen we read outside the buffer. Avoid that. */
00152           if (++src == src_end) break;
00153         }
00154 
00155         if (last_colour == 0 || colour == 0 || pixels == 255) {
00156           if (count_dst != NULL) {
00157             /* Write how many non-transparent bytes we get */
00158             *count_dst = pixels;
00159             pixels = 0;
00160             count_dst = NULL;
00161           }
00162           /* As long as we find transparency bytes, keep counting */
00163           if (colour == 0) {
00164             last_colour = 0;
00165             trans++;
00166             continue;
00167           }
00168           /* No longer transparency, so write the amount of transparent bytes */
00169           *dst = trans;
00170           dst++;
00171           trans = 0;
00172           /* Reserve a byte for the pixel counter */
00173           count_dst = dst;
00174           dst++;
00175         }
00176         last_colour = colour;
00177         pixels++;
00178         *dst = colour;
00179         dst++;
00180       }
00181 
00182       if (count_dst != NULL) *count_dst = pixels;
00183 
00184       /* Write line-ending */
00185       *dst = 0; dst++;
00186       *dst = 0; dst++;
00187     }
00188   }
00189 
00190   uint size = dst - (byte *)temp_dst;
00191 
00192   /* Safety check, to make sure we guessed the size correctly */
00193   assert(size < memory);
00194 
00195   /* Allocate the exact amount of memory we need */
00196   Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + size);
00197 
00198   dest_sprite->height = sprite->height;
00199   dest_sprite->width  = sprite->width;
00200   dest_sprite->x_offs = sprite->x_offs;
00201   dest_sprite->y_offs = sprite->y_offs;
00202   memcpy(dest_sprite->data, temp_dst, size);
00203 
00204   return dest_sprite;
00205 }

Generated on Mon Jun 8 23:04:02 2009 for OpenTTD by  doxygen 1.5.6