快速jpeg压缩与解压,libjpeg库的使用

一、压缩大致步骤:
1、分配和初始化一个JPEG压缩对象
2、指定压缩数据的目标(例如,一个文件)
3、设置压缩参数,包括图像的大小及色彩
4、jpeg_start_compress(...);
5、 while (scan lines remain to be written)
jpeg_write_scanlines(...);
jpeg_finish_compress(...);
6、释放JPEG压缩对象

二、解压大致步骤:
1、分配和初始化一个JPEG解压对象
2、指定压缩数据源(例如,一个文件)
3、调用jpeg_read_header()来获取图像信息
4、设置解压参数
5、jpeg_start_decompress(...);
6、 while (scan lines remain to be read)
jpeg_read_scanlines(...);
jpeg_finish_decompress(...);
7、释放JPEG解压对象


三、JPEG库没有静态变量:所有状态都保存在压缩/解压缩对象中。因此,它可以同时使用多个JPEG对象处理多个压缩/解压操作。

四、请注意,RGB图像是每像素有三个数据,灰度图像只有一个。很多人都没有没有注意到这一点,结果发现他们的程序不能处理灰度JPEG。

五、使用默认的异常处理时,步骤如下:

先初始化异常结构(jpeg_error_mgr),将指针保存在err变量中,然后再调用jpeg_create_compress()初始化压缩对象:

struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
...
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);


==============================================================================================================

Outline of typical usage
------------------------

The rough outline of a JPEG compression operation is:

Allocate and initialize a JPEG compression object
Specify the destination for the compressed data (eg, a file)
Set parameters for compression, including image size & colorspace
jpeg_start_compress(...);
while (scan lines remain to be written)
jpeg_write_scanlines(...);
jpeg_finish_compress(...);
Release the JPEG compression object

A JPEG compression object holds parameters and working state for the JPEG
library. We make creation/destruction of the object separate from starting
or finishing compression of an image; the same object can be re-used for a
series of image compression operations. This makes it easy to re-use the
same parameter settings for a sequence of images. Re-use of a JPEG object
also has important implications for processing abbreviated JPEG datastreams,
as discussed later.

The image data to be compressed is supplied to jpeg_write_scanlines() from
in-memory buffers. If the application is doing file-to-file compression,
reading image data from the source file is the application's responsibility.
The library emits compressed data by calling a "data destination manager",
which typically will write the data into a file; but the application can
provide its own destination manager to do something else.

Similarly, the rough outline of a JPEG decompression operation is:

Allocate and initialize a JPEG decompression object
Specify the source of the compressed data (eg, a file)
Call jpeg_read_header() to obtain image info
Set parameters for decompression
jpeg_start_decompress(...);
while (scan lines remain to be read)
jpeg_read_scanlines(...);
jpeg_finish_decompress(...);
Release the JPEG decompression object

This is comparable to the compression outline except that reading the
datastream header is a separate step. This is helpful because information
about the image's size, colorspace, etc is available when the application
selects decompression parameters. For example, the application can choose an
output scaling ratio that will fit the image into the available screen size.

The decompression library obtains compressed data by calling a data source
manager, which typically will read the data from a file; but other behaviors
can be obtained with a custom source manager. Decompressed data is delivered
into in-memory buffers passed to jpeg_read_scanlines().

It is possible to abort an incomplete compression or decompression operation
by calling jpeg_abort(); or, if you do not need to retain the JPEG object,
simply release it by calling jpeg_destroy().

JPEG compression and decompression objects are two separate struct types.
However, they share some common fields, and certain routines such as
jpeg_destroy() can work on either type of object.

The JPEG library has no static variables: all state is in the compression
or decompression object. Therefore it is possible to process multiple
compression and decompression operations concurrently, using multiple JPEG
objects.

Both compression and decompression can be done in an incremental memory-to-
memory fashion, if suitable source/destination managers are used. See the
section on "I/O suspension" for more details.


你可能感兴趣的:(快速jpeg压缩与解压,libjpeg库的使用)