Skip to content

open two JPG images, and the colors in the second image were incorrect. #108

@libaineu2004

Description

@libaineu2004

The original image for the first picture is
Image
The original image for the second picture is
Image

I opened the two pictures one after the other.
I used gpujepg to decode the image, and then used OpenCV to save it. The colors in the second image are like this.
Image
the colors in the second image were incorrect.

UseGpuJpeg.h

#ifndef USEGPUJPEG_H
#define USEGPUJPEG_H

#include "usegpujpeg_global.h"

class UseGpuJpeg
{
public:
    bool init(int gpuId = 0);
    void uninit(void);

public:
    bool decode(const std::string &pathname, void *&buf, int &width, int &height, int &bytesPerLine, int &channel);

private:
    gpujpeg_decoder *m_pDecoder = nullptr;
};

#endif //USEGPUJPEG_H

UseGpuJpeg.cpp

#include "usegpujpeg.h"

#include "libgpujpeg/gpujpeg.h"
#include "libgpujpeg/gpujpeg_common.h"

bool UseGpuJpeg::init(int gpuId)
{
    if (gpujpeg_init_device(gpuId, 0))
    {
        std::cout << "error: gpujpeg_init_device, gpuId is " << gpuId << std::endl;
        return false;
    }

    m_pDecoder = gpujpeg_decoder_create(0);
    gpujpeg_decoder_set_option(m_pDecoder, GPUJPEG_DEC_OPT_ALIGNMENT_BYTES_INT, "4");
    if (m_pDecoder == nullptr)
    {
        std::cout << "error: gpujpeg_decoder_create fail" << std::endl;
        return false;
    }

    return true;
}

void UseGpuJpeg::uninit()
{
    if (m_pDecoder)
    {
        gpujpeg_decoder_destroy(m_pDecoder);
    }
}

bool UseGpuJpeg::decode(const std::string &pathname, void *&buf, int &width, int &height, int &bytesPerLine, int &channel)
{
    if (m_pDecoder == nullptr)
    {
        return false;
    }

    //load image
    size_t input_image_size = 0;
    uint8_t *input_image;
    if (gpujpeg_image_load_from_file(pathname.c_str(), &input_image, &input_image_size) != 0)
    {
        return false;
    }

    //set decoder default output destination
    struct gpujpeg_decoder_output decoder_output;
    gpujpeg_decoder_output_set_default(&decoder_output);

    //decompress the image
    if (gpujpeg_decoder_decode(m_pDecoder, input_image, input_image_size, &decoder_output) != 0)
    {
        return false;
    }

    gpujpeg_image_destroy(input_image);

    //output
    buf = decoder_output.data;
    width = decoder_output.param_image.width;
    height = decoder_output.param_image.height;

    if (decoder_output.param_image.pixel_format == GPUJPEG_U8)
    {
        channel = 1;
    }
    else if (decoder_output.param_image.pixel_format == GPUJPEG_4444_U8_P0123)
    {
        channel = 4;
    }
    else
    {
        channel = 3;
    }

    int bytesPerLine1 = width * channel + decoder_output.param_image.width_padding;
    bytesPerLine = ((width * channel * 8) + 31) / 32 * 4;

    return true;
}

test.cpp

m_pGpuJpeg = new UseGpuJpeg();
m_pGpuJpeg->init();

void *buf = 0;
int width = 0;
int height = 0;
int bytesPerLine = 0;
int channel = 0;

//src1
m_pGpuJpeg->decode("src1.jpg", buf, width, height, bytesPerLine, channel);

cv::Mat m;
if (channel == 1)
{
m = cv::Mat(height, width, CV_8UC1, buf, bytesPerLine);
}
else if (channel == 3)
{
m = cv::Mat(height, width, CV_8UC3, buf, bytesPerLine);
cv::cvtColor(m, m, cv::COLOR_RGB2BGR);
}

cv::imwrite("d:/decode1.jpg", m);

//src2
m_pGpuJpeg->decode("src2.jpg", buf, width, height, bytesPerLine, channel);

if (channel == 1)
{
m = cv::Mat(height, width, CV_8UC1, buf, bytesPerLine);
}
else if (channel == 3)
{
m = cv::Mat(height, width, CV_8UC3, buf, bytesPerLine);
cv::cvtColor(m, m, cv::COLOR_RGB2BGR);
}

cv::imwrite("d:/decode2.jpg", m);

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions