Ejemplo n.º 1
0
/*Wrapper function for all of the intermediary image decompression functions.
  This function stores the intermediary components between the compress format
  and the RGB format. It also calls the free-ing functions, which are universal
  to compress and decompress.*/
void decompress40(FILE *input)
{
        unsigned height, width;
        int read = fscanf(input, "COMP40 Compressed image format 2\n%u %u", 
                          &width, &height);
        assert(read == 2);

        int c = getc(input);
        assert(c == '\n');

        UArray2_T words = UArray2_new(height/2, width/2, sizeof(uint64_t));
        UArray2_map_row_major(words, store_compressed, &input);

        Pnm_components wordparts = decomp_word_array(words);
        UArray2_free(&words);

        Pnm_cv compvid = decomp_components_array(wordparts);
        wordparts_free(wordparts);

        Pnm_ppm decompressed = decomp_cv_array(compvid);
        compvid_free(compvid);

        Pnm_ppmwrite(stdout, decompressed);
        Pnm_ppmfree(&decompressed);
}
Ejemplo n.º 2
0
/* This function takes in the file, reads the header, utilizes
 * functions in compute40 module to perform the decompress, completes the
 * decompressed ppm, and calls ppmwrite() to write the image to the 
 * stdout */
void decompress40(FILE *input)
{
        unsigned height, width;
        int read = fscanf(input, "COMP40 Compressed image format 2\n%u %u", 
                          &width, &height);
        assert(read == 2);
        int c = getc(input);
        assert(c == '\n');

        A2Methods_T methods = uarray2_methods_blocked;
        A2 array = methods->new_with_blocksize(width / 2, height / 2, 
                                               sizeof(uint64_t), 1);
        assert(array);
        A2 result = methods->new_with_blocksize(width, height,
                                                sizeof(struct Pnm_rgb), 2);
        assert(result);
        
        methods->map_block_major(array, read_compressed, input);
 
        methods->map_block_major(array, decompress, result);

        assert(result);

        struct Pnm_ppm pixmap = { .width = width, .height = height,
                                  .denominator = DENOMINATOR, .pixels = result,
                                  .methods = uarray2_methods_blocked}; 

        Pnm_ppmwrite(stdout, &pixmap);

        methods->free(&result);
        methods->free(&array);
}
Ejemplo n.º 3
0
/* decompress40()
 * Takes in a file pointer to a binary compressed image
 * Creates and frees the arrays needed by the 4 other decompression modules 
 * used by compress40 and calls their functions in the correct order to
 * decompress the given file into a ppm image
 * Writes the new ppm image to standard output
 */
void decompress40(FILE *input)
{
        methods = uarray2_methods_blocked;

        A2Methods_UArray2 compressed_array = read_packed_elems(input, methods);
        A2Methods_UArray2 small_video_img = unpack_codewords(compressed_array);
        methods->free(&compressed_array);
        A2Methods_UArray2 video_array = small_video_to_video(small_video_img, 
                                                             methods);
        methods->free(&small_video_img);
        Pnm_ppm decompressed_image = video_array_to_rgb_array(video_array, 
                                                              methods);
        methods->free(&video_array);

        Pnm_ppmwrite(stdout, decompressed_image);
        Pnm_ppmfree(&decompressed_image);
}
Ejemplo n.º 4
0
/* reads compressed image, wirtes PPM decompressed image */
extern void decompress40(FILE *input)
{
    unsigned height, width;

    int read 
    = fscanf(input, "COMP40 Compressed image format 2\n%u %u", &width, &height);

    assert(read == 2);
    int c = getc(input);
    assert(c == '\n');

    Word_image w_image = Word_image_new( width/2,  height/2);
    uint64_t temp = 0;
    uint64_t *temp2 = NULL;
    unsigned col, row;

    for (row = 0; row < (height/2); row++){
        for (col = 0; col < (width/2); col++){
            temp = Bitpack_newu(temp, 8, 0, getc(input));
            temp = Bitpack_newu(temp, 8, 8, getc(input));
            temp = Bitpack_newu(temp, 8, 16, getc(input));
            temp = Bitpack_newu(temp, 8, 24, getc(input));
            temp2 = UArray2_at(w_image->words, col, row);
            *temp2 = temp;
        }
    }

    WordFields_image wf_image2 = wordImage_to_wordFields (w_image);
    Word_image_free(&w_image);

    VideoColor_image vc_image_result = wordFields_to_videoColor (wf_image2);
    WordFields_image_free(&wf_image2);
 
    Pnm_ppm result = videoColor_to_pnm(vc_image_result);
    

    Pnm_ppmwrite(stdout, result);
    

    VideoColor_image_free(&vc_image_result);
    Pnm_ppmfree(&result);

}
Ejemplo n.º 5
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);
}
Ejemplo n.º 6
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);
}