Coverage Report

Created: 2026-09-01 07:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/src/turbojpeg-mp.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2009-2026 D. R. Commander
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions are met:
6
 *
7
 * - Redistributions of source code must retain the above copyright notice,
8
 *   this list of conditions and the following disclaimer.
9
 * - Redistributions in binary form must reproduce the above copyright notice,
10
 *   this list of conditions and the following disclaimer in the documentation
11
 *   and/or other materials provided with the distribution.
12
 * - Neither the name of the libjpeg-turbo Project nor the names of its
13
 *   contributors may be used to endorse or promote products derived from this
14
 *   software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 * POSSIBILITY OF SUCH DAMAGE.
27
 */
28
29
/* TurboJPEG API functions that must be compiled for multiple data
30
   precisions */
31
32
#if BITS_IN_JSAMPLE == 8
33
75.2M
#define _JSAMPLE  JSAMPLE
34
113k
#define _JSAMPROW  JSAMPROW
35
75.0M
#define _buffer  buffer
36
87.0k
#define _jinit_read_png  jinit_read_png
37
0
#define _jinit_write_png  jinit_write_png
38
43.3k
#define _jinit_read_ppm  jinit_read_ppm
39
0
#define _jinit_write_ppm  jinit_write_ppm
40
2.27k
#define _jpeg_crop_scanline  jpeg_crop_scanline
41
32.6M
#define _jpeg_read_scanlines  jpeg_read_scanlines
42
4.53k
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
43
72.1k
#define _jpeg_write_scanlines  jpeg_write_scanlines
44
#elif BITS_IN_JSAMPLE == 12
45
39.6M
#define _JSAMPLE  J12SAMPLE
46
69.5k
#define _JSAMPROW  J12SAMPROW
47
39.5M
#define _buffer  buffer12
48
65.2k
#define _jinit_read_png  j12init_read_png
49
0
#define _jinit_write_png  j12init_write_png
50
27.2k
#define _jinit_read_ppm  j12init_read_ppm
51
0
#define _jinit_write_ppm  j12init_write_ppm
52
872
#define _jpeg_crop_scanline  jpeg12_crop_scanline
53
9.46M
#define _jpeg_read_scanlines  jpeg12_read_scanlines
54
1.73k
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
55
57.8k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
56
#elif BITS_IN_JSAMPLE == 16
57
13.3M
#define _JSAMPLE  J16SAMPLE
58
32.3k
#define _JSAMPROW  J16SAMPROW
59
13.2M
#define _buffer  buffer16
60
31.4k
#define _jinit_read_png  j16init_read_png
61
0
#define _jinit_write_png  j16init_write_png
62
11.5k
#define _jinit_read_ppm  j16init_read_ppm
63
0
#define _jinit_write_ppm  j16init_write_ppm
64
6.63M
#define _jpeg_read_scanlines  jpeg16_read_scanlines
65
21.1k
#define _jpeg_write_scanlines  jpeg16_write_scanlines
66
#endif
67
68
0
#define _GET_NAME(name, suffix)  name##suffix
69
0
#define GET_NAME(name, suffix)  _GET_NAME(name, suffix)
70
501k
#define _GET_STRING(name, suffix)  #name #suffix
71
501k
#define GET_STRING(name, suffix)  _GET_STRING(name, suffix)
72
73
74
/******************************** Compressor *********************************/
75
76
/* TurboJPEG 3.0+ */
77
DLLEXPORT int GET_NAME(tj3Compress, BITS_IN_JSAMPLE)
78
  (tjhandle handle, const _JSAMPLE *srcBuf, int width, int pitch, int height,
79
   int pixelFormat, unsigned char **jpegBuf, size_t *jpegSize)
80
151k
{
81
151k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
82
151k
  int i, retval = 0;
83
151k
  boolean alloc = TRUE;
84
151k
  _JSAMPROW *row_pointer = NULL;
85
86
151k
  GET_CINSTANCE(handle)
87
151k
  if ((this->init & COMPRESS) == 0)
88
151k
    THROW("Instance has not been initialized for compression");
89
90
151k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
91
151k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
92
151k
      jpegSize == NULL)
93
151k
    THROW("Invalid argument");
94
95
151k
  if (!this->lossless && this->quality == -1)
96
151k
    THROW("TJPARAM_QUALITY must be specified");
97
151k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
98
151k
    THROW("TJPARAM_SUBSAMP must be specified");
99
100
151k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
101
0
  else if (pitch < width * tjPixelSize[pixelFormat])
102
151k
    THROW("Invalid argument");
103
104
151k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
105
151k
    THROW("Memory allocation failure");
106
107
151k
  CATCH_LIBJPEG(this);
108
109
151k
  cinfo->image_width = width;
110
151k
  cinfo->image_height = height;
111
151k
  cinfo->data_precision = BITS_IN_JSAMPLE;
112
#if BITS_IN_JSAMPLE == 8
113
72.1k
  if (this->lossless && this->precision >= 2 &&
114
29.1k
      this->precision <= BITS_IN_JSAMPLE)
115
#else
116
78.9k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
117
42.9k
      this->precision <= BITS_IN_JSAMPLE)
118
42.9k
#endif
119
72.0k
    cinfo->data_precision = this->precision;
120
121
151k
  setCompDefaults(this, pixelFormat, FALSE);
122
151k
  if (this->noRealloc) alloc = FALSE;
123
151k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
124
125
151k
  jpeg_start_compress(cinfo, TRUE);
126
151k
  if (this->iccBuf != NULL && this->iccSize != 0)
127
128k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
128
225M
  for (i = 0; i < height; i++) {
129
225M
    if (this->bottomUp)
130
33.0M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
131
192M
    else
132
192M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
133
225M
  }
134
302k
  while (cinfo->next_scanline < cinfo->image_height)
135
151k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
136
151k
                          cinfo->image_height - cinfo->next_scanline);
137
151k
  jpeg_finish_compress(cinfo);
138
139
151k
bailout:
140
151k
  if (cinfo->global_state > CSTATE_START && alloc)
141
132
    (*cinfo->dest->term_destination) (cinfo);
142
151k
  if (cinfo->global_state > CSTATE_START || retval == -1)
143
132
    jpeg_abort_compress(cinfo);
144
151k
  free(row_pointer);
145
151k
  if (this->jerr.warning) retval = -1;
146
151k
  return retval;
147
151k
}
tj3Compress8
Line
Count
Source
80
72.1k
{
81
72.1k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
82
72.1k
  int i, retval = 0;
83
72.1k
  boolean alloc = TRUE;
84
72.1k
  _JSAMPROW *row_pointer = NULL;
85
86
72.1k
  GET_CINSTANCE(handle)
87
72.1k
  if ((this->init & COMPRESS) == 0)
88
72.1k
    THROW("Instance has not been initialized for compression");
89
90
72.1k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
91
72.1k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
92
72.1k
      jpegSize == NULL)
93
72.1k
    THROW("Invalid argument");
94
95
72.1k
  if (!this->lossless && this->quality == -1)
96
72.1k
    THROW("TJPARAM_QUALITY must be specified");
97
72.1k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
98
72.1k
    THROW("TJPARAM_SUBSAMP must be specified");
99
100
72.1k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
101
0
  else if (pitch < width * tjPixelSize[pixelFormat])
102
72.1k
    THROW("Invalid argument");
103
104
72.1k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
105
72.1k
    THROW("Memory allocation failure");
106
107
72.1k
  CATCH_LIBJPEG(this);
108
109
72.1k
  cinfo->image_width = width;
110
72.1k
  cinfo->image_height = height;
111
72.1k
  cinfo->data_precision = BITS_IN_JSAMPLE;
112
72.1k
#if BITS_IN_JSAMPLE == 8
113
72.1k
  if (this->lossless && this->precision >= 2 &&
114
29.1k
      this->precision <= BITS_IN_JSAMPLE)
115
#else
116
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
117
      this->precision <= BITS_IN_JSAMPLE)
118
#endif
119
29.1k
    cinfo->data_precision = this->precision;
120
121
72.1k
  setCompDefaults(this, pixelFormat, FALSE);
122
72.1k
  if (this->noRealloc) alloc = FALSE;
123
72.1k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
124
125
72.1k
  jpeg_start_compress(cinfo, TRUE);
126
72.1k
  if (this->iccBuf != NULL && this->iccSize != 0)
127
62.1k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
128
89.8M
  for (i = 0; i < height; i++) {
129
89.7M
    if (this->bottomUp)
130
13.4M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
131
76.2M
    else
132
76.2M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
133
89.7M
  }
134
144k
  while (cinfo->next_scanline < cinfo->image_height)
135
72.1k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
136
72.1k
                          cinfo->image_height - cinfo->next_scanline);
137
72.1k
  jpeg_finish_compress(cinfo);
138
139
72.1k
bailout:
140
72.1k
  if (cinfo->global_state > CSTATE_START && alloc)
141
0
    (*cinfo->dest->term_destination) (cinfo);
142
72.1k
  if (cinfo->global_state > CSTATE_START || retval == -1)
143
0
    jpeg_abort_compress(cinfo);
144
72.1k
  free(row_pointer);
145
72.1k
  if (this->jerr.warning) retval = -1;
146
72.1k
  return retval;
147
72.1k
}
tj3Compress12
Line
Count
Source
80
57.8k
{
81
57.8k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
82
57.8k
  int i, retval = 0;
83
57.8k
  boolean alloc = TRUE;
84
57.8k
  _JSAMPROW *row_pointer = NULL;
85
86
57.8k
  GET_CINSTANCE(handle)
87
57.8k
  if ((this->init & COMPRESS) == 0)
88
57.8k
    THROW("Instance has not been initialized for compression");
89
90
57.8k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
91
57.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
92
57.8k
      jpegSize == NULL)
93
57.8k
    THROW("Invalid argument");
94
95
57.8k
  if (!this->lossless && this->quality == -1)
96
57.8k
    THROW("TJPARAM_QUALITY must be specified");
97
57.8k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
98
57.8k
    THROW("TJPARAM_SUBSAMP must be specified");
99
100
57.8k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
101
0
  else if (pitch < width * tjPixelSize[pixelFormat])
102
57.8k
    THROW("Invalid argument");
103
104
57.8k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
105
57.8k
    THROW("Memory allocation failure");
106
107
57.8k
  CATCH_LIBJPEG(this);
108
109
57.8k
  cinfo->image_width = width;
110
57.8k
  cinfo->image_height = height;
111
57.8k
  cinfo->data_precision = BITS_IN_JSAMPLE;
112
#if BITS_IN_JSAMPLE == 8
113
  if (this->lossless && this->precision >= 2 &&
114
      this->precision <= BITS_IN_JSAMPLE)
115
#else
116
57.8k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
117
21.7k
      this->precision <= BITS_IN_JSAMPLE)
118
21.7k
#endif
119
21.7k
    cinfo->data_precision = this->precision;
120
121
57.8k
  setCompDefaults(this, pixelFormat, FALSE);
122
57.8k
  if (this->noRealloc) alloc = FALSE;
123
57.8k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
124
125
57.8k
  jpeg_start_compress(cinfo, TRUE);
126
57.8k
  if (this->iccBuf != NULL && this->iccSize != 0)
127
47.8k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
128
100M
  for (i = 0; i < height; i++) {
129
100M
    if (this->bottomUp)
130
14.5M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
131
85.6M
    else
132
85.6M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
133
100M
  }
134
115k
  while (cinfo->next_scanline < cinfo->image_height)
135
57.8k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
136
57.8k
                          cinfo->image_height - cinfo->next_scanline);
137
57.8k
  jpeg_finish_compress(cinfo);
138
139
57.8k
bailout:
140
57.8k
  if (cinfo->global_state > CSTATE_START && alloc)
141
52
    (*cinfo->dest->term_destination) (cinfo);
142
57.8k
  if (cinfo->global_state > CSTATE_START || retval == -1)
143
52
    jpeg_abort_compress(cinfo);
144
57.8k
  free(row_pointer);
145
57.8k
  if (this->jerr.warning) retval = -1;
146
57.8k
  return retval;
147
57.8k
}
tj3Compress16
Line
Count
Source
80
21.1k
{
81
21.1k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
82
21.1k
  int i, retval = 0;
83
21.1k
  boolean alloc = TRUE;
84
21.1k
  _JSAMPROW *row_pointer = NULL;
85
86
21.1k
  GET_CINSTANCE(handle)
87
21.1k
  if ((this->init & COMPRESS) == 0)
88
21.1k
    THROW("Instance has not been initialized for compression");
89
90
21.1k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
91
21.1k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
92
21.1k
      jpegSize == NULL)
93
21.1k
    THROW("Invalid argument");
94
95
21.1k
  if (!this->lossless && this->quality == -1)
96
21.1k
    THROW("TJPARAM_QUALITY must be specified");
97
21.1k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
98
21.1k
    THROW("TJPARAM_SUBSAMP must be specified");
99
100
21.1k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
101
0
  else if (pitch < width * tjPixelSize[pixelFormat])
102
21.1k
    THROW("Invalid argument");
103
104
21.1k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
105
21.1k
    THROW("Memory allocation failure");
106
107
21.1k
  CATCH_LIBJPEG(this);
108
109
21.1k
  cinfo->image_width = width;
110
21.1k
  cinfo->image_height = height;
111
21.1k
  cinfo->data_precision = BITS_IN_JSAMPLE;
112
#if BITS_IN_JSAMPLE == 8
113
  if (this->lossless && this->precision >= 2 &&
114
      this->precision <= BITS_IN_JSAMPLE)
115
#else
116
21.1k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
117
21.1k
      this->precision <= BITS_IN_JSAMPLE)
118
21.1k
#endif
119
21.1k
    cinfo->data_precision = this->precision;
120
121
21.1k
  setCompDefaults(this, pixelFormat, FALSE);
122
21.1k
  if (this->noRealloc) alloc = FALSE;
123
21.1k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
124
125
21.1k
  jpeg_start_compress(cinfo, TRUE);
126
21.1k
  if (this->iccBuf != NULL && this->iccSize != 0)
127
18.1k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
128
35.1M
  for (i = 0; i < height; i++) {
129
35.1M
    if (this->bottomUp)
130
5.03M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
131
30.1M
    else
132
30.1M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
133
35.1M
  }
134
42.2k
  while (cinfo->next_scanline < cinfo->image_height)
135
21.1k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
136
21.1k
                          cinfo->image_height - cinfo->next_scanline);
137
21.1k
  jpeg_finish_compress(cinfo);
138
139
21.1k
bailout:
140
21.1k
  if (cinfo->global_state > CSTATE_START && alloc)
141
80
    (*cinfo->dest->term_destination) (cinfo);
142
21.1k
  if (cinfo->global_state > CSTATE_START || retval == -1)
143
80
    jpeg_abort_compress(cinfo);
144
21.1k
  free(row_pointer);
145
21.1k
  if (this->jerr.warning) retval = -1;
146
21.1k
  return retval;
147
21.1k
}
148
149
150
/******************************* Decompressor ********************************/
151
152
/* TurboJPEG 3.0+ */
153
DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE)
154
  (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize,
155
   _JSAMPLE *dstBuf, int pitch, int pixelFormat)
156
64.1k
{
157
64.1k
  static const char FUNCTION_NAME[] =
158
64.1k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
159
64.1k
  _JSAMPROW *row_pointer = NULL;
160
64.1k
  int croppedHeight, i, retval = 0;
161
#if BITS_IN_JSAMPLE != 16
162
  int scaledWidth;
163
#endif
164
64.1k
  struct my_progress_mgr progress;
165
166
64.1k
  GET_DINSTANCE(handle);
167
64.1k
  if ((this->init & DECOMPRESS) == 0)
168
64.1k
    THROW("Instance has not been initialized for decompression");
169
170
64.1k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
171
64.1k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
172
64.1k
    THROW("Invalid argument");
173
174
64.1k
  if (this->scanLimit) {
175
64.1k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
176
64.1k
    progress.pub.progress_monitor = my_progress_monitor;
177
64.1k
    progress.this = this;
178
64.1k
    dinfo->progress = &progress.pub;
179
64.1k
  } else
180
0
    dinfo->progress = NULL;
181
182
64.1k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
183
184
64.1k
  CATCH_LIBJPEG(this);
185
186
64.1k
  if (dinfo->global_state <= DSTATE_INHEADER) {
187
64.1k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
188
64.1k
    jpeg_read_header(dinfo, TRUE);
189
64.1k
  }
190
26.8k
  setDecompParameters(this);
191
#if BITS_IN_JSAMPLE == 12
192
5.86k
  if (this->precision == 8 && !this->lossless)
193
0
    dinfo->data_precision = 12;
194
#endif
195
26.8k
  if (this->maxPixels &&
196
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
197
0
      (unsigned long long)this->maxPixels)
198
26.8k
    THROW("Image is too large");
199
26.8k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
200
#if BITS_IN_JSAMPLE != 16
201
24.4k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
202
#endif
203
26.8k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
204
26.8k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
205
206
26.8k
  dinfo->scale_num = this->scalingFactor.num;
207
26.8k
  dinfo->scale_denom = this->scalingFactor.denom;
208
209
26.8k
  jpeg_start_decompress(dinfo);
210
211
#if BITS_IN_JSAMPLE != 16
212
24.4k
  if (this->croppingRegion.x != 0 ||
213
21.3k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
214
3.15k
    JDIMENSION crop_x = this->croppingRegion.x;
215
3.15k
    JDIMENSION crop_w = this->croppingRegion.w;
216
217
3.15k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
218
3.15k
    if ((int)crop_x != this->croppingRegion.x)
219
9
      THROWI("Unexplained mismatch between specified (%d) and\n"
220
3.15k
             "actual (%d) cropping region left boundary",
221
3.15k
             this->croppingRegion.x, (int)crop_x);
222
3.14k
    if ((int)crop_w != this->croppingRegion.w)
223
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
224
3.14k
             "actual (%d) cropping region width",
225
3.14k
             this->croppingRegion.w, (int)crop_w);
226
3.14k
  }
227
24.4k
#endif
228
229
26.8k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
230
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
231
26.8k
    THROW("Invalid argument");
232
233
26.8k
  croppedHeight = dinfo->output_height;
234
#if BITS_IN_JSAMPLE != 16
235
24.4k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
236
3.14k
    croppedHeight = this->croppingRegion.h;
237
#endif
238
26.8k
  if ((row_pointer =
239
26.8k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
240
26.8k
    THROW("Memory allocation failure");
241
26.8k
  CATCH_LIBJPEG(this);
242
113M
  for (i = 0; i < (int)croppedHeight; i++) {
243
113M
    if (this->bottomUp)
244
35.2M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
245
78.4M
    else
246
78.4M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
247
113M
  }
248
249
#if BITS_IN_JSAMPLE != 16
250
21.5k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
251
3.14k
    if (this->croppingRegion.y != 0) {
252
3.14k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
253
254
3.14k
      if ((int)lines != this->croppingRegion.y)
255
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
256
               "actual (%d) cropping region upper boundary",
257
3.14k
               this->croppingRegion.y, (int)lines);
258
3.14k
    }
259
125k
    while ((int)dinfo->output_scanline <
260
125k
           this->croppingRegion.y + this->croppingRegion.h)
261
122k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
262
122k
                                               this->croppingRegion.y],
263
122k
                           this->croppingRegion.y + this->croppingRegion.h -
264
122k
                           dinfo->output_scanline);
265
3.14k
    if (this->croppingRegion.y + this->croppingRegion.h !=
266
3.14k
        (int)dinfo->output_height) {
267
3.12k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
268
                                                     this->croppingRegion.y -
269
                                                     this->croppingRegion.h);
270
271
3.12k
      if (lines != dinfo->output_height - this->croppingRegion.y -
272
3.12k
                   this->croppingRegion.h)
273
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
274
3.12k
               "actual (%d) cropping region lower boundary",
275
3.12k
               this->croppingRegion.y + this->croppingRegion.h,
276
3.12k
               (int)(dinfo->output_height - lines));
277
3.12k
    }
278
3.14k
  } else
279
18.3k
#endif
280
18.3k
  {
281
48.6M
    while (dinfo->output_scanline < dinfo->output_height)
282
48.6M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
283
48.6M
                           dinfo->output_height - dinfo->output_scanline);
284
18.3k
  }
285
21.5k
  jpeg_finish_decompress(dinfo);
286
287
64.1k
bailout:
288
64.1k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
289
64.1k
  free(row_pointer);
290
64.1k
  if (this->jerr.warning) retval = -1;
291
64.1k
  return retval;
292
21.5k
}
tj3Decompress8
Line
Count
Source
156
41.2k
{
157
41.2k
  static const char FUNCTION_NAME[] =
158
41.2k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
159
41.2k
  _JSAMPROW *row_pointer = NULL;
160
41.2k
  int croppedHeight, i, retval = 0;
161
41.2k
#if BITS_IN_JSAMPLE != 16
162
41.2k
  int scaledWidth;
163
41.2k
#endif
164
41.2k
  struct my_progress_mgr progress;
165
166
41.2k
  GET_DINSTANCE(handle);
167
41.2k
  if ((this->init & DECOMPRESS) == 0)
168
41.2k
    THROW("Instance has not been initialized for decompression");
169
170
41.2k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
171
41.2k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
172
41.2k
    THROW("Invalid argument");
173
174
41.2k
  if (this->scanLimit) {
175
41.2k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
176
41.2k
    progress.pub.progress_monitor = my_progress_monitor;
177
41.2k
    progress.this = this;
178
41.2k
    dinfo->progress = &progress.pub;
179
41.2k
  } else
180
0
    dinfo->progress = NULL;
181
182
41.2k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
183
184
41.2k
  CATCH_LIBJPEG(this);
185
186
41.2k
  if (dinfo->global_state <= DSTATE_INHEADER) {
187
41.2k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
188
41.2k
    jpeg_read_header(dinfo, TRUE);
189
41.2k
  }
190
18.5k
  setDecompParameters(this);
191
#if BITS_IN_JSAMPLE == 12
192
  if (this->precision == 8 && !this->lossless)
193
    dinfo->data_precision = 12;
194
#endif
195
18.5k
  if (this->maxPixels &&
196
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
197
0
      (unsigned long long)this->maxPixels)
198
18.5k
    THROW("Image is too large");
199
18.5k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
200
18.5k
#if BITS_IN_JSAMPLE != 16
201
18.5k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
202
18.5k
#endif
203
18.5k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
204
18.5k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
205
206
18.5k
  dinfo->scale_num = this->scalingFactor.num;
207
18.5k
  dinfo->scale_denom = this->scalingFactor.denom;
208
209
18.5k
  jpeg_start_decompress(dinfo);
210
211
18.5k
#if BITS_IN_JSAMPLE != 16
212
18.5k
  if (this->croppingRegion.x != 0 ||
213
16.3k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
214
2.27k
    JDIMENSION crop_x = this->croppingRegion.x;
215
2.27k
    JDIMENSION crop_w = this->croppingRegion.w;
216
217
2.27k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
218
2.27k
    if ((int)crop_x != this->croppingRegion.x)
219
6
      THROWI("Unexplained mismatch between specified (%d) and\n"
220
2.27k
             "actual (%d) cropping region left boundary",
221
2.27k
             this->croppingRegion.x, (int)crop_x);
222
2.27k
    if ((int)crop_w != this->croppingRegion.w)
223
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
224
2.27k
             "actual (%d) cropping region width",
225
2.27k
             this->croppingRegion.w, (int)crop_w);
226
2.27k
  }
227
18.5k
#endif
228
229
18.5k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
230
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
231
18.5k
    THROW("Invalid argument");
232
233
18.5k
  croppedHeight = dinfo->output_height;
234
18.5k
#if BITS_IN_JSAMPLE != 16
235
18.5k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
236
2.27k
    croppedHeight = this->croppingRegion.h;
237
18.5k
#endif
238
18.5k
  if ((row_pointer =
239
18.5k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
240
18.5k
    THROW("Memory allocation failure");
241
18.5k
  CATCH_LIBJPEG(this);
242
69.6M
  for (i = 0; i < (int)croppedHeight; i++) {
243
69.5M
    if (this->bottomUp)
244
23.5M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
245
46.0M
    else
246
46.0M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
247
69.5M
  }
248
249
16.0k
#if BITS_IN_JSAMPLE != 16
250
16.3k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
251
2.27k
    if (this->croppingRegion.y != 0) {
252
2.27k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
253
254
2.27k
      if ((int)lines != this->croppingRegion.y)
255
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
256
2.27k
               "actual (%d) cropping region upper boundary",
257
2.27k
               this->croppingRegion.y, (int)lines);
258
2.27k
    }
259
90.9k
    while ((int)dinfo->output_scanline <
260
90.9k
           this->croppingRegion.y + this->croppingRegion.h)
261
88.6k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
262
88.6k
                                               this->croppingRegion.y],
263
88.6k
                           this->croppingRegion.y + this->croppingRegion.h -
264
88.6k
                           dinfo->output_scanline);
265
2.27k
    if (this->croppingRegion.y + this->croppingRegion.h !=
266
2.27k
        (int)dinfo->output_height) {
267
2.25k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
268
2.25k
                                                     this->croppingRegion.y -
269
2.25k
                                                     this->croppingRegion.h);
270
271
2.25k
      if (lines != dinfo->output_height - this->croppingRegion.y -
272
2.25k
                   this->croppingRegion.h)
273
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
274
2.25k
               "actual (%d) cropping region lower boundary",
275
2.25k
               this->croppingRegion.y + this->croppingRegion.h,
276
2.25k
               (int)(dinfo->output_height - lines));
277
2.25k
    }
278
2.27k
  } else
279
13.7k
#endif
280
13.7k
  {
281
32.5M
    while (dinfo->output_scanline < dinfo->output_height)
282
32.5M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
283
32.5M
                           dinfo->output_height - dinfo->output_scanline);
284
13.7k
  }
285
16.0k
  jpeg_finish_decompress(dinfo);
286
287
41.2k
bailout:
288
41.2k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
289
41.2k
  free(row_pointer);
290
41.2k
  if (this->jerr.warning) retval = -1;
291
41.2k
  return retval;
292
16.0k
}
tj3Decompress12
Line
Count
Source
156
11.7k
{
157
11.7k
  static const char FUNCTION_NAME[] =
158
11.7k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
159
11.7k
  _JSAMPROW *row_pointer = NULL;
160
11.7k
  int croppedHeight, i, retval = 0;
161
11.7k
#if BITS_IN_JSAMPLE != 16
162
11.7k
  int scaledWidth;
163
11.7k
#endif
164
11.7k
  struct my_progress_mgr progress;
165
166
11.7k
  GET_DINSTANCE(handle);
167
11.7k
  if ((this->init & DECOMPRESS) == 0)
168
11.7k
    THROW("Instance has not been initialized for decompression");
169
170
11.7k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
171
11.7k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
172
11.7k
    THROW("Invalid argument");
173
174
11.7k
  if (this->scanLimit) {
175
11.7k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
176
11.7k
    progress.pub.progress_monitor = my_progress_monitor;
177
11.7k
    progress.this = this;
178
11.7k
    dinfo->progress = &progress.pub;
179
11.7k
  } else
180
0
    dinfo->progress = NULL;
181
182
11.7k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
183
184
11.7k
  CATCH_LIBJPEG(this);
185
186
11.7k
  if (dinfo->global_state <= DSTATE_INHEADER) {
187
11.7k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
188
11.7k
    jpeg_read_header(dinfo, TRUE);
189
11.7k
  }
190
5.86k
  setDecompParameters(this);
191
5.86k
#if BITS_IN_JSAMPLE == 12
192
5.86k
  if (this->precision == 8 && !this->lossless)
193
0
    dinfo->data_precision = 12;
194
5.86k
#endif
195
5.86k
  if (this->maxPixels &&
196
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
197
0
      (unsigned long long)this->maxPixels)
198
5.86k
    THROW("Image is too large");
199
5.86k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
200
5.86k
#if BITS_IN_JSAMPLE != 16
201
5.86k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
202
5.86k
#endif
203
5.86k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
204
5.86k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
205
206
5.86k
  dinfo->scale_num = this->scalingFactor.num;
207
5.86k
  dinfo->scale_denom = this->scalingFactor.denom;
208
209
5.86k
  jpeg_start_decompress(dinfo);
210
211
5.86k
#if BITS_IN_JSAMPLE != 16
212
5.86k
  if (this->croppingRegion.x != 0 ||
213
4.99k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
214
872
    JDIMENSION crop_x = this->croppingRegion.x;
215
872
    JDIMENSION crop_w = this->croppingRegion.w;
216
217
872
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
218
872
    if ((int)crop_x != this->croppingRegion.x)
219
3
      THROWI("Unexplained mismatch between specified (%d) and\n"
220
872
             "actual (%d) cropping region left boundary",
221
872
             this->croppingRegion.x, (int)crop_x);
222
869
    if ((int)crop_w != this->croppingRegion.w)
223
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
224
869
             "actual (%d) cropping region width",
225
869
             this->croppingRegion.w, (int)crop_w);
226
869
  }
227
5.86k
#endif
228
229
5.86k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
230
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
231
5.86k
    THROW("Invalid argument");
232
233
5.86k
  croppedHeight = dinfo->output_height;
234
5.86k
#if BITS_IN_JSAMPLE != 16
235
5.86k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
236
869
    croppedHeight = this->croppingRegion.h;
237
5.86k
#endif
238
5.86k
  if ((row_pointer =
239
5.86k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
240
5.86k
    THROW("Memory allocation failure");
241
5.86k
  CATCH_LIBJPEG(this);
242
20.7M
  for (i = 0; i < (int)croppedHeight; i++) {
243
20.7M
    if (this->bottomUp)
244
6.52M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
245
14.2M
    else
246
14.2M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
247
20.7M
  }
248
249
5.49k
#if BITS_IN_JSAMPLE != 16
250
5.49k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
251
869
    if (this->croppingRegion.y != 0) {
252
869
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
253
254
869
      if ((int)lines != this->croppingRegion.y)
255
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
256
869
               "actual (%d) cropping region upper boundary",
257
869
               this->croppingRegion.y, (int)lines);
258
869
    }
259
34.3k
    while ((int)dinfo->output_scanline <
260
34.3k
           this->croppingRegion.y + this->croppingRegion.h)
261
33.4k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
262
33.4k
                                               this->croppingRegion.y],
263
33.4k
                           this->croppingRegion.y + this->croppingRegion.h -
264
33.4k
                           dinfo->output_scanline);
265
869
    if (this->croppingRegion.y + this->croppingRegion.h !=
266
869
        (int)dinfo->output_height) {
267
862
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
268
862
                                                     this->croppingRegion.y -
269
862
                                                     this->croppingRegion.h);
270
271
862
      if (lines != dinfo->output_height - this->croppingRegion.y -
272
862
                   this->croppingRegion.h)
273
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
274
862
               "actual (%d) cropping region lower boundary",
275
862
               this->croppingRegion.y + this->croppingRegion.h,
276
862
               (int)(dinfo->output_height - lines));
277
862
    }
278
869
  } else
279
4.62k
#endif
280
4.62k
  {
281
9.43M
    while (dinfo->output_scanline < dinfo->output_height)
282
9.43M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
283
9.43M
                           dinfo->output_height - dinfo->output_scanline);
284
4.62k
  }
285
5.49k
  jpeg_finish_decompress(dinfo);
286
287
11.7k
bailout:
288
11.7k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
289
11.7k
  free(row_pointer);
290
11.7k
  if (this->jerr.warning) retval = -1;
291
11.7k
  return retval;
292
5.49k
}
tj3Decompress16
Line
Count
Source
156
11.1k
{
157
11.1k
  static const char FUNCTION_NAME[] =
158
11.1k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
159
11.1k
  _JSAMPROW *row_pointer = NULL;
160
11.1k
  int croppedHeight, i, retval = 0;
161
#if BITS_IN_JSAMPLE != 16
162
  int scaledWidth;
163
#endif
164
11.1k
  struct my_progress_mgr progress;
165
166
11.1k
  GET_DINSTANCE(handle);
167
11.1k
  if ((this->init & DECOMPRESS) == 0)
168
11.1k
    THROW("Instance has not been initialized for decompression");
169
170
11.1k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
171
11.1k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
172
11.1k
    THROW("Invalid argument");
173
174
11.1k
  if (this->scanLimit) {
175
11.1k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
176
11.1k
    progress.pub.progress_monitor = my_progress_monitor;
177
11.1k
    progress.this = this;
178
11.1k
    dinfo->progress = &progress.pub;
179
11.1k
  } else
180
0
    dinfo->progress = NULL;
181
182
11.1k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
183
184
11.1k
  CATCH_LIBJPEG(this);
185
186
11.1k
  if (dinfo->global_state <= DSTATE_INHEADER) {
187
11.1k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
188
11.1k
    jpeg_read_header(dinfo, TRUE);
189
11.1k
  }
190
2.40k
  setDecompParameters(this);
191
#if BITS_IN_JSAMPLE == 12
192
  if (this->precision == 8 && !this->lossless)
193
    dinfo->data_precision = 12;
194
#endif
195
2.40k
  if (this->maxPixels &&
196
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
197
0
      (unsigned long long)this->maxPixels)
198
2.40k
    THROW("Image is too large");
199
2.40k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
200
#if BITS_IN_JSAMPLE != 16
201
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
202
#endif
203
2.40k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
204
2.40k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
205
206
2.40k
  dinfo->scale_num = this->scalingFactor.num;
207
2.40k
  dinfo->scale_denom = this->scalingFactor.denom;
208
209
2.40k
  jpeg_start_decompress(dinfo);
210
211
#if BITS_IN_JSAMPLE != 16
212
  if (this->croppingRegion.x != 0 ||
213
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
214
    JDIMENSION crop_x = this->croppingRegion.x;
215
    JDIMENSION crop_w = this->croppingRegion.w;
216
217
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
218
    if ((int)crop_x != this->croppingRegion.x)
219
      THROWI("Unexplained mismatch between specified (%d) and\n"
220
             "actual (%d) cropping region left boundary",
221
             this->croppingRegion.x, (int)crop_x);
222
    if ((int)crop_w != this->croppingRegion.w)
223
      THROWI("Unexplained mismatch between specified (%d) and\n"
224
             "actual (%d) cropping region width",
225
             this->croppingRegion.w, (int)crop_w);
226
  }
227
#endif
228
229
2.40k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
230
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
231
2.40k
    THROW("Invalid argument");
232
233
2.40k
  croppedHeight = dinfo->output_height;
234
#if BITS_IN_JSAMPLE != 16
235
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
236
    croppedHeight = this->croppingRegion.h;
237
#endif
238
2.40k
  if ((row_pointer =
239
2.40k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
240
2.40k
    THROW("Memory allocation failure");
241
2.40k
  CATCH_LIBJPEG(this);
242
23.3M
  for (i = 0; i < (int)croppedHeight; i++) {
243
23.3M
    if (this->bottomUp)
244
5.16M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
245
18.2M
    else
246
18.2M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
247
23.3M
  }
248
249
#if BITS_IN_JSAMPLE != 16
250
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
251
    if (this->croppingRegion.y != 0) {
252
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
253
254
      if ((int)lines != this->croppingRegion.y)
255
        THROWI("Unexplained mismatch between specified (%d) and\n"
256
               "actual (%d) cropping region upper boundary",
257
               this->croppingRegion.y, (int)lines);
258
    }
259
    while ((int)dinfo->output_scanline <
260
           this->croppingRegion.y + this->croppingRegion.h)
261
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
262
                                               this->croppingRegion.y],
263
                           this->croppingRegion.y + this->croppingRegion.h -
264
                           dinfo->output_scanline);
265
    if (this->croppingRegion.y + this->croppingRegion.h !=
266
        (int)dinfo->output_height) {
267
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
268
                                                     this->croppingRegion.y -
269
                                                     this->croppingRegion.h);
270
271
      if (lines != dinfo->output_height - this->croppingRegion.y -
272
                   this->croppingRegion.h)
273
        THROWI("Unexplained mismatch between specified (%d) and\n"
274
               "actual (%d) cropping region lower boundary",
275
               this->croppingRegion.y + this->croppingRegion.h,
276
               (int)(dinfo->output_height - lines));
277
    }
278
  } else
279
#endif
280
1.35k
  {
281
6.64M
    while (dinfo->output_scanline < dinfo->output_height)
282
6.63M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
283
6.63M
                           dinfo->output_height - dinfo->output_scanline);
284
1.35k
  }
285
1.35k
  jpeg_finish_decompress(dinfo);
286
287
11.1k
bailout:
288
11.1k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
289
11.1k
  free(row_pointer);
290
11.1k
  if (this->jerr.warning) retval = -1;
291
11.1k
  return retval;
292
1.35k
}
293
294
295
/*************************** Packed-Pixel Image I/O **************************/
296
297
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
298
299
/* TurboJPEG 3.0+ */
300
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
301
static
302
#endif
303
_JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
304
  (tjhandle handle, FILE *file, int *width, int align, int *height,
305
   int *pixelFormat)
306
286k
{
307
286k
  static const char FUNCTION_NAME[] =
308
286k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
309
310
286k
  int retval = 0, tempc;
311
286k
  size_t pitch;
312
286k
  tjhandle handle2 = NULL;
313
286k
  tjinstance *this2;
314
286k
  j_compress_ptr cinfo = NULL;
315
286k
  cjpeg_source_ptr src = NULL;
316
286k
  _JSAMPLE *dstBuf = NULL;
317
286k
  boolean invert;
318
319
286k
  GET_TJINSTANCE(handle, NULL)
320
321
286k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
322
286k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
323
286k
    THROW("Invalid argument");
324
286k
  if ((align & (align - 1)) != 0)
325
286k
    THROW("Alignment must be a power of 2");
326
327
  /* The instance handle passed to this function is used only for parameter
328
     retrieval.  Create a new temporary instance to avoid interfering with the
329
     libjpeg state of the primary instance. */
330
286k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
331
286k
  this2 = (tjinstance *)handle2;
332
286k
  cinfo = &this2->cinfo;
333
334
286k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
286k
  else if (tempc == EOF)
337
286k
    THROW("Input file contains no data");
338
339
286k
  CATCH_LIBJPEG2();
340
341
286k
  cinfo->data_precision = BITS_IN_JSAMPLE;
342
286k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
343
286k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
344
286k
  if (tempc == 'B') {
345
17.9k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
346
17.9k
      THROW("Could not initialize bitmap loader");
347
17.9k
    invert = !this->bottomUp;
348
268k
  } else if (tempc == 0x89) {
349
#if BITS_IN_JSAMPLE == 8
350
87.0k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
351
#else
352
96.7k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
353
64.4k
        this->precision <= BITS_IN_JSAMPLE)
354
64.4k
#endif
355
151k
      cinfo->data_precision = this->precision;
356
183k
    if ((src = _jinit_read_png(cinfo)) == NULL)
357
183k
      THROW("Could not initialize PNG loader");
358
183k
    invert = this->bottomUp;
359
183k
  } else if (tempc == 'P') {
360
#if BITS_IN_JSAMPLE == 8
361
43.3k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
362
#else
363
38.8k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
364
23.1k
        this->precision <= BITS_IN_JSAMPLE)
365
23.1k
#endif
366
66.4k
      cinfo->data_precision = this->precision;
367
82.2k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
368
82.2k
      THROW("Could not initialize PPM loader");
369
82.2k
    invert = this->bottomUp;
370
82.2k
  } else
371
283k
    THROW("Unsupported file type");
372
373
283k
  CATCH_LIBJPEG2();
374
375
114k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
376
377
114k
  src->input_file = file;
378
  /* Refuse to load images larger than the specified size. */
379
114k
  src->max_pixels = this->maxPixels;
380
114k
  (*src->start_input) (cinfo, src);
381
114k
  if (tempc == 'B') {
382
5.23k
    if (cinfo->X_density && cinfo->Y_density) {
383
967
      this->xDensity = cinfo->X_density;
384
967
      this->yDensity = cinfo->Y_density;
385
967
      this->densityUnits = cinfo->density_unit;
386
967
    }
387
109k
  } else if (tempc == 0x89 && (this->init & COMPRESS) &&
388
39.4k
             (this->saveMarkers == 2 || this->saveMarkers == 4)) {
389
39.4k
    JOCTET *iccBuf = NULL;
390
39.4k
    unsigned int iccLen = 0;
391
392
39.4k
    if ((*src->read_icc_profile) (cinfo, src, &iccBuf, &iccLen) && iccBuf &&
393
199
        iccLen)
394
199
      tj3SetICCProfile(handle, (unsigned char *)iccBuf, iccLen);
395
39.4k
  }
396
114k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
397
398
114k
  *width = cinfo->image_width;  *height = cinfo->image_height;
399
114k
  *pixelFormat = cs2pf[cinfo->in_color_space];
400
401
114k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
402
403
114k
  if (
404
#if ULLONG_MAX > SIZE_MAX
405
      (unsigned long long)pitch * (unsigned long long)(*height) *
406
      (unsigned long long)sizeof(_JSAMPLE) >
407
      (unsigned long long)((size_t)-1) ||
408
#endif
409
114k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
410
114k
                                   sizeof(_JSAMPLE))) == NULL)
411
114k
    THROW("Memory allocation failure");
412
413
114k
  CATCH_LIBJPEG2();
414
415
127M
  while (cinfo->next_scanline < cinfo->image_height) {
416
127M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
417
418
255M
    for (i = 0; i < nlines; i++) {
419
127M
      _JSAMPLE *dstptr;
420
127M
      int row;
421
422
127M
      row = cinfo->next_scanline + i;
423
127M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
424
96.2M
      else dstptr = &dstBuf[row * pitch];
425
127M
      memcpy(dstptr, src->_buffer[i],
426
127M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
427
127M
    }
428
127M
    cinfo->next_scanline += nlines;
429
127M
  }
430
431
286k
bailout:
432
286k
  if (src)
433
283k
    (*src->finish_input) (cinfo, src);
434
286k
  tj3Destroy(handle2);
435
286k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
436
286k
  return dstBuf;
437
53.6k
}
_tj3LoadImageFromFileHandle8
Line
Count
Source
306
149k
{
307
149k
  static const char FUNCTION_NAME[] =
308
149k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
309
310
149k
  int retval = 0, tempc;
311
149k
  size_t pitch;
312
149k
  tjhandle handle2 = NULL;
313
149k
  tjinstance *this2;
314
149k
  j_compress_ptr cinfo = NULL;
315
149k
  cjpeg_source_ptr src = NULL;
316
149k
  _JSAMPLE *dstBuf = NULL;
317
149k
  boolean invert;
318
319
149k
  GET_TJINSTANCE(handle, NULL)
320
321
149k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
322
149k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
323
149k
    THROW("Invalid argument");
324
149k
  if ((align & (align - 1)) != 0)
325
149k
    THROW("Alignment must be a power of 2");
326
327
  /* The instance handle passed to this function is used only for parameter
328
     retrieval.  Create a new temporary instance to avoid interfering with the
329
     libjpeg state of the primary instance. */
330
149k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
331
149k
  this2 = (tjinstance *)handle2;
332
149k
  cinfo = &this2->cinfo;
333
334
149k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
149k
  else if (tempc == EOF)
337
149k
    THROW("Input file contains no data");
338
339
149k
  CATCH_LIBJPEG2();
340
341
149k
  cinfo->data_precision = BITS_IN_JSAMPLE;
342
149k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
343
149k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
344
149k
  if (tempc == 'B') {
345
17.8k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
346
17.8k
      THROW("Could not initialize bitmap loader");
347
17.8k
    invert = !this->bottomUp;
348
131k
  } else if (tempc == 0x89) {
349
87.0k
#if BITS_IN_JSAMPLE == 8
350
87.0k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
351
#else
352
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
353
        this->precision <= BITS_IN_JSAMPLE)
354
#endif
355
87.0k
      cinfo->data_precision = this->precision;
356
87.0k
    if ((src = _jinit_read_png(cinfo)) == NULL)
357
87.0k
      THROW("Could not initialize PNG loader");
358
87.0k
    invert = this->bottomUp;
359
87.0k
  } else if (tempc == 'P') {
360
43.3k
#if BITS_IN_JSAMPLE == 8
361
43.3k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
362
#else
363
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
364
        this->precision <= BITS_IN_JSAMPLE)
365
#endif
366
43.3k
      cinfo->data_precision = this->precision;
367
43.3k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
368
43.3k
      THROW("Could not initialize PPM loader");
369
43.3k
    invert = this->bottomUp;
370
43.3k
  } else
371
148k
    THROW("Unsupported file type");
372
373
148k
  CATCH_LIBJPEG2();
374
375
59.0k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
376
377
59.0k
  src->input_file = file;
378
  /* Refuse to load images larger than the specified size. */
379
59.0k
  src->max_pixels = this->maxPixels;
380
59.0k
  (*src->start_input) (cinfo, src);
381
59.0k
  if (tempc == 'B') {
382
5.23k
    if (cinfo->X_density && cinfo->Y_density) {
383
967
      this->xDensity = cinfo->X_density;
384
967
      this->yDensity = cinfo->Y_density;
385
967
      this->densityUnits = cinfo->density_unit;
386
967
    }
387
53.8k
  } else if (tempc == 0x89 && (this->init & COMPRESS) &&
388
16.6k
             (this->saveMarkers == 2 || this->saveMarkers == 4)) {
389
16.6k
    JOCTET *iccBuf = NULL;
390
16.6k
    unsigned int iccLen = 0;
391
392
16.6k
    if ((*src->read_icc_profile) (cinfo, src, &iccBuf, &iccLen) && iccBuf &&
393
173
        iccLen)
394
173
      tj3SetICCProfile(handle, (unsigned char *)iccBuf, iccLen);
395
16.6k
  }
396
59.0k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
397
398
59.0k
  *width = cinfo->image_width;  *height = cinfo->image_height;
399
59.0k
  *pixelFormat = cs2pf[cinfo->in_color_space];
400
401
59.0k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
402
403
59.0k
  if (
404
#if ULLONG_MAX > SIZE_MAX
405
      (unsigned long long)pitch * (unsigned long long)(*height) *
406
      (unsigned long long)sizeof(_JSAMPLE) >
407
      (unsigned long long)((size_t)-1) ||
408
#endif
409
59.0k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
410
59.0k
                                   sizeof(_JSAMPLE))) == NULL)
411
59.0k
    THROW("Memory allocation failure");
412
413
59.0k
  CATCH_LIBJPEG2();
414
415
75.1M
  while (cinfo->next_scanline < cinfo->image_height) {
416
75.1M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
417
418
150M
    for (i = 0; i < nlines; i++) {
419
75.0M
      _JSAMPLE *dstptr;
420
75.0M
      int row;
421
422
75.0M
      row = cinfo->next_scanline + i;
423
75.0M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
424
51.1M
      else dstptr = &dstBuf[row * pitch];
425
75.0M
      memcpy(dstptr, src->_buffer[i],
426
75.0M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
427
75.0M
    }
428
75.1M
    cinfo->next_scanline += nlines;
429
75.1M
  }
430
431
149k
bailout:
432
149k
  if (src)
433
148k
    (*src->finish_input) (cinfo, src);
434
149k
  tj3Destroy(handle2);
435
149k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
436
149k
  return dstBuf;
437
29.7k
}
_tj3LoadImageFromFileHandle12
Line
Count
Source
306
93.8k
{
307
93.8k
  static const char FUNCTION_NAME[] =
308
93.8k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
309
310
93.8k
  int retval = 0, tempc;
311
93.8k
  size_t pitch;
312
93.8k
  tjhandle handle2 = NULL;
313
93.8k
  tjinstance *this2;
314
93.8k
  j_compress_ptr cinfo = NULL;
315
93.8k
  cjpeg_source_ptr src = NULL;
316
93.8k
  _JSAMPLE *dstBuf = NULL;
317
93.8k
  boolean invert;
318
319
93.8k
  GET_TJINSTANCE(handle, NULL)
320
321
93.8k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
322
93.8k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
323
93.8k
    THROW("Invalid argument");
324
93.8k
  if ((align & (align - 1)) != 0)
325
93.8k
    THROW("Alignment must be a power of 2");
326
327
  /* The instance handle passed to this function is used only for parameter
328
     retrieval.  Create a new temporary instance to avoid interfering with the
329
     libjpeg state of the primary instance. */
330
93.8k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
331
93.8k
  this2 = (tjinstance *)handle2;
332
93.8k
  cinfo = &this2->cinfo;
333
334
93.8k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
93.8k
  else if (tempc == EOF)
337
93.8k
    THROW("Input file contains no data");
338
339
93.8k
  CATCH_LIBJPEG2();
340
341
93.8k
  cinfo->data_precision = BITS_IN_JSAMPLE;
342
93.8k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
343
93.8k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
344
93.8k
  if (tempc == 'B') {
345
56
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
346
56
      THROW("Could not initialize bitmap loader");
347
56
    invert = !this->bottomUp;
348
93.7k
  } else if (tempc == 0x89) {
349
#if BITS_IN_JSAMPLE == 8
350
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
351
#else
352
65.2k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
353
32.9k
        this->precision <= BITS_IN_JSAMPLE)
354
32.9k
#endif
355
32.9k
      cinfo->data_precision = this->precision;
356
65.2k
    if ((src = _jinit_read_png(cinfo)) == NULL)
357
65.2k
      THROW("Could not initialize PNG loader");
358
65.2k
    invert = this->bottomUp;
359
65.2k
  } else if (tempc == 'P') {
360
#if BITS_IN_JSAMPLE == 8
361
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
362
#else
363
27.2k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
364
11.5k
        this->precision <= BITS_IN_JSAMPLE)
365
11.5k
#endif
366
11.5k
      cinfo->data_precision = this->precision;
367
27.2k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
368
27.2k
      THROW("Could not initialize PPM loader");
369
27.2k
    invert = this->bottomUp;
370
27.2k
  } else
371
92.6k
    THROW("Unsupported file type");
372
373
92.6k
  CATCH_LIBJPEG2();
374
375
38.1k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
376
377
38.1k
  src->input_file = file;
378
  /* Refuse to load images larger than the specified size. */
379
38.1k
  src->max_pixels = this->maxPixels;
380
38.1k
  (*src->start_input) (cinfo, src);
381
38.1k
  if (tempc == 'B') {
382
0
    if (cinfo->X_density && cinfo->Y_density) {
383
0
      this->xDensity = cinfo->X_density;
384
0
      this->yDensity = cinfo->Y_density;
385
0
      this->densityUnits = cinfo->density_unit;
386
0
    }
387
38.1k
  } else if (tempc == 0x89 && (this->init & COMPRESS) &&
388
15.1k
             (this->saveMarkers == 2 || this->saveMarkers == 4)) {
389
15.1k
    JOCTET *iccBuf = NULL;
390
15.1k
    unsigned int iccLen = 0;
391
392
15.1k
    if ((*src->read_icc_profile) (cinfo, src, &iccBuf, &iccLen) && iccBuf &&
393
13
        iccLen)
394
13
      tj3SetICCProfile(handle, (unsigned char *)iccBuf, iccLen);
395
15.1k
  }
396
38.1k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
397
398
38.1k
  *width = cinfo->image_width;  *height = cinfo->image_height;
399
38.1k
  *pixelFormat = cs2pf[cinfo->in_color_space];
400
401
38.1k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
402
403
38.1k
  if (
404
#if ULLONG_MAX > SIZE_MAX
405
      (unsigned long long)pitch * (unsigned long long)(*height) *
406
      (unsigned long long)sizeof(_JSAMPLE) >
407
      (unsigned long long)((size_t)-1) ||
408
#endif
409
38.1k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
410
38.1k
                                   sizeof(_JSAMPLE))) == NULL)
411
38.1k
    THROW("Memory allocation failure");
412
413
38.1k
  CATCH_LIBJPEG2();
414
415
39.5M
  while (cinfo->next_scanline < cinfo->image_height) {
416
39.5M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
417
418
79.0M
    for (i = 0; i < nlines; i++) {
419
39.5M
      _JSAMPLE *dstptr;
420
39.5M
      int row;
421
422
39.5M
      row = cinfo->next_scanline + i;
423
39.5M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
424
33.7M
      else dstptr = &dstBuf[row * pitch];
425
39.5M
      memcpy(dstptr, src->_buffer[i],
426
39.5M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
427
39.5M
    }
428
39.5M
    cinfo->next_scanline += nlines;
429
39.5M
  }
430
431
93.8k
bailout:
432
93.8k
  if (src)
433
92.5k
    (*src->finish_input) (cinfo, src);
434
93.8k
  tj3Destroy(handle2);
435
93.8k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
436
93.8k
  return dstBuf;
437
17.6k
}
_tj3LoadImageFromFileHandle16
Line
Count
Source
306
43.4k
{
307
43.4k
  static const char FUNCTION_NAME[] =
308
43.4k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
309
310
43.4k
  int retval = 0, tempc;
311
43.4k
  size_t pitch;
312
43.4k
  tjhandle handle2 = NULL;
313
43.4k
  tjinstance *this2;
314
43.4k
  j_compress_ptr cinfo = NULL;
315
43.4k
  cjpeg_source_ptr src = NULL;
316
43.4k
  _JSAMPLE *dstBuf = NULL;
317
43.4k
  boolean invert;
318
319
43.4k
  GET_TJINSTANCE(handle, NULL)
320
321
43.4k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
322
43.4k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
323
43.4k
    THROW("Invalid argument");
324
43.4k
  if ((align & (align - 1)) != 0)
325
43.4k
    THROW("Alignment must be a power of 2");
326
327
  /* The instance handle passed to this function is used only for parameter
328
     retrieval.  Create a new temporary instance to avoid interfering with the
329
     libjpeg state of the primary instance. */
330
43.4k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
331
43.4k
  this2 = (tjinstance *)handle2;
332
43.4k
  cinfo = &this2->cinfo;
333
334
43.4k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
43.4k
  else if (tempc == EOF)
337
43.4k
    THROW("Input file contains no data");
338
339
43.4k
  CATCH_LIBJPEG2();
340
341
43.3k
  cinfo->data_precision = BITS_IN_JSAMPLE;
342
43.3k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
343
43.3k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
344
43.3k
  if (tempc == 'B') {
345
7
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
346
7
      THROW("Could not initialize bitmap loader");
347
7
    invert = !this->bottomUp;
348
43.3k
  } else if (tempc == 0x89) {
349
#if BITS_IN_JSAMPLE == 8
350
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
351
#else
352
31.4k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
353
31.4k
        this->precision <= BITS_IN_JSAMPLE)
354
31.4k
#endif
355
31.4k
      cinfo->data_precision = this->precision;
356
31.4k
    if ((src = _jinit_read_png(cinfo)) == NULL)
357
31.4k
      THROW("Could not initialize PNG loader");
358
31.4k
    invert = this->bottomUp;
359
31.4k
  } else if (tempc == 'P') {
360
#if BITS_IN_JSAMPLE == 8
361
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
362
#else
363
11.5k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
364
11.5k
        this->precision <= BITS_IN_JSAMPLE)
365
11.5k
#endif
366
11.5k
      cinfo->data_precision = this->precision;
367
11.5k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
368
11.5k
      THROW("Could not initialize PPM loader");
369
11.5k
    invert = this->bottomUp;
370
11.5k
  } else
371
43.0k
    THROW("Unsupported file type");
372
373
43.0k
  CATCH_LIBJPEG2();
374
375
17.0k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
376
377
17.0k
  src->input_file = file;
378
  /* Refuse to load images larger than the specified size. */
379
17.0k
  src->max_pixels = this->maxPixels;
380
17.0k
  (*src->start_input) (cinfo, src);
381
17.0k
  if (tempc == 'B') {
382
0
    if (cinfo->X_density && cinfo->Y_density) {
383
0
      this->xDensity = cinfo->X_density;
384
0
      this->yDensity = cinfo->Y_density;
385
0
      this->densityUnits = cinfo->density_unit;
386
0
    }
387
17.0k
  } else if (tempc == 0x89 && (this->init & COMPRESS) &&
388
7.64k
             (this->saveMarkers == 2 || this->saveMarkers == 4)) {
389
7.64k
    JOCTET *iccBuf = NULL;
390
7.64k
    unsigned int iccLen = 0;
391
392
7.64k
    if ((*src->read_icc_profile) (cinfo, src, &iccBuf, &iccLen) && iccBuf &&
393
13
        iccLen)
394
13
      tj3SetICCProfile(handle, (unsigned char *)iccBuf, iccLen);
395
7.64k
  }
396
17.0k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
397
398
17.0k
  *width = cinfo->image_width;  *height = cinfo->image_height;
399
17.0k
  *pixelFormat = cs2pf[cinfo->in_color_space];
400
401
17.0k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
402
403
17.0k
  if (
404
#if ULLONG_MAX > SIZE_MAX
405
      (unsigned long long)pitch * (unsigned long long)(*height) *
406
      (unsigned long long)sizeof(_JSAMPLE) >
407
      (unsigned long long)((size_t)-1) ||
408
#endif
409
17.0k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
410
17.0k
                                   sizeof(_JSAMPLE))) == NULL)
411
17.0k
    THROW("Memory allocation failure");
412
413
17.0k
  CATCH_LIBJPEG2();
414
415
13.2M
  while (cinfo->next_scanline < cinfo->image_height) {
416
13.2M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
417
418
26.5M
    for (i = 0; i < nlines; i++) {
419
13.2M
      _JSAMPLE *dstptr;
420
13.2M
      int row;
421
422
13.2M
      row = cinfo->next_scanline + i;
423
13.2M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
424
11.3M
      else dstptr = &dstBuf[row * pitch];
425
13.2M
      memcpy(dstptr, src->_buffer[i],
426
13.2M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
427
13.2M
    }
428
13.2M
    cinfo->next_scanline += nlines;
429
13.2M
  }
430
431
43.4k
bailout:
432
43.4k
  if (src)
433
43.0k
    (*src->finish_input) (cinfo, src);
434
43.4k
  tj3Destroy(handle2);
435
43.4k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
436
43.4k
  return dstBuf;
437
6.23k
}
438
439
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
440
441
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
442
  (tjhandle handle, const char *filename, int *width, int align, int *height,
443
   int *pixelFormat)
444
0
{
445
0
  static const char FUNCTION_NAME[] =
446
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
447
448
0
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
449
450
0
  int retval = 0;
451
0
  _JSAMPLE *dstBuf = NULL;
452
0
  FILE *file = NULL;
453
454
0
  GET_TJINSTANCE(handle, NULL)
455
456
0
  if (!filename)
457
0
    THROW("Invalid argument");
458
459
#ifdef _MSC_VER
460
  if (fopen_s(&file, filename, "rb") || file == NULL)
461
#else
462
0
  if ((file = fopen(filename, "rb")) == NULL)
463
0
#endif
464
0
    THROW_UNIX("Cannot open input file");
465
466
0
  dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
467
0
             (handle, file, width, align, height, pixelFormat);
468
469
0
bailout:
470
0
  if (file) fclose(file);
471
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
472
0
  return dstBuf;
473
474
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
475
476
  static const char ERROR_MSG[] =
477
    "16-bit data precision requires lossless JPEG,\n"
478
    "which was disabled at build time.";
479
  _JSAMPLE *retval = NULL;
480
481
  GET_TJINSTANCE(handle, NULL)
482
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
483
           ERROR_MSG);
484
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
485
486
bailout:
487
  return retval;
488
489
#endif
490
0
}
Unexecuted instantiation: tj3LoadImage8
Unexecuted instantiation: tj3LoadImage12
Unexecuted instantiation: tj3LoadImage16
491
492
493
/* TurboJPEG 3.0+ */
494
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
495
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
496
   int pitch, int height, int pixelFormat)
497
0
{
498
0
  static const char FUNCTION_NAME[] =
499
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
500
0
  int retval = 0;
501
502
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
503
504
0
  tjhandle handle2 = NULL;
505
0
  tjinstance *this2;
506
0
  j_decompress_ptr dinfo = NULL;
507
0
  djpeg_dest_ptr dst;
508
0
  FILE *file = NULL;
509
0
  const char *ptr = NULL;
510
0
  boolean invert;
511
512
0
  GET_TJINSTANCE(handle, -1)
513
514
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
515
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
516
0
    THROW("Invalid argument");
517
518
  /* The instance handle passed to this function is used only for parameter
519
     retrieval.  Create a new temporary instance to avoid interfering with the
520
     libjpeg state of the primary instance. */
521
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
522
0
    return -1;
523
0
  this2 = (tjinstance *)handle2;
524
0
  dinfo = &this2->dinfo;
525
526
#ifdef _MSC_VER
527
  if (fopen_s(&file, filename, "wb") || file == NULL)
528
#else
529
0
  if ((file = fopen(filename, "wb")) == NULL)
530
0
#endif
531
0
    THROW_UNIX("Cannot open output file");
532
533
0
  CATCH_LIBJPEG2();
534
535
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
536
0
  dinfo->image_width = width;  dinfo->image_height = height;
537
0
  dinfo->global_state = DSTATE_READY;
538
0
  dinfo->scale_num = dinfo->scale_denom = 1;
539
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
540
541
0
  ptr = strrchr(filename, '.');
542
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
543
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
544
0
      THROW("Could not initialize bitmap writer");
545
0
    invert = !this->bottomUp;
546
0
    dinfo->X_density = (UINT16)this->xDensity;
547
0
    dinfo->Y_density = (UINT16)this->yDensity;
548
0
    dinfo->density_unit = (UINT8)this->densityUnits;
549
0
  } else if (ptr && !strcasecmp(ptr, ".png")) {
550
#if BITS_IN_JSAMPLE == 8
551
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
552
#else
553
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
554
0
        this->precision <= BITS_IN_JSAMPLE)
555
0
#endif
556
0
      dinfo->data_precision = this->precision;
557
0
    if ((dst = _jinit_write_png(dinfo)) == NULL)
558
0
      THROW("Could not initialize PNG writer");
559
0
    invert = this->bottomUp;
560
561
0
    if ((this->init & DECOMPRESS) && this->decompICCBuf &&
562
0
        this->decompICCSize) {
563
0
      unsigned char *iccBuf = (unsigned char *)malloc(this->decompICCSize);
564
565
0
      if (!iccBuf)
566
0
        THROW("Memory allocation failure");
567
0
      memcpy(iccBuf, this->decompICCBuf, this->decompICCSize);
568
0
      (*dst->write_icc_profile) (dinfo, dst, iccBuf,
569
0
                                 (unsigned int)this->decompICCSize);
570
0
    }
571
0
  } else {
572
#if BITS_IN_JSAMPLE == 8
573
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
574
#else
575
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
576
0
        this->precision <= BITS_IN_JSAMPLE)
577
0
#endif
578
0
      dinfo->data_precision = this->precision;
579
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
580
0
      THROW("Could not initialize PPM writer");
581
0
    invert = this->bottomUp;
582
0
  }
583
584
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
585
586
0
  dst->output_file = file;
587
0
  (*dst->start_output) (dinfo, dst);
588
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
589
590
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
591
0
  else if (pitch < width * tjPixelSize[pixelFormat])
592
0
    THROW("Invalid argument");
593
594
0
  while (dinfo->output_scanline < dinfo->output_height) {
595
0
    _JSAMPLE *rowptr;
596
597
0
    if (invert)
598
0
      rowptr =
599
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
600
0
    else
601
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
602
0
    memcpy(dst->_buffer[0], rowptr,
603
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
604
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
605
0
    dinfo->output_scanline++;
606
0
  }
607
608
0
  (*dst->finish_output) (dinfo, dst);
609
610
0
bailout:
611
0
  tj3Destroy(handle2);
612
0
  if (file) fclose(file);
613
0
  return retval;
614
615
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
616
617
  GET_TJINSTANCE(handle, -1)
618
  THROW("16-bit data precision requires lossless JPEG,\n"
619
        "which was disabled at build time.")
620
bailout:
621
  return retval;
622
623
#endif
624
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
625
626
627
#undef _JSAMPLE
628
#undef _JSAMPROW
629
#undef _buffer
630
#undef _jinit_read_png
631
#undef _jinit_write_png
632
#undef _jinit_read_ppm
633
#undef _jinit_write_ppm
634
#undef _jpeg_crop_scanline
635
#undef _jpeg_read_scanlines
636
#undef _jpeg_skip_scanlines
637
#undef _jpeg_write_scanlines