Skip to content

input a JPG image that is not 4-byte aligned, decode error #107

Open

Description

@libaineu2004

If input a JPG image whose width is not aligned in 4-byte increments, after opening and decoding, and then saving it using OpenCV, the image will be in a disorderly state.

src image, the image resolution is 61*57.
Image

target image, not good
Image

gpujpeg_decoder_decode(m_pDecoder, input_image, input_image_size, &decoder_output);
decoder_output.param_image.width_padding is always equal to 0


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;
    }

    // Fix me: param_image.width_padding is always equal to 0
    int bytesPerLine1 = width * channel + decoder_output.param_image.width_padding;
    bytesPerLine = ((width * channel * 8) + 31) / 32 * 4;

    return true;
}
void *buf = 0;
            int width = 0;
            int height = 0;
            int bytesPerLine = 0;
            int channel = 0;
            m_pGpuJpeg->decode("src.jpg", buf, width, height, bytesPerLine, channel);

            //use opencv
            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:/decode.jpg", m);

Activity

  1. MartinPulec commented on Mar 19, 2026

    @MartinPulec
    Collaborator

    Hi, I've tried your image with gpujpegtool bundled with GPUJPEG and the result seem to be ok for me:
    gpujpegtool -d 566025371-39b24251-267a-41a5-aaf9-6657d1ad4624.jpg out.png
    (or using out.pnm)

    decoder_output.param_image.width_padding is always 0, as you wrote. It is actually an user setting on encoder, not set by GPUJPEG - I am not entirely sure if it can be currently set for the decoder.

    The thing I would like point to is that GPUJPEG decoder doesn't pad the output in any way by default, so if the input has 61 pixels, the output line size will be 183 bytes. Your expression bytesPerLine = ((width * channel * 8) + 31) / 32 * 4; yields 184 B.

    I don't know if 4-byte padding is OpenCV hard requirement. If so, your inquiry would translate to the requirement to have the decoder output 4B line-aligned?

  2. self-assigned this
    on Mar 19, 2026
  3. MartinPulec commented on Mar 19, 2026

    @MartinPulec
    Collaborator

    I am not entirely sure if it can be currently set for the decoder.

    I've added decoder option to enforce output alignment. usage (before gpujpeg_decoder_decode):
    gpujpeg_decoder_set_option(m_pDecoder, GPUJPEG_DEC_OPT_ALIGNMENT_BYTES_INT, "4");

    For me, it is slightly more demanding to test than to write it, so it is not entirely properly tested but I've tried with your image: ./gpujpegtool -d 566025371-39b24251-267a-41a5-aaf9-6657d1ad4624.jpg out.png -O dec_opt_alignment_bytes=4 - the result is shifted to the opposite side than your output so I suppose it will compensate well :D (the opt is incorrect in my command, when the alignment is not expected):

    Image
  4. added a commit that references this issue on Mar 19, 2026
  5. libaineu2004 commented on Mar 19, 2026

    @libaineu2004
    Author

    I am not entirely sure if it can be currently set for the decoder.

    I've added decoder option to enforce output alignment. usage (before gpujpeg_decoder_decode): gpujpeg_decoder_set_option(m_pDecoder, GPUJPEG_DEC_OPT_ALIGNMENT_BYTES_INT, "4");

    For me, it is slightly more demanding to test than to write it, so it is not entirely properly tested but I've tried with your image: ./gpujpegtool -d 566025371-39b24251-267a-41a5-aaf9-6657d1ad4624.jpg out.png -O dec_opt_alignment_bytes=4 - the result is shifted to the opposite side than your output so I suppose it will compensate well :D (the opt is incorrect in my command, when the alignment is not expected):

    Image

    I conducted the following tests:

    1. Using gpujpeg_decoder_set_option
    gpujpeg_decoder_set_option(m_pDecoder, GPUJPEG_DEC_OPT_ALIGNMENT_BYTES_INT, "4");
    m_pGpuJpeg->decode("src.jpg", buf, width, height, bytesPerLine, channel);
    
    //use opencv
    cv::Mat m;
    if (channel == 1)
    {
    // With parameter "bytesPerLine"
    m = cv::Mat(height, width, CV_8UC1, buf, bytesPerLine);
    }
    else if (channel == 3)
    {
    // With parameter "bytesPerLine"
    m = cv::Mat(height, width, CV_8UC3, buf, bytesPerLine);
    cv::cvtColor(m, m, cv::COLOR_RGB2BGR);
    }
    
    cv::imwrite("d:/decode.jpg", m);
    

    The output image result is correct.

    1. Do not use gpujpeg_decoder_set_option
    //gpujpeg_decoder_set_option(m_pDecoder, GPUJPEG_DEC_OPT_ALIGNMENT_BYTES_INT, "4");
    m_pGpuJpeg->decode("src.jpg", buf, width, height, bytesPerLine, channel);
    
    //use opencv
    cv::Mat m;
    if (channel == 1)
    {
    // No parameter "bytesPerLine" 
    m = cv::Mat(height, width, CV_8UC1, buf);
    }
    else if (channel == 3)
    {
    // No parameter "bytesPerLine" 
    m = cv::Mat(height, width, CV_8UC3, buf);
    cv::cvtColor(m, m, cv::COLOR_RGB2BGR);
    }
    
    cv::imwrite("d:/decode.jpg", m);
    

    The output image results are also correct.

    1. It is evident that the parameter "bytesPerLine" plays a decisive role in OpenCV. The results are different when "bytesPerLine" is present and when it is absent.
    m = cv::Mat(height, width, CV_8UC1, buf, bytesPerLine);
    

    or

    m = cv::Mat(height, width, CV_8UC1, buf);
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions