Search in sources :

Example 11 with ImageSize

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

the class DocumentBean method setRequest.

/**
 * Sets the current DigilibRequest. Also completes information in the request.
 *
 * @param dlRequest
 *            The dlRequest to set.
 */
public void setRequest(DigilibServletRequest dlRequest) throws Exception {
    this.dlRequest = dlRequest;
    if (dirCache == null) {
        return;
    }
    String fn = dlRequest.getFilePath();
    // get information about the file
    ImageSet fileset = (ImageSet) dirCache.getFile(fn, dlRequest.getAsInt("pn"));
    if (fileset == null) {
        return;
    }
    // add file name
    dlRequest.setValue("img.fn", fileset);
    // add dpi
    dlRequest.setValue("img.dpix", new Double(fileset.getResX()));
    dlRequest.setValue("img.dpiy", new Double(fileset.getResY()));
    // get number of pages in directory
    DocuDirectory dd = dirCache.getDirectory(fn);
    if (dd != null) {
        // add pt
        dlRequest.setValue("pt", dd.size());
    }
    // get original pixel size
    ImageInput origfile = fileset.getBiggest();
    // check image for size (TODO: just if mo=hires?)
    ImageSize pixsize = origfile.getSize();
    if (pixsize != null) {
        // add pixel size
        dlRequest.setValue("img.pix_x", new Integer(pixsize.getWidth()));
        dlRequest.setValue("img.pix_y", new Integer(pixsize.getHeight()));
    }
}
Also used : ImageInput(digilib.io.ImageInput) ImageSize(digilib.util.ImageSize) DocuDirectory(digilib.io.DocuDirectory) ImageSet(digilib.io.ImageSet)

Example 12 with ImageSize

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

the class ServletOps method sendIiifInfo.

/**
 * Returns IIIF compatible image information as application/json response.
 *
 * @param dlReq
 * @param response
 * @param logger
 * @throws FileOpException
 * @throws ServletException
 * @see <a href="http://www-sul.stanford.edu/iiif/image-api/1.1/#info">IIIF Image Information Request</a>
 */
public static void sendIiifInfo(DigilibServletRequest dlReq, HttpServletResponse response, Logger logger) throws ServletException {
    if (response == null) {
        logger.error("No response!");
        return;
    }
    /*
         * get image size
         */
    ImageSize size = null;
    ImageSet imageSet = null;
    try {
        // get original image size
        imageSet = dlReq.getJobDescription().getImageSet();
        ImageInput img = imageSet.getBiggest();
        size = img.getSize();
    } catch (FileOpException e) {
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        } catch (IOException e1) {
            throw new ServletException("Unable to write error response!", e);
        }
    }
    /*
         * get resource URL
         */
    String url = dlReq.getServletRequest().getRequestURL().toString();
    if (url.endsWith("/info.json")) {
        url = url.substring(0, url.lastIndexOf("/info.json"));
    } else if (url.endsWith("/")) {
        url = url.substring(0, url.lastIndexOf("/"));
    }
    /*
         * send response
         */
    response.setCharacterEncoding("UTF-8");
    logger.debug("sending info.json");
    try {
        /*
             * set CORS header ACAO "*" for info response as per IIIF spec
             */
        if (corsForInfoRequests) {
            String origin = dlReq.getServletRequest().getHeader("Origin");
            if (origin != null) {
                response.setHeader("Access-Control-Allow-Origin", "*");
            }
        }
        if (dlConfig.getAsString("iiif-api-version").startsWith("2.")) {
            /*
                 * IIIF Image API version 2 image information
                 */
            // use JSON-LD content type only when asked
            String accept = dlReq.getServletRequest().getHeader("Accept");
            if (accept != null && accept.contains("application/ld+json")) {
                response.setContentType("application/ld+json");
            } else {
                response.setContentType("application/json");
            }
            // write info.json
            ServletOutputStream out = response.getOutputStream();
            JsonGenerator info = Json.createGenerator(out);
            // top level object
            info.writeStartObject().write("@context", "http://iiif.io/api/image/2/context.json").write("@id", url).write("protocol", "http://iiif.io/api/image").write("width", size.width).write("height", size.height);
            // profile[ array
            info.writeStartArray("profile").write("http://iiif.io/api/image/2/level2.json");
            // profile[{ object
            info.writeStartObject();
            // profile[{formats[
            info.writeStartArray("formats").write("jpg").write("png").writeEnd();
            // profile[{qualities[
            info.writeStartArray("qualities").write("color").write("gray").writeEnd();
            // profile[{maxArea
            if (dlConfig.getAsInt("max-image-size") > 0) {
                info.write("maxArea", dlConfig.getAsInt("max-image-size"));
            }
            // profile[{supports[
            info.writeStartArray("supports").write("mirroring").write("rotationArbitrary").write("sizeAboveFull").write("regionSquare").writeEnd();
            // profile[{}
            info.writeEnd();
            // profile[]
            info.writeEnd();
            // add size of original and prescaled images
            int numImgs = imageSet.size();
            if (numImgs > 0) {
                // sizes[
                info.writeStartArray("sizes");
                for (int i = numImgs - 1; i >= 0; --i) {
                    ImageInput ii = imageSet.get(i);
                    ImageSize is = ii.getSize();
                    // sizes[{
                    info.writeStartObject().write("width", is.getWidth()).write("height", is.getHeight()).writeEnd();
                }
                // sizes[]
                info.writeEnd();
            }
            // end info.json
            info.writeEnd();
            info.close();
        } else {
            /*
                 * IIIF Image API version 1 image information
                 */
            response.setContentType("application/json,application/ld+json");
            // write info.json
            ServletOutputStream out = response.getOutputStream();
            JsonGenerator info = Json.createGenerator(out);
            // top level object
            info.writeStartObject().write("@context", "http://library.stanford.edu/iiif/image-api/1.1/context.json").write("@id", url).write("width", size.width).write("height", size.height);
            // formats[
            info.writeStartArray("formats").write("jpg").write("png").writeEnd();
            // qualities[
            info.writeStartArray("qualities").write("native").write("color").write("gray").writeEnd();
            // profile
            info.write("profile", "http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level2");
            // end info.json
            info.writeEnd();
            info.close();
        }
    } catch (IOException e) {
        throw new ServletException("Unable to write response!", e);
    }
}
Also used : ServletException(javax.servlet.ServletException) ImageSize(digilib.util.ImageSize) ImageInput(digilib.io.ImageInput) ServletOutputStream(javax.servlet.ServletOutputStream) FileOpException(digilib.io.FileOpException) JsonGenerator(javax.json.stream.JsonGenerator) ImageSet(digilib.io.ImageSet) IOException(java.io.IOException)

Example 13 with ImageSize

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

the class Manifester method writeCanvases.

/**
 * @param dlDir
 * @param url
 * @param manifest
 * @param servletUrl
 */
protected void writeCanvases(JsonGenerator manifest, ManifestParams params) {
    /*
		 * list of canvases
		 */
    manifest.writeStartArray("canvases");
    int idx = 0;
    for (DocuDirent imgFile : params.docuDir) {
        idx += 1;
        ImageFileSet imgFs = (ImageFileSet) imgFile;
        ImageInput img = imgFs.getBiggest();
        ImageSize imgSize = img.getSize();
        if (imgSize == null)
            continue;
        /*
			 * canvas
			 */
        writeCanvas(manifest, idx, imgFile, imgSize, params);
    }
    // canvases
    manifest.writeEnd();
}
Also used : ImageInput(digilib.io.ImageInput) ImageSize(digilib.util.ImageSize) ImageFileSet(digilib.io.ImageFileSet) DocuDirent(digilib.io.DocuDirent)

Example 14 with ImageSize

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

the class JAIDocuImage method identify.

/* Check image size and type and store in ImageFile f */
public ImageInput identify(ImageInput input) throws IOException {
    this.input = input;
    // try parent method first
    ImageInput imf = super.identify(input);
    if (imf != null) {
        return imf;
    }
    /*
		 * try JAI
		 */
    logger.debug("identifying (JAI) " + input);
    try {
        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());
        input.setSize(d);
        logger.debug("image size: " + d);
        return input;
    } catch (Exception e) {
        throw new FileOpException("ERROR: unable to identify image!");
    }
}
Also used : RenderedOp(javax.media.jai.RenderedOp) ImageInput(digilib.io.ImageInput) ImageSize(digilib.util.ImageSize) FileOpException(digilib.io.FileOpException) IOException(java.io.IOException) FileOpException(digilib.io.FileOpException)

Example 15 with ImageSize

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

the class ImageInfoDocuImage method identify.

/* Check image size and type and store in ImageFile f */
public ImageInput identify(ImageInput ii) throws IOException {
    logger.debug("identifying (ImageInfo) " + ii);
    RandomAccessFile raf = null;
    try {
        // set up ImageInfo object
        ImageInfo iif = new ImageInfo();
        if (ii.hasImageInputStream()) {
            iif.setInput(ii.getImageInputStream());
        } else if (ii.hasFile()) {
            raf = new RandomAccessFile(ii.getFile(), "r");
            iif.setInput(raf);
        } else {
            return null;
        }
        iif.setCollectComments(false);
        iif.setDetermineImageNumber(false);
        // try with ImageInfo first
        if (iif.check()) {
            ImageSize d = new ImageSize(iif.getWidth(), iif.getHeight());
            ii.setSize(d);
            String mt = iif.getMimeType();
            // fix image/pjpeg
            if (mt.equals("image/pjpeg")) {
                mt = "image/jpeg";
            }
            ii.setMimetype(mt);
            logger.debug("image size: " + ii.getSize());
            return ii;
        }
    } catch (Exception e) {
        logger.debug("ImageInfoDocuimage unable to identify.", e);
    } finally {
        // close file, don't close stream(?)
        if (raf != null) {
            raf.close();
        }
    }
    return null;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ImageSize(digilib.util.ImageSize) ImageInfo(org.devlib.schmidt.imageinfo.ImageInfo) IOException(java.io.IOException)

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