/** * Gets a list of <code>IIOImage</code> objects for an image file. * * @param imageFile input image file. It can be any of the supported formats, including TIFF, * JPEG, GIF, PNG, BMP, JPEG, and PDF if GPL Ghostscript is installed * @return a list of <code>IIOImage</code> objects * @throws Exception */ public static List<IIOImage> getIIOImageList(File imageFile) throws IOException { File workingTiffFile = null; ImageReader reader = null; ImageInputStream iis = null; try { // convert PDF to TIFF if (imageFile.getName().toLowerCase().endsWith(".pdf")) { workingTiffFile = PdfUtilities.convertPdf2Tiff(imageFile); imageFile = workingTiffFile; } List<IIOImage> iioImageList = new ArrayList<IIOImage>(); String imageFileName = imageFile.getName(); String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1); if (imageFormat.matches("(pbm|pgm|ppm)")) { imageFormat = "pnm"; } else if (imageFormat.equals("jp2")) { imageFormat = "jpeg2000"; } Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat); reader = readers.next(); if (reader == null) { throw new RuntimeException( "Need to install JAI Image I/O package.\nhttps://jai-imageio.dev.java.net"); } iis = ImageIO.createImageInputStream(imageFile); reader.setInput(iis); int imageTotal = reader.getNumImages(true); for (int i = 0; i < imageTotal; i++) { // IIOImage oimage = new IIOImage(reader.read(i), null, // reader.getImageMetadata(i)); IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam()); iioImageList.add(oimage); } return iioImageList; } finally { try { if (iis != null) { iis.close(); } if (reader != null) { reader.dispose(); } } catch (Exception e) { // ignore } if (workingTiffFile != null && workingTiffFile.exists()) { workingTiffFile.delete(); } } }
/** * Creates a list of TIFF image files from an image file. It basically converts images of other * formats to TIFF format, or a multi-page TIFF image to multiple TIFF image files. * * @param imageFile input image file * @param index an index of the page; -1 means all pages, as in a multi-page TIFF image * @return a list of TIFF image files * @throws Exception */ public static List<File> createTiffFiles(File imageFile, int index) throws IOException { List<File> tiffFiles = new ArrayList<File>(); String imageFileName = imageFile.getName(); String imageFormat = imageFileName.substring(imageFileName.lastIndexOf('.') + 1); Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(imageFormat); ImageReader reader = readers.next(); if (reader == null) { throw new RuntimeException( "Need to install JAI Image I/O package.\nhttps://jai-imageio.dev.java.net"); } ImageInputStream iis = ImageIO.createImageInputStream(imageFile); reader.setInput(iis); // Read the stream metadata // IIOMetadata streamMetadata = reader.getStreamMetadata(); // Set up the writeParam TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US); tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_DISABLED); // Get tif writer and set output to file Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(TIFF_FORMAT); ImageWriter writer = writers.next(); // Read the stream metadata IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(tiffWriteParam); int imageTotal = reader.getNumImages(true); for (int i = 0; i < imageTotal; i++) { // all if index == -1; otherwise, only index-th if (index == -1 || i == index) { // BufferedImage bi = reader.read(i); // IIOImage oimage = new IIOImage(bi, null, reader.getImageMetadata(i)); IIOImage oimage = reader.readAll(i, reader.getDefaultReadParam()); File tiffFile = File.createTempFile(OUTPUT_FILE_NAME, TIFF_EXT); ImageOutputStream ios = ImageIO.createImageOutputStream(tiffFile); writer.setOutput(ios); writer.write(streamMetadata, oimage, tiffWriteParam); ios.close(); tiffFiles.add(tiffFile); } } writer.dispose(); reader.dispose(); return tiffFiles; }
private static MalletTexture.Meta createMeta(final String _path, final InputStream _stream) { try (final ImageInputStream in = ImageIO.createImageInputStream(_stream)) { final Iterator<ImageReader> readers = ImageIO.getImageReaders(in); if (readers.hasNext()) { final ImageReader reader = readers.next(); try { reader.setInput(in); // Add additional Meta information to MalletTexture as // and when it becomes needed. It shouldn't hold too much (RGB, RGBA, Mono, endinese, // 32, 24-bit, etc) // data as a game-developer shouldn't need detailed information. return new MalletTexture.Meta(_path, reader.getHeight(0), reader.getWidth(0)); } finally { reader.dispose(); } } } catch (IOException ex) { ex.printStackTrace(); } return null; }
public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) { super.setInput(input, seekForwardOnly, ignoreMetadata); iis = (ImageInputStream) input; // Always works if (iis != null) iis.setByteOrder(ByteOrder.LITTLE_ENDIAN); gotHeader = false; }