Mercurial > hg > digilib-old
changeset 593:7768ea8f59cf stream
more work on stream input -- cleaning up references to files
author | robcast |
---|---|
date | Fri, 07 Jan 2011 12:00:10 +0100 |
parents | f7e2b6f29b6d |
children | e8668edcb880 |
files | servlet/src/digilib/image/ImageJobDescription.java servlet/src/digilib/image/ImageWorker.java servlet/src/digilib/servlet/Scaler.java |
diffstat | 3 files changed, 123 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- a/servlet/src/digilib/image/ImageJobDescription.java Thu Jan 06 20:53:18 2011 +0100 +++ b/servlet/src/digilib/image/ImageJobDescription.java Fri Jan 07 12:00:10 2011 +0100 @@ -18,15 +18,15 @@ import digilib.util.Parameter; import digilib.util.ParameterMap; - -/** - * A container class for storing a set of instructional parameters - * used for content generating classes like MakePDF. +/** + * A class for storing the set of parameters necessary for scaling images + * with an ImageWorker. * - * This contains the functionality formerly found in Scaler, processRequest, only factorized. + * This contains the functionality formerly found in Scaler.processRequest(), + * only factorized. * * @author cmielack, casties - * + * */ public class ImageJobDescription extends ParameterMap { @@ -34,8 +34,8 @@ DigilibConfiguration dlConfig = null; protected static Logger logger = Logger.getLogger("digilib.servlet"); - ImageInput fileToLoad = null; - ImageSet fileset = null; + ImageInput input = null; + ImageSet imageSet = null; DocuDirectory fileDir = null; String filePath = null; ImageSize expectedSourceSize = null; @@ -43,9 +43,9 @@ Rectangle2D userImgArea = null; Rectangle2D outerUserImgArea = null; Boolean imageSendable = null; - String mimeType; - Integer paramDW; - Integer paramDH; + String mimeType = null; + Integer paramDW = null; + Integer paramDH = null; /** create empty ImageJobDescription. * @param dlcfg @@ -126,44 +126,56 @@ } + /** Returns the mime-type (of the input). + * @return + * @throws IOException + */ public String getMimeType() throws IOException { if (mimeType == null) { - fileToLoad = getFileToLoad(); - mimeType = fileToLoad.getMimetype(); + input = getInput(); + mimeType = input.getMimetype(); } return mimeType; } - public ImageInput getFileToLoad() throws IOException { - if(fileToLoad == null){ - fileset = getFileset(); + /** Returns the ImageInput to use. + * @return + * @throws IOException + */ + public ImageInput getInput() throws IOException { + if(input == null){ + imageSet = getImageSet(); /* select a resolution */ - if (getHiresOnly()) { + if (isHiresOnly()) { // get first element (= highest resolution) - fileToLoad = fileset.getBiggest(); - } else if (getLoresOnly()) { + input = imageSet.getBiggest(); + } else if (isLoresOnly()) { // enforced lores uses next smaller resolution - fileToLoad = fileset.getNextSmaller(getExpectedSourceSize()); - if (fileToLoad == null) { + input = imageSet.getNextSmaller(getExpectedSourceSize()); + if (input == null) { // this is the smallest we have - fileToLoad = fileset.getSmallest(); + input = imageSet.getSmallest(); } } else { // autores: use next higher resolution - fileToLoad = fileset.getNextBigger(getExpectedSourceSize()); - if (fileToLoad == null) { + input = imageSet.getNextBigger(getExpectedSourceSize()); + if (input == null) { // this is the highest we have - fileToLoad = fileset.getBiggest(); + input = imageSet.getBiggest(); } } - logger.info("Planning to load: " + fileToLoad); + logger.info("Planning to load: " + input); } - return fileToLoad; + return input; } + /** Returns the DocuDirectory for the input (file). + * @return + * @throws FileOpException + */ public DocuDirectory getFileDirectory() throws FileOpException { if(fileDir == null){ DocuDirCache dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); @@ -176,19 +188,26 @@ return fileDir; } - public ImageSet getFileset() throws FileOpException { - if(fileset==null){ + /** Returns the ImageSet to load. + * @return + * @throws FileOpException + */ + public ImageSet getImageSet() throws FileOpException { + if(imageSet==null){ DocuDirCache dirCache = (DocuDirCache) dlConfig.getValue("servlet.dir.cache"); - fileset = (ImageSet) dirCache.getFile(getFilePath(), getAsInt("pn"), FileClass.IMAGE); - if (fileset == null) { + imageSet = (ImageSet) dirCache.getFile(getFilePath(), getAsInt("pn"), FileClass.IMAGE); + if (imageSet == null) { throw new FileOpException("File " + getFilePath() + "(" + getAsInt("pn") + ") not found."); } } - return fileset; + return imageSet; } + /** Returns the file path name from the request. + * @return + */ public String getFilePath() { if(filePath == null){ String s = this.getAsString("request.path"); @@ -198,32 +217,36 @@ return filePath; } - public boolean getHiresOnly(){ + public boolean isHiresOnly(){ return hasOption("clip") || hasOption("hires"); } - public boolean getLoresOnly(){ + public boolean isLoresOnly(){ return hasOption("lores"); } - public boolean getScaleToFit() { + public boolean isScaleToFit() { return !(hasOption("clip") || hasOption("osize") || hasOption("ascale")); } - public boolean getAbsoluteScale(){ + public boolean isAbsoluteScale(){ return hasOption("osize") || hasOption("ascale"); } + /** Returns the minimum size the source image should have for scaling. + * @return + * @throws IOException + */ public ImageSize getExpectedSourceSize() throws IOException { if (expectedSourceSize == null){ expectedSourceSize = new ImageSize(); - if (getScaleToFit()) { + if (isScaleToFit()) { // scale to fit -- calculate minimum source size float scale = (1 / Math.min(getAsFloat("ww"), getAsFloat("wh"))) * getAsFloat("ws"); expectedSourceSize.setSize((int) (getDw() * scale), (int) (getDh() * scale)); - } else if (getAbsoluteScale() && hasOption("ascale")) { + } else if (isAbsoluteScale() && hasOption("ascale")) { // absolute scale -- apply scale to hires size expectedSourceSize = getHiresSize().getScaled(getAsFloat("scale")); } else { @@ -235,12 +258,16 @@ return expectedSourceSize; } + /** Returns the size of the highest resolution image. + * @return + * @throws IOException + */ public ImageSize getHiresSize() throws IOException { logger.debug("get_hiresSize()"); ImageSize hiresSize = null; - ImageSet fileset = getFileset(); - if (getAbsoluteScale()) { + ImageSet fileset = getImageSet(); + if (isAbsoluteScale()) { ImageInput hiresFile = fileset.getBiggest(); hiresSize = hiresFile.getSize(); } @@ -261,7 +288,7 @@ float areaWidth; float areaHeight; float ws = getAsFloat("ws"); - ImageSize imgSize = getFileToLoad().getSize(); + ImageSize imgSize = getInput().getSize(); // user window area in [0,1] coordinates Rectangle2D relUserArea = new Rectangle2D.Float(getAsFloat("wx"), getAsFloat("wy"), getAsFloat("ww"), getAsFloat("wh")); @@ -272,20 +299,20 @@ userImgArea = imgTrafo.createTransformedShape( relUserArea).getBounds2D(); - if (getScaleToFit()) { + if (isScaleToFit()) { // calculate scaling factors based on inner user area areaWidth = (float) userImgArea.getWidth(); areaHeight = (float) userImgArea.getHeight(); float scaleX = getDw() / areaWidth * ws; float scaleY = getDh() / areaHeight * ws; scaleXY = (scaleX > scaleY) ? scaleY : scaleX; - } else if (getAbsoluteScale()) { + } else if (isAbsoluteScale()) { // absolute scaling factor if (hasOption("osize")) { // get original resolution from metadata - fileset.checkMeta(); - float origResX = fileset.getResX(); - float origResY = fileset.getResY(); + imageSet.checkMeta(); + float origResX = imageSet.getResX(); + float origResY = imageSet.getResY(); if ((origResX == 0) || (origResY == 0)) { throw new ImageOpException("Missing image DPI information!"); } @@ -323,6 +350,11 @@ return (float) scaleXY; } + /** Returns the width of the destination image. + * Uses dh parameter and aspect ratio if dw parameter is empty. + * @return + * @throws IOException + */ public int getDw() throws IOException { logger.debug("get_paramDW()"); if (paramDW == null) { @@ -330,7 +362,7 @@ paramDW = getAsInt("dw"); paramDH = getAsInt("dh"); - float imgAspect = getFileToLoad().getAspect(); + float imgAspect = getInput().getAspect(); if (paramDW == 0) { // calculate dw paramDW = Math.round(paramDH * imgAspect); @@ -344,6 +376,11 @@ return paramDW; } + /** Returns the height of the destination image. + * Uses dw parameter and aspect ratio if dh parameter is empty. + * @return + * @throws IOException + */ public int getDh() throws IOException { logger.debug("get_paramDH()"); if (paramDH == null) { @@ -351,7 +388,7 @@ paramDW = getAsInt("dw"); paramDH = getAsInt("dh"); - float imgAspect = getFileToLoad().getAspect(); + float imgAspect = getInput().getAspect(); if (paramDW == 0) { // calculate dw paramDW = Math.round(paramDH * imgAspect); @@ -365,9 +402,12 @@ return paramDH; } - public Integer getScaleQual(){ + /** Returns image quality as an integer. + * @return + */ + public int getScaleQual(){ logger.debug("get_scaleQual()"); - Integer qual = dlConfig.getAsInt("default-quality"); + int qual = dlConfig.getAsInt("default-quality"); if(hasOption("q0")) qual = 0; else if(hasOption("q1")) @@ -378,21 +418,33 @@ } + /** + * Returns the area of the source image that will be transformed into the + * destination image. + * + * @return + * @throws IOException + * @throws ImageOpException + */ public Rectangle2D getUserImgArea() throws IOException, ImageOpException{ if(userImgArea == null) { // getScaleXY sets userImgArea getScaleXY(); } return userImgArea; - } + /** Returns the maximal area of the source image that will be used. + * @return + * @throws IOException + * @throws ImageOpException + */ public Rectangle2D getOuterUserImgArea() throws IOException, ImageOpException { if(outerUserImgArea == null){ outerUserImgArea = getUserImgArea(); // image size in pixels - ImageSize imgSize = getFileToLoad().getSize(); + ImageSize imgSize = getInput().getSize(); Rectangle2D imgBounds = new Rectangle2D.Float(0, 0, imgSize.getWidth(), imgSize.getHeight()); @@ -442,11 +494,13 @@ || hasOption("rawfile"); } - /** Could the image be sent without processing? - * Takes image type and additional image operations into account. - * Does not check requested size transformation. + /** + * Returns if the image can be sent without processing. Takes image type and + * additional image operations into account. Does not check requested size + * transformation. + * * @return - * @throws IOException + * @throws IOException */ public boolean isImageSendable() throws IOException { // cached result? @@ -469,15 +523,22 @@ } + /** + * 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 = getFileToLoad().getSize(); + ImageSize is = getInput().getSize(); ImageSize ess = getExpectedSourceSize(); // nt = no transform required boolean nt = isImageSendable() && ( // lores: send if smaller - (getLoresOnly() && is.isSmallerThan(ess)) + (isLoresOnly() && is.isSmallerThan(ess)) // else send if it fits - || (!(getLoresOnly() || getHiresOnly()) && is.fitsIn(ess))); + || (!(isLoresOnly() || isHiresOnly()) && is.fitsIn(ess))); return ! nt; } } \ No newline at end of file
--- a/servlet/src/digilib/image/ImageWorker.java Thu Jan 06 20:53:18 2011 +0100 +++ b/servlet/src/digilib/image/ImageWorker.java Fri Jan 07 12:00:10 2011 +0100 @@ -69,12 +69,12 @@ logger.debug("Using subsampling: " + subsamp + " rest " + scaleXY); } - docuImage.loadSubimage(jobinfo.getFileToLoad(), loadRect, (int) subsamp); + docuImage.loadSubimage(jobinfo.getInput(), loadRect, (int) subsamp); logger.debug("SUBSAMP: " + subsamp + " -> " + docuImage.getSize()); docuImage.scale(scaleXY, scaleXY); } else { // else load and crop the whole file - docuImage.loadImage(jobinfo.getFileToLoad()); + docuImage.loadImage(jobinfo.getInput()); docuImage.crop((int) loadRect.getX(), (int) loadRect.getY(), (int) loadRect.getWidth(), (int) loadRect.getHeight()); docuImage.scale(scaleXY, scaleXY);
--- a/servlet/src/digilib/servlet/Scaler.java Thu Jan 06 20:53:18 2011 +0100 +++ b/servlet/src/digilib/servlet/Scaler.java Fri Jan 07 12:00:10 2011 +0100 @@ -195,7 +195,7 @@ /* * check if we can fast-track without scaling */ - ImageInput fileToLoad = (ImageInput) jobTicket.getFileToLoad(); + ImageInput fileToLoad = (ImageInput) jobTicket.getInput(); // check permissions if (useAuthorization) {