[gegl] Add gegl_memset_pattern



commit bf597a185af2be4afd62bc0a5f6e12856cb090eb
Author: Daniel Sabo <DanielSabo gmail com>
Date:   Mon Oct 14 14:16:12 2013 -0700

    Add gegl_memset_pattern
    
    gegl_memset_pattern is a multi byte version of memset, which is
    useful for filling many pixels with the same color. It is faster
    than memcpy when the pixel size is unknown, if the pixel size is
    fixed use memcpy so the compiler can fully inline the copy.

 gegl/gegl-utils.c |   43 +++++++++++++++++++++++++++++++++++++++++++
 gegl/gegl-utils.h |   15 +++++++++++++++
 2 files changed, 58 insertions(+), 0 deletions(-)
---
diff --git a/gegl/gegl-utils.c b/gegl/gegl-utils.c
index 21981f7..e947bd2 100644
--- a/gegl/gegl-utils.c
+++ b/gegl/gegl-utils.c
@@ -276,3 +276,46 @@ gegl_free (gpointer buf)
   g_assert (buf);
   g_free (*((gpointer*)buf -1));
 }
+
+#define MAKE_COPY_CASE(typesize)\
+case typesize: \
+  while (count--) \
+    { \
+      memcpy (dst, src, typesize); \
+      dst += typesize; \
+    } \
+  return;
+
+void
+gegl_memset_pattern (void * restrict       dst_ptr,
+                     const void * restrict src_ptr,
+                     gint                  pattern_size,
+                     gint                  count)
+{
+  guchar *dst = dst_ptr;
+  const guchar *src = src_ptr;
+
+  switch (pattern_size)
+  {
+    case 1: /* Y u8 */
+      memset (dst, *src, count);
+      return;
+MAKE_COPY_CASE(2) /* YA u8 */
+MAKE_COPY_CASE(3) /* RGB u8 */
+MAKE_COPY_CASE(4) /* RGBA u8 */
+MAKE_COPY_CASE(6) /* RGB u16 */
+MAKE_COPY_CASE(8) /* RGBA u16 */
+MAKE_COPY_CASE(12) /* RGB float */
+MAKE_COPY_CASE(16) /* RGBA float */
+    default:
+      while (count--)
+        {
+          memcpy (dst, src, pattern_size);
+          dst += pattern_size;
+        }
+      return;
+  }
+}
+
+#undef MAKE_COPY_CASE
+
diff --git a/gegl/gegl-utils.h b/gegl/gegl-utils.h
index d314c6b..2bd035a 100644
--- a/gegl/gegl-utils.h
+++ b/gegl/gegl-utils.h
@@ -229,6 +229,21 @@ void     gegl_free                    (gpointer mem);
  */
 gpointer gegl_calloc (gsize size, int n_memb) G_GNUC_MALLOC;
 
+/**
+ * gegl_memset_pattern: (skip)
+ * @dst_ptr: pointer to copy to
+ * @src_ptr: pointer to copy from
+ * @pattern_size: the length of @src_ptr
+ * @count: number of copies
+ *
+ * Fill @dst_ptr with @count copies of the byes in @src_ptr.
+ */
+void gegl_memset_pattern              (void *       dst_ptr,
+                                       const void * src_ptr,
+                                       gint         pattern_size,
+                                       gint         count);
+
+
 #define GEGL_FLOAT_EPSILON            (1e-5)
 #define GEGL_FLOAT_IS_ZERO(value)     (_gegl_float_epsilon_zero ((value)))
 #define GEGL_FLOAT_EQUAL(v1, v2)      (_gegl_float_epsilon_equal ((v1), (v2)))


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