LCOV - code coverage report
Current view: top level - filter/source/graphicfilter/itiff - lzwdecom.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 94 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 "lzwdecom.hxx"
      22             : 
      23             : #define MAX_TABLE_SIZE 4096
      24             : 
      25           0 : LZWDecompressor::LZWDecompressor()
      26             :     : pIStream(NULL)
      27             :     , nTableSize(0)
      28             :     , bEOIFound(false)
      29             :     , bInvert(false)
      30             :     , bFirst(true)
      31             :     , nOldCode(0)
      32             :     , pOutBufData(NULL)
      33             :     , nOutBufDataLen(0)
      34             :     , nInputBitsBuf(0)
      35           0 :     , nInputBitsBufSize(0)
      36             : {
      37             :     sal_uInt16 i;
      38             : 
      39           0 :     pTable=new LZWTableEntry[MAX_TABLE_SIZE];
      40           0 :     pOutBuf=new sal_uInt8[MAX_TABLE_SIZE];
      41           0 :     for (i=0; i<MAX_TABLE_SIZE; i++)
      42             :     {
      43           0 :         pTable[i].nPrevCode=0;
      44           0 :         pTable[i].nDataCount=1;
      45           0 :         pTable[i].nData=(sal_uInt8)i;
      46             :     }
      47           0 : }
      48             : 
      49             : 
      50           0 : LZWDecompressor::~LZWDecompressor()
      51             : {
      52           0 :     delete[] pOutBuf;
      53           0 :     delete[] pTable;
      54           0 : }
      55             : 
      56             : 
      57           0 : void LZWDecompressor::StartDecompression(SvStream & rIStream)
      58             : {
      59           0 :     pIStream=&rIStream;
      60             : 
      61           0 :     nTableSize=258;
      62             : 
      63           0 :     bEOIFound=sal_False;
      64             : 
      65           0 :     nOutBufDataLen=0;
      66             : 
      67           0 :     pIStream->ReadUChar( nInputBitsBuf );
      68             : 
      69           0 :     nInputBitsBufSize=8;
      70             : 
      71           0 :     if ( bFirst )
      72             :     {
      73           0 :         bInvert = nInputBitsBuf == 1;
      74           0 :         bFirst = sal_False;
      75             :     }
      76             : 
      77           0 :     if ( bInvert )
      78           0 :         nInputBitsBuf = ( ( nInputBitsBuf & 1 ) << 7 ) | ( ( nInputBitsBuf & 2 ) << 5 ) | ( ( nInputBitsBuf & 4 ) << 3 ) | ( ( nInputBitsBuf & 8 ) << 1 ) | ( ( nInputBitsBuf & 16 ) >> 1 ) | ( ( nInputBitsBuf & 32 ) >> 3 ) | ( ( nInputBitsBuf & 64 ) >> 5 ) | ( (nInputBitsBuf & 128 ) >> 7 );
      79           0 : }
      80             : 
      81             : 
      82           0 : sal_uLong LZWDecompressor::Decompress(sal_uInt8 * pTarget, sal_uLong nMaxCount)
      83             : {
      84             :     sal_uLong nCount;
      85             : 
      86           0 :     if (pIStream==NULL) return 0;
      87             : 
      88           0 :     nCount=0;
      89             :     for (;;) {
      90             : 
      91           0 :         if (pIStream->GetError()) break;
      92             : 
      93           0 :         if (((sal_uLong)nOutBufDataLen)>=nMaxCount) {
      94           0 :             nOutBufDataLen = nOutBufDataLen - (sal_uInt16)nMaxCount;
      95           0 :             nCount+=nMaxCount;
      96           0 :             while (nMaxCount>0) {
      97           0 :                 *(pTarget++)=*(pOutBufData++);
      98           0 :                 nMaxCount--;
      99             :             }
     100           0 :             break;
     101             :         }
     102             : 
     103           0 :         nMaxCount-=(sal_uLong)nOutBufDataLen;
     104           0 :         nCount+=nOutBufDataLen;
     105           0 :         while (nOutBufDataLen>0) {
     106           0 :             *(pTarget++)=*(pOutBufData++);
     107           0 :             nOutBufDataLen--;
     108             :         }
     109             : 
     110           0 :         if (bEOIFound==sal_True) break;
     111             : 
     112           0 :         DecompressSome();
     113             : 
     114             :     }
     115             : 
     116           0 :     return nCount;
     117             : }
     118             : 
     119             : 
     120           0 : sal_uInt16 LZWDecompressor::GetNextCode()
     121             : {
     122             :     sal_uInt16 nBits,nCode;
     123             : 
     124           0 :     if      (nTableSize<511)  nBits=9;
     125           0 :     else if (nTableSize<1023) nBits=10;
     126           0 :     else if (nTableSize<2047) nBits=11;
     127           0 :     else                      nBits=12;
     128             : 
     129           0 :     nCode=0;
     130           0 :     do {
     131           0 :         if (nInputBitsBufSize<=nBits)
     132             :         {
     133           0 :             nCode=(nCode<<nInputBitsBufSize) | nInputBitsBuf;
     134           0 :             nBits = nBits - nInputBitsBufSize;
     135           0 :             pIStream->ReadUChar( nInputBitsBuf );
     136           0 :             if ( bInvert )
     137           0 :                 nInputBitsBuf = ( ( nInputBitsBuf & 1 ) << 7 ) | ( ( nInputBitsBuf & 2 ) << 5 ) | ( ( nInputBitsBuf & 4 ) << 3 ) | ( ( nInputBitsBuf & 8 ) << 1 ) | ( ( nInputBitsBuf & 16 ) >> 1 ) | ( ( nInputBitsBuf & 32 ) >> 3 ) | ( ( nInputBitsBuf & 64 ) >> 5 ) | ( (nInputBitsBuf & 128 ) >> 7 );
     138           0 :             nInputBitsBufSize=8;
     139             :         }
     140             :         else
     141             :         {
     142           0 :             nCode=(nCode<<nBits) | (nInputBitsBuf>>(nInputBitsBufSize-nBits));
     143           0 :             nInputBitsBufSize = nInputBitsBufSize - nBits;
     144           0 :             nInputBitsBuf&=0x00ff>>(8-nInputBitsBufSize);
     145           0 :             nBits=0;
     146             :         }
     147             :     } while (nBits>0);
     148             : 
     149           0 :     return nCode;
     150             : }
     151             : 
     152             : 
     153           0 : void LZWDecompressor::AddToTable(sal_uInt16 nPrevCode, sal_uInt16 nCodeFirstData)
     154             : {
     155           0 :     if (nTableSize >= MAX_TABLE_SIZE)
     156             :     {
     157             :         //It might be possible to force emit a 256 to flush the buffer and try
     158             :         //to continue later?
     159             :         SAL_WARN("filter.tiff", "Too much data at scanline");
     160           0 :         bEOIFound = sal_True;
     161           0 :         return;
     162             :     }
     163             : 
     164           0 :     while (pTable[nCodeFirstData].nDataCount>1)
     165           0 :         nCodeFirstData=pTable[nCodeFirstData].nPrevCode;
     166             : 
     167           0 :     pTable[nTableSize].nPrevCode=nPrevCode;
     168           0 :     pTable[nTableSize].nDataCount=pTable[nPrevCode].nDataCount+1;
     169           0 :     pTable[nTableSize].nData=pTable[nCodeFirstData].nData;
     170             : 
     171           0 :     nTableSize++;
     172             : }
     173             : 
     174             : 
     175           0 : void LZWDecompressor::DecompressSome()
     176             : {
     177             :     sal_uInt16 i,nCode;
     178             : 
     179           0 :     nCode=GetNextCode();
     180           0 :     if (nCode==256)
     181             :     {
     182           0 :         nTableSize=258;
     183           0 :         nCode=GetNextCode();
     184           0 :         if (nCode==257)
     185             :         {
     186           0 :             bEOIFound=sal_True;
     187             :         }
     188             :     }
     189           0 :     else if (nCode<nTableSize)
     190           0 :         AddToTable(nOldCode,nCode);
     191           0 :     else if (nCode==nTableSize)
     192           0 :         AddToTable(nOldCode,nOldCode);
     193             :     else
     194             :     {
     195           0 :         bEOIFound=sal_True;
     196             :     }
     197             : 
     198           0 :     if (bEOIFound)
     199           0 :         return;
     200             : 
     201           0 :     nOldCode=nCode;
     202             : 
     203           0 :     nOutBufDataLen=pTable[nCode].nDataCount;
     204           0 :     pOutBufData=pOutBuf+nOutBufDataLen;
     205           0 :     for (i=0; i<nOutBufDataLen; i++)
     206             :     {
     207           0 :         *(--pOutBufData)=pTable[nCode].nData;
     208           0 :         nCode=pTable[nCode].nPrevCode;
     209             :     }
     210             : }
     211             : 
     212             : 
     213             : 
     214             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10