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     {
convertToBitmapEx(const drawinglayer::primitive2d::Primitive2DSequence & rSeq,const geometry::ViewInformation2D & rViewInformation2D,sal_uInt32 nDiscreteWidth,sal_uInt32 nDiscreteHeight,sal_uInt32 nMaxQuadratPixels)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 #ifdef DBG_UTIL
53             static bool bDoSaveForVisualControl(false);
54 #endif
55 
56             if(rSeq.hasElements() && nDiscreteWidth && nDiscreteHeight)
57             {
58                 // get destination size in pixels
59                 const MapMode aMapModePixel(MAP_PIXEL);
60                 const sal_uInt32 nViewVisibleArea(nDiscreteWidth * nDiscreteHeight);
61                 double fReduceFactor(1.0);
62                 drawinglayer::primitive2d::Primitive2DSequence aSequence(rSeq);
63 
64                 if(nViewVisibleArea > nMaxQuadratPixels)
65                 {
66                     // reduce render size
67                     fReduceFactor = sqrt((double)nMaxQuadratPixels / (double)nViewVisibleArea);
68                     nDiscreteWidth = basegfx::fround((double)nDiscreteWidth * fReduceFactor);
69                     nDiscreteHeight = basegfx::fround((double)nDiscreteHeight * fReduceFactor);
70 
71                     const drawinglayer::primitive2d::Primitive2DReference aEmbed(
72                         new drawinglayer::primitive2d::TransformPrimitive2D(
73                             basegfx::tools::createScaleB2DHomMatrix(fReduceFactor, fReduceFactor),
74                             rSeq));
75 
76                     aSequence = drawinglayer::primitive2d::Primitive2DSequence(&aEmbed, 1);
77                 }
78 
79                 const Point aEmptyPoint;
80                 const Size aSizePixel(nDiscreteWidth, nDiscreteHeight);
81                 geometry::ViewInformation2D aViewInformation2D(rViewInformation2D);
82                 VirtualDevice maContent;
83 
84                 // prepare vdev
85                 maContent.SetOutputSizePixel(aSizePixel, false);
86                 maContent.SetMapMode(aMapModePixel);
87 
88                 // set to all white
89                 maContent.SetBackground(Wallpaper(Color(COL_WHITE)));
90                 maContent.Erase();
91 
92                 // create pixel processor, also already takes care of AAing and
93                 // checking the getOptionsDrawinglayer().IsAntiAliasing() switch. If
94                 // not wanted, change after this call as needed
95                 processor2d::BaseProcessor2D* pContentProcessor = processor2d::createPixelProcessor2DFromOutputDevice(
96                     maContent,
97                     aViewInformation2D);
98 
99                 if(pContentProcessor)
100                 {
101                     // render content
102                     pContentProcessor->process(aSequence);
103 
104                     // get content
105                     maContent.EnableMapMode(false);
106                     const Bitmap aContent(maContent.GetBitmap(aEmptyPoint, aSizePixel));
107 
108 #ifdef DBG_UTIL
109                     if(bDoSaveForVisualControl)
110                     {
111                         SvFileStream aNew((const String&)String(ByteString( "c:\\test_content.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
112                         ::vcl::PNGWriter aPNGWriter(aContent);
113                         aPNGWriter.Write(aNew);
114                     }
115 #endif
116                     // prepare for mask creation
117                     maContent.SetMapMode(aMapModePixel);
118 
119                     // set alpha to all white (fully transparent)
120                     maContent.Erase();
121 
122                     // embed primitives to paint them black
123                     const primitive2d::Primitive2DReference xRef(
124                         new primitive2d::ModifiedColorPrimitive2D(
125                             aSequence,
126                             basegfx::BColorModifierSharedPtr(
127                                 new basegfx::BColorModifier_replace(
128                                     basegfx::BColor(0.0, 0.0, 0.0)))));
129                     const primitive2d::Primitive2DSequence xSeq(&xRef, 1);
130 
131                     // render
132                     pContentProcessor->process(xSeq);
133                     delete pContentProcessor;
134 
135                     // get alpha cahannel from vdev
136                     maContent.EnableMapMode(false);
137                     const Bitmap aAlpha(maContent.GetBitmap(aEmptyPoint, aSizePixel));
138 #ifdef DBG_UTIL
139                     if(bDoSaveForVisualControl)
140                     {
141                         SvFileStream aNew((const String&)String(ByteString( "c:\\test_alpha.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
142                         ::vcl::PNGWriter aPNGWriter(aAlpha);
143                         aPNGWriter.Write(aNew);
144                     }
145 #endif
146 
147                     // create BitmapEx result
148                     aRetval = BitmapEx(aContent, AlphaMask(aAlpha));
149 #ifdef DBG_UTIL
150                     if(bDoSaveForVisualControl)
151                     {
152                         SvFileStream aNew((const String&)String(ByteString( "c:\\test_combined.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
153                         ::vcl::PNGWriter aPNGWriter(aRetval);
154                         aPNGWriter.Write(aNew);
155                     }
156 #endif
157                 }
158             }
159 
160             return aRetval;
161         }
162 
163     } // end of namespace tools
164 } // end of namespace drawinglayer
165 
166 //////////////////////////////////////////////////////////////////////////////
167 // eof
168