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