|
| static void | jpeg_error_catch_all_1 (j_common_ptr cinfo) |
| |
| static void | jpeg_error_catch_all_2 (j_common_ptr cinfo) |
| |
| static l_uint8 | jpeg_getc (j_decompress_ptr cinfo) |
| |
| static boolean | jpeg_comment_callback (j_decompress_ptr cinfo) |
| |
| PIX * | pixReadJpeg (const char *filename, l_int32 cmapflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint) |
| |
| PIX * | pixReadStreamJpeg (FILE *fp, l_int32 cmapflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint) |
| |
| l_ok | readHeaderJpeg (const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pspp, l_int32 *pycck, l_int32 *pcmyk) |
| |
| l_ok | freadHeaderJpeg (FILE *fp, l_int32 *pw, l_int32 *ph, l_int32 *pspp, l_int32 *pycck, l_int32 *pcmyk) |
| |
| l_int32 | fgetJpegResolution (FILE *fp, l_int32 *pxres, l_int32 *pyres) |
| |
| l_int32 | fgetJpegComment (FILE *fp, l_uint8 **pcomment) |
| |
| l_ok | pixWriteJpeg (const char *filename, PIX *pix, l_int32 quality, l_int32 progressive) |
| |
| l_ok | pixWriteStreamJpeg (FILE *fp, PIX *pixs, l_int32 quality, l_int32 progressive) |
| |
| PIX * | pixReadMemJpeg (const l_uint8 *data, size_t size, l_int32 cmflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint) |
| |
| l_ok | readHeaderMemJpeg (const l_uint8 *data, size_t size, l_int32 *pw, l_int32 *ph, l_int32 *pspp, l_int32 *pycck, l_int32 *pcmyk) |
| |
| l_ok | readResolutionMemJpeg (const l_uint8 *data, size_t size, l_int32 *pxres, l_int32 *pyres) |
| |
| l_ok | pixWriteMemJpeg (l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 quality, l_int32 progressive) |
| |
| l_ok | pixSetChromaSampling (PIX *pix, l_int32 sampling) |
| |
Read jpeg from file
PIX *pixReadJpeg() [special top level]
PIX *pixReadStreamJpeg()
Read jpeg metadata from file
l_int32 readHeaderJpeg()
l_int32 freadHeaderJpeg()
l_int32 fgetJpegResolution()
l_int32 fgetJpegComment()
Write jpeg to file
l_int32 pixWriteJpeg() [special top level]
l_int32 pixWriteStreamJpeg()
Read/write to memory
PIX *pixReadMemJpeg()
l_int32 readHeaderMemJpeg()
l_int32 readResolutionMemJpeg()
l_int32 pixWriteMemJpeg()
Setting special flag for chroma sampling on write
l_int32 pixSetChromaSampling()
Static system helpers
static void jpeg_error_catch_all_1()
static void jpeg_error_catch_all_2()
static l_uint8 jpeg_getc()
static l_int32 jpeg_comment_callback()
Documentation: libjpeg.doc can be found, along with all
source code, at ftp://ftp.uu.net/graphics/jpeg
Download and untar the file: jpegsrc.v6b.tar.gz
A good paper on jpeg can also be found there: wallace.ps.gz
The functions in libjpeg make it very simple to compress
and decompress images. On input (decompression from file),
3 component color images can be read into either an 8 bpp Pix
with a colormap or a 32 bpp Pix with RGB components. For output
(compression to file), all color Pix, whether 8 bpp with a
colormap or 32 bpp, are written compressed as a set of three
8 bpp (rgb) images.
Low-level error handling
------------------------
The default behavior of the jpeg library is to call exit.
This is often undesirable, and the caller should make the
decision when to abort a process. To prevent the jpeg library
from calling exit(), setjmp() has been inserted into all
readers and writers, and the cinfo struct has been set up so that
the low-level jpeg library will call a special error handler
that doesn't exit, instead of the default function error_exit().
To avoid race conditions and make these functions thread-safe in
the rare situation where calls to two threads are simultaneously
failing on bad jpegs, we insert a local copy of the jmp_buf struct
into the cinfo.client_data field, and use this on longjmp.
For extracting the jpeg comment, we have the added complication
that the client_data field must also return the jpeg comment,
and we use a different error handler.
How to avoid subsampling the chroma channels
--------------------------------------------
By default, the U,V (chroma) channels use 2x2 subsampling (aka 4.2.0).
Higher quality for color, using full resolution (4.4.4) for the chroma,
is obtained by setting a field in the pix before writing:
pixSetChromaSampling(pix, L_NO_CHROMA_SAMPLING_JPEG);
The field can be reset for default 4.2.0 subsampling with
pixSetChromaSampling(pix, 0);
How to extract just the luminance channel in reading RGB
--------------------------------------------------------
For higher resolution and faster decoding of an RGB image, you
can extract just the 8 bpp luminance channel, using pixReadJpeg(),
where you use L_JPEG_READ_LUMINANCE for the hint arg.
How to continue to read if the data is corrupted
------------------------------------------------
By default, if data is corrupted we make every effort to fail
to return a pix. (Failure is not always possible with bad
data, because in some situations, such as during arithmetic
decoding, the low-level jpeg library will not abort or raise
a warning.) To attempt to ignore warnings and get a pix when data
is corrupted, use L_JPEG_CONTINUE_WITH_BAD_DATA in the hint arg.
Compressing to memory and decompressing from memory
---------------------------------------------------
On systems like Windows without fmemopen() and open_memstream(),
we write data to a temp file and read it back for operations
between pix and compressed-data, such as pixReadMemJpeg() and
pixWriteMemJpeg().
Definition in file jpegio.c.
| PIX * pixReadJpeg |
( |
const char * | filename, |
|
|
l_int32 | cmapflag, |
|
|
l_int32 | reduction, |
|
|
l_int32 * | pnwarn, |
|
|
l_int32 | hint ) |
pixReadJpeg()
- Parameters
-
| [in] | filename | |
| [in] | cmapflag | 0 for no colormap in returned pix; 1 to return an 8 bpp cmapped pix if spp = 3 or 4 |
| [in] | reduction | scaling factor: 1, 2, 4 or 8 |
| [out] | pnwarn | [optional] number of warnings about corrupted data |
| [in] | hint | a bitwise OR of L_JPEG_* values; 0 for default |
- Returns
- pix, or NULL on error
Notes:
(1) This is a special function for reading jpeg files.
(2) Use this if you want the jpeg library to create
an 8 bpp colormapped image.
(3) Images reduced by factors of 2, 4 or 8 can be returned
significantly faster than full resolution images.
(4) If the jpeg data is bad, depending on the severity of the
data corruption one of two things will happen:
(a) 0 or more warnings are generated, or
(b) the library will immediately attempt to exit. This is
caught by our error handler and no pix will be returned.
If data corruption causes a warning, the default action
is to abort the read. The reason is that malformed jpeg
data sequences exist that prevent termination of the read.
To allow the decoding to continue after corrupted data is
encountered, include L_JPEG_CONTINUE_WITH_BAD_DATA in hint.
(5) The possible hint values are given in the enum in imageio.h:
* L_JPEG_READ_LUMINANCE
* L_JPEG_CONTINUE_WITH_BAD_DATA
Default (0) is to do neither, and to fail on warning of data
corruption.
Definition at line 209 of file jpegio.c.
References pixReadStreamJpeg().
| l_ok pixWriteStreamJpeg |
( |
FILE * | fp, |
|
|
PIX * | pixs, |
|
|
l_int32 | quality, |
|
|
l_int32 | progressive ) |
pixWriteStreamJpeg()
- Parameters
-
| [in] | fp | file stream |
| [in] | pixs | any depth; cmap is OK |
| [in] | quality | 1 - 100; 75 is default value; 0 is also default |
| [in] | progressive | 0 for baseline sequential; 1 for progressive |
- Returns
- 0 if OK, 1 on error
Notes:
(1) Progressive encoding gives better compression, at the
expense of slower encoding and decoding.
(2) Standard chroma subsampling is 2x2 on both the U and V
channels. For highest quality, use no subsampling; this
option is set by pixSetChromaSampling(pix, 0).
(3) The only valid pixel depths in leptonica are 1, 2, 4, 8, 16
and 32 bpp. However, it is possible, and in some cases desirable,
to write out a jpeg file using an rgb pix that has 24 bpp.
This can be created by appending the raster data for a 24 bpp
image (with proper scanline padding) directly to a 24 bpp
pix that was created without a data array.
(4) There are two compression paths in this function:
* Grayscale image, no colormap: compress as 8 bpp image.
* rgb full color image: copy each line into the color
line buffer, and compress as three 8 bpp images.
(5) Under the covers, the jpeg library transforms rgb to a
luminance-chromaticity triple, each component of which is
also 8 bits, and compresses that. It uses 2 Huffman tables,
a higher resolution one (with more quantization levels)
for luminosity and a lower resolution one for the chromas.
Definition at line 791 of file jpegio.c.
References COLOR_BLUE, COLOR_GREEN, COLOR_RED, GET_DATA_BYTE, jpeg_error_catch_all_1(), L_NO_CHROMA_SAMPLING_JPEG, REMOVE_CMAP_BASED_ON_SRC, and Pix::special.
Referenced by pixWriteJpeg(), and pixWriteMemJpeg().