bump product version to 6.4.0.3
[LibreOffice.git] / drawinglayer / source / tools / converters.cxx
blob010f71810c304933c1a1d8df42673752ec6e5366
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
30 #ifdef DBG_UTIL
31 #include <tools/stream.hxx>
32 #include <vcl/pngwrite.hxx>
33 #endif
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)
45 BitmapEx aRetval;
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)
56 // reduce render size
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),
64 rSeq));
66 aSequence = drawinglayer::primitive2d::Primitive2DContainer { aEmbed };
69 const Point aEmptyPoint;
70 const Size aSizePixel(nDiscreteWidth, nDiscreteHeight);
71 ScopedVclPtrInstance< VirtualDevice > pContent;
73 // prepare vdev
74 pContent->SetOutputSizePixel(aSizePixel, false);
75 pContent->SetMapMode(aMapModePixel);
77 // set to all white
78 pContent->SetBackground(Wallpaper(COL_WHITE));
79 pContent->Erase();
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(
85 *pContent,
86 rViewInformation2D);
88 if(pContentProcessor)
90 #ifdef DBG_UTIL
91 static bool bDoSaveForVisualControl(false); // loplugin:constvars:ignore
92 #endif
93 // render content
94 pContentProcessor->process(aSequence);
96 // get content
97 pContent->EnableMapMode(false);
98 const Bitmap aContent(pContent->GetBitmap(aEmptyPoint, aSizePixel));
100 #ifdef DBG_UTIL
101 if(bDoSaveForVisualControl)
103 SvFileStream aNew(
104 #ifdef _WIN32
105 "c:\\test_content.png"
106 #else
107 "~/test_content.png"
108 #endif
109 , StreamMode::WRITE|StreamMode::TRUNC);
110 BitmapEx aContentEx(aContent);
111 vcl::PNGWriter aPNGWriter(aContentEx);
112 aPNGWriter.Write(aNew);
114 #endif
115 // prepare for mask creation
116 pContent->SetMapMode(aMapModePixel);
118 // set alpha to all white (fully transparent)
119 pContent->Erase();
121 // embed primitives to paint them black
122 const primitive2d::Primitive2DReference xRef(
123 new primitive2d::ModifiedColorPrimitive2D(
124 aSequence,
125 basegfx::BColorModifierSharedPtr(
126 new basegfx::BColorModifier_replace(
127 basegfx::BColor(0.0, 0.0, 0.0)))));
128 const primitive2d::Primitive2DContainer xSeq { xRef };
130 // render
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));
137 #ifdef DBG_UTIL
138 if(bDoSaveForVisualControl)
140 SvFileStream aNew(
141 #ifdef _WIN32
142 "c:\\test_alpha.png"
143 #else
144 "~/test_alpha.png"
145 #endif
146 , StreamMode::WRITE|StreamMode::TRUNC);
147 BitmapEx aAlphaEx(aAlpha);
148 vcl::PNGWriter aPNGWriter(aAlphaEx);
149 aPNGWriter.Write(aNew);
151 #endif
153 // create BitmapEx result
154 aRetval = BitmapEx(aContent, AlphaMask(aAlpha));
155 #ifdef DBG_UTIL
156 if(bDoSaveForVisualControl)
158 SvFileStream aNew(
159 #ifdef _WIN32
160 "c:\\test_combined.png"
161 #else
162 "~/test_combined.png"
163 #endif
164 , StreamMode::WRITE|StreamMode::TRUNC);
165 vcl::PNGWriter aPNGWriter(aRetval);
166 aPNGWriter.Write(aNew);
168 #endif
172 return aRetval;
175 } // end of namespace drawinglayer
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */