bump product version to 4.1.6.2
[LibreOffice.git] / include / basegfx / pixel / bpixel.hxx
bloba2c59101fa78d2e33b30b4ff4cf52ee4ed5b0c8d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef _BGFX_PIXEL_BPIXEL_HXX
21 #define _BGFX_PIXEL_BPIXEL_HXX
23 #include <sal/types.h>
24 #include <basegfx/numeric/ftools.hxx>
25 #include <basegfx/color/bcolor.hxx>
26 #include <basegfx/basegfxdllapi.h>
28 //////////////////////////////////////////////////////////////////////////////
29 // predeclarations
31 //////////////////////////////////////////////////////////////////////////////
33 namespace basegfx
35 class BASEGFX_DLLPUBLIC BPixel
37 protected:
38 union
40 struct
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;
49 struct
51 // bitfield
52 unsigned mnValue : 32; // all values
53 } maCombinedRGBO;
54 } maPixelUnion;
56 public:
57 BPixel()
59 maPixelUnion.maCombinedRGBO.mnValue = 0L;
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)
66 maPixelUnion.maRGBO.mnR = nRed;
67 maPixelUnion.maRGBO.mnG = nGreen;
68 maPixelUnion.maRGBO.mnB = nBlue;
69 maPixelUnion.maRGBO.mnO = nOpacity;
72 // constructor from BColor which uses double precision color, so change it
73 // to local integer format. It will also be clamped here.
74 BPixel(const BColor& rColor, sal_uInt8 nOpacity)
76 maPixelUnion.maRGBO.mnR = sal_uInt8((rColor.getRed() * 255.0) + 0.5);
77 maPixelUnion.maRGBO.mnG = sal_uInt8((rColor.getGreen() * 255.0) + 0.5);
78 maPixelUnion.maRGBO.mnB = sal_uInt8((rColor.getBlue() * 255.0) + 0.5);
79 maPixelUnion.maRGBO.mnO = nOpacity;
82 // copy constructor
83 BPixel(const BPixel& rPixel)
85 maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
88 ~BPixel()
91 // assignment operator
92 BPixel& operator=( const BPixel& rPixel )
94 maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
95 return *this;
98 // data access read
99 sal_uInt8 getRed() const { return maPixelUnion.maRGBO.mnR; }
100 sal_uInt8 getGreen() const { return maPixelUnion.maRGBO.mnG; }
101 sal_uInt8 getBlue() const { return maPixelUnion.maRGBO.mnB; }
102 sal_uInt8 getOpacity() const { return maPixelUnion.maRGBO.mnO; }
103 sal_uInt32 getRedGreenBlueOpacity() const { return maPixelUnion.maCombinedRGBO.mnValue; }
105 // data access write
106 void setRed(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnR = nNew; }
107 void setGreen(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnG = nNew; }
108 void setBlue(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnB = nNew; }
109 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; }
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(); }
119 bool operator==( const BPixel& rPixel ) const
121 return (rPixel.maPixelUnion.maCombinedRGBO.mnValue == maPixelUnion.maCombinedRGBO.mnValue);
124 bool operator!=( const BPixel& rPixel ) const
126 return (rPixel.maPixelUnion.maCombinedRGBO.mnValue != maPixelUnion.maCombinedRGBO.mnValue);
129 // empty element
130 static const BPixel& getEmptyBPixel();
133 //////////////////////////////////////////////////////////////////////////
134 // external operators
136 inline BPixel minimum(const BPixel& rTupA, const BPixel& rTupB)
138 BPixel aMin(
139 (rTupB.getRed() < rTupA.getRed()) ? rTupB.getRed() : rTupA.getRed(),
140 (rTupB.getGreen() < rTupA.getGreen()) ? rTupB.getGreen() : rTupA.getGreen(),
141 (rTupB.getBlue() < rTupA.getBlue()) ? rTupB.getBlue() : rTupA.getBlue(),
142 (rTupB.getOpacity() < rTupA.getOpacity()) ? rTupB.getOpacity() : rTupA.getOpacity());
143 return aMin;
146 inline BPixel maximum(const BPixel& rTupA, const BPixel& rTupB)
148 BPixel aMax(
149 (rTupB.getRed() > rTupA.getRed()) ? rTupB.getRed() : rTupA.getRed(),
150 (rTupB.getGreen() > rTupA.getGreen()) ? rTupB.getGreen() : rTupA.getGreen(),
151 (rTupB.getBlue() > rTupA.getBlue()) ? rTupB.getBlue() : rTupA.getBlue(),
152 (rTupB.getOpacity() > rTupA.getOpacity()) ? rTupB.getOpacity() : rTupA.getOpacity());
153 return aMax;
156 inline BPixel interpolate(const BPixel& rOld1, const BPixel& rOld2, double t)
158 if(rOld1 == rOld2)
160 return rOld1;
162 else if(0.0 >= t)
164 return rOld1;
166 else if(1.0 <= t)
168 return rOld2;
170 else
172 const sal_uInt32 nFactor(fround(256.0 * t));
173 const sal_uInt32 nNegFac(256L - nFactor);
174 return BPixel(
175 (sal_uInt8)(((sal_uInt32)rOld1.getRed() * nNegFac + (sal_uInt32)rOld2.getRed() * nFactor) >> 8L),
176 (sal_uInt8)(((sal_uInt32)rOld1.getGreen() * nNegFac + (sal_uInt32)rOld2.getGreen() * nFactor) >> 8L),
177 (sal_uInt8)(((sal_uInt32)rOld1.getBlue() * nNegFac + (sal_uInt32)rOld2.getBlue() * nFactor) >> 8L),
178 (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() * nNegFac + (sal_uInt32)rOld2.getOpacity() * nFactor) >> 8L));
182 inline BPixel average(const BPixel& rOld1, const BPixel& rOld2)
184 if(rOld1 == rOld2)
186 return rOld1;
188 else
190 return BPixel(
191 (sal_uInt8)(((sal_uInt32)rOld1.getRed() + (sal_uInt32)rOld2.getRed()) >> 1L),
192 (sal_uInt8)(((sal_uInt32)rOld1.getGreen() + (sal_uInt32)rOld2.getGreen()) >> 1L),
193 (sal_uInt8)(((sal_uInt32)rOld1.getBlue() + (sal_uInt32)rOld2.getBlue()) >> 1L),
194 (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() + (sal_uInt32)rOld2.getOpacity()) >> 1L));
198 inline BPixel average(const BPixel& rOld1, const BPixel& rOld2, const BPixel& rOld3)
200 if(rOld1 == rOld2 && rOld2 == rOld3)
202 return rOld1;
204 else
206 return BPixel(
207 (sal_uInt8)(((sal_uInt32)rOld1.getRed() + (sal_uInt32)rOld2.getRed() + (sal_uInt32)rOld3.getRed()) / 3L),
208 (sal_uInt8)(((sal_uInt32)rOld1.getGreen() + (sal_uInt32)rOld2.getGreen() + (sal_uInt32)rOld3.getGreen()) / 3L),
209 (sal_uInt8)(((sal_uInt32)rOld1.getBlue() + (sal_uInt32)rOld2.getBlue() + (sal_uInt32)rOld3.getBlue()) / 3L),
210 (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() + (sal_uInt32)rOld2.getOpacity() + (sal_uInt32)rOld3.getOpacity()) / 3L));
213 } // end of namespace basegfx
215 #endif /* _BGFX_PIXEL_BPIXEL_HXX */
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */