4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1994-1996, Thomas G. Lane.
6 * libjpeg-turbo Modifications:
7 * Copyright (C) 2022, D. R. Commander.
8 * For conditions of distribution and use, see the accompanying README.ijg
11 * This file contains application interface code for the compression half
12 * of the JPEG library. These are the "standard" API routines that are
13 * used in the normal full-compression case. They are not used by a
14 * transcoding-only application. Note that if an application links in
15 * jpeg_start_compress, it will end up linking in the entire compressor.
16 * We thus must separate this file from jcapimin.c to avoid linking the
17 * whole compression library into a transcoder.
20 #define JPEG_INTERNALS
23 #include "jsamplecomp.h"
26 #if BITS_IN_JSAMPLE == 8
29 * Compression initialization.
30 * Before calling this, all parameters and a data destination must be set up.
32 * We require a write_all_tables parameter as a failsafe check when writing
33 * multiple datastreams from the same compression object. Since prior runs
34 * will have left all the tables marked sent_table=TRUE, a subsequent run
35 * would emit an abbreviated stream (no tables) by default. This may be what
36 * is wanted, but for safety's sake it should not be the default behavior:
37 * programmers should have to make a deliberate choice to emit abbreviated
38 * images. Therefore the documentation and examples should encourage people
39 * to pass write_all_tables=TRUE; then it will take active thought to do the
44 jpeg_start_compress(j_compress_ptr cinfo, boolean write_all_tables)
46 if (cinfo->global_state != CSTATE_START)
47 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
50 jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
52 /* (Re)initialize error mgr and destination modules */
53 (*cinfo->err->reset_error_mgr) ((j_common_ptr)cinfo);
54 (*cinfo->dest->init_destination) (cinfo);
55 /* Perform master selection of active modules */
56 jinit_compress_master(cinfo);
57 /* Set up for the first pass */
58 (*cinfo->master->prepare_for_pass) (cinfo);
59 /* Ready for application to drive first pass through _jpeg_write_scanlines
60 * or _jpeg_write_raw_data.
62 cinfo->next_scanline = 0;
63 cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
70 * Write some scanlines of data to the JPEG compressor.
72 * The return value will be the number of lines actually written.
73 * This should be less than the supplied num_lines only in case that
74 * the data destination module has requested suspension of the compressor,
75 * or if more than image_height scanlines are passed in.
77 * Note: we warn about excess calls to _jpeg_write_scanlines() since
78 * this likely signals an application programmer error. However,
79 * excess scanlines passed in the last valid call are *silently* ignored,
80 * so that the application need not adjust num_lines for end-of-image
81 * when using a multiple-scanline buffer.
85 _jpeg_write_scanlines(j_compress_ptr cinfo, _JSAMPARRAY scanlines,
88 #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
89 JDIMENSION row_ctr, rows_left;
91 if (cinfo->data_precision != BITS_IN_JSAMPLE)
92 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
94 if (cinfo->global_state != CSTATE_SCANNING)
95 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
96 if (cinfo->next_scanline >= cinfo->image_height)
97 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
99 /* Call progress monitor hook if present */
100 if (cinfo->progress != NULL) {
101 cinfo->progress->pass_counter = (long)cinfo->next_scanline;
102 cinfo->progress->pass_limit = (long)cinfo->image_height;
103 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
106 /* Give master control module another chance if this is first call to
107 * _jpeg_write_scanlines. This lets output of the frame/scan headers be
108 * delayed so that application can write COM, etc, markers between
109 * jpeg_start_compress and _jpeg_write_scanlines.
111 if (cinfo->master->call_pass_startup)
112 (*cinfo->master->pass_startup) (cinfo);
114 /* Ignore any extra scanlines at bottom of image. */
115 rows_left = cinfo->image_height - cinfo->next_scanline;
116 if (num_lines > rows_left)
117 num_lines = rows_left;
120 (*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, num_lines);
121 cinfo->next_scanline += row_ctr;
124 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
130 #if BITS_IN_JSAMPLE != 16
133 * Alternate entry point to write raw data.
134 * Processes exactly one iMCU row per call, unless suspended.
138 _jpeg_write_raw_data(j_compress_ptr cinfo, _JSAMPIMAGE data,
139 JDIMENSION num_lines)
141 JDIMENSION lines_per_iMCU_row;
143 if (cinfo->data_precision != BITS_IN_JSAMPLE)
144 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
146 if (cinfo->master->lossless)
147 ERREXIT(cinfo, JERR_NOTIMPL);
149 if (cinfo->global_state != CSTATE_RAW_OK)
150 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
151 if (cinfo->next_scanline >= cinfo->image_height) {
152 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
156 /* Call progress monitor hook if present */
157 if (cinfo->progress != NULL) {
158 cinfo->progress->pass_counter = (long)cinfo->next_scanline;
159 cinfo->progress->pass_limit = (long)cinfo->image_height;
160 (*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
163 /* Give master control module another chance if this is first call to
164 * _jpeg_write_raw_data. This lets output of the frame/scan headers be
165 * delayed so that application can write COM, etc, markers between
166 * jpeg_start_compress and _jpeg_write_raw_data.
168 if (cinfo->master->call_pass_startup)
169 (*cinfo->master->pass_startup) (cinfo);
171 /* Verify that at least one iMCU row has been passed. */
172 lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
173 if (num_lines < lines_per_iMCU_row)
174 ERREXIT(cinfo, JERR_BUFFER_SIZE);
176 /* Directly compress the row. */
177 if (!(*cinfo->coef->_compress_data) (cinfo, data)) {
178 /* If compressor did not consume the whole row, suspend processing. */
182 /* OK, we processed one iMCU row. */
183 cinfo->next_scanline += lines_per_iMCU_row;
184 return lines_per_iMCU_row;
187 #endif /* BITS_IN_JSAMPLE != 16 */