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 public static class ImageUtil
30 /// Retrieves supported colorspaces for a <see cref="ImageFormat"/> that represents formats for <see cref="ImageEncoder"/> and <see cref="ImageDecoder"/>.
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)
37 ValidationUtil.ValidateEnum(typeof(ImageFormat), format, nameof(format));
39 var colorspaces = new List<ColorSpace>();
41 ForeachSupportedColorspace(format,
42 (colorspace, _) => { colorspaces.Add(colorspace.ToCommonColorSpace()); return true; }).
43 ThrowIfFailed("Failed to get supported color-space list from native handle");
49 /// Calculates the size of the image buffer for the specified resolution and color-space.
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.<br/>
57 /// height of <paramref name="resolution"/> is less than or equal to zero.
59 /// <exception cref="ArgumentException"><paramref name="colorSpace"/> is invalid.</exception>
60 public static int CalculateBufferSize(Size resolution, ColorSpace colorSpace)
62 if (resolution.Width <= 0)
64 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Width,
65 "width can't be less than or equal to zero.");
67 if (resolution.Height <= 0)
69 throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Height,
70 "height can't be less than or equal to zero.");
73 ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace));
76 global::Interop.ImageUtil.CalculateBufferSize(resolution.Width, resolution.Height,
77 colorSpace.ToImageColorSpace(), out bufferSize)
78 .ThrowIfFailed("Failed to calculate buffer size for given parameter");
80 return (int)bufferSize;
84 /// Extracts representative color from an image buffer.
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.<br/>
96 /// height of <paramref name="size"/> is less than or equal to zero.
98 public static Color GetColor(byte[] buffer, Size size)
102 throw new ArgumentNullException(nameof(buffer));
105 if (buffer.Length == 0)
107 throw new ArgumentException("buffer is empty.", nameof(buffer));
112 throw new ArgumentOutOfRangeException(nameof(size), size.Width,
113 "width can't be less than or equal to zero.");
115 if (size.Height <= 0)
117 throw new ArgumentOutOfRangeException(nameof(size), size.Height,
118 "height can't be less than or equal to zero.");
123 ExtractColorFromMemory(buffer, size.Width, size.Height, out r, out g, out b)
124 .ThrowIfFailed("Failed to extract color from buffer");
126 return Color.FromRgb(r, g, b);