LZWDecode.cpp 5.17 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Craig Watson's avatar
Craig Watson committed
2 3
 * Copyright (C) 2015-2016 Département de l'Instruction Publique (DIP-SEM)
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
Claudio Valerio's avatar
Claudio Valerio committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
Claudio Valerio's avatar
Claudio Valerio committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
Claudio Valerio's avatar
Claudio Valerio committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
25
 */
26 27


Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

30
#include <iostream>
31
#include <QtGlobal>
32 33 34
#include "LZWDecode.h"
#include "FilterPredictor.h"

35 36
#include "core/memcheck.h"

37 38 39 40 41 42 43 44 45 46 47 48 49
// method performs decoding
using namespace merge_lib;

LZWDecode::LZWDecode():
      _predict(NULL),
      _dummy(""),
      _encoded(_dummy),
      _curSymbolIndex(0),
      _earlyChange(1),
      _readBuf(0),
      _readBits(0),
      _nextCode(0),
      _bitsToRead(0),
50 51 52
      _first(true),
      _curSequenceLength(0)

53 54 55 56 57 58 59 60 61 62 63
{
   clearTable();
}
LZWDecode::~LZWDecode()
{
   if( _predict ) 
   {
      delete _predict;
   }
}

Claudio Valerio's avatar
Claudio Valerio committed
64 65
bool LZWDecode::encode(std::string & decoded)
{
66
    Q_UNUSED(decoded);
Claudio Valerio's avatar
Claudio Valerio committed
67 68 69
    return true;
}

70 71 72 73 74 75 76
void LZWDecode::initialize(Object * objectWithStream)
{
   if( objectWithStream )
   {
      std::string head;
      objectWithStream->getHeader(head);

77
      if((int) head.find(FilterPredictor::DECODE_PARAM_TOKEN)  != -1 )
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
      {
         _predict = new FilterPredictor();
         _predict->initialize(objectWithStream);
         _earlyChange = _predict->getEarlyChange();
      }
      _readBits = 0;
      _readBuf = 0;
      clearTable();
   }
}

void LZWDecode::clearTable() 
{
   _nextCode = 258;
   _bitsToRead = 9;
   _curSequenceLength = 0;
   _first = true;
}

int LZWDecode::getCode() 
{
   int c = 0;
   int code = 0;

   while (_readBits < _bitsToRead) 
   {
      if( _curSymbolIndex < _encoded.size() )
      {
         c = _encoded[_curSymbolIndex++];
      }
      else
      {
         return EOF;
      }
      _readBuf = (_readBuf << 8) | (c & 0xff);
      _readBits += 8;
   }
   code = (_readBuf >> (_readBits - _bitsToRead)) & ((1 << _bitsToRead) - 1);
   _readBits -= _bitsToRead;
   return code;
}

// Method performs LZW decoding
bool LZWDecode::decode(std::string & encoded)
{
   _curSymbolIndex = 0;
   _encoded = encoded;

   // LZW decoding
   std::string decoded;
   struct DecodingTable
Claudio Valerio's avatar
Claudio Valerio committed
129
   {    
130 131 132 133 134 135 136
      int length;
      int head;
      unsigned tail;
   } decTable[4097];

   int prevCode = 0;     
   int newChar = 0;      
Claudio Valerio's avatar
Claudio Valerio committed
137
   unsigned curSequence[4097];    
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
   int nextLength = 0;

   clearTable();
   while(1)
   {
      int code = getCode();
      if( code == EOF || code == 257 )
      {
         // finish
         break;
      }
      if( code == 256 )
      {
         clearTable();
         continue;
      }
      if( _nextCode >= 4997 )
      {
         std::cout<<"Bad LZW stream - unexpected clearTable\n";
         clearTable();
         continue;
      }
      nextLength = _curSequenceLength + 1;
      if( code < 256 )
      {
         curSequence[ 0 ] = code;
         _curSequenceLength = 1;
      }
      else if( code < _nextCode )
      {
         //lets take sequence from table
         _curSequenceLength = decTable[code].length;
         int j = code;
         for( int i = _curSequenceLength - 1; i > 0; i--)
         {
            curSequence[ i ] = decTable[j].tail;
            j = decTable[ j ].head;
         }
         curSequence[0] = j;
      }
      else if( code == _nextCode )
      {
        curSequence[ _curSequenceLength ] = newChar;  
         ++_curSequenceLength;
      }
      else
      {
         std::cout<<"Bad LZW stream - unexpected code "<<code<<"\n";
         break;
      }
      newChar = curSequence[0];      
      if( _first ) 
      {
         _first = false;
      }
      else
      {
         // lets build decoding table
         decTable[ _nextCode ].length = nextLength;
         decTable[ _nextCode ].head = prevCode;
         decTable[ _nextCode ].tail = newChar;
         ++ _nextCode;
         // processing of PDF LZW parameter
         if (_nextCode + _earlyChange == 512)
         {
            _bitsToRead = 10;
         }
         else if (_nextCode + _earlyChange == 1024)
         {
            _bitsToRead = 11;
         }
         else if (_nextCode + _earlyChange == 2048)
         {
            _bitsToRead = 12;
         }
      }
      prevCode = code;
      // put current sequence to output stream
      for(int i = 0;i < _curSequenceLength;i++)
      {
         decoded += (char)curSequence[ i ];
      }
   }
   encoded = decoded;

   // if predictor exists for that object, then lets decode it
   if( _predict )
   {
      _predict->decode(encoded);
   }
Claudio Valerio's avatar
Claudio Valerio committed
228
   return true;    
229 230 231 232
}