Coverage Report

Created: 2026-01-25 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/src/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
17.6M
#define _JSAMPLE  JSAMPLE
34
6.39k
#define _JSAMPROW  JSAMPROW
35
17.6M
#define _buffer  buffer
36
12.0k
#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
0
#define _jpeg_read_scanlines  jpeg_read_scanlines
40
0
#define _jpeg_skip_scanlines  jpeg_skip_scanlines
41
6.31k
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
0
#define _JSAMPLE  J12SAMPLE
44
0
#define _JSAMPROW  J12SAMPROW
45
0
#define _buffer  buffer12
46
0
#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
0
#define _jpeg_read_scanlines  jpeg12_read_scanlines
50
0
#define _jpeg_skip_scanlines  jpeg12_skip_scanlines
51
0
#define _jpeg_write_scanlines  jpeg12_write_scanlines
52
#elif BITS_IN_JSAMPLE == 16
53
0
#define _JSAMPLE  J16SAMPLE
54
0
#define _JSAMPROW  J16SAMPROW
55
0
#define _buffer  buffer16
56
0
#define _jinit_read_ppm  j16init_read_ppm
57
0
#define _jinit_write_ppm  j16init_write_ppm
58
0
#define _jpeg_read_scanlines  jpeg16_read_scanlines
59
0
#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
24.9k
#define _GET_STRING(name, suffix)  #name #suffix
65
24.9k
#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
6.39k
{
75
6.39k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
6.39k
  int i, retval = 0;
77
6.39k
  boolean alloc = TRUE;
78
6.39k
  _JSAMPROW *row_pointer = NULL;
79
80
6.39k
  GET_CINSTANCE(handle)
81
6.39k
  if ((this->init & COMPRESS) == 0)
82
6.39k
    THROW("Instance has not been initialized for compression");
83
84
6.39k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
6.39k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
6.39k
      jpegSize == NULL)
87
6.39k
    THROW("Invalid argument");
88
89
6.39k
  if (!this->lossless && this->quality == -1)
90
6.39k
    THROW("TJPARAM_QUALITY must be specified");
91
6.39k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
6.39k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
6.39k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
6.39k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
6.39k
    THROW("Memory allocation failure");
98
99
6.39k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
79
    retval = -1;  goto bailout;
102
79
  }
103
104
6.31k
  cinfo->image_width = width;
105
6.31k
  cinfo->image_height = height;
106
6.31k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
#if BITS_IN_JSAMPLE == 8
108
6.39k
  if (this->lossless && this->precision >= 2 &&
109
6.39k
      this->precision <= BITS_IN_JSAMPLE)
110
#else
111
0
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
112
0
      this->precision <= BITS_IN_JSAMPLE)
113
0
#endif
114
6.39k
    cinfo->data_precision = this->precision;
115
116
6.31k
  setCompDefaults(this, pixelFormat, FALSE);
117
6.31k
  if (this->noRealloc) alloc = FALSE;
118
6.31k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
119
120
6.31k
  jpeg_start_compress(cinfo, TRUE);
121
6.31k
  if (this->iccBuf != NULL && this->iccSize != 0)
122
4.89k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
123
15.2M
  for (i = 0; i < height; i++) {
124
15.2M
    if (this->bottomUp)
125
2.25M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
126
12.9M
    else
127
12.9M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
128
15.2M
  }
129
12.6k
  while (cinfo->next_scanline < cinfo->image_height)
130
6.31k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
131
6.31k
                          cinfo->image_height - cinfo->next_scanline);
132
6.31k
  jpeg_finish_compress(cinfo);
133
134
6.39k
bailout:
135
6.39k
  if (cinfo->global_state > CSTATE_START && alloc)
136
0
    (*cinfo->dest->term_destination) (cinfo);
137
6.39k
  if (cinfo->global_state > CSTATE_START || retval == -1)
138
79
    jpeg_abort_compress(cinfo);
139
6.39k
  free(row_pointer);
140
6.39k
  if (this->jerr.warning) retval = -1;
141
6.39k
  return retval;
142
6.31k
}
tj3Compress8
Line
Count
Source
74
6.39k
{
75
6.39k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
6.39k
  int i, retval = 0;
77
6.39k
  boolean alloc = TRUE;
78
6.39k
  _JSAMPROW *row_pointer = NULL;
79
80
6.39k
  GET_CINSTANCE(handle)
81
6.39k
  if ((this->init & COMPRESS) == 0)
82
6.39k
    THROW("Instance has not been initialized for compression");
83
84
6.39k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
6.39k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
6.39k
      jpegSize == NULL)
87
6.39k
    THROW("Invalid argument");
88
89
6.39k
  if (!this->lossless && this->quality == -1)
90
6.39k
    THROW("TJPARAM_QUALITY must be specified");
91
6.39k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
6.39k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
6.39k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
6.39k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
6.39k
    THROW("Memory allocation failure");
98
99
6.39k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
79
    retval = -1;  goto bailout;
102
79
  }
103
104
6.31k
  cinfo->image_width = width;
105
6.31k
  cinfo->image_height = height;
106
6.31k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
6.31k
#if BITS_IN_JSAMPLE == 8
108
6.39k
  if (this->lossless && this->precision >= 2 &&
109
6.39k
      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
6.39k
    cinfo->data_precision = this->precision;
115
116
6.31k
  setCompDefaults(this, pixelFormat, FALSE);
117
6.31k
  if (this->noRealloc) alloc = FALSE;
118
6.31k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
119
120
6.31k
  jpeg_start_compress(cinfo, TRUE);
121
6.31k
  if (this->iccBuf != NULL && this->iccSize != 0)
122
4.89k
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
123
15.2M
  for (i = 0; i < height; i++) {
124
15.2M
    if (this->bottomUp)
125
2.25M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
126
12.9M
    else
127
12.9M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
128
15.2M
  }
129
12.6k
  while (cinfo->next_scanline < cinfo->image_height)
130
6.31k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
131
6.31k
                          cinfo->image_height - cinfo->next_scanline);
132
6.31k
  jpeg_finish_compress(cinfo);
133
134
6.39k
bailout:
135
6.39k
  if (cinfo->global_state > CSTATE_START && alloc)
136
0
    (*cinfo->dest->term_destination) (cinfo);
137
6.39k
  if (cinfo->global_state > CSTATE_START || retval == -1)
138
79
    jpeg_abort_compress(cinfo);
139
6.39k
  free(row_pointer);
140
6.39k
  if (this->jerr.warning) retval = -1;
141
6.39k
  return retval;
142
6.31k
}
Unexecuted instantiation: tj3Compress12
Unexecuted instantiation: tj3Compress16
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
0
{
152
0
  static const char FUNCTION_NAME[] =
153
0
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
154
0
  _JSAMPROW *row_pointer = NULL;
155
0
  int croppedHeight, i, retval = 0;
156
#if BITS_IN_JSAMPLE != 16
157
  int scaledWidth;
158
#endif
159
0
  struct my_progress_mgr progress;
160
161
0
  GET_DINSTANCE(handle);
162
0
  if ((this->init & DECOMPRESS) == 0)
163
0
    THROW("Instance has not been initialized for decompression");
164
165
0
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
166
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
167
0
    THROW("Invalid argument");
168
169
0
  if (this->scanLimit) {
170
0
    memset(&progress, 0, sizeof(struct my_progress_mgr));
171
0
    progress.pub.progress_monitor = my_progress_monitor;
172
0
    progress.this = this;
173
0
    dinfo->progress = &progress.pub;
174
0
  } else
175
0
    dinfo->progress = NULL;
176
177
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
178
179
0
  if (setjmp(this->jerr.setjmp_buffer)) {
180
    /* If we get here, the JPEG code has signaled an error. */
181
0
    retval = -1;  goto bailout;
182
0
  }
183
184
0
  if (dinfo->global_state <= DSTATE_INHEADER) {
185
0
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
186
0
    jpeg_read_header(dinfo, TRUE);
187
0
  }
188
0
  setDecompParameters(this);
189
0
  if (this->maxPixels &&
190
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
191
0
      (unsigned long long)this->maxPixels)
192
0
    THROW("Image is too large");
193
0
  this->dinfo.out_color_space = pf2cs[pixelFormat];
194
#if BITS_IN_JSAMPLE != 16
195
0
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
196
#endif
197
0
  dinfo->do_fancy_upsampling = !this->fastUpsample;
198
0
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
199
200
0
  dinfo->scale_num = this->scalingFactor.num;
201
0
  dinfo->scale_denom = this->scalingFactor.denom;
202
203
0
  jpeg_start_decompress(dinfo);
204
205
#if BITS_IN_JSAMPLE != 16
206
0
  if (this->croppingRegion.x != 0 ||
207
0
      (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
0
#endif
222
223
0
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
224
225
0
  croppedHeight = dinfo->output_height;
226
#if BITS_IN_JSAMPLE != 16
227
0
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
228
0
    croppedHeight = this->croppingRegion.h;
229
#endif
230
0
  if ((row_pointer =
231
0
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
232
0
    THROW("Memory allocation failure");
233
0
  if (setjmp(this->jerr.setjmp_buffer)) {
234
    /* If we get here, the JPEG code has signaled an error. */
235
0
    retval = -1;  goto bailout;
236
0
  }
237
0
  for (i = 0; i < (int)croppedHeight; i++) {
238
0
    if (this->bottomUp)
239
0
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
240
0
    else
241
0
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
242
0
  }
243
244
#if BITS_IN_JSAMPLE != 16
245
0
  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
0
#endif
275
0
  {
276
0
    while (dinfo->output_scanline < dinfo->output_height)
277
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
278
0
                           dinfo->output_height - dinfo->output_scanline);
279
0
  }
280
0
  jpeg_finish_decompress(dinfo);
281
282
0
bailout:
283
0
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
284
0
  free(row_pointer);
285
0
  if (this->jerr.warning) retval = -1;
286
0
  return retval;
287
0
}
Unexecuted instantiation: tj3Decompress8
Unexecuted instantiation: tj3Decompress12
Unexecuted instantiation: tj3Decompress16
288
289
290
/*************************** Packed-Pixel Image I/O **************************/
291
292
/* TurboJPEG 3.0+ */
293
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
294
static
295
#endif
296
_JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
297
  (tjhandle handle, FILE *file, int *width, int align, int *height,
298
   int *pixelFormat)
299
18.5k
{
300
18.5k
  static const char FUNCTION_NAME[] =
301
18.5k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
302
303
18.5k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
304
305
18.5k
  int retval = 0, tempc;
306
18.5k
  size_t pitch;
307
18.5k
  tjhandle handle2 = NULL;
308
18.5k
  tjinstance *this2;
309
18.5k
  j_compress_ptr cinfo = NULL;
310
18.5k
  cjpeg_source_ptr src;
311
18.5k
  _JSAMPLE *dstBuf = NULL;
312
18.5k
  boolean invert;
313
314
18.5k
  GET_TJINSTANCE(handle, NULL)
315
316
18.5k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
317
18.5k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
318
18.5k
    THROW("Invalid argument");
319
18.5k
  if ((align & (align - 1)) != 0)
320
18.5k
    THROW("Alignment must be a power of 2");
321
322
  /* The instance handle passed to this function is used only for parameter
323
     retrieval.  Create a new temporary instance to avoid interfering with the
324
     libjpeg state of the primary instance. */
325
18.5k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
326
18.5k
  this2 = (tjinstance *)handle2;
327
18.5k
  cinfo = &this2->cinfo;
328
329
18.5k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
330
0
    THROW_UNIX("Could not read input file")
331
18.5k
  else if (tempc == EOF)
332
18.5k
    THROW("Input file contains no data");
333
334
18.5k
  if (setjmp(this2->jerr.setjmp_buffer)) {
335
    /* If we get here, the JPEG code has signaled an error. */
336
6.22k
    retval = -1;  goto bailout;
337
6.22k
  }
338
339
12.2k
  cinfo->data_precision = BITS_IN_JSAMPLE;
340
12.2k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
341
12.2k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
342
12.2k
  if (tempc == 'B') {
343
6.11k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
344
6.11k
      THROW("Could not initialize bitmap loader");
345
6.11k
    invert = !this->bottomUp;
346
12.0k
  } else if (tempc == 'P') {
347
#if BITS_IN_JSAMPLE == 8
348
12.0k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
349
#else
350
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
351
0
        this->precision <= BITS_IN_JSAMPLE)
352
0
#endif
353
12.0k
      cinfo->data_precision = this->precision;
354
12.0k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
355
12.0k
      THROW("Could not initialize PPM loader");
356
12.0k
    invert = this->bottomUp;
357
12.0k
  } else
358
18.4E
    THROW("Unsupported file type");
359
360
18.1k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
361
362
18.1k
  src->input_file = file;
363
  /* Refuse to load images larger than the specified size. */
364
18.1k
  src->max_pixels = this->maxPixels;
365
18.1k
  (*src->start_input) (cinfo, src);
366
18.1k
  if (tempc == 'B') {
367
1.92k
    if (cinfo->X_density && cinfo->Y_density) {
368
360
      this->xDensity = cinfo->X_density;
369
360
      this->yDensity = cinfo->Y_density;
370
360
      this->densityUnits = cinfo->density_unit;
371
360
    }
372
1.92k
  }
373
18.1k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
374
375
18.1k
  *width = cinfo->image_width;  *height = cinfo->image_height;
376
18.1k
  *pixelFormat = cs2pf[cinfo->in_color_space];
377
378
18.1k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
379
18.1k
  if (
380
#if ULLONG_MAX > SIZE_MAX
381
      (unsigned long long)pitch * (unsigned long long)(*height) >
382
      (unsigned long long)((size_t)-1) ||
383
#endif
384
18.1k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
385
18.1k
                                   sizeof(_JSAMPLE))) == NULL)
386
18.1k
    THROW("Memory allocation failure");
387
388
18.1k
  if (setjmp(this2->jerr.setjmp_buffer)) {
389
    /* If we get here, the JPEG code has signaled an error. */
390
7.65k
    retval = -1;  goto bailout;
391
7.65k
  }
392
393
17.6M
  while (cinfo->next_scanline < cinfo->image_height) {
394
17.6M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
395
396
35.2M
    for (i = 0; i < nlines; i++) {
397
17.6M
      _JSAMPLE *dstptr;
398
17.6M
      int row;
399
400
17.6M
      row = cinfo->next_scanline + i;
401
17.6M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
402
12.0M
      else dstptr = &dstBuf[row * pitch];
403
17.6M
      memcpy(dstptr, src->_buffer[i],
404
17.6M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
405
17.6M
    }
406
17.6M
    cinfo->next_scanline += nlines;
407
17.6M
  }
408
409
10.5k
  (*src->finish_input) (cinfo, src);
410
411
18.5k
bailout:
412
18.5k
  tj3Destroy(handle2);
413
18.5k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
414
18.5k
  return dstBuf;
415
416
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
417
418
  static const char ERROR_MSG[] =
419
    "16-bit data precision requires lossless JPEG,\n"
420
    "which was disabled at build time.";
421
  _JSAMPLE *retval = NULL;
422
423
  GET_TJINSTANCE(handle, NULL)
424
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
425
           ERROR_MSG);
426
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
427
428
bailout:
429
  return retval;
430
431
#endif
432
10.5k
}
_tj3LoadImageFromFileHandle8
Line
Count
Source
299
18.5k
{
300
18.5k
  static const char FUNCTION_NAME[] =
301
18.5k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
302
303
18.5k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
304
305
18.5k
  int retval = 0, tempc;
306
18.5k
  size_t pitch;
307
18.5k
  tjhandle handle2 = NULL;
308
18.5k
  tjinstance *this2;
309
18.5k
  j_compress_ptr cinfo = NULL;
310
18.5k
  cjpeg_source_ptr src;
311
18.5k
  _JSAMPLE *dstBuf = NULL;
312
18.5k
  boolean invert;
313
314
18.5k
  GET_TJINSTANCE(handle, NULL)
315
316
18.5k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
317
18.5k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
318
18.5k
    THROW("Invalid argument");
319
18.5k
  if ((align & (align - 1)) != 0)
320
18.5k
    THROW("Alignment must be a power of 2");
321
322
  /* The instance handle passed to this function is used only for parameter
323
     retrieval.  Create a new temporary instance to avoid interfering with the
324
     libjpeg state of the primary instance. */
325
18.5k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
326
18.5k
  this2 = (tjinstance *)handle2;
327
18.5k
  cinfo = &this2->cinfo;
328
329
18.5k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
330
0
    THROW_UNIX("Could not read input file")
331
18.5k
  else if (tempc == EOF)
332
18.5k
    THROW("Input file contains no data");
333
334
18.5k
  if (setjmp(this2->jerr.setjmp_buffer)) {
335
    /* If we get here, the JPEG code has signaled an error. */
336
6.22k
    retval = -1;  goto bailout;
337
6.22k
  }
338
339
12.2k
  cinfo->data_precision = BITS_IN_JSAMPLE;
340
12.2k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
341
12.2k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
342
12.2k
  if (tempc == 'B') {
343
6.11k
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
344
6.11k
      THROW("Could not initialize bitmap loader");
345
6.11k
    invert = !this->bottomUp;
346
12.0k
  } else if (tempc == 'P') {
347
12.0k
#if BITS_IN_JSAMPLE == 8
348
12.0k
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
349
#else
350
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
351
        this->precision <= BITS_IN_JSAMPLE)
352
#endif
353
12.0k
      cinfo->data_precision = this->precision;
354
12.0k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
355
12.0k
      THROW("Could not initialize PPM loader");
356
12.0k
    invert = this->bottomUp;
357
12.0k
  } else
358
18.4E
    THROW("Unsupported file type");
359
360
18.1k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
361
362
18.1k
  src->input_file = file;
363
  /* Refuse to load images larger than the specified size. */
364
18.1k
  src->max_pixels = this->maxPixels;
365
18.1k
  (*src->start_input) (cinfo, src);
366
18.1k
  if (tempc == 'B') {
367
1.92k
    if (cinfo->X_density && cinfo->Y_density) {
368
360
      this->xDensity = cinfo->X_density;
369
360
      this->yDensity = cinfo->Y_density;
370
360
      this->densityUnits = cinfo->density_unit;
371
360
    }
372
1.92k
  }
373
18.1k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
374
375
18.1k
  *width = cinfo->image_width;  *height = cinfo->image_height;
376
18.1k
  *pixelFormat = cs2pf[cinfo->in_color_space];
377
378
18.1k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
379
18.1k
  if (
380
#if ULLONG_MAX > SIZE_MAX
381
      (unsigned long long)pitch * (unsigned long long)(*height) >
382
      (unsigned long long)((size_t)-1) ||
383
#endif
384
18.1k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
385
18.1k
                                   sizeof(_JSAMPLE))) == NULL)
386
18.1k
    THROW("Memory allocation failure");
387
388
18.1k
  if (setjmp(this2->jerr.setjmp_buffer)) {
389
    /* If we get here, the JPEG code has signaled an error. */
390
7.65k
    retval = -1;  goto bailout;
391
7.65k
  }
392
393
17.6M
  while (cinfo->next_scanline < cinfo->image_height) {
394
17.6M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
395
396
35.2M
    for (i = 0; i < nlines; i++) {
397
17.6M
      _JSAMPLE *dstptr;
398
17.6M
      int row;
399
400
17.6M
      row = cinfo->next_scanline + i;
401
17.6M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
402
12.0M
      else dstptr = &dstBuf[row * pitch];
403
17.6M
      memcpy(dstptr, src->_buffer[i],
404
17.6M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
405
17.6M
    }
406
17.6M
    cinfo->next_scanline += nlines;
407
17.6M
  }
408
409
10.5k
  (*src->finish_input) (cinfo, src);
410
411
18.5k
bailout:
412
18.5k
  tj3Destroy(handle2);
413
18.5k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
414
18.5k
  return dstBuf;
415
416
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
417
418
  static const char ERROR_MSG[] =
419
    "16-bit data precision requires lossless JPEG,\n"
420
    "which was disabled at build time.";
421
  _JSAMPLE *retval = NULL;
422
423
  GET_TJINSTANCE(handle, NULL)
424
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
425
           ERROR_MSG);
426
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
427
428
bailout:
429
  return retval;
430
431
#endif
432
10.5k
}
Unexecuted instantiation: _tj3LoadImageFromFileHandle12
Unexecuted instantiation: _tj3LoadImageFromFileHandle16
433
434
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
435
  (tjhandle handle, const char *filename, int *width, int align, int *height,
436
   int *pixelFormat)
437
0
{
438
0
  static const char FUNCTION_NAME[] =
439
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
440
441
0
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
442
443
0
  int retval = 0;
444
0
  _JSAMPLE *dstBuf = NULL;
445
0
  FILE *file = NULL;
446
447
0
  GET_TJINSTANCE(handle, NULL)
448
449
0
  if (!filename)
450
0
    THROW("Invalid argument");
451
452
#ifdef _MSC_VER
453
  if (fopen_s(&file, filename, "rb") || file == NULL)
454
#else
455
0
  if ((file = fopen(filename, "rb")) == NULL)
456
0
#endif
457
0
    THROW_UNIX("Cannot open input file");
458
459
0
  dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
460
0
             (handle, file, width, align, height, pixelFormat);
461
462
0
bailout:
463
0
  if (file) fclose(file);
464
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
465
0
  return dstBuf;
466
467
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
468
469
  static const char ERROR_MSG[] =
470
    "16-bit data precision requires lossless JPEG,\n"
471
    "which was disabled at build time.";
472
  _JSAMPLE *retval = NULL;
473
474
  GET_TJINSTANCE(handle, NULL)
475
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
476
           ERROR_MSG);
477
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
478
479
bailout:
480
  return retval;
481
482
#endif
483
0
}
Unexecuted instantiation: tj3LoadImage8
Unexecuted instantiation: tj3LoadImage12
Unexecuted instantiation: tj3LoadImage16
484
485
486
/* TurboJPEG 3.0+ */
487
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
488
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
489
   int pitch, int height, int pixelFormat)
490
0
{
491
0
  static const char FUNCTION_NAME[] =
492
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
493
0
  int retval = 0;
494
495
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
496
497
0
  tjhandle handle2 = NULL;
498
0
  tjinstance *this2;
499
0
  j_decompress_ptr dinfo = NULL;
500
0
  djpeg_dest_ptr dst;
501
0
  FILE *file = NULL;
502
0
  char *ptr = NULL;
503
0
  boolean invert;
504
505
0
  GET_TJINSTANCE(handle, -1)
506
507
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
508
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
509
0
    THROW("Invalid argument");
510
511
  /* The instance handle passed to this function is used only for parameter
512
     retrieval.  Create a new temporary instance to avoid interfering with the
513
     libjpeg state of the primary instance. */
514
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
515
0
    return -1;
516
0
  this2 = (tjinstance *)handle2;
517
0
  dinfo = &this2->dinfo;
518
519
#ifdef _MSC_VER
520
  if (fopen_s(&file, filename, "wb") || file == NULL)
521
#else
522
0
  if ((file = fopen(filename, "wb")) == NULL)
523
0
#endif
524
0
    THROW_UNIX("Cannot open output file");
525
526
0
  if (setjmp(this2->jerr.setjmp_buffer)) {
527
    /* If we get here, the JPEG code has signaled an error. */
528
0
    retval = -1;  goto bailout;
529
0
  }
530
531
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
532
0
  dinfo->image_width = width;  dinfo->image_height = height;
533
0
  dinfo->global_state = DSTATE_READY;
534
0
  dinfo->scale_num = dinfo->scale_denom = 1;
535
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
536
537
0
  ptr = strrchr(filename, '.');
538
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
539
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
540
0
      THROW("Could not initialize bitmap writer");
541
0
    invert = !this->bottomUp;
542
0
    dinfo->X_density = (UINT16)this->xDensity;
543
0
    dinfo->Y_density = (UINT16)this->yDensity;
544
0
    dinfo->density_unit = (UINT8)this->densityUnits;
545
0
  } else {
546
#if BITS_IN_JSAMPLE == 8
547
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
548
#else
549
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
550
0
        this->precision <= BITS_IN_JSAMPLE)
551
0
#endif
552
0
      dinfo->data_precision = this->precision;
553
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
554
0
      THROW("Could not initialize PPM writer");
555
0
    invert = this->bottomUp;
556
0
  }
557
558
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
559
560
0
  dst->output_file = file;
561
0
  (*dst->start_output) (dinfo, dst);
562
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
563
564
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
565
566
0
  while (dinfo->output_scanline < dinfo->output_height) {
567
0
    _JSAMPLE *rowptr;
568
569
0
    if (invert)
570
0
      rowptr =
571
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
572
0
    else
573
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
574
0
    memcpy(dst->_buffer[0], rowptr,
575
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
576
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
577
0
    dinfo->output_scanline++;
578
0
  }
579
580
0
  (*dst->finish_output) (dinfo, dst);
581
582
0
bailout:
583
0
  tj3Destroy(handle2);
584
0
  if (file) fclose(file);
585
0
  return retval;
586
587
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
588
589
  GET_TJINSTANCE(handle, -1)
590
  THROW("16-bit data precision requires lossless JPEG,\n"
591
        "which was disabled at build time.")
592
bailout:
593
  return retval;
594
595
#endif
596
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
597
598
599
#undef _JSAMPLE
600
#undef _JSAMPROW
601
#undef _buffer
602
#undef _jinit_read_ppm
603
#undef _jinit_write_ppm
604
#undef _jpeg_crop_scanline
605
#undef _jpeg_read_scanlines
606
#undef _jpeg_skip_scanlines
607
#undef _jpeg_write_scanlines