Coverage Report

Created: 2025-07-01 06:26

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