[Multimedia.Util] Modified to throw NotSupportedException for NotSupported error...
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Util / ImageUtil / FlipTransform.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Threading.Tasks;
19 using static Interop.ImageUtil;
20 using static Interop.ImageUtil.Transform;
21
22 namespace Tizen.Multimedia.Util
23 {
24     /// <summary>
25     /// Flips an image.
26     /// </summary>
27     /// <seealso cref="Rotation"/>
28     /// <since_tizen> 4 </since_tizen>
29     public class FlipTransform : ImageTransform
30     {
31         private Flips _flip;
32
33         /// <summary>
34         /// Initializes a new instance of the <see cref="FlipTransform"/> class.
35         /// </summary>
36         /// <param name="flip">The value how to flip an image.</param>
37         /// <exception cref="ArgumentException"><paramref name="flip"/> is invalid.</exception>
38         /// <exception cref="ArgumentOutOfRangeException"><paramref name="flip"/> is <see cref="Flips.None"/>.</exception>
39         /// <since_tizen> 4 </since_tizen>
40         public FlipTransform(Flips flip)
41         {
42             Flip = flip;
43         }
44
45         /// <summary>
46         /// Gets or sets the value how to flip an image.
47         /// </summary>
48         /// <exception cref="ArgumentException"><paramref name="value"/> is invalid.</exception>
49         /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> is <see cref="Flips.None"/>.</exception>
50         /// <since_tizen> 4 </since_tizen>
51         public Flips Flip
52         {
53             get { return _flip; }
54             set
55             {
56                 ValidationUtil.ValidateFlagsEnum(value, Flips.Horizontal | Flips.Vertical, nameof(Flips));
57
58                 if (value == Flips.None)
59                 {
60                     throw new ArgumentOutOfRangeException(nameof(value), "Flip can't be None.");
61                 }
62
63                 _flip = value;
64             }
65         }
66
67         internal override string GenerateNotSupportedErrorMessage(VideoMediaFormat format)
68             => $"'{format.MimeType}' is not supported by FlipTransform.";
69
70         internal override void Configure(TransformHandle handle)
71         {
72             // intended blank
73         }
74
75         private async Task<MediaPacket> ApplyAsync(TransformHandle handle, MediaPacket source,
76             ImageRotation rotation)
77         {
78             SetRotation(handle, rotation);
79             return await RunAsync(handle, source);
80         }
81
82         internal override async Task<MediaPacket> ApplyAsync(MediaPacket source)
83         {
84             using (TransformHandle handle = CreateHandle())
85             {
86                 if (Flip.HasFlag(Flips.Vertical | Flips.Horizontal))
87                 {
88                     var flipped = await ApplyAsync(handle, source, ImageRotation.FlipHorizontal);
89                     try
90                     {
91                         return await ApplyAsync(handle, flipped, ImageRotation.FlipVertical);
92                     }
93                     finally
94                     {
95                         flipped.Dispose();
96                     }
97                 }
98
99                 return await ApplyAsync(handle, source, Flip.HasFlag(Flips.Horizontal) ?
100                     ImageRotation.FlipHorizontal : ImageRotation.FlipVertical);
101             }
102         }
103     }
104 }