LCOV - code coverage report
Current view: top level - libreoffice/svtools/source/filter/ixbm - xbmread.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 180 0.0 %
Date: 2012-12-27 Functions: 0 9 0.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             : #define _XBMPRIVATE
      22             : #include <ctype.h>
      23             : #include <comphelper/string.hxx>
      24             : #include "xbmread.hxx"
      25             : 
      26             : // -------------
      27             : // - XBMReader -
      28             : // -------------
      29             : 
      30           0 : XBMReader::XBMReader( SvStream& rStm ) :
      31             :             rIStm           ( rStm ),
      32             :             pAcc1           ( NULL ),
      33           0 :             nLastPos        ( rStm.Tell() ),
      34             :             nWidth          ( 0 ),
      35             :             nHeight         ( 0 ),
      36           0 :             bStatus         ( sal_True )
      37             : {
      38           0 :     pHexTable = new short[ 256 ];
      39           0 :     maUpperName = rtl::OUString("SVIXBM");
      40           0 :     InitTable();
      41           0 : }
      42             : 
      43             : // ------------------------------------------------------------------------
      44             : 
      45           0 : XBMReader::~XBMReader()
      46             : {
      47           0 :     delete[] pHexTable;
      48             : 
      49           0 :     if( pAcc1 )
      50           0 :         aBmp1.ReleaseAccess( pAcc1 );
      51           0 : }
      52             : 
      53             : // ------------------------------------------------------------------------
      54             : 
      55           0 : void XBMReader::InitTable()
      56             : {
      57           0 :     memset( pHexTable, 0, sizeof( short ) * 256 );
      58             : 
      59           0 :     pHexTable[(int)'0'] = 0;
      60           0 :     pHexTable[(int)'1'] = 1;
      61           0 :     pHexTable[(int)'2'] = 2;
      62           0 :     pHexTable[(int)'3'] = 3;
      63           0 :     pHexTable[(int)'4'] = 4;
      64           0 :     pHexTable[(int)'5'] = 5;
      65           0 :     pHexTable[(int)'6'] = 6;
      66           0 :     pHexTable[(int)'7'] = 7;
      67           0 :     pHexTable[(int)'8'] = 8;
      68           0 :     pHexTable[(int)'9'] = 9;
      69           0 :     pHexTable[(int)'A'] = 10;
      70           0 :     pHexTable[(int)'B'] = 11;
      71           0 :     pHexTable[(int)'C'] = 12;
      72           0 :     pHexTable[(int)'D'] = 13;
      73           0 :     pHexTable[(int)'E'] = 14;
      74           0 :     pHexTable[(int)'F'] = 15;
      75           0 :     pHexTable[(int)'X'] = 0;
      76           0 :     pHexTable[(int)'a'] = 10;
      77           0 :     pHexTable[(int)'b'] = 11;
      78           0 :     pHexTable[(int)'c'] = 12;
      79           0 :     pHexTable[(int)'d'] = 13;
      80           0 :     pHexTable[(int)'e'] = 14;
      81           0 :     pHexTable[(int)'f'] = 15;
      82           0 :     pHexTable[(int)'x'] = 0;
      83           0 :     pHexTable[(int)' '] =     -1;
      84           0 :     pHexTable[(int)','] = -1;
      85           0 :     pHexTable[(int)'}'] = -1;
      86           0 :     pHexTable[(int)'\n'] = -1;
      87           0 :     pHexTable[(int)'\t'] = -1;
      88           0 :     pHexTable[(int)'\0'] = -1;
      89           0 : }
      90             : 
      91             : // ------------------------------------------------------------------------
      92             : 
      93           0 : rtl::OString XBMReader::FindTokenLine( SvStream* pInStm, const char* pTok1,
      94             :                                  const char* pTok2, const char* pTok3 )
      95             : {
      96           0 :     rtl::OString aRet;
      97             :     sal_Int32 nPos1, nPos2, nPos3;
      98             : 
      99           0 :     bStatus = sal_False;
     100             : 
     101           0 :     do
     102             :     {
     103           0 :         if( !pInStm->ReadLine( aRet ) )
     104           0 :             break;
     105             : 
     106           0 :         if( pTok1 )
     107             :         {
     108           0 :             if( ( nPos1 = aRet.indexOf( pTok1 ) ) != -1 )
     109             :             {
     110           0 :                 bStatus = sal_True;
     111             : 
     112           0 :                 if( pTok2 )
     113             :                 {
     114           0 :                     bStatus = sal_False;
     115             : 
     116           0 :                     if( ( ( nPos2 = aRet.indexOf( pTok2 ) ) != -1 ) &&
     117             :                          ( nPos2 > nPos1 ) )
     118             :                     {
     119           0 :                         bStatus = sal_True;
     120             : 
     121           0 :                         if( pTok3 )
     122             :                         {
     123           0 :                             bStatus = sal_False;
     124             : 
     125           0 :                             if( ( ( nPos3 = aRet.indexOf( pTok3 ) ) != -1 ) && ( nPos3 > nPos2 ) )
     126           0 :                                 bStatus = sal_True;
     127             :                         }
     128             :                     }
     129             :                 }
     130             :             }
     131             :         }
     132             :     }
     133           0 :     while( !bStatus );
     134             : 
     135           0 :     return aRet;
     136             : }
     137             : 
     138             : // ------------------------------------------------------------------------
     139             : 
     140           0 : long XBMReader::ParseDefine( const sal_Char* pDefine )
     141             : {
     142           0 :     long    nRet = 0;
     143           0 :     char*   pTmp = (char*) pDefine;
     144             :     unsigned char   cTmp;
     145             : 
     146             :     // bis zum Ende gehen
     147           0 :     pTmp += ( strlen( pDefine ) - 1 );
     148           0 :     cTmp = *pTmp--;
     149             : 
     150             :     // letzte Ziffer suchen
     151           0 :     while( pHexTable[ cTmp ] == -1 )
     152           0 :         cTmp = *pTmp--;
     153             : 
     154             :     // bis vor die Zahl laufen
     155           0 :     while( pHexTable[ cTmp ] != -1 )
     156           0 :         cTmp = *pTmp--;
     157             : 
     158             :     // auf Anfang der Zahl gehen
     159           0 :     pTmp += 2;
     160             : 
     161             :     // Hex lesen
     162           0 :     if( ( pTmp[0] == '0' ) && ( ( pTmp[1] == 'X' ) || ( pTmp[1] == 'x' ) ) )
     163             :     {
     164           0 :         pTmp += 2;
     165           0 :         cTmp = *pTmp++;
     166             : 
     167           0 :         while ( pHexTable[ cTmp ] != -1 )
     168             :         {
     169           0 :             nRet = ( nRet << 4 ) + pHexTable[ cTmp ];
     170           0 :             cTmp = *pTmp++;
     171             :         }
     172             :     }
     173             :     // Dezimal lesen
     174             :     else
     175             :     {
     176           0 :         cTmp = *pTmp++;
     177           0 :         while( ( cTmp >= '0' ) && ( cTmp <= '9' ) )
     178             :         {
     179           0 :             nRet = nRet * 10 + ( cTmp - '0' );
     180           0 :             cTmp = *pTmp++;
     181             :         }
     182             :     }
     183             : 
     184           0 :     return nRet;
     185             : }
     186             : 
     187             : // ------------------------------------------------------------------------
     188             : 
     189           0 : sal_Bool XBMReader::ParseData( SvStream* pInStm, const rtl::OString& aLastLine, XBMFormat eFormat )
     190             : {
     191           0 :     rtl::OString    aLine;
     192           0 :     long            nRow = 0;
     193           0 :     long            nCol = 0;
     194           0 :     long            nBits = ( eFormat == XBM10 ) ? 16 : 8;
     195             :     long            nBit;
     196             :     sal_uInt16          nValue;
     197             :     sal_uInt16          nDigits;
     198           0 :     sal_Bool            bFirstLine = sal_True;
     199             : 
     200           0 :     while( nRow < nHeight )
     201             :     {
     202           0 :         if( bFirstLine )
     203             :         {
     204             :             sal_Int32 nPos;
     205             : 
     206             :             // einfuehrende geschweifte Klammer loeschen
     207           0 :             if( (nPos = ( aLine = aLastLine ).indexOf('{') ) != -1 )
     208           0 :                 aLine = aLine.copy(nPos + 1);
     209             : 
     210           0 :             bFirstLine = sal_False;
     211             :         }
     212           0 :         else if( !pInStm->ReadLine( aLine ) )
     213           0 :             break;
     214             : 
     215           0 :         if (!aLine.isEmpty())
     216             :         {
     217           0 :             const sal_Int32 nCount = comphelper::string::getTokenCount(aLine, ',');
     218             : 
     219           0 :             for( sal_Int32 i = 0; ( i < nCount ) && ( nRow < nHeight ); ++i )
     220             :             {
     221           0 :                 const rtl::OString aToken(comphelper::string::getToken(aLine,i, ','));
     222           0 :                 const sal_Int32 nLen = aToken.getLength();
     223           0 :                 sal_Bool bProcessed = sal_False;
     224             : 
     225           0 :                 nBit = nDigits = nValue = 0;
     226             : 
     227           0 :                 for (sal_Int32 n = 0; n < nLen; ++n)
     228             :                 {
     229           0 :                     const unsigned char cChar = aToken[n];
     230           0 :                     const short         nTable = pHexTable[ cChar ];
     231             : 
     232           0 :                     if( isxdigit( cChar ) || !nTable )
     233             :                     {
     234           0 :                         nValue = ( nValue << 4 ) + nTable;
     235           0 :                         nDigits++;
     236           0 :                         bProcessed = sal_True;
     237             :                     }
     238           0 :                     else if( ( nTable < 0 ) && nDigits )
     239             :                     {
     240           0 :                         bProcessed = sal_True;
     241           0 :                         break;
     242             :                     }
     243             :                 }
     244             : 
     245           0 :                 if( bProcessed )
     246             :                 {
     247           0 :                     while( ( nCol < nWidth ) && ( nBit < nBits ) )
     248           0 :                         pAcc1->SetPixel( nRow, nCol++, ( nValue & ( 1 << nBit++ ) ) ? aBlack : aWhite );
     249             : 
     250           0 :                     if( nCol == nWidth )
     251           0 :                         nCol = 0, nRow++;
     252             :                 }
     253           0 :             }
     254             :         }
     255             :     }
     256             : 
     257           0 :     return sal_True;
     258             : }
     259             : 
     260             : // ------------------------------------------------------------------------
     261             : 
     262           0 : ReadState XBMReader::ReadXBM( Graphic& rGraphic )
     263             : {
     264             :     ReadState   eReadState;
     265             :     sal_uInt8       cDummy;
     266             : 
     267             :     // sehen, ob wir _alles_ lesen koennen
     268           0 :     rIStm.Seek( STREAM_SEEK_TO_END );
     269           0 :     rIStm >> cDummy;
     270             : 
     271             :     // falls wir nicht alles lesen koennen
     272             :     // kehren wir zurueck und warten auf neue Daten
     273           0 :     if ( rIStm.GetError() != ERRCODE_IO_PENDING )
     274             :     {
     275           0 :         rIStm.Seek( nLastPos );
     276           0 :         bStatus = sal_False;
     277           0 :         rtl::OString aLine = FindTokenLine( &rIStm, "#define", "_width" );
     278             : 
     279           0 :         if ( bStatus )
     280             :         {
     281             :             int nValue;
     282           0 :             if ( ( nValue = (int) ParseDefine( aLine.getStr() ) ) > 0 )
     283             :             {
     284           0 :                 nWidth = nValue;
     285           0 :                 aLine = FindTokenLine( &rIStm, "#define", "_height" );
     286             : 
     287             :                 // Falls die Hoehe nicht folgt, suchen wir noch
     288             :                 // einmal vom Anfang der Datei an
     289           0 :                 if ( !bStatus )
     290             :                 {
     291           0 :                     rIStm.Seek( nLastPos );
     292           0 :                     aLine = FindTokenLine( &rIStm, "#define", "_height" );
     293             :                 }
     294             :             }
     295             :             else
     296           0 :                 bStatus = sal_False;
     297             : 
     298           0 :             if ( bStatus )
     299             :             {
     300           0 :                 if ( ( nValue = (int) ParseDefine( aLine.getStr() ) ) > 0 )
     301             :                 {
     302           0 :                     nHeight = nValue;
     303           0 :                     aLine = FindTokenLine( &rIStm, "static", "_bits" );
     304             : 
     305           0 :                     if ( bStatus )
     306             :                     {
     307           0 :                         XBMFormat eFormat = XBM10;
     308             : 
     309           0 :                         if (aLine.indexOfL(RTL_CONSTASCII_STRINGPARAM("short")) != -1)
     310           0 :                             eFormat = XBM10;
     311           0 :                         else if (aLine.indexOfL(RTL_CONSTASCII_STRINGPARAM("char")) != -1)
     312           0 :                             eFormat = XBM11;
     313             :                         else
     314           0 :                             bStatus = sal_False;
     315             : 
     316           0 :                         if ( bStatus && nWidth && nHeight )
     317             :                         {
     318           0 :                             aBmp1 = Bitmap( Size( nWidth, nHeight ), 1 );
     319           0 :                             pAcc1 = aBmp1.AcquireWriteAccess();
     320             : 
     321           0 :                             if( pAcc1 )
     322             :                             {
     323           0 :                                 aWhite = pAcc1->GetBestMatchingColor( Color( COL_WHITE ) );
     324           0 :                                 aBlack = pAcc1->GetBestMatchingColor( Color( COL_BLACK ) );
     325           0 :                                 bStatus = ParseData( &rIStm, aLine, eFormat );
     326             :                             }
     327             :                             else
     328           0 :                                 bStatus = sal_False;
     329             :                         }
     330             :                     }
     331             :                 }
     332             :             }
     333             :         }
     334             : 
     335           0 :         if( bStatus )
     336             :         {
     337           0 :             Bitmap aBlackBmp( Size( pAcc1->Width(), pAcc1->Height() ), 1 );
     338             : 
     339           0 :             aBmp1.ReleaseAccess( pAcc1 ), pAcc1 = NULL;
     340           0 :             aBlackBmp.Erase( Color( COL_BLACK ) );
     341           0 :             rGraphic = BitmapEx( aBlackBmp, aBmp1 );
     342           0 :             eReadState = XBMREAD_OK;
     343             :         }
     344             :         else
     345           0 :             eReadState = XBMREAD_ERROR;
     346             :     }
     347             :     else
     348             :     {
     349           0 :         rIStm.ResetError();
     350           0 :         eReadState = XBMREAD_NEED_MORE;
     351             :     }
     352             : 
     353           0 :     return eReadState;
     354             : }
     355             : 
     356             : // -------------
     357             : // - ImportXBM -
     358             : // -------------
     359             : 
     360           0 : sal_Bool ImportXBM( SvStream& rStm, Graphic& rGraphic )
     361             : {
     362           0 :     XBMReader*  pXBMReader = (XBMReader*) rGraphic.GetContext();
     363             :     ReadState   eReadState;
     364           0 :     sal_Bool        bRet = sal_True;
     365             : 
     366           0 :     if( !pXBMReader )
     367           0 :         pXBMReader = new XBMReader( rStm );
     368             : 
     369           0 :     rGraphic.SetContext( NULL );
     370           0 :     eReadState = pXBMReader->ReadXBM( rGraphic );
     371             : 
     372           0 :     if( eReadState == XBMREAD_ERROR )
     373             :     {
     374           0 :         bRet = sal_False;
     375           0 :         delete pXBMReader;
     376             :     }
     377           0 :     else if( eReadState == XBMREAD_OK )
     378           0 :         delete pXBMReader;
     379             :     else
     380           0 :         rGraphic.SetContext( pXBMReader );
     381             : 
     382           0 :     return bRet;
     383             : }
     384             : 
     385             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10