kusano 7d535a
/*
kusano 7d535a
 * jcapimin.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1998, Thomas G. Lane.
kusano 7d535a
 * Modified 2003-2010 by Guido Vollbeding.
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 code for the compression half
kusano 7d535a
 * of the JPEG library.  These are the "minimum" API routines that may be
kusano 7d535a
 * needed in either the normal full-compression case or the transcoding-only
kusano 7d535a
 * case.
kusano 7d535a
 *
kusano 7d535a
 * Most of the routines intended to be called directly by an application
kusano 7d535a
 * are in this file or in jcapistd.c.  But also see jcparam.c for
kusano 7d535a
 * parameter-setup helper routines, jcomapi.c for routines shared by
kusano 7d535a
 * compression and decompression, and jctrans.c for the transcoding case.
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
 * Initialization of a JPEG compression object.
kusano 7d535a
 * The error manager must already be set up (in case memory manager fails).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
kusano 7d535a
{
kusano 7d535a
  int i;
kusano 7d535a
kusano 7d535a
  /* Guard against version mismatches between library and caller. */
kusano 7d535a
  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
kusano 7d535a
  if (version != JPEG_LIB_VERSION)
kusano 7d535a
    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
kusano 7d535a
  if (structsize != SIZEOF(struct jpeg_compress_struct))
kusano 7d535a
    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
kusano 7d535a
	     (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
kusano 7d535a
kusano 7d535a
  /* For debugging purposes, we zero the whole master structure.
kusano 7d535a
   * But the application has already set the err pointer, and may have set
kusano 7d535a
   * client_data, so we have to save and restore those fields.
kusano 7d535a
   * Note: if application hasn't set client_data, tools like Purify may
kusano 7d535a
   * complain here.
kusano 7d535a
   */
kusano 7d535a
  {
kusano 7d535a
    struct jpeg_error_mgr * err = cinfo->err;
kusano 7d535a
    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
kusano 7d535a
    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
kusano 7d535a
    cinfo->err = err;
kusano 7d535a
    cinfo->client_data = client_data;
kusano 7d535a
  }
kusano 7d535a
  cinfo->is_decompressor = FALSE;
kusano 7d535a
kusano 7d535a
  /* Initialize a memory manager instance for this object */
kusano 7d535a
  jinit_memory_mgr((j_common_ptr) cinfo);
kusano 7d535a
kusano 7d535a
  /* Zero out pointers to permanent structures. */
kusano 7d535a
  cinfo->progress = NULL;
kusano 7d535a
  cinfo->dest = NULL;
kusano 7d535a
kusano 7d535a
  cinfo->comp_info = NULL;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < NUM_QUANT_TBLS; i++) {
kusano 7d535a
    cinfo->quant_tbl_ptrs[i] = NULL;
kusano 7d535a
    cinfo->q_scale_factor[i] = 100;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
kusano 7d535a
    cinfo->dc_huff_tbl_ptrs[i] = NULL;
kusano 7d535a
    cinfo->ac_huff_tbl_ptrs[i] = NULL;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  /* Must do it here for emit_dqt in case jpeg_write_tables is used */
kusano 7d535a
  cinfo->block_size = DCTSIZE;
kusano 7d535a
  cinfo->natural_order = jpeg_natural_order;
kusano 7d535a
  cinfo->lim_Se = DCTSIZE2-1;
kusano 7d535a
kusano 7d535a
  cinfo->script_space = NULL;
kusano 7d535a
kusano 7d535a
  cinfo->input_gamma = 1.0;	/* in case application forgets */
kusano 7d535a
kusano 7d535a
  /* OK, I'm ready */
kusano 7d535a
  cinfo->global_state = CSTATE_START;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Destruction of a JPEG compression object
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_destroy_compress (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Abort processing of a JPEG compression operation,
kusano 7d535a
 * but don't destroy the object itself.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_abort_compress (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Forcibly suppress or un-suppress all quantization and Huffman tables.
kusano 7d535a
 * Marks all currently defined tables as already written (if suppress)
kusano 7d535a
 * or not written (if !suppress).  This will control whether they get emitted
kusano 7d535a
 * by a subsequent jpeg_start_compress call.
kusano 7d535a
 *
kusano 7d535a
 * This routine is exported for use by applications that want to produce
kusano 7d535a
 * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
kusano 7d535a
 * since it is called by jpeg_start_compress, we put it here --- otherwise
kusano 7d535a
 * jcparam.o would be linked whether the application used it or not.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
kusano 7d535a
{
kusano 7d535a
  int i;
kusano 7d535a
  JQUANT_TBL * qtbl;
kusano 7d535a
  JHUFF_TBL * htbl;
kusano 7d535a
kusano 7d535a
  for (i = 0; i < NUM_QUANT_TBLS; i++) {
kusano 7d535a
    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
kusano 7d535a
      qtbl->sent_table = suppress;
kusano 7d535a
  }
kusano 7d535a
kusano 7d535a
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
kusano 7d535a
    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
kusano 7d535a
      htbl->sent_table = suppress;
kusano 7d535a
    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
kusano 7d535a
      htbl->sent_table = suppress;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Finish JPEG compression.
kusano 7d535a
 *
kusano 7d535a
 * If a multipass operating mode was selected, this may do a great deal of
kusano 7d535a
 * work including most of the actual output.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_finish_compress (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  JDIMENSION iMCU_row;
kusano 7d535a
kusano 7d535a
  if (cinfo->global_state == CSTATE_SCANNING ||
kusano 7d535a
      cinfo->global_state == CSTATE_RAW_OK) {
kusano 7d535a
    /* Terminate first pass */
kusano 7d535a
    if (cinfo->next_scanline < cinfo->image_height)
kusano 7d535a
      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
kusano 7d535a
    (*cinfo->master->finish_pass) (cinfo);
kusano 7d535a
  } else if (cinfo->global_state != CSTATE_WRCOEFS)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
  /* Perform any remaining passes */
kusano 7d535a
  while (! cinfo->master->is_last_pass) {
kusano 7d535a
    (*cinfo->master->prepare_for_pass) (cinfo);
kusano 7d535a
    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
kusano 7d535a
      if (cinfo->progress != NULL) {
kusano 7d535a
	cinfo->progress->pass_counter = (long) iMCU_row;
kusano 7d535a
	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
kusano 7d535a
	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
kusano 7d535a
      }
kusano 7d535a
      /* We bypass the main controller and invoke coef controller directly;
kusano 7d535a
       * all work is being done from the coefficient buffer.
kusano 7d535a
       */
kusano 7d535a
      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
kusano 7d535a
	ERREXIT(cinfo, JERR_CANT_SUSPEND);
kusano 7d535a
    }
kusano 7d535a
    (*cinfo->master->finish_pass) (cinfo);
kusano 7d535a
  }
kusano 7d535a
  /* Write EOI, do final cleanup */
kusano 7d535a
  (*cinfo->marker->write_file_trailer) (cinfo);
kusano 7d535a
  (*cinfo->dest->term_destination) (cinfo);
kusano 7d535a
  /* We can use jpeg_abort to release memory and reset global_state */
kusano 7d535a
  jpeg_abort((j_common_ptr) cinfo);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Write a special marker.
kusano 7d535a
 * This is only recommended for writing COM or APPn markers.
kusano 7d535a
 * Must be called after jpeg_start_compress() and before
kusano 7d535a
 * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_write_marker (j_compress_ptr cinfo, int marker,
kusano 7d535a
		   const JOCTET *dataptr, unsigned int datalen)
kusano 7d535a
{
kusano 7d535a
  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
kusano 7d535a
kusano 7d535a
  if (cinfo->next_scanline != 0 ||
kusano 7d535a
      (cinfo->global_state != CSTATE_SCANNING &&
kusano 7d535a
       cinfo->global_state != CSTATE_RAW_OK &&
kusano 7d535a
       cinfo->global_state != CSTATE_WRCOEFS))
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
kusano 7d535a
  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
kusano 7d535a
  write_marker_byte = cinfo->marker->write_marker_byte;	/* copy for speed */
kusano 7d535a
  while (datalen--) {
kusano 7d535a
    (*write_marker_byte) (cinfo, *dataptr);
kusano 7d535a
    dataptr++;
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/* Same, but piecemeal. */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
kusano 7d535a
{
kusano 7d535a
  if (cinfo->next_scanline != 0 ||
kusano 7d535a
      (cinfo->global_state != CSTATE_SCANNING &&
kusano 7d535a
       cinfo->global_state != CSTATE_RAW_OK &&
kusano 7d535a
       cinfo->global_state != CSTATE_WRCOEFS))
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
kusano 7d535a
  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_write_m_byte (j_compress_ptr cinfo, int val)
kusano 7d535a
{
kusano 7d535a
  (*cinfo->marker->write_marker_byte) (cinfo, val);
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Alternate compression function: just write an abbreviated table file.
kusano 7d535a
 * Before calling this, all parameters and a data destination must be set up.
kusano 7d535a
 *
kusano 7d535a
 * To produce a pair of files containing abbreviated tables and abbreviated
kusano 7d535a
 * image data, one would proceed as follows:
kusano 7d535a
 *
kusano 7d535a
 *		initialize JPEG object
kusano 7d535a
 *		set JPEG parameters
kusano 7d535a
 *		set destination to table file
kusano 7d535a
 *		jpeg_write_tables(cinfo);
kusano 7d535a
 *		set destination to image file
kusano 7d535a
 *		jpeg_start_compress(cinfo, FALSE);
kusano 7d535a
 *		write data...
kusano 7d535a
 *		jpeg_finish_compress(cinfo);
kusano 7d535a
 *
kusano 7d535a
 * jpeg_write_tables has the side effect of marking all tables written
kusano 7d535a
 * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
kusano 7d535a
 * will not re-emit the tables unless it is passed write_all_tables=TRUE.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_write_tables (j_compress_ptr cinfo)
kusano 7d535a
{
kusano 7d535a
  if (cinfo->global_state != CSTATE_START)
kusano 7d535a
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
kusano 7d535a
kusano 7d535a
  /* (Re)initialize error mgr and destination modules */
kusano 7d535a
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
kusano 7d535a
  (*cinfo->dest->init_destination) (cinfo);
kusano 7d535a
  /* Initialize the marker writer ... bit of a crock to do it here. */
kusano 7d535a
  jinit_marker_writer(cinfo);
kusano 7d535a
  /* Write them tables! */
kusano 7d535a
  (*cinfo->marker->write_tables_only) (cinfo);
kusano 7d535a
  /* And clean up. */
kusano 7d535a
  (*cinfo->dest->term_destination) (cinfo);
kusano 7d535a
  /*
kusano 7d535a
   * In library releases up through v6a, we called jpeg_abort() here to free
kusano 7d535a
   * any working memory allocated by the destination manager and marker
kusano 7d535a
   * writer.  Some applications had a problem with that: they allocated space
kusano 7d535a
   * of their own from the library memory manager, and didn't want it to go
kusano 7d535a
   * away during write_tables.  So now we do nothing.  This will cause a
kusano 7d535a
   * memory leak if an app calls write_tables repeatedly without doing a full
kusano 7d535a
   * compression cycle or otherwise resetting the JPEG object.  However, that
kusano 7d535a
   * seems less bad than unexpectedly freeing memory in the normal case.
kusano 7d535a
   * An app that prefers the old behavior can call jpeg_abort for itself after
kusano 7d535a
   * each call to jpeg_write_tables().
kusano 7d535a
   */
kusano 7d535a
}