[Multimedia.Util] Modified to throw NotSupportedException for NotSupported error...
[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 static 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             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         /// Calculates the size of the image buffer for the specified resolution and color-space.
52         /// </summary>
53         /// <param name="resolution">The resolution of the image.</param>
54         /// <param name="colorSpace"><see cref="ColorSpace"/> of the image.</param>
55         /// <returns>The buffer size.</returns>
56         /// <exception cref="ArgumentOutOfRangeException">
57         ///     width of <paramref name="resolution"/> is less than or equal to zero.<br/>
58         ///     -or-<br/>
59         ///     height of <paramref name="resolution"/> is less than or equal to zero.
60         /// </exception>
61         /// <exception cref="ArgumentException"><paramref name="colorSpace"/> is invalid.</exception>
62         /// <since_tizen> 4 </since_tizen>
63         public static int CalculateBufferSize(Size resolution, ColorSpace colorSpace)
64         {
65             if (resolution.Width <= 0)
66             {
67                 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Width,
68                     "width can't be less than or equal to zero.");
69             }
70             if (resolution.Height <= 0)
71             {
72                 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Height,
73                     "height can't be less than or equal to zero.");
74             }
75
76             ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace));
77
78             uint bufferSize;
79             global::Interop.ImageUtil.CalculateBufferSize(resolution.Width, resolution.Height,
80                 colorSpace.ToImageColorSpace(), out bufferSize)
81                 .ThrowIfFailed("Failed to calculate buffer size for given parameter");
82
83             return (int)bufferSize;
84         }
85
86         /// <summary>
87         /// Extracts representative color from an image buffer.
88         /// </summary>
89         /// <param name="buffer">Raw image buffer.</param>
90         /// <param name="size">Resolution of the image.</param>
91         /// <remarks>The image should be <see cref="ColorSpace.Rgb888"/>.</remarks>
92         /// <returns>The representative color of the image.</returns>
93         /// <see cref="BitmapFrame"/>
94         /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
95         /// <exception cref="ArgumentException"><paramref name="buffer"/> is empty.</exception>
96         /// <exception cref="ArgumentOutOfRangeException">
97         ///     width of <paramref name="size"/> is less than or equal to zero.<br/>
98         ///     -or-<br/>
99         ///     height of <paramref name="size"/> is less than or equal to zero.
100         /// </exception>
101         /// <since_tizen> 4 </since_tizen>
102         public static Color GetColor(byte[] buffer, Size size)
103         {
104             if (buffer == null)
105             {
106                 throw new ArgumentNullException(nameof(buffer));
107             }
108
109             if (buffer.Length == 0)
110             {
111                 throw new ArgumentException("buffer is empty.", nameof(buffer));
112             }
113
114             if (size.Width <= 0)
115             {
116                 throw new ArgumentOutOfRangeException(nameof(size), size.Width,
117                     "width can't be less than or equal to zero.");
118             }
119             if (size.Height <= 0)
120             {
121                 throw new ArgumentOutOfRangeException(nameof(size), size.Height,
122                     "height can't be less than or equal to zero.");
123             }
124
125             ExtractColorFromMemory(buffer, size.Width, size.Height, out var r, out var g, out var b)
126                 .ThrowIfFailed("Failed to extract color from buffer");
127
128             return Color.FromRgb(r, g, b);
129         }
130     }
131 }