Coverage Report

Created: 2026-01-25 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/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
106M
#define _JSAMPLE  JSAMPLE
34
51.1k
#define _JSAMPROW  JSAMPROW
35
106M
#define _buffer  buffer
36
47.7k
#define _jinit_read_ppm  jinit_read_ppm
37
0
#define _jinit_write_ppm  jinit_write_ppm
38
1.53k
#define _jpeg_crop_scanline  jpeg_crop_scanline
39
30.1M
#define _jpeg_read_scanlines  jpeg_read_scanlines
40
3.02k
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
41
18.0k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
70.4M
#define _JSAMPLE  J12SAMPLE
44
38.8k
#define _JSAMPROW  J12SAMPROW
45
70.4M
#define _buffer  buffer12
46
30.7k
#define _jinit_read_ppm  j12init_read_ppm
47
0
#define _jinit_write_ppm  j12init_write_ppm
48
906
#define _jpeg_crop_scanline  jpeg12_crop_scanline
49
20.7M
#define _jpeg_read_scanlines  jpeg12_read_scanlines
50
1.75k
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
51
17.9k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
14.9M
#define _JSAMPLE  J16SAMPLE
54
11.4k
#define _JSAMPROW  J16SAMPROW
55
14.9M
#define _buffer  buffer16
56
10.6k
#define _jinit_read_ppm  j16init_read_ppm
57
0
#define _jinit_write_ppm  j16init_write_ppm
58
1.47M
#define _jpeg_read_scanlines  jpeg16_read_scanlines
59
4.31k
#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
208k
#define _GET_STRING(name, suffix)  #name #suffix
65
208k
#define GET_STRING(name, suffix)  _GET_STRING(name, suffix)
66
67
68
/******************************** Compressor *********************************/
69
70
/* TurboJPEG 3+ */
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
40.5k
{
75
40.5k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
40.5k
  int i, retval = 0;
77
40.5k
  boolean alloc = TRUE;
78
40.5k
  _JSAMPROW *row_pointer = NULL;
79
80
40.5k
  GET_CINSTANCE(handle)
81
40.5k
  if ((this->init & COMPRESS) == 0)
82
40.5k
    THROW("Instance has not been initialized for compression");
83
84
40.5k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
40.5k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
40.5k
      jpegSize == NULL)
87
40.5k
    THROW("Invalid argument");
88
89
40.5k
  if (!this->lossless && this->quality == -1)
90
40.5k
    THROW("TJPARAM_QUALITY must be specified");
91
40.5k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
40.5k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
40.5k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
40.5k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
40.5k
    THROW("Memory allocation failure");
98
99
40.5k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
302
    retval = -1;  goto bailout;
102
302
  }
103
104
40.2k
  cinfo->image_width = width;
105
40.2k
  cinfo->image_height = height;
106
40.2k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
40.2k
  setCompDefaults(this, pixelFormat, FALSE);
109
40.2k
  if (this->noRealloc) {
110
34.6k
    alloc = FALSE;
111
34.6k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
34.6k
  }
113
40.2k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
40.2k
  jpeg_start_compress(cinfo, TRUE);
116
146M
  for (i = 0; i < height; i++) {
117
146M
    if (this->bottomUp)
118
20.9M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
125M
    else
120
125M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
146M
  }
122
80.5k
  while (cinfo->next_scanline < cinfo->image_height)
123
40.2k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
40.2k
                          cinfo->image_height - cinfo->next_scanline);
125
40.2k
  jpeg_finish_compress(cinfo);
126
127
40.5k
bailout:
128
40.5k
  if (cinfo->global_state > CSTATE_START && alloc)
129
31
    (*cinfo->dest->term_destination) (cinfo);
130
40.5k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
302
    jpeg_abort_compress(cinfo);
132
40.5k
  free(row_pointer);
133
40.5k
  if (this->jerr.warning) retval = -1;
134
40.5k
  return retval;
135
40.2k
}
tj3Compress8
Line
Count
Source
74
18.1k
{
75
18.1k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.1k
  int i, retval = 0;
77
18.1k
  boolean alloc = TRUE;
78
18.1k
  _JSAMPROW *row_pointer = NULL;
79
80
18.1k
  GET_CINSTANCE(handle)
81
18.1k
  if ((this->init & COMPRESS) == 0)
82
18.1k
    THROW("Instance has not been initialized for compression");
83
84
18.1k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.1k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.1k
      jpegSize == NULL)
87
18.1k
    THROW("Invalid argument");
88
89
18.1k
  if (!this->lossless && this->quality == -1)
90
18.1k
    THROW("TJPARAM_QUALITY must be specified");
91
18.1k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.1k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.1k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
18.1k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
18.1k
    THROW("Memory allocation failure");
98
99
18.1k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
135
    retval = -1;  goto bailout;
102
135
  }
103
104
18.0k
  cinfo->image_width = width;
105
18.0k
  cinfo->image_height = height;
106
18.0k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
18.0k
  setCompDefaults(this, pixelFormat, FALSE);
109
18.0k
  if (this->noRealloc) {
110
15.5k
    alloc = FALSE;
111
15.5k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
15.5k
  }
113
18.0k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
18.0k
  jpeg_start_compress(cinfo, TRUE);
116
64.6M
  for (i = 0; i < height; i++) {
117
64.5M
    if (this->bottomUp)
118
9.32M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
55.2M
    else
120
55.2M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
64.5M
  }
122
36.0k
  while (cinfo->next_scanline < cinfo->image_height)
123
18.0k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
18.0k
                          cinfo->image_height - cinfo->next_scanline);
125
18.0k
  jpeg_finish_compress(cinfo);
126
127
18.1k
bailout:
128
18.1k
  if (cinfo->global_state > CSTATE_START && alloc)
129
0
    (*cinfo->dest->term_destination) (cinfo);
130
18.1k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
135
    jpeg_abort_compress(cinfo);
132
18.1k
  free(row_pointer);
133
18.1k
  if (this->jerr.warning) retval = -1;
134
18.1k
  return retval;
135
18.0k
}
tj3Compress12
Line
Count
Source
74
18.0k
{
75
18.0k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.0k
  int i, retval = 0;
77
18.0k
  boolean alloc = TRUE;
78
18.0k
  _JSAMPROW *row_pointer = NULL;
79
80
18.0k
  GET_CINSTANCE(handle)
81
18.0k
  if ((this->init & COMPRESS) == 0)
82
18.0k
    THROW("Instance has not been initialized for compression");
83
84
18.0k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.0k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.0k
      jpegSize == NULL)
87
18.0k
    THROW("Invalid argument");
88
89
18.0k
  if (!this->lossless && this->quality == -1)
90
18.0k
    THROW("TJPARAM_QUALITY must be specified");
91
18.0k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.0k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.0k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
18.0k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
18.0k
    THROW("Memory allocation failure");
98
99
18.0k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
95
    retval = -1;  goto bailout;
102
95
  }
103
104
17.9k
  cinfo->image_width = width;
105
17.9k
  cinfo->image_height = height;
106
17.9k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
17.9k
  setCompDefaults(this, pixelFormat, FALSE);
109
17.9k
  if (this->noRealloc) {
110
15.3k
    alloc = FALSE;
111
15.3k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
15.3k
  }
113
17.9k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
17.9k
  jpeg_start_compress(cinfo, TRUE);
116
68.0M
  for (i = 0; i < height; i++) {
117
68.0M
    if (this->bottomUp)
118
9.72M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
58.3M
    else
120
58.3M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
68.0M
  }
122
35.8k
  while (cinfo->next_scanline < cinfo->image_height)
123
17.9k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
17.9k
                          cinfo->image_height - cinfo->next_scanline);
125
17.9k
  jpeg_finish_compress(cinfo);
126
127
18.0k
bailout:
128
18.0k
  if (cinfo->global_state > CSTATE_START && alloc)
129
0
    (*cinfo->dest->term_destination) (cinfo);
130
18.0k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
95
    jpeg_abort_compress(cinfo);
132
18.0k
  free(row_pointer);
133
18.0k
  if (this->jerr.warning) retval = -1;
134
18.0k
  return retval;
135
17.9k
}
tj3Compress16
Line
Count
Source
74
4.35k
{
75
4.35k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
4.35k
  int i, retval = 0;
77
4.35k
  boolean alloc = TRUE;
78
4.35k
  _JSAMPROW *row_pointer = NULL;
79
80
4.35k
  GET_CINSTANCE(handle)
81
4.35k
  if ((this->init & COMPRESS) == 0)
82
4.35k
    THROW("Instance has not been initialized for compression");
83
84
4.35k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
4.35k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
4.35k
      jpegSize == NULL)
87
4.35k
    THROW("Invalid argument");
88
89
4.35k
  if (!this->lossless && this->quality == -1)
90
4.35k
    THROW("TJPARAM_QUALITY must be specified");
91
4.35k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
4.35k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
4.35k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
4.35k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
4.35k
    THROW("Memory allocation failure");
98
99
4.35k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
72
    retval = -1;  goto bailout;
102
72
  }
103
104
4.28k
  cinfo->image_width = width;
105
4.28k
  cinfo->image_height = height;
106
4.28k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
4.28k
  setCompDefaults(this, pixelFormat, FALSE);
109
4.28k
  if (this->noRealloc) {
110
3.72k
    alloc = FALSE;
111
3.72k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
3.72k
  }
113
4.28k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
4.28k
  jpeg_start_compress(cinfo, TRUE);
116
13.5M
  for (i = 0; i < height; i++) {
117
13.5M
    if (this->bottomUp)
118
1.93M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
11.5M
    else
120
11.5M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
13.5M
  }
122
8.59k
  while (cinfo->next_scanline < cinfo->image_height)
123
4.31k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
4.31k
                          cinfo->image_height - cinfo->next_scanline);
125
4.28k
  jpeg_finish_compress(cinfo);
126
127
4.35k
bailout:
128
4.35k
  if (cinfo->global_state > CSTATE_START && alloc)
129
31
    (*cinfo->dest->term_destination) (cinfo);
130
4.35k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
72
    jpeg_abort_compress(cinfo);
132
4.35k
  free(row_pointer);
133
4.35k
  if (this->jerr.warning) retval = -1;
134
4.35k
  return retval;
135
4.28k
}
136
137
138
/******************************* Decompressor ********************************/
139
140
/* TurboJPEG 3+ */
141
DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE)
142
  (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize,
143
   _JSAMPLE *dstBuf, int pitch, int pixelFormat)
144
60.8k
{
145
60.8k
  static const char FUNCTION_NAME[] =
146
60.8k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
60.8k
  _JSAMPROW *row_pointer = NULL;
148
60.8k
  int croppedHeight, i, retval = 0;
149
#if BITS_IN_JSAMPLE != 16
150
  int scaledWidth;
151
#endif
152
60.8k
  struct my_progress_mgr progress;
153
154
60.8k
  GET_DINSTANCE(handle);
155
60.8k
  if ((this->init & DECOMPRESS) == 0)
156
60.8k
    THROW("Instance has not been initialized for decompression");
157
158
60.8k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
60.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
60.8k
    THROW("Invalid argument");
161
162
60.8k
  if (this->scanLimit) {
163
60.8k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
60.8k
    progress.pub.progress_monitor = my_progress_monitor;
165
60.8k
    progress.this = this;
166
60.8k
    dinfo->progress = &progress.pub;
167
60.8k
  } else
168
0
    dinfo->progress = NULL;
169
170
60.8k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
171
172
60.8k
  if (setjmp(this->jerr.setjmp_buffer)) {
173
    /* If we get here, the JPEG code has signaled an error. */
174
28.2k
    retval = -1;  goto bailout;
175
28.2k
  }
176
177
60.8k
  if (dinfo->global_state <= DSTATE_INHEADER) {
178
60.8k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
179
60.8k
    jpeg_read_header(dinfo, TRUE);
180
60.8k
  }
181
32.6k
  setDecompParameters(this);
182
32.6k
  if (this->maxPixels &&
183
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
184
0
      (unsigned long long)this->maxPixels)
185
32.6k
    THROW("Image is too large");
186
32.6k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
187
#if BITS_IN_JSAMPLE != 16
188
29.7k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
189
#endif
190
32.6k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
191
32.6k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
192
193
32.6k
  dinfo->scale_num = this->scalingFactor.num;
194
32.6k
  dinfo->scale_denom = this->scalingFactor.denom;
195
196
32.6k
  jpeg_start_decompress(dinfo);
197
198
#if BITS_IN_JSAMPLE != 16
199
29.7k
  if (this->croppingRegion.x != 0 ||
200
27.2k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
201
2.43k
    JDIMENSION crop_x = this->croppingRegion.x;
202
2.43k
    JDIMENSION crop_w = this->croppingRegion.w;
203
204
2.43k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
205
2.43k
    if ((int)crop_x != this->croppingRegion.x)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
2.43k
             "actual (%d) cropping region left boundary",
208
2.43k
             this->croppingRegion.x, (int)crop_x);
209
2.43k
    if ((int)crop_w != this->croppingRegion.w)
210
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
211
2.43k
             "actual (%d) cropping region width",
212
2.43k
             this->croppingRegion.w, (int)crop_w);
213
2.43k
  }
214
29.7k
#endif
215
216
32.6k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
217
218
29.7k
  croppedHeight = dinfo->output_height;
219
#if BITS_IN_JSAMPLE != 16
220
29.7k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
221
2.43k
    croppedHeight = this->croppingRegion.h;
222
#endif
223
32.6k
  if ((row_pointer =
224
32.6k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
225
32.6k
    THROW("Memory allocation failure");
226
32.6k
  if (setjmp(this->jerr.setjmp_buffer)) {
227
    /* If we get here, the JPEG code has signaled an error. */
228
6.66k
    retval = -1;  goto bailout;
229
6.66k
  }
230
132M
  for (i = 0; i < (int)croppedHeight; i++) {
231
132M
    if (this->bottomUp)
232
64.1M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
68.6M
    else
234
68.6M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
132M
  }
236
237
#if BITS_IN_JSAMPLE != 16
238
27.2k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
239
2.43k
    if (this->croppingRegion.y != 0) {
240
2.43k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
241
242
2.43k
      if ((int)lines != this->croppingRegion.y)
243
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
244
               "actual (%d) cropping region upper boundary",
245
2.43k
               this->croppingRegion.y, (int)lines);
246
2.43k
    }
247
96.2k
    while ((int)dinfo->output_scanline <
248
96.2k
           this->croppingRegion.y + this->croppingRegion.h)
249
93.8k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
250
93.8k
                                               this->croppingRegion.y],
251
93.8k
                           this->croppingRegion.y + this->croppingRegion.h -
252
93.8k
                           dinfo->output_scanline);
253
2.43k
    if (this->croppingRegion.y + this->croppingRegion.h !=
254
2.43k
        (int)dinfo->output_height) {
255
2.34k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
256
                                                     this->croppingRegion.y -
257
                                                     this->croppingRegion.h);
258
259
2.34k
      if (lines != dinfo->output_height - this->croppingRegion.y -
260
2.34k
                   this->croppingRegion.h)
261
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
262
2.34k
               "actual (%d) cropping region lower boundary",
263
2.34k
               this->croppingRegion.y + this->croppingRegion.h,
264
2.34k
               (int)(dinfo->output_height - lines));
265
2.34k
    }
266
2.43k
  } else
267
22.9k
#endif
268
22.9k
  {
269
52.3M
    while (dinfo->output_scanline < dinfo->output_height)
270
52.2M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
52.2M
                           dinfo->output_height - dinfo->output_scanline);
272
22.9k
  }
273
25.3k
  jpeg_finish_decompress(dinfo);
274
275
60.8k
bailout:
276
60.8k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
277
60.8k
  free(row_pointer);
278
60.8k
  if (this->jerr.warning) retval = -1;
279
60.8k
  return retval;
280
25.3k
}
tj3Decompress8
Line
Count
Source
144
33.0k
{
145
33.0k
  static const char FUNCTION_NAME[] =
146
33.0k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
33.0k
  _JSAMPROW *row_pointer = NULL;
148
33.0k
  int croppedHeight, i, retval = 0;
149
33.0k
#if BITS_IN_JSAMPLE != 16
150
33.0k
  int scaledWidth;
151
33.0k
#endif
152
33.0k
  struct my_progress_mgr progress;
153
154
33.0k
  GET_DINSTANCE(handle);
155
33.0k
  if ((this->init & DECOMPRESS) == 0)
156
33.0k
    THROW("Instance has not been initialized for decompression");
157
158
33.0k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
33.0k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
33.0k
    THROW("Invalid argument");
161
162
33.0k
  if (this->scanLimit) {
163
33.0k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
33.0k
    progress.pub.progress_monitor = my_progress_monitor;
165
33.0k
    progress.this = this;
166
33.0k
    dinfo->progress = &progress.pub;
167
33.0k
  } else
168
0
    dinfo->progress = NULL;
169
170
33.0k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
171
172
33.0k
  if (setjmp(this->jerr.setjmp_buffer)) {
173
    /* If we get here, the JPEG code has signaled an error. */
174
16.1k
    retval = -1;  goto bailout;
175
16.1k
  }
176
177
33.0k
  if (dinfo->global_state <= DSTATE_INHEADER) {
178
33.0k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
179
33.0k
    jpeg_read_header(dinfo, TRUE);
180
33.0k
  }
181
16.8k
  setDecompParameters(this);
182
16.8k
  if (this->maxPixels &&
183
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
184
0
      (unsigned long long)this->maxPixels)
185
16.8k
    THROW("Image is too large");
186
16.8k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
187
16.8k
#if BITS_IN_JSAMPLE != 16
188
16.8k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
189
16.8k
#endif
190
16.8k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
191
16.8k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
192
193
16.8k
  dinfo->scale_num = this->scalingFactor.num;
194
16.8k
  dinfo->scale_denom = this->scalingFactor.denom;
195
196
16.8k
  jpeg_start_decompress(dinfo);
197
198
16.8k
#if BITS_IN_JSAMPLE != 16
199
16.8k
  if (this->croppingRegion.x != 0 ||
200
15.2k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
201
1.53k
    JDIMENSION crop_x = this->croppingRegion.x;
202
1.53k
    JDIMENSION crop_w = this->croppingRegion.w;
203
204
1.53k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
205
1.53k
    if ((int)crop_x != this->croppingRegion.x)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
1.53k
             "actual (%d) cropping region left boundary",
208
1.53k
             this->croppingRegion.x, (int)crop_x);
209
1.53k
    if ((int)crop_w != this->croppingRegion.w)
210
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
211
1.53k
             "actual (%d) cropping region width",
212
1.53k
             this->croppingRegion.w, (int)crop_w);
213
1.53k
  }
214
16.8k
#endif
215
216
16.8k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
217
218
16.8k
  croppedHeight = dinfo->output_height;
219
16.8k
#if BITS_IN_JSAMPLE != 16
220
16.8k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
221
1.53k
    croppedHeight = this->croppingRegion.h;
222
16.8k
#endif
223
16.8k
  if ((row_pointer =
224
16.8k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
225
16.8k
    THROW("Memory allocation failure");
226
16.8k
  if (setjmp(this->jerr.setjmp_buffer)) {
227
    /* If we get here, the JPEG code has signaled an error. */
228
2.65k
    retval = -1;  goto bailout;
229
2.65k
  }
230
63.9M
  for (i = 0; i < (int)croppedHeight; i++) {
231
63.9M
    if (this->bottomUp)
232
32.6M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
31.2M
    else
234
31.2M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
63.9M
  }
236
237
14.1k
#if BITS_IN_JSAMPLE != 16
238
15.2k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
239
1.53k
    if (this->croppingRegion.y != 0) {
240
1.53k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
241
242
1.53k
      if ((int)lines != this->croppingRegion.y)
243
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
244
1.53k
               "actual (%d) cropping region upper boundary",
245
1.53k
               this->croppingRegion.y, (int)lines);
246
1.53k
    }
247
62.9k
    while ((int)dinfo->output_scanline <
248
62.9k
           this->croppingRegion.y + this->croppingRegion.h)
249
61.4k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
250
61.4k
                                               this->croppingRegion.y],
251
61.4k
                           this->croppingRegion.y + this->croppingRegion.h -
252
61.4k
                           dinfo->output_scanline);
253
1.53k
    if (this->croppingRegion.y + this->croppingRegion.h !=
254
1.53k
        (int)dinfo->output_height) {
255
1.49k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
256
1.49k
                                                     this->croppingRegion.y -
257
1.49k
                                                     this->croppingRegion.h);
258
259
1.49k
      if (lines != dinfo->output_height - this->croppingRegion.y -
260
1.49k
                   this->croppingRegion.h)
261
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
262
1.49k
               "actual (%d) cropping region lower boundary",
263
1.49k
               this->croppingRegion.y + this->croppingRegion.h,
264
1.49k
               (int)(dinfo->output_height - lines));
265
1.49k
    }
266
1.53k
  } else
267
12.6k
#endif
268
12.6k
  {
269
30.0M
    while (dinfo->output_scanline < dinfo->output_height)
270
30.0M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
30.0M
                           dinfo->output_height - dinfo->output_scanline);
272
12.6k
  }
273
14.1k
  jpeg_finish_decompress(dinfo);
274
275
33.0k
bailout:
276
33.0k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
277
33.0k
  free(row_pointer);
278
33.0k
  if (this->jerr.warning) retval = -1;
279
33.0k
  return retval;
280
14.1k
}
tj3Decompress12
Line
Count
Source
144
20.7k
{
145
20.7k
  static const char FUNCTION_NAME[] =
146
20.7k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
20.7k
  _JSAMPROW *row_pointer = NULL;
148
20.7k
  int croppedHeight, i, retval = 0;
149
20.7k
#if BITS_IN_JSAMPLE != 16
150
20.7k
  int scaledWidth;
151
20.7k
#endif
152
20.7k
  struct my_progress_mgr progress;
153
154
20.7k
  GET_DINSTANCE(handle);
155
20.7k
  if ((this->init & DECOMPRESS) == 0)
156
20.7k
    THROW("Instance has not been initialized for decompression");
157
158
20.7k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
20.7k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
20.7k
    THROW("Invalid argument");
161
162
20.7k
  if (this->scanLimit) {
163
20.7k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
20.7k
    progress.pub.progress_monitor = my_progress_monitor;
165
20.7k
    progress.this = this;
166
20.7k
    dinfo->progress = &progress.pub;
167
20.7k
  } else
168
0
    dinfo->progress = NULL;
169
170
20.7k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
171
172
20.7k
  if (setjmp(this->jerr.setjmp_buffer)) {
173
    /* If we get here, the JPEG code has signaled an error. */
174
7.89k
    retval = -1;  goto bailout;
175
7.89k
  }
176
177
20.7k
  if (dinfo->global_state <= DSTATE_INHEADER) {
178
20.7k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
179
20.7k
    jpeg_read_header(dinfo, TRUE);
180
20.7k
  }
181
12.8k
  setDecompParameters(this);
182
12.8k
  if (this->maxPixels &&
183
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
184
0
      (unsigned long long)this->maxPixels)
185
12.8k
    THROW("Image is too large");
186
12.8k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
187
12.8k
#if BITS_IN_JSAMPLE != 16
188
12.8k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
189
12.8k
#endif
190
12.8k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
191
12.8k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
192
193
12.8k
  dinfo->scale_num = this->scalingFactor.num;
194
12.8k
  dinfo->scale_denom = this->scalingFactor.denom;
195
196
12.8k
  jpeg_start_decompress(dinfo);
197
198
12.8k
#if BITS_IN_JSAMPLE != 16
199
12.8k
  if (this->croppingRegion.x != 0 ||
200
11.9k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
201
906
    JDIMENSION crop_x = this->croppingRegion.x;
202
906
    JDIMENSION crop_w = this->croppingRegion.w;
203
204
906
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
205
906
    if ((int)crop_x != this->croppingRegion.x)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
906
             "actual (%d) cropping region left boundary",
208
906
             this->croppingRegion.x, (int)crop_x);
209
906
    if ((int)crop_w != this->croppingRegion.w)
210
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
211
906
             "actual (%d) cropping region width",
212
906
             this->croppingRegion.w, (int)crop_w);
213
906
  }
214
12.8k
#endif
215
216
12.8k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
217
218
12.8k
  croppedHeight = dinfo->output_height;
219
12.8k
#if BITS_IN_JSAMPLE != 16
220
12.8k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
221
906
    croppedHeight = this->croppingRegion.h;
222
12.8k
#endif
223
12.8k
  if ((row_pointer =
224
12.8k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
225
12.8k
    THROW("Memory allocation failure");
226
12.8k
  if (setjmp(this->jerr.setjmp_buffer)) {
227
    /* If we get here, the JPEG code has signaled an error. */
228
1.67k
    retval = -1;  goto bailout;
229
1.67k
  }
230
47.7M
  for (i = 0; i < (int)croppedHeight; i++) {
231
47.6M
    if (this->bottomUp)
232
22.9M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
24.7M
    else
234
24.7M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
47.6M
  }
236
237
11.2k
#if BITS_IN_JSAMPLE != 16
238
11.9k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
239
906
    if (this->croppingRegion.y != 0) {
240
906
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
241
242
906
      if ((int)lines != this->croppingRegion.y)
243
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
244
906
               "actual (%d) cropping region upper boundary",
245
906
               this->croppingRegion.y, (int)lines);
246
906
    }
247
33.2k
    while ((int)dinfo->output_scanline <
248
33.2k
           this->croppingRegion.y + this->croppingRegion.h)
249
32.3k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
250
32.3k
                                               this->croppingRegion.y],
251
32.3k
                           this->croppingRegion.y + this->croppingRegion.h -
252
32.3k
                           dinfo->output_scanline);
253
906
    if (this->croppingRegion.y + this->croppingRegion.h !=
254
906
        (int)dinfo->output_height) {
255
850
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
256
850
                                                     this->croppingRegion.y -
257
850
                                                     this->croppingRegion.h);
258
259
850
      if (lines != dinfo->output_height - this->croppingRegion.y -
260
850
                   this->croppingRegion.h)
261
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
262
850
               "actual (%d) cropping region lower boundary",
263
850
               this->croppingRegion.y + this->croppingRegion.h,
264
850
               (int)(dinfo->output_height - lines));
265
850
    }
266
906
  } else
267
10.3k
#endif
268
10.3k
  {
269
20.7M
    while (dinfo->output_scanline < dinfo->output_height)
270
20.7M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
20.7M
                           dinfo->output_height - dinfo->output_scanline);
272
10.3k
  }
273
11.2k
  jpeg_finish_decompress(dinfo);
274
275
20.7k
bailout:
276
20.7k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
277
20.7k
  free(row_pointer);
278
20.7k
  if (this->jerr.warning) retval = -1;
279
20.7k
  return retval;
280
11.2k
}
tj3Decompress16
Line
Count
Source
144
7.06k
{
145
7.06k
  static const char FUNCTION_NAME[] =
146
7.06k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
7.06k
  _JSAMPROW *row_pointer = NULL;
148
7.06k
  int croppedHeight, i, retval = 0;
149
#if BITS_IN_JSAMPLE != 16
150
  int scaledWidth;
151
#endif
152
7.06k
  struct my_progress_mgr progress;
153
154
7.06k
  GET_DINSTANCE(handle);
155
7.06k
  if ((this->init & DECOMPRESS) == 0)
156
7.06k
    THROW("Instance has not been initialized for decompression");
157
158
7.06k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
7.06k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
7.06k
    THROW("Invalid argument");
161
162
7.06k
  if (this->scanLimit) {
163
7.06k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
7.06k
    progress.pub.progress_monitor = my_progress_monitor;
165
7.06k
    progress.this = this;
166
7.06k
    dinfo->progress = &progress.pub;
167
7.06k
  } else
168
0
    dinfo->progress = NULL;
169
170
7.06k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
171
172
7.06k
  if (setjmp(this->jerr.setjmp_buffer)) {
173
    /* If we get here, the JPEG code has signaled an error. */
174
4.16k
    retval = -1;  goto bailout;
175
4.16k
  }
176
177
7.06k
  if (dinfo->global_state <= DSTATE_INHEADER) {
178
7.06k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
179
7.06k
    jpeg_read_header(dinfo, TRUE);
180
7.06k
  }
181
2.90k
  setDecompParameters(this);
182
2.90k
  if (this->maxPixels &&
183
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
184
0
      (unsigned long long)this->maxPixels)
185
2.90k
    THROW("Image is too large");
186
2.90k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
187
#if BITS_IN_JSAMPLE != 16
188
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
189
#endif
190
2.90k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
191
2.90k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
192
193
2.90k
  dinfo->scale_num = this->scalingFactor.num;
194
2.90k
  dinfo->scale_denom = this->scalingFactor.denom;
195
196
2.90k
  jpeg_start_decompress(dinfo);
197
198
#if BITS_IN_JSAMPLE != 16
199
  if (this->croppingRegion.x != 0 ||
200
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
201
    JDIMENSION crop_x = this->croppingRegion.x;
202
    JDIMENSION crop_w = this->croppingRegion.w;
203
204
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
205
    if ((int)crop_x != this->croppingRegion.x)
206
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
             "actual (%d) cropping region left boundary",
208
             this->croppingRegion.x, (int)crop_x);
209
    if ((int)crop_w != this->croppingRegion.w)
210
      THROWI("Unexplained mismatch between specified (%d) and\n"
211
             "actual (%d) cropping region width",
212
             this->croppingRegion.w, (int)crop_w);
213
  }
214
#endif
215
216
2.90k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
217
218
2.90k
  croppedHeight = dinfo->output_height;
219
#if BITS_IN_JSAMPLE != 16
220
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
221
    croppedHeight = this->croppingRegion.h;
222
#endif
223
2.90k
  if ((row_pointer =
224
2.90k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
225
2.90k
    THROW("Memory allocation failure");
226
2.90k
  if (setjmp(this->jerr.setjmp_buffer)) {
227
    /* If we get here, the JPEG code has signaled an error. */
228
2.33k
    retval = -1;  goto bailout;
229
2.33k
  }
230
21.2M
  for (i = 0; i < (int)croppedHeight; i++) {
231
21.2M
    if (this->bottomUp)
232
8.53M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
12.6M
    else
234
12.6M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
21.2M
  }
236
237
#if BITS_IN_JSAMPLE != 16
238
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
239
    if (this->croppingRegion.y != 0) {
240
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
241
242
      if ((int)lines != this->croppingRegion.y)
243
        THROWI("Unexplained mismatch between specified (%d) and\n"
244
               "actual (%d) cropping region upper boundary",
245
               this->croppingRegion.y, (int)lines);
246
    }
247
    while ((int)dinfo->output_scanline <
248
           this->croppingRegion.y + this->croppingRegion.h)
249
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
250
                                               this->croppingRegion.y],
251
                           this->croppingRegion.y + this->croppingRegion.h -
252
                           dinfo->output_scanline);
253
    if (this->croppingRegion.y + this->croppingRegion.h !=
254
        (int)dinfo->output_height) {
255
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
256
                                                     this->croppingRegion.y -
257
                                                     this->croppingRegion.h);
258
259
      if (lines != dinfo->output_height - this->croppingRegion.y -
260
                   this->croppingRegion.h)
261
        THROWI("Unexplained mismatch between specified (%d) and\n"
262
               "actual (%d) cropping region lower boundary",
263
               this->croppingRegion.y + this->croppingRegion.h,
264
               (int)(dinfo->output_height - lines));
265
    }
266
  } else
267
#endif
268
573
  {
269
1.47M
    while (dinfo->output_scanline < dinfo->output_height)
270
1.47M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
1.47M
                           dinfo->output_height - dinfo->output_scanline);
272
573
  }
273
573
  jpeg_finish_decompress(dinfo);
274
275
7.06k
bailout:
276
7.06k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
277
7.06k
  free(row_pointer);
278
7.06k
  if (this->jerr.warning) retval = -1;
279
7.06k
  return retval;
280
573
}
281
282
283
/*************************** Packed-Pixel Image I/O **************************/
284
285
/* TurboJPEG 3+ */
286
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
287
static
288
#endif
289
_JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
290
  (tjhandle handle, FILE *file, int *width, int align, int *height,
291
   int *pixelFormat)
292
107k
{
293
107k
  static const char FUNCTION_NAME[] =
294
107k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
295
296
107k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
297
298
107k
  int retval = 0, tempc;
299
107k
  size_t pitch;
300
107k
  tjhandle handle2 = NULL;
301
107k
  tjinstance *this2;
302
107k
  j_compress_ptr cinfo = NULL;
303
107k
  cjpeg_source_ptr src;
304
107k
  _JSAMPLE *dstBuf = NULL;
305
107k
  boolean invert;
306
307
107k
  GET_TJINSTANCE(handle, NULL)
308
309
107k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
310
107k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
311
107k
    THROW("Invalid argument");
312
107k
  if ((align & (align - 1)) != 0)
313
107k
    THROW("Alignment must be a power of 2");
314
315
  /* The instance handle passed to this function is used only for parameter
316
     retrieval.  Create a new temporary instance to avoid interfering with the
317
     libjpeg state of the primary instance. */
318
107k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
319
107k
  this2 = (tjinstance *)handle2;
320
107k
  cinfo = &this2->cinfo;
321
322
107k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
323
0
    THROW_UNIX("Could not read input file")
324
107k
  else if (tempc == EOF)
325
107k
    THROW("Input file contains no data");
326
327
107k
  if (setjmp(this2->jerr.setjmp_buffer)) {
328
    /* If we get here, the JPEG code has signaled an error. */
329
23.8k
    retval = -1;  goto bailout;
330
23.8k
  }
331
332
83.5k
  cinfo->data_precision = BITS_IN_JSAMPLE;
333
83.5k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
334
83.5k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
335
83.5k
  if (tempc == 'B') {
336
17.7k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
337
17.7k
      THROW("Could not initialize bitmap loader");
338
17.7k
    invert = !this->bottomUp;
339
89.1k
  } else if (tempc == 'P') {
340
89.1k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
341
89.1k
      THROW("Could not initialize PPM loader");
342
89.1k
    invert = this->bottomUp;
343
89.1k
  } else
344
18.4E
    THROW("Unsupported file type");
345
346
106k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
347
348
106k
  src->input_file = file;
349
  /* Refuse to load images larger than the specified size. */
350
106k
  src->max_pixels = this->maxPixels;
351
106k
  (*src->start_input) (cinfo, src);
352
106k
  if (tempc == 'B') {
353
5.65k
    if (cinfo->X_density && cinfo->Y_density) {
354
1.58k
      this->xDensity = cinfo->X_density;
355
1.58k
      this->yDensity = cinfo->Y_density;
356
1.58k
      this->densityUnits = cinfo->density_unit;
357
1.58k
    }
358
5.65k
  }
359
106k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
360
361
106k
  *width = cinfo->image_width;  *height = cinfo->image_height;
362
106k
  *pixelFormat = cs2pf[cinfo->in_color_space];
363
364
106k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
365
106k
  if (
366
#if ULLONG_MAX > SIZE_MAX
367
      (unsigned long long)pitch * (unsigned long long)(*height) >
368
      (unsigned long long)((size_t)-1) ||
369
#endif
370
106k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
371
106k
                                   sizeof(_JSAMPLE))) == NULL)
372
106k
    THROW("Memory allocation failure");
373
374
106k
  if (setjmp(this2->jerr.setjmp_buffer)) {
375
    /* If we get here, the JPEG code has signaled an error. */
376
31.1k
    retval = -1;  goto bailout;
377
31.1k
  }
378
379
191M
  while (cinfo->next_scanline < cinfo->image_height) {
380
191M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
381
382
383M
    for (i = 0; i < nlines; i++) {
383
191M
      _JSAMPLE *dstptr;
384
191M
      int row;
385
386
191M
      row = cinfo->next_scanline + i;
387
191M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
388
158M
      else dstptr = &dstBuf[row * pitch];
389
191M
      memcpy(dstptr, src->_buffer[i],
390
191M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
391
191M
    }
392
191M
    cinfo->next_scanline += nlines;
393
191M
  }
394
395
75.7k
  (*src->finish_input) (cinfo, src);
396
397
107k
bailout:
398
107k
  tj3Destroy(handle2);
399
107k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
400
107k
  return dstBuf;
401
402
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
403
404
  static const char ERROR_MSG[] =
405
    "16-bit data precision requires lossless JPEG,\n"
406
    "which was disabled at build time.";
407
  _JSAMPLE *retval = NULL;
408
409
  GET_TJINSTANCE(handle, NULL)
410
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
411
           ERROR_MSG);
412
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
413
414
bailout:
415
  return retval;
416
417
#endif
418
75.7k
}
_tj3LoadImageFromFileHandle8
Line
Count
Source
292
65.7k
{
293
65.7k
  static const char FUNCTION_NAME[] =
294
65.7k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
295
296
65.7k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
297
298
65.7k
  int retval = 0, tempc;
299
65.7k
  size_t pitch;
300
65.7k
  tjhandle handle2 = NULL;
301
65.7k
  tjinstance *this2;
302
65.7k
  j_compress_ptr cinfo = NULL;
303
65.7k
  cjpeg_source_ptr src;
304
65.7k
  _JSAMPLE *dstBuf = NULL;
305
65.7k
  boolean invert;
306
307
65.7k
  GET_TJINSTANCE(handle, NULL)
308
309
65.7k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
310
65.7k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
311
65.7k
    THROW("Invalid argument");
312
65.7k
  if ((align & (align - 1)) != 0)
313
65.7k
    THROW("Alignment must be a power of 2");
314
315
  /* The instance handle passed to this function is used only for parameter
316
     retrieval.  Create a new temporary instance to avoid interfering with the
317
     libjpeg state of the primary instance. */
318
65.7k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
319
65.7k
  this2 = (tjinstance *)handle2;
320
65.7k
  cinfo = &this2->cinfo;
321
322
65.7k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
323
0
    THROW_UNIX("Could not read input file")
324
65.7k
  else if (tempc == EOF)
325
65.7k
    THROW("Input file contains no data");
326
327
65.7k
  if (setjmp(this2->jerr.setjmp_buffer)) {
328
    /* If we get here, the JPEG code has signaled an error. */
329
17.9k
    retval = -1;  goto bailout;
330
17.9k
  }
331
332
47.7k
  cinfo->data_precision = BITS_IN_JSAMPLE;
333
47.7k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
334
47.7k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
335
47.7k
  if (tempc == 'B') {
336
17.7k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
337
17.7k
      THROW("Could not initialize bitmap loader");
338
17.7k
    invert = !this->bottomUp;
339
47.7k
  } else if (tempc == 'P') {
340
47.7k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
341
47.7k
      THROW("Could not initialize PPM loader");
342
47.7k
    invert = this->bottomUp;
343
47.7k
  } else
344
18.4E
    THROW("Unsupported file type");
345
346
65.4k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
347
348
65.4k
  src->input_file = file;
349
  /* Refuse to load images larger than the specified size. */
350
65.4k
  src->max_pixels = this->maxPixels;
351
65.4k
  (*src->start_input) (cinfo, src);
352
65.4k
  if (tempc == 'B') {
353
5.65k
    if (cinfo->X_density && cinfo->Y_density) {
354
1.58k
      this->xDensity = cinfo->X_density;
355
1.58k
      this->yDensity = cinfo->Y_density;
356
1.58k
      this->densityUnits = cinfo->density_unit;
357
1.58k
    }
358
5.65k
  }
359
65.4k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
360
361
65.4k
  *width = cinfo->image_width;  *height = cinfo->image_height;
362
65.4k
  *pixelFormat = cs2pf[cinfo->in_color_space];
363
364
65.4k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
365
65.4k
  if (
366
#if ULLONG_MAX > SIZE_MAX
367
      (unsigned long long)pitch * (unsigned long long)(*height) >
368
      (unsigned long long)((size_t)-1) ||
369
#endif
370
65.4k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
371
65.4k
                                   sizeof(_JSAMPLE))) == NULL)
372
65.4k
    THROW("Memory allocation failure");
373
374
65.4k
  if (setjmp(this2->jerr.setjmp_buffer)) {
375
    /* If we get here, the JPEG code has signaled an error. */
376
17.9k
    retval = -1;  goto bailout;
377
17.9k
  }
378
379
106M
  while (cinfo->next_scanline < cinfo->image_height) {
380
106M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
381
382
212M
    for (i = 0; i < nlines; i++) {
383
106M
      _JSAMPLE *dstptr;
384
106M
      int row;
385
386
106M
      row = cinfo->next_scanline + i;
387
106M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
388
85.5M
      else dstptr = &dstBuf[row * pitch];
389
106M
      memcpy(dstptr, src->_buffer[i],
390
106M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
391
106M
    }
392
106M
    cinfo->next_scanline += nlines;
393
106M
  }
394
395
47.4k
  (*src->finish_input) (cinfo, src);
396
397
65.7k
bailout:
398
65.7k
  tj3Destroy(handle2);
399
65.7k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
400
65.7k
  return dstBuf;
401
402
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
403
404
  static const char ERROR_MSG[] =
405
    "16-bit data precision requires lossless JPEG,\n"
406
    "which was disabled at build time.";
407
  _JSAMPLE *retval = NULL;
408
409
  GET_TJINSTANCE(handle, NULL)
410
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
411
           ERROR_MSG);
412
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
413
414
bailout:
415
  return retval;
416
417
#endif
418
47.4k
}
_tj3LoadImageFromFileHandle12
Line
Count
Source
292
30.9k
{
293
30.9k
  static const char FUNCTION_NAME[] =
294
30.9k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
295
296
30.9k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
297
298
30.9k
  int retval = 0, tempc;
299
30.9k
  size_t pitch;
300
30.9k
  tjhandle handle2 = NULL;
301
30.9k
  tjinstance *this2;
302
30.9k
  j_compress_ptr cinfo = NULL;
303
30.9k
  cjpeg_source_ptr src;
304
30.9k
  _JSAMPLE *dstBuf = NULL;
305
30.9k
  boolean invert;
306
307
30.9k
  GET_TJINSTANCE(handle, NULL)
308
309
30.9k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
310
30.9k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
311
30.9k
    THROW("Invalid argument");
312
30.9k
  if ((align & (align - 1)) != 0)
313
30.9k
    THROW("Alignment must be a power of 2");
314
315
  /* The instance handle passed to this function is used only for parameter
316
     retrieval.  Create a new temporary instance to avoid interfering with the
317
     libjpeg state of the primary instance. */
318
30.9k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
319
30.9k
  this2 = (tjinstance *)handle2;
320
30.9k
  cinfo = &this2->cinfo;
321
322
30.9k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
323
0
    THROW_UNIX("Could not read input file")
324
30.9k
  else if (tempc == EOF)
325
30.9k
    THROW("Input file contains no data");
326
327
30.9k
  if (setjmp(this2->jerr.setjmp_buffer)) {
328
    /* If we get here, the JPEG code has signaled an error. */
329
3.98k
    retval = -1;  goto bailout;
330
3.98k
  }
331
332
26.9k
  cinfo->data_precision = BITS_IN_JSAMPLE;
333
26.9k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
334
26.9k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
335
26.9k
  if (tempc == 'B') {
336
14
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
337
14
      THROW("Could not initialize bitmap loader");
338
14
    invert = !this->bottomUp;
339
30.7k
  } else if (tempc == 'P') {
340
30.7k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
341
30.7k
      THROW("Could not initialize PPM loader");
342
30.7k
    invert = this->bottomUp;
343
30.7k
  } else
344
18.4E
    THROW("Unsupported file type");
345
346
30.7k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
347
348
30.7k
  src->input_file = file;
349
  /* Refuse to load images larger than the specified size. */
350
30.7k
  src->max_pixels = this->maxPixels;
351
30.7k
  (*src->start_input) (cinfo, src);
352
30.7k
  if (tempc == 'B') {
353
0
    if (cinfo->X_density && cinfo->Y_density) {
354
0
      this->xDensity = cinfo->X_density;
355
0
      this->yDensity = cinfo->Y_density;
356
0
      this->densityUnits = cinfo->density_unit;
357
0
    }
358
0
  }
359
30.7k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
360
361
30.7k
  *width = cinfo->image_width;  *height = cinfo->image_height;
362
30.7k
  *pixelFormat = cs2pf[cinfo->in_color_space];
363
364
30.7k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
365
30.7k
  if (
366
#if ULLONG_MAX > SIZE_MAX
367
      (unsigned long long)pitch * (unsigned long long)(*height) >
368
      (unsigned long long)((size_t)-1) ||
369
#endif
370
30.7k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
371
30.7k
                                   sizeof(_JSAMPLE))) == NULL)
372
30.7k
    THROW("Memory allocation failure");
373
374
30.7k
  if (setjmp(this2->jerr.setjmp_buffer)) {
375
    /* If we get here, the JPEG code has signaled an error. */
376
8.77k
    retval = -1;  goto bailout;
377
8.77k
  }
378
379
70.4M
  while (cinfo->next_scanline < cinfo->image_height) {
380
70.4M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
381
382
140M
    for (i = 0; i < nlines; i++) {
383
70.4M
      _JSAMPLE *dstptr;
384
70.4M
      int row;
385
386
70.4M
      row = cinfo->next_scanline + i;
387
70.4M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
388
60.3M
      else dstptr = &dstBuf[row * pitch];
389
70.4M
      memcpy(dstptr, src->_buffer[i],
390
70.4M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
391
70.4M
    }
392
70.4M
    cinfo->next_scanline += nlines;
393
70.4M
  }
394
395
22.0k
  (*src->finish_input) (cinfo, src);
396
397
30.9k
bailout:
398
30.9k
  tj3Destroy(handle2);
399
30.9k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
400
30.9k
  return dstBuf;
401
402
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
403
404
  static const char ERROR_MSG[] =
405
    "16-bit data precision requires lossless JPEG,\n"
406
    "which was disabled at build time.";
407
  _JSAMPLE *retval = NULL;
408
409
  GET_TJINSTANCE(handle, NULL)
410
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
411
           ERROR_MSG);
412
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
413
414
bailout:
415
  return retval;
416
417
#endif
418
22.0k
}
_tj3LoadImageFromFileHandle16
Line
Count
Source
292
10.7k
{
293
10.7k
  static const char FUNCTION_NAME[] =
294
10.7k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
295
296
10.7k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
297
298
10.7k
  int retval = 0, tempc;
299
10.7k
  size_t pitch;
300
10.7k
  tjhandle handle2 = NULL;
301
10.7k
  tjinstance *this2;
302
10.7k
  j_compress_ptr cinfo = NULL;
303
10.7k
  cjpeg_source_ptr src;
304
10.7k
  _JSAMPLE *dstBuf = NULL;
305
10.7k
  boolean invert;
306
307
10.7k
  GET_TJINSTANCE(handle, NULL)
308
309
10.7k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
310
10.7k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
311
10.7k
    THROW("Invalid argument");
312
10.7k
  if ((align & (align - 1)) != 0)
313
10.7k
    THROW("Alignment must be a power of 2");
314
315
  /* The instance handle passed to this function is used only for parameter
316
     retrieval.  Create a new temporary instance to avoid interfering with the
317
     libjpeg state of the primary instance. */
318
10.7k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
319
10.7k
  this2 = (tjinstance *)handle2;
320
10.7k
  cinfo = &this2->cinfo;
321
322
10.7k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
323
0
    THROW_UNIX("Could not read input file")
324
10.7k
  else if (tempc == EOF)
325
10.7k
    THROW("Input file contains no data");
326
327
10.7k
  if (setjmp(this2->jerr.setjmp_buffer)) {
328
    /* If we get here, the JPEG code has signaled an error. */
329
1.88k
    retval = -1;  goto bailout;
330
1.88k
  }
331
332
8.85k
  cinfo->data_precision = BITS_IN_JSAMPLE;
333
8.85k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
334
8.85k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
335
8.85k
  if (tempc == 'B') {
336
7
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
337
7
      THROW("Could not initialize bitmap loader");
338
7
    invert = !this->bottomUp;
339
10.6k
  } else if (tempc == 'P') {
340
10.6k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
341
10.6k
      THROW("Could not initialize PPM loader");
342
10.6k
    invert = this->bottomUp;
343
10.6k
  } else
344
18.4E
    THROW("Unsupported file type");
345
346
10.6k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
347
348
10.6k
  src->input_file = file;
349
  /* Refuse to load images larger than the specified size. */
350
10.6k
  src->max_pixels = this->maxPixels;
351
10.6k
  (*src->start_input) (cinfo, src);
352
10.6k
  if (tempc == 'B') {
353
0
    if (cinfo->X_density && cinfo->Y_density) {
354
0
      this->xDensity = cinfo->X_density;
355
0
      this->yDensity = cinfo->Y_density;
356
0
      this->densityUnits = cinfo->density_unit;
357
0
    }
358
0
  }
359
10.6k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
360
361
10.6k
  *width = cinfo->image_width;  *height = cinfo->image_height;
362
10.6k
  *pixelFormat = cs2pf[cinfo->in_color_space];
363
364
10.6k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
365
10.6k
  if (
366
#if ULLONG_MAX > SIZE_MAX
367
      (unsigned long long)pitch * (unsigned long long)(*height) >
368
      (unsigned long long)((size_t)-1) ||
369
#endif
370
10.6k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
371
10.6k
                                   sizeof(_JSAMPLE))) == NULL)
372
10.6k
    THROW("Memory allocation failure");
373
374
10.6k
  if (setjmp(this2->jerr.setjmp_buffer)) {
375
    /* If we get here, the JPEG code has signaled an error. */
376
4.40k
    retval = -1;  goto bailout;
377
4.40k
  }
378
379
14.9M
  while (cinfo->next_scanline < cinfo->image_height) {
380
14.9M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
381
382
29.9M
    for (i = 0; i < nlines; i++) {
383
14.9M
      _JSAMPLE *dstptr;
384
14.9M
      int row;
385
386
14.9M
      row = cinfo->next_scanline + i;
387
14.9M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
388
12.8M
      else dstptr = &dstBuf[row * pitch];
389
14.9M
      memcpy(dstptr, src->_buffer[i],
390
14.9M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
391
14.9M
    }
392
14.9M
    cinfo->next_scanline += nlines;
393
14.9M
  }
394
395
6.24k
  (*src->finish_input) (cinfo, src);
396
397
10.7k
bailout:
398
10.7k
  tj3Destroy(handle2);
399
10.7k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
400
10.7k
  return dstBuf;
401
402
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
403
404
  static const char ERROR_MSG[] =
405
    "16-bit data precision requires lossless JPEG,\n"
406
    "which was disabled at build time.";
407
  _JSAMPLE *retval = NULL;
408
409
  GET_TJINSTANCE(handle, NULL)
410
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
411
           ERROR_MSG);
412
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
413
414
bailout:
415
  return retval;
416
417
#endif
418
6.24k
}
419
420
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
421
  (tjhandle handle, const char *filename, int *width, int align, int *height,
422
   int *pixelFormat)
423
0
{
424
0
  static const char FUNCTION_NAME[] =
425
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
426
427
0
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
428
429
0
  int retval = 0;
430
0
  _JSAMPLE *dstBuf = NULL;
431
0
  FILE *file = NULL;
432
433
0
  GET_TJINSTANCE(handle, NULL)
434
435
0
  if (!filename)
436
0
    THROW("Invalid argument");
437
438
#ifdef _MSC_VER
439
  if (fopen_s(&file, filename, "rb") || file == NULL)
440
#else
441
0
  if ((file = fopen(filename, "rb")) == NULL)
442
0
#endif
443
0
    THROW_UNIX("Cannot open input file");
444
445
0
  dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
446
0
             (handle, file, width, align, height, pixelFormat);
447
448
0
bailout:
449
0
  if (file) fclose(file);
450
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
451
0
  return dstBuf;
452
453
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
454
455
  static const char ERROR_MSG[] =
456
    "16-bit data precision requires lossless JPEG,\n"
457
    "which was disabled at build time.";
458
  _JSAMPLE *retval = NULL;
459
460
  GET_TJINSTANCE(handle, NULL)
461
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
462
           ERROR_MSG);
463
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
464
465
bailout:
466
  return retval;
467
468
#endif
469
0
}
Unexecuted instantiation: tj3LoadImage8
Unexecuted instantiation: tj3LoadImage12
Unexecuted instantiation: tj3LoadImage16
470
471
472
/* TurboJPEG 3+ */
473
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
474
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
475
   int pitch, int height, int pixelFormat)
476
0
{
477
0
  static const char FUNCTION_NAME[] =
478
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
479
0
  int retval = 0;
480
481
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
482
483
0
  tjhandle handle2 = NULL;
484
0
  tjinstance *this2;
485
0
  j_decompress_ptr dinfo = NULL;
486
0
  djpeg_dest_ptr dst;
487
0
  FILE *file = NULL;
488
0
  char *ptr = NULL;
489
0
  boolean invert;
490
491
0
  GET_TJINSTANCE(handle, -1)
492
493
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
494
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
495
0
    THROW("Invalid argument");
496
497
  /* The instance handle passed to this function is used only for parameter
498
     retrieval.  Create a new temporary instance to avoid interfering with the
499
     libjpeg state of the primary instance. */
500
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
501
0
    return -1;
502
0
  this2 = (tjinstance *)handle2;
503
0
  dinfo = &this2->dinfo;
504
505
#ifdef _MSC_VER
506
  if (fopen_s(&file, filename, "wb") || file == NULL)
507
#else
508
0
  if ((file = fopen(filename, "wb")) == NULL)
509
0
#endif
510
0
    THROW_UNIX("Cannot open output file");
511
512
0
  if (setjmp(this2->jerr.setjmp_buffer)) {
513
    /* If we get here, the JPEG code has signaled an error. */
514
0
    retval = -1;  goto bailout;
515
0
  }
516
517
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
518
0
  dinfo->image_width = width;  dinfo->image_height = height;
519
0
  dinfo->global_state = DSTATE_READY;
520
0
  dinfo->scale_num = dinfo->scale_denom = 1;
521
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
522
523
0
  ptr = strrchr(filename, '.');
524
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
525
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
526
0
      THROW("Could not initialize bitmap writer");
527
0
    invert = !this->bottomUp;
528
0
    dinfo->X_density = (UINT16)this->xDensity;
529
0
    dinfo->Y_density = (UINT16)this->yDensity;
530
0
    dinfo->density_unit = (UINT8)this->densityUnits;
531
0
  } else {
532
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
533
0
      THROW("Could not initialize PPM writer");
534
0
    invert = this->bottomUp;
535
0
  }
536
537
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
538
539
0
  dst->output_file = file;
540
0
  (*dst->start_output) (dinfo, dst);
541
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
542
543
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
544
545
0
  while (dinfo->output_scanline < dinfo->output_height) {
546
0
    _JSAMPLE *rowptr;
547
548
0
    if (invert)
549
0
      rowptr =
550
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
551
0
    else
552
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
553
0
    memcpy(dst->_buffer[0], rowptr,
554
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
555
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
556
0
    dinfo->output_scanline++;
557
0
  }
558
559
0
  (*dst->finish_output) (dinfo, dst);
560
561
0
bailout:
562
0
  tj3Destroy(handle2);
563
0
  if (file) fclose(file);
564
0
  return retval;
565
566
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
567
568
  GET_TJINSTANCE(handle, -1)
569
  THROW("16-bit data precision requires lossless JPEG,\n"
570
        "which was disabled at build time.")
571
bailout:
572
  return retval;
573
574
#endif
575
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
576
577
578
#undef _JSAMPLE
579
#undef _JSAMPROW
580
#undef _buffer
581
#undef _jinit_read_ppm
582
#undef _jinit_write_ppm
583
#undef _jpeg_crop_scanline
584
#undef _jpeg_read_scanlines
585
#undef _jpeg_skip_scanlines
586
#undef _jpeg_write_scanlines