From ea3df2f6629b0f7595e9bcba2a9257d2f17acf83 Mon Sep 17 00:00:00 2001 From: DRC Date: Fri, 21 Nov 2014 15:33:19 +0000 Subject: [PATCH 1/2] Sometimes the sampling factors in grayscale images can be > 1 (for instance, if compressing using 'cjpeg -sample 2x2 -grayscale'.) Technically, sampling factors have no meaning with grayscale JPEGs, and the libjpeg decompressor ignores them in that case. Thus, the TurboJPEG decompressor should ignore them as well. git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/branches/1.3.x@1419 632fc199-4ca6-4c93-a231-07263d6284db --- ChangeLog.txt | 9 +++++++++ turbojpeg.c | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/ChangeLog.txt b/ChangeLog.txt index 927286a5..326ba5d9 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -28,6 +28,15 @@ about every 25 million iterations. [6] Fixed a build issue on OS X PowerPC platforms (md5cmp failed to build because OS X does not provide the le32toh() and htole32() functions.) +[7] The TurboJPEG API previously generated an error ("Could not determine +subsampling type for JPEG image") when attempting to decompress grayscale JPEG +images that were compressed with a sampling factor other than 1 (for instance, +with 'cjpeg -grayscale -sample 2x2'). Subsampling technically has no meaning +with grayscale JPEGs, and thus the horizontal and vertical sampling factors +for such images are ignored by the decompressor. However, the TurboJPEG API +was being too rigid and was expecting the sampling factors to be equal to 1 +before it treated the image as a grayscale JPEG. + 1.3.1 ===== diff --git a/turbojpeg.c b/turbojpeg.c index 7b9811d5..33ae875b 100644 --- a/turbojpeg.c +++ b/turbojpeg.c @@ -279,6 +279,14 @@ static int setDecompDefaults(struct jpeg_decompress_struct *dinfo, static int getSubsamp(j_decompress_ptr dinfo) { int retval=-1, i, k; + + /* The sampling factors actually have no meaning with grayscale JPEG files, + and in fact it's possible to generate grayscale JPEGs with sampling + factors > 1 (even though those sampling factors are ignored by the + decompressor.) Thus, we need to treat grayscale as a special case. */ + if(dinfo->num_components==1 && dinfo->jpeg_color_space==JCS_GRAYSCALE) + return TJSAMP_GRAY; + for(i=0; inum_components==pixelsize[i]) From 10c3e5d44eacf0e0f1934712daecacd93df266f9 Mon Sep 17 00:00:00 2001 From: DRC Date: Fri, 21 Nov 2014 15:35:33 +0000 Subject: [PATCH 2/2] Make TJCompressor.close() and TJDecompressor.close() idempotent git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/branches/1.3.x@1420 632fc199-4ca6-4c93-a231-07263d6284db --- ChangeLog.txt | 9 ++++++++- java/org/libjpegturbo/turbojpeg/TJCompressor.java | 3 ++- java/org/libjpegturbo/turbojpeg/TJDecompressor.java | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 326ba5d9..e366c6db 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -28,7 +28,14 @@ about every 25 million iterations. [6] Fixed a build issue on OS X PowerPC platforms (md5cmp failed to build because OS X does not provide the le32toh() and htole32() functions.) -[7] The TurboJPEG API previously generated an error ("Could not determine +[7] The close() method in the TJCompressor and TJDecompressor Java classes is +now idempotent. Previously, that method would call the native tjDestroy() +function even if the TurboJPEG instance had already been destroyed. This +caused an exception to be thrown during finalization, if the close() method had +already been called. The exception was caught, but it was still an expensive +operation. + +[8] The TurboJPEG API previously generated an error ("Could not determine subsampling type for JPEG image") when attempting to decompress grayscale JPEG images that were compressed with a sampling factor other than 1 (for instance, with 'cjpeg -grayscale -sample 2x2'). Subsampling technically has no meaning diff --git a/java/org/libjpegturbo/turbojpeg/TJCompressor.java b/java/org/libjpegturbo/turbojpeg/TJCompressor.java index 6fb653f0..29c8b2ab 100644 --- a/java/org/libjpegturbo/turbojpeg/TJCompressor.java +++ b/java/org/libjpegturbo/turbojpeg/TJCompressor.java @@ -496,7 +496,8 @@ public class TJCompressor { * Free the native structures associated with this compressor instance. */ public void close() throws Exception { - destroy(); + if (handle != 0) + destroy(); } protected void finalize() throws Throwable { diff --git a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java index ec8ab772..8b98dcfc 100644 --- a/java/org/libjpegturbo/turbojpeg/TJDecompressor.java +++ b/java/org/libjpegturbo/turbojpeg/TJDecompressor.java @@ -594,7 +594,8 @@ public class TJDecompressor { * Free the native structures associated with this decompressor instance. */ public void close() throws Exception { - destroy(); + if (handle != 0) + destroy(); } protected void finalize() throws Throwable {