You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
input a JPG image that is not 4-byte aligned, decode error #107
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.
target image, not good
gpujpeg_decoder_decode(m_pDecoder, input_image, input_image_size, &decoder_output);
decoder_output.param_image.width_padding is always equal to 0
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?
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):
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):
I conducted the following tests:
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.
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.
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);
Activity
MartinPulec commentedon Mar 19, 2026
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_paddingis 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?
MartinPulec commentedon Mar 19, 2026
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):decoder: add alignment option
libaineu2004 commentedon Mar 19, 2026
I conducted the following tests:
The output image result is correct.
The output image results are also correct.
or