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