diff options
author | Johannes 'josch' Schauer <josch@debian.org> | 2018-07-20 07:21:40 +0200 |
---|---|---|
committer | Johannes 'josch' Schauer <josch@debian.org> | 2018-07-20 07:21:40 +0200 |
commit | aa564ac57de87724808ae3c2c6baf92688d181cc (patch) | |
tree | ffa2a656b8a0935220ae295a95971e0d97576daf /src/tests | |
parent | 20da8c12b9524ea6d405cf12df5a2f426b60b4c5 (diff) |
Import upstream version 0.3.0
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/__init__.py | 231 | ||||
-rw-r--r-- | src/tests/input/CMYK.tif | bin | 0 -> 22286 bytes | |||
-rw-r--r-- | src/tests/input/animation.gif | bin | 0 -> 1930 bytes | |||
-rw-r--r-- | src/tests/input/gray.png | bin | 0 -> 814 bytes | |||
-rw-r--r-- | src/tests/input/mono.tif | bin | 0 -> 262 bytes | |||
-rw-r--r-- | src/tests/input/normal.png | bin | 1130 -> 4992 bytes | |||
-rw-r--r-- | src/tests/output/CMYK.jpg.pdf | bin | 5560 -> 5558 bytes | |||
-rw-r--r-- | src/tests/output/CMYK.tif.pdf | bin | 1724 -> 1722 bytes | |||
-rw-r--r-- | src/tests/output/animation.gif.pdf | bin | 0 -> 6070 bytes | |||
-rw-r--r-- | src/tests/output/gray.png.pdf | bin | 0 -> 1329 bytes | |||
-rw-r--r-- | src/tests/output/mono.png.pdf | bin | 915 -> 958 bytes | |||
-rw-r--r-- | src/tests/output/mono.tif.pdf | bin | 0 -> 921 bytes | |||
-rw-r--r-- | src/tests/output/normal.jpg.pdf | bin | 3091 -> 3089 bytes | |||
-rw-r--r-- | src/tests/output/normal.png.pdf | bin | 1573 -> 1670 bytes |
14 files changed, 141 insertions, 90 deletions
diff --git a/src/tests/__init__.py b/src/tests/__init__.py index 506fc48..b1c1797 100644 --- a/src/tests/__init__.py +++ b/src/tests/__init__.py @@ -1,14 +1,23 @@ import unittest -import os import img2pdf +import os +import struct +import sys import zlib from PIL import Image -from io import BytesIO -import struct +from io import StringIO, BytesIO HERE = os.path.dirname(__file__) +PY3 = sys.version_info[0] >= 3 + +if PY3: + PdfReaderIO = StringIO +else: + PdfReaderIO = BytesIO + + # convert +set date:create +set date:modify -define png:exclude-chunk=time # we define some variables so that the table below can be narrower @@ -17,6 +26,7 @@ psp = (504, 972) # --pagesize portrait isl = (756, 324) # --imgsize landscape isp = (324, 756) # --imgsize portrait border = (162, 270) # --border +poster = (97200, 50400) # there is no need to have test cases with the same images with inverted # orientation (landscape/portrait) because --pagesize and --imgsize are # already inverted @@ -395,6 +405,8 @@ layout_test_cases = [ (972, 504), (864, 432)), (psl, isl, border, f_enlarge, 1, (972, 504), (756, 252), # 179 (972, 504), (864, 432)), + (poster, None, None, f_fill, 0, (97200, 50400), (151200, 50400), + (97200, 50400), (100800, 50400)), ] @@ -459,6 +471,10 @@ def test_suite(): files = os.listdir(os.path.join(HERE, "input")) for with_pdfrw, test_name in [(a, b) for a in [True, False] for b in files]: + # we do not test animation.gif with pdfrw because it doesn't support + # saving hexadecimal palette data + if test_name == 'animation.gif' and with_pdfrw: + continue inputf = os.path.join(HERE, "input", test_name) if not os.path.isfile(inputf): continue @@ -470,107 +486,142 @@ def test_suite(): orig_imgdata = inf.read() output = img2pdf.convert(orig_imgdata, nodate=True, with_pdfrw=with_pdfrw) - from io import StringIO, BytesIO from pdfrw import PdfReader, PdfName, PdfWriter from pdfrw.py23_diffs import convert_load, convert_store - x = PdfReader(StringIO(convert_load(output))) + x = PdfReader(PdfReaderIO(convert_load(output))) self.assertEqual(sorted(x.keys()), [PdfName.Info, PdfName.Root, PdfName.Size]) - self.assertEqual(x.Size, '7') + self.assertIn(x.Root.Pages.Count, ('1', '2')) + if len(x.Root.Pages.Kids) == '1': + self.assertEqual(x.Size, '7') + self.assertEqual(len(x.Root.Pages.Kids), 1) + elif len(x.Root.Pages.Kids) == '2': + self.assertEqual(x.Size, '10') + self.assertEqual(len(x.Root.Pages.Kids), 2) self.assertEqual(x.Info, {}) self.assertEqual(sorted(x.Root.keys()), [PdfName.Pages, PdfName.Type]) self.assertEqual(x.Root.Type, PdfName.Catalog) self.assertEqual(sorted(x.Root.Pages.keys()), [PdfName.Count, PdfName.Kids, PdfName.Type]) - self.assertEqual(x.Root.Pages.Count, '1') self.assertEqual(x.Root.Pages.Type, PdfName.Pages) - self.assertEqual(len(x.Root.Pages.Kids), 1) - self.assertEqual(sorted(x.Root.Pages.Kids[0].keys()), - [PdfName.Contents, PdfName.MediaBox, - PdfName.Parent, PdfName.Resources, PdfName.Type]) - self.assertEqual(x.Root.Pages.Kids[0].MediaBox, - ['0', '0', '115', '48']) - self.assertEqual(x.Root.Pages.Kids[0].Parent, x.Root.Pages) - self.assertEqual(x.Root.Pages.Kids[0].Type, PdfName.Page) - self.assertEqual(x.Root.Pages.Kids[0].Resources.keys(), - [PdfName.XObject]) - self.assertEqual(x.Root.Pages.Kids[0].Resources.XObject.keys(), - [PdfName.Im0]) - self.assertEqual(x.Root.Pages.Kids[0].Contents.keys(), - [PdfName.Length]) - self.assertEqual(x.Root.Pages.Kids[0].Contents.Length, - str(len(x.Root.Pages.Kids[0].Contents.stream))) - self.assertEqual(x.Root.Pages.Kids[0].Contents.stream, - "q\n115.0000 0 0 48.0000 0.0000 0.0000 cm\n/Im0 " - "Do\nQ") + orig_img = Image.open(f) + for pagenum in range(len(x.Root.Pages.Kids)): + # retrieve the original image frame that this page was + # generated from + orig_img.seek(pagenum) + cur_page = x.Root.Pages.Kids[pagenum] - imgprops = x.Root.Pages.Kids[0].Resources.XObject.Im0 + ndpi = orig_img.info.get("dpi", (96.0, 96.0)) + # In python3, the returned dpi value for some tiff images will + # not be an integer but a float. To make the behaviour of + # img2pdf the same between python2 and python3, we convert that + # float into an integer by rounding. + # Search online for the 72.009 dpi problem for more info. + ndpi = (int(round(ndpi[0])), int(round(ndpi[1]))) + imgwidthpx, imgheightpx = orig_img.size + pagewidth = 72.0*imgwidthpx/ndpi[0] + pageheight = 72.0*imgheightpx/ndpi[1] - # test if the filter is valid: - self.assertIn( - imgprops.Filter, [[PdfName.DCTDecode], [PdfName.JPXDecode], - [PdfName.FlateDecode], - [PdfName.CCITTFaxDecode]]) - # test if the colorspace is valid - self.assertIn( - imgprops.ColorSpace, [PdfName.DeviceGray, PdfName.DeviceRGB, - PdfName.DeviceCMYK]) - # test if the image has correct size - orig_img = Image.open(f) - self.assertEqual(imgprops.Width, str(orig_img.size[0])) - self.assertEqual(imgprops.Height, str(orig_img.size[1])) - # if the input file is a jpeg then it should've been copied - # verbatim into the PDF - if imgprops.Filter in [[PdfName.DCTDecode], [PdfName.JPXDecode]]: - self.assertEqual( - x.Root.Pages.Kids[0].Resources.XObject.Im0.stream, - convert_load(orig_imgdata)) - elif imgprops.Filter == [PdfName.CCITTFaxDecode]: - tiff_header = tiff_header_for_ccitt( - int(imgprops.Width), int(imgprops.Height), - int(imgprops.Length), 4) - imgio = BytesIO() - imgio.write(tiff_header) - imgio.write(convert_store( - x.Root.Pages.Kids[0].Resources.XObject.Im0.stream)) - imgio.seek(0) - im = Image.open(imgio) - self.assertEqual(im.tobytes(), orig_img.tobytes()) - try: - im.close() - except AttributeError: - pass + def format_float(f): + if int(f) == f: + return str(int(f)) + else: + return ("%.4f" % f).rstrip("0") + + self.assertEqual(sorted(cur_page.keys()), + [PdfName.Contents, PdfName.MediaBox, + PdfName.Parent, PdfName.Resources, + PdfName.Type]) + self.assertEqual(cur_page.MediaBox, + ['0', '0', format_float(pagewidth), + format_float(pageheight)]) + self.assertEqual(cur_page.Parent, x.Root.Pages) + self.assertEqual(cur_page.Type, PdfName.Page) + self.assertEqual(cur_page.Resources.keys(), + [PdfName.XObject]) + self.assertEqual(cur_page.Resources.XObject.keys(), + [PdfName.Im0]) + self.assertEqual(cur_page.Contents.keys(), + [PdfName.Length]) + self.assertEqual(cur_page.Contents.Length, + str(len(cur_page.Contents.stream))) + self.assertEqual(cur_page.Contents.stream, + "q\n%.4f 0 0 %.4f 0.0000 0.0000 cm\n" + "/Im0 Do\nQ" % (pagewidth, pageheight)) + + imgprops = cur_page.Resources.XObject.Im0 + + # test if the filter is valid: + self.assertIn( + imgprops.Filter, [PdfName.DCTDecode, PdfName.JPXDecode, + PdfName.FlateDecode, + [PdfName.CCITTFaxDecode]]) + + # test if the image has correct size + self.assertEqual(imgprops.Width, str(orig_img.size[0])) + self.assertEqual(imgprops.Height, str(orig_img.size[1])) + # if the input file is a jpeg then it should've been copied + # verbatim into the PDF + if imgprops.Filter in [PdfName.DCTDecode, + PdfName.JPXDecode]: + self.assertEqual( + cur_page.Resources.XObject.Im0.stream, + convert_load(orig_imgdata)) + elif imgprops.Filter == [PdfName.CCITTFaxDecode]: + tiff_header = tiff_header_for_ccitt( + int(imgprops.Width), int(imgprops.Height), + int(imgprops.Length), 4) + imgio = BytesIO() + imgio.write(tiff_header) + imgio.write(convert_store( + cur_page.Resources.XObject.Im0.stream)) + imgio.seek(0) + im = Image.open(imgio) + self.assertEqual(im.tobytes(), orig_img.tobytes()) + try: + im.close() + except AttributeError: + pass - elif imgprops.Filter == [PdfName.FlateDecode]: - # otherwise, the data is flate encoded and has to be equal to - # the pixel data of the input image - imgdata = zlib.decompress( - convert_store( - x.Root.Pages.Kids[0].Resources.XObject.Im0.stream)) - colorspace = imgprops.ColorSpace - if colorspace == PdfName.DeviceGray: - colorspace = 'L' - elif colorspace == PdfName.DeviceRGB: - colorspace = 'RGB' - elif colorspace == PdfName.DeviceCMYK: - colorspace = 'CMYK' - else: - raise Exception("invalid colorspace") - im = Image.frombytes(colorspace, (int(imgprops.Width), - int(imgprops.Height)), - imgdata) - if orig_img.mode == '1': - orig_img = orig_img.convert("L") - elif orig_img.mode not in ("RGB", "L", "CMYK", "CMYK;I"): - orig_img = orig_img.convert("RGB") - self.assertEqual(im.tobytes(), orig_img.tobytes()) - # the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not have - # the close() method - try: - im.close() - except AttributeError: - pass + elif imgprops.Filter == PdfName.FlateDecode: + # otherwise, the data is flate encoded and has to be equal + # to the pixel data of the input image + imgdata = zlib.decompress( + convert_store(cur_page.Resources.XObject.Im0.stream)) + if imgprops.DecodeParms: + if orig_img.format == 'PNG': + pngidat, palette = img2pdf.parse_png(orig_imgdata) + else: + pngbuffer = BytesIO() + orig_img.save(pngbuffer, format="png") + pngidat, palette = img2pdf.parse_png(pngbuffer.getvalue()) + self.assertEqual(zlib.decompress(pngidat), imgdata) + else: + colorspace = imgprops.ColorSpace + if colorspace == PdfName.DeviceGray: + colorspace = 'L' + elif colorspace == PdfName.DeviceRGB: + colorspace = 'RGB' + elif colorspace == PdfName.DeviceCMYK: + colorspace = 'CMYK' + else: + raise Exception("invalid colorspace") + im = Image.frombytes(colorspace, (int(imgprops.Width), + int(imgprops.Height)), + imgdata) + if orig_img.mode == '1': + self.assertEqual(im.tobytes(), + orig_img.convert("L").tobytes()) + elif orig_img.mode not in ("RGB", "L", "CMYK", "CMYK;I"): + self.assertEqual(im.tobytes(), + orig_img.convert("RGB").tobytes()) + # the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not + # have the close() method + try: + im.close() + except AttributeError: + pass # now use pdfrw to parse and then write out both pdfs and check the # result for equality y = PdfReader(out) diff --git a/src/tests/input/CMYK.tif b/src/tests/input/CMYK.tif Binary files differnew file mode 100644 index 0000000..8e3803e --- /dev/null +++ b/src/tests/input/CMYK.tif diff --git a/src/tests/input/animation.gif b/src/tests/input/animation.gif Binary files differnew file mode 100644 index 0000000..af4b278 --- /dev/null +++ b/src/tests/input/animation.gif diff --git a/src/tests/input/gray.png b/src/tests/input/gray.png Binary files differnew file mode 100644 index 0000000..48247fd --- /dev/null +++ b/src/tests/input/gray.png diff --git a/src/tests/input/mono.tif b/src/tests/input/mono.tif Binary files differnew file mode 100644 index 0000000..53e85bc --- /dev/null +++ b/src/tests/input/mono.tif diff --git a/src/tests/input/normal.png b/src/tests/input/normal.png Binary files differindex 87b9a6e..394f965 100644 --- a/src/tests/input/normal.png +++ b/src/tests/input/normal.png diff --git a/src/tests/output/CMYK.jpg.pdf b/src/tests/output/CMYK.jpg.pdf Binary files differindex bfe67f3..9efbe16 100644 --- a/src/tests/output/CMYK.jpg.pdf +++ b/src/tests/output/CMYK.jpg.pdf diff --git a/src/tests/output/CMYK.tif.pdf b/src/tests/output/CMYK.tif.pdf Binary files differindex b00586b..242bac7 100644 --- a/src/tests/output/CMYK.tif.pdf +++ b/src/tests/output/CMYK.tif.pdf diff --git a/src/tests/output/animation.gif.pdf b/src/tests/output/animation.gif.pdf Binary files differnew file mode 100644 index 0000000..fdfd460 --- /dev/null +++ b/src/tests/output/animation.gif.pdf diff --git a/src/tests/output/gray.png.pdf b/src/tests/output/gray.png.pdf Binary files differnew file mode 100644 index 0000000..3f2d4c3 --- /dev/null +++ b/src/tests/output/gray.png.pdf diff --git a/src/tests/output/mono.png.pdf b/src/tests/output/mono.png.pdf Binary files differindex eda3ec7..c773715 100644 --- a/src/tests/output/mono.png.pdf +++ b/src/tests/output/mono.png.pdf diff --git a/src/tests/output/mono.tif.pdf b/src/tests/output/mono.tif.pdf Binary files differnew file mode 100644 index 0000000..d23e65e --- /dev/null +++ b/src/tests/output/mono.tif.pdf diff --git a/src/tests/output/normal.jpg.pdf b/src/tests/output/normal.jpg.pdf Binary files differindex 87d2645..7acbe20 100644 --- a/src/tests/output/normal.jpg.pdf +++ b/src/tests/output/normal.jpg.pdf diff --git a/src/tests/output/normal.png.pdf b/src/tests/output/normal.png.pdf Binary files differindex 2628c5d..971475f 100644 --- a/src/tests/output/normal.png.pdf +++ b/src/tests/output/normal.png.pdf |