image compression with libjpeg
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.
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.
image compression with libjpeg的更多相关文章
- 让Mac OS X中的PHP支持GD
GD库已经是近乎于是现在主流PHP程序的标配了,所以也必须让Mac OS X中的PHP支持GD.在网上搜索了好多,最终按照这个方式成功实现,如何让Mac OS X支持PHP,请查看<让PHP跑在 ...
- Android仿微信高效压缩图片(libjpeg)
用过ios手机的同学应该很明显感觉到,ios拍照1M的图片要比安卓拍照排出来的5M的图片还要清晰.这是为什么呢? 这得了解android底层是如何对图片进行处理的. 当时谷歌开发Android的时候, ...
- MinGW64 how-to(内含编译openssl,libjpeg,libcurl等例子)
Index of contents Setting up the MinGW 64 environment Step 1) building libiconv Step 2) building lib ...
- 使用libjpeg.framework压缩UIImage
+(void)writeFile:(NSString *)filePath withQuality:(int)quality { //初始化图片参数 UIImage *image=[UIImage i ...
- Dynamic range compression
这段时间终于把手头的东西都搞完了,还剩下一个AEC这个模块,这个模块跟整个系统机制有很大关系,单独的模块意义不大. 另外,刚写完一个分类器,希望能大幅提升音乐流派分类的准确率. 下周正式开搞AEC,把 ...
- tomcat gzip compression not working for large js files
solution 1: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&quo ...
- libjpeg 编译makfile
一.准备 首先需要下载libjpeg库,网址在这里:http://www.ijg.org/ 如果不想编译就在这下载吧:http://pan.baidu.com/s/1hqeTDre 二.编译 1. 解 ...
- Motion images compression and restoration based on computer vision
This technique should apply to both normal video (consequtive sequences of pictures of real world) a ...
- 编译libjpeg库
最近在写车牌识别软件,需要用到BMP转成JPG的功能,自然就想到借助libjpeg来实现 OS: win7 64位 编译器: VS2008 1. 下载源代码下载地址:http://www.ijg.or ...
随机推荐
- 090-PHP数组过滤函数array_filter
<?php function odd($x){ //定义过滤偶数的函数 if($x%2==1) return TRUE; } function even($x){ //定义过滤奇数的函数 if( ...
- HDU 1542 线段树离散化+扫描线 平面面积计算
也是很久之前的题目,一直没做 做完之后觉得基本的离散化和扫描线还是不难的,由于本题要离散x点的坐标,最后要计算被覆盖的x轴上的长度,所以不能用普通的建树法,建树建到r-l==1的时候就停止,表示某段而 ...
- 【数据库】Function&Procedure&Package
Function/Procedure都是可独立编译并存储在数据库中的,区别是Function有返回值. Package则是数据和过程.函数的集合体. CREATE PROCEDURE dorepeat ...
- storm on yarn安装时 提交到yarn失败 failed
最近在部署storm on yarn ,部署参考文章 http://www.tuicool.com/articles/BFr2Yvhttp://blog.csdn.net/jiushuai/artic ...
- Aizu 2155 Magic Slayer 背包DP
这是上上次对抗赛的题目了 其实现在发现整个代码从头到尾,都是用了背包,怪我们背包没深入学好. 比赛的时候,聪哥提出的一种思路是,预处理一下,背包出 ALL攻击 和 single攻击的 血量对应的最小花 ...
- 一、VIP课程:互联网工程专题 01-Git基本概念与核心命令掌握
第一课:Git基本概念与核心命令掌握.docx 课程概要: GIT 体系概述 GIT 核心命令使用 GIT 底层原理 一.GIT体系概述 1.使用方式区别 从本地把文件推送远程服务,SVN只需要com ...
- 读写锁ReentrantReadWriteLock源代码浅析
1.简介 并发中常用的ReentrantLock,是一种典型的排他锁,这类锁在同一时刻只允许一个线程进行访问,实际上将并行操作变成了串行操作.在并发量大的业务中,其整体效率.吞吐量不能满足实现的需要. ...
- Linux 下 OpenCV3 安装
编译安装OpenCV3 从官网下载:http://opencv.org/releases.html 选择一个较新版本的opencv3.X,下载source源代码 下载之后解压,并cd到该文件夹进行编译 ...
- [ACTF2020 新生赛]Include
0x00 知识点 本地文件包含 ?file=php://filter/read/convert.base64-encode/resource=index.php ?file=php://filter/ ...
- SQL基础之JDBC、连接池
JDBC JDBC四个核心对象 这几个类都是在java.sql包中 DriverManager(类): 数据库驱动管理类.这个类的作用:1)注册驱动; 2)创建java代码和数据库之间的连接,即获取C ...