diff options
| author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2012-04-20 13:57:22 +0200 | 
|---|---|---|
| committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2012-04-20 13:57:22 +0200 | 
| commit | dd1d4771adb3583403e0fe90e09eaa79a9ff2e88 (patch) | |
| tree | fa69d398284196e97308b4010b3712b4ff95961c | |
| parent | b39b9697bcc9270953ecadf11d2bedbc4f87ced6 (diff) | |
| download | cv-dd1d4771adb3583403e0fe90e09eaa79a9ff2e88.tar.gz cv-dd1d4771adb3583403e0fe90e09eaa79a9ff2e88.tar.bz2 cv-dd1d4771adb3583403e0fe90e09eaa79a9ff2e88.zip | |
hst: Calculate cell position instead of caching
Its easy to calculate the index from pointer offset,
so we can avoid an initialization loop.
| -rw-r--r-- | hst.c | 42 | 
1 files changed, 21 insertions, 21 deletions
| @@ -1,5 +1,6 @@  #include <gdk-pixbuf/gdk-pixbuf.h>  #include <glib.h> +#include <string.h>  #ifndef INTERVAL  #define INTERVAL 8 @@ -7,7 +8,6 @@  #define BLOCKS (256 / INTERVAL)  struct cell { -	int i, j, k;  	int count;  	struct cell *max;  }; @@ -38,13 +38,24 @@ compare_cell(gconstpointer a, gconstpointer b)  }  static void +calculate_index(struct hst *hst, struct cell *cell, int *i, int *j, int *k) +{ +	size_t offset = cell - &hst->cells[0][0][0]; + +	*i = offset / (BLOCKS*BLOCKS); +	*j = (offset - *i * (BLOCKS*BLOCKS)) / BLOCKS; +	*k = (offset - *i * (BLOCKS*BLOCKS) - *j * BLOCKS); +} + +static void  add_neighbors(struct hst *hst, struct cell *cell)  { -	int i, j, k;	 +	int i, j, k, ci, cj, ck; -	for (i = MAX(cell->i - 1, 0); i < MIN(cell->i + 1, BLOCKS); ++i) { -		for (j = MAX(cell->j - 1, 0); j < MIN(cell->j + 1, BLOCKS); ++j) { -			for (k = MAX(cell->k - 1, 0); k < MIN(cell->k + 1, BLOCKS); ++k) { +	calculate_index(hst, cell, &ci, &cj, &ck); +	for (i = MAX(ci - 1, 0); i < MIN(ci + 1, BLOCKS); ++i) { +		for (j = MAX(cj - 1, 0); j < MIN(cj + 1, BLOCKS); ++j) { +			for (k = MAX(ck - 1, 0); k < MIN(ck + 1, BLOCKS); ++k) {  				if (hst->cells[i][j][k].count <= cell->count &&  				    hst->cells[i][j][k].max == NULL) {  				    	hst->cells[i][j][k].max = cell->max; @@ -68,6 +79,7 @@ main(int argc, char *argv[])  	struct cell *cell;  	g_type_init(); +	memset(&hst, 0, sizeof hst);  	if (argc < 2)  		return 1; @@ -76,19 +88,6 @@ main(int argc, char *argv[])  	if (!image)  		return 1; -	hst.list = NULL; -	for (i = 0; i < BLOCKS; ++i) { -		for (j = 0; j < BLOCKS; ++j) { -			for (k = 0; k < BLOCKS; ++k) { -				hst.cells[i][j][k].i = i; -				hst.cells[i][j][k].j = j; -				hst.cells[i][j][k].k = k; -				hst.cells[i][j][k].count = 0; -				hst.cells[i][j][k].max = NULL; -			} -		} -	} -  	pixbuf_get(image, &row, &width, &height, &nch, &stride);  	for (y = 0; y < height; ++y, row += stride)  		for (x = 0, p = row; x < width; ++x, p += nch) @@ -114,9 +113,10 @@ main(int argc, char *argv[])  		for (x = 0, p = row; x < width; ++x, p += nch) {  			cell = &hst.cells[p[0] / INTERVAL][p[1] / INTERVAL][p[2] / INTERVAL]; -			p[0] = cell->max->i * INTERVAL; -			p[1] = cell->max->j * INTERVAL; -			p[2] = cell->max->k * INTERVAL; +			calculate_index(&hst, cell->max, &i, &j, &k); +			p[0] = i * INTERVAL; +			p[1] = j * INTERVAL; +			p[2] = k * INTERVAL;  		}  	} | 
