media/libjpeg/jcapimin.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jcapimin.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,292 @@
     1.4 +/*
     1.5 + * jcapimin.c
     1.6 + *
     1.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
     1.8 + * Modified 2003-2010 by Guido Vollbeding.
     1.9 + * This file is part of the Independent JPEG Group's software.
    1.10 + * For conditions of distribution and use, see the accompanying README file.
    1.11 + *
    1.12 + * This file contains application interface code for the compression half
    1.13 + * of the JPEG library.  These are the "minimum" API routines that may be
    1.14 + * needed in either the normal full-compression case or the transcoding-only
    1.15 + * case.
    1.16 + *
    1.17 + * Most of the routines intended to be called directly by an application
    1.18 + * are in this file or in jcapistd.c.  But also see jcparam.c for
    1.19 + * parameter-setup helper routines, jcomapi.c for routines shared by
    1.20 + * compression and decompression, and jctrans.c for the transcoding case.
    1.21 + */
    1.22 +
    1.23 +#define JPEG_INTERNALS
    1.24 +#include "jinclude.h"
    1.25 +#include "jpeglib.h"
    1.26 +
    1.27 +
    1.28 +/*
    1.29 + * Initialization of a JPEG compression object.
    1.30 + * The error manager must already be set up (in case memory manager fails).
    1.31 + */
    1.32 +
    1.33 +GLOBAL(void)
    1.34 +jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
    1.35 +{
    1.36 +  int i;
    1.37 +
    1.38 +  /* Guard against version mismatches between library and caller. */
    1.39 +  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
    1.40 +  if (version != JPEG_LIB_VERSION)
    1.41 +    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
    1.42 +  if (structsize != SIZEOF(struct jpeg_compress_struct))
    1.43 +    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
    1.44 +	     (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
    1.45 +
    1.46 +  /* For debugging purposes, we zero the whole master structure.
    1.47 +   * But the application has already set the err pointer, and may have set
    1.48 +   * client_data, so we have to save and restore those fields.
    1.49 +   * Note: if application hasn't set client_data, tools like Purify may
    1.50 +   * complain here.
    1.51 +   */
    1.52 +  {
    1.53 +    struct jpeg_error_mgr * err = cinfo->err;
    1.54 +    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
    1.55 +    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
    1.56 +    cinfo->err = err;
    1.57 +    cinfo->client_data = client_data;
    1.58 +  }
    1.59 +  cinfo->is_decompressor = FALSE;
    1.60 +
    1.61 +  /* Initialize a memory manager instance for this object */
    1.62 +  jinit_memory_mgr((j_common_ptr) cinfo);
    1.63 +
    1.64 +  /* Zero out pointers to permanent structures. */
    1.65 +  cinfo->progress = NULL;
    1.66 +  cinfo->dest = NULL;
    1.67 +
    1.68 +  cinfo->comp_info = NULL;
    1.69 +
    1.70 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
    1.71 +    cinfo->quant_tbl_ptrs[i] = NULL;
    1.72 +#if JPEG_LIB_VERSION >= 70
    1.73 +    cinfo->q_scale_factor[i] = 100;
    1.74 +#endif
    1.75 +  }
    1.76 +
    1.77 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
    1.78 +    cinfo->dc_huff_tbl_ptrs[i] = NULL;
    1.79 +    cinfo->ac_huff_tbl_ptrs[i] = NULL;
    1.80 +  }
    1.81 +
    1.82 +#if JPEG_LIB_VERSION >= 80
    1.83 +  /* Must do it here for emit_dqt in case jpeg_write_tables is used */
    1.84 +  cinfo->block_size = DCTSIZE;
    1.85 +  cinfo->natural_order = jpeg_natural_order;
    1.86 +  cinfo->lim_Se = DCTSIZE2-1;
    1.87 +#endif
    1.88 +
    1.89 +  cinfo->script_space = NULL;
    1.90 +
    1.91 +  cinfo->input_gamma = 1.0;	/* in case application forgets */
    1.92 +
    1.93 +  /* OK, I'm ready */
    1.94 +  cinfo->global_state = CSTATE_START;
    1.95 +}
    1.96 +
    1.97 +
    1.98 +/*
    1.99 + * Destruction of a JPEG compression object
   1.100 + */
   1.101 +
   1.102 +GLOBAL(void)
   1.103 +jpeg_destroy_compress (j_compress_ptr cinfo)
   1.104 +{
   1.105 +  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
   1.106 +}
   1.107 +
   1.108 +
   1.109 +/*
   1.110 + * Abort processing of a JPEG compression operation,
   1.111 + * but don't destroy the object itself.
   1.112 + */
   1.113 +
   1.114 +GLOBAL(void)
   1.115 +jpeg_abort_compress (j_compress_ptr cinfo)
   1.116 +{
   1.117 +  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
   1.118 +}
   1.119 +
   1.120 +
   1.121 +/*
   1.122 + * Forcibly suppress or un-suppress all quantization and Huffman tables.
   1.123 + * Marks all currently defined tables as already written (if suppress)
   1.124 + * or not written (if !suppress).  This will control whether they get emitted
   1.125 + * by a subsequent jpeg_start_compress call.
   1.126 + *
   1.127 + * This routine is exported for use by applications that want to produce
   1.128 + * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
   1.129 + * since it is called by jpeg_start_compress, we put it here --- otherwise
   1.130 + * jcparam.o would be linked whether the application used it or not.
   1.131 + */
   1.132 +
   1.133 +GLOBAL(void)
   1.134 +jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
   1.135 +{
   1.136 +  int i;
   1.137 +  JQUANT_TBL * qtbl;
   1.138 +  JHUFF_TBL * htbl;
   1.139 +
   1.140 +  for (i = 0; i < NUM_QUANT_TBLS; i++) {
   1.141 +    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
   1.142 +      qtbl->sent_table = suppress;
   1.143 +  }
   1.144 +
   1.145 +  for (i = 0; i < NUM_HUFF_TBLS; i++) {
   1.146 +    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
   1.147 +      htbl->sent_table = suppress;
   1.148 +    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
   1.149 +      htbl->sent_table = suppress;
   1.150 +  }
   1.151 +}
   1.152 +
   1.153 +
   1.154 +/*
   1.155 + * Finish JPEG compression.
   1.156 + *
   1.157 + * If a multipass operating mode was selected, this may do a great deal of
   1.158 + * work including most of the actual output.
   1.159 + */
   1.160 +
   1.161 +GLOBAL(void)
   1.162 +jpeg_finish_compress (j_compress_ptr cinfo)
   1.163 +{
   1.164 +  JDIMENSION iMCU_row;
   1.165 +
   1.166 +  if (cinfo->global_state == CSTATE_SCANNING ||
   1.167 +      cinfo->global_state == CSTATE_RAW_OK) {
   1.168 +    /* Terminate first pass */
   1.169 +    if (cinfo->next_scanline < cinfo->image_height)
   1.170 +      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
   1.171 +    (*cinfo->master->finish_pass) (cinfo);
   1.172 +  } else if (cinfo->global_state != CSTATE_WRCOEFS)
   1.173 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.174 +  /* Perform any remaining passes */
   1.175 +  while (! cinfo->master->is_last_pass) {
   1.176 +    (*cinfo->master->prepare_for_pass) (cinfo);
   1.177 +    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
   1.178 +      if (cinfo->progress != NULL) {
   1.179 +	cinfo->progress->pass_counter = (long) iMCU_row;
   1.180 +	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
   1.181 +	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
   1.182 +      }
   1.183 +      /* We bypass the main controller and invoke coef controller directly;
   1.184 +       * all work is being done from the coefficient buffer.
   1.185 +       */
   1.186 +      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
   1.187 +	ERREXIT(cinfo, JERR_CANT_SUSPEND);
   1.188 +    }
   1.189 +    (*cinfo->master->finish_pass) (cinfo);
   1.190 +  }
   1.191 +  /* Write EOI, do final cleanup */
   1.192 +  (*cinfo->marker->write_file_trailer) (cinfo);
   1.193 +  (*cinfo->dest->term_destination) (cinfo);
   1.194 +  /* We can use jpeg_abort to release memory and reset global_state */
   1.195 +  jpeg_abort((j_common_ptr) cinfo);
   1.196 +}
   1.197 +
   1.198 +
   1.199 +/*
   1.200 + * Write a special marker.
   1.201 + * This is only recommended for writing COM or APPn markers.
   1.202 + * Must be called after jpeg_start_compress() and before
   1.203 + * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
   1.204 + */
   1.205 +
   1.206 +GLOBAL(void)
   1.207 +jpeg_write_marker (j_compress_ptr cinfo, int marker,
   1.208 +		   const JOCTET *dataptr, unsigned int datalen)
   1.209 +{
   1.210 +  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
   1.211 +
   1.212 +  if (cinfo->next_scanline != 0 ||
   1.213 +      (cinfo->global_state != CSTATE_SCANNING &&
   1.214 +       cinfo->global_state != CSTATE_RAW_OK &&
   1.215 +       cinfo->global_state != CSTATE_WRCOEFS))
   1.216 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.217 +
   1.218 +  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
   1.219 +  write_marker_byte = cinfo->marker->write_marker_byte;	/* copy for speed */
   1.220 +  while (datalen--) {
   1.221 +    (*write_marker_byte) (cinfo, *dataptr);
   1.222 +    dataptr++;
   1.223 +  }
   1.224 +}
   1.225 +
   1.226 +/* Same, but piecemeal. */
   1.227 +
   1.228 +GLOBAL(void)
   1.229 +jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
   1.230 +{
   1.231 +  if (cinfo->next_scanline != 0 ||
   1.232 +      (cinfo->global_state != CSTATE_SCANNING &&
   1.233 +       cinfo->global_state != CSTATE_RAW_OK &&
   1.234 +       cinfo->global_state != CSTATE_WRCOEFS))
   1.235 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.236 +
   1.237 +  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
   1.238 +}
   1.239 +
   1.240 +GLOBAL(void)
   1.241 +jpeg_write_m_byte (j_compress_ptr cinfo, int val)
   1.242 +{
   1.243 +  (*cinfo->marker->write_marker_byte) (cinfo, val);
   1.244 +}
   1.245 +
   1.246 +
   1.247 +/*
   1.248 + * Alternate compression function: just write an abbreviated table file.
   1.249 + * Before calling this, all parameters and a data destination must be set up.
   1.250 + *
   1.251 + * To produce a pair of files containing abbreviated tables and abbreviated
   1.252 + * image data, one would proceed as follows:
   1.253 + *
   1.254 + *		initialize JPEG object
   1.255 + *		set JPEG parameters
   1.256 + *		set destination to table file
   1.257 + *		jpeg_write_tables(cinfo);
   1.258 + *		set destination to image file
   1.259 + *		jpeg_start_compress(cinfo, FALSE);
   1.260 + *		write data...
   1.261 + *		jpeg_finish_compress(cinfo);
   1.262 + *
   1.263 + * jpeg_write_tables has the side effect of marking all tables written
   1.264 + * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
   1.265 + * will not re-emit the tables unless it is passed write_all_tables=TRUE.
   1.266 + */
   1.267 +
   1.268 +GLOBAL(void)
   1.269 +jpeg_write_tables (j_compress_ptr cinfo)
   1.270 +{
   1.271 +  if (cinfo->global_state != CSTATE_START)
   1.272 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.273 +
   1.274 +  /* (Re)initialize error mgr and destination modules */
   1.275 +  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
   1.276 +  (*cinfo->dest->init_destination) (cinfo);
   1.277 +  /* Initialize the marker writer ... bit of a crock to do it here. */
   1.278 +  jinit_marker_writer(cinfo);
   1.279 +  /* Write them tables! */
   1.280 +  (*cinfo->marker->write_tables_only) (cinfo);
   1.281 +  /* And clean up. */
   1.282 +  (*cinfo->dest->term_destination) (cinfo);
   1.283 +  /*
   1.284 +   * In library releases up through v6a, we called jpeg_abort() here to free
   1.285 +   * any working memory allocated by the destination manager and marker
   1.286 +   * writer.  Some applications had a problem with that: they allocated space
   1.287 +   * of their own from the library memory manager, and didn't want it to go
   1.288 +   * away during write_tables.  So now we do nothing.  This will cause a
   1.289 +   * memory leak if an app calls write_tables repeatedly without doing a full
   1.290 +   * compression cycle or otherwise resetting the JPEG object.  However, that
   1.291 +   * seems less bad than unexpectedly freeing memory in the normal case.
   1.292 +   * An app that prefers the old behavior can call jpeg_abort for itself after
   1.293 +   * each call to jpeg_write_tables().
   1.294 +   */
   1.295 +}

mercurial