diff options
author | dcommander <dcommander@632fc199-4ca6-4c93-a231-07263d6284db> | 2011-02-23 20:51:54 +0000 |
---|---|---|
committer | dcommander <dcommander@632fc199-4ca6-4c93-a231-07263d6284db> | 2011-02-23 20:51:54 +0000 |
commit | 08dbc6f3368ae909845f40c1705b785bb3cc04f1 (patch) | |
tree | 326c56228767f43a31609ed95fe0176b4e95af61 /java/TJExample.java | |
parent | 380c31467721b18073e68e93a2c661dadcc77887 (diff) |
Numerous enhancements, including using the new BufferedImage support to read/write non-JPEG files
git-svn-id: svn://svn.code.sf.net/p/libjpeg-turbo/code/trunk@434 632fc199-4ca6-4c93-a231-07263d6284db
Diffstat (limited to 'java/TJExample.java')
-rw-r--r-- | java/TJExample.java | 190 |
1 files changed, 137 insertions, 53 deletions
diff --git a/java/TJExample.java b/java/TJExample.java index 4db7a63..7e64d2e 100644 --- a/java/TJExample.java +++ b/java/TJExample.java @@ -32,84 +32,168 @@ */ import java.io.*; +import java.awt.image.*; +import javax.imageio.*; import org.libjpegturbo.turbojpeg.*; public class TJExample { - public static final String classname=new TJExample().getClass().getName(); + public static final String classname = new TJExample().getClass().getName(); private static void usage() { - System.out.println("\nUSAGE: java "+classname+" <Input file> <Output file> [options]\n"); + System.out.println("\nUSAGE: java " + classname + " <Input file> <Output file> [options]\n"); + System.out.println("Input and output files can be any image format that the Java Image I/O"); + System.out.println("extensions understand. If either filename ends in a .jpg extension, then"); + System.out.println("TurboJPEG will be used to compress or decompress the file.\n"); System.out.println("Options:\n"); - System.out.println("-scale 1/N = scale the width/height of the output image by a factor of 1/N"); - System.out.println(" (N = 1, 2, 4, or 8}\n"); + System.out.println("-scale 1/N = if the input image is a JPEG file, scale the width/height of the"); + System.out.println(" output image by a factor of 1/N (N = 1, 2, 4, or 8}\n"); + System.out.println("-samp <444|422|420|gray> = If the output image is a JPEG file, this specifies"); + System.out.println(" the level of chrominance subsampling to use when"); + System.out.println(" recompressing it. Default is to use the same level"); + System.out.println(" of subsampling as the input, if the input is a JPEG"); + System.out.println(" file, or 4:4:4 otherwise.\n"); + System.out.println("-q <1-100> = If the output image is a JPEG file, this specifies the JPEG"); + System.out.println(" quality to use when recompressing it (default = 95).\n"); System.exit(1); } + private final static String sampName[] = { + "4:4:4", "4:2:2", "4:2:0", "Grayscale" + }; + public static void main(String argv[]) { + BufferedImage img = null; byte [] bmpBuf = null; + try { - if(argv.length<2) { + if(argv.length < 2) { usage(); } - int scalefactor=1; - if(argv.length>2) { - for(int i=2; i<argv.length; i++) { - if(argv[i].equalsIgnoreCase("-scale") && i<argv.length-1) { - String [] scalearg=argv[++i].split("/"); - if(scalearg.length!=2 || Integer.parseInt(scalearg[0])!=1 - || (scalefactor=Integer.parseInt(scalearg[1]))<1 - || scalefactor>8 || (scalefactor&(scalefactor-1))!=0) - usage(); + int scaleFactor = 1; + String inFormat = "jpg", outFormat = "jpg"; + int outSubsamp = -1, outQual = 95; + + if(argv.length > 2) { + for(int i = 2; i < argv.length; i++) { + if(argv[i].length() < 2) continue; + if(argv[i].length() > 2 + && argv[i].substring(0, 3).equalsIgnoreCase("-sc")) { + if(i < argv.length - 1) { + String [] scaleArg = argv[++i].split("/"); + if(scaleArg.length != 2 || Integer.parseInt(scaleArg[0]) != 1 + || (scaleFactor = Integer.parseInt(scaleArg[1])) < 1 + || scaleFactor > 8 || (scaleFactor & (scaleFactor - 1)) != 0) + usage(); + } + else usage(); + } + if(argv[i].substring(0, 2).equalsIgnoreCase("-h") + || argv[i].equalsIgnoreCase("-?")) + usage(); + if(argv[i].length() > 2 + && argv[i].substring(0, 3).equalsIgnoreCase("-sa")) { + if(i < argv.length - 1) { + i++; + if(argv[i].substring(0, 1).equalsIgnoreCase("g")) + outSubsamp = TJ.SAMP_GRAY; + else if(argv[i].equals("444")) outSubsamp = TJ.SAMP_444; + else if(argv[i].equals("422")) outSubsamp = TJ.SAMP_422; + else if(argv[i].equals("420")) outSubsamp = TJ.SAMP_420; + else usage(); + } + else usage(); + } + if(argv[i].substring(0, 2).equalsIgnoreCase("-q")) { + if(i < argv.length - 1) { + int qual = Integer.parseInt(argv[++i]); + if(qual >= 1 && qual <= 100) outQual = qual; + else usage(); + } + else usage(); } } } + String [] inFileTokens = argv[0].split("\\."); + if(inFileTokens.length > 1) + inFormat = inFileTokens[inFileTokens.length - 1]; + String [] outFileTokens = argv[1].split("\\."); + if(outFileTokens.length > 1) + outFormat = outFileTokens[outFileTokens.length - 1]; + + File file = new File(argv[0]); + int width, height, subsamp = TJ.SAMP_444; + + if(inFormat.equalsIgnoreCase("jpg")) { + FileInputStream fis = new FileInputStream(file); + int inputSize = fis.available(); + if(inputSize < 1) { + System.out.println("Input file contains no data"); + System.exit(1); + } + byte [] inputBuf = new byte[inputSize]; + fis.read(inputBuf); + fis.close(); + + TJDecompressor tjd = new TJDecompressor(inputBuf); + width = tjd.getWidth(); + height = tjd.getHeight(); + int inSubsamp = tjd.getSubsamp(); + System.out.println("Source Image: " + width + " x " + height + + " pixels, " + sampName[inSubsamp] + " subsampling"); + if(outSubsamp < 0) outSubsamp = inSubsamp; + + if(scaleFactor != 1) { + width = (width + scaleFactor - 1)/scaleFactor; + height = (height + scaleFactor - 1)/scaleFactor; + } - File file=new File(argv[0]); - FileInputStream fis=new FileInputStream(file); - int inputsize=fis.available(); - if(inputsize<1) { - System.out.println("Input file contains no data"); - System.exit(1); + if(!outFormat.equalsIgnoreCase("jpg")) { + img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + tjd.decompress(img, 0); + } + else bmpBuf = tjd.decompress(width, 0, height, TJ.PF_BGRX, 0); + tjd.close(); } - byte [] inputbuf=new byte[inputsize]; - fis.read(inputbuf); - fis.close(); - - TJDecompressor tjd=new TJDecompressor(inputbuf); - int width=tjd.getWidth(); - int height=tjd.getHeight(); - int subsamp=tjd.getSubsamp(); - System.out.print("Source Image: "+width+" x "+height+" pixels, "); - switch(subsamp) { - case TJ.SAMP_444: System.out.println("4:4:4 subsampling"); break; - case TJ.SAMP_422: System.out.println("4:2:2 subsampling"); break; - case TJ.SAMP_420: System.out.println("4:2:0 subsampling"); break; - case TJ.SAMP_GRAY: System.out.println("Grayscale"); break; - default: System.out.println("Unknown subsampling"); break; + else { + img = ImageIO.read(file); + width = img.getWidth(); + height = img.getHeight(); + if(outSubsamp < 0) { + if(img.getType() == BufferedImage.TYPE_BYTE_GRAY) + outSubsamp = TJ.SAMP_GRAY; + else outSubsamp = TJ.SAMP_444; + } } + System.out.print("Dest. Image (" + outFormat + "): " + width + " x " + + height + " pixels"); + + if(outFormat.equalsIgnoreCase("jpg")) { + System.out.println(", " + sampName[outSubsamp] + + " subsampling, quality = " + outQual); + TJCompressor tjc = new TJCompressor(); + int jpegSize; + byte [] jpegBuf = new byte[TJ.bufSize(width, height)]; + + if(img != null) + jpegSize = tjc.compress(img, jpegBuf, outSubsamp, outQual, 0); + else { + tjc.setBitmapBuffer(bmpBuf, width, 0, height, TJ.PF_BGRX); + jpegSize = tjc.compress(jpegBuf, outSubsamp, outQual, 0); + } + tjc.close(); - if(scalefactor!=1) { - width=(width+scalefactor-1)/scalefactor; - height=(height+scalefactor-1)/scalefactor; - System.out.println("Dest. Image: "+width+" x "+height - +" pixels"); + file = new File(argv[1]); + FileOutputStream fos = new FileOutputStream(file); + fos.write(jpegBuf, 0, jpegSize); + fos.close(); + } + else { + file = new File(argv[1]); + ImageIO.write(img, outFormat, file); } - - byte [] tmpbuf=tjd.decompress(width, 0, height, TJ.PF_BGR, TJ.BOTTOMUP); - tjd.close(); - - TJCompressor tjc=new TJCompressor(tmpbuf, width, 0, height, TJ.PF_BGR); - byte [] outputbuf=new byte[TJ.bufSize(width, height)]; - int outputsize=tjc.compress(outputbuf, subsamp, 95, TJ.BOTTOMUP); - tjc.close(); - - file=new File(argv[1]); - FileOutputStream fos=new FileOutputStream(file); - fos.write(outputbuf, 0, outputsize); - fos.close(); } catch(Exception e) { System.out.println(e); |