diff --git a/documents/references.md b/documents/references.md index 2107c290b00d65b69648cf2b7c55942f54766181..2090fc06131a109dabe655eb814f558adac23a7b 100644 --- a/documents/references.md +++ b/documents/references.md @@ -11,32 +11,29 @@ Various references used in project. "A Tour of C++", by Bjarne Stroustrup ### File and Directory Handling -#### Checking if File Path Exists -<https://stackoverflow.com/a/27588225> -#### Checking if Path is File or Dir -<https://stackoverflow.com/a/43281413> +#### Path Management +* Check if file path exists - <https://stackoverflow.com/a/27588225> +* Check if path is file or directory - <https://stackoverflow.com/a/43281413> +* Get file name from path - <https://stackoverflow.com/a/24386991> +* Path value to string - <https://stackoverflow.com/a/45401869> -#### Getting All Files in Folder -<https://stackoverflow.com/a/612176> +#### Folder Management +* Check if dir exists - <https://stackoverflow.com/a/18101042> +* Create directory - <https://www.geeksforgeeks.org/create-directoryfolder-cc-program/> +* Get all files in folder - <https://stackoverflow.com/a/612176> -#### Getting File Name from Path -<https://stackoverflow.com/a/24386991> - -#### Path Value to Str -<https://stackoverflow.com/a/45401869> ### Other #### C++ Strings -<https://stackoverflow.com/a/3535904> - -### C++ Classes -<https://www.w3schools.com/cpp/cpp_classes.asp> +* What is a C++ string - <https://stackoverflow.com/a/3535904> +* String splitting - <https://stackoverflow.com/a/14266139> -### Structs Vs Classes in C++ -<https://stackoverflow.com/a/36917400> +#### C++ Classes +* General Class Syntax - <https://www.w3schools.com/cpp/cpp_classes.asp> +* Structs Vs Classes in C++ - <https://stackoverflow.com/a/36917400> -### Type Casting +#### Type Casting <https://en.cppreference.com/w/cpp/language/static_cast> #### Reading In & Manipulating Image Files diff --git a/part_1/main.cu b/part_1/main.cu index 8b9364e76e13f299a1ad13c1a98f98c1b84d2cc7..5889de2be8726610ebbd004a1f5833b6d5b1992b 100644 --- a/part_1/main.cu +++ b/part_1/main.cu @@ -12,6 +12,8 @@ #include <iostream> #include <iterator> #include <string> +#include <sys/stat.h> +#include <sys/types.h> #include <vector> // User Import Headers. @@ -71,6 +73,92 @@ class Image { int bit_depth; Rgb *pixel_arr; // Array of pixel data. + /** + * Calculates class input_path and output_path values. + * + * :param image_path: Path to an image on the system. + */ + void calculate_paths(std::string image_path) { + // std::cout << "\n\n\n\n\nCalculating input/output paths..." << std::endl; + // std::cout << "image_path: " << image_path << std::endl; + + // // Calculate output path location. + output_path = "../output/"; + std::string base_filename = image_path.substr(image_path.find_last_of("/\\") + 1); + + // Grab directory substrings and generate full output path. + size_t pos = 0; + std::string token; + while ((pos = image_path.find("/")) != std::string::npos) { + + // Pull current substring value from input path. + token = image_path.substr(0, image_path.find("/")); + // std::cout << " " << token << std::endl; + image_path.erase(0, pos + 1); + + // Check if directory value is value we care about. + if ( (token != ".") && (token != "..") && (token != "input") ) { + output_path += token + "/"; + } + + // Check if directory exists. + struct stat info; + if ( (stat(output_path.c_str(), &info) != 0) || (! info.st_mode & S_IFDIR)) { + // Directory does not exist. + mkdir(output_path.c_str(), 0777); + } + + } + + output_path += base_filename; + // std::cout << "output_path: " << output_path << std::endl; + + // std::cout << "Input/output paths calculated.\n\n\n\n\n" << std::endl; + } + + + /** + * Imports image file data, using class variable data. + */ + void import_image_file() { + // Open input file. + std::ifstream input_file; + input_file.open(input_path, std::ios::binary | std::ios::in); + + if (! input_file.good()) { + // Failed to open file. + std::cerr << "Failed to open file \"" + input_path + "\"." << std::endl; + } else { + // File opened. Grab data. + std::string header; + input_file >> header; + if (strcmp(header.c_str(), "P6") != 0) { + throw("Can't read input file"); + } + + // Save image values to class. + input_file >> width >> height >> bit_depth; + + // Create and populate image. + // std::cout << "Parsing input image" << std::endl; + pixel_arr = new Rgb[width * height]; + + // Skip empty lines in necessary until we get to the binary data. + input_file.ignore(256, '\n'); + + // Read each pixel one by one and convert bytes to floats. + unsigned char pix[3]; + for (int index = 0; index < width * height; ++index) { + input_file.read(reinterpret_cast<char *>(pix), 3); + pixel_arr[index].r = pix[0] / 255.f; + pixel_arr[index].g = pix[1] / 255.f; + pixel_arr[index].b = pix[2] / 255.f; + } + } + + input_file.close(); + } + public: // Public Variables. @@ -107,10 +195,27 @@ class Image { * Constructor that takes input path to image file. * Output path is determined based on parsing input path. */ - Image(char* user_path) { + Image(std::string user_path) { std::cout << "\nConstructing Image class." << std::endl; - // populate here + std::cout << "user_path: " << user_path << std::endl; + + // Re-validate path for saving to class.. + path_validator_struct* return_struct = validate_path(user_path); + int path_type = return_struct->file_type; + if (return_struct->err_code) { + std::cerr << "Error verifying file type: " << return_struct->err_code.message() << std::endl; + return; + } + free_path_validator_struct(return_struct); + if (path_type == TYPE_FILE) { + input_path = user_path; + calculate_paths(user_path); + import_image_file(); + } else { + std::cerr << "Provided path is not to image file. Class initialization failed." << std::endl; + return; + } std::cout << "Image class populated." << std::endl; } @@ -291,59 +396,9 @@ void process_dir(std::string path_str) { */ void process_file(std::string path_str) { - Image image; + Image image(path_str); image.display_properties(); image.save(); - - // // Open input file. - // std::ifstream input_file; - // input_file.open(path_str, std::ios::binary | std::ios::in); - // if (! input_file.good()) { - // std::cout << "Failed to open file \"" + path_str + "\".\n"; - // } - - // // Calculate output path location. - // std::string base_filename = path_str.substr(path_str.find_last_of("/\\") + 1); - // std::string output_path_str = "../output/" + base_filename; - - // // Open output file. - // std::ofstream output_file; - // output_file.open(output_path_str, std::ios::binary | std::ios::out); - // if (! output_file.good()) { - // std::cout << "Failed to open file \"" + output_path_str + "\".\n"; - // } - - // // Copy file over. - // if (input_file.good() && output_file.good()) { - // // output_file << input_file; - - // // int buffer[100]; - // // while (input_file.read( (char*) &buffer, sizeof(buffer)) ) { - // // output_file.write( (char*) &buffer, sizeof(buffer) ); - // // } - - // // std::copy( - // // std::istreambuf_iterator<char>(input_file), - // // std::istreambuf_iterator<char>( ), - // // std::ostreambuf_iterator<char>(output_file) - // // ); - - // std::string header; - // input_file >> header; - // if (strcmp(header.c_str(), "P6") != 0) { - // throw("Can't read input file"); - // } else { - // printf("header: %s\n\n", header.c_str()); - // } - - // input_file >> width >> height >> bit_depth; - // printf("width: %i\n", width); - // printf("height: %i\n", height); - // printf("bit depth: %i\n", bit_depth); - - // input_file.close(); - // output_file.close(); - // } }