170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * jcapimin.c
370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Copyright (C) 1994-1998, Thomas G. Lane.
570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file is part of the Independent JPEG Group's software.
670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * For conditions of distribution and use, see the accompanying README file.
770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This file contains application interface code for the compression half
970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * of the JPEG library.  These are the "minimum" API routines that may be
1070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * needed in either the normal full-compression case or the transcoding-only
1170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * case.
1270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
1370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Most of the routines intended to be called directly by an application
1470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * are in this file or in jcapistd.c.  But also see jcparam.c for
1570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * parameter-setup helper routines, jcomapi.c for routines shared by
1670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * compression and decompression, and jctrans.c for the transcoding case.
1770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
1870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
1970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#define JPEG_INTERNALS
2070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jinclude.h"
2170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine#include "jpeglib.h"
2270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
2370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
2470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
2570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Initialization of a JPEG compression object.
2670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * The error manager must already be set up (in case memory manager fails).
2770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
2870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
2970a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
3070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
3170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
3270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int i;
3370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
3470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Guard against version mismatches between library and caller. */
3570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->mem = NULL;		/* so jpeg_destroy knows mem mgr not called */
3670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (version != JPEG_LIB_VERSION)
3770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
3870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (structsize != SIZEOF(struct jpeg_compress_struct))
3970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
4070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	     (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
4170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
4270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* For debugging purposes, we zero the whole master structure.
4370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * But the application has already set the err pointer, and may have set
4470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * client_data, so we have to save and restore those fields.
4570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * Note: if application hasn't set client_data, tools like Purify may
4670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * complain here.
4770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
4870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  {
4970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    struct jpeg_error_mgr * err = cinfo->err;
5070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
5170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
5270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    cinfo->err = err;
5370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    cinfo->client_data = client_data;
5470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
5570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->is_decompressor = FALSE;
5670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
5770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Initialize a memory manager instance for this object */
5870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jinit_memory_mgr((j_common_ptr) cinfo);
5970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Zero out pointers to permanent structures. */
6170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->progress = NULL;
6270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->dest = NULL;
6370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->comp_info = NULL;
6570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = 0; i < NUM_QUANT_TBLS; i++)
6770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    cinfo->quant_tbl_ptrs[i] = NULL;
6870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
6970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = 0; i < NUM_HUFF_TBLS; i++) {
7070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    cinfo->dc_huff_tbl_ptrs[i] = NULL;
7170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    cinfo->ac_huff_tbl_ptrs[i] = NULL;
7270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
7370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->script_space = NULL;
7570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->input_gamma = 1.0;	/* in case application forgets */
7770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
7870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* OK, I'm ready */
7970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  cinfo->global_state = CSTATE_START;
8070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
8170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
8270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
8370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
8470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Destruction of a JPEG compression object
8570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
8670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
8770a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
8870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_destroy_compress (j_compress_ptr cinfo)
8970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
9070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
9170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
9270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
9570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Abort processing of a JPEG compression operation,
9670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * but don't destroy the object itself.
9770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
9870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
9970a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
10070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_abort_compress (j_compress_ptr cinfo)
10170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
10270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
10370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
10470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
10570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
10670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
10770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Forcibly suppress or un-suppress all quantization and Huffman tables.
10870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Marks all currently defined tables as already written (if suppress)
10970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * or not written (if !suppress).  This will control whether they get emitted
11070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * by a subsequent jpeg_start_compress call.
11170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
11270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This routine is exported for use by applications that want to produce
11370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
11470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * since it is called by jpeg_start_compress, we put it here --- otherwise
11570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * jcparam.o would be linked whether the application used it or not.
11670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
11770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
11870a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
11970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
12070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
12170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  int i;
12270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JQUANT_TBL * qtbl;
12370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JHUFF_TBL * htbl;
12470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
12570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = 0; i < NUM_QUANT_TBLS; i++) {
12670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
12770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      qtbl->sent_table = suppress;
12870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
12970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
13070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  for (i = 0; i < NUM_HUFF_TBLS; i++) {
13170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
13270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      htbl->sent_table = suppress;
13370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
13470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      htbl->sent_table = suppress;
13570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
13670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
13770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
13870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
13970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
14070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Finish JPEG compression.
14170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
14270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * If a multipass operating mode was selected, this may do a great deal of
14370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * work including most of the actual output.
14470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
14570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
14670a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
14770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_finish_compress (j_compress_ptr cinfo)
14870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
14970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JDIMENSION iMCU_row;
15070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
15170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (cinfo->global_state == CSTATE_SCANNING ||
15270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      cinfo->global_state == CSTATE_RAW_OK) {
15370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    /* Terminate first pass */
15470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    if (cinfo->next_scanline < cinfo->image_height)
15570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
15670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    (*cinfo->master->finish_pass) (cinfo);
15770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  } else if (cinfo->global_state != CSTATE_WRCOEFS)
15870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
15970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Perform any remaining passes */
16070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (! cinfo->master->is_last_pass) {
16170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    (*cinfo->master->prepare_for_pass) (cinfo);
16270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
16370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (cinfo->progress != NULL) {
16470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	cinfo->progress->pass_counter = (long) iMCU_row;
16570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
16670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
16770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      }
16870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      /* We bypass the main controller and invoke coef controller directly;
16970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       * all work is being done from the coefficient buffer.
17070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       */
17170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
17270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine	ERREXIT(cinfo, JERR_CANT_SUSPEND);
17370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    }
17470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    (*cinfo->master->finish_pass) (cinfo);
17570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
17670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Write EOI, do final cleanup */
17770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->marker->write_file_trailer) (cinfo);
17870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->dest->term_destination) (cinfo);
17970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* We can use jpeg_abort to release memory and reset global_state */
18070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jpeg_abort((j_common_ptr) cinfo);
18170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
18270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
18370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
18470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
18570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Write a special marker.
18670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * This is only recommended for writing COM or APPn markers.
18770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Must be called after jpeg_start_compress() and before
18870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
18970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
19070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
19170a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
19270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_write_marker (j_compress_ptr cinfo, int marker,
19370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine		   const JOCTET *dataptr, unsigned int datalen)
19470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
19570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
19670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
19770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (cinfo->next_scanline != 0 ||
19870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      (cinfo->global_state != CSTATE_SCANNING &&
19970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       cinfo->global_state != CSTATE_RAW_OK &&
20070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       cinfo->global_state != CSTATE_WRCOEFS))
20170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
20270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
20370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
20470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  write_marker_byte = cinfo->marker->write_marker_byte;	/* copy for speed */
20570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  while (datalen--) {
20670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    (*write_marker_byte) (cinfo, *dataptr);
20770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    dataptr++;
20870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  }
20970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
21070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
21170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/* Same, but piecemeal. */
21270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
21370a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
21470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
21570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
21670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (cinfo->next_scanline != 0 ||
21770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine      (cinfo->global_state != CSTATE_SCANNING &&
21870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       cinfo->global_state != CSTATE_RAW_OK &&
21970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine       cinfo->global_state != CSTATE_WRCOEFS))
22070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
22170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
22370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
22470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
22570a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
22670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_write_m_byte (j_compress_ptr cinfo, int val)
22770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
22870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->marker->write_marker_byte) (cinfo, val);
22970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
23070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
23170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
23270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine/*
23370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Alternate compression function: just write an abbreviated table file.
23470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * Before calling this, all parameters and a data destination must be set up.
23570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
23670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * To produce a pair of files containing abbreviated tables and abbreviated
23770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * image data, one would proceed as follows:
23870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
23970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *		initialize JPEG object
24070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *		set JPEG parameters
24170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *		set destination to table file
24270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *		jpeg_write_tables(cinfo);
24370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *		set destination to image file
24470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *		jpeg_start_compress(cinfo, FALSE);
24570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *		write data...
24670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *		jpeg_finish_compress(cinfo);
24770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine *
24870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * jpeg_write_tables has the side effect of marking all tables written
24970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
25070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine * will not re-emit the tables unless it is passed write_all_tables=TRUE.
25170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine */
25270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25370a18cd874a22452aca9e39e22275ed4538ed20bVladimir ChtchetkineGLOBAL(void)
25470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkinejpeg_write_tables (j_compress_ptr cinfo)
25570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine{
25670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  if (cinfo->global_state != CSTATE_START)
25770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
25870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine
25970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* (Re)initialize error mgr and destination modules */
26070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
26170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->dest->init_destination) (cinfo);
26270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Initialize the marker writer ... bit of a crock to do it here. */
26370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  jinit_marker_writer(cinfo);
26470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* Write them tables! */
26570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->marker->write_tables_only) (cinfo);
26670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /* And clean up. */
26770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  (*cinfo->dest->term_destination) (cinfo);
26870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine  /*
26970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * In library releases up through v6a, we called jpeg_abort() here to free
27070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * any working memory allocated by the destination manager and marker
27170a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * writer.  Some applications had a problem with that: they allocated space
27270a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * of their own from the library memory manager, and didn't want it to go
27370a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * away during write_tables.  So now we do nothing.  This will cause a
27470a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * memory leak if an app calls write_tables repeatedly without doing a full
27570a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * compression cycle or otherwise resetting the JPEG object.  However, that
27670a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * seems less bad than unexpectedly freeing memory in the normal case.
27770a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * An app that prefers the old behavior can call jpeg_abort for itself after
27870a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   * each call to jpeg_write_tables().
27970a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine   */
28070a18cd874a22452aca9e39e22275ed4538ed20bVladimir Chtchetkine}
281