Line data Source code
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 :
21 : #include <canvas/debug.hxx>
22 : #include <tools/diagnose_ex.h>
23 :
24 : #include <rtl/math.hxx>
25 :
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>
35 :
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>
41 :
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>
49 :
50 : #include <canvas/canvastools.hxx>
51 : #include <o3tl/compat_functional.hxx>
52 :
53 : #include "impltools.hxx"
54 : #include "canvasbitmap.hxx"
55 :
56 : #include <numeric>
57 :
58 :
59 : using namespace ::com::sun::star;
60 :
61 : namespace vclcanvas
62 : {
63 : namespace tools
64 : {
65 0 : ::BitmapEx bitmapExFromXBitmap( const uno::Reference< rendering::XBitmap >& xBitmap )
66 : {
67 : // TODO(F3): CanvasCustomSprite should also be tunnelled
68 : // through (also implements XIntegerBitmap interface)
69 0 : CanvasBitmap* pBitmapImpl = dynamic_cast< CanvasBitmap* >( xBitmap.get() );
70 :
71 0 : if( pBitmapImpl )
72 : {
73 0 : return pBitmapImpl->getBitmap();
74 : }
75 : else
76 : {
77 0 : SpriteCanvas* pCanvasImpl = dynamic_cast< SpriteCanvas* >( xBitmap.get() );
78 0 : if( pCanvasImpl && pCanvasImpl->getBackBuffer() )
79 : {
80 : // TODO(F3): mind the plain Canvas impl. Consolidate with CWS canvas05
81 0 : const ::OutputDevice& rDev( pCanvasImpl->getBackBuffer()->getOutDev() );
82 0 : const ::Point aEmptyPoint;
83 : return rDev.GetBitmapEx( aEmptyPoint,
84 0 : rDev.GetOutputSizePixel() );
85 : }
86 :
87 : // TODO(F2): add support for floating point bitmap formats
88 : uno::Reference< rendering::XIntegerReadOnlyBitmap > xIntBmp(
89 0 : xBitmap, uno::UNO_QUERY_THROW );
90 :
91 0 : ::BitmapEx aBmpEx = ::vcl::unotools::bitmapExFromXBitmap( xIntBmp );
92 0 : if( !!aBmpEx )
93 0 : return aBmpEx;
94 :
95 : // TODO(F1): extract pixel from XBitmap interface
96 0 : ENSURE_OR_THROW( false,
97 0 : "bitmapExFromXBitmap(): could not extract bitmap" );
98 : }
99 :
100 : return ::BitmapEx();
101 : }
102 :
103 0 : bool setupFontTransform( ::Point& o_rPoint,
104 : ::Font& io_rVCLFont,
105 : const rendering::ViewState& rViewState,
106 : const rendering::RenderState& rRenderState,
107 : ::OutputDevice& rOutDev )
108 : {
109 0 : ::basegfx::B2DHomMatrix aMatrix;
110 :
111 : ::canvas::tools::mergeViewAndRenderTransform(aMatrix,
112 : rViewState,
113 0 : rRenderState);
114 :
115 0 : ::basegfx::B2DTuple aScale;
116 0 : ::basegfx::B2DTuple aTranslate;
117 : double nRotate, nShearX;
118 :
119 0 : aMatrix.decompose( aScale, aTranslate, nRotate, nShearX );
120 :
121 : // query font metric _before_ tampering with width and height
122 0 : if( !::rtl::math::approxEqual(aScale.getX(), aScale.getY()) )
123 : {
124 : // retrieve true font width
125 0 : const sal_Int32 nFontWidth( rOutDev.GetFontMetric( io_rVCLFont ).GetWidth() );
126 :
127 0 : const sal_Int32 nScaledFontWidth( ::basegfx::fround(nFontWidth * aScale.getX()) );
128 :
129 0 : if( !nScaledFontWidth )
130 : {
131 : // scale is smaller than one pixel - disable text
132 : // output altogether
133 0 : return false;
134 : }
135 :
136 0 : io_rVCLFont.SetWidth( nScaledFontWidth );
137 : }
138 :
139 0 : if( !::rtl::math::approxEqual(aScale.getY(), 1.0) )
140 : {
141 0 : const sal_Int32 nFontHeight( io_rVCLFont.GetHeight() );
142 0 : io_rVCLFont.SetHeight( ::basegfx::fround(nFontHeight * aScale.getY()) );
143 : }
144 :
145 0 : io_rVCLFont.SetOrientation( static_cast< short >( ::basegfx::fround(-fmod(nRotate, 2*M_PI)*(1800.0/M_PI)) ) );
146 :
147 : // TODO(F2): Missing functionality in VCL: shearing
148 0 : o_rPoint.X() = ::basegfx::fround(aTranslate.getX());
149 0 : o_rPoint.Y() = ::basegfx::fround(aTranslate.getY());
150 :
151 0 : return true;
152 : }
153 :
154 0 : bool isRectangle( const PolyPolygon& rPolyPoly )
155 : {
156 : // exclude some cheap cases first
157 0 : if( rPolyPoly.Count() != 1 )
158 0 : return false;
159 :
160 0 : const ::Polygon& rPoly( rPolyPoly[0] );
161 :
162 0 : sal_uInt16 nCount( rPoly.GetSize() );
163 0 : if( nCount < 4 )
164 0 : return false;
165 :
166 : // delegate to basegfx
167 0 : return ::basegfx::tools::isRectangle( rPoly.getB2DPolygon() );
168 : }
169 :
170 :
171 : // VCL-Canvas related
172 :
173 :
174 0 : ::Point mapRealPoint2D( const geometry::RealPoint2D& rPoint,
175 : const rendering::ViewState& rViewState,
176 : const rendering::RenderState& rRenderState )
177 : {
178 0 : ::basegfx::B2DPoint aPoint( ::basegfx::unotools::b2DPointFromRealPoint2D(rPoint) );
179 :
180 0 : ::basegfx::B2DHomMatrix aMatrix;
181 : aPoint *= ::canvas::tools::mergeViewAndRenderTransform(aMatrix,
182 : rViewState,
183 0 : rRenderState);
184 :
185 0 : return ::vcl::unotools::pointFromB2DPoint( aPoint );
186 : }
187 :
188 0 : ::PolyPolygon mapPolyPolygon( const ::basegfx::B2DPolyPolygon& rPoly,
189 : const rendering::ViewState& rViewState,
190 : const rendering::RenderState& rRenderState )
191 : {
192 0 : ::basegfx::B2DHomMatrix aMatrix;
193 : ::canvas::tools::mergeViewAndRenderTransform(aMatrix,
194 : rViewState,
195 0 : rRenderState);
196 :
197 0 : ::basegfx::B2DPolyPolygon aTemp( rPoly );
198 :
199 0 : aTemp.transform( aMatrix );
200 :
201 0 : return ::PolyPolygon( aTemp );
202 : }
203 :
204 0 : ::BitmapEx transformBitmap( const BitmapEx& rBitmap,
205 : const ::basegfx::B2DHomMatrix& rTransform,
206 : const uno::Sequence< double >& rDeviceColor,
207 : ModulationMode eModulationMode )
208 : {
209 : SAL_INFO( "canvas.vcl", "::vclcanvas::tools::transformBitmap()" );
210 : SAL_INFO( "canvas.vcl", "::vclcanvas::tools::transformBitmap: 0x" << std::hex << &rBitmap );
211 :
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 0 : const Size aBmpSize( rBitmap.GetSizePixel() );
217 0 : ::basegfx::B2DRectangle aDestRect;
218 :
219 0 : bool bCopyBack( false );
220 :
221 : // calc effective transformation for bitmap
222 : const ::basegfx::B2DRectangle aSrcRect( 0, 0,
223 0 : aBmpSize.Width(),
224 0 : aBmpSize.Height() );
225 : ::canvas::tools::calcTransformedRectBounds( aDestRect,
226 : aSrcRect,
227 0 : rTransform );
228 :
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 0 : ::basegfx::B2DHomMatrix aLocalTransform;
234 : ::canvas::tools::calcRectToOriginTransform( aLocalTransform,
235 : aSrcRect,
236 0 : rTransform );
237 :
238 0 : const bool bModulateColors( eModulationMode == MODULATE_WITH_DEVICECOLOR &&
239 0 : rDeviceColor.getLength() > 2 );
240 0 : const double nRedModulation( bModulateColors ? rDeviceColor[0] : 1.0 );
241 0 : const double nGreenModulation( bModulateColors ? rDeviceColor[1] : 1.0 );
242 0 : const double nBlueModulation( bModulateColors ? rDeviceColor[2] : 1.0 );
243 0 : const double nAlphaModulation( bModulateColors && rDeviceColor.getLength() > 3 ?
244 0 : rDeviceColor[3] : 1.0 );
245 :
246 0 : Bitmap aSrcBitmap( rBitmap.GetBitmap() );
247 0 : Bitmap aSrcAlpha;
248 :
249 : // differentiate mask and alpha channel (on-off
250 : // vs. multi-level transparency)
251 0 : if( rBitmap.IsTransparent() )
252 : {
253 0 : if( rBitmap.IsAlpha() )
254 0 : aSrcAlpha = rBitmap.GetAlpha().GetBitmap();
255 : else
256 0 : aSrcAlpha = rBitmap.GetMask();
257 : }
258 :
259 0 : Bitmap::ScopedReadAccess pReadAccess( aSrcBitmap );
260 0 : Bitmap::ScopedReadAccess pAlphaReadAccess( rBitmap.IsTransparent() ?
261 : aSrcAlpha.AcquireReadAccess() :
262 : (BitmapReadAccess*)NULL,
263 0 : aSrcAlpha );
264 :
265 0 : if( pReadAccess.get() == NULL ||
266 0 : (pAlphaReadAccess.get() == NULL && rBitmap.IsTransparent()) )
267 : {
268 : // TODO(E2): Error handling!
269 0 : ENSURE_OR_THROW( false,
270 : "transformBitmap(): could not access source bitmap" );
271 : }
272 :
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];
277 :
278 0 : if( rBitmap.IsTransparent() )
279 : {
280 0 : if( rBitmap.IsAlpha() )
281 : {
282 : // source already has alpha channel - 1:1 mapping,
283 : // i.e. aAlphaMap[0]=0,...,aAlphaMap[255]=255.
284 0 : ::o3tl::iota( aAlphaMap, &aAlphaMap[256], 0 );
285 : }
286 : else
287 : {
288 : // mask transparency - determine used palette colors
289 0 : const BitmapColor& rCol0( pAlphaReadAccess->GetPaletteColor( 0 ) );
290 0 : const BitmapColor& rCol1( pAlphaReadAccess->GetPaletteColor( 1 ) );
291 :
292 : // shortcut for true luminance calculation
293 : // (assumes that palette is grey-level)
294 0 : aAlphaMap[0] = rCol0.GetRed();
295 0 : aAlphaMap[1] = rCol1.GetRed();
296 : }
297 : }
298 : // else: mapping table is not used
299 :
300 : const Size aDestBmpSize( ::basegfx::fround( aDestRect.getWidth() ),
301 0 : ::basegfx::fround( aDestRect.getHeight() ) );
302 :
303 0 : if( aDestBmpSize.Width() == 0 || aDestBmpSize.Height() == 0 )
304 0 : return BitmapEx();
305 :
306 0 : Bitmap aDstBitmap( aDestBmpSize, aSrcBitmap.GetBitCount(), &pReadAccess->GetPalette() );
307 0 : Bitmap aDstAlpha( AlphaMask( aDestBmpSize ).GetBitmap() );
308 :
309 : {
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 0 : Bitmap::ScopedWriteAccess pWriteAccess( aDstBitmap );
316 0 : Bitmap::ScopedWriteAccess pAlphaWriteAccess( aDstAlpha );
317 :
318 :
319 0 : if( pWriteAccess.get() != NULL &&
320 0 : pAlphaWriteAccess.get() != NULL &&
321 0 : rTransform.isInvertible() )
322 : {
323 : // we're doing inverse mapping here, i.e. mapping
324 : // points from the destination bitmap back to the
325 : // source
326 0 : ::basegfx::B2DHomMatrix aTransform( aLocalTransform );
327 0 : aTransform.invert();
328 :
329 : // for the time being, always read as ARGB
330 0 : for( int y=0; y<aDestBmpSize.Height(); ++y )
331 : {
332 0 : if( bModulateColors )
333 : {
334 : // TODO(P2): Have different branches for
335 : // alpha-only modulation (color
336 : // modulations eq. 1.0)
337 :
338 : // modulate all color channels with given
339 : // values
340 :
341 : // differentiate mask and alpha channel (on-off
342 : // vs. multi-level transparency)
343 0 : if( rBitmap.IsTransparent() )
344 : {
345 : // Handling alpha and mask just the same...
346 0 : for( int x=0; x<aDestBmpSize.Width(); ++x )
347 : {
348 0 : ::basegfx::B2DPoint aPoint(x,y);
349 0 : aPoint *= aTransform;
350 :
351 0 : const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
352 0 : const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
353 0 : if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
354 0 : nSrcY < 0 || nSrcY >= aBmpSize.Height() )
355 : {
356 0 : pAlphaWriteAccess->SetPixel( y, x, BitmapColor(255) );
357 : }
358 : else
359 : {
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 0 : const sal_uInt8 cMappedAlphaIdx = aAlphaMap[ pAlphaReadAccess->GetPixelIndex( nSrcY, nSrcX ) ];
368 0 : const sal_uInt8 cModulatedAlphaIdx = 255U - static_cast<sal_uInt8>( nAlphaModulation* (255U - cMappedAlphaIdx) + .5 );
369 0 : pAlphaWriteAccess->SetPixelIndex( y, x, cModulatedAlphaIdx );
370 0 : BitmapColor aColor( pReadAccess->GetPixel( nSrcY, nSrcX ) );
371 :
372 : aColor.SetRed(
373 : static_cast<sal_uInt8>(
374 0 : nRedModulation *
375 0 : aColor.GetRed() + .5 ));
376 : aColor.SetGreen(
377 : static_cast<sal_uInt8>(
378 0 : nGreenModulation *
379 0 : aColor.GetGreen() + .5 ));
380 : aColor.SetBlue(
381 : static_cast<sal_uInt8>(
382 0 : nBlueModulation *
383 0 : aColor.GetBlue() + .5 ));
384 :
385 : pWriteAccess->SetPixel( y, x,
386 0 : aColor );
387 : }
388 0 : }
389 : }
390 : else
391 : {
392 0 : for( int x=0; x<aDestBmpSize.Width(); ++x )
393 : {
394 0 : ::basegfx::B2DPoint aPoint(x,y);
395 0 : aPoint *= aTransform;
396 :
397 0 : const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
398 0 : const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
399 0 : if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
400 0 : nSrcY < 0 || nSrcY >= aBmpSize.Height() )
401 : {
402 0 : pAlphaWriteAccess->SetPixel( y, x, BitmapColor(255) );
403 : }
404 : else
405 : {
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 0 : nAlphaModulation*255.0
418 0 : + .5 ) ) );
419 :
420 : BitmapColor aColor( pReadAccess->GetPixel( nSrcY,
421 0 : nSrcX ) );
422 :
423 : aColor.SetRed(
424 : static_cast<sal_uInt8>(
425 0 : nRedModulation *
426 0 : aColor.GetRed() + .5 ));
427 : aColor.SetGreen(
428 : static_cast<sal_uInt8>(
429 0 : nGreenModulation *
430 0 : aColor.GetGreen() + .5 ));
431 : aColor.SetBlue(
432 : static_cast<sal_uInt8>(
433 0 : nBlueModulation *
434 0 : aColor.GetBlue() + .5 ));
435 :
436 : pWriteAccess->SetPixel( y, x,
437 0 : aColor );
438 : }
439 0 : }
440 : }
441 : }
442 : else
443 : {
444 : // differentiate mask and alpha channel (on-off
445 : // vs. multi-level transparency)
446 0 : if( rBitmap.IsTransparent() )
447 : {
448 : // Handling alpha and mask just the same...
449 0 : for( int x=0; x<aDestBmpSize.Width(); ++x )
450 : {
451 0 : ::basegfx::B2DPoint aPoint(x,y);
452 0 : aPoint *= aTransform;
453 :
454 0 : const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
455 0 : const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
456 0 : if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
457 0 : nSrcY < 0 || nSrcY >= aBmpSize.Height() )
458 : {
459 0 : pAlphaWriteAccess->SetPixelIndex( y, x, 255 );
460 : }
461 : else
462 : {
463 0 : const sal_uInt8 cAlphaIdx = pAlphaReadAccess->GetPixelIndex( nSrcY, nSrcX );
464 0 : pAlphaWriteAccess->SetPixelIndex( y, x, aAlphaMap[ cAlphaIdx ] );
465 0 : pWriteAccess->SetPixel( y, x, pReadAccess->GetPixel( nSrcY, nSrcX ) );
466 : }
467 0 : }
468 : }
469 : else
470 : {
471 0 : for( int x=0; x<aDestBmpSize.Width(); ++x )
472 : {
473 0 : ::basegfx::B2DPoint aPoint(x,y);
474 0 : aPoint *= aTransform;
475 :
476 0 : const int nSrcX( ::basegfx::fround( aPoint.getX() ) );
477 0 : const int nSrcY( ::basegfx::fround( aPoint.getY() ) );
478 0 : if( nSrcX < 0 || nSrcX >= aBmpSize.Width() ||
479 0 : nSrcY < 0 || nSrcY >= aBmpSize.Height() )
480 : {
481 0 : pAlphaWriteAccess->SetPixel( y, x, BitmapColor(255) );
482 : }
483 : else
484 : {
485 0 : pAlphaWriteAccess->SetPixel( y, x, BitmapColor(0) );
486 : pWriteAccess->SetPixel( y, x, pReadAccess->GetPixel( nSrcY,
487 0 : nSrcX ) );
488 : }
489 0 : }
490 : }
491 : }
492 : }
493 :
494 0 : bCopyBack = true;
495 : }
496 : else
497 : {
498 : // TODO(E2): Error handling!
499 0 : ENSURE_OR_THROW( false,
500 : "transformBitmap(): could not access bitmap" );
501 0 : }
502 : }
503 :
504 0 : if( bCopyBack )
505 0 : return BitmapEx( aDstBitmap, AlphaMask( aDstAlpha ) );
506 : else
507 0 : return BitmapEx();
508 : }
509 : }
510 0 : }
511 :
512 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|