2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Threading.Tasks;
20 using System.Runtime.InteropServices;
22 namespace Tizen.Multimedia
24 static internal class ThumbnailExtractorLog
26 internal const string LogTag = "Tizen.Multimedia.ThumbnailExtractor";
30 /// The Thumbnail extractor class provides a set of functions to extract the thumbnail data of the input media file
32 public class ThumbnailExtractor : IDisposable
34 private bool _disposed = false;
35 internal IntPtr _handle = IntPtr.Zero;
38 /// Thumbnail extractor constructor
41 /// If you need the thumbnail of the specified size, use ThumbnailExtractor(path, width, height) or ThumbnailExtractor(path, size).
43 /// <param name="path"> The path of the media file to extract the thumbnail </param>
44 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
45 public ThumbnailExtractor(string path)
49 throw new ArgumentNullException(nameof(path));
52 ThumbnailExtractorError ret = Interop.ThumbnailExtractor.Create(out _handle);
53 ThumbnailExtractorErrorFactory.ThrowIfError(ret, "Failed to create constructor");
57 ThumbnailExtractorErrorFactory.ThrowIfError(
58 Interop.ThumbnailExtractor.SetPath(_handle, path), "Failed to set the path");
62 Interop.ThumbnailExtractor.Destroy(_handle);
63 _handle = IntPtr.Zero;
68 private void Create(String path, int width, int height)
72 throw new ArgumentNullException(nameof(path));
77 throw new ArgumentOutOfRangeException(nameof(width), "The width must be greater than zero:[" + width + "]");
82 throw new ArgumentOutOfRangeException(nameof(height), "The height must be greater than zero:[" + height + "]");
85 ThumbnailExtractorError ret = Interop.ThumbnailExtractor.Create(out _handle);
86 ThumbnailExtractorErrorFactory.ThrowIfError(ret, "Failed to create constructor");
90 ThumbnailExtractorErrorFactory.ThrowIfError(
91 Interop.ThumbnailExtractor.SetPath(_handle, path), "Failed to set the path");
93 ThumbnailExtractorErrorFactory.ThrowIfError(
94 Interop.ThumbnailExtractor.SetSize(_handle, width, height), "Failed to set the size");
98 Interop.ThumbnailExtractor.Destroy(_handle);
99 _handle = IntPtr.Zero;
105 /// Thumbnail extractor constructor
108 /// If you need the thumbnail of the default size, use ThumbnailExtractor(path). The default size is 320x240. \n
109 /// If the set width is not a multiple of 8, it can be changed by inner process. \n
110 /// The width will be a multiple of 8 greater than the set value.
112 /// <param name="path"> The path of the media file to extract the thumbnail </param>
113 /// <param name="width"> The width of the thumbnail </param>
114 /// <param name="height"> The height of the thumbnail </param>
115 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
116 /// <exception cref="ArgumentOutOfRangeException">
117 /// <paramref name="width"/> or <paramref name="height"/> is less than zero.
119 public ThumbnailExtractor(string path, int width, int height)
121 Create(path, width, height);
125 /// Thumbnail extractor constructor
128 /// If you need the thumbnail of the default size, use ThumbnailExtractor(path). The default size is 320x240. \n
129 /// If the set width is not a multiple of 8, it can be changed by inner process. \n
130 /// The width will be a multiple of 8 greater than the set value.
132 /// <param name="path"> The path of the media file to extract the thumbnail </param>
133 /// <param name="size"> The size to extract the thumbnail </param>
134 /// <exception cref="ArgumentNullException"><paramref name="path"/> is null.</exception>
135 /// <exception cref="ArgumentOutOfRangeException">
136 /// A value of <paramref name="size"/> is less than zero.
138 public ThumbnailExtractor(string path, Size size)
140 Create(path, size.Width, size.Height);
144 /// Extracts the thumbnail for the given media, asynchronously
147 /// Task for creation of Thumbnail. See <see cref="ThumbnailData"/> details.
149 /// <exception cref="ArgumentException">Requested <paramref name="path"/> does not exist.</exception>
150 /// <exception cref="OutOfMemoryException">Memory allocation failed.</exception>
151 /// <exception cref="InvalidOperationException">Internal processing failed.</exception>
152 /// <exception cref="UnauthorizedAccessException">Inaccessible for the requested <paramref name="path"/>.</exception>
153 public Task<ThumbnailData> Extract()
155 if (_handle == IntPtr.Zero)
157 throw new ObjectDisposedException(nameof(ThumbnailExtractor), "Failed to extract thumbnail");
160 var task = new TaskCompletionSource<ThumbnailData>();
162 Interop.ThumbnailExtractor.ThumbnailExtractCallback extractCallback = (ThumbnailExtractorError error,
170 if (error == ThumbnailExtractorError.None)
174 byte[] tmpBuf = new byte[thumbSize];
175 Marshal.Copy(thumbData, tmpBuf, 0, thumbSize);
177 task.SetResult(new ThumbnailData(tmpBuf, thumbWidth, thumbHeight));
181 task.SetException(new InvalidOperationException("[" + error + "] Fail to copy data"));
185 Interop.Libc.Free(thumbData);
190 task.SetException(new InvalidOperationException("[" + error + "] Fail to create thumbnail"));
194 IntPtr id = IntPtr.Zero;
195 ThumbnailExtractorError res = Interop.ThumbnailExtractor.Extract(_handle, extractCallback, IntPtr.Zero, out id);
196 if (id != IntPtr.Zero)
198 Interop.Libc.Free(id);
202 ThumbnailExtractorErrorFactory.ThrowIfError(res, "Failed to extract thumbnail");
208 /// Thumbnail Utility destructor
210 ~ThumbnailExtractor()
215 protected virtual void Dispose(bool disposing)
221 // To be used if there are any other disposable objects
224 if (_handle != IntPtr.Zero)
226 Interop.ThumbnailExtractor.Destroy(_handle);
227 _handle = IntPtr.Zero;
234 public void Dispose()
237 GC.SuppressFinalize(this);