1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_drawinglayer.hxx"
26 
27 #include <drawinglayer/texture/texture3d.hxx>
28 #include <vcl/bmpacc.hxx>
29 #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx>
30 
31 //////////////////////////////////////////////////////////////////////////////
32 
33 namespace drawinglayer
34 {
35 	namespace texture
36 	{
37 		GeoTexSvxMono::GeoTexSvxMono(const basegfx::BColor& rSingleColor, double fOpacity)
38 		:	maSingleColor(rSingleColor),
39 			mfOpacity(fOpacity)
40 		{
41 		}
42 
43 		bool GeoTexSvxMono::operator==(const GeoTexSvx& rGeoTexSvx) const
44 		{
45 			const GeoTexSvxMono* pCompare = dynamic_cast< const GeoTexSvxMono* >(&rGeoTexSvx);
46 			return (pCompare
47 				&& maSingleColor == pCompare->maSingleColor
48 				&& mfOpacity == pCompare->mfOpacity);
49 		}
50 
51 		void GeoTexSvxMono::modifyBColor(const basegfx::B2DPoint& /*rUV*/, basegfx::BColor& rBColor, double& /*rfOpacity*/) const
52 		{
53 			rBColor = maSingleColor;
54 		}
55 
56 		void GeoTexSvxMono::modifyOpacity(const basegfx::B2DPoint& /*rUV*/, double& rfOpacity) const
57 		{
58 			rfOpacity = mfOpacity;
59 		}
60 	} // end of namespace texture
61 } // end of namespace drawinglayer
62 
63 //////////////////////////////////////////////////////////////////////////////
64 
65 namespace drawinglayer
66 {
67 	namespace texture
68 	{
69 		GeoTexSvxBitmap::GeoTexSvxBitmap(const Bitmap& rBitmap, const basegfx::B2DPoint& rTopLeft, const basegfx::B2DVector& rSize)
70 		:	maBitmap(rBitmap),
71 			mpRead(0L),
72 			maTopLeft(rTopLeft),
73 			maSize(rSize),
74 			mfMulX(0.0),
75 			mfMulY(0.0)
76 		{
77 			mpRead = maBitmap.AcquireReadAccess();
78 			OSL_ENSURE(mpRead, "GeoTexSvxBitmap: Got no read access to Bitmap (!)");
79 			mfMulX = (double)mpRead->Width() / maSize.getX();
80 			mfMulY = (double)mpRead->Height() / maSize.getY();
81 		}
82 
83 		GeoTexSvxBitmap::~GeoTexSvxBitmap()
84 		{
85 			delete mpRead;
86 		}
87 
88 		bool GeoTexSvxBitmap::impIsValid(const basegfx::B2DPoint& rUV, sal_Int32& rX, sal_Int32& rY) const
89 		{
90 			if(mpRead)
91 			{
92 				rX = (sal_Int32)((rUV.getX() - maTopLeft.getX()) * mfMulX);
93 
94 				if(rX >= 0L && rX < mpRead->Width())
95 				{
96 					rY = (sal_Int32)((rUV.getY() - maTopLeft.getY()) * mfMulY);
97 
98 					return (rY >= 0L && rY < mpRead->Height());
99 				}
100 			}
101 
102 			return false;
103 		}
104 
105 		void GeoTexSvxBitmap::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
106 		{
107 			sal_Int32 nX, nY;
108 
109 			if(impIsValid(rUV, nX, nY))
110 			{
111 				const double fConvertColor(1.0 / 255.0);
112 				const BitmapColor aBMCol(mpRead->GetColor(nY, nX));
113 				const basegfx::BColor aBSource(
114 					(double)aBMCol.GetRed() * fConvertColor,
115 					(double)aBMCol.GetGreen() * fConvertColor,
116 					(double)aBMCol.GetBlue() * fConvertColor);
117 
118 				rBColor = aBSource;
119 			}
120 			else
121 			{
122 				rfOpacity = 0.0;
123 			}
124 		}
125 
126 		void GeoTexSvxBitmap::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
127 		{
128 			sal_Int32 nX, nY;
129 
130 			if(impIsValid(rUV, nX, nY))
131 			{
132 				const BitmapColor aBMCol(mpRead->GetColor(nY, nX));
133 				const Color aColor(aBMCol.GetRed(), aBMCol.GetGreen(), aBMCol.GetBlue());
134 
135 				rfOpacity = ((double)(0xff - aColor.GetLuminance()) * (1.0 / 255.0));
136 			}
137 			else
138 			{
139 				rfOpacity = 0.0;
140 			}
141 		}
142 	} // end of namespace texture
143 } // end of namespace drawinglayer
144 
145 //////////////////////////////////////////////////////////////////////////////
146 
147 namespace drawinglayer
148 {
149 	namespace texture
150 	{
151 		GeoTexSvxBitmapTiled::GeoTexSvxBitmapTiled(const Bitmap& rBitmap, const basegfx::B2DPoint& rTopLeft, const basegfx::B2DVector& rSize)
152 		:	GeoTexSvxBitmap(rBitmap, rTopLeft, rSize)
153 		{
154 		}
155 
156 		void GeoTexSvxBitmapTiled::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
157 		{
158 			if(mpRead)
159 			{
160 				GeoTexSvxBitmap::modifyBColor(impGetCorrected(rUV), rBColor, rfOpacity);
161 			}
162 		}
163 
164 		void GeoTexSvxBitmapTiled::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
165 		{
166 			if(mpRead)
167 			{
168 				GeoTexSvxBitmap::modifyOpacity(impGetCorrected(rUV), rfOpacity);
169 			}
170 		}
171 	} // end of namespace texture
172 } // end of namespace drawinglayer
173 
174 //////////////////////////////////////////////////////////////////////////////
175 
176 namespace drawinglayer
177 {
178 	namespace texture
179 	{
180 		GeoTexSvxMultiHatch::GeoTexSvxMultiHatch(const primitive3d::HatchTexturePrimitive3D& rPrimitive, double fLogicPixelSize)
181 		:	mfLogicPixelSize(fLogicPixelSize),
182 			mp0(0L),
183 			mp1(0L),
184 			mp2(0L)
185 		{
186 			const attribute::FillHatchAttribute& rHatch(rPrimitive.getHatch());
187 			const basegfx::B2DRange aOutlineRange(0.0, 0.0, rPrimitive.getTextureSize().getX(), rPrimitive.getTextureSize().getY());
188 			const double fAngleA(rHatch.getAngle());
189 			maColor = rHatch.getColor();
190 			mbFillBackground = rHatch.isFillBackground();
191 			mp0 = new GeoTexSvxHatch(aOutlineRange, rHatch.getDistance(), fAngleA);
192 
193 			if(attribute::HATCHSTYLE_DOUBLE == rHatch.getStyle() || attribute::HATCHSTYLE_TRIPLE == rHatch.getStyle())
194 			{
195 				mp1 = new GeoTexSvxHatch(aOutlineRange, rHatch.getDistance(), fAngleA + F_PI2);
196 			}
197 
198 			if(attribute::HATCHSTYLE_TRIPLE == rHatch.getStyle())
199 			{
200 				mp2 = new GeoTexSvxHatch(aOutlineRange, rHatch.getDistance(), fAngleA + F_PI4);
201 			}
202 		}
203 
204 		GeoTexSvxMultiHatch::~GeoTexSvxMultiHatch()
205 		{
206 			delete mp0;
207 			delete mp1;
208 			delete mp2;
209 		}
210 
211 		bool GeoTexSvxMultiHatch::impIsOnHatch(const basegfx::B2DPoint& rUV) const
212 		{
213 			if(mp0->getDistanceToHatch(rUV) < mfLogicPixelSize)
214 			{
215 				return true;
216 			}
217 
218 			if(mp1 && mp1->getDistanceToHatch(rUV) < mfLogicPixelSize)
219 			{
220 				return true;
221 			}
222 
223 			if(mp2 && mp2->getDistanceToHatch(rUV) < mfLogicPixelSize)
224 			{
225 				return true;
226 			}
227 
228 			return false;
229 		}
230 
231 		void GeoTexSvxMultiHatch::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
232 		{
233 			if(impIsOnHatch(rUV))
234 			{
235 				rBColor = maColor;
236 			}
237 			else if(!mbFillBackground)
238 			{
239 				rfOpacity = 0.0;
240 			}
241 		}
242 
243 		void GeoTexSvxMultiHatch::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
244 		{
245 			if(mbFillBackground || impIsOnHatch(rUV))
246 			{
247 				rfOpacity = 1.0;
248 			}
249 			else
250 			{
251 				rfOpacity = 0.0;
252 			}
253 		}
254 	} // end of namespace texture
255 } // end of namespace drawinglayer
256 
257 //////////////////////////////////////////////////////////////////////////////
258 // eof
259