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 <texture/texture3d.hxx>
25 #include <vcl/BitmapReadAccess.hxx>
26 #include <vcl/BitmapTools.hxx>
27 #include <primitive3d/hatchtextureprimitive3d.hxx>
28 #include <sal/log.hxx>
29 #include <osl/diagnose.h>
31 namespace drawinglayer::texture
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
;
61 GeoTexSvxBitmapEx::GeoTexSvxBitmapEx(
62 const BitmapEx
& rBitmapEx
,
63 const basegfx::B2DRange
& rRange
)
64 : maBitmapEx(rBitmapEx
),
65 maTopLeft(rRange
.getMinimum()),
66 maSize(rRange
.getRange()),
69 mbIsAlpha(maBitmapEx
.IsAlpha())
71 if(vcl::bitmap::convertBitmap32To24Plus8(maBitmapEx
,maBitmapEx
))
72 mbIsAlpha
= maBitmapEx
.IsAlpha();
73 // #121194# Todo: use alpha channel, too (for 3d)
74 maBitmap
= maBitmapEx
.GetBitmap();
78 maTransparence
= rBitmapEx
.GetAlphaMask().GetBitmap();
79 mpReadTransparence
= maTransparence
;
82 if (!maBitmap
.IsEmpty())
83 mpReadBitmap
= maBitmap
;
84 SAL_WARN_IF(!mpReadBitmap
, "drawinglayer", "GeoTexSvxBitmapEx: Got no read access to Bitmap");
87 mfMulX
= static_cast<double>(mpReadBitmap
->Width()) / maSize
.getX();
88 mfMulY
= static_cast<double>(mpReadBitmap
->Height()) / maSize
.getY();
91 if(maSize
.getX() <= 1.0)
96 if(maSize
.getY() <= 1.0)
102 GeoTexSvxBitmapEx::~GeoTexSvxBitmapEx()
106 sal_uInt8
GeoTexSvxBitmapEx::impGetAlpha(sal_Int32 rX
, sal_Int32 rY
) const
110 OSL_ENSURE(mpReadTransparence
, "OOps, transparence type Bitmap, but no read access created in the constructor (?)");
111 const BitmapColor
aBitmapColor(mpReadTransparence
->GetPixel(rY
, rX
));
112 return aBitmapColor
.GetIndex();
117 bool GeoTexSvxBitmapEx::impIsValid(const basegfx::B2DPoint
& rUV
, sal_Int32
& rX
, sal_Int32
& rY
) const
121 rX
= static_cast<sal_Int32
>((rUV
.getX() - maTopLeft
.getX()) * mfMulX
);
123 if(rX
>= 0 && rX
< mpReadBitmap
->Width())
125 rY
= static_cast<sal_Int32
>((rUV
.getY() - maTopLeft
.getY()) * mfMulY
);
127 return (rY
>= 0 && rY
< mpReadBitmap
->Height());
134 void GeoTexSvxBitmapEx::modifyBColor(const basegfx::B2DPoint
& rUV
, basegfx::BColor
& rBColor
, double& rfOpacity
) const
138 if(impIsValid(rUV
, nX
, nY
))
140 const double fConvertColor(1.0 / 255.0);
141 const BitmapColor
aBMCol(mpReadBitmap
->GetColor(nY
, nX
));
142 const basegfx::BColor
aBSource(
143 static_cast<double>(aBMCol
.GetRed()) * fConvertColor
,
144 static_cast<double>(aBMCol
.GetGreen()) * fConvertColor
,
145 static_cast<double>(aBMCol
.GetBlue()) * fConvertColor
);
151 // when we have alpha, make use of it
152 const sal_uInt8
aAlpha(impGetAlpha(nX
, nY
));
154 rfOpacity
= (static_cast<double>(aAlpha
) * (1.0 / 255.0));
167 void GeoTexSvxBitmapEx::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
171 if(impIsValid(rUV
, nX
, nY
))
175 // this texture has an alpha part, use it
176 const sal_uInt8
aAlpha(impGetAlpha(nX
, nY
));
177 const double fNewOpacity(static_cast<double>(aAlpha
) * (1.0 / 255.0));
179 rfOpacity
= 1.0 - ((1.0 - fNewOpacity
) * (1.0 - rfOpacity
));
183 // this texture is a color bitmap used as transparence map
184 const BitmapColor
aBMCol(mpReadBitmap
->GetColor(nY
, nX
));
185 const Color
aColor(aBMCol
.GetRed(), aBMCol
.GetGreen(), aBMCol
.GetBlue());
187 rfOpacity
= (static_cast<double>(0xff - aColor
.GetLuminance()) * (1.0 / 255.0));
197 basegfx::B2DPoint
GeoTexSvxBitmapExTiled::impGetCorrected(const basegfx::B2DPoint
& rUV
) const
199 double fX(rUV
.getX() - maTopLeft
.getX());
200 double fY(rUV
.getY() - maTopLeft
.getY());
204 const sal_Int32
nCol(static_cast< sal_Int32
>((fY
< 0.0 ? maSize
.getY() -fY
: fY
) / maSize
.getY()));
208 fX
+= mfOffsetX
* maSize
.getX();
211 else if(mbUseOffsetY
)
213 const sal_Int32
nRow(static_cast< sal_Int32
>((fX
< 0.0 ? maSize
.getX() -fX
: fX
) / maSize
.getX()));
217 fY
+= mfOffsetY
* maSize
.getY();
221 fX
= fmod(fX
, maSize
.getX());
222 fY
= fmod(fY
, maSize
.getY());
234 return basegfx::B2DPoint(fX
+ maTopLeft
.getX(), fY
+ maTopLeft
.getY());
237 GeoTexSvxBitmapExTiled::GeoTexSvxBitmapExTiled(
238 const BitmapEx
& rBitmapEx
,
239 const basegfx::B2DRange
& rRange
,
242 : GeoTexSvxBitmapEx(rBitmapEx
, rRange
),
243 mfOffsetX(std::clamp(fOffsetX
, 0.0, 1.0)),
244 mfOffsetY(std::clamp(fOffsetY
, 0.0, 1.0)),
245 mbUseOffsetX(!basegfx::fTools::equalZero(mfOffsetX
)),
246 mbUseOffsetY(!mbUseOffsetX
&& !basegfx::fTools::equalZero(mfOffsetY
))
250 void GeoTexSvxBitmapExTiled::modifyBColor(const basegfx::B2DPoint
& rUV
, basegfx::BColor
& rBColor
, double& rfOpacity
) const
254 GeoTexSvxBitmapEx::modifyBColor(impGetCorrected(rUV
), rBColor
, rfOpacity
);
258 void GeoTexSvxBitmapExTiled::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
262 GeoTexSvxBitmapEx::modifyOpacity(impGetCorrected(rUV
), rfOpacity
);
267 GeoTexSvxMultiHatch::GeoTexSvxMultiHatch(
268 const primitive3d::HatchTexturePrimitive3D
& rPrimitive
,
269 double fLogicPixelSize
)
270 : mfLogicPixelSize(fLogicPixelSize
)
272 const attribute::FillHatchAttribute
& rHatch(rPrimitive
.getHatch());
273 const basegfx::B2DRange
aOutlineRange(0.0, 0.0, rPrimitive
.getTextureSize().getX(), rPrimitive
.getTextureSize().getY());
274 const double fAngleA(rHatch
.getAngle());
275 maColor
= rHatch
.getColor();
276 mbFillBackground
= rHatch
.isFillBackground();
277 mp0
.reset( new GeoTexSvxHatch(
280 rHatch
.getDistance(),
283 if(attribute::HatchStyle::Double
== rHatch
.getStyle() || attribute::HatchStyle::Triple
== rHatch
.getStyle())
285 mp1
.reset( new GeoTexSvxHatch(
288 rHatch
.getDistance(),
292 if(attribute::HatchStyle::Triple
== rHatch
.getStyle())
294 mp2
.reset( new GeoTexSvxHatch(
297 rHatch
.getDistance(),
302 GeoTexSvxMultiHatch::~GeoTexSvxMultiHatch()
306 bool GeoTexSvxMultiHatch::impIsOnHatch(const basegfx::B2DPoint
& rUV
) const
308 if(mp0
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
313 if(mp1
&& mp1
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
318 if(mp2
&& mp2
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
326 void GeoTexSvxMultiHatch::modifyBColor(const basegfx::B2DPoint
& rUV
, basegfx::BColor
& rBColor
, double& rfOpacity
) const
328 if(impIsOnHatch(rUV
))
332 else if(!mbFillBackground
)
338 void GeoTexSvxMultiHatch::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
340 if(mbFillBackground
|| impIsOnHatch(rUV
))
350 } // end of namespace
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */