From: DRC Date: Fri, 14 Mar 2014 08:53:33 +0000 (+0000) Subject: Streamline the BufferedImage functionality in the compressor so that it works the... X-Git-Tag: 1.3.90~128 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b14813947ee5abacb40fc20455c8d7af1adfd3a7;p=libjpeg-turbo Streamline the BufferedImage functionality in the compressor so that it works the same way as compressing a "normal" image, and deprecate the old BufferedImage methods and other redundant methods. Eliminate the use of deprecated features in the test programs. git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@1168 632fc199-4ca6-4c93-a231-07263d6284db --- diff --git a/java/TJBench.java b/java/TJBench.java index 5257a1b..50cbadf 100644 --- a/java/TJBench.java +++ b/java/TJBench.java @@ -165,7 +165,7 @@ class TJBench { if (yuv == YUVDECODE) tjd.decompressToYUV(dstBuf, scaledw, yuvpad, scaledh, flags); else - tjd.decompress(dstBuf, scaledw, pitch, scaledh, pf, flags); + tjd.decompress(dstBuf, 0, 0, scaledw, pitch, scaledh, pf, flags); /* Benchmark */ for (i = 0, start = getTime(); (elapsed = getTime() - start) < benchTime; @@ -259,7 +259,7 @@ class TJBench { int ps = TJ.getPixelSize(pf), i; int yuvSize = 0; - yuvSize = TJ.bufSizeYUV(w, h, subsamp); + yuvSize = TJ.bufSizeYUV(w, yuvpad, h, subsamp); dstBuf = new byte[yuvSize]; if (quiet == 0) @@ -273,7 +273,7 @@ class TJBench { (flags & TJ.FLAG_BOTTOMUP) != 0 ? "BU" : "TD", subNameLong[subsamp]); - tjc = new TJCompressor(srcBuf, w, 0, h, pf); + tjc = new TJCompressor(srcBuf, 0, 0, w, 0, h, pf); tjc.setSubsamp(subsamp); /* Execute once to preload cache */ @@ -362,7 +362,7 @@ class TJBench { if (yuv == YUVCOMPRESS) tjc.setSourceImageYUV(srcBuf, tilew, yuvpad, tileh); else - tjc.setSourceImage(srcBuf, tilew, pitch, tileh, pf); + tjc.setSourceImage(srcBuf, 0, 0, tilew, pitch, tileh, pf); tjc.setJPEGQuality(jpegQual); tjc.setSubsamp(subsamp); @@ -618,8 +618,6 @@ class TJBench { System.out.println("-bottomup = Test bottom-up compression/decompression"); System.out.println("-tile = Test performance of the codec when the image is encoded as separate"); System.out.println(" tiles of varying sizes."); - System.out.println("-forcemmx, -forcesse, -forcesse2, -forcesse3 ="); - System.out.println(" Force MMX, SSE, SSE2, or SSE3 code paths in the underlying codec"); System.out.println("-rgb, -bgr, -rgbx, -bgrx, -xbgr, -xrgb ="); System.out.println(" Test the specified color conversion path in the codec (default: BGR)"); System.out.println("-fastupsample = Use the fastest chrominance upsampling algorithm available in"); @@ -729,22 +727,6 @@ class TJBench { if (argv[i].equalsIgnoreCase("-tile")) { doTile = true; xformOpt |= TJTransform.OPT_CROP; } - if (argv[i].equalsIgnoreCase("-forcesse3")) { - System.out.println("Forcing SSE3 code\n"); - flags |= TJ.FLAG_FORCESSE3; - } - if (argv[i].equalsIgnoreCase("-forcesse2")) { - System.out.println("Forcing SSE2 code\n"); - flags |= TJ.FLAG_FORCESSE2; - } - if (argv[i].equalsIgnoreCase("-forcesse")) { - System.out.println("Forcing SSE code\n"); - flags |= TJ.FLAG_FORCESSE; - } - if (argv[i].equalsIgnoreCase("-forcemmx")) { - System.out.println("Forcing MMX code\n"); - flags |= TJ.FLAG_FORCEMMX; - } if (argv[i].equalsIgnoreCase("-fastupsample")) { System.out.println("Using fast upsampling code\n"); flags |= TJ.FLAG_FASTUPSAMPLE; diff --git a/java/TJExample.java b/java/TJExample.java index a4971a4..7562114 100644 --- a/java/TJExample.java +++ b/java/TJExample.java @@ -1,5 +1,5 @@ /* - * Copyright (C)2011-2012 D. R. Commander. All Rights Reserved. + * Copyright (C)2011-2012, 2014 D. R. Commander. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -324,11 +324,11 @@ public class TJExample implements TJCustomFilter { tjc.setSubsamp(outSubsamp); tjc.setJPEGQuality(outQual); if (img != null) - jpegBuf = tjc.compress(img, flags); + tjc.setSourceImage(img, 0, 0, 0, 0); else { - tjc.setSourceImage(bmpBuf, width, 0, height, TJ.PF_BGRX); - jpegBuf = tjc.compress(flags); + tjc.setSourceImage(bmpBuf, 0, 0, width, 0, height, TJ.PF_BGRX); } + jpegBuf = tjc.compress(flags); jpegSize = tjc.getCompressedSize(); tjc.close(); diff --git a/java/TJUnitTest.java b/java/TJUnitTest.java index f7eef66..0bf8367 100644 --- a/java/TJUnitTest.java +++ b/java/TJUnitTest.java @@ -736,21 +736,16 @@ public class TJUnitTest { tjc.setSubsamp(subsamp); tjc.setJPEGQuality(jpegQual); tjc.setYUVPad(pad); - if (bi) { - if (yuv == YUVENCODE) - tjc.encodeYUV(img, dstBuf, flags); - else - tjc.compress(img, dstBuf, flags); - } else { - if (yuv == YUVDECODE) - tjc.setSourceImageYUV(srcBuf, w, pad, h); - else - tjc.setSourceImage(srcBuf, w, 0, h, pf); - if (yuv == YUVENCODE) - tjc.encodeYUV(dstBuf, flags); - else - tjc.compress(dstBuf, flags); - } + if (yuv == YUVDECODE) + tjc.setSourceImageYUV(srcBuf, w, pad, h); + else if (bi) + tjc.setSourceImage(img, 0, 0, 0, 0); + else + tjc.setSourceImage(srcBuf, 0, 0, w, 0, h, pf); + if (yuv == YUVENCODE) + tjc.encodeYUV(dstBuf, flags); + else + tjc.compress(dstBuf, flags); size = tjc.getCompressedSize(); if (yuv == YUVENCODE) @@ -955,7 +950,7 @@ public class TJUnitTest { for (i = 0; i < w * h * 4; i++) { srcBuf[i] = (byte)(r.nextInt(2) * 255); } - tjc.setSourceImage(srcBuf, w, 0, h, TJ.PF_BGRX); + tjc.setSourceImage(srcBuf, 0, 0, w, 0, h, TJ.PF_BGRX); tjc.setSubsamp(subsamp); tjc.setJPEGQuality(100); tjc.setYUVPad(pad); @@ -972,7 +967,7 @@ public class TJUnitTest { for (i = 0; i < h * w * 4; i++) { srcBuf[i] = (byte)(r.nextInt(2) * 255); } - tjc.setSourceImage(srcBuf, h, 0, w, TJ.PF_BGRX); + tjc.setSourceImage(srcBuf, 0, 0, h, 0, w, TJ.PF_BGRX); if (yuv == YUVENCODE) tjc.encodeYUV(dstBuf, 0); else diff --git a/java/doc/deprecated-list.html b/java/doc/deprecated-list.html index 9f3b4fd..bcf6858 100644 --- a/java/doc/deprecated-list.html +++ b/java/doc/deprecated-list.html @@ -83,6 +83,7 @@ function windowTitle() Contents @@ -126,6 +127,20 @@ function windowTitle()           Use TJ.bufSizeYUV(int, int, int, int) instead.  +org.libjpegturbo.turbojpeg.TJCompressor.compress(BufferedImage, byte[], int) +
+          Use + TJCompressor.setSourceImage(BufferedImage, int, int, int, int) and + TJCompressor.compress(byte[], int) instead.  + + +org.libjpegturbo.turbojpeg.TJCompressor.compress(BufferedImage, int) +
+          Use + TJCompressor.setSourceImage(BufferedImage, int, int, int, int) and + TJCompressor.compress(int) instead.  + + org.libjpegturbo.turbojpeg.TJDecompressor.decompress(byte[], int, int, int, int, int)
          Use @@ -143,6 +158,20 @@ function windowTitle()           Use TJDecompressor.decompressToYUV(int, int, int, int) instead.  +org.libjpegturbo.turbojpeg.TJCompressor.encodeYUV(BufferedImage, byte[], int) +
+          Use + TJCompressor.setSourceImage(BufferedImage, int, int, int, int) and + TJCompressor.encodeYUV(byte[], int) instead.  + + +org.libjpegturbo.turbojpeg.TJCompressor.encodeYUV(BufferedImage, int) +
+          Use + TJCompressor.setSourceImage(BufferedImage, int, int, int, int) and + TJCompressor.encodeYUV(int) instead.  + + org.libjpegturbo.turbojpeg.TJCompressor.setSourceImage(byte[], int, int, int, int)
          Use @@ -151,6 +180,21 @@ function windowTitle()  

+ + + + + + + + +
+Deprecated Constructors
org.libjpegturbo.turbojpeg.TJCompressor(byte[], int, int, int, int) +
+          Use + TJCompressor.TJCompressor(byte[], int, int, int, int, int, int) instead. 
+  +


diff --git a/java/doc/index-all.html b/java/doc/index-all.html index 81d9b45..9db1075 100644 --- a/java/doc/index-all.html +++ b/java/doc/index-all.html @@ -113,12 +113,14 @@ Method in class org.libjpegturbo.turbojpeg.compress(BufferedImage, byte[], int) - Method in class org.libjpegturbo.turbojpeg.TJCompressor -
Compress the uncompressed source image stored in srcImage - and output a JPEG image to the given destination buffer. +
Deprecated. Use + TJCompressor.setSourceImage(BufferedImage, int, int, int, int) and + TJCompressor.compress(byte[], int) instead.
compress(BufferedImage, int) - Method in class org.libjpegturbo.turbojpeg.TJCompressor -
Compress the uncompressed source image stored in srcImage - and return a buffer containing a JPEG image. +
Deprecated. Use + TJCompressor.setSourceImage(BufferedImage, int, int, int, int) and + TJCompressor.compress(int) instead.
CS_CMYK - Static variable in class org.libjpegturbo.turbojpeg.TJ
CMYK colorspace. @@ -200,12 +202,14 @@ Method in class org.libjpegturbo.turbojpeg.encodeYUV(BufferedImage, byte[], int) - Method in class org.libjpegturbo.turbojpeg.TJCompressor -
Encode the uncompressed source image stored in srcImage - and output a YUV planar image to the given destination buffer. +
Deprecated. Use + TJCompressor.setSourceImage(BufferedImage, int, int, int, int) and + TJCompressor.encodeYUV(byte[], int) instead.
encodeYUV(BufferedImage, int) - Method in class org.libjpegturbo.turbojpeg.TJCompressor -
Encode the uncompressed source image stored in srcImage - and return a buffer containing a YUV planar image. +
Deprecated. Use + TJCompressor.setSourceImage(BufferedImage, int, int, int, int) and + TJCompressor.encodeYUV(int) instead.
equals(TJScalingFactor) - Method in class org.libjpegturbo.turbojpeg.TJScalingFactor
Returns true or false, depending on whether this instance and @@ -521,6 +525,9 @@ Method in class org.libjpegturbo.turbojpeg.TJCompressor
Deprecated. Use TJCompressor.setSourceImage(byte[], int, int, int, int, int, int) instead. +
setSourceImage(BufferedImage, int, int, int, int) - +Method in class org.libjpegturbo.turbojpeg.TJCompressor +
Associate an uncompressed source image with this compressor instance.
setSourceImageYUV(byte[], int, int, int) - Method in class org.libjpegturbo.turbojpeg.TJCompressor
Associate an uncompressed YUV planar source image with this compressor @@ -545,10 +552,14 @@ Constructor for class org.libjpegturbo.turbojpeg.TJCompressor(byte[], int, int, int, int) - Constructor for class org.libjpegturbo.turbojpeg.TJCompressor +
Deprecated. Use + TJCompressor.TJCompressor(byte[], int, int, int, int, int, int) instead. +
TJCompressor(byte[], int, int, int, int, int, int) - +Constructor for class org.libjpegturbo.turbojpeg.TJCompressor
Create a TurboJPEG compressor instance and associate the uncompressed source image stored in srcImage with the newly-created instance. -
TJCompressor(byte[], int, int, int, int, int, int) - +
TJCompressor(BufferedImage, int, int, int, int) - Constructor for class org.libjpegturbo.turbojpeg.TJCompressor
Create a TurboJPEG compressor instance and associate the uncompressed source image stored in srcImage with the newly-created diff --git a/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html b/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html index 35114c7..b6452f3 100644 --- a/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html +++ b/java/doc/org/libjpegturbo/turbojpeg/TJCompressor.html @@ -120,6 +120,18 @@ TurboJPEG compressor           Create a TurboJPEG compressor instance. +TJCompressor(java.awt.image.BufferedImage srcImage, + int x, + int y, + int width, + int height) + +
+          Create a TurboJPEG compressor instance and associate the uncompressed + source image stored in srcImage with the newly-created + instance. + + TJCompressor(byte[] srcImage, int width, int pitch, @@ -127,9 +139,8 @@ TurboJPEG compressor int pixelFormat)
-          Create a TurboJPEG compressor instance and associate the uncompressed - source image stored in srcImage with the newly-created - instance. +          Deprecated. Use + TJCompressor(byte[], int, int, int, int, int, int) instead. TJCompressor(byte[] srcImage, @@ -171,8 +182,9 @@ TurboJPEG compressor int flags)
-          Compress the uncompressed source image stored in srcImage - and output a JPEG image to the given destination buffer. +          Deprecated. Use + setSourceImage(BufferedImage, int, int, int, int) and + compress(byte[], int) instead. @@ -181,8 +193,9 @@ TurboJPEG compressor int flags)
-          Compress the uncompressed source image stored in srcImage - and return a buffer containing a JPEG image. +          Deprecated. Use + setSourceImage(BufferedImage, int, int, int, int) and + compress(int) instead. @@ -211,8 +224,9 @@ TurboJPEG compressor int flags)
-          Encode the uncompressed source image stored in srcImage - and output a YUV planar image to the given destination buffer. +          Deprecated. Use + setSourceImage(BufferedImage, int, int, int, int) and + encodeYUV(byte[], int) instead. @@ -221,8 +235,9 @@ TurboJPEG compressor int flags)
-          Encode the uncompressed source image stored in srcImage - and return a buffer containing a YUV planar image. +          Deprecated. Use + setSourceImage(BufferedImage, int, int, int, int) and + encodeYUV(int) instead. @@ -271,6 +286,18 @@ TurboJPEG compressor  void +setSourceImage(java.awt.image.BufferedImage srcImage, + int x, + int y, + int width, + int height) + +
+          Associate an uncompressed source image with this compressor instance. + + + + void setSourceImage(byte[] srcImage, int width, int pitch, @@ -365,7 +392,30 @@ public TJCompressor()

TJCompressor

+@Deprecated
+public TJCompressor(byte[] srcImage,
+                               int width,
+                               int pitch,
+                               int height,
+                               int pixelFormat)
+             throws java.lang.Exception
+
+
Deprecated. Use + TJCompressor(byte[], int, int, int, int, int, int) instead. +

+

+ +
Throws: +
java.lang.Exception
+
+
+ +

+TJCompressor

+
 public TJCompressor(byte[] srcImage,
+                    int x,
+                    int y,
                     int width,
                     int pitch,
                     int height,
@@ -377,23 +427,21 @@ public TJCompressor(byte[] srcImage,
  instance.
 

-
Parameters:
srcImage - see setSourceImage(byte[], int, int, int, int, int, int) for description
width - see setSourceImage(byte[], int, int, int, int, int, int) for description
pitch - see setSourceImage(byte[], int, int, int, int, int, int) for description
height - see setSourceImage(byte[], int, int, int, int, int, int) for description
pixelFormat - pixel format of the source image (one of +
Parameters:
srcImage - see setSourceImage(byte[], int, int, int, int, int, int) for description
x - see setSourceImage(byte[], int, int, int, int, int, int) for description
y - see setSourceImage(byte[], int, int, int, int, int, int) for description
width - see setSourceImage(byte[], int, int, int, int, int, int) for description
pitch - see setSourceImage(byte[], int, int, int, int, int, int) for description
height - see setSourceImage(byte[], int, int, int, int, int, int) for description
pixelFormat - pixel format of the source image (one of TJ.PF_*)
Throws:
java.lang.Exception

-

+

TJCompressor

-public TJCompressor(byte[] srcImage,
+public TJCompressor(java.awt.image.BufferedImage srcImage,
                     int x,
                     int y,
                     int width,
-                    int pitch,
-                    int height,
-                    int pixelFormat)
+                    int height)
              throws java.lang.Exception
Create a TurboJPEG compressor instance and associate the uncompressed @@ -401,8 +449,12 @@ public TJCompressor(byte[] srcImage, instance.

-
Parameters:
srcImage - see setSourceImage(byte[], int, int, int, int, int, int) for description
x - see setSourceImage(byte[], int, int, int, int, int, int) for description
y - see setSourceImage(byte[], int, int, int, int, int, int) for description
width - see setSourceImage(byte[], int, int, int, int, int, int) for description
pitch - see setSourceImage(byte[], int, int, int, int, int, int) for description
height - see setSourceImage(byte[], int, int, int, int, int, int) for description
pixelFormat - pixel format of the source image (one of - TJ.PF_*) +
Parameters:
srcImage - see + setSourceImage(BufferedImage, int, int, int, int) for description
x - see + setSourceImage(BufferedImage, int, int, int, int) for description
y - see + setSourceImage(BufferedImage, int, int, int, int) for description
width - see + setSourceImage(BufferedImage, int, int, int, int) for description
height - see + setSourceImage(BufferedImage, int, int, int, int) for description
Throws:
java.lang.Exception
@@ -474,6 +526,31 @@ setSourceImage
+

+setSourceImage

+
+public void setSourceImage(java.awt.image.BufferedImage srcImage,
+                           int x,
+                           int y,
+                           int width,
+                           int height)
+                    throws java.lang.Exception
+
+
Associate an uncompressed source image with this compressor instance. +

+

+
Parameters:
srcImage - a BufferedImage instance containing RGB or + grayscale pixels to be compressed
x - x offset (in pixels) of the region in the source image from which + the JPEG image should be compressed
y - y offset (in pixels) of the region in the source image from which + the JPEG image should be compressed
width - width (in pixels) of the region in the source image from + which the JPEG image should be compressed (0 = compress the whole image)
height - height (in pixels) of the region in the source image from + which the JPEG image should be compressed (0 = compress the whole image) +
Throws: +
java.lang.Exception
+
+
+
+

setSourceImageYUV

@@ -593,19 +670,18 @@ public byte[] compress(int flags)
 

compress

-public void compress(java.awt.image.BufferedImage srcImage,
-                     byte[] dstBuf,
-                     int flags)
+@Deprecated
+public void compress(java.awt.image.BufferedImage srcImage,
+                                byte[] dstBuf,
+                                int flags)
               throws java.lang.Exception
-
Compress the uncompressed source image stored in srcImage - and output a JPEG image to the given destination buffer. +
Deprecated. Use + setSourceImage(BufferedImage, int, int, int, int) and + compress(byte[], int) instead.

-
Parameters:
srcImage - a BufferedImage instance containing RGB or - grayscale pixels to be compressed
dstBuf - buffer that will receive the JPEG image. Use - TJ.bufSize(int, int, int) to determine the maximum size for this buffer based on - the image width, height, and level of chrominance subsampling.
flags - the bitwise OR of one or more of TJ.FLAG_* +
Throws:
java.lang.Exception
@@ -615,18 +691,17 @@ public void compress(java.awt.image.BufferedImage srcImage,

compress

-public byte[] compress(java.awt.image.BufferedImage srcImage,
-                       int flags)
+@Deprecated
+public byte[] compress(java.awt.image.BufferedImage srcImage,
+                                  int flags)
                 throws java.lang.Exception
-
Compress the uncompressed source image stored in srcImage - and return a buffer containing a JPEG image. +
Deprecated. Use + setSourceImage(BufferedImage, int, int, int, int) and + compress(int) instead.

-
Parameters:
srcImage - a BufferedImage instance containing RGB or - grayscale pixels to be compressed
flags - the bitwise OR of one or more of TJ.FLAG_* -
Returns:
a buffer containing a JPEG image. The length of this buffer will - not be equal to the size of the JPEG image. Use getCompressedSize() to obtain the size of the JPEG image. +
Throws:
java.lang.Exception
@@ -708,20 +783,18 @@ public byte[] encodeYUV(int flags)

encodeYUV

-public void encodeYUV(java.awt.image.BufferedImage srcImage,
-                      byte[] dstBuf,
-                      int flags)
+@Deprecated
+public void encodeYUV(java.awt.image.BufferedImage srcImage,
+                                 byte[] dstBuf,
+                                 int flags)
                throws java.lang.Exception
-
Encode the uncompressed source image stored in srcImage - and output a YUV planar image to the given destination buffer. See - encodeYUV(byte[], int) for more detail. +
Deprecated. Use + setSourceImage(BufferedImage, int, int, int, int) and + encodeYUV(byte[], int) instead.

-
Parameters:
srcImage - a BufferedImage instance containing RGB or - grayscale pixels to be encoded
dstBuf - buffer that will receive the YUV planar image. Use - TJ.bufSizeYUV(int, int, int, int) to determine the appropriate size for this buffer - based on the image width, height, and level of chrominance subsampling.
flags - the bitwise OR of one or more of TJ.FLAG_* +
Throws:
java.lang.Exception
@@ -731,18 +804,17 @@ public void encodeYUV(java.awt.image.BufferedImage srcImage,

encodeYUV

-public byte[] encodeYUV(java.awt.image.BufferedImage srcImage,
-                        int flags)
+@Deprecated
+public byte[] encodeYUV(java.awt.image.BufferedImage srcImage,
+                                   int flags)
                  throws java.lang.Exception
-
Encode the uncompressed source image stored in srcImage - and return a buffer containing a YUV planar image. See - encodeYUV(byte[], int) for more detail. +
Deprecated. Use + setSourceImage(BufferedImage, int, int, int, int) and + encodeYUV(int) instead.

-
Parameters:
srcImage - a BufferedImage instance containing RGB or - grayscale pixels to be encoded
flags - the bitwise OR of one or more of TJ.FLAG_* -
Returns:
a buffer containing a YUV planar image +
Throws:
java.lang.Exception
diff --git a/java/org/libjpegturbo/turbojpeg/TJCompressor.java b/java/org/libjpegturbo/turbojpeg/TJCompressor.java index 63a7fa5..ed8d8e1 100644 --- a/java/org/libjpegturbo/turbojpeg/TJCompressor.java +++ b/java/org/libjpegturbo/turbojpeg/TJCompressor.java @@ -1,5 +1,5 @@ /* - * Copyright (C)2011-2013 D. R. Commander. All Rights Reserved. + * Copyright (C)2011-2014 D. R. Commander. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -47,21 +47,10 @@ public class TJCompressor { } /** - * Create a TurboJPEG compressor instance and associate the uncompressed - * source image stored in srcImage with the newly-created - * instance. - * - * @param srcImage see {@link #setSourceImage} for description - * - * @param width see {@link #setSourceImage} for description - * - * @param pitch see {@link #setSourceImage} for description - * - * @param height see {@link #setSourceImage} for description - * - * @param pixelFormat pixel format of the source image (one of - * {@link TJ TJ.PF_*}) + * @deprecated Use + * {@link #TJCompressor(byte[], int, int, int, int, int, int)} instead. */ + @Deprecated public TJCompressor(byte[] srcImage, int width, int pitch, int height, int pixelFormat) throws Exception { setSourceImage(srcImage, width, pitch, height, pixelFormat); @@ -92,6 +81,31 @@ public class TJCompressor { setSourceImage(srcImage, x, y, width, pitch, height, pixelFormat); } + /** + * Create a TurboJPEG compressor instance and associate the uncompressed + * source image stored in srcImage with the newly-created + * instance. + * + * @param srcImage see + * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description + * + * @param x see + * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description + * + * @param y see + * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description + * + * @param width see + * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description + * + * @param height see + * {@link #setSourceImage(BufferedImage, int, int, int, int)} for description + */ + public TJCompressor(BufferedImage srcImage, int x, int y, int width, + int height) throws Exception { + setSourceImage(srcImage, x, y, width, height); + } + /** * Associate an uncompressed source image with this compressor instance. * @@ -139,6 +153,7 @@ public class TJCompressor { srcPixelFormat = pixelFormat; srcX = x; srcY = y; + srcBufInt = null; srcIsYUV = false; } @@ -153,6 +168,88 @@ public class TJCompressor { srcX = srcY = -1; } + /** + * Associate an uncompressed source image with this compressor instance. + * + * @param srcImage a BufferedImage instance containing RGB or + * grayscale pixels to be compressed + * + * @param x x offset (in pixels) of the region in the source image from which + * the JPEG image should be compressed + * + * @param y y offset (in pixels) of the region in the source image from which + * the JPEG image should be compressed + * + * @param width width (in pixels) of the region in the source image from + * which the JPEG image should be compressed (0 = compress the whole image) + * + * @param height height (in pixels) of the region in the source image from + * which the JPEG image should be compressed (0 = compress the whole image) + */ + public void setSourceImage(BufferedImage srcImage, int x, int y, int width, + int height) throws Exception { + if (handle == 0) init(); + if (srcImage == null || x < 0 || y < 0 || width < 0 || height < 0) + throw new Exception("Invalid argument in setSourceImage()"); + srcX = x; + srcY = y; + srcWidth = (width == 0) ? srcImage.getWidth(): width; + srcHeight = (height == 0) ? srcImage.getHeight() : height; + if (x + width > srcImage.getWidth() || y + height > srcImage.getHeight()) + throw new Exception("Compression region exceeds the bounds of the source image"); + + int pixelFormat; + boolean intPixels = false; + if (byteOrder == null) + byteOrder = ByteOrder.nativeOrder(); + switch(srcImage.getType()) { + case BufferedImage.TYPE_3BYTE_BGR: + pixelFormat = TJ.PF_BGR; break; + case BufferedImage.TYPE_4BYTE_ABGR: + case BufferedImage.TYPE_4BYTE_ABGR_PRE: + pixelFormat = TJ.PF_XBGR; break; + case BufferedImage.TYPE_BYTE_GRAY: + pixelFormat = TJ.PF_GRAY; break; + case BufferedImage.TYPE_INT_BGR: + if (byteOrder == ByteOrder.BIG_ENDIAN) + pixelFormat = TJ.PF_XBGR; + else + pixelFormat = TJ.PF_RGBX; + intPixels = true; break; + case BufferedImage.TYPE_INT_RGB: + case BufferedImage.TYPE_INT_ARGB: + case BufferedImage.TYPE_INT_ARGB_PRE: + if (byteOrder == ByteOrder.BIG_ENDIAN) + pixelFormat = TJ.PF_XRGB; + else + pixelFormat = TJ.PF_BGRX; + intPixels = true; break; + default: + throw new Exception("Unsupported BufferedImage format"); + } + srcPixelFormat = pixelFormat; + + WritableRaster wr = srcImage.getRaster(); + if (intPixels) { + SinglePixelPackedSampleModel sm = + (SinglePixelPackedSampleModel)srcImage.getSampleModel(); + srcStride = sm.getScanlineStride(); + DataBufferInt db = (DataBufferInt)wr.getDataBuffer(); + srcBufInt = db.getData(); + srcBuf = null; + } else { + ComponentSampleModel sm = + (ComponentSampleModel)srcImage.getSampleModel(); + int pixelSize = sm.getPixelStride(); + if (pixelSize != TJ.getPixelSize(pixelFormat)) + throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage"); + srcPitch = sm.getScanlineStride(); + DataBufferByte db = (DataBufferByte)wr.getDataBuffer(); + srcBuf = db.getData(); + srcBufInt = null; + } + } + /** * Associate an uncompressed YUV planar source image with this compressor * instance. @@ -237,16 +334,17 @@ public class TJCompressor { public void compress(byte[] dstBuf, int flags) throws Exception { if (dstBuf == null || flags < 0) throw new Exception("Invalid argument in compress()"); - if (srcBuf == null) + if (srcBuf == null && (srcBufInt == null || srcIsYUV)) throw new Exception(NO_ASSOC_ERROR); if (jpegQuality < 0) throw new Exception("JPEG Quality not set"); if (subsamp < 0) throw new Exception("Subsampling level not set"); + if (srcIsYUV) compressedSize = compressFromYUV(srcBuf, srcWidth, srcYUVPad, srcHeight, subsamp, dstBuf, jpegQuality, flags); - else { + else if (srcBuf != null) { if (srcX >= 0 && srcY >= 0) compressedSize = compress(srcBuf, srcX, srcY, srcWidth, srcPitch, srcHeight, srcPixelFormat, dstBuf, subsamp, @@ -255,6 +353,15 @@ public class TJCompressor { compressedSize = compress(srcBuf, srcWidth, srcPitch, srcHeight, srcPixelFormat, dstBuf, subsamp, jpegQuality, flags); + } else if (srcBufInt != null) { + if (srcX >= 0 && srcY >= 0) + compressedSize = compress(srcBufInt, srcX, srcY, srcWidth, srcStride, + srcHeight, srcPixelFormat, dstBuf, subsamp, + jpegQuality, flags); + else + compressedSize = compress(srcBufInt, srcWidth, srcStride, srcHeight, + srcPixelFormat, dstBuf, subsamp, jpegQuality, + flags); } } @@ -277,109 +384,26 @@ public class TJCompressor { } /** - * Compress the uncompressed source image stored in srcImage - * and output a JPEG image to the given destination buffer. - * - * @param srcImage a BufferedImage instance containing RGB or - * grayscale pixels to be compressed - * - * @param dstBuf buffer that will receive the JPEG image. Use - * {@link TJ#bufSize} to determine the maximum size for this buffer based on - * the image width, height, and level of chrominance subsampling. - * - * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*} + * @deprecated Use + * {@link #setSourceImage(BufferedImage, int, int, int, int)} and + * {@link #compress(byte[], int)} instead. */ + @Deprecated public void compress(BufferedImage srcImage, byte[] dstBuf, int flags) throws Exception { - if (srcImage == null || dstBuf == null || flags < 0) - throw new Exception("Invalid argument in compress()"); - int width = srcImage.getWidth(); - int height = srcImage.getHeight(); - int pixelFormat; - boolean intPixels = false; - if (byteOrder == null) - byteOrder = ByteOrder.nativeOrder(); - switch(srcImage.getType()) { - case BufferedImage.TYPE_3BYTE_BGR: - pixelFormat = TJ.PF_BGR; break; - case BufferedImage.TYPE_4BYTE_ABGR: - case BufferedImage.TYPE_4BYTE_ABGR_PRE: - pixelFormat = TJ.PF_XBGR; break; - case BufferedImage.TYPE_BYTE_GRAY: - pixelFormat = TJ.PF_GRAY; break; - case BufferedImage.TYPE_INT_BGR: - if (byteOrder == ByteOrder.BIG_ENDIAN) - pixelFormat = TJ.PF_XBGR; - else - pixelFormat = TJ.PF_RGBX; - intPixels = true; break; - case BufferedImage.TYPE_INT_RGB: - case BufferedImage.TYPE_INT_ARGB: - case BufferedImage.TYPE_INT_ARGB_PRE: - if (byteOrder == ByteOrder.BIG_ENDIAN) - pixelFormat = TJ.PF_XRGB; - else - pixelFormat = TJ.PF_BGRX; - intPixels = true; break; - default: - throw new Exception("Unsupported BufferedImage format"); - } - WritableRaster wr = srcImage.getRaster(); - if (jpegQuality < 0) - throw new Exception("JPEG Quality not set"); - if (subsamp < 0) - throw new Exception("Subsampling level not set"); - if (intPixels) { - SinglePixelPackedSampleModel sm = - (SinglePixelPackedSampleModel)srcImage.getSampleModel(); - int stride = sm.getScanlineStride(); - DataBufferInt db = (DataBufferInt)wr.getDataBuffer(); - int[] buf = db.getData(); - if (srcX >= 0 && srcY >= 0) - compressedSize = compress(buf, srcX, srcY, width, stride, height, - pixelFormat, dstBuf, subsamp, jpegQuality, - flags); - else - compressedSize = compress(buf, width, stride, height, pixelFormat, - dstBuf, subsamp, jpegQuality, flags); - } else { - ComponentSampleModel sm = - (ComponentSampleModel)srcImage.getSampleModel(); - int pixelSize = sm.getPixelStride(); - if (pixelSize != TJ.getPixelSize(pixelFormat)) - throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage"); - int pitch = sm.getScanlineStride(); - DataBufferByte db = (DataBufferByte)wr.getDataBuffer(); - byte[] buf = db.getData(); - if (srcX >= 0 && srcY >= 0) - compressedSize = compress(buf, srcX, srcY, width, pitch, height, - pixelFormat, dstBuf, subsamp, jpegQuality, - flags); - else - compressedSize = compress(buf, width, pitch, height, pixelFormat, - dstBuf, subsamp, jpegQuality, flags); - } + setSourceImage(srcImage, 0, 0, 0, 0); + compress(dstBuf, flags); } /** - * Compress the uncompressed source image stored in srcImage - * and return a buffer containing a JPEG image. - * - * @param srcImage a BufferedImage instance containing RGB or - * grayscale pixels to be compressed - * - * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*} - * - * @return a buffer containing a JPEG image. The length of this buffer will - * not be equal to the size of the JPEG image. Use {@link - * #getCompressedSize} to obtain the size of the JPEG image. + * @deprecated Use + * {@link #setSourceImage(BufferedImage, int, int, int, int)} and + * {@link #compress(int)} instead. */ + @Deprecated public byte[] compress(BufferedImage srcImage, int flags) throws Exception { - int width = srcImage.getWidth(); - int height = srcImage.getHeight(); - byte[] buf = new byte[TJ.bufSize(width, height, subsamp)]; - compress(srcImage, buf, flags); - return buf; + setSourceImage(srcImage, 0, 0, 0, 0); + return compress(flags); } @@ -424,12 +448,20 @@ public class TJCompressor { public void encodeYUV(byte[] dstBuf, int flags) throws Exception { if (dstBuf == null || flags < 0) throw new Exception("Invalid argument in compress()"); - if (srcBuf == null) + if (srcBuf == null && srcBufInt == null) throw new Exception(NO_ASSOC_ERROR); + if (srcIsYUV) + throw new Exception("Source image is not correct type"); if (subsamp < 0) throw new Exception("Subsampling level not set"); - encodeYUV(srcBuf, srcWidth, srcPitch, srcHeight, srcPixelFormat, dstBuf, - yuvPad, subsamp, flags); + + if (srcBufInt != null) { + encodeYUV(srcBufInt, srcWidth, srcStride, srcHeight, srcPixelFormat, + dstBuf, yuvPad, subsamp, flags); + } else { + encodeYUV(srcBuf, srcWidth, srcPitch, srcHeight, srcPixelFormat, dstBuf, + yuvPad, subsamp, flags); + } compressedSize = TJ.bufSizeYUV(srcWidth, yuvPad, srcHeight, subsamp); } @@ -453,98 +485,26 @@ public class TJCompressor { } /** - * Encode the uncompressed source image stored in srcImage - * and output a YUV planar image to the given destination buffer. See - * {@link #encodeYUV(byte[], int)} for more detail. - * - * @param srcImage a BufferedImage instance containing RGB or - * grayscale pixels to be encoded - * - * @param dstBuf buffer that will receive the YUV planar image. Use - * {@link TJ#bufSizeYUV} to determine the appropriate size for this buffer - * based on the image width, height, and level of chrominance subsampling. - * - * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*} + * @deprecated Use + * {@link #setSourceImage(BufferedImage, int, int, int, int)} and + * {@link #encodeYUV(byte[], int)} instead. */ + @Deprecated public void encodeYUV(BufferedImage srcImage, byte[] dstBuf, int flags) throws Exception { - if (srcImage == null || dstBuf == null || flags < 0) - throw new Exception("Invalid argument in encodeYUV()"); - int width = srcImage.getWidth(); - int height = srcImage.getHeight(); - int pixelFormat; boolean intPixels = false; - if (byteOrder == null) - byteOrder = ByteOrder.nativeOrder(); - switch(srcImage.getType()) { - case BufferedImage.TYPE_3BYTE_BGR: - pixelFormat = TJ.PF_BGR; break; - case BufferedImage.TYPE_4BYTE_ABGR: - case BufferedImage.TYPE_4BYTE_ABGR_PRE: - pixelFormat = TJ.PF_XBGR; break; - case BufferedImage.TYPE_BYTE_GRAY: - pixelFormat = TJ.PF_GRAY; break; - case BufferedImage.TYPE_INT_BGR: - if (byteOrder == ByteOrder.BIG_ENDIAN) - pixelFormat = TJ.PF_XBGR; - else - pixelFormat = TJ.PF_RGBX; - intPixels = true; break; - case BufferedImage.TYPE_INT_RGB: - case BufferedImage.TYPE_INT_ARGB: - case BufferedImage.TYPE_INT_ARGB_PRE: - if (byteOrder == ByteOrder.BIG_ENDIAN) - pixelFormat = TJ.PF_XRGB; - else - pixelFormat = TJ.PF_BGRX; - intPixels = true; break; - default: - throw new Exception("Unsupported BufferedImage format"); - } - WritableRaster wr = srcImage.getRaster(); - if (subsamp < 0) throw new Exception("Subsampling level not set"); - if (intPixels) { - SinglePixelPackedSampleModel sm = - (SinglePixelPackedSampleModel)srcImage.getSampleModel(); - int stride = sm.getScanlineStride(); - DataBufferInt db = (DataBufferInt)wr.getDataBuffer(); - int[] buf = db.getData(); - encodeYUV(buf, width, stride, height, pixelFormat, dstBuf, yuvPad, - subsamp, flags); - } else { - ComponentSampleModel sm = - (ComponentSampleModel)srcImage.getSampleModel(); - int pixelSize = sm.getPixelStride(); - if (pixelSize != TJ.getPixelSize(pixelFormat)) - throw new Exception("Inconsistency between pixel format and pixel size in BufferedImage"); - int pitch = sm.getScanlineStride(); - DataBufferByte db = (DataBufferByte)wr.getDataBuffer(); - byte[] buf = db.getData(); - encodeYUV(buf, width, pitch, height, pixelFormat, dstBuf, yuvPad, - subsamp, flags); - } - compressedSize = TJ.bufSizeYUV(width, yuvPad, height, subsamp); + setSourceImage(srcImage, 0, 0, 0, 0); + encodeYUV(dstBuf, flags); } /** - * Encode the uncompressed source image stored in srcImage - * and return a buffer containing a YUV planar image. See - * {@link #encodeYUV(byte[], int)} for more detail. - * - * @param srcImage a BufferedImage instance containing RGB or - * grayscale pixels to be encoded - * - * @param flags the bitwise OR of one or more of {@link TJ TJ.FLAG_*} - * - * @return a buffer containing a YUV planar image + * @deprecated Use + * {@link #setSourceImage(BufferedImage, int, int, int, int)} and + * {@link #encodeYUV(int)} instead. */ + @Deprecated public byte[] encodeYUV(BufferedImage srcImage, int flags) throws Exception { - if (subsamp < 0) - throw new Exception("Subsampling level not set"); - int width = srcImage.getWidth(); - int height = srcImage.getHeight(); - byte[] buf = new byte[TJ.bufSizeYUV(width, yuvPad, height, subsamp)]; - encodeYUV(srcImage, buf, flags); - return buf; + setSourceImage(srcImage, 0, 0, 0, 0); + return encodeYUV(flags); } /** @@ -621,11 +581,13 @@ public class TJCompressor { private long handle = 0; private byte[] srcBuf = null; + private int[] srcBufInt = null; private int srcWidth = 0; private int srcHeight = 0; private int srcX = -1; private int srcY = -1; private int srcPitch = 0; + private int srcStride = 0; private int srcPixelFormat = -1; private int srcYUVPad = -1; private boolean srcIsYUV;