fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / canvas / source / vcl / impltools.cxx
blob0dce58095324b6c46b7c1eae85f55c128fdc5aad
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <canvas/debug.hxx>
22 #include <tools/diagnose_ex.h>
24 #include <rtl/math.hxx>
26 #include <com/sun/star/geometry/RealSize2D.hpp>
27 #include <com/sun/star/geometry/RealPoint2D.hpp>
28 #include <com/sun/star/geometry/RealRectangle2D.hpp>
29 #include <com/sun/star/rendering/RenderState.hpp>
30 #include <com/sun/star/rendering/XCanvas.hpp>
31 #include <com/sun/star/rendering/XBitmap.hpp>
32 #include <com/sun/star/rendering/XPolyPolygon2D.hpp>
33 #include <com/sun/star/geometry/RealBezierSegment2D.hpp>
34 #include <com/sun/star/rendering/XIntegerBitmap.hpp>
36 #include <vcl/salbtype.hxx>
37 #include <vcl/bmpacc.hxx>
38 #include <vcl/bitmapex.hxx>
39 #include <vcl/metric.hxx>
40 #include <vcl/canvastools.hxx>
42 #include <basegfx/point/b2dpoint.hxx>
43 #include <basegfx/tuple/b2dtuple.hxx>
44 #include <basegfx/polygon/b2dpolygontools.hxx>
45 #include <basegfx/range/b2drectangle.hxx>
46 #include <basegfx/matrix/b2dhommatrix.hxx>
47 #include <basegfx/tools/canvastools.hxx>
48 #include <basegfx/numeric/ftools.hxx>
50 #include <canvas/canvastools.hxx>
51 #include <o3tl/compat_functional.hxx>
53 #include "impltools.hxx"
54 #include "canvasbitmap.hxx"
56 #include <numeric>
59 using namespace ::com::sun::star;
61 namespace vclcanvas
63 namespace tools
65 ::BitmapEx bitmapExFromXBitmap( const uno::Reference< rendering::XBitmap >& xBitmap )
67 // TODO(F3): CanvasCustomSprite should also be tunnelled
68 // through (also implements XIntegerBitmap interface)
69 CanvasBitmap* pBitmapImpl = dynamic_cast< CanvasBitmap* >( xBitmap.get() );
71 if( pBitmapImpl )
73 return pBitmapImpl->getBitmap();
75 else
77 SpriteCanvas* pCanvasImpl = dynamic_cast< SpriteCanvas* >( xBitmap.get() );
78 if( pCanvasImpl && pCanvasImpl->getBackBuffer() )
80 // TODO(F3): mind the plain Canvas impl. Consolidate with CWS canvas05
81 const ::OutputDevice& rDev( pCanvasImpl->getBackBuffer()->getOutDev() );
82 const ::Point aEmptyPoint;
83 return rDev.GetBitmapEx( aEmptyPoint,
84 rDev.GetOutputSizePixel() );
87 // TODO(F2): add support for floating point bitmap formats
88 uno::Reference< rendering::XIntegerReadOnlyBitmap > xIntBmp(
89 xBitmap, uno::UNO_QUERY_THROW );
91 ::BitmapEx aBmpEx = ::vcl::unotools::bitmapExFromXBitmap( xIntBmp );
92 if( !!aBmpEx )
93 return aBmpEx;
95 // TODO(F1): extract pixel from XBitmap interface
96 ENSURE_OR_THROW( false,
97 "bitmapExFromXBitmap(): could not extract bitmap" );
100 return ::BitmapEx();
103 bool setupFontTransform( ::Point& o_rPoint,
104 ::Font& io_rVCLFont,
105 const rendering::ViewState& rViewState,
106 const rendering::RenderState& rRenderState,
107 ::OutputDevice& rOutDev )
109 ::basegfx::B2DHomMatrix aMatrix;
111 ::canvas::tools::mergeViewAndRenderTransform(aMatrix,
112 rViewState,
113 rRenderState);
115 ::basegfx::B2DTuple aScale;
116 ::basegfx::B2DTuple aTranslate;
117 double nRotate, nShearX;
119 aMatrix.decompose( aScale, aTranslate, nRotate, nShearX );
121 // query font metric _before_ tampering with width and height
122 if( !::rtl::math::approxEqual(aScale.getX(), aScale.getY()) )
124 // retrieve true font width
125 const sal_Int32 nFontWidth( rOutDev.GetFontMetric( io_rVCLFont ).GetWidth() );
127 const sal_Int32 nScaledFontWidth( ::basegfx::fround(nFontWidth * aScale.getX()) );
129 if( !nScaledFontWidth )
131 // scale is smaller than one pixel - disable text
132 // output altogether
133 return false;
136 io_rVCLFont.SetWidth( nScaledFontWidth );
139 if( !::rtl::math::approxEqual(aScale.getY(), 1.0) )
141 const sal_Int32 nFontHeight( io_rVCLFont.GetHeight() );
142 io_rVCLFont.SetHeight( ::basegfx::fround(nFontHeight * aScale.getY()) );
145 io_rVCLFont.SetOrientation( static_cast< short >( ::basegfx::fround(-fmod(nRotate, 2*M_PI)*(1800.0/M_PI)) ) );
147 // TODO(F2): Missing functionality in VCL: shearing
148 o_rPoint.X() = ::basegfx::fround(aTranslate.getX());
149 o_rPoint.Y() = ::basegfx::fround(aTranslate.getY());
151 return true;
154 bool isRectangle( const PolyPolygon& rPolyPoly )
156 // exclude some cheap cases first
157 if( rPolyPoly.Count() != 1 )
158 return false;
160 const ::Polygon& rPoly( rPolyPoly[0] );
162 sal_uInt16 nCount( rPoly.GetSize() );
163 if( nCount < 4 )
164 return false;
166 // delegate to basegfx
167 return ::basegfx::tools::isRectangle( rPoly.getB2DPolygon() );
171 // VCL-Canvas related
172 //---------------------------------------------------------------------
174 ::Point mapRealPoint2D( const geometry::RealPoint2D& rPoint,
175 const rendering::ViewState& rViewState,
176 const rendering::RenderState& rRenderState )
178 ::basegfx::B2DPoint aPoint( ::basegfx::unotools::b2DPointFromRealPoint2D(rPoint) );
180 ::basegfx::B2DHomMatrix aMatrix;
181 aPoint *= ::canvas::tools::mergeViewAndRenderTransform(aMatrix,
182 rViewState,
183 rRenderState);
185 return ::vcl::unotools::pointFromB2DPoint( aPoint );
188 ::PolyPolygon mapPolyPolygon( const ::basegfx::B2DPolyPolygon& rPoly,
189 const rendering::ViewState& rViewState,
190 const rendering::RenderState& rRenderState )
192 ::basegfx::B2DHomMatrix aMatrix;
193 ::canvas::tools::mergeViewAndRenderTransform(aMatrix,
194 rViewState,
195 rRenderState);
197 ::basegfx::B2DPolyPolygon aTemp( rPoly );
199 aTemp.transform( aMatrix );
201 return ::PolyPolygon( aTemp );
204 ::BitmapEx transformBitmap( const BitmapEx& rBitmap,
205 const ::basegfx::B2DHomMatrix& rTransform,
206 const uno::Sequence< double >& rDeviceColor,
207 ModulationMode eModulationMode )
209 SAL_INFO( "canvas.vcl", "::vclcanvas::tools::transformBitmap()" );
210 SAL_INFO( "canvas.vcl", "::vclcanvas::tools::transformBitmap: 0x" << std::hex << &rBitmap );
212 // calc transformation and size of bitmap to be
213 // generated. Note, that the translational components are
214 // deleted from the transformation; this can be handled by
215 // an offset when painting the bitmap
216 const Size aBmpSize( rBitmap.GetSizePixel() );
217 ::basegfx::B2DRectangle aDestRect;
219 bool bCopyBack( false );
221 // calc effective transformation for bitmap
222 const ::basegfx::B2DRectangle aSrcRect( 0, 0,
223 aBmpSize.Width(),
224 aBmpSize.Height() );
225 ::canvas::tools::calcTransformedRectBounds( aDestRect,
226 aSrcRect,
227 rTransform );
229 // re-center bitmap, such that it's left, top border is
230 // aligned with (0,0). The method takes the given
231 // rectangle, and calculates a transformation that maps
232 // this rectangle unscaled to the origin.
233 ::basegfx::B2DHomMatrix aLocalTransform;
234 ::canvas::tools::calcRectToOriginTransform( aLocalTransform,
235 aSrcRect,
236 rTransform );
238 const bool bModulateColors( eModulationMode == MODULATE_WITH_DEVICECOLOR &&
239 rDeviceColor.getLength() > 2 );
240 const double nRedModulation( bModulateColors ? rDeviceColor[0] : 1.0 );
241 const double nGreenModulation( bModulateColors ? rDeviceColor[1] : 1.0 );
242 const double nBlueModulation( bModulateColors ? rDeviceColor[2] : 1.0 );
243 const double nAlphaModulation( bModulateColors && rDeviceColor.getLength() > 3 ?
244 rDeviceColor[3] : 1.0 );
246 Bitmap aSrcBitmap( rBitmap.GetBitmap() );
247 Bitmap aSrcAlpha;
249 // differentiate mask and alpha channel (on-off
250 // vs. multi-level transparency)
251 if( rBitmap.IsTransparent() )
253 if( rBitmap.IsAlpha() )
254 aSrcAlpha = rBitmap.GetAlpha().GetBitmap();
255 else
256 aSrcAlpha = rBitmap.GetMask();
259 Bitmap::ScopedReadAccess pReadAccess( aSrcBitmap );
260 Bitmap::ScopedReadAccess pAlphaReadAccess( rBitmap.IsTransparent() ?
261 aSrcAlpha.AcquireReadAccess() :
262 (BitmapReadAccess*)NULL,
263 aSrcAlpha );
265 if( pReadAccess.get() == NULL ||
266 (pAlphaReadAccess.get() == NULL && rBitmap.IsTransparent()) )
268 // TODO(E2): Error handling!
269 ENSURE_OR_THROW( false,
270 "transformBitmap(): could not access source bitmap" );
273 // mapping table, to translate pAlphaReadAccess' pixel
274 // values into destination alpha values (needed e.g. for
275 // paletted 1-bit masks).
276 sal_uInt8 aAlphaMap[256];
278 if( rBitmap.IsTransparent() )
280 if( rBitmap.IsAlpha() )
282 // source already has alpha channel - 1:1 mapping,
283 // i.e. aAlphaMap[0]=0,...,aAlphaMap[255]=255.
284 ::o3tl::iota( aAlphaMap, &aAlphaMap[256], 0 );
286 else
288 // mask transparency - determine used palette colors
289 const BitmapColor& rCol0( pAlphaReadAccess->GetPaletteColor( 0 ) );
290 const BitmapColor& rCol1( pAlphaReadAccess->GetPaletteColor( 1 ) );
292 // shortcut for true luminance calculation
293 // (assumes that palette is grey-level)
294 aAlphaMap[0] = rCol0.GetRed();
295 aAlphaMap[1] = rCol1.GetRed();
298 // else: mapping table is not used
300 const Size aDestBmpSize( ::basegfx::fround( aDestRect.getWidth() ),
301 ::basegfx::fround( aDestRect.getHeight() ) );
303 if( aDestBmpSize.Width() == 0 || aDestBmpSize.Height() == 0 )
304 return BitmapEx();
306 Bitmap aDstBitmap( aDestBmpSize, aSrcBitmap.GetBitCount(), &pReadAccess->GetPalette() );
307 Bitmap aDstAlpha( AlphaMask( aDestBmpSize ).GetBitmap() );
310 // just to be on the safe side: let the
311 // ScopedAccessors get destructed before
312 // copy-constructing the resulting bitmap. This will
313 // rule out the possibility that cached accessor data
314 // is not yet written back.
315 Bitmap::ScopedWriteAccess pWriteAccess( aDstBitmap );
316 Bitmap::ScopedWriteAccess pAlphaWriteAccess( aDstAlpha );
319 if( pWriteAccess.get() != NULL &&
320 pAlphaWriteAccess.get() != NULL &&
321 rTransform.isInvertible() )
323 // we're doing inverse mapping here, i.e. mapping
324 // points from the destination bitmap back to the
325 // source
326 ::basegfx::B2DHomMatrix aTransform( aLocalTransform );
327 aTransform.invert();
329 // for the time being, always read as ARGB
330 for( int y=0; y<aDestBmpSize.Height(); ++y )
332 if( bModulateColors )
334 // TODO(P2): Have different branches for
335 // alpha-only modulation (color
336 // modulations eq. 1.0)
338 // modulate all color channels with given
339 // values
341 // differentiate mask and alpha channel (on-off
342 // vs. multi-level transparency)
343 if( rBitmap.IsTransparent() )
345 // Handling alpha and mask just the same...
346 for( int x=0; x<aDestBmpSize.Width(); ++x )
348 ::basegfx::B2DPoint aPoint(x,y);
349 aPoint *= aTransform;
351 const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
352 const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
353 if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
354 nSrcY < 0 || nSrcY >= aBmpSize.Height() )
356 pAlphaWriteAccess->SetPixel( y, x, BitmapColor(255) );
358 else
360 // modulate alpha with
361 // nAlphaModulation. This is a
362 // little bit verbose, formula
363 // is 255 - (255-pixAlpha)*nAlphaModulation
364 // (invert 'alpha' pixel value,
365 // to get the standard alpha
366 // channel behaviour)
367 const sal_uInt8 cMappedAlphaIdx = aAlphaMap[ pAlphaReadAccess->GetPixelIndex( nSrcY, nSrcX ) ];
368 const sal_uInt8 cModulatedAlphaIdx = 255U - static_cast<sal_uInt8>( nAlphaModulation* (255U - cMappedAlphaIdx) + .5 );
369 pAlphaWriteAccess->SetPixelIndex( y, x, cModulatedAlphaIdx );
370 BitmapColor aColor( pReadAccess->GetPixel( nSrcY, nSrcX ) );
372 aColor.SetRed(
373 static_cast<sal_uInt8>(
374 nRedModulation *
375 aColor.GetRed() + .5 ));
376 aColor.SetGreen(
377 static_cast<sal_uInt8>(
378 nGreenModulation *
379 aColor.GetGreen() + .5 ));
380 aColor.SetBlue(
381 static_cast<sal_uInt8>(
382 nBlueModulation *
383 aColor.GetBlue() + .5 ));
385 pWriteAccess->SetPixel( y, x,
386 aColor );
390 else
392 for( int x=0; x<aDestBmpSize.Width(); ++x )
394 ::basegfx::B2DPoint aPoint(x,y);
395 aPoint *= aTransform;
397 const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
398 const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
399 if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
400 nSrcY < 0 || nSrcY >= aBmpSize.Height() )
402 pAlphaWriteAccess->SetPixel( y, x, BitmapColor(255) );
404 else
406 // modulate alpha with
407 // nAlphaModulation. This is a
408 // little bit verbose, formula
409 // is 255 - 255*nAlphaModulation
410 // (invert 'alpha' pixel value,
411 // to get the standard alpha
412 // channel behaviour)
413 pAlphaWriteAccess->SetPixel( y, x,
414 BitmapColor(
415 255U -
416 static_cast<sal_uInt8>(
417 nAlphaModulation*255.0
418 + .5 ) ) );
420 BitmapColor aColor( pReadAccess->GetPixel( nSrcY,
421 nSrcX ) );
423 aColor.SetRed(
424 static_cast<sal_uInt8>(
425 nRedModulation *
426 aColor.GetRed() + .5 ));
427 aColor.SetGreen(
428 static_cast<sal_uInt8>(
429 nGreenModulation *
430 aColor.GetGreen() + .5 ));
431 aColor.SetBlue(
432 static_cast<sal_uInt8>(
433 nBlueModulation *
434 aColor.GetBlue() + .5 ));
436 pWriteAccess->SetPixel( y, x,
437 aColor );
442 else
444 // differentiate mask and alpha channel (on-off
445 // vs. multi-level transparency)
446 if( rBitmap.IsTransparent() )
448 // Handling alpha and mask just the same...
449 for( int x=0; x<aDestBmpSize.Width(); ++x )
451 ::basegfx::B2DPoint aPoint(x,y);
452 aPoint *= aTransform;
454 const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
455 const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
456 if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
457 nSrcY < 0 || nSrcY >= aBmpSize.Height() )
459 pAlphaWriteAccess->SetPixelIndex( y, x, 255 );
461 else
463 const sal_uInt8 cAlphaIdx = pAlphaReadAccess->GetPixelIndex( nSrcY, nSrcX );
464 pAlphaWriteAccess->SetPixelIndex( y, x, aAlphaMap[ cAlphaIdx ] );
465 pWriteAccess->SetPixel( y, x, pReadAccess->GetPixel( nSrcY, nSrcX ) );
469 else
471 for( int x=0; x<aDestBmpSize.Width(); ++x )
473 ::basegfx::B2DPoint aPoint(x,y);
474 aPoint *= aTransform;
476 const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
477 const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
478 if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
479 nSrcY < 0 || nSrcY >= aBmpSize.Height() )
481 pAlphaWriteAccess->SetPixel( y, x, BitmapColor(255) );
483 else
485 pAlphaWriteAccess->SetPixel( y, x, BitmapColor(0) );
486 pWriteAccess->SetPixel( y, x, pReadAccess->GetPixel( nSrcY,
487 nSrcX ) );
494 bCopyBack = true;
496 else
498 // TODO(E2): Error handling!
499 ENSURE_OR_THROW( false,
500 "transformBitmap(): could not access bitmap" );
504 if( bCopyBack )
505 return BitmapEx( aDstBitmap, AlphaMask( aDstAlpha ) );
506 else
507 return BitmapEx();
512 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */