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