Discussion:
[Libjpeg-turbo-users] tjCompressFromYUV
Alessio Agneessens
2016-03-23 11:26:36 UTC
Permalink
Hello,

may I ask a question about the tjCompressFromYUV() function?

I'd like to compress a planar 4:2:0 YUV buffer to a jpeg image, but
tjCompressFromYUV() fails, and the error string returned by tjGetErrorStr()
is "Bogus input colorspace".

What am I doing wrong?

Below is my code if it can help.

Thanks in advance,
Alessio

#define PADDING 2
tjhandle tjh;unsigned long buf_size;unsigned char *jpegBuf =
NULL;unsigned long jpegSize;int width = 352;int height = 288;int
quality = 70;unsigned char *ucp_frame;int j;FILE *fp = NULL;

ucp_frame = malloc(width * height * 3 / 2);if ( NULL == ucp_frame ) {
printf("malloc error ucp_frame\n");
return 0;}

fp = fopen("planar_352x288.raw", "rb");if( NULL == fp ) {
printf("fopen error\n");
return 0;}

j = fread( ucp_frame, 1, width * height * 3 / 2, fp);if( j != width *
height * 3 / 2 ) {
printf("fread error\n");
return 0;}
fclose(fp);

tjh = tjInitCompress();if( NULL == tjh ) {
printf("tjInitCompress error '%s'\n", tjGetErrorStr() );
return 0;}

buf_size = tjBufSizeYUV2( width, PADDING, height, TJSAMP_420);
jpegBuf = tjAlloc(buf_size);
if( tjCompressFromYUV( tjh, ucp_frame, width, PADDING, height,
TJSAMP_420, &jpegBuf, &jpegSize, quality,
TJFLAG_NOREALLOC ) ) {
printf("tjCompressFromYUV error '%s'\n", tjGetErrorStr() );}
DRC
2016-03-23 14:18:25 UTC
Permalink
NOTE: It's difficult to reproduce a specific problem such as this when
you don't provide a fully working standalone test program and a test
image that demonstrates the problem, nor do you indicate which O/S you
are running this on, nor do you indicate which version of libjpeg-turbo
you are using. I was able to hack together a test program based on your
code, and I was able to hack together a YUV420P test image with the
exact dimensions that your image seems to have (352x288), but it works
fine for me. That means that one of the following are true:

(1) Your test program is part of a larger program, and something outside
of the context of the code you posted is causing the problem. Again,
this is why you should always provide a standalone test program that
demonstrates the failure, not a code snippet. This also makes me a lot
happier, because I don't have to spend 20 or 30 minutes of my time
creating such a program based on your code. And in the process of
creating that test program, you may discover that the standalone program
works. That will help you determine why the same code doesn't work in
the larger program.

(2) Something about your specific test image is causing the failure (but
I can't imagine what that would be. Bytes are bytes.)

(3) Something about your build environment is messed up (but I can't
imagine what that would be.)

(4) You are somehow stomping on memory. I suggest running your program
through valgrind to make sure. When compressing from YUV (as opposed to
compressing from an RGB image), the colorspace converter is never
invoked, so the only place where "Bogus input colorspace"
(JERR_BAD_IN_COLORSPACE) could be encountered is in
jpeg_default_colorspace() (jcparam.c), which is called from
setCompDefaults() (turbojpeg.c), which is called from
tjCompressFromYUV() (turbojpeg.c). As you can see in the TurboJPEG
code, setCompDefaults() will set cinfo->in_color_space to JCS_EXT_RGB if
the pixel format parameter is TJPF_RGB, which it always is when the call
is made from the body of tjCompressFromYUV(). So I cannot fathom why
the JERR_BAD_IN_COLORSPACE error is being tripped unless somehow the
jpeg_compress_struct is being corrupted.
Post by Alessio Agneessens
Hello,
may I ask a question about the tjCompressFromYUV() function?
I'd like to compress a planar 4:2:0 YUV buffer to a jpeg image, but
tjCompressFromYUV() fails, and the error string returned by
tjGetErrorStr() is "Bogus input colorspace".
What am I doing wrong?
Below is my code if it can help.
Thanks in advance,
Alessio
|#define PADDING 2
tjhandle tjh;
unsigned long buf_size;
unsigned char *jpegBuf = NULL;
unsigned long jpegSize;
int width = 352;
int height = 288;
int quality = 70;
unsigned char *ucp_frame;
int j;
FILE *fp = NULL;
ucp_frame = malloc(width * height * 3 / 2);
if ( NULL == ucp_frame ) {
printf("malloc error ucp_frame\n");
return 0;
}
fp = fopen("planar_352x288.raw", "rb");
if( NULL == fp ) {
printf("fopen error\n");
return 0;
}
j = fread( ucp_frame, 1, width * height * 3 / 2, fp);
if( j != width * height * 3 / 2 ) {
printf("fread error\n");
return 0;
}
fclose(fp);
tjh = tjInitCompress();
if( NULL == tjh ) {
printf("tjInitCompress error '%s'\n", tjGetErrorStr() );
return 0;
}
buf_size = tjBufSizeYUV2( width, PADDING, height, TJSAMP_420);
jpegBuf = tjAlloc(buf_size);
if( tjCompressFromYUV( tjh, ucp_frame, width, PADDING, height,
TJSAMP_420, &jpegBuf, &jpegSize, quality,
TJFLAG_NOREALLOC ) ) {
printf("tjCompressFromYUV error '%s'\n", tjGetErrorStr() );
}|
Alessio Agneessens
2016-03-23 15:17:57 UTC
Permalink
Thank you for your quick reply.

I'm sorry my request wasn't more precise, however your first suggestion
turned out to be useful: with a smaller test program the tjCompressFromYUV
call performs as expected.
I will look into what was wrong with the first version.

Thanks for your time,

Best regards
Alessio
Post by DRC
NOTE: It's difficult to reproduce a specific problem such as this when
you don't provide a fully working standalone test program and a test
image that demonstrates the problem, nor do you indicate which O/S you
are running this on, nor do you indicate which version of libjpeg-turbo
you are using. I was able to hack together a test program based on your
code, and I was able to hack together a YUV420P test image with the
exact dimensions that your image seems to have (352x288), but it works
(1) Your test program is part of a larger program, and something outside
of the context of the code you posted is causing the problem. Again,
this is why you should always provide a standalone test program that
demonstrates the failure, not a code snippet. This also makes me a lot
happier, because I don't have to spend 20 or 30 minutes of my time
creating such a program based on your code. And in the process of
creating that test program, you may discover that the standalone program
works. That will help you determine why the same code doesn't work in
the larger program.
(2) Something about your specific test image is causing the failure (but
I can't imagine what that would be. Bytes are bytes.)
(3) Something about your build environment is messed up (but I can't
imagine what that would be.)
(4) You are somehow stomping on memory. I suggest running your program
through valgrind to make sure. When compressing from YUV (as opposed to
compressing from an RGB image), the colorspace converter is never
invoked, so the only place where "Bogus input colorspace"
(JERR_BAD_IN_COLORSPACE) could be encountered is in
jpeg_default_colorspace() (jcparam.c), which is called from
setCompDefaults() (turbojpeg.c), which is called from
tjCompressFromYUV() (turbojpeg.c). As you can see in the TurboJPEG
code, setCompDefaults() will set cinfo->in_color_space to JCS_EXT_RGB if
the pixel format parameter is TJPF_RGB, which it always is when the call
is made from the body of tjCompressFromYUV(). So I cannot fathom why
the JERR_BAD_IN_COLORSPACE error is being tripped unless somehow the
jpeg_compress_struct is being corrupted.
Post by Alessio Agneessens
Hello,
may I ask a question about the tjCompressFromYUV() function?
I'd like to compress a planar 4:2:0 YUV buffer to a jpeg image, but
tjCompressFromYUV() fails, and the error string returned by
tjGetErrorStr() is "Bogus input colorspace".
What am I doing wrong?
Below is my code if it can help.
Thanks in advance,
Alessio
|#define PADDING 2
tjhandle tjh;
unsigned long buf_size;
unsigned char *jpegBuf = NULL;
unsigned long jpegSize;
int width = 352;
int height = 288;
int quality = 70;
unsigned char *ucp_frame;
int j;
FILE *fp = NULL;
ucp_frame = malloc(width * height * 3 / 2);
if ( NULL == ucp_frame ) {
printf("malloc error ucp_frame\n");
return 0;
}
fp = fopen("planar_352x288.raw", "rb");
if( NULL == fp ) {
printf("fopen error\n");
return 0;
}
j = fread( ucp_frame, 1, width * height * 3 / 2, fp);
if( j != width * height * 3 / 2 ) {
printf("fread error\n");
return 0;
}
fclose(fp);
tjh = tjInitCompress();
if( NULL == tjh ) {
printf("tjInitCompress error '%s'\n", tjGetErrorStr() );
return 0;
}
buf_size = tjBufSizeYUV2( width, PADDING, height, TJSAMP_420);
jpegBuf = tjAlloc(buf_size);
if( tjCompressFromYUV( tjh, ucp_frame, width, PADDING, height,
TJSAMP_420, &jpegBuf, &jpegSize, quality,
TJFLAG_NOREALLOC ) ) {
printf("tjCompressFromYUV error '%s'\n", tjGetErrorStr() );
}|
------------------------------------------------------------------------------
Transform Data into Opportunity.
Accelerate data analysis in your applications with
Intel Data Analytics Acceleration Library.
Click to learn more.
http://pubads.g.doubleclick.net/gampad/clk?id=278785351&iu=/4140
_______________________________________________
Libjpeg-turbo-users mailing list
https://lists.sourceforge.net/lists/listinfo/libjpeg-turbo-users
Loading...