use of javax.imageio.IIOException in project imageio-ext by geosolutions-it.
the class TIFFBaseJPEGCompressor method getImageMetadata.
/**
* Retrieves image metadata with non-core nodes removed.
*/
private IIOMetadata getImageMetadata(boolean pruneTables) throws IIOException {
if (DEBUG) {
System.out.println("getImageMetadata(" + pruneTables + ")");
}
if (JPEGImageMetadata == null && IMAGE_METADATA_NAME.equals(JPEGWriter.getOriginatingProvider().getNativeImageMetadataFormatName())) {
TIFFImageWriter tiffWriter = (TIFFImageWriter) this.writer;
// Get default image metadata.
JPEGImageMetadata = JPEGWriter.getDefaultImageMetadata(tiffWriter.imageType, JPEGParam);
// Get the DOM tree.
Node tree = JPEGImageMetadata.getAsTree(IMAGE_METADATA_NAME);
// Remove unwanted marker segments.
try {
pruneNodes(tree, pruneTables);
} catch (IllegalArgumentException e) {
throw new IIOException("Error pruning unwanted nodes", e);
}
// Set the DOM back into the metadata.
try {
JPEGImageMetadata.setFromTree(IMAGE_METADATA_NAME, tree);
} catch (IIOInvalidTreeException e) {
// present of JPEGTables field.
throw new IIOException("Cannot set pruned image metadata!", e);
}
}
return JPEGImageMetadata;
}
use of javax.imageio.IIOException in project acs-aem-commons by Adobe-Consulting-Services.
the class AbstractRenditionModifyingProcess method execute.
public final void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaData, WorkflowHelper workflowHelper) throws WorkflowException {
String[] args = workflowHelper.buildArguments(metaData);
final String renditionName = workflowHelper.getValuesFromArgs(ARG_RENDITION_NAME, args).size() > 0 ? workflowHelper.getValuesFromArgs(ARG_RENDITION_NAME, args).get(0) : null;
// image quality: from 0 t0 100%
final String qualityStr = workflowHelper.getValuesFromArgs(ARG_QUALITY, args).size() > 0 ? workflowHelper.getValuesFromArgs(ARG_QUALITY, args).get(0) : DEFAULT_QUALITY;
if (renditionName == null) {
log.warn("Rendition name was not configured in arguments. Skipping.");
return;
}
final WorkflowHelper.AssetResourceResolverPair pair = workflowHelper.getAssetFromPayload(workItem, workflowSession);
if (pair == null) {
return;
}
final Rendition rendition = pair.asset.getRendition(new PrefixRenditionPicker(renditionName));
if (rendition == null) {
log.warn("Rendition name {} was not available for asset {}. Skipping.", renditionName, pair.asset);
return;
}
Layer layer = null;
try {
layer = new Layer(rendition.getStream());
layer = processLayer(layer, rendition, workflowSession, args);
String mimetype = layer.getMimeType();
double quality = mimetype.equals("image/gif") ? workflowHelper.getQuality(MAX_GIF_QUALITY, qualityStr) : workflowHelper.getQuality(1.0, qualityStr);
saveImage(pair.asset, rendition, layer, mimetype, quality, workflowHelper);
} catch (IIOException e) {
log.warn("Unable to load image layer from " + rendition.getPath(), e);
} catch (IOException e) {
log.warn("Unable to load image layer from " + rendition.getPath(), e);
} finally {
if (layer != null) {
layer.dispose();
layer = null;
}
pair.resourceResolver.close();
}
}
use of javax.imageio.IIOException in project pdfbox by apache.
the class DCTFilter method decode.
@Override
public DecodeResult decode(InputStream encoded, OutputStream decoded, COSDictionary parameters, int index, DecodeOptions options) throws IOException {
ImageReader reader = findImageReader("JPEG", "a suitable JAI I/O image filter is not installed");
try (ImageInputStream iis = ImageIO.createImageInputStream(encoded)) {
// skip one LF if there
if (iis.read() != 0x0A) {
iis.seek(0);
}
reader.setInput(iis);
ImageReadParam irp = reader.getDefaultReadParam();
irp.setSourceSubsampling(options.getSubsamplingX(), options.getSubsamplingY(), options.getSubsamplingOffsetX(), options.getSubsamplingOffsetY());
irp.setSourceRegion(options.getSourceRegion());
options.setFilterSubsampled(true);
String numChannels = getNumChannels(reader);
// get the raster using horrible JAI workarounds
ImageIO.setUseCache(false);
Raster raster;
// after "can't get metadata" because "no meta" file was CMYK
if ("3".equals(numChannels) || numChannels.isEmpty()) {
try {
// I'd like to use ImageReader#readRaster but it is buggy and can't read RGB correctly
BufferedImage image = reader.read(0, irp);
raster = image.getRaster();
} catch (IIOException e) {
// JAI can't read CMYK JPEGs using ImageReader#read or ImageIO.read but
// fortunately ImageReader#readRaster isn't buggy when reading 4-channel files
LOG.debug("Couldn't read use read() for RGB image - using readRaster() as fallback", e);
raster = reader.readRaster(0, irp);
}
} else {
// JAI can't read CMYK JPEGs using ImageReader#read or ImageIO.read but
// fortunately ImageReader#readRaster isn't buggy when reading 4-channel files
raster = reader.readRaster(0, irp);
}
// special handling for 4-component images
if (raster.getNumBands() == 4) {
// get APP14 marker
Integer transform;
try {
transform = getAdobeTransform(reader.getImageMetadata(0));
} catch (IIOException | NegativeArraySizeException e) {
// we really tried asking nicely, now we're using brute force.
LOG.debug("Couldn't read usÃng getAdobeTransform() - using getAdobeTransformByBruteForce() as fallback", e);
transform = getAdobeTransformByBruteForce(iis);
}
int colorTransform = transform != null ? transform : 0;
// 0 = Unknown (RGB or CMYK), 1 = YCbCr, 2 = YCCK
switch(colorTransform) {
case 0:
// already CMYK
break;
case 1:
raster = fromYCbCrtoCMYK(raster);
break;
case 2:
raster = fromYCCKtoCMYK(raster);
break;
default:
throw new IllegalArgumentException("Unknown colorTransform");
}
} else if (raster.getNumBands() == 3) {
// BGR to RGB
raster = fromBGRtoRGB(raster);
}
DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
decoded.write(dataBuffer.getData());
} finally {
reader.dispose();
}
return new DecodeResult(parameters);
}
use of javax.imageio.IIOException in project robolectric by robolectric.
the class ImageUtil method getImageFromStream.
static RobolectricBufferedImage getImageFromStream(String fileName, InputStream is) {
if (!initialized) {
// Stops ImageIO from creating temp files when reading images
// from input stream.
ImageIO.setUseCache(false);
initialized = true;
}
String format = null;
try {
ImageInputStream imageStream = createImageInputStream(is);
Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
if (!readers.hasNext()) {
return null;
}
ImageReader reader = readers.next();
try {
reader.setInput(imageStream);
format = reader.getFormatName();
int minIndex = reader.getMinIndex();
BufferedImage image = reader.read(minIndex);
return RobolectricBufferedImage.create(image, ("image/" + format).toLowerCase());
} finally {
reader.dispose();
}
} catch (IOException e) {
Throwable cause = e.getCause();
if (FORMAT_NAME_PNG.equalsIgnoreCase(format) && cause instanceof IIOException && cause.getMessage() != null && cause.getMessage().contains("Invalid chunk length")) {
String pngFileName = "(" + (fileName == null ? "not given PNG file name" : fileName) + ")";
System.err.println("The PNG file" + pngFileName + " cannot be decoded. This may be due to an OpenJDK issue with certain PNG files." + " See https://github.com/robolectric/robolectric/issues/6812 for more details.");
}
throw new RuntimeException(e);
}
}
use of javax.imageio.IIOException in project plantuml-server by plantuml.
the class ProxyServlet method doGet.
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
final String fmt = request.getParameter("fmt");
final String source = request.getParameter("src");
final String index = request.getParameter("idx");
final URL srcUrl;
// Check if the src URL is valid
try {
srcUrl = new URL(source);
} catch (MalformedURLException mue) {
mue.printStackTrace();
return;
}
// generate the response
String diagmarkup = getSource(srcUrl);
SourceStringReader reader = new SourceStringReader(diagmarkup);
int n = index == null ? 0 : Integer.parseInt(index);
List<BlockUml> blocks = reader.getBlocks();
BlockUml block = blocks.get(n);
Diagram diagram = block.getDiagram();
UmlSource umlSrc = diagram.getSource();
String uml = umlSrc.getPlainString();
// System.out.println("uml=" + uml);
// generate the response
DiagramResponse dr = new DiagramResponse(response, getOutputFormat(fmt), request);
try {
dr.sendDiagram(uml, 0);
} catch (IIOException iioe) {
// Browser has closed the connection, so the HTTP OutputStream is closed
// Silently catch the exception to avoid annoying log
}
dr = null;
}
Aggregations