Coverage Report

Created: 2026-05-11 06:55

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