8bpp_optimized.cpp

Go to the documentation of this file.
00001 /* $Id: 8bpp_optimized.cpp 26541 2014-04-29 18:18:52Z 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         case BM_CRASH_REMAP: {
00088           const uint8 *remap = bp->remap;
00089           do {
00090             uint m = remap[*src];
00091             if (m != 0) *dst = m;
00092             dst++; src++;
00093           } while (--pixels != 0);
00094           break;
00095         }
00096 
00097         case BM_TRANSPARENT: {
00098           const uint8 *remap = bp->remap;
00099           src += pixels;
00100           do {
00101             *dst = remap[*dst];
00102             dst++;
00103           } while (--pixels != 0);
00104           break;
00105         }
00106 
00107         default:
00108           memcpy(dst, src, pixels);
00109           dst += pixels; src += pixels;
00110           break;
00111       }
00112     }
00113   }
00114 }
00115 
00116 Sprite *Blitter_8bppOptimized::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
00117 {
00118   /* Make memory for all zoom-levels */
00119   uint memory = sizeof(SpriteData);
00120 
00121   ZoomLevel zoom_min;
00122   ZoomLevel zoom_max;
00123 
00124   if (sprite->type == ST_FONT) {
00125     zoom_min = ZOOM_LVL_NORMAL;
00126     zoom_max = ZOOM_LVL_NORMAL;
00127   } else {
00128     zoom_min = _settings_client.gui.zoom_min;
00129     zoom_max = _settings_client.gui.zoom_max;
00130     if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
00131   }
00132 
00133   for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
00134     memory += sprite[i].width * sprite[i].height;
00135   }
00136 
00137   /* We have no idea how much memory we really need, so just guess something */
00138   memory *= 5;
00139 
00140   /* Don't allocate memory each time, but just keep some
00141    * memory around as this function is called quite often
00142    * and the memory usage is quite low. */
00143   static ReusableBuffer<byte> temp_buffer;
00144   SpriteData *temp_dst = (SpriteData *)temp_buffer.Allocate(memory);
00145   memset(temp_dst, 0, sizeof(*temp_dst));
00146   byte *dst = temp_dst->data;
00147 
00148   /* Make the sprites per zoom-level */
00149   for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
00150     /* Store the index table */
00151     uint offset = dst - temp_dst->data;
00152     temp_dst->offset[i] = offset;
00153 
00154     /* cache values, because compiler can't cache it */
00155     int scaled_height = sprite[i].height;
00156     int scaled_width  = sprite[i].width;
00157 
00158     for (int y = 0; y < scaled_height; y++) {
00159       uint trans = 0;
00160       uint pixels = 0;
00161       uint last_colour = 0;
00162       byte *count_dst = NULL;
00163 
00164       /* Store the scaled image */
00165       const SpriteLoader::CommonPixel *src = &sprite[i].data[y * sprite[i].width];
00166 
00167       for (int x = 0; x < scaled_width; x++) {
00168         uint colour = src++->m;
00169 
00170         if (last_colour == 0 || colour == 0 || pixels == 255) {
00171           if (count_dst != NULL) {
00172             /* Write how many non-transparent bytes we get */
00173             *count_dst = pixels;
00174             pixels = 0;
00175             count_dst = NULL;
00176           }
00177           /* As long as we find transparency bytes, keep counting */
00178           if (colour == 0 && trans != 255) {
00179             last_colour = 0;
00180             trans++;
00181             continue;
00182           }
00183           /* No longer transparency, so write the amount of transparent bytes */
00184           *dst = trans;
00185           dst++;
00186           trans = 0;
00187           /* Reserve a byte for the pixel counter */
00188           count_dst = dst;
00189           dst++;
00190         }
00191         last_colour = colour;
00192         if (colour == 0) {
00193           trans++;
00194         } else {
00195           pixels++;
00196           *dst = colour;
00197           dst++;
00198         }
00199       }
00200 
00201       if (count_dst != NULL) *count_dst = pixels;
00202 
00203       /* Write line-ending */
00204       *dst = 0; dst++;
00205       *dst = 0; dst++;
00206     }
00207   }
00208 
00209   uint size = dst - (byte *)temp_dst;
00210 
00211   /* Safety check, to make sure we guessed the size correctly */
00212   assert(size < memory);
00213 
00214   /* Allocate the exact amount of memory we need */
00215   Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + size);
00216 
00217   dest_sprite->height = sprite->height;
00218   dest_sprite->width  = sprite->width;
00219   dest_sprite->x_offs = sprite->x_offs;
00220   dest_sprite->y_offs = sprite->y_offs;
00221   memcpy(dest_sprite->data, temp_dst, size);
00222 
00223   return dest_sprite;
00224 }