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 
28 #include <canvas/canvastools.hxx>
29 
30 #include "dx_canvasbitmap.hxx"
31 #include "dx_impltools.hxx"
32 
33 
34 using namespace ::com::sun::star;
35 
36 namespace 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  struct AlphaDIB
60  {
61  BITMAPINFOHEADER bmiHeader;
62  RGBQUAD bmiColors[256];
64  : bmiHeader({0,0,0,1,8,BI_RGB,0,0,0,0,0})
65  {
66  for (size_t i = 0; i < 256; ++i)
67  {
68  // this here fills palette with grey level colors, starting
69  // from 0,0,0 up to 255,255,255
70  BYTE const b(i);
71  bmiColors[i] = { b,b,b,b };
72  }
73  }
74  };
75 
76  uno::Any SAL_CALL CanvasBitmap::getFastPropertyValue( sal_Int32 nHandle )
77  {
78  uno::Any aRes;
79  // 0 ... get BitmapEx
80  // 1 ... get Pixbuf with bitmap RGB content
81  // 2 ... get Pixbuf with bitmap alpha mask
82  switch( nHandle )
83  {
84  // sorry, no BitmapEx here...
85  case 0:
86  aRes <<= reinterpret_cast<sal_Int64>( nullptr );
87  break;
88 
89  case 1:
90  {
91  if(!mpBitmap->hasAlpha())
92  {
93  HBITMAP aHBmp;
94  mpBitmap->getBitmap()->GetHBITMAP(Gdiplus::Color(), &aHBmp );
95 
96  uno::Sequence< uno::Any > args(1);
97  args[0] <<= sal_Int64(aHBmp);
98 
99  aRes <<= args;
100  }
101  else
102  {
103  // need to copy&convert the bitmap, since dx
104  // canvas uses inline alpha channel
105  HDC hScreenDC=GetDC(nullptr);
106  const basegfx::B2IVector aSize(mpBitmap->getSize());
107  HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC,
108  aSize.getX(),
109  aSize.getY() );
110  if( !hBmpBitmap )
111  return aRes;
112 
113  BITMAPINFOHEADER aBIH;
114 
115  aBIH.biSize = sizeof( BITMAPINFOHEADER );
116  aBIH.biWidth = aSize.getX();
117  aBIH.biHeight = -aSize.getY();
118  aBIH.biPlanes = 1;
119  aBIH.biBitCount = 32;
120  aBIH.biCompression = BI_RGB; // expects pixel in
121  // bbggrrxx format
122  // (little endian)
123  aBIH.biSizeImage = 0;
124  aBIH.biXPelsPerMeter = 0;
125  aBIH.biYPelsPerMeter = 0;
126  aBIH.biClrUsed = 0;
127  aBIH.biClrImportant = 0;
128 
129  Gdiplus::BitmapData aBmpData;
130  aBmpData.Width = aSize.getX();
131  aBmpData.Height = aSize.getY();
132  aBmpData.Stride = 4*aBmpData.Width;
133  aBmpData.PixelFormat = PixelFormat32bppARGB;
134  aBmpData.Scan0 = nullptr;
135  const Gdiplus::Rect aRect( 0,0,aSize.getX(),aSize.getY() );
136  BitmapSharedPtr pGDIPlusBitmap=mpBitmap->getBitmap();
137  if( Gdiplus::Ok != pGDIPlusBitmap->LockBits( &aRect,
138  Gdiplus::ImageLockModeRead,
139  PixelFormat32bppARGB, // outputs ARGB (big endian)
140  &aBmpData ) )
141  {
142  // failed to lock, bail out
143  return aRes;
144  }
145 
146  // now aBmpData.Scan0 contains our bits - push
147  // them into HBITMAP, ignoring alpha
148  SetDIBits( hScreenDC, hBmpBitmap, 0, aSize.getY(), aBmpData.Scan0, reinterpret_cast<PBITMAPINFO>(&aBIH), DIB_RGB_COLORS );
149 
150  pGDIPlusBitmap->UnlockBits( &aBmpData );
151 
152  uno::Sequence< uno::Any > args(1);
153  args[0] <<= sal_Int64(hBmpBitmap);
154 
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(1);
230  args[0] <<= sal_Int64(hBmpBitmap);
231 
232  aRes <<= args;
233  }
234  }
235  break;
236  }
237 
238  return aRes;
239  }
240 
242  {
243  return "DXCanvas.CanvasBitmap";
244  }
245 
246  sal_Bool SAL_CALL CanvasBitmap::supportsService( const OUString& ServiceName )
247  {
248  return cppu::supportsService( this, ServiceName );
249  }
250 
251  uno::Sequence< OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames( )
252  {
253  return { "com.sun.star.rendering.CanvasBitmap" };
254  }
255 
256 }
257 
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
std::shared_ptr< IBitmap > IBitmapSharedPtr
Definition: dx_ibitmap.hxx:59
unsigned char BYTE
tuple args
BITMAPINFOHEADER bmiHeader
float x
Definition: dx_9rm.cxx:189
IBitmapSharedPtr mpBitmap
CanvasBitmap(const IBitmapSharedPtr &rSurface, const DeviceRef &rDevice)
Create a canvas bitmap for the given surface.
COMReference< IDirect3DDevice9 > mpDevice
Definition: dx_9rm.cxx:168
virtual css::uno::Any SAL_CALL getFastPropertyValue(sal_Int32 nHandle) override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
virtual void disposeThis() override
Dispose all internal references.
float y
Definition: dx_9rm.cxx:189
RGBQUAD bmiColors[256]
int i
unsigned char sal_Bool
Gdiplus::BitmapData aBmpData
std::shared_ptr< Gdiplus::Bitmap > BitmapSharedPtr
Definition: dx_winstuff.hxx:63
#define ENSURE_OR_THROW(c, m)
unsigned char sal_uInt8
DeviceRef mpDevice
MUST hold here, too, since CanvasHelper only contains a raw pointer (without refcounting) ...
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override