示例#1
0
/* This function takes in the file and utilizes functions in compute40
 * module to perform the compression and outputing, and frees the memory
 */
void compress40(FILE *input)
{
        A2Methods_T methods = uarray2_methods_blocked;
        Pnm_ppm image = Pnm_ppmread(input, methods);

        assert(image);
        assert(image->width > 1 && image->height > 1);
        
        image->pixels = image_trimmer(image);
 

        A2 result = methods->new_with_blocksize(image->width / BLOCKSIZE,
                                                image->height / BLOCKSIZE, 
                                                sizeof(uint64_t), 1);
 
        methods->map_block_major(result, compress, image);
        
        assert(result);
        print_result(result, methods);

        methods->free(&result);
        FREE(result);
        Pnm_ppmfree(&image);
        
}
示例#2
0
文件: ppmdiff.c 项目: byung01/arith
int main(int argc, char *argv[]) {
        
        /* default to UArray2 methods */
        A2Methods_T methods = uarray2_methods_plain; 
        assert(methods);

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

        assert (argc <= 3);

        FILE *fp1, *fp2;
        Pnm_ppm image1, image2;

        fp1 = open_file(argc, argv[1]);
        fp2 = open_file(argc, argv[2]);

        image1 = Pnm_ppmread(fp1, methods);
        image2 = Pnm_ppmread(fp2, methods);

        if (!valid_dims(image1, image2)) {
                fprintf(stderr, "Invalid Dimensions\n");
                fprintf(stdout, "1.0\n");
        } else {
                compute_diff(image1, image2);
        }

        fclose(fp1);
        fclose(fp2);

        Pnm_ppmfree(&image1);
        Pnm_ppmfree(&image2);

        return 0;
}
示例#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);
}
示例#4
0
/* compress40()
 * Takes in a file pointer to a .ppm image
 * Creates and frees the arrays needed by the 4 other compression modules used
 * by compress40 and calls their functions in the correct order to compress
 * the given image
 * Prints the binary compressed image
 */
void compress40(FILE *input)
{
        methods = uarray2_methods_blocked;

        Pnm_ppm input_img = Pnm_ppmread(input, methods);
        A2Methods_UArray2 video_img = trim_rgb_array_to_video_array(input_img, 
                                                                    methods);
        Pnm_ppmfree(&input_img);
        A2Methods_UArray2 small_video_img = video_to_small_video(video_img, 
                                                                 methods);
        methods->free(&video_img);
        A2Methods_UArray2 compressed_img = pack_codewords(small_video_img, 
                                                       methods);
        methods->free(&small_video_img);

        print_packed_elems(compressed_img);
        methods->free(&compressed_img);
}
/* 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);

}
/* reads a PPM image, writes compressed image */
extern void compress40  (FILE *input)
{
    A2Methods_T methods = uarray2_methods_plain; 
    assert(methods);

    Pnm_ppm original = get_image(input, methods);
    trim_image(original);

    VideoColor_image vc_image = pnm_to_videoColor(original);
    Pnm_ppmfree(&original);

    WordFields_image wFields_image = videoColor_to_wordFields (vc_image);
    VideoColor_image_free(&vc_image); 

    Word_image w_image = wordFields_to_wordImage (wFields_image); 
    WordFields_image_free(&wFields_image);

    compress_print(w_image);
    Word_image_free(&w_image);
}  
示例#7
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);
}
示例#8
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);
}
示例#9
0
int main(int argc, char* argv[]) {

FILE * fp = NULL;
if (argc == 1 || argc == 2 || argc > 3){ 
  fprintf(stderr, "Please enter the correct number of arguments(3).\n"); 
  exit(EXIT_FAILURE);
}    


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

Pnm_ppm ppm_file1;
Pnm_ppm ppm_file2;

  if (!strcmp(argv[1], "-") && !strcmp(argv[2], "-")){
  fprintf(stderr, "Error please specify an image not from stdin.\n");
  exit(EXIT_FAILURE);
  }


    
    if (!strcmp(argv[1], "-")) {
      ppm_file1 = Pnm_ppmread(stdin, methods);
      fp = fopen(argv[2], "r");
      if(fp == NULL) {exit(1);}
      ppm_file2 = Pnm_ppmread(fp, methods);
      fclose(fp);
     }
      else{
           fp = fopen(argv[1], "r");
           if(fp == NULL){ exit(1);}
           ppm_file1 = Pnm_ppmread(fp, methods);
           fclose(fp);

          if (!strcmp(argv[2], "-")) 
            ppm_file2 = Pnm_ppmread(stdin, methods);
          else{
            fp = fopen(argv[2], "r");
            if(fp == NULL){ exit(1);}
            ppm_file2 = Pnm_ppmread(fp, methods);
            fclose(fp);
          }
       }
A2Methods_UArray2 image1 = ppm_file1->pixels;
A2Methods_UArray2 image2 = ppm_file2->pixels;
int height1 = methods->height(image1);
int height2 = methods->height(image2);
int width1 = methods->width(image1);
int width2 = methods->width(image2);
if(abs(height1 - height2) > 1 ||  abs(width1 - width2) > 1){
	fprintf(stderr, "dimensions: h%d, %d; w%d, %d\n", height1, height2, width1, width2);
    fprintf(stderr, "Dimesions of images are differnt.\n");
    fprintf(stdout, "%lf", 1.0);
    exit(EXIT_FAILURE);
}

int height, width;
if(height1 < height2)
   height = height1;
else
   height = height2;
if(width1 < width2)
   width = width1;
else
   width = width2;

Pnm_rgb  temp1;
Pnm_rgb  temp2;
double r1 = 0;
double r2 = 0;
double g1 = 0;
double g2 = 0;
double b1 = 0;
double b2 = 0;
double sum = 0.0;
for(int j = 0; j < height; j++){
    for(int i = 0; i < width; i++){
       temp1 = methods->at(image1, i, j);
	   r1 = temp1->red/255.0;
	   g1 = temp1->green/255.0;
	   b1 = temp1->blue/255.0;
       temp2 = methods->at(image2, i, j);
	   r2 = temp2->red/255.0;
	   g2 = temp2->green/255.0;
	   b2 = temp2->blue/255.0;
	sum += pow(((r1 - r2)), 2.0)/(3.0*width*height);
	sum += pow(((g1 - g2)), 2.0)/(3.0*width*height);
	sum += pow(((b1 - b2)), 2.0)/(3.0*width*height);
   }
}
double answer = sqrt(sum);
printf("%.4f\n", answer );
//Pnm_ppmwrite(stdout, ppm_file1);
//methods->free(&im	age1);
//methods->free(&image2);
Pnm_ppmfree(&ppm_file1);
Pnm_ppmfree(&ppm_file2);

}