LibreOffice Module vcl (master) 1
BitmapEx.cxx
Go to the documentation of this file.
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/log.hxx>
21#include <rtl/math.hxx>
23#include <osl/diagnose.h>
26
27#include <vcl/ImageTree.hxx>
28#include <vcl/outdev.hxx>
29#include <vcl/alpha.hxx>
30#include <vcl/bitmapex.hxx>
31#include <vcl/svapp.hxx>
32#include <vcl/virdev.hxx>
33#include <vcl/settings.hxx>
35
36// BitmapEx::Create
37#include <salbmp.hxx>
38#include <salinst.hxx>
39#include <svdata.hxx>
42
43#include <o3tl/any.hxx>
44#include <tools/stream.hxx>
46
47#include <com/sun/star/beans/XFastPropertySet.hpp>
48
49#include <memory>
50
51using namespace ::com::sun::star;
52
54{
55}
56
57BitmapEx::BitmapEx( const BitmapEx& ) = default;
58
59BitmapEx::BitmapEx( const BitmapEx& rBitmapEx, Point aSrc, Size aSize )
60{
61 if( rBitmapEx.IsEmpty() || aSize.IsEmpty() )
62 return;
63
64 maBitmap = Bitmap(aSize, rBitmapEx.maBitmap.getPixelFormat());
65 SetSizePixel(aSize);
66 if( rBitmapEx.IsAlpha() )
68
69 tools::Rectangle aDestRect( Point( 0, 0 ), aSize );
70 tools::Rectangle aSrcRect( aSrc, aSize );
71 CopyPixel( aDestRect, aSrcRect, &rBitmapEx );
72}
73
75{
76 maBitmap = Bitmap(aSize, ePixelFormat);
77 SetSizePixel(aSize);
78}
79
80BitmapEx::BitmapEx( const OUString& rIconName )
81{
82 loadFromIconTheme( rIconName );
83}
84
85void BitmapEx::loadFromIconTheme( const OUString& rIconName )
86{
87 bool bSuccess;
88 OUString aIconTheme;
89
90 try
91 {
93 bSuccess = ImageTree::get().loadImage(rIconName, aIconTheme, *this, true);
94 }
95 catch (...)
96 {
97 bSuccess = false;
98 }
99
100 SAL_WARN_IF( !bSuccess, "vcl", "BitmapEx::BitmapEx(): could not load image " << rIconName << " via icon theme " << aIconTheme);
101}
102
104 maBitmap ( rBmp ),
105 maBitmapSize ( maBitmap.GetSizePixel() )
106{
107}
108
109BitmapEx::BitmapEx( const Bitmap& rBmp, const Bitmap& rMask ) :
110 maBitmap ( rBmp ),
111 maBitmapSize ( maBitmap.GetSizePixel() )
112{
113 if (rMask.IsEmpty())
114 return;
115
116 assert(typeid(rMask) != typeid(AlphaMask)
117 && "If this mask is actually an AlphaMask, then it will be inverted unnecessarily "
118 "and the alpha channel will be wrong");
119
121 {
122 maAlphaMask = rMask;
124 }
125 else if( rMask.getPixelFormat() == vcl::PixelFormat::N8_BPP )
126 {
127 BitmapEx aMaskEx(rMask);
129 aMaskEx.Invert();
130 maAlphaMask = aMaskEx.GetBitmap();
131 }
132 else
133 {
134 // convert to alpha bitmap
135 SAL_WARN( "vcl", "BitmapEx: forced mask to monochrome");
136 BitmapEx aMaskEx(rMask);
138 aMaskEx.Invert();
139 maAlphaMask = aMaskEx.GetBitmap();
140 }
141
143 {
144 OSL_ENSURE(false, "Mask size differs from Bitmap size, corrected Mask (!)");
146 }
147}
148
149BitmapEx::BitmapEx( const Bitmap& rBmp, const AlphaMask& rAlphaMask ) :
150 maBitmap ( rBmp ),
151 maAlphaMask ( rAlphaMask.ImplGetBitmap() ),
152 maBitmapSize ( maBitmap.GetSizePixel() )
153{
155 {
156 OSL_ENSURE(false, "Alpha size differs from Bitmap size, corrected Mask (!)");
158 }
159}
160
161
162BitmapEx::BitmapEx( const Bitmap& rBmp, const Color& rTransparentColor ) :
163 maBitmap ( rBmp ),
164 maBitmapSize ( maBitmap.GetSizePixel() )
165{
166 maAlphaMask = maBitmap.CreateAlphaMask( rTransparentColor );
167
169 "BitmapEx::BitmapEx(): size mismatch for bitmap and alpha mask.");
170}
171
172
173BitmapEx& BitmapEx::operator=( const BitmapEx& ) = default;
174
175bool BitmapEx::operator==( const BitmapEx& rBitmapEx ) const
176{
177 if (GetSizePixel() != rBitmapEx.GetSizePixel())
178 return false;
179
180 if (maBitmap != rBitmapEx.maBitmap)
181 return false;
182
183 return maAlphaMask == rBitmapEx.maAlphaMask;
184}
185
187{
188 return( maBitmap.IsEmpty() && maAlphaMask.IsEmpty() );
189}
190
192{
195}
196
198{
199 SetEmpty();
200}
201
203{
205}
206
208{
209 return !maAlphaMask.IsEmpty();
210}
211
213{
214 return maBitmap;
215}
216
217Bitmap BitmapEx::GetBitmap( Color aTransparentReplaceColor ) const
218{
219 Bitmap aRetBmp( maBitmap );
220
221 if( !maAlphaMask.IsEmpty() )
222 {
223 aRetBmp.Replace( maAlphaMask, aTransparentReplaceColor );
224 }
225
226 return aRetBmp;
227}
228
229sal_Int64 BitmapEx::GetSizeBytes() const
230{
231 sal_Int64 nSizeBytes = maBitmap.GetSizeBytes();
232
233 if( !maAlphaMask.IsEmpty() )
234 nSizeBytes += maAlphaMask.GetSizeBytes();
235
236 return nSizeBytes;
237}
238
240{
242
243 if( !maAlphaMask.IsEmpty() )
244 {
246 BCToBCOA( maAlphaMask.GetChecksum(), aBCOA );
247 nCrc = vcl_get_checksum( nCrc, aBCOA, BITMAP_CHECKSUM_SIZE );
248 }
249
250 return nCrc;
251}
252
253void BitmapEx::SetSizePixel(const Size& rNewSize)
254{
255 maBitmapSize = rNewSize;
256}
257
259{
260 bool bRet = false;
261
262 if (!maBitmap.IsEmpty())
263 bRet = maBitmap.Invert();
264
265 return bRet;
266}
267
269{
270 bool bRet = false;
271
272 if( !maBitmap.IsEmpty() )
273 {
274 bRet = maBitmap.Mirror( nMirrorFlags );
275
276 if( bRet && !maAlphaMask.IsEmpty() )
277 maAlphaMask.Mirror( nMirrorFlags );
278 }
279
280 return bRet;
281}
282
283bool BitmapEx::Scale( const double& rScaleX, const double& rScaleY, BmpScaleFlag nScaleFlag )
284{
285 bool bRet = false;
286
287 if( !maBitmap.IsEmpty() )
288 {
289 bRet = maBitmap.Scale( rScaleX, rScaleY, nScaleFlag );
290
291 if( bRet && !maAlphaMask.IsEmpty() )
292 {
293 maAlphaMask.Scale( rScaleX, rScaleY, nScaleFlag );
294 }
295
297
299 "BitmapEx::Scale(): size mismatch for bitmap and alpha mask." );
300 }
301
302 return bRet;
303}
304
305bool BitmapEx::Scale( const Size& rNewSize, BmpScaleFlag nScaleFlag )
306{
307 bool bRet;
308
310 && (rNewSize.Width() != GetSizePixel().Width()
311 || rNewSize.Height() != GetSizePixel().Height() ) )
312 {
313 bRet = Scale( static_cast<double>(rNewSize.Width()) / GetSizePixel().Width(),
314 static_cast<double>(rNewSize.Height()) / GetSizePixel().Height(),
315 nScaleFlag );
316 }
317 else
318 {
319 bRet = true;
320 }
321
322 return bRet;
323}
324
325bool BitmapEx::Rotate( Degree10 nAngle10, const Color& rFillColor )
326{
327 bool bRet = false;
328
329 if( !maBitmap.IsEmpty() )
330 {
331 const bool bTransRotate = ( COL_TRANSPARENT == rFillColor );
332
333 if( bTransRotate )
334 {
335 bRet = maBitmap.Rotate( nAngle10, COL_BLACK );
336
337 if( maAlphaMask.IsEmpty() )
338 {
340 maAlphaMask.Erase( 0 );
341 }
342
343 if( bRet && !maAlphaMask.IsEmpty() )
345 }
346 else
347 {
348 bRet = maBitmap.Rotate( nAngle10, rFillColor );
349
350 if( bRet && !maAlphaMask.IsEmpty() )
352 }
353
355
357 "BitmapEx::Rotate(): size mismatch for bitmap and alpha mask.");
358 }
359
360 return bRet;
361}
362
363bool BitmapEx::Crop( const tools::Rectangle& rRectPixel )
364{
365 bool bRet = false;
366
367 if( !maBitmap.IsEmpty() )
368 {
369 bRet = maBitmap.Crop( rRectPixel );
370
371 if( bRet && !maAlphaMask.IsEmpty() )
372 maAlphaMask.Crop( rRectPixel );
373
375
377 "BitmapEx::Crop(): size mismatch for bitmap and alpha mask.");
378 }
379
380 return bRet;
381}
382
384{
385 return !maBitmap.IsEmpty() && maBitmap.Convert( eConversion );
386}
387
388void BitmapEx::Expand( sal_Int32 nDX, sal_Int32 nDY, bool bExpandTransparent )
389{
390 bool bRet = false;
391
392 if( maBitmap.IsEmpty() )
393 return;
394
395 bRet = maBitmap.Expand( nDX, nDY );
396
397 if( bRet && !maAlphaMask.IsEmpty() )
398 {
399 Color aColor( bExpandTransparent ? COL_ALPHA_TRANSPARENT : COL_ALPHA_OPAQUE );
400 maAlphaMask.Expand( nDX, nDY, &aColor );
401 }
402
404
406 "BitmapEx::Expand(): size mismatch for bitmap and alpha mask.");
407}
408
409bool BitmapEx::CopyPixel( const tools::Rectangle& rRectDst, const tools::Rectangle& rRectSrc,
410 const BitmapEx* pBmpExSrc )
411{
412 bool bRet = false;
413
414 if( !pBmpExSrc || pBmpExSrc->IsEmpty() )
415 {
416 if( !maBitmap.IsEmpty() )
417 {
418 bRet = maBitmap.CopyPixel( rRectDst, rRectSrc );
419
420 if( bRet && !maAlphaMask.IsEmpty() )
421 maAlphaMask.CopyPixel( rRectDst, rRectSrc );
422 }
423 }
424 else
425 {
426 if( !maBitmap.IsEmpty() )
427 {
428 bRet = maBitmap.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->maBitmap );
429
430 if( bRet )
431 {
432 if( pBmpExSrc->IsAlpha() )
433 {
434 if( IsAlpha() )
435 // cast to use the optimized AlphaMask::CopyPixel
436 maAlphaMask.CopyPixel_AlphaOptimized( rRectDst, rRectSrc, &pBmpExSrc->maAlphaMask );
437 else
438 {
439 sal_uInt8 nTransparencyOpaque = 0;
440 std::optional<AlphaMask> pAlpha(std::in_place, GetSizePixel(), &nTransparencyOpaque);
441
442 maAlphaMask = pAlpha->ImplGetBitmap();
443 pAlpha.reset();
444 maAlphaMask.CopyPixel( rRectDst, rRectSrc, &pBmpExSrc->maAlphaMask );
445 }
446 }
447 else if (IsAlpha())
448 {
449 sal_uInt8 nTransparencyOpaque = 0;
450 const AlphaMask aAlphaSrc(pBmpExSrc->GetSizePixel(), &nTransparencyOpaque);
451
452 maAlphaMask.CopyPixel( rRectDst, rRectSrc, &aAlphaSrc.ImplGetBitmap() );
453 }
454 }
455 }
456 }
457
458 return bRet;
459}
460
461bool BitmapEx::Erase( const Color& rFillColor )
462{
463 bool bRet = false;
464
465 if( !maBitmap.IsEmpty() )
466 {
467 bRet = maBitmap.Erase( rFillColor );
468
469 if( bRet && !maAlphaMask.IsEmpty() )
470 {
471 // Respect transparency on fill color
472 if( rFillColor.IsTransparent() )
473 maAlphaMask.Erase( 255 - rFillColor.GetAlpha() );
474 else
475 maAlphaMask.Erase( 0 );
476 }
477 }
478
479 return bRet;
480}
481
482void BitmapEx::Replace( const Color& rSearchColor, const Color& rReplaceColor )
483{
484 if (!maBitmap.IsEmpty())
485 maBitmap.Replace( rSearchColor, rReplaceColor );
486}
487
488void BitmapEx::Replace( const Color* pSearchColors, const Color* pReplaceColors, size_t nColorCount )
489{
490 if (!maBitmap.IsEmpty())
491 maBitmap.Replace( pSearchColors, pReplaceColors, nColorCount, /*pTols*/nullptr );
492}
493
494bool BitmapEx::Adjust( short nLuminancePercent, short nContrastPercent,
495 short nChannelRPercent, short nChannelGPercent, short nChannelBPercent,
496 double fGamma, bool bInvert, bool msoBrightness )
497{
498 return !maBitmap.IsEmpty() && maBitmap.Adjust( nLuminancePercent, nContrastPercent,
499 nChannelRPercent, nChannelGPercent, nChannelBPercent,
500 fGamma, bInvert, msoBrightness );
501}
502
503void BitmapEx::Draw( OutputDevice* pOutDev, const Point& rDestPt ) const
504{
505 pOutDev->DrawBitmapEx( rDestPt, *this );
506}
507
509 const Point& rDestPt, const Size& rDestSize ) const
510{
511 pOutDev->DrawBitmapEx( rDestPt, rDestSize, *this );
512}
513
514BitmapEx BitmapEx:: AutoScaleBitmap(BitmapEx const & aBitmap, const tools::Long aStandardSize)
515{
516 Point aEmptyPoint(0,0);
517 double imgposX = 0;
518 double imgposY = 0;
519 BitmapEx aRet = aBitmap;
520 double imgOldWidth = aRet.GetSizePixel().Width();
521 double imgOldHeight = aRet.GetSizePixel().Height();
522
523 if (imgOldWidth >= aStandardSize || imgOldHeight >= aStandardSize)
524 {
525 sal_Int32 imgNewWidth = 0;
526 sal_Int32 imgNewHeight = 0;
527 if (imgOldWidth >= imgOldHeight)
528 {
529 imgNewWidth = aStandardSize;
530 imgNewHeight = sal_Int32(imgOldHeight / (imgOldWidth / aStandardSize) + 0.5);
531 imgposX = 0;
532 imgposY = (aStandardSize - (imgOldHeight / (imgOldWidth / aStandardSize) + 0.5)) / 2 + 0.5;
533 }
534 else
535 {
536 imgNewHeight = aStandardSize;
537 imgNewWidth = sal_Int32(imgOldWidth / (imgOldHeight / aStandardSize) + 0.5);
538 imgposY = 0;
539 imgposX = (aStandardSize - (imgOldWidth / (imgOldHeight / aStandardSize) + 0.5)) / 2 + 0.5;
540 }
541
542 Size aScaledSize( imgNewWidth, imgNewHeight );
543 aRet.Scale( aScaledSize, BmpScaleFlag::BestQuality );
544 }
545 else
546 {
547 imgposX = (aStandardSize - imgOldWidth) / 2 + 0.5;
548 imgposY = (aStandardSize - imgOldHeight) / 2 + 0.5;
549 }
550
551 Size aStdSize( aStandardSize, aStandardSize );
552 tools::Rectangle aRect(aEmptyPoint, aStdSize );
553
555 aVirDevice->SetOutputSizePixel( aStdSize );
556 aVirDevice->SetFillColor( COL_TRANSPARENT );
557 aVirDevice->SetLineColor( COL_TRANSPARENT );
558
559 // Draw a rect into virDevice
560 aVirDevice->DrawRect( aRect );
561 Point aPointPixel( static_cast<tools::Long>(imgposX), static_cast<tools::Long>(imgposY) );
562 aVirDevice->DrawBitmapEx( aPointPixel, aRet );
563 aRet = aVirDevice->GetBitmapEx( aEmptyPoint, aStdSize );
564
565 return aRet;
566}
567
568sal_uInt8 BitmapEx::GetAlpha(sal_Int32 nX, sal_Int32 nY) const
569{
570 if(maBitmap.IsEmpty())
571 return 0;
572
573 if (nX < 0 || nX >= GetSizePixel().Width() || nY < 0 || nY >= GetSizePixel().Height())
574 return 0;
575
577 return GetPixelColor(nX, nY).GetAlpha();
578
579 sal_uInt8 nAlpha(0);
580 if (maAlphaMask.IsEmpty())
581 {
582 // Not transparent, ergo all covered
583 nAlpha = 255;
584 }
585 else
586 {
587 Bitmap aTestBitmap(maAlphaMask);
588 Bitmap::ScopedReadAccess pRead(aTestBitmap);
589
590 if(pRead)
591 {
592 const BitmapColor aBitmapColor(pRead->GetPixel(nY, nX));
593 nAlpha = aBitmapColor.GetIndex();
594 }
595 }
596 return nAlpha;
597}
598
599
600Color BitmapEx::GetPixelColor(sal_Int32 nX, sal_Int32 nY) const
601{
602 Bitmap::ScopedReadAccess pReadAccess( const_cast<Bitmap&>(maBitmap) );
603 assert(pReadAccess);
604
605 BitmapColor aColor = pReadAccess->GetColor(nY, nX);
606
607 if (IsAlpha())
608 {
609 AlphaMask aAlpha = GetAlphaMask();
610 AlphaMask::ScopedReadAccess pAlphaReadAccess(aAlpha);
611 aColor.SetAlpha(pAlphaReadAccess->GetPixel(nY, nX).GetIndex());
612 }
614 {
615 aColor.SetAlpha(255);
616 }
617 return aColor;
618}
619
620// Shift alpha transparent pixels between cppcanvas/ implementations
621// and vcl in a generally grotesque and under-performing fashion
622bool BitmapEx::Create( const css::uno::Reference< css::rendering::XBitmapCanvas > &xBitmapCanvas,
623 const Size &rSize )
624{
625 uno::Reference< beans::XFastPropertySet > xFastPropertySet( xBitmapCanvas, uno::UNO_QUERY );
626 if( xFastPropertySet )
627 {
628 // 0 means get BitmapEx
629 uno::Any aAny = xFastPropertySet->getFastPropertyValue( 0 );
630 std::unique_ptr<BitmapEx> xBitmapEx(reinterpret_cast<BitmapEx*>(*o3tl::doAccess<sal_Int64>(aAny)));
631 if( xBitmapEx )
632 {
633 *this = *xBitmapEx;
634 return true;
635 }
636 }
637
638 std::shared_ptr<SalBitmap> pSalBmp;
639 std::shared_ptr<SalBitmap> pSalMask;
640
642
643 Size aLocalSize(rSize);
644 if( pSalBmp->Create( xBitmapCanvas, aLocalSize ) )
645 {
646 pSalMask = ImplGetSVData()->mpDefInst->CreateSalBitmap();
647 if ( pSalMask->Create( xBitmapCanvas, aLocalSize, true ) )
648 {
649 *this = BitmapEx(Bitmap(pSalBmp), Bitmap(pSalMask) );
650 return true;
651 }
652 else
653 {
654 *this = BitmapEx(Bitmap(pSalBmp));
655 return true;
656 }
657 }
658
659 return false;
660}
661
662namespace
663{
664 Bitmap impTransformBitmap(
665 const Bitmap& rSource,
666 const Size& rDestinationSize,
667 const basegfx::B2DHomMatrix& rTransform,
668 bool bSmooth)
669 {
670 Bitmap aDestination(rDestinationSize, vcl::PixelFormat::N24_BPP);
671 BitmapScopedWriteAccess xWrite(aDestination);
672
673 if(xWrite)
674 {
675 Bitmap::ScopedReadAccess xRead(const_cast< Bitmap& >(rSource));
676
677 if (xRead)
678 {
679 const Size aDestinationSizePixel(aDestination.GetSizePixel());
680 const BitmapColor aOutside(BitmapColor(0xff, 0xff, 0xff));
681
682 for(tools::Long y(0); y < aDestinationSizePixel.getHeight(); y++)
683 {
684 Scanline pScanline = xWrite->GetScanline( y );
685 for(tools::Long x(0); x < aDestinationSizePixel.getWidth(); x++)
686 {
687 const basegfx::B2DPoint aSourceCoor(rTransform * basegfx::B2DPoint(x, y));
688
689 if(bSmooth)
690 {
691 xWrite->SetPixelOnData(
692 pScanline,
693 x,
694 xRead->GetInterpolatedColorWithFallback(
695 aSourceCoor.getY(),
696 aSourceCoor.getX(),
697 aOutside));
698 }
699 else
700 {
701 // this version does the correct <= 0.0 checks, so no need
702 // to do the static_cast< sal_Int32 > self and make an error
703 xWrite->SetPixelOnData(
704 pScanline,
705 x,
706 xRead->GetColorWithFallback(
707 aSourceCoor.getY(),
708 aSourceCoor.getX(),
709 aOutside));
710 }
711 }
712 }
713 }
714 }
715 xWrite.reset();
716
717 rSource.AdaptBitCount(aDestination);
718
719 return aDestination;
720 }
721
723 bool implTransformNeedsSmooth(const basegfx::B2DHomMatrix& rTransformation)
724 {
725 basegfx::B2DVector aScale, aTranslate;
726 double fRotate, fShearX;
727 rTransformation.decompose(aScale, aTranslate, fRotate, fShearX);
728 if (aScale != basegfx::B2DVector(1, 1))
729 {
730 return true;
731 }
732
733 fRotate = fmod( fRotate, 2 * M_PI );
734 if (fRotate < 0)
735 {
736 fRotate += 2 * M_PI;
737 }
738 if (!rtl::math::approxEqual(fRotate, 0)
739 && !rtl::math::approxEqual(fRotate, M_PI_2)
740 && !rtl::math::approxEqual(fRotate, M_PI)
741 && !rtl::math::approxEqual(fRotate, 3 * M_PI_2))
742 {
743 return true;
744 }
745
746 if (!rtl::math::approxEqual(fShearX, 0))
747 {
748 return true;
749 }
750
751 return false;
752 }
753} // end of anonymous namespace
754
756 double fWidth,
757 double fHeight,
758 const basegfx::B2DHomMatrix& rTransformation) const
759{
760 if(fWidth <= 1 || fHeight <= 1)
761 return BitmapEx();
762
763 // force destination to 24 bit, we want to smooth output
764 const Size aDestinationSize(basegfx::fround(fWidth), basegfx::fround(fHeight));
765 bool bSmooth = implTransformNeedsSmooth(rTransformation);
766 const Bitmap aDestination(impTransformBitmap(GetBitmap(), aDestinationSize, rTransformation, bSmooth));
767
768 // create mask
769 if(IsAlpha())
770 {
771 const Bitmap aAlpha(impTransformBitmap(GetAlphaMask().GetBitmap(), aDestinationSize, rTransformation, bSmooth));
772 return BitmapEx(aDestination, AlphaMask(aAlpha));
773 }
774
775 return BitmapEx(aDestination);
776}
777
779 const basegfx::B2DHomMatrix& rTransformation,
780 const basegfx::B2DRange& rVisibleRange,
781 double fMaximumArea) const
782{
783 BitmapEx aRetval;
784
785 if(IsEmpty())
786 return aRetval;
787
788 const sal_uInt32 nSourceWidth(GetSizePixel().Width());
789 const sal_uInt32 nSourceHeight(GetSizePixel().Height());
790
791 if(!nSourceWidth || !nSourceHeight)
792 return aRetval;
793
794 // Get aOutlineRange
795 basegfx::B2DRange aOutlineRange(0.0, 0.0, 1.0, 1.0);
796
797 aOutlineRange.transform(rTransformation);
798
799 // create visible range from it by moving from relative to absolute
800 basegfx::B2DRange aVisibleRange(rVisibleRange);
801
802 aVisibleRange.transform(
804 aOutlineRange.getRange(),
805 aOutlineRange.getMinimum()));
806
807 // get target size (which is visible range's size)
808 double fWidth(aVisibleRange.getWidth());
809 double fHeight(aVisibleRange.getHeight());
810
811 if(fWidth < 1.0 || fHeight < 1.0)
812 {
813 return aRetval;
814 }
815
816 // test if discrete size (pixel) maybe too big and limit it
817 const double fArea(fWidth * fHeight);
818 const bool bNeedToReduce(basegfx::fTools::more(fArea, fMaximumArea));
819 double fReduceFactor(1.0);
820
821 if(bNeedToReduce)
822 {
823 fReduceFactor = sqrt(fMaximumArea / fArea);
824 fWidth *= fReduceFactor;
825 fHeight *= fReduceFactor;
826 }
827
828 // Build complete transform from source pixels to target pixels.
829 // Start by scaling from source pixel size to unit coordinates
830 basegfx::B2DHomMatrix aTransform(
832 1.0 / nSourceWidth,
833 1.0 / nSourceHeight));
834
835 // multiply with given transform which leads from unit coordinates inside
836 // aOutlineRange
837 aTransform = rTransformation * aTransform;
838
839 // subtract top-left of absolute VisibleRange
840 aTransform.translate(
841 -aVisibleRange.getMinX(),
842 -aVisibleRange.getMinY());
843
844 // scale to target pixels (if needed)
845 if(bNeedToReduce)
846 {
847 aTransform.scale(fReduceFactor, fReduceFactor);
848 }
849
850 // invert to get transformation from target pixel coordinates to source pixels
851 aTransform.invert();
852
853 // create bitmap using source, destination and linear back-transformation
854 aRetval = TransformBitmapEx(fWidth, fHeight, aTransform);
855
856 return aRetval;
857}
858
860{
861 Bitmap aChangedBitmap(GetBitmap());
862 bool bDone(false);
863
864 for(sal_uInt32 a(rBColorModifierStack.count()); a && !bDone; )
865 {
866 const basegfx::BColorModifierSharedPtr& rModifier = rBColorModifierStack.getBColorModifier(--a);
867 const basegfx::BColorModifier_replace* pReplace = dynamic_cast< const basegfx::BColorModifier_replace* >(rModifier.get());
868
869 if(pReplace)
870 {
871 // complete replace
872 if(IsAlpha())
873 {
874 // clear bitmap with dest color
875 if (vcl::isPalettePixelFormat(aChangedBitmap.getPixelFormat()))
876 {
877 // For e.g. 8bit Bitmaps, the nearest color to the given erase color is
878 // determined and used -> this may be different from what is wanted here.
879 // Better create a new bitmap with the needed color explicitly.
880 Bitmap::ScopedReadAccess xReadAccess(aChangedBitmap);
881 OSL_ENSURE(xReadAccess, "Got no Bitmap ReadAccess ?!?");
882
883 if(xReadAccess)
884 {
885 BitmapPalette aNewPalette(xReadAccess->GetPalette());
886 aNewPalette[0] = BitmapColor(Color(pReplace->getBColor()));
887 aChangedBitmap = Bitmap(
888 aChangedBitmap.GetSizePixel(),
889 aChangedBitmap.getPixelFormat(),
890 &aNewPalette);
891 }
892 }
893 aChangedBitmap.Erase(Color(pReplace->getBColor()));
894 }
895 else
896 {
897 // erase bitmap, caller will know to paint direct
898 aChangedBitmap.SetEmpty();
899 }
900
901 bDone = true;
902 }
903 else
904 {
905 BitmapScopedWriteAccess xContent(aChangedBitmap);
906
907 if(xContent)
908 {
909 const double fConvertColor(1.0 / 255.0);
910
911 if(xContent->HasPalette())
912 {
913 const sal_uInt16 nCount(xContent->GetPaletteEntryCount());
914
915 for(sal_uInt16 b(0); b < nCount; b++)
916 {
917 const BitmapColor& rCol = xContent->GetPaletteColor(b);
918 const basegfx::BColor aBSource(
919 rCol.GetRed() * fConvertColor,
920 rCol.GetGreen() * fConvertColor,
921 rCol.GetBlue() * fConvertColor);
922 const basegfx::BColor aBDest(rModifier->getModifiedColor(aBSource));
923 xContent->SetPaletteColor(b, BitmapColor(Color(aBDest)));
924 }
925 }
926 else if(ScanlineFormat::N24BitTcBgr == xContent->GetScanlineFormat())
927 {
928 for(tools::Long y(0); y < xContent->Height(); y++)
929 {
930 Scanline pScan = xContent->GetScanline(y);
931
932 for(tools::Long x(0); x < xContent->Width(); x++)
933 {
934 const basegfx::BColor aBSource(
935 *(pScan + 2)* fConvertColor,
936 *(pScan + 1) * fConvertColor,
937 *pScan * fConvertColor);
938 const basegfx::BColor aBDest(rModifier->getModifiedColor(aBSource));
939 *pScan++ = static_cast< sal_uInt8 >(aBDest.getBlue() * 255.0);
940 *pScan++ = static_cast< sal_uInt8 >(aBDest.getGreen() * 255.0);
941 *pScan++ = static_cast< sal_uInt8 >(aBDest.getRed() * 255.0);
942 }
943 }
944 }
945 else if(ScanlineFormat::N24BitTcRgb == xContent->GetScanlineFormat())
946 {
947 for(tools::Long y(0); y < xContent->Height(); y++)
948 {
949 Scanline pScan = xContent->GetScanline(y);
950
951 for(tools::Long x(0); x < xContent->Width(); x++)
952 {
953 const basegfx::BColor aBSource(
954 *pScan * fConvertColor,
955 *(pScan + 1) * fConvertColor,
956 *(pScan + 2) * fConvertColor);
957 const basegfx::BColor aBDest(rModifier->getModifiedColor(aBSource));
958 *pScan++ = static_cast< sal_uInt8 >(aBDest.getRed() * 255.0);
959 *pScan++ = static_cast< sal_uInt8 >(aBDest.getGreen() * 255.0);
960 *pScan++ = static_cast< sal_uInt8 >(aBDest.getBlue() * 255.0);
961 }
962 }
963 }
964 else
965 {
966 for(tools::Long y(0); y < xContent->Height(); y++)
967 {
968 Scanline pScanline = xContent->GetScanline( y );
969 for(tools::Long x(0); x < xContent->Width(); x++)
970 {
971 const BitmapColor aBMCol(xContent->GetColor(y, x));
972 const basegfx::BColor aBSource(
973 static_cast<double>(aBMCol.GetRed()) * fConvertColor,
974 static_cast<double>(aBMCol.GetGreen()) * fConvertColor,
975 static_cast<double>(aBMCol.GetBlue()) * fConvertColor);
976 const basegfx::BColor aBDest(rModifier->getModifiedColor(aBSource));
977
978 xContent->SetPixelOnData(pScanline, x, BitmapColor(Color(aBDest)));
979 }
980 }
981 }
982 }
983 }
984 }
985
986 if(aChangedBitmap.IsEmpty())
987 {
988 return BitmapEx();
989 }
990 else
991 {
992 if(IsAlpha())
993 {
994 return BitmapEx(aChangedBitmap, GetAlphaMask());
995 }
996 else
997 {
998 return BitmapEx(aChangedBitmap);
999 }
1000 }
1001}
1002
1004 const Size& rSize,
1005 sal_uInt8 nAlpha,
1006 Color aColorTopLeft,
1007 Color aColorBottomRight)
1008{
1009 const sal_uInt32 nW(rSize.Width());
1010 const sal_uInt32 nH(rSize.Height());
1011
1012 if(nW || nH)
1013 {
1014 Color aColTopRight(aColorTopLeft);
1015 Color aColBottomLeft(aColorTopLeft);
1016 const sal_uInt32 nDE(nW + nH);
1017
1018 aColTopRight.Merge(aColorBottomRight, 255 - sal_uInt8((nW * 255) / nDE));
1019 aColBottomLeft.Merge(aColorBottomRight, 255 - sal_uInt8((nH * 255) / nDE));
1020
1021 return createBlendFrame(rSize, nAlpha, aColorTopLeft, aColTopRight, aColorBottomRight, aColBottomLeft);
1022 }
1023
1024 return BitmapEx();
1025}
1026
1028 const Size& rSize,
1029 sal_uInt8 nAlpha,
1030 Color aColorTopLeft,
1031 Color aColorTopRight,
1032 Color aColorBottomRight,
1033 Color aColorBottomLeft)
1034{
1035 // FIXME the call sites are actually passing in transparency
1036 nAlpha = 255 - nAlpha;
1037 BlendFrameCache* pBlendFrameCache = ImplGetBlendFrameCache();
1038
1039 if(pBlendFrameCache->m_aLastSize == rSize
1040 && pBlendFrameCache->m_nLastAlpha == nAlpha
1041 && pBlendFrameCache->m_aLastColorTopLeft == aColorTopLeft
1042 && pBlendFrameCache->m_aLastColorTopRight == aColorTopRight
1043 && pBlendFrameCache->m_aLastColorBottomRight == aColorBottomRight
1044 && pBlendFrameCache->m_aLastColorBottomLeft == aColorBottomLeft)
1045 {
1046 return pBlendFrameCache->m_aLastResult;
1047 }
1048
1049 pBlendFrameCache->m_aLastSize = rSize;
1050 pBlendFrameCache->m_nLastAlpha = nAlpha;
1051 pBlendFrameCache->m_aLastColorTopLeft = aColorTopLeft;
1052 pBlendFrameCache->m_aLastColorTopRight = aColorTopRight;
1053 pBlendFrameCache->m_aLastColorBottomRight = aColorBottomRight;
1054 pBlendFrameCache->m_aLastColorBottomLeft = aColorBottomLeft;
1055 pBlendFrameCache->m_aLastResult.Clear();
1056
1057 const tools::Long nW(rSize.Width());
1058 const tools::Long nH(rSize.Height());
1059
1060 if(nW > 1 && nH > 1)
1061 {
1062 sal_uInt8 aEraseTrans(0xff);
1063 Bitmap aContent(rSize, vcl::PixelFormat::N24_BPP);
1064 AlphaMask aAlpha(rSize, &aEraseTrans);
1065
1066 aContent.Erase(COL_BLACK);
1067
1068 BitmapScopedWriteAccess pContent(aContent);
1069 AlphaScopedWriteAccess pAlpha(aAlpha);
1070
1071 if(pContent && pAlpha)
1072 {
1073 tools::Long x(0);
1074 tools::Long y(0);
1075 Scanline pScanContent = pContent->GetScanline( 0 );
1076 Scanline pScanAlpha = pContent->GetScanline( 0 );
1077
1078 // x == 0, y == 0, top-left corner
1079 pContent->SetPixelOnData(pScanContent, 0, aColorTopLeft);
1080 pAlpha->SetPixelOnData(pScanAlpha, 0, BitmapColor(nAlpha));
1081
1082 // y == 0, top line left to right
1083 for(x = 1; x < nW - 1; x++)
1084 {
1085 Color aMix(aColorTopLeft);
1086
1087 aMix.Merge(aColorTopRight, 255 - sal_uInt8((x * 255) / nW));
1088 pContent->SetPixelOnData(pScanContent, x, aMix);
1089 pAlpha->SetPixelOnData(pScanAlpha, x, BitmapColor(nAlpha));
1090 }
1091
1092 // x == nW - 1, y == 0, top-right corner
1093 // #i123690# Caution! When nW is 1, x == nW is possible (!)
1094 if(x < nW)
1095 {
1096 pContent->SetPixelOnData(pScanContent, x, aColorTopRight);
1097 pAlpha->SetPixelOnData(pScanAlpha, x, BitmapColor(nAlpha));
1098 }
1099
1100 // x == 0 and nW - 1, left and right line top-down
1101 for(y = 1; y < nH - 1; y++)
1102 {
1103 pScanContent = pContent->GetScanline( y );
1104 pScanAlpha = pContent->GetScanline( y );
1105 Color aMixA(aColorTopLeft);
1106
1107 aMixA.Merge(aColorBottomLeft, 255 - sal_uInt8((y * 255) / nH));
1108 pContent->SetPixelOnData(pScanContent, 0, aMixA);
1109 pAlpha->SetPixelOnData(pScanAlpha, 0, BitmapColor(nAlpha));
1110
1111 // #i123690# Caution! When nW is 1, x == nW is possible (!)
1112 if(x < nW)
1113 {
1114 Color aMixB(aColorTopRight);
1115
1116 aMixB.Merge(aColorBottomRight, 255 - sal_uInt8((y * 255) / nH));
1117 pContent->SetPixelOnData(pScanContent, x, aMixB);
1118 pAlpha->SetPixelOnData(pScanAlpha, x, BitmapColor(nAlpha));
1119 }
1120 }
1121
1122 // #i123690# Caution! When nH is 1, y == nH is possible (!)
1123 if(y < nH)
1124 {
1125 // x == 0, y == nH - 1, bottom-left corner
1126 pContent->SetPixelOnData(pScanContent, 0, aColorBottomLeft);
1127 pAlpha->SetPixelOnData(pScanAlpha, 0, BitmapColor(nAlpha));
1128
1129 // y == nH - 1, bottom line left to right
1130 for(x = 1; x < nW - 1; x++)
1131 {
1132 Color aMix(aColorBottomLeft);
1133
1134 aMix.Merge(aColorBottomRight, 255 - sal_uInt8(((x - 0)* 255) / nW));
1135 pContent->SetPixelOnData(pScanContent, x, aMix);
1136 pAlpha->SetPixelOnData(pScanAlpha, x, BitmapColor(nAlpha));
1137 }
1138
1139 // x == nW - 1, y == nH - 1, bottom-right corner
1140 // #i123690# Caution! When nW is 1, x == nW is possible (!)
1141 if(x < nW)
1142 {
1143 pContent->SetPixelOnData(pScanContent, x, aColorBottomRight);
1144 pAlpha->SetPixelOnData(pScanAlpha, x, BitmapColor(nAlpha));
1145 }
1146 }
1147
1148 pContent.reset();
1149 pAlpha.reset();
1150
1151 pBlendFrameCache->m_aLastResult = BitmapEx(aContent, aAlpha);
1152 }
1153 }
1154
1155 return pBlendFrameCache->m_aLastResult;
1156}
1157
1158void BitmapEx::Replace(const Color& rSearchColor,
1159 const Color& rReplaceColor,
1160 sal_uInt8 nTolerance)
1161{
1162 maBitmap.Replace(rSearchColor, rReplaceColor, nTolerance);
1163}
1164
1165void BitmapEx::Replace( const Color* pSearchColors,
1166 const Color* pReplaceColors,
1167 size_t nColorCount,
1168 sal_uInt8 const * pTols )
1169{
1170 maBitmap.Replace( pSearchColors, pReplaceColors, nColorCount, pTols );
1171}
1172
1174{
1175 if( IsAlpha() )
1176 {
1177 maBitmap.Replace( GetAlphaMask(), rColor );
1178 maAlphaMask = Bitmap();
1180 }
1181}
1182
1183static Bitmap DetectEdges( const Bitmap& rBmp )
1184{
1185 constexpr sal_uInt8 cEdgeDetectThreshold = 128;
1186 const Size aSize( rBmp.GetSizePixel() );
1187
1188 if( ( aSize.Width() <= 2 ) || ( aSize.Height() <= 2 ) )
1189 return rBmp;
1190
1191 Bitmap aWorkBmp( rBmp );
1192
1193 if( !aWorkBmp.Convert( BmpConversion::N8BitGreys ) )
1194 return rBmp;
1195
1197 pVirDev->SetOutputSizePixel(aSize);
1198 Bitmap::ScopedReadAccess pReadAcc(aWorkBmp);
1199 if( !pReadAcc )
1200 return rBmp;
1201
1202 const tools::Long nWidth = aSize.Width();
1203 const tools::Long nWidth2 = nWidth - 2;
1204 const tools::Long nHeight = aSize.Height();
1205 const tools::Long nHeight2 = nHeight - 2;
1206 const tools::Long lThres2 = static_cast<tools::Long>(cEdgeDetectThreshold) * cEdgeDetectThreshold;
1207 tools::Long nSum1;
1208 tools::Long nSum2;
1209 tools::Long lGray;
1210
1211 // initialize border with white pixels
1212 pVirDev->SetLineColor( COL_WHITE );
1213 pVirDev->DrawLine( Point(), Point( nWidth - 1, 0L ) );
1214 pVirDev->DrawLine( Point( nWidth - 1, 0L ), Point( nWidth - 1, nHeight - 1 ) );
1215 pVirDev->DrawLine( Point( nWidth - 1, nHeight - 1 ), Point( 0L, nHeight - 1 ) );
1216 pVirDev->DrawLine( Point( 0, nHeight - 1 ), Point() );
1217
1218 for( tools::Long nY = 0, nY1 = 1, nY2 = 2; nY < nHeight2; nY++, nY1++, nY2++ )
1219 {
1220 Scanline pScanlineRead = pReadAcc->GetScanline( nY );
1221 Scanline pScanlineRead1 = pReadAcc->GetScanline( nY1 );
1222 Scanline pScanlineRead2 = pReadAcc->GetScanline( nY2 );
1223 for( tools::Long nX = 0, nXDst = 1, nXTmp; nX < nWidth2; nX++, nXDst++ )
1224 {
1225 nXTmp = nX;
1226
1227 nSum2 = pReadAcc->GetIndexFromData( pScanlineRead, nXTmp++ );
1228 nSum1 = -nSum2;
1229 nSum2 += static_cast<tools::Long>(pReadAcc->GetIndexFromData( pScanlineRead, nXTmp++ )) << 1;
1230 lGray = pReadAcc->GetIndexFromData( pScanlineRead, nXTmp );
1231 nSum1 += lGray;
1232 nSum2 += lGray;
1233
1234 nSum1 += static_cast<tools::Long>(pReadAcc->GetIndexFromData( pScanlineRead1, nXTmp )) << 1;
1235 nXTmp -= 2;
1236 nSum1 -= static_cast<tools::Long>(pReadAcc->GetIndexFromData( pScanlineRead1, nXTmp )) << 1;
1237
1238 lGray = -static_cast<tools::Long>(pReadAcc->GetIndexFromData( pScanlineRead2, nXTmp++ ));
1239 nSum1 += lGray;
1240 nSum2 += lGray;
1241 nSum2 -= static_cast<tools::Long>(pReadAcc->GetIndexFromData( pScanlineRead2, nXTmp++ )) << 1;
1242 lGray = static_cast<tools::Long>(pReadAcc->GetIndexFromData( pScanlineRead2, nXTmp ));
1243 nSum1 += lGray;
1244 nSum2 -= lGray;
1245
1246 if( ( nSum1 * nSum1 + nSum2 * nSum2 ) < lThres2 )
1247 pVirDev->DrawPixel( Point(nXDst, nY), COL_WHITE );
1248 else
1249 pVirDev->DrawPixel( Point(nXDst, nY), COL_BLACK );
1250 }
1251 }
1252
1253 pReadAcc.reset();
1254
1255 Bitmap aRetBmp = pVirDev->GetBitmap(Point(0,0), aSize);
1256
1257 if( aRetBmp.IsEmpty() )
1258 aRetBmp = rBmp;
1259 else
1260 {
1261 aRetBmp.SetPrefMapMode( rBmp.GetPrefMapMode() );
1262 aRetBmp.SetPrefSize( rBmp.GetPrefSize() );
1263 }
1264
1265 return aRetBmp;
1266}
1267
1269tools::Polygon BitmapEx::GetContour( bool bContourEdgeDetect,
1270 const tools::Rectangle* pWorkRectPixel )
1271{
1272 Bitmap aWorkBmp;
1273 tools::Polygon aRetPoly;
1274 tools::Rectangle aWorkRect( Point(), maBitmap.GetSizePixel() );
1275
1276 if( pWorkRectPixel )
1277 aWorkRect.Intersection( *pWorkRectPixel );
1278
1279 aWorkRect.Normalize();
1280
1281 if( ( aWorkRect.GetWidth() > 4 ) && ( aWorkRect.GetHeight() > 4 ) )
1282 {
1283 // if the flag is set, we need to detect edges
1284 if( bContourEdgeDetect )
1285 aWorkBmp = DetectEdges( maBitmap );
1286 else
1287 aWorkBmp = maBitmap;
1288
1289 BitmapReadAccess* pAcc = aWorkBmp.AcquireReadAccess();
1290
1291 const tools::Long nWidth = pAcc ? pAcc->Width() : 0;
1292 const tools::Long nHeight = pAcc ? pAcc->Height() : 0;
1293
1294 if (pAcc && nWidth && nHeight)
1295 {
1296 const Size& rPrefSize = aWorkBmp.GetPrefSize();
1297 const double fFactorX = static_cast<double>(rPrefSize.Width()) / nWidth;
1298 const double fFactorY = static_cast<double>(rPrefSize.Height()) / nHeight;
1299 const tools::Long nStartX1 = aWorkRect.Left() + 1;
1300 const tools::Long nEndX1 = aWorkRect.Right();
1301 const tools::Long nStartX2 = nEndX1 - 1;
1302 const tools::Long nStartY1 = aWorkRect.Top() + 1;
1303 const tools::Long nEndY1 = aWorkRect.Bottom();
1304 std::unique_ptr<Point[]> pPoints1;
1305 std::unique_ptr<Point[]> pPoints2;
1306 tools::Long nX, nY;
1307 sal_uInt16 nPolyPos = 0;
1308 const BitmapColor aBlack = pAcc->GetBestMatchingColor( COL_BLACK );
1309
1310 pPoints1.reset(new Point[ nHeight ]);
1311 pPoints2.reset(new Point[ nHeight ]);
1312
1313 for ( nY = nStartY1; nY < nEndY1; nY++ )
1314 {
1315 nX = nStartX1;
1316 Scanline pScanline = pAcc->GetScanline( nY );
1317
1318 // scan row from left to right
1319 while( nX < nEndX1 )
1320 {
1321 if( aBlack == pAcc->GetPixelFromData( pScanline, nX ) )
1322 {
1323 pPoints1[ nPolyPos ] = Point( nX, nY );
1324 nX = nStartX2;
1325
1326 // this loop always breaks eventually as there is at least one pixel
1327 while( true )
1328 {
1329 if( aBlack == pAcc->GetPixelFromData( pScanline, nX ) )
1330 {
1331 pPoints2[ nPolyPos ] = Point( nX, nY );
1332 break;
1333 }
1334
1335 nX--;
1336 }
1337
1338 nPolyPos++;
1339 break;
1340 }
1341
1342 nX++;
1343 }
1344 }
1345
1346 const sal_uInt16 nNewSize1 = nPolyPos << 1;
1347
1348 aRetPoly = tools::Polygon( nPolyPos, pPoints1.get() );
1349 aRetPoly.SetSize( nNewSize1 + 1 );
1350 aRetPoly[ nNewSize1 ] = aRetPoly[ 0 ];
1351
1352 for( sal_uInt16 j = nPolyPos; nPolyPos < nNewSize1; )
1353 aRetPoly[ nPolyPos++ ] = pPoints2[ --j ];
1354
1355 if( ( fFactorX != 0. ) && ( fFactorY != 0. ) )
1356 aRetPoly.Scale( fFactorX, fFactorY );
1357 }
1358
1360 }
1361
1362 return aRetPoly;
1363}
1364
1366{
1367 AlphaMask aAlphaMask(GetAlphaMask());
1368 BitmapScopedWriteAccess pAlphaWriteAccess(aAlphaMask);
1370 assert( pReadAccess.get() && pAlphaWriteAccess.get() );
1371 if ( !(pReadAccess.get() && pAlphaWriteAccess.get()) )
1372 return;
1373
1374 for ( tools::Long nY = 0; nY < pReadAccess->Height(); nY++ )
1375 {
1376 Scanline pScanline = pAlphaWriteAccess->GetScanline( nY );
1377 Scanline pScanlineRead = pReadAccess->GetScanline( nY );
1378 for ( tools::Long nX = 0; nX < pReadAccess->Width(); nX++ )
1379 {
1380 const sal_uInt8 cIndex = pReadAccess->GetPixelFromData( pScanlineRead, nX ).GetIndex();
1381 if ( cIndex == cIndexFrom )
1382 pAlphaWriteAccess->SetPixelOnData( pScanline, nX, BitmapColor(nAlphaTo) );
1383 }
1384 }
1385 *this = BitmapEx( GetBitmap(), aAlphaMask );
1386}
1387
1389{
1390 AlphaMask aAlpha;
1391
1392 if (!IsAlpha())
1393 {
1394 aAlpha = AlphaMask(GetSizePixel(), &cTrans);
1395 }
1396 else
1397 {
1398 aAlpha = GetAlphaMask();
1399 BitmapScopedWriteAccess pA(aAlpha);
1400 assert(pA);
1401
1402 if( !pA )
1403 return;
1404
1405 sal_uLong nTrans = cTrans;
1406 const tools::Long nWidth = pA->Width(), nHeight = pA->Height();
1407
1408 if( pA->GetScanlineFormat() == ScanlineFormat::N8BitPal )
1409 {
1410 for( tools::Long nY = 0; nY < nHeight; nY++ )
1411 {
1412 Scanline pAScan = pA->GetScanline( nY );
1413
1414 for( tools::Long nX = 0; nX < nWidth; nX++ )
1415 {
1416 sal_uLong nNewTrans = nTrans + (255 - *pAScan);
1417 // clamp to 255
1418 nNewTrans = ( nNewTrans & 0xffffff00 ) ? 255 : nNewTrans;
1419 *pAScan++ = static_cast<sal_uInt8>( 255 - nNewTrans );
1420 }
1421 }
1422 }
1423 else
1424 {
1425 BitmapColor aAlphaValue( 0 );
1426
1427 for( tools::Long nY = 0; nY < nHeight; nY++ )
1428 {
1429 Scanline pScanline = pA->GetScanline( nY );
1430 for( tools::Long nX = 0; nX < nWidth; nX++ )
1431 {
1432 sal_uLong nNewTrans = nTrans + (255 - pA->GetIndexFromData( pScanline, nX ));
1433 // clamp to 255
1434 nNewTrans = ( nNewTrans & 0xffffff00 ) ? 255 : nNewTrans;
1435 // convert back to alpha
1436 aAlphaValue.SetIndex( static_cast<sal_uInt8>(255 - nNewTrans) );
1437 pA->SetPixelOnData( pScanline, nX, aAlphaValue );
1438 }
1439 }
1440 }
1441 }
1442 *this = BitmapEx( GetBitmap(), aAlpha );
1443}
1444
1446{
1447 Bitmap aNewMask = maBitmap.CreateMask( maskColor, nTol );
1448 if ( IsAlpha() )
1449 aNewMask.CombineOr( maAlphaMask );
1450 maAlphaMask = aNewMask;
1451}
1452
1456void BitmapEx::GetColorModel(css::uno::Sequence< sal_Int32 >& rRGBPalette,
1457 sal_uInt32& rnRedMask, sal_uInt32& rnGreenMask, sal_uInt32& rnBlueMask, sal_uInt32& rnAlphaMask, sal_uInt32& rnTransparencyIndex,
1458 sal_uInt32& rnWidth, sal_uInt32& rnHeight, sal_uInt8& rnBitCount)
1459{
1460 Bitmap::ScopedReadAccess pReadAccess( maBitmap );
1461 assert( pReadAccess );
1462
1463 if( pReadAccess->HasPalette() )
1464 {
1465 sal_uInt16 nPalCount = pReadAccess->GetPaletteEntryCount();
1466
1467 if( nPalCount )
1468 {
1469 rRGBPalette = css::uno::Sequence< sal_Int32 >( nPalCount + 1 );
1470
1471 sal_Int32* pTmp = rRGBPalette.getArray();
1472
1473 for( sal_uInt32 i = 0; i < nPalCount; i++, pTmp++ )
1474 {
1475 const BitmapColor& rCol = pReadAccess->GetPaletteColor( static_cast<sal_uInt16>(i) );
1476
1477 *pTmp = static_cast<sal_Int32>(rCol.GetRed()) << sal_Int32(24);
1478 *pTmp |= static_cast<sal_Int32>(rCol.GetGreen()) << sal_Int32(16);
1479 *pTmp |= static_cast<sal_Int32>(rCol.GetBlue()) << sal_Int32(8);
1480 *pTmp |= sal_Int32(0x000000ffL);
1481 }
1482
1483 if( IsAlpha() )
1484 {
1485 // append transparent entry
1486 *pTmp = sal_Int32(0xffffff00L);
1487 rnTransparencyIndex = nPalCount;
1488 nPalCount++;
1489 }
1490 else
1491 rnTransparencyIndex = 0;
1492 }
1493 }
1494 else
1495 {
1496 rnRedMask = 0xff000000UL;
1497 rnGreenMask = 0x00ff0000UL;
1498 rnBlueMask = 0x0000ff00UL;
1499 rnAlphaMask = 0x000000ffUL;
1500 rnTransparencyIndex = 0;
1501 }
1502
1503 rnWidth = pReadAccess->Width();
1504 rnHeight = pReadAccess->Height();
1505 rnBitCount = pReadAccess->GetBitCount();
1506}
1507
1508void BitmapEx::DumpAsPng(const char* pFileName) const
1509{
1510 OUString sPath;
1511 if (pFileName)
1512 {
1513 sPath = OUString::fromUtf8(pFileName);
1514 }
1515 else if (const char* pEnv = std::getenv("VCL_DUMP_BMP_PATH"))
1516 {
1517 sPath = OUString::fromUtf8(pEnv);
1518 }
1519 else
1520 {
1521 sPath = "file:///tmp/bitmap.png";
1522 }
1523 SvFileStream aStream(sPath, StreamMode::STD_READWRITE | StreamMode::TRUNC);
1524 assert(aStream.good());
1525 vcl::PngImageWriter aWriter(aStream);
1526 aWriter.write(*this);
1527}
1528
1529/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static Bitmap DetectEdges(const Bitmap &rBmp)
Definition: BitmapEx.cxx:1183
BitmapEx createBlendFrame(const Size &rSize, sal_uInt8 nAlpha, Color aColorTopLeft, Color aColorBottomRight)
Create a blend frame as BitmapEx.
Definition: BitmapEx.cxx:1003
sal_uInt8 * Scanline
Definition: Scanline.hxx:26
#define BITMAP_CHECKSUM_SIZE
Definition: checksum.hxx:28
sal_uInt8 BitmapChecksumOctetArray[BITMAP_CHECKSUM_SIZE]
Definition: checksum.hxx:31
sal_uInt64 BitmapChecksum
Definition: checksum.hxx:30
void BCToBCOA(BitmapChecksum n, BitmapChecksumOctetArray p)
Definition: checksum.hxx:34
BitmapChecksum vcl_get_checksum(BitmapChecksum Checksum, const void *Data, sal_uInt32 DatLen)
Definition: checksum.hxx:72
const StyleSettings & GetStyleSettings() const
bool IsEmpty() const
SAL_DLLPRIVATE const Bitmap & ImplGetBitmap() const
Definition: alpha.cxx:72
void Erase(sal_uInt8 cTransparency)
Definition: alpha.cxx:82
static OutputDevice * GetDefaultDevice()
Get the default "device" (in this case the default window).
Definition: svapp.cxx:1043
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:638
sal_uInt8 GetIndex() const
Definition: BitmapColor.hxx:70
void SetIndex(sal_uInt8 cIndex)
Definition: BitmapColor.hxx:75
tools::Polygon GetContour(bool bContourEdgeDetect, const tools::Rectangle *pWorkRect)
Get contours in image.
Definition: BitmapEx.cxx:1269
Bitmap maBitmap
Definition: bitmapex.hxx:467
bool Invert()
Perform the Invert operation on every pixel.
Definition: BitmapEx.cxx:258
void ClearAlpha()
Definition: BitmapEx.cxx:202
const AlphaMask & GetAlphaMask() const
Definition: bitmapex.hxx:71
sal_Int64 GetSizeBytes() const
Definition: BitmapEx.cxx:229
BitmapEx & operator=(const BitmapEx &rBitmapEx)
BitmapEx getTransformed(const basegfx::B2DHomMatrix &rTransformation, const basegfx::B2DRange &rVisibleRange, double fMaximumArea) const
Create transformed Bitmap.
Definition: BitmapEx.cxx:778
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
Scale the bitmap.
Definition: BitmapEx.cxx:305
void ChangeColorAlpha(sal_uInt8 cIndexFrom, sal_Int8 nAlphaTo)
Definition: BitmapEx.cxx:1365
void ReplaceTransparency(const Color &rColor)
Replace transparency with given color.
Definition: BitmapEx.cxx:1173
static BitmapEx AutoScaleBitmap(BitmapEx const &aBitmap, const tools::Long aStandardSize)
Definition: BitmapEx.cxx:514
bool Erase(const Color &rFillColor)
Fill the entire bitmap with the given color.
Definition: BitmapEx.cxx:461
Size maBitmapSize
Definition: bitmapex.hxx:469
void Clear()
Definition: BitmapEx.cxx:197
bool Convert(BmpConversion eConversion)
Convert bitmap format.
Definition: BitmapEx.cxx:383
::Color GetPixelColor(sal_Int32 nX, sal_Int32 nY) const
Get pixel color (including alpha) at given position.
Definition: BitmapEx.cxx:600
void Expand(sal_Int32 nDX, sal_Int32 nDY, bool bExpandTransparent=false)
Expand the bitmap by pixel padding.
Definition: BitmapEx.cxx:388
bool IsAlpha() const
Definition: BitmapEx.cxx:207
bool Rotate(Degree10 nAngle10, const Color &rFillColor)
Rotate bitmap by the specified angle.
Definition: BitmapEx.cxx:325
BitmapChecksum GetChecksum() const
Definition: BitmapEx.cxx:239
BitmapEx TransformBitmapEx(double fWidth, double fHeight, const basegfx::B2DHomMatrix &rTransformation) const
Create transformed Bitmap.
Definition: BitmapEx.cxx:755
bool CopyPixel(const tools::Rectangle &rRectDst, const tools::Rectangle &rRectSrc, const BitmapEx *pBmpExSrc)
Copy a rectangular area from another bitmap.
Definition: BitmapEx.cxx:409
bool IsEmpty() const
Definition: BitmapEx.cxx:186
bool Mirror(BmpMirrorFlags nMirrorFlags)
Mirror the bitmap.
Definition: BitmapEx.cxx:268
bool operator==(const BitmapEx &rBitmapEx) const
Definition: BitmapEx.cxx:175
Bitmap GetBitmap(Color aTransparentReplaceColor) const
Definition: BitmapEx.cxx:217
sal_uInt8 GetAlpha(sal_Int32 nX, sal_Int32 nY) const
Get alpha at given position.
Definition: BitmapEx.cxx:568
BitmapEx()
Definition: BitmapEx.cxx:53
void Replace(const Color &rSearchColor, const Color &rReplaceColor)
Replace all pixel having the search color with the specified color.
Definition: BitmapEx.cxx:482
void CombineMaskOr(Color maskColor, sal_uInt8 nTol)
Definition: BitmapEx.cxx:1445
void loadFromIconTheme(const OUString &rIconName)
Definition: BitmapEx.cxx:85
bool Create(const css::uno::Reference< css::rendering::XBitmapCanvas > &xBitmapCanvas, const Size &rSize)
populate from a canvas implementation
Definition: BitmapEx.cxx:622
bool Crop(const tools::Rectangle &rRectPixel)
Crop the bitmap.
Definition: BitmapEx.cxx:363
void AdjustTransparency(sal_uInt8 cTrans)
Definition: BitmapEx.cxx:1388
void Draw(OutputDevice *pOutDev, const Point &rDestPt) const
Definition: BitmapEx.cxx:503
const Bitmap & GetBitmap() const
Gives direct access to the contained bitmap.
Definition: BitmapEx.cxx:212
void SetSizePixel(const Size &rNewSize)
Definition: BitmapEx.cxx:253
bool Adjust(short nLuminancePercent, short nContrastPercent, short nChannelRPercent, short nChannelGPercent, short nChannelBPercent, double fGamma=1.0, bool bInvert=false, bool msoBrightness=false)
Change various global color characteristics.
Definition: BitmapEx.cxx:494
BitmapEx ModifyBitmapEx(const basegfx::BColorModifierStack &rBColorModifierStack) const
Create ColorStack-modified version of this BitmapEx.
Definition: BitmapEx.cxx:859
const Size & GetSizePixel() const
Definition: bitmapex.hxx:73
AlphaMask maAlphaMask
Definition: bitmapex.hxx:468
void SetEmpty()
Definition: BitmapEx.cxx:191
void GetColorModel(css::uno::Sequence< sal_Int32 > &rRGBPalette, sal_uInt32 &rnRedMask, sal_uInt32 &rnGreenMask, sal_uInt32 &rnBlueMask, sal_uInt32 &rnAlphaMask, sal_uInt32 &rnTransparencyIndex, sal_uInt32 &rnWidth, sal_uInt32 &rnHeight, sal_uInt8 &rnBitCount)
Retrieves the color model data we need for the XImageConsumer stuff.
Definition: BitmapEx.cxx:1456
void DumpAsPng(const char *pFileName=nullptr) const
Dumps the pixels as PNG in bitmap.png.
Definition: BitmapEx.cxx:1508
static bool Filter(BitmapEx &rBmpEx, BitmapFilter const &rFilter)
tools::Long Height() const
tools::Long Width() const
const BitmapPalette & GetPalette() const
BitmapColor GetBestMatchingColor(const BitmapColor &rBitmapColor) const
bool HasPalette() const
sal_uInt16 GetPaletteEntryCount() const
sal_uInt16 GetBitCount() const
const BitmapColor & GetPaletteColor(sal_uInt16 nColor) const
BitmapColor GetPixel(tools::Long nY, tools::Long nX) const
BitmapColor GetColor(tools::Long nY, tools::Long nX) const
BitmapColor GetPixelFromData(const sal_uInt8 *pData, tools::Long nX) const
sal_uInt8 GetIndexFromData(const sal_uInt8 *pData, tools::Long nX) const
Scanline GetScanline(tools::Long nY) const
bool CombineOr(const Bitmap &rMask)
Perform boolean OR operation with another bitmap.
sal_Int64 GetSizeBytes() const
void SetPrefMapMode(const MapMode &rMapMode)
bool Rotate(Degree10 nAngle10, const Color &rFillColor)
Rotate bitmap by the specified angle.
bool Crop(const tools::Rectangle &rRectPixel)
Crop the bitmap.
bool HasGreyPalette8Bit() const
Bitmap CreateMask(const Color &rTransColor) const
Create on-off mask from bitmap.
bool Convert(BmpConversion eConversion)
Convert bitmap format.
static const BitmapPalette & GetGreyPalette(int nEntries)
BitmapChecksum GetChecksum() const
bool Replace(const AlphaMask &rAlpha, const Color &rMergeColor)
Merge bitmap with given background color according to specified alpha mask.
const MapMode & GetPrefMapMode() const
Size GetSizePixel() const
static void ReleaseAccess(BitmapInfoAccess *pAccess)
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
Scale the bitmap.
bool CopyPixel_AlphaOptimized(const tools::Rectangle &rRectDst, const tools::Rectangle &rRectSrc, const Bitmap *pBmpSrc)
bool IsEmpty() const
BitmapReadAccess * AcquireReadAccess()
bool Adjust(short nLuminancePercent, short nContrastPercent=0, short nChannelRPercent=0, short nChannelGPercent=0, short nChannelBPercent=0, double fGamma=1.0, bool bInvert=false, bool msoBrightness=false)
Change various global color characteristics.
void SetPrefSize(const Size &rSize)
bool Invert()
Perform the Invert operation on every pixel.
Definition: bitmappaint.cxx:61
bool CopyPixel(const tools::Rectangle &rRectDst, const tools::Rectangle &rRectSrc, const Bitmap *pBmpSrc=nullptr)
Copy a rectangular area from another bitmap.
const Size & GetPrefSize() const
bool Erase(const Color &rFillColor)
Fill the entire bitmap with the given color.
Definition: bitmappaint.cxx:34
void AdaptBitCount(Bitmap &rNew) const
bool Expand(sal_Int32 nDX, sal_Int32 nDY, const Color *pInitColor=nullptr)
Expand the bitmap by pixel padding.
vcl::PixelFormat getPixelFormat() const
AlphaMask CreateAlphaMask(const Color &rTransColor) const
Create on-off alpha mask from bitmap.
bool Mirror(BmpMirrorFlags nMirrorFlags)
Mirror the bitmap.
void SetEmpty()
sal_uInt8 GetBlue() const
void Merge(const Color &rMergeColor, sal_uInt8 cTransparency)
sal_uInt8 GetAlpha() const
sal_uInt8 GetRed() const
bool IsTransparent() const
sal_uInt8 GetGreen() const
void SetAlpha(sal_uInt8 nAlpha)
static VCL_DLLPUBLIC ImageTree & get()
Definition: ImageTree.cxx:16
VCL_DLLPUBLIC bool loadImage(OUString const &name, OUString const &style, BitmapEx &bitmap, bool localized, const ImageLoadFlags eFlags=ImageLoadFlags::NONE)
Definition: ImageTree.cxx:50
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:170
void DrawBitmapEx(const Point &rDestPt, const BitmapEx &rBitmapEx)
Definition: bitmapex.cxx:33
virtual std::shared_ptr< SalBitmap > CreateSalBitmap()=0
bool IsEmpty() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
OUString DetermineIconTheme() const
Determine which icon theme should be used.
bool good() const
A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for refer...
Definition: vclptr.hxx:58
bool decompose(B2DTuple &rScale, B2DTuple &rTranslate, double &rRotate, double &rShearX) const
void translate(double fX, double fY)
void scale(double fX, double fY)
BASEGFX_DLLPUBLIC void transform(const B2DHomMatrix &rMatrix)
B2DVector getRange() const
B2DPoint getMinimum() const
const BColorModifierSharedPtr & getBColorModifier(sal_uInt32 nIndex) const
sal_uInt32 count() const
const ::basegfx::BColor & getBColor() const
double getBlue() const
double getRed() const
double getGreen() const
TYPE getWidth() const
TYPE getMinX() const
TYPE getMinY() const
TYPE getHeight() const
void SetSize(sal_uInt16 nNewSize)
void Scale(double fScaleX, double fScaleY)
constexpr tools::Long GetWidth() const
constexpr tools::Long Top() const
constexpr tools::Long Right() const
constexpr tools::Long GetHeight() const
tools::Rectangle & Intersection(const tools::Rectangle &rRect)
constexpr tools::Long Left() const
constexpr tools::Long Bottom() const
bool write(const BitmapEx &rBitmap)
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
constexpr ::Color COL_ALPHA_OPAQUE(0xff, 0xff, 0xff)
constexpr ::Color COL_ALPHA_TRANSPARENT(0x00, 0x00, 0x00)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
constexpr ::Color COL_TRANSPARENT(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
int nCount
float y
float x
BmpMirrorFlags
uno_Any a
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
bool more(const T &rfValA, const T &rfValB)
B2DHomMatrix createScaleTranslateB2DHomMatrix(double fScaleX, double fScaleY, double fTranslateX, double fTranslateY)
B2DHomMatrix createScaleB2DHomMatrix(double fScaleX, double fScaleY)
std::shared_ptr< BColorModifier > BColorModifierSharedPtr
B2IRange fround(const B2DRange &rRange)
int i
long Long
constexpr bool isPalettePixelFormat(PixelFormat ePixelFormat)
Is it a pixel format that forces creation of a palette.
Definition: BitmapTypes.hxx:28
PixelFormat
Pixel format of the bitmap in bits per pixel.
Definition: BitmapTypes.hxx:20
sal_uIntPtr sal_uLong
Color m_aLastColorTopLeft
Definition: svdata.hxx:353
BitmapEx m_aLastResult
Definition: svdata.hxx:357
Color m_aLastColorBottomRight
Definition: svdata.hxx:355
Color m_aLastColorTopRight
Definition: svdata.hxx:354
Size m_aLastSize
Definition: svdata.hxx:351
sal_uInt8 m_nLastAlpha
Definition: svdata.hxx:352
Color m_aLastColorBottomLeft
Definition: svdata.hxx:356
SalInstance * mpDefInst
Definition: svdata.hxx:389
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:77
BlendFrameCache * ImplGetBlendFrameCache()
Definition: svdata.cxx:325
unsigned char sal_uInt8
signed char sal_Int8