[Pixman] [PATCH 7/7] Add bswap() macros and use them

Søren Sandmann sandmann at daimi.au.dk
Sat Apr 24 13:48:58 PDT 2010


From: Benjamin Otte <otte at gnome.org>

Code taken from Cairo.
---
 configure.ac            |    1 +
 pixman/pixman-access.c  |   35 +++++++++--------------------------
 pixman/pixman-private.h |   18 ++++++++++++++++++
 3 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/configure.ac b/configure.ac
index aabe721..7aed963 100644
--- a/configure.ac
+++ b/configure.ac
@@ -74,6 +74,7 @@ AC_PROG_CC
 AM_PROG_AS
 AC_PROG_LIBTOOL
 AC_CHECK_FUNCS([getisax])
+AC_CHECK_HEADERS(byteswap.h)
 AC_C_BIGENDIAN
 AC_C_INLINE
 
diff --git a/pixman/pixman-access.c b/pixman/pixman-access.c
index fa0a267..5e203f6 100644
--- a/pixman/pixman-access.c
+++ b/pixman/pixman-access.c
@@ -184,11 +184,8 @@ fetch_scanline_b8g8r8a8 (pixman_image_t *image,
     while (pixel < end)
     {
 	uint32_t p = READ (image, pixel++);
-
-	*buffer++ = (((p & 0xff000000) >> 24)	|
-	             ((p & 0x00ff0000) >> 8)	|
-	             ((p & 0x0000ff00) << 8)	|
-	             ((p & 0x000000ff) << 24));
+	
+	*buffer++ = bswap_32 (p);
     }
 }
 
@@ -209,10 +206,7 @@ fetch_scanline_b8g8r8x8 (pixman_image_t *image,
     {
 	uint32_t p = READ (image, pixel++);
 	
-	*buffer++ = (0xff000000 |
-	             ((p & 0xff000000) >> 24)	|
-	             ((p & 0x00ff0000) >> 8)	|
-	             ((p & 0x0000ff00) << 8));
+	*buffer++ = 0xff000000 | bswap_32 (p);
     }
 }
 
@@ -1285,10 +1279,7 @@ fetch_pixel_b8g8r8a8 (bits_image_t *image,
     uint32_t *bits = image->bits + line * image->rowstride;
     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
     
-    return ((pixel & 0xff000000) >> 24 |
-	    (pixel & 0x00ff0000) >> 8 |
-	    (pixel & 0x0000ff00) << 8 |
-	    (pixel & 0x000000ff) << 24);
+    return bswap_32 (pixel);
 }
 
 static uint32_t
@@ -1299,10 +1290,7 @@ fetch_pixel_b8g8r8x8 (bits_image_t *image,
     uint32_t *bits = image->bits + line * image->rowstride;
     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
     
-    return ((0xff000000) |
-	    (pixel & 0xff000000) >> 24 |
-	    (pixel & 0x00ff0000) >> 8 |
-	    (pixel & 0x0000ff00) << 8);
+    return 0xff000000 | bswap_32 (pixel);
 }
 
 static uint32_t
@@ -1997,11 +1985,8 @@ store_scanline_b8g8r8a8 (bits_image_t *  image,
     
     for (i = 0; i < width; ++i)
     {
-	WRITE (image, pixel++,
-	       ((values[i] >> 24) & 0x000000ff) |
-	       ((values[i] >>  8) & 0x0000ff00) |
-	       ((values[i] <<  8) & 0x00ff0000) |
-	       ((values[i] << 24) & 0xff000000));
+        uint32_t p = bswap_32 (values[i]);
+	WRITE (image, pixel++, p);
     }
 }
 
@@ -2018,10 +2003,8 @@ store_scanline_b8g8r8x8 (bits_image_t *  image,
     
     for (i = 0; i < width; ++i)
     {
-	WRITE (image, pixel++,
-	       ((values[i] >>  8) & 0x0000ff00) |
-	       ((values[i] <<  8) & 0x00ff0000) |
-	       ((values[i] << 24) & 0xff000000));
+        uint32_t p = bswap_32 (values[i]);
+	WRITE (image, pixel++, p);
     }
 }
 
diff --git a/pixman/pixman-private.h b/pixman/pixman-private.h
index d5767af..c4f5064 100644
--- a/pixman/pixman-private.h
+++ b/pixman/pixman-private.h
@@ -686,6 +686,24 @@ pixman_region16_copy_from_region32 (pixman_region16_t *dst,
 #  define MAX(a, b) ((a > b) ? a : b)
 #endif
 
+/* Byte swapping */
+#if HAVE_BYTESWAP_H
+# include <byteswap.h>
+#endif
+#ifndef bswap_16
+# define bswap_16(p) \
+	(((((uint16_t)(p)) & 0x00ff) << 8) | \
+	  (((uint16_t)(p))           >> 8));
+#endif
+#ifndef bswap_32
+# define bswap_32(p) \
+         (((((uint32_t)(p)) & 0x000000ff) << 24) | \
+	  ((((uint32_t)(p)) & 0x0000ff00) << 8)  | \
+	  ((((uint32_t)(p)) & 0x00ff0000) >> 8)  | \
+	  ((((uint32_t)(p)))              >> 24));
+#endif
+
+
 /* Integer division that rounds towards -infinity */
 #define DIV(a, b)					   \
     ((((a) < 0) == ((b) < 0)) ? (a) / (b) :                \
-- 
1.7.0.1



More information about the Pixman mailing list