rawls.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <string.h>
  2. #include <iostream>
  3. #include <math.h>
  4. #include <tuple>
  5. namespace rawls{
  6. /*
  7. * Gamma conversion functions
  8. */
  9. inline float GammaCorrect(float value) {
  10. if (value <= 0.0031308f) return 12.92f * value;
  11. return 1.055f * std::pow(value, (float)(1.f / 2.4f)) - 0.055f;
  12. }
  13. template <typename T, typename U, typename V>
  14. inline T Clamp(T val, U low, V high) {
  15. if (val < low)
  16. return low;
  17. else if (val > high)
  18. return high;
  19. else
  20. return val;
  21. }
  22. inline bool HasExtension(const std::string &value, const std::string &ending) {
  23. if (ending.size() > value.size()) return false;
  24. return std::equal(
  25. ending.rbegin(), ending.rend(), value.rbegin(),
  26. [](char a, char b) { return std::tolower(a) == std::tolower(b); });
  27. }
  28. //#define GAMMA_CONVERT(v) (float) rawls::Clamp(255.f * rawls::GammaCorrect(v) + 0.5f, 0.f, 255.f)
  29. inline float GammaConvert(float value){
  30. return rawls::Clamp(255.f * rawls::GammaCorrect(value) + 0.5f, 0.f, 255.f);
  31. }
  32. /*
  33. * Save and write functions
  34. */
  35. bool convertToPPM(std::string imageName, std::string outfileName);
  36. bool convertToPNG(std::string imageName, std::string outfileName);
  37. bool saveAsPPM(unsigned width, unsigned height, unsigned nbChanels, float* buffer, std::string outfileName);
  38. bool saveAsPNG(unsigned width, unsigned height, unsigned nbChanels, float* buffer, std::string outfileName);
  39. /*
  40. * Read `.rawls` image and fill buffer pass as parameter
  41. *
  42. * `float*` is pointer to data buffer and needed to be deleted
  43. */
  44. float* getPixelsRAWLS(std::string filename);
  45. /*
  46. * Returns tuple with <width, height, channels> information of image
  47. */
  48. std::tuple<unsigned, unsigned, unsigned> getDimensionsRAWLS(std::string filename);
  49. /*
  50. * Returns tuple with <width, height, channels, buffer*> information of image
  51. *
  52. * `float*` is pointer to data buffer and needed to be deleted
  53. */
  54. std::tuple<unsigned, unsigned, unsigned, float*> getDataRAWLS(std::string filename);
  55. };