in java/TJExample.java [147:398]
public static void main(String[] argv) {
try {
TJScalingFactor scalingFactor = TJ.UNSCALED;
int outSubsamp = -1, outQual = -1;
TJTransform xform = new TJTransform();
boolean display = false, fastUpsample = false, fastDCT = false;
int width, height;
String inFormat = "jpg", outFormat = "jpg";
BufferedImage img = null;
byte[] imgBuf = null;
if (argv.length < 2)
usage();
if (argv[1].substring(0, 2).equalsIgnoreCase("-d"))
display = true;
/* Parse arguments. */
for (int i = 2; i < argv.length; i++) {
if (argv[i].length() < 2)
continue;
else if (argv[i].length() > 2 &&
argv[i].substring(0, 3).equalsIgnoreCase("-sc") &&
i < argv.length - 1) {
int match = 0;
String[] scaleArg = argv[++i].split("/");
if (scaleArg.length == 2) {
TJScalingFactor tempsf =
new TJScalingFactor(Integer.parseInt(scaleArg[0]),
Integer.parseInt(scaleArg[1]));
for (int j = 0; j < SCALING_FACTORS.length; j++) {
if (tempsf.equals(SCALING_FACTORS[j])) {
scalingFactor = SCALING_FACTORS[j];
match = 1;
break;
}
}
}
if (match != 1)
usage();
} else if (argv[i].length() > 2 &&
argv[i].substring(0, 3).equalsIgnoreCase("-su") &&
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 if (argv[i].substring(0, 2).equalsIgnoreCase("-q") &&
i < argv.length - 1) {
outQual = Integer.parseInt(argv[++i]);
if (outQual < 1 || outQual > 100)
usage();
} else if (argv[i].substring(0, 2).equalsIgnoreCase("-g"))
xform.options |= TJTransform.OPT_GRAY;
else if (argv[i].equalsIgnoreCase("-hflip"))
xform.op = TJTransform.OP_HFLIP;
else if (argv[i].equalsIgnoreCase("-vflip"))
xform.op = TJTransform.OP_VFLIP;
else if (argv[i].equalsIgnoreCase("-transpose"))
xform.op = TJTransform.OP_TRANSPOSE;
else if (argv[i].equalsIgnoreCase("-transverse"))
xform.op = TJTransform.OP_TRANSVERSE;
else if (argv[i].equalsIgnoreCase("-rot90"))
xform.op = TJTransform.OP_ROT90;
else if (argv[i].equalsIgnoreCase("-rot180"))
xform.op = TJTransform.OP_ROT180;
else if (argv[i].equalsIgnoreCase("-rot270"))
xform.op = TJTransform.OP_ROT270;
else if (argv[i].equalsIgnoreCase("-custom"))
xform.cf = new TJExample();
else if (argv[i].length() > 2 &&
argv[i].substring(0, 2).equalsIgnoreCase("-c") &&
i < argv.length - 1) {
String[] cropArg = argv[++i].split("[x\\+]");
if (cropArg.length != 4)
usage();
xform.width = Integer.parseInt(cropArg[0]);
xform.height = Integer.parseInt(cropArg[1]);
xform.x = Integer.parseInt(cropArg[2]);
xform.y = Integer.parseInt(cropArg[3]);
if (xform.x < 0 || xform.y < 0 || xform.width < 1 ||
xform.height < 1)
usage();
xform.options |= TJTransform.OPT_CROP;
} else if (argv[i].substring(0, 2).equalsIgnoreCase("-d"))
display = true;
else if (argv[i].equalsIgnoreCase("-fastupsample")) {
System.out.println("Using fast upsampling code");
fastUpsample = true;
} else if (argv[i].equalsIgnoreCase("-fastdct")) {
System.out.println("Using fastest DCT/IDCT algorithm");
fastDCT = true;
} else usage();
}
/* Determine input and output image formats based on file extensions. */
String[] inFileTokens = argv[0].split("\\.");
if (inFileTokens.length > 1)
inFormat = inFileTokens[inFileTokens.length - 1];
String[] outFileTokens;
if (display)
outFormat = "bmp";
else {
outFileTokens = argv[1].split("\\.");
if (outFileTokens.length > 1)
outFormat = outFileTokens[outFileTokens.length - 1];
}
if (inFormat.equalsIgnoreCase("jpg")) {
/* Input image is a JPEG image. Decompress and/or transform it. */
boolean doTransform = (xform.op != TJTransform.OP_NONE ||
xform.options != 0 || xform.cf != null);
/* Read the JPEG file into memory. */
File jpegFile = new File(argv[0]);
FileInputStream fis = new FileInputStream(jpegFile);
int jpegSize = fis.available();
if (jpegSize < 1) {
System.out.println("Input file contains no data");
System.exit(1);
}
byte[] jpegBuf = new byte[jpegSize];
fis.read(jpegBuf);
fis.close();
TJDecompressor tjd;
if (doTransform) {
/* Transform it. */
TJTransformer tjt = new TJTransformer(jpegBuf);
TJTransform[] xforms = new TJTransform[1];
xforms[0] = xform;
xforms[0].options |= TJTransform.OPT_TRIM;
TJDecompressor[] tjds = tjt.transform(xforms);
tjd = tjds[0];
tjt.close();
} else
tjd = new TJDecompressor(jpegBuf);
tjd.set(TJ.PARAM_FASTUPSAMPLE, fastUpsample ? 1 : 0);
tjd.set(TJ.PARAM_FASTDCT, fastDCT ? 1 : 0);
width = tjd.getWidth();
height = tjd.getHeight();
int inSubsamp = tjd.get(TJ.PARAM_SUBSAMP);
int inColorspace = tjd.get(TJ.PARAM_COLORSPACE);
if (tjd.get(TJ.PARAM_LOSSLESS) == 1)
scalingFactor = TJ.UNSCALED;
System.out.println((doTransform ? "Transformed" : "Input") +
" Image (jpg): " + width + " x " + height +
" pixels, " + SUBSAMP_NAME[inSubsamp] +
" subsampling, " + COLORSPACE_NAME[inColorspace]);
if (outFormat.equalsIgnoreCase("jpg") && doTransform &&
scalingFactor.isOne() && outSubsamp < 0 && outQual < 0) {
/* Input image has been transformed, and no re-compression options
have been selected. Write the transformed image to disk and
exit. */
File outFile = new File(argv[1]);
FileOutputStream fos = new FileOutputStream(outFile);
fos.write(tjd.getJPEGBuf(), 0, tjd.getJPEGSize());
fos.close();
System.exit(0);
}
/* Scaling and/or a non-JPEG output image format and/or compression
options have been selected, so we need to decompress the
input/transformed image. */
tjd.setScalingFactor(scalingFactor);
width = scalingFactor.getScaled(width);
height = scalingFactor.getScaled(height);
if (outSubsamp < 0)
outSubsamp = inSubsamp;
if (!outFormat.equalsIgnoreCase("jpg"))
img = tjd.decompress8(BufferedImage.TYPE_INT_RGB);
else
imgBuf = tjd.decompress8(0, TJ.PF_BGRX);
tjd.close();
} else {
/* Input image is not a JPEG image. Load it into memory. */
img = ImageIO.read(new File(argv[0]));
if (img == null)
throw new Exception("Input image type not supported.");
width = img.getWidth();
height = img.getHeight();
if (outSubsamp < 0) {
if (img.getType() == BufferedImage.TYPE_BYTE_GRAY)
outSubsamp = TJ.SAMP_GRAY;
else
outSubsamp = DEFAULT_SUBSAMP;
}
System.out.println("Input Image: " + width + " x " + height +
" pixels");
}
System.gc();
if (!display)
System.out.print("Output Image (" + outFormat + "): " + width +
" x " + height + " pixels");
if (display) {
/* Display the uncompressed image */
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "Output Image",
JOptionPane.PLAIN_MESSAGE);
} else if (outFormat.equalsIgnoreCase("jpg")) {
/* Output image format is JPEG. Compress the uncompressed image. */
if (outQual < 0)
outQual = DEFAULT_QUALITY;
System.out.println(", " + SUBSAMP_NAME[outSubsamp] +
" subsampling, quality = " + outQual);
TJCompressor tjc = new TJCompressor();
tjc.set(TJ.PARAM_SUBSAMP, outSubsamp);
tjc.set(TJ.PARAM_QUALITY, outQual);
tjc.set(TJ.PARAM_FASTDCT, fastDCT ? 1 : 0);
if (img != null)
tjc.setSourceImage(img, 0, 0, 0, 0);
else
tjc.setSourceImage(imgBuf, 0, 0, width, 0, height, TJ.PF_BGRX);
byte[] jpegBuf = tjc.compress();
int jpegSize = tjc.getCompressedSize();
tjc.close();
/* Write the JPEG image to disk. */
File outFile = new File(argv[1]);
FileOutputStream fos = new FileOutputStream(outFile);
fos.write(jpegBuf, 0, jpegSize);
fos.close();
} else {
/* Output image format is not JPEG. Save the uncompressed image
directly to disk. */
System.out.print("\n");
File outFile = new File(argv[1]);
ImageIO.write(img, outFormat, outFile);
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}