/** * Searches for the location of the code continuous stream in the encoded image * * @return returns the size of the header from the original encoded image */ private int computeHeaderSize(){ int headerSize = 0; try (FileImageInputStream file = new FileImageInputStream(this.fileOutput)) { this.boxReader = new BoxReader(file, file.length(), new BoxListener()); this.boxReader.getFileLength(); Box box; do { box = this.boxReader.readBox(); if (box.getSymbol().equals(CONTIGUOUS_CODESTREAM)) { headerSize = (int) box.getPosition(); } } while (!box.getSymbol().equals(CONTIGUOUS_CODESTREAM)); } catch (IOException e) { logger.warning(e.getMessage()); } return headerSize; }
public static void main(String[] args) { try { File f = new File("ImageInputStreamFromRAF.tmp"); RandomAccessFile raf = new RandomAccessFile(f, "rw"); ImageInputStream istream = ImageIO.createImageInputStream(raf); ImageOutputStream ostream = ImageIO.createImageOutputStream(raf); f.delete(); if (istream == null) { throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) returned null!"); } if (ostream == null) { throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) returned null!"); } if (!(istream instanceof FileImageInputStream)) { throw new RuntimeException("ImageIO.createImageInputStream(RandomAccessFile) did not return a FileImageInputStream!"); } if (!(ostream instanceof FileImageOutputStream)) { throw new RuntimeException("ImageIO.createImageOutputStream(RandomAccessFile) did not return a FileImageOutputStream!"); } } catch (IOException ioe) { throw new RuntimeException("Unexpected IOException: " + ioe); } }
private Dimension getImageDim(final String path, final String suffix) throws ProcessingException { Dimension result = null; Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); try { ImageInputStream stream = new FileImageInputStream(new File(path)); reader.setInput(stream); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); result = new Dimension(width, height); } catch (IOException e) { throw new ProcessingException(e.getMessage()); } finally { reader.dispose(); } } else { throw new ProcessingException("No reader found for given format: " + suffix); } return result; }
/** * Gets image dimensions for given file * * @param imgFile image file * @return dimensions of image * @throws IOException if the file is not a known image */ public static Dimension getImageDimension(File imgFile) throws IOException { int pos = imgFile.getName().lastIndexOf("."); if (pos == -1) throw new IOException("No extension for file: " + imgFile.getAbsolutePath()); String suffix = imgFile.getName().substring(pos + 1); Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); try { ImageInputStream stream = new FileImageInputStream(imgFile); reader.setInput(stream); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); return new Dimension(width, height); } finally { reader.dispose(); } } throw new IOException("Not a known image file: " + imgFile.getAbsolutePath()); }
private BufferedImage loadImage(File file) { Iterator<ImageReader> it = ImageIO. getImageReadersByMIMEType("image/png"); if (it.hasNext()) { try { ImageReader reader = it.next(); reader.setInput(new FileImageInputStream(file)); BufferedImage image = reader.read(0); return image; } catch (IOException ex) { Logger.getLogger(BlockView.class.getName()).log(Level.SEVERE, null, ex); } } return null; }
public static boolean doesImageFitBox(FileHandle fileHandle, float width, float height) { boolean result = false; String suffix = fileHandle.extension(); Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); try { ImageInputStream stream = new FileImageInputStream(fileHandle.file()); reader.setInput(stream); int imageWidth = reader.getWidth(reader.getMinIndex()); int imageHeight = reader.getHeight(reader.getMinIndex()); result = imageWidth < width && imageHeight < height; } catch (IOException e) { Gdx.app.error(Utils.class.getName(), "error checking image dimensions", e); } finally { reader.dispose(); } } else { Gdx.app.error(Utils.class.getName(), "No reader available to check image dimensions"); } return result; }
@Override protected Void doInBackground() throws Exception { if (files.isEmpty()) { return null; } Iterator<ImageReader> iterator = ImageIO.getImageReadersBySuffix("dds"); if (iterator.hasNext()){ ImageReader imageReader = iterator.next(); imageReader.setInput(new FileImageInputStream(file)); mipMapMax = imageReader.getNumImages(true); if (imageIndex > mipMapMax || imageIndex < 0) { throw new IOException("imageIndex ("+imageIndex+") not found"); } BufferedImage image = imageReader.read(imageIndex); format = imageReader.getFormatName(); if (type == ColorType.YCOCG) DDSUtil.decodeYCoCg(image); if (type == ColorType.YCOCG_SCALED) DDSUtil.decodeYCoCgScaled(image); if (type == ColorType.ALPHA_EXPONENT) DDSUtil.decodeAlphaExponent(image); if (!alpha || !red || !green || !blue) DDSUtil.showColors(image, alpha, red, green, blue); item = new Item(image, mipMapMax, file); } return null; }
public boolean isImage(String path) { final String ext = FilenameUtils.getExtension(path); if (ext.length() < 2) return false; final Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix(ext); if (it.hasNext()) { final ImageReader r = it.next(); try { ImageInputStream stream = new FileImageInputStream(new File(path)); r.setInput(stream); int width = r.getWidth(r.getMinIndex()); int height = r.getHeight(r.getMinIndex()); r.dispose(); stream.close(); if ((width > 0) && (height > 0)) return true; } catch (IOException e) { return false; } finally { r.dispose(); } } return false; }
public static Resolution getImageResolution(final String path) throws IOException { Resolution result = null; String suffix = getFileSuffix(path); Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); ImageInputStream stream = new FileImageInputStream(new File(path)); reader.setInput(stream); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); result = new Resolution(width, height); reader.dispose(); } else { logger.error("No reader found for given format: " + suffix); } return result; }
private static Dimension getImageDimension(File imgFile) throws IOException { int pos = imgFile.getName().lastIndexOf("."); if (pos == -1) throw new IOException("No extension for file: " + imgFile.getAbsolutePath()); String suffix = imgFile.getName().substring(pos + 1); Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); while (iter.hasNext()) { ImageReader reader = iter.next(); try { ImageInputStream stream = new FileImageInputStream(imgFile); reader.setInput(stream); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); return new Dimension(width, height); } catch (IOException e) { LOG.log(Level.WARNING, "Error reading: " + imgFile.getAbsolutePath(), e); } finally { reader.dispose(); } } throw new IOException("Not a known image file: " + imgFile.getAbsolutePath()); }
public JAXBProviderTests() { super(); //Create a DataSource from a String String string = "Sending a JAXB generated string object to Source Provider endpoint"; stringDS = new ByteArrayDataSource(string.getBytes(),"text/plain"); try { //Create a DataSource from an image File file = new File(imageResourceDir + File.separator + "test.jpg"); ImageInputStream fiis = new FileImageInputStream(file); Image image = ImageIO.read(fiis); imageDS = new DataSourceImpl("image/jpeg", "test.jpg", image); } catch (Exception e) { throw new RuntimeException(e); } }
public void setUp() throws Exception { String imageResourceDir = System.getProperty("basedir",".")+"/"+"test-resources"+File.separator+"image"; File file = new File(imageResourceDir+File.separator+"test.jpg"); ImageInputStream fiis = new FileImageInputStream(file); Image image = ImageIO.read(fiis); imageDS = new DataSourceImpl("image/jpeg","test.jpg",image); String textResourceDir = System.getProperty("basedir",".")+"/"+"test/org/apache/axis2/jaxws/xmlhttp"; File file2 = new File(textResourceDir+File.separator+"README.txt"); txtDS = new FileDataSource(file2); String resourceDir = System.getProperty("basedir",".")+"/"+"test-resources"; File file3 = new File(resourceDir+File.separator+"log4j.properties"); attachmentDS = new FileDataSource(file3); }
@Test public void checkBadWriter() throws NitfFormatException { NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(getInputStream("/WithBE.ntf"))); SlottedParseStrategy parseStrategy = new SlottedParseStrategy(SlottedParseStrategy.ALL_SEGMENT_DATA); HeapStrategyConfiguration heapStrategyConfiguration = new HeapStrategyConfiguration(length -> length > ABOUT_100K); HeapStrategy<ImageInputStream> imageDataStrategy = new ConfigurableHeapStrategy<>(heapStrategyConfiguration, file -> new FileImageInputStream(file), is -> new MemoryCacheImageInputStream(is)); parseStrategy.setImageHeapStrategy(imageDataStrategy); NitfParser.parse(reader, parseStrategy); // Introduce deliberate issue // Introduce deliberate issue ImageSegment imageSegment = parseStrategy.getDataSource().getImageSegments().get(0); TargetId originalTargetId = imageSegment.getImageTargetId(); TargetIdImpl newTargetId = new TargetIdImpl(); newTargetId.setBasicEncyclopediaNumber(originalTargetId.getBasicEncyclopediaNumber()); newTargetId.setOSuffix(originalTargetId.getOSuffix()); newTargetId.setCountryCode(null); imageSegment.setImageTargetId(newTargetId); NitfWriter writer = new NitfFileWriter(parseStrategy.getDataSource(), "checkBadWriter.ntf"); assertEquals(0, LOGGER.getLoggingEvents().size()); writer.write(); assertThat(LOGGER.getLoggingEvents(), is(Arrays.asList(LoggingEvent.error("Could not write", "Cannot generate string target identifier with null country code")))); }
@Test public void checkBadStreamWriter() throws NitfFormatException, FileNotFoundException { OutputStream outputStream = new FileOutputStream("checkBadStreamWriter.ntf"); NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(getInputStream("/WithBE.ntf"))); SlottedParseStrategy parseStrategy = new SlottedParseStrategy(SlottedParseStrategy.ALL_SEGMENT_DATA); HeapStrategyConfiguration heapStrategyConfiguration = new HeapStrategyConfiguration(length -> length > ABOUT_100K); HeapStrategy<ImageInputStream> imageDataStrategy = new ConfigurableHeapStrategy<>(heapStrategyConfiguration, file -> new FileImageInputStream(file), is -> new MemoryCacheImageInputStream(is)); parseStrategy.setImageHeapStrategy(imageDataStrategy); NitfParser.parse(reader, parseStrategy); // Introduce deliberate issue ImageSegment imageSegment = parseStrategy.getDataSource().getImageSegments().get(0); TargetId originalTargetId = imageSegment.getImageTargetId(); TargetIdImpl newTargetId = new TargetIdImpl(); newTargetId.setBasicEncyclopediaNumber(originalTargetId.getBasicEncyclopediaNumber()); newTargetId.setOSuffix(originalTargetId.getOSuffix()); newTargetId.setCountryCode(null); imageSegment.setImageTargetId(newTargetId); NitfWriter writer = new NitfOutputStreamWriter(parseStrategy.getDataSource(), outputStream); assertEquals(0, LOGGER.getLoggingEvents().size()); writer.write(); assertThat(LOGGER.getLoggingEvents(), is(Arrays.asList(LoggingEvent.error("Could not write", "Cannot generate string target identifier with null country code")))); }
protected void roundTripFile(String sourceFileName) throws URISyntaxException, NitfFormatException, IOException { String outputFile = FilenameUtils.getName(sourceFileName); NitfReader reader = new NitfInputStreamReader(new BufferedInputStream(getInputStream(sourceFileName))); SlottedParseStrategy parseStrategy = new SlottedParseStrategy(SlottedParseStrategy.ALL_SEGMENT_DATA); HeapStrategyConfiguration heapStrategyConfiguration = new HeapStrategyConfiguration(length -> length > ABOUT_100K); HeapStrategy<ImageInputStream> imageDataStrategy = new ConfigurableHeapStrategy<>(heapStrategyConfiguration, file -> new FileImageInputStream(file), is -> new MemoryCacheImageInputStream(is)); parseStrategy.setImageHeapStrategy(imageDataStrategy); NitfParser.parse(reader, parseStrategy); NitfWriter writer = new NitfFileWriter(parseStrategy.getDataSource(), outputFile); writer.write(); assertTrue(FileUtils.contentEquals(new File(getClass().getResource(sourceFileName).toURI()), new File(outputFile))); assertTrue(new File(outputFile).delete()); // Do the same again, but with stream writing try ( OutputStream outputStream = new FileOutputStream(outputFile)) { writer = new NitfOutputStreamWriter(parseStrategy.getDataSource(), outputStream); writer.write(); assertTrue(FileUtils.contentEquals(new File(getClass().getResource(sourceFileName).toURI()), new File(outputFile))); } assertTrue(new File(outputFile).delete()); }
private java.awt.Dimension getImageSize(File image) throws IOException { String filename = image.getName(); int p = filename.lastIndexOf('.'); if (p == -1) { return null; } String suffix = filename.substring(p + 1).toLowerCase(); Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix(suffix); if (readers.hasNext()) { ImageReader reader = readers.next(); try { try (ImageInputStream in = new FileImageInputStream(image)) { reader.setInput(in); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); return new java.awt.Dimension(width, height); } } finally { reader.dispose(); } } else { return null; } }
public ImageInputStream createInputStreamInstance(Object input, boolean useCache, File cacheDir) { if (input instanceof File) { try { return new FileImageInputStream((File)input); } catch (Exception e) { return null; } } else { throw new IllegalArgumentException(); } }
public ImageInputStream createInputStreamInstance(Object input, boolean useCache, File cacheDir) { if (input instanceof RandomAccessFile) { try { return new FileImageInputStream((RandomAccessFile)input); } catch (Exception e) { return null; } } else { throw new IllegalArgumentException ("input not a RandomAccessFile!"); } }
public static BufferedImage readTestImage(String fileName, BufferedImage dest, Rectangle srcROI) { BufferedImage bi = null; try { FileImageInputStream is = new FileImageInputStream(new File(fileName)); ImageReader reader = (ImageReader)ImageIO.getImageReaders(is).next(); ImageReadParam param = reader.getDefaultReadParam(); if (dest != null) { param.setDestination(dest); } if (srcROI != null) { param.setSourceRegion(srcROI); } reader.setInput(is); bi = reader.read(0, param); } catch (IOException e) { System.err.println("Error " + e + " when reading file: " + fileName); throw new RuntimeException(e); } return bi; }
/** Reads image in the specified image file. For multiimage tiff files, the first image is read. */ public static BufferedImage readImage(File imgFile) throws FileNotFoundException, IOException { ImageInputStream iis = new FileImageInputStream(imgFile); BufferedImage img; try{ img = readImage(iis); } catch(IOException e){ throw new IOException("Could not read image: "+imgFile.getAbsolutePath(), e); } return img; }
public static Dimension readImageDimensionsWithImageIO(File imgFile) throws FileNotFoundException, IOException { Dimension dim = null; logger.debug("Loading file with imageIO..."); ImageInputStream iis = new FileImageInputStream(imgFile); final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); while (readers.hasNext()) { ImageReader reader = readers.next(); try { logger.debug("reader format = "+reader.getFormatName()); reader.setInput(iis); final int xDim = reader.getWidth(0); final int yDim = reader.getHeight(0); logger.debug("Success with reader impl: " + reader.getClass().getCanonicalName()); dim = new Dimension(xDim, yDim); } catch(Exception e) { logger.warn("Could not read image dimensions with reader: "+reader.getFormatName()+": " + e.getMessage()); logger.debug("Cause: ", e); logger.debug("Reader impl: " + reader.getClass().getCanonicalName()); } finally { reader.dispose(); } if(dim != null){ break; } } if(dim == null) { throw new IOException("Could not read image dimensions with ImageIO."); } return dim; }
public BufferedImage load(FileObject file) throws IOException, URISyntaxException { if (file.getExt().equalsIgnoreCase("tga")) { ImageInputStream in = new FileImageInputStream(new File(file.toURI())); TGAImageReaderSpi spi = new TGAImageReaderSpi(); TGAImageReader rea = new TGAImageReader(spi); rea.setInput(in); return rea.read(0); } else { BufferedImage image = ImageIO.read(file.getInputStream()); return image; } }