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.
18 using System.Collections.Generic;
20 using static Interop.ImageUtil;
22 namespace Tizen.Multimedia.Util
25 /// Provides utilities for images.
27 /// <since_tizen> 4 </since_tizen>
28 public static class ImageUtil
31 /// Retrieves supported colorspaces for a <see cref="ImageFormat"/> that represents formats for <see cref="ImageEncoder"/> and <see cref="ImageDecoder"/>.
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)
39 ValidationUtil.ValidateEnum(typeof(ImageFormat), format, nameof(format));
41 var colorspaces = new List<ColorSpace>();
43 ForeachSupportedColorspace(format,
44 (colorspace, _) => { colorspaces.Add(colorspace.ToCommonColorSpace()); return true; }).
45 ThrowIfFailed("Failed to get supported color-space list from native handle");
51 /// Calculates the size of the image buffer for the specified resolution and color-space.
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/>
59 /// height of <paramref name="resolution"/> is less than or equal to zero.
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)
65 if (resolution.Width <= 0)
67 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Width,
68 "width can't be less than or equal to zero.");
70 if (resolution.Height <= 0)
72 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Height,
73 "height can't be less than or equal to zero.");
76 ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace));
79 global::Interop.ImageUtil.CalculateBufferSize(resolution.Width, resolution.Height,
80 colorSpace.ToImageColorSpace(), out bufferSize)
81 .ThrowIfFailed("Failed to calculate buffer size for given parameter");
83 return (int)bufferSize;
87 /// Extracts representative color from an image buffer.
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/>
99 /// height of <paramref name="size"/> is less than or equal to zero.
101 /// <since_tizen> 4 </since_tizen>
102 public static Color GetColor(byte[] buffer, Size size)
106 throw new ArgumentNullException(nameof(buffer));
109 if (buffer.Length == 0)
111 throw new ArgumentException("buffer is empty.", nameof(buffer));
116 throw new ArgumentOutOfRangeException(nameof(size), size.Width,
117 "width can't be less than or equal to zero.");
119 if (size.Height <= 0)
121 throw new ArgumentOutOfRangeException(nameof(size), size.Height,
122 "height can't be less than or equal to zero.");
125 ExtractColorFromMemory(buffer, size.Width, size.Height, out var r, out var g, out var b)
126 .ThrowIfFailed("Failed to extract color from buffer");
128 return Color.FromRgb(r, g, b);