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 NativeUtil = 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 NativeUtil.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 [Obsolete("Please do not use! This will be deprecated in level 6.")]
64 public static int CalculateBufferSize(Size resolution, ColorSpace colorSpace)
66 if (resolution.Width <= 0)
68 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Width,
69 "width can't be less than or equal to zero.");
71 if (resolution.Height <= 0)
73 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Height,
74 "height can't be less than or equal to zero.");
77 ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace));
80 global::Interop.ImageUtil.CalculateBufferSize(resolution.Width, resolution.Height,
81 colorSpace.ToImageColorSpace(), out bufferSize)
82 .ThrowIfFailed("Failed to calculate buffer size for given parameter");
84 return (int)bufferSize;
88 /// Extracts representative color from an image buffer.
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/>
100 /// height of <paramref name="size"/> is less than or equal to zero.
102 /// <since_tizen> 4 </since_tizen>
103 public static Color GetColor(byte[] buffer, Size size)
107 throw new ArgumentNullException(nameof(buffer));
110 if (buffer.Length == 0)
112 throw new ArgumentException("buffer is empty.", nameof(buffer));
117 throw new ArgumentOutOfRangeException(nameof(size), size.Width,
118 "width can't be less than or equal to zero.");
120 if (size.Height <= 0)
122 throw new ArgumentOutOfRangeException(nameof(size), size.Height,
123 "height can't be less than or equal to zero.");
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");
129 return Color.FromRgb(r, g, b);