1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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 #ifndef V4L2_CAMERA_HAL_METADATA_SCALING_CONVERTER_H_
18 #define V4L2_CAMERA_HAL_METADATA_SCALING_CONVERTER_H_
19 
20 #include "common.h"
21 #include "converter_interface.h"
22 
23 namespace v4l2_camera_hal {
24 
25 // An ScalingConverter scales values up or down.
26 template <typename TMetadata, typename TV4L2>
27 class ScalingConverter : public ConverterInterface<TMetadata, TV4L2> {
28  public:
29   ScalingConverter(TMetadata v4l2_to_metadata_numerator,
30                    TMetadata v4l2_to_metadata_denominator);
31 
32   virtual int MetadataToV4L2(TMetadata value, TV4L2* conversion) override;
33   virtual int V4L2ToMetadata(TV4L2 value, TMetadata* conversion) override;
34 
35  private:
36   const TMetadata v4l2_to_metadata_numerator_;
37   const TMetadata v4l2_to_metadata_denominator_;
38 
39   DISALLOW_COPY_AND_ASSIGN(ScalingConverter);
40 };
41 
42 // -----------------------------------------------------------------------------
43 
44 template <typename TMetadata, typename TV4L2>
ScalingConverter(TMetadata v4l2_to_metadata_numerator,TMetadata v4l2_to_metadata_denominator)45 ScalingConverter<TMetadata, TV4L2>::ScalingConverter(
46     TMetadata v4l2_to_metadata_numerator,
47     TMetadata v4l2_to_metadata_denominator)
48     : v4l2_to_metadata_numerator_(v4l2_to_metadata_numerator),
49       v4l2_to_metadata_denominator_(v4l2_to_metadata_denominator) {
50   HAL_LOG_ENTER();
51 }
52 
53 template <typename TMetadata, typename TV4L2>
MetadataToV4L2(TMetadata value,TV4L2 * conversion)54 int ScalingConverter<TMetadata, TV4L2>::MetadataToV4L2(TMetadata value,
55                                                        TV4L2* conversion) {
56   HAL_LOG_ENTER();
57 
58   *conversion = static_cast<TV4L2>(value * v4l2_to_metadata_denominator_ /
59                                    v4l2_to_metadata_numerator_);
60   return 0;
61 }
62 
63 template <typename TMetadata, typename TV4L2>
V4L2ToMetadata(TV4L2 value,TMetadata * conversion)64 int ScalingConverter<TMetadata, TV4L2>::V4L2ToMetadata(TV4L2 value,
65                                                        TMetadata* conversion) {
66   HAL_LOG_ENTER();
67 
68   *conversion = static_cast<TMetadata>(value) * v4l2_to_metadata_numerator_ /
69                 v4l2_to_metadata_denominator_;
70   return 0;
71 }
72 
73 }  // namespace v4l2_camera_hal
74 
75 #endif  // V4L2_CAMERA_HAL_METADATA_SCALING_CONVERTER_H_
76