Ejemplo n.º 1
0
def __write_openjp2(img_array, filename, offset, comp_prec=8, verbose=False, **kwargs):

    cparams = __populate_cparams(**kwargs)
    comptparms = __populate_comptparms(img_array, comp_prec, cparams)

    with ExitStack() as stack:
        image = opj2.image_create(comptparms, opj2.CLRSPC_GRAY)
        stack.callback(opj2.image_destroy, image)

        __populate_image_struct(image, img_array, comp_prec, cparams)

        codec = opj2.create_compress(cparams.codec_fmt)
        stack.callback(opj2.destroy_codec, codec)

        info_handler = _INFO_CALLBACK if verbose else None
        opj2.set_info_handler(codec, info_handler)
        opj2.set_warning_handler(codec, _WARNING_CALLBACK)
        opj2.set_error_handler(codec, _ERROR_CALLBACK)

        opj2.setup_encoder(codec, cparams, image)

        strm = opj2.stream_create_default_file_stream(filename, False)
        stack.callback(opj2.stream_destroy, strm)

        stream_seek(strm, offset)

        opj2.start_compress(codec, image, strm)
        opj2.encode(codec, strm)
        opj2.end_compress(codec, strm)
Ejemplo n.º 2
0
def tile_decoder(**kwargs):
    """Fixture called with various configurations by many tests.

    Reads a tile.  That's all it does.
    """
    stream = openjp2.stream_create_default_file_stream(kwargs['filename'],
                                                       True)
    dparam = openjp2.set_default_decoder_parameters()

    dparam.decod_format = kwargs['codec_format']

    # Do not use layer decoding limitation.
    dparam.cp_layer = 0

    # do not use resolution reductions.
    dparam.cp_reduce = 0

    codec = openjp2.create_decompress(kwargs['codec_format'])

    openjp2.set_info_handler(codec, None)
    openjp2.set_warning_handler(codec, None)
    openjp2.set_error_handler(codec, None)

    openjp2.setup_decoder(codec, dparam)
    image = openjp2.read_header(stream, codec)
    openjp2.set_decode_area(codec, image,
                            kwargs['x0'], kwargs['y0'],
                            kwargs['x1'], kwargs['y1'])

    data = np.zeros((1150, 2048, 3), dtype=np.uint8)
    while True:
        rargs = openjp2.read_tile_header(codec, stream)
        tidx = rargs[0]
        size = rargs[1]
        go_on = rargs[-1]
        if not go_on:
            break
        openjp2.decode_tile_data(codec, tidx, data, size, stream)

    openjp2.end_decompress(codec, stream)
    openjp2.destroy_codec(codec)
    openjp2.stream_destroy(stream)
    openjp2.image_destroy(image)
Ejemplo n.º 3
0
def tile_decoder(**kwargs):
    """Fixture called with various configurations by many tests.

    Reads a tile.  That's all it does.
    """
    stream = openjp2.stream_create_default_file_stream(kwargs['filename'],
                                                       True)
    dparam = openjp2.set_default_decoder_parameters()

    dparam.decod_format = kwargs['codec_format']

    # Do not use layer decoding limitation.
    dparam.cp_layer = 0

    # do not use resolution reductions.
    dparam.cp_reduce = 0

    codec = openjp2.create_decompress(kwargs['codec_format'])

    openjp2.set_info_handler(codec, None)
    openjp2.set_warning_handler(codec, None)
    openjp2.set_error_handler(codec, None)

    openjp2.setup_decoder(codec, dparam)
    image = openjp2.read_header(stream, codec)
    openjp2.set_decode_area(codec, image,
                            kwargs['x0'], kwargs['y0'],
                            kwargs['x1'], kwargs['y1'])

    data = np.zeros((1150, 2048, 3), dtype=np.uint8)
    while True:
        rargs = openjp2.read_tile_header(codec, stream)
        tidx = rargs[0]
        size = rargs[1]
        go_on = rargs[-1]
        if not go_on:
            break
        openjp2.decode_tile_data(codec, tidx, data, size, stream)

    openjp2.end_decompress(codec, stream)
    openjp2.destroy_codec(codec)
    openjp2.stream_destroy(stream)
    openjp2.image_destroy(image)
Ejemplo n.º 4
0
def tile_encoder(**kwargs):
    """Fixture used by many tests."""
    num_tiles = ((kwargs['image_width'] / kwargs['tile_width']) *
                 (kwargs['image_height'] / kwargs['tile_height']))
    tile_size = ((kwargs['tile_width'] * kwargs['tile_height']) *
                 (kwargs['num_comps'] * kwargs['comp_prec'] / 8))

    data = np.random.random((kwargs['tile_height'],
                             kwargs['tile_width'],
                             kwargs['num_comps']))
    data = (data * 255).astype(np.uint8)

    l_param = openjp2.set_default_encoder_parameters()

    l_param.tcp_numlayers = 1
    l_param.cp_fixed_quality = 1
    l_param.tcp_distoratio[0] = 20

    # position of the tile grid aligned with the image
    l_param.cp_tx0 = 0
    l_param.cp_ty0 = 0

    # tile size, we are using tile based encoding
    l_param.tile_size_on = 1
    l_param.cp_tdx = kwargs['tile_width']
    l_param.cp_tdy = kwargs['tile_height']

    # use irreversible encoding
    l_param.irreversible = kwargs['irreversible']

    l_param.numresolution = 6

    l_param.prog_order = glymur.core.LRCP

    l_params = (openjp2.ImageComptParmType * kwargs['num_comps'])()
    for j in range(kwargs['num_comps']):
        l_params[j].dx = 1
        l_params[j].dy = 1
        l_params[j].h = kwargs['image_height']
        l_params[j].w = kwargs['image_width']
        l_params[j].sgnd = 0
        l_params[j].prec = kwargs['comp_prec']
        l_params[j].x0 = 0
        l_params[j].y0 = 0

    codec = openjp2.create_compress(kwargs['codec'])

    openjp2.set_info_handler(codec, None)
    openjp2.set_warning_handler(codec, None)
    openjp2.set_error_handler(codec, None)

    cspace = openjp2.CLRSPC_SRGB
    l_image = openjp2.image_tile_create(l_params, cspace)

    l_image.contents.x0 = 0
    l_image.contents.y0 = 0
    l_image.contents.x1 = kwargs['image_width']
    l_image.contents.y1 = kwargs['image_height']
    l_image.contents.color_space = openjp2.CLRSPC_SRGB

    openjp2.setup_encoder(codec, l_param, l_image)

    stream = openjp2.stream_create_default_file_stream(kwargs['filename'],
                                                       False)
    openjp2.start_compress(codec, l_image, stream)

    for j in np.arange(num_tiles):
        openjp2.write_tile(codec, j, data, tile_size, stream)

    openjp2.end_compress(codec, stream)
    openjp2.stream_destroy(stream)
    openjp2.destroy_codec(codec)
    openjp2.image_destroy(l_image)
Ejemplo n.º 5
0
def tile_encoder(**kwargs):
    """Fixture used by many tests."""
    num_tiles = ((kwargs['image_width'] / kwargs['tile_width']) *
                 (kwargs['image_height'] / kwargs['tile_height']))
    tile_size = ((kwargs['tile_width'] * kwargs['tile_height']) *
                 (kwargs['num_comps'] * kwargs['comp_prec'] / 8))

    data = np.random.random(
        (kwargs['tile_height'], kwargs['tile_width'], kwargs['num_comps']))
    data = (data * 255).astype(np.uint8)

    l_param = openjp2.set_default_encoder_parameters()

    l_param.tcp_numlayers = 1
    l_param.cp_fixed_quality = 1
    l_param.tcp_distoratio[0] = 20

    # position of the tile grid aligned with the image
    l_param.cp_tx0 = 0
    l_param.cp_ty0 = 0

    # tile size, we are using tile based encoding
    l_param.tile_size_on = 1
    l_param.cp_tdx = kwargs['tile_width']
    l_param.cp_tdy = kwargs['tile_height']

    # use irreversible encoding
    l_param.irreversible = kwargs['irreversible']

    l_param.numresolution = 6

    l_param.prog_order = glymur.core.LRCP

    l_params = (openjp2.ImageComptParmType * kwargs['num_comps'])()
    for j in range(kwargs['num_comps']):
        l_params[j].dx = 1
        l_params[j].dy = 1
        l_params[j].h = kwargs['image_height']
        l_params[j].w = kwargs['image_width']
        l_params[j].sgnd = 0
        l_params[j].prec = kwargs['comp_prec']
        l_params[j].x0 = 0
        l_params[j].y0 = 0

    codec = openjp2.create_compress(kwargs['codec'])

    openjp2.set_info_handler(codec, None)
    openjp2.set_warning_handler(codec, None)
    openjp2.set_error_handler(codec, None)

    cspace = openjp2.CLRSPC_SRGB
    l_image = openjp2.image_tile_create(l_params, cspace)

    l_image.contents.x0 = 0
    l_image.contents.y0 = 0
    l_image.contents.x1 = kwargs['image_width']
    l_image.contents.y1 = kwargs['image_height']
    l_image.contents.color_space = openjp2.CLRSPC_SRGB

    openjp2.setup_encoder(codec, l_param, l_image)

    stream = openjp2.stream_create_default_file_stream(kwargs['filename'],
                                                       False)
    openjp2.start_compress(codec, l_image, stream)

    for j in np.arange(num_tiles):
        openjp2.write_tile(codec, j, data, tile_size, stream)

    openjp2.end_compress(codec, stream)
    openjp2.stream_destroy(stream)
    openjp2.destroy_codec(codec)
    openjp2.image_destroy(l_image)
Ejemplo n.º 6
0
    def test_write_tiles_3D(self):
        """
        Test writing tiles for an RGB image.
        """

        img = fixtures.skimage.data.astronaut()

        image_height, image_width, num_comps = img.shape

        tile_height, tile_width = 256, 256

        comp_prec = 8
        irreversible = False

        cblockh_init, cblockw_init = 64, 64

        numresolution = 6

        cparams = openjp2.set_default_encoder_parameters()

        outfile = str(self.temp_j2k_filename).encode()
        num_pad_bytes = openjp2.PATH_LEN - len(outfile)
        outfile += b'0' * num_pad_bytes
        cparams.outfile = outfile

        # not from openjpeg test file
        cparams.cp_disto_alloc = 1

        cparams.tile_size_on = openjp2.TRUE
        cparams.cp_tdx = tile_width
        cparams.cp_tdy = tile_height

        cparams.cblockw_init, cparams.cblockh_init = cblockw_init, cblockh_init

        # not from openjpeg test file
        cparams.mode = 0

        cparams.irreversible = 1 if irreversible else 0

        cparams.numresolution = numresolution
        cparams.prog_order = glymur.core.PROGRESSION_ORDER['LRCP']

        cparams.tcp_mct = 1

        cparams.tcp_numlayers = 1
        cparams.tcp_rates[0] = 0
        cparams.tcp_distoratio[0] = 0

        # comptparms == l_params
        comptparms = (openjp2.ImageComptParmType * num_comps)()
        for j in range(num_comps):
            comptparms[j].dx = 1
            comptparms[j].dy = 1
            comptparms[j].w = image_width
            comptparms[j].h = image_height
            comptparms[j].x0 = 0
            comptparms[j].y0 = 0
            comptparms[j].prec = comp_prec
            comptparms[j].bpp = comp_prec
            comptparms[j].sgnd = 0

        with ExitStack() as stack:
            codec = openjp2.create_compress(openjp2.CODEC_J2K)
            stack.callback(openjp2.destroy_codec, codec)

            info_handler = _INFO_CALLBACK

            openjp2.set_info_handler(codec, info_handler)
            openjp2.set_warning_handler(codec, _WARNING_CALLBACK)
            openjp2.set_error_handler(codec, _ERROR_CALLBACK)

            image = openjp2.image_tile_create(comptparms, openjp2.CLRSPC_SRGB)
            stack.callback(openjp2.image_destroy, image)

            image.contents.x0, image.contents.y0 = 0, 0
            image.contents.x1, image.contents.y1 = image_width, image_height
            image.contents.color_space = openjp2.CLRSPC_SRGB

            openjp2.setup_encoder(codec, cparams, image)

            filename = str(self.temp_j2k_filename)
            strm = openjp2.stream_create_default_file_stream(filename, False)
            stack.callback(openjp2.stream_destroy, strm)

            openjp2.start_compress(codec, image, strm)

            # have to change the memory layout of 3D images in order to use
            # opj_write_tile
            openjp2.write_tile(
                codec, 0, _set_planar_pixel_order(img[0:256, 0:256, :]), strm
            )
            openjp2.write_tile(
                codec, 1, _set_planar_pixel_order(img[0:256, 256:512, :]), strm
            )
            openjp2.write_tile(
                codec, 2, _set_planar_pixel_order(img[256:512, 0:256, :]), strm
            )
            openjp2.write_tile(
                codec, 3, _set_planar_pixel_order(img[256:512, 256:512, :]),
                strm
            )

            openjp2.end_compress(codec, strm)
Ejemplo n.º 7
0
    def test_tile_write_moon(self):
        """
        Test writing tiles for a 2D image.
        """
        img = fixtures.skimage.data.moon()

        num_comps = 1
        image_height, image_width = img.shape

        tile_height, tile_width = 256, 256

        comp_prec = 8
        irreversible = True

        cblockh_init, cblockw_init = 64, 64

        numresolution = 6

        cparams = openjp2.set_default_encoder_parameters()

        cparams.tile_size_on = openjp2.TRUE
        cparams.cp_tdx = tile_width
        cparams.cp_tdy = tile_height

        cparams.cblockw_init, cparams.cblockh_init = cblockw_init, cblockh_init

        cparams.irreversible = 1 if irreversible else 0

        cparams.numresolution = numresolution
        cparams.prog_order = glymur.core.PROGRESSION_ORDER['LRCP']

        # greyscale so no mct
        cparams.tcp_mct = 0

        # comptparms == l_params
        comptparms = (openjp2.ImageComptParmType * num_comps)()
        for j in range(num_comps):
            comptparms[j].dx = 1
            comptparms[j].dy = 1
            comptparms[j].w = tile_width
            comptparms[j].h = tile_height
            comptparms[j].x0 = 0
            comptparms[j].y0 = 0
            comptparms[j].prec = comp_prec
            comptparms[j].bpp = comp_prec
            comptparms[j].sgnd = 0

        with ExitStack() as stack:

            codec = openjp2.create_compress(openjp2.CODEC_J2K)
            stack.callback(openjp2.destroy_codec, codec)

            info_handler = _INFO_CALLBACK

            openjp2.set_info_handler(codec, info_handler)
            openjp2.set_warning_handler(codec, _WARNING_CALLBACK)
            openjp2.set_error_handler(codec, _ERROR_CALLBACK)

            # l_params == comptparms
            # l_image == tile
            image = openjp2.image_tile_create(comptparms, openjp2.CLRSPC_GRAY)
            stack.callback(openjp2.image_destroy, image)

            image.contents.x0, image.contents.y0 = 0, 0
            image.contents.x1, image.contents.y1 = image_width, image_height
            image.contents.color_space = openjp2.CLRSPC_GRAY

            openjp2.setup_encoder(codec, cparams, image)

            filename = str(self.temp_j2k_filename)
            strm = openjp2.stream_create_default_file_stream(filename, False)
            stack.callback(openjp2.stream_destroy, strm)

            openjp2.start_compress(codec, image, strm)

            openjp2.write_tile(codec, 0, img[0:256, 0:256].copy(), strm)
            openjp2.write_tile(codec, 1, img[0:256, 256:512].copy(), strm)
            openjp2.write_tile(codec, 2, img[256:512, 0:256].copy(), strm)
            openjp2.write_tile(codec, 3, img[256:512, 256:512].copy(), strm)

            openjp2.end_compress(codec, strm)