Commit e98c381d4f413137743dd6e5c31bf56ee2dde3f0

Authored by David Mayerich
1 parent 48673e7e

Changed image.h to allow loading of HDR data and conversion between data types

Showing 2 changed files with 24 additions and 4 deletions   Show diff stats
stim/image/bmp.h
1 1 #ifndef STIM_BMP_H
2 2 #define STIM_BMP_H
3 3  
  4 +#include <fstream>
  5 +#include <iostream>
  6 +
4 7 namespace stim {
5 8 #pragma pack(1)
6 9 typedef unsigned int DWORD;
... ...
stim/image/image.h
... ... @@ -14,7 +14,7 @@
14 14 #endif
15 15 #include <vector>
16 16 #include <iostream>
17   -#include <climits> //use limits and remove the MIN and MAX macros
  17 +#include <limits> //use limits and remove the MIN and MAX macros
18 18 #include <typeinfo>
19 19 #include <fstream>
20 20 #include <cstring>
... ... @@ -276,8 +276,9 @@ public:
276 276 allocate(cols, rows, channels); //allocate space for the image
277 277 size_t img_bytes = bytes();
278 278 unsigned char* cv_ptr = (unsigned char*)cvImage.data;
279   - if (C() == 1) //if this is a single-color image, just copy the data
280   - type_copy<unsigned char, T>(cv_ptr, img, size());
  279 + //if (C() == 1) //if this is a single-color image, just copy the data
  280 + // type_copy<T, T>(cv_ptr, img, size());
  281 + memcpy(img, cv_ptr, bytes());
281 282 if(C() == 3) //if this is a 3-color image, OpenCV uses BGR interleaving
282 283 from_opencv(cv_ptr, X(), Y());
283 284 #else
... ... @@ -342,7 +343,6 @@ public:
342 343 cv::imwrite(filename, cvImage);
343 344 free(buffer);
344 345 #else
345   - stim::filename file(filename);
346 346 if (file.extension() == "ppm")
347 347 save_netpbm(filename);
348 348 else if (file.extension() == "bmp")
... ... @@ -354,6 +354,23 @@ public:
354 354 #endif
355 355 }
356 356  
  357 + /// Returns an image cast to the specified format
  358 + template<typename U>
  359 + image<U> convert() {
  360 +
  361 + image<U> new_image(R[1], R[2], R[0]); //create a new image with the destination data type
  362 +
  363 + size_t ni = R[0] * R[1] * R[2]; //calculate the number of data points in the image
  364 +
  365 + double inmax = (std::numeric_limits<T>::max)(); //get the maximum value for the input image
  366 + double outmax = (std::numeric_limits<U>::max)(); //get the maximum value for the output image
  367 + for (size_t i = 0; i < ni; i++) { //for each pixel in the image
  368 + if (img[i] > outmax) new_image(i) = outmax; //if the source pixel is greater than the maximum destination pixel, set the output to maximum
  369 + else new_image(i) = img[i]; //otherwise, copy the source value and cast it to the destination value
  370 + }
  371 + return new_image;
  372 + }
  373 +
357 374 void set_interleaved(T* buffer, size_t width, size_t height, size_t channels){
358 375 allocate(width, height, channels);
359 376 memcpy(img, buffer, bytes());
... ...