Coverage Report

Created: 2026-12-01 06:32

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