Numerous enhancements, including using the new BufferedImage support to read/write non-JPEG files

git-svn-id: svn+ssh://svn.code.sf.net/p/libjpeg-turbo/code/trunk@434 632fc199-4ca6-4c93-a231-07263d6284db
This commit is contained in:
DRC
2011-02-23 20:51:54 +00:00
parent b605cac79e
commit 026f7cea77

View File

@@ -32,84 +32,168 @@
*/ */
import java.io.*; import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import org.libjpegturbo.turbojpeg.*; import org.libjpegturbo.turbojpeg.*;
public class TJExample { 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() { 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("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("-scale 1/N = if the input image is a JPEG file, scale the width/height of the");
System.out.println(" (N = 1, 2, 4, or 8}\n"); 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); System.exit(1);
} }
private final static String sampName[] = {
"4:4:4", "4:2:2", "4:2:0", "Grayscale"
};
public static void main(String argv[]) { public static void main(String argv[]) {
BufferedImage img = null; byte [] bmpBuf = null;
try { try {
if(argv.length<2) { if(argv.length < 2) {
usage(); usage();
} }
int scalefactor=1; int scaleFactor = 1;
if(argv.length>2) { String inFormat = "jpg", outFormat = "jpg";
for(int i=2; i<argv.length; i++) { int outSubsamp = -1, outQual = 95;
if(argv[i].equalsIgnoreCase("-scale") && i<argv.length-1) {
String [] scalearg=argv[++i].split("/"); if(argv.length > 2) {
if(scalearg.length!=2 || Integer.parseInt(scalearg[0])!=1 for(int i = 2; i < argv.length; i++) {
|| (scalefactor=Integer.parseInt(scalearg[1]))<1 if(argv[i].length() < 2) continue;
|| scalefactor>8 || (scalefactor&(scalefactor-1))!=0) if(argv[i].length() > 2
usage(); && 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]); File file = new File(argv[0]);
FileInputStream fis=new FileInputStream(file); int width, height, subsamp = TJ.SAMP_444;
int inputsize=fis.available();
if(inputsize<1) { if(inFormat.equalsIgnoreCase("jpg")) {
System.out.println("Input file contains no data"); FileInputStream fis = new FileInputStream(file);
System.exit(1); 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;
}
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]; else {
fis.read(inputbuf); img = ImageIO.read(file);
fis.close(); width = img.getWidth();
height = img.getHeight();
TJDecompressor tjd=new TJDecompressor(inputbuf); if(outSubsamp < 0) {
int width=tjd.getWidth(); if(img.getType() == BufferedImage.TYPE_BYTE_GRAY)
int height=tjd.getHeight(); outSubsamp = TJ.SAMP_GRAY;
int subsamp=tjd.getSubsamp(); else outSubsamp = TJ.SAMP_444;
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;
} }
System.out.print("Dest. Image (" + outFormat + "): " + width + " x "
+ height + " pixels");
if(scalefactor!=1) { if(outFormat.equalsIgnoreCase("jpg")) {
width=(width+scalefactor-1)/scalefactor; System.out.println(", " + sampName[outSubsamp]
height=(height+scalefactor-1)/scalefactor; + " subsampling, quality = " + outQual);
System.out.println("Dest. Image: "+width+" x "+height TJCompressor tjc = new TJCompressor();
+" pixels"); 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();
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) { } catch(Exception e) {
System.out.println(e); System.out.println(e);