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

Basic HEIF Implementation #314

Merged
merged 4 commits into from
Dec 29, 2017
Merged
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
3 changes: 3 additions & 0 deletions Source/com/drew/imaging/ImageMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.drew.imaging.bmp.BmpMetadataReader;
import com.drew.imaging.eps.EpsMetadataReader;
import com.drew.imaging.gif.GifMetadataReader;
import com.drew.imaging.heif.HeifMetadataReader;
import com.drew.imaging.ico.IcoMetadataReader;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.mp4.Mp4MetadataReader;
Expand Down Expand Up @@ -174,6 +175,8 @@ public static Metadata readMetadata(@NotNull final InputStream inputStream, fina
return Mp4MetadataReader.readMetadata(inputStream);
case Eps:
return EpsMetadataReader.readMetadata(inputStream);
case Heif:
return HeifMetadataReader.readMetadata(inputStream);
case Unknown:
throw new ImageProcessingException("File format could not be determined");
default:
Expand Down
56 changes: 56 additions & 0 deletions Source/com/drew/imaging/heif/HeifHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2002-2017 Drew Noakes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.imaging.heif;

import com.drew.lang.SequentialReader;
import com.drew.lang.annotations.NotNull;
import com.drew.metadata.Metadata;
import com.drew.metadata.heif.HeifDirectory;
import com.drew.metadata.heif.boxes.Box;

import java.io.IOException;

public abstract class HeifHandler<T extends HeifDirectory>
{
protected Metadata metadata;
protected T directory;

public HeifHandler(Metadata metadata)
{
this.metadata = metadata;
this.directory = getDirectory();
metadata.addDirectory(directory);
}

protected abstract T getDirectory();

protected abstract boolean shouldAcceptBox(@NotNull Box box);

protected abstract boolean shouldAcceptContainer(@NotNull Box box);

protected abstract HeifHandler processBox(@NotNull Box box, @NotNull byte[] payload) throws IOException;

/**
* There is potential for a box to both contain other boxes and contain information, so this method will
* handle those occurences.
*/
protected abstract void processContainer(@NotNull Box box, @NotNull SequentialReader reader) throws IOException;
}
47 changes: 47 additions & 0 deletions Source/com/drew/imaging/heif/HeifMetadataReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2002-2017 Drew Noakes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.imaging.heif;

import com.drew.imaging.mp4.Mp4Reader;
import com.drew.lang.annotations.NotNull;
import com.drew.metadata.Metadata;
import com.drew.metadata.heif.HeifBoxHandler;
import com.drew.metadata.mp4.Mp4BoxHandler;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.DataFormatException;

public class HeifMetadataReader
{
@NotNull
public static Metadata readMetadata(@NotNull InputStream inputStream) throws IOException
{
try {
Metadata metadata = new Metadata();
new HeifReader().extract(metadata, inputStream, new HeifBoxHandler(metadata));
return metadata;
} catch (DataFormatException e) {
e.printStackTrace();
}
return null;
}
}
66 changes: 66 additions & 0 deletions Source/com/drew/imaging/heif/HeifReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2002-2017 Drew Noakes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.imaging.heif;

import com.drew.lang.StreamReader;
import com.drew.metadata.Metadata;
import com.drew.metadata.heif.boxes.Box;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.DataFormatException;

public class HeifReader
{
public void extract(Metadata metadata, InputStream inputStream, HeifHandler handler) throws IOException, DataFormatException
{
StreamReader reader = new StreamReader(inputStream);
reader.setMotorolaByteOrder(true);

processBoxes(reader, -1, handler);
}

private void processBoxes(StreamReader reader, long atomEnd, HeifHandler handler)
{
try {
while ((atomEnd == -1) ? true : reader.getPosition() < atomEnd) {

Box box = new Box(reader);

// Determine if fourCC is container/atom and process accordingly
// Unknown atoms will be skipped

if (handler.shouldAcceptContainer(box)) {
handler.processContainer(box, reader);
processBoxes(reader, box.size + reader.getPosition() - 8, handler);
} else if (handler.shouldAcceptBox(box)) {
handler = handler.processBox(box, reader.getBytes((int)box.size - 8));
} else if (box.size > 1) {
reader.skip(box.size - 8);
} else if (box.size == -1) {
break;
}
}
} catch (IOException e) {
// Currently, reader relies on IOException to end
}
}
}
103 changes: 103 additions & 0 deletions Source/com/drew/metadata/heif/HeifBoxHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2002-2017 Drew Noakes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.metadata.heif;

import com.drew.imaging.heif.HeifHandler;
import com.drew.lang.SequentialByteArrayReader;
import com.drew.lang.SequentialReader;
import com.drew.lang.annotations.NotNull;
import com.drew.metadata.Metadata;
import com.drew.metadata.heif.boxes.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
* @author Payton Garland
*/
public class HeifBoxHandler extends HeifHandler<HeifDirectory>
{
HandlerBox handlerBox;

private HeifHandlerFactory handlerFactory = new HeifHandlerFactory(this);

public HeifBoxHandler(Metadata metadata)
{
super(metadata);
}

@Override
protected HeifDirectory getDirectory()
{
return new HeifDirectory();
}

@Override
public boolean shouldAcceptBox(@NotNull Box box)
{
List<String> boxes = Arrays.asList(HeifBoxTypes.BOX_FILE_TYPE,
HeifBoxTypes.BOX_HANDLER,
HeifBoxTypes.BOX_HVC1);

return boxes.contains(box.type);
}

@Override
public boolean shouldAcceptContainer(Box box)
{
return box.type.equals(HeifContainerTypes.BOX_METADATA)
|| box.type.equals(HeifContainerTypes.BOX_IMAGE_PROPERTY)
|| box.type.equals(HeifContainerTypes.BOX_ITEM_PROPERTY);
}

@Override
public HeifHandler processBox(@NotNull Box box, @NotNull byte[] payload) throws IOException
{
if (payload != null) {
SequentialReader reader = new SequentialByteArrayReader(payload);
if (box.type.equals(HeifBoxTypes.BOX_FILE_TYPE)) {
processFileType(reader, box);
}else if (box.type.equals(HeifBoxTypes.BOX_HANDLER)) {
handlerBox = new HandlerBox(reader, box);
return handlerFactory.getHandler(handlerBox, metadata);
}
}
return this;
}

@Override
public void processContainer(@NotNull Box box, @NotNull SequentialReader reader) throws IOException
{
if (box.type.equals(HeifContainerTypes.BOX_METADATA)) {
new FullBox(reader, box);
}
}

private void processFileType(@NotNull SequentialReader reader, @NotNull Box box) throws IOException
{
FileTypeBox fileTypeBox = new FileTypeBox(reader, box);
fileTypeBox.addMetadata(directory);
if (!fileTypeBox.getCompatibleBrands().contains("mif1")) {
directory.addError("File Type Box does not contain required brand, mif1");
}
}
}
55 changes: 55 additions & 0 deletions Source/com/drew/metadata/heif/HeifBoxTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2002-2017 Drew Noakes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.metadata.heif;

import java.util.ArrayList;

/**
* @author Payton Garland
*/
public class HeifBoxTypes
{
public static final String BOX_FILE_TYPE = "ftyp";
public static final String BOX_PRIMARY_ITEM = "pitm";
public static final String BOX_ITEM_PROTECTION = "ipro";
public static final String BOX_ITEM_INFO = "iinf";
public static final String BOX_ITEM_LOCATION = "iloc";
public static final String BOX_HANDLER = "hdlr";
public static final String BOX_HVC1 = "hvc1";
public static final String BOX_IMAGE_SPATIAL_EXTENTS = "ispe";
public static final String BOX_AUXILIARY_TYPE_PROPERTY = "auxC";
public static final String BOX_HEVC_CONFIGURATION = "hvcC";

public static ArrayList<String> _boxList = new ArrayList<String>();

static {
_boxList.add(BOX_FILE_TYPE);
_boxList.add(BOX_ITEM_PROTECTION);
_boxList.add(BOX_PRIMARY_ITEM);
_boxList.add(BOX_ITEM_INFO);
_boxList.add(BOX_ITEM_LOCATION);
_boxList.add(BOX_HANDLER);
_boxList.add(BOX_HVC1);
_boxList.add(BOX_IMAGE_SPATIAL_EXTENTS);
_boxList.add(BOX_AUXILIARY_TYPE_PROPERTY);
_boxList.add(BOX_HEVC_CONFIGURATION);
}
}
41 changes: 41 additions & 0 deletions Source/com/drew/metadata/heif/HeifContainerTypes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2002-2017 Drew Noakes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* More information about this project is available at:
*
* https://drewnoakes.com/code/exif/
* https://github.com/drewnoakes/metadata-extractor
*/
package com.drew.metadata.heif;

import java.util.ArrayList;

/**
* @author Payton Garland
*/
public class HeifContainerTypes
{
public static final String BOX_METADATA = "meta";
public static final String BOX_IMAGE_PROPERTY = "iprp";
public static final String BOX_ITEM_PROPERTY = "ipco";

public static ArrayList<String> _containerList = new ArrayList<String>();

static {
_containerList.add(BOX_METADATA);
_containerList.add(BOX_IMAGE_PROPERTY);
_containerList.add(BOX_ITEM_PROPERTY);
}
}
Loading