1 /*
2  * Copyright (C) 2011 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 #pragma once
18 
19 #include <stdint.h>
20 
21 namespace android {
22 
23 // LinearTransform defines a structure which hold the definition of a
24 // transformation from single dimensional coordinate system A into coordinate
25 // system B (and back again).  Values in A and in B are 64 bit, the linear
26 // scale factor is expressed as a rational number using two 32 bit values.
27 //
28 // Specifically, let
29 // f(a) = b
30 // F(b) = f^-1(b) = a
31 // then
32 //
33 // f(a) = (((a - a_zero) * a_to_b_numer) / a_to_b_denom) + b_zero;
34 //
35 // and
36 //
37 // F(b) = (((b - b_zero) * a_to_b_denom) / a_to_b_numer) + a_zero;
38 //
39 struct LinearTransform {
40   int64_t  a_zero;
41   int64_t  b_zero;
42   int32_t  a_to_b_numer;
43   uint32_t a_to_b_denom;
44 
45   // Transform from A->B
46   // Returns true on success, or false in the case of a singularity or an
47   // overflow.
48   bool doForwardTransform(int64_t a_in, int64_t* b_out) const;
49 
50   // Transform from B->A
51   // Returns true on success, or false in the case of a singularity or an
52   // overflow.
53   bool doReverseTransform(int64_t b_in, int64_t* a_out) const;
54 
55   // Helpers which will reduce the fraction N/D using Euclid's method.
56   template <class T> static void reduce(T* N, T* D);
57   static void reduce(int32_t* N, uint32_t* D);
58 };
59 
60 
61 }
62