Coverage Report

Created: 2026-10-17 07:27

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-2026 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
95.8M
#define _JSAMPLE  JSAMPLE
34
58.6k
#define _JSAMPROW  JSAMPROW
35
95.8M
#define _buffer  buffer
36
48.7k
#define _jinit_read_ppm  jinit_read_ppm
37
0
#define _jinit_write_ppm  jinit_write_ppm
38
2.14k
#define _jpeg_crop_scanline  jpeg_crop_scanline
39
32.2M
#define _jpeg_read_scanlines  jpeg_read_scanlines
40
4.25k
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
41
18.9k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
66.3M
#define _JSAMPLE  J12SAMPLE
44
42.1k
#define _JSAMPROW  J12SAMPROW
45
66.3M
#define _buffer  buffer12
46
31.6k
#define _jinit_read_ppm  j12init_read_ppm
47
0
#define _jinit_write_ppm  j12init_write_ppm
48
1.16k
#define _jpeg_crop_scanline  jpeg12_crop_scanline
49
17.4M
#define _jpeg_read_scanlines  jpeg12_read_scanlines
50
2.30k
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
51
18.6k
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
14.4M
#define _JSAMPLE  J16SAMPLE
54
15.4k
#define _JSAMPROW  J16SAMPROW
55
14.4M
#define _buffer  buffer16
56
11.1k
#define _jinit_read_ppm  j16init_read_ppm
57
0
#define _jinit_write_ppm  j16init_write_ppm
58
6.18M
#define _jpeg_read_scanlines  jpeg16_read_scanlines
59
4.77k
#define _jpeg_write_scanlines  jpeg16_write_scanlines
60
#endif
61
62
0
#define _GET_NAME(name, suffix)  name##suffix
63
0
#define GET_NAME(name, suffix)  _GET_NAME(name, suffix)
64
224k
#define _GET_STRING(name, suffix)  #name #suffix
65
224k
#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
42.3k
{
75
42.3k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
42.3k
  int i, retval = 0;
77
42.3k
  boolean alloc = TRUE;
78
42.3k
  _JSAMPROW *row_pointer = NULL;
79
80
42.3k
  GET_CINSTANCE(handle)
81
42.3k
  if ((this->init & COMPRESS) == 0)
82
42.3k
    THROW("Instance has not been initialized for compression");
83
84
42.3k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
42.3k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
42.3k
      jpegSize == NULL)
87
42.3k
    THROW("Invalid argument");
88
89
42.3k
  if (!this->lossless && this->quality == -1)
90
42.3k
    THROW("TJPARAM_QUALITY must be specified");
91
42.3k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
42.3k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
42.3k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
42.3k
    THROW("Invalid argument");
97
98
42.3k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
42.3k
    THROW("Memory allocation failure");
100
101
42.3k
  CATCH_LIBJPEG(this);
102
103
42.3k
  cinfo->image_width = width;
104
42.3k
  cinfo->image_height = height;
105
42.3k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
42.3k
  setCompDefaults(this, pixelFormat, FALSE);
108
42.3k
  if (this->noRealloc) {
109
36.1k
    alloc = FALSE;
110
36.1k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
36.1k
  }
112
42.3k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
42.3k
  jpeg_start_compress(cinfo, TRUE);
115
141M
  for (i = 0; i < height; i++) {
116
141M
    if (this->bottomUp)
117
20.4M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
121M
    else
119
121M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
141M
  }
121
84.6k
  while (cinfo->next_scanline < cinfo->image_height)
122
42.3k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
42.3k
                          cinfo->image_height - cinfo->next_scanline);
124
42.3k
  jpeg_finish_compress(cinfo);
125
126
42.3k
bailout:
127
42.3k
  if (cinfo->global_state > CSTATE_START && alloc)
128
31
    (*cinfo->dest->term_destination) (cinfo);
129
42.3k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
31
    jpeg_abort_compress(cinfo);
131
42.3k
  free(row_pointer);
132
42.3k
  if (this->jerr.warning) retval = -1;
133
42.3k
  return retval;
134
42.3k
}
tj3Compress8
Line
Count
Source
74
18.9k
{
75
18.9k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.9k
  int i, retval = 0;
77
18.9k
  boolean alloc = TRUE;
78
18.9k
  _JSAMPROW *row_pointer = NULL;
79
80
18.9k
  GET_CINSTANCE(handle)
81
18.9k
  if ((this->init & COMPRESS) == 0)
82
18.9k
    THROW("Instance has not been initialized for compression");
83
84
18.9k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.9k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.9k
      jpegSize == NULL)
87
18.9k
    THROW("Invalid argument");
88
89
18.9k
  if (!this->lossless && this->quality == -1)
90
18.9k
    THROW("TJPARAM_QUALITY must be specified");
91
18.9k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.9k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.9k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
18.9k
    THROW("Invalid argument");
97
98
18.9k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
18.9k
    THROW("Memory allocation failure");
100
101
18.9k
  CATCH_LIBJPEG(this);
102
103
18.9k
  cinfo->image_width = width;
104
18.9k
  cinfo->image_height = height;
105
18.9k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
18.9k
  setCompDefaults(this, pixelFormat, FALSE);
108
18.9k
  if (this->noRealloc) {
109
16.1k
    alloc = FALSE;
110
16.1k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
16.1k
  }
112
18.9k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
18.9k
  jpeg_start_compress(cinfo, TRUE);
115
61.3M
  for (i = 0; i < height; i++) {
116
61.2M
    if (this->bottomUp)
117
8.90M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
52.3M
    else
119
52.3M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
61.2M
  }
121
37.8k
  while (cinfo->next_scanline < cinfo->image_height)
122
18.9k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
18.9k
                          cinfo->image_height - cinfo->next_scanline);
124
18.9k
  jpeg_finish_compress(cinfo);
125
126
18.9k
bailout:
127
18.9k
  if (cinfo->global_state > CSTATE_START && alloc)
128
0
    (*cinfo->dest->term_destination) (cinfo);
129
18.9k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
0
    jpeg_abort_compress(cinfo);
131
18.9k
  free(row_pointer);
132
18.9k
  if (this->jerr.warning) retval = -1;
133
18.9k
  return retval;
134
18.9k
}
tj3Compress12
Line
Count
Source
74
18.6k
{
75
18.6k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
18.6k
  int i, retval = 0;
77
18.6k
  boolean alloc = TRUE;
78
18.6k
  _JSAMPROW *row_pointer = NULL;
79
80
18.6k
  GET_CINSTANCE(handle)
81
18.6k
  if ((this->init & COMPRESS) == 0)
82
18.6k
    THROW("Instance has not been initialized for compression");
83
84
18.6k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
18.6k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
18.6k
      jpegSize == NULL)
87
18.6k
    THROW("Invalid argument");
88
89
18.6k
  if (!this->lossless && this->quality == -1)
90
18.6k
    THROW("TJPARAM_QUALITY must be specified");
91
18.6k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
18.6k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
18.6k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
18.6k
    THROW("Invalid argument");
97
98
18.6k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
18.6k
    THROW("Memory allocation failure");
100
101
18.6k
  CATCH_LIBJPEG(this);
102
103
18.6k
  cinfo->image_width = width;
104
18.6k
  cinfo->image_height = height;
105
18.6k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
18.6k
  setCompDefaults(this, pixelFormat, FALSE);
108
18.6k
  if (this->noRealloc) {
109
15.9k
    alloc = FALSE;
110
15.9k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
15.9k
  }
112
18.6k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
18.6k
  jpeg_start_compress(cinfo, TRUE);
115
66.2M
  for (i = 0; i < height; i++) {
116
66.1M
    if (this->bottomUp)
117
9.46M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
56.7M
    else
119
56.7M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
66.1M
  }
121
37.3k
  while (cinfo->next_scanline < cinfo->image_height)
122
18.6k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
18.6k
                          cinfo->image_height - cinfo->next_scanline);
124
18.6k
  jpeg_finish_compress(cinfo);
125
126
18.6k
bailout:
127
18.6k
  if (cinfo->global_state > CSTATE_START && alloc)
128
0
    (*cinfo->dest->term_destination) (cinfo);
129
18.6k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
0
    jpeg_abort_compress(cinfo);
131
18.6k
  free(row_pointer);
132
18.6k
  if (this->jerr.warning) retval = -1;
133
18.6k
  return retval;
134
18.6k
}
tj3Compress16
Line
Count
Source
74
4.77k
{
75
4.77k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
4.77k
  int i, retval = 0;
77
4.77k
  boolean alloc = TRUE;
78
4.77k
  _JSAMPROW *row_pointer = NULL;
79
80
4.77k
  GET_CINSTANCE(handle)
81
4.77k
  if ((this->init & COMPRESS) == 0)
82
4.77k
    THROW("Instance has not been initialized for compression");
83
84
4.77k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
4.77k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
4.77k
      jpegSize == NULL)
87
4.77k
    THROW("Invalid argument");
88
89
4.77k
  if (!this->lossless && this->quality == -1)
90
4.77k
    THROW("TJPARAM_QUALITY must be specified");
91
4.77k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
4.77k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
4.77k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
0
  else if (pitch < width * tjPixelSize[pixelFormat])
96
4.77k
    THROW("Invalid argument");
97
98
4.77k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
99
4.77k
    THROW("Memory allocation failure");
100
101
4.77k
  CATCH_LIBJPEG(this);
102
103
4.74k
  cinfo->image_width = width;
104
4.74k
  cinfo->image_height = height;
105
4.74k
  cinfo->data_precision = BITS_IN_JSAMPLE;
106
107
4.74k
  setCompDefaults(this, pixelFormat, FALSE);
108
4.74k
  if (this->noRealloc) {
109
4.07k
    alloc = FALSE;
110
4.07k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
111
4.07k
  }
112
4.74k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
113
114
4.74k
  jpeg_start_compress(cinfo, TRUE);
115
14.4M
  for (i = 0; i < height; i++) {
116
14.4M
    if (this->bottomUp)
117
2.06M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
118
12.3M
    else
119
12.3M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
120
14.4M
  }
121
9.51k
  while (cinfo->next_scanline < cinfo->image_height)
122
4.77k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
123
4.77k
                          cinfo->image_height - cinfo->next_scanline);
124
4.74k
  jpeg_finish_compress(cinfo);
125
126
4.77k
bailout:
127
4.77k
  if (cinfo->global_state > CSTATE_START && alloc)
128
31
    (*cinfo->dest->term_destination) (cinfo);
129
4.77k
  if (cinfo->global_state > CSTATE_START || retval == -1)
130
31
    jpeg_abort_compress(cinfo);
131
4.77k
  free(row_pointer);
132
4.77k
  if (this->jerr.warning) retval = -1;
133
4.77k
  return retval;
134
4.74k
}
135
136
137
/******************************* Decompressor ********************************/
138
139
/* TurboJPEG 3+ */
140
DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE)
141
  (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize,
142
   _JSAMPLE *dstBuf, int pitch, int pixelFormat)
143
73.8k
{
144
73.8k
  static const char FUNCTION_NAME[] =
145
73.8k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
73.8k
  _JSAMPROW *row_pointer = NULL;
147
73.8k
  int croppedHeight, i, retval = 0;
148
#if BITS_IN_JSAMPLE != 16
149
  int scaledWidth;
150
#endif
151
73.8k
  struct my_progress_mgr progress;
152
153
73.8k
  GET_DINSTANCE(handle);
154
73.8k
  if ((this->init & DECOMPRESS) == 0)
155
73.8k
    THROW("Instance has not been initialized for decompression");
156
157
73.8k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
73.8k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
73.8k
    THROW("Invalid argument");
160
161
73.8k
  if (this->scanLimit) {
162
73.8k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
73.8k
    progress.pub.progress_monitor = my_progress_monitor;
164
73.8k
    progress.this = this;
165
73.8k
    dinfo->progress = &progress.pub;
166
73.8k
  } else
167
0
    dinfo->progress = NULL;
168
169
73.8k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
73.8k
  CATCH_LIBJPEG(this);
172
173
73.8k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
73.8k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
73.8k
    jpeg_read_header(dinfo, TRUE);
176
73.8k
  }
177
31.0k
  setDecompParameters(this);
178
31.0k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
31.0k
    THROW("Image is too large");
182
31.0k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
#if BITS_IN_JSAMPLE != 16
184
28.7k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
#endif
186
31.0k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
31.0k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
31.0k
  dinfo->scale_num = this->scalingFactor.num;
190
31.0k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
31.0k
  jpeg_start_decompress(dinfo);
193
194
#if BITS_IN_JSAMPLE != 16
195
28.7k
  if (this->croppingRegion.x != 0 ||
196
25.4k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
3.30k
    JDIMENSION crop_x = this->croppingRegion.x;
198
3.30k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
3.30k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
3.30k
    if ((int)crop_x != this->croppingRegion.x)
202
9
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
3.30k
             "actual (%d) cropping region left boundary",
204
3.30k
             this->croppingRegion.x, (int)crop_x);
205
3.29k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
3.29k
             "actual (%d) cropping region width",
208
3.29k
             this->croppingRegion.w, (int)crop_w);
209
3.29k
  }
210
28.7k
#endif
211
212
31.0k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
31.0k
    THROW("Invalid argument");
215
216
31.0k
  croppedHeight = dinfo->output_height;
217
#if BITS_IN_JSAMPLE != 16
218
28.7k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
3.29k
    croppedHeight = this->croppingRegion.h;
220
#endif
221
31.0k
  if ((row_pointer =
222
31.0k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
31.0k
    THROW("Memory allocation failure");
224
31.0k
  CATCH_LIBJPEG(this);
225
126M
  for (i = 0; i < (int)croppedHeight; i++) {
226
126M
    if (this->bottomUp)
227
40.8M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
86.1M
    else
229
86.1M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
126M
  }
231
232
#if BITS_IN_JSAMPLE != 16
233
25.4k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
3.29k
    if (this->croppingRegion.y != 0) {
235
3.29k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
3.29k
      if ((int)lines != this->croppingRegion.y)
238
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
               "actual (%d) cropping region upper boundary",
240
3.29k
               this->croppingRegion.y, (int)lines);
241
3.29k
    }
242
126k
    while ((int)dinfo->output_scanline <
243
126k
           this->croppingRegion.y + this->croppingRegion.h)
244
123k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
123k
                                               this->croppingRegion.y],
246
123k
                           this->croppingRegion.y + this->croppingRegion.h -
247
123k
                           dinfo->output_scanline);
248
3.29k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
3.29k
        (int)dinfo->output_height) {
250
3.26k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
                                                     this->croppingRegion.y -
252
                                                     this->croppingRegion.h);
253
254
3.26k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
3.26k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
3.26k
               "actual (%d) cropping region lower boundary",
258
3.26k
               this->croppingRegion.y + this->croppingRegion.h,
259
3.26k
               (int)(dinfo->output_height - lines));
260
3.26k
    }
261
3.29k
  } else
262
21.4k
#endif
263
21.4k
  {
264
55.8M
    while (dinfo->output_scanline < dinfo->output_height)
265
55.7M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
55.7M
                           dinfo->output_height - dinfo->output_scanline);
267
21.4k
  }
268
24.7k
  jpeg_finish_decompress(dinfo);
269
270
73.8k
bailout:
271
73.8k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
73.8k
  free(row_pointer);
273
73.8k
  if (this->jerr.warning) retval = -1;
274
73.8k
  return retval;
275
24.7k
}
tj3Decompress8
Line
Count
Source
143
39.7k
{
144
39.7k
  static const char FUNCTION_NAME[] =
145
39.7k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
39.7k
  _JSAMPROW *row_pointer = NULL;
147
39.7k
  int croppedHeight, i, retval = 0;
148
39.7k
#if BITS_IN_JSAMPLE != 16
149
39.7k
  int scaledWidth;
150
39.7k
#endif
151
39.7k
  struct my_progress_mgr progress;
152
153
39.7k
  GET_DINSTANCE(handle);
154
39.7k
  if ((this->init & DECOMPRESS) == 0)
155
39.7k
    THROW("Instance has not been initialized for decompression");
156
157
39.7k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
39.7k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
39.7k
    THROW("Invalid argument");
160
161
39.7k
  if (this->scanLimit) {
162
39.7k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
39.7k
    progress.pub.progress_monitor = my_progress_monitor;
164
39.7k
    progress.this = this;
165
39.7k
    dinfo->progress = &progress.pub;
166
39.7k
  } else
167
0
    dinfo->progress = NULL;
168
169
39.7k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
39.7k
  CATCH_LIBJPEG(this);
172
173
39.7k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
39.7k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
39.7k
    jpeg_read_header(dinfo, TRUE);
176
39.7k
  }
177
17.6k
  setDecompParameters(this);
178
17.6k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
17.6k
    THROW("Image is too large");
182
17.6k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
17.6k
#if BITS_IN_JSAMPLE != 16
184
17.6k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
17.6k
#endif
186
17.6k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
17.6k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
17.6k
  dinfo->scale_num = this->scalingFactor.num;
190
17.6k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
17.6k
  jpeg_start_decompress(dinfo);
193
194
17.6k
#if BITS_IN_JSAMPLE != 16
195
17.6k
  if (this->croppingRegion.x != 0 ||
196
15.5k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
2.14k
    JDIMENSION crop_x = this->croppingRegion.x;
198
2.14k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
2.14k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
2.14k
    if ((int)crop_x != this->croppingRegion.x)
202
6
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
2.14k
             "actual (%d) cropping region left boundary",
204
2.14k
             this->croppingRegion.x, (int)crop_x);
205
2.13k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
2.13k
             "actual (%d) cropping region width",
208
2.13k
             this->croppingRegion.w, (int)crop_w);
209
2.13k
  }
210
17.6k
#endif
211
212
17.6k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
17.6k
    THROW("Invalid argument");
215
216
17.6k
  croppedHeight = dinfo->output_height;
217
17.6k
#if BITS_IN_JSAMPLE != 16
218
17.6k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
2.13k
    croppedHeight = this->croppingRegion.h;
220
17.6k
#endif
221
17.6k
  if ((row_pointer =
222
17.6k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
17.6k
    THROW("Memory allocation failure");
224
17.6k
  CATCH_LIBJPEG(this);
225
67.6M
  for (i = 0; i < (int)croppedHeight; i++) {
226
67.5M
    if (this->bottomUp)
227
22.9M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
44.6M
    else
229
44.6M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
67.5M
  }
231
232
15.2k
#if BITS_IN_JSAMPLE != 16
233
15.5k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
2.13k
    if (this->croppingRegion.y != 0) {
235
2.13k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
2.13k
      if ((int)lines != this->croppingRegion.y)
238
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
2.13k
               "actual (%d) cropping region upper boundary",
240
2.13k
               this->croppingRegion.y, (int)lines);
241
2.13k
    }
242
84.6k
    while ((int)dinfo->output_scanline <
243
84.6k
           this->croppingRegion.y + this->croppingRegion.h)
244
82.5k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
82.5k
                                               this->croppingRegion.y],
246
82.5k
                           this->croppingRegion.y + this->croppingRegion.h -
247
82.5k
                           dinfo->output_scanline);
248
2.13k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
2.13k
        (int)dinfo->output_height) {
250
2.12k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
2.12k
                                                     this->croppingRegion.y -
252
2.12k
                                                     this->croppingRegion.h);
253
254
2.12k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
2.12k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
2.12k
               "actual (%d) cropping region lower boundary",
258
2.12k
               this->croppingRegion.y + this->croppingRegion.h,
259
2.12k
               (int)(dinfo->output_height - lines));
260
2.12k
    }
261
2.13k
  } else
262
13.1k
#endif
263
13.1k
  {
264
32.2M
    while (dinfo->output_scanline < dinfo->output_height)
265
32.1M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
32.1M
                           dinfo->output_height - dinfo->output_scanline);
267
13.1k
  }
268
15.2k
  jpeg_finish_decompress(dinfo);
269
270
39.7k
bailout:
271
39.7k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
39.7k
  free(row_pointer);
273
39.7k
  if (this->jerr.warning) retval = -1;
274
39.7k
  return retval;
275
15.2k
}
tj3Decompress12
Line
Count
Source
143
23.4k
{
144
23.4k
  static const char FUNCTION_NAME[] =
145
23.4k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
23.4k
  _JSAMPROW *row_pointer = NULL;
147
23.4k
  int croppedHeight, i, retval = 0;
148
23.4k
#if BITS_IN_JSAMPLE != 16
149
23.4k
  int scaledWidth;
150
23.4k
#endif
151
23.4k
  struct my_progress_mgr progress;
152
153
23.4k
  GET_DINSTANCE(handle);
154
23.4k
  if ((this->init & DECOMPRESS) == 0)
155
23.4k
    THROW("Instance has not been initialized for decompression");
156
157
23.4k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
23.4k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
23.4k
    THROW("Invalid argument");
160
161
23.4k
  if (this->scanLimit) {
162
23.4k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
23.4k
    progress.pub.progress_monitor = my_progress_monitor;
164
23.4k
    progress.this = this;
165
23.4k
    dinfo->progress = &progress.pub;
166
23.4k
  } else
167
0
    dinfo->progress = NULL;
168
169
23.4k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
23.4k
  CATCH_LIBJPEG(this);
172
173
23.4k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
23.4k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
23.4k
    jpeg_read_header(dinfo, TRUE);
176
23.4k
  }
177
11.0k
  setDecompParameters(this);
178
11.0k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
11.0k
    THROW("Image is too large");
182
11.0k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
11.0k
#if BITS_IN_JSAMPLE != 16
184
11.0k
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
11.0k
#endif
186
11.0k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
11.0k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
11.0k
  dinfo->scale_num = this->scalingFactor.num;
190
11.0k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
11.0k
  jpeg_start_decompress(dinfo);
193
194
11.0k
#if BITS_IN_JSAMPLE != 16
195
11.0k
  if (this->croppingRegion.x != 0 ||
196
9.91k
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
1.16k
    JDIMENSION crop_x = this->croppingRegion.x;
198
1.16k
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
1.16k
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
1.16k
    if ((int)crop_x != this->croppingRegion.x)
202
3
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
1.16k
             "actual (%d) cropping region left boundary",
204
1.16k
             this->croppingRegion.x, (int)crop_x);
205
1.15k
    if ((int)crop_w != this->croppingRegion.w)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
1.15k
             "actual (%d) cropping region width",
208
1.15k
             this->croppingRegion.w, (int)crop_w);
209
1.15k
  }
210
11.0k
#endif
211
212
11.0k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
11.0k
    THROW("Invalid argument");
215
216
11.0k
  croppedHeight = dinfo->output_height;
217
11.0k
#if BITS_IN_JSAMPLE != 16
218
11.0k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
1.15k
    croppedHeight = this->croppingRegion.h;
220
11.0k
#endif
221
11.0k
  if ((row_pointer =
222
11.0k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
11.0k
    THROW("Memory allocation failure");
224
11.0k
  CATCH_LIBJPEG(this);
225
36.7M
  for (i = 0; i < (int)croppedHeight; i++) {
226
36.7M
    if (this->bottomUp)
227
12.7M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
24.0M
    else
229
24.0M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
36.7M
  }
231
232
9.50k
#if BITS_IN_JSAMPLE != 16
233
9.91k
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
1.15k
    if (this->croppingRegion.y != 0) {
235
1.15k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
1.15k
      if ((int)lines != this->croppingRegion.y)
238
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
1.15k
               "actual (%d) cropping region upper boundary",
240
1.15k
               this->croppingRegion.y, (int)lines);
241
1.15k
    }
242
41.7k
    while ((int)dinfo->output_scanline <
243
41.7k
           this->croppingRegion.y + this->croppingRegion.h)
244
40.5k
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
40.5k
                                               this->croppingRegion.y],
246
40.5k
                           this->croppingRegion.y + this->croppingRegion.h -
247
40.5k
                           dinfo->output_scanline);
248
1.15k
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
1.15k
        (int)dinfo->output_height) {
250
1.14k
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
1.14k
                                                     this->croppingRegion.y -
252
1.14k
                                                     this->croppingRegion.h);
253
254
1.14k
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
1.14k
                   this->croppingRegion.h)
256
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
1.14k
               "actual (%d) cropping region lower boundary",
258
1.14k
               this->croppingRegion.y + this->croppingRegion.h,
259
1.14k
               (int)(dinfo->output_height - lines));
260
1.14k
    }
261
1.15k
  } else
262
8.35k
#endif
263
8.35k
  {
264
17.4M
    while (dinfo->output_scanline < dinfo->output_height)
265
17.4M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
17.4M
                           dinfo->output_height - dinfo->output_scanline);
267
8.35k
  }
268
9.50k
  jpeg_finish_decompress(dinfo);
269
270
23.4k
bailout:
271
23.4k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
23.4k
  free(row_pointer);
273
23.4k
  if (this->jerr.warning) retval = -1;
274
23.4k
  return retval;
275
9.50k
}
tj3Decompress16
Line
Count
Source
143
10.6k
{
144
10.6k
  static const char FUNCTION_NAME[] =
145
10.6k
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
146
10.6k
  _JSAMPROW *row_pointer = NULL;
147
10.6k
  int croppedHeight, i, retval = 0;
148
#if BITS_IN_JSAMPLE != 16
149
  int scaledWidth;
150
#endif
151
10.6k
  struct my_progress_mgr progress;
152
153
10.6k
  GET_DINSTANCE(handle);
154
10.6k
  if ((this->init & DECOMPRESS) == 0)
155
10.6k
    THROW("Instance has not been initialized for decompression");
156
157
10.6k
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
158
10.6k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
159
10.6k
    THROW("Invalid argument");
160
161
10.6k
  if (this->scanLimit) {
162
10.6k
    memset(&progress, 0, sizeof(struct my_progress_mgr));
163
10.6k
    progress.pub.progress_monitor = my_progress_monitor;
164
10.6k
    progress.this = this;
165
10.6k
    dinfo->progress = &progress.pub;
166
10.6k
  } else
167
0
    dinfo->progress = NULL;
168
169
10.6k
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
170
171
10.6k
  CATCH_LIBJPEG(this);
172
173
10.6k
  if (dinfo->global_state <= DSTATE_INHEADER) {
174
10.6k
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
175
10.6k
    jpeg_read_header(dinfo, TRUE);
176
10.6k
  }
177
2.32k
  setDecompParameters(this);
178
2.32k
  if (this->maxPixels &&
179
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
180
0
      (unsigned long long)this->maxPixels)
181
2.32k
    THROW("Image is too large");
182
2.32k
  this->dinfo.out_color_space = pf2cs[pixelFormat];
183
#if BITS_IN_JSAMPLE != 16
184
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
185
#endif
186
2.32k
  dinfo->do_fancy_upsampling = !this->fastUpsample;
187
2.32k
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
188
189
2.32k
  dinfo->scale_num = this->scalingFactor.num;
190
2.32k
  dinfo->scale_denom = this->scalingFactor.denom;
191
192
2.32k
  jpeg_start_decompress(dinfo);
193
194
#if BITS_IN_JSAMPLE != 16
195
  if (this->croppingRegion.x != 0 ||
196
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
197
    JDIMENSION crop_x = this->croppingRegion.x;
198
    JDIMENSION crop_w = this->croppingRegion.w;
199
200
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
201
    if ((int)crop_x != this->croppingRegion.x)
202
      THROWI("Unexplained mismatch between specified (%d) and\n"
203
             "actual (%d) cropping region left boundary",
204
             this->croppingRegion.x, (int)crop_x);
205
    if ((int)crop_w != this->croppingRegion.w)
206
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
             "actual (%d) cropping region width",
208
             this->croppingRegion.w, (int)crop_w);
209
  }
210
#endif
211
212
2.32k
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
213
0
  else if ((JDIMENSION)pitch < dinfo->output_width * tjPixelSize[pixelFormat])
214
2.32k
    THROW("Invalid argument");
215
216
2.32k
  croppedHeight = dinfo->output_height;
217
#if BITS_IN_JSAMPLE != 16
218
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
219
    croppedHeight = this->croppingRegion.h;
220
#endif
221
2.32k
  if ((row_pointer =
222
2.32k
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
223
2.32k
    THROW("Memory allocation failure");
224
2.32k
  CATCH_LIBJPEG(this);
225
22.6M
  for (i = 0; i < (int)croppedHeight; i++) {
226
22.6M
    if (this->bottomUp)
227
5.20M
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228
17.4M
    else
229
17.4M
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
230
22.6M
  }
231
232
#if BITS_IN_JSAMPLE != 16
233
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
234
    if (this->croppingRegion.y != 0) {
235
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236
237
      if ((int)lines != this->croppingRegion.y)
238
        THROWI("Unexplained mismatch between specified (%d) and\n"
239
               "actual (%d) cropping region upper boundary",
240
               this->croppingRegion.y, (int)lines);
241
    }
242
    while ((int)dinfo->output_scanline <
243
           this->croppingRegion.y + this->croppingRegion.h)
244
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
245
                                               this->croppingRegion.y],
246
                           this->croppingRegion.y + this->croppingRegion.h -
247
                           dinfo->output_scanline);
248
    if (this->croppingRegion.y + this->croppingRegion.h !=
249
        (int)dinfo->output_height) {
250
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
251
                                                     this->croppingRegion.y -
252
                                                     this->croppingRegion.h);
253
254
      if (lines != dinfo->output_height - this->croppingRegion.y -
255
                   this->croppingRegion.h)
256
        THROWI("Unexplained mismatch between specified (%d) and\n"
257
               "actual (%d) cropping region lower boundary",
258
               this->croppingRegion.y + this->croppingRegion.h,
259
               (int)(dinfo->output_height - lines));
260
    }
261
  } else
262
#endif
263
1.32k
  {
264
6.18M
    while (dinfo->output_scanline < dinfo->output_height)
265
6.18M
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
266
6.18M
                           dinfo->output_height - dinfo->output_scanline);
267
1.32k
  }
268
1.32k
  jpeg_finish_decompress(dinfo);
269
270
10.6k
bailout:
271
10.6k
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272
10.6k
  free(row_pointer);
273
10.6k
  if (this->jerr.warning) retval = -1;
274
10.6k
  return retval;
275
1.32k
}
276
277
278
/*************************** Packed-Pixel Image I/O **************************/
279
280
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
281
282
/* TurboJPEG 3+ */
283
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
284
static
285
#endif
286
_JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
287
  (tjhandle handle, FILE *file, int *width, int align, int *height,
288
   int *pixelFormat)
289
108k
{
290
108k
  static const char FUNCTION_NAME[] =
291
108k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
108k
  int retval = 0, tempc;
294
108k
  size_t pitch;
295
108k
  tjhandle handle2 = NULL;
296
108k
  tjinstance *this2;
297
108k
  j_compress_ptr cinfo = NULL;
298
108k
  cjpeg_source_ptr src;
299
108k
  _JSAMPLE *dstBuf = NULL;
300
108k
  boolean invert;
301
302
108k
  GET_TJINSTANCE(handle, NULL)
303
304
108k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
108k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
108k
    THROW("Invalid argument");
307
108k
  if ((align & (align - 1)) != 0)
308
108k
    THROW("Alignment must be a power of 2");
309
310
  /* The instance handle passed to this function is used only for parameter
311
     retrieval.  Create a new temporary instance to avoid interfering with the
312
     libjpeg state of the primary instance. */
313
108k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
108k
  this2 = (tjinstance *)handle2;
315
108k
  cinfo = &this2->cinfo;
316
317
108k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
108k
  else if (tempc == EOF)
320
108k
    THROW("Input file contains no data");
321
322
108k
  CATCH_LIBJPEG2();
323
324
85.2k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
85.2k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
85.2k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
85.2k
  if (tempc == 'B') {
328
16.5k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
16.5k
      THROW("Could not initialize bitmap loader");
330
16.5k
    invert = !this->bottomUp;
331
91.6k
  } else if (tempc == 'P') {
332
91.6k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
91.6k
      THROW("Could not initialize PPM loader");
334
91.6k
    invert = this->bottomUp;
335
91.6k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
108k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
108k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
108k
  src->max_pixels = this->maxPixels;
343
108k
  (*src->start_input) (cinfo, src);
344
108k
  if (tempc == 'B') {
345
4.92k
    if (cinfo->X_density && cinfo->Y_density) {
346
1.07k
      this->xDensity = cinfo->X_density;
347
1.07k
      this->yDensity = cinfo->Y_density;
348
1.07k
      this->densityUnits = cinfo->density_unit;
349
1.07k
    }
350
4.92k
  }
351
108k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
108k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
108k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
108k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
108k
  if (
359
#if ULLONG_MAX > SIZE_MAX
360
      (unsigned long long)pitch * (unsigned long long)(*height) *
361
      (unsigned long long)sizeof(_JSAMPLE) >
362
      (unsigned long long)((size_t)-1) ||
363
#endif
364
108k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
108k
                                   sizeof(_JSAMPLE))) == NULL)
366
108k
    THROW("Memory allocation failure");
367
368
108k
  CATCH_LIBJPEG2();
369
370
176M
  while (cinfo->next_scanline < cinfo->image_height) {
371
176M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
353M
    for (i = 0; i < nlines; i++) {
374
176M
      _JSAMPLE *dstptr;
375
176M
      int row;
376
377
176M
      row = cinfo->next_scanline + i;
378
176M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
146M
      else dstptr = &dstBuf[row * pitch];
380
176M
      memcpy(dstptr, src->_buffer[i],
381
176M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
176M
    }
383
176M
    cinfo->next_scanline += nlines;
384
176M
  }
385
386
77.4k
  (*src->finish_input) (cinfo, src);
387
388
108k
bailout:
389
108k
  tj3Destroy(handle2);
390
108k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
108k
  return dstBuf;
392
77.4k
}
_tj3LoadImageFromFileHandle8
Line
Count
Source
289
65.6k
{
290
65.6k
  static const char FUNCTION_NAME[] =
291
65.6k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
65.6k
  int retval = 0, tempc;
294
65.6k
  size_t pitch;
295
65.6k
  tjhandle handle2 = NULL;
296
65.6k
  tjinstance *this2;
297
65.6k
  j_compress_ptr cinfo = NULL;
298
65.6k
  cjpeg_source_ptr src;
299
65.6k
  _JSAMPLE *dstBuf = NULL;
300
65.6k
  boolean invert;
301
302
65.6k
  GET_TJINSTANCE(handle, NULL)
303
304
65.6k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
65.6k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
65.6k
    THROW("Invalid argument");
307
65.6k
  if ((align & (align - 1)) != 0)
308
65.6k
    THROW("Alignment must be a power of 2");
309
310
  /* The instance handle passed to this function is used only for parameter
311
     retrieval.  Create a new temporary instance to avoid interfering with the
312
     libjpeg state of the primary instance. */
313
65.6k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
65.6k
  this2 = (tjinstance *)handle2;
315
65.6k
  cinfo = &this2->cinfo;
316
317
65.6k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
65.6k
  else if (tempc == EOF)
320
65.6k
    THROW("Input file contains no data");
321
322
65.6k
  CATCH_LIBJPEG2();
323
324
48.1k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
48.1k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
48.1k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
48.1k
  if (tempc == 'B') {
328
16.5k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
16.5k
      THROW("Could not initialize bitmap loader");
330
16.5k
    invert = !this->bottomUp;
331
48.7k
  } else if (tempc == 'P') {
332
48.7k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
48.7k
      THROW("Could not initialize PPM loader");
334
48.7k
    invert = this->bottomUp;
335
48.7k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
65.2k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
65.2k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
65.2k
  src->max_pixels = this->maxPixels;
343
65.2k
  (*src->start_input) (cinfo, src);
344
65.2k
  if (tempc == 'B') {
345
4.92k
    if (cinfo->X_density && cinfo->Y_density) {
346
1.07k
      this->xDensity = cinfo->X_density;
347
1.07k
      this->yDensity = cinfo->Y_density;
348
1.07k
      this->densityUnits = cinfo->density_unit;
349
1.07k
    }
350
4.92k
  }
351
65.2k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
65.2k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
65.2k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
65.2k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
65.2k
  if (
359
#if ULLONG_MAX > SIZE_MAX
360
      (unsigned long long)pitch * (unsigned long long)(*height) *
361
      (unsigned long long)sizeof(_JSAMPLE) >
362
      (unsigned long long)((size_t)-1) ||
363
#endif
364
65.2k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
65.2k
                                   sizeof(_JSAMPLE))) == NULL)
366
65.2k
    THROW("Memory allocation failure");
367
368
65.2k
  CATCH_LIBJPEG2();
369
370
95.8M
  while (cinfo->next_scanline < cinfo->image_height) {
371
95.8M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
191M
    for (i = 0; i < nlines; i++) {
374
95.8M
      _JSAMPLE *dstptr;
375
95.8M
      int row;
376
377
95.8M
      row = cinfo->next_scanline + i;
378
95.8M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
77.3M
      else dstptr = &dstBuf[row * pitch];
380
95.8M
      memcpy(dstptr, src->_buffer[i],
381
95.8M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
95.8M
    }
383
95.8M
    cinfo->next_scanline += nlines;
384
95.8M
  }
385
386
47.9k
  (*src->finish_input) (cinfo, src);
387
388
65.6k
bailout:
389
65.6k
  tj3Destroy(handle2);
390
65.6k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
65.6k
  return dstBuf;
392
47.9k
}
_tj3LoadImageFromFileHandle12
Line
Count
Source
289
31.9k
{
290
31.9k
  static const char FUNCTION_NAME[] =
291
31.9k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
31.9k
  int retval = 0, tempc;
294
31.9k
  size_t pitch;
295
31.9k
  tjhandle handle2 = NULL;
296
31.9k
  tjinstance *this2;
297
31.9k
  j_compress_ptr cinfo = NULL;
298
31.9k
  cjpeg_source_ptr src;
299
31.9k
  _JSAMPLE *dstBuf = NULL;
300
31.9k
  boolean invert;
301
302
31.9k
  GET_TJINSTANCE(handle, NULL)
303
304
31.9k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
31.9k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
31.9k
    THROW("Invalid argument");
307
31.9k
  if ((align & (align - 1)) != 0)
308
31.9k
    THROW("Alignment must be a power of 2");
309
310
  /* The instance handle passed to this function is used only for parameter
311
     retrieval.  Create a new temporary instance to avoid interfering with the
312
     libjpeg state of the primary instance. */
313
31.9k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
31.9k
  this2 = (tjinstance *)handle2;
315
31.9k
  cinfo = &this2->cinfo;
316
317
31.9k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
31.9k
  else if (tempc == EOF)
320
31.9k
    THROW("Input file contains no data");
321
322
31.9k
  CATCH_LIBJPEG2();
323
324
27.7k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
27.7k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
27.7k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
27.7k
  if (tempc == 'B') {
328
14
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
14
      THROW("Could not initialize bitmap loader");
330
14
    invert = !this->bottomUp;
331
31.6k
  } else if (tempc == 'P') {
332
31.6k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
31.6k
      THROW("Could not initialize PPM loader");
334
31.6k
    invert = this->bottomUp;
335
31.6k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
31.7k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
31.7k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
31.7k
  src->max_pixels = this->maxPixels;
343
31.7k
  (*src->start_input) (cinfo, src);
344
31.7k
  if (tempc == 'B') {
345
0
    if (cinfo->X_density && cinfo->Y_density) {
346
0
      this->xDensity = cinfo->X_density;
347
0
      this->yDensity = cinfo->Y_density;
348
0
      this->densityUnits = cinfo->density_unit;
349
0
    }
350
0
  }
351
31.7k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
31.7k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
31.7k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
31.7k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
31.7k
  if (
359
#if ULLONG_MAX > SIZE_MAX
360
      (unsigned long long)pitch * (unsigned long long)(*height) *
361
      (unsigned long long)sizeof(_JSAMPLE) >
362
      (unsigned long long)((size_t)-1) ||
363
#endif
364
31.7k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
31.7k
                                   sizeof(_JSAMPLE))) == NULL)
366
31.7k
    THROW("Memory allocation failure");
367
368
31.7k
  CATCH_LIBJPEG2();
369
370
66.3M
  while (cinfo->next_scanline < cinfo->image_height) {
371
66.3M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
132M
    for (i = 0; i < nlines; i++) {
374
66.3M
      _JSAMPLE *dstptr;
375
66.3M
      int row;
376
377
66.3M
      row = cinfo->next_scanline + i;
378
66.3M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
56.8M
      else dstptr = &dstBuf[row * pitch];
380
66.3M
      memcpy(dstptr, src->_buffer[i],
381
66.3M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
66.3M
    }
383
66.3M
    cinfo->next_scanline += nlines;
384
66.3M
  }
385
386
22.8k
  (*src->finish_input) (cinfo, src);
387
388
31.9k
bailout:
389
31.9k
  tj3Destroy(handle2);
390
31.9k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
31.9k
  return dstBuf;
392
22.8k
}
_tj3LoadImageFromFileHandle16
Line
Count
Source
289
11.2k
{
290
11.2k
  static const char FUNCTION_NAME[] =
291
11.2k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
292
293
11.2k
  int retval = 0, tempc;
294
11.2k
  size_t pitch;
295
11.2k
  tjhandle handle2 = NULL;
296
11.2k
  tjinstance *this2;
297
11.2k
  j_compress_ptr cinfo = NULL;
298
11.2k
  cjpeg_source_ptr src;
299
11.2k
  _JSAMPLE *dstBuf = NULL;
300
11.2k
  boolean invert;
301
302
11.2k
  GET_TJINSTANCE(handle, NULL)
303
304
11.2k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
305
11.2k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
306
11.2k
    THROW("Invalid argument");
307
11.2k
  if ((align & (align - 1)) != 0)
308
11.2k
    THROW("Alignment must be a power of 2");
309
310
  /* The instance handle passed to this function is used only for parameter
311
     retrieval.  Create a new temporary instance to avoid interfering with the
312
     libjpeg state of the primary instance. */
313
11.2k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
314
11.2k
  this2 = (tjinstance *)handle2;
315
11.2k
  cinfo = &this2->cinfo;
316
317
11.2k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
318
0
    THROW_UNIX("Could not read input file")
319
11.2k
  else if (tempc == EOF)
320
11.2k
    THROW("Input file contains no data");
321
322
11.2k
  CATCH_LIBJPEG2();
323
324
9.33k
  cinfo->data_precision = BITS_IN_JSAMPLE;
325
9.33k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
326
9.33k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
327
9.33k
  if (tempc == 'B') {
328
7
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
329
7
      THROW("Could not initialize bitmap loader");
330
7
    invert = !this->bottomUp;
331
11.1k
  } else if (tempc == 'P') {
332
11.1k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
333
11.1k
      THROW("Could not initialize PPM loader");
334
11.1k
    invert = this->bottomUp;
335
11.1k
  } else
336
18.4E
    THROW("Unsupported file type");
337
338
11.1k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
339
340
11.1k
  src->input_file = file;
341
  /* Refuse to load images larger than the specified size. */
342
11.1k
  src->max_pixels = this->maxPixels;
343
11.1k
  (*src->start_input) (cinfo, src);
344
11.1k
  if (tempc == 'B') {
345
0
    if (cinfo->X_density && cinfo->Y_density) {
346
0
      this->xDensity = cinfo->X_density;
347
0
      this->yDensity = cinfo->Y_density;
348
0
      this->densityUnits = cinfo->density_unit;
349
0
    }
350
0
  }
351
11.1k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
352
353
11.1k
  *width = cinfo->image_width;  *height = cinfo->image_height;
354
11.1k
  *pixelFormat = cs2pf[cinfo->in_color_space];
355
356
11.1k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
357
358
11.1k
  if (
359
#if ULLONG_MAX > SIZE_MAX
360
      (unsigned long long)pitch * (unsigned long long)(*height) *
361
      (unsigned long long)sizeof(_JSAMPLE) >
362
      (unsigned long long)((size_t)-1) ||
363
#endif
364
11.1k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
365
11.1k
                                   sizeof(_JSAMPLE))) == NULL)
366
11.1k
    THROW("Memory allocation failure");
367
368
11.1k
  CATCH_LIBJPEG2();
369
370
14.4M
  while (cinfo->next_scanline < cinfo->image_height) {
371
14.4M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
372
373
28.9M
    for (i = 0; i < nlines; i++) {
374
14.4M
      _JSAMPLE *dstptr;
375
14.4M
      int row;
376
377
14.4M
      row = cinfo->next_scanline + i;
378
14.4M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
379
12.3M
      else dstptr = &dstBuf[row * pitch];
380
14.4M
      memcpy(dstptr, src->_buffer[i],
381
14.4M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
382
14.4M
    }
383
14.4M
    cinfo->next_scanline += nlines;
384
14.4M
  }
385
386
6.72k
  (*src->finish_input) (cinfo, src);
387
388
11.2k
bailout:
389
11.2k
  tj3Destroy(handle2);
390
11.2k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
391
11.2k
  return dstBuf;
392
6.72k
}
393
394
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
395
396
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
397
  (tjhandle handle, const char *filename, int *width, int align, int *height,
398
   int *pixelFormat)
399
0
{
400
0
  static const char FUNCTION_NAME[] =
401
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
402
403
0
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
404
405
0
  int retval = 0;
406
0
  _JSAMPLE *dstBuf = NULL;
407
0
  FILE *file = NULL;
408
409
0
  GET_TJINSTANCE(handle, NULL)
410
411
0
  if (!filename)
412
0
    THROW("Invalid argument");
413
414
#ifdef _MSC_VER
415
  if (fopen_s(&file, filename, "rb") || file == NULL)
416
#else
417
0
  if ((file = fopen(filename, "rb")) == NULL)
418
0
#endif
419
0
    THROW_UNIX("Cannot open input file");
420
421
0
  dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
422
0
             (handle, file, width, align, height, pixelFormat);
423
424
0
bailout:
425
0
  if (file) fclose(file);
426
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
427
0
  return dstBuf;
428
429
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
430
431
  static const char ERROR_MSG[] =
432
    "16-bit data precision requires lossless JPEG,\n"
433
    "which was disabled at build time.";
434
  _JSAMPLE *retval = NULL;
435
436
  GET_TJINSTANCE(handle, NULL)
437
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
438
           ERROR_MSG);
439
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
440
441
bailout:
442
  return retval;
443
444
#endif
445
0
}
Unexecuted instantiation: tj3LoadImage8
Unexecuted instantiation: tj3LoadImage12
Unexecuted instantiation: tj3LoadImage16
446
447
448
/* TurboJPEG 3+ */
449
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
450
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
451
   int pitch, int height, int pixelFormat)
452
0
{
453
0
  static const char FUNCTION_NAME[] =
454
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
455
0
  int retval = 0;
456
457
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
458
459
0
  tjhandle handle2 = NULL;
460
0
  tjinstance *this2;
461
0
  j_decompress_ptr dinfo = NULL;
462
0
  djpeg_dest_ptr dst;
463
0
  FILE *file = NULL;
464
0
  const char *ptr = NULL;
465
0
  boolean invert;
466
467
0
  GET_TJINSTANCE(handle, -1)
468
469
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
470
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
471
0
    THROW("Invalid argument");
472
473
  /* The instance handle passed to this function is used only for parameter
474
     retrieval.  Create a new temporary instance to avoid interfering with the
475
     libjpeg state of the primary instance. */
476
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
477
0
    return -1;
478
0
  this2 = (tjinstance *)handle2;
479
0
  dinfo = &this2->dinfo;
480
481
#ifdef _MSC_VER
482
  if (fopen_s(&file, filename, "wb") || file == NULL)
483
#else
484
0
  if ((file = fopen(filename, "wb")) == NULL)
485
0
#endif
486
0
    THROW_UNIX("Cannot open output file");
487
488
0
  CATCH_LIBJPEG2();
489
490
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
491
0
  dinfo->image_width = width;  dinfo->image_height = height;
492
0
  dinfo->global_state = DSTATE_READY;
493
0
  dinfo->scale_num = dinfo->scale_denom = 1;
494
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
495
496
0
  ptr = strrchr(filename, '.');
497
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
498
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
499
0
      THROW("Could not initialize bitmap writer");
500
0
    invert = !this->bottomUp;
501
0
    dinfo->X_density = (UINT16)this->xDensity;
502
0
    dinfo->Y_density = (UINT16)this->yDensity;
503
0
    dinfo->density_unit = (UINT8)this->densityUnits;
504
0
  } else {
505
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
506
0
      THROW("Could not initialize PPM writer");
507
0
    invert = this->bottomUp;
508
0
  }
509
510
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
511
512
0
  dst->output_file = file;
513
0
  (*dst->start_output) (dinfo, dst);
514
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
515
516
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
517
0
  else if (pitch < width * tjPixelSize[pixelFormat])
518
0
    THROW("Invalid argument");
519
520
0
  while (dinfo->output_scanline < dinfo->output_height) {
521
0
    _JSAMPLE *rowptr;
522
523
0
    if (invert)
524
0
      rowptr =
525
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
526
0
    else
527
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
528
0
    memcpy(dst->_buffer[0], rowptr,
529
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
530
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
531
0
    dinfo->output_scanline++;
532
0
  }
533
534
0
  (*dst->finish_output) (dinfo, dst);
535
536
0
bailout:
537
0
  tj3Destroy(handle2);
538
0
  if (file) fclose(file);
539
0
  return retval;
540
541
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
542
543
  GET_TJINSTANCE(handle, -1)
544
  THROW("16-bit data precision requires lossless JPEG,\n"
545
        "which was disabled at build time.")
546
bailout:
547
  return retval;
548
549
#endif
550
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
551
552
553
#undef _JSAMPLE
554
#undef _JSAMPROW
555
#undef _buffer
556
#undef _jinit_read_ppm
557
#undef _jinit_write_ppm
558
#undef _jpeg_crop_scanline
559
#undef _jpeg_read_scanlines
560
#undef _jpeg_skip_scanlines
561
#undef _jpeg_write_scanlines