Coverage Report

Created: 2026-10-08 08:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.1.x/src/turbojpeg-mp.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2009-2026 D. R. Commander.  All Rights Reserved.
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
79.9M
#define _JSAMPLE  JSAMPLE
34
108k
#define _JSAMPROW  JSAMPROW
35
79.8M
#define _buffer  buffer
36
48.0k
#define _jinit_read_ppm  jinit_read_ppm
37
0
#define _jinit_write_ppm  jinit_write_ppm
38
2.16k
#define _jpeg_crop_scanline  jpeg_crop_scanline
39
32.2M
#define _jpeg_read_scanlines  jpeg_read_scanlines
40
4.29k
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
41
68.4k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
66.0M
#define _JSAMPLE  J12SAMPLE
44
80.6k
#define _JSAMPROW  J12SAMPROW
45
65.9M
#define _buffer  buffer12
46
33.8k
#define _jinit_read_ppm  j12init_read_ppm
47
0
#define _jinit_write_ppm  j12init_write_ppm
48
1.16k
#define _jpeg_crop_scanline  jpeg12_crop_scanline
49
16.6M
#define _jpeg_read_scanlines  jpeg12_read_scanlines
50
2.31k
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
51
56.8k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
21.6M
#define _JSAMPLE  J16SAMPLE
54
32.3k
#define _JSAMPROW  J16SAMPROW
55
21.5M
#define _buffer  buffer16
56
13.3k
#define _jinit_read_ppm  j16init_read_ppm
57
0
#define _jinit_write_ppm  j16init_write_ppm
58
6.32M
#define _jpeg_read_scanlines  jpeg16_read_scanlines
59
21.4k
#define _jpeg_write_scanlines  jpeg16_write_scanlines
60
#endif
61
62
0
#define _GET_NAME(name, suffix)  name##suffix
63
0
#define GET_NAME(name, suffix)  _GET_NAME(name, suffix)
64
337k
#define _GET_STRING(name, suffix)  #name #suffix
65
337k
#define GET_STRING(name, suffix)  _GET_STRING(name, suffix)
66
67
68
/******************************** Compressor *********************************/
69
70
/* TurboJPEG 3.0+ */
71
DLLEXPORT int GET_NAME(tj3Compress, BITS_IN_JSAMPLE)
72
  (tjhandle handle, const _JSAMPLE *srcBuf, int width, int pitch, int height,
73
   int pixelFormat, unsigned char **jpegBuf, size_t *jpegSize)
74
146k
{
75
146k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
146k
  int i, retval = 0;
77
146k
  boolean alloc = TRUE;
78
146k
  _JSAMPROW *row_pointer = NULL;
79
80
146k
  GET_CINSTANCE(handle)
81
146k
  if ((this->init & COMPRESS) == 0)
82
146k
    THROW("Instance has not been initialized for compression");
83
84
146k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
146k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
146k
      jpegSize == NULL)
87
146k
    THROW("Invalid argument");
88
89
146k
  if (!this->lossless && this->quality == -1)
90
146k
    THROW("TJPARAM_QUALITY must be specified");
91
146k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
146k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
146k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
146k
    THROW("Invalid argument");
97
98
146k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
146k
    THROW("Memory allocation failure");
100
101
146k
  CATCH_LIBJPEG(this);
102
103
146k
  cinfo->image_width = width;
104
146k
  cinfo->image_height = height;
105
146k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
#if BITS_IN_JSAMPLE == 8
107
68.4k
  if (this->lossless && this->precision >= 2 &&
108
27.1k
      this->precision <= BITS_IN_JSAMPLE)
109
#else
110
78.2k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
111
43.0k
      this->precision <= BITS_IN_JSAMPLE)
112
43.0k
#endif
113
70.1k
    cinfo->data_precision = this->precision;
114
115
146k
  setCompDefaults(this, pixelFormat, FALSE);
116
146k
  if (this->noRealloc) alloc = FALSE;
117
146k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
118
119
146k
  jpeg_start_compress(cinfo, TRUE);
120
146k
  if (this->iccBuf != NULL && this->iccSize != 0)
121
124k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
122
221M
  for (i = 0; i < height; i++) {
123
220M
    if (this->bottomUp)
124
32.5M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
125
188M
    else
126
188M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
127
220M
  }
128
293k
  while (cinfo->next_scanline < cinfo->image_height)
129
146k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
130
146k
                          cinfo->image_height - cinfo->next_scanline);
131
146k
  jpeg_finish_compress(cinfo);
132
133
146k
bailout:
134
146k
  if (cinfo->global_state > CSTATE_START && alloc)
135
131
    (*cinfo->dest->term_destination) (cinfo);
136
146k
  if (cinfo->global_state > CSTATE_START || retval == -1)
137
131
    jpeg_abort_compress(cinfo);
138
146k
  free(row_pointer);
139
146k
  if (this->jerr.warning) retval = -1;
140
146k
  return retval;
141
146k
}
tj3Compress8
Line
Count
Source
74
68.4k
{
75
68.4k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
68.4k
  int i, retval = 0;
77
68.4k
  boolean alloc = TRUE;
78
68.4k
  _JSAMPROW *row_pointer = NULL;
79
80
68.4k
  GET_CINSTANCE(handle)
81
68.4k
  if ((this->init & COMPRESS) == 0)
82
68.4k
    THROW("Instance has not been initialized for compression");
83
84
68.4k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
68.4k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
68.4k
      jpegSize == NULL)
87
68.4k
    THROW("Invalid argument");
88
89
68.4k
  if (!this->lossless && this->quality == -1)
90
68.4k
    THROW("TJPARAM_QUALITY must be specified");
91
68.4k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
68.4k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
68.4k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
68.4k
    THROW("Invalid argument");
97
98
68.4k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
68.4k
    THROW("Memory allocation failure");
100
101
68.4k
  CATCH_LIBJPEG(this);
102
103
68.4k
  cinfo->image_width = width;
104
68.4k
  cinfo->image_height = height;
105
68.4k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
68.4k
#if BITS_IN_JSAMPLE == 8
107
68.4k
  if (this->lossless && this->precision >= 2 &&
108
27.1k
      this->precision <= BITS_IN_JSAMPLE)
109
#else
110
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
111
      this->precision <= BITS_IN_JSAMPLE)
112
#endif
113
27.1k
    cinfo->data_precision = this->precision;
114
115
68.4k
  setCompDefaults(this, pixelFormat, FALSE);
116
68.4k
  if (this->noRealloc) alloc = FALSE;
117
68.4k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
118
119
68.4k
  jpeg_start_compress(cinfo, TRUE);
120
68.4k
  if (this->iccBuf != NULL && this->iccSize != 0)
121
58.9k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
122
89.3M
  for (i = 0; i < height; i++) {
123
89.2M
    if (this->bottomUp)
124
13.4M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
125
75.7M
    else
126
75.7M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
127
89.2M
  }
128
136k
  while (cinfo->next_scanline < cinfo->image_height)
129
68.4k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
130
68.4k
                          cinfo->image_height - cinfo->next_scanline);
131
68.4k
  jpeg_finish_compress(cinfo);
132
133
68.4k
bailout:
134
68.4k
  if (cinfo->global_state > CSTATE_START && alloc)
135
0
    (*cinfo->dest->term_destination) (cinfo);
136
68.4k
  if (cinfo->global_state > CSTATE_START || retval == -1)
137
0
    jpeg_abort_compress(cinfo);
138
68.4k
  free(row_pointer);
139
68.4k
  if (this->jerr.warning) retval = -1;
140
68.4k
  return retval;
141
68.4k
}
tj3Compress12
Line
Count
Source
74
56.8k
{
75
56.8k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
56.8k
  int i, retval = 0;
77
56.8k
  boolean alloc = TRUE;
78
56.8k
  _JSAMPROW *row_pointer = NULL;
79
80
56.8k
  GET_CINSTANCE(handle)
81
56.8k
  if ((this->init & COMPRESS) == 0)
82
56.8k
    THROW("Instance has not been initialized for compression");
83
84
56.8k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
56.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
56.8k
      jpegSize == NULL)
87
56.8k
    THROW("Invalid argument");
88
89
56.8k
  if (!this->lossless && this->quality == -1)
90
56.8k
    THROW("TJPARAM_QUALITY must be specified");
91
56.8k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
56.8k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
56.8k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
56.8k
    THROW("Invalid argument");
97
98
56.8k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
56.8k
    THROW("Memory allocation failure");
100
101
56.8k
  CATCH_LIBJPEG(this);
102
103
56.8k
  cinfo->image_width = width;
104
56.8k
  cinfo->image_height = height;
105
56.8k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
#if BITS_IN_JSAMPLE == 8
107
  if (this->lossless && this->precision >= 2 &&
108
      this->precision <= BITS_IN_JSAMPLE)
109
#else
110
56.8k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
111
21.5k
      this->precision <= BITS_IN_JSAMPLE)
112
21.5k
#endif
113
21.5k
    cinfo->data_precision = this->precision;
114
115
56.8k
  setCompDefaults(this, pixelFormat, FALSE);
116
56.8k
  if (this->noRealloc) alloc = FALSE;
117
56.8k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
118
119
56.8k
  jpeg_start_compress(cinfo, TRUE);
120
56.8k
  if (this->iccBuf != NULL && this->iccSize != 0)
121
47.1k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
122
102M
  for (i = 0; i < height; i++) {
123
102M
    if (this->bottomUp)
124
14.8M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
125
87.4M
    else
126
87.4M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
127
102M
  }
128
113k
  while (cinfo->next_scanline < cinfo->image_height)
129
56.8k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
130
56.8k
                          cinfo->image_height - cinfo->next_scanline);
131
56.8k
  jpeg_finish_compress(cinfo);
132
133
56.8k
bailout:
134
56.8k
  if (cinfo->global_state > CSTATE_START && alloc)
135
51
    (*cinfo->dest->term_destination) (cinfo);
136
56.8k
  if (cinfo->global_state > CSTATE_START || retval == -1)
137
51
    jpeg_abort_compress(cinfo);
138
56.8k
  free(row_pointer);
139
56.8k
  if (this->jerr.warning) retval = -1;
140
56.8k
  return retval;
141
56.8k
}
tj3Compress16
Line
Count
Source
74
21.4k
{
75
21.4k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
21.4k
  int i, retval = 0;
77
21.4k
  boolean alloc = TRUE;
78
21.4k
  _JSAMPROW *row_pointer = NULL;
79
80
21.4k
  GET_CINSTANCE(handle)
81
21.4k
  if ((this->init & COMPRESS) == 0)
82
21.4k
    THROW("Instance has not been initialized for compression");
83
84
21.4k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
21.4k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
21.4k
      jpegSize == NULL)
87
21.4k
    THROW("Invalid argument");
88
89
21.4k
  if (!this->lossless && this->quality == -1)
90
21.4k
    THROW("TJPARAM_QUALITY must be specified");
91
21.4k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
21.4k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
21.4k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
21.4k
    THROW("Invalid argument");
97
98
21.4k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
21.4k
    THROW("Memory allocation failure");
100
101
21.4k
  CATCH_LIBJPEG(this);
102
103
21.4k
  cinfo->image_width = width;
104
21.4k
  cinfo->image_height = height;
105
21.4k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
#if BITS_IN_JSAMPLE == 8
107
  if (this->lossless && this->precision >= 2 &&
108
      this->precision <= BITS_IN_JSAMPLE)
109
#else
110
21.4k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
111
21.4k
      this->precision <= BITS_IN_JSAMPLE)
112
21.4k
#endif
113
21.4k
    cinfo->data_precision = this->precision;
114
115
21.4k
  setCompDefaults(this, pixelFormat, FALSE);
116
21.4k
  if (this->noRealloc) alloc = FALSE;
117
21.4k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
118
119
21.4k
  jpeg_start_compress(cinfo, TRUE);
120
21.4k
  if (this->iccBuf != NULL && this->iccSize != 0)
121
18.5k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
122
29.5M
  for (i = 0; i < height; i++) {
123
29.4M
    if (this->bottomUp)
124
4.21M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
125
25.2M
    else
126
25.2M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
127
29.4M
  }
128
42.8k
  while (cinfo->next_scanline < cinfo->image_height)
129
21.4k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
130
21.4k
                          cinfo->image_height - cinfo->next_scanline);
131
21.4k
  jpeg_finish_compress(cinfo);
132
133
21.4k
bailout:
134
21.4k
  if (cinfo->global_state > CSTATE_START && alloc)
135
80
    (*cinfo->dest->term_destination) (cinfo);
136
21.4k
  if (cinfo->global_state > CSTATE_START || retval == -1)
137
80
    jpeg_abort_compress(cinfo);
138
21.4k
  free(row_pointer);
139
21.4k
  if (this->jerr.warning) retval = -1;
140
21.4k
  return retval;
141
21.4k
}
142
143
144
/******************************* Decompressor ********************************/
145
146
/* TurboJPEG 3.0+ */
147
DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE)
148
  (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize,
149
   _JSAMPLE *dstBuf, int pitch, int pixelFormat)
150
74.3k
{
151
74.3k
  static const char FUNCTION_NAME[] =
152
74.3k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
153
74.3k
  _JSAMPROW *row_pointer = NULL;
154
74.3k
  int croppedHeight, i, retval = 0;
155
#if BITS_IN_JSAMPLE != 16
156
  int scaledWidth;
157
#endif
158
74.3k
  struct my_progress_mgr progress;
159
160
74.3k
  GET_DINSTANCE(handle);
161
74.3k
  if ((this->init & DECOMPRESS) == 0)
162
74.3k
    THROW("Instance has not been initialized for decompression");
163
164
74.3k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
165
74.3k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
166
74.3k
    THROW("Invalid argument");
167
168
74.3k
  if (this->scanLimit) {
169
74.3k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
170
74.3k
    progress.pub.progress_monitor = my_progress_monitor;
171
74.3k
    progress.this = this;
172
74.3k
    dinfo->progress = &progress.pub;
173
74.3k
  } else
174
0
    dinfo->progress = NULL;
175
176
74.3k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
177
178
74.3k
  CATCH_LIBJPEG(this);
179
180
74.3k
  if (dinfo->global_state <= DSTATE_INHEADER) {
181
74.3k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
182
74.3k
    jpeg_read_header(dinfo, TRUE);
183
74.3k
  }
184
31.3k
  setDecompParameters(this);
185
31.3k
  if (this->maxPixels &&
186
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
187
0
      (unsigned long long)this->maxPixels)
188
31.3k
    THROW("Image is too large");
189
31.3k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
190
#if BITS_IN_JSAMPLE != 16
191
28.9k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
192
#endif
193
31.3k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
194
31.3k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
195
196
31.3k
  dinfo->scale_num = this->scalingFactor.num;
197
31.3k
  dinfo->scale_denom = this->scalingFactor.denom;
198
199
31.3k
  jpeg_start_decompress(dinfo);
200
201
#if BITS_IN_JSAMPLE != 16
202
28.9k
  if (this->croppingRegion.x != 0 ||
203
25.6k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
204
3.32k
    JDIMENSION crop_x = this->croppingRegion.x;
205
3.32k
    JDIMENSION crop_w = this->croppingRegion.w;
206
207
3.32k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
208
3.32k
    if ((int)crop_x != this->croppingRegion.x)
209
9
      THROWI("Unexplained mismatch between specified (%d) and\n"
210
3.32k
             "actual (%d) cropping region left boundary",
211
3.32k
             this->croppingRegion.x, (int)crop_x);
212
3.31k
    if ((int)crop_w != this->croppingRegion.w)
213
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
214
3.31k
             "actual (%d) cropping region width",
215
3.31k
             this->croppingRegion.w, (int)crop_w);
216
3.31k
  }
217
28.9k
#endif
218
219
31.3k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
220
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
221
31.3k
    THROW("Invalid argument");
222
223
31.3k
  croppedHeight = dinfo->output_height;
224
#if BITS_IN_JSAMPLE != 16
225
28.9k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
226
3.31k
    croppedHeight = this->croppingRegion.h;
227
#endif
228
31.3k
  if ((row_pointer =
229
31.3k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
230
31.3k
    THROW("Memory allocation failure");
231
31.3k
  CATCH_LIBJPEG(this);
232
126M
  for (i = 0; i < (int)croppedHeight; i++) {
233
126M
    if (this->bottomUp)
234
40.3M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
235
85.7M
    else
236
85.7M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
237
126M
  }
238
239
#if BITS_IN_JSAMPLE != 16
240
25.6k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
241
3.31k
    if (this->croppingRegion.y != 0) {
242
3.31k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
243
244
3.31k
      if ((int)lines != this->croppingRegion.y)
245
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
246
               "actual (%d) cropping region upper boundary",
247
3.31k
               this->croppingRegion.y, (int)lines);
248
3.31k
    }
249
128k
    while ((int)dinfo->output_scanline <
250
128k
           this->croppingRegion.y + this->croppingRegion.h)
251
125k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
252
125k
                                               this->croppingRegion.y],
253
125k
                           this->croppingRegion.y + this->croppingRegion.h -
254
125k
                           dinfo->output_scanline);
255
3.31k
    if (this->croppingRegion.y + this->croppingRegion.h !=
256
3.31k
        (int)dinfo->output_height) {
257
3.29k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
258
                                                     this->croppingRegion.y -
259
                                                     this->croppingRegion.h);
260
261
3.29k
      if (lines != dinfo->output_height - this->croppingRegion.y -
262
3.29k
                   this->croppingRegion.h)
263
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
264
3.29k
               "actual (%d) cropping region lower boundary",
265
3.29k
               this->croppingRegion.y + this->croppingRegion.h,
266
3.29k
               (int)(dinfo->output_height - lines));
267
3.29k
    }
268
3.31k
  } else
269
21.6k
#endif
270
21.6k
  {
271
55.0M
    while (dinfo->output_scanline < dinfo->output_height)
272
55.0M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
273
55.0M
                           dinfo->output_height - dinfo->output_scanline);
274
21.6k
  }
275
24.9k
  jpeg_finish_decompress(dinfo);
276
277
74.3k
bailout:
278
74.3k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
279
74.3k
  free(row_pointer);
280
74.3k
  if (this->jerr.warning) retval = -1;
281
74.3k
  return retval;
282
24.9k
}
tj3Decompress8
Line
Count
Source
150
39.6k
{
151
39.6k
  static const char FUNCTION_NAME[] =
152
39.6k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
153
39.6k
  _JSAMPROW *row_pointer = NULL;
154
39.6k
  int croppedHeight, i, retval = 0;
155
39.6k
#if BITS_IN_JSAMPLE != 16
156
39.6k
  int scaledWidth;
157
39.6k
#endif
158
39.6k
  struct my_progress_mgr progress;
159
160
39.6k
  GET_DINSTANCE(handle);
161
39.6k
  if ((this->init & DECOMPRESS) == 0)
162
39.6k
    THROW("Instance has not been initialized for decompression");
163
164
39.6k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
165
39.6k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
166
39.6k
    THROW("Invalid argument");
167
168
39.6k
  if (this->scanLimit) {
169
39.6k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
170
39.6k
    progress.pub.progress_monitor = my_progress_monitor;
171
39.6k
    progress.this = this;
172
39.6k
    dinfo->progress = &progress.pub;
173
39.6k
  } else
174
0
    dinfo->progress = NULL;
175
176
39.6k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
177
178
39.6k
  CATCH_LIBJPEG(this);
179
180
39.6k
  if (dinfo->global_state <= DSTATE_INHEADER) {
181
39.6k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
182
39.6k
    jpeg_read_header(dinfo, TRUE);
183
39.6k
  }
184
17.7k
  setDecompParameters(this);
185
17.7k
  if (this->maxPixels &&
186
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
187
0
      (unsigned long long)this->maxPixels)
188
17.7k
    THROW("Image is too large");
189
17.7k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
190
17.7k
#if BITS_IN_JSAMPLE != 16
191
17.7k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
192
17.7k
#endif
193
17.7k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
194
17.7k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
195
196
17.7k
  dinfo->scale_num = this->scalingFactor.num;
197
17.7k
  dinfo->scale_denom = this->scalingFactor.denom;
198
199
17.7k
  jpeg_start_decompress(dinfo);
200
201
17.7k
#if BITS_IN_JSAMPLE != 16
202
17.7k
  if (this->croppingRegion.x != 0 ||
203
15.5k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
204
2.16k
    JDIMENSION crop_x = this->croppingRegion.x;
205
2.16k
    JDIMENSION crop_w = this->croppingRegion.w;
206
207
2.16k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
208
2.16k
    if ((int)crop_x != this->croppingRegion.x)
209
6
      THROWI("Unexplained mismatch between specified (%d) and\n"
210
2.16k
             "actual (%d) cropping region left boundary",
211
2.16k
             this->croppingRegion.x, (int)crop_x);
212
2.15k
    if ((int)crop_w != this->croppingRegion.w)
213
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
214
2.15k
             "actual (%d) cropping region width",
215
2.15k
             this->croppingRegion.w, (int)crop_w);
216
2.15k
  }
217
17.7k
#endif
218
219
17.7k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
220
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
221
17.7k
    THROW("Invalid argument");
222
223
17.7k
  croppedHeight = dinfo->output_height;
224
17.7k
#if BITS_IN_JSAMPLE != 16
225
17.7k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
226
2.15k
    croppedHeight = this->croppingRegion.h;
227
17.7k
#endif
228
17.7k
  if ((row_pointer =
229
17.7k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
230
17.7k
    THROW("Memory allocation failure");
231
17.7k
  CATCH_LIBJPEG(this);
232
67.5M
  for (i = 0; i < (int)croppedHeight; i++) {
233
67.5M
    if (this->bottomUp)
234
22.8M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
235
44.7M
    else
236
44.7M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
237
67.5M
  }
238
239
15.3k
#if BITS_IN_JSAMPLE != 16
240
15.5k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
241
2.15k
    if (this->croppingRegion.y != 0) {
242
2.15k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
243
244
2.15k
      if ((int)lines != this->croppingRegion.y)
245
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
246
2.15k
               "actual (%d) cropping region upper boundary",
247
2.15k
               this->croppingRegion.y, (int)lines);
248
2.15k
    }
249
86.3k
    while ((int)dinfo->output_scanline <
250
86.3k
           this->croppingRegion.y + this->croppingRegion.h)
251
84.2k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
252
84.2k
                                               this->croppingRegion.y],
253
84.2k
                           this->croppingRegion.y + this->croppingRegion.h -
254
84.2k
                           dinfo->output_scanline);
255
2.15k
    if (this->croppingRegion.y + this->croppingRegion.h !=
256
2.15k
        (int)dinfo->output_height) {
257
2.14k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
258
2.14k
                                                     this->croppingRegion.y -
259
2.14k
                                                     this->croppingRegion.h);
260
261
2.14k
      if (lines != dinfo->output_height - this->croppingRegion.y -
262
2.14k
                   this->croppingRegion.h)
263
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
264
2.14k
               "actual (%d) cropping region lower boundary",
265
2.14k
               this->croppingRegion.y + this->croppingRegion.h,
266
2.14k
               (int)(dinfo->output_height - lines));
267
2.14k
    }
268
2.15k
  } else
269
13.2k
#endif
270
13.2k
  {
271
32.1M
    while (dinfo->output_scanline < dinfo->output_height)
272
32.1M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
273
32.1M
                           dinfo->output_height - dinfo->output_scanline);
274
13.2k
  }
275
15.3k
  jpeg_finish_decompress(dinfo);
276
277
39.6k
bailout:
278
39.6k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
279
39.6k
  free(row_pointer);
280
39.6k
  if (this->jerr.warning) retval = -1;
281
39.6k
  return retval;
282
15.3k
}
tj3Decompress12
Line
Count
Source
150
23.7k
{
151
23.7k
  static const char FUNCTION_NAME[] =
152
23.7k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
153
23.7k
  _JSAMPROW *row_pointer = NULL;
154
23.7k
  int croppedHeight, i, retval = 0;
155
23.7k
#if BITS_IN_JSAMPLE != 16
156
23.7k
  int scaledWidth;
157
23.7k
#endif
158
23.7k
  struct my_progress_mgr progress;
159
160
23.7k
  GET_DINSTANCE(handle);
161
23.7k
  if ((this->init & DECOMPRESS) == 0)
162
23.7k
    THROW("Instance has not been initialized for decompression");
163
164
23.7k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
165
23.7k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
166
23.7k
    THROW("Invalid argument");
167
168
23.7k
  if (this->scanLimit) {
169
23.7k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
170
23.7k
    progress.pub.progress_monitor = my_progress_monitor;
171
23.7k
    progress.this = this;
172
23.7k
    dinfo->progress = &progress.pub;
173
23.7k
  } else
174
0
    dinfo->progress = NULL;
175
176
23.7k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
177
178
23.7k
  CATCH_LIBJPEG(this);
179
180
23.7k
  if (dinfo->global_state <= DSTATE_INHEADER) {
181
23.7k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
182
23.7k
    jpeg_read_header(dinfo, TRUE);
183
23.7k
  }
184
11.1k
  setDecompParameters(this);
185
11.1k
  if (this->maxPixels &&
186
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
187
0
      (unsigned long long)this->maxPixels)
188
11.1k
    THROW("Image is too large");
189
11.1k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
190
11.1k
#if BITS_IN_JSAMPLE != 16
191
11.1k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
192
11.1k
#endif
193
11.1k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
194
11.1k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
195
196
11.1k
  dinfo->scale_num = this->scalingFactor.num;
197
11.1k
  dinfo->scale_denom = this->scalingFactor.denom;
198
199
11.1k
  jpeg_start_decompress(dinfo);
200
201
11.1k
#if BITS_IN_JSAMPLE != 16
202
11.1k
  if (this->croppingRegion.x != 0 ||
203
10.0k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
204
1.16k
    JDIMENSION crop_x = this->croppingRegion.x;
205
1.16k
    JDIMENSION crop_w = this->croppingRegion.w;
206
207
1.16k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
208
1.16k
    if ((int)crop_x != this->croppingRegion.x)
209
3
      THROWI("Unexplained mismatch between specified (%d) and\n"
210
1.16k
             "actual (%d) cropping region left boundary",
211
1.16k
             this->croppingRegion.x, (int)crop_x);
212
1.16k
    if ((int)crop_w != this->croppingRegion.w)
213
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
214
1.16k
             "actual (%d) cropping region width",
215
1.16k
             this->croppingRegion.w, (int)crop_w);
216
1.16k
  }
217
11.1k
#endif
218
219
11.1k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
220
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
221
11.1k
    THROW("Invalid argument");
222
223
11.1k
  croppedHeight = dinfo->output_height;
224
11.1k
#if BITS_IN_JSAMPLE != 16
225
11.1k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
226
1.16k
    croppedHeight = this->croppingRegion.h;
227
11.1k
#endif
228
11.1k
  if ((row_pointer =
229
11.1k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
230
11.1k
    THROW("Memory allocation failure");
231
11.1k
  CATCH_LIBJPEG(this);
232
35.3M
  for (i = 0; i < (int)croppedHeight; i++) {
233
35.3M
    if (this->bottomUp)
234
12.1M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
235
23.1M
    else
236
23.1M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
237
35.3M
  }
238
239
9.59k
#if BITS_IN_JSAMPLE != 16
240
10.0k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
241
1.16k
    if (this->croppingRegion.y != 0) {
242
1.16k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
243
244
1.16k
      if ((int)lines != this->croppingRegion.y)
245
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
246
1.16k
               "actual (%d) cropping region upper boundary",
247
1.16k
               this->croppingRegion.y, (int)lines);
248
1.16k
    }
249
42.0k
    while ((int)dinfo->output_scanline <
250
42.0k
           this->croppingRegion.y + this->croppingRegion.h)
251
40.8k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
252
40.8k
                                               this->croppingRegion.y],
253
40.8k
                           this->croppingRegion.y + this->croppingRegion.h -
254
40.8k
                           dinfo->output_scanline);
255
1.16k
    if (this->croppingRegion.y + this->croppingRegion.h !=
256
1.16k
        (int)dinfo->output_height) {
257
1.15k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
258
1.15k
                                                     this->croppingRegion.y -
259
1.15k
                                                     this->croppingRegion.h);
260
261
1.15k
      if (lines != dinfo->output_height - this->croppingRegion.y -
262
1.15k
                   this->croppingRegion.h)
263
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
264
1.15k
               "actual (%d) cropping region lower boundary",
265
1.15k
               this->croppingRegion.y + this->croppingRegion.h,
266
1.15k
               (int)(dinfo->output_height - lines));
267
1.15k
    }
268
1.16k
  } else
269
8.43k
#endif
270
8.43k
  {
271
16.6M
    while (dinfo->output_scanline < dinfo->output_height)
272
16.6M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
273
16.6M
                           dinfo->output_height - dinfo->output_scanline);
274
8.43k
  }
275
9.59k
  jpeg_finish_decompress(dinfo);
276
277
23.7k
bailout:
278
23.7k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
279
23.7k
  free(row_pointer);
280
23.7k
  if (this->jerr.warning) retval = -1;
281
23.7k
  return retval;
282
9.59k
}
tj3Decompress16
Line
Count
Source
150
10.8k
{
151
10.8k
  static const char FUNCTION_NAME[] =
152
10.8k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
153
10.8k
  _JSAMPROW *row_pointer = NULL;
154
10.8k
  int croppedHeight, i, retval = 0;
155
#if BITS_IN_JSAMPLE != 16
156
  int scaledWidth;
157
#endif
158
10.8k
  struct my_progress_mgr progress;
159
160
10.8k
  GET_DINSTANCE(handle);
161
10.8k
  if ((this->init & DECOMPRESS) == 0)
162
10.8k
    THROW("Instance has not been initialized for decompression");
163
164
10.8k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
165
10.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
166
10.8k
    THROW("Invalid argument");
167
168
10.8k
  if (this->scanLimit) {
169
10.8k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
170
10.8k
    progress.pub.progress_monitor = my_progress_monitor;
171
10.8k
    progress.this = this;
172
10.8k
    dinfo->progress = &progress.pub;
173
10.8k
  } else
174
0
    dinfo->progress = NULL;
175
176
10.8k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
177
178
10.8k
  CATCH_LIBJPEG(this);
179
180
10.8k
  if (dinfo->global_state <= DSTATE_INHEADER) {
181
10.8k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
182
10.8k
    jpeg_read_header(dinfo, TRUE);
183
10.8k
  }
184
2.36k
  setDecompParameters(this);
185
2.36k
  if (this->maxPixels &&
186
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
187
0
      (unsigned long long)this->maxPixels)
188
2.36k
    THROW("Image is too large");
189
2.36k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
190
#if BITS_IN_JSAMPLE != 16
191
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
192
#endif
193
2.36k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
194
2.36k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
195
196
2.36k
  dinfo->scale_num = this->scalingFactor.num;
197
2.36k
  dinfo->scale_denom = this->scalingFactor.denom;
198
199
2.36k
  jpeg_start_decompress(dinfo);
200
201
#if BITS_IN_JSAMPLE != 16
202
  if (this->croppingRegion.x != 0 ||
203
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
204
    JDIMENSION crop_x = this->croppingRegion.x;
205
    JDIMENSION crop_w = this->croppingRegion.w;
206
207
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
208
    if ((int)crop_x != this->croppingRegion.x)
209
      THROWI("Unexplained mismatch between specified (%d) and\n"
210
             "actual (%d) cropping region left boundary",
211
             this->croppingRegion.x, (int)crop_x);
212
    if ((int)crop_w != this->croppingRegion.w)
213
      THROWI("Unexplained mismatch between specified (%d) and\n"
214
             "actual (%d) cropping region width",
215
             this->croppingRegion.w, (int)crop_w);
216
  }
217
#endif
218
219
2.36k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
220
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
221
2.36k
    THROW("Invalid argument");
222
223
2.36k
  croppedHeight = dinfo->output_height;
224
#if BITS_IN_JSAMPLE != 16
225
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
226
    croppedHeight = this->croppingRegion.h;
227
#endif
228
2.36k
  if ((row_pointer =
229
2.36k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
230
2.36k
    THROW("Memory allocation failure");
231
2.36k
  CATCH_LIBJPEG(this);
232
23.2M
  for (i = 0; i < (int)croppedHeight; i++) {
233
23.2M
    if (this->bottomUp)
234
5.40M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
235
17.8M
    else
236
17.8M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
237
23.2M
  }
238
239
#if BITS_IN_JSAMPLE != 16
240
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
241
    if (this->croppingRegion.y != 0) {
242
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
243
244
      if ((int)lines != this->croppingRegion.y)
245
        THROWI("Unexplained mismatch between specified (%d) and\n"
246
               "actual (%d) cropping region upper boundary",
247
               this->croppingRegion.y, (int)lines);
248
    }
249
    while ((int)dinfo->output_scanline <
250
           this->croppingRegion.y + this->croppingRegion.h)
251
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
252
                                               this->croppingRegion.y],
253
                           this->croppingRegion.y + this->croppingRegion.h -
254
                           dinfo->output_scanline);
255
    if (this->croppingRegion.y + this->croppingRegion.h !=
256
        (int)dinfo->output_height) {
257
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
258
                                                     this->croppingRegion.y -
259
                                                     this->croppingRegion.h);
260
261
      if (lines != dinfo->output_height - this->croppingRegion.y -
262
                   this->croppingRegion.h)
263
        THROWI("Unexplained mismatch between specified (%d) and\n"
264
               "actual (%d) cropping region lower boundary",
265
               this->croppingRegion.y + this->croppingRegion.h,
266
               (int)(dinfo->output_height - lines));
267
    }
268
  } else
269
#endif
270
1.35k
  {
271
6.32M
    while (dinfo->output_scanline < dinfo->output_height)
272
6.32M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
273
6.32M
                           dinfo->output_height - dinfo->output_scanline);
274
1.35k
  }
275
1.35k
  jpeg_finish_decompress(dinfo);
276
277
10.8k
bailout:
278
10.8k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
279
10.8k
  free(row_pointer);
280
10.8k
  if (this->jerr.warning) retval = -1;
281
10.8k
  return retval;
282
1.35k
}
283
284
285
/*************************** Packed-Pixel Image I/O **************************/
286
287
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
288
289
/* TurboJPEG 3.0+ */
290
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
291
static
292
#endif
293
_JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
294
  (tjhandle handle, FILE *file, int *width, int align, int *height,
295
   int *pixelFormat)
296
115k
{
297
115k
  static const char FUNCTION_NAME[] =
298
115k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
299
300
115k
  int retval = 0, tempc;
301
115k
  size_t pitch;
302
115k
  tjhandle handle2 = NULL;
303
115k
  tjinstance *this2;
304
115k
  j_compress_ptr cinfo = NULL;
305
115k
  cjpeg_source_ptr src;
306
115k
  _JSAMPLE *dstBuf = NULL;
307
115k
  boolean invert;
308
309
115k
  GET_TJINSTANCE(handle, NULL)
310
311
115k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
312
115k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
313
115k
    THROW("Invalid argument");
314
115k
  if ((align & (align - 1)) != 0)
315
115k
    THROW("Alignment must be a power of 2");
316
317
  /* The instance handle passed to this function is used only for parameter
318
     retrieval.  Create a new temporary instance to avoid interfering with the
319
     libjpeg state of the primary instance. */
320
115k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
321
115k
  this2 = (tjinstance *)handle2;
322
115k
  cinfo = &this2->cinfo;
323
324
115k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
325
0
    THROW_UNIX("Could not read input file")
326
115k
  else if (tempc == EOF)
327
115k
    THROW("Input file contains no data");
328
329
115k
  CATCH_LIBJPEG2();
330
331
91.3k
  cinfo->data_precision = BITS_IN_JSAMPLE;
332
91.3k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333
91.3k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
334
91.3k
  if (tempc == 'B') {
335
17.6k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336
17.6k
      THROW("Could not initialize bitmap loader");
337
17.6k
    invert = !this->bottomUp;
338
95.3k
  } else if (tempc == 'P') {
339
#if BITS_IN_JSAMPLE == 8
340
48.0k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
341
#else
342
47.2k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
343
26.4k
        this->precision <= BITS_IN_JSAMPLE)
344
26.4k
#endif
345
74.5k
      cinfo->data_precision = this->precision;
346
95.3k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
347
95.3k
      THROW("Could not initialize PPM loader");
348
95.3k
    invert = this->bottomUp;
349
95.3k
  } else
350
18.4E
    THROW("Unsupported file type");
351
352
112k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
353
354
112k
  src->input_file = file;
355
  /* Refuse to load images larger than the specified size. */
356
112k
  src->max_pixels = this->maxPixels;
357
112k
  (*src->start_input) (cinfo, src);
358
112k
  if (tempc == 'B') {
359
5.44k
    if (cinfo->X_density && cinfo->Y_density) {
360
1.44k
      this->xDensity = cinfo->X_density;
361
1.44k
      this->yDensity = cinfo->Y_density;
362
1.44k
      this->densityUnits = cinfo->density_unit;
363
1.44k
    }
364
5.44k
  }
365
112k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
366
367
112k
  *width = cinfo->image_width;  *height = cinfo->image_height;
368
112k
  *pixelFormat = cs2pf[cinfo->in_color_space];
369
370
112k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
371
372
112k
  if (
373
#if ULLONG_MAX > SIZE_MAX
374
      (unsigned long long)pitch * (unsigned long long)(*height) *
375
      (unsigned long long)sizeof(_JSAMPLE) >
376
      (unsigned long long)((size_t)-1) ||
377
#endif
378
112k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
379
112k
                                   sizeof(_JSAMPLE))) == NULL)
380
112k
    THROW("Memory allocation failure");
381
382
112k
  CATCH_LIBJPEG2();
383
384
167M
  while (cinfo->next_scanline < cinfo->image_height) {
385
167M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
386
387
334M
    for (i = 0; i < nlines; i++) {
388
167M
      _JSAMPLE *dstptr;
389
167M
      int row;
390
391
167M
      row = cinfo->next_scanline + i;
392
167M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
393
134M
      else dstptr = &dstBuf[row * pitch];
394
167M
      memcpy(dstptr, src->_buffer[i],
395
167M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
396
167M
    }
397
167M
    cinfo->next_scanline += nlines;
398
167M
  }
399
400
76.5k
  (*src->finish_input) (cinfo, src);
401
402
115k
bailout:
403
115k
  tj3Destroy(handle2);
404
115k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
405
115k
  return dstBuf;
406
76.5k
}
_tj3LoadImageFromFileHandle8
Line
Count
Source
296
66.8k
{
297
66.8k
  static const char FUNCTION_NAME[] =
298
66.8k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
299
300
66.8k
  int retval = 0, tempc;
301
66.8k
  size_t pitch;
302
66.8k
  tjhandle handle2 = NULL;
303
66.8k
  tjinstance *this2;
304
66.8k
  j_compress_ptr cinfo = NULL;
305
66.8k
  cjpeg_source_ptr src;
306
66.8k
  _JSAMPLE *dstBuf = NULL;
307
66.8k
  boolean invert;
308
309
66.8k
  GET_TJINSTANCE(handle, NULL)
310
311
66.8k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
312
66.8k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
313
66.8k
    THROW("Invalid argument");
314
66.8k
  if ((align & (align - 1)) != 0)
315
66.8k
    THROW("Alignment must be a power of 2");
316
317
  /* The instance handle passed to this function is used only for parameter
318
     retrieval.  Create a new temporary instance to avoid interfering with the
319
     libjpeg state of the primary instance. */
320
66.8k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
321
66.8k
  this2 = (tjinstance *)handle2;
322
66.8k
  cinfo = &this2->cinfo;
323
324
66.8k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
325
0
    THROW_UNIX("Could not read input file")
326
66.8k
  else if (tempc == EOF)
327
66.8k
    THROW("Input file contains no data");
328
329
66.8k
  CATCH_LIBJPEG2();
330
331
49.0k
  cinfo->data_precision = BITS_IN_JSAMPLE;
332
49.0k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333
49.0k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
334
49.0k
  if (tempc == 'B') {
335
17.4k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336
17.4k
      THROW("Could not initialize bitmap loader");
337
17.4k
    invert = !this->bottomUp;
338
48.0k
  } else if (tempc == 'P') {
339
48.0k
#if BITS_IN_JSAMPLE == 8
340
48.0k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
341
#else
342
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
343
        this->precision <= BITS_IN_JSAMPLE)
344
#endif
345
48.0k
      cinfo->data_precision = this->precision;
346
48.0k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
347
48.0k
      THROW("Could not initialize PPM loader");
348
48.0k
    invert = this->bottomUp;
349
48.0k
  } else
350
18.4E
    THROW("Unsupported file type");
351
352
65.5k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
353
354
65.5k
  src->input_file = file;
355
  /* Refuse to load images larger than the specified size. */
356
65.5k
  src->max_pixels = this->maxPixels;
357
65.5k
  (*src->start_input) (cinfo, src);
358
65.5k
  if (tempc == 'B') {
359
5.44k
    if (cinfo->X_density && cinfo->Y_density) {
360
1.44k
      this->xDensity = cinfo->X_density;
361
1.44k
      this->yDensity = cinfo->Y_density;
362
1.44k
      this->densityUnits = cinfo->density_unit;
363
1.44k
    }
364
5.44k
  }
365
65.5k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
366
367
65.5k
  *width = cinfo->image_width;  *height = cinfo->image_height;
368
65.5k
  *pixelFormat = cs2pf[cinfo->in_color_space];
369
370
65.5k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
371
372
65.5k
  if (
373
#if ULLONG_MAX > SIZE_MAX
374
      (unsigned long long)pitch * (unsigned long long)(*height) *
375
      (unsigned long long)sizeof(_JSAMPLE) >
376
      (unsigned long long)((size_t)-1) ||
377
#endif
378
65.5k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
379
65.5k
                                   sizeof(_JSAMPLE))) == NULL)
380
65.5k
    THROW("Memory allocation failure");
381
382
65.5k
  CATCH_LIBJPEG2();
383
384
79.9M
  while (cinfo->next_scanline < cinfo->image_height) {
385
79.8M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
386
387
159M
    for (i = 0; i < nlines; i++) {
388
79.8M
      _JSAMPLE *dstptr;
389
79.8M
      int row;
390
391
79.8M
      row = cinfo->next_scanline + i;
392
79.8M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
393
59.8M
      else dstptr = &dstBuf[row * pitch];
394
79.8M
      memcpy(dstptr, src->_buffer[i],
395
79.8M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
396
79.8M
    }
397
79.8M
    cinfo->next_scanline += nlines;
398
79.8M
  }
399
400
46.9k
  (*src->finish_input) (cinfo, src);
401
402
66.8k
bailout:
403
66.8k
  tj3Destroy(handle2);
404
66.8k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
405
66.8k
  return dstBuf;
406
46.9k
}
_tj3LoadImageFromFileHandle12
Line
Count
Source
296
35.2k
{
297
35.2k
  static const char FUNCTION_NAME[] =
298
35.2k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
299
300
35.2k
  int retval = 0, tempc;
301
35.2k
  size_t pitch;
302
35.2k
  tjhandle handle2 = NULL;
303
35.2k
  tjinstance *this2;
304
35.2k
  j_compress_ptr cinfo = NULL;
305
35.2k
  cjpeg_source_ptr src;
306
35.2k
  _JSAMPLE *dstBuf = NULL;
307
35.2k
  boolean invert;
308
309
35.2k
  GET_TJINSTANCE(handle, NULL)
310
311
35.2k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
312
35.2k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
313
35.2k
    THROW("Invalid argument");
314
35.2k
  if ((align & (align - 1)) != 0)
315
35.2k
    THROW("Alignment must be a power of 2");
316
317
  /* The instance handle passed to this function is used only for parameter
318
     retrieval.  Create a new temporary instance to avoid interfering with the
319
     libjpeg state of the primary instance. */
320
35.2k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
321
35.2k
  this2 = (tjinstance *)handle2;
322
35.2k
  cinfo = &this2->cinfo;
323
324
35.2k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
325
0
    THROW_UNIX("Could not read input file")
326
35.2k
  else if (tempc == EOF)
327
35.2k
    THROW("Input file contains no data");
328
329
35.2k
  CATCH_LIBJPEG2();
330
331
30.6k
  cinfo->data_precision = BITS_IN_JSAMPLE;
332
30.6k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333
30.6k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
334
30.6k
  if (tempc == 'B') {
335
105
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336
105
      THROW("Could not initialize bitmap loader");
337
105
    invert = !this->bottomUp;
338
33.8k
  } else if (tempc == 'P') {
339
#if BITS_IN_JSAMPLE == 8
340
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
341
#else
342
33.8k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
343
13.1k
        this->precision <= BITS_IN_JSAMPLE)
344
13.1k
#endif
345
13.1k
      cinfo->data_precision = this->precision;
346
33.8k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
347
33.8k
      THROW("Could not initialize PPM loader");
348
33.8k
    invert = this->bottomUp;
349
33.8k
  } else
350
18.4E
    THROW("Unsupported file type");
351
352
33.9k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
353
354
33.9k
  src->input_file = file;
355
  /* Refuse to load images larger than the specified size. */
356
33.9k
  src->max_pixels = this->maxPixels;
357
33.9k
  (*src->start_input) (cinfo, src);
358
33.9k
  if (tempc == 'B') {
359
0
    if (cinfo->X_density && cinfo->Y_density) {
360
0
      this->xDensity = cinfo->X_density;
361
0
      this->yDensity = cinfo->Y_density;
362
0
      this->densityUnits = cinfo->density_unit;
363
0
    }
364
0
  }
365
33.9k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
366
367
33.9k
  *width = cinfo->image_width;  *height = cinfo->image_height;
368
33.9k
  *pixelFormat = cs2pf[cinfo->in_color_space];
369
370
33.9k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
371
372
33.9k
  if (
373
#if ULLONG_MAX > SIZE_MAX
374
      (unsigned long long)pitch * (unsigned long long)(*height) *
375
      (unsigned long long)sizeof(_JSAMPLE) >
376
      (unsigned long long)((size_t)-1) ||
377
#endif
378
33.9k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
379
33.9k
                                   sizeof(_JSAMPLE))) == NULL)
380
33.9k
    THROW("Memory allocation failure");
381
382
33.9k
  CATCH_LIBJPEG2();
383
384
66.0M
  while (cinfo->next_scanline < cinfo->image_height) {
385
66.0M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
386
387
131M
    for (i = 0; i < nlines; i++) {
388
65.9M
      _JSAMPLE *dstptr;
389
65.9M
      int row;
390
391
65.9M
      row = cinfo->next_scanline + i;
392
65.9M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
393
56.4M
      else dstptr = &dstBuf[row * pitch];
394
65.9M
      memcpy(dstptr, src->_buffer[i],
395
65.9M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
396
65.9M
    }
397
66.0M
    cinfo->next_scanline += nlines;
398
66.0M
  }
399
400
22.1k
  (*src->finish_input) (cinfo, src);
401
402
35.2k
bailout:
403
35.2k
  tj3Destroy(handle2);
404
35.2k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
405
35.2k
  return dstBuf;
406
22.1k
}
_tj3LoadImageFromFileHandle16
Line
Count
Source
296
13.8k
{
297
13.8k
  static const char FUNCTION_NAME[] =
298
13.8k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
299
300
13.8k
  int retval = 0, tempc;
301
13.8k
  size_t pitch;
302
13.8k
  tjhandle handle2 = NULL;
303
13.8k
  tjinstance *this2;
304
13.8k
  j_compress_ptr cinfo = NULL;
305
13.8k
  cjpeg_source_ptr src;
306
13.8k
  _JSAMPLE *dstBuf = NULL;
307
13.8k
  boolean invert;
308
309
13.8k
  GET_TJINSTANCE(handle, NULL)
310
311
13.8k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
312
13.8k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
313
13.8k
    THROW("Invalid argument");
314
13.8k
  if ((align & (align - 1)) != 0)
315
13.8k
    THROW("Alignment must be a power of 2");
316
317
  /* The instance handle passed to this function is used only for parameter
318
     retrieval.  Create a new temporary instance to avoid interfering with the
319
     libjpeg state of the primary instance. */
320
13.8k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
321
13.8k
  this2 = (tjinstance *)handle2;
322
13.8k
  cinfo = &this2->cinfo;
323
324
13.8k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
325
0
    THROW_UNIX("Could not read input file")
326
13.8k
  else if (tempc == EOF)
327
13.8k
    THROW("Input file contains no data");
328
329
13.8k
  CATCH_LIBJPEG2();
330
331
11.6k
  cinfo->data_precision = BITS_IN_JSAMPLE;
332
11.6k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333
11.6k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
334
11.6k
  if (tempc == 'B') {
335
49
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336
49
      THROW("Could not initialize bitmap loader");
337
49
    invert = !this->bottomUp;
338
13.3k
  } else if (tempc == 'P') {
339
#if BITS_IN_JSAMPLE == 8
340
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
341
#else
342
13.3k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
343
13.3k
        this->precision <= BITS_IN_JSAMPLE)
344
13.3k
#endif
345
13.3k
      cinfo->data_precision = this->precision;
346
13.3k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
347
13.3k
      THROW("Could not initialize PPM loader");
348
13.3k
    invert = this->bottomUp;
349
13.3k
  } else
350
18.4E
    THROW("Unsupported file type");
351
352
13.4k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
353
354
13.4k
  src->input_file = file;
355
  /* Refuse to load images larger than the specified size. */
356
13.4k
  src->max_pixels = this->maxPixels;
357
13.4k
  (*src->start_input) (cinfo, src);
358
13.4k
  if (tempc == 'B') {
359
0
    if (cinfo->X_density && cinfo->Y_density) {
360
0
      this->xDensity = cinfo->X_density;
361
0
      this->yDensity = cinfo->Y_density;
362
0
      this->densityUnits = cinfo->density_unit;
363
0
    }
364
0
  }
365
13.4k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
366
367
13.4k
  *width = cinfo->image_width;  *height = cinfo->image_height;
368
13.4k
  *pixelFormat = cs2pf[cinfo->in_color_space];
369
370
13.4k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
371
372
13.4k
  if (
373
#if ULLONG_MAX > SIZE_MAX
374
      (unsigned long long)pitch * (unsigned long long)(*height) *
375
      (unsigned long long)sizeof(_JSAMPLE) >
376
      (unsigned long long)((size_t)-1) ||
377
#endif
378
13.4k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
379
13.4k
                                   sizeof(_JSAMPLE))) == NULL)
380
13.4k
    THROW("Memory allocation failure");
381
382
13.4k
  CATCH_LIBJPEG2();
383
384
21.6M
  while (cinfo->next_scanline < cinfo->image_height) {
385
21.6M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
386
387
43.1M
    for (i = 0; i < nlines; i++) {
388
21.5M
      _JSAMPLE *dstptr;
389
21.5M
      int row;
390
391
21.5M
      row = cinfo->next_scanline + i;
392
21.5M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
393
18.5M
      else dstptr = &dstBuf[row * pitch];
394
21.5M
      memcpy(dstptr, src->_buffer[i],
395
21.5M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
396
21.5M
    }
397
21.6M
    cinfo->next_scanline += nlines;
398
21.6M
  }
399
400
7.40k
  (*src->finish_input) (cinfo, src);
401
402
13.8k
bailout:
403
13.8k
  tj3Destroy(handle2);
404
13.8k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
405
13.8k
  return dstBuf;
406
7.40k
}
407
408
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
409
410
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
411
  (tjhandle handle, const char *filename, int *width, int align, int *height,
412
   int *pixelFormat)
413
0
{
414
0
  static const char FUNCTION_NAME[] =
415
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
416
417
0
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
418
419
0
  int retval = 0;
420
0
  _JSAMPLE *dstBuf = NULL;
421
0
  FILE *file = NULL;
422
423
0
  GET_TJINSTANCE(handle, NULL)
424
425
0
  if (!filename)
426
0
    THROW("Invalid argument");
427
428
#ifdef _MSC_VER
429
  if (fopen_s(&file, filename, "rb") || file == NULL)
430
#else
431
0
  if ((file = fopen(filename, "rb")) == NULL)
432
0
#endif
433
0
    THROW_UNIX("Cannot open input file");
434
435
0
  dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
436
0
             (handle, file, width, align, height, pixelFormat);
437
438
0
bailout:
439
0
  if (file) fclose(file);
440
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
441
0
  return dstBuf;
442
443
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
444
445
  static const char ERROR_MSG[] =
446
    "16-bit data precision requires lossless JPEG,\n"
447
    "which was disabled at build time.";
448
  _JSAMPLE *retval = NULL;
449
450
  GET_TJINSTANCE(handle, NULL)
451
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
452
           ERROR_MSG);
453
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
454
455
bailout:
456
  return retval;
457
458
#endif
459
0
}
Unexecuted instantiation: tj3LoadImage8
Unexecuted instantiation: tj3LoadImage12
Unexecuted instantiation: tj3LoadImage16
460
461
462
/* TurboJPEG 3.0+ */
463
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
464
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
465
   int pitch, int height, int pixelFormat)
466
0
{
467
0
  static const char FUNCTION_NAME[] =
468
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
469
0
  int retval = 0;
470
471
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
472
473
0
  tjhandle handle2 = NULL;
474
0
  tjinstance *this2;
475
0
  j_decompress_ptr dinfo = NULL;
476
0
  djpeg_dest_ptr dst;
477
0
  FILE *file = NULL;
478
0
  const char *ptr = NULL;
479
0
  boolean invert;
480
481
0
  GET_TJINSTANCE(handle, -1)
482
483
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
484
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
485
0
    THROW("Invalid argument");
486
487
  /* The instance handle passed to this function is used only for parameter
488
     retrieval.  Create a new temporary instance to avoid interfering with the
489
     libjpeg state of the primary instance. */
490
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
491
0
    return -1;
492
0
  this2 = (tjinstance *)handle2;
493
0
  dinfo = &this2->dinfo;
494
495
#ifdef _MSC_VER
496
  if (fopen_s(&file, filename, "wb") || file == NULL)
497
#else
498
0
  if ((file = fopen(filename, "wb")) == NULL)
499
0
#endif
500
0
    THROW_UNIX("Cannot open output file");
501
502
0
  CATCH_LIBJPEG2();
503
504
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
505
0
  dinfo->image_width = width;  dinfo->image_height = height;
506
0
  dinfo->global_state = DSTATE_READY;
507
0
  dinfo->scale_num = dinfo->scale_denom = 1;
508
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
509
510
0
  ptr = strrchr(filename, '.');
511
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
512
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
513
0
      THROW("Could not initialize bitmap writer");
514
0
    invert = !this->bottomUp;
515
0
    dinfo->X_density = (UINT16)this->xDensity;
516
0
    dinfo->Y_density = (UINT16)this->yDensity;
517
0
    dinfo->density_unit = (UINT8)this->densityUnits;
518
0
  } else {
519
#if BITS_IN_JSAMPLE == 8
520
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
521
#else
522
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
523
0
        this->precision <= BITS_IN_JSAMPLE)
524
0
#endif
525
0
      dinfo->data_precision = this->precision;
526
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
527
0
      THROW("Could not initialize PPM writer");
528
0
    invert = this->bottomUp;
529
0
  }
530
531
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
532
533
0
  dst->output_file = file;
534
0
  (*dst->start_output) (dinfo, dst);
535
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
536
537
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
538
0
  else if (pitch < width * tjPixelSize[pixelFormat])
539
0
    THROW("Invalid argument");
540
541
0
  while (dinfo->output_scanline < dinfo->output_height) {
542
0
    _JSAMPLE *rowptr;
543
544
0
    if (invert)
545
0
      rowptr =
546
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
547
0
    else
548
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
549
0
    memcpy(dst->_buffer[0], rowptr,
550
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
551
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
552
0
    dinfo->output_scanline++;
553
0
  }
554
555
0
  (*dst->finish_output) (dinfo, dst);
556
557
0
bailout:
558
0
  tj3Destroy(handle2);
559
0
  if (file) fclose(file);
560
0
  return retval;
561
562
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
563
564
  GET_TJINSTANCE(handle, -1)
565
  THROW("16-bit data precision requires lossless JPEG,\n"
566
        "which was disabled at build time.")
567
bailout:
568
  return retval;
569
570
#endif
571
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
572
573
574
#undef _JSAMPLE
575
#undef _JSAMPROW
576
#undef _buffer
577
#undef _jinit_read_ppm
578
#undef _jinit_write_ppm
579
#undef _jpeg_crop_scanline
580
#undef _jpeg_read_scanlines
581
#undef _jpeg_skip_scanlines
582
#undef _jpeg_write_scanlines