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 : namespace basegfx
29 : {
30 : class BASEGFX_DLLPUBLIC BPixel
31 : {
32 : protected:
33 : union
34 : {
35 : struct
36 : {
37 : // bitfield
38 : unsigned mnR : 8; // red intensity
39 : unsigned mnG : 8; // green intensity
40 : unsigned mnB : 8; // blue intensity
41 : unsigned mnO : 8; // opacity, 0 == full transparence
42 : } maRGBO;
43 :
44 : struct
45 : {
46 : // bitfield
47 : unsigned mnValue : 32; // all values
48 : } maCombinedRGBO;
49 : } maPixelUnion;
50 :
51 : public:
52 2825320 : BPixel()
53 : {
54 2825320 : maPixelUnion.maCombinedRGBO.mnValue = 0L;
55 2825320 : }
56 :
57 : // use explicit here to make sure everyone knows what he is doing. Values range from
58 : // 0..255 integer here.
59 : explicit BPixel(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue, sal_uInt8 nOpacity)
60 : {
61 : maPixelUnion.maRGBO.mnR = nRed;
62 : maPixelUnion.maRGBO.mnG = nGreen;
63 : maPixelUnion.maRGBO.mnB = nBlue;
64 : maPixelUnion.maRGBO.mnO = nOpacity;
65 : }
66 :
67 : // constructor from BColor which uses double precision color, so change it
68 : // to local integer format. It will also be clamped here.
69 2532546 : BPixel(const BColor& rColor, sal_uInt8 nOpacity)
70 : {
71 2532546 : maPixelUnion.maRGBO.mnR = sal_uInt8((rColor.getRed() * 255.0) + 0.5);
72 2532546 : maPixelUnion.maRGBO.mnG = sal_uInt8((rColor.getGreen() * 255.0) + 0.5);
73 2532546 : maPixelUnion.maRGBO.mnB = sal_uInt8((rColor.getBlue() * 255.0) + 0.5);
74 2532546 : maPixelUnion.maRGBO.mnO = nOpacity;
75 2532546 : }
76 :
77 : // copy constructor
78 : BPixel(const BPixel& rPixel)
79 : {
80 : maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
81 : }
82 :
83 5357866 : ~BPixel()
84 5357866 : {}
85 :
86 : // assignment operator
87 2532546 : BPixel& operator=( const BPixel& rPixel )
88 : {
89 2532546 : maPixelUnion.maCombinedRGBO.mnValue = rPixel.maPixelUnion.maCombinedRGBO.mnValue;
90 2532546 : return *this;
91 : }
92 :
93 : // data access read
94 2387864 : sal_uInt8 getRed() const { return maPixelUnion.maRGBO.mnR; }
95 2387864 : sal_uInt8 getGreen() const { return maPixelUnion.maRGBO.mnG; }
96 2387864 : sal_uInt8 getBlue() const { return maPixelUnion.maRGBO.mnB; }
97 5568084 : sal_uInt8 getOpacity() const { return maPixelUnion.maRGBO.mnO; }
98 : sal_uInt32 getRedGreenBlueOpacity() const { return maPixelUnion.maCombinedRGBO.mnValue; }
99 :
100 : // data access write
101 354900 : void setRed(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnR = nNew; }
102 354900 : void setGreen(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnG = nNew; }
103 354900 : void setBlue(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnB = nNew; }
104 0 : void setOpacity(sal_uInt8 nNew) { maPixelUnion.maRGBO.mnO = nNew; }
105 : void setRedGreenBlueOpacity(sal_uInt32 nRedGreenBlueOpacity) { maPixelUnion.maCombinedRGBO.mnValue = nRedGreenBlueOpacity; }
106 : void setRedGreenBlue(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB) { maPixelUnion.maRGBO.mnR = nR; maPixelUnion.maRGBO.mnG = nG; maPixelUnion.maRGBO.mnB = nB; }
107 :
108 : // comparators
109 : bool isInvisible() const { return (0 == maPixelUnion.maRGBO.mnO); }
110 : bool isVisible() const { return (0 != maPixelUnion.maRGBO.mnO); }
111 : bool isEmpty() const { return isInvisible(); }
112 : bool isUsed() const { return isVisible(); }
113 :
114 : bool operator==( const BPixel& rPixel ) const
115 : {
116 : return (rPixel.maPixelUnion.maCombinedRGBO.mnValue == maPixelUnion.maCombinedRGBO.mnValue);
117 : }
118 :
119 : bool operator!=( const BPixel& rPixel ) const
120 : {
121 : return (rPixel.maPixelUnion.maCombinedRGBO.mnValue != maPixelUnion.maCombinedRGBO.mnValue);
122 : }
123 : };
124 :
125 :
126 : // external operators
127 :
128 : inline BPixel minimum(const BPixel& rTupA, const BPixel& rTupB)
129 : {
130 : return BPixel(
131 : std::min(rTupB.getRed(), rTupA.getRed()),
132 : std::min(rTupB.getGreen(), rTupA.getGreen()),
133 : std::min(rTupB.getBlue(), rTupA.getBlue()),
134 : std::min(rTupB.getOpacity(), rTupA.getOpacity()));
135 : }
136 :
137 : inline BPixel maximum(const BPixel& rTupA, const BPixel& rTupB)
138 : {
139 : return BPixel(
140 : std::max(rTupB.getRed(), rTupA.getRed()),
141 : std::max(rTupB.getGreen(), rTupA.getGreen()),
142 : std::max(rTupB.getBlue(), rTupA.getBlue()),
143 : std::max(rTupB.getOpacity(), rTupA.getOpacity()));
144 : }
145 :
146 : inline BPixel interpolate(const BPixel& rOld1, const BPixel& rOld2, double t)
147 : {
148 : if(rOld1 == rOld2)
149 : {
150 : return rOld1;
151 : }
152 : else if(0.0 >= t)
153 : {
154 : return rOld1;
155 : }
156 : else if(1.0 <= t)
157 : {
158 : return rOld2;
159 : }
160 : else
161 : {
162 : const sal_uInt32 nFactor(fround(256.0 * t));
163 : const sal_uInt32 nNegFac(256L - nFactor);
164 :
165 : return BPixel(
166 : (sal_uInt8)(((sal_uInt32)rOld1.getRed() * nNegFac + (sal_uInt32)rOld2.getRed() * nFactor) >> 8L),
167 : (sal_uInt8)(((sal_uInt32)rOld1.getGreen() * nNegFac + (sal_uInt32)rOld2.getGreen() * nFactor) >> 8L),
168 : (sal_uInt8)(((sal_uInt32)rOld1.getBlue() * nNegFac + (sal_uInt32)rOld2.getBlue() * nFactor) >> 8L),
169 : (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() * nNegFac + (sal_uInt32)rOld2.getOpacity() * nFactor) >> 8L));
170 : }
171 : }
172 :
173 : inline BPixel average(const BPixel& rOld1, const BPixel& rOld2)
174 : {
175 : return BPixel(
176 : rOld1.getRed() == rOld2.getRed() ? rOld1.getRed() : (sal_uInt8)(((sal_uInt32)rOld1.getRed() + (sal_uInt32)rOld2.getRed()) >> 1L),
177 : rOld1.getGreen() == rOld2.getGreen() ? rOld1.getGreen() : (sal_uInt8)(((sal_uInt32)rOld1.getGreen() + (sal_uInt32)rOld2.getGreen()) >> 1L),
178 : rOld1.getBlue() == rOld2.getBlue() ? rOld1.getBlue() : (sal_uInt8)(((sal_uInt32)rOld1.getBlue() + (sal_uInt32)rOld2.getBlue()) >> 1L),
179 : rOld1.getOpacity() == rOld2.getOpacity() ? rOld1.getOpacity() : (sal_uInt8)(((sal_uInt32)rOld1.getOpacity() + (sal_uInt32)rOld2.getOpacity()) >> 1L));
180 : }
181 :
182 : inline BPixel average(const BPixel& rOld1, const BPixel& rOld2, const BPixel& rOld3)
183 : {
184 : return BPixel(
185 : (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),
186 : (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),
187 : (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),
188 : (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));
189 : }
190 : } // end of namespace basegfx
191 :
192 : #endif // INCLUDED_BASEGFX_PIXEL_BPIXEL_HXX
193 :
194 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|