forked from 0xC0000054/pdn-webp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMetadataCustomMarshaler.cs
More file actions
144 lines (123 loc) · 4.91 KB
/
Copy pathMetadataCustomMarshaler.cs
File metadata and controls
144 lines (123 loc) · 4.91 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
////////////////////////////////////////////////////////////////////////
//
// This file is part of pdn-webp, a FileType plugin for Paint.NET
// that loads and saves WebP images.
//
// Copyright (c) 2011-2019 Nicholas Hayes
//
// This file is licensed under the MIT License.
// See LICENSE.txt for complete licensing and attribution information.
//
////////////////////////////////////////////////////////////////////////
using System;
using System.Runtime.InteropServices;
namespace WebPFileType
{
internal sealed class MetadataCustomMarshaler : ICustomMarshaler
{
// This must be kept in sync with the MetadataParams structure in WebP.h.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1049:TypesThatOwnNativeResourcesShouldBeDisposable")]
[StructLayout(LayoutKind.Sequential)]
private struct NativeMetadataParams
{
public IntPtr iccProfile;
public UIntPtr iccProfileSize;
public IntPtr exif;
public UIntPtr exifSize;
public IntPtr xmp;
public UIntPtr xmpSize;
}
private static readonly int NativeMetadataParamsSize = Marshal.SizeOf(typeof(NativeMetadataParams));
private static readonly MetadataCustomMarshaler instance = new MetadataCustomMarshaler();
#pragma warning disable IDE0060 // Remove unused parameter
public static ICustomMarshaler GetInstance(string cookie)
#pragma warning restore IDE0060 // Remove unused parameter
{
return instance;
}
private MetadataCustomMarshaler()
{
}
public void CleanUpManagedData(object ManagedObj)
{
}
public void CleanUpNativeData(IntPtr pNativeData)
{
unsafe
{
if (pNativeData != IntPtr.Zero)
{
NativeMetadataParams* metadata = (NativeMetadataParams*)pNativeData;
if (metadata->iccProfile != IntPtr.Zero)
{
Marshal.FreeHGlobal(metadata->iccProfile);
}
if (metadata->exif != IntPtr.Zero)
{
Marshal.FreeHGlobal(metadata->exif);
}
if (metadata->xmp != IntPtr.Zero)
{
Marshal.FreeHGlobal(metadata->xmp);
}
Marshal.FreeHGlobal(pNativeData);
}
}
}
public int GetNativeDataSize()
{
return NativeMetadataParamsSize;
}
public IntPtr MarshalManagedToNative(object ManagedObj)
{
if (ManagedObj == null)
{
return IntPtr.Zero;
}
WebPNative.MetadataParams metadata = (WebPNative.MetadataParams)ManagedObj;
IntPtr nativeStructure = Marshal.AllocHGlobal(NativeMetadataParamsSize);
unsafe
{
NativeMetadataParams* nativeMetadata = (NativeMetadataParams*)nativeStructure;
if (metadata.iccProfile != null && metadata.iccProfile.Length > 0)
{
nativeMetadata->iccProfile = Marshal.AllocHGlobal(metadata.iccProfile.Length);
Marshal.Copy(metadata.iccProfile, 0, nativeMetadata->iccProfile, metadata.iccProfile.Length);
nativeMetadata->iccProfileSize = new UIntPtr((uint)metadata.iccProfile.Length);
}
else
{
nativeMetadata->iccProfile = IntPtr.Zero;
nativeMetadata->iccProfileSize = UIntPtr.Zero;
}
if (metadata.exif != null && metadata.exif.Length > 0)
{
nativeMetadata->exif = Marshal.AllocHGlobal(metadata.exif.Length);
Marshal.Copy(metadata.exif, 0, nativeMetadata->exif, metadata.exif.Length);
nativeMetadata->exifSize = new UIntPtr((uint)metadata.exif.Length);
}
else
{
nativeMetadata->exif = IntPtr.Zero;
nativeMetadata->exifSize = UIntPtr.Zero;
}
if (metadata.xmp != null && metadata.xmp.Length > 0)
{
nativeMetadata->xmp = Marshal.AllocHGlobal(metadata.xmp.Length);
Marshal.Copy(metadata.xmp, 0, nativeMetadata->xmp, metadata.xmp.Length);
nativeMetadata->xmpSize = new UIntPtr((uint)metadata.xmp.Length);
}
else
{
nativeMetadata->xmp = IntPtr.Zero;
nativeMetadata->xmpSize = UIntPtr.Zero;
}
}
return nativeStructure;
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
return null;
}
}
}