Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / drawinglayer / source / texture / texture3d.cxx
bloba3915d5f290d477a19983853727b40c6de8f2812
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <drawinglayer/texture/texture3d.hxx>
21 #include <vcl/bitmapaccess.hxx>
22 #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx>
25 namespace drawinglayer
27 namespace texture
29 GeoTexSvxMono::GeoTexSvxMono(
30 const basegfx::BColor& rSingleColor,
31 double fOpacity)
32 : maSingleColor(rSingleColor),
33 mfOpacity(fOpacity)
37 bool GeoTexSvxMono::operator==(const GeoTexSvx& rGeoTexSvx) const
39 const GeoTexSvxMono* pCompare = dynamic_cast< const GeoTexSvxMono* >(&rGeoTexSvx);
41 return (pCompare
42 && maSingleColor == pCompare->maSingleColor
43 && mfOpacity == pCompare->mfOpacity);
46 void GeoTexSvxMono::modifyBColor(const basegfx::B2DPoint& /*rUV*/, basegfx::BColor& rBColor, double& /*rfOpacity*/) const
48 rBColor = maSingleColor;
51 void GeoTexSvxMono::modifyOpacity(const basegfx::B2DPoint& /*rUV*/, double& rfOpacity) const
53 rfOpacity = mfOpacity;
55 } // end of namespace texture
56 } // end of namespace drawinglayer
59 namespace drawinglayer
61 namespace texture
63 GeoTexSvxBitmapEx::GeoTexSvxBitmapEx(
64 const BitmapEx& rBitmapEx,
65 const basegfx::B2DRange& rRange)
66 : maBitmapEx(rBitmapEx),
67 mpReadBitmap(nullptr),
68 maTransparence(),
69 mpReadTransparence(nullptr),
70 maTopLeft(rRange.getMinimum()),
71 maSize(rRange.getRange()),
72 mfMulX(0.0),
73 mfMulY(0.0),
74 mbIsAlpha(false),
75 mbIsTransparent(maBitmapEx.IsTransparent())
77 // #121194# Todo: use alpha channel, too (for 3d)
78 mpReadBitmap = maBitmapEx.GetBitmap().AcquireReadAccess();
79 OSL_ENSURE(mpReadBitmap, "GeoTexSvxBitmapEx: Got no read access to Bitmap (!)");
81 if(mbIsTransparent)
83 if(maBitmapEx.IsAlpha())
85 mbIsAlpha = true;
86 maTransparence = rBitmapEx.GetAlpha().GetBitmap();
88 else
90 maTransparence = rBitmapEx.GetMask();
93 mpReadTransparence = maTransparence.AcquireReadAccess();
96 mfMulX = (double)mpReadBitmap->Width() / maSize.getX();
97 mfMulY = (double)mpReadBitmap->Height() / maSize.getY();
99 if(maSize.getX() <= 1.0)
101 maSize.setX(1.0);
104 if(maSize.getY() <= 1.0)
106 maSize.setY(1.0);
110 GeoTexSvxBitmapEx::~GeoTexSvxBitmapEx()
112 delete mpReadTransparence;
113 delete mpReadBitmap;
116 sal_uInt8 GeoTexSvxBitmapEx::impGetTransparence(sal_Int32& rX, sal_Int32& rY) const
118 switch(maBitmapEx.GetTransparentType())
120 case TRANSPARENT_NONE:
122 break;
124 case TRANSPARENT_COLOR:
126 const BitmapColor aBitmapColor(mpReadBitmap->GetColor(rY, rX));
128 if(maBitmapEx.GetTransparentColor() == aBitmapColor.operator Color())
130 return 255;
133 break;
135 case TRANSPARENT_BITMAP:
137 OSL_ENSURE(mpReadTransparence, "OOps, transparence type Bitmap, but no read access created in the constructor (?)");
138 const BitmapColor aBitmapColor(mpReadTransparence->GetPixel(rY, rX));
140 if(mbIsAlpha)
142 return aBitmapColor.GetIndex();
144 else
146 if(0x00 != aBitmapColor.GetIndex())
148 return 255;
151 break;
155 return 0;
158 bool GeoTexSvxBitmapEx::impIsValid(const basegfx::B2DPoint& rUV, sal_Int32& rX, sal_Int32& rY) const
160 if(mpReadBitmap)
162 rX = (sal_Int32)((rUV.getX() - maTopLeft.getX()) * mfMulX);
164 if(rX >= 0L && rX < mpReadBitmap->Width())
166 rY = (sal_Int32)((rUV.getY() - maTopLeft.getY()) * mfMulY);
168 return (rY >= 0L && rY < mpReadBitmap->Height());
172 return false;
175 void GeoTexSvxBitmapEx::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
177 sal_Int32 nX, nY;
179 if(impIsValid(rUV, nX, nY))
181 const double fConvertColor(1.0 / 255.0);
182 const BitmapColor aBMCol(mpReadBitmap->GetColor(nY, nX));
183 const basegfx::BColor aBSource(
184 (double)aBMCol.GetRed() * fConvertColor,
185 (double)aBMCol.GetGreen() * fConvertColor,
186 (double)aBMCol.GetBlue() * fConvertColor);
188 rBColor = aBSource;
190 if(mbIsTransparent)
192 // when we have a transparence, make use of it
193 const sal_uInt8 aLuminance(impGetTransparence(nX, nY));
195 rfOpacity = ((double)(0xff - aLuminance) * (1.0 / 255.0));
197 else
199 rfOpacity = 1.0;
202 else
204 rfOpacity = 0.0;
208 void GeoTexSvxBitmapEx::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
210 sal_Int32 nX, nY;
212 if(impIsValid(rUV, nX, nY))
214 if(mbIsTransparent)
216 // this texture has an alpha part, use it
217 const sal_uInt8 aLuminance(impGetTransparence(nX, nY));
218 const double fNewOpacity((double)(0xff - aLuminance) * (1.0 / 255.0));
220 rfOpacity = 1.0 - ((1.0 - fNewOpacity) * (1.0 - rfOpacity));
222 else
224 // this texture is a color bitmap used as transparence map
225 const BitmapColor aBMCol(mpReadBitmap->GetColor(nY, nX));
226 const Color aColor(aBMCol.GetRed(), aBMCol.GetGreen(), aBMCol.GetBlue());
228 rfOpacity = ((double)(0xff - aColor.GetLuminance()) * (1.0 / 255.0));
231 else
233 rfOpacity = 0.0;
236 } // end of namespace texture
237 } // end of namespace drawinglayer
240 namespace drawinglayer
242 namespace texture
244 basegfx::B2DPoint GeoTexSvxBitmapExTiled::impGetCorrected(const basegfx::B2DPoint& rUV) const
246 double fX(rUV.getX() - maTopLeft.getX());
247 double fY(rUV.getY() - maTopLeft.getY());
249 if(mbUseOffsetX)
251 const sal_Int32 nCol(static_cast< sal_Int32 >((fY < 0.0 ? maSize.getY() -fY : fY) / maSize.getY()));
253 if(nCol % 2)
255 fX += mfOffsetX * maSize.getX();
258 else if(mbUseOffsetY)
260 const sal_Int32 nRow(static_cast< sal_Int32 >((fX < 0.0 ? maSize.getX() -fX : fX) / maSize.getX()));
262 if(nRow % 2)
264 fY += mfOffsetY * maSize.getY();
268 fX = fmod(fX, maSize.getX());
269 fY = fmod(fY, maSize.getY());
271 if(fX < 0.0)
273 fX += maSize.getX();
276 if(fY < 0.0)
278 fY += maSize.getY();
281 return basegfx::B2DPoint(fX + maTopLeft.getX(), fY + maTopLeft.getY());
284 GeoTexSvxBitmapExTiled::GeoTexSvxBitmapExTiled(
285 const BitmapEx& rBitmapEx,
286 const basegfx::B2DRange& rRange,
287 double fOffsetX,
288 double fOffsetY)
289 : GeoTexSvxBitmapEx(rBitmapEx, rRange),
290 mfOffsetX(basegfx::clamp(fOffsetX, 0.0, 1.0)),
291 mfOffsetY(basegfx::clamp(fOffsetY, 0.0, 1.0)),
292 mbUseOffsetX(!basegfx::fTools::equalZero(mfOffsetX)),
293 mbUseOffsetY(!mbUseOffsetX && !basegfx::fTools::equalZero(mfOffsetY))
297 void GeoTexSvxBitmapExTiled::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
299 if(mpReadBitmap)
301 GeoTexSvxBitmapEx::modifyBColor(impGetCorrected(rUV), rBColor, rfOpacity);
305 void GeoTexSvxBitmapExTiled::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
307 if(mpReadBitmap)
309 GeoTexSvxBitmapEx::modifyOpacity(impGetCorrected(rUV), rfOpacity);
312 } // end of namespace texture
313 } // end of namespace drawinglayer
316 namespace drawinglayer
318 namespace texture
320 GeoTexSvxMultiHatch::GeoTexSvxMultiHatch(
321 const primitive3d::HatchTexturePrimitive3D& rPrimitive,
322 double fLogicPixelSize)
323 : mfLogicPixelSize(fLogicPixelSize),
324 mp0(nullptr),
325 mp1(nullptr),
326 mp2(nullptr)
328 const attribute::FillHatchAttribute& rHatch(rPrimitive.getHatch());
329 const basegfx::B2DRange aOutlineRange(0.0, 0.0, rPrimitive.getTextureSize().getX(), rPrimitive.getTextureSize().getY());
330 const double fAngleA(rHatch.getAngle());
331 maColor = rHatch.getColor();
332 mbFillBackground = rHatch.isFillBackground();
333 mp0 = new GeoTexSvxHatch(
334 aOutlineRange,
335 aOutlineRange,
336 rHatch.getDistance(),
337 fAngleA);
339 if(attribute::HatchStyle::Double == rHatch.getStyle() || attribute::HatchStyle::Triple == rHatch.getStyle())
341 mp1 = new GeoTexSvxHatch(
342 aOutlineRange,
343 aOutlineRange,
344 rHatch.getDistance(),
345 fAngleA + F_PI2);
348 if(attribute::HatchStyle::Triple == rHatch.getStyle())
350 mp2 = new GeoTexSvxHatch(
351 aOutlineRange,
352 aOutlineRange,
353 rHatch.getDistance(),
354 fAngleA + F_PI4);
358 GeoTexSvxMultiHatch::~GeoTexSvxMultiHatch()
360 delete mp0;
361 delete mp1;
362 delete mp2;
365 bool GeoTexSvxMultiHatch::impIsOnHatch(const basegfx::B2DPoint& rUV) const
367 if(mp0->getDistanceToHatch(rUV) < mfLogicPixelSize)
369 return true;
372 if(mp1 && mp1->getDistanceToHatch(rUV) < mfLogicPixelSize)
374 return true;
377 if(mp2 && mp2->getDistanceToHatch(rUV) < mfLogicPixelSize)
379 return true;
382 return false;
385 void GeoTexSvxMultiHatch::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
387 if(impIsOnHatch(rUV))
389 rBColor = maColor;
391 else if(!mbFillBackground)
393 rfOpacity = 0.0;
397 void GeoTexSvxMultiHatch::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
399 if(mbFillBackground || impIsOnHatch(rUV))
401 rfOpacity = 1.0;
403 else
405 rfOpacity = 0.0;
408 } // end of namespace texture
409 } // end of namespace drawinglayer
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */