Coverage Report

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