Coverage Report

Created: 2025-08-09 06:49

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