Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / drawinglayer / source / texture / texture3d.cxx
blob3c5f7d5e29bf0bdecb22c4e4928201dff9e829f6
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 <sal/config.h>
22 #include <algorithm>
24 #include <drawinglayer/texture/texture3d.hxx>
25 #include <vcl/bitmapaccess.hxx>
26 #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx>
27 #include <sal/log.hxx>
29 namespace drawinglayer
31 namespace texture
33 GeoTexSvxMono::GeoTexSvxMono(
34 const basegfx::BColor& rSingleColor,
35 double fOpacity)
36 : maSingleColor(rSingleColor),
37 mfOpacity(fOpacity)
41 bool GeoTexSvxMono::operator==(const GeoTexSvx& rGeoTexSvx) const
43 const GeoTexSvxMono* pCompare = dynamic_cast< const GeoTexSvxMono* >(&rGeoTexSvx);
45 return (pCompare
46 && maSingleColor == pCompare->maSingleColor
47 && mfOpacity == pCompare->mfOpacity);
50 void GeoTexSvxMono::modifyBColor(const basegfx::B2DPoint& /*rUV*/, basegfx::BColor& rBColor, double& /*rfOpacity*/) const
52 rBColor = maSingleColor;
55 void GeoTexSvxMono::modifyOpacity(const basegfx::B2DPoint& /*rUV*/, double& rfOpacity) const
57 rfOpacity = mfOpacity;
59 } // end of namespace texture
60 } // end of namespace drawinglayer
63 namespace drawinglayer
65 namespace texture
67 GeoTexSvxBitmapEx::GeoTexSvxBitmapEx(
68 const BitmapEx& rBitmapEx,
69 const basegfx::B2DRange& rRange)
70 : maBitmapEx(rBitmapEx),
71 maTransparence(),
72 maTopLeft(rRange.getMinimum()),
73 maSize(rRange.getRange()),
74 mfMulX(0.0),
75 mfMulY(0.0),
76 mbIsAlpha(false),
77 mbIsTransparent(maBitmapEx.IsTransparent())
79 // #121194# Todo: use alpha channel, too (for 3d)
80 maBitmap = maBitmapEx.GetBitmap();
82 if(mbIsTransparent)
84 if(maBitmapEx.IsAlpha())
86 mbIsAlpha = true;
87 maTransparence = rBitmapEx.GetAlpha().GetBitmap();
89 else
91 maTransparence = rBitmapEx.GetMask();
94 mpReadTransparence = Bitmap::ScopedReadAccess(maTransparence);
97 if (!!maBitmap)
98 mpReadBitmap = Bitmap::ScopedReadAccess(maBitmap);
99 SAL_WARN_IF(!mpReadBitmap, "drawinglayer", "GeoTexSvxBitmapEx: Got no read access to Bitmap");
100 if (mpReadBitmap)
102 mfMulX = static_cast<double>(mpReadBitmap->Width()) / maSize.getX();
103 mfMulY = static_cast<double>(mpReadBitmap->Height()) / maSize.getY();
106 if(maSize.getX() <= 1.0)
108 maSize.setX(1.0);
111 if(maSize.getY() <= 1.0)
113 maSize.setY(1.0);
117 GeoTexSvxBitmapEx::~GeoTexSvxBitmapEx()
121 sal_uInt8 GeoTexSvxBitmapEx::impGetTransparence(sal_Int32 rX, sal_Int32 rY) const
123 switch(maBitmapEx.GetTransparentType())
125 case TransparentType::NONE:
127 break;
129 case TransparentType::Color:
131 const BitmapColor aBitmapColor(mpReadBitmap->GetColor(rY, rX));
133 if(maBitmapEx.GetTransparentColor() == aBitmapColor)
135 return 255;
138 break;
140 case TransparentType::Bitmap:
142 OSL_ENSURE(mpReadTransparence, "OOps, transparence type Bitmap, but no read access created in the constructor (?)");
143 const BitmapColor aBitmapColor(mpReadTransparence->GetPixel(rY, rX));
145 if(mbIsAlpha)
147 return aBitmapColor.GetIndex();
149 else
151 if(0x00 != aBitmapColor.GetIndex())
153 return 255;
156 break;
160 return 0;
163 bool GeoTexSvxBitmapEx::impIsValid(const basegfx::B2DPoint& rUV, sal_Int32& rX, sal_Int32& rY) const
165 if(mpReadBitmap)
167 rX = static_cast<sal_Int32>((rUV.getX() - maTopLeft.getX()) * mfMulX);
169 if(rX >= 0 && rX < mpReadBitmap->Width())
171 rY = static_cast<sal_Int32>((rUV.getY() - maTopLeft.getY()) * mfMulY);
173 return (rY >= 0 && rY < mpReadBitmap->Height());
177 return false;
180 void GeoTexSvxBitmapEx::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
182 sal_Int32 nX, nY;
184 if(impIsValid(rUV, nX, nY))
186 const double fConvertColor(1.0 / 255.0);
187 const BitmapColor aBMCol(mpReadBitmap->GetColor(nY, nX));
188 const basegfx::BColor aBSource(
189 static_cast<double>(aBMCol.GetRed()) * fConvertColor,
190 static_cast<double>(aBMCol.GetGreen()) * fConvertColor,
191 static_cast<double>(aBMCol.GetBlue()) * fConvertColor);
193 rBColor = aBSource;
195 if(mbIsTransparent)
197 // when we have a transparence, make use of it
198 const sal_uInt8 aLuminance(impGetTransparence(nX, nY));
200 rfOpacity = (static_cast<double>(0xff - aLuminance) * (1.0 / 255.0));
202 else
204 rfOpacity = 1.0;
207 else
209 rfOpacity = 0.0;
213 void GeoTexSvxBitmapEx::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
215 sal_Int32 nX, nY;
217 if(impIsValid(rUV, nX, nY))
219 if(mbIsTransparent)
221 // this texture has an alpha part, use it
222 const sal_uInt8 aLuminance(impGetTransparence(nX, nY));
223 const double fNewOpacity(static_cast<double>(0xff - aLuminance) * (1.0 / 255.0));
225 rfOpacity = 1.0 - ((1.0 - fNewOpacity) * (1.0 - rfOpacity));
227 else
229 // this texture is a color bitmap used as transparence map
230 const BitmapColor aBMCol(mpReadBitmap->GetColor(nY, nX));
231 const Color aColor(aBMCol.GetRed(), aBMCol.GetGreen(), aBMCol.GetBlue());
233 rfOpacity = (static_cast<double>(0xff - aColor.GetLuminance()) * (1.0 / 255.0));
236 else
238 rfOpacity = 0.0;
241 } // end of namespace texture
242 } // end of namespace drawinglayer
245 namespace drawinglayer
247 namespace texture
249 basegfx::B2DPoint GeoTexSvxBitmapExTiled::impGetCorrected(const basegfx::B2DPoint& rUV) const
251 double fX(rUV.getX() - maTopLeft.getX());
252 double fY(rUV.getY() - maTopLeft.getY());
254 if(mbUseOffsetX)
256 const sal_Int32 nCol(static_cast< sal_Int32 >((fY < 0.0 ? maSize.getY() -fY : fY) / maSize.getY()));
258 if(nCol % 2)
260 fX += mfOffsetX * maSize.getX();
263 else if(mbUseOffsetY)
265 const sal_Int32 nRow(static_cast< sal_Int32 >((fX < 0.0 ? maSize.getX() -fX : fX) / maSize.getX()));
267 if(nRow % 2)
269 fY += mfOffsetY * maSize.getY();
273 fX = fmod(fX, maSize.getX());
274 fY = fmod(fY, maSize.getY());
276 if(fX < 0.0)
278 fX += maSize.getX();
281 if(fY < 0.0)
283 fY += maSize.getY();
286 return basegfx::B2DPoint(fX + maTopLeft.getX(), fY + maTopLeft.getY());
289 GeoTexSvxBitmapExTiled::GeoTexSvxBitmapExTiled(
290 const BitmapEx& rBitmapEx,
291 const basegfx::B2DRange& rRange,
292 double fOffsetX,
293 double fOffsetY)
294 : GeoTexSvxBitmapEx(rBitmapEx, rRange),
295 mfOffsetX(std::clamp(fOffsetX, 0.0, 1.0)),
296 mfOffsetY(std::clamp(fOffsetY, 0.0, 1.0)),
297 mbUseOffsetX(!basegfx::fTools::equalZero(mfOffsetX)),
298 mbUseOffsetY(!mbUseOffsetX && !basegfx::fTools::equalZero(mfOffsetY))
302 void GeoTexSvxBitmapExTiled::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
304 if(mpReadBitmap)
306 GeoTexSvxBitmapEx::modifyBColor(impGetCorrected(rUV), rBColor, rfOpacity);
310 void GeoTexSvxBitmapExTiled::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
312 if(mpReadBitmap)
314 GeoTexSvxBitmapEx::modifyOpacity(impGetCorrected(rUV), rfOpacity);
317 } // end of namespace texture
318 } // end of namespace drawinglayer
321 namespace drawinglayer
323 namespace texture
325 GeoTexSvxMultiHatch::GeoTexSvxMultiHatch(
326 const primitive3d::HatchTexturePrimitive3D& rPrimitive,
327 double fLogicPixelSize)
328 : mfLogicPixelSize(fLogicPixelSize)
330 const attribute::FillHatchAttribute& rHatch(rPrimitive.getHatch());
331 const basegfx::B2DRange aOutlineRange(0.0, 0.0, rPrimitive.getTextureSize().getX(), rPrimitive.getTextureSize().getY());
332 const double fAngleA(rHatch.getAngle());
333 maColor = rHatch.getColor();
334 mbFillBackground = rHatch.isFillBackground();
335 mp0.reset( new GeoTexSvxHatch(
336 aOutlineRange,
337 aOutlineRange,
338 rHatch.getDistance(),
339 fAngleA) );
341 if(attribute::HatchStyle::Double == rHatch.getStyle() || attribute::HatchStyle::Triple == rHatch.getStyle())
343 mp1.reset( new GeoTexSvxHatch(
344 aOutlineRange,
345 aOutlineRange,
346 rHatch.getDistance(),
347 fAngleA + F_PI2) );
350 if(attribute::HatchStyle::Triple == rHatch.getStyle())
352 mp2.reset( new GeoTexSvxHatch(
353 aOutlineRange,
354 aOutlineRange,
355 rHatch.getDistance(),
356 fAngleA + F_PI4) );
360 GeoTexSvxMultiHatch::~GeoTexSvxMultiHatch()
364 bool GeoTexSvxMultiHatch::impIsOnHatch(const basegfx::B2DPoint& rUV) const
366 if(mp0->getDistanceToHatch(rUV) < mfLogicPixelSize)
368 return true;
371 if(mp1 && mp1->getDistanceToHatch(rUV) < mfLogicPixelSize)
373 return true;
376 if(mp2 && mp2->getDistanceToHatch(rUV) < mfLogicPixelSize)
378 return true;
381 return false;
384 void GeoTexSvxMultiHatch::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
386 if(impIsOnHatch(rUV))
388 rBColor = maColor;
390 else if(!mbFillBackground)
392 rfOpacity = 0.0;
396 void GeoTexSvxMultiHatch::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
398 if(mbFillBackground || impIsOnHatch(rUV))
400 rfOpacity = 1.0;
402 else
404 rfOpacity = 0.0;
407 } // end of namespace texture
408 } // end of namespace drawinglayer
410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */