Coverage Report

Created: 2025-07-01 06:27

/src/libjpeg-turbo.3.0.x/jcmainct.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * jcmainct.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1994-1996, Thomas G. Lane.
6
 * Lossless JPEG Modifications:
7
 * Copyright (C) 1999, Ken Murchison.
8
 * libjpeg-turbo Modifications:
9
 * Copyright (C) 2022, 2024, D. R. Commander.
10
 * For conditions of distribution and use, see the accompanying README.ijg
11
 * file.
12
 *
13
 * This file contains the main buffer controller for compression.
14
 * The main buffer lies between the pre-processor and the JPEG
15
 * compressor proper; it holds downsampled data in the JPEG colorspace.
16
 */
17
18
#define JPEG_INTERNALS
19
#include "jinclude.h"
20
#include "jpeglib.h"
21
#include "jsamplecomp.h"
22
23
24
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
25
26
/* Private buffer controller object */
27
28
typedef struct {
29
  struct jpeg_c_main_controller pub; /* public fields */
30
31
  JDIMENSION cur_iMCU_row;      /* number of current iMCU row */
32
  JDIMENSION rowgroup_ctr;      /* counts row groups received in iMCU row */
33
  boolean suspended;            /* remember if we suspended output */
34
  J_BUF_MODE pass_mode;         /* current operating mode */
35
36
  /* If using just a strip buffer, this points to the entire set of buffers
37
   * (we allocate one for each component).  In the full-image case, this
38
   * points to the currently accessible strips of the virtual arrays.
39
   */
40
  _JSAMPARRAY buffer[MAX_COMPONENTS];
41
} my_main_controller;
42
43
typedef my_main_controller *my_main_ptr;
44
45
46
/* Forward declarations */
47
METHODDEF(void) process_data_simple_main(j_compress_ptr cinfo,
48
                                         _JSAMPARRAY input_buf,
49
                                         JDIMENSION *in_row_ctr,
50
                                         JDIMENSION in_rows_avail);
51
52
53
/*
54
 * Initialize for a processing pass.
55
 */
56
57
METHODDEF(void)
58
start_pass_main(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
59
60.7k
{
60
60.7k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
61
62
  /* Do nothing in raw-data mode. */
63
60.7k
  if (cinfo->raw_data_in)
64
11.2k
    return;
65
66
49.4k
  if (pass_mode != JBUF_PASS_THRU)
67
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
68
69
49.4k
  main_ptr->cur_iMCU_row = 0;   /* initialize counters */
70
49.4k
  main_ptr->rowgroup_ctr = 0;
71
49.4k
  main_ptr->suspended = FALSE;
72
49.4k
  main_ptr->pass_mode = pass_mode;      /* save mode for use by process_data */
73
49.4k
  main_ptr->pub._process_data = process_data_simple_main;
74
49.4k
}
75
76
77
/*
78
 * Process some data.
79
 * This routine handles the simple pass-through mode,
80
 * where we have only a strip buffer.
81
 */
82
83
METHODDEF(void)
84
process_data_simple_main(j_compress_ptr cinfo, _JSAMPARRAY input_buf,
85
                         JDIMENSION *in_row_ctr, JDIMENSION in_rows_avail)
86
49.4k
{
87
49.4k
  my_main_ptr main_ptr = (my_main_ptr)cinfo->main;
88
49.4k
  JDIMENSION data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
89
90
41.4M
  while (main_ptr->cur_iMCU_row < cinfo->total_iMCU_rows) {
91
    /* Read input data if we haven't filled the main buffer yet */
92
41.4M
    if (main_ptr->rowgroup_ctr < data_unit)
93
41.4M
      (*cinfo->prep->_pre_process_data) (cinfo, input_buf, in_row_ctr,
94
41.4M
                                         in_rows_avail, main_ptr->buffer,
95
41.4M
                                         &main_ptr->rowgroup_ctr, data_unit);
96
97
    /* If we don't have a full iMCU row buffered, return to application for
98
     * more data.  Note that preprocessor will always pad to fill the iMCU row
99
     * at the bottom of the image.
100
     */
101
41.4M
    if (main_ptr->rowgroup_ctr != data_unit)
102
0
      return;
103
104
    /* Send the completed row to the compressor */
105
41.4M
    if (!(*cinfo->coef->_compress_data) (cinfo, main_ptr->buffer)) {
106
      /* If compressor did not consume the whole row, then we must need to
107
       * suspend processing and return to the application.  In this situation
108
       * we pretend we didn't yet consume the last input row; otherwise, if
109
       * it happened to be the last row of the image, the application would
110
       * think we were done.
111
       */
112
0
      if (!main_ptr->suspended) {
113
0
        (*in_row_ctr)--;
114
0
        main_ptr->suspended = TRUE;
115
0
      }
116
0
      return;
117
0
    }
118
    /* We did finish the row.  Undo our little suspension hack if a previous
119
     * call suspended; then mark the main buffer empty.
120
     */
121
41.4M
    if (main_ptr->suspended) {
122
0
      (*in_row_ctr)++;
123
0
      main_ptr->suspended = FALSE;
124
0
    }
125
41.4M
    main_ptr->rowgroup_ctr = 0;
126
41.4M
    main_ptr->cur_iMCU_row++;
127
41.4M
  }
128
49.4k
}
129
130
131
/*
132
 * Initialize main buffer controller.
133
 */
134
135
GLOBAL(void)
136
_jinit_c_main_controller(j_compress_ptr cinfo, boolean need_full_buffer)
137
60.7k
{
138
60.7k
  my_main_ptr main_ptr;
139
60.7k
  int ci;
140
60.7k
  jpeg_component_info *compptr;
141
60.7k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
142
143
60.7k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
144
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
145
146
60.7k
  main_ptr = (my_main_ptr)
147
60.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
148
60.7k
                                sizeof(my_main_controller));
149
60.7k
  memset(main_ptr, 0, sizeof(my_main_controller));
150
60.7k
  cinfo->main = (struct jpeg_c_main_controller *)main_ptr;
151
60.7k
  main_ptr->pub.start_pass = start_pass_main;
152
153
  /* We don't need to create a buffer in raw-data mode. */
154
60.7k
  if (cinfo->raw_data_in)
155
11.2k
    return;
156
157
  /* Create the buffer.  It holds downsampled data, so each component
158
   * may be of a different size.
159
   */
160
49.4k
  if (need_full_buffer) {
161
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
162
49.4k
  } else {
163
    /* Allocate a strip buffer for each component */
164
181k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
165
131k
         ci++, compptr++) {
166
131k
      main_ptr->buffer[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
167
131k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
168
131k
         compptr->width_in_blocks * data_unit,
169
131k
         (JDIMENSION)(compptr->v_samp_factor * data_unit));
170
131k
    }
171
49.4k
  }
172
49.4k
}
j12init_c_main_controller
Line
Count
Source
137
26.7k
{
138
26.7k
  my_main_ptr main_ptr;
139
26.7k
  int ci;
140
26.7k
  jpeg_component_info *compptr;
141
26.7k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
142
143
26.7k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
144
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
145
146
26.7k
  main_ptr = (my_main_ptr)
147
26.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
148
26.7k
                                sizeof(my_main_controller));
149
26.7k
  memset(main_ptr, 0, sizeof(my_main_controller));
150
26.7k
  cinfo->main = (struct jpeg_c_main_controller *)main_ptr;
151
26.7k
  main_ptr->pub.start_pass = start_pass_main;
152
153
  /* We don't need to create a buffer in raw-data mode. */
154
26.7k
  if (cinfo->raw_data_in)
155
0
    return;
156
157
  /* Create the buffer.  It holds downsampled data, so each component
158
   * may be of a different size.
159
   */
160
26.7k
  if (need_full_buffer) {
161
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
162
26.7k
  } else {
163
    /* Allocate a strip buffer for each component */
164
96.5k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
165
69.7k
         ci++, compptr++) {
166
69.7k
      main_ptr->buffer[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
167
69.7k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
168
69.7k
         compptr->width_in_blocks * data_unit,
169
69.7k
         (JDIMENSION)(compptr->v_samp_factor * data_unit));
170
69.7k
    }
171
26.7k
  }
172
26.7k
}
j16init_c_main_controller
Line
Count
Source
137
4.56k
{
138
4.56k
  my_main_ptr main_ptr;
139
4.56k
  int ci;
140
4.56k
  jpeg_component_info *compptr;
141
4.56k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
142
143
4.56k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
144
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
145
146
4.56k
  main_ptr = (my_main_ptr)
147
4.56k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
148
4.56k
                                sizeof(my_main_controller));
149
4.56k
  memset(main_ptr, 0, sizeof(my_main_controller));
150
4.56k
  cinfo->main = (struct jpeg_c_main_controller *)main_ptr;
151
4.56k
  main_ptr->pub.start_pass = start_pass_main;
152
153
  /* We don't need to create a buffer in raw-data mode. */
154
4.56k
  if (cinfo->raw_data_in)
155
0
    return;
156
157
  /* Create the buffer.  It holds downsampled data, so each component
158
   * may be of a different size.
159
   */
160
4.56k
  if (need_full_buffer) {
161
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
162
4.56k
  } else {
163
    /* Allocate a strip buffer for each component */
164
17.7k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
165
13.1k
         ci++, compptr++) {
166
13.1k
      main_ptr->buffer[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
167
13.1k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
168
13.1k
         compptr->width_in_blocks * data_unit,
169
13.1k
         (JDIMENSION)(compptr->v_samp_factor * data_unit));
170
13.1k
    }
171
4.56k
  }
172
4.56k
}
jinit_c_main_controller
Line
Count
Source
137
29.4k
{
138
29.4k
  my_main_ptr main_ptr;
139
29.4k
  int ci;
140
29.4k
  jpeg_component_info *compptr;
141
29.4k
  int data_unit = cinfo->master->lossless ? 1 : DCTSIZE;
142
143
29.4k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
144
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
145
146
29.4k
  main_ptr = (my_main_ptr)
147
29.4k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
148
29.4k
                                sizeof(my_main_controller));
149
29.4k
  memset(main_ptr, 0, sizeof(my_main_controller));
150
29.4k
  cinfo->main = (struct jpeg_c_main_controller *)main_ptr;
151
29.4k
  main_ptr->pub.start_pass = start_pass_main;
152
153
  /* We don't need to create a buffer in raw-data mode. */
154
29.4k
  if (cinfo->raw_data_in)
155
11.2k
    return;
156
157
  /* Create the buffer.  It holds downsampled data, so each component
158
   * may be of a different size.
159
   */
160
18.1k
  if (need_full_buffer) {
161
0
    ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
162
18.1k
  } else {
163
    /* Allocate a strip buffer for each component */
164
67.0k
    for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
165
48.8k
         ci++, compptr++) {
166
48.8k
      main_ptr->buffer[ci] = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
167
48.8k
        ((j_common_ptr)cinfo, JPOOL_IMAGE,
168
48.8k
         compptr->width_in_blocks * data_unit,
169
48.8k
         (JDIMENSION)(compptr->v_samp_factor * data_unit));
170
48.8k
    }
171
18.1k
  }
172
18.1k
}
173
174
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */