Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Metadata filter #225

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions Source/com/drew/imaging/ImageMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
import com.drew.lang.RandomAccessStreamReader;
import com.drew.lang.StringUtil;
import com.drew.lang.annotations.NotNull;
import com.drew.lang.annotations.Nullable;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.Tag;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifThumbnailDirectory;
import com.drew.metadata.file.FileMetadataReader;
import com.drew.metadata.filter.MetadataFilter;

import java.io.*;
import java.util.ArrayList;
Expand Down Expand Up @@ -87,7 +89,22 @@ public class ImageMetadataReader
@NotNull
public static Metadata readMetadata(@NotNull final InputStream inputStream) throws ImageProcessingException, IOException
{
return readMetadata(inputStream, -1);
return readMetadata(inputStream, -1, null);
}

/**
* Reads metadata from an {@link InputStream}.
*
* @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 filter a {@link MetadataFilter} or <code>null</code>.
* @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, @Nullable final MetadataFilter filter) throws ImageProcessingException, IOException
{
return readMetadata(inputStream, -1, filter);
}

/**
Expand All @@ -101,6 +118,22 @@ public static Metadata readMetadata(@NotNull final InputStream inputStream) thro
*/
@NotNull
public static Metadata readMetadata(@NotNull final InputStream inputStream, final long streamLength) throws ImageProcessingException, IOException
{
return readMetadata(inputStream, streamLength, null);
}

/**
* 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.
* @param filter a {@link MetadataFilter} or <code>null</code>.
* @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, @Nullable final MetadataFilter filter) throws ImageProcessingException, IOException
{
BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream
? (BufferedInputStream)inputStream
Expand All @@ -109,39 +142,39 @@ public static Metadata readMetadata(@NotNull final InputStream inputStream, fina
FileType fileType = FileTypeDetector.detectFileType(bufferedInputStream);

if (fileType == FileType.Jpeg)
return JpegMetadataReader.readMetadata(bufferedInputStream);
return JpegMetadataReader.readMetadata(bufferedInputStream, filter);

if (fileType == FileType.Tiff ||
fileType == FileType.Arw ||
fileType == FileType.Cr2 ||
fileType == FileType.Nef ||
fileType == FileType.Orf ||
fileType == FileType.Rw2)
return TiffMetadataReader.readMetadata(new RandomAccessStreamReader(bufferedInputStream, RandomAccessStreamReader.DEFAULT_CHUNK_LENGTH, streamLength));
return TiffMetadataReader.readMetadata(new RandomAccessStreamReader(bufferedInputStream, RandomAccessStreamReader.DEFAULT_CHUNK_LENGTH, streamLength), filter);

if (fileType == FileType.Psd)
return PsdMetadataReader.readMetadata(bufferedInputStream);
return PsdMetadataReader.readMetadata(bufferedInputStream, filter);

if (fileType == FileType.Png)
return PngMetadataReader.readMetadata(bufferedInputStream);
return PngMetadataReader.readMetadata(bufferedInputStream, filter);

if (fileType == FileType.Bmp)
return BmpMetadataReader.readMetadata(bufferedInputStream);
return BmpMetadataReader.readMetadata(bufferedInputStream, filter);

if (fileType == FileType.Gif)
return GifMetadataReader.readMetadata(bufferedInputStream);
return GifMetadataReader.readMetadata(bufferedInputStream, filter);

if (fileType == FileType.Ico)
return IcoMetadataReader.readMetadata(bufferedInputStream);
return IcoMetadataReader.readMetadata(bufferedInputStream, filter);

if (fileType == FileType.Pcx)
return PcxMetadataReader.readMetadata(bufferedInputStream);
return PcxMetadataReader.readMetadata(bufferedInputStream, filter);

if (fileType == FileType.Riff)
return WebpMetadataReader.readMetadata(bufferedInputStream);
return WebpMetadataReader.readMetadata(bufferedInputStream, filter);

if (fileType == FileType.Raf)
return RafMetadataReader.readMetadata(bufferedInputStream);
return RafMetadataReader.readMetadata(bufferedInputStream, filter);

throw new ImageProcessingException("File format is not supported");
}
Expand All @@ -155,15 +188,29 @@ public static Metadata readMetadata(@NotNull final InputStream inputStream, fina
*/
@NotNull
public static Metadata readMetadata(@NotNull final File file) throws ImageProcessingException, IOException
{
return readMetadata(file, null);
}

/**
* Reads {@link Metadata} from a {@link File} object.
*
* @param file a file from which the image data may be read.
* @param filter a {@link MetadataFilter} or <code>null</code>.
* @return a populated {@link Metadata} object containing directories of tags with values and any processing errors.
* @throws ImageProcessingException for general processing errors.
*/
@NotNull
public static Metadata readMetadata(@NotNull final File file, @Nullable final MetadataFilter filter) throws ImageProcessingException, IOException
{
InputStream inputStream = new FileInputStream(file);
Metadata metadata;
try {
metadata = readMetadata(inputStream, file.length());
metadata = readMetadata(inputStream, file.length(), filter);
} finally {
inputStream.close();
}
new FileMetadataReader().read(file, metadata);
new FileMetadataReader().read(file, metadata, filter);
return metadata;
}

Expand Down
18 changes: 16 additions & 2 deletions Source/com/drew/imaging/bmp/BmpMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

import com.drew.lang.StreamReader;
import com.drew.lang.annotations.NotNull;
import com.drew.lang.annotations.Nullable;
import com.drew.metadata.Metadata;
import com.drew.metadata.bmp.BmpReader;
import com.drew.metadata.filter.MetadataFilter;

import java.io.*;

Expand All @@ -37,11 +39,17 @@ public class BmpMetadataReader
{
@NotNull
public static Metadata readMetadata(@NotNull File file) throws IOException
{
return readMetadata(file, null);
}

@NotNull
public static Metadata readMetadata(@NotNull File file, @Nullable final MetadataFilter filter) throws IOException
{
FileInputStream stream = null;
try {
stream = new FileInputStream(file);
return readMetadata(stream);
return readMetadata(stream, filter);
} finally {
if (stream != null) {
stream.close();
Expand All @@ -51,9 +59,15 @@ public static Metadata readMetadata(@NotNull File file) throws IOException

@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream)
{
return readMetadata(inputStream, null);
}

@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream, @Nullable final MetadataFilter filter)
{
Metadata metadata = new Metadata();
new BmpReader().extract(new StreamReader(inputStream), metadata);
new BmpReader().extract(new StreamReader(inputStream), metadata, filter);
return metadata;
}
}
20 changes: 17 additions & 3 deletions Source/com/drew/imaging/gif/GifMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

import com.drew.lang.StreamReader;
import com.drew.lang.annotations.NotNull;
import com.drew.lang.annotations.Nullable;
import com.drew.metadata.Metadata;
import com.drew.metadata.file.FileMetadataReader;
import com.drew.metadata.filter.MetadataFilter;
import com.drew.metadata.gif.GifReader;

import java.io.File;
Expand All @@ -41,23 +43,35 @@ public class GifMetadataReader
{
@NotNull
public static Metadata readMetadata(@NotNull File file) throws IOException
{
return readMetadata(file, null);
}

@NotNull
public static Metadata readMetadata(@NotNull File file, @Nullable final MetadataFilter filter) throws IOException
{
InputStream inputStream = new FileInputStream(file);
Metadata metadata;
try {
metadata = readMetadata(inputStream);
metadata = readMetadata(inputStream, filter);
} finally {
inputStream.close();
}
new FileMetadataReader().read(file, metadata);
new FileMetadataReader().read(file, metadata, filter);
return metadata;
}

@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream)
{
return readMetadata(inputStream, null);
}

@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream, @Nullable final MetadataFilter filter)
{
Metadata metadata = new Metadata();
new GifReader().extract(new StreamReader(inputStream), metadata);
new GifReader().extract(new StreamReader(inputStream), metadata, filter);
return metadata;
}
}
20 changes: 17 additions & 3 deletions Source/com/drew/imaging/ico/IcoMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

import com.drew.lang.StreamReader;
import com.drew.lang.annotations.NotNull;
import com.drew.lang.annotations.Nullable;
import com.drew.metadata.Metadata;
import com.drew.metadata.file.FileMetadataReader;
import com.drew.metadata.filter.MetadataFilter;
import com.drew.metadata.ico.IcoReader;

import java.io.*;
Expand All @@ -37,23 +39,35 @@ public class IcoMetadataReader
{
@NotNull
public static Metadata readMetadata(@NotNull File file) throws IOException
{
return readMetadata(file, null);
}

@NotNull
public static Metadata readMetadata(@NotNull File file, @Nullable final MetadataFilter filter) throws IOException
{
InputStream inputStream = new FileInputStream(file);
Metadata metadata;
try {
metadata = readMetadata(inputStream);
metadata = readMetadata(inputStream, filter);
} finally {
inputStream.close();
}
new FileMetadataReader().read(file, metadata);
new FileMetadataReader().read(file, metadata, filter);
return metadata;
}

@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream)
{
return readMetadata(inputStream, null);
}

@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream, @Nullable final MetadataFilter filter)
{
Metadata metadata = new Metadata();
new IcoReader().extract(new StreamReader(inputStream), metadata);
new IcoReader().extract(new StreamReader(inputStream), metadata, filter);
return metadata;
}
}
Loading