public HeifHandler(Metadata metadata) { this.metadata = metadata; this.directory = getDirectory(); metadata.addDirectory(directory); }
public Mp4Handler(@NotNull Metadata metadata) { this.metadata = metadata; this.directory = getDirectory(); metadata.addDirectory(directory); }
public AviRiffHandler(@NotNull Metadata metadata) { _directory = new AviDirectory(); metadata.addDirectory(_directory); }
public QuickTimeHandler(@NotNull Metadata metadata) { this.metadata = metadata; this.directory = getDirectory(); metadata.addDirectory(directory); }
public WavRiffHandler(@NotNull Metadata metadata) { _directory = new WavDirectory(); metadata.addDirectory(_directory); }
protected void pushDirectory(@NotNull Class<? extends Directory> directoryClass) { Directory newDirectory; try { newDirectory = directoryClass.newInstance(); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } // If this is the first directory, don't add to the stack if (_currentDirectory == null) { // Apply any pending root parent to this new directory if (_rootParentDirectory != null) { newDirectory.setParent(_rootParentDirectory); _rootParentDirectory = null; } } else { // The current directory is pushed onto the stack, and set as the new directory's parent _directoryStack.push(_currentDirectory); newDirectory.setParent(_currentDirectory); } _currentDirectory = newDirectory; _metadata.addDirectory(_currentDirectory); }
public void read(@NotNull File file, @NotNull Metadata metadata) throws IOException { if (!file.isFile()) throw new IOException("File object must reference a file"); if (!file.exists()) throw new IOException("File does not exist"); if (!file.canRead()) throw new IOException("File is not readable"); FileSystemDirectory directory = metadata.getFirstDirectoryOfType(FileSystemDirectory.class); if (directory == null) { directory = new FileSystemDirectory(); metadata.addDirectory(directory); } directory.setString(FileSystemDirectory.TAG_FILE_NAME, file.getName()); directory.setLong(FileSystemDirectory.TAG_FILE_SIZE, file.length()); directory.setDate(FileSystemDirectory.TAG_FILE_MODIFIED_DATE, new Date(file.lastModified())); } }
private static void readGifExtensionBlock(SequentialReader reader, Metadata metadata) throws IOException { byte extensionLabel = reader.getInt8(); short blockSizeBytes = reader.getUInt8(); long blockStartPos = reader.getPosition(); switch (extensionLabel) { case (byte) 0x01: Directory plainTextBlock = readPlainTextBlock(reader, blockSizeBytes); if (plainTextBlock != null) metadata.addDirectory(plainTextBlock); break; case (byte) 0xf9: metadata.addDirectory(readControlBlock(reader, blockSizeBytes)); break; case (byte) 0xfe: metadata.addDirectory(readCommentBlock(reader, blockSizeBytes)); break; case (byte) 0xff: readApplicationExtensionBlock(reader, blockSizeBytes, metadata); break; default: metadata.addDirectory(new ErrorDirectory(String.format("Unsupported GIF extension block with type 0x%02X.", extensionLabel))); break; } long skipCount = blockStartPos + blockSizeBytes - reader.getPosition(); if (skipCount > 0) reader.skip(skipCount); }
/** * Reads metadata from an {@link InputStream} of known length. * * @param inputStream a stream from which the file data may be read. The stream must be positioned at the * beginning of the file's data. * @param streamLength the length of the stream, if known, otherwise -1. * @return a populated {@link Metadata} object containing directories of tags with values and any processing errors. * @throws ImageProcessingException if the file type is unknown, or for general processing errors. */ @NotNull public static Metadata readMetadata(@NotNull final InputStream inputStream, final long streamLength) throws ImageProcessingException, IOException { BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream)inputStream : new BufferedInputStream(inputStream); FileType fileType = FileTypeDetector.detectFileType(bufferedInputStream); Metadata metadata = readMetadata(bufferedInputStream, streamLength, fileType); metadata.addDirectory(new FileTypeDirectory(fileType)); return metadata; }
protected void addError(@NotNull String errorMessage, final @NotNull Metadata metadata) { ErrorDirectory directory = metadata.getFirstDirectoryOfType(ErrorDirectory.class); if (directory == null) { metadata.addDirectory(new ErrorDirectory(errorMessage)); } else { directory.addError(errorMessage); } } }
/** * Performs the JFXX data extraction, adding found values to the specified * instance of {@link Metadata}. */ public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata) { JfxxDirectory directory = new JfxxDirectory(); metadata.addDirectory(directory); try { // For JFXX, the tag number is also the offset into the segment directory.setInt(JfxxDirectory.TAG_EXTENSION_CODE, reader.getUInt8(JfxxDirectory.TAG_EXTENSION_CODE)); } catch (IOException me) { directory.addError(me.getMessage()); } } }
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType) { for (byte[] segmentBytes : segments) { JpegCommentDirectory directory = new JpegCommentDirectory(); metadata.addDirectory(directory); // The entire contents of the directory are the comment directory.setStringValue(JpegCommentDirectory.TAG_COMMENT, new StringValue(segmentBytes, null)); } } }
public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segmentType) { JpegDirectory directory = metadata.getFirstDirectoryOfType(JpegDirectory.class); if (directory == null) { ErrorDirectory errorDirectory = new ErrorDirectory(); metadata.addDirectory(errorDirectory); errorDirectory.addError("DNL segment found without SOFx - illegal JPEG format"); return; } SequentialReader reader = new SequentialByteArrayReader(segmentBytes); try { // Only set height from DNL if it's not already defined Integer i = directory.getInteger(JpegDirectory.TAG_IMAGE_HEIGHT); if (i == null || i == 0) { directory.setInt(JpegDirectory.TAG_IMAGE_HEIGHT, reader.getUInt16()); } } catch (IOException ex) { directory.addError(ex.getMessage()); } } }
public void extract(@NotNull SequentialReader reader, @NotNull Metadata metadata) { Directory directory = new AdobeJpegDirectory(); metadata.addDirectory(directory); try { reader.setMotorolaByteOrder(false); if (!reader.getString(PREAMBLE.length()).equals(PREAMBLE)) { directory.addError("Invalid Adobe JPEG data header."); return; } directory.setInt(AdobeJpegDirectory.TAG_DCT_ENCODE_VERSION, reader.getUInt16()); directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS0, reader.getUInt16()); directory.setInt(AdobeJpegDirectory.TAG_APP14_FLAGS1, reader.getUInt16()); directory.setInt(AdobeJpegDirectory.TAG_COLOR_TRANSFORM, reader.getInt8()); } catch (IOException ex) { directory.addError("IO exception processing data: " + ex.getMessage()); } } }
/** * Performs the DHT tables extraction, adding found tables to the specified * instance of {@link Metadata}. */ public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata) { HuffmanTablesDirectory directory = metadata.getFirstDirectoryOfType(HuffmanTablesDirectory.class); if (directory == null) { directory = new HuffmanTablesDirectory(); metadata.addDirectory(directory); } try { while (reader.available() > 0) { byte header = reader.getByte(); HuffmanTableClass tableClass = HuffmanTableClass.typeOf((header & 0xF0) >> 4); int tableDestinationId = header & 0xF; byte[] lBytes = getBytes(reader, 16); int vCount = 0; for (byte b : lBytes) { vCount += (b & 0xFF); } byte[] vBytes = getBytes(reader, vCount); directory.getTables().add(new HuffmanTable(tableClass, tableDestinationId, lBytes, vBytes)); } } catch (IOException me) { directory.addError(me.getMessage()); } directory.setInt(HuffmanTablesDirectory.TAG_NUMBER_OF_TABLES, directory.getTables().size()); }
/** * Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}. * <p> * The extraction is done with Adobe's XMPCore library. */ public void extract(@NotNull final String xmpString, @NotNull Metadata metadata, @Nullable Directory parentDirectory) { XmpDirectory directory = new XmpDirectory(); if (parentDirectory != null) directory.setParent(parentDirectory); try { XMPMeta xmpMeta = XMPMetaFactory.parseFromString(xmpString); directory.setXMPMeta(xmpMeta); } catch (XMPException e) { directory.addError("Error processing XMP data: " + e.getMessage()); } if (!directory.isEmpty()) metadata.addDirectory(directory); }
/** * Performs the Jfif data extraction, adding found values to the specified * instance of {@link Metadata}. */ public void extract(@NotNull final RandomAccessReader reader, @NotNull final Metadata metadata) { JfifDirectory directory = new JfifDirectory(); metadata.addDirectory(directory); try { // For JFIF, the tag number is also the offset into the segment directory.setInt(JfifDirectory.TAG_VERSION, reader.getUInt16(JfifDirectory.TAG_VERSION)); directory.setInt(JfifDirectory.TAG_UNITS, reader.getUInt8(JfifDirectory.TAG_UNITS)); directory.setInt(JfifDirectory.TAG_RESX, reader.getUInt16(JfifDirectory.TAG_RESX)); directory.setInt(JfifDirectory.TAG_RESY, reader.getUInt16(JfifDirectory.TAG_RESY)); directory.setInt(JfifDirectory.TAG_THUMB_WIDTH, reader.getUInt8(JfifDirectory.TAG_THUMB_WIDTH)); directory.setInt(JfifDirectory.TAG_THUMB_HEIGHT, reader.getUInt8(JfifDirectory.TAG_THUMB_HEIGHT)); } catch (IOException me) { directory.addError(me.getMessage()); } } }
public void extract(byte[] segmentBytes, Metadata metadata, JpegSegmentType segmentType) metadata.addDirectory(directory);
/** * Performs the XMP data extraction, adding found values to the specified instance of {@link Metadata}. * <p> * The extraction is done with Adobe's XMPCore library. */ public void extract(@NotNull final byte[] xmpBytes, int offset, int length, @NotNull Metadata metadata, @Nullable Directory parentDirectory) { XmpDirectory directory = new XmpDirectory(); if (parentDirectory != null) directory.setParent(parentDirectory); try { XMPMeta xmpMeta; // If all xmpBytes are requested, no need to make a new ByteBuffer if (offset == 0 && length == xmpBytes.length) { xmpMeta = XMPMetaFactory.parseFromBuffer(xmpBytes); } else { ByteBuffer buffer = new ByteBuffer(xmpBytes, offset, length); xmpMeta = XMPMetaFactory.parse(buffer.getByteStream()); } directory.setXMPMeta(xmpMeta); } catch (XMPException e) { directory.addError("Error processing XMP data: " + e.getMessage()); } if (!directory.isEmpty()) metadata.addDirectory(directory); }
metadata.addDirectory(directory);