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 <drawinglayer/texture/texture3d.hxx>
21 #include <vcl/bitmapaccess.hxx>
22 #include <drawinglayer/primitive3d/hatchtextureprimitive3d.hxx>
25 namespace drawinglayer
29 GeoTexSvxMono::GeoTexSvxMono(
30 const basegfx::BColor
& rSingleColor
,
32 : maSingleColor(rSingleColor
),
37 bool GeoTexSvxMono::operator==(const GeoTexSvx
& rGeoTexSvx
) const
39 const GeoTexSvxMono
* pCompare
= dynamic_cast< const GeoTexSvxMono
* >(&rGeoTexSvx
);
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
63 GeoTexSvxBitmapEx::GeoTexSvxBitmapEx(
64 const BitmapEx
& rBitmapEx
,
65 const basegfx::B2DRange
& rRange
)
66 : maBitmapEx(rBitmapEx
),
67 mpReadBitmap(nullptr),
69 mpReadTransparence(nullptr),
70 maTopLeft(rRange
.getMinimum()),
71 maSize(rRange
.getRange()),
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 (!)");
83 if(maBitmapEx
.IsAlpha())
86 maTransparence
= rBitmapEx
.GetAlpha().GetBitmap();
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)
104 if(maSize
.getY() <= 1.0)
110 GeoTexSvxBitmapEx::~GeoTexSvxBitmapEx()
112 delete mpReadTransparence
;
116 sal_uInt8
GeoTexSvxBitmapEx::impGetTransparence(sal_Int32
& rX
, sal_Int32
& rY
) const
118 switch(maBitmapEx
.GetTransparentType())
120 case TRANSPARENT_NONE
:
124 case TRANSPARENT_COLOR
:
126 const BitmapColor
aBitmapColor(mpReadBitmap
->GetColor(rY
, rX
));
128 if(maBitmapEx
.GetTransparentColor() == aBitmapColor
.operator Color())
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
));
142 return aBitmapColor
.GetIndex();
146 if(0x00 != aBitmapColor
.GetIndex())
158 bool GeoTexSvxBitmapEx::impIsValid(const basegfx::B2DPoint
& rUV
, sal_Int32
& rX
, sal_Int32
& rY
) const
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());
175 void GeoTexSvxBitmapEx::modifyBColor(const basegfx::B2DPoint
& rUV
, basegfx::BColor
& rBColor
, double& rfOpacity
) const
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
);
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));
208 void GeoTexSvxBitmapEx::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
212 if(impIsValid(rUV
, nX
, nY
))
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
));
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));
236 } // end of namespace texture
237 } // end of namespace drawinglayer
240 namespace drawinglayer
244 basegfx::B2DPoint
GeoTexSvxBitmapExTiled::impGetCorrected(const basegfx::B2DPoint
& rUV
) const
246 double fX(rUV
.getX() - maTopLeft
.getX());
247 double fY(rUV
.getY() - maTopLeft
.getY());
251 const sal_Int32
nCol(static_cast< sal_Int32
>((fY
< 0.0 ? maSize
.getY() -fY
: fY
) / maSize
.getY()));
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()));
264 fY
+= mfOffsetY
* maSize
.getY();
268 fX
= fmod(fX
, maSize
.getX());
269 fY
= fmod(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
,
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
301 GeoTexSvxBitmapEx::modifyBColor(impGetCorrected(rUV
), rBColor
, rfOpacity
);
305 void GeoTexSvxBitmapExTiled::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
309 GeoTexSvxBitmapEx::modifyOpacity(impGetCorrected(rUV
), rfOpacity
);
312 } // end of namespace texture
313 } // end of namespace drawinglayer
316 namespace drawinglayer
320 GeoTexSvxMultiHatch::GeoTexSvxMultiHatch(
321 const primitive3d::HatchTexturePrimitive3D
& rPrimitive
,
322 double fLogicPixelSize
)
323 : mfLogicPixelSize(fLogicPixelSize
),
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(
336 rHatch
.getDistance(),
339 if(attribute::HatchStyle::Double
== rHatch
.getStyle() || attribute::HatchStyle::Triple
== rHatch
.getStyle())
341 mp1
= new GeoTexSvxHatch(
344 rHatch
.getDistance(),
348 if(attribute::HatchStyle::Triple
== rHatch
.getStyle())
350 mp2
= new GeoTexSvxHatch(
353 rHatch
.getDistance(),
358 GeoTexSvxMultiHatch::~GeoTexSvxMultiHatch()
365 bool GeoTexSvxMultiHatch::impIsOnHatch(const basegfx::B2DPoint
& rUV
) const
367 if(mp0
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
372 if(mp1
&& mp1
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
377 if(mp2
&& mp2
->getDistanceToHatch(rUV
) < mfLogicPixelSize
)
385 void GeoTexSvxMultiHatch::modifyBColor(const basegfx::B2DPoint
& rUV
, basegfx::BColor
& rBColor
, double& rfOpacity
) const
387 if(impIsOnHatch(rUV
))
391 else if(!mbFillBackground
)
397 void GeoTexSvxMultiHatch::modifyOpacity(const basegfx::B2DPoint
& rUV
, double& rfOpacity
) const
399 if(mbFillBackground
|| impIsOnHatch(rUV
))
408 } // end of namespace texture
409 } // end of namespace drawinglayer
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */