/* New blocked 2d array: blocksize = square root of # of cells in block */ extern T UArray2b_new (int width, int height, int size, int blocksize) { assert(width > 0 && height > 0); assert(size > 0 && blocksize > 0); T uarray2b = malloc(sizeof(*uarray2b)); /* Set given values */ uarray2b->width = width; uarray2b->height = height; uarray2b->size = size; uarray2b->blocksize = blocksize; /* Ceil up WidInBlocks to include partically filled blocks */ uarray2b->WidInBlocks = ceil((double) width/blocksize); uarray2b->HgtInBlocks = ceil((double) height/blocksize); /* Create outer UArray2 */ uarray2b->grid = UArray2_new(uarray2b->WidInBlocks, uarray2b->HgtInBlocks, size * blocksize * blocksize); /* Loop through outer UArray2, at each cell, create inner UArray2 * to store elements of a block */ for (int i = 0; i < uarray2b->WidInBlocks; i++) { for (int j = 0; j < uarray2b->HgtInBlocks; j++) { UArray2_T *temp = UArray2_at(uarray2b->grid, i, j); *temp = UArray2_new(blocksize, blocksize, size); } } return uarray2b; }
/*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 ")); }
/* puts each pixel from reader into the 2D array in order */ UArray2_T pixels_to_array(Pnmrdr_T reader, Pnmrdr_mapdata data, FILE *fp) { unsigned pixel, area, i; int row = 0; int col = 0; unsigned *index; UArray2_T uarray2; uarray2 = UArray2_new(data.width, data.height, sizeof(unsigned)); area = data.width * data.height; /* puts each pixel in 2D array */ for (i = 0; i < area; i++) { pixel = Pnmrdr_get(reader); index = (unsigned *)UArray2_at(uarray2, col, row); *index = pixel; col++; /* at end of row, reset column to 0 */ if ((unsigned)col >= data.width) { col = 0; /* moves down 1 row */ row++; } } if (fp != NULL) { fclose(fp); } Pnmrdr_free(&reader); return uarray2; }
/*creates and returns a new Word_image with the given width and height creates a new UArray2 with space for uint64_t*/ Word_image Word_image_new(unsigned width, unsigned height) { Word_image image = NEW(image); image->width = width; image->height = height; image->words = UArray2_new(width, height, sizeof(uint64_t)); return image; }
/* initialize each block within the shell UArray2 */ void make_arrs(int col, int row, UArray2_T a, void *val, void *cl) { (void) col; (void) row; (void) a; struct element_size *e_s = cl; UArray2_T *inner; inner = val; *inner = UArray2_new(e_s->blocksize, e_s->blocksize, e_s->size); }
void make_sub_arrays(T array2b, int big_width, int big_height) { UArray2_T *location ; for (int row = 0; row < big_height; row++) { for (int col = 0; col < big_width; col ++) { UArray2_T uarray2 = UArray2_new(array2b->blocksize, array2b->blocksize,array2b->size); location = UArray2_at(array2b->blockarray, col, row); *location = uarray2; } } }
/* 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; }
extern T UArray2b_new (int width, int height, int size, int blocksize) { T array2b; NEW(array2b); UArray2_T blockarray; int big_width = (int)ceil((float)width / (float)blocksize); int big_height = (int)ceil((float)height / (float)blocksize); blockarray = UArray2_new(big_width, big_height, sizeof(UArray2_T)); array2b->blockarray = blockarray; array2b->width = width; array2b->height = height; array2b->blocksize = blocksize; array2b->size = size; make_sub_arrays(array2b, big_width, big_height); return array2b; }
//create using a 2d array containing a T array of UArray2_T T UArray2b_new(int width, int height, int size, int blocksize) { T blockArray = malloc(sizeof(*blockArray)); blockArray->height = height; blockArray->width = width; blockArray->blocksize = blocksize; int bwidth = width/blocksize; if (width%blocksize != 0) bwidth++; int bheight = height/blocksize; if (height%blocksize != 0) bheight++; blockArray->blocks = UArray2_new(bwidth, bheight, sizeof(UArray_T)); for (int i=0; i<bwidth; i++) { for (int j=0; j<bheight; j++) { UArray_T *pblock = UArray2_at(blockArray->blocks, i, j); *pblock = UArray_new(blocksize*blocksize, size); } } return blockArray; }
/* main() * * Main function for the program. Takes a file containing the puzzle to be * checked as an argument or from stdin and creates a new 9x9 2-Dimensional * array to store the solution while it is being examined. Calls functions to * read in the solution and check the puzzle. Calls exit(0) if the program * returns from the check function successfully to indicate that the puzzle was * indeed correct. */ int main(int argc, char *argv[]) { FILE *srcfile; UArray2_T solution = UArray2_new(9, 9, sizeof(int)); assert(argc <= 2); if (argc == 1) { srcfile = stdin; assert(srcfile != NULL); } else { srcfile = fopen (argv[1], "rb"); assert(srcfile != NULL); } read_in_solution(solution, srcfile); fclose(srcfile); check_solution(solution); UArray2_free(&solution); exit(0); }