Coverage Report

Created: 2025-10-13 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/src/turbojpeg-mp.c
Line
Count
Source
1
/*
2
 * Copyright (C)2009-2024 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
192M
#define _JSAMPLE  JSAMPLE
34
47.0k
#define _JSAMPROW  JSAMPROW
35
192M
#define _buffer  buffer
36
93.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
19.0M
#define _jpeg_read_scanlines  jpeg_read_scanlines
40
0
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
41
35.0k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
149M
#define _JSAMPLE  J12SAMPLE
44
43.5k
#define _JSAMPROW  J12SAMPROW
45
148M
#define _buffer  buffer12
46
59.8k
#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
14.1M
#define _jpeg_read_scanlines  jpeg12_read_scanlines
50
0
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
51
33.4k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
32.1M
#define _JSAMPLE  J16SAMPLE
54
10.2k
#define _JSAMPROW  J16SAMPROW
55
32.0M
#define _buffer  buffer16
56
22.1k
#define _jinit_read_ppm  j16init_read_ppm
57
0
#define _jinit_write_ppm  j16init_write_ppm
58
653k
#define _jpeg_read_scanlines  jpeg16_read_scanlines
59
8.27k
#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
313k
#define _GET_STRING(name, suffix)  #name #suffix
65
313k
#define GET_STRING(name, suffix)  _GET_STRING(name, suffix)
66
67
68
/******************************** Compressor *********************************/
69
70
/* TurboJPEG 3.0+ */
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
77.2k
{
75
77.2k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
77.2k
  int i, retval = 0;
77
77.2k
  boolean alloc = TRUE;
78
77.2k
  _JSAMPROW *row_pointer = NULL;
79
80
77.2k
  GET_CINSTANCE(handle)
81
77.2k
  if ((this->init & COMPRESS) == 0)
82
77.2k
    THROW("Instance has not been initialized for compression");
83
84
77.2k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
77.2k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
77.2k
      jpegSize == NULL)
87
77.2k
    THROW("Invalid argument");
88
89
77.2k
  if (!this->lossless && this->quality == -1)
90
77.2k
    THROW("TJPARAM_QUALITY must be specified");
91
77.2k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
77.2k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
77.2k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
77.2k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
77.2k
    THROW("Memory allocation failure");
98
99
77.2k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
554
    retval = -1;  goto bailout;
102
554
  }
103
104
76.7k
  cinfo->image_width = width;
105
76.7k
  cinfo->image_height = height;
106
76.7k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
#if BITS_IN_JSAMPLE == 8
108
35.0k
  if (this->lossless && this->precision >= 2 &&
109
8.61k
      this->precision <= BITS_IN_JSAMPLE)
110
#else
111
41.6k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
112
16.3k
      this->precision <= BITS_IN_JSAMPLE)
113
16.3k
#endif
114
24.9k
    cinfo->data_precision = this->precision;
115
116
76.7k
  setCompDefaults(this, pixelFormat);
117
76.7k
  if (this->noRealloc) alloc = FALSE;
118
76.7k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
119
120
76.7k
  jpeg_start_compress(cinfo, TRUE);
121
76.7k
  if (this->iccBuf != NULL && this->iccSize != 0)
122
0
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
123
287M
  for (i = 0; i < height; i++) {
124
287M
    if (this->bottomUp)
125
41.4M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
126
245M
    else
127
245M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
128
287M
  }
129
153k
  while (cinfo->next_scanline < cinfo->image_height)
130
76.7k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
131
76.7k
                          cinfo->image_height - cinfo->next_scanline);
132
76.7k
  jpeg_finish_compress(cinfo);
133
134
77.2k
bailout:
135
77.2k
  if (cinfo->global_state > CSTATE_START && alloc)
136
71
    (*cinfo->dest->term_destination) (cinfo);
137
77.2k
  if (cinfo->global_state > CSTATE_START || retval == -1)
138
554
    jpeg_abort_compress(cinfo);
139
77.2k
  free(row_pointer);
140
77.2k
  if (this->jerr.warning) retval = -1;
141
77.2k
  return retval;
142
76.7k
}
tj3Compress8
Line
Count
Source
74
35.3k
{
75
35.3k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
35.3k
  int i, retval = 0;
77
35.3k
  boolean alloc = TRUE;
78
35.3k
  _JSAMPROW *row_pointer = NULL;
79
80
35.3k
  GET_CINSTANCE(handle)
81
35.3k
  if ((this->init & COMPRESS) == 0)
82
35.3k
    THROW("Instance has not been initialized for compression");
83
84
35.3k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
35.3k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
35.3k
      jpegSize == NULL)
87
35.3k
    THROW("Invalid argument");
88
89
35.3k
  if (!this->lossless && this->quality == -1)
90
35.3k
    THROW("TJPARAM_QUALITY must be specified");
91
35.3k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
35.3k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
35.3k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
35.3k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
35.3k
    THROW("Memory allocation failure");
98
99
35.3k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
216
    retval = -1;  goto bailout;
102
216
  }
103
104
35.0k
  cinfo->image_width = width;
105
35.0k
  cinfo->image_height = height;
106
35.0k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
35.0k
#if BITS_IN_JSAMPLE == 8
108
35.0k
  if (this->lossless && this->precision >= 2 &&
109
8.61k
      this->precision <= BITS_IN_JSAMPLE)
110
#else
111
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
112
      this->precision <= BITS_IN_JSAMPLE)
113
#endif
114
8.61k
    cinfo->data_precision = this->precision;
115
116
35.0k
  setCompDefaults(this, pixelFormat);
117
35.0k
  if (this->noRealloc) alloc = FALSE;
118
35.0k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
119
120
35.0k
  jpeg_start_compress(cinfo, TRUE);
121
35.0k
  if (this->iccBuf != NULL && this->iccSize != 0)
122
0
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
123
114M
  for (i = 0; i < height; i++) {
124
114M
    if (this->bottomUp)
125
16.6M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
126
97.8M
    else
127
97.8M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
128
114M
  }
129
70.1k
  while (cinfo->next_scanline < cinfo->image_height)
130
35.0k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
131
35.0k
                          cinfo->image_height - cinfo->next_scanline);
132
35.0k
  jpeg_finish_compress(cinfo);
133
134
35.3k
bailout:
135
35.3k
  if (cinfo->global_state > CSTATE_START && alloc)
136
0
    (*cinfo->dest->term_destination) (cinfo);
137
35.3k
  if (cinfo->global_state > CSTATE_START || retval == -1)
138
216
    jpeg_abort_compress(cinfo);
139
35.3k
  free(row_pointer);
140
35.3k
  if (this->jerr.warning) retval = -1;
141
35.3k
  return retval;
142
35.0k
}
tj3Compress12
Line
Count
Source
74
33.6k
{
75
33.6k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
33.6k
  int i, retval = 0;
77
33.6k
  boolean alloc = TRUE;
78
33.6k
  _JSAMPROW *row_pointer = NULL;
79
80
33.6k
  GET_CINSTANCE(handle)
81
33.6k
  if ((this->init & COMPRESS) == 0)
82
33.6k
    THROW("Instance has not been initialized for compression");
83
84
33.6k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
33.6k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
33.6k
      jpegSize == NULL)
87
33.6k
    THROW("Invalid argument");
88
89
33.6k
  if (!this->lossless && this->quality == -1)
90
33.6k
    THROW("TJPARAM_QUALITY must be specified");
91
33.6k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
33.6k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
33.6k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
33.6k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
33.6k
    THROW("Memory allocation failure");
98
99
33.6k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
190
    retval = -1;  goto bailout;
102
190
  }
103
104
33.4k
  cinfo->image_width = width;
105
33.4k
  cinfo->image_height = height;
106
33.4k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
#if BITS_IN_JSAMPLE == 8
108
  if (this->lossless && this->precision >= 2 &&
109
      this->precision <= BITS_IN_JSAMPLE)
110
#else
111
33.4k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
112
7.94k
      this->precision <= BITS_IN_JSAMPLE)
113
7.94k
#endif
114
7.94k
    cinfo->data_precision = this->precision;
115
116
33.4k
  setCompDefaults(this, pixelFormat);
117
33.4k
  if (this->noRealloc) alloc = FALSE;
118
33.4k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
119
120
33.4k
  jpeg_start_compress(cinfo, TRUE);
121
33.4k
  if (this->iccBuf != NULL && this->iccSize != 0)
122
0
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
123
143M
  for (i = 0; i < height; i++) {
124
143M
    if (this->bottomUp)
125
20.5M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
126
123M
    else
127
123M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
128
143M
  }
129
66.8k
  while (cinfo->next_scanline < cinfo->image_height)
130
33.4k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
131
33.4k
                          cinfo->image_height - cinfo->next_scanline);
132
33.4k
  jpeg_finish_compress(cinfo);
133
134
33.6k
bailout:
135
33.6k
  if (cinfo->global_state > CSTATE_START && alloc)
136
19
    (*cinfo->dest->term_destination) (cinfo);
137
33.6k
  if (cinfo->global_state > CSTATE_START || retval == -1)
138
190
    jpeg_abort_compress(cinfo);
139
33.6k
  free(row_pointer);
140
33.6k
  if (this->jerr.warning) retval = -1;
141
33.6k
  return retval;
142
33.4k
}
tj3Compress16
Line
Count
Source
74
8.36k
{
75
8.36k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
8.36k
  int i, retval = 0;
77
8.36k
  boolean alloc = TRUE;
78
8.36k
  _JSAMPROW *row_pointer = NULL;
79
80
8.36k
  GET_CINSTANCE(handle)
81
8.36k
  if ((this->init & COMPRESS) == 0)
82
8.36k
    THROW("Instance has not been initialized for compression");
83
84
8.36k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
8.36k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
8.36k
      jpegSize == NULL)
87
8.36k
    THROW("Invalid argument");
88
89
8.36k
  if (!this->lossless && this->quality == -1)
90
8.36k
    THROW("TJPARAM_QUALITY must be specified");
91
8.36k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
8.36k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
8.36k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
8.36k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
8.36k
    THROW("Memory allocation failure");
98
99
8.36k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
148
    retval = -1;  goto bailout;
102
148
  }
103
104
8.21k
  cinfo->image_width = width;
105
8.21k
  cinfo->image_height = height;
106
8.21k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
#if BITS_IN_JSAMPLE == 8
108
  if (this->lossless && this->precision >= 2 &&
109
      this->precision <= BITS_IN_JSAMPLE)
110
#else
111
8.36k
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
112
8.36k
      this->precision <= BITS_IN_JSAMPLE)
113
8.36k
#endif
114
8.36k
    cinfo->data_precision = this->precision;
115
116
8.21k
  setCompDefaults(this, pixelFormat);
117
8.21k
  if (this->noRealloc) alloc = FALSE;
118
8.21k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
119
120
8.21k
  jpeg_start_compress(cinfo, TRUE);
121
8.21k
  if (this->iccBuf != NULL && this->iccSize != 0)
122
0
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
123
29.2M
  for (i = 0; i < height; i++) {
124
29.2M
    if (this->bottomUp)
125
4.17M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
126
25.0M
    else
127
25.0M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
128
29.2M
  }
129
16.4k
  while (cinfo->next_scanline < cinfo->image_height)
130
8.27k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
131
8.27k
                          cinfo->image_height - cinfo->next_scanline);
132
8.21k
  jpeg_finish_compress(cinfo);
133
134
8.36k
bailout:
135
8.36k
  if (cinfo->global_state > CSTATE_START && alloc)
136
52
    (*cinfo->dest->term_destination) (cinfo);
137
8.36k
  if (cinfo->global_state > CSTATE_START || retval == -1)
138
148
    jpeg_abort_compress(cinfo);
139
8.36k
  free(row_pointer);
140
8.36k
  if (this->jerr.warning) retval = -1;
141
8.36k
  return retval;
142
8.21k
}
143
144
145
/******************************* Decompressor ********************************/
146
147
/* TurboJPEG 3.0+ */
148
DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE)
149
  (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize,
150
   _JSAMPLE *dstBuf, int pitch, int pixelFormat)
151
23.6k
{
152
23.6k
  static const char FUNCTION_NAME[] =
153
23.6k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
154
23.6k
  _JSAMPROW *row_pointer = NULL;
155
23.6k
  int croppedHeight, i, retval = 0;
156
#if BITS_IN_JSAMPLE != 16
157
  int scaledWidth;
158
#endif
159
23.6k
  struct my_progress_mgr progress;
160
161
23.6k
  GET_DINSTANCE(handle);
162
23.6k
  if ((this->init & DECOMPRESS) == 0)
163
23.6k
    THROW("Instance has not been initialized for decompression");
164
165
23.6k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
166
23.6k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
167
23.6k
    THROW("Invalid argument");
168
169
23.6k
  if (this->scanLimit) {
170
23.6k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
171
23.6k
    progress.pub.progress_monitor = my_progress_monitor;
172
23.6k
    progress.this = this;
173
23.6k
    dinfo->progress = &progress.pub;
174
23.6k
  } else
175
0
    dinfo->progress = NULL;
176
177
23.6k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
178
179
23.6k
  if (setjmp(this->jerr.setjmp_buffer)) {
180
    /* If we get here, the JPEG code has signaled an error. */
181
6.27k
    retval = -1;  goto bailout;
182
6.27k
  }
183
184
23.6k
  if (dinfo->global_state <= DSTATE_INHEADER) {
185
23.6k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
186
23.6k
    jpeg_read_header(dinfo, TRUE);
187
23.6k
  }
188
17.4k
  setDecompParameters(this);
189
17.4k
  if (this->maxPixels &&
190
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
191
0
      (unsigned long long)this->maxPixels)
192
17.4k
    THROW("Image is too large");
193
17.4k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
194
#if BITS_IN_JSAMPLE != 16
195
16.0k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
196
#endif
197
17.4k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
198
17.4k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
199
200
17.4k
  dinfo->scale_num = this->scalingFactor.num;
201
17.4k
  dinfo->scale_denom = this->scalingFactor.denom;
202
203
17.4k
  jpeg_start_decompress(dinfo);
204
205
#if BITS_IN_JSAMPLE != 16
206
16.0k
  if (this->croppingRegion.x != 0 ||
207
16.0k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
208
0
    JDIMENSION crop_x = this->croppingRegion.x;
209
0
    JDIMENSION crop_w = this->croppingRegion.w;
210
211
0
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
212
0
    if ((int)crop_x != this->croppingRegion.x)
213
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
214
0
             "actual (%d) cropping region left boundary",
215
0
             this->croppingRegion.x, (int)crop_x);
216
0
    if ((int)crop_w != this->croppingRegion.w)
217
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
218
0
             "actual (%d) cropping region width",
219
0
             this->croppingRegion.w, (int)crop_w);
220
0
  }
221
16.0k
#endif
222
223
17.4k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
224
225
16.0k
  croppedHeight = dinfo->output_height;
226
#if BITS_IN_JSAMPLE != 16
227
16.0k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
228
0
    croppedHeight = this->croppingRegion.h;
229
#endif
230
17.4k
  if ((row_pointer =
231
17.4k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
232
17.4k
    THROW("Memory allocation failure");
233
17.4k
  if (setjmp(this->jerr.setjmp_buffer)) {
234
    /* If we get here, the JPEG code has signaled an error. */
235
2.78k
    retval = -1;  goto bailout;
236
2.78k
  }
237
81.7M
  for (i = 0; i < (int)croppedHeight; i++) {
238
81.6M
    if (this->bottomUp)
239
70.9M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
240
10.7M
    else
241
10.7M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
242
81.6M
  }
243
244
#if BITS_IN_JSAMPLE != 16
245
16.0k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
246
0
    if (this->croppingRegion.y != 0) {
247
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
248
249
0
      if ((int)lines != this->croppingRegion.y)
250
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
251
               "actual (%d) cropping region upper boundary",
252
0
               this->croppingRegion.y, (int)lines);
253
0
    }
254
0
    while ((int)dinfo->output_scanline <
255
0
           this->croppingRegion.y + this->croppingRegion.h)
256
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
257
0
                                               this->croppingRegion.y],
258
0
                           this->croppingRegion.y + this->croppingRegion.h -
259
0
                           dinfo->output_scanline);
260
0
    if (this->croppingRegion.y + this->croppingRegion.h !=
261
0
        (int)dinfo->output_height) {
262
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
263
                                                     this->croppingRegion.y -
264
                                                     this->croppingRegion.h);
265
266
0
      if (lines != dinfo->output_height - this->croppingRegion.y -
267
0
                   this->croppingRegion.h)
268
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
269
0
               "actual (%d) cropping region lower boundary",
270
0
               this->croppingRegion.y + this->croppingRegion.h,
271
0
               (int)(dinfo->output_height - lines));
272
0
    }
273
0
  } else
274
14.2k
#endif
275
14.2k
  {
276
33.9M
    while (dinfo->output_scanline < dinfo->output_height)
277
33.9M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
278
33.9M
                           dinfo->output_height - dinfo->output_scanline);
279
14.2k
  }
280
14.2k
  jpeg_finish_decompress(dinfo);
281
282
23.6k
bailout:
283
23.6k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
284
23.6k
  free(row_pointer);
285
23.6k
  if (this->jerr.warning) retval = -1;
286
23.6k
  return retval;
287
14.2k
}
tj3Decompress8
Line
Count
Source
151
11.7k
{
152
11.7k
  static const char FUNCTION_NAME[] =
153
11.7k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
154
11.7k
  _JSAMPROW *row_pointer = NULL;
155
11.7k
  int croppedHeight, i, retval = 0;
156
11.7k
#if BITS_IN_JSAMPLE != 16
157
11.7k
  int scaledWidth;
158
11.7k
#endif
159
11.7k
  struct my_progress_mgr progress;
160
161
11.7k
  GET_DINSTANCE(handle);
162
11.7k
  if ((this->init & DECOMPRESS) == 0)
163
11.7k
    THROW("Instance has not been initialized for decompression");
164
165
11.7k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
166
11.7k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
167
11.7k
    THROW("Invalid argument");
168
169
11.7k
  if (this->scanLimit) {
170
11.7k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
171
11.7k
    progress.pub.progress_monitor = my_progress_monitor;
172
11.7k
    progress.this = this;
173
11.7k
    dinfo->progress = &progress.pub;
174
11.7k
  } else
175
0
    dinfo->progress = NULL;
176
177
11.7k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
178
179
11.7k
  if (setjmp(this->jerr.setjmp_buffer)) {
180
    /* If we get here, the JPEG code has signaled an error. */
181
3.42k
    retval = -1;  goto bailout;
182
3.42k
  }
183
184
11.7k
  if (dinfo->global_state <= DSTATE_INHEADER) {
185
11.7k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
186
11.7k
    jpeg_read_header(dinfo, TRUE);
187
11.7k
  }
188
8.35k
  setDecompParameters(this);
189
8.35k
  if (this->maxPixels &&
190
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
191
0
      (unsigned long long)this->maxPixels)
192
8.35k
    THROW("Image is too large");
193
8.35k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
194
8.35k
#if BITS_IN_JSAMPLE != 16
195
8.35k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
196
8.35k
#endif
197
8.35k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
198
8.35k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
199
200
8.35k
  dinfo->scale_num = this->scalingFactor.num;
201
8.35k
  dinfo->scale_denom = this->scalingFactor.denom;
202
203
8.35k
  jpeg_start_decompress(dinfo);
204
205
8.35k
#if BITS_IN_JSAMPLE != 16
206
8.35k
  if (this->croppingRegion.x != 0 ||
207
8.35k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
208
0
    JDIMENSION crop_x = this->croppingRegion.x;
209
0
    JDIMENSION crop_w = this->croppingRegion.w;
210
211
0
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
212
0
    if ((int)crop_x != this->croppingRegion.x)
213
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
214
0
             "actual (%d) cropping region left boundary",
215
0
             this->croppingRegion.x, (int)crop_x);
216
0
    if ((int)crop_w != this->croppingRegion.w)
217
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
218
0
             "actual (%d) cropping region width",
219
0
             this->croppingRegion.w, (int)crop_w);
220
0
  }
221
8.35k
#endif
222
223
8.35k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
224
225
8.35k
  croppedHeight = dinfo->output_height;
226
8.35k
#if BITS_IN_JSAMPLE != 16
227
8.35k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
228
0
    croppedHeight = this->croppingRegion.h;
229
8.35k
#endif
230
8.35k
  if ((row_pointer =
231
8.35k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
232
8.35k
    THROW("Memory allocation failure");
233
8.35k
  if (setjmp(this->jerr.setjmp_buffer)) {
234
    /* If we get here, the JPEG code has signaled an error. */
235
1.08k
    retval = -1;  goto bailout;
236
1.08k
  }
237
40.1M
  for (i = 0; i < (int)croppedHeight; i++) {
238
40.1M
    if (this->bottomUp)
239
36.6M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
240
3.45M
    else
241
3.45M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
242
40.1M
  }
243
244
7.26k
#if BITS_IN_JSAMPLE != 16
245
8.35k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
246
0
    if (this->croppingRegion.y != 0) {
247
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
248
249
0
      if ((int)lines != this->croppingRegion.y)
250
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
251
0
               "actual (%d) cropping region upper boundary",
252
0
               this->croppingRegion.y, (int)lines);
253
0
    }
254
0
    while ((int)dinfo->output_scanline <
255
0
           this->croppingRegion.y + this->croppingRegion.h)
256
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
257
0
                                               this->croppingRegion.y],
258
0
                           this->croppingRegion.y + this->croppingRegion.h -
259
0
                           dinfo->output_scanline);
260
0
    if (this->croppingRegion.y + this->croppingRegion.h !=
261
0
        (int)dinfo->output_height) {
262
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
263
0
                                                     this->croppingRegion.y -
264
0
                                                     this->croppingRegion.h);
265
266
0
      if (lines != dinfo->output_height - this->croppingRegion.y -
267
0
                   this->croppingRegion.h)
268
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
269
0
               "actual (%d) cropping region lower boundary",
270
0
               this->croppingRegion.y + this->croppingRegion.h,
271
0
               (int)(dinfo->output_height - lines));
272
0
    }
273
0
  } else
274
7.26k
#endif
275
7.26k
  {
276
19.0M
    while (dinfo->output_scanline < dinfo->output_height)
277
19.0M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
278
19.0M
                           dinfo->output_height - dinfo->output_scanline);
279
7.26k
  }
280
7.26k
  jpeg_finish_decompress(dinfo);
281
282
11.7k
bailout:
283
11.7k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
284
11.7k
  free(row_pointer);
285
11.7k
  if (this->jerr.warning) retval = -1;
286
11.7k
  return retval;
287
7.26k
}
tj3Decompress12
Line
Count
Source
151
9.98k
{
152
9.98k
  static const char FUNCTION_NAME[] =
153
9.98k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
154
9.98k
  _JSAMPROW *row_pointer = NULL;
155
9.98k
  int croppedHeight, i, retval = 0;
156
9.98k
#if BITS_IN_JSAMPLE != 16
157
9.98k
  int scaledWidth;
158
9.98k
#endif
159
9.98k
  struct my_progress_mgr progress;
160
161
9.98k
  GET_DINSTANCE(handle);
162
9.98k
  if ((this->init & DECOMPRESS) == 0)
163
9.98k
    THROW("Instance has not been initialized for decompression");
164
165
9.98k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
166
9.98k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
167
9.98k
    THROW("Invalid argument");
168
169
9.98k
  if (this->scanLimit) {
170
9.98k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
171
9.98k
    progress.pub.progress_monitor = my_progress_monitor;
172
9.98k
    progress.this = this;
173
9.98k
    dinfo->progress = &progress.pub;
174
9.98k
  } else
175
0
    dinfo->progress = NULL;
176
177
9.98k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
178
179
9.98k
  if (setjmp(this->jerr.setjmp_buffer)) {
180
    /* If we get here, the JPEG code has signaled an error. */
181
2.26k
    retval = -1;  goto bailout;
182
2.26k
  }
183
184
9.98k
  if (dinfo->global_state <= DSTATE_INHEADER) {
185
9.98k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
186
9.98k
    jpeg_read_header(dinfo, TRUE);
187
9.98k
  }
188
7.71k
  setDecompParameters(this);
189
7.71k
  if (this->maxPixels &&
190
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
191
0
      (unsigned long long)this->maxPixels)
192
7.71k
    THROW("Image is too large");
193
7.71k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
194
7.71k
#if BITS_IN_JSAMPLE != 16
195
7.71k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
196
7.71k
#endif
197
7.71k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
198
7.71k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
199
200
7.71k
  dinfo->scale_num = this->scalingFactor.num;
201
7.71k
  dinfo->scale_denom = this->scalingFactor.denom;
202
203
7.71k
  jpeg_start_decompress(dinfo);
204
205
7.71k
#if BITS_IN_JSAMPLE != 16
206
7.71k
  if (this->croppingRegion.x != 0 ||
207
7.71k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
208
0
    JDIMENSION crop_x = this->croppingRegion.x;
209
0
    JDIMENSION crop_w = this->croppingRegion.w;
210
211
0
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
212
0
    if ((int)crop_x != this->croppingRegion.x)
213
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
214
0
             "actual (%d) cropping region left boundary",
215
0
             this->croppingRegion.x, (int)crop_x);
216
0
    if ((int)crop_w != this->croppingRegion.w)
217
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
218
0
             "actual (%d) cropping region width",
219
0
             this->croppingRegion.w, (int)crop_w);
220
0
  }
221
7.71k
#endif
222
223
7.71k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
224
225
7.71k
  croppedHeight = dinfo->output_height;
226
7.71k
#if BITS_IN_JSAMPLE != 16
227
7.71k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
228
0
    croppedHeight = this->croppingRegion.h;
229
7.71k
#endif
230
7.71k
  if ((row_pointer =
231
7.71k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
232
7.71k
    THROW("Memory allocation failure");
233
7.71k
  if (setjmp(this->jerr.setjmp_buffer)) {
234
    /* If we get here, the JPEG code has signaled an error. */
235
729
    retval = -1;  goto bailout;
236
729
  }
237
32.1M
  for (i = 0; i < (int)croppedHeight; i++) {
238
32.1M
    if (this->bottomUp)
239
24.9M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
240
7.24M
    else
241
7.24M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
242
32.1M
  }
243
244
6.98k
#if BITS_IN_JSAMPLE != 16
245
7.71k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
246
0
    if (this->croppingRegion.y != 0) {
247
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
248
249
0
      if ((int)lines != this->croppingRegion.y)
250
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
251
0
               "actual (%d) cropping region upper boundary",
252
0
               this->croppingRegion.y, (int)lines);
253
0
    }
254
0
    while ((int)dinfo->output_scanline <
255
0
           this->croppingRegion.y + this->croppingRegion.h)
256
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
257
0
                                               this->croppingRegion.y],
258
0
                           this->croppingRegion.y + this->croppingRegion.h -
259
0
                           dinfo->output_scanline);
260
0
    if (this->croppingRegion.y + this->croppingRegion.h !=
261
0
        (int)dinfo->output_height) {
262
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
263
0
                                                     this->croppingRegion.y -
264
0
                                                     this->croppingRegion.h);
265
266
0
      if (lines != dinfo->output_height - this->croppingRegion.y -
267
0
                   this->croppingRegion.h)
268
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
269
0
               "actual (%d) cropping region lower boundary",
270
0
               this->croppingRegion.y + this->croppingRegion.h,
271
0
               (int)(dinfo->output_height - lines));
272
0
    }
273
0
  } else
274
6.98k
#endif
275
6.98k
  {
276
14.1M
    while (dinfo->output_scanline < dinfo->output_height)
277
14.1M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
278
14.1M
                           dinfo->output_height - dinfo->output_scanline);
279
6.98k
  }
280
6.98k
  jpeg_finish_decompress(dinfo);
281
282
9.98k
bailout:
283
9.98k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
284
9.98k
  free(row_pointer);
285
9.98k
  if (this->jerr.warning) retval = -1;
286
9.98k
  return retval;
287
6.98k
}
tj3Decompress16
Line
Count
Source
151
1.92k
{
152
1.92k
  static const char FUNCTION_NAME[] =
153
1.92k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
154
1.92k
  _JSAMPROW *row_pointer = NULL;
155
1.92k
  int croppedHeight, i, retval = 0;
156
#if BITS_IN_JSAMPLE != 16
157
  int scaledWidth;
158
#endif
159
1.92k
  struct my_progress_mgr progress;
160
161
1.92k
  GET_DINSTANCE(handle);
162
1.92k
  if ((this->init & DECOMPRESS) == 0)
163
1.92k
    THROW("Instance has not been initialized for decompression");
164
165
1.92k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
166
1.92k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
167
1.92k
    THROW("Invalid argument");
168
169
1.92k
  if (this->scanLimit) {
170
1.92k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
171
1.92k
    progress.pub.progress_monitor = my_progress_monitor;
172
1.92k
    progress.this = this;
173
1.92k
    dinfo->progress = &progress.pub;
174
1.92k
  } else
175
0
    dinfo->progress = NULL;
176
177
1.92k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
178
179
1.92k
  if (setjmp(this->jerr.setjmp_buffer)) {
180
    /* If we get here, the JPEG code has signaled an error. */
181
583
    retval = -1;  goto bailout;
182
583
  }
183
184
1.92k
  if (dinfo->global_state <= DSTATE_INHEADER) {
185
1.92k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
186
1.92k
    jpeg_read_header(dinfo, TRUE);
187
1.92k
  }
188
1.33k
  setDecompParameters(this);
189
1.33k
  if (this->maxPixels &&
190
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
191
0
      (unsigned long long)this->maxPixels)
192
1.33k
    THROW("Image is too large");
193
1.33k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
194
#if BITS_IN_JSAMPLE != 16
195
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
196
#endif
197
1.33k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
198
1.33k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
199
200
1.33k
  dinfo->scale_num = this->scalingFactor.num;
201
1.33k
  dinfo->scale_denom = this->scalingFactor.denom;
202
203
1.33k
  jpeg_start_decompress(dinfo);
204
205
#if BITS_IN_JSAMPLE != 16
206
  if (this->croppingRegion.x != 0 ||
207
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
208
    JDIMENSION crop_x = this->croppingRegion.x;
209
    JDIMENSION crop_w = this->croppingRegion.w;
210
211
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
212
    if ((int)crop_x != this->croppingRegion.x)
213
      THROWI("Unexplained mismatch between specified (%d) and\n"
214
             "actual (%d) cropping region left boundary",
215
             this->croppingRegion.x, (int)crop_x);
216
    if ((int)crop_w != this->croppingRegion.w)
217
      THROWI("Unexplained mismatch between specified (%d) and\n"
218
             "actual (%d) cropping region width",
219
             this->croppingRegion.w, (int)crop_w);
220
  }
221
#endif
222
223
1.33k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
224
225
1.33k
  croppedHeight = dinfo->output_height;
226
#if BITS_IN_JSAMPLE != 16
227
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
228
    croppedHeight = this->croppingRegion.h;
229
#endif
230
1.33k
  if ((row_pointer =
231
1.33k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
232
1.33k
    THROW("Memory allocation failure");
233
1.33k
  if (setjmp(this->jerr.setjmp_buffer)) {
234
    /* If we get here, the JPEG code has signaled an error. */
235
968
    retval = -1;  goto bailout;
236
968
  }
237
9.41M
  for (i = 0; i < (int)croppedHeight; i++) {
238
9.41M
    if (this->bottomUp)
239
9.41M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
240
1.58k
    else
241
1.58k
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
242
9.41M
  }
243
244
#if BITS_IN_JSAMPLE != 16
245
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
246
    if (this->croppingRegion.y != 0) {
247
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
248
249
      if ((int)lines != this->croppingRegion.y)
250
        THROWI("Unexplained mismatch between specified (%d) and\n"
251
               "actual (%d) cropping region upper boundary",
252
               this->croppingRegion.y, (int)lines);
253
    }
254
    while ((int)dinfo->output_scanline <
255
           this->croppingRegion.y + this->croppingRegion.h)
256
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
257
                                               this->croppingRegion.y],
258
                           this->croppingRegion.y + this->croppingRegion.h -
259
                           dinfo->output_scanline);
260
    if (this->croppingRegion.y + this->croppingRegion.h !=
261
        (int)dinfo->output_height) {
262
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
263
                                                     this->croppingRegion.y -
264
                                                     this->croppingRegion.h);
265
266
      if (lines != dinfo->output_height - this->croppingRegion.y -
267
                   this->croppingRegion.h)
268
        THROWI("Unexplained mismatch between specified (%d) and\n"
269
               "actual (%d) cropping region lower boundary",
270
               this->croppingRegion.y + this->croppingRegion.h,
271
               (int)(dinfo->output_height - lines));
272
    }
273
  } else
274
#endif
275
370
  {
276
653k
    while (dinfo->output_scanline < dinfo->output_height)
277
653k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
278
653k
                           dinfo->output_height - dinfo->output_scanline);
279
370
  }
280
370
  jpeg_finish_decompress(dinfo);
281
282
1.92k
bailout:
283
1.92k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
284
1.92k
  free(row_pointer);
285
1.92k
  if (this->jerr.warning) retval = -1;
286
1.92k
  return retval;
287
370
}
288
289
290
/*************************** Packed-Pixel Image I/O **************************/
291
292
/* TurboJPEG 3.0+ */
293
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
294
  (tjhandle handle, const char *filename, int *width, int align, int *height,
295
   int *pixelFormat)
296
212k
{
297
212k
  static const char FUNCTION_NAME[] =
298
212k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
299
300
212k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
301
302
212k
  int retval = 0, tempc;
303
212k
  size_t pitch;
304
212k
  tjhandle handle2 = NULL;
305
212k
  tjinstance *this2;
306
212k
  j_compress_ptr cinfo = NULL;
307
212k
  cjpeg_source_ptr src;
308
212k
  _JSAMPLE *dstBuf = NULL;
309
212k
  FILE *file = NULL;
310
212k
  boolean invert;
311
312
212k
  GET_TJINSTANCE(handle, NULL)
313
314
212k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
315
212k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
316
212k
    THROW("Invalid argument");
317
212k
  if ((align & (align - 1)) != 0)
318
212k
    THROW("Alignment must be a power of 2");
319
320
  /* The instance handle passed to this function is used only for parameter
321
     retrieval.  Create a new temporary instance to avoid interfering with the
322
     libjpeg state of the primary instance. */
323
212k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
324
212k
  this2 = (tjinstance *)handle2;
325
212k
  cinfo = &this2->cinfo;
326
327
#ifdef _MSC_VER
328
  if (fopen_s(&file, filename, "rb") || file == NULL)
329
#else
330
212k
  if ((file = fopen(filename, "rb")) == NULL)
331
0
#endif
332
212k
    THROW_UNIX("Cannot open input file");
333
334
212k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
212k
  else if (tempc == EOF)
337
212k
    THROW("Input file contains no data");
338
339
212k
  if (setjmp(this2->jerr.setjmp_buffer)) {
340
    /* If we get here, the JPEG code has signaled an error. */
341
47.5k
    retval = -1;  goto bailout;
342
47.5k
  }
343
344
164k
  cinfo->data_precision = BITS_IN_JSAMPLE;
345
164k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
346
164k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
347
164k
  if (tempc == 'B') {
348
35.1k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
349
35.1k
      THROW("Could not initialize bitmap loader");
350
35.1k
    invert = !this->bottomUp;
351
175k
  } else if (tempc == 'P') {
352
#if BITS_IN_JSAMPLE == 8
353
93.7k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
354
#else
355
81.9k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
356
43.4k
        this->precision <= BITS_IN_JSAMPLE)
357
43.4k
#endif
358
137k
      cinfo->data_precision = this->precision;
359
175k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
360
175k
      THROW("Could not initialize PPM loader");
361
175k
    invert = this->bottomUp;
362
175k
  } else
363
18.4E
    THROW("Unsupported file type");
364
365
210k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
366
367
210k
  src->input_file = file;
368
  /* Refuse to load images larger than the specified size. */
369
210k
  src->max_pixels = this->maxPixels;
370
210k
  (*src->start_input) (cinfo, src);
371
210k
  if (tempc == 'B') {
372
11.2k
    if (cinfo->X_density && cinfo->Y_density) {
373
2.15k
      this->xDensity = cinfo->X_density;
374
2.15k
      this->yDensity = cinfo->Y_density;
375
2.15k
      this->densityUnits = cinfo->density_unit;
376
2.15k
    }
377
11.2k
  }
378
210k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
379
380
210k
  *width = cinfo->image_width;  *height = cinfo->image_height;
381
210k
  *pixelFormat = cs2pf[cinfo->in_color_space];
382
383
210k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
384
210k
  if (
385
#if ULLONG_MAX > SIZE_MAX
386
      (unsigned long long)pitch * (unsigned long long)(*height) >
387
      (unsigned long long)((size_t)-1) ||
388
#endif
389
210k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
390
210k
                                   sizeof(_JSAMPLE))) == NULL)
391
210k
    THROW("Memory allocation failure");
392
393
210k
  if (setjmp(this2->jerr.setjmp_buffer)) {
394
    /* If we get here, the JPEG code has signaled an error. */
395
64.5k
    retval = -1;  goto bailout;
396
64.5k
  }
397
398
373M
  while (cinfo->next_scanline < cinfo->image_height) {
399
373M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
400
401
747M
    for (i = 0; i < nlines; i++) {
402
373M
      _JSAMPLE *dstptr;
403
373M
      int row;
404
405
373M
      row = cinfo->next_scanline + i;
406
373M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
407
303M
      else dstptr = &dstBuf[row * pitch];
408
373M
      memcpy(dstptr, src->_buffer[i],
409
373M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
410
373M
    }
411
373M
    cinfo->next_scanline += nlines;
412
373M
  }
413
414
146k
  (*src->finish_input) (cinfo, src);
415
416
212k
bailout:
417
212k
  tj3Destroy(handle2);
418
212k
  if (file) fclose(file);
419
212k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
420
212k
  return dstBuf;
421
422
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
423
424
  static const char ERROR_MSG[] =
425
    "16-bit data precision requires lossless JPEG,\n"
426
    "which was disabled at build time.";
427
  _JSAMPLE *retval = NULL;
428
429
  GET_TJINSTANCE(handle, NULL)
430
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
431
           ERROR_MSG);
432
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
433
434
bailout:
435
  return retval;
436
437
#endif
438
146k
}
tj3LoadImage8
Line
Count
Source
296
129k
{
297
129k
  static const char FUNCTION_NAME[] =
298
129k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
299
300
129k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
301
302
129k
  int retval = 0, tempc;
303
129k
  size_t pitch;
304
129k
  tjhandle handle2 = NULL;
305
129k
  tjinstance *this2;
306
129k
  j_compress_ptr cinfo = NULL;
307
129k
  cjpeg_source_ptr src;
308
129k
  _JSAMPLE *dstBuf = NULL;
309
129k
  FILE *file = NULL;
310
129k
  boolean invert;
311
312
129k
  GET_TJINSTANCE(handle, NULL)
313
314
129k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
315
129k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
316
129k
    THROW("Invalid argument");
317
129k
  if ((align & (align - 1)) != 0)
318
129k
    THROW("Alignment must be a power of 2");
319
320
  /* The instance handle passed to this function is used only for parameter
321
     retrieval.  Create a new temporary instance to avoid interfering with the
322
     libjpeg state of the primary instance. */
323
129k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
324
129k
  this2 = (tjinstance *)handle2;
325
129k
  cinfo = &this2->cinfo;
326
327
#ifdef _MSC_VER
328
  if (fopen_s(&file, filename, "rb") || file == NULL)
329
#else
330
129k
  if ((file = fopen(filename, "rb")) == NULL)
331
0
#endif
332
129k
    THROW_UNIX("Cannot open input file");
333
334
129k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
129k
  else if (tempc == EOF)
337
129k
    THROW("Input file contains no data");
338
339
129k
  if (setjmp(this2->jerr.setjmp_buffer)) {
340
    /* If we get here, the JPEG code has signaled an error. */
341
35.5k
    retval = -1;  goto bailout;
342
35.5k
  }
343
344
94.0k
  cinfo->data_precision = BITS_IN_JSAMPLE;
345
94.0k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
346
94.0k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
347
94.0k
  if (tempc == 'B') {
348
35.1k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
349
35.1k
      THROW("Could not initialize bitmap loader");
350
35.1k
    invert = !this->bottomUp;
351
93.7k
  } else if (tempc == 'P') {
352
93.7k
#if BITS_IN_JSAMPLE == 8
353
93.7k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
354
#else
355
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
356
        this->precision <= BITS_IN_JSAMPLE)
357
#endif
358
93.7k
      cinfo->data_precision = this->precision;
359
93.7k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
360
93.7k
      THROW("Could not initialize PPM loader");
361
93.7k
    invert = this->bottomUp;
362
93.7k
  } else
363
18.4E
    THROW("Unsupported file type");
364
365
128k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
366
367
128k
  src->input_file = file;
368
  /* Refuse to load images larger than the specified size. */
369
128k
  src->max_pixels = this->maxPixels;
370
128k
  (*src->start_input) (cinfo, src);
371
128k
  if (tempc == 'B') {
372
11.2k
    if (cinfo->X_density && cinfo->Y_density) {
373
2.15k
      this->xDensity = cinfo->X_density;
374
2.15k
      this->yDensity = cinfo->Y_density;
375
2.15k
      this->densityUnits = cinfo->density_unit;
376
2.15k
    }
377
11.2k
  }
378
128k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
379
380
128k
  *width = cinfo->image_width;  *height = cinfo->image_height;
381
128k
  *pixelFormat = cs2pf[cinfo->in_color_space];
382
383
128k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
384
128k
  if (
385
#if ULLONG_MAX > SIZE_MAX
386
      (unsigned long long)pitch * (unsigned long long)(*height) >
387
      (unsigned long long)((size_t)-1) ||
388
#endif
389
128k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
390
128k
                                   sizeof(_JSAMPLE))) == NULL)
391
128k
    THROW("Memory allocation failure");
392
393
128k
  if (setjmp(this2->jerr.setjmp_buffer)) {
394
    /* If we get here, the JPEG code has signaled an error. */
395
36.4k
    retval = -1;  goto bailout;
396
36.4k
  }
397
398
192M
  while (cinfo->next_scanline < cinfo->image_height) {
399
192M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
400
401
385M
    for (i = 0; i < nlines; i++) {
402
192M
      _JSAMPLE *dstptr;
403
192M
      int row;
404
405
192M
      row = cinfo->next_scanline + i;
406
192M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
407
148M
      else dstptr = &dstBuf[row * pitch];
408
192M
      memcpy(dstptr, src->_buffer[i],
409
192M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
410
192M
    }
411
192M
    cinfo->next_scanline += nlines;
412
192M
  }
413
414
92.5k
  (*src->finish_input) (cinfo, src);
415
416
129k
bailout:
417
129k
  tj3Destroy(handle2);
418
129k
  if (file) fclose(file);
419
129k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
420
129k
  return dstBuf;
421
422
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
423
424
  static const char ERROR_MSG[] =
425
    "16-bit data precision requires lossless JPEG,\n"
426
    "which was disabled at build time.";
427
  _JSAMPLE *retval = NULL;
428
429
  GET_TJINSTANCE(handle, NULL)
430
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
431
           ERROR_MSG);
432
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
433
434
bailout:
435
  return retval;
436
437
#endif
438
92.5k
}
tj3LoadImage12
Line
Count
Source
296
60.3k
{
297
60.3k
  static const char FUNCTION_NAME[] =
298
60.3k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
299
300
60.3k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
301
302
60.3k
  int retval = 0, tempc;
303
60.3k
  size_t pitch;
304
60.3k
  tjhandle handle2 = NULL;
305
60.3k
  tjinstance *this2;
306
60.3k
  j_compress_ptr cinfo = NULL;
307
60.3k
  cjpeg_source_ptr src;
308
60.3k
  _JSAMPLE *dstBuf = NULL;
309
60.3k
  FILE *file = NULL;
310
60.3k
  boolean invert;
311
312
60.3k
  GET_TJINSTANCE(handle, NULL)
313
314
60.3k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
315
60.3k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
316
60.3k
    THROW("Invalid argument");
317
60.3k
  if ((align & (align - 1)) != 0)
318
60.3k
    THROW("Alignment must be a power of 2");
319
320
  /* The instance handle passed to this function is used only for parameter
321
     retrieval.  Create a new temporary instance to avoid interfering with the
322
     libjpeg state of the primary instance. */
323
60.3k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
324
60.3k
  this2 = (tjinstance *)handle2;
325
60.3k
  cinfo = &this2->cinfo;
326
327
#ifdef _MSC_VER
328
  if (fopen_s(&file, filename, "rb") || file == NULL)
329
#else
330
60.3k
  if ((file = fopen(filename, "rb")) == NULL)
331
0
#endif
332
60.3k
    THROW_UNIX("Cannot open input file");
333
334
60.3k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
60.3k
  else if (tempc == EOF)
337
60.3k
    THROW("Input file contains no data");
338
339
60.3k
  if (setjmp(this2->jerr.setjmp_buffer)) {
340
    /* If we get here, the JPEG code has signaled an error. */
341
8.11k
    retval = -1;  goto bailout;
342
8.11k
  }
343
344
52.2k
  cinfo->data_precision = BITS_IN_JSAMPLE;
345
52.2k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
346
52.2k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
347
52.2k
  if (tempc == 'B') {
348
28
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
349
28
      THROW("Could not initialize bitmap loader");
350
28
    invert = !this->bottomUp;
351
59.8k
  } else if (tempc == 'P') {
352
#if BITS_IN_JSAMPLE == 8
353
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
354
#else
355
59.8k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
356
21.2k
        this->precision <= BITS_IN_JSAMPLE)
357
21.2k
#endif
358
21.2k
      cinfo->data_precision = this->precision;
359
59.8k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
360
59.8k
      THROW("Could not initialize PPM loader");
361
59.8k
    invert = this->bottomUp;
362
59.8k
  } else
363
18.4E
    THROW("Unsupported file type");
364
365
59.8k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
366
367
59.8k
  src->input_file = file;
368
  /* Refuse to load images larger than the specified size. */
369
59.8k
  src->max_pixels = this->maxPixels;
370
59.8k
  (*src->start_input) (cinfo, src);
371
59.8k
  if (tempc == 'B') {
372
0
    if (cinfo->X_density && cinfo->Y_density) {
373
0
      this->xDensity = cinfo->X_density;
374
0
      this->yDensity = cinfo->Y_density;
375
0
      this->densityUnits = cinfo->density_unit;
376
0
    }
377
0
  }
378
59.8k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
379
380
59.8k
  *width = cinfo->image_width;  *height = cinfo->image_height;
381
59.8k
  *pixelFormat = cs2pf[cinfo->in_color_space];
382
383
59.8k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
384
59.8k
  if (
385
#if ULLONG_MAX > SIZE_MAX
386
      (unsigned long long)pitch * (unsigned long long)(*height) >
387
      (unsigned long long)((size_t)-1) ||
388
#endif
389
59.8k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
390
59.8k
                                   sizeof(_JSAMPLE))) == NULL)
391
59.8k
    THROW("Memory allocation failure");
392
393
59.8k
  if (setjmp(this2->jerr.setjmp_buffer)) {
394
    /* If we get here, the JPEG code has signaled an error. */
395
18.1k
    retval = -1;  goto bailout;
396
18.1k
  }
397
398
149M
  while (cinfo->next_scanline < cinfo->image_height) {
399
148M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
400
401
297M
    for (i = 0; i < nlines; i++) {
402
148M
      _JSAMPLE *dstptr;
403
148M
      int row;
404
405
148M
      row = cinfo->next_scanline + i;
406
148M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
407
127M
      else dstptr = &dstBuf[row * pitch];
408
148M
      memcpy(dstptr, src->_buffer[i],
409
148M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
410
148M
    }
411
148M
    cinfo->next_scanline += nlines;
412
148M
  }
413
414
41.7k
  (*src->finish_input) (cinfo, src);
415
416
60.3k
bailout:
417
60.3k
  tj3Destroy(handle2);
418
60.3k
  if (file) fclose(file);
419
60.3k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
420
60.3k
  return dstBuf;
421
422
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
423
424
  static const char ERROR_MSG[] =
425
    "16-bit data precision requires lossless JPEG,\n"
426
    "which was disabled at build time.";
427
  _JSAMPLE *retval = NULL;
428
429
  GET_TJINSTANCE(handle, NULL)
430
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
431
           ERROR_MSG);
432
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
433
434
bailout:
435
  return retval;
436
437
#endif
438
41.7k
}
tj3LoadImage16
Line
Count
Source
296
22.4k
{
297
22.4k
  static const char FUNCTION_NAME[] =
298
22.4k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
299
300
22.4k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
301
302
22.4k
  int retval = 0, tempc;
303
22.4k
  size_t pitch;
304
22.4k
  tjhandle handle2 = NULL;
305
22.4k
  tjinstance *this2;
306
22.4k
  j_compress_ptr cinfo = NULL;
307
22.4k
  cjpeg_source_ptr src;
308
22.4k
  _JSAMPLE *dstBuf = NULL;
309
22.4k
  FILE *file = NULL;
310
22.4k
  boolean invert;
311
312
22.4k
  GET_TJINSTANCE(handle, NULL)
313
314
22.4k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
315
22.4k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
316
22.4k
    THROW("Invalid argument");
317
22.4k
  if ((align & (align - 1)) != 0)
318
22.4k
    THROW("Alignment must be a power of 2");
319
320
  /* The instance handle passed to this function is used only for parameter
321
     retrieval.  Create a new temporary instance to avoid interfering with the
322
     libjpeg state of the primary instance. */
323
22.4k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
324
22.4k
  this2 = (tjinstance *)handle2;
325
22.4k
  cinfo = &this2->cinfo;
326
327
#ifdef _MSC_VER
328
  if (fopen_s(&file, filename, "rb") || file == NULL)
329
#else
330
22.4k
  if ((file = fopen(filename, "rb")) == NULL)
331
0
#endif
332
22.4k
    THROW_UNIX("Cannot open input file");
333
334
22.4k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
22.4k
  else if (tempc == EOF)
337
22.4k
    THROW("Input file contains no data");
338
339
22.4k
  if (setjmp(this2->jerr.setjmp_buffer)) {
340
    /* If we get here, the JPEG code has signaled an error. */
341
3.85k
    retval = -1;  goto bailout;
342
3.85k
  }
343
344
18.6k
  cinfo->data_precision = BITS_IN_JSAMPLE;
345
18.6k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
346
18.6k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
347
18.6k
  if (tempc == 'B') {
348
14
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
349
14
      THROW("Could not initialize bitmap loader");
350
14
    invert = !this->bottomUp;
351
22.1k
  } else if (tempc == 'P') {
352
#if BITS_IN_JSAMPLE == 8
353
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
354
#else
355
22.1k
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
356
22.1k
        this->precision <= BITS_IN_JSAMPLE)
357
22.1k
#endif
358
22.1k
      cinfo->data_precision = this->precision;
359
22.1k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
360
22.1k
      THROW("Could not initialize PPM loader");
361
22.1k
    invert = this->bottomUp;
362
22.1k
  } else
363
18.4E
    THROW("Unsupported file type");
364
365
22.1k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
366
367
22.1k
  src->input_file = file;
368
  /* Refuse to load images larger than the specified size. */
369
22.1k
  src->max_pixels = this->maxPixels;
370
22.1k
  (*src->start_input) (cinfo, src);
371
22.1k
  if (tempc == 'B') {
372
0
    if (cinfo->X_density && cinfo->Y_density) {
373
0
      this->xDensity = cinfo->X_density;
374
0
      this->yDensity = cinfo->Y_density;
375
0
      this->densityUnits = cinfo->density_unit;
376
0
    }
377
0
  }
378
22.1k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
379
380
22.1k
  *width = cinfo->image_width;  *height = cinfo->image_height;
381
22.1k
  *pixelFormat = cs2pf[cinfo->in_color_space];
382
383
22.1k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
384
22.1k
  if (
385
#if ULLONG_MAX > SIZE_MAX
386
      (unsigned long long)pitch * (unsigned long long)(*height) >
387
      (unsigned long long)((size_t)-1) ||
388
#endif
389
22.1k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
390
22.1k
                                   sizeof(_JSAMPLE))) == NULL)
391
22.1k
    THROW("Memory allocation failure");
392
393
22.1k
  if (setjmp(this2->jerr.setjmp_buffer)) {
394
    /* If we get here, the JPEG code has signaled an error. */
395
9.96k
    retval = -1;  goto bailout;
396
9.96k
  }
397
398
32.1M
  while (cinfo->next_scanline < cinfo->image_height) {
399
32.1M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
400
401
64.2M
    for (i = 0; i < nlines; i++) {
402
32.0M
      _JSAMPLE *dstptr;
403
32.0M
      int row;
404
405
32.0M
      row = cinfo->next_scanline + i;
406
32.0M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
407
27.5M
      else dstptr = &dstBuf[row * pitch];
408
32.0M
      memcpy(dstptr, src->_buffer[i],
409
32.0M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
410
32.0M
    }
411
32.1M
    cinfo->next_scanline += nlines;
412
32.1M
  }
413
414
12.2k
  (*src->finish_input) (cinfo, src);
415
416
22.4k
bailout:
417
22.4k
  tj3Destroy(handle2);
418
22.4k
  if (file) fclose(file);
419
22.4k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
420
22.4k
  return dstBuf;
421
422
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
423
424
  static const char ERROR_MSG[] =
425
    "16-bit data precision requires lossless JPEG,\n"
426
    "which was disabled at build time.";
427
  _JSAMPLE *retval = NULL;
428
429
  GET_TJINSTANCE(handle, NULL)
430
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
431
           ERROR_MSG);
432
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
433
434
bailout:
435
  return retval;
436
437
#endif
438
12.2k
}
439
440
441
/* TurboJPEG 3.0+ */
442
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
443
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
444
   int pitch, int height, int pixelFormat)
445
0
{
446
0
  static const char FUNCTION_NAME[] =
447
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
448
0
  int retval = 0;
449
450
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
451
452
0
  tjhandle handle2 = NULL;
453
0
  tjinstance *this2;
454
0
  j_decompress_ptr dinfo = NULL;
455
0
  djpeg_dest_ptr dst;
456
0
  FILE *file = NULL;
457
0
  char *ptr = NULL;
458
0
  boolean invert;
459
460
0
  GET_TJINSTANCE(handle, -1)
461
462
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
463
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
464
0
    THROW("Invalid argument");
465
466
  /* The instance handle passed to this function is used only for parameter
467
     retrieval.  Create a new temporary instance to avoid interfering with the
468
     libjpeg state of the primary instance. */
469
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
470
0
    return -1;
471
0
  this2 = (tjinstance *)handle2;
472
0
  dinfo = &this2->dinfo;
473
474
#ifdef _MSC_VER
475
  if (fopen_s(&file, filename, "wb") || file == NULL)
476
#else
477
0
  if ((file = fopen(filename, "wb")) == NULL)
478
0
#endif
479
0
    THROW_UNIX("Cannot open output file");
480
481
0
  if (setjmp(this2->jerr.setjmp_buffer)) {
482
    /* If we get here, the JPEG code has signaled an error. */
483
0
    retval = -1;  goto bailout;
484
0
  }
485
486
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
487
0
  dinfo->image_width = width;  dinfo->image_height = height;
488
0
  dinfo->global_state = DSTATE_READY;
489
0
  dinfo->scale_num = dinfo->scale_denom = 1;
490
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
491
492
0
  ptr = strrchr(filename, '.');
493
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
494
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
495
0
      THROW("Could not initialize bitmap writer");
496
0
    invert = !this->bottomUp;
497
0
    dinfo->X_density = (UINT16)this->xDensity;
498
0
    dinfo->Y_density = (UINT16)this->yDensity;
499
0
    dinfo->density_unit = (UINT8)this->densityUnits;
500
0
  } else {
501
#if BITS_IN_JSAMPLE == 8
502
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
503
#else
504
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
505
0
        this->precision <= BITS_IN_JSAMPLE)
506
0
#endif
507
0
      dinfo->data_precision = this->precision;
508
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
509
0
      THROW("Could not initialize PPM writer");
510
0
    invert = this->bottomUp;
511
0
  }
512
513
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
514
515
0
  dst->output_file = file;
516
0
  (*dst->start_output) (dinfo, dst);
517
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
518
519
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
520
521
0
  while (dinfo->output_scanline < dinfo->output_height) {
522
0
    _JSAMPLE *rowptr;
523
524
0
    if (invert)
525
0
      rowptr =
526
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
527
0
    else
528
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
529
0
    memcpy(dst->_buffer[0], rowptr,
530
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
531
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
532
0
    dinfo->output_scanline++;
533
0
  }
534
535
0
  (*dst->finish_output) (dinfo, dst);
536
537
0
bailout:
538
0
  tj3Destroy(handle2);
539
0
  if (file) fclose(file);
540
0
  return retval;
541
542
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
543
544
  GET_TJINSTANCE(handle, -1)
545
  THROW("16-bit data precision requires lossless JPEG,\n"
546
        "which was disabled at build time.")
547
bailout:
548
  return retval;
549
550
#endif
551
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
552
553
554
#undef _JSAMPLE
555
#undef _JSAMPROW
556
#undef _buffer
557
#undef _jinit_read_ppm
558
#undef _jinit_write_ppm
559
#undef _jpeg_crop_scanline
560
#undef _jpeg_read_scanlines
561
#undef _jpeg_skip_scanlines
562
#undef _jpeg_write_scanlines