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