1
0
mirror of https://github.com/libjpeg-turbo/libjpeg-turbo.git synced 2026-10-23 11:48:53 +03:00

294 Commits

Author SHA1 Message Date
DRC
2303b7c551 JNI: Guard against signed int overflow w/huge vals
fc324109d5 checked for unsigned int
overflow, but jsize is always a signed int, even when using a 64-bit
JVM.  (This reflects the fact that array sizes in Java are always signed
ints.)  Thus, array sizes should be checked against INT_MAX, not
UINT_MAX.

In almost all cases, this is a belt-and-suspenders measure.  With the
exception of TJCompressor.loadSourceImage(), the array size checks in
question are checking a Java array, which can never have more than
INT_MAX elements.  Thus, even if the previous unsigned int overflow
checks failed to catch signed int overflow, the error would have been
caught in a different place.  With TJCompressor.loadSourceImage(), it
was possible to load a packed-pixel image large enough that arraySize
overflowed a signed int and caused the NewByteArray() or NewShortArray()
JNI methods to crash the JVM.  However, this would have required a
multi-gigabyte packed-pixel image.

This commit also improves the array size checks in
TJCompressor.loadSourceImage() and TJDecompressor.saveImage() so that
they take into account row alignment/pitch.

Fixes #877
2026-03-24 11:59:35 -04:00
DRC
55bdf1b5c2 TJ Doc: Fancy upsamp. requires 4:2:2, 4:2:0, 4:4:0
Also, for the sake of readability, don't set TJ*PARAM_FASTUPSAMPLE in
TJUnitTest except when testing those subsampling levels.
2026-03-14 10:38:22 -04:00
DRC
239386b770 TJUnitTest: Test 1/8 scaling w/ 4:2:0 subsampling
It works as long as YUV decompression isn't enabled.
2026-03-14 09:56:09 -04:00
DRC
fb8d8e2009 tj3Transform(): Fix double ICC profile embedding
If TJPARAM_SAVEMARKERS was set to 2 or 4, TJXOPT_COPYNONE was not
specified, an ICC profile was extracted from the source image, and
another ICC profile was associated with the TurboJPEG instance using
tj3SetICCProfile(), tj3Transform() embedded both profiles in the
destination image.  This was mainly problematic because it might have
caused the destination image to exceed the worst-case size reported by
tj3TransformBufSize().

jpegtran would actually embed both ICC profiles as well, except that it
removes ICC profiles from the marker copying option if -icc is
specified.  The documented TurboJPEG API behavior is for TJXOPT_COPYNONE
to take precedence over TJPARAM_SAVEMARKERS and for TJPARAM_SAVEMARKERS
to take precedence over the associated ICC profile.  It would've been
easier to let the associated ICC profile take precedence, but the intent
of the TurboJPEG 3.1 API design was to allow either the associated or
extracted ICC profile to be selected on a per-transform basis using
TJXOPT_COPYNONE.  Basically, if TJPARAM_SAVEMARKERS is 2 or 4 and
TJXOPT_COPYNONE is not specified, that means "always copy the ICC
profile from the source image, even if it doesn't have one."
tj3TransformBufSize() assumes that behavior.
2026-03-12 12:09:26 -04:00
DRC
4d293d9400 Java: Use C++-style comments where practical 2026-02-26 14:28:08 -05:00
DRC
90a39204b5 Java & TJBench: Code readability tweaks 2026-02-25 15:00:13 -05:00
DRC
aa0a7a8e60 Java: Clean up cleanup
Use try-with-resources statements, where possible, to automatically
close FileInputStream, FileOutputStream, TJCompressor, TJDecompressor,
and TJTransformer instances.  We can't rely on the finalize() method in
the TurboJPEG classes anymore, because it has been deprecated and marked
for removal (although, as of this writing, it has yet to be removed.)
2026-02-24 12:40:56 -05:00
DRC
3f75326708 TJTran.java: Remove unneeded code 2026-02-23 10:25:20 -05:00
DRC
7708b35f46 Java examples: "C API" = "Java API"
(oops)
2026-02-22 14:23:22 -05:00
DRC
36dd2ddcb5 "TurboJPEG * wrapper" = "TurboJPEG * API" 2026-02-22 10:16:53 -05:00
DRC
f44f4a7181 Java doc: "function" = "method" 2026-02-22 10:16:53 -05:00
DRC
27949ae444 java/README.md: Wordsmithing 2026-02-20 16:53:10 -05:00
DRC
dea6ba4aaa JNI: Further guard against C/Java API mismatches
- Since the existing run-time API mismatch checks compared constants
  that are known at compile time, replace those checks with compile-time
  checks.  This ensures that an API mismatch is caught early.

- Implement actual run-time API mismatch checks to ensure that tj3Get(),
  tj3DecompressHeader(), and tj3LoadImage*() do not return subsampling,
  colorspace, or pixel format values that the TurboJPEG Java API can't
  handle.

- Check subsampling, colorspace, and transform operation arguments
  against the maximum values supported by the TurboJPEG Java API.  This
  prevents a (currently hypothetical) hard-to-diagnose failure whereby
  the C API would not catch out-of-bounds arguments from the Java API if
  the JNI library was accidentally used at run time with a newer version
  of the TurboJPEG API library that supports more options.  (TurboJPEG
  3.2 will likely support more subsampling options.)
2026-02-20 16:52:59 -05:00
DRC
c96e93b1f2 Build: Fix issues with blank install directories
If CMAKE_INSTALL_*DIR is blank, it means "install these components at
the top level of ${CMAKE_INSTALL_PREFIX}."  However, we need to expand
these variables to their full-path equivalents so that CMake doesn't
complain about a missing argument in install*() or try to install the
components in the root directory.
2026-02-18 09:22:11 -05:00
DRC
87cc94567a Markdown version of java/README 2026-02-17 21:15:27 -05:00
DRC
3c004e430d Java: Clean up Un*x JNI library loader
- Use System.mapLibraryName() rather than specifying a platform-specific
  shared library prefix and extension.

- Don't try to load libturbojpeg.dylib from /usr/lib on macOS.  With
  7175e51792 (libjpeg-turbo 1.3.0), we
  stopped installing libturbojpeg.dylib under /usr/lib, and such is not
  even possible with modern macOS releases.  Most likely, that fallback
  code remained in libjpeg-turbo 1.3.x because early versions of the
  TurboJPEG Java API attempted to allow mixing and matching of versions
  between the Java front end and JNI library.  (That was a relic of Java
  Web Start, the idea being that turbojpeg.jar could be deployed
  remotely via JWS and use a client-side installation of the TurboJPEG
  API library.  Eventually, that strategy became impossible to support.)
2026-02-17 20:44:15 -05:00
DRC
2fbf1db0d0 turbojpeg-jni.c: Fix typo 2026-02-17 11:26:29 -05:00
DRC
1a3e45c108 TJBench: Reorder global variables
... to match the order of TurboJPEG parameters and TJBench command-line
options.
2026-02-16 18:44:54 -05:00
DRC
14332331eb TJUnitTest.java: Document -bmp in usage screen
(oversight from 79b8d65f0f)
2026-02-15 15:56:23 -05:00
DRC
5029f55544 TJUnitTest: Use TJ*UNSCALED 2026-02-15 15:56:06 -05:00
DRC
e82016c6bf Consistify copyright header formatting
- Put a space after (C) in BSD-licensed source files.

- Add missing trailing period in jcphuff-sse2.asm.

- Remove comma after copyright date in src/md5/md5.c.
2026-02-02 08:22:43 -05:00
DRC
d8e8be9ac7 TJUnitTest: Test top-down PPM/bottom-up BMP load
(oops)
2026-01-14 15:42:27 -05:00
DRC
3cea8ccd51 rdppm.c: Fix CMYK upconversion/downconversion
If the data precision of the PBMPLUS file does not match the target data
precision, then the grayscale or RGB samples are rescaled to the target
data precision.  Thus, we need to pass (1 << cinfo->data_precision) - 1
to rgb_to_cmyk() instead of maxval.  This commit also modifies
TJUnitTest so that it validates the correctness of upconversion and
downconversion in the PPM reader.

Fixes #841
2025-12-09 18:24:36 -05:00
DRC
fc4b417a90 TJCompressor.loadSourceImage: Unset unused buffers
If loading a 2-to-8-bit image, unset srcBuf12 and srcBuf16.  If loading
a 9-to-12-bit image, unset srcBuf8 and srcBuf16.  If loading a
13-to-16-bit image, unset srcBuf8 and srcBuf12.  Otherwise,
TJCompressor.getSourceBuf() will return srcBuf8 or srcBuf12, in order,
if any previous invocation of TJCompressor.loadSourceImage() set them,
irrespective of the buffer that was set by the most recent invocation.

This probably helps with garbage collection as well, since it signals to
the GC that the unused buffers are really unused.
2025-12-09 18:21:21 -05:00
DRC
fc324109d5 JNI: Guard against int overflow w/ huge X, Y vals
This is not a security vulnerability, since applications that pass such
values to the Java API would fail regardless, and such a bug would never
make it into the wild.  By contrast, a security vulnerability arises
from applications that work correctly with most input data sets but
trigger a library failure, such as a buffer overrun, with one or more
specific input data sets.  As with most imaging APIs, the libjpeg-turbo
APIs rely upon the calling application to pass appropriately-sized
buffers and appropriate size/dimension arguments.  The failure to do so
is no more the fault of libjpeg-turbo than calling
'buf = malloc(1); buf[2] = 0;' is the fault of the C library.

Buffer size checking is a bonus feature of the Java API that isn't (and
can't be) provided by the C API, so this commit merely hardens the bonus
feature against API abuse, in keeping with the Java paradigm of throwing
an exception rather than crashing due to a caller-imposed buffer
overrun.
2025-12-03 14:52:03 -05:00
DRC
466c344878 TurboJPEG doc tweaks
- Clarify that YUV encoding performs downsampling as well as color
  conversion.

- Clarify that TJPARAM_LOSSLESS is ignored by tj3CompressFromYUV*8().

- Clarify that tj3CompressFromYUV*8() generates only YCbCr or grayscale
  JPEG images, i.e. that TJPARAM_COLORSPACE has no effect.
2025-10-23 10:42:23 -04:00
DRC
f74989d8c7 Clean up #include directives
This is subtle, but #include <header.h> searches directories specified
with -I, then system include directories.  #include "header.h" searches
the current source directory, then directories specified with -I, then
system include directories.

Using bracketed #include directives for jpeglib.h, jinclude.h, jerror.h,
cdjpeg.h, and turbojpeg.h only worked because the build system
explicitly passed -I{source_directory}/src/ to the compiler.  Referring
to 51cee03629, it's better for the source
code to have as few dependencies on our specific build system as
possible.

Since jpeglib.h, jinclude.h, jerror.h, and turbojpeg.h can be installed
in system include directories, it's also better for internal references
to those headers to resolve internally first, to avoid potential
conflicts between the system-installed version of libjpeg-turbo and the
version being built.  (Such conflicts would never have occurred with our
build system, but they might have occurred due to misintegration with a
downstream build system.)
2025-09-25 12:24:31 -04:00
DRC
c889b1da56 TJBench: Require additional argument with -copy
(oversight from e4c67aff50)
2025-06-12 10:08:21 -04:00
DRC
d7932a2709 TJ doc: Density params require YCbCr or grayscale
Since libjpeg-turbo does not support Exif, the only way it can embed
density information in a JPEG image is by using the JFIF marker, which
is only written if the JPEG colorspace is YCbCr or grayscale.
(Referring to the conversation under #793, we may need to further
restrict that to 8-bit-per-sample JPEG images, because the JFIF spec
requires 8-bit data precision.)
2024-10-30 12:21:50 -04:00
DRC
1b1356a565 TJComp: Explicitly specify data precision
This is just a code readability thing.  The logic effectively caused the
data precision to default to 8 if -precision was not specified, but that
was not obvious.

This commit also modifies tjcomptest so that it tests TJComp with no
-precision argument, to ensure that the data precision defaults to 8.
2024-09-23 15:24:48 -04:00
DRC
0d58f09997 TJTran: ICC profiles are now supported 2024-09-14 13:23:36 -04:00
DRC
6d02718d9a JNI: Guard against int. overflow w/ ICC profiles 2024-09-14 13:23:36 -04:00
DRC
9b01f5a057 TJ: Add func/method for computing xformed buf size 2024-09-14 13:23:32 -04:00
DRC
6f1fe2d113 turbojpeg-jni.c: Fix int. conv. warnings w/ VC++ 2024-09-09 10:54:16 -04:00
DRC
a272858212 TurboJPEG: ICC profile support 2024-09-06 19:55:41 -04:00
DRC
c519d7b679 Don't ignore JPEG buf size with TJPARAM_NOREALLOC
Since the introduction of TJFLAG_NOREALLOC in libjpeg-turbo 1.2.x, the
TurboJPEG C API documentation has (confusingly) stated that:

- if the JPEG buffer pointer points to a pre-allocated buffer, then the
JPEG buffer size must be specified, and

- the JPEG buffer size should be specified if the JPEG buffer is
pre-allocated to an arbitrary size.

The documentation never explicitly stated that the JPEG buffer size
should be specified if the JPEG buffer is pre-allocated to a worst-case
size, but since focus does not imply exclusion, it also never explicitly
stated the reverse.  Furthermore, the documentation never stated that
this was contingent upon TJPARAM_NOREALLOC/TJFLAG_NOREALLOC.  However,
effectively the compression and lossless transformation functions
ignored the JPEG buffer size(s) passed to them, and assumed that the
JPEG buffer(s) had been allocated to a worst-case size, if
TJPARAM_NOREALLOC/TJFLAG_NOREALLOC was set.  This behavior was an
accidental and undocumented throwback to libjpeg-turbo 1.1.x, in which
the tjCompress() function provided no way to specify the JPEG buffer
size.  It was always a bad idea for applications to rely upon that
behavior (although our own TJBench application unfortunately did.)
However, if such applications exist in the wild, the new behavior would
constitute a breaking change, so it has been introduced only into
libjpeg-turbo 3.1.x and only into TurboJPEG 3 API functions.  The
previous behavior has been retained when calling functions from the
TurboJPEG 2.1.x API and prior versions.

Did I mention that APIs are hard?
2024-09-06 19:55:27 -04:00
DRC
5f05c75ac1 Merge branch 'main' into dev 2024-09-06 19:55:20 -04:00
DRC
b3f0abe377 TJ: Calc. xformed buf sizes based on dst. subsamp
With respect to tj3Transform(), this addresses an oversight from
bb1d540a80.

Note to self: A convenience function/method for computing the worst-case
transformed JPEG size for a particular transform would be nice.
2024-09-06 19:04:28 -04:00
DRC
6d9591703f Minor TurboJPEG doc tweaks
- When transforming, the worst-case JPEG buffer size depends on the
  subsampling level used in the destination image, since a grayscale
  transform might have been applied.

- Parentheses Police
2024-09-05 22:31:40 -04:00
DRC
8db41dad22 Merge branch 'main' into dev 2024-09-05 15:15:08 -04:00
DRC
758e8a8e9f Java: Use Java-style method nomenclature
:: is a C++ thing.
2024-09-05 14:57:15 -04:00
DRC
e4c67aff50 TJBench: More argument consistification
-copynone --> -copy none

Add '-copy all', even though it's the default.

-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb, -gray, -cmyk -->
-pixelformat {rgb|bgr|rgbx|bgrx|xbgr|xrgb|gray|cmyk}
(This is mainly so -gray won't interfere with -grayscale.)

Fix an ArrayIndexOutOfBoundsException that occurred when passing -dct
to the Java version without specifying the DCT algorithm (oversight from
24fbf64d31a0758c63bcc27cf5d92fc5611717d0.)
2024-09-04 12:41:15 -04:00
DRC
d43ed7a1ff Merge branch 'main' into dev 2024-09-04 08:38:13 -04:00
DRC
5550c80fdc Doc: "compress operation"="compression operation"
(consistification)
2024-09-03 17:20:10 -04:00
DRC
37851a32c0 TurboJPEG: Add restart markers when transforming 2024-09-03 09:26:33 -04:00
DRC
debf57bc1d TJBench: Improve usage screen readability 2024-09-01 14:05:37 -04:00
DRC
fad6100704 Replace TJExample with IJG workalike programs 2024-09-01 14:05:15 -04:00
DRC
6d9f1f816c Merge branch 'main' into dev 2024-09-01 14:04:20 -04:00
DRC
797c6ccd98 Doc: Further clarify MCU definition 2024-09-01 11:25:29 -04:00
DRC
3e303e7235 TJBench: Allow British spellings in arguments 2024-08-31 18:42:19 -04:00