Leptonica 1.68
C Image Processing Library

jpegio.c File Reference

Read/write jpeg format from/to file and memory. More...

#include <string.h>
#include "allheaders.h"
#include <setjmp.h>
#include "jpeglib.h"

Go to the source code of this file.

Defines

#define DEBUG_INFO   0

Functions

static void jpeg_error_do_not_exit (j_common_ptr cinfo)
static l_uint8 jpeg_getc (j_decompress_ptr cinfo)
static boolean jpeg_comment_callback (j_decompress_ptr cinfo)
static l_int32 extractJpegHeaderDataFallback (const void *data, size_t nbytes, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp)
static l_int32 locateJpegImageParameters (l_uint8 *, size_t, l_int32 *)
static l_int32 getNextJpegMarker (l_uint8 *, size_t, l_int32 *)
static l_int32 getTwoByteParameter (l_uint8 *, l_int32)
PIXpixReadJpeg (const char *filename, l_int32 cmflag, l_int32 reduction, l_int32 *pnwarn)
PIXpixReadStreamJpeg (FILE *fp, l_int32 cmflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint)
l_int32 readHeaderJpeg (const char *filename, l_int32 *pw, l_int32 *ph, l_int32 *pspp, l_int32 *pycck, l_int32 *pcmyk)
l_int32 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 pixWriteJpeg (const char *filename, PIX *pix, l_int32 quality, l_int32 progressive)
l_int32 pixWriteStreamJpeg (FILE *fp, PIX *pix, l_int32 quality, l_int32 progressive)
PIXpixReadMemJpeg (const l_uint8 *cdata, size_t size, l_int32 cmflag, l_int32 reduction, l_int32 *pnwarn, l_int32 hint)
l_int32 readHeaderMemJpeg (const l_uint8 *cdata, size_t size, l_int32 *pw, l_int32 *ph, l_int32 *pspp, l_int32 *pycck, l_int32 *pcmyk)
l_int32 pixWriteMemJpeg (l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 quality, l_int32 progressive)
void l_jpegSetNoChromaSampling (l_int32 flag)
l_int32 extractJpegDataFromFile (const char *filein, l_uint8 **pdata, size_t *pnbytes, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp)
l_int32 extractJpegDataFromArray (const void *data, size_t nbytes, l_int32 *pw, l_int32 *ph, l_int32 *pbps, l_int32 *pspp)

Variables

static jmp_buf jpeg_jmpbuf
static l_int32 var_JPEG_NO_CHROMA_SAMPLING = 0

Detailed Description

Read/write jpeg format from/to file and memory.

  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()

  Write jpeg to file
        l_int32          pixWriteJpeg()  [ special top level ]
        l_int32          pixWriteStreamJpeg()

  Read/write to memory
        PIX             *pixReadMemJpeg()
        l_int32          readHeaderMemJpeg()
        l_int32          pixWriteMemJpeg()

  Setting special flag(s)
        void             l_jpegSetNoChromaSampling()

  Static system helpers
        static void      jpeg_error_do_not_exit()
        static l_uint8   jpeg_getc()
        static l_int32   jpeg_comment_callback()

  Extraction of jpeg header information by parsing
        l_int32          extractJpegDataFromFile()
        l_int32          extractJpegDataFromArray()
        static l_int32   extractJpegHeaderDataFallback()
        static l_int32   locateJpegImageParameters()
        static l_int32   getNextJpegMarker()
        static l_int32   getTwoByteParameter()

  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.

  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.  So I inserted setjmp(s)
  in the reader and writer, wrote a static error handler that
  does not exit, and set up the cinfo structure so that the
  low-level jpeg library will call this error handler instead
  of the default function error_exit().

  There is a special flag for not subsampling the U,V (chroma)
  channels.  This gives higher quality for the color, which is
  important for some situations.  The standard subsampling is
  2x2 on both channels.
    var_JPEG_NO_CHROMA_SAMPLING: default is 0 (false)
  This is set with l_jpegSetNoChromaSampling().

Definition in file jpegio.c.


Define Documentation

#define DEBUG_INFO   0

Definition at line 127 of file jpegio.c.


Function Documentation

static void jpeg_error_do_not_exit ( j_common_ptr  cinfo) [static]

Definition at line 1088 of file jpegio.c.

References jpeg_jmpbuf.

Referenced by fgetJpegResolution(), freadHeaderJpeg(), pixReadStreamJpeg(), and pixWriteStreamJpeg().

static l_uint8 jpeg_getc ( j_decompress_ptr  cinfo) [static]

Definition at line 1098 of file jpegio.c.

Referenced by jpeg_comment_callback().

static boolean jpeg_comment_callback ( j_decompress_ptr  cinfo) [static]

Definition at line 1117 of file jpegio.c.

References FREE, jpeg_getc(), and MALLOC.

Referenced by pixReadStreamJpeg().

static l_int32 extractJpegHeaderDataFallback ( const void *  data,
size_t  nbytes,
l_int32 pw,
l_int32 ph,
l_int32 pbps,
l_int32 pspp 
) [static]

extractJpegHeaderDataFallback()

Input: data (binary data consisting of the entire jpeg file) nbytes (size of binary data) &w (<optional return>=""> image width) &h (<optional return>=""> image height) &bps (<optional return>=""> bits/sample; should be 8) &spp (<optional return>=""> samples/pixel; should be 1 or 3) Return: 0 if OK, 1 on error

Notes: (1) This gets the header data by uncompressing the jpeg data into a pix. It can be used when header parsing fails. (2) This cannot distinguish between 3 and 4 spp, so it returns 3.

Definition at line 1297 of file jpegio.c.

References ERROR_INT, NULL, pixDestroy(), pixGetDimensions(), pixReadMemJpeg(), and PROCNAME.

Referenced by extractJpegDataFromArray().

static l_int32 locateJpegImageParameters ( l_uint8 inarray,
size_t  size,
l_int32 pindex 
) [static]

Definition at line 1372 of file jpegio.c.

References ERROR_INT, getNextJpegMarker(), getTwoByteParameter(), and PROCNAME.

Referenced by extractJpegDataFromArray().

static l_int32 getNextJpegMarker ( l_uint8 array,
size_t  size,
l_int32 pindex 
) [static]

Definition at line 1467 of file jpegio.c.

References ERROR_INT, and PROCNAME.

Referenced by locateJpegImageParameters().

static l_int32 getTwoByteParameter ( l_uint8 array,
l_int32  index 
) [static]

Definition at line 1504 of file jpegio.c.

Referenced by extractJpegDataFromArray(), and locateJpegImageParameters().

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

pixReadJpeg()

Input: filename colormap flag (0 means return RGB image if color; 1 means create colormap and return 8 bpp palette image if color) reduction (scaling factor: 1, 2, 4 or 8) &pnwarn (<optional return>=""> number of warnings about corrupted data) Return: pix, or null on error

Images reduced by factors of 2, 4 or 8 can be returned significantly faster than full resolution images.

The jpeg library will return warnings (or exit) if the jpeg data is bad. Use this function if you want the jpeg library to create an 8 bpp palette image, or to tell if the jpeg data has been corrupted. For corrupt jpeg data, there are two possible outcomes: (1) a damaged pix will be returned, along with a nonzero number of warnings, or (2) for sufficiently serious problems, the library will attempt to exit (caught by our error handler) and no pix will be returned.

Definition at line 160 of file jpegio.c.

References ERROR_PTR, fopenReadStream(), NULL, JbData::pix, pixReadStreamJpeg(), and PROCNAME.

Referenced by main().

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

pixReadStreamJpeg()

Input: stream colormap flag (0 means return RGB image if color; 1 means create colormap and return 8 bpp palette image if color) reduction (scaling factor: 1, 2, 4 or 8) &pnwarn (<optional return>=""> number of warnings) hint: (a bitwise OR of L_HINT_* values); use 0 for no hints Return: pix, or null on error

Usage: see pixReadJpeg()

Definition at line 205 of file jpegio.c.

References CALLOC, COLOR_BLUE, COLOR_GREEN, COLOR_RED, composeRGBPixel(), ERROR_PTR, FALSE, FREE, JbData::h, jpeg_comment_callback(), jpeg_error_do_not_exit(), jpeg_jmpbuf, L_HINT_GRAY, L_MAX, L_MIN, NULL, JbData::pix, pixcmapAddColor(), pixcmapCreate(), pixCreate(), pixDestroy(), pixGetData(), pixGetWpl(), pixSetColormap(), pixSetText(), pixSetXRes(), pixSetYRes(), PROCNAME, SET_DATA_BYTE, TRUE, and JbData::w.

Referenced by pixReadJpeg(), pixReadMemJpeg(), and pixReadStream().

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

readHeaderJpeg()

Input: filename &w (<optional return>="">) &h (<optional return>="">) &spp (<optional return>="">, samples/pixel) &ycck (<optional return>="">, 1 if ycck color space; 0 otherwise) &cmyk (<optional return>="">, 1 if cmyk color space; 0 otherwise) Return: 0 if OK, 1 on error

Definition at line 411 of file jpegio.c.

References ERROR_INT, fopenReadStream(), freadHeaderJpeg(), NULL, and PROCNAME.

Referenced by pixReadHeader().

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

freadHeaderJpeg()

Input: stream &w (<optional return>="">) &h (<optional return>="">) &spp (<optional return>="">, samples/pixel) &ycck (<optional return>="">, 1 if ycck color space; 0 otherwise) &cmyk (<optional return>="">, 1 if cmyk color space; 0 otherwise) Return: 0 if OK, 1 on error

Definition at line 447 of file jpegio.c.

References ERROR_INT, jpeg_error_do_not_exit(), jpeg_jmpbuf, PROCNAME, and TRUE.

Referenced by l_generateJpegData(), and readHeaderJpeg().

l_int32 fgetJpegResolution ( FILE *  fp,
l_int32 pxres,
l_int32 pyres 
)

Definition at line 504 of file jpegio.c.

References ERROR_INT, jpeg_error_do_not_exit(), jpeg_jmpbuf, PROCNAME, and TRUE.

Referenced by l_generateJpegData().

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

pixWriteJpeg()

Input: filename pix quality (1 - 100; 75 is default) progressive (0 for baseline sequential; 1 for progressive) Return: 0 if OK; 1 on error

Definition at line 559 of file jpegio.c.

References ERROR_INT, fopenWriteStream(), NULL, pixWriteStreamJpeg(), and PROCNAME.

Referenced by main(), pixGenerateJpegData(), pixWriteImpliedFormat(), and pixWriteMemJpeg().

l_int32 pixWriteStreamJpeg ( FILE *  fp,
PIX pix,
l_int32  quality,
l_int32  progressive 
)

pixWriteStreamJpeg()

Input: stream pix (8 or 32 bpp) quality (1 - 100; 75 is default value; 0 is also default) progressive (0 for baseline sequential; 1 for progressive) Return: 0 if OK, 1 on error

Notes: (1) Under the covers, the library transforms rgb to a luminence-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. (2) Progressive encoding gives better compression, at the expense of slower encoding and decoding. (3) Standard chroma subsampling is 2x2 on both the U and V channels. For highest quality, use no subsampling. This option is set by l_jpegSetNoChromaSampling(1). (4) There are three possibilities: * 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. * 8 bpp colormapped image: convert each line to three 8 bpp line images in the color line buffer, and compress as three 8 bpp images. (5) 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. See note in pixWriteStreamPng() for an example.

Definition at line 622 of file jpegio.c.

References CALLOC, COLOR_BLUE, COLOR_GREEN, COLOR_RED, ERROR_INT, FALSE, FREE, GET_DATA_BYTE, jpeg_error_do_not_exit(), jpeg_jmpbuf, NULL, pixcmapToArrays(), pixGetColormap(), pixGetData(), pixGetDimensions(), pixGetText(), pixGetWpl(), pixGetXRes(), pixGetYRes(), PROCNAME, TRUE, and var_JPEG_NO_CHROMA_SAMPLING.

Referenced by pixWriteJpeg(), and pixWriteStream().

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

pixReadMemJpeg()

Input: cdata (const; jpeg-encoded) size (of data) colormap flag (0 means return RGB image if color; 1 means create colormap and return 8 bpp palette image if color) reduction (scaling factor: 1, 2, 4 or 8) &pnwarn (<optional return>=""> number of warnings) hint (bitwise OR of L_HINT_* values; use 0 for no hint) Return: pix, or null on error

Notes: (1) We are cheating here -- writing the data out to file in jpeg format and then reading it back.

Definition at line 946 of file jpegio.c.

References ERROR_PTR, fopenReadStream(), FREE, genTempFilename(), l_binaryWrite(), NULL, pixReadStreamJpeg(), and PROCNAME.

Referenced by extractJpegHeaderDataFallback(), and pixReadMem().

l_int32 readHeaderMemJpeg ( const l_uint8 cdata,
size_t  size,
l_int32 pw,
l_int32 ph,
l_int32 pspp,
l_int32 pycck,
l_int32 pcmyk 
)

readHeaderMemJpeg()

Input: cdata (const; jpeg-encoded) size (of data) &w (<optional return>="">) &h (<optional return>="">) &spp (<optional return>="">, samples/pixel) &ycck (<optional return>="">, 1 if ycck color space; 0 otherwise) &cmyk (<optional return>="">, 1 if cmyk color space; 0 otherwise) Return: 0 if OK, 1 on error

Notes: (1) Parse the jpeg string without without jpeg library calls.

Definition at line 994 of file jpegio.c.

References ERROR_INT, extractJpegDataFromArray(), NULL, and PROCNAME.

Referenced by pixReadHeaderMem().

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

pixWriteMemJpeg()

Input: &data (<return> data of jpeg compressed image) &size (<return> size of returned data) pix quality (1 - 100; 75 is default value; 0 is also default) progressive (0 for baseline sequential; 1 for progressive) Return: 0 if OK, 1 on error

Notes: (1) We are cheating here -- writing the data out to file in jpeg format and then reading it back.

Definition at line 1028 of file jpegio.c.

References ERROR_INT, FREE, genTempFilename(), l_binaryRead(), pixWriteJpeg(), and PROCNAME.

Referenced by pixWriteMem().

void l_jpegSetNoChromaSampling ( l_int32  flag)

l_jpegSetNoChromaSampling()

Input: flag (0 for standard 2x2 chroma subsampling) 1 for no chroma subsampling (high quality)) Return: void

Definition at line 1074 of file jpegio.c.

References var_JPEG_NO_CHROMA_SAMPLING.

Referenced by main().

l_int32 extractJpegDataFromFile ( const char *  filein,
l_uint8 **  pdata,
size_t *  pnbytes,
l_int32 pw,
l_int32 ph,
l_int32 pbps,
l_int32 pspp 
)

extractJpegDataFromFile()

Input: filein &data (<return> binary data consisting of the entire jpeg file) &nbytes (<return> size of binary data) &w (<optional return>=""> image width) &h (<optional return>=""> image height) &bps (<optional return>=""> bits/sample; should be 8) &spp (<optional return>=""> samples/pixel; should be 1 or 3) Return: 0 if OK, 1 on error

Definition at line 1162 of file jpegio.c.

References ERROR_INT, extractJpegDataFromArray(), findFileFormat(), FREE, IFF_JFIF_JPEG, l_binaryRead(), NULL, and PROCNAME.

l_int32 extractJpegDataFromArray ( const void *  data,
size_t  nbytes,
l_int32 pw,
l_int32 ph,
l_int32 pbps,
l_int32 pspp 
)

extractJpegDataFromArray()

Input: data (binary data consisting of the entire jpeg file) nbytes (size of binary data) &w (<optional return>=""> image width) &h (<optional return>=""> image height) &bps (<optional return>=""> bits/sample; should be 8) &spp (<optional return>=""> samples/pixel; should be 1, 3 or 4) Return: 0 if OK, 1 on error

Definition at line 1219 of file jpegio.c.

References ERROR_INT, extractJpegHeaderDataFallback(), getTwoByteParameter(), L_WARNING, locateJpegImageParameters(), and PROCNAME.

Referenced by extractJpegDataFromFile(), and readHeaderMemJpeg().


Variable Documentation

Definition at line 124 of file jpegio.c.

Referenced by l_jpegSetNoChromaSampling(), and pixWriteStreamJpeg().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines