1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_drawinglayer.hxx"
30 
31 #include <drawinglayer/texture/texture3d.hxx>
32 #include <vcl/bmpacc.hxx>
33 #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx>
34 
35 //////////////////////////////////////////////////////////////////////////////
36 
37 namespace drawinglayer
38 {
39 	namespace texture
40 	{
41 		GeoTexSvxMono::GeoTexSvxMono(const basegfx::BColor& rSingleColor, double fOpacity)
42 		:	maSingleColor(rSingleColor),
43 			mfOpacity(fOpacity)
44 		{
45 		}
46 
47 		bool GeoTexSvxMono::operator==(const GeoTexSvx& rGeoTexSvx) const
48 		{
49 			const GeoTexSvxMono* pCompare = dynamic_cast< const GeoTexSvxMono* >(&rGeoTexSvx);
50 			return (pCompare
51 				&& maSingleColor == pCompare->maSingleColor
52 				&& mfOpacity == pCompare->mfOpacity);
53 		}
54 
55 		void GeoTexSvxMono::modifyBColor(const basegfx::B2DPoint& /*rUV*/, basegfx::BColor& rBColor, double& /*rfOpacity*/) const
56 		{
57 			rBColor = maSingleColor;
58 		}
59 
60 		void GeoTexSvxMono::modifyOpacity(const basegfx::B2DPoint& /*rUV*/, double& rfOpacity) const
61 		{
62 			rfOpacity = mfOpacity;
63 		}
64 	} // end of namespace texture
65 } // end of namespace drawinglayer
66 
67 //////////////////////////////////////////////////////////////////////////////
68 
69 namespace drawinglayer
70 {
71 	namespace texture
72 	{
73 		GeoTexSvxBitmap::GeoTexSvxBitmap(const Bitmap& rBitmap, const basegfx::B2DPoint& rTopLeft, const basegfx::B2DVector& rSize)
74 		:	maBitmap(rBitmap),
75 			mpRead(0L),
76 			maTopLeft(rTopLeft),
77 			maSize(rSize),
78 			mfMulX(0.0),
79 			mfMulY(0.0)
80 		{
81 			mpRead = maBitmap.AcquireReadAccess();
82 			OSL_ENSURE(mpRead, "GeoTexSvxBitmap: Got no read access to Bitmap (!)");
83 			mfMulX = (double)mpRead->Width() / maSize.getX();
84 			mfMulY = (double)mpRead->Height() / maSize.getY();
85 		}
86 
87 		GeoTexSvxBitmap::~GeoTexSvxBitmap()
88 		{
89 			delete mpRead;
90 		}
91 
92 		bool GeoTexSvxBitmap::impIsValid(const basegfx::B2DPoint& rUV, sal_Int32& rX, sal_Int32& rY) const
93 		{
94 			if(mpRead)
95 			{
96 				rX = (sal_Int32)((rUV.getX() - maTopLeft.getX()) * mfMulX);
97 
98 				if(rX >= 0L && rX < mpRead->Width())
99 				{
100 					rY = (sal_Int32)((rUV.getY() - maTopLeft.getY()) * mfMulY);
101 
102 					return (rY >= 0L && rY < mpRead->Height());
103 				}
104 			}
105 
106 			return false;
107 		}
108 
109 		void GeoTexSvxBitmap::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
110 		{
111 			sal_Int32 nX, nY;
112 
113 			if(impIsValid(rUV, nX, nY))
114 			{
115 				const double fConvertColor(1.0 / 255.0);
116 				const BitmapColor aBMCol(mpRead->GetColor(nY, nX));
117 				const basegfx::BColor aBSource(
118 					(double)aBMCol.GetRed() * fConvertColor,
119 					(double)aBMCol.GetGreen() * fConvertColor,
120 					(double)aBMCol.GetBlue() * fConvertColor);
121 
122 				rBColor = aBSource;
123 			}
124 			else
125 			{
126 				rfOpacity = 0.0;
127 			}
128 		}
129 
130 		void GeoTexSvxBitmap::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
131 		{
132 			sal_Int32 nX, nY;
133 
134 			if(impIsValid(rUV, nX, nY))
135 			{
136 				const BitmapColor aBMCol(mpRead->GetColor(nY, nX));
137 				const Color aColor(aBMCol.GetRed(), aBMCol.GetGreen(), aBMCol.GetBlue());
138 
139 				rfOpacity = ((double)(0xff - aColor.GetLuminance()) * (1.0 / 255.0));
140 			}
141 			else
142 			{
143 				rfOpacity = 0.0;
144 			}
145 		}
146 	} // end of namespace texture
147 } // end of namespace drawinglayer
148 
149 //////////////////////////////////////////////////////////////////////////////
150 
151 namespace drawinglayer
152 {
153 	namespace texture
154 	{
155 		GeoTexSvxBitmapTiled::GeoTexSvxBitmapTiled(const Bitmap& rBitmap, const basegfx::B2DPoint& rTopLeft, const basegfx::B2DVector& rSize)
156 		:	GeoTexSvxBitmap(rBitmap, rTopLeft, rSize)
157 		{
158 		}
159 
160 		void GeoTexSvxBitmapTiled::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
161 		{
162 			if(mpRead)
163 			{
164 				GeoTexSvxBitmap::modifyBColor(impGetCorrected(rUV), rBColor, rfOpacity);
165 			}
166 		}
167 
168 		void GeoTexSvxBitmapTiled::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
169 		{
170 			if(mpRead)
171 			{
172 				GeoTexSvxBitmap::modifyOpacity(impGetCorrected(rUV), rfOpacity);
173 			}
174 		}
175 	} // end of namespace texture
176 } // end of namespace drawinglayer
177 
178 //////////////////////////////////////////////////////////////////////////////
179 
180 namespace drawinglayer
181 {
182 	namespace texture
183 	{
184 		GeoTexSvxMultiHatch::GeoTexSvxMultiHatch(const primitive3d::HatchTexturePrimitive3D& rPrimitive, double fLogicPixelSize)
185 		:	mfLogicPixelSize(fLogicPixelSize),
186 			mp0(0L),
187 			mp1(0L),
188 			mp2(0L)
189 		{
190 			const attribute::FillHatchAttribute& rHatch(rPrimitive.getHatch());
191 			const basegfx::B2DRange aOutlineRange(0.0, 0.0, rPrimitive.getTextureSize().getX(), rPrimitive.getTextureSize().getY());
192 			const double fAngleA(rHatch.getAngle());
193 			maColor = rHatch.getColor();
194 			mbFillBackground = rHatch.isFillBackground();
195 			mp0 = new GeoTexSvxHatch(aOutlineRange, rHatch.getDistance(), fAngleA);
196 
197 			if(attribute::HATCHSTYLE_DOUBLE == rHatch.getStyle() || attribute::HATCHSTYLE_TRIPLE == rHatch.getStyle())
198 			{
199 				mp1 = new GeoTexSvxHatch(aOutlineRange, rHatch.getDistance(), fAngleA + F_PI2);
200 			}
201 
202 			if(attribute::HATCHSTYLE_TRIPLE == rHatch.getStyle())
203 			{
204 				mp2 = new GeoTexSvxHatch(aOutlineRange, rHatch.getDistance(), fAngleA + F_PI4);
205 			}
206 		}
207 
208 		GeoTexSvxMultiHatch::~GeoTexSvxMultiHatch()
209 		{
210 			delete mp0;
211 			delete mp1;
212 			delete mp2;
213 		}
214 
215 		bool GeoTexSvxMultiHatch::impIsOnHatch(const basegfx::B2DPoint& rUV) const
216 		{
217 			if(mp0->getDistanceToHatch(rUV) < mfLogicPixelSize)
218 			{
219 				return true;
220 			}
221 
222 			if(mp1 && mp1->getDistanceToHatch(rUV) < mfLogicPixelSize)
223 			{
224 				return true;
225 			}
226 
227 			if(mp2 && mp2->getDistanceToHatch(rUV) < mfLogicPixelSize)
228 			{
229 				return true;
230 			}
231 
232 			return false;
233 		}
234 
235 		void GeoTexSvxMultiHatch::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
236 		{
237 			if(impIsOnHatch(rUV))
238 			{
239 				rBColor = maColor;
240 			}
241 			else if(!mbFillBackground)
242 			{
243 				rfOpacity = 0.0;
244 			}
245 		}
246 
247 		void GeoTexSvxMultiHatch::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
248 		{
249 			if(mbFillBackground || impIsOnHatch(rUV))
250 			{
251 				rfOpacity = 1.0;
252 			}
253 			else
254 			{
255 				rfOpacity = 0.0;
256 			}
257 		}
258 	} // end of namespace texture
259 } // end of namespace drawinglayer
260 
261 //////////////////////////////////////////////////////////////////////////////
262 // eof
263