From e98c381d4f413137743dd6e5c31bf56ee2dde3f0 Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Wed, 13 Sep 2017 13:43:52 -0500 Subject: [PATCH] Changed image.h to allow loading of HDR data and conversion between data types --- stim/image/bmp.h | 3 +++ stim/image/image.h | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/stim/image/bmp.h b/stim/image/bmp.h index b80a88b..e444dcb 100644 --- a/stim/image/bmp.h +++ b/stim/image/bmp.h @@ -1,6 +1,9 @@ #ifndef STIM_BMP_H #define STIM_BMP_H +#include +#include + namespace stim { #pragma pack(1) typedef unsigned int DWORD; diff --git a/stim/image/image.h b/stim/image/image.h index c74167e..a7bde6b 100644 --- a/stim/image/image.h +++ b/stim/image/image.h @@ -14,7 +14,7 @@ #endif #include #include -#include //use limits and remove the MIN and MAX macros +#include //use limits and remove the MIN and MAX macros #include #include #include @@ -276,8 +276,9 @@ public: allocate(cols, rows, channels); //allocate space for the image size_t img_bytes = bytes(); unsigned char* cv_ptr = (unsigned char*)cvImage.data; - if (C() == 1) //if this is a single-color image, just copy the data - type_copy(cv_ptr, img, size()); + //if (C() == 1) //if this is a single-color image, just copy the data + // type_copy(cv_ptr, img, size()); + memcpy(img, cv_ptr, bytes()); if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving from_opencv(cv_ptr, X(), Y()); #else @@ -342,7 +343,6 @@ public: cv::imwrite(filename, cvImage); free(buffer); #else - stim::filename file(filename); if (file.extension() == "ppm") save_netpbm(filename); else if (file.extension() == "bmp") @@ -354,6 +354,23 @@ public: #endif } + /// Returns an image cast to the specified format + template + image convert() { + + image new_image(R[1], R[2], R[0]); //create a new image with the destination data type + + size_t ni = R[0] * R[1] * R[2]; //calculate the number of data points in the image + + double inmax = (std::numeric_limits::max)(); //get the maximum value for the input image + double outmax = (std::numeric_limits::max)(); //get the maximum value for the output image + for (size_t i = 0; i < ni; i++) { //for each pixel in the image + if (img[i] > outmax) new_image(i) = outmax; //if the source pixel is greater than the maximum destination pixel, set the output to maximum + else new_image(i) = img[i]; //otherwise, copy the source value and cast it to the destination value + } + return new_image; + } + void set_interleaved(T* buffer, size_t width, size_t height, size_t channels){ allocate(width, height, channels); memcpy(img, buffer, bytes()); -- libgit2 0.21.4