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/virdev.hxx>
28 #include <converters.hxx>
31 #include <tools/stream.hxx>
32 #include <vcl/pngwrite.hxx>
35 namespace drawinglayer
38 BitmapEx
convertToBitmapEx(
39 const drawinglayer::primitive2d::Primitive2DContainer
& rSeq
,
40 const geometry::ViewInformation2D
& rViewInformation2D
,
41 sal_uInt32 nDiscreteWidth
,
42 sal_uInt32 nDiscreteHeight
,
43 sal_uInt32 nMaxQuadratPixels
)
47 if(!rSeq
.empty() && nDiscreteWidth
&& nDiscreteHeight
)
49 // get destination size in pixels
50 const MapMode
aMapModePixel(MapUnit::MapPixel
);
51 const sal_uInt32
nViewVisibleArea(nDiscreteWidth
* nDiscreteHeight
);
52 drawinglayer::primitive2d::Primitive2DContainer
aSequence(rSeq
);
54 if(nViewVisibleArea
> nMaxQuadratPixels
)
57 double fReduceFactor
= sqrt(static_cast<double>(nMaxQuadratPixels
) / static_cast<double>(nViewVisibleArea
));
58 nDiscreteWidth
= basegfx::fround(static_cast<double>(nDiscreteWidth
) * fReduceFactor
);
59 nDiscreteHeight
= basegfx::fround(static_cast<double>(nDiscreteHeight
) * fReduceFactor
);
61 const drawinglayer::primitive2d::Primitive2DReference
aEmbed(
62 new drawinglayer::primitive2d::TransformPrimitive2D(
63 basegfx::utils::createScaleB2DHomMatrix(fReduceFactor
, fReduceFactor
),
66 aSequence
= drawinglayer::primitive2d::Primitive2DContainer
{ aEmbed
};
69 const Point aEmptyPoint
;
70 const Size
aSizePixel(nDiscreteWidth
, nDiscreteHeight
);
71 ScopedVclPtrInstance
< VirtualDevice
> pContent
;
74 pContent
->SetOutputSizePixel(aSizePixel
, false);
75 pContent
->SetMapMode(aMapModePixel
);
78 pContent
->SetBackground(Wallpaper(COL_WHITE
));
81 // create pixel processor, also already takes care of AAing and
82 // checking the getOptionsDrawinglayer().IsAntiAliasing() switch. If
83 // not wanted, change after this call as needed
84 std::unique_ptr
<processor2d::BaseProcessor2D
> pContentProcessor
= processor2d::createPixelProcessor2DFromOutputDevice(
91 static bool bDoSaveForVisualControl(false); // loplugin:constvars:ignore
94 pContentProcessor
->process(aSequence
);
97 pContent
->EnableMapMode(false);
98 const Bitmap
aContent(pContent
->GetBitmap(aEmptyPoint
, aSizePixel
));
101 if(bDoSaveForVisualControl
)
105 "c:\\test_content.png"
109 , StreamMode::WRITE
|StreamMode::TRUNC
);
110 BitmapEx
aContentEx(aContent
);
111 vcl::PNGWriter
aPNGWriter(aContentEx
);
112 aPNGWriter
.Write(aNew
);
115 // prepare for mask creation
116 pContent
->SetMapMode(aMapModePixel
);
118 // set alpha to all white (fully transparent)
121 // embed primitives to paint them black
122 const primitive2d::Primitive2DReference
xRef(
123 new primitive2d::ModifiedColorPrimitive2D(
125 basegfx::BColorModifierSharedPtr(
126 new basegfx::BColorModifier_replace(
127 basegfx::BColor(0.0, 0.0, 0.0)))));
128 const primitive2d::Primitive2DContainer xSeq
{ xRef
};
131 pContentProcessor
->process(xSeq
);
132 pContentProcessor
.reset();
134 // get alpha channel from vdev
135 pContent
->EnableMapMode(false);
136 const Bitmap
aAlpha(pContent
->GetBitmap(aEmptyPoint
, aSizePixel
));
138 if(bDoSaveForVisualControl
)
146 , StreamMode::WRITE
|StreamMode::TRUNC
);
147 BitmapEx
aAlphaEx(aAlpha
);
148 vcl::PNGWriter
aPNGWriter(aAlphaEx
);
149 aPNGWriter
.Write(aNew
);
153 // create BitmapEx result
154 aRetval
= BitmapEx(aContent
, AlphaMask(aAlpha
));
156 if(bDoSaveForVisualControl
)
160 "c:\\test_combined.png"
162 "~/test_combined.png"
164 , StreamMode::WRITE
|StreamMode::TRUNC
);
165 vcl::PNGWriter
aPNGWriter(aRetval
);
166 aPNGWriter
.Write(aNew
);
175 } // end of namespace drawinglayer
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */