LibreOffice Module canvas (master) 1
dx_canvasbitmap.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
22#include <memory>
23
25#include <tools/diagnose_ex.h>
26#include <vcl/bitmapex.hxx>
27
29
30#include "dx_canvasbitmap.hxx"
31#include "dx_impltools.hxx"
32
33
34using namespace ::com::sun::star;
35
36namespace dxcanvas
37{
39 const DeviceRef& rDevice ) :
40 mpDevice( rDevice ),
41 mpBitmap( rBitmap )
42 {
44 "CanvasBitmap::CanvasBitmap(): Invalid surface or device" );
45
46 maCanvasHelper.setDevice( *mpDevice );
47 maCanvasHelper.setTarget( mpBitmap );
48 }
49
51 {
52 mpBitmap.reset();
53 mpDevice.clear();
54
55 // forward to parent
56 CanvasBitmap_Base::disposeThis();
57 }
58
59 namespace {
60
61 struct AlphaDIB
62 {
63 BITMAPINFOHEADER bmiHeader;
64 RGBQUAD bmiColors[256];
65 AlphaDIB()
66 : bmiHeader({0,0,0,1,8,BI_RGB,0,0,0,0,0})
67 {
68 for (size_t i = 0; i < 256; ++i)
69 {
70 // this here fills palette with grey level colors, starting
71 // from 0,0,0 up to 255,255,255
72 BYTE const b(i);
73 bmiColors[i] = { b,b,b,b };
74 }
75 }
76 };
77
78 }
79
80 uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )
81 {
82 uno::Any aRes;
83 // 0 ... get BitmapEx
84 // 1 ... get Pixbuf with bitmap RGB content
85 // 2 ... get Pixbuf with bitmap alpha mask
86 switch( nHandle )
87 {
88 // sorry, no BitmapEx here...
89 case 0:
90 aRes <<= reinterpret_cast<sal_Int64>( nullptr );
91 break;
92
93 case 1:
94 {
95 if(!mpBitmap->hasAlpha())
96 {
97 HBITMAP aHBmp;
98 mpBitmap->getBitmap()->GetHBITMAP(Gdiplus::Color(), &aHBmp );
99
100 uno::Sequence< uno::Any > args{ uno::Any(reinterpret_cast<sal_Int64>(aHBmp)) };
101 aRes <<= args;
102 }
103 else
104 {
105 // need to copy&convert the bitmap, since dx
106 // canvas uses inline alpha channel
107 HDC hScreenDC=GetDC(nullptr);
108 const basegfx::B2IVector aSize(mpBitmap->getSize());
109 HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC,
110 aSize.getX(),
111 aSize.getY() );
112 if( !hBmpBitmap )
113 return aRes;
114
115 BITMAPINFOHEADER aBIH;
116
117 aBIH.biSize = sizeof( BITMAPINFOHEADER );
118 aBIH.biWidth = aSize.getX();
119 aBIH.biHeight = -aSize.getY();
120 aBIH.biPlanes = 1;
121 aBIH.biBitCount = 32;
122 aBIH.biCompression = BI_RGB; // expects pixel in
123 // bbggrrxx format
124 // (little endian)
125 aBIH.biSizeImage = 0;
126 aBIH.biXPelsPerMeter = 0;
127 aBIH.biYPelsPerMeter = 0;
128 aBIH.biClrUsed = 0;
129 aBIH.biClrImportant = 0;
130
131 Gdiplus::BitmapData aBmpData;
132 aBmpData.Width = aSize.getX();
133 aBmpData.Height = aSize.getY();
134 aBmpData.Stride = 4*aBmpData.Width;
135 aBmpData.PixelFormat = PixelFormat32bppARGB;
136 aBmpData.Scan0 = nullptr;
137 const Gdiplus::Rect aRect( 0,0,aSize.getX(),aSize.getY() );
138 BitmapSharedPtr pGDIPlusBitmap=mpBitmap->getBitmap();
139 if( Gdiplus::Ok != pGDIPlusBitmap->LockBits( &aRect,
140 Gdiplus::ImageLockModeRead,
141 PixelFormat32bppARGB, // outputs ARGB (big endian)
142 &aBmpData ) )
143 {
144 // failed to lock, bail out
145 return aRes;
146 }
147
148 // now aBmpData.Scan0 contains our bits - push
149 // them into HBITMAP, ignoring alpha
150 SetDIBits( hScreenDC, hBmpBitmap, 0, aSize.getY(), aBmpData.Scan0, reinterpret_cast<PBITMAPINFO>(&aBIH), DIB_RGB_COLORS );
151
152 pGDIPlusBitmap->UnlockBits( &aBmpData );
153
154 uno::Sequence< uno::Any > args{ uno::Any(reinterpret_cast<sal_Int64>(hBmpBitmap)) };
155 aRes <<= args;
156 }
157 }
158 break;
159
160 case 2:
161 {
162 if(!mpBitmap->hasAlpha())
163 {
164 return aRes;
165 }
166 else
167 {
168 static AlphaDIB aDIB;
169
170 // need to copy&convert the bitmap, since dx
171 // canvas uses inline alpha channel
172 HDC hScreenDC=GetDC(nullptr);
173 const basegfx::B2IVector aSize(mpBitmap->getSize());
174 HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC, aSize.getX(), aSize.getY() );
175 if( !hBmpBitmap )
176 return aRes;
177
178 aDIB.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
179 aDIB.bmiHeader.biWidth = aSize.getX();
180 aDIB.bmiHeader.biHeight = -aSize.getY();
181 aDIB.bmiHeader.biPlanes = 1;
182 aDIB.bmiHeader.biBitCount = 8;
183 aDIB.bmiHeader.biCompression = BI_RGB;
184 aDIB.bmiHeader.biSizeImage = 0;
185 aDIB.bmiHeader.biXPelsPerMeter = 0;
186 aDIB.bmiHeader.biYPelsPerMeter = 0;
187 aDIB.bmiHeader.biClrUsed = 0;
188 aDIB.bmiHeader.biClrImportant = 0;
189
190 Gdiplus::BitmapData aBmpData;
191 aBmpData.Width = aSize.getX();
192 aBmpData.Height = aSize.getY();
193 aBmpData.Stride = 4*aBmpData.Width;
194 aBmpData.PixelFormat = PixelFormat32bppARGB;
195 aBmpData.Scan0 = nullptr;
196 const Gdiplus::Rect aRect( 0,0,aSize.getX(),aSize.getY() );
197 BitmapSharedPtr pGDIPlusBitmap=mpBitmap->getBitmap();
198 if( Gdiplus::Ok != pGDIPlusBitmap->LockBits( &aRect,
199 Gdiplus::ImageLockModeRead,
200 PixelFormat32bppARGB, // outputs ARGB (big endian)
201 &aBmpData ) )
202 {
203 // failed to lock, bail out
204 return aRes;
205 }
206
207 // copy only alpha channel to pAlphaBits
208 const sal_Int32 nScanWidth((aSize.getX() + 3) & ~3);
209 std::unique_ptr<sal_uInt8[]> pAlphaBits( new sal_uInt8[nScanWidth*aSize.getY()] );
210 const sal_uInt8* pInBits=static_cast<sal_uInt8*>(aBmpData.Scan0);
211 pInBits+=3;
212 for( sal_Int32 y=0; y<aSize.getY(); ++y )
213 {
214 sal_uInt8* pOutBits=pAlphaBits.get()+y*nScanWidth;
215 for( sal_Int32 x=0; x<aSize.getX(); ++x )
216 {
217 *pOutBits++ = 255-*pInBits;
218 pInBits += 4;
219 }
220 }
221
222 pGDIPlusBitmap->UnlockBits( &aBmpData );
223
224 // set bits to newly create HBITMAP
225 SetDIBits( hScreenDC, hBmpBitmap, 0,
226 aSize.getY(), pAlphaBits.get(),
227 reinterpret_cast<PBITMAPINFO>(&aDIB), DIB_RGB_COLORS );
228
229 uno::Sequence< uno::Any > args{ uno::Any(reinterpret_cast<sal_Int64>(hBmpBitmap)) };
230 aRes <<= args;
231 }
232 }
233 break;
234 }
235
236 return aRes;
237 }
238
240 {
241 return "DXCanvas.CanvasBitmap";
242 }
243
244 sal_Bool SAL_CALL CanvasBitmap::supportsService( const OUString& ServiceName )
245 {
246 return cppu::supportsService( this, ServiceName );
247 }
248
249 uno::Sequence< OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames( )
250 {
251 return { "com.sun.star.rendering.CanvasBitmap" };
252 }
253
254}
255
256/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
FPDF_BITMAP mpBitmap
TYPE getX() const
TYPE getY() const
virtual OUString SAL_CALL getImplementationName() override
virtual css::uno::Any SAL_CALL getFastPropertyValue(sal_Int32 nHandle) override
CanvasBitmap(const IBitmapSharedPtr &rSurface, const DeviceRef &rDevice)
Create a canvas bitmap for the given surface.
IBitmapSharedPtr mpBitmap
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
DeviceRef mpDevice
MUST hold here, too, since CanvasHelper only contains a raw pointer (without refcounting)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void disposeThis() override
Dispose all internal references.
#define ENSURE_OR_THROW(c, m)
sal::systools::COMReference< IDirect3DDevice9 > mpDevice
Definition: dx_9rm.cxx:169
float y
Definition: dx_9rm.cxx:190
float x
Definition: dx_9rm.cxx:190
BITMAPINFOHEADER bmiHeader
RGBQUAD bmiColors[256]
Gdiplus::BitmapData aBmpData
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
std::shared_ptr< IBitmap > IBitmapSharedPtr
Definition: dx_ibitmap.hxx:58
std::shared_ptr< Gdiplus::Bitmap > BitmapSharedPtr
Definition: dx_winstuff.hxx:60
int i
args
sal_Int32 nHandle
unsigned char sal_uInt8
unsigned char sal_Bool
unsigned char BYTE