From 82c7c1b9ee5603a041e9b0c9a7b18877f672f6fa Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Fri, 23 Apr 2021 19:28:52 -0400 Subject: [PATCH] Correct handling of greyscale (P5) bitmap images as input --- documents/references.md | 19 ++++++-------- part_1/image.cu | 58 +++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/documents/references.md b/documents/references.md index 9af7096..0962eb9 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 ee9c2b1..15dd08a 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); } } -- GitLab