Leptonica  1.82.0
Image processing and image analysis suite
jpegio.c File Reference
#include <string.h>
#include "allheaders.h"
#include <setjmp.h>
#include "jpeglib.h"

Go to the source code of this file.

Data Structures

struct  callback_data
 

Macros

#define DEBUG_INFO   0
 

Functions

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)
 
PIXpixReadJpeg (const char *filename, l_int32 cmapflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint)
 
PIXpixReadStreamJpeg (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)
 
PIXpixReadMemJpeg (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)
 

Detailed Description


   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.

Function Documentation

◆ freadHeaderJpeg()

l_ok freadHeaderJpeg ( FILE *  fp,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pspp,
l_int32 *  pycck,
l_int32 *  pcmyk 
)

freadHeaderJpeg()

Parameters
[in]fpfile stream
[out]pw[optional]
[out]ph[optional]
[out]pspp[optional] samples/pixel
[out]pycck[optional] 1 if ycck color space; 0 otherwise
[out]pcmyk[optional] 1 if cmyk color space; 0 otherwise
Returns
0 if OK, 1 on error

Definition at line 551 of file jpegio.c.

References jpeg_error_catch_all_1().

Referenced by readHeaderJpeg(), and readHeaderMemJpeg().

◆ jpeg_comment_callback()

static boolean jpeg_comment_callback ( j_decompress_ptr  cinfo)
static

jpeg_comment_callback()

Notes: (1) This is used to read the jpeg comment (JPEG_COM). See the note above the declaration for why it returns a "boolean".

Definition at line 1275 of file jpegio.c.

◆ jpeg_error_catch_all_1()

static void jpeg_error_catch_all_1 ( j_common_ptr  cinfo)
static

jpeg_error_catch_all_1()

Notes: (1) The default jpeg error_exit() kills the process, but we never want a call to leptonica to kill a process. If you do want this behavior, remove the calls to these error handlers. (2) This is used where cinfo->client_data holds only jmpbuf.

Definition at line 1223 of file jpegio.c.

Referenced by freadHeaderJpeg(), and pixReadStreamJpeg().

◆ jpeg_error_catch_all_2()

static void jpeg_error_catch_all_2 ( j_common_ptr  cinfo)
static

jpeg_error_catch_all_2()

Notes: (1) This is used where cinfo->client_data needs to hold both the jmpbuf and the jpeg comment data. (2) On error, the comment data will be freed by the caller.

Definition at line 1240 of file jpegio.c.

◆ pixReadJpeg()

PIX* pixReadJpeg ( const char *  filename,
l_int32  cmapflag,
l_int32  reduction,
l_int32 *  pnwarn,
l_int32  hint 
)

pixReadJpeg()

Parameters
[in]filename
[in]cmapflag0 for no colormap in returned pix; 1 to return an 8 bpp cmapped pix if spp = 3 or 4
[in]reductionscaling factor: 1, 2, 4 or 8
[out]pnwarn[optional] number of warnings about corrupted data
[in]hinta 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 208 of file jpegio.c.

References fopenReadStream(), and pixReadStreamJpeg().

◆ pixReadMemJpeg()

PIX* pixReadMemJpeg ( const l_uint8 *  data,
size_t  size,
l_int32  cmflag,
l_int32  reduction,
l_int32 *  pnwarn,
l_int32  hint 
)

pixReadMemJpeg()

Parameters
[in]dataconst; jpeg-encoded
[in]sizeof data
[in]cmflagcolormap flag 0 means return RGB image if color; 1 means create a colormap and return an 8 bpp colormapped image if color
[in]reductionscaling factor: 1, 2, 4 or 8
[out]pnwarn[optional] number of warnings
[in]hinta bitwise OR of L_JPEG_* values; 0 for default
Returns
pix, or NULL on error
Notes:
     (1) The size byte of data must be a null character.
     (2) The only hint flag so far is L_JPEG_READ_LUMINANCE,
         given in the enum in imageio.h.
     (3) See pixReadJpeg() for usage.

Definition at line 1001 of file jpegio.c.

References fopenReadFromMemory(), and pixReadStreamJpeg().

◆ pixReadStreamJpeg()

PIX* pixReadStreamJpeg ( FILE *  fp,
l_int32  cmapflag,
l_int32  reduction,
l_int32 *  pnwarn,
l_int32  hint 
)

pixReadStreamJpeg()

Parameters
[in]fpfile stream
[in]cmapflag0 for no colormap in returned pix; 1 to return an 8 bpp cmapped pix if spp = 3 or 4
[in]reductionscaling factor: 1, 2, 4 or 8
[out]pnwarn[optional] number of warnings
[in]hinta bitwise OR of L_JPEG_* values; 0 for default
Returns
pix, or NULL on error
Notes:
     (1) For usage, see pixReadJpeg().
     (2) The jpeg comment, if it exists, is not stored in the pix.

Definition at line 264 of file jpegio.c.

References jpeg_error_catch_all_1(), L_JPEG_READ_LUMINANCE, pixCreate(), and pixDestroy().

Referenced by pixReadJpeg(), and pixReadMemJpeg().

◆ pixSetChromaSampling()

l_ok pixSetChromaSampling ( PIX pix,
l_int32  sampling 
)

pixSetChromaSampling()

Parameters
[in]pix
[in]sampling1 for subsampling; 0 for no subsampling
Returns
0 if OK, 1 on error
Notes:
     (1) The default is for 2x2 chroma subsampling because the files are
         considerably smaller and the appearance is typically satisfactory.
         To get full resolution output in the chroma channels for
         jpeg writing, call this with sampling == 0.

Definition at line 1195 of file jpegio.c.

◆ pixWriteJpeg()

l_ok pixWriteJpeg ( const char *  filename,
PIX pix,
l_int32  quality,
l_int32  progressive 
)

pixWriteJpeg()

Parameters
[in]filename
[in]pixany depth; cmap is OK
[in]quality1 - 100; 75 is default
[in]progressive0 for baseline sequential; 1 for progressive
Returns
0 if OK; 1 on error

Definition at line 741 of file jpegio.c.

References fopenWriteStream(), and pixWriteStreamJpeg().

◆ pixWriteMemJpeg()

l_ok pixWriteMemJpeg ( l_uint8 **  pdata,
size_t *  psize,
PIX pix,
l_int32  quality,
l_int32  progressive 
)

pixWriteMemJpeg()

Parameters
[out]pdatadata of jpeg compressed image
[out]psizesize of returned data
[in]pixany depth; cmap is OK
[in]quality1 - 100; 75 is default value; 0 is also default
[in]progressive0 for baseline sequential; 1 for progressive
Returns
0 if OK, 1 on error
Notes:
     (1) See pixWriteStreamJpeg() for usage.  This version writes to
         memory instead of to a file stream.

Definition at line 1131 of file jpegio.c.

References fopenWriteWinTempfile(), l_binaryReadStream(), and pixWriteStreamJpeg().

◆ pixWriteStreamJpeg()

l_ok pixWriteStreamJpeg ( FILE *  fp,
PIX pixs,
l_int32  quality,
l_int32  progressive 
)

pixWriteStreamJpeg()

Parameters
[in]fpfile stream
[in]pixsany depth; cmap is OK
[in]quality1 - 100; 75 is default value; 0 is also default
[in]progressive0 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 802 of file jpegio.c.

References pixGetDimensions().

Referenced by pixWriteJpeg(), and pixWriteMemJpeg().

◆ readHeaderJpeg()

l_ok readHeaderJpeg ( const char *  filename,
l_int32 *  pw,
l_int32 *  ph,
l_int32 *  pspp,
l_int32 *  pycck,
l_int32 *  pcmyk 
)

readHeaderJpeg()

Parameters
[in]filename
[out]pw[optional]
[out]ph[optional]
[out]pspp[optional] samples/pixel
[out]pycck[optional] 1 if ycck color space; 0 otherwise
[out]pcmyk[optional] 1 if cmyk color space; 0 otherwise
Returns
0 if OK, 1 on error

Definition at line 509 of file jpegio.c.

References fopenReadStream(), and freadHeaderJpeg().

Referenced by l_generateJpegData().

◆ readHeaderMemJpeg()

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 
)

readHeaderMemJpeg()

Parameters
[in]dataconst; jpeg-encoded
[in]sizeof data
[out]pw[optional] width
[out]ph[optional] height
[out]pspp[optional] samples/pixel
[out]pycck[optional] 1 if ycck color space; 0 otherwise
[out]pcmyk[optional] 1 if cmyk color space; 0 otherwise
Returns
0 if OK, 1 on error

Definition at line 1048 of file jpegio.c.

References fopenReadFromMemory(), and freadHeaderJpeg().

Referenced by l_generateJpegDataMem().

◆ readResolutionMemJpeg()

l_ok readResolutionMemJpeg ( const l_uint8 *  data,
size_t  size,
l_int32 *  pxres,
l_int32 *  pyres 
)

readResolutionMemJpeg()

Parameters
[in]dataconst; jpeg-encoded
[in]sizeof data
[out]pxres[optional]
[out]pyres[optional]
Returns
0 if OK, 1 on error

Definition at line 1089 of file jpegio.c.

References fopenReadFromMemory().

Referenced by l_generateJpegDataMem().