Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libjpeg-turbo/src/jcapistd.c
21874 views
1
/*
2
* jcapistd.c
3
*
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, 2024, D. R. Commander.
8
* For conditions of distribution and use, see the accompanying README.ijg
9
* file.
10
*
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.
18
*/
19
20
#define JPEG_INTERNALS
21
#include "jinclude.h"
22
#include "jpeglib.h"
23
#include "jsamplecomp.h"
24
25
26
#if BITS_IN_JSAMPLE == 8
27
28
/*
29
* Compression initialization.
30
* Before calling this, all parameters and a data destination must be set up.
31
*
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
40
* wrong thing.
41
*/
42
43
GLOBAL(void)
44
jpeg_start_compress(j_compress_ptr cinfo, boolean write_all_tables)
45
{
46
if (cinfo->global_state != CSTATE_START)
47
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
48
49
if (write_all_tables)
50
jpeg_suppress_tables(cinfo, FALSE); /* mark all tables to be written */
51
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.
61
*/
62
cinfo->next_scanline = 0;
63
cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
64
}
65
66
#endif
67
68
69
/*
70
* Write some scanlines of data to the JPEG compressor.
71
*
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.
76
*
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.
82
*/
83
84
GLOBAL(JDIMENSION)
85
_jpeg_write_scanlines(j_compress_ptr cinfo, _JSAMPARRAY scanlines,
86
JDIMENSION num_lines)
87
{
88
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
89
JDIMENSION row_ctr, rows_left;
90
91
#ifdef C_LOSSLESS_SUPPORTED
92
if (cinfo->master->lossless) {
93
#if BITS_IN_JSAMPLE == 8
94
if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
95
#else
96
if (cinfo->data_precision > BITS_IN_JSAMPLE ||
97
cinfo->data_precision < BITS_IN_JSAMPLE - 3)
98
#endif
99
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
100
} else
101
#endif
102
{
103
if (cinfo->data_precision != BITS_IN_JSAMPLE)
104
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
105
}
106
107
if (cinfo->global_state != CSTATE_SCANNING)
108
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
109
if (cinfo->next_scanline >= cinfo->image_height)
110
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
111
112
/* Call progress monitor hook if present */
113
if (cinfo->progress != NULL) {
114
cinfo->progress->pass_counter = (long)cinfo->next_scanline;
115
cinfo->progress->pass_limit = (long)cinfo->image_height;
116
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
117
}
118
119
/* Give master control module another chance if this is first call to
120
* _jpeg_write_scanlines. This lets output of the frame/scan headers be
121
* delayed so that application can write COM, etc, markers between
122
* jpeg_start_compress and _jpeg_write_scanlines.
123
*/
124
if (cinfo->master->call_pass_startup)
125
(*cinfo->master->pass_startup) (cinfo);
126
127
/* Ignore any extra scanlines at bottom of image. */
128
rows_left = cinfo->image_height - cinfo->next_scanline;
129
if (num_lines > rows_left)
130
num_lines = rows_left;
131
132
row_ctr = 0;
133
if (cinfo->main->_process_data == NULL)
134
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
135
(*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, num_lines);
136
cinfo->next_scanline += row_ctr;
137
return row_ctr;
138
#else
139
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
140
return 0;
141
#endif
142
}
143
144
145
#if BITS_IN_JSAMPLE != 16
146
147
/*
148
* Alternate entry point to write raw data.
149
* Processes exactly one iMCU row per call, unless suspended.
150
*/
151
152
GLOBAL(JDIMENSION)
153
_jpeg_write_raw_data(j_compress_ptr cinfo, _JSAMPIMAGE data,
154
JDIMENSION num_lines)
155
{
156
JDIMENSION lines_per_iMCU_row;
157
158
if (cinfo->data_precision != BITS_IN_JSAMPLE)
159
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
160
161
if (cinfo->master->lossless)
162
ERREXIT(cinfo, JERR_NOTIMPL);
163
164
if (cinfo->global_state != CSTATE_RAW_OK)
165
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
166
if (cinfo->next_scanline >= cinfo->image_height) {
167
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
168
return 0;
169
}
170
171
/* Call progress monitor hook if present */
172
if (cinfo->progress != NULL) {
173
cinfo->progress->pass_counter = (long)cinfo->next_scanline;
174
cinfo->progress->pass_limit = (long)cinfo->image_height;
175
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
176
}
177
178
/* Give master control module another chance if this is first call to
179
* _jpeg_write_raw_data. This lets output of the frame/scan headers be
180
* delayed so that application can write COM, etc, markers between
181
* jpeg_start_compress and _jpeg_write_raw_data.
182
*/
183
if (cinfo->master->call_pass_startup)
184
(*cinfo->master->pass_startup) (cinfo);
185
186
/* Verify that at least one iMCU row has been passed. */
187
lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
188
if (num_lines < lines_per_iMCU_row)
189
ERREXIT(cinfo, JERR_BUFFER_SIZE);
190
191
/* Directly compress the row. */
192
if (cinfo->coef->_compress_data == NULL)
193
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
194
if (!(*cinfo->coef->_compress_data) (cinfo, data)) {
195
/* If compressor did not consume the whole row, suspend processing. */
196
return 0;
197
}
198
199
/* OK, we processed one iMCU row. */
200
cinfo->next_scanline += lines_per_iMCU_row;
201
return lines_per_iMCU_row;
202
}
203
204
#endif /* BITS_IN_JSAMPLE != 16 */
205
206