Coverage Report

Created: 2026-01-25 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/turbojpeg-mp.c
Line
Count
Source
1
/*
2
 * Copyright (C)2009-2026 D. R. Commander.  All Rights Reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or without
5
 * modification, are permitted provided that the following conditions are met:
6
 *
7
 * - Redistributions of source code must retain the above copyright notice,
8
 *   this list of conditions and the following disclaimer.
9
 * - Redistributions in binary form must reproduce the above copyright notice,
10
 *   this list of conditions and the following disclaimer in the documentation
11
 *   and/or other materials provided with the distribution.
12
 * - Neither the name of the libjpeg-turbo Project nor the names of its
13
 *   contributors may be used to endorse or promote products derived from this
14
 *   software without specific prior written permission.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
 * POSSIBILITY OF SUCH DAMAGE.
27
 */
28
29
/* TurboJPEG API functions that must be compiled for multiple data
30
   precisions */
31
32
#if BITS_IN_JSAMPLE == 8
33
0
#define _JSAMPLE  JSAMPLE
34
0
#define _JSAMPROW  JSAMPROW
35
0
#define _buffer  buffer
36
0
#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
0
#define _jpeg_write_scanlines  jpeg_write_scanlines
42
#elif BITS_IN_JSAMPLE == 12
43
55.3M
#define _JSAMPLE  J12SAMPLE
44
13.7k
#define _JSAMPROW  J12SAMPROW
45
55.3M
#define _buffer  buffer12
46
20.2k
#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
13.6k
#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
34.0k
#define _GET_STRING(name, suffix)  #name #suffix
65
34.0k
#define GET_STRING(name, suffix)  _GET_STRING(name, suffix)
66
67
68
/******************************** Compressor *********************************/
69
70
/* TurboJPEG 3+ */
71
DLLEXPORT int GET_NAME(tj3Compress, BITS_IN_JSAMPLE)
72
  (tjhandle handle, const _JSAMPLE *srcBuf, int width, int pitch, int height,
73
   int pixelFormat, unsigned char **jpegBuf, size_t *jpegSize)
74
13.7k
{
75
13.7k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
13.7k
  int i, retval = 0;
77
13.7k
  boolean alloc = TRUE;
78
13.7k
  _JSAMPROW *row_pointer = NULL;
79
80
13.7k
  GET_CINSTANCE(handle)
81
13.7k
  if ((this->init & COMPRESS) == 0)
82
13.7k
    THROW("Instance has not been initialized for compression");
83
84
13.7k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
13.7k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
13.7k
      jpegSize == NULL)
87
13.7k
    THROW("Invalid argument");
88
89
13.7k
  if (!this->lossless && this->quality == -1)
90
13.7k
    THROW("TJPARAM_QUALITY must be specified");
91
13.7k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
13.7k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
13.7k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
13.7k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
13.7k
    THROW("Memory allocation failure");
98
99
13.7k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
48
    retval = -1;  goto bailout;
102
48
  }
103
104
13.6k
  cinfo->image_width = width;
105
13.6k
  cinfo->image_height = height;
106
13.6k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
13.6k
  setCompDefaults(this, pixelFormat, FALSE);
109
13.6k
  if (this->noRealloc) {
110
11.7k
    alloc = FALSE;
111
11.7k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
11.7k
  }
113
13.6k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
13.6k
  jpeg_start_compress(cinfo, TRUE);
116
53.9M
  for (i = 0; i < height; i++) {
117
53.9M
    if (this->bottomUp)
118
7.71M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
46.2M
    else
120
46.2M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
53.9M
  }
122
27.3k
  while (cinfo->next_scanline < cinfo->image_height)
123
13.6k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
13.6k
                          cinfo->image_height - cinfo->next_scanline);
125
13.6k
  jpeg_finish_compress(cinfo);
126
127
13.7k
bailout:
128
13.7k
  if (cinfo->global_state > CSTATE_START && alloc)
129
0
    (*cinfo->dest->term_destination) (cinfo);
130
13.7k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
48
    jpeg_abort_compress(cinfo);
132
13.7k
  free(row_pointer);
133
13.7k
  if (this->jerr.warning) retval = -1;
134
13.7k
  return retval;
135
13.6k
}
Unexecuted instantiation: tj3Compress8
tj3Compress12
Line
Count
Source
74
13.7k
{
75
13.7k
  static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
76
13.7k
  int i, retval = 0;
77
13.7k
  boolean alloc = TRUE;
78
13.7k
  _JSAMPROW *row_pointer = NULL;
79
80
13.7k
  GET_CINSTANCE(handle)
81
13.7k
  if ((this->init & COMPRESS) == 0)
82
13.7k
    THROW("Instance has not been initialized for compression");
83
84
13.7k
  if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85
13.7k
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
86
13.7k
      jpegSize == NULL)
87
13.7k
    THROW("Invalid argument");
88
89
13.7k
  if (!this->lossless && this->quality == -1)
90
13.7k
    THROW("TJPARAM_QUALITY must be specified");
91
13.7k
  if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92
13.7k
    THROW("TJPARAM_SUBSAMP must be specified");
93
94
13.7k
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
95
96
13.7k
  if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97
13.7k
    THROW("Memory allocation failure");
98
99
13.7k
  if (setjmp(this->jerr.setjmp_buffer)) {
100
    /* If we get here, the JPEG code has signaled an error. */
101
48
    retval = -1;  goto bailout;
102
48
  }
103
104
13.6k
  cinfo->image_width = width;
105
13.6k
  cinfo->image_height = height;
106
13.6k
  cinfo->data_precision = BITS_IN_JSAMPLE;
107
108
13.6k
  setCompDefaults(this, pixelFormat, FALSE);
109
13.6k
  if (this->noRealloc) {
110
11.7k
    alloc = FALSE;
111
11.7k
    *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
112
11.7k
  }
113
13.6k
  jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
114
115
13.6k
  jpeg_start_compress(cinfo, TRUE);
116
53.9M
  for (i = 0; i < height; i++) {
117
53.9M
    if (this->bottomUp)
118
7.71M
      row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
119
46.2M
    else
120
46.2M
      row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
121
53.9M
  }
122
27.3k
  while (cinfo->next_scanline < cinfo->image_height)
123
13.6k
    _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124
13.6k
                          cinfo->image_height - cinfo->next_scanline);
125
13.6k
  jpeg_finish_compress(cinfo);
126
127
13.7k
bailout:
128
13.7k
  if (cinfo->global_state > CSTATE_START && alloc)
129
0
    (*cinfo->dest->term_destination) (cinfo);
130
13.7k
  if (cinfo->global_state > CSTATE_START || retval == -1)
131
48
    jpeg_abort_compress(cinfo);
132
13.7k
  free(row_pointer);
133
13.7k
  if (this->jerr.warning) retval = -1;
134
13.7k
  return retval;
135
13.6k
}
Unexecuted instantiation: tj3Compress16
136
137
138
/******************************* Decompressor ********************************/
139
140
/* TurboJPEG 3+ */
141
DLLEXPORT int GET_NAME(tj3Decompress, BITS_IN_JSAMPLE)
142
  (tjhandle handle, const unsigned char *jpegBuf, size_t jpegSize,
143
   _JSAMPLE *dstBuf, int pitch, int pixelFormat)
144
0
{
145
0
  static const char FUNCTION_NAME[] =
146
0
    GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147
0
  _JSAMPROW *row_pointer = NULL;
148
0
  int croppedHeight, i, retval = 0;
149
#if BITS_IN_JSAMPLE != 16
150
  int scaledWidth;
151
#endif
152
0
  struct my_progress_mgr progress;
153
154
0
  GET_DINSTANCE(handle);
155
0
  if ((this->init & DECOMPRESS) == 0)
156
0
    THROW("Instance has not been initialized for decompression");
157
158
0
  if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160
0
    THROW("Invalid argument");
161
162
0
  if (this->scanLimit) {
163
0
    memset(&progress, 0, sizeof(struct my_progress_mgr));
164
0
    progress.pub.progress_monitor = my_progress_monitor;
165
0
    progress.this = this;
166
0
    dinfo->progress = &progress.pub;
167
0
  } else
168
0
    dinfo->progress = NULL;
169
170
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
171
172
0
  if (setjmp(this->jerr.setjmp_buffer)) {
173
    /* If we get here, the JPEG code has signaled an error. */
174
0
    retval = -1;  goto bailout;
175
0
  }
176
177
0
  if (dinfo->global_state <= DSTATE_INHEADER) {
178
0
    jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
179
0
    jpeg_read_header(dinfo, TRUE);
180
0
  }
181
0
  setDecompParameters(this);
182
0
  if (this->maxPixels &&
183
0
      (unsigned long long)this->jpegWidth * this->jpegHeight >
184
0
      (unsigned long long)this->maxPixels)
185
0
    THROW("Image is too large");
186
0
  this->dinfo.out_color_space = pf2cs[pixelFormat];
187
#if BITS_IN_JSAMPLE != 16
188
0
  scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
189
#endif
190
0
  dinfo->do_fancy_upsampling = !this->fastUpsample;
191
0
  this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
192
193
0
  dinfo->scale_num = this->scalingFactor.num;
194
0
  dinfo->scale_denom = this->scalingFactor.denom;
195
196
0
  jpeg_start_decompress(dinfo);
197
198
#if BITS_IN_JSAMPLE != 16
199
0
  if (this->croppingRegion.x != 0 ||
200
0
      (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
201
0
    JDIMENSION crop_x = this->croppingRegion.x;
202
0
    JDIMENSION crop_w = this->croppingRegion.w;
203
204
0
    _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
205
0
    if ((int)crop_x != this->croppingRegion.x)
206
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
207
0
             "actual (%d) cropping region left boundary",
208
0
             this->croppingRegion.x, (int)crop_x);
209
0
    if ((int)crop_w != this->croppingRegion.w)
210
0
      THROWI("Unexplained mismatch between specified (%d) and\n"
211
0
             "actual (%d) cropping region width",
212
0
             this->croppingRegion.w, (int)crop_w);
213
0
  }
214
0
#endif
215
216
0
  if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
217
218
0
  croppedHeight = dinfo->output_height;
219
#if BITS_IN_JSAMPLE != 16
220
0
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
221
0
    croppedHeight = this->croppingRegion.h;
222
#endif
223
0
  if ((row_pointer =
224
0
       (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
225
0
    THROW("Memory allocation failure");
226
0
  if (setjmp(this->jerr.setjmp_buffer)) {
227
    /* If we get here, the JPEG code has signaled an error. */
228
0
    retval = -1;  goto bailout;
229
0
  }
230
0
  for (i = 0; i < (int)croppedHeight; i++) {
231
0
    if (this->bottomUp)
232
0
      row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
233
0
    else
234
0
      row_pointer[i] = &dstBuf[i * (size_t)pitch];
235
0
  }
236
237
#if BITS_IN_JSAMPLE != 16
238
0
  if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
239
0
    if (this->croppingRegion.y != 0) {
240
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
241
242
0
      if ((int)lines != this->croppingRegion.y)
243
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
244
               "actual (%d) cropping region upper boundary",
245
0
               this->croppingRegion.y, (int)lines);
246
0
    }
247
0
    while ((int)dinfo->output_scanline <
248
0
           this->croppingRegion.y + this->croppingRegion.h)
249
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
250
0
                                               this->croppingRegion.y],
251
0
                           this->croppingRegion.y + this->croppingRegion.h -
252
0
                           dinfo->output_scanline);
253
0
    if (this->croppingRegion.y + this->croppingRegion.h !=
254
0
        (int)dinfo->output_height) {
255
0
      JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
256
                                                     this->croppingRegion.y -
257
                                                     this->croppingRegion.h);
258
259
0
      if (lines != dinfo->output_height - this->croppingRegion.y -
260
0
                   this->croppingRegion.h)
261
0
        THROWI("Unexplained mismatch between specified (%d) and\n"
262
0
               "actual (%d) cropping region lower boundary",
263
0
               this->croppingRegion.y + this->croppingRegion.h,
264
0
               (int)(dinfo->output_height - lines));
265
0
    }
266
0
  } else
267
0
#endif
268
0
  {
269
0
    while (dinfo->output_scanline < dinfo->output_height)
270
0
      _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
271
0
                           dinfo->output_height - dinfo->output_scanline);
272
0
  }
273
0
  jpeg_finish_decompress(dinfo);
274
275
0
bailout:
276
0
  if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
277
0
  free(row_pointer);
278
0
  if (this->jerr.warning) retval = -1;
279
0
  return retval;
280
0
}
Unexecuted instantiation: tj3Decompress8
Unexecuted instantiation: tj3Decompress12
Unexecuted instantiation: tj3Decompress16
281
282
283
/*************************** Packed-Pixel Image I/O **************************/
284
285
/* TurboJPEG 3+ */
286
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
287
static
288
#endif
289
_JSAMPLE *GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
290
  (tjhandle handle, FILE *file, int *width, int align, int *height,
291
   int *pixelFormat)
292
20.3k
{
293
20.3k
  static const char FUNCTION_NAME[] =
294
20.3k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
295
296
20.3k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
297
298
20.3k
  int retval = 0, tempc;
299
20.3k
  size_t pitch;
300
20.3k
  tjhandle handle2 = NULL;
301
20.3k
  tjinstance *this2;
302
20.3k
  j_compress_ptr cinfo = NULL;
303
20.3k
  cjpeg_source_ptr src;
304
20.3k
  _JSAMPLE *dstBuf = NULL;
305
20.3k
  boolean invert;
306
307
20.3k
  GET_TJINSTANCE(handle, NULL)
308
309
20.3k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
310
20.3k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
311
20.3k
    THROW("Invalid argument");
312
20.3k
  if ((align & (align - 1)) != 0)
313
20.3k
    THROW("Alignment must be a power of 2");
314
315
  /* The instance handle passed to this function is used only for parameter
316
     retrieval.  Create a new temporary instance to avoid interfering with the
317
     libjpeg state of the primary instance. */
318
20.3k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
319
20.3k
  this2 = (tjinstance *)handle2;
320
20.3k
  cinfo = &this2->cinfo;
321
322
20.3k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
323
0
    THROW_UNIX("Could not read input file")
324
20.3k
  else if (tempc == EOF)
325
20.3k
    THROW("Input file contains no data");
326
327
20.3k
  if (setjmp(this2->jerr.setjmp_buffer)) {
328
    /* If we get here, the JPEG code has signaled an error. */
329
2.14k
    retval = -1;  goto bailout;
330
2.14k
  }
331
332
18.1k
  cinfo->data_precision = BITS_IN_JSAMPLE;
333
18.1k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
334
18.1k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
335
18.1k
  if (tempc == 'B') {
336
7
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
337
7
      THROW("Could not initialize bitmap loader");
338
7
    invert = !this->bottomUp;
339
20.2k
  } else if (tempc == 'P') {
340
20.2k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
341
20.2k
      THROW("Could not initialize PPM loader");
342
20.2k
    invert = this->bottomUp;
343
20.2k
  } else
344
18.4E
    THROW("Unsupported file type");
345
346
20.2k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
347
348
20.2k
  src->input_file = file;
349
  /* Refuse to load images larger than the specified size. */
350
20.2k
  src->max_pixels = this->maxPixels;
351
20.2k
  (*src->start_input) (cinfo, src);
352
20.2k
  if (tempc == 'B') {
353
0
    if (cinfo->X_density && cinfo->Y_density) {
354
0
      this->xDensity = cinfo->X_density;
355
0
      this->yDensity = cinfo->Y_density;
356
0
      this->densityUnits = cinfo->density_unit;
357
0
    }
358
0
  }
359
20.2k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
360
361
20.2k
  *width = cinfo->image_width;  *height = cinfo->image_height;
362
20.2k
  *pixelFormat = cs2pf[cinfo->in_color_space];
363
364
20.2k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
365
20.2k
  if (
366
#if ULLONG_MAX > SIZE_MAX
367
      (unsigned long long)pitch * (unsigned long long)(*height) >
368
      (unsigned long long)((size_t)-1) ||
369
#endif
370
20.2k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
371
20.2k
                                   sizeof(_JSAMPLE))) == NULL)
372
20.2k
    THROW("Memory allocation failure");
373
374
20.2k
  if (setjmp(this2->jerr.setjmp_buffer)) {
375
    /* If we get here, the JPEG code has signaled an error. */
376
4.38k
    retval = -1;  goto bailout;
377
4.38k
  }
378
379
55.3M
  while (cinfo->next_scanline < cinfo->image_height) {
380
55.3M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
381
382
110M
    for (i = 0; i < nlines; i++) {
383
55.3M
      _JSAMPLE *dstptr;
384
55.3M
      int row;
385
386
55.3M
      row = cinfo->next_scanline + i;
387
55.3M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
388
47.4M
      else dstptr = &dstBuf[row * pitch];
389
55.3M
      memcpy(dstptr, src->_buffer[i],
390
55.3M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
391
55.3M
    }
392
55.3M
    cinfo->next_scanline += nlines;
393
55.3M
  }
394
395
15.8k
  (*src->finish_input) (cinfo, src);
396
397
20.3k
bailout:
398
20.3k
  tj3Destroy(handle2);
399
20.3k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
400
20.3k
  return dstBuf;
401
402
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
403
404
  static const char ERROR_MSG[] =
405
    "16-bit data precision requires lossless JPEG,\n"
406
    "which was disabled at build time.";
407
  _JSAMPLE *retval = NULL;
408
409
  GET_TJINSTANCE(handle, NULL)
410
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
411
           ERROR_MSG);
412
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
413
414
bailout:
415
  return retval;
416
417
#endif
418
15.8k
}
Unexecuted instantiation: _tj3LoadImageFromFileHandle8
_tj3LoadImageFromFileHandle12
Line
Count
Source
292
20.3k
{
293
20.3k
  static const char FUNCTION_NAME[] =
294
20.3k
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
295
296
20.3k
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
297
298
20.3k
  int retval = 0, tempc;
299
20.3k
  size_t pitch;
300
20.3k
  tjhandle handle2 = NULL;
301
20.3k
  tjinstance *this2;
302
20.3k
  j_compress_ptr cinfo = NULL;
303
20.3k
  cjpeg_source_ptr src;
304
20.3k
  _JSAMPLE *dstBuf = NULL;
305
20.3k
  boolean invert;
306
307
20.3k
  GET_TJINSTANCE(handle, NULL)
308
309
20.3k
  if (!file || !width || align < 1 || !height || !pixelFormat ||
310
20.3k
      *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
311
20.3k
    THROW("Invalid argument");
312
20.3k
  if ((align & (align - 1)) != 0)
313
20.3k
    THROW("Alignment must be a power of 2");
314
315
  /* The instance handle passed to this function is used only for parameter
316
     retrieval.  Create a new temporary instance to avoid interfering with the
317
     libjpeg state of the primary instance. */
318
20.3k
  if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
319
20.3k
  this2 = (tjinstance *)handle2;
320
20.3k
  cinfo = &this2->cinfo;
321
322
20.3k
  if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
323
0
    THROW_UNIX("Could not read input file")
324
20.3k
  else if (tempc == EOF)
325
20.3k
    THROW("Input file contains no data");
326
327
20.3k
  if (setjmp(this2->jerr.setjmp_buffer)) {
328
    /* If we get here, the JPEG code has signaled an error. */
329
2.14k
    retval = -1;  goto bailout;
330
2.14k
  }
331
332
18.1k
  cinfo->data_precision = BITS_IN_JSAMPLE;
333
18.1k
  if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
334
18.1k
  else cinfo->in_color_space = pf2cs[*pixelFormat];
335
18.1k
  if (tempc == 'B') {
336
7
    if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
337
7
      THROW("Could not initialize bitmap loader");
338
7
    invert = !this->bottomUp;
339
20.2k
  } else if (tempc == 'P') {
340
20.2k
    if ((src = _jinit_read_ppm(cinfo)) == NULL)
341
20.2k
      THROW("Could not initialize PPM loader");
342
20.2k
    invert = this->bottomUp;
343
20.2k
  } else
344
18.4E
    THROW("Unsupported file type");
345
346
20.2k
  cinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
347
348
20.2k
  src->input_file = file;
349
  /* Refuse to load images larger than the specified size. */
350
20.2k
  src->max_pixels = this->maxPixels;
351
20.2k
  (*src->start_input) (cinfo, src);
352
20.2k
  if (tempc == 'B') {
353
0
    if (cinfo->X_density && cinfo->Y_density) {
354
0
      this->xDensity = cinfo->X_density;
355
0
      this->yDensity = cinfo->Y_density;
356
0
      this->densityUnits = cinfo->density_unit;
357
0
    }
358
0
  }
359
20.2k
  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
360
361
20.2k
  *width = cinfo->image_width;  *height = cinfo->image_height;
362
20.2k
  *pixelFormat = cs2pf[cinfo->in_color_space];
363
364
20.2k
  pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
365
20.2k
  if (
366
#if ULLONG_MAX > SIZE_MAX
367
      (unsigned long long)pitch * (unsigned long long)(*height) >
368
      (unsigned long long)((size_t)-1) ||
369
#endif
370
20.2k
      (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
371
20.2k
                                   sizeof(_JSAMPLE))) == NULL)
372
20.2k
    THROW("Memory allocation failure");
373
374
20.2k
  if (setjmp(this2->jerr.setjmp_buffer)) {
375
    /* If we get here, the JPEG code has signaled an error. */
376
4.38k
    retval = -1;  goto bailout;
377
4.38k
  }
378
379
55.3M
  while (cinfo->next_scanline < cinfo->image_height) {
380
55.3M
    int i, nlines = (*src->get_pixel_rows) (cinfo, src);
381
382
110M
    for (i = 0; i < nlines; i++) {
383
55.3M
      _JSAMPLE *dstptr;
384
55.3M
      int row;
385
386
55.3M
      row = cinfo->next_scanline + i;
387
55.3M
      if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
388
47.4M
      else dstptr = &dstBuf[row * pitch];
389
55.3M
      memcpy(dstptr, src->_buffer[i],
390
55.3M
             (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
391
55.3M
    }
392
55.3M
    cinfo->next_scanline += nlines;
393
55.3M
  }
394
395
15.8k
  (*src->finish_input) (cinfo, src);
396
397
20.3k
bailout:
398
20.3k
  tj3Destroy(handle2);
399
20.3k
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
400
20.3k
  return dstBuf;
401
402
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
403
404
  static const char ERROR_MSG[] =
405
    "16-bit data precision requires lossless JPEG,\n"
406
    "which was disabled at build time.";
407
  _JSAMPLE *retval = NULL;
408
409
  GET_TJINSTANCE(handle, NULL)
410
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
411
           ERROR_MSG);
412
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
413
414
bailout:
415
  return retval;
416
417
#endif
418
15.8k
}
Unexecuted instantiation: _tj3LoadImageFromFileHandle16
419
420
DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
421
  (tjhandle handle, const char *filename, int *width, int align, int *height,
422
   int *pixelFormat)
423
0
{
424
0
  static const char FUNCTION_NAME[] =
425
0
    GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
426
427
0
#if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
428
429
0
  int retval = 0;
430
0
  _JSAMPLE *dstBuf = NULL;
431
0
  FILE *file = NULL;
432
433
0
  GET_TJINSTANCE(handle, NULL)
434
435
0
  if (!filename)
436
0
    THROW("Invalid argument");
437
438
#ifdef _MSC_VER
439
  if (fopen_s(&file, filename, "rb") || file == NULL)
440
#else
441
0
  if ((file = fopen(filename, "rb")) == NULL)
442
0
#endif
443
0
    THROW_UNIX("Cannot open input file");
444
445
0
  dstBuf = GET_NAME(_tj3LoadImageFromFileHandle, BITS_IN_JSAMPLE)
446
0
             (handle, file, width, align, height, pixelFormat);
447
448
0
bailout:
449
0
  if (file) fclose(file);
450
0
  if (retval < 0) { free(dstBuf);  dstBuf = NULL; }
451
0
  return dstBuf;
452
453
#else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
454
455
  static const char ERROR_MSG[] =
456
    "16-bit data precision requires lossless JPEG,\n"
457
    "which was disabled at build time.";
458
  _JSAMPLE *retval = NULL;
459
460
  GET_TJINSTANCE(handle, NULL)
461
  SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
462
           ERROR_MSG);
463
  this->isInstanceError = TRUE;  THROWG(ERROR_MSG, NULL)
464
465
bailout:
466
  return retval;
467
468
#endif
469
0
}
Unexecuted instantiation: tj3LoadImage8
Unexecuted instantiation: tj3LoadImage12
Unexecuted instantiation: tj3LoadImage16
470
471
472
/* TurboJPEG 3+ */
473
DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
474
  (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
475
   int pitch, int height, int pixelFormat)
476
0
{
477
0
  static const char FUNCTION_NAME[] =
478
0
    GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
479
0
  int retval = 0;
480
481
0
#if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
482
483
0
  tjhandle handle2 = NULL;
484
0
  tjinstance *this2;
485
0
  j_decompress_ptr dinfo = NULL;
486
0
  djpeg_dest_ptr dst;
487
0
  FILE *file = NULL;
488
0
  char *ptr = NULL;
489
0
  boolean invert;
490
491
0
  GET_TJINSTANCE(handle, -1)
492
493
0
  if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
494
0
      pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
495
0
    THROW("Invalid argument");
496
497
  /* The instance handle passed to this function is used only for parameter
498
     retrieval.  Create a new temporary instance to avoid interfering with the
499
     libjpeg state of the primary instance. */
500
0
  if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
501
0
    return -1;
502
0
  this2 = (tjinstance *)handle2;
503
0
  dinfo = &this2->dinfo;
504
505
#ifdef _MSC_VER
506
  if (fopen_s(&file, filename, "wb") || file == NULL)
507
#else
508
0
  if ((file = fopen(filename, "wb")) == NULL)
509
0
#endif
510
0
    THROW_UNIX("Cannot open output file");
511
512
0
  if (setjmp(this2->jerr.setjmp_buffer)) {
513
    /* If we get here, the JPEG code has signaled an error. */
514
0
    retval = -1;  goto bailout;
515
0
  }
516
517
0
  this2->dinfo.out_color_space = pf2cs[pixelFormat];
518
0
  dinfo->image_width = width;  dinfo->image_height = height;
519
0
  dinfo->global_state = DSTATE_READY;
520
0
  dinfo->scale_num = dinfo->scale_denom = 1;
521
0
  dinfo->data_precision = BITS_IN_JSAMPLE;
522
523
0
  ptr = strrchr(filename, '.');
524
0
  if (ptr && !strcasecmp(ptr, ".bmp")) {
525
0
    if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
526
0
      THROW("Could not initialize bitmap writer");
527
0
    invert = !this->bottomUp;
528
0
    dinfo->X_density = (UINT16)this->xDensity;
529
0
    dinfo->Y_density = (UINT16)this->yDensity;
530
0
    dinfo->density_unit = (UINT8)this->densityUnits;
531
0
  } else {
532
0
    if ((dst = _jinit_write_ppm(dinfo)) == NULL)
533
0
      THROW("Could not initialize PPM writer");
534
0
    invert = this->bottomUp;
535
0
  }
536
537
0
  dinfo->mem->max_memory_to_use = (long)this->maxMemory * 1048576L;
538
539
0
  dst->output_file = file;
540
0
  (*dst->start_output) (dinfo, dst);
541
0
  (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
542
543
0
  if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
544
545
0
  while (dinfo->output_scanline < dinfo->output_height) {
546
0
    _JSAMPLE *rowptr;
547
548
0
    if (invert)
549
0
      rowptr =
550
0
        (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
551
0
    else
552
0
      rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
553
0
    memcpy(dst->_buffer[0], rowptr,
554
0
           width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
555
0
    (*dst->put_pixel_rows) (dinfo, dst, 1);
556
0
    dinfo->output_scanline++;
557
0
  }
558
559
0
  (*dst->finish_output) (dinfo, dst);
560
561
0
bailout:
562
0
  tj3Destroy(handle2);
563
0
  if (file) fclose(file);
564
0
  return retval;
565
566
#else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
567
568
  GET_TJINSTANCE(handle, -1)
569
  THROW("16-bit data precision requires lossless JPEG,\n"
570
        "which was disabled at build time.")
571
bailout:
572
  return retval;
573
574
#endif
575
0
}
Unexecuted instantiation: tj3SaveImage8
Unexecuted instantiation: tj3SaveImage12
Unexecuted instantiation: tj3SaveImage16
576
577
578
#undef _JSAMPLE
579
#undef _JSAMPROW
580
#undef _buffer
581
#undef _jinit_read_ppm
582
#undef _jinit_write_ppm
583
#undef _jpeg_crop_scanline
584
#undef _jpeg_read_scanlines
585
#undef _jpeg_skip_scanlines
586
#undef _jpeg_write_scanlines