media/libjpeg/jdapistd.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jdapistd.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,278 @@
     1.4 +/*
     1.5 + * jdapistd.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 + * libjpeg-turbo Modifications:
    1.10 + * Copyright (C) 2010, D. R. Commander.
    1.11 + * For conditions of distribution and use, see the accompanying README file.
    1.12 + *
    1.13 + * This file contains application interface code for the decompression half
    1.14 + * of the JPEG library.  These are the "standard" API routines that are
    1.15 + * used in the normal full-decompression case.  They are not used by a
    1.16 + * transcoding-only application.  Note that if an application links in
    1.17 + * jpeg_start_decompress, it will end up linking in the entire decompressor.
    1.18 + * We thus must separate this file from jdapimin.c to avoid linking the
    1.19 + * whole decompression library into a transcoder.
    1.20 + */
    1.21 +
    1.22 +#define JPEG_INTERNALS
    1.23 +#include "jinclude.h"
    1.24 +#include "jpeglib.h"
    1.25 +#include "jpegcomp.h"
    1.26 +
    1.27 +
    1.28 +/* Forward declarations */
    1.29 +LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
    1.30 +
    1.31 +
    1.32 +/*
    1.33 + * Decompression initialization.
    1.34 + * jpeg_read_header must be completed before calling this.
    1.35 + *
    1.36 + * If a multipass operating mode was selected, this will do all but the
    1.37 + * last pass, and thus may take a great deal of time.
    1.38 + *
    1.39 + * Returns FALSE if suspended.  The return value need be inspected only if
    1.40 + * a suspending data source is used.
    1.41 + */
    1.42 +
    1.43 +GLOBAL(boolean)
    1.44 +jpeg_start_decompress (j_decompress_ptr cinfo)
    1.45 +{
    1.46 +  if (cinfo->global_state == DSTATE_READY) {
    1.47 +    /* First call: initialize master control, select active modules */
    1.48 +    jinit_master_decompress(cinfo);
    1.49 +    if (cinfo->buffered_image) {
    1.50 +      /* No more work here; expecting jpeg_start_output next */
    1.51 +      cinfo->global_state = DSTATE_BUFIMAGE;
    1.52 +      return TRUE;
    1.53 +    }
    1.54 +    cinfo->global_state = DSTATE_PRELOAD;
    1.55 +  }
    1.56 +  if (cinfo->global_state == DSTATE_PRELOAD) {
    1.57 +    /* If file has multiple scans, absorb them all into the coef buffer */
    1.58 +    if (cinfo->inputctl->has_multiple_scans) {
    1.59 +#ifdef D_MULTISCAN_FILES_SUPPORTED
    1.60 +      for (;;) {
    1.61 +	int retcode;
    1.62 +	/* Call progress monitor hook if present */
    1.63 +	if (cinfo->progress != NULL)
    1.64 +	  (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
    1.65 +	/* Absorb some more input */
    1.66 +	retcode = (*cinfo->inputctl->consume_input) (cinfo);
    1.67 +	if (retcode == JPEG_SUSPENDED)
    1.68 +	  return FALSE;
    1.69 +	if (retcode == JPEG_REACHED_EOI)
    1.70 +	  break;
    1.71 +	/* Advance progress counter if appropriate */
    1.72 +	if (cinfo->progress != NULL &&
    1.73 +	    (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
    1.74 +	  if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
    1.75 +	    /* jdmaster underestimated number of scans; ratchet up one scan */
    1.76 +	    cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
    1.77 +	  }
    1.78 +	}
    1.79 +      }
    1.80 +#else
    1.81 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
    1.82 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
    1.83 +    }
    1.84 +    cinfo->output_scan_number = cinfo->input_scan_number;
    1.85 +  } else if (cinfo->global_state != DSTATE_PRESCAN)
    1.86 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    1.87 +  /* Perform any dummy output passes, and set up for the final pass */
    1.88 +  return output_pass_setup(cinfo);
    1.89 +}
    1.90 +
    1.91 +
    1.92 +/*
    1.93 + * Set up for an output pass, and perform any dummy pass(es) needed.
    1.94 + * Common subroutine for jpeg_start_decompress and jpeg_start_output.
    1.95 + * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
    1.96 + * Exit: If done, returns TRUE and sets global_state for proper output mode.
    1.97 + *       If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
    1.98 + */
    1.99 +
   1.100 +LOCAL(boolean)
   1.101 +output_pass_setup (j_decompress_ptr cinfo)
   1.102 +{
   1.103 +  if (cinfo->global_state != DSTATE_PRESCAN) {
   1.104 +    /* First call: do pass setup */
   1.105 +    (*cinfo->master->prepare_for_output_pass) (cinfo);
   1.106 +    cinfo->output_scanline = 0;
   1.107 +    cinfo->global_state = DSTATE_PRESCAN;
   1.108 +  }
   1.109 +  /* Loop over any required dummy passes */
   1.110 +  while (cinfo->master->is_dummy_pass) {
   1.111 +#ifdef QUANT_2PASS_SUPPORTED
   1.112 +    /* Crank through the dummy pass */
   1.113 +    while (cinfo->output_scanline < cinfo->output_height) {
   1.114 +      JDIMENSION last_scanline;
   1.115 +      /* Call progress monitor hook if present */
   1.116 +      if (cinfo->progress != NULL) {
   1.117 +	cinfo->progress->pass_counter = (long) cinfo->output_scanline;
   1.118 +	cinfo->progress->pass_limit = (long) cinfo->output_height;
   1.119 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   1.120 +      }
   1.121 +      /* Process some data */
   1.122 +      last_scanline = cinfo->output_scanline;
   1.123 +      (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
   1.124 +				    &cinfo->output_scanline, (JDIMENSION) 0);
   1.125 +      if (cinfo->output_scanline == last_scanline)
   1.126 +	return FALSE;		/* No progress made, must suspend */
   1.127 +    }
   1.128 +    /* Finish up dummy pass, and set up for another one */
   1.129 +    (*cinfo->master->finish_output_pass) (cinfo);
   1.130 +    (*cinfo->master->prepare_for_output_pass) (cinfo);
   1.131 +    cinfo->output_scanline = 0;
   1.132 +#else
   1.133 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.134 +#endif /* QUANT_2PASS_SUPPORTED */
   1.135 +  }
   1.136 +  /* Ready for application to drive output pass through
   1.137 +   * jpeg_read_scanlines or jpeg_read_raw_data.
   1.138 +   */
   1.139 +  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
   1.140 +  return TRUE;
   1.141 +}
   1.142 +
   1.143 +
   1.144 +/*
   1.145 + * Read some scanlines of data from the JPEG decompressor.
   1.146 + *
   1.147 + * The return value will be the number of lines actually read.
   1.148 + * This may be less than the number requested in several cases,
   1.149 + * including bottom of image, data source suspension, and operating
   1.150 + * modes that emit multiple scanlines at a time.
   1.151 + *
   1.152 + * Note: we warn about excess calls to jpeg_read_scanlines() since
   1.153 + * this likely signals an application programmer error.  However,
   1.154 + * an oversize buffer (max_lines > scanlines remaining) is not an error.
   1.155 + */
   1.156 +
   1.157 +GLOBAL(JDIMENSION)
   1.158 +jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
   1.159 +		     JDIMENSION max_lines)
   1.160 +{
   1.161 +  JDIMENSION row_ctr;
   1.162 +
   1.163 +  if (cinfo->global_state != DSTATE_SCANNING)
   1.164 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.165 +  if (cinfo->output_scanline >= cinfo->output_height) {
   1.166 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
   1.167 +    return 0;
   1.168 +  }
   1.169 +
   1.170 +  /* Call progress monitor hook if present */
   1.171 +  if (cinfo->progress != NULL) {
   1.172 +    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
   1.173 +    cinfo->progress->pass_limit = (long) cinfo->output_height;
   1.174 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   1.175 +  }
   1.176 +
   1.177 +  /* Process some data */
   1.178 +  row_ctr = 0;
   1.179 +  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
   1.180 +  cinfo->output_scanline += row_ctr;
   1.181 +  return row_ctr;
   1.182 +}
   1.183 +
   1.184 +
   1.185 +/*
   1.186 + * Alternate entry point to read raw data.
   1.187 + * Processes exactly one iMCU row per call, unless suspended.
   1.188 + */
   1.189 +
   1.190 +GLOBAL(JDIMENSION)
   1.191 +jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
   1.192 +		    JDIMENSION max_lines)
   1.193 +{
   1.194 +  JDIMENSION lines_per_iMCU_row;
   1.195 +
   1.196 +  if (cinfo->global_state != DSTATE_RAW_OK)
   1.197 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.198 +  if (cinfo->output_scanline >= cinfo->output_height) {
   1.199 +    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
   1.200 +    return 0;
   1.201 +  }
   1.202 +
   1.203 +  /* Call progress monitor hook if present */
   1.204 +  if (cinfo->progress != NULL) {
   1.205 +    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
   1.206 +    cinfo->progress->pass_limit = (long) cinfo->output_height;
   1.207 +    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   1.208 +  }
   1.209 +
   1.210 +  /* Verify that at least one iMCU row can be returned. */
   1.211 +  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
   1.212 +  if (max_lines < lines_per_iMCU_row)
   1.213 +    ERREXIT(cinfo, JERR_BUFFER_SIZE);
   1.214 +
   1.215 +  /* Decompress directly into user's buffer. */
   1.216 +  if (! (*cinfo->coef->decompress_data) (cinfo, data))
   1.217 +    return 0;			/* suspension forced, can do nothing more */
   1.218 +
   1.219 +  /* OK, we processed one iMCU row. */
   1.220 +  cinfo->output_scanline += lines_per_iMCU_row;
   1.221 +  return lines_per_iMCU_row;
   1.222 +}
   1.223 +
   1.224 +
   1.225 +/* Additional entry points for buffered-image mode. */
   1.226 +
   1.227 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   1.228 +
   1.229 +/*
   1.230 + * Initialize for an output pass in buffered-image mode.
   1.231 + */
   1.232 +
   1.233 +GLOBAL(boolean)
   1.234 +jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
   1.235 +{
   1.236 +  if (cinfo->global_state != DSTATE_BUFIMAGE &&
   1.237 +      cinfo->global_state != DSTATE_PRESCAN)
   1.238 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.239 +  /* Limit scan number to valid range */
   1.240 +  if (scan_number <= 0)
   1.241 +    scan_number = 1;
   1.242 +  if (cinfo->inputctl->eoi_reached &&
   1.243 +      scan_number > cinfo->input_scan_number)
   1.244 +    scan_number = cinfo->input_scan_number;
   1.245 +  cinfo->output_scan_number = scan_number;
   1.246 +  /* Perform any dummy output passes, and set up for the real pass */
   1.247 +  return output_pass_setup(cinfo);
   1.248 +}
   1.249 +
   1.250 +
   1.251 +/*
   1.252 + * Finish up after an output pass in buffered-image mode.
   1.253 + *
   1.254 + * Returns FALSE if suspended.  The return value need be inspected only if
   1.255 + * a suspending data source is used.
   1.256 + */
   1.257 +
   1.258 +GLOBAL(boolean)
   1.259 +jpeg_finish_output (j_decompress_ptr cinfo)
   1.260 +{
   1.261 +  if ((cinfo->global_state == DSTATE_SCANNING ||
   1.262 +       cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
   1.263 +    /* Terminate this pass. */
   1.264 +    /* We do not require the whole pass to have been completed. */
   1.265 +    (*cinfo->master->finish_output_pass) (cinfo);
   1.266 +    cinfo->global_state = DSTATE_BUFPOST;
   1.267 +  } else if (cinfo->global_state != DSTATE_BUFPOST) {
   1.268 +    /* BUFPOST = repeat call after a suspension, anything else is error */
   1.269 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.270 +  }
   1.271 +  /* Read markers looking for SOS or EOI */
   1.272 +  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
   1.273 +	 ! cinfo->inputctl->eoi_reached) {
   1.274 +    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
   1.275 +      return FALSE;		/* Suspend, come back later */
   1.276 +  }
   1.277 +  cinfo->global_state = DSTATE_BUFIMAGE;
   1.278 +  return TRUE;
   1.279 +}
   1.280 +
   1.281 +#endif /* D_MULTISCAN_FILES_SUPPORTED */

mercurial