media/libjpeg/jdatasrc.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jdatasrc.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,283 @@
     1.4 +/*
     1.5 + * jdatasrc.c
     1.6 + *
     1.7 + * This file was part of the Independent JPEG Group's software:
     1.8 + * Copyright (C) 1994-1996, Thomas G. Lane.
     1.9 + * Modified 2009-2011 by Guido Vollbeding.
    1.10 + * libjpeg-turbo Modifications:
    1.11 + * Copyright (C) 2013, D. R. Commander.
    1.12 + * For conditions of distribution and use, see the accompanying README file.
    1.13 + *
    1.14 + * This file contains decompression data source routines for the case of
    1.15 + * reading JPEG data from memory or from a file (or any stdio stream).
    1.16 + * While these routines are sufficient for most applications,
    1.17 + * some will want to use a different source manager.
    1.18 + * IMPORTANT: we assume that fread() will correctly transcribe an array of
    1.19 + * JOCTETs from 8-bit-wide elements on external storage.  If char is wider
    1.20 + * than 8 bits on your machine, you may need to do some tweaking.
    1.21 + */
    1.22 +
    1.23 +/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
    1.24 +#include "jinclude.h"
    1.25 +#include "jpeglib.h"
    1.26 +#include "jerror.h"
    1.27 +
    1.28 +
    1.29 +/* Expanded data source object for stdio input */
    1.30 +
    1.31 +typedef struct {
    1.32 +  struct jpeg_source_mgr pub;	/* public fields */
    1.33 +
    1.34 +  FILE * infile;		/* source stream */
    1.35 +  JOCTET * buffer;		/* start of buffer */
    1.36 +  boolean start_of_file;	/* have we gotten any data yet? */
    1.37 +} my_source_mgr;
    1.38 +
    1.39 +typedef my_source_mgr * my_src_ptr;
    1.40 +
    1.41 +#define INPUT_BUF_SIZE  4096	/* choose an efficiently fread'able size */
    1.42 +
    1.43 +
    1.44 +/*
    1.45 + * Initialize source --- called by jpeg_read_header
    1.46 + * before any data is actually read.
    1.47 + */
    1.48 +
    1.49 +METHODDEF(void)
    1.50 +init_source (j_decompress_ptr cinfo)
    1.51 +{
    1.52 +  my_src_ptr src = (my_src_ptr) cinfo->src;
    1.53 +
    1.54 +  /* We reset the empty-input-file flag for each image,
    1.55 +   * but we don't clear the input buffer.
    1.56 +   * This is correct behavior for reading a series of images from one source.
    1.57 +   */
    1.58 +  src->start_of_file = TRUE;
    1.59 +}
    1.60 +
    1.61 +#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
    1.62 +METHODDEF(void)
    1.63 +init_mem_source (j_decompress_ptr cinfo)
    1.64 +{
    1.65 +  /* no work necessary here */
    1.66 +}
    1.67 +#endif
    1.68 +
    1.69 +
    1.70 +/*
    1.71 + * Fill the input buffer --- called whenever buffer is emptied.
    1.72 + *
    1.73 + * In typical applications, this should read fresh data into the buffer
    1.74 + * (ignoring the current state of next_input_byte & bytes_in_buffer),
    1.75 + * reset the pointer & count to the start of the buffer, and return TRUE
    1.76 + * indicating that the buffer has been reloaded.  It is not necessary to
    1.77 + * fill the buffer entirely, only to obtain at least one more byte.
    1.78 + *
    1.79 + * There is no such thing as an EOF return.  If the end of the file has been
    1.80 + * reached, the routine has a choice of ERREXIT() or inserting fake data into
    1.81 + * the buffer.  In most cases, generating a warning message and inserting a
    1.82 + * fake EOI marker is the best course of action --- this will allow the
    1.83 + * decompressor to output however much of the image is there.  However,
    1.84 + * the resulting error message is misleading if the real problem is an empty
    1.85 + * input file, so we handle that case specially.
    1.86 + *
    1.87 + * In applications that need to be able to suspend compression due to input
    1.88 + * not being available yet, a FALSE return indicates that no more data can be
    1.89 + * obtained right now, but more may be forthcoming later.  In this situation,
    1.90 + * the decompressor will return to its caller (with an indication of the
    1.91 + * number of scanlines it has read, if any).  The application should resume
    1.92 + * decompression after it has loaded more data into the input buffer.  Note
    1.93 + * that there are substantial restrictions on the use of suspension --- see
    1.94 + * the documentation.
    1.95 + *
    1.96 + * When suspending, the decompressor will back up to a convenient restart point
    1.97 + * (typically the start of the current MCU). next_input_byte & bytes_in_buffer
    1.98 + * indicate where the restart point will be if the current call returns FALSE.
    1.99 + * Data beyond this point must be rescanned after resumption, so move it to
   1.100 + * the front of the buffer rather than discarding it.
   1.101 + */
   1.102 +
   1.103 +METHODDEF(boolean)
   1.104 +fill_input_buffer (j_decompress_ptr cinfo)
   1.105 +{
   1.106 +  my_src_ptr src = (my_src_ptr) cinfo->src;
   1.107 +  size_t nbytes;
   1.108 +
   1.109 +  nbytes = JFREAD(src->infile, src->buffer, INPUT_BUF_SIZE);
   1.110 +
   1.111 +  if (nbytes <= 0) {
   1.112 +    if (src->start_of_file)	/* Treat empty input file as fatal error */
   1.113 +      ERREXIT(cinfo, JERR_INPUT_EMPTY);
   1.114 +    WARNMS(cinfo, JWRN_JPEG_EOF);
   1.115 +    /* Insert a fake EOI marker */
   1.116 +    src->buffer[0] = (JOCTET) 0xFF;
   1.117 +    src->buffer[1] = (JOCTET) JPEG_EOI;
   1.118 +    nbytes = 2;
   1.119 +  }
   1.120 +
   1.121 +  src->pub.next_input_byte = src->buffer;
   1.122 +  src->pub.bytes_in_buffer = nbytes;
   1.123 +  src->start_of_file = FALSE;
   1.124 +
   1.125 +  return TRUE;
   1.126 +}
   1.127 +
   1.128 +#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
   1.129 +METHODDEF(boolean)
   1.130 +fill_mem_input_buffer (j_decompress_ptr cinfo)
   1.131 +{
   1.132 +  static const JOCTET mybuffer[4] = {
   1.133 +    (JOCTET) 0xFF, (JOCTET) JPEG_EOI, 0, 0
   1.134 +  };
   1.135 +
   1.136 +  /* The whole JPEG data is expected to reside in the supplied memory
   1.137 +   * buffer, so any request for more data beyond the given buffer size
   1.138 +   * is treated as an error.
   1.139 +   */
   1.140 +  WARNMS(cinfo, JWRN_JPEG_EOF);
   1.141 +
   1.142 +  /* Insert a fake EOI marker */
   1.143 +
   1.144 +  cinfo->src->next_input_byte = mybuffer;
   1.145 +  cinfo->src->bytes_in_buffer = 2;
   1.146 +
   1.147 +  return TRUE;
   1.148 +}
   1.149 +#endif
   1.150 +
   1.151 +
   1.152 +/*
   1.153 + * Skip data --- used to skip over a potentially large amount of
   1.154 + * uninteresting data (such as an APPn marker).
   1.155 + *
   1.156 + * Writers of suspendable-input applications must note that skip_input_data
   1.157 + * is not granted the right to give a suspension return.  If the skip extends
   1.158 + * beyond the data currently in the buffer, the buffer can be marked empty so
   1.159 + * that the next read will cause a fill_input_buffer call that can suspend.
   1.160 + * Arranging for additional bytes to be discarded before reloading the input
   1.161 + * buffer is the application writer's problem.
   1.162 + */
   1.163 +
   1.164 +METHODDEF(void)
   1.165 +skip_input_data (j_decompress_ptr cinfo, long num_bytes)
   1.166 +{
   1.167 +  struct jpeg_source_mgr * src = cinfo->src;
   1.168 +
   1.169 +  /* Just a dumb implementation for now.  Could use fseek() except
   1.170 +   * it doesn't work on pipes.  Not clear that being smart is worth
   1.171 +   * any trouble anyway --- large skips are infrequent.
   1.172 +   */
   1.173 +  if (num_bytes > 0) {
   1.174 +    while (num_bytes > (long) src->bytes_in_buffer) {
   1.175 +      num_bytes -= (long) src->bytes_in_buffer;
   1.176 +      (void) (*src->fill_input_buffer) (cinfo);
   1.177 +      /* note we assume that fill_input_buffer will never return FALSE,
   1.178 +       * so suspension need not be handled.
   1.179 +       */
   1.180 +    }
   1.181 +    src->next_input_byte += (size_t) num_bytes;
   1.182 +    src->bytes_in_buffer -= (size_t) num_bytes;
   1.183 +  }
   1.184 +}
   1.185 +
   1.186 +
   1.187 +/*
   1.188 + * An additional method that can be provided by data source modules is the
   1.189 + * resync_to_restart method for error recovery in the presence of RST markers.
   1.190 + * For the moment, this source module just uses the default resync method
   1.191 + * provided by the JPEG library.  That method assumes that no backtracking
   1.192 + * is possible.
   1.193 + */
   1.194 +
   1.195 +
   1.196 +/*
   1.197 + * Terminate source --- called by jpeg_finish_decompress
   1.198 + * after all data has been read.  Often a no-op.
   1.199 + *
   1.200 + * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
   1.201 + * application must deal with any cleanup that should happen even
   1.202 + * for error exit.
   1.203 + */
   1.204 +
   1.205 +METHODDEF(void)
   1.206 +term_source (j_decompress_ptr cinfo)
   1.207 +{
   1.208 +  /* no work necessary here */
   1.209 +}
   1.210 +
   1.211 +
   1.212 +/*
   1.213 + * Prepare for input from a stdio stream.
   1.214 + * The caller must have already opened the stream, and is responsible
   1.215 + * for closing it after finishing decompression.
   1.216 + */
   1.217 +
   1.218 +GLOBAL(void)
   1.219 +jpeg_stdio_src (j_decompress_ptr cinfo, FILE * infile)
   1.220 +{
   1.221 +  my_src_ptr src;
   1.222 +
   1.223 +  /* The source object and input buffer are made permanent so that a series
   1.224 +   * of JPEG images can be read from the same file by calling jpeg_stdio_src
   1.225 +   * only before the first one.  (If we discarded the buffer at the end of
   1.226 +   * one image, we'd likely lose the start of the next one.)
   1.227 +   * This makes it unsafe to use this manager and a different source
   1.228 +   * manager serially with the same JPEG object.  Caveat programmer.
   1.229 +   */
   1.230 +  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
   1.231 +    cinfo->src = (struct jpeg_source_mgr *)
   1.232 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
   1.233 +				  SIZEOF(my_source_mgr));
   1.234 +    src = (my_src_ptr) cinfo->src;
   1.235 +    src->buffer = (JOCTET *)
   1.236 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
   1.237 +				  INPUT_BUF_SIZE * SIZEOF(JOCTET));
   1.238 +  }
   1.239 +
   1.240 +  src = (my_src_ptr) cinfo->src;
   1.241 +  src->pub.init_source = init_source;
   1.242 +  src->pub.fill_input_buffer = fill_input_buffer;
   1.243 +  src->pub.skip_input_data = skip_input_data;
   1.244 +  src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
   1.245 +  src->pub.term_source = term_source;
   1.246 +  src->infile = infile;
   1.247 +  src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
   1.248 +  src->pub.next_input_byte = NULL; /* until buffer loaded */
   1.249 +}
   1.250 +
   1.251 +
   1.252 +#if JPEG_LIB_VERSION >= 80 || defined(MEM_SRCDST_SUPPORTED)
   1.253 +/*
   1.254 + * Prepare for input from a supplied memory buffer.
   1.255 + * The buffer must contain the whole JPEG data.
   1.256 + */
   1.257 +
   1.258 +GLOBAL(void)
   1.259 +jpeg_mem_src (j_decompress_ptr cinfo,
   1.260 +	      unsigned char * inbuffer, unsigned long insize)
   1.261 +{
   1.262 +  struct jpeg_source_mgr * src;
   1.263 +
   1.264 +  if (inbuffer == NULL || insize == 0)	/* Treat empty input as fatal error */
   1.265 +    ERREXIT(cinfo, JERR_INPUT_EMPTY);
   1.266 +
   1.267 +  /* The source object is made permanent so that a series of JPEG images
   1.268 +   * can be read from the same buffer by calling jpeg_mem_src only before
   1.269 +   * the first one.
   1.270 +   */
   1.271 +  if (cinfo->src == NULL) {	/* first time for this JPEG object? */
   1.272 +    cinfo->src = (struct jpeg_source_mgr *)
   1.273 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
   1.274 +				  SIZEOF(struct jpeg_source_mgr));
   1.275 +  }
   1.276 +
   1.277 +  src = cinfo->src;
   1.278 +  src->init_source = init_mem_source;
   1.279 +  src->fill_input_buffer = fill_mem_input_buffer;
   1.280 +  src->skip_input_data = skip_input_data;
   1.281 +  src->resync_to_restart = jpeg_resync_to_restart; /* use default method */
   1.282 +  src->term_source = term_source;
   1.283 +  src->bytes_in_buffer = (size_t) insize;
   1.284 +  src->next_input_byte = (JOCTET *) inbuffer;
   1.285 +}
   1.286 +#endif

mercurial