1ddde725dSArmin Le Grand /**************************************************************
2ddde725dSArmin Le Grand  *
3ddde725dSArmin Le Grand  * Licensed to the Apache Software Foundation (ASF) under one
4ddde725dSArmin Le Grand  * or more contributor license agreements.  See the NOTICE file
5ddde725dSArmin Le Grand  * distributed with this work for additional information
6ddde725dSArmin Le Grand  * regarding copyright ownership.  The ASF licenses this file
7ddde725dSArmin Le Grand  * to you under the Apache License, Version 2.0 (the
8ddde725dSArmin Le Grand  * "License"); you may not use this file except in compliance
9ddde725dSArmin Le Grand  * with the License.  You may obtain a copy of the License at
10ddde725dSArmin Le Grand  *
112b45cf47SArmin Le Grand  *   http://www.apache.org/licenses/LICENSE-2.0
12ddde725dSArmin Le Grand  *
13ddde725dSArmin Le Grand  * Unless required by applicable law or agreed to in writing,
14ddde725dSArmin Le Grand  * software distributed under the License is distributed on an
15ddde725dSArmin Le Grand  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ddde725dSArmin Le Grand  * KIND, either express or implied.  See the License for the
17ddde725dSArmin Le Grand  * specific language governing permissions and limitations
18ddde725dSArmin Le Grand  * under the License.
19ddde725dSArmin Le Grand  *
20ddde725dSArmin Le Grand  *************************************************************/
21ddde725dSArmin Le Grand 
22ddde725dSArmin Le Grand // MARKER(update_precomp.py): autogen include statement, do not remove
23ddde725dSArmin Le Grand #include "precompiled_drawinglayer.hxx"
24ddde725dSArmin Le Grand 
25ddde725dSArmin Le Grand #include <drawinglayer/tools/converters.hxx>
26ddde725dSArmin Le Grand #include <drawinglayer/geometry/viewinformation2d.hxx>
27ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
28ddde725dSArmin Le Grand #include <basegfx/matrix/b2dhommatrixtools.hxx>
29ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
30a7185797SArmin Le Grand #include <drawinglayer/processor2d/processor2dtools.hxx>
31ddde725dSArmin Le Grand #include <vcl/virdev.hxx>
32ddde725dSArmin Le Grand 
33ddde725dSArmin Le Grand #ifdef DBG_UTIL
34ddde725dSArmin Le Grand #include <tools/stream.hxx>
3545fd3b9aSArmin Le Grand #include <vcl/pngwrite.hxx>
36ddde725dSArmin Le Grand #endif
37ddde725dSArmin Le Grand 
38ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
39ddde725dSArmin Le Grand 
40ddde725dSArmin Le Grand namespace drawinglayer
41ddde725dSArmin Le Grand {
42ddde725dSArmin Le Grand     namespace tools
43ddde725dSArmin Le Grand     {
convertToBitmapEx(const drawinglayer::primitive2d::Primitive2DSequence & rSeq,const geometry::ViewInformation2D & rViewInformation2D,sal_uInt32 nDiscreteWidth,sal_uInt32 nDiscreteHeight,sal_uInt32 nMaxQuadratPixels)44a7185797SArmin Le Grand         BitmapEx convertToBitmapEx(
45ddde725dSArmin Le Grand             const drawinglayer::primitive2d::Primitive2DSequence& rSeq,
46ddde725dSArmin Le Grand             const geometry::ViewInformation2D& rViewInformation2D,
47ddde725dSArmin Le Grand             sal_uInt32 nDiscreteWidth,
48ddde725dSArmin Le Grand             sal_uInt32 nDiscreteHeight,
49ddde725dSArmin Le Grand             sal_uInt32 nMaxQuadratPixels)
50ddde725dSArmin Le Grand         {
51ddde725dSArmin Le Grand             BitmapEx aRetval;
527eeb9ceeSArmin Le Grand #ifdef DBG_UTIL
537eeb9ceeSArmin Le Grand             static bool bDoSaveForVisualControl(false);
547eeb9ceeSArmin Le Grand #endif
55ddde725dSArmin Le Grand 
56ddde725dSArmin Le Grand             if(rSeq.hasElements() && nDiscreteWidth && nDiscreteHeight)
57ddde725dSArmin Le Grand             {
58ddde725dSArmin Le Grand                 // get destination size in pixels
59ddde725dSArmin Le Grand                 const MapMode aMapModePixel(MAP_PIXEL);
60ddde725dSArmin Le Grand                 const sal_uInt32 nViewVisibleArea(nDiscreteWidth * nDiscreteHeight);
61ddde725dSArmin Le Grand                 double fReduceFactor(1.0);
62ddde725dSArmin Le Grand                 drawinglayer::primitive2d::Primitive2DSequence aSequence(rSeq);
63ddde725dSArmin Le Grand 
64ddde725dSArmin Le Grand                 if(nViewVisibleArea > nMaxQuadratPixels)
65ddde725dSArmin Le Grand                 {
66ddde725dSArmin Le Grand                     // reduce render size
67ddde725dSArmin Le Grand                     fReduceFactor = sqrt((double)nMaxQuadratPixels / (double)nViewVisibleArea);
68ddde725dSArmin Le Grand                     nDiscreteWidth = basegfx::fround((double)nDiscreteWidth * fReduceFactor);
69ddde725dSArmin Le Grand                     nDiscreteHeight = basegfx::fround((double)nDiscreteHeight * fReduceFactor);
70ddde725dSArmin Le Grand 
71ddde725dSArmin Le Grand                     const drawinglayer::primitive2d::Primitive2DReference aEmbed(
72ddde725dSArmin Le Grand                         new drawinglayer::primitive2d::TransformPrimitive2D(
73ddde725dSArmin Le Grand                             basegfx::tools::createScaleB2DHomMatrix(fReduceFactor, fReduceFactor),
74ddde725dSArmin Le Grand                             rSeq));
75ddde725dSArmin Le Grand 
76ddde725dSArmin Le Grand                     aSequence = drawinglayer::primitive2d::Primitive2DSequence(&aEmbed, 1);
77ddde725dSArmin Le Grand                 }
78ddde725dSArmin Le Grand 
79ddde725dSArmin Le Grand                 const Point aEmptyPoint;
80ddde725dSArmin Le Grand                 const Size aSizePixel(nDiscreteWidth, nDiscreteHeight);
81ddde725dSArmin Le Grand                 geometry::ViewInformation2D aViewInformation2D(rViewInformation2D);
82ddde725dSArmin Le Grand                 VirtualDevice maContent;
83ddde725dSArmin Le Grand 
84ddde725dSArmin Le Grand                 // prepare vdev
85ddde725dSArmin Le Grand                 maContent.SetOutputSizePixel(aSizePixel, false);
86ddde725dSArmin Le Grand                 maContent.SetMapMode(aMapModePixel);
87ddde725dSArmin Le Grand 
88ddde725dSArmin Le Grand                 // set to all white
89ddde725dSArmin Le Grand                 maContent.SetBackground(Wallpaper(Color(COL_WHITE)));
90ddde725dSArmin Le Grand                 maContent.Erase();
91ddde725dSArmin Le Grand 
927eeb9ceeSArmin Le Grand                 // create pixel processor, also already takes care of AAing and
937eeb9ceeSArmin Le Grand                 // checking the getOptionsDrawinglayer().IsAntiAliasing() switch. If
947eeb9ceeSArmin Le Grand                 // not wanted, change after this call as needed
95a7185797SArmin Le Grand                 processor2d::BaseProcessor2D* pContentProcessor = processor2d::createPixelProcessor2DFromOutputDevice(
96a7185797SArmin Le Grand                     maContent,
97a7185797SArmin Le Grand                     aViewInformation2D);
98ddde725dSArmin Le Grand 
99a7185797SArmin Le Grand                 if(pContentProcessor)
100a7185797SArmin Le Grand                 {
101a7185797SArmin Le Grand                     // render content
102a7185797SArmin Le Grand                     pContentProcessor->process(aSequence);
103a7185797SArmin Le Grand 
104a7185797SArmin Le Grand                     // get content
105a7185797SArmin Le Grand                     maContent.EnableMapMode(false);
106a7185797SArmin Le Grand                     const Bitmap aContent(maContent.GetBitmap(aEmptyPoint, aSizePixel));
107a7185797SArmin Le Grand 
1087eeb9ceeSArmin Le Grand #ifdef DBG_UTIL
1097eeb9ceeSArmin Le Grand                     if(bDoSaveForVisualControl)
1107eeb9ceeSArmin Le Grand                     {
1117eeb9ceeSArmin Le Grand                         SvFileStream aNew((const String&)String(ByteString( "c:\\test_content.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
1127eeb9ceeSArmin Le Grand                         ::vcl::PNGWriter aPNGWriter(aContent);
1137eeb9ceeSArmin Le Grand                         aPNGWriter.Write(aNew);
1147eeb9ceeSArmin Le Grand                     }
1157eeb9ceeSArmin Le Grand #endif
116a7185797SArmin Le Grand                     // prepare for mask creation
117a7185797SArmin Le Grand                     maContent.SetMapMode(aMapModePixel);
118a7185797SArmin Le Grand 
119a7185797SArmin Le Grand                     // set alpha to all white (fully transparent)
120a7185797SArmin Le Grand                     maContent.Erase();
121a7185797SArmin Le Grand 
122a7185797SArmin Le Grand                     // embed primitives to paint them black
123a7185797SArmin Le Grand                     const primitive2d::Primitive2DReference xRef(
124a7185797SArmin Le Grand                         new primitive2d::ModifiedColorPrimitive2D(
125a7185797SArmin Le Grand                             aSequence,
12649c58f9bSArmin Le Grand                             basegfx::BColorModifierSharedPtr(
12749c58f9bSArmin Le Grand                                 new basegfx::BColorModifier_replace(
12849c58f9bSArmin Le Grand                                     basegfx::BColor(0.0, 0.0, 0.0)))));
129a7185797SArmin Le Grand                     const primitive2d::Primitive2DSequence xSeq(&xRef, 1);
130a7185797SArmin Le Grand 
131a7185797SArmin Le Grand                     // render
132a7185797SArmin Le Grand                     pContentProcessor->process(xSeq);
133a7185797SArmin Le Grand                     delete pContentProcessor;
134a7185797SArmin Le Grand 
135*4e167d9cSJohn Bampton                     // get alpha channel from vdev
136a7185797SArmin Le Grand                     maContent.EnableMapMode(false);
1377eeb9ceeSArmin Le Grand                     const Bitmap aAlpha(maContent.GetBitmap(aEmptyPoint, aSizePixel));
1387eeb9ceeSArmin Le Grand #ifdef DBG_UTIL
1397eeb9ceeSArmin Le Grand                     if(bDoSaveForVisualControl)
1407eeb9ceeSArmin Le Grand                     {
1417eeb9ceeSArmin Le Grand                         SvFileStream aNew((const String&)String(ByteString( "c:\\test_alpha.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
1427eeb9ceeSArmin Le Grand                         ::vcl::PNGWriter aPNGWriter(aAlpha);
1437eeb9ceeSArmin Le Grand                         aPNGWriter.Write(aNew);
1447eeb9ceeSArmin Le Grand                     }
1457eeb9ceeSArmin Le Grand #endif
146a7185797SArmin Le Grand 
147a7185797SArmin Le Grand                     // create BitmapEx result
1487eeb9ceeSArmin Le Grand                     aRetval = BitmapEx(aContent, AlphaMask(aAlpha));
149ddde725dSArmin Le Grand #ifdef DBG_UTIL
1507eeb9ceeSArmin Le Grand                     if(bDoSaveForVisualControl)
1517eeb9ceeSArmin Le Grand                     {
1527eeb9ceeSArmin Le Grand                         SvFileStream aNew((const String&)String(ByteString( "c:\\test_combined.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
1537eeb9ceeSArmin Le Grand                         ::vcl::PNGWriter aPNGWriter(aRetval);
1547eeb9ceeSArmin Le Grand                         aPNGWriter.Write(aNew);
1557eeb9ceeSArmin Le Grand                     }
156ddde725dSArmin Le Grand #endif
1577eeb9ceeSArmin Le Grand                 }
1587eeb9ceeSArmin Le Grand             }
159ddde725dSArmin Le Grand 
160ddde725dSArmin Le Grand             return aRetval;
161ddde725dSArmin Le Grand         }
162ddde725dSArmin Le Grand 
163ddde725dSArmin Le Grand     } // end of namespace tools
164ddde725dSArmin Le Grand } // end of namespace drawinglayer
165ddde725dSArmin Le Grand 
166ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
167ddde725dSArmin Le Grand // eof
168