def _parse_siz_segment(cls, fptr):
        """Parse the SIZ segment.

        Parameters
        ----------
        fptr : file
            Open file object.

        Returns
        -------
        SIZSegment
            The current SIZ segment.
        """
        offset = fptr.tell() - 2

        read_buffer = fptr.read(2)
        length, = struct.unpack('>H', read_buffer)

        read_buffer = fptr.read(length - 2)
        data = struct.unpack_from('>HIIIIIIIIH', read_buffer)

        rsiz = data[0]
        if rsiz not in _KNOWN_PROFILES:
            msg = "Invalid profile: (Rsiz={rsiz}).".format(rsiz=rsiz)
            warnings.warn(msg, UserWarning)

        xysiz = (data[1], data[2])
        xyosiz = (data[3], data[4])
        xytsiz = (data[5], data[6])
        xytosiz = (data[7], data[8])

        # Csiz is the number of components
        Csiz = data[9]

        data = struct.unpack_from('>' + 'B' * (length - 36 - 2),
                                  read_buffer, offset=36)

        bitdepth = tuple(((x & 0x7f) + 1) for x in data[0::3])
        signed = tuple(((x & 0x80) > 0) for x in data[0::3])
        xrsiz = data[1::3]
        yrsiz = data[2::3]

        for j, subsampling in enumerate(zip(xrsiz, yrsiz)):
            if 0 in subsampling:
                msg = ("Invalid subsampling value for component {comp}: "
                       "dx={dx}, dy={dy}.")
                msg = msg.format(comp=j, dx=subsampling[0], dy=subsampling[1])
                warnings.warn(msg, UserWarning)

        try:
            num_tiles_x = (xysiz[0] - xyosiz[0]) / (xytsiz[0] - xytosiz[0])
            num_tiles_y = (xysiz[1] - xyosiz[1]) / (xytsiz[1] - xytosiz[1])
        except ZeroDivisionError:
            msg = ("Invalid tile specification:  "
                   "size of {num_tile_rows} x {num_tile_cols}, "
                   "offset of {row_offset} x {col_offset}.")
            msg = msg.format(num_tile_rows=xytsiz[1],
                             num_tile_cols=xytsiz[0],
                             row_offset=xytosiz[1],
                             col_offset=xytosiz[0])
            warnings.warn(msg, UserWarning)
        else:
            numtiles = math.ceil(num_tiles_x) * math.ceil(num_tiles_y)
            if numtiles > 65535:
                msg = "Invalid number of tiles: ({numtiles})."
                msg = msg.format(numtiles=numtiles)
                warnings.warn(msg, UserWarning)

        kwargs = {
            'rsiz': rsiz,
            'xysiz': xysiz,
            'xyosiz': xyosiz,
            'xytsiz': xytsiz,
            'xytosiz': xytosiz,
            'Csiz': Csiz,
            'bitdepth': bitdepth,
            'signed': signed,
            'xyrsiz': (xrsiz, yrsiz),
            'length': length,
            'offset': offset
        }
        segment = SIZsegment(**kwargs)

        # Need to keep track of the number of components from SIZ for
        # other segments.
        cls._csiz = Csiz

        return segment
def _parse_siz_segment ( cls , fptr ) : offset = fptr . tell ( ) - 2 read_buffer = fptr . read ( 2 ) length , = struct . unpack ( '>H' , read_buffer ) read_buffer = fptr . read ( length - 2 ) data = struct . unpack_from ( '>HIIIIIIIIH' , read_buffer ) rsiz = data [ 0 ] if rsiz not in _KNOWN_PROFILES : msg = "Invalid profile: (Rsiz={rsiz})." . format ( rsiz = rsiz ) warnings . warn ( msg , UserWarning ) xysiz = ( data [ 1 ] , data [ 2 ] ) xyosiz = ( data [ 3 ] , data [ 4 ] ) xytsiz = ( data [ 5 ] , data [ 6 ] ) xytosiz = ( data [ 7 ] , data [ 8 ] ) Csiz = data [ 9 ] data = struct . unpack_from ( '>' + 'B' * ( length - 36 - 2 ) , read_buffer , offset = 36 ) bitdepth = tuple ( ( ( x & 0x7f ) + 1 ) for x in data [ 0 : : 3 ] ) signed = tuple ( ( ( x & 0x80 ) > 0 ) for x in data [ 0 : : 3 ] ) xrsiz = data [ 1 : : 3 ] yrsiz = data [ 2 : : 3 ] for j , subsampling in enumerate ( zip ( xrsiz , yrsiz ) ) : if 0 in subsampling : msg = ( "Invalid subsampling value for component {comp}: " "dx={dx}, dy={dy}." ) msg = msg . format ( comp = j , dx = subsampling [ 0 ] , dy = subsampling [ 1 ] ) warnings . warn ( msg , UserWarning ) try : num_tiles_x = ( xysiz [ 0 ] - xyosiz [ 0 ] ) / ( xytsiz [ 0 ] - xytosiz [ 0 ] ) num_tiles_y = ( xysiz [ 1 ] - xyosiz [ 1 ] ) / ( xytsiz [ 1 ] - xytosiz [ 1 ] ) except ZeroDivisionError : msg = ( "Invalid tile specification:  " "size of {num_tile_rows} x {num_tile_cols}, " "offset of {row_offset} x {col_offset}." ) msg = msg . format ( num_tile_rows = xytsiz [ 1 ] , num_tile_cols = xytsiz [ 0 ] , row_offset = xytosiz [ 1 ] , col_offset = xytosiz [ 0 ] ) warnings . warn ( msg , UserWarning ) else : numtiles = math . ceil ( num_tiles_x ) * math . ceil ( num_tiles_y ) if numtiles > 65535 : msg = "Invalid number of tiles: ({numtiles})." msg = msg . format ( numtiles = numtiles ) warnings . warn ( msg , UserWarning ) kwargs = { 'rsiz' : rsiz , 'xysiz' : xysiz , 'xyosiz' : xyosiz , 'xytsiz' : xytsiz , 'xytosiz' : xytosiz , 'Csiz' : Csiz , 'bitdepth' : bitdepth , 'signed' : signed , 'xyrsiz' : ( xrsiz , yrsiz ) , 'length' : length , 'offset' : offset } segment = SIZsegment ( ** kwargs ) cls . _csiz = Csiz return segment
Parse the SIZ segment.

        Parameters
        ----------
        fptr : file
            Open file object.

        Returns
        -------
        SIZSegment
            The current SIZ segment.