Example #1
0
bool WebPDecoder::readData(Mat &img)
{
    if( m_width > 0 && m_height > 0 )
    {
        if (img.cols != m_width || img.rows != m_height || img.type() != m_type)
        {
            img.create(m_height, m_width, m_type);
        }

        uchar* out_data = img.data;
        size_t out_data_size = img.cols * img.rows * img.elemSize();

        uchar *res_ptr = 0;
        if (channels == 3)
        {
            res_ptr = WebPDecodeBGRInto(data.data, data.total(), out_data,
                                        out_data_size, img.step);
        }
        else if (channels == 4)
        {
            res_ptr = WebPDecodeBGRAInto(data.data, data.total(), out_data,
                                         out_data_size, img.step);
        }

        if(res_ptr == out_data)
        {
            return true;
        }
    }

    return false;
}
Example #2
0
bool WEBPImageDecoder::decode(bool onlySize)
{
    // Minimum number of bytes needed to ensure one can parse size information.
    static const size_t sizeOfHeader = 30;
    // Number of bytes per pixel.
    static const int bytesPerPixel = 3;

    if (failed())
        return false;
    const size_t dataSize = m_data->buffer().size();
    const uint8_t* dataBytes =
        reinterpret_cast<const uint8_t*>(m_data->buffer().data());
    int width, height;
    if (dataSize < sizeOfHeader)
        return true;
    if (!WebPGetInfo(dataBytes, dataSize, &width, &height))
        return setFailed();
    if (!ImageDecoder::isSizeAvailable() && !setSize(width, height))
        return setFailed();
    if (onlySize)
        return true;

    // FIXME: Add support for progressive decoding.
    if (!isAllDataReceived())
        return true;
    ASSERT(!m_frameBufferCache.isEmpty());
    RGBA32Buffer& buffer = m_frameBufferCache[0];
    if (buffer.status() == RGBA32Buffer::FrameEmpty) {
        ASSERT(width == size().width());
        ASSERT(height == size().height());
        if (!buffer.setSize(width, height))
            return setFailed();
    }
    const int stride = width * bytesPerPixel;
    Vector<uint8_t> rgb;
    rgb.resize(height * stride);
    if (!WebPDecodeBGRInto(dataBytes, dataSize, rgb.data(), rgb.size(), stride))
        return setFailed();
    // FIXME: remove this data copy.
    for (int y = 0; y < height; ++y) {
        const uint8_t* const src = &rgb[y * stride];
        for (int x = 0; x < width; ++x)
            buffer.setRGBA(x, y, src[bytesPerPixel * x + 2], src[bytesPerPixel * x + 1], src[bytesPerPixel * x + 0], 0xff);
    }
    buffer.setStatus(RGBA32Buffer::FrameComplete);
    buffer.setHasAlpha(false);
    buffer.setRect(IntRect(IntPoint(), size()));
    return true;
}
Example #3
0
bool WebPDecoder::readData(Mat &img)
{
    if( m_width > 0 && m_height > 0 )
    {
        uchar* out_data = img.data;
        unsigned int out_data_size = m_width * m_height * 3 * sizeof(uchar);

        uchar *res_ptr = WebPDecodeBGRInto(data.data, data.total(), out_data, out_data_size, m_width * 3);

        if(res_ptr == out_data)
        {
            return true;
        }
    }

    return false;
}
Example #4
0
bool WebPDecoder::readData(Mat &img)
{
    CV_CheckGE(m_width, 0, ""); CV_CheckGE(m_height, 0, "");

    CV_CheckEQ(img.cols, m_width, "");
    CV_CheckEQ(img.rows, m_height, "");

    if (m_buf.empty())
    {
        fs.seekg(0, std::ios::beg); CV_Assert(fs && "File stream error");
        data.create(1, validateToInt(fs_size), CV_8UC1);
        fs.read((char*)data.ptr(), fs_size);
        CV_Assert(fs && "Can't read file data");
        fs.close();
    }
    CV_Assert(data.type() == CV_8UC1); CV_Assert(data.rows == 1);

    {
        Mat read_img;
        CV_CheckType(img.type(), img.type() == CV_8UC1 || img.type() == CV_8UC3 || img.type() == CV_8UC4, "");
        if (img.type() != m_type)
        {
            read_img.create(m_height, m_width, m_type);
        }
        else
        {
            read_img = img;  // copy header
        }

        uchar* out_data = read_img.ptr();
        size_t out_data_size = read_img.dataend - out_data;

        uchar *res_ptr = NULL;
        if (channels == 3)
        {
            CV_CheckTypeEQ(read_img.type(), CV_8UC3, "");
            res_ptr = WebPDecodeBGRInto(data.ptr(), data.total(), out_data,
                                        (int)out_data_size, (int)read_img.step);
        }
        else if (channels == 4)
        {
            CV_CheckTypeEQ(read_img.type(), CV_8UC4, "");
            res_ptr = WebPDecodeBGRAInto(data.ptr(), data.total(), out_data,
                                         (int)out_data_size, (int)read_img.step);
        }

        if (res_ptr != out_data)
            return false;

        if (read_img.data == img.data && img.type() == m_type)
        {
            // nothing
        }
        else if (img.type() == CV_8UC1)
        {
            cvtColor(read_img, img, COLOR_BGR2GRAY);
        }
        else if (img.type() == CV_8UC3 && m_type == CV_8UC4)
        {
            cvtColor(read_img, img, COLOR_BGRA2BGR);
        }
        else if (img.type() == CV_8UC4 && m_type == CV_8UC3)
        {
            cvtColor(read_img, img, COLOR_BGR2BGRA);
        }
        else
        {
            CV_Error(Error::StsInternal, "");
        }
    }
    return true;
}