kusano 7d535a
/*
kusano 7d535a
 * jcomapi.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1997, Thomas G. Lane.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains application interface routines that are used for both
kusano 7d535a
 * compression and decompression.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Abort processing of a JPEG compression or decompression operation,
kusano 7d535a
 * but don't destroy the object itself.
kusano 7d535a
 *
kusano 7d535a
 * For this, we merely clean up all the nonpermanent memory pools.
kusano 7d535a
 * Note that temp files (virtual arrays) are not allowed to belong to
kusano 7d535a
 * the permanent pool, so we will be able to close all temp files here.
kusano 7d535a
 * Closing a data source or destination, if necessary, is the application's
kusano 7d535a
 * responsibility.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_abort (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  int pool;
kusano 7d535a
kusano 7d535a
  /* Do nothing if called on a not-initialized or destroyed JPEG object. */
kusano 7d535a
  if (cinfo->mem == NULL)
kusano 7d535a
    return;
kusano 7d535a
kusano 7d535a
  /* Releasing pools in reverse order might help avoid fragmentation
kusano 7d535a
   * with some (brain-damaged) malloc libraries.
kusano 7d535a
   */
kusano 7d535a
  for (pool = JPOOL_NUMPOOLS-1; pool > JPOOL_PERMANENT; pool--) {
kusano 7d535a
    (*cinfo->mem->free_pool) (cinfo, pool);
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Reset overall state for possible reuse of object */
kusano 7d535a
  if (cinfo->is_decompressor) {
kusano 7d535a
    cinfo->global_state = DSTATE_START;
kusano 7d535a
    /* Try to keep application from accessing now-deleted marker list.
kusano 7d535a
     * A bit kludgy to do it here, but this is the most central place.
kusano 7d535a
     */
kusano 7d535a
    ((j_decompress_ptr) cinfo)->marker_list = NULL;
kusano 7d535a
  } else {
kusano 7d535a
    cinfo->global_state = CSTATE_START;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Destruction of a JPEG object.
kusano 7d535a
 *
kusano 7d535a
 * Everything gets deallocated except the master jpeg_compress_struct itself
kusano 7d535a
 * and the error manager struct.  Both of these are supplied by the application
kusano 7d535a
 * and must be freed, if necessary, by the application.  (Often they are on
kusano 7d535a
 * the stack and so don't need to be freed anyway.)
kusano 7d535a
 * Closing a data source or destination, if necessary, is the application's
kusano 7d535a
 * responsibility.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_destroy (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  /* We need only tell the memory manager to release everything. */
kusano 7d535a
  /* NB: mem pointer is NULL if memory mgr failed to initialize. */
kusano 7d535a
  if (cinfo->mem != NULL)
kusano 7d535a
    (*cinfo->mem->self_destruct) (cinfo);
kusano 7d535a
  cinfo->mem = NULL;		/* be safe if jpeg_destroy is called twice */
kusano 7d535a
  cinfo->global_state = 0;	/* mark it destroyed */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Convenience routines for allocating quantization and Huffman tables.
kusano 7d535a
 * (Would jutils.c be a more reasonable place to put these?)
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(JQUANT_TBL *)
kusano 7d535a
jpeg_alloc_quant_table (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  JQUANT_TBL *tbl;
kusano 7d535a
kusano 7d535a
  tbl = (JQUANT_TBL *)
kusano 7d535a
    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JQUANT_TBL));
kusano 7d535a
  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
kusano 7d535a
  return tbl;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
GLOBAL(JHUFF_TBL *)
kusano 7d535a
jpeg_alloc_huff_table (j_common_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  JHUFF_TBL *tbl;
kusano 7d535a
kusano 7d535a
  tbl = (JHUFF_TBL *)
kusano 7d535a
    (*cinfo->mem->alloc_small) (cinfo, JPOOL_PERMANENT, SIZEOF(JHUFF_TBL));
kusano 7d535a
  tbl->sent_table = FALSE;	/* make sure this is false in any new table */
kusano 7d535a
  return tbl;
kusano 7d535a
}