8bpp_optimized.cpp

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

Generated on Fri Mar 18 23:17:33 2011 for OpenTTD by  doxygen 1.6.1