diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2015-03-29 14:35:11 +0900 |
---|---|---|
committer | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2015-03-29 18:26:31 +0900 |
commit | 024bc58aefdce2becb170939eac68e18cce2977c (patch) | |
tree | 9c70bb27395211a86efb1d05ac71438bacd0f5b8 | |
parent | 0bd502af47b53bc4340df7874bd726361e3bbad9 (diff) |
vcl: test for bitmap convert, use scoped read/write access
Change-Id: I702d3e1ced356efce28ebfec141776f07645b2f9
-rw-r--r-- | vcl/qa/cppunit/BitmapTest.cxx | 36 | ||||
-rw-r--r-- | vcl/source/gdi/bitmap.cxx | 43 | ||||
-rw-r--r-- | vcl/source/gdi/bitmap3.cxx | 179 |
3 files changed, 150 insertions, 108 deletions
diff --git a/vcl/qa/cppunit/BitmapTest.cxx b/vcl/qa/cppunit/BitmapTest.cxx index cc8534f22a6a..dc2070ddcae3 100644 --- a/vcl/qa/cppunit/BitmapTest.cxx +++ b/vcl/qa/cppunit/BitmapTest.cxx @@ -25,13 +25,49 @@ namespace class BitmapTest : public CppUnit::TestFixture { + void testConvert(); void testScale(); CPPUNIT_TEST_SUITE(BitmapTest); + CPPUNIT_TEST(testConvert); CPPUNIT_TEST(testScale); CPPUNIT_TEST_SUITE_END(); }; +void BitmapTest::testConvert() +{ + Bitmap aBitmap(Size(10, 10), 8); + + aBitmap.Erase(COL_LIGHTGRAYBLUE); + + CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(8), aBitmap.GetBitCount()); + { + Bitmap::ScopedReadAccess pReadAccess(aBitmap); + CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(8), pReadAccess->GetBitCount()); + CPPUNIT_ASSERT_EQUAL(static_cast<sal_uLong>(10), pReadAccess->GetScanlineSize()); + CPPUNIT_ASSERT(pReadAccess->HasPalette()); + const BitmapColor& rColor = pReadAccess->GetPaletteColor(pReadAccess->GetPixelIndex(1, 1)); + CPPUNIT_ASSERT_EQUAL(sal_Int32(204), sal_Int32(rColor.GetRed())); + CPPUNIT_ASSERT_EQUAL(sal_Int32(204), sal_Int32(rColor.GetGreen())); + CPPUNIT_ASSERT_EQUAL(sal_Int32(255), sal_Int32(rColor.GetBlue())); + } + + aBitmap.Convert(BMP_CONVERSION_24BIT); + + CPPUNIT_ASSERT_EQUAL(sal_uInt16(24), aBitmap.GetBitCount()); + { + Bitmap::ScopedReadAccess pReadAccess(aBitmap); + // 24 bit Bitmap on SVP backend uses 32bit BGRX format + CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt16>(32), pReadAccess->GetBitCount()); + CPPUNIT_ASSERT_EQUAL(sal_uLong(40), pReadAccess->GetScanlineSize()); + CPPUNIT_ASSERT(!pReadAccess->HasPalette()); + Color aColor = pReadAccess->GetPixel(0, 0); + CPPUNIT_ASSERT_EQUAL(sal_Int32(204), sal_Int32(aColor.GetRed())); + CPPUNIT_ASSERT_EQUAL(sal_Int32(204), sal_Int32(aColor.GetGreen())); + CPPUNIT_ASSERT_EQUAL(sal_Int32(255), sal_Int32(aColor.GetBlue())); + } +} + void BitmapTest::testScale() { bool bExportBitmap(false); diff --git a/vcl/source/gdi/bitmap.cxx b/vcl/source/gdi/bitmap.cxx index 2315ab1dbdc7..71ae7a3ddba8 100644 --- a/vcl/source/gdi/bitmap.cxx +++ b/vcl/source/gdi/bitmap.cxx @@ -443,52 +443,52 @@ void Bitmap::ReleaseAccess( BitmapInfoAccess* pBitmapAccess ) delete pBitmapAccess; } -bool Bitmap::Erase( const Color& rFillColor ) +bool Bitmap::Erase(const Color& rFillColor) { - if( !(*this) ) + if (IsEmpty()) return true; - BitmapWriteAccess* pWriteAcc = AcquireWriteAccess(); - bool bRet = false; + Bitmap::ScopedWriteAccess pWriteAcc(*this); + bool bRet = false; - if( pWriteAcc ) + if (pWriteAcc) { const sal_uLong nFormat = pWriteAcc->GetScanlineFormat(); - sal_uInt8 cIndex = 0; - bool bFast = false; + sal_uInt8 cIndex = 0; + bool bFast = false; - switch( nFormat ) + switch (nFormat) { - case( BMP_FORMAT_1BIT_MSB_PAL ): - case( BMP_FORMAT_1BIT_LSB_PAL ): + case BMP_FORMAT_1BIT_MSB_PAL: + case BMP_FORMAT_1BIT_LSB_PAL: { - cIndex = (sal_uInt8) pWriteAcc->GetBestPaletteIndex( rFillColor ); - cIndex = ( cIndex ? 255 : 0 ); + cIndex = static_cast<sal_uInt8>(pWriteAcc->GetBestPaletteIndex(rFillColor)); + cIndex = (cIndex ? 255 : 0); bFast = true; } break; - case( BMP_FORMAT_4BIT_MSN_PAL ): - case( BMP_FORMAT_4BIT_LSN_PAL ): + case BMP_FORMAT_4BIT_MSN_PAL: + case BMP_FORMAT_4BIT_LSN_PAL: { - cIndex = (sal_uInt8) pWriteAcc->GetBestPaletteIndex( rFillColor ); + cIndex = static_cast<sal_uInt8>(pWriteAcc->GetBestPaletteIndex(rFillColor)); cIndex = cIndex | ( cIndex << 4 ); bFast = true; } break; - case( BMP_FORMAT_8BIT_PAL ): + case BMP_FORMAT_8BIT_PAL: { - cIndex = (sal_uInt8) pWriteAcc->GetBestPaletteIndex( rFillColor ); + cIndex = static_cast<sal_uInt8>(pWriteAcc->GetBestPaletteIndex(rFillColor)); bFast = true; } break; - case( BMP_FORMAT_24BIT_TC_BGR ): - case( BMP_FORMAT_24BIT_TC_RGB ): + case BMP_FORMAT_24BIT_TC_BGR: + case BMP_FORMAT_24BIT_TC_RGB: { - if( ( rFillColor.GetRed() == rFillColor.GetGreen() ) && - ( rFillColor.GetRed() == rFillColor.GetBlue() ) ) + if (rFillColor.GetRed() == rFillColor.GetGreen() && + rFillColor.GetRed() == rFillColor.GetBlue()) { cIndex = rFillColor.GetRed(); bFast = true; @@ -516,7 +516,6 @@ bool Bitmap::Erase( const Color& rFillColor ) pWriteAcc->FillRect( aRect ); } - ReleaseAccess( pWriteAcc ); bRet = true; } diff --git a/vcl/source/gdi/bitmap3.cxx b/vcl/source/gdi/bitmap3.cxx index d8e18af5252e..5bf275d98832 100644 --- a/vcl/source/gdi/bitmap3.cxx +++ b/vcl/source/gdi/bitmap3.cxx @@ -588,69 +588,77 @@ bool Bitmap::ImplMakeGreyscales( sal_uInt16 nGreys ) return bRet; } -bool Bitmap::ImplConvertUp( sal_uInt16 nBitCount, Color* pExtColor ) +bool Bitmap::ImplConvertUp(sal_uInt16 nBitCount, Color* pExtColor) { DBG_ASSERT( nBitCount > GetBitCount(), "New BitCount must be greater!" ); - BitmapReadAccess* pReadAcc = AcquireReadAccess(); + Bitmap::ScopedReadAccess pReadAcc(*this); bool bRet = false; - if( pReadAcc ) + if (pReadAcc) { - BitmapPalette aPal; - Bitmap aNewBmp( GetSizePixel(), nBitCount, pReadAcc->HasPalette() ? &pReadAcc->GetPalette() : &aPal ); - BitmapWriteAccess* pWriteAcc = aNewBmp.AcquireWriteAccess(); + BitmapPalette aPalette; + Bitmap aNewBmp(GetSizePixel(), nBitCount, pReadAcc->HasPalette() ? &pReadAcc->GetPalette() : &aPalette); + Bitmap::ScopedWriteAccess pWriteAcc(aNewBmp); - if( pWriteAcc ) + if (pWriteAcc) { const long nWidth = pWriteAcc->Width(); const long nHeight = pWriteAcc->Height(); - if( pWriteAcc->HasPalette() ) + if (pWriteAcc->HasPalette()) { const sal_uInt16 nOldCount = 1 << GetBitCount(); - const BitmapPalette& rOldPal = pReadAcc->GetPalette(); + const BitmapPalette& rOldPalette = pReadAcc->GetPalette(); - aPal.SetEntryCount( 1 << nBitCount ); + aPalette.SetEntryCount(1 << nBitCount); - for( sal_uInt16 i = 0; i < nOldCount; i++ ) - aPal[ i ] = rOldPal[ i ]; + for (sal_uInt16 i = 0; i < nOldCount; i++) + aPalette[i] = rOldPalette[i]; - if( pExtColor ) - aPal[ aPal.GetEntryCount() - 1 ] = *pExtColor; + if (pExtColor) + aPalette[aPalette.GetEntryCount() - 1] = *pExtColor; - pWriteAcc->SetPalette( aPal ); + pWriteAcc->SetPalette(aPalette); - for( long nY = 0L; nY < nHeight; nY++ ) - for( long nX = 0L; nX < nWidth; nX++ ) - pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPixel( nY, nX ) ); + for (long nY = 0L; nY < nHeight; nY++) + { + for (long nX = 0L; nX < nWidth; nX++) + { + pWriteAcc->SetPixel(nY, nX, pReadAcc->GetPixel(nY, nX)); + } + } } else { - if( pReadAcc->HasPalette() ) + if (pReadAcc->HasPalette()) { - for( long nY = 0L; nY < nHeight; nY++ ) - for( long nX = 0L; nX < nWidth; nX++ ) - pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nY, nX ) ) ); + for (long nY = 0L; nY < nHeight; nY++) + { + for (long nX = 0L; nX < nWidth; nX++) + { + pWriteAcc->SetPixel(nY, nX, pReadAcc->GetPaletteColor(pReadAcc->GetPixelIndex(nY, nX))); + } + } } else { - for( long nY = 0L; nY < nHeight; nY++ ) - for( long nX = 0L; nX < nWidth; nX++ ) - pWriteAcc->SetPixel( nY, nX, pReadAcc->GetPixel( nY, nX ) ); + for (long nY = 0L; nY < nHeight; nY++) + { + for (long nX = 0L; nX < nWidth; nX++) + { + pWriteAcc->SetPixel(nY, nX, pReadAcc->GetPixel(nY, nX)); + } + } } } - - aNewBmp.ReleaseAccess( pWriteAcc ); bRet = true; } - ReleaseAccess( pReadAcc ); - - if( bRet ) + if (bRet) { - const MapMode aMap( maPrefMapMode ); - const Size aSize( maPrefSize ); + const MapMode aMap(maPrefMapMode); + const Size aSize(maPrefSize); *this = aNewBmp; @@ -662,116 +670,115 @@ bool Bitmap::ImplConvertUp( sal_uInt16 nBitCount, Color* pExtColor ) return bRet; } -bool Bitmap::ImplConvertDown( sal_uInt16 nBitCount, Color* pExtColor ) +bool Bitmap::ImplConvertDown(sal_uInt16 nBitCount, Color* pExtColor) { - DBG_ASSERT( nBitCount <= GetBitCount(), "New BitCount must be lower ( or equal when pExtColor is set )!" ); + DBG_ASSERT(nBitCount <= GetBitCount(), "New BitCount must be lower ( or equal when pExtColor is set )!"); - BitmapReadAccess* pReadAcc = AcquireReadAccess(); + Bitmap::ScopedReadAccess pReadAcc(*this); bool bRet = false; - if( pReadAcc ) + if (pReadAcc) { - BitmapPalette aPal; - Bitmap aNewBmp( GetSizePixel(), nBitCount, &aPal ); - BitmapWriteAccess* pWriteAcc = aNewBmp.AcquireWriteAccess(); + BitmapPalette aPalette; + Bitmap aNewBmp(GetSizePixel(), nBitCount, &aPalette); + Bitmap::ScopedWriteAccess pWriteAcc(aNewBmp); - if( pWriteAcc ) + if (pWriteAcc) { const sal_uInt16 nCount = 1 << nBitCount; const long nWidth = pWriteAcc->Width(); const long nWidth1 = nWidth - 1L; const long nHeight = pWriteAcc->Height(); - Octree aOctree( *pReadAcc, pExtColor ? ( nCount - 1 ) : nCount ); - InverseColorMap aColorMap( aPal = aOctree.GetPalette() ); + Octree aOctree(*pReadAcc, pExtColor ? (nCount - 1) : nCount); + aPalette = aOctree.GetPalette(); + InverseColorMap aColorMap(aPalette); BitmapColor aColor; ImpErrorQuad aErrQuad; - boost::scoped_array<ImpErrorQuad> pErrQuad1(new ImpErrorQuad[ nWidth ]); - boost::scoped_array<ImpErrorQuad> pErrQuad2(new ImpErrorQuad[ nWidth ]); - ImpErrorQuad* pQLine1 = pErrQuad1.get(); - ImpErrorQuad* pQLine2 = 0; + std::vector<ImpErrorQuad> pErrQuad1(nWidth); + std::vector<ImpErrorQuad> pErrQuad2(nWidth); + ImpErrorQuad* pQLine1 = pErrQuad1.data(); + ImpErrorQuad* pQLine2 = NULL; long nYTmp = 0L; sal_uInt8 cIndex; bool bQ1 = true; - if( pExtColor ) + if (pExtColor) { - aPal.SetEntryCount( aPal.GetEntryCount() + 1 ); - aPal[ aPal.GetEntryCount() - 1 ] = *pExtColor; + aPalette.SetEntryCount(aPalette.GetEntryCount() + 1); + aPalette[aPalette.GetEntryCount() - 1] = *pExtColor; } // set Black/White always, if we have enough space - if( aPal.GetEntryCount() < ( nCount - 1 ) ) + if (aPalette.GetEntryCount() < (nCount - 1)) { - aPal.SetEntryCount( aPal.GetEntryCount() + 2 ); - aPal[ aPal.GetEntryCount() - 2 ] = Color( COL_BLACK ); - aPal[ aPal.GetEntryCount() - 1 ] = Color( COL_WHITE ); + aPalette.SetEntryCount(aPalette.GetEntryCount() + 2); + aPalette[aPalette.GetEntryCount() - 2] = Color(COL_BLACK); + aPalette[aPalette.GetEntryCount() - 1] = Color(COL_WHITE); } - pWriteAcc->SetPalette( aPal ); + pWriteAcc->SetPalette(aPalette); - for( long nY = 0L; nY < std::min( nHeight, 2L ); nY++, nYTmp++ ) + for (long nY = 0L; nY < std::min(nHeight, 2L); nY++, nYTmp++) { - pQLine2 = !nY ? pErrQuad1.get() : pErrQuad2.get(); - for( long nX = 0L; nX < nWidth; nX++ ) + pQLine2 = !nY ? pErrQuad1.data() : pErrQuad2.data(); + for (long nX = 0L; nX < nWidth; nX++) { - if( pReadAcc->HasPalette() ) - pQLine2[ nX ] = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nYTmp, nX ) ); + if (pReadAcc->HasPalette()) + pQLine2[nX] = pReadAcc->GetPaletteColor(pReadAcc->GetPixelIndex(nYTmp, nX)); else - pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX ); + pQLine2[nX] = pReadAcc->GetPixel(nYTmp, nX); } } - for( long nY = 0L; nY < nHeight; nY++, nYTmp++ ) + for (long nY = 0L; nY < nHeight; nY++, nYTmp++) { // first pixel in the line - cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( pQLine1[ 0 ].ImplGetColor() ); - pWriteAcc->SetPixelIndex( nY, 0, cIndex ); + cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex(pQLine1[0].ImplGetColor()); + pWriteAcc->SetPixelIndex(nY, 0, cIndex); long nX; - for( nX = 1L; nX < nWidth1; nX++ ) + for (nX = 1L; nX < nWidth1; nX++) { - cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( aColor = pQLine1[ nX ].ImplGetColor() ); - aErrQuad = ( ImpErrorQuad( aColor ) -= pWriteAcc->GetPaletteColor( cIndex ) ); - pQLine1[ ++nX ].ImplAddColorError7( aErrQuad ); - pQLine2[ nX-- ].ImplAddColorError1( aErrQuad ); - pQLine2[ nX-- ].ImplAddColorError5( aErrQuad ); - pQLine2[ nX++ ].ImplAddColorError3( aErrQuad ); - pWriteAcc->SetPixelIndex( nY, nX, cIndex ); + aColor = pQLine1[nX].ImplGetColor(); + cIndex = static_cast<sal_uInt8>(aColorMap.GetBestPaletteIndex(aColor)); + aErrQuad = (ImpErrorQuad(aColor) -= pWriteAcc->GetPaletteColor(cIndex)); + pQLine1[++nX].ImplAddColorError7(aErrQuad); + pQLine2[nX--].ImplAddColorError1(aErrQuad); + pQLine2[nX--].ImplAddColorError5(aErrQuad); + pQLine2[nX++].ImplAddColorError3(aErrQuad); + pWriteAcc->SetPixelIndex(nY, nX, cIndex); } // Last RowPixel - if( nX < nWidth ) + if (nX < nWidth) { - cIndex = (sal_uInt8) aColorMap.GetBestPaletteIndex( pQLine1[ nWidth1 ].ImplGetColor() ); - pWriteAcc->SetPixelIndex( nY, nX, cIndex ); + cIndex = static_cast<sal_uInt8>(aColorMap.GetBestPaletteIndex(pQLine1[nWidth1].ImplGetColor())); + pWriteAcc->SetPixelIndex(nY, nX, cIndex); } // Refill/copy row buffer pQLine1 = pQLine2; - pQLine2 = ( bQ1 = !bQ1 ) ? pErrQuad2.get() : pErrQuad1.get(); + pQLine2 = (bQ1 = !bQ1) ? pErrQuad2.data() : pErrQuad1.data(); - if( nYTmp < nHeight ) + if (nYTmp < nHeight) { - for( nX = 0L; nX < nWidth; nX++ ) + for (nX = 0L; nX < nWidth; nX++) { - if( pReadAcc->HasPalette() ) - pQLine2[ nX ] = pReadAcc->GetPaletteColor( pReadAcc->GetPixelIndex( nYTmp, nX ) ); + if (pReadAcc->HasPalette()) + pQLine2[nX] = pReadAcc->GetPaletteColor(pReadAcc->GetPixelIndex(nYTmp, nX)); else - pQLine2[ nX ] = pReadAcc->GetPixel( nYTmp, nX ); + pQLine2[nX] = pReadAcc->GetPixel(nYTmp, nX); } } } - aNewBmp.ReleaseAccess( pWriteAcc ); bRet = true; } - ReleaseAccess( pReadAcc ); - - if( bRet ) + if(bRet) { - const MapMode aMap( maPrefMapMode ); - const Size aSize( maPrefSize ); + const MapMode aMap(maPrefMapMode); + const Size aSize(maPrefSize); *this = aNewBmp; |