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 0 : BPixel()
58 : {
59 0 : maPixelUnion.maCombinedRGBO.mnValue = 0L;
60 0 : }
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 0 : BPixel(const BColor& rColor, sal_uInt8 nOpacity)
75 : {
76 0 : maPixelUnion.maRGBO.mnR = sal_uInt8((rColor.getRed() * 255.0) + 0.5);
77 0 : maPixelUnion.maRGBO.mnG = sal_uInt8((rColor.getGreen() * 255.0) + 0.5);
78 0 : maPixelUnion.maRGBO.mnB = sal_uInt8((rColor.getBlue() * 255.0) + 0.5);
79 0 : maPixelUnion.maRGBO.mnO = nOpacity;
80 0 : }
81 :
82 : // copy constructor
83 : BPixel(const BPixel& rPixel)
84 : {
85 : maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
86 : }
87 :
88 0 : ~BPixel()
89 0 : {}
90 :
91 : // assignment operator
92 0 : BPixel& operator=( const BPixel& rPixel )
93 : {
94 0 : maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
95 0 : return *this;
96 : }
97 :
98 : // data access read
99 0 : sal_uInt8 getRed() const { return maPixelUnion.maRGBO.mnR; }
100 0 : sal_uInt8 getGreen() const { return maPixelUnion.maRGBO.mnG; }
101 0 : sal_uInt8 getBlue() const { return maPixelUnion.maRGBO.mnB; }
102 0 : sal_uInt8 getOpacity() const { return maPixelUnion.maRGBO.mnO; }
103 : sal_uInt32 getRedGreenBlueOpacity() const { return maPixelUnion.maCombinedRGBO.mnValue; }
104 :
105 : // data access write
106 0 : void setRed(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnR = nNew; }
107 0 : void setGreen(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnG = nNew; }
108 0 : 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 : 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;
144 : }
145 :
146 : inline BPixel maximum(const BPixel& rTupA, const BPixel& rTupB)
147 : {
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;
154 : }
155 :
156 : inline BPixel interpolate(const BPixel& rOld1, const BPixel& rOld2, double t)
157 : {
158 : if(rOld1 == rOld2)
159 : {
160 : return rOld1;
161 : }
162 : else if(0.0 >= t)
163 : {
164 : return rOld1;
165 : }
166 : else if(1.0 <= t)
167 : {
168 : return rOld2;
169 : }
170 : else
171 : {
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));
179 : }
180 : }
181 :
182 : inline BPixel average(const BPixel& rOld1, const BPixel& rOld2)
183 : {
184 : if(rOld1 == rOld2)
185 : {
186 : return rOld1;
187 : }
188 : else
189 : {
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));
195 : }
196 : }
197 :
198 : inline BPixel average(const BPixel& rOld1, const BPixel& rOld2, const BPixel& rOld3)
199 : {
200 : if(rOld1 == rOld2 && rOld2 == rOld3)
201 : {
202 : return rOld1;
203 : }
204 : else
205 : {
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));
211 : }
212 : }
213 : } // end of namespace basegfx
214 :
215 : #endif /* _BGFX_PIXEL_BPIXEL_HXX */
216 :
217 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|