[babl] extensions/gggl{,-lies}.c: fix conversions u16 <-> u8



commit 07cc0809228236c7fdcab6b61c4d515f9b93dc84
Author: Massimo Valentini <mvalentini src gnome org>
Date:   Sat Jul 14 17:07:38 2012 +0200

    extensions/gggl{,-lies}.c: fix conversions u16 <-> u8
    
    u16 maximum value is 0x10000 - 1 == (0x100 - 1) (0x100 + 1)
    u8 maximum value is 0x100 - 1
    thus the direct conversion requires multiplication/division per
    0x100+1
    
    division per 0x100+1 is implemented using 1 term of (1 + x)^-1
    MacLaurin expansion
    
    (1+x)^-1 == 1 + (-1) x
    
    a * (1 + 1/256)^-1 * (256^-1) == (a - a / 256) / 256
    
    with integer division rounded toward the nearest.

 extensions/gggl-lies.c |    5 +++--
 extensions/gggl.c      |    5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)
---
diff --git a/extensions/gggl-lies.c b/extensions/gggl-lies.c
index 450fd75..7f99b00 100644
--- a/extensions/gggl-lies.c
+++ b/extensions/gggl-lies.c
@@ -342,7 +342,8 @@ conv_16_8 (unsigned char *src, unsigned char *dst, long samples)
 
   while (n--)
     {
-      (*(unsigned char *) dst) = (*(unsigned short *) src) >> 8;
+#define div_257(a) ((((a)+128)-(((a)+128)>>8))>>8)
+      (*(unsigned char *) dst) = div_257 (*(unsigned short *) src);
       dst                     += 1;
       src                     += 2;
     }
@@ -356,7 +357,7 @@ conv_8_16 (unsigned char *src, unsigned char *dst, long samples)
 
   while (n--)
     {
-      (*(unsigned short *) dst) = (*(unsigned char *) src) << 8;
+      (*(unsigned short *) dst) = ((*(unsigned char *) src) << 8) | *src;
       dst                      += 2;
       src                      += 1;
     }
diff --git a/extensions/gggl.c b/extensions/gggl.c
index c530988..307c574 100644
--- a/extensions/gggl.c
+++ b/extensions/gggl.c
@@ -380,7 +380,8 @@ conv_16_8 (unsigned char *src, unsigned char *dst, long samples)
 
   while (n--)
     {
-      (*(unsigned char *) dst) = (*(unsigned short *) src) >> 8;
+#define div_257(a) ((((a)+128)-(((a)+128)>>8))>>8)
+      (*(unsigned char *) dst) = div_257 (*(unsigned short *) src);
       dst                     += 1;
       src                     += 2;
     }
@@ -394,7 +395,7 @@ conv_8_16 (unsigned char *src, unsigned char *dst, long samples)
 
   while (n--)
     {
-      (*(unsigned short *) dst) = (*(unsigned char *) src) << 8;
+      (*(unsigned short *) dst) = ((*(unsigned char *) src) << 8) | *src;
       dst                      += 2;
       src                      += 1;
     }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]