32bpp_sse2.cpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_sse2.cpp 26259 2014-01-13 18:17:17Z rubidium $ */
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   uint all_sprites_size = 0;
00040   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
00041     const SpriteLoader::Sprite *src_sprite = &sprite[z];
00042     sd.infos[z].sprite_width = src_sprite->width;
00043     sd.infos[z].sprite_offset = all_sprites_size;
00044     sd.infos[z].sprite_line_size = sizeof(Colour) * src_sprite->width + sizeof(uint32) * META_LENGTH;
00045 
00046     const uint rgba_size = sd.infos[z].sprite_line_size * src_sprite->height;
00047     sd.infos[z].mv_offset = all_sprites_size + rgba_size;
00048 
00049     const uint mv_size = sizeof(MapValue) * src_sprite->width * src_sprite->height;
00050     all_sprites_size += rgba_size + mv_size;
00051   }
00052 
00053   Sprite *dst_sprite = (Sprite *) allocator(sizeof(Sprite) + sizeof(SpriteData) + all_sprites_size);
00054   dst_sprite->height = sprite->height;
00055   dst_sprite->width  = sprite->width;
00056   dst_sprite->x_offs = sprite->x_offs;
00057   dst_sprite->y_offs = sprite->y_offs;
00058   memcpy(dst_sprite->data, &sd, sizeof(SpriteData));
00059 
00060   /* Copy colours and determine flags. */
00061   bool has_remap = false;
00062   bool has_anim = false;
00063   bool has_translucency = false;
00064   for (ZoomLevel z = zoom_min; z <= zoom_max; z++) {
00065     const SpriteLoader::Sprite *src_sprite = &sprite[z];
00066     const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *) src_sprite->data;
00067     Colour *dst_rgba_line = (Colour *) &dst_sprite->data[sizeof(SpriteData) + sd.infos[z].sprite_offset];
00068     MapValue *dst_mv = (MapValue *) &dst_sprite->data[sizeof(SpriteData) + sd.infos[z].mv_offset];
00069     for (uint y = src_sprite->height; y != 0; y--) {
00070       Colour *dst_rgba = dst_rgba_line + META_LENGTH;
00071       for (uint x = src_sprite->width; x != 0; x--) {
00072         if (src->a != 0) {
00073           dst_rgba->a = src->a;
00074           if (src->a != 0 && src->a != 255) has_translucency = true;
00075           dst_mv->m = src->m;
00076           if (src->m != 0) {
00077             /* Do some accounting for flags. */
00078             has_remap = true;
00079             if (src->m >= PALETTE_ANIM_START) has_anim = true;
00080 
00081             /* Get brightest value (or default brightness if it's a black pixel). */
00082             const uint8 rgb_max = max(src->r, max(src->g, src->b));
00083             dst_mv->v = (rgb_max == 0) ? Blitter_32bppBase::DEFAULT_BRIGHTNESS : rgb_max;
00084 
00085             /* Pre-convert the mapping channel to a RGB value. */
00086             const Colour colour = AdjustBrightneSSE(Blitter_32bppBase::LookupColourInPalette(src->m), dst_mv->v);
00087             dst_rgba->r = colour.r;
00088             dst_rgba->g = colour.g;
00089             dst_rgba->b = colour.b;
00090           } else {
00091             dst_rgba->r = src->r;
00092             dst_rgba->g = src->g;
00093             dst_rgba->b = src->b;
00094             dst_mv->v = Blitter_32bppBase::DEFAULT_BRIGHTNESS;
00095           }
00096         } else {
00097           dst_rgba->data = 0;
00098           *(uint16*) dst_mv = 0;
00099         }
00100         dst_rgba++;
00101         dst_mv++;
00102         src++;
00103       }
00104 
00105       /* Count the number of transparent pixels from the left. */
00106       dst_rgba = dst_rgba_line + META_LENGTH;
00107       uint32 nb_pix_transp = 0;
00108       for (uint x = src_sprite->width; x != 0; x--) {
00109         if (dst_rgba->a == 0) nb_pix_transp++;
00110         else break;
00111         dst_rgba++;
00112       }
00113       (*dst_rgba_line).data = nb_pix_transp;
00114 
00115       Colour *nb_right = dst_rgba_line + 1;
00116       dst_rgba_line = (Colour*) ((byte*) dst_rgba_line + sd.infos[z].sprite_line_size);
00117 
00118       /* Count the number of transparent pixels from the right. */
00119       dst_rgba = dst_rgba_line - 1;
00120       nb_pix_transp = 0;
00121       for (uint x = src_sprite->width; x != 0; x--) {
00122         if (dst_rgba->a == 0) nb_pix_transp++;
00123         else break;
00124         dst_rgba--;
00125       }
00126       (*nb_right).data = nb_pix_transp;
00127     }
00128   }
00129 
00130   /* Store sprite flags. */
00131   sd.flags = SF_NONE;
00132   if (has_translucency) sd.flags |= SF_TRANSLUCENT;
00133   if (!has_remap) sd.flags |= SF_NO_REMAP;
00134   if (!has_anim) sd.flags |= SF_NO_ANIM;
00135   memcpy(dst_sprite->data, &sd, sizeof(SpriteData));
00136 
00137   return dst_sprite;
00138 }
00139 
00140 #endif /* WITH_SSE */