use of javax.imageio.ImageTypeSpecifier in project imageio-ext by geosolutions-it.
the class HDF4Test method rasterBandsProperties.
/**
* This test method retrieves properties from each band of the sample
* dataset
*/
@Test
public void rasterBandsProperties() throws FileNotFoundException, IOException {
if (!isLinux && isDriverAvailable)
return;
try {
ImageReader reader = new HDF4ImageReaderSpi().createReaderInstance();
String fileName = "TOVS_DAILY_AM_870330_NG.HDF";
File file = TestData.file(this, fileName);
reader.setInput(file);
final int numImages = 3;
final int startIndex = 2;
int bands;
Iterator<ImageTypeSpecifier> it;
SampleModel sm;
for (int i = startIndex; i < startIndex + numImages; i++) {
it = reader.getImageTypes(i);
ImageTypeSpecifier its;
if (it.hasNext()) {
its = (ImageTypeSpecifier) it.next();
sm = its.getSampleModel();
bands = sm.getNumBands();
double d;
StringBuffer sb = new StringBuffer("RasterBands properties retrieval").append(" Image: ").append(i);
HDF4ImageReader mReader = (HDF4ImageReader) reader;
for (int j = 0; j < bands; j++) {
sb.append(" \n\t Band: ").append(j).append(" --- ");
try {
d = mReader.getNoDataValue(i, j);
sb.append("NoDataV=").append(d);
} catch (IllegalArgumentException iae) {
sb.append(" NoDataV=NotAvailable");
}
try {
d = mReader.getOffset(i, j);
sb.append(" Off=").append(d);
} catch (IllegalArgumentException iae) {
sb.append(" Off=NotAvailable");
}
try {
d = mReader.getScale(i, j);
sb.append(" Scale=").append(d);
} catch (IllegalArgumentException iae) {
sb.append(" Scale=NotAvailable");
}
try {
d = mReader.getMinimum(i, j);
sb.append(" Min=").append(d);
} catch (IllegalArgumentException iae) {
sb.append(" Min=NotAvailable");
}
try {
d = mReader.getMaximum(i, j);
sb.append(" MAX=").append(d);
} catch (IllegalArgumentException iae) {
sb.append(" MAX=NotAvailable");
}
}
LOGGER.info(sb.toString());
}
}
reader.dispose();
} catch (FileNotFoundException fnfe) {
warningMessage();
}
}
use of javax.imageio.ImageTypeSpecifier in project OpenOLAT by OpenOLAT.
the class ImageHelperImpl method calcScaledSize.
private static SizeAndBufferedImage calcScaledSize(ImageInputStream stream, String suffix, int maxWidth, int maxHeight, boolean fill) {
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try {
reader.setInput(stream, true, true);
int width = reader.getWidth(reader.getMinIndex());
int height = reader.getHeight(reader.getMinIndex());
Size size = new Size(width, height, false);
Size scaledSize = computeScaledSize(width, height, maxWidth, maxHeight, fill);
SizeAndBufferedImage all = new SizeAndBufferedImage(size, scaledSize);
int readerMinIndex = reader.getMinIndex();
ImageReadParam param = reader.getDefaultReadParam();
Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
while (imageTypes.hasNext()) {
try {
ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
param.setDestinationType(imageTypeSpecifier);
}
double memoryKB = (width * height * 4) / 1024d;
if (memoryKB > 2000) {
// check limit at 20MB
double free = Runtime.getRuntime().freeMemory() / 1024d;
if (free > memoryKB) {
all.setImage(reader.read(readerMinIndex, param));
} else {
// make sub sampling to save memory
int ratio = (int) Math.round(Math.sqrt(memoryKB / free));
param.setSourceSubsampling(ratio, ratio, 0, 0);
all.setImage(reader.read(readerMinIndex, param));
}
} else {
all.setImage(reader.read(readerMinIndex, param));
}
return all;
} catch (IllegalArgumentException e) {
log.warn(e.getMessage(), e);
}
}
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
reader.dispose();
}
} else {
log.error("No reader found for given format: " + suffix, null);
}
return null;
}
use of javax.imageio.ImageTypeSpecifier in project polymap4-core by Polymap4.
the class ImageEncodeProcessor method imageioEncodeJPEG.
private void imageioEncodeJPEG(Image image, ChunkedResponseOutputStream out) throws IOException {
// this code is from http://forums.sun.com/thread.jspa?threadID=5197061
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setSourceBands(new int[] { 0, 1, 2 });
ColorModel cm = new DirectColorModel(24, // Red
0x00ff0000, // Green
0x0000ff00, // Blue
0x000000ff, // Alpha
0x0);
param.setDestinationType(new ImageTypeSpecifier(cm, cm.createCompatibleSampleModel(1, 1)));
ImageOutputStream imageOut = ImageIO.createImageOutputStream(out);
writer.setOutput(imageOut);
writer.write(null, new IIOImage((RenderedImage) image, null, null), param);
writer.dispose();
imageOut.close();
}
use of javax.imageio.ImageTypeSpecifier in project tess4j by nguyenq.
the class ImageIOHelper method mergeTiff.
/**
* Merges multiple images into one multi-page TIFF image.
*
* @param imageList a list of <code>IIOImage</code> objects
* @param outputTiff the output TIFF file
* @param compressionType valid values: LZW, CCITT T.6, PackBits
* @throws IOException
*/
public static void mergeTiff(List<IIOImage> imageList, File outputTiff, String compressionType) throws IOException {
if (imageList == null || imageList.isEmpty()) {
// if no image
return;
}
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT);
if (!writers.hasNext()) {
throw new RuntimeException(JAI_IMAGE_WRITER_MESSAGE);
}
ImageWriter writer = writers.next();
// Set up the writeParam
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
// tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); // comment out to preserve original sizes
if (compressionType != null) {
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setCompressionType(compressionType);
}
// Get the stream metadata
IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam);
ImageOutputStream ios = ImageIO.createImageOutputStream(outputTiff);
writer.setOutput(ios);
int dpiX = 300;
int dpiY = 300;
for (IIOImage iioImage : imageList) {
// Get the default image metadata.
ImageTypeSpecifier imageType = ImageTypeSpecifier.createFromRenderedImage(iioImage.getRenderedImage());
IIOMetadata imageMetadata = writer.getDefaultImageMetadata(imageType, null);
imageMetadata = setDPIViaAPI(imageMetadata, dpiX, dpiY);
iioImage.setMetadata(imageMetadata);
}
IIOImage firstIioImage = imageList.remove(0);
writer.write(streamMetadata, firstIioImage, tiffWriteParam);
int i = 1;
for (IIOImage iioImage : imageList) {
writer.writeInsert(i++, iioImage, tiffWriteParam);
}
ios.close();
writer.dispose();
}
use of javax.imageio.ImageTypeSpecifier in project pdfbox by apache.
the class JPEGFactory method encodeImageToJPEGStream.
private static void encodeImageToJPEGStream(BufferedImage image, float quality, int dpi, OutputStream out) throws IOException {
// encode to JPEG
ImageOutputStream ios = null;
ImageWriter imageWriter = null;
try {
// find JAI writer
imageWriter = getJPEGImageWriter();
ios = ImageIO.createImageOutputStream(out);
imageWriter.setOutput(ios);
// add compression
JPEGImageWriteParam jpegParam = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
jpegParam.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
jpegParam.setCompressionQuality(quality);
// add metadata
ImageTypeSpecifier imageTypeSpecifier = new ImageTypeSpecifier(image);
IIOMetadata data = imageWriter.getDefaultImageMetadata(imageTypeSpecifier, jpegParam);
Element tree = (Element) data.getAsTree("javax_imageio_jpeg_image_1.0");
Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
jfif.setAttribute("Xdensity", Integer.toString(dpi));
jfif.setAttribute("Ydensity", Integer.toString(dpi));
// 1 = dots/inch
jfif.setAttribute("resUnits", "1");
// write
imageWriter.write(data, new IIOImage(image, null, null), jpegParam);
} finally {
// clean up
IOUtils.closeQuietly(out);
if (ios != null) {
ios.close();
}
if (imageWriter != null) {
imageWriter.dispose();
}
}
}
Aggregations