rgb_to_hsv.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "rgb_to_hsv.h"
  9. template <typename R,typename H>
  10. IGL_INLINE void igl::rgb_to_hsv(const R * rgb, H * hsv)
  11. {
  12. // http://en.literateprograms.org/RGB_to_HSV_color_space_conversion_%28C%29
  13. R rgb_max = 0.0;
  14. R rgb_min = 1.0;
  15. rgb_max = (rgb[0]>rgb_max?rgb[0]:rgb_max);
  16. rgb_max = (rgb[1]>rgb_max?rgb[1]:rgb_max);
  17. rgb_max = (rgb[2]>rgb_max?rgb[2]:rgb_max);
  18. rgb_min = (rgb[0]<rgb_min?rgb[0]:rgb_min);
  19. rgb_min = (rgb[1]<rgb_min?rgb[1]:rgb_min);
  20. rgb_min = (rgb[2]<rgb_min?rgb[2]:rgb_min);
  21. //hsv[2] = rgb_max;
  22. hsv[2] = rgb_max;
  23. if(hsv[2] == 0)
  24. {
  25. hsv[0]=hsv[1]=0;
  26. return;
  27. }
  28. // normalize
  29. R rgb_n[3];
  30. rgb_n[0] = rgb[0]/hsv[2];
  31. rgb_n[1] = rgb[1]/hsv[2];
  32. rgb_n[2] = rgb[2]/hsv[2];
  33. // Recomput max min?
  34. rgb_max = 0;
  35. rgb_max = (rgb_n[0]>rgb_max?rgb_n[0]:rgb_max);
  36. rgb_max = (rgb_n[1]>rgb_max?rgb_n[1]:rgb_max);
  37. rgb_max = (rgb_n[2]>rgb_max?rgb_n[2]:rgb_max);
  38. rgb_min = 1;
  39. rgb_min = (rgb_n[0]<rgb_min?rgb_n[0]:rgb_min);
  40. rgb_min = (rgb_n[1]<rgb_min?rgb_n[1]:rgb_min);
  41. rgb_min = (rgb_n[2]<rgb_min?rgb_n[2]:rgb_min);
  42. hsv[1] = rgb_max - rgb_min;
  43. if(hsv[1] == 0)
  44. {
  45. hsv[0] = 0;
  46. return;
  47. }
  48. rgb_n[0] = (rgb_n[0] - rgb_min)/(rgb_max - rgb_min);
  49. rgb_n[1] = (rgb_n[1] - rgb_min)/(rgb_max - rgb_min);
  50. rgb_n[2] = (rgb_n[2] - rgb_min)/(rgb_max - rgb_min);
  51. // Recomput max min?
  52. rgb_max = 0;
  53. rgb_max = (rgb_n[0]>rgb_max?rgb_n[0]:rgb_max);
  54. rgb_max = (rgb_n[1]>rgb_max?rgb_n[1]:rgb_max);
  55. rgb_max = (rgb_n[2]>rgb_max?rgb_n[2]:rgb_max);
  56. rgb_min = 1;
  57. rgb_min = (rgb_n[0]<rgb_min?rgb_n[0]:rgb_min);
  58. rgb_min = (rgb_n[1]<rgb_min?rgb_n[1]:rgb_min);
  59. rgb_min = (rgb_n[2]<rgb_min?rgb_n[2]:rgb_min);
  60. if (rgb_max == rgb_n[0]) {
  61. hsv[0] = 0.0 + 60.0*(rgb_n[1] - rgb_n[2]);
  62. if (hsv[0] < 0.0) {
  63. hsv[0] += 360.0;
  64. }
  65. } else if (rgb_max == rgb_n[1]) {
  66. hsv[0] = 120.0 + 60.0*(rgb_n[2] - rgb_n[0]);
  67. } else /* rgb_max == rgb_n[2] */ {
  68. hsv[0] = 240.0 + 60.0*(rgb_n[0] - rgb_n[1]);
  69. }
  70. }
  71. template <typename DerivedR,typename DerivedH>
  72. IGL_INLINE void igl::rgb_to_hsv(
  73. const Eigen::PlainObjectBase<DerivedR> & R,
  74. Eigen::PlainObjectBase<DerivedH> & H)
  75. {
  76. assert(R.cols() == 3);
  77. H.resizeLike(R);
  78. for(typename DerivedR::Index r = 0;r<R.rows();r++)
  79. {
  80. typename DerivedR::Scalar rgb[3];
  81. rgb[0] = R(r,0);
  82. rgb[1] = R(r,1);
  83. rgb[2] = R(r,2);
  84. typename DerivedH::Scalar hsv[] = {0,0,0};
  85. rgb_to_hsv(rgb,hsv);
  86. H(r,0) = hsv[0];
  87. H(r,1) = hsv[1];
  88. H(r,2) = hsv[2];
  89. }
  90. }
  91. #ifdef IGL_STATIC_LIBRARY
  92. // Explicit template instantiation
  93. template void igl::rgb_to_hsv<float, double>(float const*, double*);
  94. template void igl::rgb_to_hsv<double, double>(double const*, double*);
  95. template void igl::rgb_to_hsv<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  96. template void igl::rgb_to_hsv<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  97. template void igl::rgb_to_hsv<Eigen::Matrix<float, 64, 3, 1, 64, 3>, Eigen::Matrix<float, 64, 3, 1, 64, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<float, 64, 3, 1, 64, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<float, 64, 3, 1, 64, 3> >&);
  98. #endif