/*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); }
int main(int argc, char *argv[]) { (void)argc; (void)argv; UArray2_T test_array; bool OK = true; test_array = UArray2_new(DIM1, DIM2, ELEMENT_SIZE); //These functions check that the dimension has been set correctly OK &= (UArray2_width(test_array) == DIM1); OK &= (UArray2_height(test_array) == DIM2); OK &= (UArray2_size(test_array) == ELEMENT_SIZE); /* Note: we are only setting a value on the corner of the array */ *((number *)UArray2_at(test_array, DIM1-1, DIM2-1)) = MARKER; printf("Trying column major\n"); UArray2_map_col_major(test_array, check_and_print, &OK); printf("Trying row major\n"); UArray2_map_row_major(test_array, check_and_print, &OK); printf(" width = %d\n", UArray2_width(test_array)); printf("height %d\n", UArray2_height(test_array)); UArray2_free(&test_array); printf("The array is %sOK!\n", (OK ? "" : "NOT ")); }
/* Takes in a pointer to an array2b and frees all of its allocated memory */ void UArray2b_free(T *array2b) { assert(array2b); assert(*array2b); UArray2_map_row_major((*array2b)->shell, free_blocks, NULL); UArray2_free(&((*array2b)->shell)); FREE(*array2b); }
/* prints the compressed image (code words) to standard output. The width and height variables describe the dimensions of the original (decompressed) image, after trimming off any odd column or row. */ void compress_print(Word_image w_image) { unsigned width = (w_image->width)*2; unsigned height = (w_image->height)*2; printf("COMP40 Compressed image format 2\n%u %u", width, height); printf("\n"); UArray2_map_row_major(w_image->words, print_c_apply, NULL); }
extern void UArray2b_free (T *array2b) { assert(array2b); UArray2_map_row_major((*array2b)->blockarray, free_sub_arrays, NULL); UArray2_free(&(*array2b)->blockarray); FREE(*array2b); }
/* this function takes in a PPM image and returns a videoColor image with video Color components that represent each pixel */ VideoColor_image pnm_to_videoColor (Pnm_ppm pnm_image) { unsigned width = pnm_image->width; unsigned height = pnm_image->height; VideoColor_image vc_image = VideoColor_image_new(width, height); UArray2_map_row_major(vc_image->pixels, rgb_vc, pnm_image); return vc_image; }
/* checks that each row, column, and 3x3 box has digits 1 through 9*/ void correct_sudoku(UArray2_T uarray2) { Bit_T bit = Bit_new(10); UArray2_map_row_major(uarray2, check_setof9, bit);; UArray2_map_col_major(uarray2, check_setof9, bit); UArray2_map_3x3_box(uarray2, check_setof9, bit); Bit_free(&bit); }
/* box_check() * * Function is passed the upper right hand corner of one of the 3x3 subsections * of the sudoku puzzle and loops through each element of the section using the * frequency array to determine if there are any repeated elements. Calls a * helper function to perform the actual duplicate check. */ void box_check(UArray2_T array, int col, int row, UArray2_T freq_arr) { UArray2_map_row_major(freq_arr, reset_to_zero, NULL); for (int i = row; i < (row + 3); i++) { for (int j = col; j < (col + 3); j++) { int *curr_num = UArray2_at(array, j, i); assert(sizeof(*curr_num) == UArray2_size(array)); check_for_duplicate(array, freq_arr, (void *)curr_num); } } }
/* col_check() * * Function is mapped to each element of the array in column-major order. For * each element it uses a helper function to check if that number has already * been seen in the current column. Reesets the 9x1 frequency array to all 0's * each time a new column is examined. */ void col_check(int col, int row, UArray2_T array, void *element, void *cl) { (void) col; (void) array; assert(sizeof(*(int *)element) == UArray2_size(array)); if (row == 0) { UArray2_map_row_major(*(UArray2_T *)cl, reset_to_zero, NULL); } check_for_duplicate(array, *(UArray2_T *)cl, element); }
/* check_solution() * * Function coordinates the solution checking process by calling helper * functions that check each portion of the puzzle. Creates a new 9x1 array that * contains ones and zeros. If a 1 is stored in element i of the array, this * indicates that the number i+1 has already been seen in that portion of the * puzzle - and thus the solution is incorrect. The function also frees this * array at the end of the checking process to avoid memory leaks. */ void check_solution(UArray2_T array) { UArray2_T curr_line_arr = UArray2_new(9, 1, sizeof(int)); UArray2_map_row_major(array, row_check, &curr_line_arr); UArray2_map_col_major(array, col_check, &curr_line_arr); for (int i = 0; i < 9; i += 3) { for (int j = 0; j < 9; j += 3) { box_check(array, j, i, curr_line_arr); } } UArray2_free(&curr_line_arr); }
/* this function takes in a videoColor image and returns a PPM image red green an blue values that represent each pixel */ Pnm_ppm videoColor_to_pnm(VideoColor_image vc_image) { unsigned width = vc_image->width; unsigned height = vc_image->height; A2Methods_T methods = uarray2_methods_plain; assert(methods); Pnm_ppm pnm_image; NEW(pnm_image); pnm_image->width = width; pnm_image->height = height; pnm_image->denominator = DENOMINATOR; pnm_image->pixels = UArray2_new(width, height, sizeof(struct Pnm_rgb)); pnm_image->methods = methods; UArray2_map_row_major(pnm_image->pixels, vc_rgb, vc_image); return pnm_image; }
/* Internally used by the two UArray2b_new functions * Initializes each element in the UArray2b */ T initialize_arr(int width, int height, int size, int blocksize) { T newArr; NEW(newArr); /* "shell" array */ newArr->height = height; newArr->width = width; newArr->size = size; newArr->blocksize = blocksize; width = block_dim(width, blocksize); height = block_dim(height, blocksize); newArr->shell = UArray2_new(width, height, sizeof(UArray2_T)); /* uarray2 of uarray2s, for blocks */ struct element_size es; es.size = size; es.blocksize = blocksize; UArray2_map_row_major(newArr->shell, make_arrs, &es); return newArr; }
/* read_in_solution() * * Function takes an empty 2-Dimensional array where the solution will be stored * for checking and a pointer to the file with the solution to be checked. Using * the Pnmrdr interface, it ensures that the format of the solution is correct * and uses a helper function to fill the 2-D array with the puzzle entries. */ void read_in_solution(UArray2_T array, FILE *srcfile) { void *reader = NULL; Pnmrdr_mapdata data; int width = 0; int height = 0; int max_intensity = 0; reader = Pnmrdr_new(srcfile); data = Pnmrdr_data(reader); assert(data.type == Pnmrdr_gray); width = data.width; height = data.height; max_intensity = data.denominator; assert(max_intensity == 9); assert(width == 9 && height == 9); UArray2_map_row_major(array, fill_array, reader); Pnmrdr_free((Pnmrdr_T *)&reader); }
/* This function prints the RGB values og every pixel in the PPM image*/ void print_pnmppm(Pnm_ppm image) { UArray2_map_row_major(image->pixels, print_pnmppm_apply, NULL); }