Blame gtkmm-osx/jpeg-6b/example.c

darco 56a656
/*
darco 56a656
 * example.c
darco 56a656
 *
darco 56a656
 * This file illustrates how to use the IJG code as a subroutine library
darco 56a656
 * to read or write JPEG image files.  You should look at this code in
darco 56a656
 * conjunction with the documentation file libjpeg.doc.
darco 56a656
 *
darco 56a656
 * This code will not do anything useful as-is, but it may be helpful as a
darco 56a656
 * skeleton for constructing routines that call the JPEG library.  
darco 56a656
 *
darco 56a656
 * We present these routines in the same coding style used in the JPEG code
darco 56a656
 * (ANSI function definitions, etc); but you are of course free to code your
darco 56a656
 * routines in a different style if you prefer.
darco 56a656
 */
darco 56a656
darco 56a656
#include <stdio.h></stdio.h>
darco 56a656
darco 56a656
/*
darco 56a656
 * Include file for users of JPEG library.
darco 56a656
 * You will need to have included system headers that define at least
darco 56a656
 * the typedefs FILE and size_t before you can include jpeglib.h.
darco 56a656
 * (stdio.h is sufficient on ANSI-conforming systems.)
darco 56a656
 * You may also wish to include "jerror.h".
darco 56a656
 */
darco 56a656
darco 56a656
#include "jpeglib.h"
darco 56a656
darco 56a656
/*
darco 56a656
 * <setjmp.h> is used for the optional error recovery mechanism shown in</setjmp.h>
darco 56a656
 * the second part of the example.
darco 56a656
 */
darco 56a656
darco 56a656
#include <setjmp.h></setjmp.h>
darco 56a656
darco 56a656
darco 56a656
darco 56a656
/******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
darco 56a656
darco 56a656
/* This half of the example shows how to feed data into the JPEG compressor.
darco 56a656
 * We present a minimal version that does not worry about refinements such
darco 56a656
 * as error recovery (the JPEG code will just exit() if it gets an error).
darco 56a656
 */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * IMAGE DATA FORMATS:
darco 56a656
 *
darco 56a656
 * The standard input image format is a rectangular array of pixels, with
darco 56a656
 * each pixel having the same number of "component" values (color channels).
darco 56a656
 * Each pixel row is an array of JSAMPLEs (which typically are unsigned chars).
darco 56a656
 * If you are working with color data, then the color values for each pixel
darco 56a656
 * must be adjacent in the row; for example, R,G,B,R,G,B,R,G,B,... for 24-bit
darco 56a656
 * RGB color.
darco 56a656
 *
darco 56a656
 * For this example, we'll assume that this data structure matches the way
darco 56a656
 * our application has stored the image in memory, so we can just pass a
darco 56a656
 * pointer to our image buffer.  In particular, let's say that the image is
darco 56a656
 * RGB color and is described by:
darco 56a656
 */
darco 56a656
darco 56a656
extern JSAMPLE * image_buffer;	/* Points to large array of R,G,B-order data */
darco 56a656
extern int image_height;	/* Number of rows in image */
darco 56a656
extern int image_width;		/* Number of columns in image */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Sample routine for JPEG compression.  We assume that the target file name
darco 56a656
 * and a compression quality factor are passed in.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
write_JPEG_file (char * filename, int quality)
darco 56a656
{
darco 56a656
  /* This struct contains the JPEG compression parameters and pointers to
darco 56a656
   * working space (which is allocated as needed by the JPEG library).
darco 56a656
   * It is possible to have several such structures, representing multiple
darco 56a656
   * compression/decompression processes, in existence at once.  We refer
darco 56a656
   * to any one struct (and its associated working data) as a "JPEG object".
darco 56a656
   */
darco 56a656
  struct jpeg_compress_struct cinfo;
darco 56a656
  /* This struct represents a JPEG error handler.  It is declared separately
darco 56a656
   * because applications often want to supply a specialized error handler
darco 56a656
   * (see the second half of this file for an example).  But here we just
darco 56a656
   * take the easy way out and use the standard error handler, which will
darco 56a656
   * print a message on stderr and call exit() if compression fails.
darco 56a656
   * Note that this struct must live as long as the main JPEG parameter
darco 56a656
   * struct, to avoid dangling-pointer problems.
darco 56a656
   */
darco 56a656
  struct jpeg_error_mgr jerr;
darco 56a656
  /* More stuff */
darco 56a656
  FILE * outfile;		/* target file */
darco 56a656
  JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
darco 56a656
  int row_stride;		/* physical row width in image buffer */
darco 56a656
darco 56a656
  /* Step 1: allocate and initialize JPEG compression object */
darco 56a656
darco 56a656
  /* We have to set up the error handler first, in case the initialization
darco 56a656
   * step fails.  (Unlikely, but it could happen if you are out of memory.)
darco 56a656
   * This routine fills in the contents of struct jerr, and returns jerr's
darco 56a656
   * address which we place into the link field in cinfo.
darco 56a656
   */
darco 56a656
  cinfo.err = jpeg_std_error(&jerr);
darco 56a656
  /* Now we can initialize the JPEG compression object. */
darco 56a656
  jpeg_create_compress(&cinfo);
darco 56a656
darco 56a656
  /* Step 2: specify data destination (eg, a file) */
darco 56a656
  /* Note: steps 2 and 3 can be done in either order. */
darco 56a656
darco 56a656
  /* Here we use the library-supplied code to send compressed data to a
darco 56a656
   * stdio stream.  You can also write your own code to do something else.
darco 56a656
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
darco 56a656
   * requires it in order to write binary files.
darco 56a656
   */
darco 56a656
  if ((outfile = fopen(filename, "wb")) == NULL) {
darco 56a656
    fprintf(stderr, "can't open %s\n", filename);
darco 56a656
    exit(1);
darco 56a656
  }
darco 56a656
  jpeg_stdio_dest(&cinfo, outfile);
darco 56a656
darco 56a656
  /* Step 3: set parameters for compression */
darco 56a656
darco 56a656
  /* First we supply a description of the input image.
darco 56a656
   * Four fields of the cinfo struct must be filled in:
darco 56a656
   */
darco 56a656
  cinfo.image_width = image_width; 	/* image width and height, in pixels */
darco 56a656
  cinfo.image_height = image_height;
darco 56a656
  cinfo.input_components = 3;		/* # of color components per pixel */
darco 56a656
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
darco 56a656
  /* Now use the library's routine to set default compression parameters.
darco 56a656
   * (You must set at least cinfo.in_color_space before calling this,
darco 56a656
   * since the defaults depend on the source color space.)
darco 56a656
   */
darco 56a656
  jpeg_set_defaults(&cinfo);
darco 56a656
  /* Now you can set any non-default parameters you wish to.
darco 56a656
   * Here we just illustrate the use of quality (quantization table) scaling:
darco 56a656
   */
darco 56a656
  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
darco 56a656
darco 56a656
  /* Step 4: Start compressor */
darco 56a656
darco 56a656
  /* TRUE ensures that we will write a complete interchange-JPEG file.
darco 56a656
   * Pass TRUE unless you are very sure of what you're doing.
darco 56a656
   */
darco 56a656
  jpeg_start_compress(&cinfo, TRUE);
darco 56a656
darco 56a656
  /* Step 5: while (scan lines remain to be written) */
darco 56a656
  /*           jpeg_write_scanlines(...); */
darco 56a656
darco 56a656
  /* Here we use the library's state variable cinfo.next_scanline as the
darco 56a656
   * loop counter, so that we don't have to keep track ourselves.
darco 56a656
   * To keep things simple, we pass one scanline per call; you can pass
darco 56a656
   * more if you wish, though.
darco 56a656
   */
darco 56a656
  row_stride = image_width * 3;	/* JSAMPLEs per row in image_buffer */
darco 56a656
darco 56a656
  while (cinfo.next_scanline < cinfo.image_height) {
darco 56a656
    /* jpeg_write_scanlines expects an array of pointers to scanlines.
darco 56a656
     * Here the array is only one element long, but you could pass
darco 56a656
     * more than one scanline at a time if that's more convenient.
darco 56a656
     */
darco 56a656
    row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
darco 56a656
    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
darco 56a656
  }
darco 56a656
darco 56a656
  /* Step 6: Finish compression */
darco 56a656
darco 56a656
  jpeg_finish_compress(&cinfo);
darco 56a656
  /* After finish_compress, we can close the output file. */
darco 56a656
  fclose(outfile);
darco 56a656
darco 56a656
  /* Step 7: release JPEG compression object */
darco 56a656
darco 56a656
  /* This is an important step since it will release a good deal of memory. */
darco 56a656
  jpeg_destroy_compress(&cinfo);
darco 56a656
darco 56a656
  /* And we're done! */
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * SOME FINE POINTS:
darco 56a656
 *
darco 56a656
 * In the above loop, we ignored the return value of jpeg_write_scanlines,
darco 56a656
 * which is the number of scanlines actually written.  We could get away
darco 56a656
 * with this because we were only relying on the value of cinfo.next_scanline,
darco 56a656
 * which will be incremented correctly.  If you maintain additional loop
darco 56a656
 * variables then you should be careful to increment them properly.
darco 56a656
 * Actually, for output to a stdio stream you needn't worry, because
darco 56a656
 * then jpeg_write_scanlines will write all the lines passed (or else exit
darco 56a656
 * with a fatal error).  Partial writes can only occur if you use a data
darco 56a656
 * destination module that can demand suspension of the compressor.
darco 56a656
 * (If you don't know what that's for, you don't need it.)
darco 56a656
 *
darco 56a656
 * If the compressor requires full-image buffers (for entropy-coding
darco 56a656
 * optimization or a multi-scan JPEG file), it will create temporary
darco 56a656
 * files for anything that doesn't fit within the maximum-memory setting.
darco 56a656
 * (Note that temp files are NOT needed if you use the default parameters.)
darco 56a656
 * On some systems you may need to set up a signal handler to ensure that
darco 56a656
 * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
darco 56a656
 *
darco 56a656
 * Scanlines MUST be supplied in top-to-bottom order if you want your JPEG
darco 56a656
 * files to be compatible with everyone else's.  If you cannot readily read
darco 56a656
 * your data in that order, you'll need an intermediate array to hold the
darco 56a656
 * image.  See rdtarga.c or rdbmp.c for examples of handling bottom-to-top
darco 56a656
 * source data using the JPEG code's internal virtual-array mechanisms.
darco 56a656
 */
darco 56a656
darco 56a656
darco 56a656
darco 56a656
/******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
darco 56a656
darco 56a656
/* This half of the example shows how to read data from the JPEG decompressor.
darco 56a656
 * It's a bit more refined than the above, in that we show:
darco 56a656
 *   (a) how to modify the JPEG library's standard error-reporting behavior;
darco 56a656
 *   (b) how to allocate workspace using the library's memory manager.
darco 56a656
 *
darco 56a656
 * Just to make this example a little different from the first one, we'll
darco 56a656
 * assume that we do not intend to put the whole image into an in-memory
darco 56a656
 * buffer, but to send it line-by-line someplace else.  We need a one-
darco 56a656
 * scanline-high JSAMPLE array as a work buffer, and we will let the JPEG
darco 56a656
 * memory manager allocate it for us.  This approach is actually quite useful
darco 56a656
 * because we don't need to remember to deallocate the buffer separately: it
darco 56a656
 * will go away automatically when the JPEG object is cleaned up.
darco 56a656
 */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * ERROR HANDLING:
darco 56a656
 *
darco 56a656
 * The JPEG library's standard error handler (jerror.c) is divided into
darco 56a656
 * several "methods" which you can override individually.  This lets you
darco 56a656
 * adjust the behavior without duplicating a lot of code, which you might
darco 56a656
 * have to update with each future release.
darco 56a656
 *
darco 56a656
 * Our example here shows how to override the "error_exit" method so that
darco 56a656
 * control is returned to the library's caller when a fatal error occurs,
darco 56a656
 * rather than calling exit() as the standard error_exit method does.
darco 56a656
 *
darco 56a656
 * We use C's setjmp/longjmp facility to return control.  This means that the
darco 56a656
 * routine which calls the JPEG library must first execute a setjmp() call to
darco 56a656
 * establish the return point.  We want the replacement error_exit to do a
darco 56a656
 * longjmp().  But we need to make the setjmp buffer accessible to the
darco 56a656
 * error_exit routine.  To do this, we make a private extension of the
darco 56a656
 * standard JPEG error handler object.  (If we were using C++, we'd say we
darco 56a656
 * were making a subclass of the regular error handler.)
darco 56a656
 *
darco 56a656
 * Here's the extended error handler struct:
darco 56a656
 */
darco 56a656
darco 56a656
struct my_error_mgr {
darco 56a656
  struct jpeg_error_mgr pub;	/* "public" fields */
darco 56a656
darco 56a656
  jmp_buf setjmp_buffer;	/* for return to caller */
darco 56a656
};
darco 56a656
darco 56a656
typedef struct my_error_mgr * my_error_ptr;
darco 56a656
darco 56a656
/*
darco 56a656
 * Here's the routine that will replace the standard error_exit method:
darco 56a656
 */
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
my_error_exit (j_common_ptr cinfo)
darco 56a656
{
darco 56a656
  /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */
darco 56a656
  my_error_ptr myerr = (my_error_ptr) cinfo->err;
darco 56a656
darco 56a656
  /* Always display the message. */
darco 56a656
  /* We could postpone this until after returning, if we chose. */
darco 56a656
  (*cinfo->err->output_message) (cinfo);
darco 56a656
darco 56a656
  /* Return control to the setjmp point */
darco 56a656
  longjmp(myerr->setjmp_buffer, 1);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Sample routine for JPEG decompression.  We assume that the source file name
darco 56a656
 * is passed in.  We want to return 1 on success, 0 on error.
darco 56a656
 */
darco 56a656
darco 56a656
darco 56a656
GLOBAL(int)
darco 56a656
read_JPEG_file (char * filename)
darco 56a656
{
darco 56a656
  /* This struct contains the JPEG decompression parameters and pointers to
darco 56a656
   * working space (which is allocated as needed by the JPEG library).
darco 56a656
   */
darco 56a656
  struct jpeg_decompress_struct cinfo;
darco 56a656
  /* We use our private extension JPEG error handler.
darco 56a656
   * Note that this struct must live as long as the main JPEG parameter
darco 56a656
   * struct, to avoid dangling-pointer problems.
darco 56a656
   */
darco 56a656
  struct my_error_mgr jerr;
darco 56a656
  /* More stuff */
darco 56a656
  FILE * infile;		/* source file */
darco 56a656
  JSAMPARRAY buffer;		/* Output row buffer */
darco 56a656
  int row_stride;		/* physical row width in output buffer */
darco 56a656
darco 56a656
  /* In this example we want to open the input file before doing anything else,
darco 56a656
   * so that the setjmp() error recovery below can assume the file is open.
darco 56a656
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
darco 56a656
   * requires it in order to read binary files.
darco 56a656
   */
darco 56a656
darco 56a656
  if ((infile = fopen(filename, "rb")) == NULL) {
darco 56a656
    fprintf(stderr, "can't open %s\n", filename);
darco 56a656
    return 0;
darco 56a656
  }
darco 56a656
darco 56a656
  /* Step 1: allocate and initialize JPEG decompression object */
darco 56a656
darco 56a656
  /* We set up the normal JPEG error routines, then override error_exit. */
darco 56a656
  cinfo.err = jpeg_std_error(&jerr.pub);
darco 56a656
  jerr.pub.error_exit = my_error_exit;
darco 56a656
  /* Establish the setjmp return context for my_error_exit to use. */
darco 56a656
  if (setjmp(jerr.setjmp_buffer)) {
darco 56a656
    /* If we get here, the JPEG code has signaled an error.
darco 56a656
     * We need to clean up the JPEG object, close the input file, and return.
darco 56a656
     */
darco 56a656
    jpeg_destroy_decompress(&cinfo);
darco 56a656
    fclose(infile);
darco 56a656
    return 0;
darco 56a656
  }
darco 56a656
  /* Now we can initialize the JPEG decompression object. */
darco 56a656
  jpeg_create_decompress(&cinfo);
darco 56a656
darco 56a656
  /* Step 2: specify data source (eg, a file) */
darco 56a656
darco 56a656
  jpeg_stdio_src(&cinfo, infile);
darco 56a656
darco 56a656
  /* Step 3: read file parameters with jpeg_read_header() */
darco 56a656
darco 56a656
  (void) jpeg_read_header(&cinfo, TRUE);
darco 56a656
  /* We can ignore the return value from jpeg_read_header since
darco 56a656
   *   (a) suspension is not possible with the stdio data source, and
darco 56a656
   *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
darco 56a656
   * See libjpeg.doc for more info.
darco 56a656
   */
darco 56a656
darco 56a656
  /* Step 4: set parameters for decompression */
darco 56a656
darco 56a656
  /* In this example, we don't need to change any of the defaults set by
darco 56a656
   * jpeg_read_header(), so we do nothing here.
darco 56a656
   */
darco 56a656
darco 56a656
  /* Step 5: Start decompressor */
darco 56a656
darco 56a656
  (void) jpeg_start_decompress(&cinfo);
darco 56a656
  /* We can ignore the return value since suspension is not possible
darco 56a656
   * with the stdio data source.
darco 56a656
   */
darco 56a656
darco 56a656
  /* We may need to do some setup of our own at this point before reading
darco 56a656
   * the data.  After jpeg_start_decompress() we have the correct scaled
darco 56a656
   * output image dimensions available, as well as the output colormap
darco 56a656
   * if we asked for color quantization.
darco 56a656
   * In this example, we need to make an output work buffer of the right size.
darco 56a656
   */ 
darco 56a656
  /* JSAMPLEs per row in output buffer */
darco 56a656
  row_stride = cinfo.output_width * cinfo.output_components;
darco 56a656
  /* Make a one-row-high sample array that will go away when done with image */
darco 56a656
  buffer = (*cinfo.mem->alloc_sarray)
darco 56a656
		((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
darco 56a656
darco 56a656
  /* Step 6: while (scan lines remain to be read) */
darco 56a656
  /*           jpeg_read_scanlines(...); */
darco 56a656
darco 56a656
  /* Here we use the library's state variable cinfo.output_scanline as the
darco 56a656
   * loop counter, so that we don't have to keep track ourselves.
darco 56a656
   */
darco 56a656
  while (cinfo.output_scanline < cinfo.output_height) {
darco 56a656
    /* jpeg_read_scanlines expects an array of pointers to scanlines.
darco 56a656
     * Here the array is only one element long, but you could ask for
darco 56a656
     * more than one scanline at a time if that's more convenient.
darco 56a656
     */
darco 56a656
    (void) jpeg_read_scanlines(&cinfo, buffer, 1);
darco 56a656
    /* Assume put_scanline_someplace wants a pointer and sample count. */
darco 56a656
    put_scanline_someplace(buffer[0], row_stride);
darco 56a656
  }
darco 56a656
darco 56a656
  /* Step 7: Finish decompression */
darco 56a656
darco 56a656
  (void) jpeg_finish_decompress(&cinfo);
darco 56a656
  /* We can ignore the return value since suspension is not possible
darco 56a656
   * with the stdio data source.
darco 56a656
   */
darco 56a656
darco 56a656
  /* Step 8: Release JPEG decompression object */
darco 56a656
darco 56a656
  /* This is an important step since it will release a good deal of memory. */
darco 56a656
  jpeg_destroy_decompress(&cinfo);
darco 56a656
darco 56a656
  /* After finish_decompress, we can close the input file.
darco 56a656
   * Here we postpone it until after no more JPEG errors are possible,
darco 56a656
   * so as to simplify the setjmp error logic above.  (Actually, I don't
darco 56a656
   * think that jpeg_destroy can do an error exit, but why assume anything...)
darco 56a656
   */
darco 56a656
  fclose(infile);
darco 56a656
darco 56a656
  /* At this point you may want to check to see whether any corrupt-data
darco 56a656
   * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
darco 56a656
   */
darco 56a656
darco 56a656
  /* And we're done! */
darco 56a656
  return 1;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * SOME FINE POINTS:
darco 56a656
 *
darco 56a656
 * In the above code, we ignored the return value of jpeg_read_scanlines,
darco 56a656
 * which is the number of scanlines actually read.  We could get away with
darco 56a656
 * this because we asked for only one line at a time and we weren't using
darco 56a656
 * a suspending data source.  See libjpeg.doc for more info.
darco 56a656
 *
darco 56a656
 * We cheated a bit by calling alloc_sarray() after jpeg_start_decompress();
darco 56a656
 * we should have done it beforehand to ensure that the space would be
darco 56a656
 * counted against the JPEG max_memory setting.  In some systems the above
darco 56a656
 * code would risk an out-of-memory error.  However, in general we don't
darco 56a656
 * know the output image dimensions before jpeg_start_decompress(), unless we
darco 56a656
 * call jpeg_calc_output_dimensions().  See libjpeg.doc for more about this.
darco 56a656
 *
darco 56a656
 * Scanlines are returned in the same order as they appear in the JPEG file,
darco 56a656
 * which is standardly top-to-bottom.  If you must emit data bottom-to-top,
darco 56a656
 * you can use one of the virtual arrays provided by the JPEG memory manager
darco 56a656
 * to invert the data.  See wrbmp.c for an example.
darco 56a656
 *
darco 56a656
 * As with compression, some operating modes may require temporary files.
darco 56a656
 * On some systems you may need to set up a signal handler to ensure that
darco 56a656
 * temporary files are deleted if the program is interrupted.  See libjpeg.doc.
darco 56a656
 */