[ImageUtil] Replace deprecated decode pinvoke func. (#929)
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Util / ImageUtil / CropTransform.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 static Interop;
19 using NativeTransform = Interop.ImageUtil.Transform;
20
21 namespace Tizen.Multimedia.Util
22 {
23     /// <summary>
24     /// Crops an image.
25     /// </summary>
26     /// <since_tizen> 4 </since_tizen>
27     public class CropTransform : ImageTransform
28     {
29         private Rectangle _region;
30
31         /// <summary>
32         /// Initializes a new instance of the <see cref="CropTransform"/> class.
33         /// </summary>
34         /// <param name="region">The crop region.</param>
35         /// <exception cref="ArgumentOutOfRangeException">
36         ///     The X-position of <paramref name="region"/> is less than zero.<br/>
37         ///     -or-<br/>
38         ///     The Y-position of <paramref name="region"/> is less than zero.<br/>
39         ///     -or-<br/>
40         ///     The width of <paramref name="region"/> is less than or equal to zero.<br/>
41         ///     -or-<br/>
42         ///     The height of <paramref name="region"/> is less than or equal to zero.
43         /// </exception>
44         /// <since_tizen> 4 </since_tizen>
45         public CropTransform(Rectangle region)
46         {
47             Region = region;
48         }
49
50         /// <summary>
51         /// Gets or sets the crop region.
52         /// </summary>
53         /// <exception cref="ArgumentOutOfRangeException">
54         ///     The X-position of <paramref name="value"/> is less than zero.<br/>
55         ///     -or-<br/>
56         ///     The Y-position of <paramref name="value"/> is less than zero.<br/>
57         ///     -or-<br/>
58         ///     The width of <paramref name="value"/> is less than or equal to zero.<br/>
59         ///     -or-<br/>
60         ///     The height of <paramref name="value"/> is less than or equal to zero.
61         /// </exception>
62         /// <since_tizen> 4 </since_tizen>
63         public Rectangle Region
64         {
65             get { return _region; }
66             set
67             {
68
69                 if (value.X < 0)
70                 {
71                     throw new ArgumentOutOfRangeException(nameof(Region), value,
72                         "X position of the region can't be less than zero.");
73                 }
74
75                 if (value.Y < 0)
76                 {
77                     throw new ArgumentOutOfRangeException(nameof(Region), value,
78                         "Y position of the region can't be less than zero.");
79                 }
80
81                 if (value.Width <= 0)
82                 {
83                     throw new ArgumentOutOfRangeException(nameof(Region), value,
84                         "Width of the region can't be less than or equal zero.");
85                 }
86
87                 if (value.Height < 0)
88                 {
89                     throw new ArgumentOutOfRangeException(nameof(Region), value,
90                         "Height of the region can't be less than or equal to zero.");
91                 }
92
93                 _region = value;
94             }
95         }
96
97         internal override void Configure(TransformHandle handle)
98         {
99             NativeTransform.SetCropArea(handle, Region.Left, Region.Top, Region.Right, Region.Bottom);
100         }
101
102         internal override void ValidateFormat(VideoMediaFormat format)
103         {
104             if (format.Size.Width < Region.Right || format.Size.Height < Region.Bottom)
105             {
106                 throw new InvalidOperationException(
107                     $"Crop region is invalid; Source size({format.Size.ToString()}), Crop region({Region.ToString()}).");
108             }
109         }
110
111         internal override string GenerateNotSupportedErrorMessage(VideoMediaFormat format)
112         {
113             return $"'{format.MimeType}' is not supported by CropTransform.";
114         }
115     }
116 }