forked from dlemstra/Magick.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmpProfile.cs
More file actions
173 lines (148 loc) · 4.96 KB
/
Copy pathXmpProfile.cs
File metadata and controls
173 lines (148 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace ImageMagick;
/// <summary>
/// Class that contains an XMP profile.
/// </summary>
public sealed class XmpProfile : ImageProfile, IXmpProfile
{
/// <summary>
/// Initializes a new instance of the <see cref="XmpProfile"/> class.
/// </summary>
/// <param name="data">A byte array containing the profile.</param>
public XmpProfile(byte[] data)
: base("xmp", CheckTrailingNULL(data))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="XmpProfile"/> class.
/// </summary>
/// <param name="document">A document containing the profile.</param>
public XmpProfile(IXPathNavigable document)
: base("xmp")
{
Throw.IfNull(document);
var navigator = document.CreateNavigator();
if (navigator is not null)
{
using var memStream = new MemoryStream();
using var writer = CreateXmlWriter(memStream);
navigator.WriteSubtree(writer);
writer.Flush();
SetData(memStream.ToArray());
}
}
/// <summary>
/// Initializes a new instance of the <see cref="XmpProfile"/> class.
/// </summary>
/// <param name="document">A document containing the profile.</param>
public XmpProfile(XDocument document)
: base("xmp")
{
Throw.IfNull(document);
using var memStream = new MemoryStream();
using var writer = CreateXmlWriter(memStream);
document.WriteTo(writer);
writer.Flush();
SetData(memStream.ToArray());
}
/// <summary>
/// Initializes a new instance of the <see cref="XmpProfile"/> class.
/// </summary>
/// <param name="stream">A stream containing the profile.</param>
public XmpProfile(Stream stream)
: base("xmp", stream)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="XmpProfile"/> class.
/// </summary>
/// <param name="fileName">The fully qualified name of the profile file, or the relative profile file name.</param>
public XmpProfile(string fileName)
: base("xmp", fileName)
{
}
/// <summary>
/// Creates an instance from the specified IXPathNavigable.
/// </summary>
/// <param name="document">A document containing the profile.</param>
/// <returns>A <see cref="XmpProfile"/>.</returns>
public static XmpProfile FromIXPathNavigable(IXPathNavigable document)
=> new XmpProfile(document);
/// <summary>
/// Creates an instance from the specified IXPathNavigable.
/// </summary>
/// <param name="document">A document containing the profile.</param>
/// <returns>A <see cref="XmpProfile"/>.</returns>
public static XmpProfile FromXDocument(XDocument document)
=> new XmpProfile(document);
/// <summary>
/// Creates a XmlReader that can be used to read the data of the profile.
/// </summary>
/// <returns>A <see cref="XmlReader"/>.</returns>
public XmlReader? CreateReader()
{
var data = GetData();
if (data is null)
return null;
var memStream = new MemoryStream(data, 0, data.Length);
var settings = XmlHelper.CreateReaderSettings();
settings.CloseInput = true;
return XmlReader.Create(memStream, settings);
}
/// <summary>
/// Converts this instance to an IXPathNavigable.
/// </summary>
/// <returns>A <see cref="IXPathNavigable"/>.</returns>
public IXPathNavigable? ToIXPathNavigable()
{
using var reader = CreateReader();
if (reader is null)
return null;
var result = XmlHelper.CreateDocument();
result.Load(reader);
return result.CreateNavigator();
}
/// <summary>
/// Converts this instance to a XDocument.
/// </summary>
/// <returns>A <see cref="XDocument"/>.</returns>
public XDocument? ToXDocument()
{
using var reader = CreateReader();
if (reader is null)
return null;
return XDocument.Load(reader);
}
private static XmlWriter CreateXmlWriter(MemoryStream memStream)
{
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false),
};
return XmlWriter.Create(memStream, settings);
}
private static byte[] CheckTrailingNULL(byte[] data)
{
Throw.IfNull(data);
var length = data.Length;
while (length > 2)
{
if (data[length - 1] != '\0')
break;
length--;
}
if (length == data.Length)
return data;
var result = new byte[length];
Buffer.BlockCopy(data, 0, result, 0, length);
return result;
}
}