Coverage Report

Created: 2026-09-01 06:36

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
0
#define _JSAMPLE  J12SAMPLE
46
0
#define _JSAMPROW  J12SAMPROW
47
0
#define _buffer  buffer12
48
0
#define _jinit_read_png  j12init_read_png
49
0
#define _jinit_write_png  j12init_write_png
50
0
#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
0
#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
0
#define _GET_STRING(name, suffix)  #name #suffix
71
0
#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
0
{
81
0
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
82
0
  int i, retval = 0;
83
0
  boolean alloc = TRUE;
84
0
  _JSAMPROW *row_pointer = NULL;
85
86
0
  GET_CINSTANCE(handle)
87
0
  if ((this->init & COMPRESS) == 0)
88
0
    THROW("Instance has not been initialized for compression");
89
90
0
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
91
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
92
0
      jpegSize == NULL)
93
0
    THROW("Invalid argument");
94
95
0
  if (!this->lossless && this->quality == -1)
96
0
    THROW("TJPARAM_QUALITY must be specified");
97
0
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
98
0
    THROW("TJPARAM_SUBSAMP must be specified");
99
100
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
101
0
  else if (pitch < width * tjPixelSize[pixelFormat])
102
0
    THROW("Invalid argument");
103
104
0
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
105
0
    THROW("Memory allocation failure");
106
107
0
  CATCH_LIBJPEG(this);
108
109
0
  cinfo->image_width = width;
110
0
  cinfo->image_height = height;
111
0
  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
0
  if (this->lossless && this->precision >= BITS_IN_JSAMPLE - 3 &&
117
0
      this->precision <= BITS_IN_JSAMPLE)
118
0
#endif
119
0
    cinfo->data_precision = this->precision;
120
121
0
  setCompDefaults(this, pixelFormat, FALSE);
122
0
  if (this->noRealloc) alloc = FALSE;
123
0
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
124
125
0
  jpeg_start_compress(cinfo, TRUE);
126
0
  if (this->iccBuf != NULL && this->iccSize != 0)
127
0
    jpeg_write_icc_profile(cinfo, this->iccBuf, (unsigned int)this->iccSize);
128
0
  for (i = 0; i < height; i++) {
129
0
    if (this->bottomUp)
130
0
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
131
0
    else
132
0
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
133
0
  }
134
0
  while (cinfo->next_scanline < cinfo->image_height)
135
0
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
136
0
                          cinfo->image_height - cinfo->next_scanline);
137
0
  jpeg_finish_compress(cinfo);
138
139
0
bailout:
140
0
  if (cinfo->global_state > CSTATE_START && alloc)
141
0
    (*cinfo->dest->term_destination) (cinfo);
142
0
  if (cinfo->global_state > CSTATE_START || retval == -1)
143
0
    jpeg_abort_compress(cinfo);
144
0
  free(row_pointer);
145
0
  if (this->jerr.warning) retval = -1;
146
0
  return retval;
147
0
}
Unexecuted instantiation: tj3Compress8
Unexecuted instantiation: tj3Compress12
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
0
{
307
0
  static const char FUNCTION_NAME[] =
308
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
309
310
0
  int retval = 0, tempc;
311
0
  size_t pitch;
312
0
  tjhandle handle2 = NULL;
313
0
  tjinstance *this2;
314
0
  j_compress_ptr cinfo = NULL;
315
0
  cjpeg_source_ptr src = NULL;
316
0
  _JSAMPLE *dstBuf = NULL;
317
0
  boolean invert;
318
319
0
  GET_TJINSTANCE(handle, NULL)
320
321
0
  if (!file || !width || align < 1 || !height || !pixelFormat ||
322
0
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
323
0
    THROW("Invalid argument");
324
0
  if ((align & (align - 1)) != 0)
325
0
    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
0
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
331
0
  this2 = (tjinstance *)handle2;
332
0
  cinfo = &this2->cinfo;
333
334
0
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
335
0
    THROW_UNIX("Could not read input file")
336
0
  else if (tempc == EOF)
337
0
    THROW("Input file contains no data");
338
339
0
  CATCH_LIBJPEG2();
340
341
0
  cinfo->data_precision = BITS_IN_JSAMPLE;
342
0
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
343
0
  else cinfo->in_color_space = pf2cs[*pixelFormat];
344
0
  if (tempc == 'B') {
345
0
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
346
0
      THROW("Could not initialize bitmap loader");
347
0
    invert = !this->bottomUp;
348
0
  } else if (tempc == 0x89) {
349
#if BITS_IN_JSAMPLE == 8
350
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
351
#else
352
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
353
0
        this->precision <= BITS_IN_JSAMPLE)
354
0
#endif
355
0
      cinfo->data_precision = this->precision;
356
0
    if ((src = _jinit_read_png(cinfo)) == NULL)
357
0
      THROW("Could not initialize PNG loader");
358
0
    invert = this->bottomUp;
359
0
  } else if (tempc == 'P') {
360
#if BITS_IN_JSAMPLE == 8
361
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
362
#else
363
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
364
0
        this->precision <= BITS_IN_JSAMPLE)
365
0
#endif
366
0
      cinfo->data_precision = this->precision;
367
0
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
368
0
      THROW("Could not initialize PPM loader");
369
0
    invert = this->bottomUp;
370
0
  } else
371
0
    THROW("Unsupported file type");
372
373
0
  CATCH_LIBJPEG2();
374
375
0
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
376
377
0
  src->input_file = file;
378
  /* Refuse to load images larger than the specified size. */
379
0
  src->max_pixels = this->maxPixels;
380
0
  (*src->start_input) (cinfo, src);
381
0
  if (tempc == 'B') {
382
0
    if (cinfo->X_density && cinfo->Y_density) {
383
0
      this->xDensity = cinfo->X_density;
384
0
      this->yDensity = cinfo->Y_density;
385
0
      this->densityUnits = cinfo->density_unit;
386
0
    }
387
0
  } else if (tempc == 0x89 && (this->init & COMPRESS) &&
388
0
             (this->saveMarkers == 2 || this->saveMarkers == 4)) {
389
0
    JOCTET *iccBuf = NULL;
390
0
    unsigned int iccLen = 0;
391
392
0
    if ((*src->read_icc_profile) (cinfo, src, &iccBuf, &iccLen) && iccBuf &&
393
0
        iccLen)
394
0
      tj3SetICCProfile(handle, (unsigned char *)iccBuf, iccLen);
395
0
  }
396
0
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
397
398
0
  *width = cinfo->image_width;  *height = cinfo->image_height;
399
0
  *pixelFormat = cs2pf[cinfo->in_color_space];
400
401
0
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
402
403
0
  if (
404
#if ULLONG_MAX > SIZE_MAX
405
      (unsigned long long)pitch * (unsigned long long)(*height) *
406
      (unsigned long long)sizeof(_JSAMPLE) >
407
      (unsigned long long)((size_t)-1) ||
408
#endif
409
0
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
410
0
                                   sizeof(_JSAMPLE))) == NULL)
411
0
    THROW("Memory allocation failure");
412
413
0
  CATCH_LIBJPEG2();
414
415
0
  while (cinfo->next_scanline < cinfo->image_height) {
416
0
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
417
418
0
    for (i = 0; i < nlines; i++) {
419
0
      _JSAMPLE *dstptr;
420
0
      int row;
421
422
0
      row = cinfo->next_scanline + i;
423
0
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
424
0
      else dstptr = &dstBuf[row * pitch];
425
0
      memcpy(dstptr, src->_buffer[i],
426
0
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
427
0
    }
428
0
    cinfo->next_scanline += nlines;
429
0
  }
430
431
0
bailout:
432
0
  if (src)
433
0
    (*src->finish_input) (cinfo, src);
434
0
  tj3Destroy(handle2);
435
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
436
0
  return dstBuf;
437
0
}
Unexecuted instantiation: _tj3LoadImageFromFileHandle8
Unexecuted instantiation: _tj3LoadImageFromFileHandle12
Unexecuted instantiation: _tj3LoadImageFromFileHandle16
438
439
#endif /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
440
441
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
442
  (tjhandle handle, const char *filename, int *width, int align, int *height,
443
   int *pixelFormat)
444
0
{
445
0
  static const char FUNCTION_NAME[] =
446
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
447
448
0
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
449
450
0
  int retval = 0;
451
0
  _JSAMPLE *dstBuf = NULL;
452
0
  FILE *file = NULL;
453
454
0
  GET_TJINSTANCE(handle, NULL)
455
456
0
  if (!filename)
457
0
    THROW("Invalid argument");
458
459
#ifdef _MSC_VER
460
  if (fopen_s(&file, filename, "rb") || file == NULL)
461
#else
462
0
  if ((file = fopen(filename, "rb")) == NULL)
463
0
#endif
464
0
    THROW_UNIX("Cannot open input file");
465
466
0
  dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
467
0
             (handle, file, width, align, height, pixelFormat);
468
469
0
bailout:
470
0
  if (file) fclose(file);
471
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
472
0
  return dstBuf;
473
474
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
475
476
  static const char ERROR_MSG[] =
477
    "16-bit data precision requires lossless JPEG,\n"
478
    "which was disabled at build time.";
479
  _JSAMPLE *retval = NULL;
480
481
  GET_TJINSTANCE(handle, NULL)
482
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
483
           ERROR_MSG);
484
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
485
486
bailout:
487
  return retval;
488
489
#endif
490
0
}
Unexecuted instantiation: tj3LoadImage8
Unexecuted instantiation: tj3LoadImage12
Unexecuted instantiation: tj3LoadImage16
491
492
493
/* TurboJPEG 3.0+ */
494
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
495
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
496
   int pitch, int height, int pixelFormat)
497
0
{
498
0
  static const char FUNCTION_NAME[] =
499
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
500
0
  int retval = 0;
501
502
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
503
504
0
  tjhandle handle2 = NULL;
505
0
  tjinstance *this2;
506
0
  j_decompress_ptr dinfo = NULL;
507
0
  djpeg_dest_ptr dst;
508
0
  FILE *file = NULL;
509
0
  const char *ptr = NULL;
510
0
  boolean invert;
511
512
0
  GET_TJINSTANCE(handle, -1)
513
514
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
515
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
516
0
    THROW("Invalid argument");
517
518
  /* The instance handle passed to this function is used only for parameter
519
     retrieval.  Create a new temporary instance to avoid interfering with the
520
     libjpeg state of the primary instance. */
521
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
522
0
    return -1;
523
0
  this2 = (tjinstance *)handle2;
524
0
  dinfo = &this2->dinfo;
525
526
#ifdef _MSC_VER
527
  if (fopen_s(&file, filename, "wb") || file == NULL)
528
#else
529
0
  if ((file = fopen(filename, "wb")) == NULL)
530
0
#endif
531
0
    THROW_UNIX("Cannot open output file");
532
533
0
  CATCH_LIBJPEG2();
534
535
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
536
0
  dinfo->image_width = width;  dinfo->image_height = height;
537
0
  dinfo->global_state = DSTATE_READY;
538
0
  dinfo->scale_num = dinfo->scale_denom = 1;
539
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
540
541
0
  ptr = strrchr(filename, '.');
542
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
543
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
544
0
      THROW("Could not initialize bitmap writer");
545
0
    invert = !this->bottomUp;
546
0
    dinfo->X_density = (UINT16)this->xDensity;
547
0
    dinfo->Y_density = (UINT16)this->yDensity;
548
0
    dinfo->density_unit = (UINT8)this->densityUnits;
549
0
  } else if (ptr && !strcasecmp(ptr, ".png")) {
550
#if BITS_IN_JSAMPLE == 8
551
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
552
#else
553
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
554
0
        this->precision <= BITS_IN_JSAMPLE)
555
0
#endif
556
0
      dinfo->data_precision = this->precision;
557
0
    if ((dst = _jinit_write_png(dinfo)) == NULL)
558
0
      THROW("Could not initialize PNG writer");
559
0
    invert = this->bottomUp;
560
561
0
    if ((this->init & DECOMPRESS) && this->decompICCBuf &&
562
0
        this->decompICCSize) {
563
0
      unsigned char *iccBuf = (unsigned char *)malloc(this->decompICCSize);
564
565
0
      if (!iccBuf)
566
0
        THROW("Memory allocation failure");
567
0
      memcpy(iccBuf, this->decompICCBuf, this->decompICCSize);
568
0
      (*dst->write_icc_profile) (dinfo, dst, iccBuf,
569
0
                                 (unsigned int)this->decompICCSize);
570
0
    }
571
0
  } else {
572
#if BITS_IN_JSAMPLE == 8
573
0
    if (this->precision >= 2 && this->precision <= BITS_IN_JSAMPLE)
574
#else
575
0
    if (this->precision >= BITS_IN_JSAMPLE - 3 &&
576
0
        this->precision <= BITS_IN_JSAMPLE)
577
0
#endif
578
0
      dinfo->data_precision = this->precision;
579
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
580
0
      THROW("Could not initialize PPM writer");
581
0
    invert = this->bottomUp;
582
0
  }
583
584
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
585
586
0
  dst->output_file = file;
587
0
  (*dst->start_output) (dinfo, dst);
588
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
589
590
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
591
0
  else if (pitch < width * tjPixelSize[pixelFormat])
592
0
    THROW("Invalid argument");
593
594
0
  while (dinfo->output_scanline < dinfo->output_height) {
595
0
    _JSAMPLE *rowptr;
596
597
0
    if (invert)
598
0
      rowptr =
599
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
600
0
    else
601
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
602
0
    memcpy(dst->_buffer[0], rowptr,
603
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
604
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
605
0
    dinfo->output_scanline++;
606
0
  }
607
608
0
  (*dst->finish_output) (dinfo, dst);
609
610
0
bailout:
611
0
  tj3Destroy(handle2);
612
0
  if (file) fclose(file);
613
0
  return retval;
614
615
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
616
617
  GET_TJINSTANCE(handle, -1)
618
  THROW("16-bit data precision requires lossless JPEG,\n"
619
        "which was disabled at build time.")
620
bailout:
621
  return retval;
622
623
#endif
624
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
625
626
627
#undef _JSAMPLE
628
#undef _JSAMPROW
629
#undef _buffer
630
#undef _jinit_read_png
631
#undef _jinit_write_png
632
#undef _jinit_read_ppm
633
#undef _jinit_write_ppm
634
#undef _jpeg_crop_scanline
635
#undef _jpeg_read_scanlines
636
#undef _jpeg_skip_scanlines
637
#undef _jpeg_write_scanlines