Generates an optimized JPEG from JPEG-encoded bytes.

    :param bytes_in: the input image's bytes
    :param quality: the output JPEG quality (default 95)

    :returns: Optimized JPEG bytes
    :rtype: bytes

    :raises ValueError: Guetzli was not able to decode the image (the image is
                        probably corrupted or is not a JPEG)

    .. code:: python

        import pyguetzli

        input_jpeg_bytes = open("./test/image.jpg", "rb").read()
        optimized_jpeg = pyguetzli.process_jpeg_bytes(input_jpeg_bytes)

def process_jpeg_bytes(bytes_in, quality=DEFAULT_JPEG_QUALITY):
    """Generates an optimized JPEG from JPEG-encoded bytes.

    :param bytes_in: the input image's bytes
    :param quality: the output JPEG quality (default 95)

    :returns: Optimized JPEG bytes
    :rtype: bytes

    :raises ValueError: Guetzli was not able to decode the image (the image is
                        probably corrupted or is not a JPEG)

    .. code:: python

        import pyguetzli

        input_jpeg_bytes = open("./test/image.jpg", "rb").read()
        optimized_jpeg = pyguetzli.process_jpeg_bytes(input_jpeg_bytes)
    """
    bytes_out_p = ffi.new("char**")
    bytes_out_p_gc = ffi.gc(bytes_out_p, lib.guetzli_free_bytes)

    length = lib.guetzli_process_jpeg_bytes(
            bytes_in,
            len(bytes_in),
            bytes_out_p_gc,
            quality
            )

    if length == 0:
        raise ValueError("Invalid JPEG: Guetzli was not able to decode the image")  # noqa

    bytes_out = ffi.cast("char*", bytes_out_p_gc[0])
    return ffi.unpack(bytes_out, length)