/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using Tizen.Common; using static Interop.ImageUtil; namespace Tizen.Multimedia.Util { /// /// Provides utilities for images. /// public static class ImageUtil { /// /// Retrieves supported colorspaces for a that represents formats for and . /// /// An IEnumerable of representing the supported color-spaces. /// The . /// is invalid. public static IEnumerable GetSupportedColorSpaces(ImageFormat format) { ValidationUtil.ValidateEnum(typeof(ImageFormat), format, nameof(format)); var colorspaces = new List(); ForeachSupportedColorspace(format, (colorspace, _) => { colorspaces.Add(colorspace.ToCommonColorSpace()); return true; }). ThrowIfFailed("Failed to get supported color-space list from native handle"); return colorspaces; } /// /// Calculates the size of the image buffer for the specified resolution and color-space. /// /// The resolution of the image. /// of the image. /// The buffer size. /// /// width of is less than or equal to zero.\n /// - or -\n /// height of is less than or equal to zero. /// /// is invalid. public static int CalculateBufferSize(Size resolution, ColorSpace colorSpace) { if (resolution.Width <= 0) { throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Width, "width can't be less than or equal to zero."); } if (resolution.Height <= 0) { throw new ArgumentOutOfRangeException(nameof(resolution), resolution.Height, "height can't be less than or equal to zero."); } ValidationUtil.ValidateEnum(typeof(ColorSpace), colorSpace, nameof(colorSpace)); uint bufferSize; global::Interop.ImageUtil.CalculateBufferSize(resolution.Width, resolution.Height, colorSpace.ToImageColorSpace(), out bufferSize) .ThrowIfFailed("Failed to calculate buffer size for given parameter"); return (int)bufferSize; } /// /// Extracts representative color from an image buffer. /// /// Raw image buffer. /// Resolution of the image. /// The image should be . /// The representative color of the image. /// /// is null. /// is empty. /// /// width of is less than or equal to zero.\n /// - or -\n /// height of is less than or equal to zero. /// public static Color GetColor(byte[] buffer, Size size) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer)); } if (buffer.Length == 0) { throw new ArgumentException("buffer is empty.", nameof(buffer)); } if (size.Width <= 0) { throw new ArgumentOutOfRangeException(nameof(size), size.Width, "width can't be less than or equal to zero."); } if (size.Height <= 0) { throw new ArgumentOutOfRangeException(nameof(size), size.Height, "height can't be less than or equal to zero."); } byte r, g, b; ExtractColorFromMemory(buffer, size.Width, size.Height, out r, out g, out b) .ThrowIfFailed("Failed to extract color from buffer"); return Color.FromRgb(r, g, b); } } }