changeset 1786:eb98e57d3969

Bring in mapBlockGetEntropy() as we will be using it in the search server.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 29 Oct 2017 04:57:42 +0200
parents 7ec862ed6514
children ccf488dac4c2
files mapsearch.c
diffstat 1 files changed, 36 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Sun Oct 29 04:56:54 2017 +0200
+++ b/mapsearch.c	Sun Oct 29 04:57:42 2017 +0200
@@ -538,6 +538,42 @@
 }
 
 
+int mapBlockGetEntropy(const MapBlock *map, const char *exclude, const int nexclude)
+{
+    unsigned char *list;
+    int num, i;
+
+    // Allocate memory for entropy array
+    if ((list = th_malloc0(256)) == NULL)
+        return -1;
+
+    // Collect sums into entropy array
+    for (int y = 0; y < map->height; y++)
+    {
+        unsigned char *c = map->data + (y * map->scansize);
+        for (int 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;
+}
+
+
 double mapMatchBlock(const MapBlock *map, const MapBlock *match, int ox, int oy, int fillCh, BOOL hardDrop)
 {
     int n, k;