1bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
2bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * jcapimin.c
3bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
45033f3e19a31e8ad40c1a79700365aefe5664494DRC * This file was part of the Independent JPEG Group's software:
55ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane * Copyright (C) 1994-1998, Thomas G. Lane.
6f18f81b7e20cf993786a3348960ab2428762deeeGuido Vollbeding * Modified 2003-2010 by Guido Vollbeding.
75033f3e19a31e8ad40c1a79700365aefe5664494DRC * It was modified by The libjpeg-turbo Project to include only code relevant
85033f3e19a31e8ad40c1a79700365aefe5664494DRC * to libjpeg-turbo.
96eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * For conditions of distribution and use, see the accompanying README.ijg
106eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis * file.
11bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
12bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This file contains application interface code for the compression half
13bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * of the JPEG library.  These are the "minimum" API routines that may be
14bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * needed in either the normal full-compression case or the transcoding-only
15bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * case.
16bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
17bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Most of the routines intended to be called directly by an application
18bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * are in this file or in jcapistd.c.  But also see jcparam.c for
19bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * parameter-setup helper routines, jcomapi.c for routines shared by
20bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * compression and decompression, and jctrans.c for the transcoding case.
21bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
22bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
23bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#define JPEG_INTERNALS
24bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "jinclude.h"
25bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "jpeglib.h"
26bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
27bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
28bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
29bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Initialization of a JPEG compression object.
30bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * The error manager must already be set up (in case memory manager fails).
31bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
32bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
33489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
34489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lanejpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
35bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
36bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int i;
37bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
38489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane  /* Guard against version mismatches between library and caller. */
39e5eaf37440b8e337ab150c017df7c03faf846c51DRC  cinfo->mem = NULL;            /* so jpeg_destroy knows mem mgr not called */
40489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane  if (version != JPEG_LIB_VERSION)
41489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
425de454b291f48382648a5d1dc2aa0fca8b5786d4DRC  if (structsize != sizeof(struct jpeg_compress_struct))
43e5eaf37440b8e337ab150c017df7c03faf846c51DRC    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
445de454b291f48382648a5d1dc2aa0fca8b5786d4DRC             (int) sizeof(struct jpeg_compress_struct), (int) structsize);
45489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane
465ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  /* For debugging purposes, we zero the whole master structure.
475ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * But the application has already set the err pointer, and may have set
485ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * client_data, so we have to save and restore those fields.
495ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * Note: if application hasn't set client_data, tools like Purify may
505ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * complain here.
51bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane   */
52bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  {
536eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis    struct jpeg_error_mgr *err = cinfo->err;
546eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis    void *client_data = cinfo->client_data; /* ignore Purify complaint here */
555de454b291f48382648a5d1dc2aa0fca8b5786d4DRC    MEMZERO(cinfo, sizeof(struct jpeg_compress_struct));
56bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->err = err;
575ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane    cinfo->client_data = client_data;
58bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
59bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->is_decompressor = FALSE;
60bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
61bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Initialize a memory manager instance for this object */
62bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  jinit_memory_mgr((j_common_ptr) cinfo);
63bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
64bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Zero out pointers to permanent structures. */
65bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->progress = NULL;
66bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->dest = NULL;
67bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
68bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->comp_info = NULL;
69bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
705996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  for (i = 0; i < NUM_QUANT_TBLS; i++) {
71bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->quant_tbl_ptrs[i] = NULL;
7236a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 70
735996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding    cinfo->q_scale_factor[i] = 100;
7436a6eec93250e390d1028b3372078810b4428eafDRC#endif
755996a25e2f50d20d6a8f09830724035b49c3927bGuido Vollbeding  }
76bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
77bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  for (i = 0; i < NUM_HUFF_TBLS; i++) {
78bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->dc_huff_tbl_ptrs[i] = NULL;
79bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    cinfo->ac_huff_tbl_ptrs[i] = NULL;
80bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
81bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
8236a6eec93250e390d1028b3372078810b4428eafDRC#if JPEG_LIB_VERSION >= 80
83f18f81b7e20cf993786a3348960ab2428762deeeGuido Vollbeding  /* Must do it here for emit_dqt in case jpeg_write_tables is used */
84f18f81b7e20cf993786a3348960ab2428762deeeGuido Vollbeding  cinfo->block_size = DCTSIZE;
85f18f81b7e20cf993786a3348960ab2428762deeeGuido Vollbeding  cinfo->natural_order = jpeg_natural_order;
86f18f81b7e20cf993786a3348960ab2428762deeeGuido Vollbeding  cinfo->lim_Se = DCTSIZE2-1;
8736a6eec93250e390d1028b3372078810b4428eafDRC#endif
88f18f81b7e20cf993786a3348960ab2428762deeeGuido Vollbeding
895ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  cinfo->script_space = NULL;
905ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
91e5eaf37440b8e337ab150c017df7c03faf846c51DRC  cinfo->input_gamma = 1.0;     /* in case application forgets */
92bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
93bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* OK, I'm ready */
94bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  cinfo->global_state = CSTATE_START;
95bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
96bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
97bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
98bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
99bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Destruction of a JPEG compression object
100bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
101bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
102489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
103bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_destroy_compress (j_compress_ptr cinfo)
104bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
105bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
106bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
107bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
108bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
109bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
110bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Abort processing of a JPEG compression operation,
111bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * but don't destroy the object itself.
112bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
113bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
114489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
115bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_abort_compress (j_compress_ptr cinfo)
116bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
117bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
118bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
119bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
120bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
121bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
122bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Forcibly suppress or un-suppress all quantization and Huffman tables.
123bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Marks all currently defined tables as already written (if suppress)
124bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * or not written (if !suppress).  This will control whether they get emitted
125bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * by a subsequent jpeg_start_compress call.
126bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
127bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This routine is exported for use by applications that want to produce
128bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
129bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * since it is called by jpeg_start_compress, we put it here --- otherwise
130bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * jcparam.o would be linked whether the application used it or not.
131bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
132bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
133489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
134bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
135bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
136bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  int i;
1376eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JQUANT_TBL *qtbl;
1386eb7d3798b5a79347c62825fc4c16f7ce673bdd0Alex Naidis  JHUFF_TBL *htbl;
139bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
140bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  for (i = 0; i < NUM_QUANT_TBLS; i++) {
141bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
142bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      qtbl->sent_table = suppress;
143bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
144bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
145bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  for (i = 0; i < NUM_HUFF_TBLS; i++) {
146bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
147bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      htbl->sent_table = suppress;
148bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
149bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      htbl->sent_table = suppress;
150bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
151bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
152bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
153bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
154bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
155bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Finish JPEG compression.
156bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
157bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * If a multipass operating mode was selected, this may do a great deal of
158bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * work including most of the actual output.
159bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
160bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
161489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
162bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_finish_compress (j_compress_ptr cinfo)
163bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
164bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  JDIMENSION iMCU_row;
165bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
166bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->global_state == CSTATE_SCANNING ||
167bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      cinfo->global_state == CSTATE_RAW_OK) {
168bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /* Terminate first pass */
169bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    if (cinfo->next_scanline < cinfo->image_height)
170bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
171bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->master->finish_pass) (cinfo);
172bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  } else if (cinfo->global_state != CSTATE_WRCOEFS)
173bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
174bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Perform any remaining passes */
175bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  while (! cinfo->master->is_last_pass) {
176bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->master->prepare_for_pass) (cinfo);
177bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
178bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (cinfo->progress != NULL) {
179e5eaf37440b8e337ab150c017df7c03faf846c51DRC        cinfo->progress->pass_counter = (long) iMCU_row;
180e5eaf37440b8e337ab150c017df7c03faf846c51DRC        cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
181e5eaf37440b8e337ab150c017df7c03faf846c51DRC        (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
182bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      }
183bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      /* We bypass the main controller and invoke coef controller directly;
184bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane       * all work is being done from the coefficient buffer.
185bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane       */
186bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
187e5eaf37440b8e337ab150c017df7c03faf846c51DRC        ERREXIT(cinfo, JERR_CANT_SUSPEND);
188bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
189bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    (*cinfo->master->finish_pass) (cinfo);
190bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  }
191bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Write EOI, do final cleanup */
192bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  (*cinfo->marker->write_file_trailer) (cinfo);
193bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  (*cinfo->dest->term_destination) (cinfo);
194bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* We can use jpeg_abort to release memory and reset global_state */
195bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  jpeg_abort((j_common_ptr) cinfo);
196bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
197bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
198bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
199bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
200bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Write a special marker.
201bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * This is only recommended for writing COM or APPn markers.
202bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Must be called after jpeg_start_compress() and before
203bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
204bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
205bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
206489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
207bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_write_marker (j_compress_ptr cinfo, int marker,
208e5eaf37440b8e337ab150c017df7c03faf846c51DRC                   const JOCTET *dataptr, unsigned int datalen)
209bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
210bc56b754e1a6a1db9ccadf64d6dda8a74140e1a3DRC  void (*write_marker_byte) (j_compress_ptr info, int val);
2115ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
2125ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  if (cinfo->next_scanline != 0 ||
2135ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane      (cinfo->global_state != CSTATE_SCANNING &&
2145ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane       cinfo->global_state != CSTATE_RAW_OK &&
2155ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane       cinfo->global_state != CSTATE_WRCOEFS))
2165ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
2175ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
2185ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
219e5eaf37440b8e337ab150c017df7c03faf846c51DRC  write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
2205ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  while (datalen--) {
2215ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane    (*write_marker_byte) (cinfo, *dataptr);
2225ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane    dataptr++;
2235ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  }
2245ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane}
2255ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
2265ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane/* Same, but piecemeal. */
2275ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
2285ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. LaneGLOBAL(void)
2295ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lanejpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
2305ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane{
231bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->next_scanline != 0 ||
232bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane      (cinfo->global_state != CSTATE_SCANNING &&
233bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane       cinfo->global_state != CSTATE_RAW_OK &&
234bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane       cinfo->global_state != CSTATE_WRCOEFS))
235bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
236bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
2375ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
2385ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane}
2395ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane
2405ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. LaneGLOBAL(void)
2415ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lanejpeg_write_m_byte (j_compress_ptr cinfo, int val)
2425ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane{
2435ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  (*cinfo->marker->write_marker_byte) (cinfo, val);
244bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
245bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
246bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
247bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
248bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Alternate compression function: just write an abbreviated table file.
249bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Before calling this, all parameters and a data destination must be set up.
250bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
251bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * To produce a pair of files containing abbreviated tables and abbreviated
252bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * image data, one would proceed as follows:
253bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
254e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              initialize JPEG object
255e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              set JPEG parameters
256e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              set destination to table file
257e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              jpeg_write_tables(cinfo);
258e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              set destination to image file
259e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              jpeg_start_compress(cinfo, FALSE);
260e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              write data...
261e5eaf37440b8e337ab150c017df7c03faf846c51DRC *              jpeg_finish_compress(cinfo);
262bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
263bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * jpeg_write_tables has the side effect of marking all tables written
264bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
265bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * will not re-emit the tables unless it is passed write_all_tables=TRUE.
266bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
267bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
268489583f5165e05d37302e8eeec58104ea0109127Thomas G. LaneGLOBAL(void)
269bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanejpeg_write_tables (j_compress_ptr cinfo)
270bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane{
271bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  if (cinfo->global_state != CSTATE_START)
272bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
273bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
274bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* (Re)initialize error mgr and destination modules */
275bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
276bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  (*cinfo->dest->init_destination) (cinfo);
277bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Initialize the marker writer ... bit of a crock to do it here. */
278bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  jinit_marker_writer(cinfo);
279bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* Write them tables! */
280bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  (*cinfo->marker->write_tables_only) (cinfo);
281bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  /* And clean up. */
282bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane  (*cinfo->dest->term_destination) (cinfo);
2835ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane  /*
2845ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * In library releases up through v6a, we called jpeg_abort() here to free
2855ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * any working memory allocated by the destination manager and marker
2865ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * writer.  Some applications had a problem with that: they allocated space
2875ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * of their own from the library memory manager, and didn't want it to go
2885ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * away during write_tables.  So now we do nothing.  This will cause a
2895ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * memory leak if an app calls write_tables repeatedly without doing a full
2905ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * compression cycle or otherwise resetting the JPEG object.  However, that
2915ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * seems less bad than unexpectedly freeing memory in the normal case.
2925ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * An app that prefers the old behavior can call jpeg_abort for itself after
2935ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   * each call to jpeg_write_tables().
2945ead57a34a398aa798f35bd7a6abad19b2e453e2Thomas G. Lane   */
295bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane}
296