Today, I used the JPEG library to read JPG files and encountered many errors during compilation.
The execution of the pai_create_decompress (& Cinfo) function fails at the start. This problem is solved using other jpeg. Lib, but the following is
(Void) pai_read_header (& Cinfo, true); function access memory error. this problem cannot be solved all the time. later, I made changes directly to another program that has used the JPEG library to read JPG files. The code for reading part is still the same, but it can run. this should be a problem with program settings. however, we still cannot find the difference between the settings of the two programs, which will be studied later. paste the Code as follows:
Struct my_error_mgr {
Struct performance_error_mgr pub;/* "public" fields */
Jmp_buf setjmp_buffer;/* for return to caller */
};
Typedef struct my_error_mgr * my_error_ptr;
Methoddef (void)
My_error_exit (j_common_ptr Cinfo)
{
/* Cinfo-> err really points to a my_error_mgr struct, so coerce pointer */
My_error_ptr myerr = (my_error_ptr) Cinfo-> err;
/* Always display the message .*/
/* We coshould postpone this until after returning, if we chose .*/
(* Cinfo-> err-> output_message) (Cinfo );
/* Return control to the setjmp point */
Longjmp (myerr-> setjmp_buffer, 1 );
}
Bool image: load (const char * filename)
{
STD: String file (filename );
If (file. substr (file. Length ()-3, file. Length ())! = "Jpg ")
Return 0;
Struct performance_decompress_struct Cinfo;
Struct my_error_mgr Jerr;
File * infile;
Jsamparray buffer;
Int row_stride;
If (infile = fopen (file. c_str (), "rb") = NULL ){
Fprintf (stderr, "can't open % s/n", file. c_str ());
Return 0;
}
Cinfo. Err = maid (& Jerr. Pub );
Jerr. Pub. error_exit = my_error_exit;
If (setjmp (Jerr. setjmp_buffer )){
Pai_destroy_decompress (& Cinfo );
Fclose (infile );
Return 0;
}
Pai_create_decompress (& Cinfo );
Performance_stdio_src (& Cinfo, infile );
(Void) pai_read_header (& Cinfo, true );
(Void) pai_start_decompress (& Cinfo );
Row_stride = Cinfo. output_width * Cinfo. output_components;
Const int Component = Cinfo. output_components;
Buffer = (* Cinfo. mem-> alloc_sarray)
(J_common_ptr) & Cinfo, jpool_image, row_stride, 1 );
W = Cinfo. output_width;
H = Cinfo. output_height;
Create (W, H );
Int y = 0;
While (Cinfo. output_scanline <Cinfo. output_height ){
(Void) pai_read_scanlines (& Cinfo, buffer, 1 );
If (Component = 3)
{
For (INT I = 0; I <W; ++ I)
Set_pixel (I, h-y-1, (unsigned char) buffer [3 * I + 0], (unsigned char) buffer [3 * I + 1], (unsigned char) buffer [3 * I + 2]);
} Else if (Component = 1)
{
For (INT I = 0; I <W; ++ I)
Set_pixel (I, h-y-1, (unsigned char) buffer [3 * I], (unsigned char) buffer [3 * I], (unsigned char) buffer [3 * I]);
}
Y ++;
}
(Void) pai_finish_decompress (& Cinfo );
Pai_destroy_decompress (& Cinfo );
Fclose (infile );
Return 1;
}