/
CIImage.cs
220 lines (194 loc) · 7.14 KB
/
CIImage.cs
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// CIImage.cs: Extensions
//
// Copyright 2011-2015 Xamarin Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using XamCore.Foundation;
using XamCore.CoreFoundation;
using XamCore.CoreGraphics;
using XamCore.ObjCRuntime;
#if !MONOMAC
using XamCore.UIKit;
using XamCore.CoreVideo;
#endif
namespace XamCore.CoreImage {
public class CIAutoAdjustmentFilterOptions {
// The default value is true.
public bool? Enhance;
// The default value is true
public bool? RedEye;
public CIFeature [] Features;
public CIImageOrientation? ImageOrientation;
[iOS (8,0)]
public bool? AutoAdjustCrop;
[iOS (8,0)]
public bool? AutoAdjustLevel;
internal NSDictionary ToDictionary ()
{
int n = 0;
if (Enhance.HasValue && Enhance.Value == false)
n++;
if (RedEye.HasValue && RedEye.Value == false)
n++;
if (ImageOrientation.HasValue)
n++;
if (Features != null && Features.Length != 0)
n++;
if (AutoAdjustCrop.HasValue && AutoAdjustCrop.Value == true)
n++;
if (AutoAdjustLevel.HasValue && AutoAdjustLevel.Value == true)
n++;
if (n == 0)
return null;
NSMutableDictionary dict = new NSMutableDictionary ();
if (Enhance.HasValue && Enhance.Value == false){
dict.LowlevelSetObject (CFBoolean.False.Handle, CIImage.AutoAdjustEnhanceKey.Handle);
}
if (RedEye.HasValue && RedEye.Value == false){
dict.LowlevelSetObject (CFBoolean.False.Handle, CIImage.AutoAdjustRedEyeKey.Handle);
}
if (Features != null && Features.Length != 0){
dict.LowlevelSetObject (NSArray.FromObjects (Features), CIImage.AutoAdjustFeaturesKey.Handle);
}
if (ImageOrientation.HasValue){
dict.LowlevelSetObject (new NSNumber ((int)ImageOrientation.Value), global::XamCore.ImageIO.CGImageProperties.Orientation.Handle);
}
if (AutoAdjustCrop.HasValue && AutoAdjustCrop.Value == true){
dict.LowlevelSetObject (CFBoolean.True.Handle, CIImage.AutoAdjustCrop.Handle);
}
if (AutoAdjustLevel.HasValue && AutoAdjustLevel.Value == true){
dict.LowlevelSetObject (CFBoolean.True.Handle, CIImage.AutoAdjustLevel.Handle);
}
#if false
for (i = 0; i < n; i++){
Console.WriteLine ("{0} {1}-{2}", i, keys [i], values [i]);
}
#endif
return dict;
}
}
public partial class CIImage {
#if !MONOMAC && !XAMCORE_2_0
[Obsolete ("A CIImage cannot be created from a CGLayer on iOS (only OSX)")]
public CIImage (CGLayer layer)
{
throw new NotSupportedException ();
}
[Obsolete ("A CIImage cannot be created from a CGLayer on iOS (only OSX)")]
public CIImage (CGLayer layer, NSDictionary d)
{
throw new NotSupportedException ();
}
[Obsolete ("A CIImage cannot be created from a CGLayer on iOS (only OSX)")]
public CIImage (CGLayer layer, CIImageInitializationOptions options)
{
throw new NotSupportedException ();
}
#endif
static CIFilter [] WrapFilters (NSArray filters)
{
if (filters == null)
return new CIFilter [0];
nuint count = filters.Count;
if (count == 0)
return new CIFilter [0];
var ret = new CIFilter [count];
for (nuint i = 0; i < count; i++){
IntPtr filterHandle = filters.ValueAt (i);
string filterName = CIFilter.GetFilterName (filterHandle);
ret [i] = CIFilter.FromName (filterName, filterHandle);
}
return ret;
}
public static CIImage FromCGImage (CGImage image, CGColorSpace colorSpace)
{
if (colorSpace == null)
throw new ArgumentNullException ("colorSpace");
using (var arr = NSArray.FromIntPtrs (new IntPtr [] { colorSpace.Handle })){
using (var keys = NSArray.FromIntPtrs (new IntPtr [] { CIImageInitializationOptionsKeys.ColorSpaceKey.Handle } )){
using (var dict = NSDictionary.FromObjectsAndKeysInternal (arr, keys)){
return FromCGImage (image, dict);
}
}
}
}
// Apple removed this API in iOS9 SDK
public CIFilter [] GetAutoAdjustmentFilters ()
{
return GetAutoAdjustmentFilters (null);
}
public CIFilter [] GetAutoAdjustmentFilters (CIAutoAdjustmentFilterOptions options)
{
NSDictionary dict = options == null ? null : options.ToDictionary ();
return WrapFilters (_GetAutoAdjustmentFilters (dict));
}
public static implicit operator CIImage (CGImage image)
{
return FromCGImage (image);
}
internal static int CIFormatToInt (CIFormat format)
{
switch (format) {
case CIFormat.ARGB8: return FormatARGB8;
case CIFormat.RGBAh: return FormatRGBAh;
#if MONOMAC
case CIFormat.RGBA16: return FormatRGBA16;
case CIFormat.RGBAf: return FormatRGBAf;
#elif !XAMCORE_3_0
case CIFormat.BGRA8: return FormatBGRA8;
case CIFormat.RGBA8: return FormatRGBA8;
#endif
case CIFormat.kRGBAf: return FormatRGBAf;
case CIFormat.kBGRA8: return FormatBGRA8;
case CIFormat.kRGBA8: return FormatRGBA8;
case CIFormat.ABGR8: return FormatABGR8;
case CIFormat.A8: return FormatA8;
case CIFormat.A16: return FormatA16;
case CIFormat.Ah: return FormatAh;
case CIFormat.Af: return FormatAf;
case CIFormat.R8: return FormatR8;
case CIFormat.R16: return FormatR16;
case CIFormat.Rh: return FormatRh;
case CIFormat.Rf: return FormatRf;
case CIFormat.RG8: return FormatRG8;
case CIFormat.RG16: return FormatRG16;
case CIFormat.RGh: return FormatRGh;
case CIFormat.RGf: return FormatRGf;
default:
throw new ArgumentOutOfRangeException ("format");
}
}
public static CIImage FromData (NSData bitmapData, nint bytesPerRow, CGSize size, CIFormat pixelFormat, CGColorSpace colorSpace)
{
return FromData (bitmapData, bytesPerRow, size, CIImage.CIFormatToInt (pixelFormat), colorSpace);
}
public static CIImage FromProvider (ICIImageProvider provider, nuint width, nuint height, CIFormat pixelFormat, CGColorSpace colorSpace, CIImageProviderOptions options)
{
return FromProvider (provider, width, height, CIImage.CIFormatToInt (pixelFormat), colorSpace, options == null ? null : options.Dictionary);
}
public CIImage (ICIImageProvider provider, nuint width, nuint height, CIFormat pixelFormat, CGColorSpace colorSpace, CIImageProviderOptions options)
: this (provider, width, height, CIImage.CIFormatToInt (pixelFormat), colorSpace, options == null ? null : options.Dictionary)
{
}
}
}