Coverage Report

Created: 2026-09-13 07:54

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
93.0M
#define _JSAMPLE  JSAMPLE
34
59.1k
#define _JSAMPROW  JSAMPROW
35
92.9M
#define _buffer  buffer
36
48.3k
#define _jinit_read_ppm  jinit_read_ppm
37
0
#define _jinit_write_ppm  jinit_write_ppm
38
2.24k
#define _jpeg_crop_scanline  jpeg_crop_scanline
39
32.3M
#define _jpeg_read_scanlines  jpeg_read_scanlines
40
4.46k
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
41
18.4k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
64.9M
#define _JSAMPLE  J12SAMPLE
44
42.4k
#define _JSAMPROW  J12SAMPROW
45
64.9M
#define _buffer  buffer12
46
31.4k
#define _jinit_read_ppm  j12init_read_ppm
47
0
#define _jinit_write_ppm  j12init_write_ppm
48
1.20k
#define _jpeg_crop_scanline  jpeg12_crop_scanline
49
16.5M
#define _jpeg_read_scanlines  jpeg12_read_scanlines
50
2.38k
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
51
18.4k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
13.8M
#define _JSAMPLE  J16SAMPLE
54
15.9k
#define _JSAMPROW  J16SAMPROW
55
13.8M
#define _buffer  buffer16
56
11.3k
#define _jinit_read_ppm  j16init_read_ppm
57
0
#define _jinit_write_ppm  j16init_write_ppm
58
6.47M
#define _jpeg_read_scanlines  jpeg16_read_scanlines
59
4.79k
#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
226k
#define _GET_STRING(name, suffix)  #name #suffix
65
226k
#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
41.6k
{
75
41.6k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
41.6k
  int i, retval = 0;
77
41.6k
  boolean alloc = TRUE;
78
41.6k
  _JSAMPROW *row_pointer = NULL;
79
80
41.6k
  GET_CINSTANCE(handle)
81
41.6k
  if ((this->init & COMPRESS) == 0)
82
41.6k
    THROW("Instance has not been initialized for compression");
83
84
41.6k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
41.6k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
41.6k
      jpegSize == NULL)
87
41.6k
    THROW("Invalid argument");
88
89
41.6k
  if (!this->lossless && this->quality == -1)
90
41.6k
    THROW("TJPARAM_QUALITY must be specified");
91
41.6k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
41.6k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
41.6k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
41.6k
    THROW("Invalid argument");
97
98
41.6k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
41.6k
    THROW("Memory allocation failure");
100
101
41.6k
  CATCH_LIBJPEG(this);
102
103
41.6k
  cinfo->image_width = width;
104
41.6k
  cinfo->image_height = height;
105
41.6k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
41.6k
  setCompDefaults(this, pixelFormat, FALSE);
108
41.6k
  if (this->noRealloc) {
109
35.5k
    alloc = FALSE;
110
35.5k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
35.5k
  }
112
41.6k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
41.6k
  jpeg_start_compress(cinfo, TRUE);
115
137M
  for (i = 0; i < height; i++) {
116
137M
    if (this->bottomUp)
117
19.8M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
117M
    else
119
117M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
137M
  }
121
83.3k
  while (cinfo->next_scanline < cinfo->image_height)
122
41.6k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
41.6k
                          cinfo->image_height - cinfo->next_scanline);
124
41.6k
  jpeg_finish_compress(cinfo);
125
126
41.6k
bailout:
127
41.6k
  if (cinfo->global_state > CSTATE_START && alloc)
128
33
    (*cinfo->dest->term_destination) (cinfo);
129
41.6k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
33
    jpeg_abort_compress(cinfo);
131
41.6k
  free(row_pointer);
132
41.6k
  if (this->jerr.warning) retval = -1;
133
41.6k
  return retval;
134
41.6k
}
tj3Compress8
Line
Count
Source
74
18.4k
{
75
18.4k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.4k
  int i, retval = 0;
77
18.4k
  boolean alloc = TRUE;
78
18.4k
  _JSAMPROW *row_pointer = NULL;
79
80
18.4k
  GET_CINSTANCE(handle)
81
18.4k
  if ((this->init & COMPRESS) == 0)
82
18.4k
    THROW("Instance has not been initialized for compression");
83
84
18.4k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.4k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.4k
      jpegSize == NULL)
87
18.4k
    THROW("Invalid argument");
88
89
18.4k
  if (!this->lossless && this->quality == -1)
90
18.4k
    THROW("TJPARAM_QUALITY must be specified");
91
18.4k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.4k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.4k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
18.4k
    THROW("Invalid argument");
97
98
18.4k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
18.4k
    THROW("Memory allocation failure");
100
101
18.4k
  CATCH_LIBJPEG(this);
102
103
18.4k
  cinfo->image_width = width;
104
18.4k
  cinfo->image_height = height;
105
18.4k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
18.4k
  setCompDefaults(this, pixelFormat, FALSE);
108
18.4k
  if (this->noRealloc) {
109
15.7k
    alloc = FALSE;
110
15.7k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
15.7k
  }
112
18.4k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
18.4k
  jpeg_start_compress(cinfo, TRUE);
115
58.9M
  for (i = 0; i < height; i++) {
116
58.9M
    if (this->bottomUp)
117
8.57M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
50.3M
    else
119
50.3M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
58.9M
  }
121
36.8k
  while (cinfo->next_scanline < cinfo->image_height)
122
18.4k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
18.4k
                          cinfo->image_height - cinfo->next_scanline);
124
18.4k
  jpeg_finish_compress(cinfo);
125
126
18.4k
bailout:
127
18.4k
  if (cinfo->global_state > CSTATE_START && alloc)
128
0
    (*cinfo->dest->term_destination) (cinfo);
129
18.4k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
0
    jpeg_abort_compress(cinfo);
131
18.4k
  free(row_pointer);
132
18.4k
  if (this->jerr.warning) retval = -1;
133
18.4k
  return retval;
134
18.4k
}
tj3Compress12
Line
Count
Source
74
18.4k
{
75
18.4k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.4k
  int i, retval = 0;
77
18.4k
  boolean alloc = TRUE;
78
18.4k
  _JSAMPROW *row_pointer = NULL;
79
80
18.4k
  GET_CINSTANCE(handle)
81
18.4k
  if ((this->init & COMPRESS) == 0)
82
18.4k
    THROW("Instance has not been initialized for compression");
83
84
18.4k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.4k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.4k
      jpegSize == NULL)
87
18.4k
    THROW("Invalid argument");
88
89
18.4k
  if (!this->lossless && this->quality == -1)
90
18.4k
    THROW("TJPARAM_QUALITY must be specified");
91
18.4k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.4k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.4k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
18.4k
    THROW("Invalid argument");
97
98
18.4k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
18.4k
    THROW("Memory allocation failure");
100
101
18.4k
  CATCH_LIBJPEG(this);
102
103
18.4k
  cinfo->image_width = width;
104
18.4k
  cinfo->image_height = height;
105
18.4k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
18.4k
  setCompDefaults(this, pixelFormat, FALSE);
108
18.4k
  if (this->noRealloc) {
109
15.7k
    alloc = FALSE;
110
15.7k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
15.7k
  }
112
18.4k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
18.4k
  jpeg_start_compress(cinfo, TRUE);
115
64.8M
  for (i = 0; i < height; i++) {
116
64.8M
    if (this->bottomUp)
117
9.26M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
55.5M
    else
119
55.5M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
64.8M
  }
121
36.8k
  while (cinfo->next_scanline < cinfo->image_height)
122
18.4k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
18.4k
                          cinfo->image_height - cinfo->next_scanline);
124
18.4k
  jpeg_finish_compress(cinfo);
125
126
18.4k
bailout:
127
18.4k
  if (cinfo->global_state > CSTATE_START && alloc)
128
0
    (*cinfo->dest->term_destination) (cinfo);
129
18.4k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
0
    jpeg_abort_compress(cinfo);
131
18.4k
  free(row_pointer);
132
18.4k
  if (this->jerr.warning) retval = -1;
133
18.4k
  return retval;
134
18.4k
}
tj3Compress16
Line
Count
Source
74
4.79k
{
75
4.79k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
4.79k
  int i, retval = 0;
77
4.79k
  boolean alloc = TRUE;
78
4.79k
  _JSAMPROW *row_pointer = NULL;
79
80
4.79k
  GET_CINSTANCE(handle)
81
4.79k
  if ((this->init & COMPRESS) == 0)
82
4.79k
    THROW("Instance has not been initialized for compression");
83
84
4.79k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
4.79k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
4.79k
      jpegSize == NULL)
87
4.79k
    THROW("Invalid argument");
88
89
4.79k
  if (!this->lossless && this->quality == -1)
90
4.79k
    THROW("TJPARAM_QUALITY must be specified");
91
4.79k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
4.79k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
4.79k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
4.79k
    THROW("Invalid argument");
97
98
4.79k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
4.79k
    THROW("Memory allocation failure");
100
101
4.79k
  CATCH_LIBJPEG(this);
102
103
4.76k
  cinfo->image_width = width;
104
4.76k
  cinfo->image_height = height;
105
4.76k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
4.76k
  setCompDefaults(this, pixelFormat, FALSE);
108
4.76k
  if (this->noRealloc) {
109
4.10k
    alloc = FALSE;
110
4.10k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
4.10k
  }
112
4.76k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
4.76k
  jpeg_start_compress(cinfo, TRUE);
115
13.8M
  for (i = 0; i < height; i++) {
116
13.8M
    if (this->bottomUp)
117
1.97M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
11.8M
    else
119
11.8M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
13.8M
  }
121
9.56k
  while (cinfo->next_scanline < cinfo->image_height)
122
4.79k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
4.79k
                          cinfo->image_height - cinfo->next_scanline);
124
4.76k
  jpeg_finish_compress(cinfo);
125
126
4.79k
bailout:
127
4.79k
  if (cinfo->global_state > CSTATE_START && alloc)
128
33
    (*cinfo->dest->term_destination) (cinfo);
129
4.79k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
33
    jpeg_abort_compress(cinfo);
131
4.79k
  free(row_pointer);
132
4.79k
  if (this->jerr.warning) retval = -1;
133
4.79k
  return retval;
134
4.76k
}
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
75.8k
{
144
75.8k
  static const char FUNCTION_NAME[] =
145
75.8k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
75.8k
  _JSAMPROW *row_pointer = NULL;
147
75.8k
  int croppedHeight, i, retval = 0;
148
#if BITS_IN_JSAMPLE != 16
149
  int scaledWidth;
150
#endif
151
75.8k
  struct my_progress_mgr progress;
152
153
75.8k
  GET_DINSTANCE(handle);
154
75.8k
  if ((this->init & DECOMPRESS) == 0)
155
75.8k
    THROW("Instance has not been initialized for decompression");
156
157
75.8k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
75.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
75.8k
    THROW("Invalid argument");
160
161
75.8k
  if (this->scanLimit) {
162
75.8k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
75.8k
    progress.pub.progress_monitor = my_progress_monitor;
164
75.8k
    progress.this = this;
165
75.8k
    dinfo->progress = &progress.pub;
166
75.8k
  } else
167
0
    dinfo->progress = NULL;
168
169
75.8k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
75.8k
  CATCH_LIBJPEG(this);
172
173
75.8k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
75.8k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
75.8k
    jpeg_read_header(dinfo, TRUE);
176
75.8k
  }
177
31.9k
  setDecompParameters(this);
178
31.9k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
31.9k
    THROW("Image is too large");
182
31.9k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
#if BITS_IN_JSAMPLE != 16
184
29.5k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
#endif
186
31.9k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
31.9k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
31.9k
  dinfo->scale_num = this->scalingFactor.num;
190
31.9k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
31.9k
  jpeg_start_decompress(dinfo);
193
194
#if BITS_IN_JSAMPLE != 16
195
29.5k
  if (this->croppingRegion.x != 0 ||
196
26.0k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
3.44k
    JDIMENSION crop_x = this->croppingRegion.x;
198
3.44k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
3.44k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
3.44k
    if ((int)crop_x != this->croppingRegion.x)
202
9
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
3.44k
             "actual (%d) cropping region left boundary",
204
3.44k
             this->croppingRegion.x, (int)crop_x);
205
3.43k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
3.43k
             "actual (%d) cropping region width",
208
3.43k
             this->croppingRegion.w, (int)crop_w);
209
3.43k
  }
210
29.5k
#endif
211
212
31.9k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
31.9k
    THROW("Invalid argument");
215
216
31.9k
  croppedHeight = dinfo->output_height;
217
#if BITS_IN_JSAMPLE != 16
218
29.5k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
3.43k
    croppedHeight = this->croppingRegion.h;
220
#endif
221
31.9k
  if ((row_pointer =
222
31.9k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
31.9k
    THROW("Memory allocation failure");
224
31.9k
  CATCH_LIBJPEG(this);
225
128M
  for (i = 0; i < (int)croppedHeight; i++) {
226
128M
    if (this->bottomUp)
227
41.0M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
87.8M
    else
229
87.8M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
128M
  }
231
232
#if BITS_IN_JSAMPLE != 16
233
26.0k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
3.43k
    if (this->croppingRegion.y != 0) {
235
3.43k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
3.43k
      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.43k
               this->croppingRegion.y, (int)lines);
241
3.43k
    }
242
133k
    while ((int)dinfo->output_scanline <
243
133k
           this->croppingRegion.y + this->croppingRegion.h)
244
129k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
129k
                                               this->croppingRegion.y],
246
129k
                           this->croppingRegion.y + this->croppingRegion.h -
247
129k
                           dinfo->output_scanline);
248
3.43k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
3.43k
        (int)dinfo->output_height) {
250
3.41k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
                                                     this->croppingRegion.y -
252
                                                     this->croppingRegion.h);
253
254
3.41k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
3.41k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
3.41k
               "actual (%d) cropping region lower boundary",
258
3.41k
               this->croppingRegion.y + this->croppingRegion.h,
259
3.41k
               (int)(dinfo->output_height - lines));
260
3.41k
    }
261
3.43k
  } else
262
21.9k
#endif
263
21.9k
  {
264
55.2M
    while (dinfo->output_scanline < dinfo->output_height)
265
55.2M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
55.2M
                           dinfo->output_height - dinfo->output_scanline);
267
21.9k
  }
268
25.4k
  jpeg_finish_decompress(dinfo);
269
270
75.8k
bailout:
271
75.8k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
75.8k
  free(row_pointer);
273
75.8k
  if (this->jerr.warning) retval = -1;
274
75.8k
  return retval;
275
25.4k
}
tj3Decompress8
Line
Count
Source
143
40.7k
{
144
40.7k
  static const char FUNCTION_NAME[] =
145
40.7k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
40.7k
  _JSAMPROW *row_pointer = NULL;
147
40.7k
  int croppedHeight, i, retval = 0;
148
40.7k
#if BITS_IN_JSAMPLE != 16
149
40.7k
  int scaledWidth;
150
40.7k
#endif
151
40.7k
  struct my_progress_mgr progress;
152
153
40.7k
  GET_DINSTANCE(handle);
154
40.7k
  if ((this->init & DECOMPRESS) == 0)
155
40.7k
    THROW("Instance has not been initialized for decompression");
156
157
40.7k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
40.7k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
40.7k
    THROW("Invalid argument");
160
161
40.7k
  if (this->scanLimit) {
162
40.7k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
40.7k
    progress.pub.progress_monitor = my_progress_monitor;
164
40.7k
    progress.this = this;
165
40.7k
    dinfo->progress = &progress.pub;
166
40.7k
  } else
167
0
    dinfo->progress = NULL;
168
169
40.7k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
40.7k
  CATCH_LIBJPEG(this);
172
173
40.7k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
40.7k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
40.7k
    jpeg_read_header(dinfo, TRUE);
176
40.7k
  }
177
18.2k
  setDecompParameters(this);
178
18.2k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
18.2k
    THROW("Image is too large");
182
18.2k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
18.2k
#if BITS_IN_JSAMPLE != 16
184
18.2k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
18.2k
#endif
186
18.2k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
18.2k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
18.2k
  dinfo->scale_num = this->scalingFactor.num;
190
18.2k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
18.2k
  jpeg_start_decompress(dinfo);
193
194
18.2k
#if BITS_IN_JSAMPLE != 16
195
18.2k
  if (this->croppingRegion.x != 0 ||
196
16.0k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
2.24k
    JDIMENSION crop_x = this->croppingRegion.x;
198
2.24k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
2.24k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
2.24k
    if ((int)crop_x != this->croppingRegion.x)
202
6
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
2.24k
             "actual (%d) cropping region left boundary",
204
2.24k
             this->croppingRegion.x, (int)crop_x);
205
2.23k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
2.23k
             "actual (%d) cropping region width",
208
2.23k
             this->croppingRegion.w, (int)crop_w);
209
2.23k
  }
210
18.2k
#endif
211
212
18.2k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
18.2k
    THROW("Invalid argument");
215
216
18.2k
  croppedHeight = dinfo->output_height;
217
18.2k
#if BITS_IN_JSAMPLE != 16
218
18.2k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
2.23k
    croppedHeight = this->croppingRegion.h;
220
18.2k
#endif
221
18.2k
  if ((row_pointer =
222
18.2k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
18.2k
    THROW("Memory allocation failure");
224
18.2k
  CATCH_LIBJPEG(this);
225
68.6M
  for (i = 0; i < (int)croppedHeight; i++) {
226
68.6M
    if (this->bottomUp)
227
23.1M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
45.5M
    else
229
45.5M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
68.6M
  }
231
232
15.7k
#if BITS_IN_JSAMPLE != 16
233
16.0k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
2.23k
    if (this->croppingRegion.y != 0) {
235
2.23k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
2.23k
      if ((int)lines != this->croppingRegion.y)
238
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
2.23k
               "actual (%d) cropping region upper boundary",
240
2.23k
               this->croppingRegion.y, (int)lines);
241
2.23k
    }
242
89.5k
    while ((int)dinfo->output_scanline <
243
89.5k
           this->croppingRegion.y + this->croppingRegion.h)
244
87.2k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
87.2k
                                               this->croppingRegion.y],
246
87.2k
                           this->croppingRegion.y + this->croppingRegion.h -
247
87.2k
                           dinfo->output_scanline);
248
2.23k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
2.23k
        (int)dinfo->output_height) {
250
2.22k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
2.22k
                                                     this->croppingRegion.y -
252
2.22k
                                                     this->croppingRegion.h);
253
254
2.22k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
2.22k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
2.22k
               "actual (%d) cropping region lower boundary",
258
2.22k
               this->croppingRegion.y + this->croppingRegion.h,
259
2.22k
               (int)(dinfo->output_height - lines));
260
2.22k
    }
261
2.23k
  } else
262
13.5k
#endif
263
13.5k
  {
264
32.2M
    while (dinfo->output_scanline < dinfo->output_height)
265
32.2M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
32.2M
                           dinfo->output_height - dinfo->output_scanline);
267
13.5k
  }
268
15.7k
  jpeg_finish_decompress(dinfo);
269
270
40.7k
bailout:
271
40.7k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
40.7k
  free(row_pointer);
273
40.7k
  if (this->jerr.warning) retval = -1;
274
40.7k
  return retval;
275
15.7k
}
tj3Decompress12
Line
Count
Source
143
23.9k
{
144
23.9k
  static const char FUNCTION_NAME[] =
145
23.9k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
23.9k
  _JSAMPROW *row_pointer = NULL;
147
23.9k
  int croppedHeight, i, retval = 0;
148
23.9k
#if BITS_IN_JSAMPLE != 16
149
23.9k
  int scaledWidth;
150
23.9k
#endif
151
23.9k
  struct my_progress_mgr progress;
152
153
23.9k
  GET_DINSTANCE(handle);
154
23.9k
  if ((this->init & DECOMPRESS) == 0)
155
23.9k
    THROW("Instance has not been initialized for decompression");
156
157
23.9k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
23.9k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
23.9k
    THROW("Invalid argument");
160
161
23.9k
  if (this->scanLimit) {
162
23.9k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
23.9k
    progress.pub.progress_monitor = my_progress_monitor;
164
23.9k
    progress.this = this;
165
23.9k
    dinfo->progress = &progress.pub;
166
23.9k
  } else
167
0
    dinfo->progress = NULL;
168
169
23.9k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
23.9k
  CATCH_LIBJPEG(this);
172
173
23.9k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
23.9k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
23.9k
    jpeg_read_header(dinfo, TRUE);
176
23.9k
  }
177
11.2k
  setDecompParameters(this);
178
11.2k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
11.2k
    THROW("Image is too large");
182
11.2k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
11.2k
#if BITS_IN_JSAMPLE != 16
184
11.2k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
11.2k
#endif
186
11.2k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
11.2k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
11.2k
  dinfo->scale_num = this->scalingFactor.num;
190
11.2k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
11.2k
  jpeg_start_decompress(dinfo);
193
194
11.2k
#if BITS_IN_JSAMPLE != 16
195
11.2k
  if (this->croppingRegion.x != 0 ||
196
10.0k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
1.20k
    JDIMENSION crop_x = this->croppingRegion.x;
198
1.20k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
1.20k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
1.20k
    if ((int)crop_x != this->croppingRegion.x)
202
3
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
1.20k
             "actual (%d) cropping region left boundary",
204
1.20k
             this->croppingRegion.x, (int)crop_x);
205
1.19k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
1.19k
             "actual (%d) cropping region width",
208
1.19k
             this->croppingRegion.w, (int)crop_w);
209
1.19k
  }
210
11.2k
#endif
211
212
11.2k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
11.2k
    THROW("Invalid argument");
215
216
11.2k
  croppedHeight = dinfo->output_height;
217
11.2k
#if BITS_IN_JSAMPLE != 16
218
11.2k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
1.19k
    croppedHeight = this->croppingRegion.h;
220
11.2k
#endif
221
11.2k
  if ((row_pointer =
222
11.2k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
11.2k
    THROW("Memory allocation failure");
224
11.2k
  CATCH_LIBJPEG(this);
225
35.8M
  for (i = 0; i < (int)croppedHeight; i++) {
226
35.7M
    if (this->bottomUp)
227
12.3M
      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
35.7M
  }
231
232
9.67k
#if BITS_IN_JSAMPLE != 16
233
10.0k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
1.19k
    if (this->croppingRegion.y != 0) {
235
1.19k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
1.19k
      if ((int)lines != this->croppingRegion.y)
238
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
1.19k
               "actual (%d) cropping region upper boundary",
240
1.19k
               this->croppingRegion.y, (int)lines);
241
1.19k
    }
242
43.5k
    while ((int)dinfo->output_scanline <
243
43.5k
           this->croppingRegion.y + this->croppingRegion.h)
244
42.3k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
42.3k
                                               this->croppingRegion.y],
246
42.3k
                           this->croppingRegion.y + this->croppingRegion.h -
247
42.3k
                           dinfo->output_scanline);
248
1.19k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
1.19k
        (int)dinfo->output_height) {
250
1.18k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
1.18k
                                                     this->croppingRegion.y -
252
1.18k
                                                     this->croppingRegion.h);
253
254
1.18k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
1.18k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
1.18k
               "actual (%d) cropping region lower boundary",
258
1.18k
               this->croppingRegion.y + this->croppingRegion.h,
259
1.18k
               (int)(dinfo->output_height - lines));
260
1.18k
    }
261
1.19k
  } else
262
8.47k
#endif
263
8.47k
  {
264
16.4M
    while (dinfo->output_scanline < dinfo->output_height)
265
16.4M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
16.4M
                           dinfo->output_height - dinfo->output_scanline);
267
8.47k
  }
268
9.67k
  jpeg_finish_decompress(dinfo);
269
270
23.9k
bailout:
271
23.9k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
23.9k
  free(row_pointer);
273
23.9k
  if (this->jerr.warning) retval = -1;
274
23.9k
  return retval;
275
9.67k
}
tj3Decompress16
Line
Count
Source
143
11.1k
{
144
11.1k
  static const char FUNCTION_NAME[] =
145
11.1k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
11.1k
  _JSAMPROW *row_pointer = NULL;
147
11.1k
  int croppedHeight, i, retval = 0;
148
#if BITS_IN_JSAMPLE != 16
149
  int scaledWidth;
150
#endif
151
11.1k
  struct my_progress_mgr progress;
152
153
11.1k
  GET_DINSTANCE(handle);
154
11.1k
  if ((this->init & DECOMPRESS) == 0)
155
11.1k
    THROW("Instance has not been initialized for decompression");
156
157
11.1k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
11.1k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
11.1k
    THROW("Invalid argument");
160
161
11.1k
  if (this->scanLimit) {
162
11.1k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
11.1k
    progress.pub.progress_monitor = my_progress_monitor;
164
11.1k
    progress.this = this;
165
11.1k
    dinfo->progress = &progress.pub;
166
11.1k
  } else
167
0
    dinfo->progress = NULL;
168
169
11.1k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
11.1k
  CATCH_LIBJPEG(this);
172
173
11.1k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
11.1k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
11.1k
    jpeg_read_header(dinfo, TRUE);
176
11.1k
  }
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
24.4M
  for (i = 0; i < (int)croppedHeight; i++) {
226
24.4M
    if (this->bottomUp)
227
5.50M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
18.9M
    else
229
18.9M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
24.4M
  }
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.37k
  {
264
6.47M
    while (dinfo->output_scanline < dinfo->output_height)
265
6.47M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
6.47M
                           dinfo->output_height - dinfo->output_scanline);
267
1.37k
  }
268
1.37k
  jpeg_finish_decompress(dinfo);
269
270
11.1k
bailout:
271
11.1k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
11.1k
  free(row_pointer);
273
11.1k
  if (this->jerr.warning) retval = -1;
274
11.1k
  return retval;
275
1.37k
}
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
108k
{
290
108k
  static const char FUNCTION_NAME[] =
291
108k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
108k
  int retval = 0, tempc;
294
108k
  size_t pitch;
295
108k
  tjhandle handle2 = NULL;
296
108k
  tjinstance *this2;
297
108k
  j_compress_ptr cinfo = NULL;
298
108k
  cjpeg_source_ptr src;
299
108k
  _JSAMPLE *dstBuf = NULL;
300
108k
  boolean invert;
301
302
108k
  GET_TJINSTANCE(handle, NULL)
303
304
108k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
108k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
108k
    THROW("Invalid argument");
307
108k
  if ((align & (align - 1)) != 0)
308
108k
    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
108k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
108k
  this2 = (tjinstance *)handle2;
315
108k
  cinfo = &this2->cinfo;
316
317
108k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
108k
  else if (tempc == EOF)
320
108k
    THROW("Input file contains no data");
321
322
108k
  CATCH_LIBJPEG2();
323
324
84.4k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
84.4k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
84.4k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
84.4k
  if (tempc == 'B') {
328
17.0k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
17.0k
      THROW("Could not initialize bitmap loader");
330
17.0k
    invert = !this->bottomUp;
331
91.0k
  } else if (tempc == 'P') {
332
91.0k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
91.0k
      THROW("Could not initialize PPM loader");
334
91.0k
    invert = this->bottomUp;
335
91.0k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
108k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
108k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
108k
  src->max_pixels = this->maxPixels;
343
108k
  (*src->start_input) (cinfo, src);
344
108k
  if (tempc == 'B') {
345
4.95k
    if (cinfo->X_density && cinfo->Y_density) {
346
1.13k
      this->xDensity = cinfo->X_density;
347
1.13k
      this->yDensity = cinfo->Y_density;
348
1.13k
      this->densityUnits = cinfo->density_unit;
349
1.13k
    }
350
4.95k
  }
351
108k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
108k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
108k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
108k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
108k
  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
108k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
108k
                                   sizeof(_JSAMPLE))) == NULL)
366
108k
    THROW("Memory allocation failure");
367
368
108k
  CATCH_LIBJPEG2();
369
370
171M
  while (cinfo->next_scanline < cinfo->image_height) {
371
171M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
343M
    for (i = 0; i < nlines; i++) {
374
171M
      _JSAMPLE *dstptr;
375
171M
      int row;
376
377
171M
      row = cinfo->next_scanline + i;
378
171M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
142M
      else dstptr = &dstBuf[row * pitch];
380
171M
      memcpy(dstptr, src->_buffer[i],
381
171M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
171M
    }
383
171M
    cinfo->next_scanline += nlines;
384
171M
  }
385
386
77.2k
  (*src->finish_input) (cinfo, src);
387
388
108k
bailout:
389
108k
  tj3Destroy(handle2);
390
108k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
108k
  return dstBuf;
392
77.2k
}
_tj3LoadImageFromFileHandle8
Line
Count
Source
289
65.7k
{
290
65.7k
  static const char FUNCTION_NAME[] =
291
65.7k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
65.7k
  int retval = 0, tempc;
294
65.7k
  size_t pitch;
295
65.7k
  tjhandle handle2 = NULL;
296
65.7k
  tjinstance *this2;
297
65.7k
  j_compress_ptr cinfo = NULL;
298
65.7k
  cjpeg_source_ptr src;
299
65.7k
  _JSAMPLE *dstBuf = NULL;
300
65.7k
  boolean invert;
301
302
65.7k
  GET_TJINSTANCE(handle, NULL)
303
304
65.7k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
65.7k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
65.7k
    THROW("Invalid argument");
307
65.7k
  if ((align & (align - 1)) != 0)
308
65.7k
    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
65.7k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
65.7k
  this2 = (tjinstance *)handle2;
315
65.7k
  cinfo = &this2->cinfo;
316
317
65.7k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
65.7k
  else if (tempc == EOF)
320
65.7k
    THROW("Input file contains no data");
321
322
65.7k
  CATCH_LIBJPEG2();
323
324
47.5k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
47.5k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
47.5k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
47.5k
  if (tempc == 'B') {
328
17.0k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
17.0k
      THROW("Could not initialize bitmap loader");
330
17.0k
    invert = !this->bottomUp;
331
48.3k
  } else if (tempc == 'P') {
332
48.3k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
48.3k
      THROW("Could not initialize PPM loader");
334
48.3k
    invert = this->bottomUp;
335
48.3k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
65.4k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
65.4k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
65.4k
  src->max_pixels = this->maxPixels;
343
65.4k
  (*src->start_input) (cinfo, src);
344
65.4k
  if (tempc == 'B') {
345
4.95k
    if (cinfo->X_density && cinfo->Y_density) {
346
1.13k
      this->xDensity = cinfo->X_density;
347
1.13k
      this->yDensity = cinfo->Y_density;
348
1.13k
      this->densityUnits = cinfo->density_unit;
349
1.13k
    }
350
4.95k
  }
351
65.4k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
65.4k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
65.4k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
65.4k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
65.4k
  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
65.4k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
65.4k
                                   sizeof(_JSAMPLE))) == NULL)
366
65.4k
    THROW("Memory allocation failure");
367
368
65.4k
  CATCH_LIBJPEG2();
369
370
93.0M
  while (cinfo->next_scanline < cinfo->image_height) {
371
93.0M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
186M
    for (i = 0; i < nlines; i++) {
374
92.9M
      _JSAMPLE *dstptr;
375
92.9M
      int row;
376
377
92.9M
      row = cinfo->next_scanline + i;
378
92.9M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
74.8M
      else dstptr = &dstBuf[row * pitch];
380
92.9M
      memcpy(dstptr, src->_buffer[i],
381
92.9M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
92.9M
    }
383
93.0M
    cinfo->next_scanline += nlines;
384
93.0M
  }
385
386
47.8k
  (*src->finish_input) (cinfo, src);
387
388
65.7k
bailout:
389
65.7k
  tj3Destroy(handle2);
390
65.7k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
65.7k
  return dstBuf;
392
47.8k
}
_tj3LoadImageFromFileHandle12
Line
Count
Source
289
31.6k
{
290
31.6k
  static const char FUNCTION_NAME[] =
291
31.6k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
31.6k
  int retval = 0, tempc;
294
31.6k
  size_t pitch;
295
31.6k
  tjhandle handle2 = NULL;
296
31.6k
  tjinstance *this2;
297
31.6k
  j_compress_ptr cinfo = NULL;
298
31.6k
  cjpeg_source_ptr src;
299
31.6k
  _JSAMPLE *dstBuf = NULL;
300
31.6k
  boolean invert;
301
302
31.6k
  GET_TJINSTANCE(handle, NULL)
303
304
31.6k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
31.6k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
31.6k
    THROW("Invalid argument");
307
31.6k
  if ((align & (align - 1)) != 0)
308
31.6k
    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
31.6k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
31.6k
  this2 = (tjinstance *)handle2;
315
31.6k
  cinfo = &this2->cinfo;
316
317
31.6k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
31.6k
  else if (tempc == EOF)
320
31.6k
    THROW("Input file contains no data");
321
322
31.6k
  CATCH_LIBJPEG2();
323
324
27.4k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
27.4k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
27.4k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
27.4k
  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
31.4k
  } else if (tempc == 'P') {
332
31.4k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
31.4k
      THROW("Could not initialize PPM loader");
334
31.4k
    invert = this->bottomUp;
335
31.4k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
31.4k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
31.4k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
31.4k
  src->max_pixels = this->maxPixels;
343
31.4k
  (*src->start_input) (cinfo, src);
344
31.4k
  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
31.4k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
31.4k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
31.4k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
31.4k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
31.4k
  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
31.4k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
31.4k
                                   sizeof(_JSAMPLE))) == NULL)
366
31.4k
    THROW("Memory allocation failure");
367
368
31.4k
  CATCH_LIBJPEG2();
369
370
64.9M
  while (cinfo->next_scanline < cinfo->image_height) {
371
64.9M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
129M
    for (i = 0; i < nlines; i++) {
374
64.9M
      _JSAMPLE *dstptr;
375
64.9M
      int row;
376
377
64.9M
      row = cinfo->next_scanline + i;
378
64.9M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
55.6M
      else dstptr = &dstBuf[row * pitch];
380
64.9M
      memcpy(dstptr, src->_buffer[i],
381
64.9M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
64.9M
    }
383
64.9M
    cinfo->next_scanline += nlines;
384
64.9M
  }
385
386
22.6k
  (*src->finish_input) (cinfo, src);
387
388
31.6k
bailout:
389
31.6k
  tj3Destroy(handle2);
390
31.6k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
31.6k
  return dstBuf;
392
22.6k
}
_tj3LoadImageFromFileHandle16
Line
Count
Source
289
11.4k
{
290
11.4k
  static const char FUNCTION_NAME[] =
291
11.4k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
11.4k
  int retval = 0, tempc;
294
11.4k
  size_t pitch;
295
11.4k
  tjhandle handle2 = NULL;
296
11.4k
  tjinstance *this2;
297
11.4k
  j_compress_ptr cinfo = NULL;
298
11.4k
  cjpeg_source_ptr src;
299
11.4k
  _JSAMPLE *dstBuf = NULL;
300
11.4k
  boolean invert;
301
302
11.4k
  GET_TJINSTANCE(handle, NULL)
303
304
11.4k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
11.4k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
11.4k
    THROW("Invalid argument");
307
11.4k
  if ((align & (align - 1)) != 0)
308
11.4k
    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.4k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
11.4k
  this2 = (tjinstance *)handle2;
315
11.4k
  cinfo = &this2->cinfo;
316
317
11.4k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
11.4k
  else if (tempc == EOF)
320
11.4k
    THROW("Input file contains no data");
321
322
11.4k
  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.3k
  } else if (tempc == 'P') {
332
11.3k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
11.3k
      THROW("Could not initialize PPM loader");
334
11.3k
    invert = this->bottomUp;
335
11.3k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
11.3k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
11.3k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
11.3k
  src->max_pixels = this->maxPixels;
343
11.3k
  (*src->start_input) (cinfo, src);
344
11.3k
  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.3k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
11.3k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
11.3k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
11.3k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
11.3k
  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.3k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
11.3k
                                   sizeof(_JSAMPLE))) == NULL)
366
11.3k
    THROW("Memory allocation failure");
367
368
11.3k
  CATCH_LIBJPEG2();
369
370
13.8M
  while (cinfo->next_scanline < cinfo->image_height) {
371
13.8M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
27.7M
    for (i = 0; i < nlines; i++) {
374
13.8M
      _JSAMPLE *dstptr;
375
13.8M
      int row;
376
377
13.8M
      row = cinfo->next_scanline + i;
378
13.8M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
11.8M
      else dstptr = &dstBuf[row * pitch];
380
13.8M
      memcpy(dstptr, src->_buffer[i],
381
13.8M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
13.8M
    }
383
13.8M
    cinfo->next_scanline += nlines;
384
13.8M
  }
385
386
6.78k
  (*src->finish_input) (cinfo, src);
387
388
11.4k
bailout:
389
11.4k
  tj3Destroy(handle2);
390
11.4k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
11.4k
  return dstBuf;
392
6.78k
}
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