| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# csplugincommon/canvas/draw_common.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2004 by Jorrit Tyberghein
    00003               (C) 2004 by Frank Richter
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_CSPLUGINCOMMON_CANVAS_DRAW_COMMON_H__
    00021 #define __CS_CSPLUGINCOMMON_CANVAS_DRAW_COMMON_H__
    00022
    00027 #include "csplugincommon/canvas/graph2d.h"
    00028
    00033
    00034 template<class Tpixel>
    00035 static void SplitAlpha (const int color, Tpixel& colorPart, uint8& alphaPart)
    00036 {
    00037   colorPart = color ^ 0xff000000;
    00038   alphaPart = 255 - (color >> 24);
    00039 }
    00040
    00042 template<class Tpixel>
    00043 class csPixMixerCopy
    00044 {
    00045   Tpixel color;
    00046 public:
    00047   csPixMixerCopy (csGraphics2D* /*G2D*/, Tpixel color, uint8 /*alpha*/)
    00048   {
    00049     csPixMixerCopy::color = color;
    00050   }
    00051   inline void Mix (Tpixel& dest)
    00052   {
    00053     dest = color;
    00054   }
    00055 };
    00056
    00058 template<class Tpixel>
    00059 class csPixMixerRGBA
    00060 {
    00061   uint32 rbMask, gaMask;
    00062   int intAlphaInv;
    00063   uint32 intRB, intGA;
    00064   int gShift, gShift2;
    00065   int alphaPrec;
    00066 public:
    00067   csPixMixerRGBA (csGraphics2D* G2D, Tpixel color, uint8 alpha)
    00068   {
    00069     alphaPrec = G2D->pfmt.GreenBits;
    00070     const int alphaShift = 8 - alphaPrec;
    00071     const int intAlpha = (alpha + 1) >> alphaShift;
    00072     intAlphaInv = (255 - alpha + 1) >> alphaShift;
    00073     gShift = G2D->pfmt.GreenShift;
    00074     gShift2 = alphaPrec - gShift;
    00075     // Makes the assumption that colors are laid out ARGB ...
    00076     rbMask = G2D->pfmt.RedMask | G2D->pfmt.BlueMask;
    00077     intRB = ((color & rbMask) * intAlpha) >> alphaPrec;
    00078     gaMask = G2D->pfmt.GreenMask | G2D->pfmt.AlphaMask;
    00079     intGA = (((color & gaMask) >> gShift) * intAlpha) >> gShift2;
    00080   }
    00081   inline void Mix (Tpixel& dest) const
    00082   {
    00083     const Tpixel destCol = dest;
    00084     const Tpixel newRB =
    00085       ((intRB + (((destCol & rbMask) * intAlphaInv) >> alphaPrec)) & rbMask);
    00086     const Tpixel newGA =
    00087       ((intGA + (((((destCol & gaMask) >> gShift) * intAlphaInv)) >> gShift2))
    00088         & gaMask);
    00089     dest = newRB | newGA;
    00090   }
    00091 };
    00092
    00094 template<class Tpixel>
    00095 class csPixMixerNoop
    00096 {
    00097 public:
    00098   csPixMixerNoop (csGraphics2D* /*G2D*/, Tpixel /*color*/, uint8 /*alpha*/)
    00099   { }
    00100   inline void Mix (Tpixel& /*dest*/)
    00101   { }
    00102 };
    00103
    00106 #endif // __CS_CSPLUGINCOMMON_CANVAS_DRAW_COMMON_H__