LibreOffice Module canvas (master)  1
canvasbitmaphelper.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 #include <sal/log.hxx>
22 
23 #include <canvas/canvastools.hxx>
24 #include <tools/diagnose_ex.h>
25 #include <vcl/bitmapex.hxx>
26 #include <vcl/BitmapTools.hxx>
27 #include <vcl/canvastools.hxx>
28 
29 #include "canvasbitmap.hxx"
30 #include "canvasbitmaphelper.hxx"
31 
32 
33 using namespace ::com::sun::star;
34 
35 namespace vclcanvas
36 {
38  mpBackBuffer(),
39  mpOutDevReference()
40  {
41  }
42 
43  void CanvasBitmapHelper::init( const BitmapEx& rBitmap,
44  rendering::XGraphicDevice& rDevice,
45  const OutDevProviderSharedPtr& rOutDevReference )
46  {
47  mpOutDevReference = rOutDevReference;
48  mpBackBuffer = std::make_shared<BitmapBackBuffer>( rBitmap, rOutDevReference->getOutDev() );
49 
50  // forward new settings to base class (ref device, output
51  // surface, no protection (own backbuffer), alpha depends on
52  // whether BmpEx is transparent or not)
53  CanvasHelper::init( rDevice,
55  false,
56  rBitmap.IsTransparent() );
57  }
58 
60  {
61  mpBackBuffer.reset();
62  mpOutDevReference.reset();
63 
64  // forward to base class
66  }
67 
68  geometry::IntegerSize2D CanvasBitmapHelper::getSize() const
69  {
70  if( !mpBackBuffer )
71  return geometry::IntegerSize2D();
72 
73  return vcl::unotools::integerSize2DFromSize( mpBackBuffer->getBitmapSizePixel() );
74  }
75 
77  {
78  // are we disposed?
79  if( mpBackBuffer )
80  mpBackBuffer->clear(); // alpha vdev needs special treatment
81  }
82 
83  uno::Reference< rendering::XBitmap > CanvasBitmapHelper::getScaledBitmap( const geometry::RealSize2D& newSize,
84  bool beFast )
85  {
87  "disposed CanvasHelper" );
88 
89  SAL_INFO( "canvas.vcl", "::vclcanvas::CanvasBitmapHelper::getScaledBitmap()" );
90 
91  if( !mpBackBuffer || mpDevice )
92  return uno::Reference< rendering::XBitmap >(); // we're disposed
93 
94  BitmapEx aRes( mpBackBuffer->getBitmapReference() );
95 
97  beFast ? BmpScaleFlag::Default : BmpScaleFlag::BestQuality );
98 
99  return uno::Reference< rendering::XBitmap >(
100  new CanvasBitmap( aRes, *mpDevice, mpOutDevReference ) );
101  }
102 
103  uno::Sequence< sal_Int8 > CanvasBitmapHelper::getData( rendering::IntegerBitmapLayout& rLayout,
104  const geometry::IntegerRectangle2D& rect )
105  {
106  SAL_INFO( "canvas.vcl", "::vclcanvas::CanvasBitmapHelper::getData()" );
107 
108  if( !mpBackBuffer )
109  return uno::Sequence< sal_Int8 >(); // we're disposed
110 
111  rLayout = getMemoryLayout();
112 
113  // TODO(F1): Support more formats.
114  const Size aBmpSize( mpBackBuffer->getBitmapReference().GetSizePixel() );
115 
116  rLayout.ScanLines = aBmpSize.Height();
117  rLayout.ScanLineBytes = aBmpSize.Width()*4;
118  rLayout.ScanLineStride = rLayout.ScanLineBytes;
119 
120  uno::Sequence< sal_Int8 > aRes = vcl::bitmap::CanvasExtractBitmapData(mpBackBuffer->getBitmapReference(), rect);
121  return aRes;
122  }
123 
124  uno::Sequence< sal_Int8 > CanvasBitmapHelper::getPixel( rendering::IntegerBitmapLayout& rLayout,
125  const geometry::IntegerPoint2D& pos )
126  {
127  SAL_INFO( "canvas.vcl", "::vclcanvas::CanvasBitmapHelper::getPixel()" );
128 
129  if( !mpBackBuffer )
130  return uno::Sequence< sal_Int8 >(); // we're disposed
131 
132  rLayout = getMemoryLayout();
133  rLayout.ScanLines = 1;
134  rLayout.ScanLineBytes = 4;
135  rLayout.ScanLineStride = rLayout.ScanLineBytes;
136 
137  const Size aBmpSize( mpBackBuffer->getBitmapReference().GetSizePixel() );
138 
139  ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aBmpSize.Width(),
140  "X coordinate out of bounds" );
141  ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aBmpSize.Height(),
142  "Y coordinate out of bounds" );
143 
144  ::Color aColor = mpBackBuffer->getBitmapReference().GetPixelColor(pos.X, pos.Y);
145 
146  uno::Sequence< sal_Int8 > aRes( 4 );
147  sal_Int8* pRes = aRes.getArray();
148  pRes[ 0 ] = aColor.GetRed();
149  pRes[ 1 ] = aColor.GetGreen();
150  pRes[ 2 ] = aColor.GetBlue();
151  pRes[ 3 ] = aColor.GetTransparency();
152 
153  return aRes;
154  }
155 
156  rendering::IntegerBitmapLayout CanvasBitmapHelper::getMemoryLayout() const
157  {
158  if( !mpOutDevProvider.get() )
159  return rendering::IntegerBitmapLayout(); // we're disposed
160 
161  rendering::IntegerBitmapLayout aBitmapLayout( ::canvas::tools::getStdMemoryLayout(getSize()) );
162  if ( !hasAlpha() )
163  aBitmapLayout.ColorSpace = canvas::tools::getStdColorSpaceWithoutAlpha();
164 
165  return aBitmapLayout;
166  }
167 
169  {
170  if( !mpBackBuffer )
171  return BitmapEx(); // we're disposed
172  else
173  return mpBackBuffer->getBitmapReference();
174  }
175 
176 }
177 
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< OutDevProvider > OutDevProviderSharedPtr
uno::Reference< rendering::XIntegerBitmapColorSpace > const & getStdColorSpaceWithoutAlpha()
Return a color space for a default RGB integer format.
css::uno::Sequence< sal_Int8 > getPixel(css::rendering::IntegerBitmapLayout &bitmapLayout, const css::geometry::IntegerPoint2D &pos)
long Height() const
signed char sal_Int8
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
BitmapBackBufferSharedPtr mpBackBuffer
void init(css::rendering::XGraphicDevice &rDevice, const OutDevProviderSharedPtr &rOutDev, bool bProtect, bool bHaveAlpha)
Initialize canvas helper.
void disposing()
Release all references.
geometry::IntegerSize2D integerSize2DFromSize(const Size &rSize)
OutDevProviderSharedPtr mpOutDevProvider
Rendering to this outdev does not preserve its state.
css::rendering::XGraphicDevice * mpDevice
Phyical output device.
css::geometry::IntegerSize2D getSize() const
void init(const BitmapEx &rBitmap, css::rendering::XGraphicDevice &rDevice, const OutDevProviderSharedPtr &rOutDevProvider)
Set a new bitmap on this helper.
rendering::IntegerBitmapLayout getStdMemoryLayout(const geometry::IntegerSize2D &rBmpSize)
OutDevProviderSharedPtr mpOutDevReference
uno::Sequence< sal_Int8 > CanvasExtractBitmapData(BitmapEx const &rBitmapEx, const geometry::IntegerRectangle2D &rect)
css::uno::Sequence< sal_Int8 > getData(css::rendering::IntegerBitmapLayout &bitmapLayout, const css::geometry::IntegerRectangle2D &rect)
#define ENSURE_ARG_OR_THROW(c, m)
#define ENSURE_OR_THROW(c, m)
css::uno::Reference< css::rendering::XBitmap > getScaledBitmap(const css::geometry::RealSize2D &newSize, bool beFast)
Size sizeFromRealSize2D(const geometry::RealSize2D &rSize)
#define SAL_INFO(area, stream)
css::rendering::IntegerBitmapLayout getMemoryLayout() const