[ImageUtil] Remove deprecated API and change native pinvoke APIs (#5950)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Util / ImageUtil / ImageUtil.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using Tizen.Common;
20 using NativeUtil = Interop.ImageUtil;
21
22 namespace Tizen.Multimedia.Util
23 {
24     /// <summary>
25     /// Provides utilities for images.
26     /// </summary>
27     /// <since_tizen> 4 </since_tizen>
28     public static class ImageUtil
29     {
30         /// <summary>
31         /// Retrieves supported colorspaces for a <see cref="ImageFormat"/> that represents formats for <see cref="ImageEncoder"/> and <see cref="ImageDecoder"/>.
32         /// </summary>
33         /// <returns>An IEnumerable of <see cref="ColorSpace"/> representing the supported color-spaces.</returns>
34         /// <param name="format">The <see cref="ImageFormat"/>.</param>
35         /// <exception cref="ArgumentException"><paramref name="format"/> is invalid.</exception>
36         /// <since_tizen> 4 </since_tizen>
37         public static IEnumerable<ColorSpace> GetSupportedColorSpaces(ImageFormat format)
38         {
39             ValidationUtil.ValidateEnum(typeof(ImageFormat), format, nameof(format));
40
41             var colorspaces = new List<ColorSpace>();
42
43             NativeUtil.ForeachSupportedColorspace(format,
44                 (colorspace, _) => { colorspaces.Add(colorspace.ToCommonColorSpace()); return true; }).
45                 ThrowIfFailed("Failed to get supported color-space list from native handle");
46
47             return colorspaces;
48         }
49
50         /// <summary>
51         /// Extracts representative color from an image buffer.
52         /// </summary>
53         /// <param name="buffer">Raw image buffer.</param>
54         /// <param name="size">Resolution of the image.</param>
55         /// <remarks>The image should be <see cref="ColorSpace.Rgb888"/>.</remarks>
56         /// <returns>The representative color of the image.</returns>
57         /// <see cref="BitmapFrame"/>
58         /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
59         /// <exception cref="ArgumentException"><paramref name="buffer"/> is empty.</exception>
60         /// <exception cref="ArgumentOutOfRangeException">
61         ///     width of <paramref name="size"/> is less than or equal to zero.<br/>
62         ///     -or-<br/>
63         ///     height of <paramref name="size"/> is less than or equal to zero.
64         /// </exception>
65         /// <since_tizen> 4 </since_tizen>
66         public static Color GetColor(byte[] buffer, Size size)
67         {
68             if (buffer == null)
69             {
70                 throw new ArgumentNullException(nameof(buffer));
71             }
72
73             if (buffer.Length == 0)
74             {
75                 throw new ArgumentException("buffer is empty.", nameof(buffer));
76             }
77
78             if (size.Width <= 0)
79             {
80                 throw new ArgumentOutOfRangeException(nameof(size), size.Width,
81                     "width can't be less than or equal to zero.");
82             }
83             if (size.Height <= 0)
84             {
85                 throw new ArgumentOutOfRangeException(nameof(size), size.Height,
86                     "height can't be less than or equal to zero.");
87             }
88
89             NativeUtil.ExtractColorFromMemory(buffer, size.Width, size.Height, out var r, out var g, out var b)
90                 .ThrowIfFailed("Failed to extract color from buffer");
91
92             return Color.FromRgb(r, g, b);
93         }
94     }
95 }