1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http:\\www.apache.org\licenses\LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_drawinglayer.hxx"
24 
25 #include <drawinglayer/tools/converters.hxx>
26 #include <drawinglayer/geometry/viewinformation2d.hxx>
27 #include <drawinglayer/processor2d/vclpixelprocessor2d.hxx>
28 #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
29 #include <basegfx/matrix/b2dhommatrixtools.hxx>
30 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
31 #include <vcl/virdev.hxx>
32 
33 #ifdef DBG_UTIL
34 #include <tools/stream.hxx>
35 #endif
36 
37 //////////////////////////////////////////////////////////////////////////////
38 
39 namespace drawinglayer
40 {
41     namespace tools
42     {
43         BitmapEx DRAWINGLAYER_DLLPUBLIC convertToBitmapEx(
44             const drawinglayer::primitive2d::Primitive2DSequence& rSeq,
45             const geometry::ViewInformation2D& rViewInformation2D,
46             sal_uInt32 nDiscreteWidth,
47             sal_uInt32 nDiscreteHeight,
48             sal_uInt32 nMaxQuadratPixels)
49         {
50             BitmapEx aRetval;
51 
52             if(rSeq.hasElements() && nDiscreteWidth && nDiscreteHeight)
53             {
54                 // get destination size in pixels
55                 const MapMode aMapModePixel(MAP_PIXEL);
56                 const sal_uInt32 nViewVisibleArea(nDiscreteWidth * nDiscreteHeight);
57                 double fReduceFactor(1.0);
58                 drawinglayer::primitive2d::Primitive2DSequence aSequence(rSeq);
59 
60                 if(nViewVisibleArea > nMaxQuadratPixels)
61                 {
62                     // reduce render size
63                     fReduceFactor = sqrt((double)nMaxQuadratPixels / (double)nViewVisibleArea);
64                     nDiscreteWidth = basegfx::fround((double)nDiscreteWidth * fReduceFactor);
65                     nDiscreteHeight = basegfx::fround((double)nDiscreteHeight * fReduceFactor);
66 
67                     const drawinglayer::primitive2d::Primitive2DReference aEmbed(
68                         new drawinglayer::primitive2d::TransformPrimitive2D(
69                             basegfx::tools::createScaleB2DHomMatrix(fReduceFactor, fReduceFactor),
70                             rSeq));
71 
72                     aSequence = drawinglayer::primitive2d::Primitive2DSequence(&aEmbed, 1);
73                 }
74 
75                 const Point aEmptyPoint;
76                 const Size aSizePixel(nDiscreteWidth, nDiscreteHeight);
77                 geometry::ViewInformation2D aViewInformation2D(rViewInformation2D);
78                 VirtualDevice maContent;
79 
80                 // prepare vdev
81                 maContent.SetOutputSizePixel(aSizePixel, false);
82                 maContent.SetMapMode(aMapModePixel);
83                 maContent.SetAntialiasing(true);
84 
85                 // set to all white
86                 maContent.SetBackground(Wallpaper(Color(COL_WHITE)));
87                 maContent.Erase();
88 
89                 // create processor
90                 processor2d::VclPixelProcessor2D aContentProcessor(aViewInformation2D, maContent);
91 
92                 // render content
93                 aContentProcessor.process(aSequence);
94 
95                 // get content
96                 maContent.EnableMapMode(false);
97                 const Bitmap aContent(maContent.GetBitmap(aEmptyPoint, aSizePixel));
98 
99                 // prepare for mask creation
100                 maContent.SetMapMode(aMapModePixel);
101                 maContent.SetAntialiasing(true);
102 
103                 // set alpha to all white (fully transparent)
104                 maContent.Erase();
105 
106                 // embed primitives to paint them black
107                 const primitive2d::Primitive2DReference xRef(
108                     new primitive2d::ModifiedColorPrimitive2D(
109                         aSequence,
110                         basegfx::BColorModifier(
111                             basegfx::BColor(0.0, 0.0, 0.0),
112                             0.5,
113                             basegfx::BCOLORMODIFYMODE_REPLACE)));
114                 const primitive2d::Primitive2DSequence xSeq(&xRef, 1);
115 
116                 // render
117                 aContentProcessor.process(xSeq);
118 
119                 // get alpha cahannel from vdev
120                 maContent.EnableMapMode(false);
121                 const AlphaMask aAlphaMask(maContent.GetBitmap(aEmptyPoint, aSizePixel));
122 
123                 // create BitmapEx result
124                 aRetval = BitmapEx(aContent, aAlphaMask);
125             }
126 
127 #ifdef DBG_UTIL
128             static bool bDoSaveForVisualControl(false);
129             if(bDoSaveForVisualControl)
130             {
131                 SvFileStream aNew((const String&)String(ByteString( "c:\\test.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
132                 aNew << aRetval;
133             }
134 #endif
135 
136             return aRetval;
137         }
138 
139     } // end of namespace tools
140 } // end of namespace drawinglayer
141 
142 //////////////////////////////////////////////////////////////////////////////
143 // eof
144