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/geometry/viewinformation2d.hxx>
21 #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
22 #include <basegfx/matrix/b2dhommatrixtools.hxx>
23 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
24 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
25 #include <drawinglayer/processor2d/processor2dtools.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/virdev.hxx>
28 #include <com/sun/star/geometry/RealRectangle2D.hpp>
29 #include <comphelper/diagnose_ex.hxx>
31 #include <drawinglayer/converters.hxx>
34 #include <tools/stream.hxx>
35 // #include <vcl/filter/PngImageWriter.hxx>
36 #include <vcl/dibtools.hxx>
39 // #include <vcl/BitmapReadAccess.hxx>
43 bool implPrepareConversion(drawinglayer::primitive2d::Primitive2DContainer
& rSequence
,
44 sal_uInt32
& rnDiscreteWidth
, sal_uInt32
& rnDiscreteHeight
,
45 const sal_uInt32 nMaxSquarePixels
)
47 if (rSequence
.empty())
50 if (rnDiscreteWidth
<= 0 || rnDiscreteHeight
<= 0)
53 const sal_uInt32
nViewVisibleArea(rnDiscreteWidth
* rnDiscreteHeight
);
55 if (nViewVisibleArea
> nMaxSquarePixels
)
59 = sqrt(static_cast<double>(nMaxSquarePixels
) / static_cast<double>(nViewVisibleArea
));
60 rnDiscreteWidth
= basegfx::fround(static_cast<double>(rnDiscreteWidth
) * fReduceFactor
);
61 rnDiscreteHeight
= basegfx::fround(static_cast<double>(rnDiscreteHeight
) * fReduceFactor
);
63 const drawinglayer::primitive2d::Primitive2DReference
aEmbed(
64 new drawinglayer::primitive2d::TransformPrimitive2D(
65 basegfx::utils::createScaleB2DHomMatrix(fReduceFactor
, fReduceFactor
),
66 std::move(rSequence
)));
68 rSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ aEmbed
};
74 AlphaMask
implcreateAlphaMask(drawinglayer::primitive2d::Primitive2DContainer
& rSequence
,
75 const drawinglayer::geometry::ViewInformation2D
& rViewInformation2D
,
76 const Size
& rSizePixel
, bool bUseLuminance
)
78 ScopedVclPtrInstance
<VirtualDevice
> pContent
;
81 if (!pContent
->SetOutputSizePixel(rSizePixel
, false))
83 SAL_WARN("vcl", "Cannot set VirtualDevice to size : " << rSizePixel
.Width() << "x"
84 << rSizePixel
.Height());
88 // create pixel processor, also already takes care of AAing and
89 // checking the getOptionsDrawinglayer().IsAntiAliasing() switch. If
90 // not wanted, change after this call as needed
91 std::unique_ptr
<drawinglayer::processor2d::BaseProcessor2D
> pContentProcessor
92 = drawinglayer::processor2d::createPixelProcessor2DFromOutputDevice(*pContent
,
95 // prepare for mask creation
96 pContent
->SetMapMode(MapMode(MapUnit::MapPixel
));
98 // set alpha to all white (fully transparent)
101 basegfx::BColorModifierSharedPtr aBColorModifier
;
105 // new mode: bUseLuminance allows simple creation of alpha channels
106 // for any content (e.g. gradients)
107 aBColorModifier
= std::make_shared
<basegfx::BColorModifier_luminance_to_alpha
>();
111 // Embed primitives to paint them black
113 = std::make_shared
<basegfx::BColorModifier_replace
>(basegfx::BColor(0.0, 0.0, 0.0));
115 // embed primitives to paint them black
116 const drawinglayer::primitive2d::Primitive2DReference
xRef(
117 new drawinglayer::primitive2d::ModifiedColorPrimitive2D(std::move(rSequence
),
119 const drawinglayer::primitive2d::Primitive2DContainer xSeq
{ xRef
};
122 pContentProcessor
->process(xSeq
);
123 pContentProcessor
.reset();
125 // get alpha channel from vdev
126 pContent
->EnableMapMode(false);
127 const Point aEmptyPoint
;
128 return AlphaMask(pContent
->GetBitmap(aEmptyPoint
, rSizePixel
));
132 namespace drawinglayer
134 AlphaMask
createAlphaMask(drawinglayer::primitive2d::Primitive2DContainer
&& rSeq
,
135 const geometry::ViewInformation2D
& rViewInformation2D
,
136 sal_uInt32 nDiscreteWidth
, sal_uInt32 nDiscreteHeight
,
137 sal_uInt32 nMaxSquarePixels
, bool bUseLuminance
)
139 drawinglayer::primitive2d::Primitive2DContainer
aSequence(std::move(rSeq
));
141 if (!implPrepareConversion(aSequence
, nDiscreteWidth
, nDiscreteHeight
, nMaxSquarePixels
))
146 const Size
aSizePixel(nDiscreteWidth
, nDiscreteHeight
);
148 return implcreateAlphaMask(aSequence
, rViewInformation2D
, aSizePixel
, bUseLuminance
);
151 BitmapEx
convertToBitmapEx(drawinglayer::primitive2d::Primitive2DContainer
&& rSeq
,
152 const geometry::ViewInformation2D
& rViewInformation2D
,
153 sal_uInt32 nDiscreteWidth
, sal_uInt32 nDiscreteHeight
,
154 sal_uInt32 nMaxSquarePixels
)
156 drawinglayer::primitive2d::Primitive2DContainer
aSequence(std::move(rSeq
));
158 if (!implPrepareConversion(aSequence
, nDiscreteWidth
, nDiscreteHeight
, nMaxSquarePixels
))
163 const Point aEmptyPoint
;
164 const Size
aSizePixel(nDiscreteWidth
, nDiscreteHeight
);
166 // Create target VirtualDevice. Go back to using a simple RGB
167 // target version (compared with former version, see history).
168 // Reasons are manyfold:
169 // - Avoid the RGBA mode for VirtualDevice (two VDevs)
170 // - It's not suggested to be used outside presentation engine
171 // - It only works *by chance* with VCLPrimitiveRenderer
172 // - Usage of two-VDev alpha-VDev avoided alpha blending against
173 // COL_WHITE in the 1st layer of targets (not in buffers below)
174 // but is kind of a 'hack' doing so
175 // - Other renderers (system-dependent PrimitiveRenderers, other
176 // than the VCL-based ones) will probably not support splitted
177 // VDevs for content/alpha, so require a method that works with
178 // RGB targeting (for now)
179 // - Less resource usage, better speed (no 2 VDevs, no merge of
181 // As long as not all our mechanisms are changed to RGBA completely,
182 // mixing these is just too dangerous and expensive and may to wrong
183 // or deliver bad quality results.
184 // Nonetheless we need a RGBA result here. Luckily we are able to
185 // create a complete and valid AlphaChannel using 'createAlphaMask'
187 // When we know the content (RGB result from renderer), alpha
188 // (result from createAlphaMask) and the start condition (content
189 // rendered against COL_WHITE), it is possible to calculate back
190 // the content, quasi 'remove' that initial blending against
192 // That is what the helper Bitmap::RemoveBlendedStartColor does.
193 // Luckily we only need it for this 'convertToBitmapEx', not in
194 // any other rendering. It could be further optimized, too.
195 // This gives good results, it is in principle comparable with
196 // the results using pre-multiplied alpha tooling, also reducing
197 // the range of values where high alpha values are used.
198 ScopedVclPtrInstance
<VirtualDevice
> pContent(*Application::GetDefaultDevice());
201 if (!pContent
->SetOutputSizePixel(aSizePixel
, false))
203 SAL_WARN("vcl", "Cannot set VirtualDevice to size : " << aSizePixel
.Width() << "x"
204 << aSizePixel
.Height());
208 // We map to pixel, use that MapMode. Init by erasing.
209 pContent
->SetMapMode(MapMode(MapUnit::MapPixel
));
212 // create pixel processor, also already takes care of AAing and
213 // checking the getOptionsDrawinglayer().IsAntiAliasing() switch. If
214 // not wanted, change after this call as needed
215 std::unique_ptr
<processor2d::BaseProcessor2D
> pContentProcessor
216 = processor2d::createPixelProcessor2DFromOutputDevice(*pContent
, rViewInformation2D
);
219 pContentProcessor
->process(aSequence
);
221 // create final BitmapEx result (content)
222 Bitmap
aRetval(pContent
->GetBitmap(aEmptyPoint
, aSizePixel
));
225 static bool bDoSaveForVisualControl(false); // loplugin:constvars:ignore
226 if (bDoSaveForVisualControl
)
228 // VCL_DUMP_BMP_PATH should be like C:/path/ or ~/path/
229 static const OUString
sDumpPath(
230 OUString::createFromAscii(std::getenv("VCL_DUMP_BMP_PATH")));
231 if (!sDumpPath
.isEmpty())
233 SvFileStream
aNew(sDumpPath
+ "test_content.bmp",
234 StreamMode::WRITE
| StreamMode::TRUNC
);
235 WriteDIB(aRetval
, aNew
, false, true);
240 // Create the AlphaMask using a method that does this always correct (also used
241 // now in GlowPrimitive2D and ShadowPrimitive2D which both only need the
242 // AlphaMask to do their job, so speeding that up, too).
243 AlphaMask
aAlpha(implcreateAlphaMask(aSequence
, rViewInformation2D
, aSizePixel
, false));
246 if (bDoSaveForVisualControl
)
248 // VCL_DUMP_BMP_PATH should be like C:/path/ or ~/path/
249 static const OUString
sDumpPath(
250 OUString::createFromAscii(std::getenv("VCL_DUMP_BMP_PATH")));
251 if (!sDumpPath
.isEmpty())
253 SvFileStream
aNew(sDumpPath
+ "test_alpha.bmp", StreamMode::WRITE
| StreamMode::TRUNC
);
254 WriteDIB(aAlpha
.GetBitmap(), aNew
, false, true);
259 if (aAlpha
.hasAlpha())
261 // Need to correct content using known alpha to get to background-free
262 // RGBA result, usable e.g. in PNG export(s) or convert-to-bitmap
263 aRetval
.RemoveBlendedStartColor(COL_WHITE
, aAlpha
);
266 // return combined result
267 return BitmapEx(aRetval
, aAlpha
);
270 BitmapEx
convertPrimitive2DContainerToBitmapEx(primitive2d::Primitive2DContainer
&& rSequence
,
271 const basegfx::B2DRange
& rTargetRange
,
272 sal_uInt32 nMaximumQuadraticPixels
,
273 const o3tl::Length eTargetUnit
,
274 const std::optional
<Size
>& rTargetDPI
)
276 if (rSequence
.empty())
281 css::geometry::RealRectangle2D aRealRect
;
282 aRealRect
.X1
= rTargetRange
.getMinX();
283 aRealRect
.Y1
= rTargetRange
.getMinY();
284 aRealRect
.X2
= rTargetRange
.getMaxX();
285 aRealRect
.Y2
= rTargetRange
.getMaxY();
289 Application::GetDefaultDevice()->LogicToPixel(Size(1, 1), MapMode(MapUnit::MapInch
)));
290 if (rTargetDPI
.has_value())
295 ::sal_uInt32 DPI_X
= aDPI
.getWidth();
296 ::sal_uInt32 DPI_Y
= aDPI
.getHeight();
297 const basegfx::B2DRange
aRange(aRealRect
.X1
, aRealRect
.Y1
, aRealRect
.X2
, aRealRect
.Y2
);
298 const double fWidth(aRange
.getWidth());
299 const double fHeight(aRange
.getHeight());
301 if (!(basegfx::fTools::more(fWidth
, 0.0) && basegfx::fTools::more(fHeight
, 0.0)))
314 if (0 == nMaximumQuadraticPixels
)
316 nMaximumQuadraticPixels
= 500000;
319 const auto aViewInformation2D
= geometry::createViewInformation2D({});
320 const sal_uInt32
nDiscreteWidth(
321 basegfx::fround(o3tl::convert(fWidth
, eTargetUnit
, o3tl::Length::in
) * DPI_X
));
322 const sal_uInt32
nDiscreteHeight(
323 basegfx::fround(o3tl::convert(fHeight
, eTargetUnit
, o3tl::Length::in
) * DPI_Y
));
325 basegfx::B2DHomMatrix
aEmbedding(
326 basegfx::utils::createTranslateB2DHomMatrix(-aRange
.getMinX(), -aRange
.getMinY()));
328 aEmbedding
.scale(nDiscreteWidth
/ fWidth
, nDiscreteHeight
/ fHeight
);
330 const primitive2d::Primitive2DReference
xEmbedRef(
331 new primitive2d::TransformPrimitive2D(aEmbedding
, std::move(rSequence
)));
332 primitive2d::Primitive2DContainer xEmbedSeq
{ xEmbedRef
};
334 BitmapEx
aBitmapEx(convertToBitmapEx(std::move(xEmbedSeq
), aViewInformation2D
,
335 nDiscreteWidth
, nDiscreteHeight
,
336 nMaximumQuadraticPixels
));
338 if (aBitmapEx
.IsEmpty())
340 aBitmapEx
.SetPrefMapMode(MapMode(MapUnit::Map100thMM
));
341 aBitmapEx
.SetPrefSize(Size(basegfx::fround(fWidth
), basegfx::fround(fHeight
)));
345 catch (const css::uno::Exception
&)
347 TOOLS_WARN_EXCEPTION("vcl", "Got no graphic::XPrimitive2DRenderer!");
349 catch (const std::exception
& e
)
351 SAL_WARN("vcl", "Got no graphic::XPrimitive2DRenderer! : " << e
.what());
356 } // end of namespace drawinglayer
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */