image compression with libjpeg
2017-09-27 13:13
239 查看
http://www.aaronmr.com/en/2010/03/test/
Working on the project I've seen in the need for compression to capture images with a webcam from the robot, to send to the client application to visualize what he sees in order to control the robot remotely. Today I was doing some uncompressed since all applications were running on the same machine and had no problems with the transmission of data and images.
Some research online on the libjpeg library of images I found some examples and I modified a bit for my needs, but then I will explain a simple example that lets you open a file. Jpg and compress it into another file. Jpg. Remember that a jpg file is a compressed image, if we open our program we must bear in mind that you will use it for other programs and even to compress it again, we must first uncompress it.
Code and example after the jump ->
At first glance does not seem very useful but for someone who begins and use it as an example to use and learn how to do this very well.
#include <stdio.h>
#include <jpeglib.h>
#include <stdlib.h>
/* we will be using this uninitialized pointer later to store raw, uncompressd image */
unsigned char *raw_image = NULL;
/* dimensions of the image we want to write */
int width = 640;
int height = 480;
int bytes_per_pixel = 3; /* or 1 for GRACYSCALE images */
int color_space = JCS_RGB; /* or JCS_GRAYSCALE for grayscale images */
/**
* read_jpeg_file Reads from a jpeg file on disk specified by filename and saves into the
* raw_image buffer in an uncompressed format.
*
* \returns positive integer if successful, -1 otherwise
* \param *filename char string specifying the file name to read from
*
*/
int read_jpeg_file( char *filename )
{
/* these are standard libjpeg structures for reading(decompression) */
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
/* libjpeg data structure for storing one row, that is, scanline of an image */
JSAMPROW row_pointer[1];
FILE *infile = fopen( filename, "rb" );
unsigned long location = 0;
int i = 0;
if ( !infile )
{
printf("Error opening jpeg file %s\n!", filename );
return -1;
}
/* here we set up the standard libjpeg error handler */
cinfo.err = jpeg_std_error( &jerr );
/* setup decompression process and source, then read JPEG header */
jpeg_create_decompress( &cinfo );
/* this makes the library read from infile */
jpeg_stdio_src( &cinfo, infile );
/* reading the image header which contains image information */
jpeg_read_header( &cinfo, TRUE );
/* Uncomment the following to output image information, if needed. */
/*--
printf( "JPEG File Information: \n" );
printf( "Image width and height: %d pixels and %d pixels.\n", cinfo.image_width, cinfo.image_height );
printf( "Color components per pixel: %d.\n", cinfo.num_components );
printf( "Color space: %d.\n", cinfo.jpeg_color_space );
--*/
/* Start decompression jpeg here */
jpeg_start_decompress( &cinfo );
/* allocate memory to hold the uncompressed image */
raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );
/* now actually read the jpeg into the raw buffer */
row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
/* read one scan line at a time */
while( cinfo.output_scanline < cinfo.image_height )
{
jpeg_read_scanlines( &cinfo, row_pointer, 1 );
for( i=0; i<cinfo.image_width*cinfo.num_components;i++)
raw_image[location++] = row_pointer[0][i];
}
/* wrap up decompression, destroy objects, free pointers and close open files */
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
free( row_pointer[0] );
fclose( infile );
/* yup, we succeeded! */
return 1;
}
/**
* write_jpeg_file Writes the raw image data stored in the raw_image buffer
* to a jpeg image with default compression and smoothing options in the file
* specified by *filename.
*
* \returns positive integer if successful, -1 otherwise
* \param *filename char string specifying the file name to save to
*
*/
int write_jpeg_file( char *filename )
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
/* this is a pointer to one row of image data */
JSAMPROW row_pointer[1];
FILE *outfile = fopen( filename, "wb" );
if ( !outfile )
{
printf("Error opening output jpeg file %s\n!", filename );
return -1;
}
cinfo.err = jpeg_std_error( &jerr );
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
/* Setting the parameters of the output file here */
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = bytes_per_pixel;
cinfo.in_color_space = color_space;
/* default compression parameters, we shouldn't be worried about these */
jpeg_set_defaults( &cinfo );
cinfo.num_components = 3;
//cinfo.data_precision = 4;
cinfo.dct_method = JDCT_FLOAT;
jpeg_set_quality(&cinfo, 15, TRUE);
/* Now do the compression .. */
jpeg_start_compress( &cinfo, TRUE );
/* like reading a file, this time write one row at a time */
while( cinfo.next_scanline < cinfo.image_height )
{
row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
jpeg_write_scanlines( &cinfo, row_pointer, 1 );
}
/* similar to read file, clean up after we're done compressing */
jpeg_finish_compress( &cinfo );
jpeg_destroy_compress( &cinfo );
fclose( outfile );
/* success code is 1! */
return 1;
}
int main()
{
char *infilename = "test.jpg", *outfilename = "test_out.jpg";
/* Try opening a jpeg*/
if( read_jpeg_file( infilename ) > 0 )
{
/* then copy it to another file */
if( write_jpeg_file( outfilename ) < 0 ) return -1;
}
else return -1;
return 0;
}
This example program makes a call to the "read_jpeg_file ()" that opens a file "test.jpg", decompresses it and put the result in a buffer (raw_image). Then a call to the "write_jpeg_file ()" which is responsible for compressing the image and store it in a file called test_out.jpg. "
To change the compression ratio we use this function:
jpeg_set_quality(&cinfo, 15, TRUE);
which we compressed to a quality image CINFO 15%, to vary the compression ratio modify the 2nd parameter, which accepts values are 0 .. 100.
Here is a jpg image and its result to a compression of 10.
![](http://www.aaronmr.com/wp-content/uploads/2010/03/test.jpg)
![](http://www.aaronmr.com/wp-content/uploads/2010/03/test_out.jpg)
where the image without compression takes 69 KB and 13 KB compressed, interesting result.
To compile this program we will do the following:
$ gcc jpeg_sample.c -o jpeg_sample -ljpeg
Source: http://www.cim.mcgill.ca/~junaed/libjpeg.php
What I do now is to introduce these functions just before sending the image and right after receiving the image in my final year project, thus hopefully get many more frames per second without the problem of wide band, and ire informed of the progress ...
Greetings and thanks for your attention.
Working on the project I've seen in the need for compression to capture images with a webcam from the robot, to send to the client application to visualize what he sees in order to control the robot remotely. Today I was doing some uncompressed since all applications were running on the same machine and had no problems with the transmission of data and images.
Some research online on the libjpeg library of images I found some examples and I modified a bit for my needs, but then I will explain a simple example that lets you open a file. Jpg and compress it into another file. Jpg. Remember that a jpg file is a compressed image, if we open our program we must bear in mind that you will use it for other programs and even to compress it again, we must first uncompress it.
Code and example after the jump ->
At first glance does not seem very useful but for someone who begins and use it as an example to use and learn how to do this very well.
#include <stdio.h>
#include <jpeglib.h>
#include <stdlib.h>
/* we will be using this uninitialized pointer later to store raw, uncompressd image */
unsigned char *raw_image = NULL;
/* dimensions of the image we want to write */
int width = 640;
int height = 480;
int bytes_per_pixel = 3; /* or 1 for GRACYSCALE images */
int color_space = JCS_RGB; /* or JCS_GRAYSCALE for grayscale images */
/**
* read_jpeg_file Reads from a jpeg file on disk specified by filename and saves into the
* raw_image buffer in an uncompressed format.
*
* \returns positive integer if successful, -1 otherwise
* \param *filename char string specifying the file name to read from
*
*/
int read_jpeg_file( char *filename )
{
/* these are standard libjpeg structures for reading(decompression) */
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
/* libjpeg data structure for storing one row, that is, scanline of an image */
JSAMPROW row_pointer[1];
FILE *infile = fopen( filename, "rb" );
unsigned long location = 0;
int i = 0;
if ( !infile )
{
printf("Error opening jpeg file %s\n!", filename );
return -1;
}
/* here we set up the standard libjpeg error handler */
cinfo.err = jpeg_std_error( &jerr );
/* setup decompression process and source, then read JPEG header */
jpeg_create_decompress( &cinfo );
/* this makes the library read from infile */
jpeg_stdio_src( &cinfo, infile );
/* reading the image header which contains image information */
jpeg_read_header( &cinfo, TRUE );
/* Uncomment the following to output image information, if needed. */
/*--
printf( "JPEG File Information: \n" );
printf( "Image width and height: %d pixels and %d pixels.\n", cinfo.image_width, cinfo.image_height );
printf( "Color components per pixel: %d.\n", cinfo.num_components );
printf( "Color space: %d.\n", cinfo.jpeg_color_space );
--*/
/* Start decompression jpeg here */
jpeg_start_decompress( &cinfo );
/* allocate memory to hold the uncompressed image */
raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );
/* now actually read the jpeg into the raw buffer */
row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
/* read one scan line at a time */
while( cinfo.output_scanline < cinfo.image_height )
{
jpeg_read_scanlines( &cinfo, row_pointer, 1 );
for( i=0; i<cinfo.image_width*cinfo.num_components;i++)
raw_image[location++] = row_pointer[0][i];
}
/* wrap up decompression, destroy objects, free pointers and close open files */
jpeg_finish_decompress( &cinfo );
jpeg_destroy_decompress( &cinfo );
free( row_pointer[0] );
fclose( infile );
/* yup, we succeeded! */
return 1;
}
/**
* write_jpeg_file Writes the raw image data stored in the raw_image buffer
* to a jpeg image with default compression and smoothing options in the file
* specified by *filename.
*
* \returns positive integer if successful, -1 otherwise
* \param *filename char string specifying the file name to save to
*
*/
int write_jpeg_file( char *filename )
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
/* this is a pointer to one row of image data */
JSAMPROW row_pointer[1];
FILE *outfile = fopen( filename, "wb" );
if ( !outfile )
{
printf("Error opening output jpeg file %s\n!", filename );
return -1;
}
cinfo.err = jpeg_std_error( &jerr );
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
/* Setting the parameters of the output file here */
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = bytes_per_pixel;
cinfo.in_color_space = color_space;
/* default compression parameters, we shouldn't be worried about these */
jpeg_set_defaults( &cinfo );
cinfo.num_components = 3;
//cinfo.data_precision = 4;
cinfo.dct_method = JDCT_FLOAT;
jpeg_set_quality(&cinfo, 15, TRUE);
/* Now do the compression .. */
jpeg_start_compress( &cinfo, TRUE );
/* like reading a file, this time write one row at a time */
while( cinfo.next_scanline < cinfo.image_height )
{
row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
jpeg_write_scanlines( &cinfo, row_pointer, 1 );
}
/* similar to read file, clean up after we're done compressing */
jpeg_finish_compress( &cinfo );
jpeg_destroy_compress( &cinfo );
fclose( outfile );
/* success code is 1! */
return 1;
}
int main()
{
char *infilename = "test.jpg", *outfilename = "test_out.jpg";
/* Try opening a jpeg*/
if( read_jpeg_file( infilename ) > 0 )
{
/* then copy it to another file */
if( write_jpeg_file( outfilename ) < 0 ) return -1;
}
else return -1;
return 0;
}
This example program makes a call to the "read_jpeg_file ()" that opens a file "test.jpg", decompresses it and put the result in a buffer (raw_image). Then a call to the "write_jpeg_file ()" which is responsible for compressing the image and store it in a file called test_out.jpg. "
To change the compression ratio we use this function:
jpeg_set_quality(&cinfo, 15, TRUE);
which we compressed to a quality image CINFO 15%, to vary the compression ratio modify the 2nd parameter, which accepts values are 0 .. 100.
Here is a jpg image and its result to a compression of 10.
![](http://www.aaronmr.com/wp-content/uploads/2010/03/test.jpg)
![](http://www.aaronmr.com/wp-content/uploads/2010/03/test_out.jpg)
where the image without compression takes 69 KB and 13 KB compressed, interesting result.
To compile this program we will do the following:
$ gcc jpeg_sample.c -o jpeg_sample -ljpeg
Source: http://www.cim.mcgill.ca/~junaed/libjpeg.php
What I do now is to introduce these functions just before sending the image and right after receiving the image in my final year project, thus hopefully get many more frames per second without the problem of wide band, and ire informed of the progress ...
Greetings and thanks for your attention.
相关文章推荐
- 【论文笔记2】图像压缩神经网络在Kodak数据集上首次超越JPEG——Full Resolution Image Compression with Recurrent Neural Networks
- php如何将webp格式图片转为jpeg(ImageMagick+libwebp)
- Study of JPEG Image Compression 图像压缩
- Error interpreting JPEG image file (Not a JPEG file: starts with 0x89 0x50)
- maven项目编译:程序包com.sun.image.codec.jpeg不存在 这个类文件的位置在jre/lib/rt.jar
- 【论文笔记1】RNN在图像压缩领域的运用——Variable Rate Image Compression with Recurrent Neural Networks
- javax.imageio.IIOException: Not a JPEG file: starts with 0x89 0x50
- initImageAppleJPEG:1512: falling back to libJPEG
- Installing ImageMagick with JPEG, TIFF and PNG Support
- Whatsapp like Image Compression in Android with demo Ambalika Saha June 28, 2013
- javax.imageio.IIOException: Not a JPEG file: starts with 0x47 0x49
- Android Studio导入Fresco项目编译报错unable to expand TAR 'imagepipeline\build\downloads\libjpeg-turbo-1.3.1.tar.gz'解决
- maven项目编译:程序包com.sun.image.codec.jpeg不存在 这个类文件的位置在jre/lib/rt.jar
- Compile libjpeg.dll with vc.net
- maven项目编译:程序包com.sun.image.codec.jpeg不存在 这个类文件的位置在jre/lib/rt.jar
- imageNamed 与 imageWithContentsOfFile的区别
- image/pjpeg和image/jpeg问题
- ImageNet Classification with Deep Convolutional Neural Networks 论文学习
- LINK : warning LNK4098: defaultlib "LIBC" conflicts with use of other libs; use /NODEFAULTLIB:library
- iOS extracts: Managing Memory with a Custom Image Loader