Example #1
0
int main(int argc, char *argv[]) {
    int rotation = 0;
    A2Methods_T methods = uarray2_methods_plain; // default to UArray2 methods
    assert(methods);
    A2Methods_mapfun *map = methods->map_default; // default to best map
    assert(map);

#define SET_METHODS(METHODS, MAP, WHAT) do { \
      methods = (METHODS); \
      assert(methods); \
      map = methods->MAP; \
      if (!map) { \
        fprintf(stderr, "%s does not support " WHAT "mapping\n", argv[0]); \
        exit(1); \
      } \
    } while(0)

    int i;
    FILE *input;
    for (i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "-row-major")) {
            SET_METHODS(uarray2_methods_plain, map_row_major, "row-major");
        } else if (!strcmp(argv[i], "-col-major")) {
            SET_METHODS(uarray2_methods_plain, map_col_major, "column-major");
        } else if (!strcmp(argv[i], "-block-major")) {
            SET_METHODS(uarray2_methods_blocked, map_block_major, "block-major");
        } else if (!strcmp(argv[i], "-rotate")) {
            assert(i + 1 < argc);
            char *endptr;
            rotation = strtol(argv[++i], &endptr, 10);
            assert(*endptr == '\0'); // parsed all correctly
            assert(rotation == 0   || rotation == 90
                   || rotation == 180 || rotation == 270);
        } else if (*argv[i] == '-') {
            fprintf(stderr, "%s: unknown option '%s'\n", argv[0], argv[i]);
            exit(1);
        } else if (argc - i > 2) {
            fprintf(stderr, "Usage: %s [-rotate <angle>] "
                    "[-{row,col,block}-major] [filename]\n", argv[0]);
            exit(1);
        } else if (i == argc-1) {
            input = fopen (argv[i], "rb");
            if (!input) {
                perror(argv[i]);
                exit(1);
            }

        } else {
            input = stdin;
            assert(stdin);
            break;
        }
    }

    /*
    Read Pixmap, error check

    perform rotation using specified mapping method:
    	formulas:









    */


    assert(input);
    Pnm_ppm pixmap = Pnm_ppmread(input, methods);
    assert(pixmap);
    Pnm_ppmwrite(stdout, pixmap);
    Pnm_ppmfree(&pixmap);
    exit(0);
}
Example #2
0
/*                                 main()
 *
 * Main function for the program reads in an image from the command line or from
 * stdin, as well as any specifications the user provided for a storage method
 * and/or desired rotation. Calls helper functions to perform the proper
 * rotation on the image and prints the final image to stdout in the ppm format.
 */
int main(int argc, char *argv[]) 
{
        int i;
        int rotation = 0;
	FILE *srcfile;
 	Pnm_ppm original_image;
	Pnm_ppm transformed_image;

        /* default to UArray2 methods */
        A2Methods_T methods = uarray2_methods_plain; 
        assert(methods);

        /* default to best map */
        A2Methods_mapfun *map = methods->map_default; 
        assert(map);

#define SET_METHODS(METHODS, MAP, WHAT) do {                            \
                methods = (METHODS);                                    \
                assert(methods != NULL);                                \
                map = methods->MAP;                                     \
                if (map == NULL) {                                      \
                        fprintf(stderr, "%s does not support "          \
                                        WHAT "mapping\n",               \
                                argv[0]);                               \
                        exit(1);                                        \
                }                                                       \
        } while (0)

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-row-major")) {
			SET_METHODS(uarray2_methods_plain, map_row_major,
				    "row-major");
		} else if (!strcmp(argv[i], "-col-major")) {
			SET_METHODS(uarray2_methods_plain, map_col_major,
				    "column-major");
		} else if (!strcmp(argv[i], "-block-major")) {
			SET_METHODS(uarray2_methods_blocked, map_block_major,
				    "block-major");
		} else if (!strcmp(argv[i], "-rotate")) {
			if (!(i + 1 < argc)) {      /* no rotate value */
				usage(argv[0]);
			}
			char *endptr;
			rotation = strtol(argv[++i], &endptr, 10);
			if (!(rotation == 0 || rotation == 90
			      || rotation == 180 || rotation == 270)) {
				fprintf(stderr, "Rotation must be "
					"0, 90 180 or 270\n");
				usage(argv[0]);
				exit(EXIT_FAILURE);
			}
			if (!(*endptr == '\0')) {    /* Not a number */
				usage(argv[0]);
			      	exit(EXIT_FAILURE);
			}
		} else if (*argv[i] == '-') {
			fprintf(stderr, "%s: unknown option '%s'\n", argv[0],
				argv[i]);
			exit(EXIT_FAILURE);
		} else if (argc - i > 1) {
			fprintf(stderr, "Too many arguments\n");
			usage(argv[0]);
		} else {
			break;
		}
	}
	
	if (i < argc) {
                srcfile = fopen (argv[i], "rb");
                assert(srcfile != NULL);
        }
        else {
                srcfile = stdin;
                assert(srcfile != NULL);
        }
	original_image = Pnm_ppmread (srcfile, methods);
  
        if (rotation == 0) {
                Pnm_ppmwrite(stdout, original_image);
                Pnm_ppmfree(&original_image);
        }
        else{
                transformed_image = transform_image (rotation, original_image, 
                                                     map, methods);
                Pnm_ppmwrite(stdout, transformed_image);        
                Pnm_ppmfree(&original_image);
                Pnm_ppmfree(&transformed_image);
        }
        fclose(srcfile);
	exit (EXIT_SUCCESS);
}