LCOV - code coverage report
Current view: top level - filter/source/graphicfilter/ipbm - ipbm.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 262 0.0 %
Date: 2014-04-14 Functions: 0 7 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             : #include <vcl/graph.hxx>
      22             : #include <vcl/bmpacc.hxx>
      23             : 
      24             : class FilterConfigItem;
      25             : 
      26             : //============================ PBMReader ==================================
      27             : 
      28             : class PBMReader {
      29             : 
      30             : private:
      31             : 
      32             :     SvStream&           mrPBM;              // the PBM file to read
      33             : 
      34             :     sal_Bool            mbStatus;
      35             :     sal_Bool            mbRemark;           // sal_False if the stream is in a comment
      36             :     sal_Bool            mbRaw;              // RAW/ASCII MODE
      37             :     sal_uLong           mnMode;             // 0->PBM, 1->PGM, 2->PPM
      38             :     Bitmap              maBmp;
      39             :     BitmapWriteAccess*  mpAcc;
      40             :     sal_uLong           mnWidth, mnHeight;  // dimensions in pixel
      41             :     sal_uLong           mnCol;
      42             :     sal_uLong           mnMaxVal;           // max value in the <missing comment>
      43             :     sal_Bool            ImplCallback( sal_uInt16 nPercent );
      44             :     sal_Bool            ImplReadBody();
      45             :     sal_Bool            ImplReadHeader();
      46             : 
      47             : public:
      48             :                         PBMReader(SvStream & rPBM);
      49             :                         ~PBMReader();
      50             :     sal_Bool                ReadPBM(Graphic & rGraphic );
      51             : };
      52             : 
      53             : //=================== Methods of PBMReader ==============================
      54             : 
      55           0 : PBMReader::PBMReader(SvStream & rPBM)
      56             :     : mrPBM(rPBM)
      57             :     , mbStatus(true)
      58             :     , mbRemark(false)
      59             :     , mbRaw(true)
      60             :     , mnMode(0)
      61             :     , mpAcc(NULL)
      62             :     , mnWidth(0)
      63             :     , mnHeight(0)
      64             :     , mnCol(0)
      65           0 :     , mnMaxVal(0)
      66             : {
      67           0 : }
      68             : 
      69           0 : PBMReader::~PBMReader()
      70             : {
      71           0 : }
      72             : 
      73           0 : sal_Bool PBMReader::ImplCallback( sal_uInt16 /*nPercent*/ )
      74             : {
      75             : /*
      76             :     if ( pCallback != NULL )
      77             :     {
      78             :         if ( ( (*pCallback)( pCallerData, nPercent ) ) == sal_True )
      79             :         {
      80             :             mrPBM.SetError( SVSTREAM_FILEFORMAT_ERROR );
      81             :             return sal_True;
      82             :         }
      83             :     }
      84             : */
      85           0 :     return sal_False;
      86             : }
      87             : 
      88           0 : sal_Bool PBMReader::ReadPBM(Graphic & rGraphic )
      89             : {
      90             :     sal_uInt16 i;
      91             : 
      92           0 :     if ( mrPBM.GetError() )
      93           0 :         return sal_False;
      94             : 
      95           0 :     mrPBM.SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
      96             : 
      97             :     // read header:
      98             : 
      99           0 :     if ( ( mbStatus = ImplReadHeader() ) == sal_False )
     100           0 :         return sal_False;
     101             : 
     102           0 :     if ( ( mnMaxVal == 0 ) || ( mnWidth == 0 ) || ( mnHeight == 0 ) )
     103           0 :         return sal_False;
     104             : 
     105             :     // 0->PBM, 1->PGM, 2->PPM
     106           0 :     switch ( mnMode )
     107             :     {
     108             :         case 0 :
     109           0 :             maBmp = Bitmap( Size( mnWidth, mnHeight ), 1 );
     110           0 :             if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == 0 )
     111           0 :                 return sal_False;
     112           0 :             mpAcc->SetPaletteEntryCount( 2 );
     113           0 :             mpAcc->SetPaletteColor( 0, BitmapColor( 0xff, 0xff, 0xff ) );
     114           0 :             mpAcc->SetPaletteColor( 1, BitmapColor( 0x00, 0x00, 0x00 ) );
     115           0 :             break;
     116             : 
     117             :         case 1 :
     118           0 :             if ( mnMaxVal <= 1 )
     119           0 :                 maBmp = Bitmap( Size( mnWidth, mnHeight ), 1);
     120           0 :             else if ( mnMaxVal <= 15 )
     121           0 :                 maBmp = Bitmap( Size( mnWidth, mnHeight ), 4);
     122             :             else
     123           0 :                 maBmp = Bitmap( Size( mnWidth, mnHeight ), 8);
     124             : 
     125           0 :             if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == 0 )
     126           0 :                 return sal_False;
     127           0 :             mnCol = (sal_uInt16)mnMaxVal + 1;
     128           0 :             if ( mnCol > 256 )
     129           0 :                 mnCol = 256;
     130             : 
     131           0 :             mpAcc->SetPaletteEntryCount( 256 );
     132           0 :             for ( i = 0; i < mnCol; i++ )
     133             :             {
     134           0 :                 sal_uLong nCount = 255 * i / mnCol;
     135           0 :                 mpAcc->SetPaletteColor( i, BitmapColor( (sal_uInt8)nCount, (sal_uInt8)nCount, (sal_uInt8)nCount ) );
     136             :             }
     137           0 :             break;
     138             :         case 2 :
     139           0 :             maBmp = Bitmap( Size( mnWidth, mnHeight ), 24 );
     140           0 :             if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == 0 )
     141           0 :                 return sal_False;
     142           0 :             break;
     143             :     }
     144             : 
     145             :     // read bitmap data
     146           0 :     mbStatus = ImplReadBody();
     147             : 
     148           0 :     if ( mpAcc )
     149             :     {
     150           0 :         maBmp.ReleaseAccess( mpAcc ), mpAcc = NULL;
     151             :     }
     152           0 :     if ( mbStatus )
     153           0 :         rGraphic = maBmp;
     154             : 
     155           0 :     return mbStatus;
     156             : }
     157             : 
     158           0 : sal_Bool PBMReader::ImplReadHeader()
     159             : {
     160             :     sal_uInt8   nID[ 2 ];
     161             :     sal_uInt8   nDat;
     162           0 :     sal_uInt8   nMax, nCount = 0;
     163           0 :     sal_Bool    bFinished = sal_False;
     164             : 
     165           0 :     mrPBM.ReadUChar( nID[ 0 ] ).ReadUChar( nID[ 1 ] );
     166           0 :     if ( nID[ 0 ] != 'P' )
     167           0 :         return sal_False;
     168           0 :     mnMaxVal = mnWidth = mnHeight = 0;
     169           0 :     switch ( nID[ 1 ] )
     170             :     {
     171             :         case '1' :
     172           0 :             mbRaw = sal_False;
     173             :         case '4' :
     174           0 :             mnMode = 0;
     175           0 :             nMax = 2;               // number of parameters in Header
     176           0 :             mnMaxVal = 1;
     177           0 :             break;
     178             :         case '2' :
     179           0 :             mbRaw = sal_False;
     180             :         case '5' :
     181           0 :             mnMode = 1;
     182           0 :             nMax = 3;
     183           0 :             break;
     184             :         case '3' :
     185           0 :             mbRaw = sal_False;
     186             :         case '6' :
     187           0 :             mnMode = 2;
     188           0 :             nMax = 3;
     189           0 :             break;
     190             :         default:
     191           0 :             return sal_False;
     192             :     }
     193           0 :     while ( bFinished == sal_False )
     194             :     {
     195           0 :         if ( mrPBM.GetError() )
     196           0 :             return sal_False;
     197             : 
     198           0 :         mrPBM.ReadUChar( nDat );
     199             : 
     200           0 :         if ( nDat == '#' )
     201             :         {
     202           0 :             mbRemark = sal_True;
     203           0 :             continue;
     204             :         }
     205           0 :         else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
     206             :         {
     207           0 :             mbRemark = sal_False;
     208           0 :             nDat = 0x20;
     209             :         }
     210           0 :         if ( mbRemark )
     211           0 :             continue;
     212             : 
     213           0 :         if ( ( nDat == 0x20 ) || ( nDat == 0x09 ) )
     214             :         {
     215           0 :             if ( ( nCount == 0 ) && mnWidth )
     216           0 :                 nCount++;
     217           0 :             else if ( ( nCount == 1 ) && mnHeight )
     218             :             {
     219           0 :                 if ( ++nCount == nMax )
     220           0 :                     bFinished = sal_True;
     221             :             }
     222           0 :             else if ( ( nCount == 2 ) && mnMaxVal )
     223             :             {
     224           0 :                 bFinished = sal_True;
     225             :             }
     226           0 :             continue;
     227             :         }
     228           0 :         if ( ( nDat >= '0' ) && ( nDat <= '9' ) )
     229             :         {
     230           0 :             nDat -= '0';
     231           0 :             if ( nCount == 0 )
     232             :             {
     233           0 :                 mnWidth *= 10;
     234           0 :                 mnWidth += nDat;
     235             :             }
     236           0 :             else if ( nCount == 1 )
     237             :             {
     238           0 :                 mnHeight *= 10;
     239           0 :                 mnHeight += nDat;
     240             :             }
     241           0 :             else if ( nCount == 2 )
     242             :             {
     243           0 :                 mnMaxVal *= 10;
     244           0 :                 mnMaxVal += nDat;
     245             :             }
     246             :         }
     247             :         else
     248           0 :             return sal_False;
     249             :     }
     250           0 :     return mbStatus;
     251             : }
     252             : 
     253           0 : sal_Bool PBMReader::ImplReadBody()
     254             : {
     255           0 :     sal_Bool    bPara, bFinished = sal_False;
     256           0 :     sal_uInt8   nDat = 0, nCount;
     257             :     sal_uLong   nGrey, nRGB[3];
     258           0 :     sal_uLong   nWidth = 0;
     259           0 :     sal_uLong   nHeight = 0;
     260             : 
     261           0 :     if ( mbRaw )
     262             :     {
     263           0 :         signed char nShift = 0;
     264           0 :         switch ( mnMode )
     265             :         {
     266             : 
     267             :             // PBM
     268             :             case 0 :
     269           0 :                 while ( nHeight != mnHeight )
     270             :                 {
     271           0 :                     if ( mrPBM.IsEof() || mrPBM.GetError() )
     272           0 :                         return sal_False;
     273             : 
     274           0 :                     if ( --nShift < 0 )
     275             :                     {
     276           0 :                         mrPBM.ReadUChar( nDat );
     277           0 :                         nShift = 7;
     278             :                     }
     279           0 :                     mpAcc->SetPixelIndex( nHeight, nWidth, nDat >> nShift );
     280           0 :                     if ( ++nWidth == mnWidth )
     281             :                     {
     282           0 :                         nShift = 0;
     283           0 :                         nWidth = 0;
     284           0 :                         nHeight++;
     285           0 :                         ImplCallback( (sal_uInt16)( ( 100 * nHeight ) / mnHeight ) );   // processing output in percent
     286             :                     }
     287             :                 }
     288           0 :                 break;
     289             : 
     290             :             // PGM
     291             :             case 1 :
     292           0 :                 while ( nHeight != mnHeight )
     293             :                 {
     294           0 :                     if ( mrPBM.IsEof() || mrPBM.GetError() )
     295           0 :                         return sal_False;
     296             : 
     297           0 :                     mrPBM.ReadUChar( nDat );
     298           0 :                     mpAcc->SetPixelIndex( nHeight, nWidth++, nDat);
     299             : 
     300           0 :                     if ( nWidth == mnWidth )
     301             :                     {
     302           0 :                         nWidth = 0;
     303           0 :                         nHeight++;
     304           0 :                         ImplCallback( (sal_uInt16)( ( 100 * nHeight ) / mnHeight ) );   // processing output in percent
     305             :                     }
     306             :                 }
     307           0 :                 break;
     308             : 
     309             :             // PPM
     310             :             case 2 :
     311           0 :                 while ( nHeight != mnHeight )
     312             :                 {
     313           0 :                     if ( mrPBM.IsEof() || mrPBM.GetError() )
     314           0 :                         return sal_False;
     315             : 
     316             :                     sal_uInt8   nR, nG, nB;
     317             :                     sal_uLong   nRed, nGreen, nBlue;
     318           0 :                     mrPBM.ReadUChar( nR ).ReadUChar( nG ).ReadUChar( nB );
     319           0 :                     nRed = 255 * nR / mnMaxVal;
     320           0 :                     nGreen = 255 * nG / mnMaxVal;
     321           0 :                     nBlue = 255 * nB / mnMaxVal;
     322           0 :                     mpAcc->SetPixel( nHeight, nWidth++, BitmapColor( (sal_uInt8)nRed, (sal_uInt8)nGreen, (sal_uInt8)nBlue ) );
     323           0 :                     if ( nWidth == mnWidth )
     324             :                     {
     325           0 :                         nWidth = 0;
     326           0 :                         nHeight++;
     327           0 :                         ImplCallback( (sal_uInt16) ( ( 100 * nHeight ) / mnHeight ) );  // processing output in percent
     328             :                     }
     329             :                 }
     330           0 :                 break;
     331             :         }
     332             :     }
     333           0 :     else switch  ( mnMode )
     334             :     {
     335             :         // PBM
     336             :         case 0 :
     337           0 :             while ( bFinished == sal_False )
     338             :             {
     339           0 :                 if ( mrPBM.IsEof() || mrPBM.GetError() )
     340           0 :                     return sal_False;
     341             : 
     342           0 :                 mrPBM.ReadUChar( nDat );
     343             : 
     344           0 :                 if ( nDat == '#' )
     345             :                 {
     346           0 :                     mbRemark = sal_True;
     347           0 :                     continue;
     348             :                 }
     349           0 :                 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
     350             :                 {
     351           0 :                     mbRemark = sal_False;
     352           0 :                     continue;
     353             :                 }
     354           0 :                 if ( mbRemark || nDat == 0x20 || nDat == 0x09 )
     355           0 :                     continue;
     356             : 
     357           0 :                 if ( nDat == '0' || nDat == '1' )
     358             :                 {
     359           0 :                     mpAcc->SetPixelIndex( nHeight, nWidth, static_cast<sal_uInt8>(nDat - '0') );
     360           0 :                     nWidth++;
     361           0 :                     if ( nWidth == mnWidth )
     362             :                     {
     363           0 :                         nWidth = 0;
     364           0 :                         if ( ++nHeight == mnHeight )
     365           0 :                             bFinished = sal_True;
     366           0 :                         ImplCallback( (sal_uInt16) ( ( 100 * nHeight ) / mnHeight ) );  // processing output in percent
     367             :                     }
     368             :                 }
     369             :                 else
     370           0 :                     return sal_False;
     371             :             }
     372           0 :             break;
     373             : 
     374             :         // PGM
     375             :         case 1 :
     376             : 
     377           0 :             bPara = sal_False;
     378           0 :             nCount = 0;
     379           0 :             nGrey = 0;
     380             : 
     381           0 :             while ( bFinished == sal_False )
     382             :             {
     383           0 :                 if ( nCount )
     384             :                 {
     385           0 :                     nCount--;
     386           0 :                     if ( nGrey <= mnMaxVal )
     387           0 :                         nGrey = 255 * nGrey / mnMaxVal;
     388           0 :                     mpAcc->SetPixelIndex( nHeight, nWidth++, static_cast<sal_uInt8>(nGrey) );
     389           0 :                     nGrey = 0;
     390           0 :                     if ( nWidth == mnWidth )
     391             :                     {
     392           0 :                         nWidth = 0;
     393           0 :                         if ( ++nHeight == mnHeight )
     394           0 :                             bFinished = sal_True;
     395           0 :                         ImplCallback( (sal_uInt16) ( ( 100 * nHeight ) / mnHeight ) );  // processing output in percent
     396             :                     }
     397           0 :                     continue;
     398             :                 }
     399             : 
     400           0 :                 if ( mrPBM.IsEof() || mrPBM.GetError() )
     401           0 :                     return sal_False;
     402             : 
     403           0 :                 mrPBM.ReadUChar( nDat );
     404             : 
     405           0 :                 if ( nDat == '#' )
     406             :                 {
     407           0 :                     mbRemark = sal_True;
     408           0 :                     if ( bPara )
     409             :                     {
     410           0 :                         bPara = sal_False;
     411           0 :                         nCount++;
     412             :                     }
     413           0 :                     continue;
     414             :                 }
     415           0 :                 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
     416             :                 {
     417           0 :                     mbRemark = sal_False;
     418           0 :                     if ( bPara )
     419             :                     {
     420           0 :                         bPara = sal_False;
     421           0 :                         nCount++;
     422             :                     }
     423           0 :                     continue;
     424             :                 }
     425             : 
     426           0 :                 if ( nDat == 0x20 || nDat == 0x09 )
     427             :                 {
     428           0 :                     if ( bPara )
     429             :                     {
     430           0 :                         bPara = sal_False;
     431           0 :                         nCount++;
     432             :                     }
     433           0 :                     continue;
     434             :                 }
     435           0 :                 if ( nDat >= '0' && nDat <= '9' )
     436             :                 {
     437           0 :                     bPara = sal_True;
     438           0 :                     nGrey *= 10;
     439           0 :                     nGrey += nDat-'0';
     440           0 :                     continue;
     441             :                 }
     442             :                 else
     443           0 :                     return sal_False;
     444             :             }
     445           0 :             break;
     446             : 
     447             : 
     448             : 
     449             :         // PPM
     450             :         case 2 :
     451             : 
     452           0 :             bPara = sal_False;
     453           0 :             nCount = 0;
     454           0 :             nRGB[ 0 ] = nRGB[ 1 ] = nRGB[ 2 ] = 0;
     455             : 
     456           0 :             while ( bFinished == sal_False )
     457             :             {
     458           0 :                 if ( nCount == 3 )
     459             :                 {
     460           0 :                     nCount = 0;
     461           0 :                     mpAcc->SetPixel( nHeight, nWidth++, BitmapColor( static_cast< sal_uInt8 >( ( nRGB[ 0 ] * 255 ) / mnMaxVal ),
     462           0 :                                                                      static_cast< sal_uInt8 >( ( nRGB[ 1 ] * 255 ) / mnMaxVal ),
     463           0 :                                                                      static_cast< sal_uInt8 >( ( nRGB[ 2 ] * 255 ) / mnMaxVal ) ) );
     464           0 :                     nCount = 0;
     465           0 :                     nRGB[ 0 ] = nRGB[ 1 ] = nRGB[ 2 ] = 0;
     466           0 :                     if ( nWidth == mnWidth )
     467             :                     {
     468           0 :                         nWidth = 0;
     469           0 :                         if ( ++nHeight == mnHeight )
     470           0 :                             bFinished = sal_True;
     471           0 :                         ImplCallback( (sal_uInt16) ( ( 100 * nHeight ) / mnHeight ) );  // processing output in percent
     472             :                     }
     473           0 :                     continue;
     474             :                 }
     475             : 
     476           0 :                 if ( mrPBM.IsEof() || mrPBM.GetError() )
     477           0 :                     return sal_False;
     478             : 
     479           0 :                 mrPBM.ReadUChar( nDat );
     480             : 
     481           0 :                 if ( nDat == '#' )
     482             :                 {
     483           0 :                     mbRemark = sal_True;
     484           0 :                     if ( bPara )
     485             :                     {
     486           0 :                         bPara = sal_False;
     487           0 :                         nCount++;
     488             :                     }
     489           0 :                     continue;
     490             :                 }
     491           0 :                 else if ( ( nDat == 0x0d ) || ( nDat == 0x0a ) )
     492             :                 {
     493           0 :                     mbRemark = sal_False;
     494           0 :                     if ( bPara )
     495             :                     {
     496           0 :                         bPara = sal_False;
     497           0 :                         nCount++;
     498             :                     }
     499           0 :                     continue;
     500             :                 }
     501             : 
     502           0 :                 if ( nDat == 0x20 || nDat == 0x09 )
     503             :                 {
     504           0 :                     if ( bPara )
     505             :                     {
     506           0 :                         bPara = sal_False;
     507           0 :                         nCount++;
     508             :                     }
     509           0 :                     continue;
     510             :                 }
     511           0 :                 if ( nDat >= '0' && nDat <= '9' )
     512             :                 {
     513           0 :                     bPara = sal_True;
     514           0 :                     nRGB[ nCount ] *= 10;
     515           0 :                     nRGB[ nCount ] += nDat-'0';
     516           0 :                     continue;
     517             :                 }
     518             :                 else
     519           0 :                     return sal_False;
     520             :             }
     521           0 :             break;
     522             :     }
     523           0 :     return mbStatus;
     524             : }
     525             : 
     526             : //================== GraphicImport - the exported function ================
     527             : 
     528             : // this needs to be kept in sync with
     529             : // ImpFilterLibCacheEntry::GetImportFunction() from
     530             : // vcl/source/filter/graphicfilter.cxx
     531             : #if defined(DISABLE_DYNLOADING)
     532             : #define GraphicImport ipbGraphicImport
     533             : #endif
     534             : 
     535             : extern "C" SAL_DLLPUBLIC_EXPORT bool SAL_CALL
     536           0 : GraphicImport( SvStream & rStream, Graphic & rGraphic, FilterConfigItem* )
     537             : {
     538           0 :     PBMReader aPBMReader(rStream);
     539             : 
     540           0 :     return aPBMReader.ReadPBM(rGraphic );
     541             : }
     542             : 
     543             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10