source: golgotha/src/i4/loaders/jpg/jcapimin.cc

Last change on this file was 80, checked in by Sam Hocevar, 12 years ago
  • Adding the Golgotha source code. Not sure what's going to be interesting in there, but since it's all public domain, there's certainly stuff to pick up.
File size: 7.9 KB
Line 
1/********************************************************************** <BR>
2  This file is part of Crack dot Com's free source code release of
3  Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
4  information about compiling & licensing issues visit this URL</a>
5  <PRE> If that doesn't help, contact Jonathan Clark at
6  golgotha_source@usa.net (Subject should have "GOLG" in it)
7***********************************************************************/
8
9/*
10 * jcapimin.c
11 *
12 * Copyright (C) 1994-1996, Thomas G. Lane.
13 * This file is part of the Independent JPEG Group's software.
14 * For conditions of distribution and use, see the accompanying README file.
15 *
16 * This file contains application interface code for the compression half
17 * of the JPEG library.  These are the "minimum" API routines that may be
18 * needed in either the normal full-compression case or the transcoding-only
19 * case.
20 *
21 * Most of the routines intended to be called directly by an application
22 * are in this file or in jcapistd.c.  But also see jcparam.c for
23 * parameter-setup helper routines, jcomapi.c for routines shared by
24 * compression and decompression, and jctrans.c for the transcoding case.
25 */
26
27#define JPEG_INTERNALS
28#include "loaders/jpg/jinclude.h"
29#include "loaders/jpg/jpeglib.h"
30
31
32/*
33 * Initialization of a JPEG compression object.
34 * The error manager must already be set up (in case memory manager fails).
35 */
36
37GLOBAL(void)
38jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
39{
40  int i;
41
42  /* Guard against version mismatches between library and caller. */
43  cinfo->mem = NULL;            /* so jpeg_destroy knows mem mgr not called */
44  if (version != JPEG_LIB_VERSION)
45    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
46  if (structsize != SIZEOF(struct jpeg_compress_struct))
47    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
48             (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
49
50  /* For debugging purposes, zero the whole master structure.
51   * But error manager pointer is already there, so save and restore it.
52   */
53  {
54    struct jpeg_error_mgr * err = cinfo->err;
55    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
56    cinfo->err = err;
57  }
58  cinfo->is_decompressor = FALSE;
59
60  /* Initialize a memory manager instance for this object */
61  jinit_memory_mgr((j_common_ptr) cinfo);
62
63  /* Zero out pointers to permanent structures. */
64  cinfo->progress = NULL;
65  cinfo->dest = NULL;
66
67  cinfo->comp_info = NULL;
68
69  for (i = 0; i < NUM_QUANT_TBLS; i++)
70    cinfo->quant_tbl_ptrs[i] = NULL;
71
72  for (i = 0; i < NUM_HUFF_TBLS; i++) {
73    cinfo->dc_huff_tbl_ptrs[i] = NULL;
74    cinfo->ac_huff_tbl_ptrs[i] = NULL;
75  }
76
77  cinfo->input_gamma = 1.0;     /* in case application forgets */
78
79  /* OK, I'm ready */
80  cinfo->global_state = CSTATE_START;
81}
82
83
84/*
85 * Destruction of a JPEG compression object
86 */
87
88GLOBAL(void)
89jpeg_destroy_compress (j_compress_ptr cinfo)
90{
91  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
92}
93
94
95/*
96 * Abort processing of a JPEG compression operation,
97 * but don't destroy the object itself.
98 */
99
100GLOBAL(void)
101jpeg_abort_compress (j_compress_ptr cinfo)
102{
103  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
104}
105
106
107/*
108 * Forcibly suppress or un-suppress all quantization and Huffman tables.
109 * Marks all currently defined tables as already written (if suppress)
110 * or not written (if !suppress).  This will control whether they get emitted
111 * by a subsequent jpeg_start_compress call.
112 *
113 * This routine is exported for use by applications that want to produce
114 * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
115 * since it is called by jpeg_start_compress, we put it here --- otherwise
116 * jcparam.o would be linked whether the application used it or not.
117 */
118
119GLOBAL(void)
120jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
121{
122  int i;
123  JQUANT_TBL * qtbl;
124  JHUFF_TBL * htbl;
125
126  for (i = 0; i < NUM_QUANT_TBLS; i++) {
127    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
128      qtbl->sent_table = suppress;
129  }
130
131  for (i = 0; i < NUM_HUFF_TBLS; i++) {
132    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
133      htbl->sent_table = suppress;
134    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
135      htbl->sent_table = suppress;
136  }
137}
138
139
140/*
141 * Finish JPEG compression.
142 *
143 * If a multipass operating mode was selected, this may do a great deal of
144 * work including most of the actual output.
145 */
146
147GLOBAL(void)
148jpeg_finish_compress (j_compress_ptr cinfo)
149{
150  JDIMENSION iMCU_row;
151
152  if (cinfo->global_state == CSTATE_SCANNING ||
153      cinfo->global_state == CSTATE_RAW_OK) {
154    /* Terminate first pass */
155    if (cinfo->next_scanline < cinfo->image_height)
156      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
157    (*cinfo->master->finish_pass) (cinfo);
158  } else if (cinfo->global_state != CSTATE_WRCOEFS)
159    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
160  /* Perform any remaining passes */
161  while (! cinfo->master->is_last_pass) {
162    (*cinfo->master->prepare_for_pass) (cinfo);
163    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
164      if (cinfo->progress != NULL) {
165        cinfo->progress->pass_counter = (long) iMCU_row;
166        cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
167        (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
168      }
169      /* We bypass the main controller and invoke coef controller directly;
170       * all work is being done from the coefficient buffer.
171       */
172      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
173        ERREXIT(cinfo, JERR_CANT_SUSPEND);
174    }
175    (*cinfo->master->finish_pass) (cinfo);
176  }
177  /* Write EOI, do final cleanup */
178  (*cinfo->marker->write_file_trailer) (cinfo);
179  (*cinfo->dest->term_destination) (cinfo);
180  /* We can use jpeg_abort to release memory and reset global_state */
181  jpeg_abort((j_common_ptr) cinfo);
182}
183
184
185/*
186 * Write a special marker.
187 * This is only recommended for writing COM or APPn markers.
188 * Must be called after jpeg_start_compress() and before
189 * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
190 */
191
192GLOBAL(void)
193jpeg_write_marker (j_compress_ptr cinfo, int marker,
194                   const JOCTET *dataptr, unsigned int datalen)
195{
196  if (cinfo->next_scanline != 0 ||
197      (cinfo->global_state != CSTATE_SCANNING &&
198       cinfo->global_state != CSTATE_RAW_OK &&
199       cinfo->global_state != CSTATE_WRCOEFS))
200    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
201
202  (*cinfo->marker->write_any_marker) (cinfo, marker, dataptr, datalen);
203}
204
205
206/*
207 * Alternate compression function: just write an abbreviated table file.
208 * Before calling this, all parameters and a data destination must be set up.
209 *
210 * To produce a pair of files containing abbreviated tables and abbreviated
211 * image data, one would proceed as follows:
212 *
213 *              initialize JPEG object
214 *              set JPEG parameters
215 *              set destination to table file
216 *              jpeg_write_tables(cinfo);
217 *              set destination to image file
218 *              jpeg_start_compress(cinfo, FALSE);
219 *              write data...
220 *              jpeg_finish_compress(cinfo);
221 *
222 * jpeg_write_tables has the side effect of marking all tables written
223 * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
224 * will not re-emit the tables unless it is passed write_all_tables=TRUE.
225 */
226
227GLOBAL(void)
228jpeg_write_tables (j_compress_ptr cinfo)
229{
230  if (cinfo->global_state != CSTATE_START)
231    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
232
233  /* (Re)initialize error mgr and destination modules */
234  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
235  (*cinfo->dest->init_destination) (cinfo);
236  /* Initialize the marker writer ... bit of a crock to do it here. */
237  jinit_marker_writer(cinfo);
238  /* Write them tables! */
239  (*cinfo->marker->write_tables_only) (cinfo);
240  /* And clean up. */
241  (*cinfo->dest->term_destination) (cinfo);
242  /* We can use jpeg_abort to release memory. */
243  jpeg_abort((j_common_ptr) cinfo);
244}
Note: See TracBrowser for help on using the repository browser.