Beispiel #1
0
 def decode(self):
     """ algorithm derived from:
     http://www.rasip.fer.hr/research/compress/algorithms/fund/lz/lzw.html
     and the PDFReference
     """
     cW = self.CLEARDICT
     baos = ""
     while True:
         pW = cW
         cW = self.nextCode()
         if cW == -1:
             raise PdfReadError("Missed the stop code in LZWDecode!")
         if cW == self.STOP:
             break
         elif cW == self.CLEARDICT:
             self.resetDict()
         elif pW == self.CLEARDICT:
             baos += self.dict[cW]
         else:
             if cW < self.dictlen:
                 baos += self.dict[cW]
                 p = self.dict[pW] + self.dict[cW][0]
                 self.dict[self.dictlen] = p
                 self.dictlen += 1
             else:
                 p = self.dict[pW] + self.dict[pW][0]
                 baos += p
                 self.dict[self.dictlen] = p
                 self.dictlen += 1
             if (self.dictlen >= (1 << self.bitspercode) - 1
                     and self.bitspercode < 12):
                 self.bitspercode += 1
     return baos
Beispiel #2
0
    def decode(data, decodeParms):
        data = decompress(data)
        predictor = 1
        if decodeParms:
            try:
                predictor = decodeParms.get("/Predictor", 1)
            except AttributeError:
                pass  # usually an array with a null object was read

        # predictor 1 == no predictor
        if predictor != 1:
            columns = decodeParms["/Columns"]
            # PNG prediction:
            if predictor >= 10 and predictor <= 15:
                output = StringIO()
                # PNG prediction can vary from row to row
                rowlength = columns + 1
                assert len(data) % rowlength == 0
                prev_rowdata = (0, ) * rowlength
                for row in xrange(len(data) / rowlength):
                    rowdata = [
                        ord(x) for x in data[(row * rowlength):((row + 1) *
                                                                rowlength)]
                    ]
                    filterByte = rowdata[0]
                    if filterByte == 0:
                        pass
                    elif filterByte == 1:
                        for i in range(2, rowlength):
                            rowdata[i] = (rowdata[i] + rowdata[i - 1]) % 256
                    elif filterByte == 2:
                        for i in range(1, rowlength):
                            rowdata[i] = (rowdata[i] + prev_rowdata[i]) % 256
                    else:
                        # unsupported PNG filter
                        raise PdfReadError("Unsupported PNG filter %r" %
                                           filterByte)
                    prev_rowdata = rowdata
                    output.write(''.join([chr(x) for x in rowdata[1:]]))
                data = output.getvalue()
            else:
                # unsupported predictor
                raise PdfReadError("Unsupported flatedecode predictor %r" %
                                   predictor)
        return data