Exemplo n.º 1
0
    def test_parse_b2(self):
        """Test that parsing the B2 file produces the correct output."""
        with open(PROCESS01_B2, 'rb') as fp:
            info = parse_jpg(fp)
            file_length = fp.tell()

        # Order by offset
        keys = sorted(info.keys(), key=lambda x: int(x.split('@')[1]))

        tempfile = NamedTemporaryFile()
        with open(tempfile.name, 'w') as tfile:
            tfile.write("Parsed version of ' testb2 hdr p', May 8, 1994, "
                        "WBP and JLM (IBM)\n")
            # Data
            for key in keys:
                writer = WRITERS[key[:3]]
                (marker, fill_bytes, data) = info[key]
                marker = '{:04x}'.format(marker)
                (name, offset) = key.split('@')
                if fill_bytes:
                    marker = 'ff' * fill_bytes + marker
                    tfile.write('      {:>2} FIL bytes\n'.format(fill_bytes))
                writer(tfile, offset, marker, name, data)

            # End
            tfile.write(
                '{} markers found in {} bytes of compressed data\n'.format(
                    len(keys), file_length))
            tfile.seek(0)

        with open(tempfile.name, 'r') as tfile:
            with open(PROCESS01_B2_REF, 'r', encoding='utf-8',
                      errors='ignore') as rfile:
                for out, ref in zip(tfile, rfile):
                    assert ref == out
Exemplo n.º 2
0
    def test_all_required_markers_found(self):
        """Required markers are:

        SOI, EOI, SOS, SOF0, DQT, DHT
        """
        # A1  and B2 are interchange format
        required_keys = ['SOI', 'EOI', 'SOS', 'SOF0', 'DQT', 'DHT']
        optional_keys = [
            'RST0', 'RST1', 'RST2', 'RST3', 'RST4', 'RST5', 'RST6', 'RST7',
            'DNL', 'DAC', 'DRI', 'COM', 'APP0', 'APP1', 'APP2', 'APP3', 'APP4',
            'APP5', 'APP6', 'APP7', 'APP8', 'APP9', 'APP10', 'APP11', 'APP12',
            'APP13', 'APP14', 'APP15'
        ]
        for info in [
                parse_jpg(open(PROCESS01_A1, 'rb')),
                parse_jpg(open(PROCESS01_B2, 'rb'))
        ]:
            keys = sorted(info.keys(), key=lambda x: int(x.split('@')[1]))
            keys = [kk.split('@')[0] for kk in keys]
            # Markers with (G) must be present
            for marker in required_keys:
                assert marker in keys

            # Markers with (-) or missing markers should not be present
            for marker in keys:
                assert marker in required_keys + optional_keys

        # B1 is in the abbreviated format
        required_keys = ['SOI', 'EOI']
        optional_keys = [
            'DQT', 'DHT', 'COM', 'APP0', 'APP1', 'APP2', 'APP3', 'APP4',
            'APP5', 'APP6', 'APP7', 'APP8', 'APP9', 'APP10', 'APP11', 'APP12',
            'APP13', 'APP14', 'APP15'
        ]

        info = parse_jpg(open(PROCESS01_B1, 'rb'))
        keys = sorted(info.keys(), key=lambda x: int(x.split('@')[1]))
        keys = [kk.split('@')[0] for kk in keys]
        # Markers with (G) must be present
        for marker in required_keys:
            assert marker in keys

        # Markers with (-) or missing markers should not be present
        for marker in keys:
            assert marker in required_keys + optional_keys
Exemplo n.º 3
0
    def test_parse_o2(self):
        """Test that parsing the O2 file produces the correct output."""
        COMMON.WRITE_SOS_DATA = True
        COMMON.WRITE_SOS_TYPE = 'Lossless'

        with open(PROCESS14_O2, 'rb') as fp:
            info = parse_jpg(fp)
            file_length = fp.tell()

        # Order by offset
        keys = sorted(info.keys(), key=lambda x: int(x.split('@')[1]))

        tempfile = NamedTemporaryFile()
        with open(tempfile.name, 'w') as tfile:
            tfile.write("Parser output for 'testo2.jpg', May 11, 1994, "
                        "WBP and JLM (IBM)\n")
            # Data
            print(keys)
            for key in keys:
                writer = WRITERS[key[:3]]
                (marker, fill_bytes, data) = info[key]
                marker = '{:04x}'.format(marker)
                (name, offset) = key.split('@')
                if fill_bytes:
                    marker = 'ff' * fill_bytes + marker
                    tfile.write('      {:>2} FIL bytes\n'.format(fill_bytes))
                writer(tfile, offset, marker, name, data)

            # End
            sos_markers = 0
            sos_keys = [kk for kk in keys if 'SOS' in kk]
            for key in sos_keys:
                # SOS contains (N - 9) markers
                _markers = [mm for mm in info[key][2].keys() if 'RST' in mm]
                sos_markers += len(_markers)

            total_markers = len(keys) + sos_markers
            tfile.write(
                '{} markers found in {} bytes of compressed data\n'.format(
                    total_markers, file_length))
            tfile.seek(0)

            COMMON.WRITE_SOS_DATA = False
            COMMON.WRITE_SOS_TYPE = 'Sequential DCT'

        with open(tempfile.name, 'r') as tfile:
            with open(PROCESS14_O2_REF, 'r', encoding='utf-8',
                      errors='ignore') as rfile:
                for out, ref in zip(tfile, rfile):
                    assert ref == out