Beispiel #1
0
def test_FlateDecode(predictor, s):
    """
    Tests FlateDecode decode() and encode() methods.
    """
    codec = FlateDecode()
    s = s.encode()
    encoded = codec.encode(s)
    assert codec.decode(encoded, {"/Predictor": predictor}) == s
Beispiel #2
0
def test_FlateDecode_unsupported_predictor():
    """
    Inputs an unsupported predictor (outside the [10, 15] range) checking
    that PdfReadError() is raised. Once this predictor support is updated
    in the future, this test case may be removed.
    """
    codec = FlateDecode()
    predictors = (-10, -1, 0, 9, 16, 20, 100)

    for predictor, s in cartesian_product(predictors, filter_inputs):
        s = s.encode()
        with pytest.raises(PdfReadError):
            codec.decode(codec.encode(s), {"/Predictor": predictor})
Beispiel #3
0
 def AddBitmap(self, data, width, height, filters):
     """
     Add wx.Bitmap from data, processed by filters.
     """
     if '/A85' in filters or '/ASCII85Decode' in filters:
         data = _AsciiBase85DecodePYTHON(data)
     if '/Fl' in filters or '/FlateDecode' in filters:
         data = FlateDecode.decode(data, None)
     if '/DCT' in filters or '/DCTDecode' in filters:
         stream = BytesIO(data)
         image = wx.Image(stream, wx.BITMAP_TYPE_JPEG)
         bitmap = wx.Bitmap(image)
     else:    
         bitmap = wx.BitmapFromBuffer(width, height, data)
     return ['DrawBitmap', (bitmap, 0, 0-height, width, height), {}]
Beispiel #4
0
 def AddBitmap(self, data, width, height, filters):
     """
     Add wx.Bitmap from data, processed by filters.
     """
     if "/A85" in filters or "/ASCII85Decode" in filters:
         data = _AsciiBase85DecodePYTHON(data)
     if "/Fl" in filters or "/FlateDecode" in filters:
         data = FlateDecode.decode(data, None)
     if "/DCT" in filters or "/DCTDecode" in filters:
         stream = cStringIO.StringIO(data)
         image = wx.Image(stream, wx.BITMAP_TYPE_JPEG)
         bitmap = wx.Bitmap(image)
     else:
         bitmap = wx.BitmapFromBuffer(width, height, data)
     return ["DrawBitmap", (bitmap, 0, 0 - height, width, height), {}]
Beispiel #5
0
 def AddBitmap(self, data, width, height, filters):
     """
     Add wx.Bitmap from data, processed by filters.
     """
     if '/A85' in filters or '/ASCII85Decode' in filters:
         data = _AsciiBase85DecodePYTHON(data)
     if '/Fl' in filters or '/FlateDecode' in filters:
         data = FlateDecode.decode(data, None)
     if '/DCT' in filters or '/DCTDecode' in filters:
         stream = BytesIO(data)
         image = wx.Image(stream, wx.BITMAP_TYPE_JPEG)
         bitmap = wx.Bitmap(image)
     else:    
         bitmap = wx.BitmapFromBuffer(width, height, data)
     return ['DrawBitmap', (bitmap, 0, 0-height, width, height), {}]
Beispiel #6
0
 def __init__(self, im):
   super().__init__()
   try:
     depth, colorspace = MODE_TO_COLORSPACE[im.mode]
   except KeyError:
     raise NotImplementedError('image mode %r not supported' % im.mode)
   w, h = im.size
   # always compress raw image data
   self._data = FlateDecode.encode(im.tobytes())
   self[NameObject("/Filter")] = NameObject('/FlateDecode')
   self[NameObject('/Type')] = NameObject('/XObject')
   self[NameObject('/Subtype')] = NameObject('/Image')
   self[NameObject('/Width')] = NumberObject(w)
   self[NameObject('/Height')] = NumberObject(h)
   self[NameObject('/BitsPerComponent')] = NumberObject(depth)
   self[NameObject('/ColorSpace')] = NameObject(colorspace)
Beispiel #7
0
 def __init__(self, im):
     super().__init__()
     try:
         depth, colorspace = MODE_TO_COLORSPACE[im.mode]
     except KeyError:
         raise NotImplementedError('image mode %r not supported' % im.mode)
     w, h = im.size
     # always compress raw image data
     self._data = FlateDecode.encode(im.tobytes())
     self[NameObject("/Filter")] = NameObject('/FlateDecode')
     self[NameObject('/Type')] = NameObject('/XObject')
     self[NameObject('/Subtype')] = NameObject('/Image')
     self[NameObject('/Width')] = NumberObject(w)
     self[NameObject('/Height')] = NumberObject(h)
     self[NameObject('/BitsPerComponent')] = NumberObject(depth)
     self[NameObject('/ColorSpace')] = NameObject(colorspace)
 def AddBitmap(self, data, width, height, filters):
     "Add wx.Bitmap from data, processed by filters"
     if '/A85' in filters or '/ASCII85Decode' in filters:
         data = _AsciiBase85DecodePYTHON(data)
     if '/Fl' in filters or '/FlateDecode' in filters:
         data = FlateDecode.decode(data, None)
     if '/CCF' in filters or '/CCITTFaxDecode' in filters:
         if VERBOSE:
             print 'PDF operation /CCITTFaxDecode is not implemented'
         return []
     if '/DCT' in filters or '/DCTDecode' in filters:
         stream = cStringIO.StringIO(data)
         image = wx.ImageFromStream(stream, wx.BITMAP_TYPE_JPEG)
         bitmap = wx.BitmapFromImage(image)
     else:
         bitmap = wx.BitmapFromBuffer(width, height, data)
     return ['DrawBitmap', (bitmap, 0, 0 - height, width, height), {}]
Beispiel #9
0
 def AddBitmap(self, data, width, height, filters):
     "Add wx.Bitmap from data, processed by filters"
     if '/A85' in filters or '/ASCII85Decode' in filters:
         data = _AsciiBase85DecodePYTHON(data)
     if '/Fl' in filters or '/FlateDecode' in filters:
         data = FlateDecode.decode(data, None)
     if '/CCF' in filters or  '/CCITTFaxDecode' in filters: 
         if VERBOSE:
             print 'PDF operation /CCITTFaxDecode is not implemented'
         return []
     if '/DCT' in filters or '/DCTDecode' in filters:
         stream = cStringIO.StringIO(data)
         image = wx.ImageFromStream(stream, wx.BITMAP_TYPE_JPEG)
         bitmap = wx.BitmapFromImage(image)
     else:    
         bitmap = wx.BitmapFromBuffer(width, height, data)
     return ['DrawBitmap', (bitmap, 0, 0-height, width, height), {}]
Beispiel #10
0
 def AddBitmap(self, data, width, height, filters):
     """
     Add wx.Bitmap from data, processed by filters.
     """
     if '/A85' in filters or '/ASCII85Decode' in filters:
         data = ASCII85Decode.decode(data)
     if '/Fl' in filters or '/FlateDecode' in filters:
         data = FlateDecode.decode(data, None)
     if '/CCF' in filters or  '/CCITTFaxDecode' in filters:
         if VERBOSE:
             print('PDF operation /CCITTFaxDecode is not implemented')
         return []
     if '/DCT' in filters or '/DCTDecode' in filters:
         stream = BytesIO(data)
         image = wx.Image(stream, wx.BITMAP_TYPE_JPEG)
         bitmap = wx.Bitmap(image)
     else:
         try:
             bitmap = wx.Bitmap.FromBuffer(width, height, data)
         except:
             return []       # any error
     return ['DrawBitmap', (bitmap, 0, 0-height, width, height), {}]
Beispiel #11
0
 def AddBitmap(self, data, width, height, filters):
     """
     Add wx.Bitmap from data, processed by filters.
     """
     if '/A85' in filters or '/ASCII85Decode' in filters:
         data = ASCII85Decode.decode(data)
     if '/Fl' in filters or '/FlateDecode' in filters:
         data = FlateDecode.decode(data, None)
     if '/CCF' in filters or  '/CCITTFaxDecode' in filters:
         if VERBOSE:
             print('PDF operation /CCITTFaxDecode is not implemented')
         return []
     if '/DCT' in filters or '/DCTDecode' in filters:
         stream = BytesIO(data)
         image = wx.Image(stream, wx.BITMAP_TYPE_JPEG)
         bitmap = wx.Bitmap(image)
     else:
         try:
             bitmap = wx.Bitmap.FromBuffer(width, height, data)
         except:
             return []       # any error
     return ['DrawBitmap', (bitmap, 0, 0-height, width, height), {}]
if '/XObject' in page['/Resources']:  # <- 這裡的if不會有東西輸出, 所以沒有img7,8那種東西
    xObject = page['/Resources']['/XObject'].getObject()

    for obj in xObject:
        if xObject[obj]['/Subtype'] == '/Image':
            size = (xObject[obj]['/Width'], xObject[obj]['/Height'])
            data = xObject[obj]._data
            if xObject[obj]['/ColorSpace'] == '/DeviceRGB':
                mode = "RGB"
            else:
                mode = "P"

            if '/Filter' in xObject[obj]:
                if xObject[obj]['/Filter'] == '/FlateDecode':
                    data = FlateDecode.decode(data,
                                              xObject[obj].get('/DecodeParms'))
                    img = Image.frombytes(mode, size, data.encode())
                    img.save(obj[1:] + ".png")
                elif xObject[obj]['/Filter'] == '/DCTDecode':
                    img = open(obj[1:] + ".jpg", "wb")
                    img.write(data)
                    img.close()
                elif xObject[obj]['/Filter'] == '/JPXDecode':
                    img = open(obj[1:] + ".jp2", "wb")
                    img.write(data)
                    img.close()
                elif xObject[obj]['/Filter'] == '/CCITTFaxDecode':
                    img = open(obj[1:] + ".tiff", "wb")
                    img.write(data)
                    img.close()
            else:
Beispiel #13
0
def test_FlateDecode_decompress_array_params(params):
    codec = FlateDecode()
    s = ""
    s = s.encode()
    encoded = codec.encode(s)
    assert codec.decode(encoded, params) == s