Coverage Report

Created: 2026-04-28 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/jcinit.c
Line
Count
Source
1
/*
2
 * jcinit.c
3
 *
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, 2026, D. R. Commander.
10
 * For conditions of distribution and use, see the accompanying README.ijg
11
 * file.
12
 *
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.
16
 *
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.
21
 */
22
23
#define JPEG_INTERNALS
24
#include "jinclude.h"
25
#include "jpeglib.h"
26
#include "jpegapicomp.h"
27
28
29
/*
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.
33
 */
34
35
GLOBAL(void)
36
jinit_compress_master(j_compress_ptr cinfo)
37
57.7k
{
38
  /* Initialize master control (includes parameter checking/processing) */
39
57.7k
  jinit_c_master_control(cinfo, FALSE /* full compression */);
40
41
  /* Preprocessing */
42
57.7k
  if (!cinfo->raw_data_in) {
43
46.2k
    if (cinfo->data_precision == 16) {
44
4.72k
#ifdef C_LOSSLESS_SUPPORTED
45
4.72k
      j16init_color_converter(cinfo);
46
4.72k
      j16init_downsampler(cinfo);
47
4.72k
      j16init_c_prep_controller(cinfo,
48
4.72k
                                FALSE /* never need full buffer here */);
49
#else
50
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
51
#endif
52
41.5k
    } else if (cinfo->data_precision == 12) {
53
18.2k
      j12init_color_converter(cinfo);
54
18.2k
      j12init_downsampler(cinfo);
55
18.2k
      j12init_c_prep_controller(cinfo,
56
18.2k
                                FALSE /* never need full buffer here */);
57
23.2k
    } else {
58
23.2k
      jinit_color_converter(cinfo);
59
23.2k
      jinit_downsampler(cinfo);
60
23.2k
      jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
61
23.2k
    }
62
46.2k
  }
63
64
57.7k
  if (cinfo->master->lossless) {
65
13.4k
#ifdef C_LOSSLESS_SUPPORTED
66
    /* Prediction, sample differencing, and point transform */
67
13.4k
    if (cinfo->data_precision == 16)
68
4.72k
      j16init_lossless_compressor(cinfo);
69
8.74k
    else if (cinfo->data_precision == 12)
70
4.37k
      j12init_lossless_compressor(cinfo);
71
4.37k
    else
72
4.37k
      jinit_lossless_compressor(cinfo);
73
    /* Entropy encoding: either Huffman or arithmetic coding. */
74
13.4k
    if (cinfo->arith_code) {
75
0
      ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
76
13.4k
    } else {
77
13.4k
      jinit_lhuff_encoder(cinfo);
78
13.4k
    }
79
80
    /* Need a full-image difference buffer in any multi-pass mode. */
81
13.4k
    if (cinfo->data_precision == 16)
82
4.72k
      j16init_c_diff_controller(cinfo, (boolean)(cinfo->num_scans > 1 ||
83
4.72k
                                                 cinfo->optimize_coding));
84
8.74k
    else if (cinfo->data_precision == 12)
85
4.37k
      j12init_c_diff_controller(cinfo, (boolean)(cinfo->num_scans > 1 ||
86
4.37k
                                                 cinfo->optimize_coding));
87
4.37k
    else
88
4.37k
      jinit_c_diff_controller(cinfo, (boolean)(cinfo->num_scans > 1 ||
89
4.37k
                                               cinfo->optimize_coding));
90
#else
91
    ERREXIT(cinfo, JERR_NOT_COMPILED);
92
#endif
93
44.3k
  } else {
94
44.3k
#if defined(DCT_ISLOW_SUPPORTED) || defined(DCT_IFAST_SUPPORTED) || \
95
44.3k
    defined(DCT_FLOAT_SUPPORTED)
96
44.3k
    if (cinfo->data_precision == 16)
97
0
      ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
98
    /* Forward DCT */
99
44.3k
    if (cinfo->data_precision == 12)
100
13.8k
      j12init_forward_dct(cinfo);
101
30.4k
    else
102
30.4k
      jinit_forward_dct(cinfo);
103
    /* Entropy encoding: either Huffman or arithmetic coding. */
104
44.3k
    if (cinfo->arith_code) {
105
13.9k
#ifdef C_ARITH_CODING_SUPPORTED
106
13.9k
      jinit_arith_encoder(cinfo);
107
#else
108
      ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
109
#endif
110
30.4k
    } else {
111
30.4k
      if (cinfo->progressive_mode) {
112
6.05k
#ifdef C_PROGRESSIVE_SUPPORTED
113
6.05k
        jinit_phuff_encoder(cinfo);
114
#else
115
        ERREXIT(cinfo, JERR_NOT_COMPILED);
116
#endif
117
6.05k
      } else
118
24.3k
        jinit_huff_encoder(cinfo);
119
30.4k
    }
120
121
    /* Need a full-image coefficient buffer in any multi-pass mode. */
122
44.3k
    if (cinfo->data_precision == 12)
123
13.8k
      j12init_c_coef_controller(cinfo, (boolean)(cinfo->num_scans > 1 ||
124
9.80k
                                                 cinfo->optimize_coding));
125
30.4k
    else
126
30.4k
      jinit_c_coef_controller(cinfo, (boolean)(cinfo->num_scans > 1 ||
127
21.7k
                                               cinfo->optimize_coding));
128
#else
129
    ERREXIT(cinfo, JERR_NOT_COMPILED);
130
#endif
131
44.3k
  }
132
133
57.7k
  if (cinfo->data_precision == 16)
134
4.72k
#ifdef C_LOSSLESS_SUPPORTED
135
4.72k
    j16init_c_main_controller(cinfo, FALSE /* never need full buffer here */);
136
#else
137
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
138
#endif
139
53.0k
  else if (cinfo->data_precision == 12)
140
18.2k
    j12init_c_main_controller(cinfo, FALSE /* never need full buffer here */);
141
34.8k
  else
142
34.8k
    jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
143
144
57.7k
  jinit_marker_writer(cinfo);
145
146
  /* We can now tell the memory manager to allocate virtual arrays. */
147
57.7k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
148
149
  /* Write the datastream header (SOI) immediately.
150
   * Frame and scan headers are postponed till later.
151
   * This lets application insert special markers after the SOI.
152
   */
153
57.7k
  (*cinfo->marker->write_file_header) (cinfo);
154
57.7k
}