Search in sources :

Example 6 with FileOpException

use of digilib.io.FileOpException in project digilib by robcast.

the class JAIImageLoaderDocuImage method writeImage.

/* Write the current image to an OutputStream. */
public void writeImage(String mt, OutputStream ostream) throws ImageOpException, FileOpException {
    logger.debug("writeImage");
    try {
        // setup output
        ParameterBlock pb3 = new ParameterBlock();
        pb3.addSource(img);
        pb3.add(ostream);
        if (mt == "image/jpeg") {
            pb3.add("JPEG");
        } else if (mt == "image/png") {
            pb3.add("PNG");
        } else {
            // unknown mime type
            throw new ImageOpException("Unknown mime type: " + mt);
        }
        // render output
        JAI.create("ImageWrite", pb3);
    } catch (RuntimeException e) {
        throw new FileOpException("Error writing image.");
    }
}
Also used : ParameterBlock(java.awt.image.renderable.ParameterBlock) FileOpException(digilib.io.FileOpException)

Example 7 with FileOpException

use of digilib.io.FileOpException 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 8 with FileOpException

use of digilib.io.FileOpException in project digilib by robcast.

the class BioFormatsDocuImage method loadImage.

@Override
public void loadImage(ImageInput ii) throws FileOpException {
    logger.debug("loadImage: " + ii);
    this.input = ii;
    reader = new ImageReader();
    try {
        // construct the object that stores OME-XML metadata
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.createOMEXMLMetadata();
        // set up the reader and associate it with the input file
        reader = new ImageReader();
        reader.setMetadataStore(meta);
        reader.setId(ii.getFile().getAbsolutePath());
        BufferedImageReader biReader = BufferedImageReader.makeBufferedImageReader(reader);
        img = biReader.openImage(0);
        logger.debug("image loaded: " + img);
    } catch (FormatException e) {
        throw new FileOpException("Unable to load image format: " + e);
    } catch (IOException e) {
        throw new FileOpException("Unable to load image file: " + e);
    } catch (ServiceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DependencyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) FileOpException(digilib.io.FileOpException) BufferedImageReader(loci.formats.gui.BufferedImageReader) IOException(java.io.IOException) ImageReader(loci.formats.ImageReader) BufferedImageReader(loci.formats.gui.BufferedImageReader) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Example 9 with FileOpException

use of digilib.io.FileOpException in project digilib by robcast.

the class ImageJ1DocuImage method writeImage.

/* (non-Javadoc)
	 * @see digilib.image.DocuImageImpl#writeImage(java.lang.String, java.io.OutputStream)
	 */
@Override
public void writeImage(String mt, OutputStream ostream) throws ImageOpException, FileOpException {
    File outFile;
    String filext = ".jpg";
    if (mt.equals("image/png")) {
        filext = ".png";
    }
    try {
        outFile = File.createTempFile("imgj_temp", filext);
    } catch (IOException e) {
        throw new FileOpException(e.toString());
    }
    // save image to file
    logger.debug("writeImage: mt=" + mt);
    if (mt.equals("image/png")) {
        PNG_Writer writer = new PNG_Writer();
        try {
            img = new ImagePlus("Image", proc);
            writer.writeImage(img, outFile.getAbsolutePath(), 0);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        img = new ImagePlus("Image", proc);
        JpegWriter.save(img, outFile.getAbsolutePath(), 70);
    }
    // now send file
    FileInputStream inFile = null;
    try {
        inFile = new FileInputStream(outFile);
        byte[] dataBuffer = new byte[4096];
        int len;
        while ((len = inFile.read(dataBuffer)) != -1) {
            // copy out file
            ostream.write(dataBuffer, 0, len);
        }
    } catch (IOException e) {
        throw new FileOpException(e.toString());
    } finally {
        try {
            if (inFile != null) {
                inFile.close();
            }
        } catch (IOException e) {
        // nothing to do
        }
    }
}
Also used : PNG_Writer(ij.plugin.PNG_Writer) FileOpException(digilib.io.FileOpException) IOException(java.io.IOException) File(java.io.File) ImagePlus(ij.ImagePlus) IOException(java.io.IOException) FileOpException(digilib.io.FileOpException) FileInputStream(java.io.FileInputStream)

Example 10 with FileOpException

use of digilib.io.FileOpException in project digilib by robcast.

the class JAIDocuImage method writeImage.

/* Write the current image to an OutputStream. */
public void writeImage(String mt, OutputStream ostream) throws ImageOpException, FileOpException {
    try {
        // setup output
        ParameterBlock pb3 = new ParameterBlock();
        pb3.addSource(img);
        pb3.add(ostream);
        if (mt == "image/jpeg") {
            pb3.add("JPEG");
        } else if (mt == "image/png") {
            pb3.add("PNG");
        } else {
            // unknown mime type
            throw new ImageOpException("Unknown mime type: " + mt);
        }
        // render output
        JAI.create("encode", pb3);
    } catch (RuntimeException e) {
        // JAI likes to throw RuntimeExceptions
        throw new FileOpException("Error writing image: " + e);
    }
}
Also used : ParameterBlock(java.awt.image.renderable.ParameterBlock) FileOpException(digilib.io.FileOpException)

Aggregations

FileOpException (digilib.io.FileOpException)18 IOException (java.io.IOException)10 ImageInput (digilib.io.ImageInput)4 ImageSize (digilib.util.ImageSize)3 ParameterBlock (java.awt.image.renderable.ParameterBlock)3 File (java.io.File)3 ImageSet (digilib.io.ImageSet)2 BufferedImage (java.awt.image.BufferedImage)2 FileInputStream (java.io.FileInputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 ImageReadParam (javax.imageio.ImageReadParam)2 ImageReader (javax.imageio.ImageReader)2 FileImageInputStream (javax.imageio.stream.FileImageInputStream)2 ImageInputStream (javax.imageio.stream.ImageInputStream)2 FormatException (loci.formats.FormatException)2 DigilibConfiguration (digilib.conf.DigilibConfiguration)1 ImageJobDescription (digilib.image.ImageJobDescription)1 ImageOpException (digilib.image.ImageOpException)1 DocuDirCache (digilib.io.DocuDirCache)1 DocuDirent (digilib.io.DocuDirent)1