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 #include <vcl/pngwrite.hxx> 36 #endif 37 38 ////////////////////////////////////////////////////////////////////////////// 39 40 namespace drawinglayer 41 { 42 namespace tools 43 { 44 BitmapEx DRAWINGLAYER_DLLPUBLIC 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 processor 91 processor2d::VclPixelProcessor2D aContentProcessor(aViewInformation2D, maContent); 92 93 // render content 94 aContentProcessor.process(aSequence); 95 96 // get content 97 maContent.EnableMapMode(false); 98 const Bitmap aContent(maContent.GetBitmap(aEmptyPoint, aSizePixel)); 99 100 // prepare for mask creation 101 maContent.SetMapMode(aMapModePixel); 102 maContent.SetAntialiasing(true); 103 104 // set alpha to all white (fully transparent) 105 maContent.Erase(); 106 107 // embed primitives to paint them black 108 const primitive2d::Primitive2DReference xRef( 109 new primitive2d::ModifiedColorPrimitive2D( 110 aSequence, 111 basegfx::BColorModifier( 112 basegfx::BColor(0.0, 0.0, 0.0), 113 0.5, 114 basegfx::BCOLORMODIFYMODE_REPLACE))); 115 const primitive2d::Primitive2DSequence xSeq(&xRef, 1); 116 117 // render 118 aContentProcessor.process(xSeq); 119 120 // get alpha cahannel from vdev 121 maContent.EnableMapMode(false); 122 const AlphaMask aAlphaMask(maContent.GetBitmap(aEmptyPoint, aSizePixel)); 123 124 // create BitmapEx result 125 aRetval = BitmapEx(aContent, aAlphaMask); 126 } 127 128 #ifdef DBG_UTIL 129 static bool bDoSaveForVisualControl(false); 130 if(bDoSaveForVisualControl) 131 { 132 SvFileStream aNew((const String&)String(ByteString( "c:\\test.png" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC); 133 ::vcl::PNGWriter aPNGWriter(aRetval); 134 aPNGWriter.Write(aNew); 135 } 136 #endif 137 138 return aRetval; 139 } 140 141 } // end of namespace tools 142 } // end of namespace drawinglayer 143 144 ////////////////////////////////////////////////////////////////////////////// 145 // eof 146