/* img_sum.c sum two imgfiles. img_sum file1 file2 > sum.out 23 Feb 2009 WHFS Updates old "sum" to be independent on file size. */ #include "img_predict.h" int main (int argc, char **argv) { short int *x, y; int i, nx, ny, n; FILE *fp; if (argc != 3) { fprintf (stderr, "usage: img_sum file1.img file2.img > sumout.img\n"); exit (-1); } if (check_imgfile_size (argv[1], &nx, &ny) ) { fprintf (stderr, "img_sum: FATAL ERROR. Cannot understand size of %s\n", argv[1]); exit (EXIT_FAILURE); } n = nx * ny; if ( (x = (short int *) malloc ( (size_t) (2 * n) ) ) == NULL) { fprintf (stderr, "img_sum: FATAL ERROR. Cannot malloc %d bytes\n", 2*n); exit (EXIT_FAILURE); } if ( (fp = fopen(argv[1], "r") ) == NULL) { fprintf (stderr, "img_sum: FATAL ERROR. Cannot open r %s\n", argv[1]); exit (-1); } if ( (fread ( (void *)x, (size_t)2, (size_t)n, fp) ) != n) { fprintf (stderr, "img_sum: FATAL ERROR reading %s\n", argv[1]); exit (-1); } fclose (fp); if ( (fp = fopen(argv[2], "r") ) == NULL) { fprintf (stderr, "img_sum: FATAL ERROR. Cannot open r %s\n", argv[2]); exit (-1); } for (i = 0; i < n; i++) { if ( (fread ( (void *)&y, (size_t)2, (size_t)1, fp) ) != 1) { fprintf (stderr, "img_sum: FATAL ERROR reading i = %d from %s\n", i, argv[2]); exit (-1); } x[i] += y; } fclose (fp); if ( (fwrite( (void *)x, (size_t)2, (size_t)n, stdout) ) != n) { fprintf (stderr, "img_sum: FATAL ERROR writing sum to stdout\n"); exit (-1); } fclose (fp); free ((void *)x); exit (EXIT_SUCCESS); }