8bpp_optimized.cpp

Go to the documentation of this file.
00001 /* $Id: 8bpp_optimized.cpp 23889 2012-02-04 13:29:13Z michi_cc $ */
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 += sprite[i].width * sprite[i].height;
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 = sprite[i].height;
00154     int scaled_width  = sprite[i].width;
00155 
00156     for (int y = 0; y < scaled_height; y++) {
00157       uint trans = 0;
00158       uint pixels = 0;
00159       uint last_colour = 0;
00160       byte *count_dst = NULL;
00161 
00162       /* Store the scaled image */
00163       const SpriteLoader::CommonPixel *src = &sprite[i].data[y * sprite[i].width];
00164 
00165       for (int x = 0; x < scaled_width; x++) {
00166         uint colour = src++->m;
00167 
00168         if (last_colour == 0 || colour == 0 || pixels == 255) {
00169           if (count_dst != NULL) {
00170             /* Write how many non-transparent bytes we get */
00171             *count_dst = pixels;
00172             pixels = 0;
00173             count_dst = NULL;
00174           }
00175           /* As long as we find transparency bytes, keep counting */
00176           if (colour == 0 && trans != 255) {
00177             last_colour = 0;
00178             trans++;
00179             continue;
00180           }
00181           /* No longer transparency, so write the amount of transparent bytes */
00182           *dst = trans;
00183           dst++;
00184           trans = 0;
00185           /* Reserve a byte for the pixel counter */
00186           count_dst = dst;
00187           dst++;
00188         }
00189         last_colour = colour;
00190         if (colour == 0) {
00191           trans++;
00192         } else {
00193           pixels++;
00194           *dst = colour;
00195           dst++;
00196         }
00197       }
00198 
00199       if (count_dst != NULL) *count_dst = pixels;
00200 
00201       /* Write line-ending */
00202       *dst = 0; dst++;
00203       *dst = 0; dst++;
00204     }
00205   }
00206 
00207   uint size = dst - (byte *)temp_dst;
00208 
00209   /* Safety check, to make sure we guessed the size correctly */
00210   assert(size < memory);
00211 
00212   /* Allocate the exact amount of memory we need */
00213   Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + size);
00214 
00215   dest_sprite->height = sprite->height;
00216   dest_sprite->width  = sprite->width;
00217   dest_sprite->x_offs = sprite->x_offs;
00218   dest_sprite->y_offs = sprite->y_offs;
00219   memcpy(dest_sprite->data, temp_dst, size);
00220 
00221   return dest_sprite;
00222 }