LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/include/basegfx/pixel - bpixel.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 21 22 95.5 %
Date: 2013-07-09 Functions: 11 12 91.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef _BGFX_PIXEL_BPIXEL_HXX
      21             : #define _BGFX_PIXEL_BPIXEL_HXX
      22             : 
      23             : #include <sal/types.h>
      24             : #include <basegfx/numeric/ftools.hxx>
      25             : #include <basegfx/color/bcolor.hxx>
      26             : #include <basegfx/basegfxdllapi.h>
      27             : 
      28             : //////////////////////////////////////////////////////////////////////////////
      29             : // predeclarations
      30             : 
      31             : //////////////////////////////////////////////////////////////////////////////
      32             : 
      33             : namespace basegfx
      34             : {
      35             :     class BASEGFX_DLLPUBLIC BPixel
      36             :     {
      37             :     protected:
      38             :         union
      39             :         {
      40             :             struct
      41             :             {
      42             :                 // bitfield
      43             :                 unsigned                                mnR : 8;        // red intensity
      44             :                 unsigned                                mnG : 8;        // green intensity
      45             :                 unsigned                                mnB : 8;        // blue intensity
      46             :                 unsigned                                mnO : 8;        // opacity, 0 == full transparence
      47             :             } maRGBO;
      48             : 
      49             :             struct
      50             :             {
      51             :                 // bitfield
      52             :                 unsigned                                mnValue : 32;   // all values
      53             :             } maCombinedRGBO;
      54             :         } maPixelUnion;
      55             : 
      56             :     public:
      57     1065068 :         BPixel()
      58             :         {
      59     1065068 :             maPixelUnion.maCombinedRGBO.mnValue = 0L;
      60     1065068 :         }
      61             : 
      62             :         // use explicit here to make sure everyone knows what he is doing. Values range from
      63             :         // 0..255 integer here.
      64             :         explicit BPixel(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue, sal_uInt8 nOpacity)
      65             :         {
      66             :             maPixelUnion.maRGBO.mnR = nRed;
      67             :             maPixelUnion.maRGBO.mnG = nGreen;
      68             :             maPixelUnion.maRGBO.mnB = nBlue;
      69             :             maPixelUnion.maRGBO.mnO = nOpacity;
      70             :         }
      71             : 
      72             :         // constructor from BColor which uses double precision color, so change it
      73             :         // to local integer format. It will also be clamped here.
      74      757241 :         BPixel(const BColor& rColor, sal_uInt8 nOpacity)
      75             :         {
      76      757241 :             maPixelUnion.maRGBO.mnR = sal_uInt8((rColor.getRed() * 255.0) + 0.5);
      77      757241 :             maPixelUnion.maRGBO.mnG = sal_uInt8((rColor.getGreen() * 255.0) + 0.5);
      78      757241 :             maPixelUnion.maRGBO.mnB = sal_uInt8((rColor.getBlue() * 255.0) + 0.5);
      79      757241 :             maPixelUnion.maRGBO.mnO = nOpacity;
      80      757241 :         }
      81             : 
      82             :         // copy constructor
      83             :         BPixel(const BPixel& rPixel)
      84             :         {
      85             :             maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
      86             :         }
      87             : 
      88     1822309 :         ~BPixel()
      89     1822309 :         {}
      90             : 
      91             :         // assignment operator
      92      757241 :         BPixel& operator=( const BPixel& rPixel )
      93             :         {
      94      757241 :             maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
      95      757241 :             return *this;
      96             :         }
      97             : 
      98             :         // data access read
      99      773708 :         sal_uInt8 getRed() const { return maPixelUnion.maRGBO.mnR; }
     100      773708 :         sal_uInt8 getGreen() const { return maPixelUnion.maRGBO.mnG; }
     101      773708 :         sal_uInt8 getBlue() const { return maPixelUnion.maRGBO.mnB; }
     102     1970624 :         sal_uInt8 getOpacity() const { return maPixelUnion.maRGBO.mnO; }
     103             :         sal_uInt32 getRedGreenBlueOpacity() const { return maPixelUnion.maCombinedRGBO.mnValue; }
     104             : 
     105             :         // data access write
     106      131848 :         void setRed(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnR = nNew; }
     107      131848 :         void setGreen(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnG = nNew; }
     108      131848 :         void setBlue(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnB = nNew; }
     109           0 :         void setOpacity(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnO = nNew; }
     110             :         void setRedGreenBlueOpacity(sal_uInt32 nRedGreenBlueOpacity) { maPixelUnion.maCombinedRGBO.mnValue = nRedGreenBlueOpacity; }
     111             :         void setRedGreenBlue(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB) { maPixelUnion.maRGBO.mnR = nR; maPixelUnion.maRGBO.mnG = nG; maPixelUnion.maRGBO.mnB = nB; }
     112             : 
     113             :         // comparators
     114             :         bool isInvisible() const { return (0 == maPixelUnion.maRGBO.mnO); }
     115             :         bool isVisible() const { return (0 != maPixelUnion.maRGBO.mnO); }
     116             :         bool isEmpty() const { return isInvisible(); }
     117             :         bool isUsed() const { return isVisible(); }
     118             : 
     119             :         bool operator==( const BPixel& rPixel ) const
     120             :         {
     121             :             return (rPixel.maPixelUnion.maCombinedRGBO.mnValue == maPixelUnion.maCombinedRGBO.mnValue);
     122             :         }
     123             : 
     124             :         bool operator!=( const BPixel& rPixel ) const
     125             :         {
     126             :             return (rPixel.maPixelUnion.maCombinedRGBO.mnValue != maPixelUnion.maCombinedRGBO.mnValue);
     127             :         }
     128             : 
     129             :         // empty element
     130             :         static const BPixel& getEmptyBPixel();
     131             :     };
     132             : 
     133             :     //////////////////////////////////////////////////////////////////////////
     134             :     // external operators
     135             : 
     136             :     inline BPixel minimum(const BPixel& rTupA, const BPixel& rTupB)
     137             :     {
     138             :         return BPixel(
     139             :             std::min(rTupB.getRed(), rTupA.getRed()),
     140             :             std::min(rTupB.getGreen(), rTupA.getGreen()),
     141             :             std::min(rTupB.getBlue(), rTupA.getBlue()),
     142             :             std::min(rTupB.getOpacity(), rTupA.getOpacity()));
     143             :     }
     144             : 
     145             :     inline BPixel maximum(const BPixel& rTupA, const BPixel& rTupB)
     146             :     {
     147             :         return BPixel(
     148             :             std::max(rTupB.getRed(), rTupA.getRed()),
     149             :             std::max(rTupB.getGreen(), rTupA.getGreen()),
     150             :             std::max(rTupB.getBlue(), rTupA.getBlue()),
     151             :             std::max(rTupB.getOpacity(), rTupA.getOpacity()));
     152             :     }
     153             : 
     154             :     inline BPixel interpolate(const BPixel& rOld1, const BPixel& rOld2, double t)
     155             :     {
     156             :         if(rOld1 == rOld2)
     157             :         {
     158             :             return rOld1;
     159             :         }
     160             :         else if(0.0 >= t)
     161             :         {
     162             :             return rOld1;
     163             :         }
     164             :         else if(1.0 <= t)
     165             :         {
     166             :             return rOld2;
     167             :         }
     168             :         else
     169             :         {
     170             :             const sal_uInt32 nFactor(fround(256.0 * t));
     171             :             const sal_uInt32 nNegFac(256L - nFactor);
     172             : 
     173             :             return BPixel(
     174             :                 (sal_uInt8)(((sal_uInt32)rOld1.getRed() * nNegFac + (sal_uInt32)rOld2.getRed() * nFactor) >> 8L),
     175             :                 (sal_uInt8)(((sal_uInt32)rOld1.getGreen() * nNegFac + (sal_uInt32)rOld2.getGreen() * nFactor) >> 8L),
     176             :                 (sal_uInt8)(((sal_uInt32)rOld1.getBlue() * nNegFac + (sal_uInt32)rOld2.getBlue() * nFactor) >> 8L),
     177             :                 (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() * nNegFac + (sal_uInt32)rOld2.getOpacity() * nFactor) >> 8L));
     178             :         }
     179             :     }
     180             : 
     181             :     inline BPixel average(const BPixel& rOld1, const BPixel& rOld2)
     182             :     {
     183             :         return BPixel(
     184             :             rOld1.getRed() == rOld2.getRed() ? rOld1.getRed() : (sal_uInt8)(((sal_uInt32)rOld1.getRed() + (sal_uInt32)rOld2.getRed()) >> 1L),
     185             :             rOld1.getGreen() == rOld2.getGreen() ? rOld1.getGreen() : (sal_uInt8)(((sal_uInt32)rOld1.getGreen() + (sal_uInt32)rOld2.getGreen()) >> 1L),
     186             :             rOld1.getBlue() == rOld2.getBlue() ? rOld1.getBlue() : (sal_uInt8)(((sal_uInt32)rOld1.getBlue() + (sal_uInt32)rOld2.getBlue()) >> 1L),
     187             :             rOld1.getOpacity() == rOld2.getOpacity() ? rOld1.getOpacity() : (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() + (sal_uInt32)rOld2.getOpacity()) >> 1L));
     188             :     }
     189             : 
     190             :     inline BPixel average(const BPixel& rOld1, const BPixel& rOld2, const BPixel& rOld3)
     191             :     {
     192             :         return BPixel(
     193             :             (rOld1.getRed() == rOld2.getRed() && rOld2.getRed() == rOld3.getRed()) ? rOld1.getRed() : (sal_uInt8)(((sal_uInt32)rOld1.getRed() + (sal_uInt32)rOld2.getRed() + (sal_uInt32)rOld3.getRed()) / 3L),
     194             :             (rOld1.getGreen() == rOld2.getGreen() && rOld2.getGreen() == rOld3.getGreen()) ? rOld1.getGreen() : (sal_uInt8)(((sal_uInt32)rOld1.getGreen() + (sal_uInt32)rOld2.getGreen() + (sal_uInt32)rOld3.getGreen()) / 3L),
     195             :             (rOld1.getBlue() == rOld2.getBlue() && rOld2.getBlue() == rOld3.getBlue()) ? rOld1.getBlue() : (sal_uInt8)(((sal_uInt32)rOld1.getBlue() + (sal_uInt32)rOld2.getBlue() + (sal_uInt32)rOld3.getBlue()) / 3L),
     196             :             (rOld1.getOpacity() == rOld2.getOpacity() && rOld2.getOpacity() == rOld3.getOpacity()) ? rOld1.getOpacity() : (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() + (sal_uInt32)rOld2.getOpacity() + (sal_uInt32)rOld3.getOpacity()) / 3L));
     197             :     }
     198             : } // end of namespace basegfx
     199             : 
     200             : #endif /* _BGFX_PIXEL_BPIXEL_HXX */
     201             : 
     202             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10