#include #include #include #include #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 mx = 0, v = p[0]; for (int i = 1; i < n; i++) { if (p[i] > v) { v = p[i]; mx = i; } } return mx; } int main(int argc, char **argv ) { char *usage = "derive-background -i -s -o "; char *output_file = NULL; char *image_list = NULL; int c; while ((c = getopt (argc, argv, "i:o:h")) != -1) switch (c) { case 'i': image_list = optarg; break; case 'o': output_file = optarg; break; case 'h': puts(usage); exit(1); break; default: break; } if( image_list == NULL || output_file == NULL ) { puts(usage); exit(1); } MagickWand *input_wand, *first_wand, *output_wand; PixelIterator *input_iterator, *output_iterator; PixelWand **pixels, *p_out; double green, red, blue; int i,j, k; int image = 0; char filename[256]; char rgb[128]; char *temp; FILE *input_file; uint8_t ***output; MagickWandGenesis(); // 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); } fclose(input_file); first_wand = NewMagickWand(); if(MagickReadImage(first_wand, filename) == MagickFalse) { ThrowWandException(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 number of images we have in the file input_file = fopen( image_list, "r"); while((fgets(filename, sizeof(filename), input_file)) != NULL) { nImages++; } fclose(input_file); printf("number of images: %d \n", nImages); // 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])); for(j = 0; j < width; j++) { output[i][j] = calloc(3, sizeof(output[0][0][0])); } } // store each pixel in the storage array. array(nImages,height,width,{ R, G, B} ) input_file = fopen( image_list, "r"); if(input_file == NULL ) { printf("error could not open input file"); exit(1); } 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