Coverage Report

Created: 2025-10-10 07:05

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