Coverage Report

Created: 2023-12-08 06:53

/src/freeimage-svn/FreeImage/trunk/Source/LibJPEG/jcinit.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jcinit.c
3
 *
4
 * Copyright (C) 1991-1997, Thomas G. Lane.
5
 * Modified 2003-2017 by Guido Vollbeding.
6
 * This file is part of the Independent JPEG Group's software.
7
 * For conditions of distribution and use, see the accompanying README file.
8
 *
9
 * This file contains initialization logic for the JPEG compressor.
10
 * This routine is in charge of selecting the modules to be executed and
11
 * making an initialization call to each one.
12
 *
13
 * Logically, this code belongs in jcmaster.c.  It's split out because
14
 * linking this routine implies linking the entire compression library.
15
 * For a transcoding-only application, we want to be able to use jcmaster.c
16
 * without linking in the whole library.
17
 */
18
19
#define JPEG_INTERNALS
20
#include "jinclude.h"
21
#include "jpeglib.h"
22
23
24
/*
25
 * Compute JPEG image dimensions and related values.
26
 * NOTE: this is exported for possible use by application.
27
 * Hence it mustn't do anything that can't be done twice.
28
 */
29
30
GLOBAL(void)
31
jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
32
/* Do computations that are needed before master selection phase */
33
0
{
34
  /* Sanity check on input image dimensions to prevent overflow in
35
   * following calculations.
36
   * We do check jpeg_width and jpeg_height in initial_setup in jcmaster.c,
37
   * but image_width and image_height can come from arbitrary data,
38
   * and we need some space for multiplication by block_size.
39
   */
40
0
  if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24))
41
0
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
42
43
0
#ifdef DCT_SCALING_SUPPORTED
44
45
  /* Compute actual JPEG image dimensions and DCT scaling choices. */
46
0
  if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) {
47
    /* Provide block_size/1 scaling */
48
0
    cinfo->jpeg_width = cinfo->image_width * cinfo->block_size;
49
0
    cinfo->jpeg_height = cinfo->image_height * cinfo->block_size;
50
0
    cinfo->min_DCT_h_scaled_size = 1;
51
0
    cinfo->min_DCT_v_scaled_size = 1;
52
0
  } else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) {
53
    /* Provide block_size/2 scaling */
54
0
    cinfo->jpeg_width = (JDIMENSION)
55
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L);
56
0
    cinfo->jpeg_height = (JDIMENSION)
57
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L);
58
0
    cinfo->min_DCT_h_scaled_size = 2;
59
0
    cinfo->min_DCT_v_scaled_size = 2;
60
0
  } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) {
61
    /* Provide block_size/3 scaling */
62
0
    cinfo->jpeg_width = (JDIMENSION)
63
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L);
64
0
    cinfo->jpeg_height = (JDIMENSION)
65
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L);
66
0
    cinfo->min_DCT_h_scaled_size = 3;
67
0
    cinfo->min_DCT_v_scaled_size = 3;
68
0
  } else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) {
69
    /* Provide block_size/4 scaling */
70
0
    cinfo->jpeg_width = (JDIMENSION)
71
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L);
72
0
    cinfo->jpeg_height = (JDIMENSION)
73
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L);
74
0
    cinfo->min_DCT_h_scaled_size = 4;
75
0
    cinfo->min_DCT_v_scaled_size = 4;
76
0
  } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) {
77
    /* Provide block_size/5 scaling */
78
0
    cinfo->jpeg_width = (JDIMENSION)
79
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L);
80
0
    cinfo->jpeg_height = (JDIMENSION)
81
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L);
82
0
    cinfo->min_DCT_h_scaled_size = 5;
83
0
    cinfo->min_DCT_v_scaled_size = 5;
84
0
  } else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) {
85
    /* Provide block_size/6 scaling */
86
0
    cinfo->jpeg_width = (JDIMENSION)
87
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L);
88
0
    cinfo->jpeg_height = (JDIMENSION)
89
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L);
90
0
    cinfo->min_DCT_h_scaled_size = 6;
91
0
    cinfo->min_DCT_v_scaled_size = 6;
92
0
  } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) {
93
    /* Provide block_size/7 scaling */
94
0
    cinfo->jpeg_width = (JDIMENSION)
95
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L);
96
0
    cinfo->jpeg_height = (JDIMENSION)
97
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L);
98
0
    cinfo->min_DCT_h_scaled_size = 7;
99
0
    cinfo->min_DCT_v_scaled_size = 7;
100
0
  } else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) {
101
    /* Provide block_size/8 scaling */
102
0
    cinfo->jpeg_width = (JDIMENSION)
103
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L);
104
0
    cinfo->jpeg_height = (JDIMENSION)
105
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L);
106
0
    cinfo->min_DCT_h_scaled_size = 8;
107
0
    cinfo->min_DCT_v_scaled_size = 8;
108
0
  } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) {
109
    /* Provide block_size/9 scaling */
110
0
    cinfo->jpeg_width = (JDIMENSION)
111
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L);
112
0
    cinfo->jpeg_height = (JDIMENSION)
113
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L);
114
0
    cinfo->min_DCT_h_scaled_size = 9;
115
0
    cinfo->min_DCT_v_scaled_size = 9;
116
0
  } else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) {
117
    /* Provide block_size/10 scaling */
118
0
    cinfo->jpeg_width = (JDIMENSION)
119
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L);
120
0
    cinfo->jpeg_height = (JDIMENSION)
121
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L);
122
0
    cinfo->min_DCT_h_scaled_size = 10;
123
0
    cinfo->min_DCT_v_scaled_size = 10;
124
0
  } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) {
125
    /* Provide block_size/11 scaling */
126
0
    cinfo->jpeg_width = (JDIMENSION)
127
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L);
128
0
    cinfo->jpeg_height = (JDIMENSION)
129
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L);
130
0
    cinfo->min_DCT_h_scaled_size = 11;
131
0
    cinfo->min_DCT_v_scaled_size = 11;
132
0
  } else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) {
133
    /* Provide block_size/12 scaling */
134
0
    cinfo->jpeg_width = (JDIMENSION)
135
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L);
136
0
    cinfo->jpeg_height = (JDIMENSION)
137
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L);
138
0
    cinfo->min_DCT_h_scaled_size = 12;
139
0
    cinfo->min_DCT_v_scaled_size = 12;
140
0
  } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) {
141
    /* Provide block_size/13 scaling */
142
0
    cinfo->jpeg_width = (JDIMENSION)
143
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L);
144
0
    cinfo->jpeg_height = (JDIMENSION)
145
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L);
146
0
    cinfo->min_DCT_h_scaled_size = 13;
147
0
    cinfo->min_DCT_v_scaled_size = 13;
148
0
  } else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) {
149
    /* Provide block_size/14 scaling */
150
0
    cinfo->jpeg_width = (JDIMENSION)
151
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L);
152
0
    cinfo->jpeg_height = (JDIMENSION)
153
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L);
154
0
    cinfo->min_DCT_h_scaled_size = 14;
155
0
    cinfo->min_DCT_v_scaled_size = 14;
156
0
  } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) {
157
    /* Provide block_size/15 scaling */
158
0
    cinfo->jpeg_width = (JDIMENSION)
159
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L);
160
0
    cinfo->jpeg_height = (JDIMENSION)
161
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L);
162
0
    cinfo->min_DCT_h_scaled_size = 15;
163
0
    cinfo->min_DCT_v_scaled_size = 15;
164
0
  } else {
165
    /* Provide block_size/16 scaling */
166
0
    cinfo->jpeg_width = (JDIMENSION)
167
0
      jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L);
168
0
    cinfo->jpeg_height = (JDIMENSION)
169
0
      jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L);
170
0
    cinfo->min_DCT_h_scaled_size = 16;
171
0
    cinfo->min_DCT_v_scaled_size = 16;
172
0
  }
173
174
#else /* !DCT_SCALING_SUPPORTED */
175
176
  /* Hardwire it to "no scaling" */
177
  cinfo->jpeg_width = cinfo->image_width;
178
  cinfo->jpeg_height = cinfo->image_height;
179
  cinfo->min_DCT_h_scaled_size = DCTSIZE;
180
  cinfo->min_DCT_v_scaled_size = DCTSIZE;
181
182
#endif /* DCT_SCALING_SUPPORTED */
183
0
}
184
185
186
/*
187
 * Master selection of compression modules.
188
 * This is done once at the start of processing an image.  We determine
189
 * which modules will be used and give them appropriate initialization calls.
190
 */
191
192
GLOBAL(void)
193
jinit_compress_master (j_compress_ptr cinfo)
194
0
{
195
0
  long samplesperrow;
196
0
  JDIMENSION jd_samplesperrow;
197
198
  /* For now, precision must match compiled-in value... */
199
0
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
200
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
201
202
  /* Sanity check on input image dimensions */
203
0
  if (cinfo->image_height <= 0 || cinfo->image_width <= 0 ||
204
0
      cinfo->input_components <= 0)
205
0
    ERREXIT(cinfo, JERR_EMPTY_IMAGE);
206
207
  /* Width of an input scanline must be representable as JDIMENSION. */
208
0
  samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
209
0
  jd_samplesperrow = (JDIMENSION) samplesperrow;
210
0
  if ((long) jd_samplesperrow != samplesperrow)
211
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
212
213
  /* Compute JPEG image dimensions and related values. */
214
0
  jpeg_calc_jpeg_dimensions(cinfo);
215
216
  /* Initialize master control (includes parameter checking/processing) */
217
0
  jinit_c_master_control(cinfo, FALSE /* full compression */);
218
219
  /* Preprocessing */
220
0
  if (! cinfo->raw_data_in) {
221
0
    jinit_color_converter(cinfo);
222
0
    jinit_downsampler(cinfo);
223
0
    jinit_c_prep_controller(cinfo, FALSE /* never need full buffer here */);
224
0
  }
225
  /* Forward DCT */
226
0
  jinit_forward_dct(cinfo);
227
  /* Entropy encoding: either Huffman or arithmetic coding. */
228
0
  if (cinfo->arith_code)
229
0
    jinit_arith_encoder(cinfo);
230
0
  else {
231
0
    jinit_huff_encoder(cinfo);
232
0
  }
233
234
  /* Need a full-image coefficient buffer in any multi-pass mode. */
235
0
  jinit_c_coef_controller(cinfo,
236
0
    (boolean) (cinfo->num_scans > 1 || cinfo->optimize_coding));
237
0
  jinit_c_main_controller(cinfo, FALSE /* never need full buffer here */);
238
239
0
  jinit_marker_writer(cinfo);
240
241
  /* We can now tell the memory manager to allocate virtual arrays. */
242
0
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
243
244
  /* Write the datastream header (SOI) immediately.
245
   * Frame and scan headers are postponed till later.
246
   * This lets application insert special markers after the SOI.
247
   */
248
0
  (*cinfo->marker->write_file_header) (cinfo);
249
0
}