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