Coverage Report

Created: 2025-06-22 06:59

/src/gdal/build/frmts/jpeg/libjpeg12/jcapimin12.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jcapimin.c
3
 *
4
 * Copyright (C) 1994-1998, Thomas G. Lane.
5
 * This file is part of the Independent JPEG Group's software.
6
 * For conditions of distribution and use, see the accompanying README file.
7
 *
8
 * This file contains application interface code for the compression half
9
 * of the JPEG library.  These are the "minimum" API routines that may be
10
 * needed in either the normal full-compression case or the transcoding-only
11
 * case.
12
 *
13
 * Most of the routines intended to be called directly by an application
14
 * are in this file or in jcapistd.c.  But also see jcparam.c for
15
 * parameter-setup helper routines, jcomapi.c for routines shared by
16
 * compression and decompression, and jctrans.c for the transcoding case.
17
 */
18
19
#define JPEG_INTERNALS
20
#include "jinclude.h"
21
#include "jpeglib.h"
22
23
24
/*
25
 * Initialization of a JPEG compression object.
26
 * The error manager must already be set up (in case memory manager fails).
27
 */
28
29
GLOBAL(void)
30
jpeg_CreateCompress (j_compress_ptr cinfo, int version, size_t structsize)
31
0
{
32
0
  int i;
33
34
  /* Guard against version mismatches between library and caller. */
35
0
  cinfo->mem = NULL;    /* so jpeg_destroy knows mem mgr not called */
36
0
  if (version != JPEG_LIB_VERSION)
37
0
    ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
38
0
  if (structsize != SIZEOF(struct jpeg_compress_struct))
39
0
    ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
40
0
       (int) SIZEOF(struct jpeg_compress_struct), (int) structsize);
41
42
  /* For debugging purposes, we zero the whole master structure.
43
   * But the application has already set the err pointer, and may have set
44
   * client_data, so we have to save and restore those fields.
45
   * Note: if application hasn't set client_data, tools like Purify may
46
   * complain here.
47
   */
48
0
  {
49
0
    struct jpeg_error_mgr * err = cinfo->err;
50
0
    void * client_data = cinfo->client_data; /* ignore Purify complaint here */
51
0
    MEMZERO(cinfo, SIZEOF(struct jpeg_compress_struct));
52
0
    cinfo->err = err;
53
0
    cinfo->client_data = client_data;
54
0
  }
55
0
  cinfo->is_decompressor = FALSE;
56
57
  /* Initialize a memory manager instance for this object */
58
0
  jinit_memory_mgr((j_common_ptr) cinfo);
59
60
  /* Zero out pointers to permanent structures. */
61
0
  cinfo->progress = NULL;
62
0
  cinfo->dest = NULL;
63
64
0
  cinfo->comp_info = NULL;
65
66
0
  for (i = 0; i < NUM_QUANT_TBLS; i++)
67
0
    cinfo->quant_tbl_ptrs[i] = NULL;
68
69
0
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
70
0
    cinfo->dc_huff_tbl_ptrs[i] = NULL;
71
0
    cinfo->ac_huff_tbl_ptrs[i] = NULL;
72
0
  }
73
74
0
  cinfo->script_space = NULL;
75
76
0
  cinfo->input_gamma = 1.0; /* in case application forgets */
77
78
  /* OK, I'm ready */
79
0
  cinfo->global_state = CSTATE_START;
80
0
}
81
82
83
/*
84
 * Destruction of a JPEG compression object
85
 */
86
87
GLOBAL(void)
88
jpeg_destroy_compress (j_compress_ptr cinfo)
89
0
{
90
0
  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
91
0
}
92
93
94
/*
95
 * Abort processing of a JPEG compression operation,
96
 * but don't destroy the object itself.
97
 */
98
99
GLOBAL(void)
100
jpeg_abort_compress (j_compress_ptr cinfo)
101
0
{
102
0
  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
103
0
}
104
105
106
/*
107
 * Forcibly suppress or un-suppress all quantization and Huffman tables.
108
 * Marks all currently defined tables as already written (if suppress)
109
 * or not written (if !suppress).  This will control whether they get emitted
110
 * by a subsequent jpeg_start_compress call.
111
 *
112
 * This routine is exported for use by applications that want to produce
113
 * abbreviated JPEG datastreams.  It logically belongs in jcparam.c, but
114
 * since it is called by jpeg_start_compress, we put it here --- otherwise
115
 * jcparam.o would be linked whether the application used it or not.
116
 */
117
118
GLOBAL(void)
119
jpeg_suppress_tables (j_compress_ptr cinfo, boolean suppress)
120
0
{
121
0
  int i;
122
0
  JQUANT_TBL * qtbl;
123
0
  JHUFF_TBL * htbl;
124
125
0
  for (i = 0; i < NUM_QUANT_TBLS; i++) {
126
0
    if ((qtbl = cinfo->quant_tbl_ptrs[i]) != NULL)
127
0
      qtbl->sent_table = suppress;
128
0
  }
129
130
0
  for (i = 0; i < NUM_HUFF_TBLS; i++) {
131
0
    if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != NULL)
132
0
      htbl->sent_table = suppress;
133
0
    if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != NULL)
134
0
      htbl->sent_table = suppress;
135
0
  }
136
0
}
137
138
139
/*
140
 * Finish JPEG compression.
141
 *
142
 * If a multipass operating mode was selected, this may do a great deal of
143
 * work including most of the actual output.
144
 */
145
146
GLOBAL(void)
147
jpeg_finish_compress (j_compress_ptr cinfo)
148
0
{
149
0
  JDIMENSION iMCU_row;
150
151
0
  if (cinfo->global_state == CSTATE_SCANNING ||
152
0
      cinfo->global_state == CSTATE_RAW_OK) {
153
    /* Terminate first pass */
154
0
    if (cinfo->next_scanline < cinfo->image_height)
155
0
      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
156
0
    (*cinfo->master->finish_pass) (cinfo);
157
0
  } else if (cinfo->global_state != CSTATE_WRCOEFS)
158
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
159
  /* Perform any remaining passes */
160
0
  while (! cinfo->master->is_last_pass) {
161
0
    (*cinfo->master->prepare_for_pass) (cinfo);
162
0
    for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++) {
163
0
      if (cinfo->progress != NULL) {
164
0
  cinfo->progress->pass_counter = (long) iMCU_row;
165
0
  cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows;
166
0
  (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
167
0
      }
168
      /* We bypass the main controller and invoke coef controller directly;
169
       * all work is being done from the coefficient buffer.
170
       */
171
0
      if (! (*cinfo->coef->compress_data) (cinfo, (JSAMPIMAGE) NULL))
172
0
  ERREXIT(cinfo, JERR_CANT_SUSPEND);
173
0
    }
174
0
    (*cinfo->master->finish_pass) (cinfo);
175
0
  }
176
  /* Write EOI, do final cleanup */
177
0
  (*cinfo->marker->write_file_trailer) (cinfo);
178
0
  (*cinfo->dest->term_destination) (cinfo);
179
  /* We can use jpeg_abort to release memory and reset global_state */
180
0
  jpeg_abort((j_common_ptr) cinfo);
181
0
}
182
183
184
/*
185
 * Write a special marker.
186
 * This is only recommended for writing COM or APPn markers.
187
 * Must be called after jpeg_start_compress() and before
188
 * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
189
 */
190
191
GLOBAL(void)
192
jpeg_write_marker (j_compress_ptr cinfo, int marker,
193
       const JOCTET *dataptr, unsigned int datalen)
194
0
{
195
0
  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
196
197
0
  if (cinfo->next_scanline != 0 ||
198
0
      (cinfo->global_state != CSTATE_SCANNING &&
199
0
       cinfo->global_state != CSTATE_RAW_OK &&
200
0
       cinfo->global_state != CSTATE_WRCOEFS))
201
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
202
203
0
  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
204
0
  write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
205
0
  while (datalen) {
206
0
    datalen --;
207
0
    (*write_marker_byte) (cinfo, *dataptr);
208
0
    dataptr++;
209
0
  }
210
0
}
211
212
/* Same, but piecemeal. */
213
214
GLOBAL(void)
215
jpeg_write_m_header (j_compress_ptr cinfo, int marker, unsigned int datalen)
216
0
{
217
0
  if (cinfo->next_scanline != 0 ||
218
0
      (cinfo->global_state != CSTATE_SCANNING &&
219
0
       cinfo->global_state != CSTATE_RAW_OK &&
220
0
       cinfo->global_state != CSTATE_WRCOEFS))
221
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
222
223
0
  (*cinfo->marker->write_marker_header) (cinfo, marker, datalen);
224
0
}
225
226
GLOBAL(void)
227
jpeg_write_m_byte (j_compress_ptr cinfo, int val)
228
0
{
229
0
  (*cinfo->marker->write_marker_byte) (cinfo, val);
230
0
}
231
232
233
/*
234
 * Alternate compression function: just write an abbreviated table file.
235
 * Before calling this, all parameters and a data destination must be set up.
236
 *
237
 * To produce a pair of files containing abbreviated tables and abbreviated
238
 * image data, one would proceed as follows:
239
 *
240
 *    initialize JPEG object
241
 *    set JPEG parameters
242
 *    set destination to table file
243
 *    jpeg_write_tables(cinfo);
244
 *    set destination to image file
245
 *    jpeg_start_compress(cinfo, FALSE);
246
 *    write data...
247
 *    jpeg_finish_compress(cinfo);
248
 *
249
 * jpeg_write_tables has the side effect of marking all tables written
250
 * (same as jpeg_suppress_tables(..., TRUE)).  Thus a subsequent start_compress
251
 * will not re-emit the tables unless it is passed write_all_tables=TRUE.
252
 */
253
254
GLOBAL(void)
255
jpeg_write_tables (j_compress_ptr cinfo)
256
0
{
257
0
  if (cinfo->global_state != CSTATE_START)
258
0
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
259
260
  /* (Re)initialize error mgr and destination modules */
261
0
  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
262
0
  (*cinfo->dest->init_destination) (cinfo);
263
  /* Initialize the marker writer ... bit of a crock to do it here. */
264
0
  jinit_marker_writer(cinfo);
265
  /* Write them tables! */
266
0
  (*cinfo->marker->write_tables_only) (cinfo);
267
  /* And clean up. */
268
0
  (*cinfo->dest->term_destination) (cinfo);
269
  /*
270
   * In library releases up through v6a, we called jpeg_abort() here to free
271
   * any working memory allocated by the destination manager and marker
272
   * writer.  Some applications had a problem with that: they allocated space
273
   * of their own from the library memory manager, and didn't want it to go
274
   * away during write_tables.  So now we do nothing.  This will cause a
275
   * memory leak if an app calls write_tables repeatedly without doing a full
276
   * compression cycle or otherwise resetting the JPEG object.  However, that
277
   * seems less bad than unexpectedly freeing memory in the normal case.
278
   * An app that prefers the old behavior can call jpeg_abort for itself after
279
   * each call to jpeg_write_tables().
280
   */
281
0
}