32bpp_sse2.cpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_sse2.cpp 26541 2014-04-29 18:18:52Z 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 #ifdef WITH_SSE
00013 
00014 #include "../stdafx.h"
00015 #include "../zoom_func.h"
00016 #include "../settings_type.h"
00017 #include "32bpp_sse2.hpp"
00018 #include "32bpp_sse_func.hpp"
00019 
00021 static FBlitter_32bppSSE2 iFBlitter_32bppSSE2;
00022 
00023 Sprite *Blitter_32bppSSE_Base::Encode(const SpriteLoader::Sprite *sprite, AllocatorProc *allocator)
00024 {
00025   /* First uint32 of a line = the number of transparent pixels from the left.
00026    * Second uint32 of a line = the number of transparent pixels from the right.
00027    * Then all RGBA then all MV.
00028    */
00029   ZoomLevel zoom_min = ZOOM_LVL_NORMAL;
00030   ZoomLevel zoom_max = ZOOM_LVL_NORMAL;
00031   if (sprite->type != ST_FONT) {
00032     zoom_min = _settings_client.gui.zoom_min;
00033     zoom_max = _settings_client.gui.zoom_max;
00034     if (zoom_max == zoom_min) zoom_max = ZOOM_LVL_MAX;
00035   }
00036 
00037   /* Calculate sizes and allocate. */
00038   SpriteData sd;
00039   memset(&sd, 0, sizeof(sd));
00040   uint all_sprites_size = 0;
00041   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
00042     const SpriteLoader::Sprite *src_sprite = &sprite[z];
00043     sd.infos[z].sprite_width = src_sprite->width;
00044     sd.infos[z].sprite_offset = all_sprites_size;
00045     sd.infos[z].sprite_line_size = sizeof(Colour) * src_sprite->width + sizeof(uint32) * META_LENGTH;
00046 
00047     const uint rgba_size = sd.infos[z].sprite_line_size * src_sprite->height;
00048     sd.infos[z].mv_offset = all_sprites_size + rgba_size;
00049 
00050     const uint mv_size = sizeof(MapValue) * src_sprite->width * src_sprite->height;
00051     all_sprites_size += rgba_size + mv_size;
00052   }
00053 
00054   Sprite *dst_sprite = (Sprite *) allocator(sizeof(Sprite) + sizeof(SpriteData) + all_sprites_size);
00055   dst_sprite->height = sprite->height;
00056   dst_sprite->width  = sprite->width;
00057   dst_sprite->x_offs = sprite->x_offs;
00058   dst_sprite->y_offs = sprite->y_offs;
00059   memcpy(dst_sprite->data, &sd, sizeof(SpriteData));
00060 
00061   /* Copy colours and determine flags. */
00062   bool has_remap = false;
00063   bool has_anim = false;
00064   bool has_translucency = false;
00065   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
00066     const SpriteLoader::Sprite *src_sprite = &sprite[z];
00067     const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *) src_sprite->data;
00068     Colour *dst_rgba_line = (Colour *) &dst_sprite->data[sizeof(SpriteData) + sd.infos[z].sprite_offset];
00069     MapValue *dst_mv = (MapValue *) &dst_sprite->data[sizeof(SpriteData) + sd.infos[z].mv_offset];
00070     for (uint y = src_sprite->height; y != 0; y--) {
00071       Colour *dst_rgba = dst_rgba_line + META_LENGTH;
00072       for (uint x = src_sprite->width; x != 0; x--) {
00073         if (src->a != 0) {
00074           dst_rgba->a = src->a;
00075           if (src->a != 0 && src->a != 255) has_translucency = true;
00076           dst_mv->m = src->m;
00077           if (src->m != 0) {
00078             /* Do some accounting for flags. */
00079             has_remap = true;
00080             if (src->m >= PALETTE_ANIM_START) has_anim = true;
00081 
00082             /* Get brightest value (or default brightness if it's a black pixel). */
00083             const uint8 rgb_max = max(src->r, max(src->g, src->b));
00084             dst_mv->v = (rgb_max == 0) ? Blitter_32bppBase::DEFAULT_BRIGHTNESS : rgb_max;
00085 
00086             /* Pre-convert the mapping channel to a RGB value. */
00087             const Colour colour = AdjustBrightneSSE(Blitter_32bppBase::LookupColourInPalette(src->m), dst_mv->v);
00088             dst_rgba->r = colour.r;
00089             dst_rgba->g = colour.g;
00090             dst_rgba->b = colour.b;
00091           } else {
00092             dst_rgba->r = src->r;
00093             dst_rgba->g = src->g;
00094             dst_rgba->b = src->b;
00095             dst_mv->v = Blitter_32bppBase::DEFAULT_BRIGHTNESS;
00096           }
00097         } else {
00098           dst_rgba->data = 0;
00099           *(uint16*) dst_mv = 0;
00100         }
00101         dst_rgba++;
00102         dst_mv++;
00103         src++;
00104       }
00105 
00106       /* Count the number of transparent pixels from the left. */
00107       dst_rgba = dst_rgba_line + META_LENGTH;
00108       uint32 nb_pix_transp = 0;
00109       for (uint x = src_sprite->width; x != 0; x--) {
00110         if (dst_rgba->a == 0) nb_pix_transp++;
00111         else break;
00112         dst_rgba++;
00113       }
00114       (*dst_rgba_line).data = nb_pix_transp;
00115 
00116       Colour *nb_right = dst_rgba_line + 1;
00117       dst_rgba_line = (Colour*) ((byte*) dst_rgba_line + sd.infos[z].sprite_line_size);
00118 
00119       /* Count the number of transparent pixels from the right. */
00120       dst_rgba = dst_rgba_line - 1;
00121       nb_pix_transp = 0;
00122       for (uint x = src_sprite->width; x != 0; x--) {
00123         if (dst_rgba->a == 0) nb_pix_transp++;
00124         else break;
00125         dst_rgba--;
00126       }
00127       (*nb_right).data = nb_pix_transp;
00128     }
00129   }
00130 
00131   /* Store sprite flags. */
00132   sd.flags = SF_NONE;
00133   if (has_translucency) sd.flags |= SF_TRANSLUCENT;
00134   if (!has_remap) sd.flags |= SF_NO_REMAP;
00135   if (!has_anim) sd.flags |= SF_NO_ANIM;
00136   memcpy(dst_sprite->data, &sd, sizeof(SpriteData));
00137 
00138   return dst_sprite;
00139 }
00140 
00141 #endif /* WITH_SSE */