LCOV - code coverage report
Current view: top level - basegfx/inc/basegfx/pixel - bpixel.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 5 0.0 %
Date: 2012-08-25 Functions: 0 2 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #ifndef _BGFX_PIXEL_BPIXEL_HXX
      30                 :            : #define _BGFX_PIXEL_BPIXEL_HXX
      31                 :            : 
      32                 :            : #include <sal/types.h>
      33                 :            : #include <basegfx/numeric/ftools.hxx>
      34                 :            : #include <basegfx/color/bcolor.hxx>
      35                 :            : #include <basegfx/basegfxdllapi.h>
      36                 :            : 
      37                 :            : //////////////////////////////////////////////////////////////////////////////
      38                 :            : // predeclarations
      39                 :            : 
      40                 :            : //////////////////////////////////////////////////////////////////////////////
      41                 :            : 
      42                 :            : namespace basegfx
      43                 :            : {
      44                 :            :     class BASEGFX_DLLPUBLIC BPixel
      45                 :            :     {
      46                 :            :     protected:
      47                 :            :         union
      48                 :            :         {
      49                 :            :             struct
      50                 :            :             {
      51                 :            :                 // bitfield
      52                 :            :                 unsigned                                mnR : 8;        // red intensity
      53                 :            :                 unsigned                                mnG : 8;        // green intensity
      54                 :            :                 unsigned                                mnB : 8;        // blue intensity
      55                 :            :                 unsigned                                mnO : 8;        // opacity, 0 == full transparence
      56                 :            :             } maRGBO;
      57                 :            : 
      58                 :            :             struct
      59                 :            :             {
      60                 :            :                 // bitfield
      61                 :            :                 unsigned                                mnValue : 32;   // all values
      62                 :            :             } maCombinedRGBO;
      63                 :            :         } maPixelUnion;
      64                 :            : 
      65                 :            :     public:
      66                 :          0 :         BPixel()
      67                 :            :         {
      68                 :          0 :             maPixelUnion.maCombinedRGBO.mnValue = 0L;
      69                 :          0 :         }
      70                 :            : 
      71                 :            :         // use explicit here to make sure everyone knows what he is doing. Values range from
      72                 :            :         // 0..255 integer here.
      73                 :            :         explicit BPixel(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue, sal_uInt8 nOpacity)
      74                 :            :         {
      75                 :            :             maPixelUnion.maRGBO.mnR = nRed;
      76                 :            :             maPixelUnion.maRGBO.mnG = nGreen;
      77                 :            :             maPixelUnion.maRGBO.mnB = nBlue;
      78                 :            :             maPixelUnion.maRGBO.mnO = nOpacity;
      79                 :            :         }
      80                 :            : 
      81                 :            :         // constructor from BColor which uses double precision color, so change it
      82                 :            :         // to local integer format. It will also be clamped here.
      83                 :            :         BPixel(const BColor& rColor, sal_uInt8 nOpacity)
      84                 :            :         {
      85                 :            :             maPixelUnion.maRGBO.mnR = sal_uInt8((rColor.getRed() * 255.0) + 0.5);
      86                 :            :             maPixelUnion.maRGBO.mnG = sal_uInt8((rColor.getGreen() * 255.0) + 0.5);
      87                 :            :             maPixelUnion.maRGBO.mnB = sal_uInt8((rColor.getBlue() * 255.0) + 0.5);
      88                 :            :             maPixelUnion.maRGBO.mnO = nOpacity;
      89                 :            :         }
      90                 :            : 
      91                 :            :         // copy constructor
      92                 :            :         BPixel(const BPixel& rPixel)
      93                 :            :         {
      94                 :            :             maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
      95                 :            :         }
      96                 :            : 
      97                 :          0 :         ~BPixel()
      98                 :          0 :         {}
      99                 :            : 
     100                 :            :         // assignment operator
     101                 :            :         BPixel& operator=( const BPixel& rPixel )
     102                 :            :         {
     103                 :            :             maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
     104                 :            :             return *this;
     105                 :            :         }
     106                 :            : 
     107                 :            :         // data access read
     108                 :            :         sal_uInt8 getRed() const { return maPixelUnion.maRGBO.mnR; }
     109                 :            :         sal_uInt8 getGreen() const { return maPixelUnion.maRGBO.mnG; }
     110                 :            :         sal_uInt8 getBlue() const { return maPixelUnion.maRGBO.mnB; }
     111                 :            :         sal_uInt8 getOpacity() const { return maPixelUnion.maRGBO.mnO; }
     112                 :            :         sal_uInt32 getRedGreenBlueOpacity() const { return maPixelUnion.maCombinedRGBO.mnValue; }
     113                 :            : 
     114                 :            :         // data access write
     115                 :            :         void setRed(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnR = nNew; }
     116                 :            :         void setGreen(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnG = nNew; }
     117                 :            :         void setBlue(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnB = nNew; }
     118                 :            :         void setOpacity(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnO = nNew; }
     119                 :            :         void setRedGreenBlueOpacity(sal_uInt32 nRedGreenBlueOpacity) { maPixelUnion.maCombinedRGBO.mnValue = nRedGreenBlueOpacity; }
     120                 :            :         void setRedGreenBlue(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB) { maPixelUnion.maRGBO.mnR = nR; maPixelUnion.maRGBO.mnG = nG; maPixelUnion.maRGBO.mnB = nB; }
     121                 :            : 
     122                 :            :         // comparators
     123                 :            :         bool isInvisible() const { return (0 == maPixelUnion.maRGBO.mnO); }
     124                 :            :         bool isVisible() const { return (0 != maPixelUnion.maRGBO.mnO); }
     125                 :            :         bool isEmpty() const { return isInvisible(); }
     126                 :            :         bool isUsed() const { return isVisible(); }
     127                 :            : 
     128                 :            :         bool operator==( const BPixel& rPixel ) const
     129                 :            :         {
     130                 :            :             return (rPixel.maPixelUnion.maCombinedRGBO.mnValue == maPixelUnion.maCombinedRGBO.mnValue);
     131                 :            :         }
     132                 :            : 
     133                 :            :         bool operator!=( const BPixel& rPixel ) const
     134                 :            :         {
     135                 :            :             return (rPixel.maPixelUnion.maCombinedRGBO.mnValue != maPixelUnion.maCombinedRGBO.mnValue);
     136                 :            :         }
     137                 :            : 
     138                 :            :         // empty element
     139                 :            :         static const BPixel& getEmptyBPixel();
     140                 :            :     };
     141                 :            : 
     142                 :            :     //////////////////////////////////////////////////////////////////////////
     143                 :            :     // external operators
     144                 :            : 
     145                 :            :     inline BPixel minimum(const BPixel& rTupA, const BPixel& rTupB)
     146                 :            :     {
     147                 :            :         BPixel aMin(
     148                 :            :             (rTupB.getRed() < rTupA.getRed()) ? rTupB.getRed() : rTupA.getRed(),
     149                 :            :             (rTupB.getGreen() < rTupA.getGreen()) ? rTupB.getGreen() : rTupA.getGreen(),
     150                 :            :             (rTupB.getBlue() < rTupA.getBlue()) ? rTupB.getBlue() : rTupA.getBlue(),
     151                 :            :             (rTupB.getOpacity() < rTupA.getOpacity()) ? rTupB.getOpacity() : rTupA.getOpacity());
     152                 :            :         return aMin;
     153                 :            :     }
     154                 :            : 
     155                 :            :     inline BPixel maximum(const BPixel& rTupA, const BPixel& rTupB)
     156                 :            :     {
     157                 :            :         BPixel aMax(
     158                 :            :             (rTupB.getRed() > rTupA.getRed()) ? rTupB.getRed() : rTupA.getRed(),
     159                 :            :             (rTupB.getGreen() > rTupA.getGreen()) ? rTupB.getGreen() : rTupA.getGreen(),
     160                 :            :             (rTupB.getBlue() > rTupA.getBlue()) ? rTupB.getBlue() : rTupA.getBlue(),
     161                 :            :             (rTupB.getOpacity() > rTupA.getOpacity()) ? rTupB.getOpacity() : rTupA.getOpacity());
     162                 :            :         return aMax;
     163                 :            :     }
     164                 :            : 
     165                 :            :     inline BPixel interpolate(const BPixel& rOld1, const BPixel& rOld2, double t)
     166                 :            :     {
     167                 :            :         if(rOld1 == rOld2)
     168                 :            :         {
     169                 :            :             return rOld1;
     170                 :            :         }
     171                 :            :         else if(0.0 >= t)
     172                 :            :         {
     173                 :            :             return rOld1;
     174                 :            :         }
     175                 :            :         else if(1.0 <= t)
     176                 :            :         {
     177                 :            :             return rOld2;
     178                 :            :         }
     179                 :            :         else
     180                 :            :         {
     181                 :            :             const sal_uInt32 nFactor(fround(256.0 * t));
     182                 :            :             const sal_uInt32 nNegFac(256L - nFactor);
     183                 :            :             return BPixel(
     184                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getRed() * nNegFac + (sal_uInt32)rOld2.getRed() * nFactor) >> 8L),
     185                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getGreen() * nNegFac + (sal_uInt32)rOld2.getGreen() * nFactor) >> 8L),
     186                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getBlue() * nNegFac + (sal_uInt32)rOld2.getBlue() * nFactor) >> 8L),
     187                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() * nNegFac + (sal_uInt32)rOld2.getOpacity() * nFactor) >> 8L));
     188                 :            :         }
     189                 :            :     }
     190                 :            : 
     191                 :            :     inline BPixel average(const BPixel& rOld1, const BPixel& rOld2)
     192                 :            :     {
     193                 :            :         if(rOld1 == rOld2)
     194                 :            :         {
     195                 :            :             return rOld1;
     196                 :            :         }
     197                 :            :         else
     198                 :            :         {
     199                 :            :             return BPixel(
     200                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getRed() + (sal_uInt32)rOld2.getRed()) >> 1L),
     201                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getGreen() + (sal_uInt32)rOld2.getGreen()) >> 1L),
     202                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getBlue() + (sal_uInt32)rOld2.getBlue()) >> 1L),
     203                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() + (sal_uInt32)rOld2.getOpacity()) >> 1L));
     204                 :            :         }
     205                 :            :     }
     206                 :            : 
     207                 :            :     inline BPixel average(const BPixel& rOld1, const BPixel& rOld2, const BPixel& rOld3)
     208                 :            :     {
     209                 :            :         if(rOld1 == rOld2 && rOld2 == rOld3)
     210                 :            :         {
     211                 :            :             return rOld1;
     212                 :            :         }
     213                 :            :         else
     214                 :            :         {
     215                 :            :             return BPixel(
     216                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getRed() + (sal_uInt32)rOld2.getRed() + (sal_uInt32)rOld3.getRed()) / 3L),
     217                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getGreen() + (sal_uInt32)rOld2.getGreen() + (sal_uInt32)rOld3.getGreen()) / 3L),
     218                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getBlue() + (sal_uInt32)rOld2.getBlue() + (sal_uInt32)rOld3.getBlue()) / 3L),
     219                 :            :                 (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() + (sal_uInt32)rOld2.getOpacity() + (sal_uInt32)rOld3.getOpacity()) / 3L));
     220                 :            :         }
     221                 :            :     }
     222                 :            : } // end of namespace basegfx
     223                 :            : 
     224                 :            : #endif /* _BGFX_PIXEL_BPIXEL_HXX */
     225                 :            : 
     226                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10