2 * Copyright (C)2009-2023 D. R. Commander. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
29 /* TurboJPEG API functions that must be compiled for multiple data
32 #if BITS_IN_JSAMPLE == 8
33 #define _JSAMPLE JSAMPLE
34 #define _JSAMPROW JSAMPROW
35 #define _buffer buffer
36 #define _jinit_read_ppm jinit_read_ppm
37 #define _jinit_write_ppm jinit_write_ppm
38 #define _jpeg_crop_scanline jpeg_crop_scanline
39 #define _jpeg_read_scanlines jpeg_read_scanlines
40 #define _jpeg_skip_scanlines jpeg_skip_scanlines
41 #define _jpeg_write_scanlines jpeg_write_scanlines
42 #elif BITS_IN_JSAMPLE == 12
43 #define _JSAMPLE J12SAMPLE
44 #define _JSAMPROW J12SAMPROW
45 #define _buffer buffer12
46 #define _jinit_read_ppm j12init_read_ppm
47 #define _jinit_write_ppm j12init_write_ppm
48 #define _jpeg_crop_scanline jpeg12_crop_scanline
49 #define _jpeg_read_scanlines jpeg12_read_scanlines
50 #define _jpeg_skip_scanlines jpeg12_skip_scanlines
51 #define _jpeg_write_scanlines jpeg12_write_scanlines
52 #elif BITS_IN_JSAMPLE == 16
53 #define _JSAMPLE J16SAMPLE
54 #define _JSAMPROW J16SAMPROW
55 #define _buffer buffer16
56 #define _jinit_read_ppm j16init_read_ppm
57 #define _jinit_write_ppm j16init_write_ppm
58 #define _jpeg_read_scanlines jpeg16_read_scanlines
59 #define _jpeg_write_scanlines jpeg16_write_scanlines
62 #define _GET_NAME(name, suffix) name##suffix
63 #define GET_NAME(name, suffix) _GET_NAME(name, suffix)
64 #define _GET_STRING(name, suffix) #name #suffix
65 #define GET_STRING(name, suffix) _GET_STRING(name, suffix)
68 /******************************** Compressor *********************************/
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)
75 static const char FUNCTION_NAME[] = GET_STRING(tj3Compress, BITS_IN_JSAMPLE);
78 _JSAMPROW *row_pointer = NULL;
81 if ((this->init & COMPRESS) == 0)
82 THROW("Instance has not been initialized for compression");
84 if (srcBuf == NULL || width <= 0 || pitch < 0 || height <= 0 ||
85 pixelFormat < 0 || pixelFormat >= TJ_NUMPF || jpegBuf == NULL ||
87 THROW("Invalid argument");
89 if (!this->lossless && this->quality == -1)
90 THROW("TJPARAM_QUALITY must be specified");
91 if (!this->lossless && this->subsamp == TJSAMP_UNKNOWN)
92 THROW("TJPARAM_SUBSAMP must be specified");
94 if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
96 if ((row_pointer = (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * height)) == NULL)
97 THROW("Memory allocation failure");
99 if (setjmp(this->jerr.setjmp_buffer)) {
100 /* If we get here, the JPEG code has signaled an error. */
101 retval = -1; goto bailout;
104 cinfo->image_width = width;
105 cinfo->image_height = height;
106 cinfo->data_precision = BITS_IN_JSAMPLE;
108 setCompDefaults(this, pixelFormat);
109 if (this->noRealloc) {
111 *jpegSize = tj3JPEGBufSize(width, height, this->subsamp);
113 jpeg_mem_dest_tj(cinfo, jpegBuf, jpegSize, alloc);
115 jpeg_start_compress(cinfo, TRUE);
116 for (i = 0; i < height; i++) {
118 row_pointer[i] = (_JSAMPROW)&srcBuf[(height - i - 1) * (size_t)pitch];
120 row_pointer[i] = (_JSAMPROW)&srcBuf[i * (size_t)pitch];
122 while (cinfo->next_scanline < cinfo->image_height)
123 _jpeg_write_scanlines(cinfo, &row_pointer[cinfo->next_scanline],
124 cinfo->image_height - cinfo->next_scanline);
125 jpeg_finish_compress(cinfo);
128 if (cinfo->global_state > CSTATE_START) {
129 if (alloc) (*cinfo->dest->term_destination) (cinfo);
130 jpeg_abort_compress(cinfo);
133 if (this->jerr.warning) retval = -1;
138 /******************************* Decompressor ********************************/
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)
145 static const char FUNCTION_NAME[] =
146 GET_STRING(tj3Decompress, BITS_IN_JSAMPLE);
147 _JSAMPROW *row_pointer = NULL;
148 int croppedHeight, i, retval = 0;
149 #if BITS_IN_JSAMPLE != 16
152 struct my_progress_mgr progress;
154 GET_DINSTANCE(handle);
155 if ((this->init & DECOMPRESS) == 0)
156 THROW("Instance has not been initialized for decompression");
158 if (jpegBuf == NULL || jpegSize <= 0 || dstBuf == NULL || pitch < 0 ||
159 pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
160 THROW("Invalid argument");
162 if (this->scanLimit) {
163 memset(&progress, 0, sizeof(struct my_progress_mgr));
164 progress.pub.progress_monitor = my_progress_monitor;
165 progress.this = this;
166 dinfo->progress = &progress.pub;
168 dinfo->progress = NULL;
170 if (setjmp(this->jerr.setjmp_buffer)) {
171 /* If we get here, the JPEG code has signaled an error. */
172 retval = -1; goto bailout;
175 if (dinfo->global_state <= DSTATE_START) {
176 jpeg_mem_src_tj(dinfo, jpegBuf, jpegSize);
177 jpeg_read_header(dinfo, TRUE);
179 setDecompParameters(this);
180 this->dinfo.out_color_space = pf2cs[pixelFormat];
181 #if BITS_IN_JSAMPLE != 16
182 scaledWidth = TJSCALED(dinfo->image_width, this->scalingFactor);
184 dinfo->do_fancy_upsampling = !this->fastUpsample;
185 this->dinfo.dct_method = this->fastDCT ? JDCT_FASTEST : JDCT_ISLOW;
187 dinfo->scale_num = this->scalingFactor.num;
188 dinfo->scale_denom = this->scalingFactor.denom;
190 jpeg_start_decompress(dinfo);
192 #if BITS_IN_JSAMPLE != 16
193 if (this->croppingRegion.x != 0 ||
194 (this->croppingRegion.w != 0 && this->croppingRegion.w != scaledWidth)) {
195 JDIMENSION crop_x = this->croppingRegion.x;
196 JDIMENSION crop_w = this->croppingRegion.w;
198 _jpeg_crop_scanline(dinfo, &crop_x, &crop_w);
199 if ((int)crop_x != this->croppingRegion.x)
200 THROWI("Unexplained mismatch between specified (%d) and\n"
201 "actual (%d) cropping region left boundary",
202 this->croppingRegion.x, (int)crop_x);
203 if ((int)crop_w != this->croppingRegion.w)
204 THROWI("Unexplained mismatch between specified (%d) and\n"
205 "actual (%d) cropping region width",
206 this->croppingRegion.w, (int)crop_w);
210 if (pitch == 0) pitch = dinfo->output_width * tjPixelSize[pixelFormat];
212 croppedHeight = dinfo->output_height;
213 #if BITS_IN_JSAMPLE != 16
214 if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0)
215 croppedHeight = this->croppingRegion.h;
218 (_JSAMPROW *)malloc(sizeof(_JSAMPROW) * croppedHeight)) == NULL)
219 THROW("Memory allocation failure");
220 if (setjmp(this->jerr.setjmp_buffer)) {
221 /* If we get here, the JPEG code has signaled an error. */
222 retval = -1; goto bailout;
224 for (i = 0; i < (int)croppedHeight; i++) {
226 row_pointer[i] = &dstBuf[(croppedHeight - i - 1) * (size_t)pitch];
228 row_pointer[i] = &dstBuf[i * (size_t)pitch];
231 #if BITS_IN_JSAMPLE != 16
232 if (this->croppingRegion.y != 0 || this->croppingRegion.h != 0) {
233 if (this->croppingRegion.y != 0) {
234 JDIMENSION lines = _jpeg_skip_scanlines(dinfo, this->croppingRegion.y);
236 if ((int)lines != this->croppingRegion.y)
237 THROWI("Unexplained mismatch between specified (%d) and\n"
238 "actual (%d) cropping region upper boundary",
239 this->croppingRegion.y, (int)lines);
241 while ((int)dinfo->output_scanline <
242 this->croppingRegion.y + this->croppingRegion.h)
243 _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline -
244 this->croppingRegion.y],
245 this->croppingRegion.y + this->croppingRegion.h -
246 dinfo->output_scanline);
247 if (this->croppingRegion.y + this->croppingRegion.h !=
248 (int)dinfo->output_height) {
249 JDIMENSION lines = _jpeg_skip_scanlines(dinfo, dinfo->output_height -
250 this->croppingRegion.y -
251 this->croppingRegion.h);
253 if (lines != dinfo->output_height - this->croppingRegion.y -
254 this->croppingRegion.h)
255 THROWI("Unexplained mismatch between specified (%d) and\n"
256 "actual (%d) cropping region lower boundary",
257 this->croppingRegion.y + this->croppingRegion.h,
258 (int)(dinfo->output_height - lines));
263 while (dinfo->output_scanline < dinfo->output_height)
264 _jpeg_read_scanlines(dinfo, &row_pointer[dinfo->output_scanline],
265 dinfo->output_height - dinfo->output_scanline);
267 jpeg_finish_decompress(dinfo);
270 if (dinfo->global_state > DSTATE_START) jpeg_abort_decompress(dinfo);
272 if (this->jerr.warning) retval = -1;
277 /*************************** Packed-Pixel Image I/O **************************/
280 DLLEXPORT _JSAMPLE *GET_NAME(tj3LoadImage, BITS_IN_JSAMPLE)
281 (tjhandle handle, const char *filename, int *width, int align, int *height,
284 static const char FUNCTION_NAME[] =
285 GET_STRING(tj3LoadImage, BITS_IN_JSAMPLE);
287 #if BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)
289 int retval = 0, tempc;
291 tjhandle handle2 = NULL;
293 j_compress_ptr cinfo = NULL;
294 cjpeg_source_ptr src;
295 _JSAMPLE *dstBuf = NULL;
299 GET_TJINSTANCE(handle, NULL)
301 if (!filename || !width || align < 1 || !height || !pixelFormat ||
302 *pixelFormat < TJPF_UNKNOWN || *pixelFormat >= TJ_NUMPF)
303 THROW("Invalid argument");
304 if ((align & (align - 1)) != 0)
305 THROW("Alignment must be a power of 2");
307 /* The instance handle passed to this function is used only for parameter
308 retrieval. Create a new temporary instance to avoid interfering with the
309 libjpeg state of the primary instance. */
310 if ((handle2 = tj3Init(TJINIT_COMPRESS)) == NULL) return NULL;
311 this2 = (tjinstance *)handle2;
312 cinfo = &this2->cinfo;
315 if (fopen_s(&file, filename, "rb") || file == NULL)
317 if ((file = fopen(filename, "rb")) == NULL)
319 THROW_UNIX("Cannot open input file");
321 if ((tempc = getc(file)) < 0 || ungetc(tempc, file) == EOF)
322 THROW_UNIX("Could not read input file")
323 else if (tempc == EOF)
324 THROW("Input file contains no data");
326 if (setjmp(this2->jerr.setjmp_buffer)) {
327 /* If we get here, the JPEG code has signaled an error. */
328 retval = -1; goto bailout;
331 cinfo->data_precision = BITS_IN_JSAMPLE;
332 if (*pixelFormat == TJPF_UNKNOWN) cinfo->in_color_space = JCS_UNKNOWN;
333 else cinfo->in_color_space = pf2cs[*pixelFormat];
335 if ((src = jinit_read_bmp(cinfo, FALSE)) == NULL)
336 THROW("Could not initialize bitmap loader");
337 invert = !this->bottomUp;
338 } else if (tempc == 'P') {
339 if ((src = _jinit_read_ppm(cinfo)) == NULL)
340 THROW("Could not initialize PPM loader");
341 invert = this->bottomUp;
343 THROW("Unsupported file type");
345 src->input_file = file;
346 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
347 /* Refuse to load images larger than 1 Megapixel when fuzzing. */
348 src->max_pixels = this->maxPixels;
350 (*src->start_input) (cinfo, src);
352 if (cinfo->X_density && cinfo->Y_density) {
353 this->xDensity = cinfo->X_density;
354 this->yDensity = cinfo->Y_density;
355 this->densityUnits = cinfo->density_unit;
358 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr)cinfo);
360 *width = cinfo->image_width; *height = cinfo->image_height;
361 *pixelFormat = cs2pf[cinfo->in_color_space];
363 pitch = PAD((*width) * tjPixelSize[*pixelFormat], align);
364 if ((unsigned long long)pitch * (unsigned long long)(*height) >
365 (unsigned long long)((size_t)-1) ||
366 (dstBuf = (_JSAMPLE *)malloc(pitch * (*height) *
367 sizeof(_JSAMPLE))) == NULL)
368 THROW("Memory allocation failure");
370 if (setjmp(this2->jerr.setjmp_buffer)) {
371 /* If we get here, the JPEG code has signaled an error. */
372 retval = -1; goto bailout;
375 while (cinfo->next_scanline < cinfo->image_height) {
376 int i, nlines = (*src->get_pixel_rows) (cinfo, src);
378 for (i = 0; i < nlines; i++) {
382 row = cinfo->next_scanline + i;
383 if (invert) dstptr = &dstBuf[((*height) - row - 1) * pitch];
384 else dstptr = &dstBuf[row * pitch];
385 memcpy(dstptr, src->_buffer[i],
386 (*width) * tjPixelSize[*pixelFormat] * sizeof(_JSAMPLE));
388 cinfo->next_scanline += nlines;
391 (*src->finish_input) (cinfo, src);
395 if (file) fclose(file);
396 if (retval < 0) { free(dstBuf); dstBuf = NULL; }
399 #else /* BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED) */
401 static const char ERROR_MSG[] =
402 "16-bit data precision requires lossless JPEG,\n"
403 "which was disabled at build time.";
404 _JSAMPLE *retval = NULL;
406 GET_TJINSTANCE(handle, NULL)
407 SNPRINTF(this->errStr, JMSG_LENGTH_MAX, "%s(): %s", FUNCTION_NAME,
409 this->isInstanceError = TRUE; THROWG(ERROR_MSG, NULL)
419 DLLEXPORT int GET_NAME(tj3SaveImage, BITS_IN_JSAMPLE)
420 (tjhandle handle, const char *filename, const _JSAMPLE *buffer, int width,
421 int pitch, int height, int pixelFormat)
423 static const char FUNCTION_NAME[] =
424 GET_STRING(tj3SaveImage, BITS_IN_JSAMPLE);
427 #if BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)
429 tjhandle handle2 = NULL;
431 j_decompress_ptr dinfo = NULL;
437 GET_TJINSTANCE(handle, -1)
439 if (!filename || !buffer || width < 1 || pitch < 0 || height < 1 ||
440 pixelFormat < 0 || pixelFormat >= TJ_NUMPF)
441 THROW("Invalid argument");
443 /* The instance handle passed to this function is used only for parameter
444 retrieval. Create a new temporary instance to avoid interfering with the
445 libjpeg state of the primary instance. */
446 if ((handle2 = tj3Init(TJINIT_DECOMPRESS)) == NULL)
448 this2 = (tjinstance *)handle2;
449 dinfo = &this2->dinfo;
452 if (fopen_s(&file, filename, "wb") || file == NULL)
454 if ((file = fopen(filename, "wb")) == NULL)
456 THROW_UNIX("Cannot open output file");
458 if (setjmp(this2->jerr.setjmp_buffer)) {
459 /* If we get here, the JPEG code has signaled an error. */
460 retval = -1; goto bailout;
463 this2->dinfo.out_color_space = pf2cs[pixelFormat];
464 dinfo->image_width = width; dinfo->image_height = height;
465 dinfo->global_state = DSTATE_READY;
466 dinfo->scale_num = dinfo->scale_denom = 1;
467 dinfo->data_precision = BITS_IN_JSAMPLE;
469 ptr = strrchr(filename, '.');
470 if (ptr && !strcasecmp(ptr, ".bmp")) {
471 if ((dst = jinit_write_bmp(dinfo, FALSE, FALSE)) == NULL)
472 THROW("Could not initialize bitmap writer");
473 invert = !this->bottomUp;
474 dinfo->X_density = (UINT16)this->xDensity;
475 dinfo->Y_density = (UINT16)this->yDensity;
476 dinfo->density_unit = (UINT8)this->densityUnits;
478 if ((dst = _jinit_write_ppm(dinfo)) == NULL)
479 THROW("Could not initialize PPM writer");
480 invert = this->bottomUp;
483 dst->output_file = file;
484 (*dst->start_output) (dinfo, dst);
485 (*dinfo->mem->realize_virt_arrays) ((j_common_ptr)dinfo);
487 if (pitch == 0) pitch = width * tjPixelSize[pixelFormat];
489 while (dinfo->output_scanline < dinfo->output_height) {
494 (_JSAMPLE *)&buffer[(height - dinfo->output_scanline - 1) * pitch];
496 rowptr = (_JSAMPLE *)&buffer[dinfo->output_scanline * pitch];
497 memcpy(dst->_buffer[0], rowptr,
498 width * tjPixelSize[pixelFormat] * sizeof(_JSAMPLE));
499 (*dst->put_pixel_rows) (dinfo, dst, 1);
500 dinfo->output_scanline++;
503 (*dst->finish_output) (dinfo, dst);
507 if (file) fclose(file);
510 #else /* BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED) */
512 GET_TJINSTANCE(handle, -1)
513 THROW("16-bit data precision requires lossless JPEG,\n"
514 "which was disabled at build time.")
525 #undef _jinit_read_ppm
526 #undef _jinit_write_ppm
527 #undef _jpeg_crop_scanline
528 #undef _jpeg_read_scanlines
529 #undef _jpeg_skip_scanlines
530 #undef _jpeg_write_scanlines