Coverage Report

Created: 2025-10-13 06:06

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
104M
#define _JSAMPLE  JSAMPLE
34
29.4k
#define _JSAMPROW  JSAMPROW
35
104M
#define _buffer  buffer
36
46.6k
#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
17.5k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
72.9M
#define _JSAMPLE  J12SAMPLE
44
27.3k
#define _JSAMPROW  J12SAMPROW
45
72.8M
#define _buffer  buffer12
46
30.1k
#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
17.2k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
13.3M
#define _JSAMPLE  J16SAMPLE
54
6.31k
#define _JSAMPROW  J16SAMPROW
55
13.3M
#define _buffer  buffer16
56
10.7k
#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
4.36k
#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
169k
#define _GET_STRING(name, suffix)  #name #suffix
65
169k
#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
39.4k
{
75
39.4k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
39.4k
  int i, retval = 0;
77
39.4k
  boolean alloc = TRUE;
78
39.4k
  _JSAMPROW *row_pointer = NULL;
79
80
39.4k
  GET_CINSTANCE(handle)
81
39.4k
  if ((this->init & COMPRESS) == 0)
82
39.4k
    THROW("Instance has not been initialized for compression");
83
84
39.4k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
39.4k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
39.4k
      jpegSize == NULL)
87
39.4k
    THROW("Invalid argument");
88
89
39.4k
  if (!this->lossless && this->quality == -1)
90
39.4k
    THROW("TJPARAM_QUALITY must be specified");
91
39.4k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
39.4k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
39.4k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
39.4k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
39.4k
    THROW("Memory allocation failure");
98
99
39.4k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
279
    retval = -1;  goto bailout;
102
279
  }
103
104
39.1k
  cinfo->image_width = width;
105
39.1k
  cinfo->image_height = height;
106
39.1k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
39.1k
  setCompDefaults(this, pixelFormat);
109
39.1k
  if (this->noRealloc) {
110
33.6k
    alloc = FALSE;
111
33.6k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
33.6k
  }
113
39.1k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
39.1k
  jpeg_start_compress(cinfo, TRUE);
116
145M
  for (i = 0; i < height; i++) {
117
145M
    if (this->bottomUp)
118
20.9M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
124M
    else
120
124M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
145M
  }
122
78.2k
  while (cinfo->next_scanline < cinfo->image_height)
123
39.1k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
39.1k
                          cinfo->image_height - cinfo->next_scanline);
125
39.1k
  jpeg_finish_compress(cinfo);
126
127
39.4k
bailout:
128
39.4k
  if (cinfo->global_state > CSTATE_START && alloc)
129
36
    (*cinfo->dest->term_destination) (cinfo);
130
39.4k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
279
    jpeg_abort_compress(cinfo);
132
39.4k
  free(row_pointer);
133
39.4k
  if (this->jerr.warning) retval = -1;
134
39.4k
  return retval;
135
39.1k
}
tj3Compress8
Line
Count
Source
74
17.6k
{
75
17.6k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
17.6k
  int i, retval = 0;
77
17.6k
  boolean alloc = TRUE;
78
17.6k
  _JSAMPROW *row_pointer = NULL;
79
80
17.6k
  GET_CINSTANCE(handle)
81
17.6k
  if ((this->init & COMPRESS) == 0)
82
17.6k
    THROW("Instance has not been initialized for compression");
83
84
17.6k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
17.6k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
17.6k
      jpegSize == NULL)
87
17.6k
    THROW("Invalid argument");
88
89
17.6k
  if (!this->lossless && this->quality == -1)
90
17.6k
    THROW("TJPARAM_QUALITY must be specified");
91
17.6k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
17.6k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
17.6k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
17.6k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
17.6k
    THROW("Memory allocation failure");
98
99
17.6k
  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.5k
  cinfo->image_width = width;
105
17.5k
  cinfo->image_height = height;
106
17.5k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
17.5k
  setCompDefaults(this, pixelFormat);
109
17.5k
  if (this->noRealloc) {
110
15.0k
    alloc = FALSE;
111
15.0k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
15.0k
  }
113
17.5k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
17.5k
  jpeg_start_compress(cinfo, TRUE);
116
62.4M
  for (i = 0; i < height; i++) {
117
62.4M
    if (this->bottomUp)
118
9.01M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
53.4M
    else
120
53.4M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
62.4M
  }
122
35.0k
  while (cinfo->next_scanline < cinfo->image_height)
123
17.5k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
17.5k
                          cinfo->image_height - cinfo->next_scanline);
125
17.5k
  jpeg_finish_compress(cinfo);
126
127
17.6k
bailout:
128
17.6k
  if (cinfo->global_state > CSTATE_START && alloc)
129
0
    (*cinfo->dest->term_destination) (cinfo);
130
17.6k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
135
    jpeg_abort_compress(cinfo);
132
17.6k
  free(row_pointer);
133
17.6k
  if (this->jerr.warning) retval = -1;
134
17.6k
  return retval;
135
17.5k
}
tj3Compress12
Line
Count
Source
74
17.3k
{
75
17.3k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
17.3k
  int i, retval = 0;
77
17.3k
  boolean alloc = TRUE;
78
17.3k
  _JSAMPROW *row_pointer = NULL;
79
80
17.3k
  GET_CINSTANCE(handle)
81
17.3k
  if ((this->init & COMPRESS) == 0)
82
17.3k
    THROW("Instance has not been initialized for compression");
83
84
17.3k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
17.3k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
17.3k
      jpegSize == NULL)
87
17.3k
    THROW("Invalid argument");
88
89
17.3k
  if (!this->lossless && this->quality == -1)
90
17.3k
    THROW("TJPARAM_QUALITY must be specified");
91
17.3k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
17.3k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
17.3k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
17.3k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
17.3k
    THROW("Memory allocation failure");
98
99
17.3k
  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.2k
  cinfo->image_width = width;
105
17.2k
  cinfo->image_height = height;
106
17.2k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
17.2k
  setCompDefaults(this, pixelFormat);
109
17.2k
  if (this->noRealloc) {
110
14.8k
    alloc = FALSE;
111
14.8k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
14.8k
  }
113
17.2k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
17.2k
  jpeg_start_compress(cinfo, TRUE);
116
70.4M
  for (i = 0; i < height; i++) {
117
70.4M
    if (this->bottomUp)
118
10.0M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
60.3M
    else
120
60.3M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
70.4M
  }
122
34.5k
  while (cinfo->next_scanline < cinfo->image_height)
123
17.2k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
17.2k
                          cinfo->image_height - cinfo->next_scanline);
125
17.2k
  jpeg_finish_compress(cinfo);
126
127
17.3k
bailout:
128
17.3k
  if (cinfo->global_state > CSTATE_START && alloc)
129
0
    (*cinfo->dest->term_destination) (cinfo);
130
17.3k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
81
    jpeg_abort_compress(cinfo);
132
17.3k
  free(row_pointer);
133
17.3k
  if (this->jerr.warning) retval = -1;
134
17.3k
  return retval;
135
17.2k
}
tj3Compress16
Line
Count
Source
74
4.39k
{
75
4.39k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
4.39k
  int i, retval = 0;
77
4.39k
  boolean alloc = TRUE;
78
4.39k
  _JSAMPROW *row_pointer = NULL;
79
80
4.39k
  GET_CINSTANCE(handle)
81
4.39k
  if ((this->init & COMPRESS) == 0)
82
4.39k
    THROW("Instance has not been initialized for compression");
83
84
4.39k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
4.39k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
4.39k
      jpegSize == NULL)
87
4.39k
    THROW("Invalid argument");
88
89
4.39k
  if (!this->lossless && this->quality == -1)
90
4.39k
    THROW("TJPARAM_QUALITY must be specified");
91
4.39k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
4.39k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
4.39k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
4.39k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
4.39k
    THROW("Memory allocation failure");
98
99
4.39k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
63
    retval = -1;  goto bailout;
102
63
  }
103
104
4.32k
  cinfo->image_width = width;
105
4.32k
  cinfo->image_height = height;
106
4.32k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
4.32k
  setCompDefaults(this, pixelFormat);
109
4.32k
  if (this->noRealloc) {
110
3.74k
    alloc = FALSE;
111
3.74k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
3.74k
  }
113
4.32k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
4.32k
  jpeg_start_compress(cinfo, TRUE);
116
12.8M
  for (i = 0; i < height; i++) {
117
12.8M
    if (this->bottomUp)
118
1.83M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
10.9M
    else
120
10.9M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
12.8M
  }
122
8.69k
  while (cinfo->next_scanline < cinfo->image_height)
123
4.36k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
4.36k
                          cinfo->image_height - cinfo->next_scanline);
125
4.32k
  jpeg_finish_compress(cinfo);
126
127
4.39k
bailout:
128
4.39k
  if (cinfo->global_state > CSTATE_START && alloc)
129
36
    (*cinfo->dest->term_destination) (cinfo);
130
4.39k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
63
    jpeg_abort_compress(cinfo);
132
4.39k
  free(row_pointer);
133
4.39k
  if (this->jerr.warning) retval = -1;
134
4.39k
  return retval;
135
4.32k
}
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.27k
    retval = -1;  goto bailout;
175
6.27k
  }
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.7M
  for (i = 0; i < (int)croppedHeight; i++) {
231
81.6M
    if (this->bottomUp)
232
70.9M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
10.7M
    else
234
10.7M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
81.6M
  }
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.9M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
33.9M
                           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.42k
    retval = -1;  goto bailout;
175
3.42k
  }
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.35k
  setDecompParameters(this);
182
8.35k
  if (this->maxPixels &&
183
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
184
0
      (unsigned long long)this->maxPixels)
185
8.35k
    THROW("Image is too large");
186
8.35k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
187
8.35k
#if BITS_IN_JSAMPLE != 16
188
8.35k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
189
8.35k
#endif
190
8.35k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
191
8.35k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
192
193
8.35k
  dinfo->scale_num = this->scalingFactor.num;
194
8.35k
  dinfo->scale_denom = this->scalingFactor.denom;
195
196
8.35k
  jpeg_start_decompress(dinfo);
197
198
8.35k
#if BITS_IN_JSAMPLE != 16
199
8.35k
  if (this->croppingRegion.x != 0 ||
200
8.35k
      (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.35k
#endif
215
216
8.35k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
217
218
8.35k
  croppedHeight = dinfo->output_height;
219
8.35k
#if BITS_IN_JSAMPLE != 16
220
8.35k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
221
0
    croppedHeight = this->croppingRegion.h;
222
8.35k
#endif
223
8.35k
  if ((row_pointer =
224
8.35k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
225
8.35k
    THROW("Memory allocation failure");
226
8.35k
  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.1M
  for (i = 0; i < (int)croppedHeight; i++) {
231
40.1M
    if (this->bottomUp)
232
36.6M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
3.45M
    else
234
3.45M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
40.1M
  }
236
237
7.26k
#if BITS_IN_JSAMPLE != 16
238
8.35k
  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.26k
#endif
268
7.26k
  {
269
19.0M
    while (dinfo->output_scanline < dinfo->output_height)
270
19.0M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
19.0M
                           dinfo->output_height - dinfo->output_scanline);
272
7.26k
  }
273
7.26k
  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.26k
}
tj3Decompress12
Line
Count
Source
144
9.98k
{
145
9.98k
  static const char FUNCTION_NAME[] =
146
9.98k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
9.98k
  _JSAMPROW *row_pointer = NULL;
148
9.98k
  int croppedHeight, i, retval = 0;
149
9.98k
#if BITS_IN_JSAMPLE != 16
150
9.98k
  int scaledWidth;
151
9.98k
#endif
152
9.98k
  struct my_progress_mgr progress;
153
154
9.98k
  GET_DINSTANCE(handle);
155
9.98k
  if ((this->init & DECOMPRESS) == 0)
156
9.98k
    THROW("Instance has not been initialized for decompression");
157
158
9.98k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
9.98k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
9.98k
    THROW("Invalid argument");
161
162
9.98k
  if (this->scanLimit) {
163
9.98k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
9.98k
    progress.pub.progress_monitor = my_progress_monitor;
165
9.98k
    progress.this = this;
166
9.98k
    dinfo->progress = &progress.pub;
167
9.98k
  } else
168
0
    dinfo->progress = NULL;
169
170
9.98k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
171
172
9.98k
  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
9.98k
  if (dinfo->global_state <= DSTATE_INHEADER) {
178
9.98k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
179
9.98k
    jpeg_read_header(dinfo, TRUE);
180
9.98k
  }
181
7.71k
  setDecompParameters(this);
182
7.71k
  if (this->maxPixels &&
183
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
184
0
      (unsigned long long)this->maxPixels)
185
7.71k
    THROW("Image is too large");
186
7.71k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
187
7.71k
#if BITS_IN_JSAMPLE != 16
188
7.71k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
189
7.71k
#endif
190
7.71k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
191
7.71k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
192
193
7.71k
  dinfo->scale_num = this->scalingFactor.num;
194
7.71k
  dinfo->scale_denom = this->scalingFactor.denom;
195
196
7.71k
  jpeg_start_decompress(dinfo);
197
198
7.71k
#if BITS_IN_JSAMPLE != 16
199
7.71k
  if (this->croppingRegion.x != 0 ||
200
7.71k
      (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.71k
#endif
215
216
7.71k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
217
218
7.71k
  croppedHeight = dinfo->output_height;
219
7.71k
#if BITS_IN_JSAMPLE != 16
220
7.71k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
221
0
    croppedHeight = this->croppingRegion.h;
222
7.71k
#endif
223
7.71k
  if ((row_pointer =
224
7.71k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
225
7.71k
    THROW("Memory allocation failure");
226
7.71k
  if (setjmp(this->jerr.setjmp_buffer)) {
227
    /* If we get here, the JPEG code has signaled an error. */
228
729
    retval = -1;  goto bailout;
229
729
  }
230
32.1M
  for (i = 0; i < (int)croppedHeight; i++) {
231
32.1M
    if (this->bottomUp)
232
24.9M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
7.24M
    else
234
7.24M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
32.1M
  }
236
237
6.98k
#if BITS_IN_JSAMPLE != 16
238
7.71k
  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
6.98k
#endif
268
6.98k
  {
269
14.1M
    while (dinfo->output_scanline < dinfo->output_height)
270
14.1M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
14.1M
                           dinfo->output_height - dinfo->output_scanline);
272
6.98k
  }
273
6.98k
  jpeg_finish_decompress(dinfo);
274
275
9.98k
bailout:
276
9.98k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
277
9.98k
  free(row_pointer);
278
9.98k
  if (this->jerr.warning) retval = -1;
279
9.98k
  return retval;
280
6.98k
}
tj3Decompress16
Line
Count
Source
144
1.92k
{
145
1.92k
  static const char FUNCTION_NAME[] =
146
1.92k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
1.92k
  _JSAMPROW *row_pointer = NULL;
148
1.92k
  int croppedHeight, i, retval = 0;
149
#if BITS_IN_JSAMPLE != 16
150
  int scaledWidth;
151
#endif
152
1.92k
  struct my_progress_mgr progress;
153
154
1.92k
  GET_DINSTANCE(handle);
155
1.92k
  if ((this->init & DECOMPRESS) == 0)
156
1.92k
    THROW("Instance has not been initialized for decompression");
157
158
1.92k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
1.92k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
1.92k
    THROW("Invalid argument");
161
162
1.92k
  if (this->scanLimit) {
163
1.92k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
1.92k
    progress.pub.progress_monitor = my_progress_monitor;
165
1.92k
    progress.this = this;
166
1.92k
    dinfo->progress = &progress.pub;
167
1.92k
  } else
168
0
    dinfo->progress = NULL;
169
170
1.92k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
171
172
1.92k
  if (setjmp(this->jerr.setjmp_buffer)) {
173
    /* If we get here, the JPEG code has signaled an error. */
174
583
    retval = -1;  goto bailout;
175
583
  }
176
177
1.92k
  if (dinfo->global_state <= DSTATE_INHEADER) {
178
1.92k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
179
1.92k
    jpeg_read_header(dinfo, TRUE);
180
1.92k
  }
181
1.33k
  setDecompParameters(this);
182
1.33k
  if (this->maxPixels &&
183
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
184
0
      (unsigned long long)this->maxPixels)
185
1.33k
    THROW("Image is too large");
186
1.33k
  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.33k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
191
1.33k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
192
193
1.33k
  dinfo->scale_num = this->scalingFactor.num;
194
1.33k
  dinfo->scale_denom = this->scalingFactor.denom;
195
196
1.33k
  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.33k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
217
218
1.33k
  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.33k
  if ((row_pointer =
224
1.33k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
225
1.33k
    THROW("Memory allocation failure");
226
1.33k
  if (setjmp(this->jerr.setjmp_buffer)) {
227
    /* If we get here, the JPEG code has signaled an error. */
228
968
    retval = -1;  goto bailout;
229
968
  }
230
9.41M
  for (i = 0; i < (int)croppedHeight; i++) {
231
9.41M
    if (this->bottomUp)
232
9.41M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
1.58k
    else
234
1.58k
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
9.41M
  }
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
370
  {
269
653k
    while (dinfo->output_scanline < dinfo->output_height)
270
653k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
653k
                           dinfo->output_height - dinfo->output_scanline);
272
370
  }
273
370
  jpeg_finish_decompress(dinfo);
274
275
1.92k
bailout:
276
1.92k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
277
1.92k
  free(row_pointer);
278
1.92k
  if (this->jerr.warning) retval = -1;
279
1.92k
  return retval;
280
370
}
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
106k
{
290
106k
  static const char FUNCTION_NAME[] =
291
106k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
106k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
294
295
106k
  int retval = 0, tempc;
296
106k
  size_t pitch;
297
106k
  tjhandle handle2 = NULL;
298
106k
  tjinstance *this2;
299
106k
  j_compress_ptr cinfo = NULL;
300
106k
  cjpeg_source_ptr src;
301
106k
  _JSAMPLE *dstBuf = NULL;
302
106k
  FILE *file = NULL;
303
106k
  boolean invert;
304
305
106k
  GET_TJINSTANCE(handle, NULL)
306
307
106k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
308
106k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
309
106k
    THROW("Invalid argument");
310
106k
  if ((align & (align - 1)) != 0)
311
106k
    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
106k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
317
106k
  this2 = (tjinstance *)handle2;
318
106k
  cinfo = &this2->cinfo;
319
320
#ifdef _MSC_VER
321
  if (fopen_s(&file, filename, "rb") || file == NULL)
322
#else
323
106k
  if ((file = fopen(filename, "rb")) == NULL)
324
0
#endif
325
106k
    THROW_UNIX("Cannot open input file");
326
327
106k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
328
0
    THROW_UNIX("Could not read input file")
329
106k
  else if (tempc == EOF)
330
106k
    THROW("Input file contains no data");
331
332
106k
  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
82.3k
  cinfo->data_precision = BITS_IN_JSAMPLE;
338
82.3k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
339
82.3k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
340
82.3k
  if (tempc == 'B') {
341
17.8k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
342
17.8k
      THROW("Could not initialize bitmap loader");
343
17.8k
    invert = !this->bottomUp;
344
87.5k
  } else if (tempc == 'P') {
345
87.5k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
346
87.5k
      THROW("Could not initialize PPM loader");
347
87.5k
    invert = this->bottomUp;
348
87.5k
  } else
349
18.4E
    THROW("Unsupported file type");
350
351
105k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
352
353
105k
  src->input_file = file;
354
  /* Refuse to load images larger than the specified size. */
355
105k
  src->max_pixels = this->maxPixels;
356
105k
  (*src->start_input) (cinfo, src);
357
105k
  if (tempc == 'B') {
358
5.72k
    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.72k
  }
364
105k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
365
366
105k
  *width = cinfo->image_width;  *height = cinfo->image_height;
367
105k
  *pixelFormat = cs2pf[cinfo->in_color_space];
368
369
105k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
370
105k
  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
105k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
376
105k
                                   sizeof(_JSAMPLE))) == NULL)
377
105k
    THROW("Memory allocation failure");
378
379
105k
  if (setjmp(this2->jerr.setjmp_buffer)) {
380
    /* If we get here, the JPEG code has signaled an error. */
381
31.1k
    retval = -1;  goto bailout;
382
31.1k
  }
383
384
190M
  while (cinfo->next_scanline < cinfo->image_height) {
385
190M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
386
387
380M
    for (i = 0; i < nlines; i++) {
388
190M
      _JSAMPLE *dstptr;
389
190M
      int row;
390
391
190M
      row = cinfo->next_scanline + i;
392
190M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
393
157M
      else dstptr = &dstBuf[row * pitch];
394
190M
      memcpy(dstptr, src->_buffer[i],
395
190M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
396
190M
    }
397
190M
    cinfo->next_scanline += nlines;
398
190M
  }
399
400
74.3k
  (*src->finish_input) (cinfo, src);
401
402
106k
bailout:
403
106k
  tj3Destroy(handle2);
404
106k
  if (file) fclose(file);
405
106k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
406
106k
  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
74.3k
}
tj3LoadImage8
Line
Count
Source
289
64.9k
{
290
64.9k
  static const char FUNCTION_NAME[] =
291
64.9k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
64.9k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
294
295
64.9k
  int retval = 0, tempc;
296
64.9k
  size_t pitch;
297
64.9k
  tjhandle handle2 = NULL;
298
64.9k
  tjinstance *this2;
299
64.9k
  j_compress_ptr cinfo = NULL;
300
64.9k
  cjpeg_source_ptr src;
301
64.9k
  _JSAMPLE *dstBuf = NULL;
302
64.9k
  FILE *file = NULL;
303
64.9k
  boolean invert;
304
305
64.9k
  GET_TJINSTANCE(handle, NULL)
306
307
64.9k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
308
64.9k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
309
64.9k
    THROW("Invalid argument");
310
64.9k
  if ((align & (align - 1)) != 0)
311
64.9k
    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.9k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
317
64.9k
  this2 = (tjinstance *)handle2;
318
64.9k
  cinfo = &this2->cinfo;
319
320
#ifdef _MSC_VER
321
  if (fopen_s(&file, filename, "rb") || file == NULL)
322
#else
323
64.9k
  if ((file = fopen(filename, "rb")) == NULL)
324
0
#endif
325
64.9k
    THROW_UNIX("Cannot open input file");
326
327
64.9k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
328
0
    THROW_UNIX("Could not read input file")
329
64.9k
  else if (tempc == EOF)
330
64.9k
    THROW("Input file contains no data");
331
332
64.9k
  if (setjmp(this2->jerr.setjmp_buffer)) {
333
    /* If we get here, the JPEG code has signaled an error. */
334
17.9k
    retval = -1;  goto bailout;
335
17.9k
  }
336
337
46.9k
  cinfo->data_precision = BITS_IN_JSAMPLE;
338
46.9k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
339
46.9k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
340
46.9k
  if (tempc == 'B') {
341
17.8k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
342
17.8k
      THROW("Could not initialize bitmap loader");
343
17.8k
    invert = !this->bottomUp;
344
46.6k
  } else if (tempc == 'P') {
345
46.6k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
346
46.6k
      THROW("Could not initialize PPM loader");
347
46.6k
    invert = this->bottomUp;
348
46.6k
  } else
349
18.4E
    THROW("Unsupported file type");
350
351
64.5k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
352
353
64.5k
  src->input_file = file;
354
  /* Refuse to load images larger than the specified size. */
355
64.5k
  src->max_pixels = this->maxPixels;
356
64.5k
  (*src->start_input) (cinfo, src);
357
64.5k
  if (tempc == 'B') {
358
5.72k
    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.72k
  }
364
64.5k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
365
366
64.5k
  *width = cinfo->image_width;  *height = cinfo->image_height;
367
64.5k
  *pixelFormat = cs2pf[cinfo->in_color_space];
368
369
64.5k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
370
64.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
64.5k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
376
64.5k
                                   sizeof(_JSAMPLE))) == NULL)
377
64.5k
    THROW("Memory allocation failure");
378
379
64.5k
  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
104M
  while (cinfo->next_scanline < cinfo->image_height) {
385
104M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
386
387
208M
    for (i = 0; i < nlines; i++) {
388
104M
      _JSAMPLE *dstptr;
389
104M
      int row;
390
391
104M
      row = cinfo->next_scanline + i;
392
104M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
393
83.8M
      else dstptr = &dstBuf[row * pitch];
394
104M
      memcpy(dstptr, src->_buffer[i],
395
104M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
396
104M
    }
397
104M
    cinfo->next_scanline += nlines;
398
104M
  }
399
400
46.6k
  (*src->finish_input) (cinfo, src);
401
402
64.9k
bailout:
403
64.9k
  tj3Destroy(handle2);
404
64.9k
  if (file) fclose(file);
405
64.9k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
406
64.9k
  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.6k
}
tj3LoadImage12
Line
Count
Source
289
30.4k
{
290
30.4k
  static const char FUNCTION_NAME[] =
291
30.4k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
30.4k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
294
295
30.4k
  int retval = 0, tempc;
296
30.4k
  size_t pitch;
297
30.4k
  tjhandle handle2 = NULL;
298
30.4k
  tjinstance *this2;
299
30.4k
  j_compress_ptr cinfo = NULL;
300
30.4k
  cjpeg_source_ptr src;
301
30.4k
  _JSAMPLE *dstBuf = NULL;
302
30.4k
  FILE *file = NULL;
303
30.4k
  boolean invert;
304
305
30.4k
  GET_TJINSTANCE(handle, NULL)
306
307
30.4k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
308
30.4k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
309
30.4k
    THROW("Invalid argument");
310
30.4k
  if ((align & (align - 1)) != 0)
311
30.4k
    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.4k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
317
30.4k
  this2 = (tjinstance *)handle2;
318
30.4k
  cinfo = &this2->cinfo;
319
320
#ifdef _MSC_VER
321
  if (fopen_s(&file, filename, "rb") || file == NULL)
322
#else
323
30.4k
  if ((file = fopen(filename, "rb")) == NULL)
324
0
#endif
325
30.4k
    THROW_UNIX("Cannot open input file");
326
327
30.4k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
328
0
    THROW_UNIX("Could not read input file")
329
30.4k
  else if (tempc == EOF)
330
30.4k
    THROW("Input file contains no data");
331
332
30.4k
  if (setjmp(this2->jerr.setjmp_buffer)) {
333
    /* If we get here, the JPEG code has signaled an error. */
334
4.08k
    retval = -1;  goto bailout;
335
4.08k
  }
336
337
26.4k
  cinfo->data_precision = BITS_IN_JSAMPLE;
338
26.4k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
339
26.4k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
340
26.4k
  if (tempc == 'B') {
341
14
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
342
14
      THROW("Could not initialize bitmap loader");
343
14
    invert = !this->bottomUp;
344
30.1k
  } else if (tempc == 'P') {
345
30.1k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
346
30.1k
      THROW("Could not initialize PPM loader");
347
30.1k
    invert = this->bottomUp;
348
30.1k
  } else
349
18.4E
    THROW("Unsupported file type");
350
351
30.1k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
352
353
30.1k
  src->input_file = file;
354
  /* Refuse to load images larger than the specified size. */
355
30.1k
  src->max_pixels = this->maxPixels;
356
30.1k
  (*src->start_input) (cinfo, src);
357
30.1k
  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
30.1k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
365
366
30.1k
  *width = cinfo->image_width;  *height = cinfo->image_height;
367
30.1k
  *pixelFormat = cs2pf[cinfo->in_color_space];
368
369
30.1k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
370
30.1k
  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
30.1k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
376
30.1k
                                   sizeof(_JSAMPLE))) == NULL)
377
30.1k
    THROW("Memory allocation failure");
378
379
30.1k
  if (setjmp(this2->jerr.setjmp_buffer)) {
380
    /* If we get here, the JPEG code has signaled an error. */
381
8.77k
    retval = -1;  goto bailout;
382
8.77k
  }
383
384
72.9M
  while (cinfo->next_scanline < cinfo->image_height) {
385
72.8M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
386
387
145M
    for (i = 0; i < nlines; i++) {
388
72.8M
      _JSAMPLE *dstptr;
389
72.8M
      int row;
390
391
72.8M
      row = cinfo->next_scanline + i;
392
72.8M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
393
62.4M
      else dstptr = &dstBuf[row * pitch];
394
72.8M
      memcpy(dstptr, src->_buffer[i],
395
72.8M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
396
72.8M
    }
397
72.8M
    cinfo->next_scanline += nlines;
398
72.8M
  }
399
400
21.4k
  (*src->finish_input) (cinfo, src);
401
402
30.4k
bailout:
403
30.4k
  tj3Destroy(handle2);
404
30.4k
  if (file) fclose(file);
405
30.4k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
406
30.4k
  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.4k
}
tj3LoadImage16
Line
Count
Source
289
10.8k
{
290
10.8k
  static const char FUNCTION_NAME[] =
291
10.8k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
10.8k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
294
295
10.8k
  int retval = 0, tempc;
296
10.8k
  size_t pitch;
297
10.8k
  tjhandle handle2 = NULL;
298
10.8k
  tjinstance *this2;
299
10.8k
  j_compress_ptr cinfo = NULL;
300
10.8k
  cjpeg_source_ptr src;
301
10.8k
  _JSAMPLE *dstBuf = NULL;
302
10.8k
  FILE *file = NULL;
303
10.8k
  boolean invert;
304
305
10.8k
  GET_TJINSTANCE(handle, NULL)
306
307
10.8k
  if (!filename || !width || align < 1 || !height || !pixelFormat ||
308
10.8k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
309
10.8k
    THROW("Invalid argument");
310
10.8k
  if ((align & (align - 1)) != 0)
311
10.8k
    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.8k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
317
10.8k
  this2 = (tjinstance *)handle2;
318
10.8k
  cinfo = &this2->cinfo;
319
320
#ifdef _MSC_VER
321
  if (fopen_s(&file, filename, "rb") || file == NULL)
322
#else
323
10.8k
  if ((file = fopen(filename, "rb")) == NULL)
324
0
#endif
325
10.8k
    THROW_UNIX("Cannot open input file");
326
327
10.8k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
328
0
    THROW_UNIX("Could not read input file")
329
10.8k
  else if (tempc == EOF)
330
10.8k
    THROW("Input file contains no data");
331
332
10.8k
  if (setjmp(this2->jerr.setjmp_buffer)) {
333
    /* If we get here, the JPEG code has signaled an error. */
334
1.92k
    retval = -1;  goto bailout;
335
1.92k
  }
336
337
8.92k
  cinfo->data_precision = BITS_IN_JSAMPLE;
338
8.92k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
339
8.92k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
340
8.92k
  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.7k
  } else if (tempc == 'P') {
345
10.7k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
346
10.7k
      THROW("Could not initialize PPM loader");
347
10.7k
    invert = this->bottomUp;
348
10.7k
  } else
349
18.4E
    THROW("Unsupported file type");
350
351
10.7k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
352
353
10.7k
  src->input_file = file;
354
  /* Refuse to load images larger than the specified size. */
355
10.7k
  src->max_pixels = this->maxPixels;
356
10.7k
  (*src->start_input) (cinfo, src);
357
10.7k
  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.7k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
365
366
10.7k
  *width = cinfo->image_width;  *height = cinfo->image_height;
367
10.7k
  *pixelFormat = cs2pf[cinfo->in_color_space];
368
369
10.7k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
370
10.7k
  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.7k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
376
10.7k
                                   sizeof(_JSAMPLE))) == NULL)
377
10.7k
    THROW("Memory allocation failure");
378
379
10.7k
  if (setjmp(this2->jerr.setjmp_buffer)) {
380
    /* If we get here, the JPEG code has signaled an error. */
381
4.40k
    retval = -1;  goto bailout;
382
4.40k
  }
383
384
13.3M
  while (cinfo->next_scanline < cinfo->image_height) {
385
13.3M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
386
387
26.6M
    for (i = 0; i < nlines; i++) {
388
13.3M
      _JSAMPLE *dstptr;
389
13.3M
      int row;
390
391
13.3M
      row = cinfo->next_scanline + i;
392
13.3M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
393
11.4M
      else dstptr = &dstBuf[row * pitch];
394
13.3M
      memcpy(dstptr, src->_buffer[i],
395
13.3M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
396
13.3M
    }
397
13.3M
    cinfo->next_scanline += nlines;
398
13.3M
  }
399
400
6.31k
  (*src->finish_input) (cinfo, src);
401
402
10.8k
bailout:
403
10.8k
  tj3Destroy(handle2);
404
10.8k
  if (file) fclose(file);
405
10.8k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
406
10.8k
  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.31k
}
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