private static double readRational(final ImageInputStream stream) throws IOException { int numerator = stream.readUnsignedShort(); int denominator = stream.readUnsignedShort(); return denominator != 0 ? numerator / (double) denominator : 1; }
private int readNumber() throws IOException { final int Ld = input.readUnsignedShort(); if (Ld != 4) { throw new IOException("ERROR: Define number format throw new IOException [Ld!=4]"); } return input.readUnsignedShort(); }
private static Calendar readDate(final ImageInputStream stream) throws IOException { Calendar calendar = Calendar.getInstance(); calendar.clear(); int month = stream.readUnsignedShort(); int date = stream.readUnsignedShort(); int year = stream.readUnsignedShort(); int hourOfDay = stream.readUnsignedShort(); int minute = stream.readUnsignedShort(); int second = stream.readUnsignedShort(); // Unused if (month == 0 && year == 0 && date == 0 && hourOfDay == 0 && minute == 0 && second == 0) { return null; } calendar.set(year, month - 1, date, hourOfDay, minute, second); return calendar; }
private boolean isPICT(final ImageInputStream pStream) throws IOException { // TODO: Need to validate better... // Size may be 0, so we can't use this for validation... pStream.readUnsignedShort(); // Sanity check bounding box int y1 = pStream.readUnsignedShort(); int x1 = pStream.readUnsignedShort(); // TODO: Figure out if frame can ever start at negative bounds... // if (x1 != 0 || y1 != 0) { // return false; // } int y2 = pStream.readUnsignedShort(); int x2 = pStream.readUnsignedShort(); if (x2 - x1 < 0 || y2 - y1 < 0) { return false; } int magic = pStream.readInt(); return (magic & 0xffff0000) == PICT.MAGIC_V1 || magic == PICT.MAGIC_V2; }
private void readFileHeader() throws IOException { imageInput.setByteOrder(ByteOrder.LITTLE_ENDIAN); // Read file header imageInput.readUnsignedShort(); // Reserved // Should be same as type as the provider int type = imageInput.readUnsignedShort(); int imageCount = imageInput.readUnsignedShort(); // Read directory directory = Directory.read(type, imageCount, imageInput); }
static void readSOI(final ImageInputStream stream) throws IOException { if (stream.readUnsignedShort() != JPEG.SOI) { throw new IIOException("Not a JPEG stream"); } }
private Scan readScan() throws IOException { int length = input.readUnsignedShort(); return Scan.read(input, length); }
private long readEntryCount(final ImageInputStream pInput) throws IOException { return longOffsets ? pInput.readLong() : pInput.readUnsignedShort(); }
PSDGlobalLayerMask(final ImageInputStream pInput, final long globalLayerMaskLength) throws IOException { colorSpace = pInput.readUnsignedShort(); // Undocumented pInput.readFully(colors, 0, colors.length); opacity = pInput.readUnsignedShort(); // 0-100 // Kind: 0: Selected (ie inverted), 1: Color protected, 128: Use value stored per layer (actually, the value is 80, not 0x80) kind = pInput.readUnsignedByte(); // Skip "Variable: Filler zeros" pInput.skipBytes(globalLayerMaskLength - 17); }
@Override protected void readData(final ImageInputStream pInput) throws IOException { version = pInput.readUnsignedShort(); cropMasks = pInput.readBoolean(); field = pInput.readUnsignedByte(); // TODO: Is this really pad? bleedWidth = pInput.readUnsignedInt(); bleedScale = pInput.readUnsignedShort(); pInput.skipBytes(size - 10); }
public static Scan read(final ImageInputStream data) throws IOException { int length = data.readUnsignedShort(); return read(new SubImageInputStream(data, length), length); }
static Frame read(final int marker, final ImageInputStream data) throws IOException { int length = data.readUnsignedShort(); return read(marker, new SubImageInputStream(data, length), length); }
private void streamInit() throws IOException { stream.seek(0); try { int soi = stream.readUnsignedShort(); if (soi != JPEG.SOI) { throw new IIOException(String.format("Not a JPEG stream (starts with: 0x%04x, expected SOI: 0x%04x)", soi, JPEG.SOI)); } segment = new Segment(soi, 0, 0, 2); segments.add(segment); currentSegment = segments.size() - 1; // 0 } catch (EOFException eof) { throw new IIOException(String.format("Not a JPEG stream (short stream. expected SOI: 0x%04x)", JPEG.SOI), eof); } }
private static boolean isAppSegmentWithId(final String segmentId, final ImageInputStream stream) throws IOException { notNull(segmentId, "segmentId"); stream.mark(); try { int length = stream.readUnsignedShort(); // Length including length field itself byte[] data = new byte[Math.min(segmentId.length() + 1, length - 2)]; stream.readFully(data); return segmentId.equals(asNullTerminatedAsciiString(data, 0)); } finally { stream.reset(); } }
@Override public Directory read(final ImageInputStream input) throws IOException { Validate.notNull(input, "input"); Map<Short, Entry> entries = new LinkedHashMap<>(); // 0x1c identifies start of a tag while (input.read() == 0x1c) { short tagId = input.readShort(); int tagByteCount = input.readUnsignedShort(); boolean array = IPTC.Tags.isArray(tagId); Entry entry = readEntry(input, tagId, tagByteCount, array, array ? entries.get(tagId) : null); if (entry != null) { entries.put(tagId, entry); } } return new IPTCDirectory(entries.values()); }
public boolean canDecodeInput(final Object pSource) throws IOException { if (!(pSource instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream) pSource; stream.mark(); try { if (stream.readInt() == PSD.SIGNATURE_8BPS) { int version = stream.readUnsignedShort(); switch (version) { case PSD.VERSION_PSD: case PSD.VERSION_PSB: break; default: return false; } return true; } return false; } finally { stream.reset(); } }
@Test public void testStreamRealData() throws IOException { ImageInputStream stream = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/invalid-icc-duplicate-sequence-numbers-rgb-internal-kodak-srgb-jfif.jpg"))); assertEquals(JPEG.SOI, stream.readUnsignedShort()); assertEquals(JPEG.DQT, stream.readUnsignedShort()); }
@Override protected void readData(final ImageInputStream pInput) throws IOException { format = pInput.readInt(); width = pInput.readInt(); height = pInput.readInt(); // This data isn't really useful, unless we're dealing with raw bytes widthBytes = pInput.readInt(); int totalSize = pInput.readInt(); // Hmm.. Is this really useful at all? // Consistency check int sizeCompressed = pInput.readInt(); if (sizeCompressed != (size - 28)) { throw new IIOException("Corrupt thumbnail in PSD document"); } // According to the spec, only 24 bits and 1 plane is supported int bits = pInput.readUnsignedShort(); int planes = pInput.readUnsignedShort(); if (bits != 24 && planes != 1) { // TODO: Warning/Exception } data = new byte[sizeCompressed]; pInput.readFully(data); }
static TGAExtensions read(final ImageInputStream stream) throws IOException { int extSize = stream.readUnsignedShort(); int softwareVersion = stream.readUnsignedShort(); int softwareLetter = stream.readByte();
static boolean canDecodeAs(final Object pSource, final int magic) throws IOException { if (!(pSource instanceof ImageInputStream)) { return false; } ImageInputStream stream = (ImageInputStream) pSource; stream.mark(); try { byte[] bom = new byte[2]; stream.readFully(bom); ByteOrder originalOrder = stream.getByteOrder(); try { if (bom[0] == 'I' && bom[1] == 'I') { stream.setByteOrder(ByteOrder.LITTLE_ENDIAN); } else if (bom[0] == 'M' && bom[1] == 'M') { stream.setByteOrder(ByteOrder.BIG_ENDIAN); } else { return false; } return stream.readUnsignedShort() == magic; } finally { stream.setByteOrder(originalOrder); } } finally { stream.reset(); } }