From 4e1cfede3f3e0f4559e82cfd5064e461ede9664e Mon Sep 17 00:00:00 2001 From: coderhyme Date: Thu, 16 Nov 2017 15:37:50 +0900 Subject: [PATCH] [Multimedia.Util] Modified to throw NotSupportedException for NotSupported error code. Additionally, moved transform classes into own files. Change-Id: I8e283ed484c16898eef8fbdbb1693763c59d0ef8 Signed-off-by: coderhyme --- .../ImageUtil/ColorSpaceTransform.cs | 88 ++++ .../ImageUtil/CropTransform.cs | 116 ++++ .../ImageUtil/FlipTransform.cs | 104 ++++ .../ImageUtil/ImageColorSpace.cs | 5 +- .../ImageUtil/ImageTransform.cs | 584 ++------------------- .../ImageUtil/ImageTransformCollection.cs | 166 ++++++ .../ImageUtil/ImageTransformGroup.cs | 81 +++ .../ImageUtil/ImageTransformer.cs | 7 + src/Tizen.Multimedia.Util/ImageUtil/ImageUtil.cs | 4 +- .../ImageUtil/ResizeTransform.cs | 84 +++ .../ImageUtil/RotateTransform.cs | 88 ++++ 11 files changed, 771 insertions(+), 556 deletions(-) create mode 100644 src/Tizen.Multimedia.Util/ImageUtil/ColorSpaceTransform.cs create mode 100644 src/Tizen.Multimedia.Util/ImageUtil/CropTransform.cs create mode 100644 src/Tizen.Multimedia.Util/ImageUtil/FlipTransform.cs create mode 100644 src/Tizen.Multimedia.Util/ImageUtil/ImageTransformCollection.cs create mode 100644 src/Tizen.Multimedia.Util/ImageUtil/ImageTransformGroup.cs create mode 100644 src/Tizen.Multimedia.Util/ImageUtil/ResizeTransform.cs create mode 100644 src/Tizen.Multimedia.Util/ImageUtil/RotateTransform.cs diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ColorSpaceTransform.cs b/src/Tizen.Multimedia.Util/ImageUtil/ColorSpaceTransform.cs new file mode 100644 index 0000000..e4a468a --- /dev/null +++ b/src/Tizen.Multimedia.Util/ImageUtil/ColorSpaceTransform.cs @@ -0,0 +1,88 @@ +/* + * 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 static Interop.ImageUtil; +using static Interop.ImageUtil.Transform; + +namespace Tizen.Multimedia.Util +{ + /// + /// Changes the colorspace of an image. + /// + /// + /// 4 + public class ColorSpaceTransform : ImageTransform + { + private ImageColorSpace _imageColorSpace; + + /// + /// Initializes a new instance of the class. + /// + /// The colorspace of output image. + /// is invalid. + /// is not supported. + /// + /// 4 + public ColorSpaceTransform(ColorSpace colorSpace) + { + ColorSpace = colorSpace; + } + + /// + /// Gets or sets the colorspace of the result image. + /// + /// is invalid. + /// is not supported. + /// + /// 4 + public ColorSpace ColorSpace + { + get { return _imageColorSpace.ToCommonColorSpace(); } + set + { + ValidationUtil.ValidateEnum(typeof(ColorSpace), value, nameof(ColorSpace)); + + _imageColorSpace = value.ToImageColorSpace(); + } + } + + internal override string GenerateNotSupportedErrorMessage(VideoMediaFormat format) + => $"Converting colorspace from '{format.MimeType}' to '{ColorSpace.ToString()}' is not supported."; + + internal override void Configure(TransformHandle handle) + { + SetColorspace(handle, _imageColorSpace); + } + + + /// + /// Gets the supported colorspaces for . + /// + /// 4 + public static IEnumerable SupportedColorSpaces + { + get + { + foreach (ImageColorSpace value in Enum.GetValues(typeof(ImageColorSpace))) + { + yield return value.ToCommonColorSpace(); + } + } + } + } +} diff --git a/src/Tizen.Multimedia.Util/ImageUtil/CropTransform.cs b/src/Tizen.Multimedia.Util/ImageUtil/CropTransform.cs new file mode 100644 index 0000000..489a0ad --- /dev/null +++ b/src/Tizen.Multimedia.Util/ImageUtil/CropTransform.cs @@ -0,0 +1,116 @@ +/* + * 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 static Interop.ImageUtil; +using static Interop.ImageUtil.Transform; + +namespace Tizen.Multimedia.Util +{ + /// + /// Crops an image. + /// + /// 4 + public class CropTransform : ImageTransform + { + private Rectangle _region; + + /// + /// Initializes a new instance of the class. + /// + /// The crop region. + /// + /// The X-position of is less than zero.
+ /// -or-
+ /// The Y-position of is less than zero.
+ /// -or-
+ /// The width of is less than or equal to zero.
+ /// -or-
+ /// The height of is less than or equal to zero. + ///
+ /// 4 + public CropTransform(Rectangle region) + { + Region = region; + } + + /// + /// Gets or sets the crop region. + /// + /// + /// The X-position of is less than zero.
+ /// -or-
+ /// The Y-position of is less than zero.
+ /// -or-
+ /// The width of is less than or equal to zero.
+ /// -or-
+ /// The height of is less than or equal to zero. + ///
+ /// 4 + public Rectangle Region + { + get { return _region; } + set + { + + if (value.X < 0) + { + throw new ArgumentOutOfRangeException(nameof(Region), value, + "X position of the region can't be less than zero."); + } + + if (value.Y < 0) + { + throw new ArgumentOutOfRangeException(nameof(Region), value, + "Y position of the region can't be less than zero."); + } + + if (value.Width <= 0) + { + throw new ArgumentOutOfRangeException(nameof(Region), value, + "Width of the region can't be less than or equal zero."); + } + + if (value.Height < 0) + { + throw new ArgumentOutOfRangeException(nameof(Region), value, + "Height of the region can't be less than or equal to zero."); + } + + _region = value; + } + } + + internal override void Configure(TransformHandle handle) + { + SetCropArea(handle, Region.Left, Region.Top, Region.Right, Region.Bottom); + } + + internal override void ValidateFormat(VideoMediaFormat format) + { + if (format.Size.Width < Region.Right || format.Size.Height < Region.Bottom) + { + throw new InvalidOperationException( + $"Crop region is invalid; Source size({format.Size.ToString()}), Crop region({Region.ToString()})."); + } + } + + internal override string GenerateNotSupportedErrorMessage(VideoMediaFormat format) + { + return $"'{format.MimeType}' is not supported by CropTransform."; + } + } +} diff --git a/src/Tizen.Multimedia.Util/ImageUtil/FlipTransform.cs b/src/Tizen.Multimedia.Util/ImageUtil/FlipTransform.cs new file mode 100644 index 0000000..71da15e --- /dev/null +++ b/src/Tizen.Multimedia.Util/ImageUtil/FlipTransform.cs @@ -0,0 +1,104 @@ +/* + * 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.Threading.Tasks; +using static Interop.ImageUtil; +using static Interop.ImageUtil.Transform; + +namespace Tizen.Multimedia.Util +{ + /// + /// Flips an image. + /// + /// + /// 4 + public class FlipTransform : ImageTransform + { + private Flips _flip; + + /// + /// Initializes a new instance of the class. + /// + /// The value how to flip an image. + /// is invalid. + /// is . + /// 4 + public FlipTransform(Flips flip) + { + Flip = flip; + } + + /// + /// Gets or sets the value how to flip an image. + /// + /// is invalid. + /// is . + /// 4 + public Flips Flip + { + get { return _flip; } + set + { + ValidationUtil.ValidateFlagsEnum(value, Flips.Horizontal | Flips.Vertical, nameof(Flips)); + + if (value == Flips.None) + { + throw new ArgumentOutOfRangeException(nameof(value), "Flip can't be None."); + } + + _flip = value; + } + } + + internal override string GenerateNotSupportedErrorMessage(VideoMediaFormat format) + => $"'{format.MimeType}' is not supported by FlipTransform."; + + internal override void Configure(TransformHandle handle) + { + // intended blank + } + + private async Task ApplyAsync(TransformHandle handle, MediaPacket source, + ImageRotation rotation) + { + SetRotation(handle, rotation); + return await RunAsync(handle, source); + } + + internal override async Task ApplyAsync(MediaPacket source) + { + using (TransformHandle handle = CreateHandle()) + { + if (Flip.HasFlag(Flips.Vertical | Flips.Horizontal)) + { + var flipped = await ApplyAsync(handle, source, ImageRotation.FlipHorizontal); + try + { + return await ApplyAsync(handle, flipped, ImageRotation.FlipVertical); + } + finally + { + flipped.Dispose(); + } + } + + return await ApplyAsync(handle, source, Flip.HasFlag(Flips.Horizontal) ? + ImageRotation.FlipHorizontal : ImageRotation.FlipVertical); + } + } + } +} diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ImageColorSpace.cs b/src/Tizen.Multimedia.Util/ImageUtil/ImageColorSpace.cs index 8c3fd1b..2939805 100644 --- a/src/Tizen.Multimedia.Util/ImageUtil/ImageColorSpace.cs +++ b/src/Tizen.Multimedia.Util/ImageUtil/ImageColorSpace.cs @@ -97,7 +97,7 @@ namespace Tizen.Multimedia.Util case ImageColorSpace.Yuyv: return ColorSpace.Yuyv; - case ImageColorSpace.Yuv422: return ColorSpace.Yuv422; + case ImageColorSpace.Yuv422: return ColorSpace.Yuv422P; case ImageColorSpace.I420: return ColorSpace.I420; @@ -141,7 +141,8 @@ namespace Tizen.Multimedia.Util case ColorSpace.Yuyv: return ImageColorSpace.Yuyv; - case ColorSpace.Yuv422: return ImageColorSpace.Yuv422; + case ColorSpace.Yuv422: + case ColorSpace.Yuv422P: return ImageColorSpace.Yuv422; case ColorSpace.I420: return ImageColorSpace.I420; diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ImageTransform.cs b/src/Tizen.Multimedia.Util/ImageUtil/ImageTransform.cs index 65cac7b..5923882 100644 --- a/src/Tizen.Multimedia.Util/ImageUtil/ImageTransform.cs +++ b/src/Tizen.Multimedia.Util/ImageUtil/ImageTransform.cs @@ -15,8 +15,6 @@ */ using System; -using System.Collections; -using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading.Tasks; @@ -37,9 +35,30 @@ namespace Tizen.Multimedia.Util internal async Task RunAsync(TransformHandle handle, MediaPacket source) { + Debug.Assert(source.Format is VideoMediaFormat); + ValidateFormat(source.Format as VideoMediaFormat); + var tcs = new TaskCompletionSource(); - TransformCompletedCallback cb = (nativehandle, errorCode, _) => + using (var cbKeeper = ObjectKeeper.Get(GetCallback(tcs, source))) + { + var result = Run(handle, source.GetHandle(), cbKeeper.Target); + + if (result == ImageUtilError.NotSupportedFormat) + { + throw new NotSupportedException( + GenerateNotSupportedErrorMessage(source.Format as VideoMediaFormat)); + } + result.ThrowIfFailed("Failed to transform given packet with " + GetType()); + + return await tcs.Task; + } + } + + private TransformCompletedCallback GetCallback(TaskCompletionSource tcs, + MediaPacket source) + { + return (nativehandle, errorCode, _) => { if (errorCode == ImageUtilError.None) { @@ -52,18 +71,16 @@ namespace Tizen.Multimedia.Util tcs.TrySetException(e); } } + else if (errorCode == ImageUtilError.NotSupportedFormat) + { + tcs.TrySetException(new NotSupportedException( + GenerateNotSupportedErrorMessage(source.Format as VideoMediaFormat))); + } else { tcs.TrySetException(errorCode.ToException("Image transformation failed")); } }; - - using (var cbKeeper = ObjectKeeper.Get(cb)) - { - Run(handle, source.GetHandle(), cb).ThrowIfFailed("Failed to transform given packet with " + GetType()); - - return await tcs.Task; - } } internal static TransformHandle CreateHandle() @@ -73,557 +90,22 @@ namespace Tizen.Multimedia.Util return handle; } - internal abstract void Configure(TransformHandle handle); - - internal virtual async Task ApplyAsync(MediaPacket source) - { - using (TransformHandle handle = CreateHandle()) - { - Configure(handle); - - return await RunAsync(handle, source); - } - } - } - - /// - /// Represents a collection of objects that can be individually accessed by index. - /// - /// 4 - public class ImageTransformCollection : IEnumerable, IList - { - private List _list = new List(); - - /// - /// Initializes a new instance of the ImageTransformCollection class. - /// - /// 4 - public ImageTransformCollection() - { - } - - /// - /// Gets or sets the at the specified index. - /// - /// The zero-based index of the to get or set. - /// The at the specified index. - /// - /// index is less than 0.
- /// -or-
- /// index is equal to or greater than . - ///
- /// 4 - public ImageTransform this[int index] - { - get { return _list[index]; } - set { _list[index] = value; } - } - - /// - /// Gets the number of items contained in the TransformCollection. - /// - /// 4 - public int Count => _list.Count; - - bool ICollection.IsReadOnly => false; - - /// - /// Adds a to the end of the collection. - /// - /// The to add. - /// - /// accepts null as a valid value for reference types and allows duplicate elements. - /// - /// 4 - public void Add(ImageTransform item) - { - if (item == null) - { - throw new ArgumentNullException(nameof(item)); - } - _list.Add(item); - } - - /// - /// Removes all items. - /// - /// 4 - public void Clear() => _list.Clear(); - - /// - /// Determines whether the contains the specified item. - /// - /// The to locate in the collection. - /// true if the is found in the collection; otherwise, false. - /// 4 - public bool Contains(ImageTransform item) => _list.Contains(item); - - /// - /// Copies the items of the collection to an array, starting at the specified array index. - /// - /// The one-dimensional array that is the destination of the items copied from the collection. - /// The zero-based index in array at which copying begins. - /// is null. - /// is less than 0. - /// - /// The number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array. - /// - /// 4 - public void CopyTo(ImageTransform[] array, int arrayIndex) => _list.CopyTo(array, arrayIndex); - - /// - /// Determines the index of the specified item in the collection. - /// - /// The to locate in the collection. - /// The index of value if found in the ; otherwise, -1. - /// 4 - public int IndexOf(ImageTransform item) => _list.IndexOf(item); - - /// - /// Inserts a into the collection at the specified index. - /// - /// The zero-based index at which should be inserted. - /// The to insert into the collection. - /// is null. - /// - /// index is less than 0.
- /// -or-
- /// index is greater than . - ///
- /// 4 - public void Insert(int index, ImageTransform item) - { - if (item == null) - { - throw new ArgumentNullException(nameof(item)); - } - _list.Insert(index, item); - } - - /// - /// Removes the first occurrence of the specified from the collection. - /// - /// The to remove. - /// true if was removed from the collection; otherwise, false. - /// 4 - public bool Remove(ImageTransform item) => _list.Remove(item); - - /// - /// Removes the at the specified index. - /// - /// The zero-based index to remove. - /// - /// index is less than 0.
- /// -or-
- /// index is equal to or greater than . - ///
- /// 4 - public void RemoveAt(int index) => _list.RemoveAt(index); - - /// - /// Returns an enumerator that can iterate through the collection. - /// - /// An enumerator that can be used to iterate through the collection. - /// 4 - public IEnumerator GetEnumerator() => _list.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator(); - } - - // TODO need to improve performance - /// - /// Represents a that is a composite of the transforms. - /// - /// 4 - public class ImageTransformGroup : ImageTransform - { - /// - /// Gets or sets the . - /// - /// 4 - public ImageTransformCollection Children { get; set; } - - /// - /// Initializes a new instance of the ImageTransformGroup class. - /// - /// 4 - public ImageTransformGroup() - { - Children = new ImageTransformCollection(); - } - - internal override void Configure(TransformHandle handle) - { - // intended blank - } - - internal override async Task ApplyAsync(MediaPacket source) - { - if (Children.Count == 0) - { - return source; - } - - var items = Children; - - MediaPacket curPacket = await items[0].ApplyAsync(source); - - for (int i = 1; i < items.Count; ++i) - { - var oldPacket = curPacket; - try - { - curPacket = await items[i].ApplyAsync(curPacket); - } - finally - { - oldPacket.Dispose(); - } - } - - return curPacket; - } - } - - /// - /// Rotates an image. - /// - /// - /// 4 - public class RotateTransform : ImageTransform - { - private Rotation _rotation; - - /// - /// Initializes a new instance of the class. - /// - /// The value how to rotate an image. - /// is invalid. - /// is . - /// 4 - public RotateTransform(Rotation rotation) - { - Rotation = rotation; - - } - - /// - /// Gets or sets the value how to rotate an image. - /// - /// is invalid. - /// is . - /// 4 - public Rotation Rotation - { - get { return _rotation; } - set - { - ValidationUtil.ValidateEnum(typeof(Rotation), value, nameof(Rotation)); - - if (value == Rotation.Rotate0) - { - throw new ArgumentOutOfRangeException(nameof(value), "Rotation can't be Rotate0."); - } - - _rotation = value; - } - } - - internal override void Configure(TransformHandle handle) - { - SetRotation(handle, GetImageRotation()); - } - - private ImageRotation GetImageRotation() - { - switch (Rotation) - { - case Rotation.Rotate90: return ImageRotation.Rotate90; - case Rotation.Rotate180: return ImageRotation.Rotate180; - case Rotation.Rotate270: return ImageRotation.Rotate270; - } - - Debug.Fail("Rotation is invalid value!"); - return ImageRotation.Rotate0; - } - } - + internal abstract string GenerateNotSupportedErrorMessage(VideoMediaFormat format); - /// - /// Flips an image. - /// - /// - /// 4 - public class FlipTransform : ImageTransform - { - private Flips _flip; - - /// - /// Initializes a new instance of the class. - /// - /// The value how to flip an image. - /// is invalid. - /// is . - /// 4 - public FlipTransform(Flips flip) - { - Flip = flip; - } - - /// - /// Gets or sets the value how to flip an image. - /// - /// is invalid. - /// is . - /// 4 - public Flips Flip - { - get { return _flip; } - set - { - ValidationUtil.ValidateFlagsEnum(value, Flips.Horizontal | Flips.Vertical, nameof(Flips)); - - if (value == Flips.None) - { - throw new ArgumentOutOfRangeException(nameof(value), "Flip can't be None."); - } - - _flip = value; - } - } - - internal override void Configure(TransformHandle handle) - { - // intended blank - } + internal abstract void Configure(TransformHandle handle); - private async Task ApplyAsync(TransformHandle handle, MediaPacket source, - ImageRotation rotation) + internal virtual void ValidateFormat(VideoMediaFormat format) { - SetRotation(handle, rotation); - return await RunAsync(handle, source); } - internal override async Task ApplyAsync(MediaPacket source) + internal virtual async Task ApplyAsync(MediaPacket source) { using (TransformHandle handle = CreateHandle()) { - if (Flip.HasFlag(Flips.Vertical | Flips.Horizontal)) - { - var flipped = await ApplyAsync(handle, source, ImageRotation.FlipHorizontal); - try - { - return await ApplyAsync(handle, flipped, ImageRotation.FlipVertical); - } - finally - { - flipped.Dispose(); - } - } - - return await ApplyAsync(handle, source, Flip.HasFlag(Flips.Horizontal) ? - ImageRotation.FlipHorizontal : ImageRotation.FlipVertical); - } - } - } - - /// - /// Changes the colorspace of an image. - /// - /// - /// 4 - public class ColorSpaceTransform : ImageTransform - { - private ImageColorSpace _imageColorSpace; - - /// - /// Initializes a new instance of the class. - /// - /// The colorspace of output image. - /// is invalid. - /// is not supported. - /// - /// 4 - public ColorSpaceTransform(ColorSpace colorSpace) - { - ColorSpace = colorSpace; - } - - /// - /// Gets or sets the colorspace of the result image. - /// - /// is invalid. - /// is not supported. - /// - /// 4 - public ColorSpace ColorSpace - { - get { return _imageColorSpace.ToCommonColorSpace(); } - set - { - ValidationUtil.ValidateEnum(typeof(ColorSpace), value, nameof(ColorSpace)); - - _imageColorSpace = value.ToImageColorSpace(); - } - } - - internal override void Configure(TransformHandle handle) - { - SetColorspace(handle, _imageColorSpace); - } - - /// - /// Gets the supported colorspaces for . - /// - /// 4 - public static IEnumerable SupportedColorSpaces - { - get - { - foreach (ImageColorSpace value in Enum.GetValues(typeof(ImageColorSpace))) - { - yield return value.ToCommonColorSpace(); - } - } - } - } - - /// - /// Crops an image. - /// - /// 4 - public class CropTransform : ImageTransform - { - private Rectangle _region; - - /// - /// Initializes a new instance of the class. - /// - /// The crop region. - /// - /// The X-position of is less than zero.
- /// -or-
- /// The Y-position of is less than zero.
- /// -or-
- /// The width of is less than or equal to zero.
- /// -or-
- /// The height of is less than or equal to zero. - ///
- /// 4 - public CropTransform(Rectangle region) - { - Region = region; - } - - /// - /// Gets or sets the crop region. - /// - /// - /// The X-position of is less than zero.
- /// -or-
- /// The Y-position of is less than zero.
- /// -or-
- /// The width of is less than or equal to zero.
- /// -or-
- /// The height of is less than or equal to zero. - ///
- /// 4 - public Rectangle Region - { - get { return _region; } - set - { - - if (value.X < 0) - { - throw new ArgumentOutOfRangeException(nameof(Region), value, - "X position of the region can't be less than zero."); - } - - if (value.Y < 0) - { - throw new ArgumentOutOfRangeException(nameof(Region), value, - "Y position of the region can't be less than zero."); - } - - if (value.Width <= 0) - { - throw new ArgumentOutOfRangeException(nameof(Region), value, - "Width of the region can't be less than or equal zero."); - } - - if (value.Height < 0) - { - throw new ArgumentOutOfRangeException(nameof(Region), value, - "Height of the region can't be less than or equal to zero."); - } - - _region = value; - } - } - - internal override void Configure(TransformHandle handle) - { - SetCropArea(handle, Region.Left, Region.Top, Region.Right, Region.Bottom); - } - } - - /// - /// Resizes an image. - /// - /// 4 - public class ResizeTransform : ImageTransform - { - private Size _size; - - /// - /// Initializes a new instance of the class. - /// - /// The size that an image is resized to. - /// - /// The width of is less than or equal to zero.
- /// -or-
- /// The height of is less than or equal to zero. - ///
- /// 4 - public ResizeTransform(Size size) - { - Size = size; - } - - /// - /// Gets or sets the size that an image is resized to. - /// - /// - /// The width of is less than or equal to zero.
- /// -or-
- /// The height of is less than or equal to zero. - ///
- /// 4 - public Size Size - { - get { return _size; } - set - { - if (value.Width <= 0) - { - throw new ArgumentOutOfRangeException(nameof(Size), value, - "Width of the size can't be less than or equal to zero."); - } - - if (value.Height <= 0) - { - throw new ArgumentOutOfRangeException(nameof(Size), value, - "Height of the size can't be less than or equal to zero."); - } + Configure(handle); - _size = value; + return await RunAsync(handle, source); } } - - internal override void Configure(TransformHandle handle) - { - SetResolution(handle, (uint)Size.Width, (uint)Size.Height); - } } } diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformCollection.cs b/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformCollection.cs new file mode 100644 index 0000000..9406044 --- /dev/null +++ b/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformCollection.cs @@ -0,0 +1,166 @@ +/* + * 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; +using System.Collections.Generic; + +namespace Tizen.Multimedia.Util +{ + /// + /// Represents a collection of objects that can be individually accessed by index. + /// + /// 4 + public class ImageTransformCollection : IEnumerable, IList + { + private List _list = new List(); + + /// + /// Initializes a new instance of the ImageTransformCollection class. + /// + /// 4 + public ImageTransformCollection() + { + } + + /// + /// Gets or sets the at the specified index. + /// + /// The zero-based index of the to get or set. + /// The at the specified index. + /// + /// index is less than 0.
+ /// -or-
+ /// index is equal to or greater than . + ///
+ /// 4 + public ImageTransform this[int index] + { + get { return _list[index]; } + set { _list[index] = value; } + } + + /// + /// Gets the number of items contained in the TransformCollection. + /// + /// 4 + public int Count => _list.Count; + + bool ICollection.IsReadOnly => false; + + /// + /// Adds a to the end of the collection. + /// + /// The to add. + /// + /// accepts null as a valid value for reference types and allows duplicate elements. + /// + /// 4 + public void Add(ImageTransform item) + { + if (item == null) + { + throw new ArgumentNullException(nameof(item)); + } + _list.Add(item); + } + + /// + /// Removes all items. + /// + /// 4 + public void Clear() => _list.Clear(); + + /// + /// Determines whether the contains the specified item. + /// + /// The to locate in the collection. + /// true if the is found in the collection; otherwise, false. + /// 4 + public bool Contains(ImageTransform item) => _list.Contains(item); + + /// + /// Copies the items of the collection to an array, starting at the specified array index. + /// + /// The one-dimensional array that is the destination of the items copied from the collection. + /// The zero-based index in array at which copying begins. + /// is null. + /// is less than 0. + /// + /// The number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array. + /// + /// 4 + public void CopyTo(ImageTransform[] array, int arrayIndex) => _list.CopyTo(array, arrayIndex); + + /// + /// Determines the index of the specified item in the collection. + /// + /// The to locate in the collection. + /// The index of value if found in the ; otherwise, -1. + /// 4 + public int IndexOf(ImageTransform item) => _list.IndexOf(item); + + /// + /// Inserts a into the collection at the specified index. + /// + /// The zero-based index at which should be inserted. + /// The to insert into the collection. + /// is null. + /// + /// index is less than 0.
+ /// -or-
+ /// index is greater than . + ///
+ /// 4 + public void Insert(int index, ImageTransform item) + { + if (item == null) + { + throw new ArgumentNullException(nameof(item)); + } + _list.Insert(index, item); + } + + /// + /// Removes the first occurrence of the specified from the collection. + /// + /// The to remove. + /// true if was removed from the collection; otherwise, false. + /// 4 + public bool Remove(ImageTransform item) => _list.Remove(item); + + /// + /// Removes the at the specified index. + /// + /// The zero-based index to remove. + /// + /// index is less than 0.
+ /// -or-
+ /// index is equal to or greater than . + ///
+ /// 4 + public void RemoveAt(int index) => _list.RemoveAt(index); + + /// + /// Returns an enumerator that can iterate through the collection. + /// + /// An enumerator that can be used to iterate through the collection. + /// 4 + public IEnumerator GetEnumerator() => _list.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => _list.GetEnumerator(); + } +} diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformGroup.cs b/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformGroup.cs new file mode 100644 index 0000000..e445d09 --- /dev/null +++ b/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformGroup.cs @@ -0,0 +1,81 @@ +/* + * 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.Threading.Tasks; +using static Interop.ImageUtil; + +namespace Tizen.Multimedia.Util +{ + // TODO need to improve performance + /// + /// Represents a that is a composite of the transforms. + /// + /// 4 + public class ImageTransformGroup : ImageTransform + { + /// + /// Gets or sets the . + /// + /// 4 + public ImageTransformCollection Children { get; set; } + + /// + /// Initializes a new instance of the ImageTransformGroup class. + /// + /// 4 + public ImageTransformGroup() + { + Children = new ImageTransformCollection(); + } + + internal override void Configure(TransformHandle handle) + { + // intended blank + } + + internal override async Task ApplyAsync(MediaPacket source) + { + if (Children.Count == 0) + { + return source; + } + + var items = Children; + + MediaPacket curPacket = await items[0].ApplyAsync(source); + + for (int i = 1; i < items.Count; ++i) + { + var oldPacket = curPacket; + try + { + curPacket = await items[i].ApplyAsync(curPacket); + } + finally + { + oldPacket.Dispose(); + } + } + + return curPacket; + } + + internal override string GenerateNotSupportedErrorMessage(VideoMediaFormat format) + { + return null; + } + } +} diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformer.cs b/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformer.cs index aa22ae7..dda3600 100644 --- a/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformer.cs +++ b/src/Tizen.Multimedia.Util/ImageUtil/ImageTransformer.cs @@ -44,8 +44,10 @@ namespace Tizen.Multimedia.Util /// -or-
/// is null. /// + /// is not video format. /// The has already been disposed of. /// Failed to apply . + /// Specified transformation is not supported. /// 4 public Task TransformAsync(MediaPacket source, ImageTransform item) { @@ -64,6 +66,11 @@ namespace Tizen.Multimedia.Util throw new ArgumentNullException(nameof(item)); } + if (source.Format is VideoMediaFormat == false) + { + throw new ArgumentException("source is not video format.", nameof(source)); + } + return item.ApplyAsync(source); } diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ImageUtil.cs b/src/Tizen.Multimedia.Util/ImageUtil/ImageUtil.cs index 1c30321..ecb1ce3 100644 --- a/src/Tizen.Multimedia.Util/ImageUtil/ImageUtil.cs +++ b/src/Tizen.Multimedia.Util/ImageUtil/ImageUtil.cs @@ -122,9 +122,7 @@ namespace Tizen.Multimedia.Util "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) + ExtractColorFromMemory(buffer, size.Width, size.Height, out var r, out var g, out var b) .ThrowIfFailed("Failed to extract color from buffer"); return Color.FromRgb(r, g, b); diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ResizeTransform.cs b/src/Tizen.Multimedia.Util/ImageUtil/ResizeTransform.cs new file mode 100644 index 0000000..8489134 --- /dev/null +++ b/src/Tizen.Multimedia.Util/ImageUtil/ResizeTransform.cs @@ -0,0 +1,84 @@ +/* + * 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 static Interop.ImageUtil; +using static Interop.ImageUtil.Transform; + +namespace Tizen.Multimedia.Util +{ + /// + /// Resizes an image. + /// + /// 4 + public class ResizeTransform : ImageTransform + { + private Size _size; + + /// + /// Initializes a new instance of the class. + /// + /// The size that an image is resized to. + /// + /// The width of is less than or equal to zero.
+ /// -or-
+ /// The height of is less than or equal to zero. + ///
+ /// 4 + public ResizeTransform(Size size) + { + Size = size; + } + + internal override string GenerateNotSupportedErrorMessage(VideoMediaFormat format) + => $"'{format.MimeType}' is not supported by ResizeTransform."; + + /// + /// Gets or sets the size that an image is resized to. + /// + /// + /// The width of is less than or equal to zero.
+ /// -or-
+ /// The height of is less than or equal to zero. + ///
+ /// 4 + public Size Size + { + get { return _size; } + set + { + if (value.Width <= 0) + { + throw new ArgumentOutOfRangeException(nameof(Size), value, + "Width of the size can't be less than or equal to zero."); + } + + if (value.Height <= 0) + { + throw new ArgumentOutOfRangeException(nameof(Size), value, + "Height of the size can't be less than or equal to zero."); + } + + _size = value; + } + } + + internal override void Configure(TransformHandle handle) + { + SetResolution(handle, (uint)Size.Width, (uint)Size.Height); + } + } +} diff --git a/src/Tizen.Multimedia.Util/ImageUtil/RotateTransform.cs b/src/Tizen.Multimedia.Util/ImageUtil/RotateTransform.cs new file mode 100644 index 0000000..0470be0 --- /dev/null +++ b/src/Tizen.Multimedia.Util/ImageUtil/RotateTransform.cs @@ -0,0 +1,88 @@ +/* + * 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.Diagnostics; +using static Interop.ImageUtil; +using static Interop.ImageUtil.Transform; + +namespace Tizen.Multimedia.Util +{ + /// + /// Rotates an image. + /// + /// + /// 4 + public class RotateTransform : ImageTransform + { + private Rotation _rotation; + + /// + /// Initializes a new instance of the class. + /// + /// The value how to rotate an image. + /// is invalid. + /// is . + /// 4 + public RotateTransform(Rotation rotation) + { + Rotation = rotation; + } + + /// + /// Gets or sets the value how to rotate an image. + /// + /// is invalid. + /// is . + /// 4 + public Rotation Rotation + { + get { return _rotation; } + set + { + ValidationUtil.ValidateEnum(typeof(Rotation), value, nameof(Rotation)); + + if (value == Rotation.Rotate0) + { + throw new ArgumentOutOfRangeException(nameof(value), "Rotation can't be Rotate0."); + } + + _rotation = value; + } + } + + internal override string GenerateNotSupportedErrorMessage(VideoMediaFormat format) + => $"'{format.MimeType}' is not supported by RotateTransform."; + + internal override void Configure(TransformHandle handle) + { + SetRotation(handle, GetImageRotation()); + } + + private ImageRotation GetImageRotation() + { + switch (Rotation) + { + case Rotation.Rotate90: return ImageRotation.Rotate90; + case Rotation.Rotate180: return ImageRotation.Rotate180; + case Rotation.Rotate270: return ImageRotation.Rotate270; + } + + Debug.Fail("Rotation is invalid value!"); + return ImageRotation.Rotate0; + } + } +} -- 2.7.4