README.md.txt contains the instruction for this assignment. files.zip contains the required materials for this assignment. testcase02.c.txt contains the format of the test functions that should be...

1 answer below »
README.md.txt contains the instruction for this assignment. files.zip contains the required materials for this assignment. testcase02.c.txt contains the format of the test functions that should be written. a-31.zip & a-41.zip contains the previous assignments that you can take functions from (and please do if you can).


When compiling, please use the makefile provided in the files.zip to compile since it converts all warnings into errors. So the code should be warning free.


Please write the code as easy as possible to understand. No hard functions/headers please. And please write the comment of the algorithms & methods used so that I can study over it. Thank you so much for your effort.


files/.Rhistory files/t0imgr.h #include #include /* Structure type that encapsulates our image: 2D array. * the rows represent the indices of the main array, * the cols represent the indices of the arrays pointed to by the pointers * in the elements of the main array. */ typedef struct { uint8_t** pixels; unsigned int rows; unsigned int cols; unsigned int reserved_rows; unsigned int reserved_cols; } imgr_t; /* A type for returning status codes */ typedef enum { IMGR_OK, IMGR_BADCOL, IMGR_BADROW, IMGR_BADALLOC, IMGR_EMPTY } imgr_result_t; /* ALL THESE FUNCTIONS REQUIRE A VALID IMGR_T POINTER AS THEIR FIRST PARAMETER. THEY SHOULD FAIL ON ASSERTION IF THIS POINTER IS NULL */ /* task 01 */ // Create a new imgr_t with initial size rows, cols, // and their reserved counterpart (initialize rows and reserved_rows // to be the same, and cols and reserved_cols to be the same). // If successful (i.e. memory allocation succeeds), returns a pointer to a // newly-allocated imgr_t. If unsuccessful, returns a null pointer. imgr_t* imgr_create(unsigned int rows, unsigned int cols); // Frees all memory allocated for im. If the pointer is null, do // nothing. If the im->pixels is null, do not attempt to free it. void imgr_destroy(imgr_t* im); /* task 02 */ // Append val to the end of the 2D array pixels. If successful, return 0, else return 1. // Use preallocation if there is not enough space reserved in the 2D array // i.e. if all reserved_rows x reserved_cols elements are filled up, // - double the size of rows if expand_row != 0 // - double the size of cols if expand_row = 0 // both options need to be tested in your t0.c. // return IMGR_OK if successful. // return IMGR_BADALLOC if `realloc()` failed. imgr_result_t imgr_append(imgr_t* im, int expand_row, int val); /* task 03 */ // Remove the point at index (i,j) (row, col) from the 2D array pixels, // reducing the number of elements // stored in the 2D array by one. The order of points in the array may change. // Use unstable remove. // If successful, return IMGR_OK, else return IMGR_BADROW if you cannot find the row i // or IMGR_BADCOL if you could find row i but couldn't find col j. imgr_result_t imgr_remove(imgr_t* im, unsigned int i, unsigned int j); /* task 04 */ // Save the 2D entire array im into a file called 'filename' in a JSON // text file array file format that can be loaded by // imgr_load_json(). Returns zero on success, or a non-zero error // code on failure. Arrays of length 0 should produce an output file // containing an empty array. // // Make sure you validate the parameters before you use them. // // The JSON output should be human-readable. // // Examples: // // The following line is a valid JSON 2D array: // [ [ 100, 200, 300 ], // [ 10, 20, 15 ] ] // // The following lines are a valid JSON array: // [ // [ // 100, // 200, // 300 // ], // [ // 10, // 20, // 15 // ] // ] int imgr_save_json(imgr_t* im, const char* filename); // Load a new 2D array from the file called 'filename', that was // previously saved using imgr_save_json(). The file may contain an array // of length 0. Returns a pointer to a newly-allocated imgr_t on // success (even if that array has length 0), or NULL on failure. // // Make sure you validate the parameter before you use it. imgr_t* imgr_load_json(const char* filename); files/Makefile # -Wextra turns on even more warnings # -Werror treats warnings as if they were errors (stopping compilation) # -Wfatal-errors causes compilation to stop after the first error # -g adds debugging information to the file CFLAGS := -Werror -Wfatal-errors -g t0: t0.c t0imgr.c gcc $(CFLAGS) -o $@ t0.c t0imgr.c submit: rm -f a.zip zip a.zip t0.c t0imgr.c clean: rm -f t0 *.o files/t0imgr.c #include "t0imgr.h" files/t0.c // author: // date: // input: // output: // description: __MACOSX/files/._t0.c t1.c // author: // date: July 19th 2021 // input: int // output: int // description: testing functions declared in t1img.h #include #include "t1img.h" #include img_t* img_init(unsigned int row, unsigned int col) { img_t* im = (img_t*)malloc(sizeof(img_t)); im->rows = row; im->cols = col; im->pixels = (uint8_t**)malloc(sizeof(uint8_t*) * im->rows); for (int i = 0; i < im-="">rows; i++) { im->pixels[i] = (uint8_t*)malloc(sizeof(uint8_t) * im->cols); } return im; } void img_print(img_t* im) { for (unsigned int i = 0; i < im-="">rows; i++) { for (unsigned int j = 0; j < im-="">cols; j++) printf("%u ", im->pixels[i][j]); printf("\n"); } } void img_delete(img_t* im) { for (unsigned int i = 0; i < im-="">rows; i++) free(im->pixels[i]); free(im->pixels); free(im); } int main() { printf("Initializing img_t instance...\n"); img_t* myIm = img_init(4, 5); printf("img_t myIm created with %u rows and %u columns\n", myIm->rows, myIm->cols); //Filling up the pixels array of myIm for (unsigned int i = 0; i < myim-="">rows; i++) { for (unsigned int j = 0; j < myim-="">cols; j++) myIm->pixels[i][j] = (uint8_t)((i + 1) * (j + 1) * 10); //Generating some numbers } printf("Pixel values assigned to myIm->pixels array. The values are:\n"); img_print(myIm); int code = img_save_binary(myIm, "MyBinaryFile.bin"); printf("img_save_binary exited successfully with code: %d\n", code); img_t* myIm2 = img_load_binary("MyBinaryFile.bin"); printf("img_load_binary exited successfully and loaded an img_t file with pixels:\n"); img_print(myIm2); img_delete(myIm); img_delete(myIm2); return 0; } t1img.c #include "t1img.h" #include int img_save_binary(img_t* im, const char* filename) { FILE* fp; uint8_t* buffer = (uint8_t*)malloc(im->rows * im->cols * sizeof(uint8_t)); for (unsigned int i = 0; i < im-="">rows; i++) { for (unsigned int j = 0; j < im-="">cols; j++) { *(buffer + i * im->cols + j) = im->pixels[i][j]; } } fp = fopen(filename, "wb"); if (fp == NULL) { printf("Could not open file for writing!\n"); return 1; } if (fwrite(&(im->rows), sizeof(unsigned int), 1, fp) != 1) { printf("Could not write in file! rows\n"); fclose(fp); return 2; } if (fwrite(&(im->cols), sizeof(unsigned int), 1, fp) != 1) { printf("Could not write in file! cols\n"); fclose(fp); return 2; } if (fwrite(buffer, sizeof(uint8_t), im->rows * im->cols, fp) != im->rows*im->cols) { printf("Could not write in file!\n"); fclose(fp); return 2; } free(buffer); fclose(fp); return 0; } img_t* img_load_binary(const char* filename) { FILE* fp; fp = fopen(filename, "rb"); if (fp == NULL) { printf("Could not open file for reading!\n"); return NULL; } img_t* im = (img_t*)malloc(sizeof(img_t)); if (fread(&(im->rows), sizeof(unsigned int), 1, fp) != 1) { printf("Could not read from file!\n"); fclose(fp); return NULL; } if (fread(&(im->cols), sizeof(unsigned int), 1, fp) != 1) { printf("Could not read from file!\n"); fclose(fp); return NULL; } if (im->rows < 0="" ||="" im-="">cols < 0)="" {="" printf("invalid="" number="" of="" rows/columns!\n");="" return="" null;="" }="" uint8_t*="" buffer="(uint8_t*)malloc(sizeof(uint8_t)" *="" im-="">rows * im->cols); im->pixels = (uint8_t**)malloc(sizeof(uint8_t*) * im->rows); for (unsigned int i = 0; i < im-="">rows; i++) { im->pixels[i] = (uint8_t*)malloc(sizeof(uint8_t) * im->cols); } if (fread(buffer, sizeof(uint8_t), im->rows*im->cols, fp) != im->rows * im->cols) { printf("Could not read from file!\n"); fclose(fp); return NULL; } for (unsigned int i = 0; i < im-="">rows; i++) { for (unsigned int j = 0; j < im-="">cols; j++) { im->pixels[i][j] = *(buffer + i * im->cols + j); } } fclose(fp); return im; } t1img.h //#pragma once #include #include /* Structure type that encapsulates our image: 2D array. * the rows represent the indices of the main array, * the cols represent the indices of the arrays pointed to by the pointers * in the elements of the main array. */ typedef struct { uint8_t** pixels; unsigned int rows; unsigned int cols; } img_t; /* task 01 */ /* Save the entire 2D array in im into a file called 'filename' in a binary file format that can be loaded by img_load_binary(). Returns zero on success, or a non-zero error code on failure. Arrays of length 0 should produce an output file containing an empty array. Make sure you validate the parameters before you use them. */ int img_save_binary(img_t* im, const char* filename); /* Load a new 2D array from the file called 'filename', that was previously saved using img_save_binary(). Returns a pointer to a newly-allocated img_t on success, or NULL on failure. Make sure you validate the parameter before you use it. */ img_t* img_load_binary(const char* filename); t2.c // author: // date: July 19th 2021 // input: int // output: int // description: testing functions declared in t2img.h #include #include "t2img.h" #include img_t* img_init(unsigned int row, unsigned int col) { img_t* im = (img_t*)malloc(sizeof(img_t)); im->rows = row; im->cols = col; im->pixels = (uint8_t**)malloc(sizeof(uint8_t*) * im->rows); for (int i = 0; i < im-="">rows; i++) { im->pixels[i] = (uint8_t*)malloc(sizeof(uint8_t) * im->cols); } return im; } void img_print(img_t*
Answered 2 days AfterJul 26, 2021

Answer To: README.md.txt contains the instruction for this assignment. files.zip contains the required...

Kamal answered on Jul 29 2021
132 Votes
// author:
// date:
// input:
// output:
// de
scription:
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here