4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1991-1997, Thomas G. Lane.
6 * Lossless JPEG Modifications:
7 * Copyright (C) 1999, Ken Murchison.
8 * libjpeg-turbo Modifications:
9 * Copyright (C) 2020, 2022, D. R. Commander.
10 * For conditions of distribution and use, see the accompanying README.ijg
13 * This file contains initialization logic for the JPEG compressor.
14 * This routine is in charge of selecting the modules to be executed and
15 * making an initialization call to each one.
17 * Logically, this code belongs in jcmaster.c. It's split out because
18 * linking this routine implies linking the entire compression library.
19 * For a transcoding-only application, we want to be able to use jcmaster.c
20 * without linking in the whole library.
23 #define JPEG_INTERNALS
26 #include "jpegapicomp.h"
30 * Master selection of compression modules.
31 * This is done once at the start of processing an image. We determine
32 * which modules will be used and give them appropriate initialization calls.
36 jinit_compress_master(j_compress_ptr cinfo
)
38 /* Initialize master control (includes parameter checking/processing) */
39 jinit_c_master_control(cinfo
, FALSE
/* full compression */);
42 if (!cinfo
->raw_data_in
) {
43 if (cinfo
->data_precision
== 16) {
44 #ifdef C_LOSSLESS_SUPPORTED
45 j16init_color_converter(cinfo
);
46 j16init_downsampler(cinfo
);
47 j16init_c_prep_controller(cinfo
,
48 FALSE
/* never need full buffer here */);
50 ERREXIT1(cinfo
, JERR_BAD_PRECISION
, cinfo
->data_precision
);
52 } else if (cinfo
->data_precision
== 12) {
53 j12init_color_converter(cinfo
);
54 j12init_downsampler(cinfo
);
55 j12init_c_prep_controller(cinfo
,
56 FALSE
/* never need full buffer here */);
58 jinit_color_converter(cinfo
);
59 jinit_downsampler(cinfo
);
60 jinit_c_prep_controller(cinfo
, FALSE
/* never need full buffer here */);
64 if (cinfo
->master
->lossless
) {
65 #ifdef C_LOSSLESS_SUPPORTED
66 /* Prediction, sample differencing, and point transform */
67 if (cinfo
->data_precision
== 16)
68 j16init_lossless_compressor(cinfo
);
69 else if (cinfo
->data_precision
== 12)
70 j12init_lossless_compressor(cinfo
);
72 jinit_lossless_compressor(cinfo
);
73 /* Entropy encoding: either Huffman or arithmetic coding. */
74 if (cinfo
->arith_code
) {
75 ERREXIT(cinfo
, JERR_ARITH_NOTIMPL
);
77 jinit_lhuff_encoder(cinfo
);
80 /* Need a full-image difference buffer in any multi-pass mode. */
81 if (cinfo
->data_precision
== 16)
82 j16init_c_diff_controller(cinfo
, (boolean
)(cinfo
->num_scans
> 1 ||
83 cinfo
->optimize_coding
));
84 else if (cinfo
->data_precision
== 12)
85 j12init_c_diff_controller(cinfo
, (boolean
)(cinfo
->num_scans
> 1 ||
86 cinfo
->optimize_coding
));
88 jinit_c_diff_controller(cinfo
, (boolean
)(cinfo
->num_scans
> 1 ||
89 cinfo
->optimize_coding
));
91 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
94 if (cinfo
->data_precision
== 16)
95 ERREXIT1(cinfo
, JERR_BAD_PRECISION
, cinfo
->data_precision
);
97 if (cinfo
->data_precision
== 12)
98 j12init_forward_dct(cinfo
);
100 jinit_forward_dct(cinfo
);
101 /* Entropy encoding: either Huffman or arithmetic coding. */
102 if (cinfo
->arith_code
) {
103 #ifdef C_ARITH_CODING_SUPPORTED
104 jinit_arith_encoder(cinfo
);
106 ERREXIT(cinfo
, JERR_ARITH_NOTIMPL
);
109 if (cinfo
->progressive_mode
) {
110 #ifdef C_PROGRESSIVE_SUPPORTED
111 jinit_phuff_encoder(cinfo
);
113 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
116 jinit_huff_encoder(cinfo
);
119 /* Need a full-image coefficient buffer in any multi-pass mode. */
120 if (cinfo
->data_precision
== 12)
121 j12init_c_coef_controller(cinfo
, (boolean
)(cinfo
->num_scans
> 1 ||
122 cinfo
->optimize_coding
));
124 jinit_c_coef_controller(cinfo
, (boolean
)(cinfo
->num_scans
> 1 ||
125 cinfo
->optimize_coding
));
128 if (cinfo
->data_precision
== 16)
129 #ifdef C_LOSSLESS_SUPPORTED
130 j16init_c_main_controller(cinfo
, FALSE
/* never need full buffer here */);
132 ERREXIT1(cinfo
, JERR_BAD_PRECISION
, cinfo
->data_precision
);
134 else if (cinfo
->data_precision
== 12)
135 j12init_c_main_controller(cinfo
, FALSE
/* never need full buffer here */);
137 jinit_c_main_controller(cinfo
, FALSE
/* never need full buffer here */);
139 jinit_marker_writer(cinfo
);
141 /* We can now tell the memory manager to allocate virtual arrays. */
142 (*cinfo
->mem
->realize_virt_arrays
) ((j_common_ptr
)cinfo
);
144 /* Write the datastream header (SOI) immediately.
145 * Frame and scan headers are postponed till later.
146 * This lets application insert special markers after the SOI.
148 (*cinfo
->marker
->write_file_header
) (cinfo
);