/* 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); }
static void double_row_major_plus() { /* store increasing integers in row-major order */ A2 array = methods->new_with_blocksize(W, H, sizeof(int), BS); int counter = 1; for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { /* col index varies faster */ int *p = methods->at(array, i, j); *p = counter++; } } counter = 1; for (int j = 0; j < H; j++) { for (int i = 0; i < W; i++) { int *p = methods->at(array, i, j); assert(*p == counter); counter++; } } if (methods->map_row_major) { counter = 1; methods->map_row_major(array, check_and_increment, &counter); } if (methods->small_map_row_major) { counter = 1; methods->small_map_row_major(array, small_check_and_increment, &counter); } methods->free(&array); }
static void test_methods(A2Methods_T methods_under_test) { methods = methods_under_test; assert(methods); assert(has_minimum_methods(methods)); assert(has_small_plain_methods(methods) || has_small_blocked_methods(methods)); assert(!(has_small_plain_methods(methods) && has_small_blocked_methods(methods))); assert(!(has_plain_methods(methods) && has_blocked_methods(methods))); if (!(has_plain_methods(methods) || has_blocked_methods(methods))) fprintf(stderr, "Some full mapping methods are missing\n"); A2 array = methods->new_with_blocksize(W, H, sizeof(unsigned), BS); copy_unsigned(methods, array, 2, 1, 99); copy_unsigned(methods, array, 3, 3, 88); copy_unsigned(methods, array, 10, 10, 77); check(array, 2, 1, 99); check(array, 3, 3, 88); check(array, 10, 10, 77); for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { unsigned n = 1000 * i + j; copy_unsigned(methods, array, i, j, n); unsigned *p = methods->at(array, i, j); assert(*p == n); } } for (int i = 0; i < W; i++) { for (int j = 0; j < H; j++) { unsigned n = 1000 * i + j; unsigned *p = methods->at(array, i, j); assert(*p == n); } } double_row_major_plus(); methods->free(&array); }
/* 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); }
/* 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); }
/* 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); }