Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/native/libjavajpeg/jctrans.c
41149 views
1
/*
2
* reserved comment block
3
* DO NOT REMOVE OR ALTER!
4
*/
5
/*
6
* jctrans.c
7
*
8
* Copyright (C) 1995-1998, Thomas G. Lane.
9
* This file is part of the Independent JPEG Group's software.
10
* For conditions of distribution and use, see the accompanying README file.
11
*
12
* This file contains library routines for transcoding compression,
13
* that is, writing raw DCT coefficient arrays to an output JPEG file.
14
* The routines in jcapimin.c will also be needed by a transcoder.
15
*/
16
17
#define JPEG_INTERNALS
18
#include "jinclude.h"
19
#include "jpeglib.h"
20
21
22
/* Forward declarations */
23
LOCAL(void) transencode_master_selection
24
JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
25
LOCAL(void) transencode_coef_controller
26
JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
27
28
29
/*
30
* Compression initialization for writing raw-coefficient data.
31
* Before calling this, all parameters and a data destination must be set up.
32
* Call jpeg_finish_compress() to actually write the data.
33
*
34
* The number of passed virtual arrays must match cinfo->num_components.
35
* Note that the virtual arrays need not be filled or even realized at
36
* the time write_coefficients is called; indeed, if the virtual arrays
37
* were requested from this compression object's memory manager, they
38
* typically will be realized during this routine and filled afterwards.
39
*/
40
41
GLOBAL(void)
42
jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
43
{
44
if (cinfo->global_state != CSTATE_START)
45
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
46
/* Mark all tables to be written */
47
jpeg_suppress_tables(cinfo, FALSE);
48
/* (Re)initialize error mgr and destination modules */
49
(*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
50
(*cinfo->dest->init_destination) (cinfo);
51
/* Perform master selection of active modules */
52
transencode_master_selection(cinfo, coef_arrays);
53
/* Wait for jpeg_finish_compress() call */
54
cinfo->next_scanline = 0; /* so jpeg_write_marker works */
55
cinfo->global_state = CSTATE_WRCOEFS;
56
}
57
58
59
/*
60
* Initialize the compression object with default parameters,
61
* then copy from the source object all parameters needed for lossless
62
* transcoding. Parameters that can be varied without loss (such as
63
* scan script and Huffman optimization) are left in their default states.
64
*/
65
66
GLOBAL(void)
67
jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
68
j_compress_ptr dstinfo)
69
{
70
JQUANT_TBL ** qtblptr;
71
jpeg_component_info *incomp, *outcomp;
72
JQUANT_TBL *c_quant, *slot_quant;
73
int tblno, ci, coefi;
74
75
/* Safety check to ensure start_compress not called yet. */
76
if (dstinfo->global_state != CSTATE_START)
77
ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
78
/* Copy fundamental image dimensions */
79
dstinfo->image_width = srcinfo->image_width;
80
dstinfo->image_height = srcinfo->image_height;
81
dstinfo->input_components = srcinfo->num_components;
82
dstinfo->in_color_space = srcinfo->jpeg_color_space;
83
/* Initialize all parameters to default values */
84
jpeg_set_defaults(dstinfo);
85
/* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
86
* Fix it to get the right header markers for the image colorspace.
87
*/
88
jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
89
dstinfo->data_precision = srcinfo->data_precision;
90
dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
91
/* Copy the source's quantization tables. */
92
for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
93
if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
94
qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
95
if (*qtblptr == NULL)
96
*qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
97
MEMCOPY((*qtblptr)->quantval,
98
srcinfo->quant_tbl_ptrs[tblno]->quantval,
99
SIZEOF((*qtblptr)->quantval));
100
(*qtblptr)->sent_table = FALSE;
101
}
102
}
103
/* Copy the source's per-component info.
104
* Note we assume jpeg_set_defaults has allocated the dest comp_info array.
105
*/
106
dstinfo->num_components = srcinfo->num_components;
107
if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
108
ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
109
MAX_COMPONENTS);
110
for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
111
ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
112
outcomp->component_id = incomp->component_id;
113
outcomp->h_samp_factor = incomp->h_samp_factor;
114
outcomp->v_samp_factor = incomp->v_samp_factor;
115
outcomp->quant_tbl_no = incomp->quant_tbl_no;
116
/* Make sure saved quantization table for component matches the qtable
117
* slot. If not, the input file re-used this qtable slot.
118
* IJG encoder currently cannot duplicate this.
119
*/
120
tblno = outcomp->quant_tbl_no;
121
if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
122
srcinfo->quant_tbl_ptrs[tblno] == NULL)
123
ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
124
slot_quant = srcinfo->quant_tbl_ptrs[tblno];
125
c_quant = incomp->quant_table;
126
if (c_quant != NULL) {
127
for (coefi = 0; coefi < DCTSIZE2; coefi++) {
128
if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
129
ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
130
}
131
}
132
/* Note: we do not copy the source's Huffman table assignments;
133
* instead we rely on jpeg_set_colorspace to have made a suitable choice.
134
*/
135
}
136
/* Also copy JFIF version and resolution information, if available.
137
* Strictly speaking this isn't "critical" info, but it's nearly
138
* always appropriate to copy it if available. In particular,
139
* if the application chooses to copy JFIF 1.02 extension markers from
140
* the source file, we need to copy the version to make sure we don't
141
* emit a file that has 1.02 extensions but a claimed version of 1.01.
142
* We will *not*, however, copy version info from mislabeled "2.01" files.
143
*/
144
if (srcinfo->saw_JFIF_marker) {
145
if (srcinfo->JFIF_major_version == 1) {
146
dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
147
dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
148
}
149
dstinfo->density_unit = srcinfo->density_unit;
150
dstinfo->X_density = srcinfo->X_density;
151
dstinfo->Y_density = srcinfo->Y_density;
152
}
153
}
154
155
156
/*
157
* Master selection of compression modules for transcoding.
158
* This substitutes for jcinit.c's initialization of the full compressor.
159
*/
160
161
LOCAL(void)
162
transencode_master_selection (j_compress_ptr cinfo,
163
jvirt_barray_ptr * coef_arrays)
164
{
165
/* Although we don't actually use input_components for transcoding,
166
* jcmaster.c's initial_setup will complain if input_components is 0.
167
*/
168
cinfo->input_components = 1;
169
/* Initialize master control (includes parameter checking/processing) */
170
jinit_c_master_control(cinfo, TRUE /* transcode only */);
171
172
/* Entropy encoding: either Huffman or arithmetic coding. */
173
if (cinfo->arith_code) {
174
ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
175
} else {
176
if (cinfo->progressive_mode) {
177
#ifdef C_PROGRESSIVE_SUPPORTED
178
jinit_phuff_encoder(cinfo);
179
#else
180
ERREXIT(cinfo, JERR_NOT_COMPILED);
181
#endif
182
} else
183
jinit_huff_encoder(cinfo);
184
}
185
186
/* We need a special coefficient buffer controller. */
187
transencode_coef_controller(cinfo, coef_arrays);
188
189
jinit_marker_writer(cinfo);
190
191
/* We can now tell the memory manager to allocate virtual arrays. */
192
(*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
193
194
/* Write the datastream header (SOI, JFIF) immediately.
195
* Frame and scan headers are postponed till later.
196
* This lets application insert special markers after the SOI.
197
*/
198
(*cinfo->marker->write_file_header) (cinfo);
199
}
200
201
202
/*
203
* The rest of this file is a special implementation of the coefficient
204
* buffer controller. This is similar to jccoefct.c, but it handles only
205
* output from presupplied virtual arrays. Furthermore, we generate any
206
* dummy padding blocks on-the-fly rather than expecting them to be present
207
* in the arrays.
208
*/
209
210
/* Private buffer controller object */
211
212
typedef struct {
213
struct jpeg_c_coef_controller pub; /* public fields */
214
215
JDIMENSION iMCU_row_num; /* iMCU row # within image */
216
JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
217
int MCU_vert_offset; /* counts MCU rows within iMCU row */
218
int MCU_rows_per_iMCU_row; /* number of such rows needed */
219
220
/* Virtual block array for each component. */
221
jvirt_barray_ptr * whole_image;
222
223
/* Workspace for constructing dummy blocks at right/bottom edges. */
224
JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
225
} my_coef_controller;
226
227
typedef my_coef_controller * my_coef_ptr;
228
229
230
LOCAL(void)
231
start_iMCU_row (j_compress_ptr cinfo)
232
/* Reset within-iMCU-row counters for a new row */
233
{
234
my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
235
236
/* In an interleaved scan, an MCU row is the same as an iMCU row.
237
* In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
238
* But at the bottom of the image, process only what's left.
239
*/
240
if (cinfo->comps_in_scan > 1) {
241
coef->MCU_rows_per_iMCU_row = 1;
242
} else {
243
if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
244
coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
245
else
246
coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
247
}
248
249
coef->mcu_ctr = 0;
250
coef->MCU_vert_offset = 0;
251
}
252
253
254
/*
255
* Initialize for a processing pass.
256
*/
257
258
METHODDEF(void)
259
start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
260
{
261
my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
262
263
if (pass_mode != JBUF_CRANK_DEST)
264
ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
265
266
coef->iMCU_row_num = 0;
267
start_iMCU_row(cinfo);
268
}
269
270
271
/*
272
* Process some data.
273
* We process the equivalent of one fully interleaved MCU row ("iMCU" row)
274
* per call, ie, v_samp_factor block rows for each component in the scan.
275
* The data is obtained from the virtual arrays and fed to the entropy coder.
276
* Returns TRUE if the iMCU row is completed, FALSE if suspended.
277
*
278
* NB: input_buf is ignored; it is likely to be a NULL pointer.
279
*/
280
281
METHODDEF(boolean)
282
compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
283
{
284
my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
285
JDIMENSION MCU_col_num; /* index of current MCU within row */
286
JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
287
JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
288
int blkn, ci, xindex, yindex, yoffset, blockcnt;
289
JDIMENSION start_col;
290
JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
291
JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
292
JBLOCKROW buffer_ptr;
293
jpeg_component_info *compptr;
294
295
/* Align the virtual buffers for the components used in this scan. */
296
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
297
compptr = cinfo->cur_comp_info[ci];
298
buffer[ci] = (*cinfo->mem->access_virt_barray)
299
((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
300
coef->iMCU_row_num * compptr->v_samp_factor,
301
(JDIMENSION) compptr->v_samp_factor, FALSE);
302
}
303
304
/* Loop to process one whole iMCU row */
305
for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
306
yoffset++) {
307
for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
308
MCU_col_num++) {
309
/* Construct list of pointers to DCT blocks belonging to this MCU */
310
blkn = 0; /* index of current DCT block within MCU */
311
for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
312
compptr = cinfo->cur_comp_info[ci];
313
start_col = MCU_col_num * compptr->MCU_width;
314
blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
315
: compptr->last_col_width;
316
for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
317
if (coef->iMCU_row_num < last_iMCU_row ||
318
yindex+yoffset < compptr->last_row_height) {
319
/* Fill in pointers to real blocks in this row */
320
buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
321
for (xindex = 0; xindex < blockcnt; xindex++)
322
MCU_buffer[blkn++] = buffer_ptr++;
323
} else {
324
/* At bottom of image, need a whole row of dummy blocks */
325
xindex = 0;
326
}
327
/* Fill in any dummy blocks needed in this row.
328
* Dummy blocks are filled in the same way as in jccoefct.c:
329
* all zeroes in the AC entries, DC entries equal to previous
330
* block's DC value. The init routine has already zeroed the
331
* AC entries, so we need only set the DC entries correctly.
332
*/
333
for (; xindex < compptr->MCU_width; xindex++) {
334
MCU_buffer[blkn] = coef->dummy_buffer[blkn];
335
MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
336
blkn++;
337
}
338
}
339
}
340
/* Try to write the MCU. */
341
if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
342
/* Suspension forced; update state counters and exit */
343
coef->MCU_vert_offset = yoffset;
344
coef->mcu_ctr = MCU_col_num;
345
return FALSE;
346
}
347
}
348
/* Completed an MCU row, but perhaps not an iMCU row */
349
coef->mcu_ctr = 0;
350
}
351
/* Completed the iMCU row, advance counters for next one */
352
coef->iMCU_row_num++;
353
start_iMCU_row(cinfo);
354
return TRUE;
355
}
356
357
358
/*
359
* Initialize coefficient buffer controller.
360
*
361
* Each passed coefficient array must be the right size for that
362
* coefficient: width_in_blocks wide and height_in_blocks high,
363
* with unitheight at least v_samp_factor.
364
*/
365
366
LOCAL(void)
367
transencode_coef_controller (j_compress_ptr cinfo,
368
jvirt_barray_ptr * coef_arrays)
369
{
370
my_coef_ptr coef;
371
JBLOCKROW buffer;
372
int i;
373
374
coef = (my_coef_ptr)
375
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
376
SIZEOF(my_coef_controller));
377
cinfo->coef = (struct jpeg_c_coef_controller *) coef;
378
coef->pub.start_pass = start_pass_coef;
379
coef->pub.compress_data = compress_output;
380
381
/* Save pointer to virtual arrays */
382
coef->whole_image = coef_arrays;
383
384
/* Allocate and pre-zero space for dummy DCT blocks. */
385
buffer = (JBLOCKROW)
386
(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
387
C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
388
jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
389
for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
390
coef->dummy_buffer[i] = buffer + i;
391
}
392
}
393
394