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.");
}
}
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);
}
}
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();
}
}
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
}
}
}
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);
}
}
Aggregations