def test_text_and_phys(self):
        img = np.arange(15).reshape(3, 5).astype(np.uint8)
        text_list = [('Monster', 'Godzilla'), ('Creation Time', None)]
        phys = (5, 4, 0)

        f = io.BytesIO()
        numpngw.write_png(f, img, filter_type=0, text_list=text_list,
                          phys=phys)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=img.shape[1],
                                   height=img.shape[0],
                                   bit_depth=8, color_type=0,
                                   interlace=0)

        file_contents = check_text(file_contents, b"Monster", b"Godzilla")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_phys(file_contents, phys)

        file_contents = check_idat(file_contents, color_type=0,
                                   bit_depth=8, interlace=0,
                                   img=img)

        check_iend(file_contents)
    def test_basic(self):
        w = 22
        h = 10
        bitdepth = 8
        np.random.seed(123)
        img = np.random.randint(0, 256, size=(h, w)).astype(np.uint8)

        f = io.BytesIO()
        numpngw.write_png(f, img, filter_type=1)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=img.shape[1],
                                   height=img.shape[0],
                                   bit_depth=bitdepth, color_type=0,
                                   interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_idat(file_contents, color_type=0,
                                   bit_depth=bitdepth, interlace=0,
                                   img=img)

        check_iend(file_contents)
    def test_write_png_bkgd(self):
        # Test creation of RGB images (color type 2), with a background color.
        w = 16
        h = 8
        np.random.seed(123)
        for bit_depth in [8, 16]:
            maxval = 2**bit_depth
            bg = (maxval - 1, maxval - 2, maxval - 3)
            dt = np.uint16 if bit_depth == 16 else np.uint8
            img = np.random.randint(0, maxval, size=(h, w, 3)).astype(dt)

            f = io.BytesIO()
            numpngw.write_png(f, img, background=bg, filter_type=0)

            file_contents = f.getvalue()

            file_contents = check_signature(file_contents)

            file_contents = check_ihdr(file_contents, width=w, height=h,
                                       bit_depth=bit_depth, color_type=2,
                                       interlace=0)

            file_contents = check_text(file_contents, b"Creation Time")
            software = numpngw._software_text().encode('latin-1')
            file_contents = check_text(file_contents, b"Software",
                                       software)

            file_contents = check_bkgd(file_contents, color=bg, color_type=2)

            file_contents = check_idat(file_contents, color_type=2,
                                       bit_depth=bit_depth, interlace=0,
                                       img=img)

            check_iend(file_contents)
Example #4
0
    def test_write_png_bkgd(self):
        # Test creation of RGB images (color type 2), with a background color.
        w = 16
        h = 8
        np.random.seed(123)
        for bit_depth in [8, 16]:
            maxval = 2**bit_depth
            bg = (maxval - 1, maxval - 2, maxval - 3)
            dt = np.uint16 if bit_depth == 16 else np.uint8
            img = np.random.randint(0, maxval, size=(h, w, 3)).astype(dt)

            f = io.BytesIO()
            numpngw.write_png(f, img, background=bg, filter_type=0)

            file_contents = f.getvalue()

            file_contents = check_signature(file_contents)

            file_contents = check_ihdr(file_contents, width=w, height=h,
                                       bit_depth=bit_depth, color_type=2,
                                       interlace=0)

            file_contents = check_text(file_contents, b"Creation Time")
            software = numpngw._software_text().encode('latin-1')
            file_contents = check_text(file_contents, b"Software",
                                       software)

            file_contents = check_bkgd(file_contents, color=bg, color_type=2)

            file_contents = check_idat(file_contents, color_type=2,
                                       bit_depth=bit_depth, interlace=0,
                                       img=img)

            check_iend(file_contents)
    def test_write_png_timestamp_gamma(self):
        np.random.seed(123)
        img = np.random.randint(0, 256, size=(10, 10)).astype(np.uint8)
        f = io.BytesIO()
        timestamp = (1452, 4, 15, 8, 9, 10)
        gamma = 2.2
        numpngw.write_png(f, img, timestamp=timestamp, gamma=gamma)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=img.shape[1], height=img.shape[0],
                                   bit_depth=8, color_type=0, interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_time(file_contents, timestamp)

        file_contents = check_gama(file_contents, gamma)

        file_contents = check_idat(file_contents, color_type=0, bit_depth=8,
                                   interlace=0, img=img)

        check_iend(file_contents)
Example #6
0
    def test_text_and_phys(self):
        img = np.arange(15).reshape(3, 5).astype(np.uint8)
        text_list = [('Monster', 'Godzilla'), ('Creation Time', None)]
        phys = (5, 4, 0)

        f = io.BytesIO()
        numpngw.write_png(f, img, filter_type=0, text_list=text_list,
                          phys=phys)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=img.shape[1],
                                   height=img.shape[0],
                                   bit_depth=8, color_type=0,
                                   interlace=0)

        file_contents = check_text(file_contents, b"Monster", b"Godzilla")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_phys(file_contents, phys)

        file_contents = check_idat(file_contents, color_type=0,
                                   bit_depth=8, interlace=0,
                                   img=img)

        check_iend(file_contents)
Example #7
0
    def test_write_png_timestamp_gamma(self):
        np.random.seed(123)
        img = np.random.randint(0, 256, size=(10, 10)).astype(np.uint8)
        f = io.BytesIO()
        timestamp = (1452, 4, 15, 8, 9, 10)
        gamma = 2.2
        numpngw.write_png(f, img, timestamp=timestamp, gamma=gamma)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=img.shape[1],
                                   height=img.shape[0],
                                   bit_depth=8,
                                   color_type=0,
                                   interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_time(file_contents, timestamp)

        file_contents = check_gama(file_contents, gamma)

        file_contents = check_idat(file_contents,
                                   color_type=0,
                                   bit_depth=8,
                                   interlace=0,
                                   img=img)

        check_iend(file_contents)
Example #8
0
    def test_basic(self):
        w = 22
        h = 10
        bitdepth = 8
        np.random.seed(123)
        img = np.random.randint(0, 256, size=(h, w)).astype(np.uint8)

        f = io.BytesIO()
        numpngw.write_png(f, img, filter_type=1)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=img.shape[1],
                                   height=img.shape[0],
                                   bit_depth=bitdepth, color_type=0,
                                   interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_idat(file_contents, color_type=0,
                                   bit_depth=bitdepth, interlace=0,
                                   img=img)

        check_iend(file_contents)
    def test_write_png_bkgd_palette(self):
        # Test creation of RGB images with a background color
        # when use_palette is True.
        w = 6
        h = 8
        np.random.seed(123)
        for bg_in_img in [True, False]:
            bit_depth = 8
            maxval = 2**bit_depth
            bg = (maxval - 1, maxval - 3, maxval - 2)

            img = np.arange(1, w*h*3 + 1, dtype=np.uint8).reshape(h, w, 3)
            if bg_in_img:
                img[-1, -1] = bg

            f = io.BytesIO()
            numpngw.write_png(f, img, background=bg, use_palette=True)

            file_contents = f.getvalue()

            file_contents = check_signature(file_contents)

            file_contents = check_ihdr(file_contents, width=w, height=h,
                                       bit_depth=bit_depth, color_type=3,
                                       interlace=0)

            file_contents = check_text(file_contents, b"Creation Time")
            software = numpngw._software_text().encode('latin-1')
            file_contents = check_text(file_contents, b"Software",
                                       software)

            # Check the PLTE chunk.
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            self.assertEqual(chunk_type, b"PLTE")
            plte = np.fromstring(chunk_data, dtype=np.uint8).reshape(-1, 3)
            expected_palette = np.arange(1, w*h*3+1,
                                         dtype=np.uint8).reshape(-1, 3)
            if bg_in_img:
                expected_palette[-1] = bg
            else:
                expected_palette = np.append(expected_palette,
                                             np.array([bg], dtype=np.uint8),
                                             axis=0)
            assert_array_equal(plte, expected_palette,
                               "unexpected palette %r %r" %
                               (plte[-2], expected_palette[-2]))

            file_contents = check_bkgd(file_contents, color=bg, color_type=3,
                                       palette=expected_palette)

            file_contents = check_idat(file_contents, color_type=3,
                                       bit_depth=bit_depth, interlace=0,
                                       img=img, palette=plte)

            check_iend(file_contents)
Example #10
0
    def test_write_png_bkgd_palette(self):
        # Test creation of RGB images with a background color
        # when use_palette is True.
        w = 6
        h = 8
        np.random.seed(123)
        for bg_in_img in [True, False]:
            bit_depth = 8
            maxval = 2**bit_depth
            bg = (maxval - 1, maxval - 3, maxval - 2)

            img = np.arange(1, w*h*3 + 1, dtype=np.uint8).reshape(h, w, 3)
            if bg_in_img:
                img[-1, -1] = bg

            f = io.BytesIO()
            numpngw.write_png(f, img, background=bg, use_palette=True)

            file_contents = f.getvalue()

            file_contents = check_signature(file_contents)

            file_contents = check_ihdr(file_contents, width=w, height=h,
                                       bit_depth=bit_depth, color_type=3,
                                       interlace=0)

            file_contents = check_text(file_contents, b"Creation Time")
            software = numpngw._software_text().encode('latin-1')
            file_contents = check_text(file_contents, b"Software",
                                       software)

            # Check the PLTE chunk.
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            self.assertEqual(chunk_type, b"PLTE")
            plte = np.frombuffer(chunk_data, dtype=np.uint8).reshape(-1, 3)
            expected_palette = np.arange(1, w*h*3+1,
                                         dtype=np.uint8).reshape(-1, 3)
            if bg_in_img:
                expected_palette[-1] = bg
            else:
                expected_palette = np.append(expected_palette,
                                             np.array([bg], dtype=np.uint8),
                                             axis=0)
            assert_array_equal(plte, expected_palette,
                               "unexpected palette %r %r" %
                               (plte[-2], expected_palette[-2]))

            file_contents = check_bkgd(file_contents, color=bg, color_type=3,
                                       palette=expected_palette)

            file_contents = check_idat(file_contents, color_type=3,
                                       bit_depth=bit_depth, interlace=0,
                                       img=img, palette=plte)

            check_iend(file_contents)
Example #11
0
    def test_write_apng_8bit_RGBA(self):
        num_frames = 4
        w = 25
        h = 15
        np.random.seed(12345)
        seq_size = (num_frames, h, w, 4)
        seq = np.random.randint(0, 256, size=seq_size).astype(np.uint8)
        f = io.BytesIO()
        numpngw.write_apng(f, seq)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents, width=w, height=h,
                                   bit_depth=8, color_type=6, interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_actl(file_contents, num_frames=num_frames,
                                   num_plays=0)

        sequence_number = 0
        file_contents = check_fctl(file_contents,
                                   sequence_number=sequence_number,
                                   width=w, height=h)
        sequence_number += 1

        file_contents = check_idat(file_contents, color_type=6, bit_depth=8,
                                   interlace=0, img=seq[0])

        for k in range(1, num_frames):
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            # Check the fdAT chunk.
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            self.assertEqual(chunk_type, b"fdAT")
            actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
            self.assertEqual(actual_seq_num, sequence_number)
            sequence_number += 1
            decompressed = zlib.decompress(chunk_data[4:])
            b = np.frombuffer(decompressed, dtype=np.uint8)
            lines = b.reshape(h, 4*w+1)
            expected_col0 = np.zeros(h, dtype=np.uint8)
            assert_array_equal(lines[:, 0], expected_col0)
            img2 = lines[:, 1:].reshape(h, w, 4)
            assert_array_equal(img2, seq[k])

        check_iend(file_contents)
Example #12
0
    def test_write_apng_8bit_RGBA(self):
        num_frames = 4
        w = 25
        h = 15
        np.random.seed(12345)
        seq_size = (num_frames, h, w, 4)
        seq = np.random.randint(0, 256, size=seq_size).astype(np.uint8)
        f = io.BytesIO()
        numpngw.write_apng(f, seq)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents, width=w, height=h,
                                   bit_depth=8, color_type=6, interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_actl(file_contents, num_frames=num_frames,
                                   num_plays=0)

        sequence_number = 0
        file_contents = check_fctl(file_contents,
                                   sequence_number=sequence_number,
                                   width=w, height=h)
        sequence_number += 1

        file_contents = check_idat(file_contents, color_type=6, bit_depth=8,
                                   interlace=0, img=seq[0])

        for k in range(1, num_frames):
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            # Check the fdAT chunk.
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            self.assertEqual(chunk_type, b"fdAT")
            actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
            self.assertEqual(actual_seq_num, sequence_number)
            sequence_number += 1
            decompressed = zlib.decompress(chunk_data[4:])
            b = np.fromstring(decompressed, dtype=np.uint8)
            lines = b.reshape(h, 4*w+1)
            expected_col0 = np.zeros(h, dtype=np.uint8)
            assert_array_equal(lines[:, 0], expected_col0)
            img2 = lines[:, 1:].reshape(h, w, 4)
            assert_array_equal(img2, seq[k])

        check_iend(file_contents)
Example #13
0
    def test_write_png_RGB(self):
        # Test creation of RGB images (color type 2), with and without
        # a `transparent` color selected, and with bit depth 8 and 16.
        w = 24
        h = 10
        np.random.seed(12345)
        for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
            for transparent in [None, (0, 0, 0)]:
                for bit_depth in [8, 16]:
                    for interlace in [0, 1]:
                        dt = np.uint16 if bit_depth == 16 else np.uint8
                        maxval = 2**bit_depth
                        img = np.random.randint(0, maxval,
                                                size=(h, w, 3)).astype(dt)
                        if transparent:
                            img[2:4, 2:4] = transparent

                        f = io.BytesIO()
                        numpngw.write_png(f,
                                          img,
                                          transparent=transparent,
                                          filter_type=filter_type,
                                          interlace=interlace)

                        file_contents = f.getvalue()

                        file_contents = check_signature(file_contents)

                        file_contents = check_ihdr(file_contents,
                                                   width=w,
                                                   height=h,
                                                   bit_depth=bit_depth,
                                                   color_type=2,
                                                   interlace=interlace)

                        file_contents = check_text(file_contents,
                                                   b"Creation Time")
                        software = numpngw._software_text().encode('latin-1')
                        file_contents = check_text(file_contents, b"Software",
                                                   software)

                        if transparent:
                            file_contents = check_trns(file_contents,
                                                       color_type=2,
                                                       transparent=transparent)

                        file_contents = check_idat(file_contents,
                                                   color_type=2,
                                                   bit_depth=bit_depth,
                                                   interlace=interlace,
                                                   img=img)

                        check_iend(file_contents)
Example #14
0
    def test_write_png_nbit_grayscale(self):
        # Test the creation of grayscale images for bit depths of 1, 2, 4
        # 8 and 16, with or without a `transparent` color selected.
        np.random.seed(123)
        for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
            for bitdepth in [1, 2, 4, 8, 16]:
                for transparent in [None, 0]:
                    for interlace in [0, 1]:
                        dt = np.uint16 if bitdepth == 16 else np.uint8
                        maxval = 2**bitdepth
                        sz = (3, 11)
                        img = np.random.randint(0, maxval, size=sz).astype(dt)
                        if transparent is not None:
                            img[2:4, 2:] = transparent

                        f = io.BytesIO()
                        numpngw.write_png(f,
                                          img,
                                          bitdepth=bitdepth,
                                          transparent=transparent,
                                          filter_type=filter_type,
                                          interlace=interlace)

                        file_contents = f.getvalue()

                        file_contents = check_signature(file_contents)

                        file_contents = check_ihdr(file_contents,
                                                   width=img.shape[1],
                                                   height=img.shape[0],
                                                   bit_depth=bitdepth,
                                                   color_type=0,
                                                   interlace=interlace)

                        file_contents = check_text(file_contents,
                                                   b"Creation Time")
                        software = numpngw._software_text().encode('latin-1')
                        file_contents = check_text(file_contents, b"Software",
                                                   software)

                        if transparent is not None:
                            file_contents = check_trns(file_contents,
                                                       color_type=0,
                                                       transparent=transparent)

                        file_contents = check_idat(file_contents,
                                                   color_type=0,
                                                   bit_depth=bitdepth,
                                                   interlace=interlace,
                                                   img=img)

                        check_iend(file_contents)
Example #15
0
    def test_write_png_nbit_grayscale(self):
        # Test the creation of grayscale images for bit depths of 1, 2, 4
        # 8 and 16, with or without a `transparent` color selected.
        np.random.seed(123)
        for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
            for bitdepth in [1, 2, 4, 8, 16]:
                for transparent in [None, 0]:
                    for interlace in [0, 1]:
                        dt = np.uint16 if bitdepth == 16 else np.uint8
                        maxval = 2**bitdepth
                        sz = (3, 11)
                        img = np.random.randint(0, maxval, size=sz).astype(dt)
                        if transparent is not None:
                            img[2:4, 2:] = transparent

                        f = io.BytesIO()
                        numpngw.write_png(f, img, bitdepth=bitdepth,
                                          transparent=transparent,
                                          filter_type=filter_type,
                                          interlace=interlace)

                        file_contents = f.getvalue()

                        file_contents = check_signature(file_contents)

                        file_contents = check_ihdr(file_contents,
                                                   width=img.shape[1],
                                                   height=img.shape[0],
                                                   bit_depth=bitdepth,
                                                   color_type=0,
                                                   interlace=interlace)

                        file_contents = check_text(file_contents,
                                                   b"Creation Time")
                        software = numpngw._software_text().encode('latin-1')
                        file_contents = check_text(file_contents, b"Software",
                                                   software)

                        if transparent is not None:
                            file_contents = check_trns(file_contents,
                                                       color_type=0,
                                                       transparent=transparent)

                        file_contents = check_idat(file_contents, color_type=0,
                                                   bit_depth=bitdepth,
                                                   interlace=interlace,
                                                   img=img)

                        check_iend(file_contents)
Example #16
0
    def test_write_png_RGB(self):
        # Test creation of RGB images (color type 2), with and without
        # a `transparent` color selected, and with bit depth 8 and 16.
        w = 24
        h = 10
        np.random.seed(12345)
        for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
            for transparent in [None, (0, 0, 0)]:
                for bit_depth in [8, 16]:
                    for interlace in [0, 1]:
                        dt = np.uint16 if bit_depth == 16 else np.uint8
                        maxval = 2**bit_depth
                        img = np.random.randint(0, maxval,
                                                size=(h, w, 3)).astype(dt)
                        if transparent:
                            img[2:4, 2:4] = transparent

                        f = io.BytesIO()
                        numpngw.write_png(f, img, transparent=transparent,
                                          filter_type=filter_type,
                                          interlace=interlace)

                        file_contents = f.getvalue()

                        file_contents = check_signature(file_contents)

                        file_contents = check_ihdr(file_contents,
                                                   width=w, height=h,
                                                   bit_depth=bit_depth,
                                                   color_type=2,
                                                   interlace=interlace)

                        file_contents = check_text(file_contents,
                                                   b"Creation Time")
                        software = numpngw._software_text().encode('latin-1')
                        file_contents = check_text(file_contents, b"Software",
                                                   software)

                        if transparent:
                            file_contents = check_trns(file_contents,
                                                       color_type=2,
                                                       transparent=transparent)

                        file_contents = check_idat(file_contents, color_type=2,
                                                   bit_depth=bit_depth,
                                                   interlace=interlace,
                                                   img=img)

                        check_iend(file_contents)
Example #17
0
    def test_write_png_with_alpha(self):
        # Test creation of grayscale+alpha and RGBA images (color types 4
        # and 6, resp.), with bit depths 8 and 16.
        w = 25
        h = 15
        np.random.seed(12345)
        for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
            for color_type in [4, 6]:
                num_channels = 2 if color_type == 4 else 4
                for bit_depth in [8, 16]:
                    for interlace in [0, 1]:
                        dt = np.uint8 if bit_depth == 8 else np.uint16
                        sz = (h, w, num_channels)
                        img = np.random.randint(0, 2**bit_depth,
                                                size=sz).astype(dt)
                        f = io.BytesIO()
                        numpngw.write_png(f,
                                          img,
                                          filter_type=filter_type,
                                          interlace=interlace)

                        file_contents = f.getvalue()

                        file_contents = check_signature(file_contents)

                        file_contents = check_ihdr(file_contents,
                                                   width=w,
                                                   height=h,
                                                   bit_depth=bit_depth,
                                                   color_type=color_type,
                                                   interlace=interlace)

                        file_contents = check_text(file_contents,
                                                   b"Creation Time")
                        software = numpngw._software_text().encode('latin-1')
                        file_contents = check_text(file_contents, b"Software",
                                                   software)

                        file_contents = check_idat(file_contents,
                                                   color_type=color_type,
                                                   bit_depth=bit_depth,
                                                   interlace=interlace,
                                                   img=img)

                        check_iend(file_contents)
Example #18
0
    def test_write_png_sbit(self):
        # Test creation of sBIT chunks for color_type 0 and 2.
        w = 7
        h = 5
        np.random.seed(123)
        for bit_depth in [8, 16]:
            for size in [(h, w), (h, w, 3)]:
                maxval = 2**bit_depth
                dt = np.uint16 if bit_depth == 16 else np.uint8
                img = np.random.randint(0, maxval, size=size).astype(dt)

                color_type = 0 if len(size) == 2 else 2

                sbit = (bit_depth - 1,)
                if color_type == 2:
                    sbit = sbit * 3

                f = io.BytesIO()
                numpngw.write_png(f, img, sbit=sbit)

                file_contents = f.getvalue()

                file_contents = check_signature(file_contents)


                file_contents = check_ihdr(file_contents, width=w, height=h,
                                           bit_depth=bit_depth,
                                           color_type=color_type,
                                           interlace=0)

                file_contents = check_text(file_contents, b"Creation Time")
                software = numpngw._software_text().encode('latin-1')
                file_contents = check_text(file_contents, b"Software",
                                           software)

                file_contents = check_sbit(file_contents, sbit=sbit,
                                           color_type=color_type)

                file_contents = check_idat(file_contents,
                                           color_type=color_type,
                                           bit_depth=bit_depth,
                                           interlace=0,
                                           img=img)

                check_iend(file_contents)
Example #19
0
    def test_write_png_max_chunk_len(self):
        # Create an 8-bit grayscale image.
        w = 250
        h = 150
        max_chunk_len = 500
        img = np.random.randint(0, 256, size=(h, w)).astype(np.uint8)
        f = io.BytesIO()
        numpngw.write_png(f, img, max_chunk_len=max_chunk_len)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=w,
                                   height=h,
                                   bit_depth=8,
                                   color_type=0,
                                   interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        zstream = b''
        while True:
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            if chunk_type != b"IDAT":
                break
            self.assertEqual(chunk_type, b"IDAT")
            zstream += chunk_data
            self.assertLessEqual(len(chunk_data), max_chunk_len)
        data = zlib.decompress(zstream)
        b = np.fromstring(data, dtype=np.uint8)
        lines = b.reshape(h, w + 1)
        img2 = lines[:, 1:].reshape(h, w)
        assert_array_equal(img2, img)

        # Check the IEND chunk; chunk_type and chunk_data were read
        # in the loop above.
        self.assertEqual(chunk_type, b"IEND")
        self.assertEqual(chunk_data, b"")

        self.assertEqual(file_contents, b"")
Example #20
0
    def test_write_png_with_alpha(self):
        # Test creation of grayscale+alpha and RGBA images (color types 4
        # and 6, resp.), with bit depths 8 and 16.
        w = 25
        h = 15
        np.random.seed(12345)
        for filter_type in [0, 1, 2, 3, 4, "heuristic", "auto"]:
            for color_type in [4, 6]:
                num_channels = 2 if color_type == 4 else 4
                for bit_depth in [8, 16]:
                    for interlace in [0, 1]:
                        dt = np.uint8 if bit_depth == 8 else np.uint16
                        sz = (h, w, num_channels)
                        img = np.random.randint(0, 2**bit_depth,
                                                size=sz).astype(dt)
                        f = io.BytesIO()
                        numpngw.write_png(f, img, filter_type=filter_type,
                                          interlace=interlace)

                        file_contents = f.getvalue()

                        file_contents = check_signature(file_contents)

                        file_contents = check_ihdr(file_contents,
                                                   width=w, height=h,
                                                   bit_depth=bit_depth,
                                                   color_type=color_type,
                                                   interlace=interlace)

                        file_contents = check_text(file_contents,
                                                   b"Creation Time")
                        software = numpngw._software_text().encode('latin-1')
                        file_contents = check_text(file_contents, b"Software",
                                                   software)

                        file_contents = check_idat(file_contents,
                                                   color_type=color_type,
                                                   bit_depth=bit_depth,
                                                   interlace=interlace,
                                                   img=img)

                        check_iend(file_contents)
Example #21
0
    def test_write_png_max_chunk_len(self):
        # Create an 8-bit grayscale image.
        w = 250
        h = 150
        max_chunk_len = 500
        img = np.random.randint(0, 256, size=(h, w)).astype(np.uint8)
        f = io.BytesIO()
        numpngw.write_png(f, img, max_chunk_len=max_chunk_len)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=w, height=h,
                                   bit_depth=8, color_type=0, interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        zstream = b''
        while True:
            chunk_type, chunk_data, file_contents = next_chunk(file_contents)
            if chunk_type != b"IDAT":
                break
            self.assertEqual(chunk_type, b"IDAT")
            zstream += chunk_data
            self.assertLessEqual(len(chunk_data), max_chunk_len)
        data = zlib.decompress(zstream)
        b = np.fromstring(data, dtype=np.uint8)
        lines = b.reshape(h, w + 1)
        img2 = lines[:, 1:].reshape(h, w)
        assert_array_equal(img2, img)

        # Check the IEND chunk; chunk_type and chunk_data were read
        # in the loop above.
        self.assertEqual(chunk_type, b"IEND")
        self.assertEqual(chunk_data, b"")

        self.assertEqual(file_contents, b"")
Example #22
0
    def test_write_png_timestamp_gamma_chromaticity(self):
        np.random.seed(123)
        img = np.random.randint(0, 256, size=(10, 10)).astype(np.uint8)
        f = io.BytesIO()
        timestamp = (1452, 4, 15, 8, 9, 10)
        gamma = 2.2
        chromaticity = [[0.500, 0.750],
                        [0.125, 0.960],
                        [0.875, 0.625],
                        [0.750, 0.375]]
        numpngw.write_png(f, img, timestamp=timestamp, gamma=gamma,
                          chromaticity=chromaticity)

        file_contents = f.getvalue()

        file_contents = check_signature(file_contents)

        file_contents = check_ihdr(file_contents,
                                   width=img.shape[1], height=img.shape[0],
                                   bit_depth=8, color_type=0, interlace=0)

        file_contents = check_text(file_contents, b"Creation Time")
        file_contents = check_text(file_contents, b"Software",
                                   numpngw._software_text().encode('latin-1'))

        file_contents = check_time(file_contents, timestamp)

        file_contents = check_gama(file_contents, gamma)

        expected_chrm = (100000*np.array(chromaticity) + 0.5).astype(np.uint32)
        file_contents = check_chrm(file_contents, expected_chrm)

        file_contents = check_idat(file_contents, color_type=0, bit_depth=8,
                                   interlace=0, img=img)

        check_iend(file_contents)
Example #23
0
    def test_write_apng_bkgd(self):
        # Test creation of RGB images (color type 2), with a background color.
        w = 16
        h = 8
        np.random.seed(123)
        num_frames = 3
        for bit_depth in [8, 16]:
            maxval = 2**bit_depth
            bg = (maxval - 1, maxval - 2, maxval - 3)
            dt = np.uint16 if bit_depth == 16 else np.uint8
            seq = np.random.randint(0, maxval,
                                    size=(num_frames, h, w, 3)).astype(dt)

            f = io.BytesIO()
            numpngw.write_apng(f, seq, background=bg, filter_type=0)

            file_contents = f.getvalue()

            file_contents = check_signature(file_contents)

            file_contents = check_ihdr(file_contents, width=w, height=h,
                                       bit_depth=bit_depth, color_type=2,
                                       interlace=0)

            file_contents = check_text(file_contents, b"Creation Time")
            software = numpngw._software_text().encode('latin-1')
            file_contents = check_text(file_contents, b"Software",
                                       software)

            file_contents = check_bkgd(file_contents, color=bg, color_type=2)

            file_contents = check_actl(file_contents, num_frames=num_frames,
                                       num_plays=0)

            sequence_number = 0
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            file_contents = check_idat(file_contents, color_type=2,
                                       bit_depth=bit_depth,
                                       interlace=0, img=seq[0])

            for k in range(1, num_frames):
                file_contents = check_fctl(file_contents,
                                           sequence_number=sequence_number,
                                           width=w, height=h)
                sequence_number += 1

                # Check the fdAT chunk.
                nxt = next_chunk(file_contents)
                chunk_type, chunk_data, file_contents = nxt
                self.assertEqual(chunk_type, b"fdAT")
                actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
                self.assertEqual(actual_seq_num, sequence_number)
                sequence_number += 1
                decompressed = zlib.decompress(chunk_data[4:])
                b = np.fromstring(decompressed, dtype=np.uint8)
                img2 = stream_to_array(b, w, h, color_type=2,
                                       bit_depth=bit_depth, interlace=0)
                assert_array_equal(img2, seq[k])

            check_iend(file_contents)
Example #24
0
    def test_write_png_8bit_RGB_palette(self):
        for interlace in [0, 1]:
            for transparent in [None, (0, 1, 2)]:
                for bitdepth in [1, 2, 4, 8]:
                    w = 13
                    h = 4
                    ncolors = min(2**bitdepth, w*h)
                    idx = np.arange(w*h).reshape(h, w) % ncolors
                    colors = np.arange(ncolors*3).reshape(ncolors, 3)
                    colors = colors.astype(np.uint8)
                    img = colors[idx]
                    f = io.BytesIO()
                    numpngw.write_png(f, img, use_palette=True,
                                      transparent=transparent,
                                      interlace=interlace,
                                      bitdepth=bitdepth)

                    file_contents = f.getvalue()

                    file_contents = check_signature(file_contents)

                    file_contents = check_ihdr(file_contents,
                                               width=img.shape[1],
                                               height=img.shape[0],
                                               bit_depth=bitdepth,
                                               color_type=3,
                                               interlace=interlace)

                    file_contents = check_text(file_contents, b"Creation Time")
                    software = numpngw._software_text().encode('latin-1')
                    file_contents = check_text(file_contents, b"Software",
                                               software)

                    # Check the PLTE chunk.
                    chunk_type, chunk_data, file_contents = \
                        next_chunk(file_contents)
                    self.assertEqual(chunk_type, b"PLTE")
                    p = np.fromstring(chunk_data,
                                      dtype=np.uint8).reshape(-1, 3)
                    n = ncolors*3
                    expected = np.arange(n, dtype=np.uint8).reshape(-1, 3)
                    assert_array_equal(p, expected)

                    if transparent is not None:
                        file_contents = check_trns(file_contents,
                                                   color_type=3,
                                                   transparent=transparent,
                                                   palette=p)

                    # Check the IDAT chunk.
                    chunk_type, chunk_data, file_contents = \
                        next_chunk(file_contents)
                    self.assertEqual(chunk_type, b"IDAT")
                    decompressed = zlib.decompress(chunk_data)
                    stream = np.fromstring(decompressed, dtype=np.uint8)
                    height, width = img.shape[:2]
                    img2 = stream_to_array(stream, width, height, color_type=3,
                                           bit_depth=bitdepth,
                                           interlace=interlace)
                    expected = idx
                    assert_array_equal(img2, expected)

                    check_iend(file_contents)
Example #25
0
    def test_write_png_8bit_RGB_palette(self):
        for interlace in [0, 1]:
            for transparent in [None, (0, 1, 2)]:
                for bitdepth in [1, 2, 4, 8]:
                    w = 13
                    h = 4
                    ncolors = min(2**bitdepth, w*h)
                    idx = np.arange(w*h).reshape(h, w) % ncolors
                    colors = np.arange(ncolors*3).reshape(ncolors, 3)
                    colors = colors.astype(np.uint8)
                    img = colors[idx]
                    f = io.BytesIO()
                    numpngw.write_png(f, img, use_palette=True,
                                      transparent=transparent,
                                      interlace=interlace,
                                      bitdepth=bitdepth)

                    file_contents = f.getvalue()

                    file_contents = check_signature(file_contents)

                    file_contents = check_ihdr(file_contents,
                                               width=img.shape[1],
                                               height=img.shape[0],
                                               bit_depth=bitdepth,
                                               color_type=3,
                                               interlace=interlace)

                    file_contents = check_text(file_contents, b"Creation Time")
                    software = numpngw._software_text().encode('latin-1')
                    file_contents = check_text(file_contents, b"Software",
                                               software)

                    # Check the PLTE chunk.
                    chunk_type, chunk_data, file_contents = \
                        next_chunk(file_contents)
                    self.assertEqual(chunk_type, b"PLTE")
                    p = np.frombuffer(chunk_data,
                                      dtype=np.uint8).reshape(-1, 3)
                    n = ncolors*3
                    expected = np.arange(n, dtype=np.uint8).reshape(-1, 3)
                    assert_array_equal(p, expected)

                    if transparent is not None:
                        file_contents = check_trns(file_contents,
                                                   color_type=3,
                                                   transparent=transparent,
                                                   palette=p)

                    # Check the IDAT chunk.
                    chunk_type, chunk_data, file_contents = \
                        next_chunk(file_contents)
                    self.assertEqual(chunk_type, b"IDAT")
                    decompressed = zlib.decompress(chunk_data)
                    stream = np.frombuffer(decompressed, dtype=np.uint8)
                    height, width = img.shape[:2]
                    img2 = stream_to_array(stream, width, height, color_type=3,
                                           bit_depth=bitdepth,
                                           interlace=interlace)
                    expected = idx
                    assert_array_equal(img2, expected)

                    check_iend(file_contents)
Example #26
0
    def test_write_apng_bkgd(self):
        # Test creation of RGB images (color type 2), with a background color.
        # Also test the chromaticity argument.
        w = 16
        h = 8
        np.random.seed(123)
        num_frames = 3
        chromaticity = [[0.500, 0.750],
                        [0.125, 0.960],
                        [0.875, 0.625],
                        [0.750, 0.375]]
        for bit_depth in [8, 16]:
            maxval = 2**bit_depth
            bg = (maxval - 1, maxval - 2, maxval - 3)
            dt = np.uint16 if bit_depth == 16 else np.uint8
            seq = np.random.randint(0, maxval,
                                    size=(num_frames, h, w, 3)).astype(dt)

            f = io.BytesIO()
            numpngw.write_apng(f, seq, background=bg, filter_type=0,
                               chromaticity=chromaticity)

            file_contents = f.getvalue()

            file_contents = check_signature(file_contents)

            file_contents = check_ihdr(file_contents, width=w, height=h,
                                       bit_depth=bit_depth, color_type=2,
                                       interlace=0)

            file_contents = check_text(file_contents, b"Creation Time")
            software = numpngw._software_text().encode('latin-1')
            file_contents = check_text(file_contents, b"Software",
                                       software)

            expected_chrm = (100000*np.array(chromaticity) + 0.5).astype(np.uint32)
            file_contents = check_chrm(file_contents, expected_chrm)

            file_contents = check_bkgd(file_contents, color=bg, color_type=2)

            file_contents = check_actl(file_contents, num_frames=num_frames,
                                       num_plays=0)

            sequence_number = 0
            file_contents = check_fctl(file_contents,
                                       sequence_number=sequence_number,
                                       width=w, height=h)
            sequence_number += 1

            file_contents = check_idat(file_contents, color_type=2,
                                       bit_depth=bit_depth,
                                       interlace=0, img=seq[0])

            for k in range(1, num_frames):
                file_contents = check_fctl(file_contents,
                                           sequence_number=sequence_number,
                                           width=w, height=h)
                sequence_number += 1

                # Check the fdAT chunk.
                nxt = next_chunk(file_contents)
                chunk_type, chunk_data, file_contents = nxt
                self.assertEqual(chunk_type, b"fdAT")
                actual_seq_num = struct.unpack("!I", chunk_data[:4])[0]
                self.assertEqual(actual_seq_num, sequence_number)
                sequence_number += 1
                decompressed = zlib.decompress(chunk_data[4:])
                b = np.frombuffer(decompressed, dtype=np.uint8)
                img2 = stream_to_array(b, w, h, color_type=2,
                                       bit_depth=bit_depth, interlace=0)
                assert_array_equal(img2, seq[k])

            check_iend(file_contents)