dbf-halloween2015

diff libs/libjpeg/jdapimin.c @ 1:c3f5c32cb210

barfed all the libraries in the source tree to make porting easier
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 00:36:56 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/libjpeg/jdapimin.c	Sun Nov 01 00:36:56 2015 +0200
     1.3 @@ -0,0 +1,395 @@
     1.4 +/*
     1.5 + * jdapimin.c
     1.6 + *
     1.7 + * Copyright (C) 1994-1998, 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 application interface code for the decompression half
    1.12 + * of the JPEG library.  These are the "minimum" API routines that may be
    1.13 + * needed in either the normal full-decompression case or the
    1.14 + * transcoding-only case.
    1.15 + *
    1.16 + * Most of the routines intended to be called directly by an application
    1.17 + * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
    1.18 + * shared by compression and decompression, and jdtrans.c for the transcoding
    1.19 + * case.
    1.20 + */
    1.21 +
    1.22 +#define JPEG_INTERNALS
    1.23 +#include "jinclude.h"
    1.24 +#include "jpeglib.h"
    1.25 +
    1.26 +
    1.27 +/*
    1.28 + * Initialization of a JPEG decompression object.
    1.29 + * The error manager must already be set up (in case memory manager fails).
    1.30 + */
    1.31 +
    1.32 +GLOBAL(void)
    1.33 +jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
    1.34 +{
    1.35 +  int i;
    1.36 +
    1.37 +  /* Guard against version mismatches between library and caller. */
    1.38 +  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
    1.39 +  if (version != JPEG_LIB_VERSION)
    1.40 +    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
    1.41 +  if (structsize != SIZEOF(struct jpeg_decompress_struct))
    1.42 +    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
    1.43 +	     (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
    1.44 +
    1.45 +  /* For debugging purposes, we zero the whole master structure.
    1.46 +   * But the application has already set the err pointer, and may have set
    1.47 +   * client_data, so we have to save and restore those fields.
    1.48 +   * Note: if application hasn't set client_data, tools like Purify may
    1.49 +   * complain here.
    1.50 +   */
    1.51 +  {
    1.52 +    struct jpeg_error_mgr * err = cinfo->err;
    1.53 +    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
    1.54 +    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
    1.55 +    cinfo->err = err;
    1.56 +    cinfo->client_data = client_data;
    1.57 +  }
    1.58 +  cinfo->is_decompressor = TRUE;
    1.59 +
    1.60 +  /* Initialize a memory manager instance for this object */
    1.61 +  jinit_memory_mgr((j_common_ptr) cinfo);
    1.62 +
    1.63 +  /* Zero out pointers to permanent structures. */
    1.64 +  cinfo->progress = NULL;
    1.65 +  cinfo->src = NULL;
    1.66 +
    1.67 +  for (i = 0; i < NUM_QUANT_TBLS; i++)
    1.68 +    cinfo->quant_tbl_ptrs[i] = NULL;
    1.69 +
    1.70 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
    1.71 +    cinfo->dc_huff_tbl_ptrs[i] = NULL;
    1.72 +    cinfo->ac_huff_tbl_ptrs[i] = NULL;
    1.73 +  }
    1.74 +
    1.75 +  /* Initialize marker processor so application can override methods
    1.76 +   * for COM, APPn markers before calling jpeg_read_header.
    1.77 +   */
    1.78 +  cinfo->marker_list = NULL;
    1.79 +  jinit_marker_reader(cinfo);
    1.80 +
    1.81 +  /* And initialize the overall input controller. */
    1.82 +  jinit_input_controller(cinfo);
    1.83 +
    1.84 +  /* OK, I'm ready */
    1.85 +  cinfo->global_state = DSTATE_START;
    1.86 +}
    1.87 +
    1.88 +
    1.89 +/*
    1.90 + * Destruction of a JPEG decompression object
    1.91 + */
    1.92 +
    1.93 +GLOBAL(void)
    1.94 +jpeg_destroy_decompress (j_decompress_ptr cinfo)
    1.95 +{
    1.96 +  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
    1.97 +}
    1.98 +
    1.99 +
   1.100 +/*
   1.101 + * Abort processing of a JPEG decompression operation,
   1.102 + * but don't destroy the object itself.
   1.103 + */
   1.104 +
   1.105 +GLOBAL(void)
   1.106 +jpeg_abort_decompress (j_decompress_ptr cinfo)
   1.107 +{
   1.108 +  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
   1.109 +}
   1.110 +
   1.111 +
   1.112 +/*
   1.113 + * Set default decompression parameters.
   1.114 + */
   1.115 +
   1.116 +LOCAL(void)
   1.117 +default_decompress_parms (j_decompress_ptr cinfo)
   1.118 +{
   1.119 +  /* Guess the input colorspace, and set output colorspace accordingly. */
   1.120 +  /* (Wish JPEG committee had provided a real way to specify this...) */
   1.121 +  /* Note application may override our guesses. */
   1.122 +  switch (cinfo->num_components) {
   1.123 +  case 1:
   1.124 +    cinfo->jpeg_color_space = JCS_GRAYSCALE;
   1.125 +    cinfo->out_color_space = JCS_GRAYSCALE;
   1.126 +    break;
   1.127 +    
   1.128 +  case 3:
   1.129 +    if (cinfo->saw_JFIF_marker) {
   1.130 +      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
   1.131 +    } else if (cinfo->saw_Adobe_marker) {
   1.132 +      switch (cinfo->Adobe_transform) {
   1.133 +      case 0:
   1.134 +	cinfo->jpeg_color_space = JCS_RGB;
   1.135 +	break;
   1.136 +      case 1:
   1.137 +	cinfo->jpeg_color_space = JCS_YCbCr;
   1.138 +	break;
   1.139 +      default:
   1.140 +	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
   1.141 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
   1.142 +	break;
   1.143 +      }
   1.144 +    } else {
   1.145 +      /* Saw no special markers, try to guess from the component IDs */
   1.146 +      int cid0 = cinfo->comp_info[0].component_id;
   1.147 +      int cid1 = cinfo->comp_info[1].component_id;
   1.148 +      int cid2 = cinfo->comp_info[2].component_id;
   1.149 +
   1.150 +      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
   1.151 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
   1.152 +      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
   1.153 +	cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
   1.154 +      else {
   1.155 +	TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
   1.156 +	cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
   1.157 +      }
   1.158 +    }
   1.159 +    /* Always guess RGB is proper output colorspace. */
   1.160 +    cinfo->out_color_space = JCS_RGB;
   1.161 +    break;
   1.162 +    
   1.163 +  case 4:
   1.164 +    if (cinfo->saw_Adobe_marker) {
   1.165 +      switch (cinfo->Adobe_transform) {
   1.166 +      case 0:
   1.167 +	cinfo->jpeg_color_space = JCS_CMYK;
   1.168 +	break;
   1.169 +      case 2:
   1.170 +	cinfo->jpeg_color_space = JCS_YCCK;
   1.171 +	break;
   1.172 +      default:
   1.173 +	WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
   1.174 +	cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
   1.175 +	break;
   1.176 +      }
   1.177 +    } else {
   1.178 +      /* No special markers, assume straight CMYK. */
   1.179 +      cinfo->jpeg_color_space = JCS_CMYK;
   1.180 +    }
   1.181 +    cinfo->out_color_space = JCS_CMYK;
   1.182 +    break;
   1.183 +    
   1.184 +  default:
   1.185 +    cinfo->jpeg_color_space = JCS_UNKNOWN;
   1.186 +    cinfo->out_color_space = JCS_UNKNOWN;
   1.187 +    break;
   1.188 +  }
   1.189 +
   1.190 +  /* Set defaults for other decompression parameters. */
   1.191 +  cinfo->scale_num = 1;		/* 1:1 scaling */
   1.192 +  cinfo->scale_denom = 1;
   1.193 +  cinfo->output_gamma = 1.0;
   1.194 +  cinfo->buffered_image = FALSE;
   1.195 +  cinfo->raw_data_out = FALSE;
   1.196 +  cinfo->dct_method = JDCT_DEFAULT;
   1.197 +  cinfo->do_fancy_upsampling = TRUE;
   1.198 +  cinfo->do_block_smoothing = TRUE;
   1.199 +  cinfo->quantize_colors = FALSE;
   1.200 +  /* We set these in case application only sets quantize_colors. */
   1.201 +  cinfo->dither_mode = JDITHER_FS;
   1.202 +#ifdef QUANT_2PASS_SUPPORTED
   1.203 +  cinfo->two_pass_quantize = TRUE;
   1.204 +#else
   1.205 +  cinfo->two_pass_quantize = FALSE;
   1.206 +#endif
   1.207 +  cinfo->desired_number_of_colors = 256;
   1.208 +  cinfo->colormap = NULL;
   1.209 +  /* Initialize for no mode change in buffered-image mode. */
   1.210 +  cinfo->enable_1pass_quant = FALSE;
   1.211 +  cinfo->enable_external_quant = FALSE;
   1.212 +  cinfo->enable_2pass_quant = FALSE;
   1.213 +}
   1.214 +
   1.215 +
   1.216 +/*
   1.217 + * Decompression startup: read start of JPEG datastream to see what's there.
   1.218 + * Need only initialize JPEG object and supply a data source before calling.
   1.219 + *
   1.220 + * This routine will read as far as the first SOS marker (ie, actual start of
   1.221 + * compressed data), and will save all tables and parameters in the JPEG
   1.222 + * object.  It will also initialize the decompression parameters to default
   1.223 + * values, and finally return JPEG_HEADER_OK.  On return, the application may
   1.224 + * adjust the decompression parameters and then call jpeg_start_decompress.
   1.225 + * (Or, if the application only wanted to determine the image parameters,
   1.226 + * the data need not be decompressed.  In that case, call jpeg_abort or
   1.227 + * jpeg_destroy to release any temporary space.)
   1.228 + * If an abbreviated (tables only) datastream is presented, the routine will
   1.229 + * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
   1.230 + * re-use the JPEG object to read the abbreviated image datastream(s).
   1.231 + * It is unnecessary (but OK) to call jpeg_abort in this case.
   1.232 + * The JPEG_SUSPENDED return code only occurs if the data source module
   1.233 + * requests suspension of the decompressor.  In this case the application
   1.234 + * should load more source data and then re-call jpeg_read_header to resume
   1.235 + * processing.
   1.236 + * If a non-suspending data source is used and require_image is TRUE, then the
   1.237 + * return code need not be inspected since only JPEG_HEADER_OK is possible.
   1.238 + *
   1.239 + * This routine is now just a front end to jpeg_consume_input, with some
   1.240 + * extra error checking.
   1.241 + */
   1.242 +
   1.243 +GLOBAL(int)
   1.244 +jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
   1.245 +{
   1.246 +  int retcode;
   1.247 +
   1.248 +  if (cinfo->global_state != DSTATE_START &&
   1.249 +      cinfo->global_state != DSTATE_INHEADER)
   1.250 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.251 +
   1.252 +  retcode = jpeg_consume_input(cinfo);
   1.253 +
   1.254 +  switch (retcode) {
   1.255 +  case JPEG_REACHED_SOS:
   1.256 +    retcode = JPEG_HEADER_OK;
   1.257 +    break;
   1.258 +  case JPEG_REACHED_EOI:
   1.259 +    if (require_image)		/* Complain if application wanted an image */
   1.260 +      ERREXIT(cinfo, JERR_NO_IMAGE);
   1.261 +    /* Reset to start state; it would be safer to require the application to
   1.262 +     * call jpeg_abort, but we can't change it now for compatibility reasons.
   1.263 +     * A side effect is to free any temporary memory (there shouldn't be any).
   1.264 +     */
   1.265 +    jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
   1.266 +    retcode = JPEG_HEADER_TABLES_ONLY;
   1.267 +    break;
   1.268 +  case JPEG_SUSPENDED:
   1.269 +    /* no work */
   1.270 +    break;
   1.271 +  }
   1.272 +
   1.273 +  return retcode;
   1.274 +}
   1.275 +
   1.276 +
   1.277 +/*
   1.278 + * Consume data in advance of what the decompressor requires.
   1.279 + * This can be called at any time once the decompressor object has
   1.280 + * been created and a data source has been set up.
   1.281 + *
   1.282 + * This routine is essentially a state machine that handles a couple
   1.283 + * of critical state-transition actions, namely initial setup and
   1.284 + * transition from header scanning to ready-for-start_decompress.
   1.285 + * All the actual input is done via the input controller's consume_input
   1.286 + * method.
   1.287 + */
   1.288 +
   1.289 +GLOBAL(int)
   1.290 +jpeg_consume_input (j_decompress_ptr cinfo)
   1.291 +{
   1.292 +  int retcode = JPEG_SUSPENDED;
   1.293 +
   1.294 +  /* NB: every possible DSTATE value should be listed in this switch */
   1.295 +  switch (cinfo->global_state) {
   1.296 +  case DSTATE_START:
   1.297 +    /* Start-of-datastream actions: reset appropriate modules */
   1.298 +    (*cinfo->inputctl->reset_input_controller) (cinfo);
   1.299 +    /* Initialize application's data source module */
   1.300 +    (*cinfo->src->init_source) (cinfo);
   1.301 +    cinfo->global_state = DSTATE_INHEADER;
   1.302 +    /*FALLTHROUGH*/
   1.303 +  case DSTATE_INHEADER:
   1.304 +    retcode = (*cinfo->inputctl->consume_input) (cinfo);
   1.305 +    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
   1.306 +      /* Set up default parameters based on header data */
   1.307 +      default_decompress_parms(cinfo);
   1.308 +      /* Set global state: ready for start_decompress */
   1.309 +      cinfo->global_state = DSTATE_READY;
   1.310 +    }
   1.311 +    break;
   1.312 +  case DSTATE_READY:
   1.313 +    /* Can't advance past first SOS until start_decompress is called */
   1.314 +    retcode = JPEG_REACHED_SOS;
   1.315 +    break;
   1.316 +  case DSTATE_PRELOAD:
   1.317 +  case DSTATE_PRESCAN:
   1.318 +  case DSTATE_SCANNING:
   1.319 +  case DSTATE_RAW_OK:
   1.320 +  case DSTATE_BUFIMAGE:
   1.321 +  case DSTATE_BUFPOST:
   1.322 +  case DSTATE_STOPPING:
   1.323 +    retcode = (*cinfo->inputctl->consume_input) (cinfo);
   1.324 +    break;
   1.325 +  default:
   1.326 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.327 +  }
   1.328 +  return retcode;
   1.329 +}
   1.330 +
   1.331 +
   1.332 +/*
   1.333 + * Have we finished reading the input file?
   1.334 + */
   1.335 +
   1.336 +GLOBAL(boolean)
   1.337 +jpeg_input_complete (j_decompress_ptr cinfo)
   1.338 +{
   1.339 +  /* Check for valid jpeg object */
   1.340 +  if (cinfo->global_state < DSTATE_START ||
   1.341 +      cinfo->global_state > DSTATE_STOPPING)
   1.342 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.343 +  return cinfo->inputctl->eoi_reached;
   1.344 +}
   1.345 +
   1.346 +
   1.347 +/*
   1.348 + * Is there more than one scan?
   1.349 + */
   1.350 +
   1.351 +GLOBAL(boolean)
   1.352 +jpeg_has_multiple_scans (j_decompress_ptr cinfo)
   1.353 +{
   1.354 +  /* Only valid after jpeg_read_header completes */
   1.355 +  if (cinfo->global_state < DSTATE_READY ||
   1.356 +      cinfo->global_state > DSTATE_STOPPING)
   1.357 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.358 +  return cinfo->inputctl->has_multiple_scans;
   1.359 +}
   1.360 +
   1.361 +
   1.362 +/*
   1.363 + * Finish JPEG decompression.
   1.364 + *
   1.365 + * This will normally just verify the file trailer and release temp storage.
   1.366 + *
   1.367 + * Returns FALSE if suspended.  The return value need be inspected only if
   1.368 + * a suspending data source is used.
   1.369 + */
   1.370 +
   1.371 +GLOBAL(boolean)
   1.372 +jpeg_finish_decompress (j_decompress_ptr cinfo)
   1.373 +{
   1.374 +  if ((cinfo->global_state == DSTATE_SCANNING ||
   1.375 +       cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
   1.376 +    /* Terminate final pass of non-buffered mode */
   1.377 +    if (cinfo->output_scanline < cinfo->output_height)
   1.378 +      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
   1.379 +    (*cinfo->master->finish_output_pass) (cinfo);
   1.380 +    cinfo->global_state = DSTATE_STOPPING;
   1.381 +  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
   1.382 +    /* Finishing after a buffered-image operation */
   1.383 +    cinfo->global_state = DSTATE_STOPPING;
   1.384 +  } else if (cinfo->global_state != DSTATE_STOPPING) {
   1.385 +    /* STOPPING = repeat call after a suspension, anything else is error */
   1.386 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.387 +  }
   1.388 +  /* Read until EOI */
   1.389 +  while (! cinfo->inputctl->eoi_reached) {
   1.390 +    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
   1.391 +      return FALSE;		/* Suspend, come back later */
   1.392 +  }
   1.393 +  /* Do final cleanup */
   1.394 +  (*cinfo->src->term_source) (cinfo);
   1.395 +  /* We can use jpeg_abort to release memory and reset global_state */
   1.396 +  jpeg_abort((j_common_ptr) cinfo);
   1.397 +  return TRUE;
   1.398 +}