LCOV - code coverage report
Current view: top level - filter/source/graphicfilter/ipcx - ipcx.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 124 186 66.7 %
Date: 2014-11-03 Functions: 10 10 100.0 %
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             : 
      21             : #include <vcl/graph.hxx>
      22             : #include <vcl/bmpacc.hxx>
      23             : 
      24             : class FilterConfigItem;
      25             : 
      26             : //============================ PCXReader ==================================
      27             : 
      28             : class PCXReader {
      29             : 
      30             : private:
      31             : 
      32             :     SvStream& m_rPCX;               // the PCX file to read
      33             : 
      34             :     Bitmap              aBmp;
      35             :     sal_uInt8           nVersion;           // PCX-Version
      36             :     sal_uInt8           nEncoding;          // compression type
      37             :     sal_uLong           nBitsPerPlanePix;   // bits per plane per pixel
      38             :     sal_uLong           nPlanes;            // no of planes
      39             :     sal_uLong           nBytesPerPlaneLin;  // bytes per plane line
      40             :     sal_uInt16          nPaletteInfo;
      41             : 
      42             :     sal_uLong           nWidth, nHeight;    // dimension in pixel
      43             :     sal_uInt16          nResX, nResY;       // resolution in pixel per inch oder 0,0
      44             :     sal_uInt16          nDestBitsPerPixel;  // bits per pixel in destination bitmap 1,4,8 or 24
      45             :     sal_uInt8*          pPalette;
      46             :     bool            nStatus;            // from now on do not read status from stream ( SJ )
      47             : 
      48             : 
      49             :     bool            Callback( sal_uInt16 nPercent );
      50             :     void                ImplReadBody(BitmapWriteAccess * pAcc);
      51             :     void                ImplReadPalette( sal_uLong nCol );
      52             :     void                ImplReadHeader();
      53             : 
      54             : public:
      55             :                         PCXReader(SvStream &rStream);
      56             :                         ~PCXReader();
      57             :     bool                ReadPCX(Graphic & rGraphic );
      58             :                         // Reads a PCX file from the stream and fills the GDIMetaFile
      59             : };
      60             : 
      61             : //=================== methods of PCXReader ==============================
      62             : 
      63          10 : PCXReader::PCXReader(SvStream &rStream)
      64             :     : m_rPCX(rStream)
      65             :     , nVersion(0)
      66             :     , nEncoding(0)
      67             :     , nBitsPerPlanePix(0)
      68             :     , nPlanes(0)
      69             :     , nBytesPerPlaneLin(0)
      70             :     , nPaletteInfo(0)
      71             :     , nWidth(0)
      72             :     , nHeight(0)
      73             :     , nResX(0)
      74             :     , nResY(0)
      75             :     , nDestBitsPerPixel(0)
      76          10 :     , nStatus(false)
      77             : {
      78          10 :     pPalette = new sal_uInt8[ 768 ];
      79          10 : }
      80             : 
      81          20 : PCXReader::~PCXReader()
      82             : {
      83          10 :     delete[] pPalette;
      84          10 : }
      85             : 
      86          30 : bool PCXReader::Callback( sal_uInt16 /*nPercent*/ )
      87             : {
      88          30 :     return false;
      89             : }
      90             : 
      91          10 : bool PCXReader::ReadPCX(Graphic & rGraphic)
      92             : {
      93          10 :     if ( m_rPCX.GetError() )
      94           0 :         return false;
      95             : 
      96          10 :     m_rPCX.SetNumberFormatInt(NUMBERFORMAT_INT_LITTLEENDIAN);
      97             : 
      98             :     // read header:
      99             : 
     100          10 :     nStatus = true;
     101             : 
     102          10 :     ImplReadHeader();
     103             : 
     104             :     // Write BMP header and conditionally (maybe invalid for now) color palette:
     105          10 :     if ( nStatus )
     106             :     {
     107           4 :         aBmp = Bitmap( Size( nWidth, nHeight ), nDestBitsPerPixel );
     108           4 :         Bitmap::ScopedWriteAccess pAcc(aBmp);
     109           4 :         if ( pAcc == 0 )
     110           0 :             return false;
     111             : 
     112           4 :         if ( nDestBitsPerPixel <= 8 )
     113             :         {
     114           2 :             sal_uInt16 nColors = 1 << nDestBitsPerPixel;
     115           2 :             sal_uInt8* pPal = pPalette;
     116           2 :             pAcc->SetPaletteEntryCount( nColors );
     117         514 :             for ( sal_uInt16 i = 0; i < nColors; i++, pPal += 3 )
     118             :             {
     119         512 :                 pAcc->SetPaletteColor( i, BitmapColor ( pPal[ 0 ], pPal[ 1 ], pPal[ 2 ] ) );
     120             :             }
     121             :         }
     122             :         // read bitmap data
     123           4 :         ImplReadBody(pAcc.get());
     124             : 
     125             :         // If an extended color palette exists at the end of the file, then read it and
     126             :         // and write again in palette:
     127           4 :         if ( nDestBitsPerPixel == 8 && nStatus )
     128             :         {
     129           0 :             sal_uInt8* pPal = pPalette;
     130           0 :             m_rPCX.SeekRel(1);
     131           0 :             ImplReadPalette(256);
     132           0 :             pAcc->SetPaletteEntryCount( 256 );
     133           0 :             for ( sal_uInt16 i = 0; i < 256; i++, pPal += 3 )
     134             :             {
     135           0 :                 pAcc->SetPaletteColor( i, BitmapColor ( pPal[ 0 ], pPal[ 1 ], pPal[ 2 ] ) );
     136             :             }
     137             :         }
     138             :     /*
     139             :         // set resolution:
     140             :         if (nResX!=0 && nResY!=0) {
     141             :             MapMode aMapMode(MAP_INCH,Point(0,0),Fraction(1,nResX),Fraction(1,nResY));
     142             :             rBitmap.SetPrefMapMode(aMapMode);
     143             :             rBitmap.SetPrefSize(Size(nWidth,nHeight));
     144             :         }
     145           4 :     */  if ( nStatus )
     146             :         {
     147           2 :             rGraphic = aBmp;
     148           2 :             return true;
     149           2 :         }
     150             :     }
     151           8 :     return false;
     152             : }
     153             : 
     154          10 : void PCXReader::ImplReadHeader()
     155             : {
     156          10 :     sal_uInt8 nbyte(0);
     157          10 :     m_rPCX.ReadUChar( nbyte ).ReadUChar( nVersion ).ReadUChar( nEncoding );
     158          10 :     if ( nbyte!=0x0a || (nVersion != 0 && nVersion != 2 && nVersion != 3 && nVersion != 5) || nEncoding > 1 )
     159             :     {
     160           0 :         nStatus = false;
     161           6 :         return;
     162             :     }
     163             : 
     164          10 :     nbyte = 0;
     165          10 :     m_rPCX.ReadUChar( nbyte ); nBitsPerPlanePix = (sal_uLong)nbyte;
     166          10 :     sal_uInt16 nMinX(0),nMinY(0),nMaxX(0),nMaxY(0);
     167          10 :     m_rPCX.ReadUInt16( nMinX ).ReadUInt16( nMinY ).ReadUInt16( nMaxX ).ReadUInt16( nMaxY );
     168             : 
     169          10 :     if ((nMinX > nMaxX) || (nMinY > nMaxY))
     170             :     {
     171           0 :         nStatus = false;
     172           0 :         return;
     173             :     }
     174             : 
     175          10 :     nWidth = nMaxX-nMinX+1;
     176          10 :     nHeight = nMaxY-nMinY+1;
     177             : 
     178          10 :     m_rPCX.ReadUInt16( nResX );
     179          10 :     m_rPCX.ReadUInt16( nResY );
     180          10 :     if ( nResX >= nWidth || nResY >= nHeight || ( nResX != nResY ) )
     181           8 :         nResX = nResY = 0;
     182             : 
     183          10 :     ImplReadPalette( 16 );
     184             : 
     185          10 :     m_rPCX.SeekRel( 1 );
     186          10 :     nbyte = 0;
     187          10 :     m_rPCX.ReadUChar( nbyte );   nPlanes = (sal_uLong)nbyte;
     188          10 :     sal_uInt16 nushort(0);
     189          10 :     m_rPCX.ReadUInt16( nushort ); nBytesPerPlaneLin = (sal_uLong)nushort;
     190          10 :     m_rPCX.ReadUInt16( nPaletteInfo );
     191             : 
     192          10 :     m_rPCX.SeekRel( 58 );
     193             : 
     194          10 :     nDestBitsPerPixel = (sal_uInt16)( nBitsPerPlanePix * nPlanes );
     195          10 :     if (nDestBitsPerPixel == 2 || nDestBitsPerPixel == 3) nDestBitsPerPixel = 4;
     196             : 
     197          10 :     if ( ( nDestBitsPerPixel != 1 && nDestBitsPerPixel != 4 && nDestBitsPerPixel != 8 && nDestBitsPerPixel != 24 )
     198           8 :         || nPlanes > 4 || nBytesPerPlaneLin < ( ( nWidth * nBitsPerPlanePix+7 ) >> 3 ) )
     199             :     {
     200           6 :         nStatus = false;
     201           6 :         return;
     202             :     }
     203             : 
     204             :     // If the bitmap has only 2 colors, the palatte is most often invalid and it is always(?)
     205             :     // a black and white image:
     206           4 :     if ( nPlanes == 1 && nBitsPerPlanePix == 1 )
     207             :     {
     208           0 :         pPalette[ 0 ] = pPalette[ 1 ] = pPalette[ 2 ] = 0x00;
     209           0 :         pPalette[ 3 ] = pPalette[ 4 ] = pPalette[ 5 ] = 0xff;
     210             :     }
     211             : }
     212             : 
     213           4 : void PCXReader::ImplReadBody(BitmapWriteAccess * pAcc)
     214             : {
     215             :     sal_uInt8   *pPlane[ 4 ], * pDest, * pSource1, * pSource2, * pSource3, *pSource4;
     216             :     sal_uLong   i, nx, ny, np, nCount, nPercent;
     217           4 :     sal_uLong   nLastPercent = 0;
     218           4 :     sal_uInt8   nDat = 0, nCol = 0;
     219             : 
     220             :     //sanity check there is enough data before trying allocation
     221           4 :     if (nBytesPerPlaneLin > m_rPCX.remainingSize() / nPlanes)
     222             :     {
     223           2 :         nStatus = false;
     224           6 :         return;
     225             :     }
     226             : 
     227           8 :     for( np = 0; np < nPlanes; np++ )
     228           6 :         pPlane[ np ] = new sal_uInt8[ nBytesPerPlaneLin ];
     229             : 
     230           2 :     nCount = 0;
     231         390 :     for ( ny = 0; ny < nHeight; ny++ )
     232             :     {
     233         388 :         if (m_rPCX.GetError() || m_rPCX.IsEof())
     234             :         {
     235           0 :             nStatus = false;
     236           0 :             break;
     237             :         }
     238         388 :         nPercent = ny * 60 / nHeight + 10;
     239         388 :         if ( ny == 0 || nLastPercent + 4 <= nPercent )
     240             :         {
     241          30 :             nLastPercent = nPercent;
     242          30 :             if ( Callback( (sal_uInt16)nPercent ) )
     243           0 :                 break;
     244             :         }
     245        1552 :         for ( np = 0; np < nPlanes; np++)
     246             :         {
     247        1164 :             if ( nEncoding == 0)
     248           0 :                 m_rPCX.Read( (void *)pPlane[ np ], nBytesPerPlaneLin );
     249             :             else
     250             :             {
     251        1164 :                 pDest = pPlane[ np ];
     252        1164 :                 nx = nBytesPerPlaneLin;
     253        2328 :                 while ( nCount > 0 && nx > 0)
     254             :                 {
     255           0 :                     *(pDest++) = nDat;
     256           0 :                     nx--;
     257           0 :                     nCount--;
     258             :                 }
     259       78804 :                 while ( nx > 0 )
     260             :                 {
     261       76572 :                     m_rPCX.ReadUChar( nDat );
     262       76572 :                     if ( ( nDat & 0xc0 ) == 0xc0 )
     263             :                     {
     264       40364 :                         nCount =( (sal_uLong)nDat ) & 0x003f;
     265       40364 :                         m_rPCX.ReadUChar( nDat );
     266       40364 :                         if ( nCount < nx )
     267             :                         {
     268       40268 :                             nx -= nCount;
     269      729156 :                             while ( nCount > 0)
     270             :                             {
     271      648620 :                                 *(pDest++) = nDat;
     272      648620 :                                 nCount--;
     273             :                             }
     274             :                         }
     275             :                         else
     276             :                         {
     277          96 :                             nCount -= nx;
     278         768 :                             do
     279             :                             {
     280         768 :                                 *(pDest++) = nDat;
     281         768 :                                 nx--;
     282             :                             }
     283             :                             while ( nx > 0 );
     284          96 :                             break;
     285             :                         }
     286             :                     }
     287             :                     else
     288             :                     {
     289       36208 :                         *(pDest++) = nDat;
     290       36208 :                         nx--;
     291             :                     }
     292             :                 }
     293             :             }
     294             :         }
     295         388 :         pSource1 = pPlane[ 0 ];
     296         388 :         pSource2 = pPlane[ 1 ];
     297         388 :         pSource3 = pPlane[ 2 ];
     298         388 :         pSource4 = pPlane[ 3 ];
     299         388 :         switch ( nBitsPerPlanePix + ( nPlanes << 8 ) )
     300             :         {
     301             :             // 2 colors
     302             :             case 0x101 :
     303           0 :                 for ( i = 0; i < nWidth; i++ )
     304             :                 {
     305           0 :                     sal_uLong nShift = ( i & 7 ) ^ 7;
     306           0 :                     if ( nShift == 0 )
     307           0 :                         pAcc->SetPixelIndex( ny, i, *(pSource1++) & 1 );
     308             :                     else
     309           0 :                         pAcc->SetPixelIndex( ny, i, (*pSource1 >> nShift ) & 1 );
     310             :                 }
     311           0 :                 break;
     312             :             // 4 colors
     313             :             case 0x102 :
     314           0 :                 for ( i = 0; i < nWidth; i++ )
     315             :                 {
     316           0 :                     switch( i & 3 )
     317             :                     {
     318             :                         case 0 :
     319           0 :                             nCol = *pSource1 >> 6;
     320           0 :                             break;
     321             :                         case 1 :
     322           0 :                             nCol = ( *pSource1 >> 4 ) & 0x03 ;
     323           0 :                             break;
     324             :                         case 2 :
     325           0 :                             nCol = ( *pSource1 >> 2 ) & 0x03;
     326           0 :                             break;
     327             :                         case 3 :
     328           0 :                             nCol = ( *pSource1++ ) & 0x03;
     329           0 :                             break;
     330             :                     }
     331           0 :                     pAcc->SetPixelIndex( ny, i, nCol );
     332             :                 }
     333           0 :                 break;
     334             :             // 256 colors
     335             :             case 0x108 :
     336           0 :                 for ( i = 0; i < nWidth; i++ )
     337             :                 {
     338           0 :                     pAcc->SetPixelIndex( ny, i, *pSource1++ );
     339             :                 }
     340           0 :                 break;
     341             :             // 8 colors
     342             :             case 0x301 :
     343           0 :                 for ( i = 0; i < nWidth; i++ )
     344             :                 {
     345           0 :                     sal_uLong nShift = ( i & 7 ) ^ 7;
     346           0 :                     if ( nShift == 0 )
     347             :                     {
     348           0 :                         nCol = ( *pSource1++ & 1) + ( ( *pSource2++ << 1 ) & 2 ) + ( ( *pSource3++ << 2 ) & 4 );
     349           0 :                         pAcc->SetPixelIndex( ny, i, nCol );
     350             :                     }
     351             :                     else
     352             :                     {
     353             :                         nCol = sal::static_int_cast< sal_uInt8 >(
     354           0 :                             ( ( *pSource1 >> nShift ) & 1)  + ( ( ( *pSource2 >> nShift ) << 1 ) & 2 ) +
     355           0 :                             ( ( ( *pSource3 >> nShift ) << 2 ) & 4 ));
     356           0 :                         pAcc->SetPixelIndex( ny, i, nCol );
     357             :                     }
     358             :                 }
     359           0 :                 break;
     360             :             // 16 colors
     361             :             case 0x401 :
     362           0 :                 for ( i = 0; i < nWidth; i++ )
     363             :                 {
     364           0 :                     sal_uLong nShift = ( i & 7 ) ^ 7;
     365           0 :                     if ( nShift == 0 )
     366             :                     {
     367           0 :                         nCol = ( *pSource1++ & 1) + ( ( *pSource2++ << 1 ) & 2 ) + ( ( *pSource3++ << 2 ) & 4 ) +
     368           0 :                             ( ( *pSource4++ << 3 ) & 8 );
     369           0 :                         pAcc->SetPixelIndex( ny, i, nCol );
     370             :                     }
     371             :                     else
     372             :                     {
     373             :                         nCol = sal::static_int_cast< sal_uInt8 >(
     374           0 :                             ( ( *pSource1 >> nShift ) & 1)  + ( ( ( *pSource2 >> nShift ) << 1 ) & 2 ) +
     375           0 :                             ( ( ( *pSource3 >> nShift ) << 2 ) & 4 ) + ( ( ( *pSource4 >> nShift ) << 3 ) & 8 ));
     376           0 :                         pAcc->SetPixelIndex( ny, i, nCol );
     377             :                     }
     378             :                 }
     379           0 :                 break;
     380             :             // 16m colors
     381             :             case 0x308 :
     382      228920 :                 for ( i = 0; i < nWidth; i++ )
     383             :                 {
     384      228532 :                     pAcc->SetPixel( ny, i, Color( *pSource1++, *pSource2++, *pSource3++ ) );
     385             : 
     386             :                 }
     387         388 :                 break;
     388             :             default :
     389           0 :                 nStatus = false;
     390           0 :                 break;
     391             :         }
     392             :     }
     393           8 :     for ( np = 0; np < nPlanes; np++ )
     394           6 :         delete[] pPlane[ np ];
     395             : }
     396             : 
     397          10 : void PCXReader::ImplReadPalette( sal_uLong nCol )
     398             : {
     399             :     sal_uInt8   r, g, b;
     400          10 :     sal_uInt8*  pPtr = pPalette;
     401         170 :     for ( sal_uLong i = 0; i < nCol; i++ )
     402             :     {
     403         160 :         m_rPCX.ReadUChar( r ).ReadUChar( g ).ReadUChar( b );
     404         160 :         *pPtr++ = r;
     405         160 :         *pPtr++ = g;
     406         160 :         *pPtr++ = b;
     407             :     }
     408          10 : }
     409             : 
     410             : //================== GraphicImport - the exported function ================
     411             : 
     412             : // this needs to be kept in sync with
     413             : // ImpFilterLibCacheEntry::GetImportFunction() from
     414             : // vcl/source/filter/graphicfilter.cxx
     415             : #if defined(DISABLE_DYNLOADING)
     416             : #define GraphicImport ipxGraphicImport
     417             : #endif
     418             : 
     419             : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
     420          10 : GraphicImport( SvStream & rStream, Graphic & rGraphic, FilterConfigItem* )
     421             : {
     422          10 :     PCXReader aPCXReader(rStream);
     423          10 :     bool nRetValue = aPCXReader.ReadPCX(rGraphic);
     424          10 :     if ( nRetValue == false )
     425           8 :         rStream.SetError( SVSTREAM_FILEFORMAT_ERROR );
     426          10 :     return nRetValue;
     427           6 : }
     428             : 
     429             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10