From 9985d310c4d37c1c721d81aa3e3150ccc5f9819f Mon Sep 17 00:00:00 2001 From: mutantturkey Date: Thu, 5 Jul 2012 15:02:55 -0400 Subject: rewrite of the histogram, don't use gsl, also calculate pointers instead of an explicit array, reduces memory usage by like a zillion percent --- fly-tools/background/main.c | 130 +++++++++++++------------------------------- 1 file changed, 38 insertions(+), 92 deletions(-) (limited to 'fly-tools') diff --git a/fly-tools/background/main.c b/fly-tools/background/main.c index f0feb96..4fa2a6e 100644 --- a/fly-tools/background/main.c +++ b/fly-tools/background/main.c @@ -6,11 +6,16 @@ #include #include #include + #define round(x)((int)((x)+0.5)) #define ThrowWandException(wand) { char *description; ExceptionType severity; description=MagickGetException(wand,&severity); (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); description=(char *) MagickRelinquishMemory(description); exit(-1); } +#define array(i,j,k,l) (array[height*width*i + width*j + k + l]) + +int nImages; +unsigned long height; +unsigned long width; -int findmax (uint8_t *p, int n) -{ +int findmax (uint8_t *p, int n) { int mx = 0, v = p[0]; for (int i = 1; i < n; i++) { if (p[i] > v) { @@ -27,11 +32,9 @@ int main(int argc, char **argv ) { char *sample_file = NULL; char *output_file = NULL; char *image_list = NULL; - bool histogram = false; int c; - int opterr = 0; - while ((c = getopt (argc, argv, "s:i:o:mh")) != -1) + while ((c = getopt (argc, argv, "s:i:o:h")) != -1) switch (c) { case 's': sample_file = optarg; @@ -42,9 +45,6 @@ int main(int argc, char **argv ) { case 'o': output_file = optarg; break; - case 'm': - histogram = true; - break; case 'h': puts(usage); exit(1); @@ -58,7 +58,7 @@ int main(int argc, char **argv ) { exit(1); } - MagickWand *magick_wand, *first_wand, *output_wand; + MagickWand *input_wand, *first_wand, *output_wand; PixelIterator *input_iterator, *output_iterator; PixelWand **pixels, *p_out; @@ -66,10 +66,6 @@ int main(int argc, char **argv ) { int i,j, k, x, y; int image = 0; - int nImages = 0; - - unsigned long height, width; - char filename[256]; char rgb[128]; @@ -77,22 +73,22 @@ int main(int argc, char **argv ) { FILE *input_file; - uint8_t ****array; uint8_t ***output; + MagickWandGenesis(); + // open the first image and get the height and width first_wand = NewMagickWand(); if(MagickReadImage(first_wand, sample_file) == MagickFalse) { ThrowWandException(first_wand); } - height = MagickGetImageHeight(first_wand); - width = MagickGetImageWidth(first_wand); + int height = MagickGetImageHeight(first_wand); + int width = MagickGetImageWidth(first_wand); first_wand = DestroyMagickWand(first_wand); - printf("height: %ld width:%ld \n", height, width); - + printf("height: %d width:%d \n", height, width); // count how many images there are in the input file FILE *count = fopen( image_list, "r"); @@ -105,6 +101,7 @@ int main(int argc, char **argv ) { printf("number of images: %d \n", nImages); + uint8_t * array = (uint8_t *)malloc(nImages*height*width*3*sizeof(uint8_t)); // initialize the storage arrays output = calloc(height, sizeof(output[0])); @@ -115,19 +112,6 @@ int main(int argc, char **argv ) { } } - array = calloc(nImages, sizeof(array[0])); - for(i = 0; i < nImages; i++) { - array[i] = calloc(height, sizeof(array[0][0])); - for(j = 0; j < (long) height; j++) { - array[i][j] = calloc(width, sizeof(array[0][0][0])); - for(k = 0; k < width; k++){ - array[i][j][k] = calloc(3, sizeof(array[0][0][0][0])); - } - } - } - - MagickWandGenesis(); - // store each pixel in the storage array. array[nImage][height][width][ { R, G, B} ] input_file = fopen ( image_list, "r" ); if ( input_file != NULL ) { @@ -135,10 +119,10 @@ int main(int argc, char **argv ) { temp = strchr(filename, '\n'); if (temp != NULL) *temp = '\0'; - magick_wand = NewMagickWand(); - MagickReadImage(magick_wand, filename); + input_wand = NewMagickWand(); + MagickReadImage(input_wand, filename); - input_iterator = NewPixelIterator(magick_wand); + input_iterator = NewPixelIterator(input_wand); printf("Image number:%d Filename: %s \n", image, filename); for (i=0; i