LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/filter/source/graphicfilter/iras - iras.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 131 0.0 %
Date: 2013-07-09 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             : #include <vcl/graph.hxx>
      22             : #include <vcl/bmpacc.hxx>
      23             : 
      24             : class FilterConfigItem;
      25             : 
      26             : #define RAS_TYPE_OLD            0x00000000      // supported formats by this filter
      27             : #define RAS_TYPE_STANDARD       0x00000001
      28             : #define RAS_TYPE_BYTE_ENCODED   0x00000002
      29             : #define RAS_TYPE_RGB_FORMAT     0x00000003
      30             : 
      31             : #define RAS_COLOR_NO_MAP        0x00000000
      32             : #define RAS_COLOR_RGB_MAP       0x00000001
      33             : #define RAS_COLOR_RAW_MAP       0x00000002
      34             : 
      35             : #define SUNRASTER_MAGICNUMBER   0x59a66a95
      36             : 
      37             : //============================ RASReader ==================================
      38             : 
      39             : class RASReader {
      40             : 
      41             : private:
      42             : 
      43             :     SvStream&           m_rRAS;                 // Die einzulesende RAS-Datei
      44             : 
      45             :     sal_Bool                mbStatus;
      46             :     Bitmap              maBmp;
      47             :     BitmapWriteAccess*  mpAcc;
      48             :     sal_uInt32          mnWidth, mnHeight;      // Bildausmass in Pixeln
      49             :     sal_uInt16              mnDstBitsPerPix;
      50             :     sal_uInt16              mnDstColors;
      51             :     sal_uInt32          mnDepth, mnImageDatSize, mnType;
      52             :     sal_uInt32          mnColorMapType, mnColorMapSize;
      53             :     sal_uInt8               mnRepCount, mnRepVal;   // RLE Decoding
      54             :     sal_Bool                mbPalette;
      55             : 
      56             :     sal_Bool                ImplReadBody();
      57             :     sal_Bool                ImplReadHeader();
      58             :     sal_uInt8               ImplGetByte();
      59             : 
      60             : public:
      61             :                         RASReader(SvStream &rRAS);
      62             :                         ~RASReader();
      63             :     sal_Bool                ReadRAS(Graphic & rGraphic);
      64             : };
      65             : 
      66             : //=================== Methoden von RASReader ==============================
      67             : 
      68           0 : RASReader::RASReader(SvStream &rRAS)
      69             :     : m_rRAS(rRAS)
      70             :     , mbStatus(sal_True)
      71             :     , mpAcc(NULL)
      72             :     , mnRepCount(0)
      73           0 :     , mbPalette(sal_False)
      74             : {
      75           0 : }
      76             : 
      77           0 : RASReader::~RASReader()
      78             : {
      79           0 : }
      80             : 
      81             : //----------------------------------------------------------------------------
      82             : 
      83           0 : sal_Bool RASReader::ReadRAS(Graphic & rGraphic)
      84             : {
      85             :     sal_uInt32 nMagicNumber;
      86             : 
      87           0 :     if ( m_rRAS.GetError() )
      88           0 :         return sal_False;
      89             : 
      90           0 :     m_rRAS.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
      91           0 :     m_rRAS >> nMagicNumber;
      92           0 :     if ( nMagicNumber != SUNRASTER_MAGICNUMBER )
      93           0 :         return sal_False;
      94             : 
      95             :     // Kopf einlesen:
      96             : 
      97           0 :     if ( ( mbStatus = ImplReadHeader() ) == sal_False )
      98           0 :         return sal_False;
      99             : 
     100           0 :     maBmp = Bitmap( Size( mnWidth, mnHeight ), mnDstBitsPerPix );
     101           0 :     if ( ( mpAcc = maBmp.AcquireWriteAccess() ) == sal_False )
     102           0 :         return sal_False;
     103             : 
     104           0 :     if ( mnDstBitsPerPix <= 8 )     // paletten bildchen
     105             :     {
     106           0 :         if ( mnColorMapType == RAS_COLOR_RAW_MAP )      // RAW Colormap wird geskipped
     107             :         {
     108           0 :             sal_uLong nCurPos = m_rRAS.Tell();
     109           0 :             m_rRAS.Seek( nCurPos + mnColorMapSize );
     110             :         }
     111           0 :         else if ( mnColorMapType == RAS_COLOR_RGB_MAP ) // RGB koennen wir auslesen
     112             :         {
     113           0 :             mnDstColors = (sal_uInt16)( mnColorMapSize / 3 );
     114             : 
     115           0 :             if ( ( 1 << mnDstBitsPerPix ) < mnDstColors )
     116           0 :                 return sal_False;
     117             : 
     118           0 :             if ( ( mnDstColors >= 2 ) && ( ( mnColorMapSize % 3 ) == 0 ) )
     119             :             {
     120           0 :                 mpAcc->SetPaletteEntryCount( mnDstColors );
     121             :                 sal_uInt16  i;
     122             :                 sal_uInt8   nRed[256], nGreen[256], nBlue[256];
     123           0 :                 for ( i = 0; i < mnDstColors; i++ ) m_rRAS >> nRed[ i ];
     124           0 :                 for ( i = 0; i < mnDstColors; i++ ) m_rRAS >> nGreen[ i ];
     125           0 :                 for ( i = 0; i < mnDstColors; i++ ) m_rRAS >> nBlue[ i ];
     126           0 :                 for ( i = 0; i < mnDstColors; i++ )
     127             :                 {
     128           0 :                     mpAcc->SetPaletteColor( i, BitmapColor( nRed[ i ], nGreen[ i ], nBlue[ i ] ) );
     129             :                 }
     130           0 :                 mbPalette = sal_True;
     131             :             }
     132             :             else
     133           0 :                 return sal_False;
     134             : 
     135             :         }
     136           0 :         else if ( mnColorMapType != RAS_COLOR_NO_MAP )  // alles andere ist kein standard
     137           0 :             return sal_False;
     138             : 
     139           0 :         if ( !mbPalette )
     140             :         {
     141           0 :             mnDstColors = 1 << mnDstBitsPerPix;
     142           0 :             mpAcc->SetPaletteEntryCount( mnDstColors );
     143           0 :             for ( sal_uInt16 i = 0; i < mnDstColors; i++ )
     144             :             {
     145           0 :                 sal_uLong nCount = 255 - ( 255 * i / ( mnDstColors - 1 ) );
     146           0 :                 mpAcc->SetPaletteColor( i, BitmapColor( (sal_uInt8)nCount, (sal_uInt8)nCount, (sal_uInt8)nCount ) );
     147             :             }
     148             :         }
     149             :     }
     150             :     else
     151             :     {
     152           0 :         if ( mnColorMapType != RAS_COLOR_NO_MAP )   // when graphic has more then 256 colors and a color map we skip
     153             :         {                                           // the colormap
     154           0 :             sal_uLong nCurPos = m_rRAS.Tell();
     155           0 :             m_rRAS.Seek( nCurPos + mnColorMapSize );
     156             :         }
     157             :     }
     158             : 
     159             :     // Bitmap-Daten einlesen
     160           0 :     mbStatus = ImplReadBody();
     161             : 
     162           0 :     if ( mpAcc )
     163             :     {
     164           0 :         maBmp.ReleaseAccess( mpAcc ), mpAcc = NULL;
     165             :     }
     166           0 :     if ( mbStatus )
     167           0 :         rGraphic = maBmp;
     168             : 
     169           0 :     return mbStatus;
     170             : }
     171             : 
     172             : //----------------------------------------------------------------------------
     173             : 
     174           0 : sal_Bool RASReader::ImplReadHeader()
     175             : {
     176           0 :     m_rRAS >> mnWidth >> mnHeight >> mnDepth >> mnImageDatSize >>
     177           0 :         mnType >> mnColorMapType >> mnColorMapSize;
     178             : 
     179           0 :     if ( mnWidth == 0 || mnHeight == 0 )
     180           0 :         mbStatus = sal_False;
     181             : 
     182           0 :     switch ( mnDepth )
     183             :     {
     184             :         case 24 :
     185             :         case  8 :
     186             :         case  1 :
     187           0 :             mnDstBitsPerPix = (sal_uInt16)mnDepth;
     188           0 :             break;
     189             :         case 32 :
     190           0 :             mnDstBitsPerPix = 24;
     191           0 :             break;
     192             : 
     193             :         default :
     194           0 :             mbStatus = sal_False;
     195             :     }
     196             : 
     197           0 :     switch ( mnType )
     198             :     {
     199             :         case RAS_TYPE_OLD :
     200             :         case RAS_TYPE_STANDARD :
     201             :         case RAS_TYPE_RGB_FORMAT :
     202             :         case RAS_TYPE_BYTE_ENCODED :            // this type will be supported later
     203           0 :             break;
     204             : 
     205             :         default:
     206           0 :             mbStatus = sal_False;
     207             :     }
     208           0 :     return mbStatus;
     209             : }
     210             : 
     211             : //----------------------------------------------------------------------------
     212             : 
     213           0 : sal_Bool RASReader::ImplReadBody()
     214             : {
     215             :     sal_uLong   x, y;
     216           0 :     sal_uInt8   nDat = 0;
     217             :     sal_uInt8    nRed, nGreen, nBlue;
     218           0 :     switch ( mnDstBitsPerPix )
     219             :     {
     220             :         case 1 :
     221           0 :             for ( y = 0; y < mnHeight; y++ )
     222             :             {
     223           0 :                 for ( x = 0; x < mnWidth; x++ )
     224             :                 {
     225           0 :                     if (!(x & 7))
     226           0 :                         nDat = ImplGetByte();
     227             :                     mpAcc->SetPixelIndex( y, x,
     228             :                         sal::static_int_cast< sal_uInt8 >(
     229           0 :                             nDat >> ( ( x & 7 ) ^ 7 )) );
     230             :                 }
     231           0 :                 if (!( ( x - 1 ) & 0x8 ) ) ImplGetByte();       // WORD ALIGNMENT ???
     232             :             }
     233           0 :             break;
     234             : 
     235             :         case 8 :
     236           0 :             for ( y = 0; y < mnHeight; y++ )
     237             :             {
     238           0 :                 for ( x = 0; x < mnWidth; x++ )
     239             :                 {
     240           0 :                     nDat = ImplGetByte();
     241           0 :                     mpAcc->SetPixelIndex( y, x, nDat );
     242             :                 }
     243           0 :                 if ( x & 1 ) ImplGetByte();                     // WORD ALIGNMENT ???
     244             :             }
     245           0 :             break;
     246             : 
     247             :         case 24 :
     248           0 :             switch ( mnDepth )
     249             :             {
     250             : 
     251             :                 case 24 :
     252           0 :                     for ( y = 0; y < mnHeight; y++ )
     253             :                     {
     254           0 :                         for ( x = 0; x < mnWidth; x++ )
     255             :                         {
     256           0 :                             if ( mnType == RAS_TYPE_RGB_FORMAT )
     257             :                             {
     258           0 :                                 nRed = ImplGetByte();
     259           0 :                                 nGreen = ImplGetByte();
     260           0 :                                 nBlue = ImplGetByte();
     261             :                             }
     262             :                             else
     263             :                             {
     264           0 :                                 nBlue = ImplGetByte();
     265           0 :                                 nGreen = ImplGetByte();
     266           0 :                                 nRed = ImplGetByte();
     267             :                             }
     268           0 :                             mpAcc->SetPixel ( y, x, BitmapColor( nRed, nGreen, nBlue ) );
     269             :                         }
     270           0 :                         if ( x & 1 ) ImplGetByte();                     // WORD ALIGNMENT ???
     271             :                     }
     272           0 :                     break;
     273             : 
     274             :                 case 32 :
     275           0 :                     for ( y = 0; y < mnHeight; y++ )
     276             :                     {
     277           0 :                         for ( x = 0; x < mnWidth; x++ )
     278             :                         {
     279           0 :                             nDat = ImplGetByte();               // pad byte > nil
     280           0 :                             if ( mnType == RAS_TYPE_RGB_FORMAT )
     281             :                             {
     282           0 :                                 nRed = ImplGetByte();
     283           0 :                                 nGreen = ImplGetByte();
     284           0 :                                 nBlue = ImplGetByte();
     285             :                             }
     286             :                             else
     287             :                             {
     288           0 :                                 nBlue = ImplGetByte();
     289           0 :                                 nGreen = ImplGetByte();
     290           0 :                                 nRed = ImplGetByte();
     291             :                             }
     292           0 :                             mpAcc->SetPixel ( y, x, BitmapColor( nRed, nGreen, nBlue ) );
     293             :                         }
     294             :                     }
     295           0 :                     break;
     296             :             }
     297           0 :             break;
     298             : 
     299             :         default:
     300           0 :             mbStatus = sal_False;
     301           0 :             break;
     302             :     }
     303           0 :     return mbStatus;
     304             : }
     305             : 
     306             : //----------------------------------------------------------------------------
     307             : 
     308           0 : sal_uInt8 RASReader::ImplGetByte()
     309             : {
     310             :     sal_uInt8 nRetVal;
     311           0 :     if ( mnType != RAS_TYPE_BYTE_ENCODED )
     312             :     {
     313           0 :         m_rRAS >> nRetVal;
     314           0 :         return nRetVal;
     315             :     }
     316             :     else
     317             :     {
     318           0 :         if ( mnRepCount )
     319             :         {
     320           0 :             mnRepCount--;
     321           0 :             return mnRepVal;
     322             :         }
     323             :         else
     324             :         {
     325           0 :             m_rRAS >> nRetVal;
     326           0 :             if ( nRetVal != 0x80 )
     327           0 :                 return nRetVal;
     328           0 :             m_rRAS >> nRetVal;
     329           0 :             if ( nRetVal == 0 )
     330           0 :                 return 0x80;
     331           0 :             mnRepCount = nRetVal    ;
     332           0 :             m_rRAS >> mnRepVal;
     333           0 :             return mnRepVal;
     334             :         }
     335             :     }
     336             : }
     337             : 
     338             : //================== GraphicImport - die exportierte Funktion ================
     339             : 
     340             : #if defined(DISABLE_DYNLOADING) || defined(LIBO_MERGELIBS)
     341             : #define GraphicImport iraGraphicImport
     342             : #endif
     343             : 
     344             : extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
     345           0 : GraphicImport(SvStream & rStream, Graphic & rGraphic, FilterConfigItem*, sal_Bool)
     346             : {
     347           0 :     RASReader aRASReader(rStream);
     348             : 
     349           0 :     return aRASReader.ReadRAS(rGraphic );
     350           0 : }
     351             : 
     352             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10