#include #include /* This program converts a list of tiff files to files in two forms: a binary file and an ascii file. The two files are equivalent; each contains the entire array. The binary output file is called array.bin, the ascii file is called array.ascii . The binary file is created first. The ascii file is created by converting the binary file to ascii. */ void executeCmd (char *cmd); int cvtFileList (char *fileName); int cvtArgList (int argc, char **argv); int binToAscii (char *inFN, char *outFN); void usage (char *name); main (int argc, char **argv) { /* Check if we have some command line arguments. */ if (argc <= 1) { /* If not, somethings wrong. */ usage (argv[0]); } else if (strcmp (argv[1], "-f") == 0) { /* if we have a -f, then we should read file names from another file. */ if (argc != 3) { /* there is no list file */ usage (argv[0]); } else { /* convert the files listed in argv[2] */ cvtFileList (argv[2]); binToAscii ("array.bin", "array.ascii"); } } else { /* convert the files listed on command line */ cvtArgList (argc, argv); binToAscii ("array.bin", "array.ascii"); } exit (0); } void usage (char *name) { printf ("\n"); printf ("Usage:\n"); printf ("\n"); printf (" %s . . . \n", name); printf (" or\n"); printf (" %s -f \n", name); printf ("\n"); printf (" In the first form, fn1, fn2, fn3, . . . should be a list\n"); printf (" of file names.\n"); printf ("\n"); printf (" In the second form, should be the name of a \n"); printf (" file that contains a list of file names.\n"); printf ("\n"); printf (" For both cases, the list of files is converted from tiff\n"); printf (" to raw bytes (one byte per pixel) and the raw bytes are\n"); printf (" concatentated together to form a single binary file named\n"); printf (" array.bin. This file is then converted to an ascii file \n"); printf (" called array.ascii. \n"); printf ("\n"); printf (" This program invokes the program \"convert\" to convert\n"); printf (" the tiff files to raw binary data. The file temp.gray\n"); printf (" is used as a temporary file.\n"); printf ("\n"); } void executeCmd (char *cmd) { printf (cmd); printf ("\n"); fflush (stdout); system (cmd); printf ("\n"); fflush (stdout); } /* convert files listed in a file. */ int cvtFileList (char *fileName) { char cmd[512]; char imgName[256]; FILE *fp; /* Open the file that contains the list of files to be converted. */ if ((fp = fopen (fileName, "r")) == NULL) { fprintf (stderr, "Unable to open file %s.\n", fileName); return -1; } printf ("\nDeleting array.bin\n"); executeCmd ("rm array.bin"); /* Read each tiff file name, convert to "gray", then concat to end of array.bin */ while (fscanf (fp, "%s", imgName) == 1) { sprintf (cmd, "convert %s temp.gray", imgName); executeCmd (cmd); executeCmd ("cat temp.gray >> array.bin"); } fclose (fp); /* remove the temporary file */ executeCmd ("rm temp.gray"); return 0; } /* end of cvtFileList */ int cvtArgList (int argc, char **argv) { int i; char cmd[512]; printf ("\nDeleting array.bin\n"); executeCmd ("rm array.bin"); /* Get each tiff file name from the list of command line args, convert to gray, then concat to end of array.bin. */ for (i = 1; i < argc; i++) { sprintf (cmd, "convert %s temp.gray", argv[i]); executeCmd (cmd); executeCmd ("cat temp.gray >> array.bin"); } /* remove the temporary file */ executeCmd ("rm temp.gray"); return (0); } /* end of cvtArgList */ int binToAscii (char *inFN, char *outFN) { FILE *inFP, *outFP; unsigned char c; printf ("Converting %s to %s . . . \n", inFN, outFN); if ( (inFP = fopen (inFN, "rb")) == NULL) { fprintf (stderr, "Error openning file < %s >\n", inFN); return -1; } if ( (outFP = fopen (outFN, "w")) == NULL) { fprintf (stderr, "Error openning file < %s >\n", outFN); return -1; } while ( fread (&c, sizeof(unsigned char), 1, inFP) == 1) { fprintf (outFP, "%d\n", c); } fclose (outFP); fclose (inFP); printf ("\n"); return 0; } /* end of binToAscii */