Ijg JPEG library running error: Corrupt JPEG data Solution

Source: Internet
Author: User

For Embedded image recognition, the image captured in v4l2 is in JPEG format, and the following error or warning occasionally occurs when the image is converted to BMP in the ijg JPEG Library:

Upload upt JPEG data: XX extraneous bytes before marker 0xxx

Or

Upt JPEG data: premature end of Data Segment

Because these errors are only considered as warnings in the ijg JPEG library, if they are not processed, the code will continue to be executed, so the captured image is not normal. This error has an impact on my processing algorithm results and I decided to exclude it.

Google Baidu finds that there are no good solutions. The best solution is to solve the corrupt JPEG data: XX extraneous bytes before marker 0xxx. He did this:

View plaincopy to clipboardprint?
First, I 've noticed that in your src.v6b.tar.gz there is a file called jerror. H, which has these lines:

Jmessage (jwrn_extraneous_data,
"Upload upt JPEG data: % u extraneous bytes before marker 0x % 02x ")


Then, I looked for jwrn_extraneous_data string and found it in jdmarker. c
And then, I commented out the warning line in that file, as below:

If (Cinfo-> marker-> discarded_bytes! = 0 ){
/* Warnms2 (Cinfo, jwrn_extraneous_data, Cinfo-> marker-> discarded_bytes, c); commented out by Denny! */
Cinfo-> marker-> discarded_bytes = 0;
}
First, I 've noticed that in your src.v6b.tar.gz there is a file called jerror. H, which has these lines:

Jmessage (jwrn_extraneous_data,
"Upload upt JPEG data: % u extraneous bytes before marker 0x % 02x ")

Then, I looked for jwrn_extraneous_data string and found it in jdmarker. c
And then, I commented out the warning line in that file, as below:

If (Cinfo-> marker-> discarded_bytes! = 0 ){
/* Warnms2 (Cinfo, jwrn_extraneous_data, Cinfo-> marker-> discarded_bytes, c); commented out by Denny! */
Cinfo-> marker-> discarded_bytes = 0;
}
 

Note warnms2 () and re-compile the ijg JPEG source code. The upt JPEG data: XX extraneous bytes before marker 0xxx will not appear. This is a bit stubborn... It's too troublesome. There should always be a good way.

Google Baidu warnms2, none ~ So we can find jwrn_extraneous_data,

Find the source code of jerror. h and see the following section:

View plaincopy to clipboardprint?
# Define warnms2 (Cinfo, code, P1, P2)

(Cinfo)-> err-> msg_code = (CODE ),

(Cinfo)-> err-> msg_parm. I [0] = (P1 ),

(Cinfo)-> err-> msg_parm. I [1] = (P2 ),

(* (Cinfo)-> err-> emit_message) (j_common_ptr) (Cinfo),-1 ))

# Define warnms2 (Cinfo, code, P1, P2)
 
(Cinfo)-> err-> msg_code = (CODE ),
 
(Cinfo)-> err-> msg_parm. I [0] = (P1 ),
 
(Cinfo)-> err-> msg_parm. I [1] = (P2 ),
 
(* (Cinfo)-> err-> emit_message) (j_common_ptr) (Cinfo),-1 ))
 
 

The error message is emit_message and the exception level is-1. It is okay to handle this exception!

The Code is as follows:

First name an exception handler function: my_emit_message

View plaincopy to clipboardprint?
Methoddef (void) my_emit_message (j_common_ptr Cinfo, int msg_level)
{
My_error_ptr myerr = (my_error_ptr) Cinfo-> err;
If (msg_level =-1) longjmp (myerr-> setjmp_buffer, 1 );
}
Methoddef (void) my_emit_message (j_common_ptr Cinfo, int msg_level)
{
My_error_ptr myerr = (my_error_ptr) Cinfo-> err;
If (msg_level =-1) longjmp (myerr-> setjmp_buffer, 1 );
}

Capture the exception again (note that no other exception types are written here for processing, such as error_exit ):

View plaincopy to clipboardprint?
Struct my_error_mgr Jerr;
Cinfo. Err = maid (& Jerr. Pub );
Jerr. Pub. emit_message = my_emit_message;

If (setjmp (Jerr. setjmp_buffer )! = 0)
{
Pai_destroy_decompress (& Cinfo); // printf ("pai_destroy_decompress/N ");

//...
Return 0;
}
Struct my_error_mgr Jerr;
Cinfo. Err = maid (& Jerr. Pub );
Jerr. Pub. emit_message = my_emit_message;

If (setjmp (Jerr. setjmp_buffer )! = 0)
{
Pai_destroy_decompress (& Cinfo); // printf ("pai_destroy_decompress/N ");

//...
Return 0;
}

In this way, the upt JPEG data: XX extraneous bytes before marker 0xxx problem is solved. However, all emit_message exceptions are blocked and considered as failures. After the code is run, the following error occurs: premature end of data segment is also one of them, so it is also solved. The code runs normally for several minutes and is blocked.

This method should be provided for your reference.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/gzz2424/archive/2010/09/15/5885656.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.