/src/libreoffice/canvas/source/vcl/canvasbitmaphelper.cxx
Line | Count | Source |
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 <comphelper/diagnose_ex.hxx> |
25 | | #include <vcl/bitmap.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 | | { |
37 | | CanvasBitmapHelper::CanvasBitmapHelper() |
38 | 0 | { |
39 | 0 | } |
40 | | |
41 | | void CanvasBitmapHelper::init( const ::Bitmap& rBitmap, |
42 | | rendering::XGraphicDevice& rDevice, |
43 | | const OutDevProviderSharedPtr& rOutDevReference ) |
44 | 0 | { |
45 | 0 | mpOutDevReference = rOutDevReference; |
46 | 0 | 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 | 0 | CanvasHelper::init( rDevice, |
52 | 0 | mpBackBuffer, |
53 | 0 | false, |
54 | 0 | rBitmap.HasAlpha() ); |
55 | 0 | } |
56 | | |
57 | | void CanvasBitmapHelper::disposing() |
58 | 0 | { |
59 | 0 | mpBackBuffer.reset(); |
60 | 0 | mpOutDevReference.reset(); |
61 | | |
62 | | // forward to base class |
63 | 0 | CanvasHelper::disposing(); |
64 | 0 | } |
65 | | |
66 | | geometry::IntegerSize2D CanvasBitmapHelper::getSize() const |
67 | 0 | { |
68 | 0 | if( !mpBackBuffer ) |
69 | 0 | return geometry::IntegerSize2D(); |
70 | | |
71 | 0 | return vcl::unotools::integerSize2DFromSize( mpBackBuffer->getBitmapSizePixel() ); |
72 | 0 | } |
73 | | |
74 | | void CanvasBitmapHelper::clear() |
75 | 0 | { |
76 | | // are we disposed? |
77 | 0 | if( mpBackBuffer ) |
78 | 0 | mpBackBuffer->clear(); // alpha vdev needs special treatment |
79 | 0 | } |
80 | | |
81 | | uno::Reference< rendering::XBitmap > CanvasBitmapHelper::getScaledBitmap( const geometry::RealSize2D& newSize, |
82 | | bool beFast ) |
83 | 0 | { |
84 | 0 | ENSURE_OR_THROW( mpDevice, |
85 | 0 | "disposed CanvasHelper" ); |
86 | |
|
87 | 0 | SAL_INFO( "canvas.vcl", "::vclcanvas::CanvasBitmapHelper::getScaledBitmap()" ); |
88 | | |
89 | 0 | if( !mpBackBuffer || mpDevice ) |
90 | 0 | return uno::Reference< rendering::XBitmap >(); // we're disposed |
91 | | |
92 | 0 | ::Bitmap aRes( mpBackBuffer->getBitmapReference() ); |
93 | |
|
94 | 0 | aRes.Scale( vcl::unotools::sizeFromRealSize2D(newSize), |
95 | 0 | beFast ? BmpScaleFlag::Default : BmpScaleFlag::BestQuality ); |
96 | |
|
97 | 0 | return uno::Reference< rendering::XBitmap >( |
98 | 0 | new CanvasBitmap( aRes, *mpDevice, mpOutDevReference ) ); |
99 | 0 | } |
100 | | |
101 | | uno::Sequence< sal_Int8 > CanvasBitmapHelper::getData( rendering::IntegerBitmapLayout& rLayout, |
102 | | const geometry::IntegerRectangle2D& rect ) |
103 | 0 | { |
104 | 0 | SAL_INFO( "canvas.vcl", "::vclcanvas::CanvasBitmapHelper::getData()" ); |
105 | | |
106 | 0 | if( !mpBackBuffer ) |
107 | 0 | return uno::Sequence< sal_Int8 >(); // we're disposed |
108 | | |
109 | 0 | rLayout = getMemoryLayout(); |
110 | | |
111 | | // TODO(F1): Support more formats. |
112 | 0 | const Size aBmpSize( mpBackBuffer->getBitmapReference().GetSizePixel() ); |
113 | |
|
114 | 0 | rLayout.ScanLines = aBmpSize.Height(); |
115 | 0 | rLayout.ScanLineBytes = aBmpSize.Width()*4; |
116 | 0 | rLayout.ScanLineStride = rLayout.ScanLineBytes; |
117 | |
|
118 | 0 | uno::Sequence< sal_Int8 > aRes = vcl::bitmap::CanvasExtractBitmapData(mpBackBuffer->getBitmapReference(), rect); |
119 | 0 | return aRes; |
120 | 0 | } |
121 | | |
122 | | uno::Sequence< sal_Int8 > CanvasBitmapHelper::getPixel( rendering::IntegerBitmapLayout& rLayout, |
123 | | const geometry::IntegerPoint2D& pos ) |
124 | 0 | { |
125 | 0 | SAL_INFO( "canvas.vcl", "::vclcanvas::CanvasBitmapHelper::getPixel()" ); |
126 | | |
127 | 0 | if( !mpBackBuffer ) |
128 | 0 | return uno::Sequence< sal_Int8 >(); // we're disposed |
129 | | |
130 | 0 | rLayout = getMemoryLayout(); |
131 | 0 | rLayout.ScanLines = 1; |
132 | 0 | rLayout.ScanLineBytes = 4; |
133 | 0 | rLayout.ScanLineStride = rLayout.ScanLineBytes; |
134 | |
|
135 | 0 | const Size aBmpSize( mpBackBuffer->getBitmapReference().GetSizePixel() ); |
136 | |
|
137 | 0 | ENSURE_ARG_OR_THROW( pos.X >= 0 && pos.X < aBmpSize.Width(), |
138 | 0 | "X coordinate out of bounds" ); |
139 | 0 | ENSURE_ARG_OR_THROW( pos.Y >= 0 && pos.Y < aBmpSize.Height(), |
140 | 0 | "Y coordinate out of bounds" ); |
141 | |
|
142 | 0 | ::Color aColor = mpBackBuffer->getBitmapReference().GetPixelColor(pos.X, pos.Y); |
143 | |
|
144 | 0 | uno::Sequence< sal_Int8 > aRes( 4 ); |
145 | 0 | sal_Int8* pRes = aRes.getArray(); |
146 | 0 | pRes[ 0 ] = aColor.GetRed(); |
147 | 0 | pRes[ 1 ] = aColor.GetGreen(); |
148 | 0 | pRes[ 2 ] = aColor.GetBlue(); |
149 | 0 | pRes[ 3 ] = aColor.GetAlpha(); |
150 | |
|
151 | 0 | return aRes; |
152 | 0 | } |
153 | | |
154 | | rendering::IntegerBitmapLayout CanvasBitmapHelper::getMemoryLayout() const |
155 | 0 | { |
156 | 0 | if( !mpOutDevProvider ) |
157 | 0 | return rendering::IntegerBitmapLayout(); // we're disposed |
158 | | |
159 | 0 | rendering::IntegerBitmapLayout aBitmapLayout( ::canvastools::getStdMemoryLayout(getSize()) ); |
160 | 0 | if ( !hasAlpha() ) |
161 | 0 | aBitmapLayout.ColorSpace = canvastools::getStdColorSpaceWithoutAlpha(); |
162 | |
|
163 | 0 | return aBitmapLayout; |
164 | 0 | } |
165 | | |
166 | | ::Bitmap CanvasBitmapHelper::getBitmap() const |
167 | 0 | { |
168 | 0 | if( !mpBackBuffer ) |
169 | 0 | return ::Bitmap(); // we're disposed |
170 | 0 | else |
171 | 0 | return mpBackBuffer->getBitmapReference(); |
172 | 0 | } |
173 | | |
174 | | } |
175 | | |
176 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |