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  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(1);
101  args[0] <<= sal_Int64(aHBmp);
102 
103  aRes <<= args;
104  }
105  else
106  {
107  // need to copy&convert the bitmap, since dx
108  // canvas uses inline alpha channel
109  HDC hScreenDC=GetDC(nullptr);
110  const basegfx::B2IVector aSize(mpBitmap->getSize());
111  HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC,
112  aSize.getX(),
113  aSize.getY() );
114  if( !hBmpBitmap )
115  return aRes;
116 
117  BITMAPINFOHEADER aBIH;
118 
119  aBIH.biSize = sizeof( BITMAPINFOHEADER );
120  aBIH.biWidth = aSize.getX();
121  aBIH.biHeight = -aSize.getY();
122  aBIH.biPlanes = 1;
123  aBIH.biBitCount = 32;
124  aBIH.biCompression = BI_RGB; // expects pixel in
125  // bbggrrxx format
126  // (little endian)
127  aBIH.biSizeImage = 0;
128  aBIH.biXPelsPerMeter = 0;
129  aBIH.biYPelsPerMeter = 0;
130  aBIH.biClrUsed = 0;
131  aBIH.biClrImportant = 0;
132 
133  Gdiplus::BitmapData aBmpData;
134  aBmpData.Width = aSize.getX();
135  aBmpData.Height = aSize.getY();
136  aBmpData.Stride = 4*aBmpData.Width;
137  aBmpData.PixelFormat = PixelFormat32bppARGB;
138  aBmpData.Scan0 = nullptr;
139  const Gdiplus::Rect aRect( 0,0,aSize.getX(),aSize.getY() );
140  BitmapSharedPtr pGDIPlusBitmap=mpBitmap->getBitmap();
141  if( Gdiplus::Ok != pGDIPlusBitmap->LockBits( &aRect,
142  Gdiplus::ImageLockModeRead,
143  PixelFormat32bppARGB, // outputs ARGB (big endian)
144  &aBmpData ) )
145  {
146  // failed to lock, bail out
147  return aRes;
148  }
149 
150  // now aBmpData.Scan0 contains our bits - push
151  // them into HBITMAP, ignoring alpha
152  SetDIBits( hScreenDC, hBmpBitmap, 0, aSize.getY(), aBmpData.Scan0, reinterpret_cast<PBITMAPINFO>(&aBIH), DIB_RGB_COLORS );
153 
154  pGDIPlusBitmap->UnlockBits( &aBmpData );
155 
156  uno::Sequence< uno::Any > args(1);
157  args[0] <<= sal_Int64(hBmpBitmap);
158 
159  aRes <<= args;
160  }
161  }
162  break;
163 
164  case 2:
165  {
166  if(!mpBitmap->hasAlpha())
167  {
168  return aRes;
169  }
170  else
171  {
172  static AlphaDIB aDIB;
173 
174  // need to copy&convert the bitmap, since dx
175  // canvas uses inline alpha channel
176  HDC hScreenDC=GetDC(nullptr);
177  const basegfx::B2IVector aSize(mpBitmap->getSize());
178  HBITMAP hBmpBitmap = CreateCompatibleBitmap( hScreenDC, aSize.getX(), aSize.getY() );
179  if( !hBmpBitmap )
180  return aRes;
181 
182  aDIB.bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
183  aDIB.bmiHeader.biWidth = aSize.getX();
184  aDIB.bmiHeader.biHeight = -aSize.getY();
185  aDIB.bmiHeader.biPlanes = 1;
186  aDIB.bmiHeader.biBitCount = 8;
187  aDIB.bmiHeader.biCompression = BI_RGB;
188  aDIB.bmiHeader.biSizeImage = 0;
189  aDIB.bmiHeader.biXPelsPerMeter = 0;
190  aDIB.bmiHeader.biYPelsPerMeter = 0;
191  aDIB.bmiHeader.biClrUsed = 0;
192  aDIB.bmiHeader.biClrImportant = 0;
193 
194  Gdiplus::BitmapData aBmpData;
195  aBmpData.Width = aSize.getX();
196  aBmpData.Height = aSize.getY();
197  aBmpData.Stride = 4*aBmpData.Width;
198  aBmpData.PixelFormat = PixelFormat32bppARGB;
199  aBmpData.Scan0 = nullptr;
200  const Gdiplus::Rect aRect( 0,0,aSize.getX(),aSize.getY() );
201  BitmapSharedPtr pGDIPlusBitmap=mpBitmap->getBitmap();
202  if( Gdiplus::Ok != pGDIPlusBitmap->LockBits( &aRect,
203  Gdiplus::ImageLockModeRead,
204  PixelFormat32bppARGB, // outputs ARGB (big endian)
205  &aBmpData ) )
206  {
207  // failed to lock, bail out
208  return aRes;
209  }
210 
211  // copy only alpha channel to pAlphaBits
212  const sal_Int32 nScanWidth((aSize.getX() + 3) & ~3);
213  std::unique_ptr<sal_uInt8[]> pAlphaBits( new sal_uInt8[nScanWidth*aSize.getY()] );
214  const sal_uInt8* pInBits=static_cast<sal_uInt8*>(aBmpData.Scan0);
215  pInBits+=3;
216  for( sal_Int32 y=0; y<aSize.getY(); ++y )
217  {
218  sal_uInt8* pOutBits=pAlphaBits.get()+y*nScanWidth;
219  for( sal_Int32 x=0; x<aSize.getX(); ++x )
220  {
221  *pOutBits++ = 255-*pInBits;
222  pInBits += 4;
223  }
224  }
225 
226  pGDIPlusBitmap->UnlockBits( &aBmpData );
227 
228  // set bits to newly create HBITMAP
229  SetDIBits( hScreenDC, hBmpBitmap, 0,
230  aSize.getY(), pAlphaBits.get(),
231  reinterpret_cast<PBITMAPINFO>(&aDIB), DIB_RGB_COLORS );
232 
233  uno::Sequence< uno::Any > args(1);
234  args[0] <<= sal_Int64(hBmpBitmap);
235 
236  aRes <<= args;
237  }
238  }
239  break;
240  }
241 
242  return aRes;
243  }
244 
246  {
247  return "DXCanvas.CanvasBitmap";
248  }
249 
250  sal_Bool SAL_CALL CanvasBitmap::supportsService( const OUString& ServiceName )
251  {
252  return cppu::supportsService( this, ServiceName );
253  }
254 
255  uno::Sequence< OUString > SAL_CALL CanvasBitmap::getSupportedServiceNames( )
256  {
257  return { "com.sun.star.rendering.CanvasBitmap" };
258  }
259 
260 }
261 
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
RGBQUAD bmiColors[256]
std::shared_ptr< IBitmap > IBitmapSharedPtr
Definition: dx_ibitmap.hxx:59
unsigned char BYTE
tuple args
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
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
BITMAPINFOHEADER bmiHeader
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