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

Generated by: LCOV version 1.10