8bpp_optimized.cpp

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