public static IptcDirectory ProcessBytes(string filePath) { Com.Drew.Metadata.Metadata metadata = new Com.Drew.Metadata.Metadata(); sbyte[] bytes = FileUtil.ReadBytes(filePath); new IptcReader().Extract(new SequentialByteArrayReader(bytes), metadata, bytes.Length); IptcDirectory directory = metadata.GetDirectory <IptcDirectory>(); NUnit.Framework.Assert.IsNotNull(directory); return(directory); }
public virtual void TestIptcEncodingUnknown2() { // This metadata has an encoding of three characters [ \ESC '%' '5' ] // It's not clear what to do with this, so it should be ignored. // Version 2.7.0 tripped up on this and threw an exception. IptcDirectory directory = ProcessBytes("Tests/Data/iptc-encoding-unknown-2.bytes"); Sharpen.Tests.IsFalse(Sharpen.Extensions.ConvertToString(directory.GetErrors()), directory.HasErrors()); Tag[] tags = Sharpen.Collections.ToArray(directory.GetTags(), new Tag[directory.GetTagCount()]); Sharpen.Tests.AreEqual(37, tags.Length); Sharpen.Tests.AreEqual("MEDWAS,MEDLON,MEDTOR,RONL,ASIA,AONL,APC,USA,CAN,SAM,BIZ", directory.GetString(IptcDirectory.TagDestination)); }
public virtual void TestIptcEncodingUnknown() { IptcDirectory directory = ProcessBytes("Tests/Data/iptc-encoding-unknown.bytes"); Sharpen.Tests.IsFalse(Sharpen.Extensions.ConvertToString(directory.GetErrors()), directory.HasErrors()); Tag[] tags = Sharpen.Collections.ToArray(directory.GetTags(), new Tag[directory.GetTagCount()]); Sharpen.Tests.AreEqual(3, tags.Length); Sharpen.Tests.AreEqual(IptcDirectory.TagApplicationRecordVersion, tags[0].GetTagType()); Sharpen.Tests.AreEqual(2, directory.GetObject(tags[0].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCaption, tags[1].GetTagType()); Sharpen.Tests.AreEqual("Das Encoding dieser Metadaten ist nicht deklariert und lässt sich nur schwer erkennen.", directory.GetObject(tags[1].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagKeywords, tags[2].GetTagType()); NUnit.Framework.CollectionAssert.AreEqual(new string[] { "häufig", "üblich", "Lösung", "Spaß" }, directory.GetStringArray(tags[2].GetTagType())); }
public virtual void TestIptcEncodingUndefinedIso() { IptcDirectory directory = ProcessBytes("Tests/Data/iptc-encoding-undefined-iso.bytes"); Sharpen.Tests.IsFalse(Sharpen.Extensions.ConvertToString(directory.GetErrors()), directory.HasErrors()); Tag[] tags = Sharpen.Collections.ToArray(directory.GetTags(), new Tag[directory.GetTagCount()]); Sharpen.Tests.AreEqual(3, tags.Length); Sharpen.Tests.AreEqual(IptcDirectory.TagEnvelopeRecordVersion, tags[0].GetTagType()); Sharpen.Tests.AreEqual(2, directory.GetObject(tags[0].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagApplicationRecordVersion, tags[1].GetTagType()); Sharpen.Tests.AreEqual(2, directory.GetObject(tags[1].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCaption, tags[2].GetTagType()); Sharpen.Tests.AreEqual("In diesem Text sind Umlaute enthalten, nämlich öfter als üblich: ÄÖÜäöüß\r", directory.GetObject(tags[2].GetTagType())); }
public virtual void TestIptc2Photoshop6BytesFromFile() { IptcDirectory directory = ProcessBytes("Tests/Data/iptc2-photoshop6.jpg.appd"); Sharpen.Tests.IsFalse(directory.GetErrors().ToString(), directory.HasErrors()); Tag[] tags = Sharpen.Collections.ToArray(directory.GetTags(), new Tag[directory.GetTagCount()]); Sharpen.Tests.AreEqual(17, tags.Length); Sharpen.Tests.AreEqual(IptcDirectory.TagApplicationRecordVersion, tags[0].GetTagType()); Sharpen.Tests.AreEqual(2, directory.GetObject(tags[0].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCaption, tags[1].GetTagType()); Sharpen.Tests.AreEqual("Caption PS6", directory.GetObject(tags[1].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCaptionWriter, tags[2].GetTagType()); Sharpen.Tests.AreEqual("CaptionWriter", directory.GetObject(tags[2].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagHeadline, tags[3].GetTagType()); Sharpen.Tests.AreEqual("Headline", directory.GetObject(tags[3].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagSpecialInstructions, tags[4].GetTagType()); Sharpen.Tests.AreEqual("Special Instr.", directory.GetObject(tags[4].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagByLine, tags[5].GetTagType()); Sharpen.Tests.AreEqual("Byline", directory.GetObject(tags[5].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagByLineTitle, tags[6].GetTagType()); Sharpen.Tests.AreEqual("BylineTitle", directory.GetObject(tags[6].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCredit, tags[7].GetTagType()); Sharpen.Tests.AreEqual("Credits", directory.GetObject(tags[7].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagSource, tags[8].GetTagType()); Sharpen.Tests.AreEqual("Source", directory.GetObject(tags[8].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagObjectName, tags[9].GetTagType()); Sharpen.Tests.AreEqual("ObjectName", directory.GetObject(tags[9].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCity, tags[10].GetTagType()); Sharpen.Tests.AreEqual("City", directory.GetObject(tags[10].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagProvinceOrState, tags[11].GetTagType()); Sharpen.Tests.AreEqual("State", directory.GetObject(tags[11].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCountryOrPrimaryLocationName, tags[12].GetTagType()); Sharpen.Tests.AreEqual("Country", directory.GetObject(tags[12].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagOriginalTransmissionReference, tags[13].GetTagType()); Sharpen.Tests.AreEqual("Transmission", directory.GetObject(tags[13].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCategory, tags[14].GetTagType()); Sharpen.Tests.AreEqual("Cat", directory.GetObject(tags[14].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagSupplementalCategories, tags[15].GetTagType()); NUnit.Framework.CollectionAssert.AreEqual(new string[] { "Supl. Category1", "Supl. Category2" }, directory.GetStringArray(tags[15].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCopyrightNotice, tags[16].GetTagType()); Sharpen.Tests.AreEqual("Copyright", directory.GetObject(tags[16].GetTagType())); }
public virtual void TestIptc1BytesFromFile() { IptcDirectory directory = ProcessBytes("Tests/Data/iptc1.jpg.appd"); Sharpen.Tests.IsFalse(Sharpen.Extensions.ConvertToString(directory.GetErrors()), directory.HasErrors()); Tag[] tags = Sharpen.Collections.ToArray(directory.GetTags(), new Tag[directory.GetTagCount()]); Sharpen.Tests.AreEqual(16, tags.Length); Sharpen.Tests.AreEqual(IptcDirectory.TagCategory, tags[0].GetTagType()); NUnit.Framework.CollectionAssert.AreEqual(new string[] { "Supl. Category2", "Supl. Category1", "Cat" }, directory.GetStringArray(tags[0].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCopyrightNotice, tags[1].GetTagType()); Sharpen.Tests.AreEqual("Copyright", directory.GetObject(tags[1].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagSpecialInstructions, tags[2].GetTagType()); Sharpen.Tests.AreEqual("Special Instr.", directory.GetObject(tags[2].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagHeadline, tags[3].GetTagType()); Sharpen.Tests.AreEqual("Headline", directory.GetObject(tags[3].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCaptionWriter, tags[4].GetTagType()); Sharpen.Tests.AreEqual("CaptionWriter", directory.GetObject(tags[4].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCaption, tags[5].GetTagType()); Sharpen.Tests.AreEqual("Caption", directory.GetObject(tags[5].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagOriginalTransmissionReference, tags[6].GetTagType()); Sharpen.Tests.AreEqual("Transmission", directory.GetObject(tags[6].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCountryOrPrimaryLocationName, tags[7].GetTagType()); Sharpen.Tests.AreEqual("Country", directory.GetObject(tags[7].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagProvinceOrState, tags[8].GetTagType()); Sharpen.Tests.AreEqual("State", directory.GetObject(tags[8].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCity, tags[9].GetTagType()); Sharpen.Tests.AreEqual("City", directory.GetObject(tags[9].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagDateCreated, tags[10].GetTagType()); Sharpen.Tests.AreEqual(new Sharpen.GregorianCalendar(2000, 0, 1).GetTime(), directory.GetObject(tags[10].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagObjectName, tags[11].GetTagType()); Sharpen.Tests.AreEqual("ObjectName", directory.GetObject(tags[11].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagSource, tags[12].GetTagType()); Sharpen.Tests.AreEqual("Source", directory.GetObject(tags[12].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagCredit, tags[13].GetTagType()); Sharpen.Tests.AreEqual("Credits", directory.GetObject(tags[13].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagByLineTitle, tags[14].GetTagType()); Sharpen.Tests.AreEqual("BylineTitle", directory.GetObject(tags[14].GetTagType())); Sharpen.Tests.AreEqual(IptcDirectory.TagByLine, tags[15].GetTagType()); Sharpen.Tests.AreEqual("Byline", directory.GetObject(tags[15].GetTagType())); }
/// <summary> /// Performs the IPTC data extraction, adding found values to the specified instance of /// <see cref="Com.Drew.Metadata.Metadata"/> /// . /// </summary> public virtual void Extract([NotNull] SequentialReader reader, [NotNull] Com.Drew.Metadata.Metadata metadata, long length) { IptcDirectory directory = new IptcDirectory(); metadata.AddDirectory(directory); int offset = 0; // for each tag while (offset < length) { // identifies start of a tag short startByte; try { startByte = reader.GetUInt8(); offset++; } catch (IOException) { directory.AddError("Unable to read starting byte of IPTC tag"); return; } if (startByte != unchecked ((int)(0x1c))) { // NOTE have seen images where there was one extra byte at the end, giving // offset==length at this point, which is not worth logging as an error. if (offset != length) { directory.AddError("Invalid IPTC tag marker at offset " + (offset - 1) + ". Expected '0x1c' but got '0x" + Sharpen.Extensions.ToHexString(startByte) + "'."); } return; } // we need at least five bytes left to read a tag if (offset + 5 >= length) { directory.AddError("Too few bytes remain for a valid IPTC tag"); return; } int directoryType; int tagType; int tagByteCount; try { directoryType = reader.GetUInt8(); tagType = reader.GetUInt8(); // TODO support Extended DataSet Tag (see 1.5(c), p14, IPTC-IIMV4.2.pdf) tagByteCount = reader.GetUInt16(); offset += 4; } catch (IOException) { directory.AddError("IPTC data segment ended mid-way through tag descriptor"); return; } if (offset + tagByteCount > length) { directory.AddError("Data for tag extends beyond end of IPTC segment"); return; } try { ProcessTag(reader, directory, directoryType, tagType, tagByteCount); } catch (IOException) { directory.AddError("Error processing IPTC tag"); return; } offset += tagByteCount; } }
/// <summary> /// Performs the IPTC data extraction, adding found values to the specified instance of /// <see cref="Com.Drew.Metadata.Metadata"/> /// . /// </summary> public virtual void Extract(SequentialReader reader, Com.Drew.Metadata.Metadata metadata, long length) { IptcDirectory directory = metadata.GetOrCreateDirectory <IptcDirectory>(); int offset = 0; // for each tag while (offset < length) { // identifies start of a tag short startByte; try { startByte = reader.GetUInt8(); offset++; } catch (IOException) { directory.AddError("Unable to read starting byte of IPTC tag"); return; } if (startByte != unchecked ((int)(0x1c))) { directory.AddError("Invalid start to IPTC tag"); return; } // we need at least five bytes left to read a tag if (offset + 5 >= length) { directory.AddError("Too few bytes remain for a valid IPTC tag"); return; } int directoryType; int tagType; int tagByteCount; try { directoryType = reader.GetUInt8(); tagType = reader.GetUInt8(); tagByteCount = reader.GetUInt16(); offset += 4; } catch (IOException) { directory.AddError("IPTC data segment ended mid-way through tag descriptor"); return; } if (offset + tagByteCount > length) { directory.AddError("Data for tag extends beyond end of IPTC segment"); return; } try { ProcessTag(reader, directory, directoryType, tagType, tagByteCount); } catch (IOException) { directory.AddError("Error processing IPTC tag"); return; } offset += tagByteCount; } }
/// <summary> /// Performs the IPTC data extraction, adding found values to the specified instance of /// <see cref="Com.Drew.Metadata.Metadata"/> /// . /// </summary> public virtual void Extract([NotNull] SequentialReader reader, [NotNull] Com.Drew.Metadata.Metadata metadata, long length) { IptcDirectory directory = new IptcDirectory(); metadata.AddDirectory(directory); int offset = 0; // for each tag while (offset < length) { // identifies start of a tag short startByte; try { startByte = reader.GetUInt8(); offset++; } catch (IOException) { directory.AddError("Unable to read starting byte of IPTC tag"); return; } if (startByte != unchecked((int)(0x1c))) { // NOTE have seen images where there was one extra byte at the end, giving // offset==length at this point, which is not worth logging as an error. if (offset != length) { directory.AddError("Invalid IPTC tag marker at offset " + (offset - 1) + ". Expected '0x1c' but got '0x" + Sharpen.Extensions.ToHexString(startByte) + "'."); } return; } // we need at least five bytes left to read a tag if (offset + 5 >= length) { directory.AddError("Too few bytes remain for a valid IPTC tag"); return; } int directoryType; int tagType; int tagByteCount; try { directoryType = reader.GetUInt8(); tagType = reader.GetUInt8(); // TODO support Extended DataSet Tag (see 1.5(c), p14, IPTC-IIMV4.2.pdf) tagByteCount = reader.GetUInt16(); offset += 4; } catch (IOException) { directory.AddError("IPTC data segment ended mid-way through tag descriptor"); return; } if (offset + tagByteCount > length) { directory.AddError("Data for tag extends beyond end of IPTC segment"); return; } try { ProcessTag(reader, directory, directoryType, tagType, tagByteCount); } catch (IOException) { directory.AddError("Error processing IPTC tag"); return; } offset += tagByteCount; } }