Detailed description of libjpeg usage in Embedded Linux and libjpeg usage
1. The question is, what is libjpeg?
I certainly didn't know it at the beginning. Okay, So Baidu. Of course, I am not the first peach-picking person. Someone has already written it.
Libjpeg is a library fully written in C language. It includes the widely used JPEG decoding, JPEG encoding, and other JPEG functions. First, let's get to know about it. In the vernacular, he is a library file that uses the C language to implement image decoding and encoding. This is what I understand. It may be wrong.
2. Use libjpeg in Linux
Use is the final principle. Well, it's time to start using libjpeg...
If you want to display an image on the Linux development board LCD, how can you display it? When the LCD displays data, the LCD controller reads RGB data in the display memory for display. If we can use a jpg image to display it, the answer is no, because the jpg image is a compressed file, the original RGB data needs to be decompressed into the video memory, so that the LCD controller can read the original RGB data for second-point display of the LCD.
The question is, how can we decompress the jpg file? The libjpeg mentioned above is useful. Well, that is, libjpeg is used to decompress the jpg image. Of course, it can not only decompress the image, but also compress the image. A lot of work has been done...
The instructor provided some references to introduce libjpeg functions. Here I decompress images on the linux development board and display them on the LCD. For detailed usage, you can find one on the Internet or contact me...
Of course, the pressure procedures have been introduced in the documents provided, and Mr. Wei also gave a brief explanation.
Follow these steps:
1. Allocate and initialize a decompression struct;
2. Extract the source file;
3. Use pai_read_header to obtain jpg information;
4. Set decompression parameters, such as Zoom in or out;
5. Start decompression: pai_start_decompress;
6. Call jpeg_read_scanlines cyclically to decompress the package;
VII. pai_finish_decompress;
8. Release the decompression struct.
Okay, the general steps are available. The following code is available. The Code is as follows:
MyJpg. c
Int main (int argc, char ** argv)
{
Struct performance_decompress_struct cinfo;
Struct performance_error_mgr jerr;
FILE * infile;
Int row_stride;
Unsigned char * buffer;
/***** Step 1: allocate and initialize a decompression struct ****/
Cinfo. err = maid (& jerr );
Pai_create_decompress (& cinfo );
/* Step 2: Specify the source file **********/
If (infile = fopen (argv [1], "rb") = NULL ){
Fprintf (stderr, "can't open % s \ n", argv [1]);
Return-1;
}
Performance_stdio_src (& cinfo, infile );
/***** Step 3: Use javas_read_header to obtain jpg information *******/
Pai_read_header (& cinfo, TRUE );
/* Print the source information */
Printf ("image_width = % d \ n", cinfo. image_width );
Printf ("image_height = % d \ n", cinfo. image_height );
Printf ("num_components = % d \ n", cinfo. num_components );
/* Step 4: Set the decompression parameters, for example, to zoom in or out ********/
& Cinfo. scale_num = xx;
& Cinfo. scale_denom = xx;
/***** Step 5: Start decompression: pai_start_decompress ******/
Pai_start_decompress (& cinfo );
// The Data Length of a row
Row_stride = cinfo. output_width * cinfo. output_components;
Buffer = malloc (row_stride );
/***** Step 6: cyclically call pai_read_scanlines to obtain extracted data in one row *****/
While (cinfo. output_scanline <cinfo. output_height)
{
(Void) pai_read_scanlines (& cinfo, & buffer, 1); // The buffer stores a row of data;
/***** Write the unzipped row of data to the LCD for display *******/
}
/***** Step 7: Release the decompression struct ***********/
Free (buffer );
Pai_finish_decompress (& cinfo );
Pai_destroy_decompress (& cinfo );
Return 0;
}
How to run the code? The libjpeg library is not installed on the Development Board at first. The first step is to install the Library first. Otherwise, an error will be reported during cross-compilation.
Step 1: Install and compile libjpeg
Tar xzf libjpeg-turbo-XXXX.tar.gz
Cd libjpeg-turbo-1.2.1. /configure -- prefix = /.. /libjpeg-turbo-xxx/tmp/-- host = arm-linux (/.. /libjpeg-turbo-xxx/tmp/directory of the decompressed path), -- host = arm-linux compiled code is used in arm-linux.
Make
Make install
Step 2: Cross-compile the MyJpg. c file
Arm-linux-gcc-o MyJpg. c-I/work/projects/13. libjpeg/libjpeg-turbo-1.2.1/tmp/include-L /.. /libjpeg-turbo-xxx/tmp/lib-ljpeg (/.. /libjpeg-turbo-xxx/tmp/lib is the decompressed path), (-L specifies the location of the Library)
Upload the compiled and lib files to the file system.
Cp MyJpg/work/nfs_root/MyJpgTest (/work/nfs_root/MyJpgTest is the Network File System Directory)
Cp libjpeg-turbo-xxx/tmp/lib/* so */work/nfs_root/MyJpgTest/lib/-d (-d to keep the original data unchanged)
You can put the compiled header files and library files into the toolchain to avoid entering such long commands during each compilation.
Then you can use arm-linux-gcc-o MyJpg. c-ljpeg to compile the file.
Step 3: run on the Development Board
Copy the selected image to the file system.