vrshoot

diff libs/libjpeg/jdatasrc.c @ 0:b2f14e535253

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