Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia.Camera / Camera / PreviewFrame.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.Diagnostics;
19 using System.Runtime.InteropServices;
20 using static Interop.Camera;
21
22 namespace Tizen.Multimedia
23 {
24     /// <summary>
25     /// The class containing preview image data.
26     /// </summary>
27     public class PreviewFrame
28     {
29         internal PreviewFrame(IntPtr ptr)
30         {
31             var unmanagedStruct = Marshal.PtrToStructure<CameraPreviewDataStruct>(ptr);
32
33             Format = unmanagedStruct.Format;
34             Resolution = new Size(unmanagedStruct.Width, unmanagedStruct.Height);
35             TimeStamp = unmanagedStruct.TimeStamp;
36             PlaneType = GetPlaneType(unmanagedStruct);
37             Plane = ConvertPlane(unmanagedStruct);
38         }
39
40         private static IPreviewPlane ConvertPlane(CameraPreviewDataStruct unmanagedStruct)
41         {
42             if (unmanagedStruct.NumOfPlanes == 1)
43             {
44                 if (unmanagedStruct.Format == CameraPixelFormat.H264 || unmanagedStruct.Format == CameraPixelFormat.Jpeg)
45                 {
46                     return new EncodedPlane(unmanagedStruct.Plane.EncodedPlane);
47                 }
48                 else
49                 {
50                     return new SinglePlane(unmanagedStruct.Plane.SinglePlane);
51                 }
52             }
53             else if (unmanagedStruct.NumOfPlanes == 2)
54             {
55                 return new DoublePlane(unmanagedStruct.Plane.DoublePlane);
56             }
57             else if (unmanagedStruct.NumOfPlanes == 3)
58             {
59                 return new TriplePlane(unmanagedStruct.Plane.TriplePlane);
60             }
61
62             Debug.Fail("Unknown preview data!");
63             return null;
64         }
65
66         private static PlaneType GetPlaneType(CameraPreviewDataStruct unmanagedStruct)
67         {
68             if (unmanagedStruct.NumOfPlanes == 1)
69             {
70                 if (unmanagedStruct.Format == CameraPixelFormat.H264 || unmanagedStruct.Format == CameraPixelFormat.Jpeg)
71                 {
72                     return PlaneType.EncodedPlane;
73                 }
74                 else
75                 {
76                     return PlaneType.SinglePlane;
77                 }
78             }
79             else if (unmanagedStruct.NumOfPlanes == 2)
80             {
81                 return PlaneType.DoublePlane;
82             }
83
84             return PlaneType.TriplePlane;
85         }
86
87         /// <summary>
88         /// The pixel format of the image.
89         /// </summary>
90         /// <since_tizen> 3 </since_tizen>
91         public CameraPixelFormat Format { get; }
92
93         /// <summary>
94         /// The resolution of the preview image.
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         public Size Resolution { get; }
98
99         /// <summary>
100         /// The timestamp of preview frame.
101         /// </summary>
102         /// <since_tizen> 3 </since_tizen>
103         public uint TimeStamp { get; }
104
105         /// <summary>
106         /// The type of preview plane. <see cref="Tizen.Multimedia.PlaneType"/>
107         /// </summary>
108         /// <since_tizen> 3 </since_tizen>
109         public PlaneType PlaneType { get; }
110
111         /// <summary>
112         /// The buffer including preview frame.
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         public IPreviewPlane Plane { get; }
116     }
117 }