Example #1
0
 def test_decode_format_raises(self):
     """Test decoding using invalid format raises."""
     index = get_indexed_datasets('1.2.840.10008.1.2.4.90')
     ds = index['693_J2KR.dcm']['ds']
     frame = next(generate_frames(ds))
     msg = r"Unsupported 'j2k_format' value: 3"
     with pytest.raises(ValueError, match=msg):
         get_parameters(frame, j2k_format=3)
Example #2
0
    def test_decode_bad_type_raises(self):
        """Test decoding using invalid type raises."""
        index = get_indexed_datasets('1.2.840.10008.1.2.4.90')
        ds = index['MR_small_jp2klossless.dcm']['ds']
        frame = tuple(next(generate_frames(ds)))
        assert not hasattr(frame, 'tell') and not isinstance(frame, bytes)

        msg = (
            r"The Python object containing the encoded JPEG 2000 data must "
            r"either be bytes or have read\(\), tell\(\) and seek\(\) methods."
        )
        with pytest.raises(TypeError, match=msg):
            get_parameters(frame)
Example #3
0
    def test_jpeg2000i(self, fname, info):
        """Test get_parameters() for the baseline datasets."""
        index = get_indexed_datasets('1.2.840.10008.1.2.4.91')
        ds = index[fname]['ds']

        frame = next(generate_frames(ds))
        params = get_parameters(frame)

        assert (info[0], info[1]) == (params['rows'], params['columns'])
        assert info[2] == params['nr_components']
        assert info[3] == params['precision']
        assert info[4] == params['is_signed']
Example #4
0
    def test_data_signed_pr_0(self):
        """Test signed JPEG data with Pixel Representation 0"""
        ds = self.ds['TOSHIBA_J2K_SIZ1_PixRep0.dcm']['ds']
        assert self.uid == ds.file_meta.TransferSyntaxUID
        assert 1 == ds.SamplesPerPixel
        assert 1 == getattr(ds, 'NumberOfFrames', 1)
        assert 'MONOCHROME' in ds.PhotometricInterpretation
        assert 16 == ds.BitsAllocated
        assert 16 == ds.BitsStored
        assert 0 == ds.PixelRepresentation
        #ds.PixelRepresentation = 1

        arr = ds.pixel_array

        frame = next(generate_frames(ds))
        params = get_parameters(frame)
        assert params['is_signed'] == True
Example #5
0
    def test_data_signed_pr_0(self):
        """Test signed JPEG data with Pixel Representation 0"""
        ds = self.ds['TOSHIBA_J2K_SIZ1_PixRep0.dcm']['ds']
        assert self.uid == ds.file_meta.TransferSyntaxUID
        assert 1 == ds.SamplesPerPixel
        assert 1 == getattr(ds, 'NumberOfFrames', 1)
        assert 'MONOCHROME' in ds.PhotometricInterpretation
        assert 16 == ds.BitsAllocated
        assert 16 == ds.BitsStored
        assert 0 == ds.PixelRepresentation

        # Note: if PR is 0 but the JPEG data is signed then... ?
        msg = (
            r"Pixel Representation value '0' \(unsigned\) in the dataset does "
            r"not match the format of the values found in the JPEG 2000 data "
            r"'signed'")
        with pytest.warns(UserWarning, match=msg):
            arr = ds.pixel_array

        frame = next(generate_frames(ds))
        params = get_parameters(frame)
        assert params['is_signed'] == True
Example #6
0
    def test_data_unsigned_pr_1(self):
        """Test unsigned JPEG data with Pixel Representation 1"""
        ds = self.ds['TOSHIBA_J2K_SIZ0_PixRep1.dcm']['ds']
        assert self.uid == ds.file_meta.TransferSyntaxUID
        assert 1 == ds.SamplesPerPixel
        assert 1 == getattr(ds, 'NumberOfFrames', 1)
        assert 'MONOCHROME' in ds.PhotometricInterpretation
        assert 16 == ds.BitsAllocated
        assert 16 == ds.BitsStored
        assert 1 == ds.PixelRepresentation

        # Note: if PR is 1 but the JPEG data is unsigned then it should
        #   probably be converted to signed using 2s complement
        msg = (
            r"Pixel Representation value '1' \(signed\) in the dataset does "
            r"not match the format of the values found in the JPEG 2000 data "
            r"'unsigned'")
        with pytest.warns(UserWarning, match=msg):
            arr = ds.pixel_array

        frame = next(generate_frames(ds))
        params = get_parameters(frame)
        assert params['is_signed'] == False
Example #7
0
def test_subsampling():
    """Test parameters with subsampled data (see #36)."""
    jpg = DIR_15444 / "2KLS" / "oj36.j2k"
    params = get_parameters(jpg)
    print(params)
Example #8
0
def test_bad_decode():
    """Test trying to decode bad data."""
    stream = b"\xff\x4f\xff\x51\x00\x00\x01"
    msg = r"Error decoding the J2K data: failed to read the header"
    with pytest.raises(RuntimeError, match=msg):
        get_parameters(stream)