/* sum_of_images.c */ #include #include #include "mypgm.h" main( ) { int number; int i, x, y; int **image3; int min, max; printf("Let's sum up images with the same size\n"); printf("Enter the number of images : "); scanf("%d", &number); /* load the first image */ printf("\n1-th image input:\n"); load_image_data( ); x_size2 = x_size1; y_size2 = y_size1; /* dynamic memory allocation */ image3 = (int **)malloc(y_size2 * sizeof(int *)); for (y = 0; y < y_size2; y++) image3[y] = (int *)malloc(x_size2 * sizeof(int)); for (y = 0; y < y_size2; y++) for (x = 0; x < x_size2; x++) image3[y][x] = image1[y][x]; /* load the remaining images */ for (i = 2; i <= number; i++) { printf("\n%d-th image input:\n", i); load_image_data( ); for (y = 0; y < y_size2; y++) for (x = 0; x < x_size2; x++) image3[y][x] += image1[y][x]; } min = max = image3[0][0]; for (y = 0; y < y_size2; y++) { for (x = 0; x < x_size2; x++) { if (image3[y][x] < min) min = image3[y][x]; if (image3[y][x] > max) max = image3[y][x]; } } if (max == min) { printf("Nothing exists!!!\n"); exit(1); } if (max <= MAX_BRIGHTNESS) { for (y = 0; y < y_size2; y++) for (x = 0; x < x_size2; x++) image2[y][x] = (unsigned char)image3[y][x]; } else { for (y = 0; y < y_size2; y++) { for (x = 0; x < x_size2; x++) { image2[y][x] = (unsigned char)((image3[y][x] - min) * MAX_BRIGHTNESS / (max - min)); } } } save_image_data( ); /* Output image save */ free(image3); return 0; }