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

Generated on Sun Jun 5 04:19:54 2011 for OpenTTD by  doxygen 1.6.1