Search in sources :

Example 1 with ImageSize

use of digilib.util.ImageSize in project digilib by robcast.

the class ImageJobDescription method getWy.

/**
 * Return the relative y-offset of the image area.
 * Uses wy parameter.
 * Converts wy in pixels to relative.
 *
 * @return
 * @throws IOException
 */
public Float getWy() throws IOException {
    // logger.debug("get_paramWY()");
    if (paramWY == null) {
        paramWY = getAsFloat("wy");
        if (hasOption(DigilibOption.pxarea)) {
            // area in absolute pixels - convert to relative
            ImageSize imgSize = getHiresSize();
            paramWY = paramWY / imgSize.getHeight();
        } else if (hasOption(DigilibOption.sqarea)) {
            // square full size area
            hiresSize = getHiresSize();
            float aspect = hiresSize.getAspect();
            if (aspect < 1) {
                // portrait
                paramWY = (1f - aspect) / 2f;
            } else {
                // landscape
                paramWY = 0f;
            }
        }
    }
    return paramWY;
}
Also used : ImageSize(digilib.util.ImageSize)

Example 2 with ImageSize

use of digilib.util.ImageSize in project digilib by robcast.

the class ImageJobDescription method isTransformRequired.

/**
 * Returns if any transformation of the source image (image manipulation or
 * format conversion) is required.
 *
 * @return
 * @throws IOException
 */
public boolean isTransformRequired() throws IOException {
    ImageSize is = getInput().getSize();
    ImageSize ess = getMinSourceSize();
    // does the image require processing?
    if (isImageSendable()) {
        // does the image require rescaling?
        if (isLoresOnly() && is.isSmallerThan(ess)) {
            // lores: send even if smaller
            return false;
        } else if (is.fitsIn(ess)) {
            // send if it fits
            return false;
        }
    }
    return true;
}
Also used : ImageSize(digilib.util.ImageSize)

Example 3 with ImageSize

use of digilib.util.ImageSize in project digilib by robcast.

the class ImageJobDescription method prepareSqueezeToFit.

/**
 * Squeeze to fit: scale factor based on destination size and user area.
 *
 * Uses separate scale factors for x and y to fill destination size changing aspect ratio.
 *
 * Sets ScaleX and ScaleY.
 */
protected Rectangle2D prepareSqueezeToFit() throws IOException {
    /*
         * calculate minimum source size
         * 
         * w_min = dw * 1/ww
         */
    minSourceSize = new ImageSize(Math.round(getAsInt("dw") / getWw()), Math.round(getAsInt("dh") / getWh()));
    /*
         * get image region of interest
         */
    // size of the currently selected input image (uses minSourceSize)
    imgSize = getImgSize();
    // transform from relative [0,1] to image coordinates.
    double areaXf = getWx() * imgSize.getWidth();
    double areaYf = getWy() * imgSize.getHeight();
    double areaWidthF = getWw() * imgSize.getWidth();
    double areaHeightF = getWh() * imgSize.getHeight();
    // round to pixels
    long areaX = Math.round(areaXf);
    long areaY = Math.round(areaYf);
    long areaHeight = Math.round(areaHeightF);
    long areaWidth = Math.round(areaWidthF);
    /*
         * calculate scale factors
         */
    scaleX = getDw() / (double) areaWidth;
    scaleY = getDh() / (double) areaHeight;
    return new Rectangle2D.Double(areaX, areaY, areaWidth, areaHeight);
}
Also used : ImageSize(digilib.util.ImageSize)

Example 4 with ImageSize

use of digilib.util.ImageSize in project digilib by robcast.

the class ImageJobDescription method prepareScaleToFit.

/**
 * Scale to fit: scale factor based on destination size dw/dh and user area.
 *
 * Uses a uniform scale factor for x and y.
 *
 * Sets ScaleX and ScaleY.
 */
protected Rectangle2D prepareScaleToFit() throws IOException {
    /*
         * prepare minimum source image size
         * 
         * minSourceSize: w_min = dw * 1/ww
         * 
         * Note: dw or dh can be empty (=0) 
         */
    float scale = (1 / Math.min(getWw(), getWh()));
    minSourceSize = new ImageSize(Math.round(getAsInt("dw") * scale), Math.round(getAsInt("dh") * scale));
    /*
         * get image region of interest
         */
    // size of the currently selected input image (uses minSourceSize)
    imgSize = getImgSize();
    // transform from relative [0,1] to image coordinates.
    double areaXf = getWx() * imgSize.getWidth();
    double areaYf = getWy() * imgSize.getHeight();
    double areaWidthF = getWw() * imgSize.getWidth();
    double areaHeightF = getWh() * imgSize.getHeight();
    // round to pixels
    long areaX = Math.round(areaXf);
    long areaY = Math.round(areaYf);
    long areaHeight = Math.round(areaHeightF);
    long areaWidth = Math.round(areaWidthF);
    /*
         * calculate scale factors
         */
    scaleX = getDw() / (double) areaWidth;
    scaleY = getDh() / (double) areaHeight;
    if (scaleX == 0) {
        // dw undefined
        scaleX = scaleY;
    } else if (scaleY == 0) {
        // dh undefined
        scaleY = scaleX;
    } else if (hasOption(DigilibOption.crop)) {
        // use the bigger factor to get fill-the-box
        if (scaleX > scaleY) {
            scaleY = scaleX;
            // crop mode uses whole destination rect
            long croppedAreaHeight = Math.round(getDh() / scaleY);
            if (areaHeight > croppedAreaHeight) {
                // center cropped area
                areaY += (areaHeight - croppedAreaHeight) / 2;
            }
            areaHeight = croppedAreaHeight;
            // re-compute scaleY
            scaleY = getDh() / (double) areaHeight;
        } else {
            scaleX = scaleY;
            // crop mode uses whole destination rect
            long croppedAreaWidth = Math.round(getDw() / scaleX);
            if (areaWidth > croppedAreaWidth) {
                // center cropped area
                areaX += (areaWidth - croppedAreaWidth) / 2;
            }
            areaWidth = croppedAreaWidth;
            // re-compute scaleX
            scaleX = getDw() / (double) areaWidth;
        }
    } else {
        // use the smaller factor to get fit-in-box
        if (scaleX > scaleY) {
            scaleX = scaleY;
            if (hasOption(DigilibOption.fill)) {
                // fill mode uses whole destination rect
                long filledAreaWidth = Math.round(getDw() / scaleX);
                if (filledAreaWidth > areaWidth) {
                    // center filled area
                    areaX -= (filledAreaWidth - areaWidth) / 2;
                }
                areaWidth = filledAreaWidth;
                // re-compute scaleX
                scaleX = getDw() / (double) areaWidth;
            }
        } else {
            scaleY = scaleX;
            if (hasOption(DigilibOption.fill)) {
                // fill mode uses whole destination rect
                long filledAreaHeight = Math.round(getDh() / scaleY);
                if (filledAreaHeight > areaHeight) {
                    // center filled area
                    areaY -= (filledAreaHeight - areaHeight) / 2;
                }
                areaHeight = filledAreaHeight;
                // re-compute scaleY
                scaleY = getDh() / (double) areaHeight;
            }
        }
    }
    return new Rectangle2D.Double(areaX, areaY, areaWidth, areaHeight);
}
Also used : ImageSize(digilib.util.ImageSize)

Example 5 with ImageSize

use of digilib.util.ImageSize in project digilib by robcast.

the class ImageJobDescription method getOuterImgArea.

/**
 * Return the maximum area of the source image that will be used.
 *
 * This was meant to include extra pixels outside the
 * imgArea when rotating by oblique angles but is not yet implemented.
 * Currently returns imgArea.
 *
 * @return
 * @throws IOException
 * @throws ImageOpException
 */
public Rectangle2D getOuterImgArea() throws IOException, ImageOpException {
    if (outerImgArea == null) {
        // calculate scale parameters
        if (imgArea == null) {
            prepareScaleParams();
        }
        // start with imgArea
        outerImgArea = imgArea;
        // image size in pixels
        ImageSize imgSize = getInput().getSize();
        Rectangle2D imgBounds = new Rectangle2D.Double(0, 0, imgSize.getWidth(), imgSize.getHeight());
        // clip area at the image border
        outerImgArea = outerImgArea.createIntersection(imgBounds);
        // check image parameters sanity
        if ((outerImgArea.getWidth() < 1) || (outerImgArea.getHeight() < 1) || (scaleX * outerImgArea.getWidth() < 2) || (scaleY * outerImgArea.getHeight() < 2)) {
            logger.error("ERROR: invalid scale parameter set!");
            logger.debug("scaleX=" + scaleX + " scaleY=" + scaleY + " outerImgArea=" + outerImgArea);
            throw new ImageOpException("Invalid scale parameter set!");
        }
    }
    return outerImgArea;
}
Also used : ImageSize(digilib.util.ImageSize) Rectangle2D(java.awt.geom.Rectangle2D)

Aggregations

ImageSize (digilib.util.ImageSize)18 ImageInput (digilib.io.ImageInput)6 IOException (java.io.IOException)5 FileOpException (digilib.io.FileOpException)3 ImageSet (digilib.io.ImageSet)2 DocuDirectory (digilib.io.DocuDirectory)1 DocuDirent (digilib.io.DocuDirent)1 ImageFileSet (digilib.io.ImageFileSet)1 Rectangle2D (java.awt.geom.Rectangle2D)1 RandomAccessFile (java.io.RandomAccessFile)1 JsonGenerator (javax.json.stream.JsonGenerator)1 RenderedOp (javax.media.jai.RenderedOp)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 FormatException (loci.formats.FormatException)1 ImageReader (loci.formats.ImageReader)1 BufferedImageReader (loci.formats.gui.BufferedImageReader)1 ImageInfo (org.devlib.schmidt.imageinfo.ImageInfo)1