util: add function to calculate aspect ratio
authorLucas De Marchi <lucas.demarchi@intel.com>
Wed, 21 Aug 2013 20:52:44 +0000 (17:52 -0300)
committerLucas De Marchi <lucas.demarchi@intel.com>
Mon, 26 Aug 2013 22:15:55 +0000 (19:15 -0300)
To calculate the aspect ratio what we need to do is to calculate the gcd
of width and height, dividing by it. We use the simple Euclidean
algorithm. Besides of being simple it's pretty fast.

src/lib/shared/util.c
src/lib/shared/util.h

index 6a27f50..2e1555f 100644 (file)
@@ -42,3 +42,33 @@ struct lms_string_size str_extract_name_from_path(
     return str;
 }
 
+/* Euclidean algorithm
+ * http://en.wikipedia.org/wiki/Euclidean_algorithm */
+static unsigned int gcd(unsigned int a, unsigned int b)
+{
+    unsigned int t;
+
+    while (b) {
+        t = b;
+        b = a % t;
+        a = t;
+    }
+
+    return a;
+}
+
+void reduce_gcd(unsigned int w, unsigned int h, unsigned int *dw,
+                unsigned int *dh)
+{
+    unsigned int f;
+
+    *dw = w;
+    *dh = h;
+
+    if (!w || !h)
+        return;
+
+    f = gcd(w, h);
+    *dw /= f;
+    *dh /= f;
+}
index 98a2569..fe55f06 100644 (file)
@@ -86,3 +86,6 @@ static inline uint16_t get_be16(const void *ptr)
 struct lms_string_size str_extract_name_from_path(
     const char *path, unsigned int pathlen, unsigned int baselen,
     const struct lms_string_size *ext, struct lms_charset_conv *cs_conv);
+
+void reduce_gcd(unsigned int w, unsigned int h, unsigned int *dw,
+                unsigned int *dh);