Filter.cpp 3.92 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
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

Claudio Valerio's avatar
Claudio Valerio committed
30 31 32 33 34 35 36 37 38 39 40 41
#include "Filter.h"
#include "Object.h"
#include "Decoder.h"
#include "ASCIIHexDecode.h"
#include "ASCII85Decode.h"
#include "LZWDecode.h"
#include "FlateDecode.h"
#include "RunLengthDecode.h"
#include "CCITTFaxDecode.h"
#include "JBIG2Decode.h"
#include "DCTDecode.h"

42 43
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
using namespace merge_lib;
std::map<std::string, Decoder *> Filter::_allDecoders;

Filter::~Filter()
{
   std::map<std::string, Decoder *>::iterator it = _allDecoders.begin();
   for(; it != _allDecoders.end(); ++it)
   {
      delete (*it).second;
   }
   _allDecoders.clear();
}

//replace coded stream with decoded
void Filter::getDecodedStream(std::string & stream)
{
   std::vector <Decoder * >  decoders = _getDecoders();
   _objectWithStream->getStream(stream);
   for(size_t i = 0; i < decoders.size(); ++i)
   {
      decoders[i]->decode(stream);
   }
}

//parse object's content and fill out vector with
//necessary decoders
std::vector <Decoder * > Filter::_getDecoders()
{
   std::string streamHeader;
   static std::string whitespacesAndDelimeters(" \t\f\v\n\r<<>>]/");
   _objectWithStream->getHeader(streamHeader);
   unsigned int filterPosition = streamHeader.find("/Filter"); 
   std::vector <Decoder * > result;   
   unsigned int startOfDecoder = filterPosition + 1;
   while(1)
   {      
      startOfDecoder = streamHeader.find("/", startOfDecoder);
81
      if((int)startOfDecoder == -1)
Claudio Valerio's avatar
Claudio Valerio committed
82 83 84 85
         break;
      else
         ++startOfDecoder;
      unsigned int endOfDecoder = streamHeader.find_first_of(whitespacesAndDelimeters, startOfDecoder);
86
      if((int)endOfDecoder == -1)
Claudio Valerio's avatar
Claudio Valerio committed
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
         break;
      std::map<std::string, Decoder *>::iterator foundDecoder = 
         _allDecoders.find(streamHeader.substr(startOfDecoder, endOfDecoder - startOfDecoder));
      if(foundDecoder == _allDecoders.end())
         break;
      Decoder * decoder = foundDecoder->second;
      decoder->initialize(_objectWithStream);
      result.push_back(decoder);
   }
   return result;


}

void Filter::_createAllDecodersSet()
{

   if(!_allDecoders.empty())
      return;
   _allDecoders.insert(std::pair<std::string, Decoder *> (std::string("ASCIIHexDecode"), new ASCIIHexDecode()));
   _allDecoders.insert(std::pair<std::string, Decoder *> (std::string("ASCII85Decode"), new ASCII85Decode()));   
   _allDecoders.insert(std::pair<std::string, Decoder *> (std::string("LZWDecode"), new LZWDecode()));   
   _allDecoders.insert(std::pair<std::string, Decoder *> (std::string("FlateDecode"), new FlateDecode()));   
   _allDecoders.insert(std::pair<std::string, Decoder *> (std::string("RunLengthDecode"), new RunLengthDecode()));   
   _allDecoders.insert(std::pair<std::string, Decoder *> (std::string("CCITTFaxDecode"), new CCITTFaxDecode()));   
   _allDecoders.insert(std::pair<std::string, Decoder *> (std::string("JBIG2Decode"), new JBIG2Decode())); 
   _allDecoders.insert(std::pair<std::string, Decoder *> (std::string("DCTDecode"), new DCTDecode())); 
}