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