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/primitive2d/modifiedcolorprimitive2d.hxx>
28 #include <basegfx/matrix/b2dhommatrixtools.hxx>
29 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
30 #include <drawinglayer/processor2d/processor2dtools.hxx>
31 #include <vcl/virdev.hxx>
32 
33 #ifdef DBG_UTIL
34 #include <tools/stream.hxx>
35 #include <vcl/pngwrite.hxx>
36 #endif
37 
38 //////////////////////////////////////////////////////////////////////////////
39 
40 namespace drawinglayer
41 {
42     namespace tools
43     {
44         BitmapEx convertToBitmapEx(
45             const drawinglayer::primitive2d::Primitive2DSequence& rSeq,
46             const geometry::ViewInformation2D& rViewInformation2D,
47             sal_uInt32 nDiscreteWidth,
48             sal_uInt32 nDiscreteHeight,
49             sal_uInt32 nMaxQuadratPixels)
50         {
51             BitmapEx aRetval;
52 
53             if(rSeq.hasElements() && nDiscreteWidth && nDiscreteHeight)
54             {
55                 // get destination size in pixels
56                 const MapMode aMapModePixel(MAP_PIXEL);
57                 const sal_uInt32 nViewVisibleArea(nDiscreteWidth * nDiscreteHeight);
58                 double fReduceFactor(1.0);
59                 drawinglayer::primitive2d::Primitive2DSequence aSequence(rSeq);
60 
61                 if(nViewVisibleArea > nMaxQuadratPixels)
62                 {
63                     // reduce render size
64                     fReduceFactor = sqrt((double)nMaxQuadratPixels / (double)nViewVisibleArea);
65                     nDiscreteWidth = basegfx::fround((double)nDiscreteWidth * fReduceFactor);
66                     nDiscreteHeight = basegfx::fround((double)nDiscreteHeight * fReduceFactor);
67 
68                     const drawinglayer::primitive2d::Primitive2DReference aEmbed(
69                         new drawinglayer::primitive2d::TransformPrimitive2D(
70                             basegfx::tools::createScaleB2DHomMatrix(fReduceFactor, fReduceFactor),
71                             rSeq));
72 
73                     aSequence = drawinglayer::primitive2d::Primitive2DSequence(&aEmbed, 1);
74                 }
75 
76                 const Point aEmptyPoint;
77                 const Size aSizePixel(nDiscreteWidth, nDiscreteHeight);
78                 geometry::ViewInformation2D aViewInformation2D(rViewInformation2D);
79                 VirtualDevice maContent;
80 
81                 // prepare vdev
82                 maContent.SetOutputSizePixel(aSizePixel, false);
83                 maContent.SetMapMode(aMapModePixel);
84                 maContent.SetAntialiasing(true);
85 
86                 // set to all white
87                 maContent.SetBackground(Wallpaper(Color(COL_WHITE)));
88                 maContent.Erase();
89 
90                 // create pixel processor
91                 processor2d::BaseProcessor2D* pContentProcessor = processor2d::createPixelProcessor2DFromOutputDevice(
92                     maContent,
93                     aViewInformation2D);
94 
95                 if(pContentProcessor)
96                 {
97                     // render content
98                     pContentProcessor->process(aSequence);
99 
100                     // get content
101                     maContent.EnableMapMode(false);
102                     const Bitmap aContent(maContent.GetBitmap(aEmptyPoint, aSizePixel));
103 
104                     // prepare for mask creation
105                     maContent.SetMapMode(aMapModePixel);
106                     maContent.SetAntialiasing(true);
107 
108                     // set alpha to all white (fully transparent)
109                     maContent.Erase();
110 
111                     // embed primitives to paint them black
112                     const primitive2d::Primitive2DReference xRef(
113                         new primitive2d::ModifiedColorPrimitive2D(
114                             aSequence,
115                             basegfx::BColorModifier(
116                                 basegfx::BColor(0.0, 0.0, 0.0),
117                                 0.5,
118                                 basegfx::BCOLORMODIFYMODE_REPLACE)));
119                     const primitive2d::Primitive2DSequence xSeq(&xRef, 1);
120 
121                     // render
122                     pContentProcessor->process(xSeq);
123                     delete pContentProcessor;
124 
125                     // get alpha cahannel from vdev
126                     maContent.EnableMapMode(false);
127                     const AlphaMask aAlphaMask(maContent.GetBitmap(aEmptyPoint, aSizePixel));
128 
129                     // create BitmapEx result
130                     aRetval = BitmapEx(aContent, aAlphaMask);
131                 }
132             }
133 
134 #ifdef DBG_UTIL
135             static bool bDoSaveForVisualControl(false);
136             if(bDoSaveForVisualControl)
137             {
138                 SvFileStream aNew((const String&)String(ByteString( "c:\\test.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
139                 ::vcl::PNGWriter aPNGWriter(aRetval);
140                 aPNGWriter.Write(aNew);
141             }
142 #endif
143 
144             return aRetval;
145         }
146 
147     } // end of namespace tools
148 } // end of namespace drawinglayer
149 
150 //////////////////////////////////////////////////////////////////////////////
151 // eof
152