Coverage Report

Created: 2026-05-11 06:56

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