try {
String param;
InputStream ins = new BufferInputStream(getStdIn());
BufferedImage image = null;
boolean useCache = false;
// Determine the scale.
double scale;
double scaleX;
double scaleY;
//log.debug("width: " + width);
//log.debug("height: " + height);
//log.debug("maxWidth: " + maxWidth);
//log.debug("maxHeight: " + maxHeight);
// if ((width == -1) && (height == -1)) {
// width = maxWidth;
// height = maxHeight;
// }
// READ IMAGE
if (inType.equals(IMG_TYPE)) {
image = ImageIO.read(ins);
if (image == null) {
throwException("No image retrieved from the pipeline!");
return;
}
} else { //OBJ_TYPE
BufferObjectReader oin = new BufferObjectReader(getStdIn());
if (oin.hasNext()) {
image = (BufferedImage) oin.next();
}
oin.close();
if (image == null) {
throwException("No image object retrieved from the pipeline!");
return;
}
}
if (!useCache) {
int imgW = image.getWidth(null);
int imgH = image.getHeight(null);
//log.debug("image width: " + imgW);
//log.debug("image height: " + imgH);
float iRatio = imgW / (float) imgH;
// if request for fullsize image truncate width and height to maximums
if ((width == -1) && (height == -1)) {
if (maxWidth != -1 && imgW > maxWidth) width = maxWidth;
if (maxHeight != -1 && imgH > maxHeight) height = maxHeight;
}
//log.debug("width: " + width);
//log.debug("height: " + height);
// if max requested height is known calculate aspect ratio width.
// If it is greater than the width specified set width with it.
if (height != -1) {
int altwidth = (int) (height * iRatio);
//log.debug("altwidth: " + altwidth);
//if(altwidth>width && width!=-1) width=altwidth;
if (altwidth > width) width = altwidth;
}
// if max requested width is known calculate aspect ratio height.
// If it is greater than the height set the height width it.
if (width != -1) {
int altheight = (int) (width / iRatio);
//log.debug("altheight: " + altheight);
//if(altheight>height && height!=-1) height=altheight;
if (altheight > height) height = altheight;
}
//log.debug("After ratio check: ");
//log.debug("width: " + width);
//log.debug("height: " + height);
if ((width > maxWidth) && (maxWidth != -1)) {
width = maxWidth;
//log.debug("Truncated width to maxWidth: " + width);
height = (int) (width / iRatio);
//log.debug("Adapted height to : " + height);
}
if (width > imgW) {
width = imgW;
//log.debug("Truncated width to img Width: " + width);
height = (int) (width / iRatio);
//log.debug("Adapted height to : " + height);
}
if (((height == -1) || (height > maxHeight)) && (maxHeight != -1)) {
height = maxHeight;
//log.debug("Truncated height to maxHeight: " + height);
width = (int) (height * iRatio);
//log.debug("Adapted width to : " + width);
}
if (height > imgH) {
height = imgH;
//log.debug("Truncated height to img Height: " + height);
width = (int) (height * iRatio);
//log.debug("Adapted width to : " + width);
}
//log.debug("width: " + width);
//log.debug("height: " + height);
if ((imgW > imgH) || (height == -1)) {
scale = (double) width / (double) imgW;
} else {
scale = (double) height / (double) imgH;
}
int scaledW = (int) (scale * imgW);
int scaledH = (int) (scale * imgH);
// Shit. No cache. Do some resizing.
/*
* response.setHeader("Cache-Control", "post-check=900,pre-check=3600");
* response.addHeader("Expires", dt(timeout) );
* response.addHeader("Last-Modified", dt(0) );
* response.setHeader("Date", dt(0) );
*/
// fetch the image
// Create an image buffer in which to paint on.
BufferedImage newImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
// Set the scale.
AffineTransform tx = new AffineTransform();
// If the image is smaller than the desired image size,
// don't bother scaling.
if (scale < 1.0d) {
tx.scale(scale, scale);
}
// Paint image.
Graphics2D g2d = newImage.createGraphics();
//RenderingHints rh = new RenderingHints(java.awt.RenderingHints.KEY_ANTIALIASING,java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
RenderingHints rh = new RenderingHints(java.awt.RenderingHints.KEY_INTERPOLATION, java.awt.RenderingHints.VALUE_INTERPOLATION_BICUBIC);
//rh.add(java.awt.RenderingHints.KEY_ANTIALIASING,java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(rh);
g2d.drawImage(image, tx, null);
g2d.dispose();
// WRITE IMAGE
if (outType.equals(IMG_TYPE)) {
BufferOutputStream bos = new BufferOutputStream(getStdOut());
ImageIO.write(newImage, "jpg", bos);
bos.close();
/*
OutputStream os = new BufferedOutputStream(new BufferOutputStream(getStdOut()));
JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(os);
jpegEncoder.encode(newImage);
os.close();
*/
} else { // OBJ_TYPE
BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
oout.writeObject(newImage);
oout.close();
}
return;
}
ins.close();
} catch (Exception e) {
// If an Exception occurs, return the error to the client.
if (canThrowEx()) {
throw new RuntimeException("Invalid maxHeight.");