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 INCLUDED_BASEGFX_PIXEL_BPIXEL_HXX
21 : #define INCLUDED_BASEGFX_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 :
130 :
131 : // external operators
132 :
133 : inline BPixel minimum(const BPixel& rTupA, const BPixel& rTupB)
134 : {
135 : return BPixel(
136 : std::min(rTupB.getRed(), rTupA.getRed()),
137 : std::min(rTupB.getGreen(), rTupA.getGreen()),
138 : std::min(rTupB.getBlue(), rTupA.getBlue()),
139 : std::min(rTupB.getOpacity(), rTupA.getOpacity()));
140 : }
141 :
142 : inline BPixel maximum(const BPixel& rTupA, const BPixel& rTupB)
143 : {
144 : return BPixel(
145 : std::max(rTupB.getRed(), rTupA.getRed()),
146 : std::max(rTupB.getGreen(), rTupA.getGreen()),
147 : std::max(rTupB.getBlue(), rTupA.getBlue()),
148 : std::max(rTupB.getOpacity(), rTupA.getOpacity()));
149 : }
150 :
151 : inline BPixel interpolate(const BPixel& rOld1, const BPixel& rOld2, double t)
152 : {
153 : if(rOld1 == rOld2)
154 : {
155 : return rOld1;
156 : }
157 : else if(0.0 >= t)
158 : {
159 : return rOld1;
160 : }
161 : else if(1.0 <= t)
162 : {
163 : return rOld2;
164 : }
165 : else
166 : {
167 : const sal_uInt32 nFactor(fround(256.0 * t));
168 : const sal_uInt32 nNegFac(256L - nFactor);
169 :
170 : return BPixel(
171 : (sal_uInt8)(((sal_uInt32)rOld1.getRed() * nNegFac + (sal_uInt32)rOld2.getRed() * nFactor) >> 8L),
172 : (sal_uInt8)(((sal_uInt32)rOld1.getGreen() * nNegFac + (sal_uInt32)rOld2.getGreen() * nFactor) >> 8L),
173 : (sal_uInt8)(((sal_uInt32)rOld1.getBlue() * nNegFac + (sal_uInt32)rOld2.getBlue() * nFactor) >> 8L),
174 : (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() * nNegFac + (sal_uInt32)rOld2.getOpacity() * nFactor) >> 8L));
175 : }
176 : }
177 :
178 : inline BPixel average(const BPixel& rOld1, const BPixel& rOld2)
179 : {
180 : return BPixel(
181 : rOld1.getRed() == rOld2.getRed() ? rOld1.getRed() : (sal_uInt8)(((sal_uInt32)rOld1.getRed() + (sal_uInt32)rOld2.getRed()) >> 1L),
182 : rOld1.getGreen() == rOld2.getGreen() ? rOld1.getGreen() : (sal_uInt8)(((sal_uInt32)rOld1.getGreen() + (sal_uInt32)rOld2.getGreen()) >> 1L),
183 : rOld1.getBlue() == rOld2.getBlue() ? rOld1.getBlue() : (sal_uInt8)(((sal_uInt32)rOld1.getBlue() + (sal_uInt32)rOld2.getBlue()) >> 1L),
184 : rOld1.getOpacity() == rOld2.getOpacity() ? rOld1.getOpacity() : (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() + (sal_uInt32)rOld2.getOpacity()) >> 1L));
185 : }
186 :
187 : inline BPixel average(const BPixel& rOld1, const BPixel& rOld2, const BPixel& rOld3)
188 : {
189 : return BPixel(
190 : (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),
191 : (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),
192 : (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),
193 : (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));
194 : }
195 : } // end of namespace basegfx
196 :
197 : #endif // INCLUDED_BASEGFX_PIXEL_BPIXEL_HXX
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|