Coverage Report

Created: 2023-06-07 06:03

/src/libjpeg-turbo.main/turbojpeg-mp.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C)2009-2023 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
101M
#define _JSAMPLE  JSAMPLE
34
21.3k
#define _JSAMPROW  JSAMPROW
35
101M
#define _buffer  buffer
36
47.7k
#define _jinit_read_ppm  jinit_read_ppm
37
0
#define _jinit_write_ppm  jinit_write_ppm
38
0
#define _jpeg_crop_scanline  jpeg_crop_scanline
39
6.56M
#define _jpeg_read_scanlines  jpeg_read_scanlines
40
0
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
41
18.0k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
103M
#define _JSAMPLE  J12SAMPLE
44
30.0k
#define _JSAMPROW  J12SAMPROW
45
103M
#define _buffer  buffer12
46
39.4k
#define _jinit_read_ppm  j12init_read_ppm
47
0
#define _jinit_write_ppm  j12init_write_ppm
48
0
#define _jpeg_crop_scanline  jpeg12_crop_scanline
49
5.06M
#define _jpeg_read_scanlines  jpeg12_read_scanlines
50
0
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
51
25.9k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
15.2M
#define _JSAMPLE  J16SAMPLE
54
5.37k
#define _JSAMPROW  J16SAMPROW
55
15.2M
#define _buffer  buffer16
56
11.2k
#define _jinit_read_ppm  j16init_read_ppm
57
0
#define _jinit_write_ppm  j16init_write_ppm
58
247k
#define _jpeg_read_scanlines  jpeg16_read_scanlines
59
4.74k
#define _jpeg_write_scanlines  jpeg16_write_scanlines
60
#endif
61
62
#define _GET_NAME(name, suffix)  name##suffix
63
#define GET_NAME(name, suffix)  _GET_NAME(name, suffix)
64
174k
#define _GET_STRING(name, suffix)  #name #suffix
65
174k
#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
49.0k
{
75
49.0k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
49.0k
  int i, retval = 0;
77
49.0k
  boolean alloc = TRUE;
78
49.0k
  _JSAMPROW *row_pointer = NULL;
79
80
49.0k
  GET_CINSTANCE(handle)
81
49.0k
  if ((this->init & COMPRESS) == 0)
82
49.0k
    THROW("Instance has not been initialized for compression");
83
84
49.0k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
49.0k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
49.0k
      jpegSize == NULL)
87
49.0k
    THROW("Invalid argument");
88
89
49.0k
  if (!this->lossless && this->quality == -1)
90
49.0k
    THROW("TJPARAM_QUALITY must be specified");
91
49.0k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
49.0k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
49.0k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
49.0k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
49.0k
    THROW("Memory allocation failure");
98
99
49.0k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
282
    retval = -1;  goto bailout;
102
282
  }
103
104
48.7k
  cinfo->image_width = width;
105
48.7k
  cinfo->image_height = height;
106
48.7k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
48.7k
  setCompDefaults(this, pixelFormat);
109
48.7k
  if (this->noRealloc) {
110
41.8k
    alloc = FALSE;
111
41.8k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
41.8k
  }
113
48.7k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
48.7k
  jpeg_start_compress(cinfo, TRUE);
116
180M
  for (i = 0; i < height; i++) {
117
179M
    if (this->bottomUp)
118
25.9M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
154M
    else
120
154M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
179M
  }
122
97.5k
  while (cinfo->next_scanline < cinfo->image_height)
123
48.8k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
48.8k
                          cinfo->image_height - cinfo->next_scanline);
125
48.7k
  jpeg_finish_compress(cinfo);
126
127
49.0k
bailout:
128
49.0k
  if (cinfo->global_state > CSTATE_START) {
129
84
    if (alloc) (*cinfo->dest->term_destination) (cinfo);
130
84
    jpeg_abort_compress(cinfo);
131
84
  }
132
49.0k
  free(row_pointer);
133
49.0k
  if (this->jerr.warning) retval = -1;
134
49.0k
  return retval;
135
48.7k
}
tj3Compress8
Line
Count
Source
74
18.1k
{
75
18.1k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.1k
  int i, retval = 0;
77
18.1k
  boolean alloc = TRUE;
78
18.1k
  _JSAMPROW *row_pointer = NULL;
79
80
18.1k
  GET_CINSTANCE(handle)
81
18.1k
  if ((this->init & COMPRESS) == 0)
82
18.1k
    THROW("Instance has not been initialized for compression");
83
84
18.1k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.1k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.1k
      jpegSize == NULL)
87
18.1k
    THROW("Invalid argument");
88
89
18.1k
  if (!this->lossless && this->quality == -1)
90
18.1k
    THROW("TJPARAM_QUALITY must be specified");
91
18.1k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.1k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.1k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
18.1k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
18.1k
    THROW("Memory allocation failure");
98
99
18.1k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
110
    retval = -1;  goto bailout;
102
110
  }
103
104
18.0k
  cinfo->image_width = width;
105
18.0k
  cinfo->image_height = height;
106
18.0k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
18.0k
  setCompDefaults(this, pixelFormat);
109
18.0k
  if (this->noRealloc) {
110
15.4k
    alloc = FALSE;
111
15.4k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
15.4k
  }
113
18.0k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
18.0k
  jpeg_start_compress(cinfo, TRUE);
116
64.4M
  for (i = 0; i < height; i++) {
117
64.4M
    if (this->bottomUp)
118
9.40M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
55.0M
    else
120
55.0M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
64.4M
  }
122
36.1k
  while (cinfo->next_scanline < cinfo->image_height)
123
18.0k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
18.0k
                          cinfo->image_height - cinfo->next_scanline);
125
18.0k
  jpeg_finish_compress(cinfo);
126
127
18.1k
bailout:
128
18.1k
  if (cinfo->global_state > CSTATE_START) {
129
17
    if (alloc) (*cinfo->dest->term_destination) (cinfo);
130
17
    jpeg_abort_compress(cinfo);
131
17
  }
132
18.1k
  free(row_pointer);
133
18.1k
  if (this->jerr.warning) retval = -1;
134
18.1k
  return retval;
135
18.0k
}
tj3Compress12
Line
Count
Source
74
26.0k
{
75
26.0k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
26.0k
  int i, retval = 0;
77
26.0k
  boolean alloc = TRUE;
78
26.0k
  _JSAMPROW *row_pointer = NULL;
79
80
26.0k
  GET_CINSTANCE(handle)
81
26.0k
  if ((this->init & COMPRESS) == 0)
82
26.0k
    THROW("Instance has not been initialized for compression");
83
84
26.0k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
26.0k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
26.0k
      jpegSize == NULL)
87
26.0k
    THROW("Invalid argument");
88
89
26.0k
  if (!this->lossless && this->quality == -1)
90
26.0k
    THROW("TJPARAM_QUALITY must be specified");
91
26.0k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
26.0k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
26.0k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
26.0k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
26.0k
    THROW("Memory allocation failure");
98
99
26.0k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
70
    retval = -1;  goto bailout;
102
70
  }
103
104
25.9k
  cinfo->image_width = width;
105
25.9k
  cinfo->image_height = height;
106
25.9k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
25.9k
  setCompDefaults(this, pixelFormat);
109
25.9k
  if (this->noRealloc) {
110
22.2k
    alloc = FALSE;
111
22.2k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
22.2k
  }
113
25.9k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
25.9k
  jpeg_start_compress(cinfo, TRUE);
116
101M
  for (i = 0; i < height; i++) {
117
101M
    if (this->bottomUp)
118
14.5M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
87.1M
    else
120
87.1M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
101M
  }
122
51.9k
  while (cinfo->next_scanline < cinfo->image_height)
123
25.9k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
25.9k
                          cinfo->image_height - cinfo->next_scanline);
125
25.9k
  jpeg_finish_compress(cinfo);
126
127
26.0k
bailout:
128
26.0k
  if (cinfo->global_state > CSTATE_START) {
129
0
    if (alloc) (*cinfo->dest->term_destination) (cinfo);
130
0
    jpeg_abort_compress(cinfo);
131
0
  }
132
26.0k
  free(row_pointer);
133
26.0k
  if (this->jerr.warning) retval = -1;
134
26.0k
  return retval;
135
25.9k
}
tj3Compress16
Line
Count
Source
74
4.78k
{
75
4.78k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
4.78k
  int i, retval = 0;
77
4.78k
  boolean alloc = TRUE;
78
4.78k
  _JSAMPROW *row_pointer = NULL;
79
80
4.78k
  GET_CINSTANCE(handle)
81
4.78k
  if ((this->init & COMPRESS) == 0)
82
4.78k
    THROW("Instance has not been initialized for compression");
83
84
4.78k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
4.78k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
4.78k
      jpegSize == NULL)
87
4.78k
    THROW("Invalid argument");
88
89
4.78k
  if (!this->lossless && this->quality == -1)
90
4.78k
    THROW("TJPARAM_QUALITY must be specified");
91
4.78k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
4.78k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
4.78k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
4.78k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
4.78k
    THROW("Memory allocation failure");
98
99
4.78k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
102
    retval = -1;  goto bailout;
102
102
  }
103
104
4.67k
  cinfo->image_width = width;
105
4.67k
  cinfo->image_height = height;
106
4.67k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
4.67k
  setCompDefaults(this, pixelFormat);
109
4.67k
  if (this->noRealloc) {
110
4.08k
    alloc = FALSE;
111
4.08k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
4.08k
  }
113
4.67k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
4.67k
  jpeg_start_compress(cinfo, TRUE);
116
13.8M
  for (i = 0; i < height; i++) {
117
13.8M
    if (this->bottomUp)
118
2.00M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
11.8M
    else
120
11.8M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
13.8M
  }
122
9.42k
  while (cinfo->next_scanline < cinfo->image_height)
123
4.74k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
4.74k
                          cinfo->image_height - cinfo->next_scanline);
125
4.67k
  jpeg_finish_compress(cinfo);
126
127
4.78k
bailout:
128
4.78k
  if (cinfo->global_state > CSTATE_START) {
129
67
    if (alloc) (*cinfo->dest->term_destination) (cinfo);
130
67
    jpeg_abort_compress(cinfo);
131
67
  }
132
4.78k
  free(row_pointer);
133
4.78k
  if (this->jerr.warning) retval = -1;
134
4.78k
  return retval;
135
4.67k
}
136
137
138
/******************************* Decompressor ********************************/
139
140
/* TurboJPEG 3+ */
141
DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE)
142
  (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize,
143
   _JSAMPLE *dstBuf, int pitch, int pixelFormat)
144
7.81k
{
145
7.81k
  static const char FUNCTION_NAME[] =
146
7.81k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
7.81k
  _JSAMPROW *row_pointer = NULL;
148
7.81k
  int croppedHeight, i, retval = 0;
149
#if BITS_IN_JSAMPLE != 16
150
  int scaledWidth;
151
#endif
152
7.81k
  struct my_progress_mgr progress;
153
154
7.81k
  GET_DINSTANCE(handle);
155
7.81k
  if ((this->init & DECOMPRESS) == 0)
156
7.81k
    THROW("Instance has not been initialized for decompression");
157
158
7.81k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
7.81k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
7.81k
    THROW("Invalid argument");
161
162
7.81k
  if (this->scanLimit) {
163
7.81k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
7.81k
    progress.pub.progress_monitor = my_progress_monitor;
165
7.81k
    progress.this = this;
166
7.81k
    dinfo->progress = &progress.pub;
167
7.81k
  } else
168
0
    dinfo->progress = NULL;
169
170
7.81k
  if (setjmp(this->jerr.setjmp_buffer)) {
171
    /* If we get here, the JPEG code has signaled an error. */
172
1.69k
    retval = -1;  goto bailout;
173
1.69k
  }
174
175
7.81k
  if (dinfo->global_state <= DSTATE_START) {
176
7.81k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
177
7.81k
    jpeg_read_header(dinfo, TRUE);
178
7.81k
  }
179
6.11k
  setDecompParameters(this);
180
6.11k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
181
#if BITS_IN_JSAMPLE != 16
182
5.67k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
183
#endif
184
6.11k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
185
6.11k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
186
187
6.11k
  dinfo->scale_num = this->scalingFactor.num;
188
6.11k
  dinfo->scale_denom = this->scalingFactor.denom;
189
190
6.11k
  jpeg_start_decompress(dinfo);
191
192
#if BITS_IN_JSAMPLE != 16
193
5.67k
  if (this->croppingRegion.x != 0 ||
194
5.67k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
195
0
    JDIMENSION crop_x = this->croppingRegion.x;
196
0
    JDIMENSION crop_w = this->croppingRegion.w;
197
198
0
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
199
0
    if ((int)crop_x != this->croppingRegion.x)
200
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
201
0
             "actual (%d) cropping region left boundary",
202
0
             this->croppingRegion.x, (int)crop_x);
203
0
    if ((int)crop_w != this->croppingRegion.w)
204
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
205
0
             "actual (%d) cropping region width",
206
0
             this->croppingRegion.w, (int)crop_w);
207
0
  }
208
5.67k
#endif
209
210
6.11k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
211
212
5.67k
  croppedHeight = dinfo->output_height;
213
#if BITS_IN_JSAMPLE != 16
214
5.67k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
215
0
    croppedHeight = this->croppingRegion.h;
216
#endif
217
6.11k
  if ((row_pointer =
218
6.11k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
219
6.11k
    THROW("Memory allocation failure");
220
6.11k
  if (setjmp(this->jerr.setjmp_buffer)) {
221
    /* If we get here, the JPEG code has signaled an error. */
222
1.00k
    retval = -1;  goto bailout;
223
1.00k
  }
224
30.8M
  for (i = 0; i < (int)croppedHeight; i++) {
225
30.8M
    if (this->bottomUp)
226
27.4M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
227
3.43M
    else
228
3.43M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
229
30.8M
  }
230
231
#if BITS_IN_JSAMPLE != 16
232
5.67k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
233
0
    if (this->croppingRegion.y != 0) {
234
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
235
236
0
      if ((int)lines != this->croppingRegion.y)
237
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
238
               "actual (%d) cropping region upper boundary",
239
0
               this->croppingRegion.y, (int)lines);
240
0
    }
241
0
    while ((int)dinfo->output_scanline <
242
0
           this->croppingRegion.y + this->croppingRegion.h)
243
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
244
0
                                               this->croppingRegion.y],
245
0
                           this->croppingRegion.y + this->croppingRegion.h -
246
0
                           dinfo->output_scanline);
247
0
    if (this->croppingRegion.y + this->croppingRegion.h !=
248
0
        (int)dinfo->output_height) {
249
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
250
                                                     this->croppingRegion.y -
251
                                                     this->croppingRegion.h);
252
253
0
      if (lines != dinfo->output_height - this->croppingRegion.y -
254
0
                   this->croppingRegion.h)
255
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
256
0
               "actual (%d) cropping region lower boundary",
257
0
               this->croppingRegion.y + this->croppingRegion.h,
258
0
               (int)(dinfo->output_height - lines));
259
0
    }
260
0
  } else
261
4.96k
#endif
262
4.96k
  {
263
11.8M
    while (dinfo->output_scanline < dinfo->output_height)
264
11.8M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
265
11.8M
                           dinfo->output_height - dinfo->output_scanline);
266
4.96k
  }
267
4.96k
  jpeg_finish_decompress(dinfo);
268
269
7.81k
bailout:
270
7.81k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
271
7.81k
  free(row_pointer);
272
7.81k
  if (this->jerr.warning) retval = -1;
273
7.81k
  return retval;
274
4.96k
}
tj3Decompress8
Line
Count
Source
144
3.18k
{
145
3.18k
  static const char FUNCTION_NAME[] =
146
3.18k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
3.18k
  _JSAMPROW *row_pointer = NULL;
148
3.18k
  int croppedHeight, i, retval = 0;
149
3.18k
#if BITS_IN_JSAMPLE != 16
150
3.18k
  int scaledWidth;
151
3.18k
#endif
152
3.18k
  struct my_progress_mgr progress;
153
154
3.18k
  GET_DINSTANCE(handle);
155
3.18k
  if ((this->init & DECOMPRESS) == 0)
156
3.18k
    THROW("Instance has not been initialized for decompression");
157
158
3.18k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
3.18k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
3.18k
    THROW("Invalid argument");
161
162
3.18k
  if (this->scanLimit) {
163
3.18k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
3.18k
    progress.pub.progress_monitor = my_progress_monitor;
165
3.18k
    progress.this = this;
166
3.18k
    dinfo->progress = &progress.pub;
167
3.18k
  } else
168
0
    dinfo->progress = NULL;
169
170
3.18k
  if (setjmp(this->jerr.setjmp_buffer)) {
171
    /* If we get here, the JPEG code has signaled an error. */
172
698
    retval = -1;  goto bailout;
173
698
  }
174
175
3.18k
  if (dinfo->global_state <= DSTATE_START) {
176
3.18k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
177
3.18k
    jpeg_read_header(dinfo, TRUE);
178
3.18k
  }
179
2.49k
  setDecompParameters(this);
180
2.49k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
181
2.49k
#if BITS_IN_JSAMPLE != 16
182
2.49k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
183
2.49k
#endif
184
2.49k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
185
2.49k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
186
187
2.49k
  dinfo->scale_num = this->scalingFactor.num;
188
2.49k
  dinfo->scale_denom = this->scalingFactor.denom;
189
190
2.49k
  jpeg_start_decompress(dinfo);
191
192
2.49k
#if BITS_IN_JSAMPLE != 16
193
2.49k
  if (this->croppingRegion.x != 0 ||
194
2.49k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
195
0
    JDIMENSION crop_x = this->croppingRegion.x;
196
0
    JDIMENSION crop_w = this->croppingRegion.w;
197
198
0
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
199
0
    if ((int)crop_x != this->croppingRegion.x)
200
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
201
0
             "actual (%d) cropping region left boundary",
202
0
             this->croppingRegion.x, (int)crop_x);
203
0
    if ((int)crop_w != this->croppingRegion.w)
204
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
205
0
             "actual (%d) cropping region width",
206
0
             this->croppingRegion.w, (int)crop_w);
207
0
  }
208
2.49k
#endif
209
210
2.49k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
211
212
2.49k
  croppedHeight = dinfo->output_height;
213
2.49k
#if BITS_IN_JSAMPLE != 16
214
2.49k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
215
0
    croppedHeight = this->croppingRegion.h;
216
2.49k
#endif
217
2.49k
  if ((row_pointer =
218
2.49k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
219
2.49k
    THROW("Memory allocation failure");
220
2.49k
  if (setjmp(this->jerr.setjmp_buffer)) {
221
    /* If we get here, the JPEG code has signaled an error. */
222
435
    retval = -1;  goto bailout;
223
435
  }
224
13.8M
  for (i = 0; i < (int)croppedHeight; i++) {
225
13.8M
    if (this->bottomUp)
226
12.5M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
227
1.29M
    else
228
1.29M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
229
13.8M
  }
230
231
2.05k
#if BITS_IN_JSAMPLE != 16
232
2.49k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
233
0
    if (this->croppingRegion.y != 0) {
234
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
235
236
0
      if ((int)lines != this->croppingRegion.y)
237
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
238
0
               "actual (%d) cropping region upper boundary",
239
0
               this->croppingRegion.y, (int)lines);
240
0
    }
241
0
    while ((int)dinfo->output_scanline <
242
0
           this->croppingRegion.y + this->croppingRegion.h)
243
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
244
0
                                               this->croppingRegion.y],
245
0
                           this->croppingRegion.y + this->croppingRegion.h -
246
0
                           dinfo->output_scanline);
247
0
    if (this->croppingRegion.y + this->croppingRegion.h !=
248
0
        (int)dinfo->output_height) {
249
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
250
0
                                                     this->croppingRegion.y -
251
0
                                                     this->croppingRegion.h);
252
253
0
      if (lines != dinfo->output_height - this->croppingRegion.y -
254
0
                   this->croppingRegion.h)
255
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
256
0
               "actual (%d) cropping region lower boundary",
257
0
               this->croppingRegion.y + this->croppingRegion.h,
258
0
               (int)(dinfo->output_height - lines));
259
0
    }
260
0
  } else
261
2.05k
#endif
262
2.05k
  {
263
6.57M
    while (dinfo->output_scanline < dinfo->output_height)
264
6.56M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
265
6.56M
                           dinfo->output_height - dinfo->output_scanline);
266
2.05k
  }
267
2.05k
  jpeg_finish_decompress(dinfo);
268
269
3.18k
bailout:
270
3.18k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
271
3.18k
  free(row_pointer);
272
3.18k
  if (this->jerr.warning) retval = -1;
273
3.18k
  return retval;
274
2.05k
}
tj3Decompress12
Line
Count
Source
144
4.02k
{
145
4.02k
  static const char FUNCTION_NAME[] =
146
4.02k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
4.02k
  _JSAMPROW *row_pointer = NULL;
148
4.02k
  int croppedHeight, i, retval = 0;
149
4.02k
#if BITS_IN_JSAMPLE != 16
150
4.02k
  int scaledWidth;
151
4.02k
#endif
152
4.02k
  struct my_progress_mgr progress;
153
154
4.02k
  GET_DINSTANCE(handle);
155
4.02k
  if ((this->init & DECOMPRESS) == 0)
156
4.02k
    THROW("Instance has not been initialized for decompression");
157
158
4.02k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
4.02k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
4.02k
    THROW("Invalid argument");
161
162
4.02k
  if (this->scanLimit) {
163
4.02k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
4.02k
    progress.pub.progress_monitor = my_progress_monitor;
165
4.02k
    progress.this = this;
166
4.02k
    dinfo->progress = &progress.pub;
167
4.02k
  } else
168
0
    dinfo->progress = NULL;
169
170
4.02k
  if (setjmp(this->jerr.setjmp_buffer)) {
171
    /* If we get here, the JPEG code has signaled an error. */
172
841
    retval = -1;  goto bailout;
173
841
  }
174
175
4.02k
  if (dinfo->global_state <= DSTATE_START) {
176
4.02k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
177
4.02k
    jpeg_read_header(dinfo, TRUE);
178
4.02k
  }
179
3.18k
  setDecompParameters(this);
180
3.18k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
181
3.18k
#if BITS_IN_JSAMPLE != 16
182
3.18k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
183
3.18k
#endif
184
3.18k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
185
3.18k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
186
187
3.18k
  dinfo->scale_num = this->scalingFactor.num;
188
3.18k
  dinfo->scale_denom = this->scalingFactor.denom;
189
190
3.18k
  jpeg_start_decompress(dinfo);
191
192
3.18k
#if BITS_IN_JSAMPLE != 16
193
3.18k
  if (this->croppingRegion.x != 0 ||
194
3.18k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
195
0
    JDIMENSION crop_x = this->croppingRegion.x;
196
0
    JDIMENSION crop_w = this->croppingRegion.w;
197
198
0
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
199
0
    if ((int)crop_x != this->croppingRegion.x)
200
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
201
0
             "actual (%d) cropping region left boundary",
202
0
             this->croppingRegion.x, (int)crop_x);
203
0
    if ((int)crop_w != this->croppingRegion.w)
204
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
205
0
             "actual (%d) cropping region width",
206
0
             this->croppingRegion.w, (int)crop_w);
207
0
  }
208
3.18k
#endif
209
210
3.18k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
211
212
3.18k
  croppedHeight = dinfo->output_height;
213
3.18k
#if BITS_IN_JSAMPLE != 16
214
3.18k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
215
0
    croppedHeight = this->croppingRegion.h;
216
3.18k
#endif
217
3.18k
  if ((row_pointer =
218
3.18k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
219
3.18k
    THROW("Memory allocation failure");
220
3.18k
  if (setjmp(this->jerr.setjmp_buffer)) {
221
    /* If we get here, the JPEG code has signaled an error. */
222
281
    retval = -1;  goto bailout;
223
281
  }
224
11.9M
  for (i = 0; i < (int)croppedHeight; i++) {
225
11.9M
    if (this->bottomUp)
226
9.77M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
227
2.13M
    else
228
2.13M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
229
11.9M
  }
230
231
2.90k
#if BITS_IN_JSAMPLE != 16
232
3.18k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
233
0
    if (this->croppingRegion.y != 0) {
234
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
235
236
0
      if ((int)lines != this->croppingRegion.y)
237
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
238
0
               "actual (%d) cropping region upper boundary",
239
0
               this->croppingRegion.y, (int)lines);
240
0
    }
241
0
    while ((int)dinfo->output_scanline <
242
0
           this->croppingRegion.y + this->croppingRegion.h)
243
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
244
0
                                               this->croppingRegion.y],
245
0
                           this->croppingRegion.y + this->croppingRegion.h -
246
0
                           dinfo->output_scanline);
247
0
    if (this->croppingRegion.y + this->croppingRegion.h !=
248
0
        (int)dinfo->output_height) {
249
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
250
0
                                                     this->croppingRegion.y -
251
0
                                                     this->croppingRegion.h);
252
253
0
      if (lines != dinfo->output_height - this->croppingRegion.y -
254
0
                   this->croppingRegion.h)
255
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
256
0
               "actual (%d) cropping region lower boundary",
257
0
               this->croppingRegion.y + this->croppingRegion.h,
258
0
               (int)(dinfo->output_height - lines));
259
0
    }
260
0
  } else
261
2.90k
#endif
262
2.90k
  {
263
5.07M
    while (dinfo->output_scanline < dinfo->output_height)
264
5.06M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
265
5.06M
                           dinfo->output_height - dinfo->output_scanline);
266
2.90k
  }
267
2.90k
  jpeg_finish_decompress(dinfo);
268
269
4.02k
bailout:
270
4.02k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
271
4.02k
  free(row_pointer);
272
4.02k
  if (this->jerr.warning) retval = -1;
273
4.02k
  return retval;
274
2.90k
}
tj3Decompress16
Line
Count
Source
144
595
{
145
595
  static const char FUNCTION_NAME[] =
146
595
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
595
  _JSAMPROW *row_pointer = NULL;
148
595
  int croppedHeight, i, retval = 0;
149
#if BITS_IN_JSAMPLE != 16
150
  int scaledWidth;
151
#endif
152
595
  struct my_progress_mgr progress;
153
154
595
  GET_DINSTANCE(handle);
155
595
  if ((this->init & DECOMPRESS) == 0)
156
595
    THROW("Instance has not been initialized for decompression");
157
158
595
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
595
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
595
    THROW("Invalid argument");
161
162
595
  if (this->scanLimit) {
163
595
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
595
    progress.pub.progress_monitor = my_progress_monitor;
165
595
    progress.this = this;
166
595
    dinfo->progress = &progress.pub;
167
595
  } else
168
0
    dinfo->progress = NULL;
169
170
595
  if (setjmp(this->jerr.setjmp_buffer)) {
171
    /* If we get here, the JPEG code has signaled an error. */
172
159
    retval = -1;  goto bailout;
173
159
  }
174
175
595
  if (dinfo->global_state <= DSTATE_START) {
176
595
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
177
595
    jpeg_read_header(dinfo, TRUE);
178
595
  }
179
436
  setDecompParameters(this);
180
436
  this->dinfo.out_color_space = pf2cs[pixelFormat];
181
#if BITS_IN_JSAMPLE != 16
182
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
183
#endif
184
436
  dinfo->do_fancy_upsampling = !this->fastUpsample;
185
436
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
186
187
436
  dinfo->scale_num = this->scalingFactor.num;
188
436
  dinfo->scale_denom = this->scalingFactor.denom;
189
190
436
  jpeg_start_decompress(dinfo);
191
192
#if BITS_IN_JSAMPLE != 16
193
  if (this->croppingRegion.x != 0 ||
194
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
195
    JDIMENSION crop_x = this->croppingRegion.x;
196
    JDIMENSION crop_w = this->croppingRegion.w;
197
198
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
199
    if ((int)crop_x != this->croppingRegion.x)
200
      THROWI("Unexplained mismatch between specified (%d) and\n"
201
             "actual (%d) cropping region left boundary",
202
             this->croppingRegion.x, (int)crop_x);
203
    if ((int)crop_w != this->croppingRegion.w)
204
      THROWI("Unexplained mismatch between specified (%d) and\n"
205
             "actual (%d) cropping region width",
206
             this->croppingRegion.w, (int)crop_w);
207
  }
208
#endif
209
210
436
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
211
212
436
  croppedHeight = dinfo->output_height;
213
#if BITS_IN_JSAMPLE != 16
214
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
215
    croppedHeight = this->croppingRegion.h;
216
#endif
217
436
  if ((row_pointer =
218
436
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
219
436
    THROW("Memory allocation failure");
220
436
  if (setjmp(this->jerr.setjmp_buffer)) {
221
    /* If we get here, the JPEG code has signaled an error. */
222
290
    retval = -1;  goto bailout;
223
290
  }
224
5.09M
  for (i = 0; i < (int)croppedHeight; i++) {
225
5.09M
    if (this->bottomUp)
226
5.08M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
227
494
    else
228
494
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
229
5.09M
  }
230
231
#if BITS_IN_JSAMPLE != 16
232
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
233
    if (this->croppingRegion.y != 0) {
234
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
235
236
      if ((int)lines != this->croppingRegion.y)
237
        THROWI("Unexplained mismatch between specified (%d) and\n"
238
               "actual (%d) cropping region upper boundary",
239
               this->croppingRegion.y, (int)lines);
240
    }
241
    while ((int)dinfo->output_scanline <
242
           this->croppingRegion.y + this->croppingRegion.h)
243
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
244
                                               this->croppingRegion.y],
245
                           this->croppingRegion.y + this->croppingRegion.h -
246
                           dinfo->output_scanline);
247
    if (this->croppingRegion.y + this->croppingRegion.h !=
248
        (int)dinfo->output_height) {
249
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
250
                                                     this->croppingRegion.y -
251
                                                     this->croppingRegion.h);
252
253
      if (lines != dinfo->output_height - this->croppingRegion.y -
254
                   this->croppingRegion.h)
255
        THROWI("Unexplained mismatch between specified (%d) and\n"
256
               "actual (%d) cropping region lower boundary",
257
               this->croppingRegion.y + this->croppingRegion.h,
258
               (int)(dinfo->output_height - lines));
259
    }
260
  } else
261
#endif
262
146
  {
263
247k
    while (dinfo->output_scanline < dinfo->output_height)
264
247k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
265
247k
                           dinfo->output_height - dinfo->output_scanline);
266
146
  }
267
146
  jpeg_finish_decompress(dinfo);
268
269
595
bailout:
270
595
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
271
595
  free(row_pointer);
272
595
  if (this->jerr.warning) retval = -1;
273
595
  return retval;
274
146
}
275
276
277
/*************************** Packed-Pixel Image I/O **************************/
278
279
/* TurboJPEG 3+ */
280
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
281
  (tjhandle handle, const char *filename, int *width, int align, int *height,
282
   int *pixelFormat)
283
117k
{
284
117k
  static const char FUNCTION_NAME[] =
285
117k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
286
287
117k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
288
289
117k
  int retval = 0, tempc;
290
117k
  size_t pitch;
291
117k
  tjhandle handle2 = NULL;
292
117k
  tjinstance *this2;
293
117k
  j_compress_ptr cinfo = NULL;
294
117k
  cjpeg_source_ptr src;
295
117k
  _JSAMPLE *dstBuf = NULL;
296
117k
  FILE *file = NULL;
297
117k
  boolean invert;
298
299
117k
  GET_TJINSTANCE(handle, NULL)
300
301
117k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
302
117k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
303
117k
    THROW("Invalid argument");
304
117k
  if ((align & (align - 1)) != 0)
305
117k
    THROW("Alignment must be a power of 2");
306
307
  /* The instance handle passed to this function is used only for parameter
308
     retrieval.  Create a new temporary instance to avoid interfering with the
309
     libjpeg state of the primary instance. */
310
117k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
311
117k
  this2 = (tjinstance *)handle2;
312
117k
  cinfo = &this2->cinfo;
313
314
#ifdef _MSC_VER
315
  if (fopen_s(&file, filename, "rb") || file == NULL)
316
#else
317
117k
  if ((file = fopen(filename, "rb")) == NULL)
318
0
#endif
319
117k
    THROW_UNIX("Cannot open input file");
320
321
117k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
322
0
    THROW_UNIX("Could not read input file")
323
117k
  else if (tempc == EOF)
324
117k
    THROW("Input file contains no data");
325
326
117k
  if (setjmp(this2->jerr.setjmp_buffer)) {
327
    /* If we get here, the JPEG code has signaled an error. */
328
24.5k
    retval = -1;  goto bailout;
329
24.5k
  }
330
331
92.6k
  cinfo->data_precision = BITS_IN_JSAMPLE;
332
92.6k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333
92.6k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
334
92.6k
  if (tempc == 'B') {
335
17.9k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336
17.9k
      THROW("Could not initialize bitmap loader");
337
17.9k
    invert = !this->bottomUp;
338
98.4k
  } else if (tempc == 'P') {
339
98.4k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
340
98.4k
      THROW("Could not initialize PPM loader");
341
98.4k
    invert = this->bottomUp;
342
98.4k
  } else
343
18.4E
    THROW("Unsupported file type");
344
345
116k
  src->input_file = file;
346
116k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
347
  /* Refuse to load images larger than 1 Megapixel when fuzzing. */
348
116k
  src->max_pixels = this->maxPixels;
349
116k
#endif
350
116k
  (*src->start_input) (cinfo, src);
351
116k
  if (tempc == 'B') {
352
5.79k
    if (cinfo->X_density && cinfo->Y_density) {
353
1.01k
      this->xDensity = cinfo->X_density;
354
1.01k
      this->yDensity = cinfo->Y_density;
355
1.01k
      this->densityUnits = cinfo->density_unit;
356
1.01k
    }
357
5.79k
  }
358
116k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
359
360
116k
  *width = cinfo->image_width;  *height = cinfo->image_height;
361
116k
  *pixelFormat = cs2pf[cinfo->in_color_space];
362
363
116k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
364
116k
  if ((unsigned long long)pitch * (unsigned long long)(*height) >
365
116k
      (unsigned long long)((size_t)-1) ||
366
116k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
367
91.8k
                                   sizeof(_JSAMPLE))) == NULL)
368
116k
    THROW("Memory allocation failure");
369
370
116k
  if (setjmp(this2->jerr.setjmp_buffer)) {
371
    /* If we get here, the JPEG code has signaled an error. */
372
31.5k
    retval = -1;  goto bailout;
373
31.5k
  }
374
375
220M
  while (cinfo->next_scanline < cinfo->image_height) {
376
220M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
377
378
440M
    for (i = 0; i < nlines; i++) {
379
220M
      _JSAMPLE *dstptr;
380
220M
      int row;
381
382
220M
      row = cinfo->next_scanline + i;
383
220M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
384
181M
      else dstptr = &dstBuf[row * pitch];
385
220M
      memcpy(dstptr, src->_buffer[i],
386
220M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
387
220M
    }
388
220M
    cinfo->next_scanline += nlines;
389
220M
  }
390
391
84.8k
  (*src->finish_input) (cinfo, src);
392
393
117k
bailout:
394
117k
  tj3Destroy(handle2);
395
117k
  if (file) fclose(file);
396
117k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
397
117k
  return dstBuf;
398
399
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
400
401
  static const char ERROR_MSG[] =
402
    "16-bit data precision requires lossless JPEG,\n"
403
    "which was disabled at build time.";
404
  _JSAMPLE *retval = NULL;
405
406
  GET_TJINSTANCE(handle, NULL)
407
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
408
           ERROR_MSG);
409
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
410
411
bailout:
412
  return retval;
413
414
#endif
415
84.8k
}
tj3LoadImage8
Line
Count
Source
283
66.1k
{
284
66.1k
  static const char FUNCTION_NAME[] =
285
66.1k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
286
287
66.1k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
288
289
66.1k
  int retval = 0, tempc;
290
66.1k
  size_t pitch;
291
66.1k
  tjhandle handle2 = NULL;
292
66.1k
  tjinstance *this2;
293
66.1k
  j_compress_ptr cinfo = NULL;
294
66.1k
  cjpeg_source_ptr src;
295
66.1k
  _JSAMPLE *dstBuf = NULL;
296
66.1k
  FILE *file = NULL;
297
66.1k
  boolean invert;
298
299
66.1k
  GET_TJINSTANCE(handle, NULL)
300
301
66.1k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
302
66.1k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
303
66.1k
    THROW("Invalid argument");
304
66.1k
  if ((align & (align - 1)) != 0)
305
66.1k
    THROW("Alignment must be a power of 2");
306
307
  /* The instance handle passed to this function is used only for parameter
308
     retrieval.  Create a new temporary instance to avoid interfering with the
309
     libjpeg state of the primary instance. */
310
66.1k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
311
66.1k
  this2 = (tjinstance *)handle2;
312
66.1k
  cinfo = &this2->cinfo;
313
314
#ifdef _MSC_VER
315
  if (fopen_s(&file, filename, "rb") || file == NULL)
316
#else
317
66.1k
  if ((file = fopen(filename, "rb")) == NULL)
318
0
#endif
319
66.1k
    THROW_UNIX("Cannot open input file");
320
321
66.1k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
322
0
    THROW_UNIX("Could not read input file")
323
66.1k
  else if (tempc == EOF)
324
66.1k
    THROW("Input file contains no data");
325
326
66.1k
  if (setjmp(this2->jerr.setjmp_buffer)) {
327
    /* If we get here, the JPEG code has signaled an error. */
328
18.2k
    retval = -1;  goto bailout;
329
18.2k
  }
330
331
47.8k
  cinfo->data_precision = BITS_IN_JSAMPLE;
332
47.8k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333
47.8k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
334
47.8k
  if (tempc == 'B') {
335
17.9k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336
17.9k
      THROW("Could not initialize bitmap loader");
337
17.9k
    invert = !this->bottomUp;
338
47.7k
  } else if (tempc == 'P') {
339
47.7k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
340
47.7k
      THROW("Could not initialize PPM loader");
341
47.7k
    invert = this->bottomUp;
342
47.7k
  } else
343
18.4E
    THROW("Unsupported file type");
344
345
65.7k
  src->input_file = file;
346
65.7k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
347
  /* Refuse to load images larger than 1 Megapixel when fuzzing. */
348
65.7k
  src->max_pixels = this->maxPixels;
349
65.7k
#endif
350
65.7k
  (*src->start_input) (cinfo, src);
351
65.7k
  if (tempc == 'B') {
352
5.79k
    if (cinfo->X_density && cinfo->Y_density) {
353
1.01k
      this->xDensity = cinfo->X_density;
354
1.01k
      this->yDensity = cinfo->Y_density;
355
1.01k
      this->densityUnits = cinfo->density_unit;
356
1.01k
    }
357
5.79k
  }
358
65.7k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
359
360
65.7k
  *width = cinfo->image_width;  *height = cinfo->image_height;
361
65.7k
  *pixelFormat = cs2pf[cinfo->in_color_space];
362
363
65.7k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
364
65.7k
  if ((unsigned long long)pitch * (unsigned long long)(*height) >
365
65.7k
      (unsigned long long)((size_t)-1) ||
366
65.7k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
367
47.4k
                                   sizeof(_JSAMPLE))) == NULL)
368
65.7k
    THROW("Memory allocation failure");
369
370
65.7k
  if (setjmp(this2->jerr.setjmp_buffer)) {
371
    /* If we get here, the JPEG code has signaled an error. */
372
18.0k
    retval = -1;  goto bailout;
373
18.0k
  }
374
375
101M
  while (cinfo->next_scanline < cinfo->image_height) {
376
101M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
377
378
203M
    for (i = 0; i < nlines; i++) {
379
101M
      _JSAMPLE *dstptr;
380
101M
      int row;
381
382
101M
      row = cinfo->next_scanline + i;
383
101M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
384
80.2M
      else dstptr = &dstBuf[row * pitch];
385
101M
      memcpy(dstptr, src->_buffer[i],
386
101M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
387
101M
    }
388
101M
    cinfo->next_scanline += nlines;
389
101M
  }
390
391
47.6k
  (*src->finish_input) (cinfo, src);
392
393
66.1k
bailout:
394
66.1k
  tj3Destroy(handle2);
395
66.1k
  if (file) fclose(file);
396
66.1k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
397
66.1k
  return dstBuf;
398
399
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
400
401
  static const char ERROR_MSG[] =
402
    "16-bit data precision requires lossless JPEG,\n"
403
    "which was disabled at build time.";
404
  _JSAMPLE *retval = NULL;
405
406
  GET_TJINSTANCE(handle, NULL)
407
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
408
           ERROR_MSG);
409
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
410
411
bailout:
412
  return retval;
413
414
#endif
415
47.6k
}
tj3LoadImage12
Line
Count
Source
283
39.6k
{
284
39.6k
  static const char FUNCTION_NAME[] =
285
39.6k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
286
287
39.6k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
288
289
39.6k
  int retval = 0, tempc;
290
39.6k
  size_t pitch;
291
39.6k
  tjhandle handle2 = NULL;
292
39.6k
  tjinstance *this2;
293
39.6k
  j_compress_ptr cinfo = NULL;
294
39.6k
  cjpeg_source_ptr src;
295
39.6k
  _JSAMPLE *dstBuf = NULL;
296
39.6k
  FILE *file = NULL;
297
39.6k
  boolean invert;
298
299
39.6k
  GET_TJINSTANCE(handle, NULL)
300
301
39.6k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
302
39.6k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
303
39.6k
    THROW("Invalid argument");
304
39.6k
  if ((align & (align - 1)) != 0)
305
39.6k
    THROW("Alignment must be a power of 2");
306
307
  /* The instance handle passed to this function is used only for parameter
308
     retrieval.  Create a new temporary instance to avoid interfering with the
309
     libjpeg state of the primary instance. */
310
39.6k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
311
39.6k
  this2 = (tjinstance *)handle2;
312
39.6k
  cinfo = &this2->cinfo;
313
314
#ifdef _MSC_VER
315
  if (fopen_s(&file, filename, "rb") || file == NULL)
316
#else
317
39.6k
  if ((file = fopen(filename, "rb")) == NULL)
318
0
#endif
319
39.6k
    THROW_UNIX("Cannot open input file");
320
321
39.6k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
322
0
    THROW_UNIX("Could not read input file")
323
39.6k
  else if (tempc == EOF)
324
39.6k
    THROW("Input file contains no data");
325
326
39.6k
  if (setjmp(this2->jerr.setjmp_buffer)) {
327
    /* If we get here, the JPEG code has signaled an error. */
328
4.37k
    retval = -1;  goto bailout;
329
4.37k
  }
330
331
35.3k
  cinfo->data_precision = BITS_IN_JSAMPLE;
332
35.3k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333
35.3k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
334
35.3k
  if (tempc == 'B') {
335
14
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336
14
      THROW("Could not initialize bitmap loader");
337
14
    invert = !this->bottomUp;
338
39.4k
  } else if (tempc == 'P') {
339
39.4k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
340
39.4k
      THROW("Could not initialize PPM loader");
341
39.4k
    invert = this->bottomUp;
342
39.4k
  } else
343
18.4E
    THROW("Unsupported file type");
344
345
39.4k
  src->input_file = file;
346
39.4k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
347
  /* Refuse to load images larger than 1 Megapixel when fuzzing. */
348
39.4k
  src->max_pixels = this->maxPixels;
349
39.4k
#endif
350
39.4k
  (*src->start_input) (cinfo, src);
351
39.4k
  if (tempc == 'B') {
352
0
    if (cinfo->X_density && cinfo->Y_density) {
353
0
      this->xDensity = cinfo->X_density;
354
0
      this->yDensity = cinfo->Y_density;
355
0
      this->densityUnits = cinfo->density_unit;
356
0
    }
357
0
  }
358
39.4k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
359
360
39.4k
  *width = cinfo->image_width;  *height = cinfo->image_height;
361
39.4k
  *pixelFormat = cs2pf[cinfo->in_color_space];
362
363
39.4k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
364
39.4k
  if ((unsigned long long)pitch * (unsigned long long)(*height) >
365
39.4k
      (unsigned long long)((size_t)-1) ||
366
39.4k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
367
35.0k
                                   sizeof(_JSAMPLE))) == NULL)
368
39.4k
    THROW("Memory allocation failure");
369
370
39.4k
  if (setjmp(this2->jerr.setjmp_buffer)) {
371
    /* If we get here, the JPEG code has signaled an error. */
372
8.99k
    retval = -1;  goto bailout;
373
8.99k
  }
374
375
103M
  while (cinfo->next_scanline < cinfo->image_height) {
376
103M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
377
378
206M
    for (i = 0; i < nlines; i++) {
379
103M
      _JSAMPLE *dstptr;
380
103M
      int row;
381
382
103M
      row = cinfo->next_scanline + i;
383
103M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
384
88.4M
      else dstptr = &dstBuf[row * pitch];
385
103M
      memcpy(dstptr, src->_buffer[i],
386
103M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
387
103M
    }
388
103M
    cinfo->next_scanline += nlines;
389
103M
  }
390
391
30.4k
  (*src->finish_input) (cinfo, src);
392
393
39.6k
bailout:
394
39.6k
  tj3Destroy(handle2);
395
39.6k
  if (file) fclose(file);
396
39.6k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
397
39.6k
  return dstBuf;
398
399
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
400
401
  static const char ERROR_MSG[] =
402
    "16-bit data precision requires lossless JPEG,\n"
403
    "which was disabled at build time.";
404
  _JSAMPLE *retval = NULL;
405
406
  GET_TJINSTANCE(handle, NULL)
407
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
408
           ERROR_MSG);
409
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
410
411
bailout:
412
  return retval;
413
414
#endif
415
30.4k
}
tj3LoadImage16
Line
Count
Source
283
11.3k
{
284
11.3k
  static const char FUNCTION_NAME[] =
285
11.3k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
286
287
11.3k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
288
289
11.3k
  int retval = 0, tempc;
290
11.3k
  size_t pitch;
291
11.3k
  tjhandle handle2 = NULL;
292
11.3k
  tjinstance *this2;
293
11.3k
  j_compress_ptr cinfo = NULL;
294
11.3k
  cjpeg_source_ptr src;
295
11.3k
  _JSAMPLE *dstBuf = NULL;
296
11.3k
  FILE *file = NULL;
297
11.3k
  boolean invert;
298
299
11.3k
  GET_TJINSTANCE(handle, NULL)
300
301
11.3k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
302
11.3k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
303
11.3k
    THROW("Invalid argument");
304
11.3k
  if ((align & (align - 1)) != 0)
305
11.3k
    THROW("Alignment must be a power of 2");
306
307
  /* The instance handle passed to this function is used only for parameter
308
     retrieval.  Create a new temporary instance to avoid interfering with the
309
     libjpeg state of the primary instance. */
310
11.3k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
311
11.3k
  this2 = (tjinstance *)handle2;
312
11.3k
  cinfo = &this2->cinfo;
313
314
#ifdef _MSC_VER
315
  if (fopen_s(&file, filename, "rb") || file == NULL)
316
#else
317
11.3k
  if ((file = fopen(filename, "rb")) == NULL)
318
0
#endif
319
11.3k
    THROW_UNIX("Cannot open input file");
320
321
11.3k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
322
0
    THROW_UNIX("Could not read input file")
323
11.3k
  else if (tempc == EOF)
324
11.3k
    THROW("Input file contains no data");
325
326
11.3k
  if (setjmp(this2->jerr.setjmp_buffer)) {
327
    /* If we get here, the JPEG code has signaled an error. */
328
1.96k
    retval = -1;  goto bailout;
329
1.96k
  }
330
331
9.42k
  cinfo->data_precision = BITS_IN_JSAMPLE;
332
9.42k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333
9.42k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
334
9.42k
  if (tempc == 'B') {
335
7
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336
7
      THROW("Could not initialize bitmap loader");
337
7
    invert = !this->bottomUp;
338
11.2k
  } else if (tempc == 'P') {
339
11.2k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
340
11.2k
      THROW("Could not initialize PPM loader");
341
11.2k
    invert = this->bottomUp;
342
11.2k
  } else
343
18.4E
    THROW("Unsupported file type");
344
345
11.2k
  src->input_file = file;
346
11.2k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
347
  /* Refuse to load images larger than 1 Megapixel when fuzzing. */
348
11.2k
  src->max_pixels = this->maxPixels;
349
11.2k
#endif
350
11.2k
  (*src->start_input) (cinfo, src);
351
11.2k
  if (tempc == 'B') {
352
0
    if (cinfo->X_density && cinfo->Y_density) {
353
0
      this->xDensity = cinfo->X_density;
354
0
      this->yDensity = cinfo->Y_density;
355
0
      this->densityUnits = cinfo->density_unit;
356
0
    }
357
0
  }
358
11.2k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
359
360
11.2k
  *width = cinfo->image_width;  *height = cinfo->image_height;
361
11.2k
  *pixelFormat = cs2pf[cinfo->in_color_space];
362
363
11.2k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
364
11.2k
  if ((unsigned long long)pitch * (unsigned long long)(*height) >
365
11.2k
      (unsigned long long)((size_t)-1) ||
366
11.2k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
367
9.31k
                                   sizeof(_JSAMPLE))) == NULL)
368
11.2k
    THROW("Memory allocation failure");
369
370
11.2k
  if (setjmp(this2->jerr.setjmp_buffer)) {
371
    /* If we get here, the JPEG code has signaled an error. */
372
4.53k
    retval = -1;  goto bailout;
373
4.53k
  }
374
375
15.2M
  while (cinfo->next_scanline < cinfo->image_height) {
376
15.2M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
377
378
30.5M
    for (i = 0; i < nlines; i++) {
379
15.2M
      _JSAMPLE *dstptr;
380
15.2M
      int row;
381
382
15.2M
      row = cinfo->next_scanline + i;
383
15.2M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
384
13.0M
      else dstptr = &dstBuf[row * pitch];
385
15.2M
      memcpy(dstptr, src->_buffer[i],
386
15.2M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
387
15.2M
    }
388
15.2M
    cinfo->next_scanline += nlines;
389
15.2M
  }
390
391
6.74k
  (*src->finish_input) (cinfo, src);
392
393
11.3k
bailout:
394
11.3k
  tj3Destroy(handle2);
395
11.3k
  if (file) fclose(file);
396
11.3k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
397
11.3k
  return dstBuf;
398
399
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
400
401
  static const char ERROR_MSG[] =
402
    "16-bit data precision requires lossless JPEG,\n"
403
    "which was disabled at build time.";
404
  _JSAMPLE *retval = NULL;
405
406
  GET_TJINSTANCE(handle, NULL)
407
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
408
           ERROR_MSG);
409
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
410
411
bailout:
412
  return retval;
413
414
#endif
415
6.74k
}
416
417
418
/* TurboJPEG 3+ */
419
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
420
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
421
   int pitch, int height, int pixelFormat)
422
0
{
423
0
  static const char FUNCTION_NAME[] =
424
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
425
0
  int retval = 0;
426
427
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
428
429
0
  tjhandle handle2 = NULL;
430
0
  tjinstance *this2;
431
0
  j_decompress_ptr dinfo = NULL;
432
0
  djpeg_dest_ptr dst;
433
0
  FILE *file = NULL;
434
0
  char *ptr = NULL;
435
0
  boolean invert;
436
437
0
  GET_TJINSTANCE(handle, -1)
438
439
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
440
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
441
0
    THROW("Invalid argument");
442
443
  /* The instance handle passed to this function is used only for parameter
444
     retrieval.  Create a new temporary instance to avoid interfering with the
445
     libjpeg state of the primary instance. */
446
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
447
0
    return -1;
448
0
  this2 = (tjinstance *)handle2;
449
0
  dinfo = &this2->dinfo;
450
451
#ifdef _MSC_VER
452
  if (fopen_s(&file, filename, "wb") || file == NULL)
453
#else
454
0
  if ((file = fopen(filename, "wb")) == NULL)
455
0
#endif
456
0
    THROW_UNIX("Cannot open output file");
457
458
0
  if (setjmp(this2->jerr.setjmp_buffer)) {
459
    /* If we get here, the JPEG code has signaled an error. */
460
0
    retval = -1;  goto bailout;
461
0
  }
462
463
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
464
0
  dinfo->image_width = width;  dinfo->image_height = height;
465
0
  dinfo->global_state = DSTATE_READY;
466
0
  dinfo->scale_num = dinfo->scale_denom = 1;
467
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
468
469
0
  ptr = strrchr(filename, '.');
470
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
471
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
472
0
      THROW("Could not initialize bitmap writer");
473
0
    invert = !this->bottomUp;
474
0
    dinfo->X_density = (UINT16)this->xDensity;
475
0
    dinfo->Y_density = (UINT16)this->yDensity;
476
0
    dinfo->density_unit = (UINT8)this->densityUnits;
477
0
  } else {
478
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
479
0
      THROW("Could not initialize PPM writer");
480
0
    invert = this->bottomUp;
481
0
  }
482
483
0
  dst->output_file = file;
484
0
  (*dst->start_output) (dinfo, dst);
485
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
486
487
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
488
489
0
  while (dinfo->output_scanline < dinfo->output_height) {
490
0
    _JSAMPLE *rowptr;
491
492
0
    if (invert)
493
0
      rowptr =
494
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
495
0
    else
496
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
497
0
    memcpy(dst->_buffer[0], rowptr,
498
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
499
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
500
0
    dinfo->output_scanline++;
501
0
  }
502
503
0
  (*dst->finish_output) (dinfo, dst);
504
505
0
bailout:
506
0
  tj3Destroy(handle2);
507
0
  if (file) fclose(file);
508
0
  return retval;
509
510
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
511
512
  GET_TJINSTANCE(handle, -1)
513
  THROW("16-bit data precision requires lossless JPEG,\n"
514
        "which was disabled at build time.")
515
bailout:
516
  return retval;
517
518
#endif
519
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
520
521
522
#undef _JSAMPLE
523
#undef _JSAMPROW
524
#undef _buffer
525
#undef _jinit_read_ppm
526
#undef _jinit_write_ppm
527
#undef _jpeg_crop_scanline
528
#undef _jpeg_read_scanlines
529
#undef _jpeg_skip_scanlines
530
#undef _jpeg_write_scanlines