From b75077e5c56fb259699c9f7ee610ef7657675cef Mon Sep 17 00:00:00 2001 From: mutantturkey Date: Thu, 5 Jul 2012 16:46:04 -0400 Subject: remove the need for a sample file by just reading the first item in the list, and use rewind to avoid re-opening the input file --- fly-tools/background/main.c | 108 +++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 56 deletions(-) (limited to 'fly-tools/background') diff --git a/fly-tools/background/main.c b/fly-tools/background/main.c index 4fa2a6e..1440a78 100644 --- a/fly-tools/background/main.c +++ b/fly-tools/background/main.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -29,16 +28,12 @@ int findmax (uint8_t *p, int n) { int main(int argc, char **argv ) { char *usage = "derive-background -i -s -o "; - char *sample_file = NULL; char *output_file = NULL; char *image_list = NULL; int c; - while ((c = getopt (argc, argv, "s:i:o:h")) != -1) + while ((c = getopt (argc, argv, "i:o:h")) != -1) switch (c) { - case 's': - sample_file = optarg; - break; case 'i': image_list = optarg; break; @@ -53,7 +48,7 @@ int main(int argc, char **argv ) { break; } - if( sample_file == NULL || image_list == NULL || output_file == NULL ) { + if( image_list == NULL || output_file == NULL ) { puts(usage); exit(1); } @@ -77,33 +72,42 @@ int main(int argc, char **argv ) { MagickWandGenesis(); - // open the first image and get the height and width + // open first image in input_file to deterimine the height and width + input_file = fopen( image_list, "r"); + if(input_file != NULL) { + fgets(filename, sizeof(filename), input_file); + temp = strchr(filename, '\n'); + if (temp != NULL) *temp = '\0'; + } else { + printf("could not open file \n"); + exit(1); + } + rewind(input_file); + first_wand = NewMagickWand(); - if(MagickReadImage(first_wand, sample_file) == MagickFalse) { + if(MagickReadImage(first_wand, filename) == MagickFalse) { ThrowWandException(first_wand); - } + } - int height = MagickGetImageHeight(first_wand); + int height = MagickGetImageHeight(first_wand); int width = MagickGetImageWidth(first_wand); first_wand = DestroyMagickWand(first_wand); printf("height: %d width:%d \n", height, width); - // count how many images there are in the input file - FILE *count = fopen( image_list, "r"); - if(count != NULL) { - while((fgets(filename, sizeof(filename), count)) != NULL) { - nImages++; - } - fclose(count); + // count number of images we have in the file + while((fgets(filename, sizeof(filename), input_file)) != NULL) { + nImages++; } + rewind(input_file); printf("number of images: %d \n", nImages); - uint8_t * array = (uint8_t *)malloc(nImages*height*width*3*sizeof(uint8_t)); // initialize the storage arrays + uint8_t * array = (uint8_t *)malloc(nImages*height*width*3*sizeof(uint8_t)); + output = calloc(height, sizeof(output[0])); for(i = 0; i < height; i++) { output[i] = calloc(width, sizeof(output[0][0])); @@ -112,45 +116,37 @@ int main(int argc, char **argv ) { } } - // store each pixel in the storage array. array[nImage][height][width][ { R, G, B} ] - input_file = fopen ( image_list, "r" ); - if ( input_file != NULL ) { - while ((fgets(filename, sizeof(filename), input_file)) != NULL ) { - temp = strchr(filename, '\n'); - if (temp != NULL) *temp = '\0'; - - input_wand = NewMagickWand(); - MagickReadImage(input_wand, filename); - - input_iterator = NewPixelIterator(input_wand); - - printf("Image number:%d Filename: %s \n", image, filename); - for (i=0; i