32bpp_base.hpp

Go to the documentation of this file.
00001 /* $Id: 32bpp_base.hpp 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 #ifndef BLITTER_32BPP_BASE_HPP
00013 #define BLITTER_32BPP_BASE_HPP
00014 
00015 #include "base.hpp"
00016 #include "../core/bitmath_func.hpp"
00017 #include "../core/math_func.hpp"
00018 #include "../gfx_func.h"
00019 
00021 class Blitter_32bppBase : public Blitter {
00022 public:
00023   /* virtual */ uint8 GetScreenDepth() { return 32; }
00024   /* virtual */ void *MoveTo(void *video, int x, int y);
00025   /* virtual */ void SetPixel(void *video, int x, int y, uint8 colour);
00026   /* virtual */ void DrawRect(void *video, int width, int height, uint8 colour);
00027   /* virtual */ void CopyFromBuffer(void *video, const void *src, int width, int height);
00028   /* virtual */ void CopyToBuffer(const void *video, void *dst, int width, int height);
00029   /* virtual */ void CopyImageToBuffer(const void *video, void *dst, int width, int height, int dst_pitch);
00030   /* virtual */ void ScrollBuffer(void *video, int &left, int &top, int &width, int &height, int scroll_x, int scroll_y);
00031   /* virtual */ int BufferSize(int width, int height);
00032   /* virtual */ void PaletteAnimate(const Palette &palette);
00033   /* virtual */ Blitter::PaletteAnimation UsePaletteAnimation();
00034   /* virtual */ int GetBytesPerPixel() { return 4; }
00035 
00039   static inline Colour LookupColourInPalette(uint index)
00040   {
00041     return _cur_palette.palette[index];
00042   }
00043 
00047   static inline Colour ComposeColourRGBANoCheck(uint r, uint g, uint b, uint a, Colour current)
00048   {
00049     uint cr = current.r;
00050     uint cg = current.g;
00051     uint cb = current.b;
00052 
00053     /* The 256 is wrong, it should be 255, but 256 is much faster... */
00054     return Colour(
00055               ((int)(r - cr) * a) / 256 + cr,
00056               ((int)(g - cg) * a) / 256 + cg,
00057               ((int)(b - cb) * a) / 256 + cb);
00058   }
00059 
00064   static inline Colour ComposeColourRGBA(uint r, uint g, uint b, uint a, Colour current)
00065   {
00066     if (a == 0) return current;
00067     if (a >= 255) return Colour(r, g, b);
00068 
00069     return ComposeColourRGBANoCheck(r, g, b, a, current);
00070   }
00071 
00075   static inline Colour ComposeColourPANoCheck(Colour colour, uint a, Colour current)
00076   {
00077     uint r  = colour.r;
00078     uint g  = colour.g;
00079     uint b  = colour.b;
00080 
00081     return ComposeColourRGBANoCheck(r, g, b, a, current);
00082   }
00083 
00088   static inline Colour ComposeColourPA(Colour colour, uint a, Colour current)
00089   {
00090     if (a == 0) return current;
00091     if (a >= 255) {
00092       colour.a = 255;
00093       return colour;
00094     }
00095 
00096     return ComposeColourPANoCheck(colour, a, current);
00097   }
00098 
00106   static inline Colour MakeTransparent(Colour colour, uint nom, uint denom = 256)
00107   {
00108     uint r = colour.r;
00109     uint g = colour.g;
00110     uint b = colour.b;
00111 
00112     return Colour(r * nom / denom, g * nom / denom, b * nom / denom);
00113   }
00114 
00122   static inline uint8 MakeDark(uint8 r, uint8 g, uint8 b)
00123   {
00124     /* Magic-numbers are ~66% of those used in MakeGrey() */
00125     return ((r * 13063) + (g * 25647) + (b * 4981)) / 65536;
00126   }
00127 
00133   static inline Colour MakeGrey(Colour colour)
00134   {
00135     uint r = colour.r;
00136     uint g = colour.g;
00137     uint b = colour.b;
00138 
00139     /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
00140      *  divide by it to normalize the value to a byte again. See heightmap.cpp for
00141      *  information about the formula. */
00142     uint grey = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
00143 
00144     return Colour(grey, grey, grey);
00145   }
00146 
00147   static const int DEFAULT_BRIGHTNESS = 128;
00148 
00149   static inline Colour AdjustBrightness(Colour colour, uint8 brightness)
00150   {
00151     /* Shortcut for normal brightness */
00152     if (brightness == DEFAULT_BRIGHTNESS) return colour;
00153 
00154     uint16 ob = 0;
00155     uint16 r = colour.r * brightness / DEFAULT_BRIGHTNESS;
00156     uint16 g = colour.g * brightness / DEFAULT_BRIGHTNESS;
00157     uint16 b = colour.b * brightness / DEFAULT_BRIGHTNESS;
00158 
00159     /* Sum overbright */
00160     if (r > 255) ob += r - 255;
00161     if (g > 255) ob += g - 255;
00162     if (b > 255) ob += b - 255;
00163 
00164     if (ob == 0) return Colour(r, g, b, colour.a);
00165 
00166     /* Reduce overbright strength */
00167     ob /= 2;
00168     return Colour(
00169                          r >= 255 ? 255 : min(r + ob * (255 - r) / 256, 255),
00170                          g >= 255 ? 255 : min(g + ob * (255 - g) / 256, 255),
00171                          b >= 255 ? 255 : min(b + ob * (255 - b) / 256, 255),
00172                          colour.a);
00173   }
00174 };
00175 
00176 #endif /* BLITTER_32BPP_BASE_HPP */