diff --git a/documents/references.md b/documents/references.md index 9af7096c45b1381096adaacd5aa6fc0c9db3ee6e..0962eb92070342fd4de1395fabb7d5d3f91dea87 100644 --- a/documents/references.md +++ b/documents/references.md @@ -56,6 +56,9 @@ Various references used in project. * Setting Double Precision - <https://stackoverflow.com/a/16280182> * Saving Precision without Immediate Output to Console - <https://stackoverflow.com/a/48945767> +#### Error - Files Compiling without "Standard" C++ Library Access +<https://stackoverflow.com/a/10271117> + ## CUDA References @@ -69,15 +72,9 @@ Various references used in project. <https://cuda-tutorial.readthedocs.io/en/latest/tutorials/tutorial01/> -## General Errors - -#### Files Compiling without "Standard" C++ Library Access -<https://stackoverflow.com/a/10271117> - - - - - -# c++ manipulate pixels in image +## Other +### Manipulating Image Pixels +<https://www.scratchapixel.com/lessons/digital-imaging/simple-image-manipulations> -https://www.scratchapixel.com/lessons/digital-imaging/simple-image-manipulations +### Portable Bitmap Formats +<https://en.wikipedia.org/wiki/Netpbm> diff --git a/part_1/image.cu b/part_1/image.cu index ee9c2b13561eb8713eb82c7f80c3be87c72986f6..15dd08a9aefc42f22b71e9f79c953a1bc837866b 100644 --- a/part_1/image.cu +++ b/part_1/image.cu @@ -91,15 +91,25 @@ void Image::import_image_file() { logger.error("Failed to open file \"" + input_path + "\""); } else { // File opened. Grab data. - std::string header; - input_file >> header; - // if (strcmp(header.c_str(), "P6") != 0) { - // // throw("Can't read input file"); - // logger.error("Wrong file type, oops: "); - // logger.error(header); - // logger.error(input_path); - // logger.error(""); - // } + std::string header_format; + input_file >> header_format; + + // Check image data format type. + if (strcmp(header_format.c_str(), "P6") == 0) { + // P6 indicates standard "binary RGB format". + logger.debug("Image is binary RGB format."); + + } else if (strcmp(header_format.c_str(), "P5") == 0) { + // P5 indicates standard "binary greyscale format". + logger.debug("Image is binary greyscale format."); + + } else { + // throw("Can't read input file"); + logger.error(""); + logger.error("Image: \"" + input_path + "\""); + logger.error("Unhandled image format type: " + header_format); + return; + } // Save image values to class. input_file >> width >> height >> bit_depth; @@ -110,13 +120,29 @@ void Image::import_image_file() { // 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; + // Read pixels based on image format. + if (strcmp(header_format.c_str(), "P6") == 0) { + // Binary RBG format. + // Read each pixel one by one and convert RGB channel bytes to floats. + unsigned char pixel[3]; + for (int index = 0; index < width * height; ++index) { + input_file.read(reinterpret_cast<char *>(pixel), 3); + pixel_arr[index].r = pixel[0] / 255.f; + pixel_arr[index].g = pixel[1] / 255.f; + pixel_arr[index].b = pixel[2] / 255.f; + } + } else if (strcmp(header_format.c_str(), "P5") == 0) { + // Binary RBG format. + // Read each pixel one by one and convert bytes to floats. + unsigned char* pixel; + pixel = (unsigned char*)calloc(1, sizeof(unsigned char)); + for (int index = 0; index < width * height; ++index) { + input_file.read(reinterpret_cast<char *>(pixel), 1); + pixel_arr[index].r = *pixel / 255.f; + pixel_arr[index].g = *pixel / 255.f; + pixel_arr[index].b = *pixel / 255.f; + } + free(pixel); } }