Example usage for java.awt.geom Rectangle2D.Float getMaxY

List of usage examples for java.awt.geom Rectangle2D.Float getMaxY

Introduction

In this page you can find the example usage for java.awt.geom Rectangle2D.Float getMaxY.

Prototype

public double getMaxY() 

Source Link

Document

Returns the largest Y coordinate of the framing rectangle of the Shape in double precision.

Usage

From source file:org.photovault.image.PhotovaultImage.java

protected RenderableOp getCropped(RenderableOp uncroppedImage) {
    float origWidth = uncroppedImage.getWidth();
    float origHeight = uncroppedImage.getHeight();

    AffineTransform xform = org.photovault.image.ImageXform.getRotateXform(rot, origWidth, origHeight);

    ParameterBlockJAI rotParams = new ParameterBlockJAI("affine");
    rotParams.addSource(uncroppedImage);
    rotParams.setParameter("transform", xform);
    rotParams.setParameter("interpolation", Interpolation.getInstance(Interpolation.INTERP_NEAREST));
    RenderingHints hints = new RenderingHints(null);
    /* /*w  ww  .  j a  va 2s  . co  m*/
     In practice, JAI optimizes the pipeline so that this trasnformation is
     concatenated with scaling from MultiresolutionImage to desired resolution.
     This transformation obeys KYE_INTERPOLATION hint so we must set it here,
     otherwise nearest neighborough interpolation will be used regardless of
     the settings on parameter block.
     */
    hints.put(JAI.KEY_INTERPOLATION, new InterpolationBilinear());
    rotatedImage = JAI.createRenderable("affine", rotParams, hints);
    rotatedImage.setProperty("org.photovault.opname", "rotated_image");
    /*
     Due to rounding errors in JAI pipeline transformations we have a danger 
     that the crop border goes outside image area. To avoind this, create a
     slightly larger background and overlay the actual image in front of it.         
     */
    RenderableOp background = ScaleDescriptor.createRenderable(rotatedImage, 1.01f, 1.01f, 0.0f, 0.0f,
            Interpolation.getInstance(Interpolation.INTERP_BILINEAR), hints);
    RenderableOp overlayed = OverlayDescriptor.createRenderable(background, rotatedImage, hints);

    ParameterBlockJAI cropParams = new ParameterBlockJAI("crop");
    cropParams.addSource(overlayed);
    float cropWidth = (float) (cropMaxX - cropMinX);
    cropWidth = (cropWidth > 0.000001) ? cropWidth : 0.000001f;
    float cropHeight = (float) (cropMaxY - cropMinY);
    cropHeight = (cropHeight > 0.000001) ? cropHeight : 0.000001f;

    float cropX = (float) (rotatedImage.getMinX() + cropMinX * rotatedImage.getWidth());
    float cropY = (float) (rotatedImage.getMinY() + cropMinY * rotatedImage.getHeight());
    float cropW = cropWidth * rotatedImage.getWidth();
    float cropH = cropHeight * rotatedImage.getHeight();
    Rectangle2D.Float cropRect = new Rectangle2D.Float(cropX, cropY, cropW, cropH);
    Rectangle2D.Float srcRect = new Rectangle2D.Float(rotatedImage.getMinX(), rotatedImage.getMinY(),
            rotatedImage.getWidth(), rotatedImage.getHeight());
    if (!srcRect.contains(cropRect)) {
        // Crop rectangle must fit inside source image
        Rectangle2D.intersect(srcRect, cropRect, cropRect);
        if (!srcRect.contains(cropRect)) {
            // Ups, we have a problem... this can happen due to rounding errors
            // (for example, if cropY is just above zero). 
            // Make the crop rectangle slightly smaller so that the rounding 
            // error is avoided
            final float correction = 5E-3f;
            if (srcRect.getMinX() > cropRect.getMinX()) {
                cropX += correction;
            }
            if (srcRect.getMinY() > cropRect.getMinY()) {
                cropY += correction;
            }
            if (srcRect.getMaxX() < cropRect.getMaxX()) {
                cropW -= correction;
            }
            if (srcRect.getMaxY() < cropRect.getMaxY()) {
                cropH -= correction;
            }
            cropRect = new Rectangle2D.Float(cropX, cropY, cropW, cropH);
            if (!srcRect.contains(cropRect)) {
                // Hmm, now I am out of ideas...
                cropX = rotatedImage.getMinX();
                cropY = rotatedImage.getMinY();
                cropW = rotatedImage.getWidth();
                cropH = rotatedImage.getHeight();
            }
        }
    }

    cropParams.setParameter("x", cropX);
    cropParams.setParameter("y", cropY);
    cropParams.setParameter("width", cropW);
    cropParams.setParameter("height", cropH);
    CropDescriptor cdesc = new CropDescriptor();
    StringBuffer msg = new StringBuffer();
    if (!cdesc.validateArguments("renderable", cropParams, msg)) {
        log.error("Error settings up crop operation: " + msg.toString());
    }

    croppedImage = JAI.createRenderable("crop", cropParams, hints);
    // Translate the image so that it begins in origo
    rotatedImage.setProperty("org.photovault.opname", "cropped_image");
    ParameterBlockJAI pbXlate = new ParameterBlockJAI("translate");
    pbXlate.addSource(croppedImage);
    pbXlate.setParameter("xTrans", (float) (-croppedImage.getMinX()));
    pbXlate.setParameter("yTrans", (float) (-croppedImage.getMinY()));
    xformCroppedImage = JAI.createRenderable("translate", pbXlate);
    rotatedImage.setProperty("org.photovault.opname", "xlated_image");
    return xformCroppedImage;
}