1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
33 GeoTexSvxMono::GeoTexSvxMono(
34 const basegfx::BColor
& rSingleColor
,
36 : maSingleColor(rSingleColor
),
41 bool GeoTexSvxMono::operator==(const GeoTexSvx
& rGeoTexSvx
) const
43 const GeoTexSvxMono
* pCompare
= dynamic_cast< const GeoTexSvxMono
* >(&rGeoTexSvx
);
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
67 GeoTexSvxBitmapEx::GeoTexSvxBitmapEx(
68 const BitmapEx
& rBitmapEx
,
69 const basegfx::B2DRange
& rRange
)
70 : maBitmapEx(rBitmapEx
),
72 maTopLeft(rRange
.getMinimum()),
73 maSize(rRange
.getRange()),
77 mbIsTransparent(maBitmapEx
.IsTransparent())
79 // #121194# Todo: use alpha channel, too (for 3d)
80 maBitmap
= maBitmapEx
.GetBitmap();
84 if(maBitmapEx
.IsAlpha())
87 maTransparence
= rBitmapEx
.GetAlpha().GetBitmap();
91 maTransparence
= rBitmapEx
.GetMask();
94 mpReadTransparence
= Bitmap::ScopedReadAccess(maTransparence
);
98 mpReadBitmap
= Bitmap::ScopedReadAccess(maBitmap
);
99 SAL_WARN_IF(!mpReadBitmap
, "drawinglayer", "GeoTexSvxBitmapEx: Got no read access to Bitmap");
102 mfMulX
= static_cast<double>(mpReadBitmap
->Width()) / maSize
.getX();
103 mfMulY
= static_cast<double>(mpReadBitmap
->Height()) / maSize
.getY();
106 if(maSize
.getX() <= 1.0)
111 if(maSize
.getY() <= 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
:
129 case TransparentType::Color
:
131 const BitmapColor
aBitmapColor(mpReadBitmap
->GetColor(rY
, rX
));
133 if(maBitmapEx
.GetTransparentColor() == aBitmapColor
)
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
));
147 return aBitmapColor
.GetIndex();
151 if(0x00 != aBitmapColor
.GetIndex())
163 bool GeoTexSvxBitmapEx::impIsValid(const basegfx::B2DPoint
& rUV
, sal_Int32
& rX
, sal_Int32
& rY
) const
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());
180 void GeoTexSvxBitmapEx::modifyBColor(const basegfx::B2DPoint
& rUV
, basegfx::BColor
& rBColor
, double& rfOpacity
) const
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
);
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));
213 void GeoTexSvxBitmapEx::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
217 if(impIsValid(rUV
, nX
, nY
))
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
));
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));
241 } // end of namespace texture
242 } // end of namespace drawinglayer
245 namespace drawinglayer
249 basegfx::B2DPoint
GeoTexSvxBitmapExTiled::impGetCorrected(const basegfx::B2DPoint
& rUV
) const
251 double fX(rUV
.getX() - maTopLeft
.getX());
252 double fY(rUV
.getY() - maTopLeft
.getY());
256 const sal_Int32
nCol(static_cast< sal_Int32
>((fY
< 0.0 ? maSize
.getY() -fY
: fY
) / maSize
.getY()));
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()));
269 fY
+= mfOffsetY
* maSize
.getY();
273 fX
= fmod(fX
, maSize
.getX());
274 fY
= fmod(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
,
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
306 GeoTexSvxBitmapEx::modifyBColor(impGetCorrected(rUV
), rBColor
, rfOpacity
);
310 void GeoTexSvxBitmapExTiled::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
314 GeoTexSvxBitmapEx::modifyOpacity(impGetCorrected(rUV
), rfOpacity
);
317 } // end of namespace texture
318 } // end of namespace drawinglayer
321 namespace drawinglayer
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(
338 rHatch
.getDistance(),
341 if(attribute::HatchStyle::Double
== rHatch
.getStyle() || attribute::HatchStyle::Triple
== rHatch
.getStyle())
343 mp1
.reset( new GeoTexSvxHatch(
346 rHatch
.getDistance(),
350 if(attribute::HatchStyle::Triple
== rHatch
.getStyle())
352 mp2
.reset( new GeoTexSvxHatch(
355 rHatch
.getDistance(),
360 GeoTexSvxMultiHatch::~GeoTexSvxMultiHatch()
364 bool GeoTexSvxMultiHatch::impIsOnHatch(const basegfx::B2DPoint
& rUV
) const
366 if(mp0
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
371 if(mp1
&& mp1
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
376 if(mp2
&& mp2
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
384 void GeoTexSvxMultiHatch::modifyBColor(const basegfx::B2DPoint
& rUV
, basegfx::BColor
& rBColor
, double& rfOpacity
) const
386 if(impIsOnHatch(rUV
))
390 else if(!mbFillBackground
)
396 void GeoTexSvxMultiHatch::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
398 if(mbFillBackground
|| impIsOnHatch(rUV
))
407 } // end of namespace texture
408 } // end of namespace drawinglayer
410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */