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