ColorSpace.tcc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <assert.h>
  2. #include "cc.h"
  3. namespace OBJREC {
  4. template<class SrcPixelType, class DstPixelType>
  5. void ColorSpace::convert ( NICE::MultiChannelImageT<DstPixelType> & dst, const NICE::MultiChannelImageT<SrcPixelType> & src,
  6. int dstColorSpace, int srcColorSpace, double dstM, double srcM )
  7. {
  8. assert ( ( srcColorSpace >= 0 ) && ( srcColorSpace < NUM_COLORSPACES ) );
  9. assert ( ( dstColorSpace >= 0 ) && ( dstColorSpace < NUM_COLORSPACES ) );
  10. dst.reInitFrom ( src );
  11. if ( ( srcColorSpace == COLORSPACE_RGB ) && ( dstColorSpace == COLORSPACE_HSL ) )
  12. {
  13. assert ( dst.channels() == 3 );
  14. assert ( src.channels() == 3 );
  15. for ( int y = 0 ; y < src.height() ; y++ )
  16. for ( int x = 0 ; x < src.width() ; x++)
  17. {
  18. double R, G, B, H, S, L;
  19. R = ( double ) src.get(x,y,0) / srcM;
  20. G = ( double ) src.get(x,y,1) / srcM;
  21. B = ( double ) src.get(x,y,2) / srcM;
  22. ColorConversion::ccRGBtoHSL ( R, G, B, &H, &S, &L );
  23. dst[0](x,y) = ( DstPixelType ) ( H * dstM );
  24. dst[1](x,y) = ( DstPixelType ) ( S * dstM );
  25. dst[2](x,y) = ( DstPixelType ) ( L * dstM );
  26. }
  27. }
  28. else if ( ( srcColorSpace == COLORSPACE_RGB ) && ( dstColorSpace == COLORSPACE_LAB ) )
  29. {
  30. for ( int y = 0 ; y < src.height() ; y++ )
  31. for ( int x = 0 ; x < src.width() ; x++ )
  32. {
  33. double R, G, B, X, Y, Z, L, a, b;
  34. R = ( double ) src.get(x,y,0) / 255.0;
  35. G = ( double ) src.get(x,y,1) / 255.0;
  36. B = ( double ) src.get(x,y,2) / 255.0;
  37. ColorConversion::ccRGBtoXYZ ( R, G, B, &X, &Y, &Z, 0 );
  38. ColorConversion::ccXYZtoCIE_Lab ( X, Y, Z, &L, &a, &b, 0 );
  39. dst[0](x,y) = ( DstPixelType ) ( L );
  40. dst[1](x,y) = ( DstPixelType ) ( a );
  41. dst[2](x,y) = ( DstPixelType ) ( b );
  42. }
  43. }
  44. else if ( ( srcColorSpace == COLORSPACE_LAB ) && ( dstColorSpace == COLORSPACE_RGB ) )
  45. {
  46. long k = 0;
  47. for ( int y = 0 ; y < src.height() ; y++ )
  48. for ( int x = 0 ; x < src.width() ; x++, k++ )
  49. {
  50. double R, G, B, X, Y, Z, L, a, b;
  51. L = ( double ) src.get(x,y,0);
  52. a = ( double ) src.get(x,y,1);
  53. b = ( double ) src.get(x,y,2);
  54. ColorConversion::ccCIE_LabtoXYZ ( L, a, b, &X, &Y, &Z, 0 );
  55. ColorConversion::ccXYZtoRGB ( X, Y, Z, &R, &G, &B, 0 );
  56. dst[0](x,y) = ( DstPixelType ) ( R );
  57. dst[1](x,y) = ( DstPixelType ) ( G );
  58. dst[2](x,y) = ( DstPixelType ) ( B );
  59. }
  60. }
  61. else if ( ( srcColorSpace == COLORSPACE_RGB ) && ( dstColorSpace == COLORSPACE_LMS ) )
  62. {
  63. long k = 0;
  64. for ( int y = 0 ; y < src.height() ; y++ )
  65. for ( int x = 0 ; x < src.width() ; x++, k++ )
  66. {
  67. double R, G, B, X, Y, Z, L, M, S;
  68. R = ( double ) src.get(x,y,0) / srcM;
  69. G = ( double ) src.get(x,y,1) / srcM;
  70. B = ( double ) src.get(x,y,2) / srcM;
  71. ColorConversion::ccRGBtoXYZ ( R, G, B, &X, &Y, &Z, 0 );
  72. ColorConversion::ccXYZtoLMS ( X, Y, Z, &L, &M, &S );
  73. dst[0](x,y) = ( DstPixelType ) ( L );
  74. dst[1](x,y) = ( DstPixelType ) ( M );
  75. dst[2](x,y) = ( DstPixelType ) ( S );
  76. }
  77. }
  78. else if ( ( srcColorSpace == COLORSPACE_RGB ) && ( dstColorSpace == COLORSPACE_OPP ) )
  79. {
  80. long k = 0;
  81. for ( int y = 0 ; y < src.height() ; y++ )
  82. for ( int x = 0 ; x < src.width() ; x++, k++ )
  83. {
  84. double R, G, B, X, Y, Z, L, M, S, Lum, LM, SLM;
  85. R = ( double ) src.get(x,y,0) / srcM;
  86. G = ( double ) src.get(x,y,1) / srcM;
  87. B = ( double ) src.get(x,y,2) / srcM;
  88. ColorConversion::ccRGBtoXYZ ( R, G, B, &X, &Y, &Z, 0 );
  89. ColorConversion::ccXYZtoLMS ( X, Y, Z, &L, &M, &S );
  90. ColorConversion::ccLMStoOPP ( L, M, S, &Lum, &LM, &SLM );
  91. dst[0](x,y) = ( DstPixelType ) ( Lum );
  92. dst[1](x,y) = ( DstPixelType ) ( LM );
  93. dst[2](x,y) = ( DstPixelType ) ( SLM );
  94. #ifdef DEBUG
  95. fprintf ( stderr, "R:%.4f G:%.4f B:%.4f X:%.4f Y:%.4f Z:%.4f L:%.4f M:%.4f S:%.4f Lum:%.4f LM:%.4f SLM:%.4f\n", R, G, B, X, Y, Z, L, M, S, Lum, LM, SLM );
  96. #endif
  97. }
  98. }
  99. else {
  100. fprintf ( stderr, "ColorSpace::convert(): not yet implemented - SrcColorSpace %d -> DstColorSpace %d!!\n", srcColorSpace, dstColorSpace );
  101. exit ( -1 );
  102. }
  103. }
  104. }