Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libjpeg-turbo/src/jcapistd.c
9904 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
(*cinfo->main->_process_data) (cinfo, scanlines, &row_ctr, num_lines);
134
cinfo->next_scanline += row_ctr;
135
return row_ctr;
136
#else
137
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
138
return 0;
139
#endif
140
}
141
142
143
#if BITS_IN_JSAMPLE != 16
144
145
/*
146
* Alternate entry point to write raw data.
147
* Processes exactly one iMCU row per call, unless suspended.
148
*/
149
150
GLOBAL(JDIMENSION)
151
_jpeg_write_raw_data(j_compress_ptr cinfo, _JSAMPIMAGE data,
152
JDIMENSION num_lines)
153
{
154
JDIMENSION lines_per_iMCU_row;
155
156
if (cinfo->data_precision != BITS_IN_JSAMPLE)
157
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
158
159
if (cinfo->master->lossless)
160
ERREXIT(cinfo, JERR_NOTIMPL);
161
162
if (cinfo->global_state != CSTATE_RAW_OK)
163
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
164
if (cinfo->next_scanline >= cinfo->image_height) {
165
WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
166
return 0;
167
}
168
169
/* Call progress monitor hook if present */
170
if (cinfo->progress != NULL) {
171
cinfo->progress->pass_counter = (long)cinfo->next_scanline;
172
cinfo->progress->pass_limit = (long)cinfo->image_height;
173
(*cinfo->progress->progress_monitor) ((j_common_ptr)cinfo);
174
}
175
176
/* Give master control module another chance if this is first call to
177
* _jpeg_write_raw_data. This lets output of the frame/scan headers be
178
* delayed so that application can write COM, etc, markers between
179
* jpeg_start_compress and _jpeg_write_raw_data.
180
*/
181
if (cinfo->master->call_pass_startup)
182
(*cinfo->master->pass_startup) (cinfo);
183
184
/* Verify that at least one iMCU row has been passed. */
185
lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
186
if (num_lines < lines_per_iMCU_row)
187
ERREXIT(cinfo, JERR_BUFFER_SIZE);
188
189
/* Directly compress the row. */
190
if (!(*cinfo->coef->_compress_data) (cinfo, data)) {
191
/* If compressor did not consume the whole row, suspend processing. */
192
return 0;
193
}
194
195
/* OK, we processed one iMCU row. */
196
cinfo->next_scanline += lines_per_iMCU_row;
197
return lines_per_iMCU_row;
198
}
199
200
#endif /* BITS_IN_JSAMPLE != 16 */
201
202