/
ThumbnailExtractor.cs
384 lines (355 loc) · 20.9 KB
/
ThumbnailExtractor.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Native = Interop.ThumbnailExtractor;
namespace Tizen.Multimedia.Util
{
/// <summary>
/// Provides the ability to extract the thumbnail from media files.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public static class ThumbnailExtractor
{
/// <summary>
/// Extracts the thumbnail for the given media with the specified path.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <returns>A task that represents the asynchronous extracting operation.</returns>
/// <remarks>The size of the thumbnail will be the default size (320x240).</remarks>
/// <param name="path">The path of the media file to extract the thumbnail.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="UnauthorizedAccessException">The caller does not have required privilege for accessing the <paramref name="path"/>.</exception>
/// <exception cref="FileFormatException">The specified file is not supported.</exception>
[Obsolete("Deprecated since API10; Will be removed in API12. Please use Extract instead.")]
public static Task<ThumbnailExtractionResult> ExtractAsync(string path)
{
return RunExtractAsync(path, null, CancellationToken.None);
}
/// <summary>
/// Extracts the thumbnail for the given media with the specified path.
/// </summary>
/// <returns>A task that represents the asynchronous extracting operation.</returns>
/// <remarks>The size of the thumbnail will be the default size(320x240).</remarks>
/// <param name="path">The path of the media file to extract the thumbnail.</param>
/// <param name="cancellationToken">The token to stop the operation.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="UnauthorizedAccessException">The caller does not have required privilege for accessing the <paramref name="path"/>.</exception>
/// <exception cref="FileFormatException">The specified file is not supported.</exception>
/// <since_tizen> 4 </since_tizen>
[Obsolete("Deprecated since API10; Will be removed in API12. Please use Extract instead.")]
public static Task<ThumbnailExtractionResult> ExtractAsync(string path, CancellationToken cancellationToken)
{
return RunExtractAsync(path, null, cancellationToken);
}
/// <summary>
/// Extracts the thumbnail for the given media with the specified path and size.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <returns>A task that represents the asynchronous extracting operation.</returns>
/// <remarks>
/// If the width is not a multiple of 8, it can be changed by the inner process.<br/>
/// The width will be a multiple of 8 greater than the set value.
/// </remarks>
/// <param name="path">The path of the media file to extract the thumbnail.</param>
/// <param name="size">The size of the thumbnail.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="UnauthorizedAccessException">The caller does not have required privilege for accessing the <paramref name="path"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// The width or the height of <paramref name="size"/> is less than or equal to zero.
/// </exception>
/// <exception cref="FileFormatException">The specified file is not supported.</exception>
[Obsolete("Deprecated since API10; Will be removed in API12. Please use Extract instead.")]
public static Task<ThumbnailExtractionResult> ExtractAsync(string path, Size size)
{
return RunExtractAsync(path, size, CancellationToken.None);
}
/// <summary>
/// Extracts the thumbnail for the given media with the specified path and size.
/// </summary>
/// <since_tizen> 4 </since_tizen>
/// <returns>A task that represents the asynchronous extracting operation.</returns>
/// <remarks>
/// If the width is not a multiple of 8, it can be changed by the inner process.<br/>
/// The width will be a multiple of 8 greater than the set value.
/// </remarks>
/// <param name="path">The path of the media file to extract the thumbnail.</param>
/// <param name="size">The size of the thumbnail.</param>
/// <param name="cancellationToken">The token to stop the operation.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="UnauthorizedAccessException">The caller does not have required privilege for accessing the <paramref name="path"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// The width or the height of <paramref name="size"/> is less than or equal to zero.
/// </exception>
/// <exception cref="FileFormatException">The specified file is not supported.</exception>
[Obsolete("Deprecated since API10; Will be removed in API12. Please use Extract instead.")]
public static Task<ThumbnailExtractionResult> ExtractAsync(string path, Size size,
CancellationToken cancellationToken)
{
return RunExtractAsync(path, size, cancellationToken);
}
private static Task<ThumbnailExtractionResult> RunExtractAsync(string path, Size? size,
CancellationToken cancellationToken)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
if (!File.Exists(path))
{
throw new FileNotFoundException("File does not exists.", path);
}
if (size.HasValue)
{
if (size.Value.Width <= 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size.Value.Width,
"The width must be greater than zero.");
}
if (size.Value.Height <= 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size.Value.Height,
"The height must be greater than zero.");
}
}
return cancellationToken.IsCancellationRequested ?
Task.FromCanceled<ThumbnailExtractionResult>(cancellationToken) :
ExtractAsyncCore(path, size, cancellationToken);
}
private static async Task<ThumbnailExtractionResult> ExtractAsyncCore(string path, Size? size,
CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<ThumbnailExtractionResult>();
Task thumbTask = null;
if (cancellationToken.CanBeCanceled)
{
cancellationToken.Register(() =>
{
if (tcs.Task.IsCompleted)
{
return;
}
tcs.TrySetCanceled();
});
}
thumbTask = Task.Factory.StartNew( () =>
{
try
{
var result = Extract(path, size.HasValue ? size.Value : new Size(320, 240));
if (result != null)
{
tcs.TrySetResult(result);
}
else
{
tcs.TrySetException(new InvalidOperationException("Failed to extract thumbnail"));
}
}
catch (Exception e)
{
Log.Error("Tizen.Multimedia.Util", e.ToString());
tcs.TrySetException(e);
}
}, cancellationToken,
TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
TaskScheduler.Default);
return await tcs.Task;
}
/// <summary>
/// Extracts the thumbnail for the given media with the specified path and size.
/// The generated thumbnail will be returned in <see cref="ThumbnailExtractionResult"/>.
/// </summary>
/// <remarks>
/// The size of generated thumbnail will be 320x240.<br/>
/// If the size of <paramref name="path"/> has different ratio from 320x240 (approximately 1.33:1),<br/>
/// thumbnail is generated in a way to keep the ratio of <paramref name="path"/>, which is based on short axis of <paramref name="path"/>.<br/>
/// For example, if the size of <paramref name="path"/> is 900x500 (1.8:1), the size of generated thumbnail is 432x240(1.8:1).<br/>
/// To set the size different from 320x240, please use <see cref="Extract(string, Size)"/>.<br/>
/// <br/>
/// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage. <br/>
/// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
/// </remarks>
/// <privilege>http://tizen.org/privilege/mediastorage</privilege>
/// <privilege>http://tizen.org/privilege/externalstorage</privilege>
/// <param name="path">The path of the media file to extract the thumbnail.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="FileFormatException">The specified file is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
/// <returns>The result of extracting operation.</returns>
/// <since_tizen> 6 </since_tizen>
public static ThumbnailExtractionResult Extract(string path)
{
return Extract(path, new Size(320, 240));
}
/// <summary>
/// Extracts the thumbnail for the given media with the specified path and size.
/// The generated thumbnail will be returned in <see cref="ThumbnailExtractionResult"/>.
/// </summary>
/// <remarks>
/// The size of generated thumbnail will be <paramref name="size"/>.<br/>
/// But, if the size of <paramref name="path"/> has different ratio with <paramref name="size"/>,<br/>
/// thumbnail will be generated in a way to keep the ratio of <paramref name="path"/>, which based on short axis of <paramref name="path"/>.<br/>
/// For example, if the size of <paramref name="path"/> is 900x500 (1.8:1)) and <paramref name="size"/> is 320x240,<br/>
/// the size of generated thumbnail is 432x240(1.8:1).<br/>
/// <br/>
/// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage. <br/>
/// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
/// </remarks>
/// <privilege>http://tizen.org/privilege/mediastorage</privilege>
/// <privilege>http://tizen.org/privilege/externalstorage</privilege>
/// <param name="path">The path of the media file to extract the thumbnail.</param>
/// <param name="size">The size of the thumbnail.</param>
/// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// The width or the height of <paramref name="size"/> is less than or equal to zero.
/// </exception>
/// <exception cref="FileFormatException">The specified file is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
/// <returns>The result of extracting operation.</returns>
/// <since_tizen> 6 </since_tizen>
public static ThumbnailExtractionResult Extract(string path, Size size)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
if (File.Exists(path) == false)
{
throw new FileNotFoundException("File does not exists.", path);
}
if (size.Width <= 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size.Width,
"The width must be greater than zero.");
}
if (size.Height <= 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size.Height,
"The height must be greater than zero.");
}
Native.ExtractToBuffer(path, (uint)size.Width, (uint)size.Height, out IntPtr thumbData,
out int dataSize, out uint thumbWidth, out uint thumbHeight).
ThrowIfError("Failed to extract thumbnail to buffer");
try
{
return new ThumbnailExtractionResult(thumbData, (int)thumbWidth, (int)thumbHeight,
dataSize);
}
finally
{
if (thumbData != IntPtr.Zero)
{
Marshal.FreeHGlobal(thumbData);
}
}
}
/// <summary>
/// Extracts the thumbnail for the given media with the specified path and size.
/// The generated thumbnail will be saved in <paramref name="resultThumbnailPath"/>.
/// </summary>
/// <remarks>
/// The size of <paramref name="resultThumbnailPath"/> image will be 320x240.<br/>
/// If the size of <paramref name="path"/> has different ratio with 320x240 (approximately 1.33:1),<br/>
/// thumbnail is generated in a way to keep the ratio of <paramref name="path"/>, which is based on short axis of <paramref name="path"/>.<br/>
/// For example, if the size of <paramref name="path"/> is 900x500 (1.8:1), the size of <paramref name="resultThumbnailPath"/> is 432x240(1.8:1).<br/>
/// To set the size different from 320x240, please use <see cref="Extract(string, Size, string)"/>.<br/>
/// <br/>
/// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage. <br/>
/// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
/// </remarks>
/// <privilege>http://tizen.org/privilege/mediastorage</privilege>
/// <privilege>http://tizen.org/privilege/externalstorage</privilege>
/// <param name="path">The path of the media file to extract the thumbnail.</param>
/// <param name="resultThumbnailPath">The path to save the generated thumbnail.</param>
/// <exception cref="ArgumentException"><paramref name="path"/> or <paramref name="resultThumbnailPath"/> is invalid.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="resultThumbnailPath"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="FileFormatException">The specified file is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
/// <since_tizen> 6 </since_tizen>
public static void Extract(string path, string resultThumbnailPath)
{
Extract(path, new Size(320, 240), resultThumbnailPath);
}
/// <summary>
/// Extracts the thumbnail for the given media with the specified path and size.
/// The generated thumbnail will be saved in <paramref name="resultThumbnailPath"/>.
/// </summary>
/// <remarks>
/// The size of <paramref name="resultThumbnailPath"/> image will be <paramref name="size"/>.<br/>
/// But, if the size of <paramref name="path"/> has different ratio with <paramref name="size"/>,<br/>
/// thumbnail will be generated in a way to keep the ratio of <paramref name="path"/>, which based on short axis of <paramref name="path"/>.<br/>
/// For example, if the size of <paramref name="path"/> is 900x500 (1.8:1) and <paramref name="size"/> is 320x240,<br/>
/// the size of <paramref name="resultThumbnailPath"/> is 432x240(1.8:1).<br/>
/// <br/>
/// If you want to access internal storage, you should add privilege http://tizen.org/privilege/mediastorage. <br/>
/// If you want to access external storage, you should add privilege http://tizen.org/privilege/externalstorage.
/// </remarks>
/// <privilege>http://tizen.org/privilege/mediastorage</privilege>
/// <privilege>http://tizen.org/privilege/externalstorage</privilege>
/// <param name="path">The path of the media file to extract the thumbnail.</param>
/// <param name="size">The size of the thumbnail.</param>
/// <param name="resultThumbnailPath">The path to save the generated thumbnail.</param>
/// <exception cref="ArgumentException"><paramref name="path"/> or <paramref name="resultThumbnailPath"/> is invalid.</exception>
/// <exception cref="ArgumentNullException"><paramref name="path"/> or <paramref name="resultThumbnailPath"/> is null.</exception>
/// <exception cref="FileNotFoundException"><paramref name="path"/> does not exist.</exception>
/// <exception cref="InvalidOperationException">An internal error occurs.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// The width or the height of <paramref name="size"/> is less than or equal to zero.
/// </exception>
/// <exception cref="FileFormatException">The specified file is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">The caller has no required privilege.</exception>
/// <since_tizen> 6 </since_tizen>
public static void Extract(string path, Size size, string resultThumbnailPath)
{
ValidationUtil.ValidateIsNullOrEmpty(path, nameof(path));
ValidationUtil.ValidateIsNullOrEmpty(resultThumbnailPath, nameof(resultThumbnailPath));
if (File.Exists(path) == false)
{
throw new FileNotFoundException("File does not exists.", path);
}
if (size.Width <= 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size.Width,
"The width must be greater than zero.");
}
if (size.Height <= 0)
{
throw new ArgumentOutOfRangeException(nameof(size), size.Height,
"The height must be greater than zero.");
}
Native.ExtractToFile(path, (uint)size.Width, (uint)size.Height, resultThumbnailPath).
ThrowIfError("Failed to extract thumbnail to file.");
}
}
}