rawls_v1.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <string.h>
  2. #include <iostream>
  3. #include <math.h>
  4. #include <tuple>
  5. namespace rawls_v1{
  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_v1::Clamp(255.f * rawls_v1::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. bool saveAsRAWLS(unsigned width, unsigned height, unsigned nbChanels, std::string comments, float* buffer, std::string outfileName);
  40. /*
  41. * Read `.rawls` image and fill buffer pass as parameter
  42. *
  43. * `float*` is pointer to data buffer and needed to be deleted
  44. */
  45. float* getPixelsRAWLS(std::string filename);
  46. /*
  47. * Returns comments from `.rawls` format
  48. */
  49. std::string getCommentsRAWLS(std::string filename);
  50. /*
  51. * Returns tuple with <width, height, channels> information of image
  52. */
  53. std::tuple<unsigned, unsigned, unsigned> getDimensionsRAWLS(std::string filename);
  54. /*
  55. * Returns tuple with <width, height, channels, buffer*> information of image
  56. *
  57. * `float*` is pointer to data buffer and needed to be deleted
  58. */
  59. std::tuple<unsigned, unsigned, unsigned, float*> getDataRAWLS(std::string filename);
  60. };