ColorTrans.cpp
上传用户:smdfuse
上传日期:2015-11-06
资源大小:98k
文件大小:2k
源码类别:

图形图象

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "ColorTrans.h"
  3. ColorTrans::ColorTrans(void)
  4. {
  5. }
  6. ColorTrans::~ColorTrans(void)
  7. {
  8. }
  9. //8bit ->> 24bit
  10. unsigned char* ColorTrans::gray2RGB2(unsigned char* gray, unsigned char* rgb, int width, int height)
  11. {
  12. int acc=0;
  13. if(rgb == NULL){
  14. rgb = (unsigned char*)malloc(sizeof(unsigned char) * width * height * 3);
  15. }
  16. for(int i = 0; i< height*width ;i++){
  17. rgb[acc] = gray[i];
  18. rgb[acc+1] = gray[i];
  19. rgb[acc+2] = gray[i];
  20. acc += 3;
  21. }
  22. return rgb;
  23. }
  24. //RGB彩色转换为RGB灰度,直接输出
  25. //输出位8位灰度
  26. bool ColorTrans::RGB2Gray8(unsigned char* rgb, unsigned char* gray, int width, int height)
  27. {
  28. int k = 0;
  29. for(int i = 0; i< height*width ;i++){
  30.   unsigned char newValue = (unsigned char)((long)((double)rgb[k+2])*0.299 + (long)((double)rgb[k+1])*0.587 + (long)((double)rgb[k])*0.114);
  31. gray[i] = newValue;
  32. k=k+3;
  33. }
  34. return true;
  35. }
  36. unsigned char* ColorTrans::RGB2Gray(unsigned char* gray, unsigned char* rgb, int width, int height)
  37. {
  38. int acc=0;
  39. if(gray == NULL){
  40. rgb = (unsigned char*)malloc(sizeof(unsigned char) * width * height);
  41. }
  42. for(int i = 0; i< height*width ;i++){
  43. gray[i] =rgb[i*3];
  44. }
  45. return gray;
  46. }
  47. /*
  48. void ColorTrans::RGBToGray(unsigned char* dib, int width, int height)
  49. {
  50. BYTE* bitmap = dib;
  51. int hHeight = width;
  52. int hWidth = height;
  53. int k=0;
  54. unsigned char newValue=0;
  55. for(int i = 0;i<hHeight;i++){
  56. for(int j=0; j <hWidth;j++){
  57. //直接设置
  58. newValue = (unsigned char)((long)((double)bitmap[k+2])*0.299 + (long)((double)bitmap[k+1])*0.587 + (long)((double)bitmap[k])*0.114);
  59. bitmap[k] = newValue;
  60. bitmap[k+1] = newValue;
  61. bitmap[k+2] = newValue;
  62. //dib->SetPixelMatrix(i,j,newValue,newValue,newValue);
  63. k=k+3;
  64. }
  65. }
  66. }
  67. */