xref: /trunk/main/drawinglayer/source/processor2d/vclhelperbufferdevice.cxx (revision 464702f4578bd67db020a330afd07883930c5e07)
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 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_drawinglayer.hxx"
26 
27 #include <vclhelperbufferdevice.hxx>
28 #include <basegfx/range/b2drange.hxx>
29 #include <vcl/bitmapex.hxx>
30 #include <basegfx/matrix/b2dhommatrix.hxx>
31 #include <tools/stream.hxx>
32 
33 //////////////////////////////////////////////////////////////////////////////
34 // support for rendering Bitmap and BitmapEx contents
35 
36 namespace drawinglayer
37 {
38     impBufferDevice::impBufferDevice(
39         OutputDevice& rOutDev,
40         const basegfx::B2DRange& rRange,
41         bool bAddOffsetToMapping)
42     :   mrOutDev(rOutDev),
43         maContent(rOutDev),
44         mpMask(0L),
45         mpAlpha(0L)
46     {
47         basegfx::B2DRange aRangePixel(rRange);
48         aRangePixel.transform(rOutDev.GetViewTransformation());
49         const Rectangle aRectPixel(
50             (sal_Int32)floor(aRangePixel.getMinX()), (sal_Int32)floor(aRangePixel.getMinY()),
51             (sal_Int32)ceil(aRangePixel.getMaxX()), (sal_Int32)ceil(aRangePixel.getMaxY()));
52         const Point aEmptyPoint;
53         maDestPixel = Rectangle(aEmptyPoint, rOutDev.GetOutputSizePixel());
54         maDestPixel.Intersection(aRectPixel);
55 
56         if(isVisible())
57         {
58             maContent.SetOutputSizePixel(maDestPixel.GetSize(), false);
59 
60             // #i93485# assert when copying from window to VDev is used
61             OSL_ENSURE(rOutDev.GetOutDevType() != OUTDEV_WINDOW,
62                 "impBufferDevice render helper: Copying from Window to VDev, this should be avoided (!)");
63 
64             const bool bWasEnabledSrc(rOutDev.IsMapModeEnabled());
65             rOutDev.EnableMapMode(false);
66             maContent.DrawOutDev(aEmptyPoint, maDestPixel.GetSize(), maDestPixel.TopLeft(), maDestPixel.GetSize(), rOutDev);
67             rOutDev.EnableMapMode(bWasEnabledSrc);
68 
69             MapMode aNewMapMode(rOutDev.GetMapMode());
70 
71             if(bAddOffsetToMapping)
72             {
73                 const Point aLogicTopLeft(rOutDev.PixelToLogic(maDestPixel.TopLeft()));
74                 aNewMapMode.SetOrigin(Point(-aLogicTopLeft.X(), -aLogicTopLeft.Y()));
75             }
76 
77             maContent.SetMapMode(aNewMapMode);
78 
79             // copy AA flag for new target
80             maContent.SetAntialiasing(mrOutDev.GetAntialiasing());
81         }
82     }
83 
84     impBufferDevice::~impBufferDevice()
85     {
86         delete mpMask;
87         delete mpAlpha;
88     }
89 
90     void impBufferDevice::paint(double fTrans)
91     {
92         const Point aEmptyPoint;
93         const Size aSizePixel(maContent.GetOutputSizePixel());
94         const bool bWasEnabledDst(mrOutDev.IsMapModeEnabled());
95         static bool bDoSaveForVisualControl(false);
96 
97         mrOutDev.EnableMapMode(false);
98         maContent.EnableMapMode(false);
99         Bitmap aContent(maContent.GetBitmap(aEmptyPoint, aSizePixel));
100 
101         if(bDoSaveForVisualControl)
102         {
103             SvFileStream aNew((const String&)String(ByteString( "c:\\content.bmp" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
104             aNew << aContent;
105         }
106 
107         if(mpAlpha)
108         {
109             mpAlpha->EnableMapMode(false);
110             const AlphaMask aAlphaMask(mpAlpha->GetBitmap(aEmptyPoint, aSizePixel));
111 
112             if(bDoSaveForVisualControl)
113             {
114                 SvFileStream aNew((const String&)String(ByteString( "c:\\transparence.bmp" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
115                 aNew << aAlphaMask.GetBitmap();
116             }
117 
118             mrOutDev.DrawBitmapEx(maDestPixel.TopLeft(), BitmapEx(aContent, aAlphaMask));
119         }
120         else if(mpMask)
121         {
122             mpMask->EnableMapMode(false);
123             const Bitmap aMask(mpMask->GetBitmap(aEmptyPoint, aSizePixel));
124 
125             if(bDoSaveForVisualControl)
126             {
127                 SvFileStream aNew((const String&)String(ByteString( "c:\\mask.bmp" ), RTL_TEXTENCODING_UTF8), STREAM_WRITE|STREAM_TRUNC);
128                 aNew << aMask;
129             }
130 
131             mrOutDev.DrawBitmapEx(maDestPixel.TopLeft(), BitmapEx(aContent, aMask));
132         }
133         else if(0.0 != fTrans)
134         {
135             sal_uInt8 nMaskValue((sal_uInt8)basegfx::fround(fTrans * 255.0));
136             const AlphaMask aAlphaMask(aSizePixel, &nMaskValue);
137             mrOutDev.DrawBitmapEx(maDestPixel.TopLeft(), BitmapEx(aContent, aAlphaMask));
138         }
139         else
140         {
141             mrOutDev.DrawBitmap(maDestPixel.TopLeft(), aContent);
142         }
143 
144         mrOutDev.EnableMapMode(bWasEnabledDst);
145     }
146 
147     VirtualDevice& impBufferDevice::getMask()
148     {
149         if(!mpMask)
150         {
151             mpMask = new VirtualDevice(mrOutDev, 1);
152             mpMask->SetOutputSizePixel(maDestPixel.GetSize(), true);
153             mpMask->SetMapMode(maContent.GetMapMode());
154 
155             // do NOT copy AA flag for mask!
156         }
157 
158         return *mpMask;
159     }
160 
161     VirtualDevice& impBufferDevice::getTransparence()
162     {
163         if(!mpAlpha)
164         {
165             mpAlpha = new VirtualDevice();
166             mpAlpha->SetOutputSizePixel(maDestPixel.GetSize(), true);
167             mpAlpha->SetMapMode(maContent.GetMapMode());
168 
169             // copy AA flag for new target; masking needs to be smooth
170             mpAlpha->SetAntialiasing(maContent.GetAntialiasing());
171         }
172 
173         return *mpAlpha;
174     }
175 } // end of namespace drawinglayer
176 
177 //////////////////////////////////////////////////////////////////////////////
178 // eof
179