# 8bpp_optimized.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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   memset(temp_dst, 0, sizeof(*temp_dst));
    00145   byte *dst = temp_dst->data;
    00146
    00147   /* Make the sprites per zoom-level */
    00148   for (ZoomLevel i = zoom_min; i <= zoom_max; i++) {
    00149     /* Store the index table */
    00150     uint offset = dst - temp_dst->data;
    00151     temp_dst->offset[i] = offset;
    00152
    00153     /* cache values, because compiler can't cache it */
    00154     int scaled_height = sprite[i].height;
    00155     int scaled_width  = sprite[i].width;
    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[i].data[y * sprite[i].width];
    00165
    00166       for (int x = 0; x < scaled_width; x++) {
    00167         uint colour = src++->m;
    00168
    00169         if (last_colour == 0 || colour == 0 || pixels == 255) {
    00170           if (count_dst != NULL) {
    00171             /* Write how many non-transparent bytes we get */
    00172             *count_dst = pixels;
    00173             pixels = 0;
    00174             count_dst = NULL;
    00175           }
    00176           /* As long as we find transparency bytes, keep counting */
    00177           if (colour == 0 && trans != 255) {
    00178             last_colour = 0;
    00179             trans++;
    00180             continue;
    00181           }
    00182           /* No longer transparency, so write the amount of transparent bytes */
    00183           *dst = trans;
    00184           dst++;
    00185           trans = 0;
    00186           /* Reserve a byte for the pixel counter */
    00187           count_dst = dst;
    00188           dst++;
    00189         }
    00190         last_colour = colour;
    00191         if (colour == 0) {
    00192           trans++;
    00193         } else {
    00194           pixels++;
    00195           *dst = colour;
    00196           dst++;
    00197         }
    00198       }
    00199
    00200       if (count_dst != NULL) *count_dst = pixels;
    00201
    00202       /* Write line-ending */
    00203       *dst = 0; dst++;
    00204       *dst = 0; dst++;
    00205     }
    00206   }
    00207
    00208   uint size = dst - (byte *)temp_dst;
    00209
    00210   /* Safety check, to make sure we guessed the size correctly */
    00211   assert(size < memory);
    00212
    00213   /* Allocate the exact amount of memory we need */
    00214   Sprite *dest_sprite = (Sprite *)allocator(sizeof(*dest_sprite) + size);
    00215
    00216   dest_sprite->height = sprite->height;
    00217   dest_sprite->width  = sprite->width;
    00218   dest_sprite->x_offs = sprite->x_offs;
    00219   dest_sprite->y_offs = sprite->y_offs;
    00220   memcpy(dest_sprite->data, temp_dst, size);
    00221
    00222   return dest_sprite;
    00223 }