LCOV - code coverage report
Current view: top level - libreoffice/vcl/headless - svpbmp.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 110 161 68.3 %
Date: 2012-12-17 Functions: 10 13 76.9 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "headless/svpbmp.hxx"
      21             : 
      22             : #include <basegfx/vector/b2ivector.hxx>
      23             : #include <basegfx/range/b2ibox.hxx>
      24             : #include <basebmp/scanlineformats.hxx>
      25             : #include <basebmp/color.hxx>
      26             : 
      27             : #include <vcl/salbtype.hxx>
      28             : #include <vcl/bitmap.hxx>
      29             : 
      30             : using namespace basebmp;
      31             : using namespace basegfx;
      32             : 
      33        5160 : SvpSalBitmap::~SvpSalBitmap()
      34             : {
      35        5160 : }
      36             : 
      37        1278 : bool SvpSalBitmap::Create( const Size& rSize,
      38             :                            sal_uInt16 nBitCount,
      39             :                            const BitmapPalette& rPalette )
      40             : {
      41        1278 :     sal_uInt32 nFormat = SVP_DEFAULT_BITMAP_FORMAT;
      42        1278 :     switch( nBitCount )
      43             :     {
      44         154 :         case 1: nFormat = Format::ONE_BIT_MSB_PAL; break;
      45           6 :         case 4: nFormat = Format::FOUR_BIT_MSB_PAL; break;
      46         622 :         case 8: nFormat = Format::EIGHT_BIT_PAL; break;
      47             : #ifdef OSL_BIGENDIAN
      48             :         case 16: nFormat = Format::SIXTEEN_BIT_MSB_TC_MASK; break;
      49             : #else
      50           0 :         case 16: nFormat = Format::SIXTEEN_BIT_LSB_TC_MASK; break;
      51             : #endif
      52         496 :         case 24: nFormat = Format::TWENTYFOUR_BIT_TC_MASK; break;
      53             : #if defined(ANDROID) || defined(IOS)
      54             :         case 32: nFormat = Format::THIRTYTWO_BIT_TC_MASK_RGBA; break;
      55             : #else
      56           0 :         case 32: nFormat = Format::THIRTYTWO_BIT_TC_MASK; break;
      57             : #endif
      58             :     }
      59        1278 :     B2IVector aSize( rSize.Width(), rSize.Height() );
      60        1278 :     if( aSize.getX() == 0 )
      61           0 :         aSize.setX( 1 );
      62        1278 :     if( aSize.getY() == 0 )
      63           0 :         aSize.setY( 1 );
      64        1278 :     if( nBitCount > 8 )
      65         496 :         m_aBitmap = createBitmapDevice( aSize, false, nFormat );
      66             :     else
      67             :     {
      68             :         // prepare palette
      69         782 :         unsigned int nEntries = 1U << nBitCount;
      70             :         std::vector<basebmp::Color>* pPalette =
      71         782 :             new std::vector<basebmp::Color>( nEntries, basebmp::Color(COL_WHITE) );
      72         782 :         unsigned int nColors = rPalette.GetEntryCount();
      73      159354 :         for( unsigned int i = 0; i < nColors; i++ )
      74             :         {
      75      158572 :             const BitmapColor& rCol = rPalette[i];
      76      158572 :             (*pPalette)[i] = basebmp::Color( rCol.GetRed(), rCol.GetGreen(), rCol.GetBlue() );
      77             :         }
      78             :         m_aBitmap = createBitmapDevice( aSize, false, nFormat,
      79             :                                         basebmp::RawMemorySharedArray(),
      80             :                                         basebmp::PaletteMemorySharedVector( pPalette )
      81         782 :                                         );
      82             :     }
      83        1278 :     return true;
      84             : }
      85             : 
      86         380 : bool SvpSalBitmap::Create( const SalBitmap& rSalBmp )
      87             : {
      88         380 :     const SvpSalBitmap& rSrc = static_cast<const SvpSalBitmap&>(rSalBmp);
      89         380 :     const BitmapDeviceSharedPtr& rSrcBmp = rSrc.getBitmap();
      90         380 :     if( rSrcBmp.get() )
      91             :     {
      92         380 :         B2IVector aSize = rSrcBmp->getSize();
      93         380 :         m_aBitmap = cloneBitmapDevice( aSize, rSrcBmp );
      94         380 :         B2IBox aRect( 0, 0, aSize.getX(), aSize.getY() );
      95         380 :         m_aBitmap->drawBitmap( rSrcBmp, aRect, aRect, DrawMode_PAINT );
      96             :     }
      97             :     else
      98           0 :         m_aBitmap.reset();
      99             : 
     100         380 :     return true;
     101             : }
     102             : 
     103          14 : bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
     104             :                            SalGraphics* /*pGraphics*/ )
     105             : {
     106          14 :     return false;
     107             : }
     108             : 
     109           6 : bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
     110             :                            sal_uInt16 /*nNewBitCount*/ )
     111             : {
     112           6 :     return false;
     113             : }
     114             : 
     115           0 : bool SvpSalBitmap::Create( const ::com::sun::star::uno::Reference< ::com::sun::star::rendering::XBitmapCanvas > /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
     116             : {
     117           0 :     return false;
     118             : }
     119             : 
     120           0 : void SvpSalBitmap::Destroy()
     121             : {
     122           0 :     m_aBitmap.reset();
     123           0 : }
     124             : 
     125        3843 : Size SvpSalBitmap::GetSize() const
     126             : {
     127        3843 :     Size aSize;
     128        3843 :     if( m_aBitmap.get() )
     129             :     {
     130        3843 :         B2IVector aVec( m_aBitmap->getSize() );
     131        3843 :         aSize = Size( aVec.getX(), aVec.getY() );
     132             :     }
     133             : 
     134        3843 :     return aSize;
     135             : }
     136             : 
     137        2055 : sal_uInt16 SvpSalBitmap::GetBitCount() const
     138             : {
     139        2055 :     sal_uInt16 nDepth = 0;
     140        2055 :     if( m_aBitmap.get() )
     141        2049 :         nDepth = getBitCountFromScanlineFormat( m_aBitmap->getScanlineFormat() );
     142        2055 :     return nDepth;
     143             : }
     144             : 
     145        3912 : BitmapBuffer* SvpSalBitmap::AcquireBuffer( bool )
     146             : {
     147        3912 :     BitmapBuffer* pBuf = NULL;
     148        3912 :     if( m_aBitmap.get() )
     149             :     {
     150        3906 :         pBuf = new BitmapBuffer();
     151        3906 :         sal_uInt16 nBitCount = 1;
     152        3906 :         switch( m_aBitmap->getScanlineFormat() )
     153             :         {
     154             :             case Format::ONE_BIT_MSB_GREY:
     155             :             case Format::ONE_BIT_MSB_PAL:
     156         295 :                 nBitCount = 1;
     157         295 :                 pBuf->mnFormat = BMP_FORMAT_1BIT_MSB_PAL;
     158         295 :                 break;
     159             :             case Format::ONE_BIT_LSB_GREY:
     160             :             case Format::ONE_BIT_LSB_PAL:
     161           0 :                 nBitCount = 1;
     162           0 :                 pBuf->mnFormat = BMP_FORMAT_1BIT_LSB_PAL;
     163           0 :                 break;
     164             :             case Format::FOUR_BIT_MSB_GREY:
     165             :             case Format::FOUR_BIT_MSB_PAL:
     166          12 :                 nBitCount = 4;
     167          12 :                 pBuf->mnFormat = BMP_FORMAT_4BIT_MSN_PAL;
     168          12 :                 break;
     169             :             case Format::FOUR_BIT_LSB_GREY:
     170             :             case Format::FOUR_BIT_LSB_PAL:
     171           0 :                 nBitCount = 4;
     172           0 :                 pBuf->mnFormat = BMP_FORMAT_4BIT_LSN_PAL;
     173           0 :                 break;
     174             :             case Format::EIGHT_BIT_PAL:
     175        2190 :                 nBitCount = 8;
     176        2190 :                 pBuf->mnFormat = BMP_FORMAT_8BIT_PAL;
     177        2190 :                 break;
     178             :             case Format::EIGHT_BIT_GREY:
     179           0 :                 nBitCount = 8;
     180           0 :                 pBuf->mnFormat = BMP_FORMAT_8BIT_PAL;
     181           0 :                 break;
     182             :             case Format::SIXTEEN_BIT_LSB_TC_MASK:
     183           0 :                 nBitCount = 16;
     184           0 :                 pBuf->mnFormat = BMP_FORMAT_16BIT_TC_LSB_MASK;
     185           0 :                 pBuf->maColorMask = ColorMask( 0xf800, 0x07e0, 0x001f );
     186           0 :                 break;
     187             :             case Format::SIXTEEN_BIT_MSB_TC_MASK:
     188           0 :                 nBitCount = 16;
     189           0 :                 pBuf->mnFormat = BMP_FORMAT_16BIT_TC_MSB_MASK;
     190           0 :                 pBuf->maColorMask = ColorMask( 0xf800, 0x07e0, 0x001f );
     191           0 :                 break;
     192             :             case Format::TWENTYFOUR_BIT_TC_MASK:
     193        1409 :                 nBitCount = 24;
     194        1409 :                 pBuf->mnFormat = BMP_FORMAT_24BIT_TC_BGR;
     195        1409 :                 break;
     196             :             case Format::THIRTYTWO_BIT_TC_MASK_BGRA:
     197           0 :                 nBitCount = 32;
     198           0 :                 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
     199             : #ifdef OSL_BIGENDIAN
     200             :                 pBuf->maColorMask = ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000 );
     201             : #else
     202           0 :                 pBuf->maColorMask = ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff );
     203             : #endif
     204           0 :                 break;
     205             :             case Format::THIRTYTWO_BIT_TC_MASK_ARGB:
     206           0 :                 nBitCount = 32;
     207           0 :                 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
     208             : #ifdef OSL_BIGENDIAN
     209             :                 pBuf->maColorMask = ColorMask( 0x00ff0000, 0x0000ff00, 0x000000ff );
     210             : #else
     211           0 :                 pBuf->maColorMask = ColorMask( 0x0000ff00, 0x00ff0000, 0xff000000 );
     212             : #endif
     213           0 :                 break;
     214             :             case Format::THIRTYTWO_BIT_TC_MASK_ABGR:
     215           0 :                 nBitCount = 32;
     216           0 :                 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
     217             : #ifdef OSL_BIGENDIAN
     218             :                 pBuf->maColorMask = ColorMask( 0x000000ff, 0x0000ff00, 0x00ff0000 );
     219             : #else
     220           0 :                 pBuf->maColorMask = ColorMask( 0xff000000, 0x00ff0000, 0x0000ff00 );
     221             : #endif
     222           0 :                 break;
     223             :             case Format::THIRTYTWO_BIT_TC_MASK_RGBA:
     224           0 :                 nBitCount = 32;
     225           0 :                 pBuf->mnFormat = BMP_FORMAT_32BIT_TC_MASK;
     226             : #ifdef OSL_BIGENDIAN
     227             :                 pBuf->maColorMask = ColorMask( 0xff000000, 0x00ff0000, 0x0000ff00 );
     228             : #else
     229           0 :                 pBuf->maColorMask = ColorMask( 0x000000ff, 0x0000ff00, 0x00ff0000 );
     230             : #endif
     231           0 :                 break;
     232             : 
     233             :             default:
     234             :                 // this is an error case !!!!!
     235           0 :                 nBitCount = 1;
     236           0 :                 pBuf->mnFormat = BMP_FORMAT_1BIT_MSB_PAL;
     237           0 :                 break;
     238             :         }
     239        3906 :         if( m_aBitmap->isTopDown() )
     240           0 :             pBuf->mnFormat |= BMP_FORMAT_TOP_DOWN;
     241             : 
     242        3906 :         B2IVector aSize = m_aBitmap->getSize();
     243        3906 :         pBuf->mnWidth           = aSize.getX();
     244        3906 :         pBuf->mnHeight          = aSize.getY();
     245        3906 :         pBuf->mnScanlineSize    = m_aBitmap->getScanlineStride();
     246        3906 :         pBuf->mnBitCount        = nBitCount;
     247        3906 :         pBuf->mpBits            = (sal_uInt8*)m_aBitmap->getBuffer().get();
     248        3906 :         if( nBitCount <= 8 )
     249             :         {
     250       12485 :             if( m_aBitmap->getScanlineFormat() == Format::EIGHT_BIT_GREY ||
     251        2497 :                 m_aBitmap->getScanlineFormat() == Format::FOUR_BIT_LSB_GREY ||
     252        2497 :                 m_aBitmap->getScanlineFormat() == Format::FOUR_BIT_MSB_GREY ||
     253        2497 :                 m_aBitmap->getScanlineFormat() == Format::ONE_BIT_LSB_GREY ||
     254        2497 :                 m_aBitmap->getScanlineFormat() == Format::ONE_BIT_MSB_GREY
     255             :                 )
     256           0 :                 pBuf->maPalette = Bitmap::GetGreyPalette( 1U << nBitCount );
     257             :             else
     258             :             {
     259        2497 :                 basebmp::PaletteMemorySharedVector aPalette = m_aBitmap->getPalette();
     260        2497 :                 if( aPalette.get() )
     261             :                 {
     262        2497 :                     unsigned int nColors = aPalette->size();
     263        2497 :                     if( nColors > 0 )
     264             :                     {
     265        2497 :                         pBuf->maPalette.SetEntryCount( nColors );
     266      563919 :                         for( unsigned int i = 0; i < nColors; i++ )
     267             :                         {
     268      561422 :                             const basebmp::Color& rCol = (*aPalette)[i];
     269      561422 :                             pBuf->maPalette[i] = BitmapColor( rCol.getRed(), rCol.getGreen(), rCol.getBlue() );
     270             :                         }
     271             :                     }
     272        2497 :                 }
     273             :             }
     274        3906 :         }
     275             :     }
     276             : 
     277        3912 :     return pBuf;
     278             : }
     279             : 
     280        3906 : void SvpSalBitmap::ReleaseBuffer( BitmapBuffer* pBuffer, bool bReadOnly )
     281             : {
     282        3906 :     if( !bReadOnly && pBuffer->maPalette.GetEntryCount() )
     283             :     {
     284             :         // palette might have changed, clone device (but recycle
     285             :         // memory)
     286        1144 :         sal_uInt16 nBitCount = 0;
     287        1144 :         switch( m_aBitmap->getScanlineFormat() )
     288             :         {
     289             :             case Format::ONE_BIT_MSB_GREY:
     290             :                 // FALLTHROUGH intended
     291             :             case Format::ONE_BIT_MSB_PAL:
     292             :                 // FALLTHROUGH intended
     293             :             case Format::ONE_BIT_LSB_GREY:
     294             :                 // FALLTHROUGH intended
     295             :             case Format::ONE_BIT_LSB_PAL:
     296         174 :                 nBitCount = 1;
     297         174 :                 break;
     298             : 
     299             :             case Format::FOUR_BIT_MSB_GREY:
     300             :                 // FALLTHROUGH intended
     301             :             case Format::FOUR_BIT_MSB_PAL:
     302             :                 // FALLTHROUGH intended
     303             :             case Format::FOUR_BIT_LSB_GREY:
     304             :                 // FALLTHROUGH intended
     305             :             case Format::FOUR_BIT_LSB_PAL:
     306           6 :                 nBitCount = 4;
     307           6 :                 break;
     308             : 
     309             :             case Format::EIGHT_BIT_PAL:
     310             :                 // FALLTHROUGH intended
     311             :             case Format::EIGHT_BIT_GREY:
     312         964 :                 nBitCount = 8;
     313         964 :                 break;
     314             : 
     315             :             default:
     316           0 :                 break;
     317             :         }
     318             : 
     319        1144 :         if( nBitCount )
     320             :         {
     321        1144 :             sal_uInt32 nEntries = 1U << nBitCount;
     322             : 
     323             :             boost::shared_ptr< std::vector<basebmp::Color> > pPal(
     324             :                 new std::vector<basebmp::Color>( nEntries,
     325        1144 :                                                  basebmp::Color(COL_WHITE)));
     326             :             const sal_uInt32 nColors = std::min(
     327        1144 :                 (sal_uInt32)pBuffer->maPalette.GetEntryCount(),
     328        1144 :                 nEntries);
     329      238566 :             for( sal_uInt32 i = 0; i < nColors; i++ )
     330             :             {
     331      237422 :                 const BitmapColor& rCol = pBuffer->maPalette[i];
     332      237422 :                 (*pPal)[i] = basebmp::Color( rCol.GetRed(), rCol.GetGreen(), rCol.GetBlue() );
     333             :             }
     334             : 
     335             :             m_aBitmap = basebmp::createBitmapDevice( m_aBitmap->getSize(),
     336        1144 :                                                      m_aBitmap->isTopDown(),
     337             :                                                      m_aBitmap->getScanlineFormat(),
     338             :                                                      m_aBitmap->getBuffer(),
     339        2288 :                                                      pPal );
     340             :         }
     341             :     }
     342             : 
     343        3906 :     delete pBuffer;
     344        3906 : }
     345             : 
     346           0 : bool SvpSalBitmap::GetSystemData( BitmapSystemData& )
     347             : {
     348           0 :     return false;
     349             : }
     350             : 
     351             : 
     352             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10