/* * pgmio.c * * Created on: Oct 31, 2019 * Author: lutton */ #include #include #include #include #include "pgmio.h" file_node_t * get_pgm_files(char path[]){ char end_point = path[strlen(path) - 1]; char file_joiner[] = "/"; if(end_point == '/'){ file_joiner[0] = '\0'; } DIR *d; struct dirent *dir; char file_name[256] = ""; int file_count = 0; int name_length = 0; char file_extn[10] = ""; char pgm_extn[10] = ".pgm"; int file_idx = 0; d = opendir(path); file_node_t *head = NULL; file_node_t *current = NULL; if(d){ // attempt to initialize first node while((dir = readdir(d)) != NULL){ strncpy(file_name, dir->d_name,256); name_length = strlen(file_name); if(name_length > 4){ for(int i = 0; i < 4; i++){ file_extn[i] = file_name[name_length - 4 + i]; } if(strcmp(pgm_extn, file_extn) == 0){ head = (file_node_t *) malloc(sizeof(file_node_t)); strcpy(head->file_name, file_name); strcat(strcat(strcpy(head->file_path, path), file_joiner), file_name); head->next = NULL; current = head; break; } } } while((dir = readdir(d)) != NULL){ strncpy(file_name, dir->d_name,256); name_length = strlen(file_name); if(name_length > 4){ for(int i = 0; i < 4; i++){ file_extn[i] = file_name[name_length - 4 + i]; } if(strcmp(pgm_extn, file_extn) == 0){ current->next = (file_node_t *) malloc(sizeof(file_node_t)); strcpy(current->next->file_name, file_name); strcat(strcat(strcpy(current->next->file_path, path), file_joiner), file_name); current->next->next = NULL; current = current->next; } } } } else { printf("Error: directory %s not found.\n", path); } closedir(d); return head; } int * get_pgm_dimensions(char path[]){ int max_line_size = 256; char delim[2] = " "; // line 1 not used char line1[max_line_size]; // dimensions stored in line 2 char line2[max_line_size]; FILE* img_file = fopen(path,"rb"); if(!img_file){ printf("File not found.\n"); return NULL; } fgets(line1, max_line_size, img_file); fgets(line2, max_line_size, img_file); fclose(img_file); char *c_token = strtok(line2, delim); char *r_token = strtok(NULL, delim); char *z_token = strtok(NULL, delim); int *dims = (int *) malloc(sizeof(int) * 3); dims[DIM_ROWS] = atoi(r_token); dims[DIM_COLUMNS] = atoi(c_token); dims[DIM_SLICES] = atoi(z_token); return dims; } int get_pgm_size(char path[]){ int *dims = get_pgm_dimensions(path); int sz = dims[0] * dims[1] * dims[2]; return sz; } int get_pgm_dimension(char path[], dim_idx i){ int *dims = get_pgm_dimensions(path); return dims[i]; } unsigned char * pgm_read(char path[]){ int sz = get_pgm_size(path); unsigned char *img = (unsigned char *) malloc(sizeof(char) * sz); // 3 header lines char line1[256]; char line2[256]; char line3[256]; FILE* img_file = fopen(path,"rb"); if(!img_file){ printf("File not found.\n"); free(img); return NULL; } fgets(line1, 256, img_file); fgets(line2, 256, img_file); fgets(line3, 256, img_file); fread(img, sizeof(char), sz, img_file); fclose(img_file); return img; } int pgm_write(unsigned char *img, char path[], int dims[]){ char line1[] = "P5\n"; char line2[256]; sprintf(line2, "%d %d %d\n", dims[0], dims[1], dims[2]); char line3[] = "255\n"; int sz = dims[0] * dims[1] * dims[2]; FILE *img_file = fopen(path, "wb"); fwrite(line1, sizeof(char), strlen(line1), img_file); fwrite(line2, sizeof(char), strlen(line2), img_file); fwrite(line3, sizeof(char), strlen(line3), img_file); fwrite(img, sizeof(char), sz, img_file); fclose(img_file); return 0; }