mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-03 11:35:29 -04:00
TMI-META: Minor improvements in XMP parsing, PSD made public and faster dumping from JPEGSegmentUtil.
This commit is contained in:
parent
086357694a
commit
cd197afc04
@ -87,7 +87,7 @@ public final class JPEGSegment implements Serializable {
|
|||||||
return data != null ? data.length - offset() : 0;
|
return data != null ? data.length - offset() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int offset() {
|
int offset() {
|
||||||
String identifier = identifier();
|
String identifier = identifier();
|
||||||
|
|
||||||
return identifier == null ? 0 : identifier.length() + 1;
|
return identifier == null ? 0 : identifier.length() + 1;
|
||||||
|
@ -33,6 +33,7 @@ import com.twelvemonkeys.imageio.metadata.exif.EXIFReader;
|
|||||||
import com.twelvemonkeys.imageio.metadata.psd.PSDReader;
|
import com.twelvemonkeys.imageio.metadata.psd.PSDReader;
|
||||||
import com.twelvemonkeys.imageio.metadata.xmp.XMP;
|
import com.twelvemonkeys.imageio.metadata.xmp.XMP;
|
||||||
import com.twelvemonkeys.imageio.metadata.xmp.XMPReader;
|
import com.twelvemonkeys.imageio.metadata.xmp.XMPReader;
|
||||||
|
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
|
||||||
|
|
||||||
import javax.imageio.IIOException;
|
import javax.imageio.IIOException;
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
@ -245,32 +246,36 @@ public final class JPEGSegmentUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
List<JPEGSegment> segments = readSegments(ImageIO.createImageInputStream(new File(args[0])), ALL_SEGMENTS);
|
for (String arg : args) {
|
||||||
|
if (args.length > 1) {
|
||||||
|
System.out.println("File: " + arg);
|
||||||
|
System.out.println("------");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<JPEGSegment> segments = readSegments(ImageIO.createImageInputStream(new File(arg)), ALL_SEGMENTS);
|
||||||
|
|
||||||
for (JPEGSegment segment : segments) {
|
for (JPEGSegment segment : segments) {
|
||||||
System.err.println("segment: " + segment);
|
System.err.println("segment: " + segment);
|
||||||
|
|
||||||
if ("Exif".equals(segment.identifier())) {
|
if ("Exif".equals(segment.identifier())) {
|
||||||
InputStream data = segment.data();
|
ImageInputStream stream = new ByteArrayImageInputStream(segment.data, segment.offset() + 1, segment.length() - 1);
|
||||||
//noinspection ResultOfMethodCallIgnored
|
|
||||||
data.read(); // Pad
|
|
||||||
|
|
||||||
ImageInputStream stream = ImageIO.createImageInputStream(data);
|
|
||||||
|
|
||||||
// Root entry is TIFF, that contains the EXIF sub-IFD
|
// Root entry is TIFF, that contains the EXIF sub-IFD
|
||||||
Directory tiff = new EXIFReader().read(stream);
|
Directory tiff = new EXIFReader().read(stream);
|
||||||
System.err.println("EXIF: " + tiff);
|
System.err.println("EXIF: " + tiff);
|
||||||
}
|
}
|
||||||
else if (XMP.NS_XAP.equals(segment.identifier())) {
|
else if (XMP.NS_XAP.equals(segment.identifier())) {
|
||||||
Directory xmp = new XMPReader().read(ImageIO.createImageInputStream(segment.data()));
|
Directory xmp = new XMPReader().read(new ByteArrayImageInputStream(segment.data, segment.offset(), segment.length()));
|
||||||
System.err.println("XMP: " + xmp);
|
System.err.println("XMP: " + xmp);
|
||||||
|
System.err.println(EXIFReader.HexDump.dump(segment.data));
|
||||||
}
|
}
|
||||||
else if ("Photoshop 3.0".equals(segment.identifier())) {
|
else if ("Photoshop 3.0".equals(segment.identifier())) {
|
||||||
// TODO: The "Photoshop 3.0" segment contains several image resources, of which one might contain
|
// TODO: The "Photoshop 3.0" segment contains several image resources, of which one might contain
|
||||||
// IPTC metadata. Probably duplicated in the XMP though...
|
// IPTC metadata. Probably duplicated in the XMP though...
|
||||||
ImageInputStream stream = ImageIO.createImageInputStream(segment.data());
|
ImageInputStream stream = new ByteArrayImageInputStream(segment.data, segment.offset(), segment.length());
|
||||||
Directory psd = new PSDReader().read(stream);
|
Directory psd = new PSDReader().read(stream);
|
||||||
System.err.println("PSD: " + psd);
|
System.err.println("PSD: " + psd);
|
||||||
|
System.err.println(EXIFReader.HexDump.dump(segment.data));
|
||||||
}
|
}
|
||||||
else if ("ICC_PROFILE".equals(segment.identifier())) {
|
else if ("ICC_PROFILE".equals(segment.identifier())) {
|
||||||
// Skip
|
// Skip
|
||||||
@ -279,5 +284,11 @@ public final class JPEGSegmentUtil {
|
|||||||
System.err.println(EXIFReader.HexDump.dump(segment.data));
|
System.err.println(EXIFReader.HexDump.dump(segment.data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (args.length > 1) {
|
||||||
|
System.out.println("------");
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ package com.twelvemonkeys.imageio.metadata.psd;
|
|||||||
* @author last modified by $Author: haraldk$
|
* @author last modified by $Author: haraldk$
|
||||||
* @version $Id: PSD.java,v 1.0 24.01.12 16:51 haraldk Exp$
|
* @version $Id: PSD.java,v 1.0 24.01.12 16:51 haraldk Exp$
|
||||||
*/
|
*/
|
||||||
interface PSD {
|
public interface PSD {
|
||||||
static final int RESOURCE_TYPE = ('8' << 24) + ('B' << 16) + ('I' << 8) + 'M';
|
static final int RESOURCE_TYPE = ('8' << 24) + ('B' << 16) + ('I' << 8) + 'M';
|
||||||
|
|
||||||
static final int RES_IPTC_NAA = 0x0404;
|
static final int RES_IPTC_NAA = 0x0404;
|
||||||
|
@ -128,20 +128,8 @@ public final class XMPReader extends MetadataReader {
|
|||||||
|
|
||||||
Object value;
|
Object value;
|
||||||
|
|
||||||
Node parseType = node.getAttributes().getNamedItemNS(XMP.NS_RDF, "parseType");
|
if (isResourceType(node)) {
|
||||||
if (parseType != null && "Resource".equals(parseType.getNodeValue())) {
|
value = parseAsResource(node);
|
||||||
// See: http://www.w3.org/TR/REC-rdf-syntax/#section-Syntax-parsetype-resource
|
|
||||||
List<Entry> entries = new ArrayList<Entry>();
|
|
||||||
|
|
||||||
for (Node child : asIterable(node.getChildNodes())) {
|
|
||||||
if (child.getNodeType() != Node.ELEMENT_NODE) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
entries.add(new XMPEntry(child.getNamespaceURI() + child.getLocalName(), child.getLocalName(), getChildTextValue(child)));
|
|
||||||
}
|
|
||||||
|
|
||||||
value = new RDFDescription(entries);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// TODO: This method contains loads of duplication an should be cleaned up...
|
// TODO: This method contains loads of duplication an should be cleaned up...
|
||||||
@ -178,6 +166,27 @@ public final class XMPReader extends MetadataReader {
|
|||||||
return new XMPDirectory(entries, toolkit);
|
return new XMPDirectory(entries, toolkit);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isResourceType(Node node) {
|
||||||
|
Node parseType = node.getAttributes().getNamedItemNS(XMP.NS_RDF, "parseType");
|
||||||
|
|
||||||
|
return parseType != null && "Resource".equals(parseType.getNodeValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
private RDFDescription parseAsResource(Node node) {
|
||||||
|
// See: http://www.w3.org/TR/REC-rdf-syntax/#section-Syntax-parsetype-resource
|
||||||
|
List<Entry> entries = new ArrayList<Entry>();
|
||||||
|
|
||||||
|
for (Node child : asIterable(node.getChildNodes())) {
|
||||||
|
if (child.getNodeType() != Node.ELEMENT_NODE) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
entries.add(new XMPEntry(child.getNamespaceURI() + child.getLocalName(), child.getLocalName(), getChildTextValue(child)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new RDFDescription(entries);
|
||||||
|
}
|
||||||
|
|
||||||
private void parseAttributesForKnownElements(Map<String, List<Entry>> subdirs, Node desc) {
|
private void parseAttributesForKnownElements(Map<String, List<Entry>> subdirs, Node desc) {
|
||||||
// NOTE: NamedNodeMap does not have any particular order...
|
// NOTE: NamedNodeMap does not have any particular order...
|
||||||
NamedNodeMap attributes = desc.getAttributes();
|
NamedNodeMap attributes = desc.getAttributes();
|
||||||
@ -201,15 +210,13 @@ public final class XMPReader extends MetadataReader {
|
|||||||
private Object getChildTextValue(final Node node) {
|
private Object getChildTextValue(final Node node) {
|
||||||
for (Node child : asIterable(node.getChildNodes())) {
|
for (Node child : asIterable(node.getChildNodes())) {
|
||||||
if (XMP.NS_RDF.equals(child.getNamespaceURI()) && "Alt".equals(child.getLocalName())) {
|
if (XMP.NS_RDF.equals(child.getNamespaceURI()) && "Alt".equals(child.getLocalName())) {
|
||||||
// Support for <rdf:Alt><rdf:li> -> return a Map<String, Object> (keyed on xml:lang?)
|
// Support for <rdf:Alt><rdf:li> -> return a Map<String, Object> keyed on xml:lang
|
||||||
Map<String, Object> alternatives = new LinkedHashMap<String, Object>();
|
Map<String, Object> alternatives = new LinkedHashMap<String, Object>();
|
||||||
for (Node alternative : asIterable(child.getChildNodes())) {
|
for (Node alternative : asIterable(child.getChildNodes())) {
|
||||||
if (XMP.NS_RDF.equals(alternative.getNamespaceURI()) && "li".equals(alternative.getLocalName())) {
|
if (XMP.NS_RDF.equals(alternative.getNamespaceURI()) && "li".equals(alternative.getLocalName())) {
|
||||||
//return getChildTextValue(alternative);
|
|
||||||
NamedNodeMap attributes = alternative.getAttributes();
|
NamedNodeMap attributes = alternative.getAttributes();
|
||||||
Node key = attributes.getNamedItem("xml:lang");
|
Node key = attributes.getNamedItem("xml:lang");
|
||||||
|
alternatives.put(key == null ? null : key.getTextContent(), getChildTextValue(alternative));
|
||||||
alternatives.put(key.getTextContent(), getChildTextValue(alternative));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,9 +242,13 @@ public final class XMPReader extends MetadataReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Need to support rdf:parseType="Resource" here as well...
|
||||||
|
if (isResourceType(node)) {
|
||||||
|
return parseAsResource(node);
|
||||||
|
}
|
||||||
|
|
||||||
Node child = node.getFirstChild();
|
Node child = node.getFirstChild();
|
||||||
String strVal = child != null ? child.getNodeValue() : null;
|
String strVal = child != null ? child.getNodeValue() : null;
|
||||||
|
|
||||||
return strVal != null ? strVal.trim() : "";
|
return strVal != null ? strVal.trim() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user