Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <drawinglayer/tools/converters.hxx>
21 : #include <drawinglayer/geometry/viewinformation2d.hxx>
22 : #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
23 : #include <basegfx/matrix/b2dhommatrixtools.hxx>
24 : #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
25 : #include <drawinglayer/processor2d/processor2dtools.hxx>
26 : #include <vcl/virdev.hxx>
27 :
28 : #ifdef DBG_UTIL
29 : #include <tools/stream.hxx>
30 : #include <vcl/pngwrite.hxx>
31 : #endif
32 :
33 :
34 :
35 : namespace drawinglayer
36 : {
37 : namespace tools
38 : {
39 294 : BitmapEx convertToBitmapEx(
40 : const drawinglayer::primitive2d::Primitive2DSequence& rSeq,
41 : const geometry::ViewInformation2D& rViewInformation2D,
42 : sal_uInt32 nDiscreteWidth,
43 : sal_uInt32 nDiscreteHeight,
44 : sal_uInt32 nMaxQuadratPixels)
45 : {
46 294 : BitmapEx aRetval;
47 : #ifdef DBG_UTIL
48 : static bool bDoSaveForVisualControl(false);
49 : #endif
50 :
51 294 : if(rSeq.hasElements() && nDiscreteWidth && nDiscreteHeight)
52 : {
53 : // get destination size in pixels
54 294 : const MapMode aMapModePixel(MAP_PIXEL);
55 294 : const sal_uInt32 nViewVisibleArea(nDiscreteWidth * nDiscreteHeight);
56 588 : drawinglayer::primitive2d::Primitive2DSequence aSequence(rSeq);
57 :
58 294 : if(nViewVisibleArea > nMaxQuadratPixels)
59 : {
60 : // reduce render size
61 0 : double fReduceFactor = sqrt((double)nMaxQuadratPixels / (double)nViewVisibleArea);
62 0 : nDiscreteWidth = basegfx::fround((double)nDiscreteWidth * fReduceFactor);
63 0 : nDiscreteHeight = basegfx::fround((double)nDiscreteHeight * fReduceFactor);
64 :
65 : const drawinglayer::primitive2d::Primitive2DReference aEmbed(
66 : new drawinglayer::primitive2d::TransformPrimitive2D(
67 : basegfx::tools::createScaleB2DHomMatrix(fReduceFactor, fReduceFactor),
68 0 : rSeq));
69 :
70 0 : aSequence = drawinglayer::primitive2d::Primitive2DSequence(&aEmbed, 1);
71 : }
72 :
73 294 : const Point aEmptyPoint;
74 294 : const Size aSizePixel(nDiscreteWidth, nDiscreteHeight);
75 588 : geometry::ViewInformation2D aViewInformation2D(rViewInformation2D);
76 588 : ScopedVclPtrInstance< VirtualDevice > maContent;
77 :
78 : // prepare vdev
79 294 : maContent->SetOutputSizePixel(aSizePixel, false);
80 294 : maContent->SetMapMode(aMapModePixel);
81 :
82 : // set to all white
83 294 : maContent->SetBackground(Wallpaper(Color(COL_WHITE)));
84 294 : maContent->Erase();
85 :
86 : // create pixel processor, also already takes care of AAing and
87 : // checking the getOptionsDrawinglayer().IsAntiAliasing() switch. If
88 : // not wanted, change after this call as needed
89 : processor2d::BaseProcessor2D* pContentProcessor = processor2d::createPixelProcessor2DFromOutputDevice(
90 294 : *maContent.get(),
91 294 : aViewInformation2D);
92 :
93 294 : if(pContentProcessor)
94 : {
95 : // render content
96 294 : pContentProcessor->process(aSequence);
97 :
98 : // get content
99 294 : maContent->EnableMapMode(false);
100 294 : const Bitmap aContent(maContent->GetBitmap(aEmptyPoint, aSizePixel));
101 :
102 : #ifdef DBG_UTIL
103 : if(bDoSaveForVisualControl)
104 : {
105 : SvFileStream aNew(OUString("c:\\test_content.png"), StreamMode::WRITE|StreamMode::TRUNC);
106 : vcl::PNGWriter aPNGWriter(aContent);
107 : aPNGWriter.Write(aNew);
108 : }
109 : #endif
110 : // prepare for mask creation
111 294 : maContent->SetMapMode(aMapModePixel);
112 :
113 : // set alpha to all white (fully transparent)
114 294 : maContent->Erase();
115 :
116 : // embed primitives to paint them black
117 : const primitive2d::Primitive2DReference xRef(
118 : new primitive2d::ModifiedColorPrimitive2D(
119 : aSequence,
120 : basegfx::BColorModifierSharedPtr(
121 : new basegfx::BColorModifier_replace(
122 588 : basegfx::BColor(0.0, 0.0, 0.0)))));
123 588 : const primitive2d::Primitive2DSequence xSeq(&xRef, 1);
124 :
125 : // render
126 294 : pContentProcessor->process(xSeq);
127 294 : delete pContentProcessor;
128 :
129 : // get alpha cahannel from vdev
130 294 : maContent->EnableMapMode(false);
131 588 : const Bitmap aAlpha(maContent->GetBitmap(aEmptyPoint, aSizePixel));
132 : #ifdef DBG_UTIL
133 : if(bDoSaveForVisualControl)
134 : {
135 : SvFileStream aNew(OUString("c:\\test_alpha.png"), StreamMode::WRITE|StreamMode::TRUNC);
136 : vcl::PNGWriter aPNGWriter(aAlpha);
137 : aPNGWriter.Write(aNew);
138 : }
139 : #endif
140 :
141 : // create BitmapEx result
142 588 : aRetval = BitmapEx(aContent, AlphaMask(aAlpha));
143 : #ifdef DBG_UTIL
144 : if(bDoSaveForVisualControl)
145 : {
146 : SvFileStream aNew(OUString("c:\\test_combined.png"), StreamMode::WRITE|StreamMode::TRUNC);
147 : vcl::PNGWriter aPNGWriter(aRetval);
148 : aPNGWriter.Write(aNew);
149 : }
150 : #endif
151 294 : }
152 : }
153 :
154 294 : return aRetval;
155 : }
156 :
157 : } // end of namespace tools
158 : } // end of namespace drawinglayer
159 :
160 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|