f497f7d1ee6d07c1b15fb3ebae9994c7ed042092
[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 the preview image data.
26     /// </summary>
27     /// <since_tizen> 4 </since_tizen>
28     public class PreviewFrame
29     {
30         internal PreviewFrame(IntPtr ptr)
31         {
32             var unmanagedStruct = Marshal.PtrToStructure<CameraPreviewDataStruct>(ptr);
33
34             Format = unmanagedStruct.Format;
35             Resolution = new Size(unmanagedStruct.Width, unmanagedStruct.Height);
36             TimeStamp = unmanagedStruct.TimeStamp;
37             PlaneType = GetPlaneType(unmanagedStruct);
38             Plane = ConvertPlane(unmanagedStruct);
39         }
40
41         private IPreviewPlane ConvertPlane(CameraPreviewDataStruct unmanagedStruct)
42         {
43             if (unmanagedStruct.NumOfPlanes == 1)
44             {
45                 if (unmanagedStruct.Format == CameraPixelFormat.H264 || unmanagedStruct.Format == CameraPixelFormat.Jpeg)
46                 {
47                     return new EncodedPlane(unmanagedStruct.Plane.EncodedPlane);
48                 }
49                 else if (unmanagedStruct.Format == CameraPixelFormat.Invz)
50                 {
51                     return new DepthPlane(unmanagedStruct.Plane.DepthPlane);
52                 }
53                 else if (unmanagedStruct.Format == CameraPixelFormat.Rgba || unmanagedStruct.Format == CameraPixelFormat.Argb)
54                 {
55                     return new RgbPlane(unmanagedStruct.Plane.RgbPlane);
56                 }
57                 else
58                 {
59                     return new SinglePlane(unmanagedStruct.Plane.SinglePlane);
60                 }
61             }
62             else if (unmanagedStruct.NumOfPlanes == 2)
63             {
64                 var size = Resolution.Width * Resolution.Height;
65                 unmanagedStruct.Plane.DoublePlane.YLength = (uint)size;
66                 unmanagedStruct.Plane.DoublePlane.UVLength = (uint)size / 2;
67                 return new DoublePlane(unmanagedStruct.Plane.DoublePlane);
68             }
69             else if (unmanagedStruct.NumOfPlanes == 3)
70             {
71                 return new TriplePlane(unmanagedStruct.Plane.TriplePlane);
72             }
73
74             Debug.Fail("Unknown preview data!");
75             return null;
76         }
77
78         private static PlaneType GetPlaneType(CameraPreviewDataStruct unmanagedStruct)
79         {
80             if (unmanagedStruct.NumOfPlanes == 1)
81             {
82                 if (unmanagedStruct.Format == CameraPixelFormat.H264 || unmanagedStruct.Format == CameraPixelFormat.Jpeg)
83                 {
84                     return PlaneType.EncodedPlane;
85                 }
86                 else if (unmanagedStruct.Format == CameraPixelFormat.Invz)
87                 {
88                     return PlaneType.DepthPlane;
89                 }
90                 else if (unmanagedStruct.Format == CameraPixelFormat.Rgba || unmanagedStruct.Format == CameraPixelFormat.Argb)
91                 {
92                     return PlaneType.RgbPlane;
93                 }
94                 else
95                 {
96                     return PlaneType.SinglePlane;
97                 }
98             }
99             else if (unmanagedStruct.NumOfPlanes == 2)
100             {
101                 return PlaneType.DoublePlane;
102             }
103
104             return PlaneType.TriplePlane;
105         }
106
107         /// <summary>
108         /// The pixel format of the image.
109         /// </summary>
110         /// <since_tizen> 4 </since_tizen>
111         public CameraPixelFormat Format { get; }
112
113         /// <summary>
114         /// The resolution of the preview image.
115         /// </summary>
116         /// <since_tizen> 4 </since_tizen>
117         public Size Resolution { get; }
118
119         /// <summary>
120         /// The time stamp of the preview frame.
121         /// </summary>
122         /// <since_tizen> 4 </since_tizen>
123         public uint TimeStamp { get; }
124
125         /// <summary>
126         /// The type of the preview plane. <see cref="Tizen.Multimedia.PlaneType"/>
127         /// </summary>
128         /// <since_tizen> 4 </since_tizen>
129         public PlaneType PlaneType { get; }
130
131         /// <summary>
132         /// The buffer including the preview frame.
133         /// </summary>
134         /// <since_tizen> 4 </since_tizen>
135         public IPreviewPlane Plane { get; }
136     }
137 }