Release 4.0.0-preview1-00051
[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     public static class ImageUtil
28     {
29         /// <summary>
30         /// Retrieves supported colorspaces for a <see cref="ImageFormat"/> that represents formats for <see cref="ImageEncoder"/> and <see cref="ImageDecoder"/>.
31         /// </summary>
32         /// <returns>An IEnumerable of <see cref="ColorSpace"/> representing the supported color-spaces.</returns>
33         /// <param name="format">The <see cref="ImageFormat"/>.</param>
34         /// <exception cref="ArgumentException"><paramref name="format"/> is invalid.</exception>
35         public static IEnumerable<ColorSpace> GetSupportedColorSpaces(ImageFormat format)
36         {
37             ValidationUtil.ValidateEnum(typeof(ImageFormat), format, nameof(format));
38
39             var colorspaces = new List<ColorSpace>();
40
41             ForeachSupportedColorspace(format,
42                 (colorspace, _) => { colorspaces.Add(colorspace.ToCommonColorSpace()); return true; }).
43                 ThrowIfFailed("Failed to get supported color-space list from native handle");
44
45             return colorspaces;
46         }
47
48         /// <summary>
49         /// Calculates the size of the image buffer for the specified resolution and color-space.
50         /// </summary>
51         /// <param name="resolution">The resolution of the image.</param>
52         /// <param name="colorSpace"><see cref="ColorSpace"/> of the image.</param>
53         /// <returns>The buffer size.</returns>
54         /// <exception cref="ArgumentOutOfRangeException">
55         ///     width of <paramref name="resolution"/> is less than or equal to zero.\n
56         ///     - or -\n
57         ///     height of <paramref name="resolution"/> is less than or equal to zero.
58         /// </exception>
59         /// <exception cref="ArgumentException"><paramref name="colorSpace"/> is invalid.</exception>
60         public static int CalculateBufferSize(Size resolution, ColorSpace colorSpace)
61         {
62             if (resolution.Width <= 0)
63             {
64                 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Width,
65                     "width can't be less than or equal to zero.");
66             }
67             if (resolution.Height <= 0)
68             {
69                 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Height,
70                     "height can't be less than or equal to zero.");
71             }
72
73             ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace));
74
75             uint bufferSize;
76             global::Interop.ImageUtil.CalculateBufferSize(resolution.Width, resolution.Height,
77                 colorSpace.ToImageColorSpace(), out bufferSize)
78                 .ThrowIfFailed("Failed to calculate buffer size for given parameter");
79
80             return (int)bufferSize;
81         }
82
83         /// <summary>
84         /// Extracts representative color from an image buffer.
85         /// </summary>
86         /// <param name="buffer">Raw image buffer.</param>
87         /// <param name="size">Resolution of the image.</param>
88         /// <remarks>The image should be <see cref="ColorSpace.Rgb888"/>.</remarks>
89         /// <returns>The representative color of the image.</returns>
90         /// <see cref="BitmapFrame"/>
91         /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
92         /// <exception cref="ArgumentException"><paramref name="buffer"/> is empty.</exception>
93         /// <exception cref="ArgumentOutOfRangeException">
94         ///     width of <paramref name="size"/> is less than or equal to zero.\n
95         ///     - or -\n
96         ///     height of <paramref name="size"/> is less than or equal to zero.
97         /// </exception>
98         public static Color GetColor(byte[] buffer, Size size)
99         {
100             if (buffer == null)
101             {
102                 throw new ArgumentNullException(nameof(buffer));
103             }
104
105             if (buffer.Length == 0)
106             {
107                 throw new ArgumentException("buffer is empty.", nameof(buffer));
108             }
109
110             if (size.Width <= 0)
111             {
112                 throw new ArgumentOutOfRangeException(nameof(size), size.Width,
113                     "width can't be less than or equal to zero.");
114             }
115             if (size.Height <= 0)
116             {
117                 throw new ArgumentOutOfRangeException(nameof(size), size.Height,
118                     "height can't be less than or equal to zero.");
119             }
120
121
122             byte r, g, b;
123             ExtractColorFromMemory(buffer, size.Width, size.Height, out r, out g, out b)
124                 .ThrowIfFailed("Failed to extract color from buffer");
125
126             return Color.FromRgb(r, g, b);
127         }
128     }
129 }