Mercurial > hg > digilib-old
changeset 588:aee436f0549d stream
more work on stream input
author | robcast |
---|---|
date | Thu, 06 Jan 2011 14:37:50 +0100 |
parents | 720d061a1b30 |
children | 73e041c710d3 |
files | servlet/src/digilib/image/DocuImage.java servlet/src/digilib/image/DocuImageImpl.java servlet/src/digilib/image/ImageInfoDocuImage.java servlet/src/digilib/image/ImageLoaderDocuImage.java servlet/src/digilib/image/ImageWorker.java servlet/src/digilib/image/JAIDocuImage.java servlet/src/digilib/image/JAIImageLoaderDocuImage.java servlet/src/digilib/io/ImageFile.java servlet/src/digilib/io/ImageFileSet.java servlet/src/digilib/io/ImageInput.java servlet/src/digilib/servlet/DigilibConfiguration.java servlet/src/digilib/servlet/Scaler.java |
diffstat | 12 files changed, 193 insertions(+), 153 deletions(-) [+] |
line wrap: on
line diff
--- a/servlet/src/digilib/image/DocuImage.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/image/DocuImage.java Thu Jan 06 14:37:50 2011 +0100 @@ -27,7 +27,6 @@ import javax.servlet.ServletException; -import digilib.io.ImageFile; import digilib.io.FileOpException; import digilib.io.ImageInput; @@ -42,10 +41,10 @@ /** Loads an image file into the Object. * - * @param f Image File. + * @param ii Image File. * @throws FileOpException Exception thrown if any error occurs. */ - public void loadImage(ImageFile f) throws FileOpException; + public void loadImage(ImageInput ii) throws FileOpException; /** This DocuImage supports the loadSubImage operation. * @@ -55,12 +54,12 @@ /** Load only a subsampled region of the image file. * - * @param f + * @param ii * @param region * @param subsample * @throws FileOpException */ - public void loadSubimage(ImageFile f, Rectangle region, int subsample) + public void loadSubimage(ImageInput ii, Rectangle region, int subsample) throws FileOpException; /** Writes the current image to a ServletResponse. @@ -96,7 +95,7 @@ */ public ImageSize getSize(); - /** The mime-type of the current image. + /** The mime-type of the image, i.e. the mime-type of the input that was read. * * @return String the mime-type of this image. */ @@ -222,9 +221,9 @@ public void dispose(); /** - * Check image size and type and store in ImageFile f + * Check image size and type and store in ImageInput ii */ - public ImageInput identify(ImageFile imgf) throws IOException; + public ImageInput identify(ImageInput ii) throws IOException; /** * Returns a list of supported image formats
--- a/servlet/src/digilib/image/DocuImageImpl.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/image/DocuImageImpl.java Thu Jan 06 14:37:50 2011 +0100 @@ -33,7 +33,6 @@ import org.apache.log4j.Logger; import digilib.io.FileOpException; -import digilib.io.ImageFile; import digilib.io.ImageInput; /** Simple abstract implementation of the <code>DocuImage</code> interface. @@ -54,13 +53,13 @@ /** epsilon for float comparisons. */ public final double epsilon = 1e-5; - - /** image mime-type */ - protected String mimeType = null; /** image size */ protected ImageSize imgSize = null; + /** ImageInput that was read */ + protected ImageInput input; + /** * Returns the quality. * @return int @@ -100,14 +99,20 @@ scale(scale, scale); } + /* (non-Javadoc) + * @see digilib.image.DocuImage#getMimetype() + */ public String getMimetype() { - return mimeType; + if (input != null) { + return input.getMimetype(); + } + return null; } /* (non-Javadoc) * @see digilib.image.DocuImage#identify(digilib.io.ImageFile) */ - public ImageInput identify(ImageFile imgf) throws IOException { + public ImageInput identify(ImageInput ii) throws IOException { // just a do-nothing implementation return null; } @@ -129,7 +134,7 @@ return false; } - public void loadSubimage(ImageFile f, Rectangle region, int subsample) + public void loadSubimage(ImageInput ii, Rectangle region, int subsample) throws FileOpException { // empty implementation } @@ -178,19 +183,11 @@ return imgSize; } - public void loadImage(ImageFile f) throws FileOpException { - // TODO Auto-generated method stub - - } + public abstract void loadImage(ImageInput ii) throws FileOpException; - public void scale(double scaleX, double scaleY) throws ImageOpException { - // TODO Auto-generated method stub - - } + public abstract void scale(double scaleX, double scaleY) throws ImageOpException; - public void writeImage(String mt, OutputStream ostream) - throws ServletException, ImageOpException { - // TODO Auto-generated method stub - } + public abstract void writeImage(String mt, OutputStream ostream) + throws ServletException, ImageOpException; }
--- a/servlet/src/digilib/image/ImageInfoDocuImage.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/image/ImageInfoDocuImage.java Thu Jan 06 14:37:50 2011 +0100 @@ -9,7 +9,6 @@ import org.marcoschmidt.image.ImageInfo; -import digilib.io.ImageFile; import digilib.io.ImageInput; /** Simple abstract implementation of the <code>DocuImage</code> interface. @@ -20,9 +19,9 @@ public abstract class ImageInfoDocuImage extends DocuImageImpl { /** Check image size and type and store in ImageFile f */ - public ImageInput identify(ImageFile imgf) throws IOException { + public ImageInput identify(ImageInput ii) throws IOException { // fileset to store the information - File f = imgf.getFile(); + File f = ii.getFile(); if (f == null) { throw new IOException("File not found!"); } @@ -36,12 +35,12 @@ // try with ImageInfo first if (iif.check()) { ImageSize d = new ImageSize(iif.getWidth(), iif.getHeight()); - imgf.setSize(d); - imgf.setMimetype(iif.getMimeType()); + ii.setSize(d); + ii.setMimetype(iif.getMimeType()); //logger.debug(" format:"+iif.getFormatName()); raf.close(); - logger.debug("image size: " + imgf.getSize()); - return imgf; + logger.debug("image size: " + ii.getSize()); + return ii; } else { raf.close(); }
--- a/servlet/src/digilib/image/ImageLoaderDocuImage.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/image/ImageLoaderDocuImage.java Thu Jan 06 14:37:50 2011 +0100 @@ -49,7 +49,6 @@ import digilib.io.FileOpException; import digilib.io.FileOps; -import digilib.io.ImageFile; import digilib.io.ImageSet; import digilib.io.ImageInput; @@ -68,8 +67,6 @@ /** File that was read */ protected File imgFile; - private ImageInput input; - /* loadSubimage is supported. */ public boolean isSubimageSupported() { return true; @@ -121,44 +118,45 @@ return Arrays.asList(formats).iterator(); } - /** Check image size and type and store in ImageFile f */ - public ImageInput identify(ImageFile imageFile) throws IOException { + /* Check image size and type and store in ImageInput */ + public ImageInput identify(ImageInput input) throws IOException { // try parent method first - ImageInput imf = super.identify(imageFile); - if (imf != null) { - return imf; + ImageInput ii = super.identify(input); + if (ii != null) { + return ii; } - // fileset to store the information - ImageSet imgfs = imageFile.getParent(); - File f = imageFile.getFile(); - if (f == null) { - throw new IOException("File not found!"); - } - logger.debug("identifying (ImageIO) " + f); + logger.debug("identifying (ImageIO) " + input); /* * try ImageReader */ - if ((reader == null) || (imgFile != imageFile.getFile())) { - getReader(imageFile); - } + reader = getReader(input); + // set size ImageSize d = new ImageSize(reader.getWidth(0), reader.getHeight(0)); - imageFile.setSize(d); - // String t = reader.getFormatName(); - String t = FileOps.mimeForFile(f); - imageFile.setMimetype(t); - // logger.debug(" format:"+t); - if (imgfs != null) { - imgfs.setAspect(d); + input.setSize(d); + // set mime type + if (input.getMimetype() == null) { + if (input.hasFile()) { + String t = FileOps.mimeForFile(input.getFile()); + input.setMimetype(t); + } else { + // FIXME: is format name a mime type??? + String t = reader.getFormatName(); + input.setMimetype(t); + } } - return imageFile; + return input; } /* load image file */ - public void loadImage(ImageFile f) throws FileOpException { - logger.debug("loadImage " + f.getFile()); + public void loadImage(ImageInput ii) throws FileOpException { + logger.debug("loadImage: " + ii); + this.input = ii; try { - img = ImageIO.read(f.getFile()); - mimeType = f.getMimetype(); + if (ii.hasImageInputStream()) { + img = ImageIO.read(ii.getImageInputStream()); + } else if (ii.hasFile()) { + img = ImageIO.read(ii.getFile()); + } } catch (IOException e) { throw new FileOpException("Error reading image."); } @@ -181,6 +179,7 @@ logger.debug("cleaning Reader!"); dispose(); } + this.input = input; ImageInputStream istream = null; if (input.hasImageInputStream()) { // stream input @@ -211,17 +210,16 @@ logger.debug("ImageIO: next reader: " + readers.next().getClass()); } */ reader.setInput(istream); - imgFile = input.getFile(); return reader; } /* Load an image file into the Object. */ - public void loadSubimage(ImageFile f, Rectangle region, int prescale) + public void loadSubimage(ImageInput ii, Rectangle region, int prescale) throws FileOpException { logger.debug("loadSubimage"); try { - if ((reader == null) || (imgFile != f.getFile())) { - getReader(f); + if ((reader == null) || (imgFile != ii.getFile())) { + getReader(ii); } // set up reader parameters ImageReadParam readParam = reader.getDefaultReadParam(); @@ -232,7 +230,6 @@ // read image logger.debug("loading.."); img = reader.read(0, readParam); - mimeType = f.getMimetype(); logger.debug("loaded"); } catch (IOException e) { throw new FileOpException("Unable to load File!");
--- a/servlet/src/digilib/image/ImageWorker.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/image/ImageWorker.java Thu Jan 06 14:37:50 2011 +0100 @@ -10,7 +10,7 @@ import org.apache.log4j.Logger; import digilib.io.FileOpException; -import digilib.io.ImageFile; +import digilib.io.ImageInput; import digilib.servlet.DigilibConfiguration; /** Worker that renders an image. @@ -71,7 +71,7 @@ + scaleXY); } - docuImage.loadSubimage((ImageFile) jobinfo.getFileToLoad(), loadRect, (int) subsamp); //FIXME: cast to file + docuImage.loadSubimage((ImageInput) jobinfo.getFileToLoad(), loadRect, (int) subsamp); //FIXME: cast to file logger.debug("SUBSAMP: " + subsamp + " -> " + docuImage.getSize()); @@ -79,7 +79,7 @@ } else { // else load and crop the whole file - docuImage.loadImage((ImageFile) jobinfo.getFileToLoad()); //FIXME: cast to file + docuImage.loadImage((ImageInput) jobinfo.getFileToLoad()); //FIXME: cast to file docuImage.crop((int) loadRect.getX(), (int) loadRect.getY(), (int) loadRect.getWidth(), (int) loadRect.getHeight());
--- a/servlet/src/digilib/image/JAIDocuImage.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/image/JAIDocuImage.java Thu Jan 06 14:37:50 2011 +0100 @@ -24,7 +24,6 @@ import java.awt.RenderingHints; import java.awt.image.RenderedImage; import java.awt.image.renderable.ParameterBlock; -import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; @@ -46,8 +45,6 @@ import digilib.io.FileOpException; import digilib.io.FileOps; -import digilib.io.ImageFile; -import digilib.io.ImageSet; import digilib.io.ImageInput; /** A DocuImage implementation using Java Advanced Imaging Library. */ @@ -105,53 +102,64 @@ } /* Check image size and type and store in ImageFile f */ - public ImageInput identify(ImageFile imageFile) throws IOException { + public ImageInput identify(ImageInput input) throws IOException { + this.input = input; // try parent method first - ImageInput imf = super.identify(imageFile); + ImageInput imf = super.identify(input); if (imf != null) { return imf; } - // fileset to store the information - ImageSet imgfs = imageFile.getParent(); - File f = imageFile.getFile(); - if (f == null) { - throw new IOException("File not found!"); - } /* * try JAI */ - logger.debug("identifying (JAI) " + f); + logger.debug("identifying (JAI) " + input); try { - RenderedOp img = JAI.create("fileload", f.getAbsolutePath()); + RenderedOp img = null; + if (input.hasFile()) { + String t = FileOps.mimeForFile(input.getFile()); + input.setMimetype(t); + img = JAI.create("fileload", input.getFile().getAbsolutePath()); + } else if (input.hasInputStream()) { + img = JAI.create("stream", input.getInputStream()); + // FIXME: where do we get the mimetype? + } else { + throw new FileOpException("unable to get data for image!"); + } ImageSize d = new ImageSize(img.getWidth(), img.getHeight()); - imageFile.setSize(d); - String t = FileOps.mimeForFile(f); - imageFile.setMimetype(t); - // logger.debug(" format:"+t); - if (imgfs != null) { - imgfs.setAspect(d); - } - logger.debug("image size: " + imageFile.getSize()); - return imageFile; + input.setSize(d); + logger.debug("image size: " + d); + return input; } catch (Exception e) { - throw new FileOpException("ERROR: unknown image file format!"); + throw new FileOpException("ERROR: unable to identify image!"); } } /* Load an image file into the Object. */ - public void loadImage(ImageFile f) throws FileOpException { - img = JAI.create("fileload", f.getFile().getAbsolutePath()); + public void loadImage(ImageInput ii) throws FileOpException { + this.input = ii; + if (ii.hasFile()) { + img = JAI.create("fileload", ii.getFile().getAbsolutePath()); + } else if (ii.hasInputStream()) { + img = JAI.create("stream", ii.getInputStream()); + } else { + throw new FileOpException("unable to get data for image!"); + } if (img == null) { throw new FileOpException("Unable to load File!"); } - mimeType = f.getMimetype(); } /* Load an image file into the Object. */ - public void loadSubimage(ImageFile f, Rectangle region, int subsample) - throws FileOpException { + public void loadSubimage(ImageInput ii, Rectangle region, int subsample) throws FileOpException { logger.debug("loadSubimage"); - img = JAI.create("fileload", f.getFile().getAbsolutePath()); + this.input = ii; + if (ii.hasFile()) { + img = JAI.create("fileload", ii.getFile().getAbsolutePath()); + } else if (ii.hasInputStream()) { + img = JAI.create("stream", ii.getInputStream()); + } else { + throw new FileOpException("unable to get data for image!"); + } if ((region.width < img.getWidth()) || (region.height < img.getHeight())) { // setup Crop @@ -175,7 +183,6 @@ // scale logger.debug("loadSubimage: scale"); img = JAI.create("scale", sp); - mimeType = f.getMimetype(); } }
--- a/servlet/src/digilib/image/JAIImageLoaderDocuImage.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/image/JAIImageLoaderDocuImage.java Thu Jan 06 14:37:50 2011 +0100 @@ -38,7 +38,7 @@ import javax.servlet.ServletException; import digilib.io.FileOpException; -import digilib.io.ImageFile; +import digilib.io.ImageInput; /** DocuImage implementation using the Java Advanced Imaging API and the ImageLoader * API of Java 1.4. @@ -80,44 +80,70 @@ /* Load an image file into the Object. */ - public void loadImage(ImageFile f) throws FileOpException { - logger.debug("loadImage: "+f.getFile()); + public void loadImage(ImageInput ii) throws FileOpException { + logger.debug("loadImage: "+ii.getFile()); //System.gc(); - img = JAI.create("ImageRead", f.getFile().getAbsolutePath()); + img = JAI.create("ImageRead", ii.getFile().getAbsolutePath()); if (img == null) { throw new FileOpException("Unable to load File!"); } - mimeType = f.getMimetype(); } /* Get an ImageReader for the image file. */ - public ImageReader getReader(ImageFile f) throws IOException { - logger.debug("preloadImage: "+f.getFile()); - //System.gc(); - RandomAccessFile rf = new RandomAccessFile(f.getFile(), "r"); - ImageInputStream istream = new FileImageInputStream(rf); - //Iterator readers = ImageIO.getImageReaders(istream); - Iterator<ImageReader> readers = ImageIO.getImageReadersByMIMEType(f.getMimetype()); - if (! readers.hasNext()) { - throw new FileOpException("Unable to load File!"); - } - reader = readers.next(); - logger.debug("JAIImageIO: this reader: " + reader.getClass()); - while (readers.hasNext()) { - logger.debug(" next reader: " + readers.next().getClass()); - } - reader.setInput(istream); - return reader; + public ImageReader getReader(ImageInput input) throws IOException { + logger.debug("get ImageReader for " + input); + if (this.reader != null) { + if (this.input == input) { + // it was the same input + logger.debug("reusing Reader"); + return reader; + } + // clean up old reader + logger.debug("cleaning Reader!"); + dispose(); + } + this.input = input; + ImageInputStream istream = null; + if (input.hasImageInputStream()) { + // stream input + istream = input.getImageInputStream(); + } else if (input.hasFile()) { + // file only input + RandomAccessFile rf = new RandomAccessFile(input.getFile(), "r"); + istream = new FileImageInputStream(rf); + } else { + throw new FileOpException("Unable to get data from ImageInput"); + } + Iterator<ImageReader> readers; + String mt = input.getMimetype(); + if (mt == null) { + logger.debug("No mime-type. Trying automagic."); + readers = ImageIO.getImageReaders(istream); + } else { + logger.debug("File type:" + mt); + readers = ImageIO.getImageReadersByMIMEType(mt); + } + if (!readers.hasNext()) { + throw new FileOpException("Can't find Reader to load File!"); + } + reader = readers.next(); + /* are there more readers? */ + logger.debug("ImageIO: this reader: " + reader.getClass()); + /* while (readers.hasNext()) { + logger.debug("ImageIO: next reader: " + readers.next().getClass()); + } */ + reader.setInput(istream); + return reader; } /* Load an image file into the Object. */ - public void loadSubimage(ImageFile f, Rectangle region, int prescale) + public void loadSubimage(ImageInput ii, Rectangle region, int prescale) throws FileOpException { - logger.debug("loadSubimage: "+f.getFile()); + logger.debug("loadSubimage: "+ii.getFile()); //System.gc(); try { - if ((reader == null) || (imgFile != f.getFile())) { - getReader(f); + if ((reader == null) || (imgFile != ii.getFile())) { + getReader(ii); } ImageReadParam readParam = reader.getDefaultReadParam(); readParam.setSourceRegion(region); @@ -137,8 +163,7 @@ if (img == null) { throw new FileOpException("Unable to load File!"); } - imgFile = f.getFile(); - mimeType = f.getMimetype(); + imgFile = ii.getFile(); }
--- a/servlet/src/digilib/io/ImageFile.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/io/ImageFile.java Thu Jan 06 14:37:50 2011 +0100 @@ -36,8 +36,6 @@ private File file = null; // file name private String name = null; - // parent ImageSet - private ImageSet parent = null; // parent directory private Directory dir = null; @@ -123,21 +121,6 @@ return file; } - /** - * @return ImageSet - */ - public ImageSet getParent() { - return parent; - } - - /** - * Sets the parent. - * @param parent The parent to set - */ - public void setParent(ImageSet parent) { - this.parent = parent; - } - /* (non-Javadoc) * @see digilib.io.ImageInput#setSize(digilib.image.ImageSize) */
--- a/servlet/src/digilib/io/ImageFileSet.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/io/ImageFileSet.java Thu Jan 06 14:37:50 2011 +0100 @@ -118,7 +118,7 @@ * file to add * @return true (always) */ - public boolean add(ImageFile f) { + public boolean add(ImageInput f) { f.setParent(this); return list.add(f); }
--- a/servlet/src/digilib/io/ImageInput.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/io/ImageInput.java Thu Jan 06 14:37:50 2011 +0100 @@ -22,6 +22,7 @@ package digilib.io; import java.io.File; +import java.io.InputStream; import javax.imageio.stream.ImageInputStream; @@ -33,6 +34,7 @@ protected String mimetype = null; // image size in pixels protected ImageSize pixelSize = null; + protected ImageSet parent = null; /** * @return ImageSize @@ -81,7 +83,22 @@ return (pixelSize != null) ? pixelSize.getAspect() : 0f; } - /** Returns if the input can be returned as ImageInputStream. + /** + * @return ImageSet + */ + public ImageSet getParent() { + return parent; + } + + /** + * Sets the parent. + * @param parent The parent to set + */ + public void setParent(ImageSet parent) { + this.parent = parent; + } + + /** Returns if the input can be returned as ImageInputStream. * * @return */ @@ -97,6 +114,22 @@ return null; } + /** Returns if the input can be returned as InputStream. + * + * @return + */ + public boolean hasInputStream() { + return false; + } + + /** Returns the input as InputStream (if available) + * + * @return + */ + public InputStream getInputStream() { + return null; + } + /** Returns if the input can be returned as File. * * @return @@ -112,6 +145,7 @@ public File getFile() { return null; } + } \ No newline at end of file
--- a/servlet/src/digilib/servlet/DigilibConfiguration.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/servlet/DigilibConfiguration.java Thu Jan 06 14:37:50 2011 +0100 @@ -35,7 +35,6 @@ import digilib.image.DocuImage; import digilib.image.DocuImageImpl; import digilib.io.FileOps; -import digilib.io.ImageFile; import digilib.io.ImageInput; import digilib.io.XMLListLoader; import digilib.util.Parameter; @@ -278,7 +277,7 @@ * @return * @throws IOException */ - public static ImageInput identifyDocuImage(ImageFile imgf) throws IOException { + public static ImageInput identifyDocuImage(ImageInput imgf) throws IOException { // use fresh DocuImage instance DocuImage di = getDocuImageInstance(); return di.identify(imgf);
--- a/servlet/src/digilib/servlet/Scaler.java Thu Jan 06 11:57:32 2011 +0100 +++ b/servlet/src/digilib/servlet/Scaler.java Thu Jan 06 14:37:50 2011 +0100 @@ -25,7 +25,7 @@ import digilib.io.DocuDirectory; import digilib.io.DocuDirent; import digilib.io.FileOps.FileClass; -import digilib.io.ImageFile; +import digilib.io.ImageInput; import digilib.util.DigilibJobCenter; @SuppressWarnings("serial") @@ -195,7 +195,7 @@ /* * check if we can fast-track without scaling */ - ImageFile fileToLoad = (ImageFile) jobTicket.getFileToLoad(); + ImageInput fileToLoad = (ImageInput) jobTicket.getFileToLoad(); // check permissions if (useAuthorization) {