1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <osl/diagnose.h>
#include <vcl/bitmap.hxx>
#include <vcl/bitmapaccess.hxx>
#include <vcl/outdev.hxx>
#include <svdata.hxx>
#include <salinst.hxx>
#include <salbmp.hxx>
#include <bitmapwriteaccess.hxx>
#include <algorithm>
#include <memory>
Bitmap::Bitmap()
{
}
Bitmap::Bitmap(const Bitmap& rBitmap)
: mxSalBmp(rBitmap.mxSalBmp)
, maPrefMapMode(rBitmap.maPrefMapMode)
, maPrefSize(rBitmap.maPrefSize)
{
}
Bitmap::Bitmap(SalBitmap* pSalBitmap)
: mxSalBmp(pSalBitmap)
, maPrefMapMode(MapMode(MapUnit::MapPixel))
, maPrefSize(mxSalBmp->GetSize())
{
}
Bitmap::Bitmap( const Size& rSizePixel, sal_uInt16 nBitCount, const BitmapPalette* pPal )
{
if (rSizePixel.Width() && rSizePixel.Height())
{
BitmapPalette aPal;
BitmapPalette* pRealPal = nullptr;
if( nBitCount <= 8 )
{
if( !pPal )
{
if( 1 == nBitCount )
{
aPal.SetEntryCount( 2 );
aPal[ 0 ] = COL_BLACK;
aPal[ 1 ] = COL_WHITE;
}
else if( ( 4 == nBitCount ) || ( 8 == nBitCount ) )
{
aPal.SetEntryCount( 1 << nBitCount );
aPal[ 0 ] = COL_BLACK;
aPal[ 1 ] = COL_BLUE;
aPal[ 2 ] = COL_GREEN;
aPal[ 3 ] = COL_CYAN;
aPal[ 4 ] = COL_RED;
aPal[ 5 ] = COL_MAGENTA;
aPal[ 6 ] = COL_BROWN;
aPal[ 7 ] = COL_GRAY;
aPal[ 8 ] = COL_LIGHTGRAY;
aPal[ 9 ] = COL_LIGHTBLUE;
aPal[ 10 ] = COL_LIGHTGREEN;
aPal[ 11 ] = COL_LIGHTCYAN;
aPal[ 12 ] = COL_LIGHTRED;
aPal[ 13 ] = COL_LIGHTMAGENTA;
aPal[ 14 ] = COL_YELLOW;
aPal[ 15 ] = COL_WHITE;
// Create dither palette
if( 8 == nBitCount )
{
sal_uInt16 nActCol = 16;
for( sal_uInt16 nB = 0; nB < 256; nB += 51 )
for( sal_uInt16 nG = 0; nG < 256; nG += 51 )
for( sal_uInt16 nR = 0; nR < 256; nR += 51 )
aPal[ nActCol++ ] = BitmapColor( static_cast<sal_uInt8>(nR), static_cast<sal_uInt8>(nG), static_cast<sal_uInt8>(nB) );
// Set standard Office colors
aPal[ nActCol++ ] = BitmapColor( 0, 184, 255 );
}
}
}
else
pRealPal = const_cast<BitmapPalette*>(pPal);
}
mxSalBmp.reset(ImplGetSVData()->mpDefInst->CreateSalBitmap());
mxSalBmp->Create( rSizePixel, nBitCount, pRealPal ? *pRealPal : aPal );
}
}
Bitmap::~Bitmap()
{
}
const BitmapPalette& Bitmap::GetGreyPalette( int nEntries )
{
static BitmapPalette aGreyPalette2;
static BitmapPalette aGreyPalette4;
static BitmapPalette aGreyPalette16;
static BitmapPalette aGreyPalette256;
// Create greyscale palette with 2, 4, 16 or 256 entries
if( 2 == nEntries || 4 == nEntries || 16 == nEntries || 256 == nEntries )
{
if( 2 == nEntries )
{
if( !aGreyPalette2.GetEntryCount() )
{
aGreyPalette2.SetEntryCount( 2 );
aGreyPalette2[ 0 ] = BitmapColor( 0, 0, 0 );
aGreyPalette2[ 1 ] = BitmapColor( 255, 255, 255 );
}
return aGreyPalette2;
}
else if( 4 == nEntries )
{
if( !aGreyPalette4.GetEntryCount() )
{
aGreyPalette4.SetEntryCount( 4 );
aGreyPalette4[ 0 ] = BitmapColor( 0, 0, 0 );
aGreyPalette4[ 1 ] = BitmapColor( 85, 85, 85 );
aGreyPalette4[ 2 ] = BitmapColor( 170, 170, 170 );
aGreyPalette4[ 3 ] = BitmapColor( 255, 255, 255 );
}
return aGreyPalette4;
}
else if( 16 == nEntries )
{
if( !aGreyPalette16.GetEntryCount() )
{
sal_uInt8 cGrey = 0;
sal_uInt8 const cGreyInc = 17;
aGreyPalette16.SetEntryCount( 16 );
for( sal_uInt16 i = 0; i < 16; i++, cGrey = sal::static_int_cast<sal_uInt8>(cGrey + cGreyInc) )
aGreyPalette16[ i ] = BitmapColor( cGrey, cGrey, cGrey );
}
return aGreyPalette16;
}
else
{
if( !aGreyPalette256.GetEntryCount() )
{
aGreyPalette256.SetEntryCount( 256 );
for( sal_uInt16 i = 0; i < 256; i++ )
aGreyPalette256[ i ] = BitmapColor( static_cast<sal_uInt8>(i), static_cast<sal_uInt8>(i), static_cast<sal_uInt8>(i) );
}
return aGreyPalette256;
}
}
else
{
OSL_FAIL( "Bitmap::GetGreyPalette: invalid entry count (2/4/16/256 allowed)" );
return aGreyPalette2;
}
}
bool BitmapPalette::IsGreyPalette() const
{
const int nEntryCount = GetEntryCount();
if( !nEntryCount ) // NOTE: an empty palette means 1:1 mapping
return true;
// See above: only certain entry values will result in a valid call to GetGreyPalette
if( nEntryCount == 2 || nEntryCount == 4 || nEntryCount == 16 || nEntryCount == 256 )
{
const BitmapPalette& rGreyPalette = Bitmap::GetGreyPalette( nEntryCount );
if( rGreyPalette == *this )
return true;
}
bool bRet = false;
// TODO: is it worth to compare the entries for the general case?
if (nEntryCount == 2)
{
const BitmapColor& rCol0(maBitmapColor[0]);
const BitmapColor& rCol1(maBitmapColor[1]);
bRet = rCol0.GetRed() == rCol0.GetGreen() && rCol0.GetRed() == rCol0.GetBlue() &&
rCol1.GetRed() == rCol1.GetGreen() && rCol1.GetRed() == rCol1.GetBlue();
}
return bRet;
}
Bitmap& Bitmap::operator=( const Bitmap& rBitmap )
{
if (this == &rBitmap)
return *this;
maPrefSize = rBitmap.maPrefSize;
maPrefMapMode = rBitmap.maPrefMapMode;
mxSalBmp = rBitmap.mxSalBmp;
return *this;
}
Bitmap& Bitmap::operator=( Bitmap&& rBitmap )
{
maPrefSize = std::move(rBitmap.maPrefSize);
maPrefMapMode = std::move(rBitmap.maPrefMapMode);
mxSalBmp = std::move(rBitmap.mxSalBmp);
return *this;
}
bool Bitmap::operator==( const Bitmap& rBmp ) const
{
if (rBmp.mxSalBmp == mxSalBmp) // Includes both are nullptr
return true;
if (!rBmp.mxSalBmp || !mxSalBmp)
return false;
if (rBmp.mxSalBmp->GetSize() != mxSalBmp->GetSize() ||
rBmp.mxSalBmp->GetBitCount() != mxSalBmp->GetBitCount())
return false;
BitmapChecksum aChecksum1, aChecksum2;
rBmp.mxSalBmp->GetChecksum(aChecksum1);
mxSalBmp->GetChecksum(aChecksum2);
return aChecksum1 == aChecksum2;
}
void Bitmap::SetEmpty()
{
maPrefMapMode = MapMode();
maPrefSize = Size();
mxSalBmp.reset();
}
Size Bitmap::GetSizePixel() const
{
return( mxSalBmp ? mxSalBmp->GetSize() : Size() );
}
sal_uInt16 Bitmap::GetBitCount() const
{
if (!mxSalBmp)
return 0;
sal_uInt16 nBitCount = mxSalBmp->GetBitCount();
return ( nBitCount <= 4 ) ? ( ( nBitCount <= 1 ) ? 1 : 4 ):
( ( nBitCount <= 8 ) ? 8 : 24);
}
bool Bitmap::HasGreyPalette() const
{
const sal_uInt16 nBitCount = GetBitCount();
bool bRet = nBitCount == 1;
ScopedInfoAccess pIAcc(const_cast<Bitmap&>(*this));
if( pIAcc )
{
bRet = pIAcc->HasPalette() && pIAcc->GetPalette().IsGreyPalette();
}
return bRet;
}
BitmapChecksum Bitmap::GetChecksum() const
{
BitmapChecksum nRet = 0;
if( mxSalBmp )
{
mxSalBmp->GetChecksum(nRet);
if (!nRet)
{
// nRet == 0 => probably, we were not able to acquire
// the buffer in SalBitmap::updateChecksum;
// so, we need to update the imp bitmap for this bitmap instance
// as we do in BitmapInfoAccess::ImplCreate
std::shared_ptr<SalBitmap> xNewImpBmp(ImplGetSVData()->mpDefInst->CreateSalBitmap());
if (xNewImpBmp->Create(*mxSalBmp, GetBitCount()))
{
Bitmap* pThis = const_cast<Bitmap*>(this);
pThis->mxSalBmp = xNewImpBmp;
mxSalBmp->GetChecksum(nRet);
}
}
}
return nRet;
}
void Bitmap::ImplMakeUnique()
{
if (mxSalBmp && mxSalBmp.use_count() > 1)
{
std::shared_ptr<SalBitmap> xOldImpBmp = mxSalBmp;
mxSalBmp.reset(ImplGetSVData()->mpDefInst->CreateSalBitmap());
mxSalBmp->Create(*xOldImpBmp);
}
}
void Bitmap::ReassignWithSize(const Bitmap& rBitmap)
{
const Size aOldSizePix(GetSizePixel());
const Size aNewSizePix(rBitmap.GetSizePixel());
const MapMode aOldMapMode(maPrefMapMode);
Size aNewPrefSize;
if ((aOldSizePix != aNewSizePix) && aOldSizePix.Width() && aOldSizePix.Height())
{
aNewPrefSize.setWidth(FRound(maPrefSize.Width() * aNewSizePix.Width() / aOldSizePix.Width()));
aNewPrefSize.setHeight(FRound(maPrefSize.Height() * aNewSizePix.Height() / aOldSizePix.Height()));
}
else
{
aNewPrefSize = maPrefSize;
}
*this = rBitmap;
maPrefSize = aNewPrefSize;
maPrefMapMode = aOldMapMode;
}
void Bitmap::ImplSetSalBitmap(const std::shared_ptr<SalBitmap>& xImpBmp)
{
mxSalBmp = xImpBmp;
}
BitmapInfoAccess* Bitmap::AcquireInfoAccess()
{
BitmapInfoAccess* pInfoAccess = new BitmapInfoAccess( *this );
if( !*pInfoAccess )
{
delete pInfoAccess;
pInfoAccess = nullptr;
}
return pInfoAccess;
}
BitmapReadAccess* Bitmap::AcquireReadAccess()
{
BitmapReadAccess* pReadAccess = new BitmapReadAccess( *this );
if( !*pReadAccess )
{
delete pReadAccess;
pReadAccess = nullptr;
}
return pReadAccess;
}
BitmapWriteAccess* Bitmap::AcquireWriteAccess()
{
BitmapWriteAccess* pWriteAccess = new BitmapWriteAccess( *this );
if( !*pWriteAccess )
{
delete pWriteAccess;
pWriteAccess = nullptr;
}
return pWriteAccess;
}
void Bitmap::ReleaseAccess( BitmapInfoAccess* pBitmapAccess )
{
delete pBitmapAccess;
}
bool Bitmap::Crop( const tools::Rectangle& rRectPixel )
{
const Size aSizePix( GetSizePixel() );
tools::Rectangle aRect( rRectPixel );
bool bRet = false;
aRect.Intersection( tools::Rectangle( Point(), aSizePix ) );
if( !aRect.IsEmpty() && aSizePix != aRect.GetSize())
{
ScopedReadAccess pReadAcc(*this);
if( pReadAcc )
{
const tools::Rectangle aNewRect( Point(), aRect.GetSize() );
Bitmap aNewBmp( aNewRect.GetSize(), GetBitCount(), &pReadAcc->GetPalette() );
BitmapScopedWriteAccess pWriteAcc(aNewBmp);
if( pWriteAcc )
{
const long nOldX = aRect.Left();
const long nOldY = aRect.Top();
const long nNewWidth = aNewRect.GetWidth();
const long nNewHeight = aNewRect.GetHeight();
for( long nY = 0, nY2 = nOldY; nY < nNewHeight; nY++, nY2++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nY);
Scanline pScanlineRead = pReadAcc->GetScanline(nY2);
for( long nX = 0, nX2 = nOldX; nX < nNewWidth; nX++, nX2++ )
pWriteAcc->SetPixelOnData( pScanline, nX, pReadAcc->GetPixelFromData( pScanlineRead, nX2 ) );
}
pWriteAcc.reset();
bRet = true;
}
pReadAcc.reset();
if( bRet )
ReassignWithSize( aNewBmp );
}
}
return bRet;
};
bool Bitmap::CopyPixel( const tools::Rectangle& rRectDst,
const tools::Rectangle& rRectSrc, const Bitmap* pBmpSrc )
{
const Size aSizePix( GetSizePixel() );
tools::Rectangle aRectDst( rRectDst );
bool bRet = false;
aRectDst.Intersection( tools::Rectangle( Point(), aSizePix ) );
if( !aRectDst.IsEmpty() )
{
if( pBmpSrc && ( pBmpSrc->mxSalBmp != mxSalBmp ) )
{
Bitmap* pSrc = const_cast<Bitmap*>(pBmpSrc);
const Size aCopySizePix( pSrc->GetSizePixel() );
tools::Rectangle aRectSrc( rRectSrc );
const sal_uInt16 nSrcBitCount = pBmpSrc->GetBitCount();
const sal_uInt16 nDstBitCount = GetBitCount();
if( nSrcBitCount > nDstBitCount )
{
int nNextIndex = 0;
if( ( nSrcBitCount == 24 ) && ( nDstBitCount < 24 ) )
Convert( BmpConversion::N24Bit );
else if( ( nSrcBitCount == 8 ) && ( nDstBitCount < 8 ) )
{
Convert( BmpConversion::N8BitColors );
nNextIndex = 16;
}
else if( ( nSrcBitCount == 4 ) && ( nDstBitCount < 4 ) )
{
Convert( BmpConversion::N4BitColors );
nNextIndex = 2;
}
if( nNextIndex )
{
ScopedReadAccess pSrcAcc(*pSrc);
BitmapScopedWriteAccess pDstAcc(*this);
if( pSrcAcc && pDstAcc )
{
const int nSrcCount = pDstAcc->GetPaletteEntryCount();
const int nDstCount = 1 << nDstBitCount;
for (int i = 0; ( i < nSrcCount ) && ( nNextIndex < nSrcCount ); ++i)
{
const BitmapColor& rSrcCol = pSrcAcc->GetPaletteColor( static_cast<sal_uInt16>(i) );
bool bFound = false;
for (int j = 0; j < nDstCount; ++j)
{
if( rSrcCol == pDstAcc->GetPaletteColor( static_cast<sal_uInt16>(j) ) )
{
bFound = true;
break;
}
}
if( !bFound )
pDstAcc->SetPaletteColor( static_cast<sal_uInt16>(nNextIndex++), rSrcCol );
}
}
}
}
aRectSrc.Intersection( tools::Rectangle( Point(), aCopySizePix ) );
if( !aRectSrc.IsEmpty() )
{
ScopedReadAccess pReadAcc(*pSrc);
if( pReadAcc )
{
BitmapScopedWriteAccess pWriteAcc(*this);
if( pWriteAcc )
{
const long nWidth = std::min( aRectSrc.GetWidth(), aRectDst.GetWidth() );
const long nHeight = std::min( aRectSrc.GetHeight(), aRectDst.GetHeight() );
const long nSrcEndX = aRectSrc.Left() + nWidth;
const long nSrcEndY = aRectSrc.Top() + nHeight;
long nDstY = aRectDst.Top();
if( pReadAcc->HasPalette() && pWriteAcc->HasPalette() )
{
const sal_uInt16 nCount = pReadAcc->GetPaletteEntryCount();
std::unique_ptr<sal_uInt8[]> pMap(new sal_uInt8[ nCount ]);
// Create index map for the color table, as the bitmap should be copied
// retaining it's color information relatively well
for( sal_uInt16 i = 0; i < nCount; i++ )
pMap[ i ] = static_cast<sal_uInt8>(pWriteAcc->GetBestPaletteIndex( pReadAcc->GetPaletteColor( i ) ));
for( long nSrcY = aRectSrc.Top(); nSrcY < nSrcEndY; nSrcY++, nDstY++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nDstY);
Scanline pScanlineRead = pReadAcc->GetScanline(nSrcY);
for( long nSrcX = aRectSrc.Left(), nDstX = aRectDst.Left(); nSrcX < nSrcEndX; nSrcX++, nDstX++ )
pWriteAcc->SetPixelOnData( pScanline, nDstX, BitmapColor( pMap[ pReadAcc->GetIndexFromData( pScanlineRead, nSrcX ) ] ));
}
}
else if( pReadAcc->HasPalette() )
{
for( long nSrcY = aRectSrc.Top(); nSrcY < nSrcEndY; nSrcY++, nDstY++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nDstY);
Scanline pScanlineRead = pReadAcc->GetScanline(nSrcY);
for( long nSrcX = aRectSrc.Left(), nDstX = aRectDst.Left(); nSrcX < nSrcEndX; nSrcX++, nDstX++ )
pWriteAcc->SetPixelOnData( pScanline, nDstX, pReadAcc->GetPaletteColor( pReadAcc->GetIndexFromData( pScanlineRead, nSrcX ) ) );
}
}
else
for( long nSrcY = aRectSrc.Top(); nSrcY < nSrcEndY; nSrcY++, nDstY++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nDstY);
Scanline pScanlineRead = pReadAcc->GetScanline(nSrcY);
for( long nSrcX = aRectSrc.Left(), nDstX = aRectDst.Left(); nSrcX < nSrcEndX; nSrcX++, nDstX++ )
pWriteAcc->SetPixelOnData( pScanline, nDstX, pReadAcc->GetPixelFromData( pScanlineRead, nSrcX ) );
}
pWriteAcc.reset();
bRet = ( nWidth > 0 ) && ( nHeight > 0 );
}
pReadAcc.reset();
}
}
}
else
{
tools::Rectangle aRectSrc( rRectSrc );
aRectSrc.Intersection( tools::Rectangle( Point(), aSizePix ) );
if( !aRectSrc.IsEmpty() && ( aRectSrc != aRectDst ) )
{
BitmapScopedWriteAccess pWriteAcc(*this);
if( pWriteAcc )
{
const long nWidth = std::min( aRectSrc.GetWidth(), aRectDst.GetWidth() );
const long nHeight = std::min( aRectSrc.GetHeight(), aRectDst.GetHeight() );
const long nSrcX = aRectSrc.Left();
const long nSrcY = aRectSrc.Top();
const long nSrcEndX1 = nSrcX + nWidth - 1;
const long nSrcEndY1 = nSrcY + nHeight - 1;
const long nDstX = aRectDst.Left();
const long nDstY = aRectDst.Top();
const long nDstEndX1 = nDstX + nWidth - 1;
const long nDstEndY1 = nDstY + nHeight - 1;
if( ( nDstX <= nSrcX ) && ( nDstY <= nSrcY ) )
{
for( long nY = nSrcY, nYN = nDstY; nY <= nSrcEndY1; nY++, nYN++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nYN);
Scanline pScanlineSrc = pWriteAcc->GetScanline(nY);
for( long nX = nSrcX, nXN = nDstX; nX <= nSrcEndX1; nX++, nXN++ )
pWriteAcc->SetPixelOnData( pScanline, nXN, pWriteAcc->GetPixelFromData( pScanlineSrc, nX ) );
}
}
else if( ( nDstX <= nSrcX ) && ( nDstY >= nSrcY ) )
{
for( long nY = nSrcEndY1, nYN = nDstEndY1; nY >= nSrcY; nY--, nYN-- )
{
Scanline pScanline = pWriteAcc->GetScanline(nYN);
Scanline pScanlineSrc = pWriteAcc->GetScanline(nY);
for( long nX = nSrcX, nXN = nDstX; nX <= nSrcEndX1; nX++, nXN++ )
pWriteAcc->SetPixelOnData( pScanline, nXN, pWriteAcc->GetPixelFromData( pScanlineSrc, nX ) );
}
}
else if( ( nDstX >= nSrcX ) && ( nDstY <= nSrcY ) )
{
for( long nY = nSrcY, nYN = nDstY; nY <= nSrcEndY1; nY++, nYN++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nYN);
Scanline pScanlineSrc = pWriteAcc->GetScanline(nY);
for( long nX = nSrcEndX1, nXN = nDstEndX1; nX >= nSrcX; nX--, nXN-- )
pWriteAcc->SetPixelOnData( pScanline, nXN, pWriteAcc->GetPixelFromData( pScanlineSrc, nX ) );
}
}
else
{
for( long nY = nSrcEndY1, nYN = nDstEndY1; nY >= nSrcY; nY--, nYN-- )
{
Scanline pScanline = pWriteAcc->GetScanline(nYN);
Scanline pScanlineSrc = pWriteAcc->GetScanline(nY);
for( long nX = nSrcEndX1, nXN = nDstEndX1; nX >= nSrcX; nX--, nXN-- )
pWriteAcc->SetPixelOnData( pScanline, nXN, pWriteAcc->GetPixelFromData( pScanlineSrc, nX ) );
}
}
pWriteAcc.reset();
bRet = true;
}
}
}
}
return bRet;
}
bool Bitmap::CopyPixel_AlphaOptimized( const tools::Rectangle& rRectDst, const tools::Rectangle& rRectSrc,
const Bitmap* pBmpSrc )
{
// Note: this code is copied from Bitmap::CopyPixel but avoids any palette lookups
// This optimization is possible because the palettes of AlphaMasks are always identical (8bit GreyPalette, see ctor)
const Size aSizePix( GetSizePixel() );
tools::Rectangle aRectDst( rRectDst );
bool bRet = false;
aRectDst.Intersection( tools::Rectangle( Point(), aSizePix ) );
if( !aRectDst.IsEmpty() )
{
if( pBmpSrc && ( pBmpSrc->mxSalBmp != mxSalBmp ) )
{
Bitmap* pSrc = const_cast<Bitmap*>(pBmpSrc);
const Size aCopySizePix( pSrc->GetSizePixel() );
tools::Rectangle aRectSrc( rRectSrc );
aRectSrc.Intersection( tools::Rectangle( Point(), aCopySizePix ) );
if( !aRectSrc.IsEmpty() )
{
ScopedReadAccess pReadAcc(*pSrc);
if( pReadAcc )
{
BitmapScopedWriteAccess pWriteAcc(*this);
if( pWriteAcc )
{
const long nWidth = std::min( aRectSrc.GetWidth(), aRectDst.GetWidth() );
const long nHeight = std::min( aRectSrc.GetHeight(), aRectDst.GetHeight() );
const long nSrcEndX = aRectSrc.Left() + nWidth;
const long nSrcEndY = aRectSrc.Top() + nHeight;
long nDstY = aRectDst.Top();
for( long nSrcY = aRectSrc.Top(); nSrcY < nSrcEndY; nSrcY++, nDstY++)
{
Scanline pScanline = pWriteAcc->GetScanline(nDstY);
Scanline pScanlineRead = pReadAcc->GetScanline(nSrcY);
for( long nSrcX = aRectSrc.Left(), nDstX = aRectDst.Left(); nSrcX < nSrcEndX; nSrcX++, nDstX++ )
pWriteAcc->SetPixelOnData( pScanline, nDstX, pReadAcc->GetPixelFromData( pScanlineRead, nSrcX ) );
}
pWriteAcc.reset();
bRet = ( nWidth > 0 ) && ( nHeight > 0 );
}
pReadAcc.reset();
}
}
}
else
{
tools::Rectangle aRectSrc( rRectSrc );
aRectSrc.Intersection( tools::Rectangle( Point(), aSizePix ) );
if( !aRectSrc.IsEmpty() && ( aRectSrc != aRectDst ) )
{
BitmapScopedWriteAccess pWriteAcc(*this);
if( pWriteAcc )
{
const long nWidth = std::min( aRectSrc.GetWidth(), aRectDst.GetWidth() );
const long nHeight = std::min( aRectSrc.GetHeight(), aRectDst.GetHeight() );
const long nSrcX = aRectSrc.Left();
const long nSrcY = aRectSrc.Top();
const long nSrcEndX1 = nSrcX + nWidth - 1;
const long nSrcEndY1 = nSrcY + nHeight - 1;
const long nDstX = aRectDst.Left();
const long nDstY = aRectDst.Top();
const long nDstEndX1 = nDstX + nWidth - 1;
const long nDstEndY1 = nDstY + nHeight - 1;
if( ( nDstX <= nSrcX ) && ( nDstY <= nSrcY ) )
{
for( long nY = nSrcY, nYN = nDstY; nY <= nSrcEndY1; nY++, nYN++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nYN);
Scanline pScanlineSrc = pWriteAcc->GetScanline(nY);
for( long nX = nSrcX, nXN = nDstX; nX <= nSrcEndX1; nX++, nXN++ )
pWriteAcc->SetPixelOnData( pScanline, nXN, pWriteAcc->GetPixelFromData( pScanlineSrc, nX ) );
}
}
else if( ( nDstX <= nSrcX ) && ( nDstY >= nSrcY ) )
{
for( long nY = nSrcEndY1, nYN = nDstEndY1; nY >= nSrcY; nY--, nYN-- )
{
Scanline pScanline = pWriteAcc->GetScanline(nYN);
Scanline pScanlineSrc = pWriteAcc->GetScanline(nY);
for( long nX = nSrcX, nXN = nDstX; nX <= nSrcEndX1; nX++, nXN++ )
pWriteAcc->SetPixelOnData( pScanline, nXN, pWriteAcc->GetPixelFromData( pScanlineSrc, nX ) );
}
}
else if( ( nDstX >= nSrcX ) && ( nDstY <= nSrcY ) )
{
for( long nY = nSrcY, nYN = nDstY; nY <= nSrcEndY1; nY++, nYN++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nYN);
Scanline pScanlineSrc = pWriteAcc->GetScanline(nY);
for( long nX = nSrcEndX1, nXN = nDstEndX1; nX >= nSrcX; nX--, nXN-- )
pWriteAcc->SetPixelOnData( pScanline, nXN, pWriteAcc->GetPixelFromData( pScanlineSrc, nX ) );
}
}
else
{
for( long nY = nSrcEndY1, nYN = nDstEndY1; nY >= nSrcY; nY--, nYN-- )
{
Scanline pScanline = pWriteAcc->GetScanline(nYN);
Scanline pScanlineSrc = pWriteAcc->GetScanline(nY);
for( long nX = nSrcEndX1, nXN = nDstEndX1; nX >= nSrcX; nX--, nXN-- )
pWriteAcc->SetPixelOnData( pScanline, nXN, pWriteAcc->GetPixelFromData( pScanlineSrc, nX ) );
}
}
pWriteAcc.reset();
bRet = true;
}
}
}
}
return bRet;
}
bool Bitmap::Expand( sal_uLong nDX, sal_uLong nDY, const Color* pInitColor )
{
bool bRet = false;
if( nDX || nDY )
{
const Size aSizePixel( GetSizePixel() );
const long nWidth = aSizePixel.Width();
const long nHeight = aSizePixel.Height();
const Size aNewSize( nWidth + nDX, nHeight + nDY );
ScopedReadAccess pReadAcc(*this);
if( pReadAcc )
{
BitmapPalette aBmpPal( pReadAcc->GetPalette() );
Bitmap aNewBmp( aNewSize, GetBitCount(), &aBmpPal );
BitmapScopedWriteAccess pWriteAcc(aNewBmp);
if( pWriteAcc )
{
BitmapColor aColor;
const long nNewX = nWidth;
const long nNewY = nHeight;
const long nNewWidth = pWriteAcc->Width();
const long nNewHeight = pWriteAcc->Height();
long nX;
long nY;
if( pInitColor )
aColor = pWriteAcc->GetBestMatchingColor( *pInitColor );
for( nY = 0; nY < nHeight; nY++ )
{
pWriteAcc->CopyScanline( nY, *pReadAcc );
if( pInitColor && nDX )
{
Scanline pScanline = pWriteAcc->GetScanline(nY);
for( nX = nNewX; nX < nNewWidth; nX++ )
pWriteAcc->SetPixelOnData( pScanline, nX, aColor );
}
}
if( pInitColor && nDY )
for( nY = nNewY; nY < nNewHeight; nY++ )
{
Scanline pScanline = pWriteAcc->GetScanline(nY);
for( nX = 0; nX < nNewWidth; nX++ )
pWriteAcc->SetPixelOnData( pScanline, nX, aColor );
}
pWriteAcc.reset();
bRet = true;
}
pReadAcc.reset();
if (bRet)
ReassignWithSize(aNewBmp);
}
}
return bRet;
}
Bitmap Bitmap::CreateDisplayBitmap( OutputDevice* pDisplay )
{
Bitmap aDispBmp( *this );
SalGraphics* pDispGraphics = pDisplay->GetGraphics();
if( mxSalBmp && pDispGraphics )
{
std::shared_ptr<SalBitmap> xImpDispBmp(ImplGetSVData()->mpDefInst->CreateSalBitmap());
if (xImpDispBmp->Create(*mxSalBmp, pDispGraphics))
aDispBmp.ImplSetSalBitmap(xImpDispBmp);
}
return aDispBmp;
}
bool Bitmap::GetSystemData( BitmapSystemData& rData ) const
{
bool bRet = false;
if (mxSalBmp)
{
SalBitmap* pSalBitmap = mxSalBmp.get();
if( pSalBitmap )
bRet = pSalBitmap->GetSystemData( rData );
}
return bRet;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|