Coverage Report

Created: 2026-07-30 07:40

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
100M
#define _JSAMPLE  JSAMPLE
34
60.5k
#define _JSAMPROW  JSAMPROW
35
100M
#define _buffer  buffer
36
49.1k
#define _jinit_read_ppm  jinit_read_ppm
37
0
#define _jinit_write_ppm  jinit_write_ppm
38
2.29k
#define _jpeg_crop_scanline  jpeg_crop_scanline
39
31.9M
#define _jpeg_read_scanlines  jpeg_read_scanlines
40
4.56k
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
41
18.8k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
69.5M
#define _JSAMPLE  J12SAMPLE
44
42.7k
#define _JSAMPROW  J12SAMPROW
45
69.5M
#define _buffer  buffer12
46
32.1k
#define _jinit_read_ppm  j12init_read_ppm
47
0
#define _jinit_write_ppm  j12init_write_ppm
48
1.27k
#define _jpeg_crop_scanline  jpeg12_crop_scanline
49
16.7M
#define _jpeg_read_scanlines  jpeg12_read_scanlines
50
2.52k
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
51
18.8k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
13.7M
#define _JSAMPLE  J16SAMPLE
54
15.5k
#define _JSAMPROW  J16SAMPROW
55
13.7M
#define _buffer  buffer16
56
11.2k
#define _jinit_read_ppm  j16init_read_ppm
57
0
#define _jinit_write_ppm  j16init_write_ppm
58
6.00M
#define _jpeg_read_scanlines  jpeg16_read_scanlines
59
4.69k
#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
229k
#define _GET_STRING(name, suffix)  #name #suffix
65
229k
#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
42.4k
{
75
42.4k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
42.4k
  int i, retval = 0;
77
42.4k
  boolean alloc = TRUE;
78
42.4k
  _JSAMPROW *row_pointer = NULL;
79
80
42.4k
  GET_CINSTANCE(handle)
81
42.4k
  if ((this->init & COMPRESS) == 0)
82
42.4k
    THROW("Instance has not been initialized for compression");
83
84
42.4k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
42.4k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
42.4k
      jpegSize == NULL)
87
42.4k
    THROW("Invalid argument");
88
89
42.4k
  if (!this->lossless && this->quality == -1)
90
42.4k
    THROW("TJPARAM_QUALITY must be specified");
91
42.4k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
42.4k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
42.4k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
42.4k
    THROW("Invalid argument");
97
98
42.4k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
42.4k
    THROW("Memory allocation failure");
100
101
42.4k
  CATCH_LIBJPEG(this);
102
103
42.3k
  cinfo->image_width = width;
104
42.3k
  cinfo->image_height = height;
105
42.3k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
42.3k
  setCompDefaults(this, pixelFormat, FALSE);
108
42.3k
  if (this->noRealloc) {
109
36.2k
    alloc = FALSE;
110
36.2k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
36.2k
  }
112
42.3k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
42.3k
  jpeg_start_compress(cinfo, TRUE);
115
146M
  for (i = 0; i < height; i++) {
116
146M
    if (this->bottomUp)
117
21.0M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
125M
    else
119
125M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
146M
  }
121
84.7k
  while (cinfo->next_scanline < cinfo->image_height)
122
42.4k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
42.4k
                          cinfo->image_height - cinfo->next_scanline);
124
42.3k
  jpeg_finish_compress(cinfo);
125
126
42.4k
bailout:
127
42.4k
  if (cinfo->global_state > CSTATE_START && alloc)
128
30
    (*cinfo->dest->term_destination) (cinfo);
129
42.4k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
30
    jpeg_abort_compress(cinfo);
131
42.4k
  free(row_pointer);
132
42.4k
  if (this->jerr.warning) retval = -1;
133
42.4k
  return retval;
134
42.3k
}
tj3Compress8
Line
Count
Source
74
18.8k
{
75
18.8k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.8k
  int i, retval = 0;
77
18.8k
  boolean alloc = TRUE;
78
18.8k
  _JSAMPROW *row_pointer = NULL;
79
80
18.8k
  GET_CINSTANCE(handle)
81
18.8k
  if ((this->init & COMPRESS) == 0)
82
18.8k
    THROW("Instance has not been initialized for compression");
83
84
18.8k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.8k
      jpegSize == NULL)
87
18.8k
    THROW("Invalid argument");
88
89
18.8k
  if (!this->lossless && this->quality == -1)
90
18.8k
    THROW("TJPARAM_QUALITY must be specified");
91
18.8k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.8k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.8k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
18.8k
    THROW("Invalid argument");
97
98
18.8k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
18.8k
    THROW("Memory allocation failure");
100
101
18.8k
  CATCH_LIBJPEG(this);
102
103
18.8k
  cinfo->image_width = width;
104
18.8k
  cinfo->image_height = height;
105
18.8k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
18.8k
  setCompDefaults(this, pixelFormat, FALSE);
108
18.8k
  if (this->noRealloc) {
109
16.1k
    alloc = FALSE;
110
16.1k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
16.1k
  }
112
18.8k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
18.8k
  jpeg_start_compress(cinfo, TRUE);
115
63.1M
  for (i = 0; i < height; i++) {
116
63.1M
    if (this->bottomUp)
117
9.18M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
53.9M
    else
119
53.9M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
63.1M
  }
121
37.7k
  while (cinfo->next_scanline < cinfo->image_height)
122
18.8k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
18.8k
                          cinfo->image_height - cinfo->next_scanline);
124
18.8k
  jpeg_finish_compress(cinfo);
125
126
18.8k
bailout:
127
18.8k
  if (cinfo->global_state > CSTATE_START && alloc)
128
0
    (*cinfo->dest->term_destination) (cinfo);
129
18.8k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
0
    jpeg_abort_compress(cinfo);
131
18.8k
  free(row_pointer);
132
18.8k
  if (this->jerr.warning) retval = -1;
133
18.8k
  return retval;
134
18.8k
}
tj3Compress12
Line
Count
Source
74
18.8k
{
75
18.8k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.8k
  int i, retval = 0;
77
18.8k
  boolean alloc = TRUE;
78
18.8k
  _JSAMPROW *row_pointer = NULL;
79
80
18.8k
  GET_CINSTANCE(handle)
81
18.8k
  if ((this->init & COMPRESS) == 0)
82
18.8k
    THROW("Instance has not been initialized for compression");
83
84
18.8k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.8k
      jpegSize == NULL)
87
18.8k
    THROW("Invalid argument");
88
89
18.8k
  if (!this->lossless && this->quality == -1)
90
18.8k
    THROW("TJPARAM_QUALITY must be specified");
91
18.8k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.8k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.8k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
18.8k
    THROW("Invalid argument");
97
98
18.8k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
18.8k
    THROW("Memory allocation failure");
100
101
18.8k
  CATCH_LIBJPEG(this);
102
103
18.8k
  cinfo->image_width = width;
104
18.8k
  cinfo->image_height = height;
105
18.8k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
18.8k
  setCompDefaults(this, pixelFormat, FALSE);
108
18.8k
  if (this->noRealloc) {
109
16.0k
    alloc = FALSE;
110
16.0k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
16.0k
  }
112
18.8k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
18.8k
  jpeg_start_compress(cinfo, TRUE);
115
69.4M
  for (i = 0; i < height; i++) {
116
69.3M
    if (this->bottomUp)
117
9.92M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
59.4M
    else
119
59.4M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
69.3M
  }
121
37.6k
  while (cinfo->next_scanline < cinfo->image_height)
122
18.8k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
18.8k
                          cinfo->image_height - cinfo->next_scanline);
124
18.8k
  jpeg_finish_compress(cinfo);
125
126
18.8k
bailout:
127
18.8k
  if (cinfo->global_state > CSTATE_START && alloc)
128
0
    (*cinfo->dest->term_destination) (cinfo);
129
18.8k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
0
    jpeg_abort_compress(cinfo);
131
18.8k
  free(row_pointer);
132
18.8k
  if (this->jerr.warning) retval = -1;
133
18.8k
  return retval;
134
18.8k
}
tj3Compress16
Line
Count
Source
74
4.69k
{
75
4.69k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
4.69k
  int i, retval = 0;
77
4.69k
  boolean alloc = TRUE;
78
4.69k
  _JSAMPROW *row_pointer = NULL;
79
80
4.69k
  GET_CINSTANCE(handle)
81
4.69k
  if ((this->init & COMPRESS) == 0)
82
4.69k
    THROW("Instance has not been initialized for compression");
83
84
4.69k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
4.69k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
4.69k
      jpegSize == NULL)
87
4.69k
    THROW("Invalid argument");
88
89
4.69k
  if (!this->lossless && this->quality == -1)
90
4.69k
    THROW("TJPARAM_QUALITY must be specified");
91
4.69k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
4.69k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
4.69k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
4.69k
    THROW("Invalid argument");
97
98
4.69k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
4.69k
    THROW("Memory allocation failure");
100
101
4.69k
  CATCH_LIBJPEG(this);
102
103
4.66k
  cinfo->image_width = width;
104
4.66k
  cinfo->image_height = height;
105
4.66k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
4.66k
  setCompDefaults(this, pixelFormat, FALSE);
108
4.66k
  if (this->noRealloc) {
109
4.01k
    alloc = FALSE;
110
4.01k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
4.01k
  }
112
4.66k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
4.66k
  jpeg_start_compress(cinfo, TRUE);
115
13.6M
  for (i = 0; i < height; i++) {
116
13.6M
    if (this->bottomUp)
117
1.95M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
11.7M
    else
119
11.7M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
13.6M
  }
121
9.36k
  while (cinfo->next_scanline < cinfo->image_height)
122
4.69k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
4.69k
                          cinfo->image_height - cinfo->next_scanline);
124
4.66k
  jpeg_finish_compress(cinfo);
125
126
4.69k
bailout:
127
4.69k
  if (cinfo->global_state > CSTATE_START && alloc)
128
30
    (*cinfo->dest->term_destination) (cinfo);
129
4.69k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
30
    jpeg_abort_compress(cinfo);
131
4.69k
  free(row_pointer);
132
4.69k
  if (this->jerr.warning) retval = -1;
133
4.69k
  return retval;
134
4.66k
}
135
136
137
/******************************* Decompressor ********************************/
138
139
/* TurboJPEG 3+ */
140
DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE)
141
  (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize,
142
   _JSAMPLE *dstBuf, int pitch, int pixelFormat)
143
76.3k
{
144
76.3k
  static const char FUNCTION_NAME[] =
145
76.3k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
76.3k
  _JSAMPROW *row_pointer = NULL;
147
76.3k
  int croppedHeight, i, retval = 0;
148
#if BITS_IN_JSAMPLE != 16
149
  int scaledWidth;
150
#endif
151
76.3k
  struct my_progress_mgr progress;
152
153
76.3k
  GET_DINSTANCE(handle);
154
76.3k
  if ((this->init & DECOMPRESS) == 0)
155
76.3k
    THROW("Instance has not been initialized for decompression");
156
157
76.3k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
76.3k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
76.3k
    THROW("Invalid argument");
160
161
76.3k
  if (this->scanLimit) {
162
76.3k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
76.3k
    progress.pub.progress_monitor = my_progress_monitor;
164
76.3k
    progress.this = this;
165
76.3k
    dinfo->progress = &progress.pub;
166
76.3k
  } else
167
0
    dinfo->progress = NULL;
168
169
76.3k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
76.3k
  CATCH_LIBJPEG(this);
172
173
76.3k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
76.3k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
76.3k
    jpeg_read_header(dinfo, TRUE);
176
76.3k
  }
177
32.6k
  setDecompParameters(this);
178
32.6k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
32.6k
    THROW("Image is too large");
182
32.6k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
#if BITS_IN_JSAMPLE != 16
184
30.2k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
#endif
186
32.6k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
32.6k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
32.6k
  dinfo->scale_num = this->scalingFactor.num;
190
32.6k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
32.6k
  jpeg_start_decompress(dinfo);
193
194
#if BITS_IN_JSAMPLE != 16
195
30.2k
  if (this->croppingRegion.x != 0 ||
196
26.6k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
3.56k
    JDIMENSION crop_x = this->croppingRegion.x;
198
3.56k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
3.56k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
3.56k
    if ((int)crop_x != this->croppingRegion.x)
202
9
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
3.56k
             "actual (%d) cropping region left boundary",
204
3.56k
             this->croppingRegion.x, (int)crop_x);
205
3.55k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
3.55k
             "actual (%d) cropping region width",
208
3.55k
             this->croppingRegion.w, (int)crop_w);
209
3.55k
  }
210
30.2k
#endif
211
212
32.6k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
32.6k
    THROW("Invalid argument");
215
216
32.6k
  croppedHeight = dinfo->output_height;
217
#if BITS_IN_JSAMPLE != 16
218
30.2k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
3.55k
    croppedHeight = this->croppingRegion.h;
220
#endif
221
32.6k
  if ((row_pointer =
222
32.6k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
32.6k
    THROW("Memory allocation failure");
224
32.6k
  CATCH_LIBJPEG(this);
225
127M
  for (i = 0; i < (int)croppedHeight; i++) {
226
127M
    if (this->bottomUp)
227
41.7M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
85.9M
    else
229
85.9M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
127M
  }
231
232
#if BITS_IN_JSAMPLE != 16
233
26.6k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
3.55k
    if (this->croppingRegion.y != 0) {
235
3.55k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
3.55k
      if ((int)lines != this->croppingRegion.y)
238
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
               "actual (%d) cropping region upper boundary",
240
3.55k
               this->croppingRegion.y, (int)lines);
241
3.55k
    }
242
135k
    while ((int)dinfo->output_scanline <
243
135k
           this->croppingRegion.y + this->croppingRegion.h)
244
131k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
131k
                                               this->croppingRegion.y],
246
131k
                           this->croppingRegion.y + this->croppingRegion.h -
247
131k
                           dinfo->output_scanline);
248
3.55k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
3.55k
        (int)dinfo->output_height) {
250
3.52k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
                                                     this->croppingRegion.y -
252
                                                     this->croppingRegion.h);
253
254
3.52k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
3.52k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
3.52k
               "actual (%d) cropping region lower boundary",
258
3.52k
               this->croppingRegion.y + this->croppingRegion.h,
259
3.52k
               (int)(dinfo->output_height - lines));
260
3.52k
    }
261
3.55k
  } else
262
22.4k
#endif
263
22.4k
  {
264
54.5M
    while (dinfo->output_scanline < dinfo->output_height)
265
54.5M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
54.5M
                           dinfo->output_height - dinfo->output_scanline);
267
22.4k
  }
268
25.9k
  jpeg_finish_decompress(dinfo);
269
270
76.3k
bailout:
271
76.3k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
76.3k
  free(row_pointer);
273
76.3k
  if (this->jerr.warning) retval = -1;
274
76.3k
  return retval;
275
25.9k
}
tj3Decompress8
Line
Count
Source
143
41.6k
{
144
41.6k
  static const char FUNCTION_NAME[] =
145
41.6k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
41.6k
  _JSAMPROW *row_pointer = NULL;
147
41.6k
  int croppedHeight, i, retval = 0;
148
41.6k
#if BITS_IN_JSAMPLE != 16
149
41.6k
  int scaledWidth;
150
41.6k
#endif
151
41.6k
  struct my_progress_mgr progress;
152
153
41.6k
  GET_DINSTANCE(handle);
154
41.6k
  if ((this->init & DECOMPRESS) == 0)
155
41.6k
    THROW("Instance has not been initialized for decompression");
156
157
41.6k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
41.6k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
41.6k
    THROW("Invalid argument");
160
161
41.6k
  if (this->scanLimit) {
162
41.6k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
41.6k
    progress.pub.progress_monitor = my_progress_monitor;
164
41.6k
    progress.this = this;
165
41.6k
    dinfo->progress = &progress.pub;
166
41.6k
  } else
167
0
    dinfo->progress = NULL;
168
169
41.6k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
41.6k
  CATCH_LIBJPEG(this);
172
173
41.6k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
41.6k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
41.6k
    jpeg_read_header(dinfo, TRUE);
176
41.6k
  }
177
18.8k
  setDecompParameters(this);
178
18.8k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
18.8k
    THROW("Image is too large");
182
18.8k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
18.8k
#if BITS_IN_JSAMPLE != 16
184
18.8k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
18.8k
#endif
186
18.8k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
18.8k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
18.8k
  dinfo->scale_num = this->scalingFactor.num;
190
18.8k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
18.8k
  jpeg_start_decompress(dinfo);
193
194
18.8k
#if BITS_IN_JSAMPLE != 16
195
18.8k
  if (this->croppingRegion.x != 0 ||
196
16.5k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
2.29k
    JDIMENSION crop_x = this->croppingRegion.x;
198
2.29k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
2.29k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
2.29k
    if ((int)crop_x != this->croppingRegion.x)
202
6
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
2.29k
             "actual (%d) cropping region left boundary",
204
2.29k
             this->croppingRegion.x, (int)crop_x);
205
2.28k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
2.28k
             "actual (%d) cropping region width",
208
2.28k
             this->croppingRegion.w, (int)crop_w);
209
2.28k
  }
210
18.8k
#endif
211
212
18.8k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
18.8k
    THROW("Invalid argument");
215
216
18.8k
  croppedHeight = dinfo->output_height;
217
18.8k
#if BITS_IN_JSAMPLE != 16
218
18.8k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
2.28k
    croppedHeight = this->croppingRegion.h;
220
18.8k
#endif
221
18.8k
  if ((row_pointer =
222
18.8k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
18.8k
    THROW("Memory allocation failure");
224
18.8k
  CATCH_LIBJPEG(this);
225
68.2M
  for (i = 0; i < (int)croppedHeight; i++) {
226
68.2M
    if (this->bottomUp)
227
23.3M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
44.8M
    else
229
44.8M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
68.2M
  }
231
232
16.2k
#if BITS_IN_JSAMPLE != 16
233
16.5k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
2.28k
    if (this->croppingRegion.y != 0) {
235
2.28k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
2.28k
      if ((int)lines != this->croppingRegion.y)
238
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
2.28k
               "actual (%d) cropping region upper boundary",
240
2.28k
               this->croppingRegion.y, (int)lines);
241
2.28k
    }
242
90.2k
    while ((int)dinfo->output_scanline <
243
90.2k
           this->croppingRegion.y + this->croppingRegion.h)
244
87.9k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
87.9k
                                               this->croppingRegion.y],
246
87.9k
                           this->croppingRegion.y + this->croppingRegion.h -
247
87.9k
                           dinfo->output_scanline);
248
2.28k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
2.28k
        (int)dinfo->output_height) {
250
2.27k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
2.27k
                                                     this->croppingRegion.y -
252
2.27k
                                                     this->croppingRegion.h);
253
254
2.27k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
2.27k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
2.27k
               "actual (%d) cropping region lower boundary",
258
2.27k
               this->croppingRegion.y + this->croppingRegion.h,
259
2.27k
               (int)(dinfo->output_height - lines));
260
2.27k
    }
261
2.28k
  } else
262
13.9k
#endif
263
13.9k
  {
264
31.8M
    while (dinfo->output_scanline < dinfo->output_height)
265
31.8M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
31.8M
                           dinfo->output_height - dinfo->output_scanline);
267
13.9k
  }
268
16.2k
  jpeg_finish_decompress(dinfo);
269
270
41.6k
bailout:
271
41.6k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
41.6k
  free(row_pointer);
273
41.6k
  if (this->jerr.warning) retval = -1;
274
41.6k
  return retval;
275
16.2k
}
tj3Decompress12
Line
Count
Source
143
23.8k
{
144
23.8k
  static const char FUNCTION_NAME[] =
145
23.8k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
23.8k
  _JSAMPROW *row_pointer = NULL;
147
23.8k
  int croppedHeight, i, retval = 0;
148
23.8k
#if BITS_IN_JSAMPLE != 16
149
23.8k
  int scaledWidth;
150
23.8k
#endif
151
23.8k
  struct my_progress_mgr progress;
152
153
23.8k
  GET_DINSTANCE(handle);
154
23.8k
  if ((this->init & DECOMPRESS) == 0)
155
23.8k
    THROW("Instance has not been initialized for decompression");
156
157
23.8k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
23.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
23.8k
    THROW("Invalid argument");
160
161
23.8k
  if (this->scanLimit) {
162
23.8k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
23.8k
    progress.pub.progress_monitor = my_progress_monitor;
164
23.8k
    progress.this = this;
165
23.8k
    dinfo->progress = &progress.pub;
166
23.8k
  } else
167
0
    dinfo->progress = NULL;
168
169
23.8k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
23.8k
  CATCH_LIBJPEG(this);
172
173
23.8k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
23.8k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
23.8k
    jpeg_read_header(dinfo, TRUE);
176
23.8k
  }
177
11.3k
  setDecompParameters(this);
178
11.3k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
11.3k
    THROW("Image is too large");
182
11.3k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
11.3k
#if BITS_IN_JSAMPLE != 16
184
11.3k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
11.3k
#endif
186
11.3k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
11.3k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
11.3k
  dinfo->scale_num = this->scalingFactor.num;
190
11.3k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
11.3k
  jpeg_start_decompress(dinfo);
193
194
11.3k
#if BITS_IN_JSAMPLE != 16
195
11.3k
  if (this->croppingRegion.x != 0 ||
196
10.1k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
1.27k
    JDIMENSION crop_x = this->croppingRegion.x;
198
1.27k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
1.27k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
1.27k
    if ((int)crop_x != this->croppingRegion.x)
202
3
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
1.27k
             "actual (%d) cropping region left boundary",
204
1.27k
             this->croppingRegion.x, (int)crop_x);
205
1.26k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
1.26k
             "actual (%d) cropping region width",
208
1.26k
             this->croppingRegion.w, (int)crop_w);
209
1.26k
  }
210
11.3k
#endif
211
212
11.3k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
11.3k
    THROW("Invalid argument");
215
216
11.3k
  croppedHeight = dinfo->output_height;
217
11.3k
#if BITS_IN_JSAMPLE != 16
218
11.3k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
1.26k
    croppedHeight = this->croppingRegion.h;
220
11.3k
#endif
221
11.3k
  if ((row_pointer =
222
11.3k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
11.3k
    THROW("Memory allocation failure");
224
11.3k
  CATCH_LIBJPEG(this);
225
36.3M
  for (i = 0; i < (int)croppedHeight; i++) {
226
36.3M
    if (this->bottomUp)
227
12.8M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
23.4M
    else
229
23.4M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
36.3M
  }
231
232
9.73k
#if BITS_IN_JSAMPLE != 16
233
10.1k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
1.26k
    if (this->croppingRegion.y != 0) {
235
1.26k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
1.26k
      if ((int)lines != this->croppingRegion.y)
238
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
1.26k
               "actual (%d) cropping region upper boundary",
240
1.26k
               this->croppingRegion.y, (int)lines);
241
1.26k
    }
242
44.7k
    while ((int)dinfo->output_scanline <
243
44.7k
           this->croppingRegion.y + this->croppingRegion.h)
244
43.5k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
43.5k
                                               this->croppingRegion.y],
246
43.5k
                           this->croppingRegion.y + this->croppingRegion.h -
247
43.5k
                           dinfo->output_scanline);
248
1.26k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
1.26k
        (int)dinfo->output_height) {
250
1.25k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
1.25k
                                                     this->croppingRegion.y -
252
1.25k
                                                     this->croppingRegion.h);
253
254
1.25k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
1.25k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
1.25k
               "actual (%d) cropping region lower boundary",
258
1.25k
               this->croppingRegion.y + this->croppingRegion.h,
259
1.25k
               (int)(dinfo->output_height - lines));
260
1.25k
    }
261
1.26k
  } else
262
8.46k
#endif
263
8.46k
  {
264
16.7M
    while (dinfo->output_scanline < dinfo->output_height)
265
16.7M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
16.7M
                           dinfo->output_height - dinfo->output_scanline);
267
8.46k
  }
268
9.73k
  jpeg_finish_decompress(dinfo);
269
270
23.8k
bailout:
271
23.8k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
23.8k
  free(row_pointer);
273
23.8k
  if (this->jerr.warning) retval = -1;
274
23.8k
  return retval;
275
9.73k
}
tj3Decompress16
Line
Count
Source
143
10.8k
{
144
10.8k
  static const char FUNCTION_NAME[] =
145
10.8k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
10.8k
  _JSAMPROW *row_pointer = NULL;
147
10.8k
  int croppedHeight, i, retval = 0;
148
#if BITS_IN_JSAMPLE != 16
149
  int scaledWidth;
150
#endif
151
10.8k
  struct my_progress_mgr progress;
152
153
10.8k
  GET_DINSTANCE(handle);
154
10.8k
  if ((this->init & DECOMPRESS) == 0)
155
10.8k
    THROW("Instance has not been initialized for decompression");
156
157
10.8k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
10.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
10.8k
    THROW("Invalid argument");
160
161
10.8k
  if (this->scanLimit) {
162
10.8k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
10.8k
    progress.pub.progress_monitor = my_progress_monitor;
164
10.8k
    progress.this = this;
165
10.8k
    dinfo->progress = &progress.pub;
166
10.8k
  } else
167
0
    dinfo->progress = NULL;
168
169
10.8k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
10.8k
  CATCH_LIBJPEG(this);
172
173
10.8k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
10.8k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
10.8k
    jpeg_read_header(dinfo, TRUE);
176
10.8k
  }
177
2.42k
  setDecompParameters(this);
178
2.42k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
2.42k
    THROW("Image is too large");
182
2.42k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
#if BITS_IN_JSAMPLE != 16
184
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
#endif
186
2.42k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
2.42k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
2.42k
  dinfo->scale_num = this->scalingFactor.num;
190
2.42k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
2.42k
  jpeg_start_decompress(dinfo);
193
194
#if BITS_IN_JSAMPLE != 16
195
  if (this->croppingRegion.x != 0 ||
196
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
    JDIMENSION crop_x = this->croppingRegion.x;
198
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
    if ((int)crop_x != this->croppingRegion.x)
202
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
             "actual (%d) cropping region left boundary",
204
             this->croppingRegion.x, (int)crop_x);
205
    if ((int)crop_w != this->croppingRegion.w)
206
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
             "actual (%d) cropping region width",
208
             this->croppingRegion.w, (int)crop_w);
209
  }
210
#endif
211
212
2.42k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
2.42k
    THROW("Invalid argument");
215
216
2.42k
  croppedHeight = dinfo->output_height;
217
#if BITS_IN_JSAMPLE != 16
218
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
    croppedHeight = this->croppingRegion.h;
220
#endif
221
2.42k
  if ((row_pointer =
222
2.42k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
2.42k
    THROW("Memory allocation failure");
224
2.42k
  CATCH_LIBJPEG(this);
225
23.0M
  for (i = 0; i < (int)croppedHeight; i++) {
226
23.0M
    if (this->bottomUp)
227
5.43M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
17.6M
    else
229
17.6M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
23.0M
  }
231
232
#if BITS_IN_JSAMPLE != 16
233
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
    if (this->croppingRegion.y != 0) {
235
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
      if ((int)lines != this->croppingRegion.y)
238
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
               "actual (%d) cropping region upper boundary",
240
               this->croppingRegion.y, (int)lines);
241
    }
242
    while ((int)dinfo->output_scanline <
243
           this->croppingRegion.y + this->croppingRegion.h)
244
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
                                               this->croppingRegion.y],
246
                           this->croppingRegion.y + this->croppingRegion.h -
247
                           dinfo->output_scanline);
248
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
        (int)dinfo->output_height) {
250
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
                                                     this->croppingRegion.y -
252
                                                     this->croppingRegion.h);
253
254
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
                   this->croppingRegion.h)
256
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
               "actual (%d) cropping region lower boundary",
258
               this->croppingRegion.y + this->croppingRegion.h,
259
               (int)(dinfo->output_height - lines));
260
    }
261
  } else
262
#endif
263
1.29k
  {
264
6.00M
    while (dinfo->output_scanline < dinfo->output_height)
265
6.00M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
6.00M
                           dinfo->output_height - dinfo->output_scanline);
267
1.29k
  }
268
1.29k
  jpeg_finish_decompress(dinfo);
269
270
10.8k
bailout:
271
10.8k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
10.8k
  free(row_pointer);
273
10.8k
  if (this->jerr.warning) retval = -1;
274
10.8k
  return retval;
275
1.29k
}
276
277
278
/*************************** Packed-Pixel Image I/O **************************/
279
280
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
281
282
/* TurboJPEG 3+ */
283
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
284
static
285
#endif
286
_JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
287
  (tjhandle handle, FILE *file, int *width, int align, int *height,
288
   int *pixelFormat)
289
111k
{
290
111k
  static const char FUNCTION_NAME[] =
291
111k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
111k
  int retval = 0, tempc;
294
111k
  size_t pitch;
295
111k
  tjhandle handle2 = NULL;
296
111k
  tjinstance *this2;
297
111k
  j_compress_ptr cinfo = NULL;
298
111k
  cjpeg_source_ptr src;
299
111k
  _JSAMPLE *dstBuf = NULL;
300
111k
  boolean invert;
301
302
111k
  GET_TJINSTANCE(handle, NULL)
303
304
111k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
111k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
111k
    THROW("Invalid argument");
307
111k
  if ((align & (align - 1)) != 0)
308
111k
    THROW("Alignment must be a power of 2");
309
310
  /* The instance handle passed to this function is used only for parameter
311
     retrieval.  Create a new temporary instance to avoid interfering with the
312
     libjpeg state of the primary instance. */
313
111k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
111k
  this2 = (tjinstance *)handle2;
315
111k
  cinfo = &this2->cinfo;
316
317
111k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
111k
  else if (tempc == EOF)
320
111k
    THROW("Input file contains no data");
321
322
111k
  CATCH_LIBJPEG2();
323
324
86.1k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
86.1k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
86.1k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
86.1k
  if (tempc == 'B') {
328
17.8k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
17.8k
      THROW("Could not initialize bitmap loader");
330
17.8k
    invert = !this->bottomUp;
331
92.5k
  } else if (tempc == 'P') {
332
92.5k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
92.5k
      THROW("Could not initialize PPM loader");
334
92.5k
    invert = this->bottomUp;
335
92.5k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
110k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
110k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
110k
  src->max_pixels = this->maxPixels;
343
110k
  (*src->start_input) (cinfo, src);
344
110k
  if (tempc == 'B') {
345
5.18k
    if (cinfo->X_density && cinfo->Y_density) {
346
1.19k
      this->xDensity = cinfo->X_density;
347
1.19k
      this->yDensity = cinfo->Y_density;
348
1.19k
      this->densityUnits = cinfo->density_unit;
349
1.19k
    }
350
5.18k
  }
351
110k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
110k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
110k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
110k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
110k
  if (
359
#if ULLONG_MAX > SIZE_MAX
360
      (unsigned long long)pitch * (unsigned long long)(*height) *
361
      (unsigned long long)sizeof(_JSAMPLE) >
362
      (unsigned long long)((size_t)-1) ||
363
#endif
364
110k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
110k
                                   sizeof(_JSAMPLE))) == NULL)
366
110k
    THROW("Memory allocation failure");
367
368
110k
  CATCH_LIBJPEG2();
369
370
184M
  while (cinfo->next_scanline < cinfo->image_height) {
371
184M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
368M
    for (i = 0; i < nlines; i++) {
374
184M
      _JSAMPLE *dstptr;
375
184M
      int row;
376
377
184M
      row = cinfo->next_scanline + i;
378
184M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
152M
      else dstptr = &dstBuf[row * pitch];
380
184M
      memcpy(dstptr, src->_buffer[i],
381
184M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
184M
    }
383
184M
    cinfo->next_scanline += nlines;
384
184M
  }
385
386
79.0k
  (*src->finish_input) (cinfo, src);
387
388
111k
bailout:
389
111k
  tj3Destroy(handle2);
390
111k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
111k
  return dstBuf;
392
79.0k
}
_tj3LoadImageFromFileHandle8
Line
Count
Source
289
67.2k
{
290
67.2k
  static const char FUNCTION_NAME[] =
291
67.2k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
67.2k
  int retval = 0, tempc;
294
67.2k
  size_t pitch;
295
67.2k
  tjhandle handle2 = NULL;
296
67.2k
  tjinstance *this2;
297
67.2k
  j_compress_ptr cinfo = NULL;
298
67.2k
  cjpeg_source_ptr src;
299
67.2k
  _JSAMPLE *dstBuf = NULL;
300
67.2k
  boolean invert;
301
302
67.2k
  GET_TJINSTANCE(handle, NULL)
303
304
67.2k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
67.2k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
67.2k
    THROW("Invalid argument");
307
67.2k
  if ((align & (align - 1)) != 0)
308
67.2k
    THROW("Alignment must be a power of 2");
309
310
  /* The instance handle passed to this function is used only for parameter
311
     retrieval.  Create a new temporary instance to avoid interfering with the
312
     libjpeg state of the primary instance. */
313
67.2k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
67.2k
  this2 = (tjinstance *)handle2;
315
67.2k
  cinfo = &this2->cinfo;
316
317
67.2k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
67.2k
  else if (tempc == EOF)
320
67.2k
    THROW("Input file contains no data");
321
322
67.2k
  CATCH_LIBJPEG2();
323
324
48.5k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
48.5k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
48.5k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
48.5k
  if (tempc == 'B') {
328
17.8k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
17.8k
      THROW("Could not initialize bitmap loader");
330
17.8k
    invert = !this->bottomUp;
331
49.1k
  } else if (tempc == 'P') {
332
49.1k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
49.1k
      THROW("Could not initialize PPM loader");
334
49.1k
    invert = this->bottomUp;
335
49.1k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
66.9k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
66.9k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
66.9k
  src->max_pixels = this->maxPixels;
343
66.9k
  (*src->start_input) (cinfo, src);
344
66.9k
  if (tempc == 'B') {
345
5.18k
    if (cinfo->X_density && cinfo->Y_density) {
346
1.19k
      this->xDensity = cinfo->X_density;
347
1.19k
      this->yDensity = cinfo->Y_density;
348
1.19k
      this->densityUnits = cinfo->density_unit;
349
1.19k
    }
350
5.18k
  }
351
66.9k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
66.9k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
66.9k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
66.9k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
66.9k
  if (
359
#if ULLONG_MAX > SIZE_MAX
360
      (unsigned long long)pitch * (unsigned long long)(*height) *
361
      (unsigned long long)sizeof(_JSAMPLE) >
362
      (unsigned long long)((size_t)-1) ||
363
#endif
364
66.9k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
66.9k
                                   sizeof(_JSAMPLE))) == NULL)
366
66.9k
    THROW("Memory allocation failure");
367
368
66.9k
  CATCH_LIBJPEG2();
369
370
100M
  while (cinfo->next_scanline < cinfo->image_height) {
371
100M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
201M
    for (i = 0; i < nlines; i++) {
374
100M
      _JSAMPLE *dstptr;
375
100M
      int row;
376
377
100M
      row = cinfo->next_scanline + i;
378
100M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
81.1M
      else dstptr = &dstBuf[row * pitch];
380
100M
      memcpy(dstptr, src->_buffer[i],
381
100M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
100M
    }
383
100M
    cinfo->next_scanline += nlines;
384
100M
  }
385
386
49.4k
  (*src->finish_input) (cinfo, src);
387
388
67.2k
bailout:
389
67.2k
  tj3Destroy(handle2);
390
67.2k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
67.2k
  return dstBuf;
392
49.4k
}
_tj3LoadImageFromFileHandle12
Line
Count
Source
289
32.3k
{
290
32.3k
  static const char FUNCTION_NAME[] =
291
32.3k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
32.3k
  int retval = 0, tempc;
294
32.3k
  size_t pitch;
295
32.3k
  tjhandle handle2 = NULL;
296
32.3k
  tjinstance *this2;
297
32.3k
  j_compress_ptr cinfo = NULL;
298
32.3k
  cjpeg_source_ptr src;
299
32.3k
  _JSAMPLE *dstBuf = NULL;
300
32.3k
  boolean invert;
301
302
32.3k
  GET_TJINSTANCE(handle, NULL)
303
304
32.3k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
32.3k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
32.3k
    THROW("Invalid argument");
307
32.3k
  if ((align & (align - 1)) != 0)
308
32.3k
    THROW("Alignment must be a power of 2");
309
310
  /* The instance handle passed to this function is used only for parameter
311
     retrieval.  Create a new temporary instance to avoid interfering with the
312
     libjpeg state of the primary instance. */
313
32.3k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
32.3k
  this2 = (tjinstance *)handle2;
315
32.3k
  cinfo = &this2->cinfo;
316
317
32.3k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
32.3k
  else if (tempc == EOF)
320
32.3k
    THROW("Input file contains no data");
321
322
32.3k
  CATCH_LIBJPEG2();
323
324
28.1k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
28.1k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
28.1k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
28.1k
  if (tempc == 'B') {
328
14
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
14
      THROW("Could not initialize bitmap loader");
330
14
    invert = !this->bottomUp;
331
32.1k
  } else if (tempc == 'P') {
332
32.1k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
32.1k
      THROW("Could not initialize PPM loader");
334
32.1k
    invert = this->bottomUp;
335
32.1k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
32.1k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
32.1k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
32.1k
  src->max_pixels = this->maxPixels;
343
32.1k
  (*src->start_input) (cinfo, src);
344
32.1k
  if (tempc == 'B') {
345
0
    if (cinfo->X_density && cinfo->Y_density) {
346
0
      this->xDensity = cinfo->X_density;
347
0
      this->yDensity = cinfo->Y_density;
348
0
      this->densityUnits = cinfo->density_unit;
349
0
    }
350
0
  }
351
32.1k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
32.1k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
32.1k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
32.1k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
32.1k
  if (
359
#if ULLONG_MAX > SIZE_MAX
360
      (unsigned long long)pitch * (unsigned long long)(*height) *
361
      (unsigned long long)sizeof(_JSAMPLE) >
362
      (unsigned long long)((size_t)-1) ||
363
#endif
364
32.1k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
32.1k
                                   sizeof(_JSAMPLE))) == NULL)
366
32.1k
    THROW("Memory allocation failure");
367
368
32.1k
  CATCH_LIBJPEG2();
369
370
69.5M
  while (cinfo->next_scanline < cinfo->image_height) {
371
69.5M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
139M
    for (i = 0; i < nlines; i++) {
374
69.5M
      _JSAMPLE *dstptr;
375
69.5M
      int row;
376
377
69.5M
      row = cinfo->next_scanline + i;
378
69.5M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
59.5M
      else dstptr = &dstBuf[row * pitch];
380
69.5M
      memcpy(dstptr, src->_buffer[i],
381
69.5M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
69.5M
    }
383
69.5M
    cinfo->next_scanline += nlines;
384
69.5M
  }
385
386
22.9k
  (*src->finish_input) (cinfo, src);
387
388
32.3k
bailout:
389
32.3k
  tj3Destroy(handle2);
390
32.3k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
32.3k
  return dstBuf;
392
22.9k
}
_tj3LoadImageFromFileHandle16
Line
Count
Source
289
11.3k
{
290
11.3k
  static const char FUNCTION_NAME[] =
291
11.3k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
11.3k
  int retval = 0, tempc;
294
11.3k
  size_t pitch;
295
11.3k
  tjhandle handle2 = NULL;
296
11.3k
  tjinstance *this2;
297
11.3k
  j_compress_ptr cinfo = NULL;
298
11.3k
  cjpeg_source_ptr src;
299
11.3k
  _JSAMPLE *dstBuf = NULL;
300
11.3k
  boolean invert;
301
302
11.3k
  GET_TJINSTANCE(handle, NULL)
303
304
11.3k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
11.3k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
11.3k
    THROW("Invalid argument");
307
11.3k
  if ((align & (align - 1)) != 0)
308
11.3k
    THROW("Alignment must be a power of 2");
309
310
  /* The instance handle passed to this function is used only for parameter
311
     retrieval.  Create a new temporary instance to avoid interfering with the
312
     libjpeg state of the primary instance. */
313
11.3k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
11.3k
  this2 = (tjinstance *)handle2;
315
11.3k
  cinfo = &this2->cinfo;
316
317
11.3k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
11.3k
  else if (tempc == EOF)
320
11.3k
    THROW("Input file contains no data");
321
322
11.3k
  CATCH_LIBJPEG2();
323
324
9.45k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
9.45k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
9.45k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
9.45k
  if (tempc == 'B') {
328
7
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
7
      THROW("Could not initialize bitmap loader");
330
7
    invert = !this->bottomUp;
331
11.2k
  } else if (tempc == 'P') {
332
11.2k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
11.2k
      THROW("Could not initialize PPM loader");
334
11.2k
    invert = this->bottomUp;
335
11.2k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
11.2k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
11.2k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
11.2k
  src->max_pixels = this->maxPixels;
343
11.2k
  (*src->start_input) (cinfo, src);
344
11.2k
  if (tempc == 'B') {
345
0
    if (cinfo->X_density && cinfo->Y_density) {
346
0
      this->xDensity = cinfo->X_density;
347
0
      this->yDensity = cinfo->Y_density;
348
0
      this->densityUnits = cinfo->density_unit;
349
0
    }
350
0
  }
351
11.2k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
11.2k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
11.2k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
11.2k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
11.2k
  if (
359
#if ULLONG_MAX > SIZE_MAX
360
      (unsigned long long)pitch * (unsigned long long)(*height) *
361
      (unsigned long long)sizeof(_JSAMPLE) >
362
      (unsigned long long)((size_t)-1) ||
363
#endif
364
11.2k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
11.2k
                                   sizeof(_JSAMPLE))) == NULL)
366
11.2k
    THROW("Memory allocation failure");
367
368
11.2k
  CATCH_LIBJPEG2();
369
370
13.7M
  while (cinfo->next_scanline < cinfo->image_height) {
371
13.7M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
27.4M
    for (i = 0; i < nlines; i++) {
374
13.7M
      _JSAMPLE *dstptr;
375
13.7M
      int row;
376
377
13.7M
      row = cinfo->next_scanline + i;
378
13.7M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
11.7M
      else dstptr = &dstBuf[row * pitch];
380
13.7M
      memcpy(dstptr, src->_buffer[i],
381
13.7M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
13.7M
    }
383
13.7M
    cinfo->next_scanline += nlines;
384
13.7M
  }
385
386
6.63k
  (*src->finish_input) (cinfo, src);
387
388
11.3k
bailout:
389
11.3k
  tj3Destroy(handle2);
390
11.3k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
11.3k
  return dstBuf;
392
6.63k
}
393
394
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
395
396
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
397
  (tjhandle handle, const char *filename, int *width, int align, int *height,
398
   int *pixelFormat)
399
0
{
400
0
  static const char FUNCTION_NAME[] =
401
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
402
403
0
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
404
405
0
  int retval = 0;
406
0
  _JSAMPLE *dstBuf = NULL;
407
0
  FILE *file = NULL;
408
409
0
  GET_TJINSTANCE(handle, NULL)
410
411
0
  if (!filename)
412
0
    THROW("Invalid argument");
413
414
#ifdef _MSC_VER
415
  if (fopen_s(&file, filename, "rb") || file == NULL)
416
#else
417
0
  if ((file = fopen(filename, "rb")) == NULL)
418
0
#endif
419
0
    THROW_UNIX("Cannot open input file");
420
421
0
  dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
422
0
             (handle, file, width, align, height, pixelFormat);
423
424
0
bailout:
425
0
  if (file) fclose(file);
426
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
427
0
  return dstBuf;
428
429
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
430
431
  static const char ERROR_MSG[] =
432
    "16-bit data precision requires lossless JPEG,\n"
433
    "which was disabled at build time.";
434
  _JSAMPLE *retval = NULL;
435
436
  GET_TJINSTANCE(handle, NULL)
437
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
438
           ERROR_MSG);
439
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
440
441
bailout:
442
  return retval;
443
444
#endif
445
0
}
Unexecuted instantiation: tj3LoadImage8
Unexecuted instantiation: tj3LoadImage12
Unexecuted instantiation: tj3LoadImage16
446
447
448
/* TurboJPEG 3+ */
449
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
450
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
451
   int pitch, int height, int pixelFormat)
452
0
{
453
0
  static const char FUNCTION_NAME[] =
454
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
455
0
  int retval = 0;
456
457
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
458
459
0
  tjhandle handle2 = NULL;
460
0
  tjinstance *this2;
461
0
  j_decompress_ptr dinfo = NULL;
462
0
  djpeg_dest_ptr dst;
463
0
  FILE *file = NULL;
464
0
  const char *ptr = NULL;
465
0
  boolean invert;
466
467
0
  GET_TJINSTANCE(handle, -1)
468
469
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
470
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
471
0
    THROW("Invalid argument");
472
473
  /* The instance handle passed to this function is used only for parameter
474
     retrieval.  Create a new temporary instance to avoid interfering with the
475
     libjpeg state of the primary instance. */
476
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
477
0
    return -1;
478
0
  this2 = (tjinstance *)handle2;
479
0
  dinfo = &this2->dinfo;
480
481
#ifdef _MSC_VER
482
  if (fopen_s(&file, filename, "wb") || file == NULL)
483
#else
484
0
  if ((file = fopen(filename, "wb")) == NULL)
485
0
#endif
486
0
    THROW_UNIX("Cannot open output file");
487
488
0
  CATCH_LIBJPEG2();
489
490
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
491
0
  dinfo->image_width = width;  dinfo->image_height = height;
492
0
  dinfo->global_state = DSTATE_READY;
493
0
  dinfo->scale_num = dinfo->scale_denom = 1;
494
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
495
496
0
  ptr = strrchr(filename, '.');
497
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
498
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
499
0
      THROW("Could not initialize bitmap writer");
500
0
    invert = !this->bottomUp;
501
0
    dinfo->X_density = (UINT16)this->xDensity;
502
0
    dinfo->Y_density = (UINT16)this->yDensity;
503
0
    dinfo->density_unit = (UINT8)this->densityUnits;
504
0
  } else {
505
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
506
0
      THROW("Could not initialize PPM writer");
507
0
    invert = this->bottomUp;
508
0
  }
509
510
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
511
512
0
  dst->output_file = file;
513
0
  (*dst->start_output) (dinfo, dst);
514
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
515
516
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
517
0
  else if (pitch < width * tjPixelSize[pixelFormat])
518
0
    THROW("Invalid argument");
519
520
0
  while (dinfo->output_scanline < dinfo->output_height) {
521
0
    _JSAMPLE *rowptr;
522
523
0
    if (invert)
524
0
      rowptr =
525
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
526
0
    else
527
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
528
0
    memcpy(dst->_buffer[0], rowptr,
529
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
530
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
531
0
    dinfo->output_scanline++;
532
0
  }
533
534
0
  (*dst->finish_output) (dinfo, dst);
535
536
0
bailout:
537
0
  tj3Destroy(handle2);
538
0
  if (file) fclose(file);
539
0
  return retval;
540
541
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
542
543
  GET_TJINSTANCE(handle, -1)
544
  THROW("16-bit data precision requires lossless JPEG,\n"
545
        "which was disabled at build time.")
546
bailout:
547
  return retval;
548
549
#endif
550
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
551
552
553
#undef _JSAMPLE
554
#undef _JSAMPROW
555
#undef _buffer
556
#undef _jinit_read_ppm
557
#undef _jinit_write_ppm
558
#undef _jpeg_crop_scanline
559
#undef _jpeg_read_scanlines
560
#undef _jpeg_skip_scanlines
561
#undef _jpeg_write_scanlines