changeset 1471:da2dc8d19038

Add mapBlockGetEntropy().
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 14 May 2014 01:39:12 +0300
parents e5502fc43a7d
children fe72ebfed971
files libmaputils.c libmaputils.h
diffstat 2 files changed, 37 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libmaputils.c	Wed May 14 01:38:56 2014 +0300
+++ b/libmaputils.c	Wed May 14 01:39:12 2014 +0300
@@ -828,3 +828,39 @@
         fputc(0xff, f);
     }
 }
+
+
+int mapBlockGetEntropy(MapBlock *map, const unsigned char *exclude, const int nexclude)
+{
+    unsigned char *list;
+    int x, y, i, num;
+
+    // Allocate memory for entropy array
+    if ((list = th_malloc0(256)) == NULL)
+        return -1;
+
+    // Collect sums into entropy array
+    for (y = 0; y < map->height; y++)
+    {
+        unsigned char *c = map->data + (y * map->scansize);
+        for (x = 0; x < map->width; x++)
+        {
+            list[*c]++;
+            c++;
+        }
+    }
+
+    // Handle exclusions
+    if (exclude != NULL && nexclude > 0)
+    {
+        for (i = 0; i < nexclude; i++)
+            list[(int) exclude[i]] = 0;
+    }
+
+    // Calculate simple entropy
+    for (num = 0, i = 0; i < 256; i++)
+        if (list[i]) num++;
+
+    th_free(list);
+    return num;
+}
--- a/libmaputils.h	Wed May 14 01:38:56 2014 +0300
+++ b/libmaputils.h	Wed May 14 01:39:12 2014 +0300
@@ -116,6 +116,7 @@
 int         mapBlockPut(MapBlock **, MapBlock *, int, int);
 void        mapBlockClean(MapBlock *, char *);
 void        mapBlockPrintRaw(FILE *, MapBlock *);
+int         mapBlockGetEntropy(MapBlock *map, const unsigned char *exclude, const int nexclude);
 
 
 #endif