English
HOME | PROJECTS | C++ CLASSES | c\c++ source code | Gallery | Guestbook | BW Servers | Contact me | Login

<< RGB HSV conversion >>

visits: 4973


This two table will let you convert easily a pixel from RGB to HSV and vice-versa. They are really useful to work with images in RGB 24bit.

  1. //*************************************//
  2. //******** created by mamo139 *********//
  3. //*** http://mamo139.altervista.org ***//
  4. //*************************************//
  5.  
  6. unsigned char ****tabella_rgb_hsv;
  7. unsigned char ****tabella_hsv_rgb;
  8. inline unsigned char char_conversion(int c){ return ((c)<255) ? ((c)>0) ? (c) : 0 : 255; }
  9.  
  10. ...
  11.  
  12. //creazione tabella conversione rgb to hsv: tabella_rgb_hsv
  13. printf("preparazione tabella rgb to hsv... ");
  14. tabella_rgb_hsv = new unsigned char ***[3];
  15. for(h=0;h<3;h++){
  16. tabella_rgb_hsv[h] = new unsigned char **[256];
  17. for(x=0;x<256;x++){
  18. tabella_rgb_hsv[h][x] = new unsigned char *[256];
  19. for(y=0;y<256;y++)
  20. tabella_rgb_hsv[h][x][y] = new unsigned char[256];
  21. }
  22. }
  23. for(x=0;x<256;x++) // x = rosso
  24. for(y=0;y<256;y++) // y = verde
  25. for(z=0;z<256;z++){ //z = blu
  26. max = x;
  27. if(y>max) max = y;
  28. if(z>max) max = z;
  29. min = x;
  30. if(y<min) min = y;
  31. if(z<min) min = z;
  32.  
  33. if(max == min)
  34. tabella_rgb_hsv[0][x][y][z] = 0;
  35. else if(x == max)//else if(x > y && x > z) //se rosso max
  36. tabella_rgb_hsv[0][x][y][z] = char_conversion( ((60*(y-z)/(max-min)+360)%360)*255/359 );
  37. else if(y == max)//else if(y> x && y > z)
  38. tabella_rgb_hsv[0][x][y][z] = char_conversion( (60*(z-x)/(max-min)+120)*255/359 );
  39. else if(z == max)
  40. tabella_rgb_hsv[0][x][y][z] = char_conversion( (60*(x-y)/(max-min)+240)*255/359 );
  41. else
  42. printf("errore rgb->hsv\n");
  43.  
  44. tabella_rgb_hsv[2][x][y][z] = (unsigned char)max;
  45.  
  46. if(max == 0)
  47. tabella_rgb_hsv[1][x][y][z] = 0;
  48. else
  49. tabella_rgb_hsv[1][x][y][z] = char_conversion((max - min)*255/max);
  50. }
  51. printf("fatto!\n");
  52.  
  53.  
  54. //creazione tabella conversione rgb to hsv: tabella_hsv_rgb
  55. printf("preparazione tabella hsv to rgb...");
  56. tabella_hsv_rgb = new unsigned char ***[3];
  57. for(h=0;h<3;h++){
  58. tabella_hsv_rgb[h] = new unsigned char **[256];
  59. for(x=0;x<256;x++){
  60. tabella_hsv_rgb[h][x] = new unsigned char *[256];
  61. for(y=0;y<256;y++)
  62. tabella_hsv_rgb[h][x][y] = new unsigned char[256];
  63. }
  64. }
  65. for(x=0;x<256;x++) // x = hue
  66. for(y=0;y<256;y++) // y = saturation
  67. for(z=0;z<256;z++){ //z = value
  68.  
  69. h = (int)floor((float)(x*359/255)/60)%6;
  70. f = (float)(x*359/255)/60 - floor((float)(x*359/255)/60);
  71. s = (float)y/255;
  72. p = (long)( z * (1.0 - s) );
  73. q = (long)( z * (1.0 - f * s ) );
  74. t = (long)( z * (1.0 - (1.0-f) * s) );
  75.  
  76. if(h == 0){
  77. tabella_hsv_rgb[0][x][y][z] = char_conversion(z);
  78. tabella_hsv_rgb[1][x][y][z] = char_conversion(t);
  79. tabella_hsv_rgb[2][x][y][z] = char_conversion(p);
  80. }
  81. if(h == 1){
  82. tabella_hsv_rgb[0][x][y][z] = char_conversion(q);
  83. tabella_hsv_rgb[1][x][y][z] = char_conversion(z);
  84. tabella_hsv_rgb[2][x][y][z] = char_conversion(p);
  85. }
  86. if(h == 2){
  87. tabella_hsv_rgb[0][x][y][z] = char_conversion(p);
  88. tabella_hsv_rgb[1][x][y][z] = char_conversion(z);
  89. tabella_hsv_rgb[2][x][y][z] = char_conversion(t);
  90. }
  91. if(h == 3){
  92. tabella_hsv_rgb[0][x][y][z] = char_conversion(p);
  93. tabella_hsv_rgb[1][x][y][z] = char_conversion(q);
  94. tabella_hsv_rgb[2][x][y][z] = char_conversion(z);
  95. }
  96. if(h == 4){
  97. tabella_hsv_rgb[0][x][y][z] = char_conversion(t);
  98. tabella_hsv_rgb[1][x][y][z] = char_conversion(p);
  99. tabella_hsv_rgb[2][x][y][z] = char_conversion(z);
  100. }
  101. if(h == 5){
  102. tabella_hsv_rgb[0][x][y][z] = char_conversion(z);
  103. tabella_hsv_rgb[1][x][y][z] = char_conversion(p);
  104. tabella_hsv_rgb[2][x][y][z] = char_conversion(q);
  105. }
  106.  
  107. }
  108. printf("fatto!\n");
  109.  
Website version: 1.03.01 by mamo139. - All the source codes are intended to be released under "GNU GPL version 3".