ColorTrans.cpp
上传用户:smdfuse
上传日期:2015-11-06
资源大小:98k
文件大小:2k
- #include "stdafx.h"
- #include "ColorTrans.h"
- ColorTrans::ColorTrans(void)
- {
- }
- ColorTrans::~ColorTrans(void)
- {
- }
- //8bit ->> 24bit
- unsigned char* ColorTrans::gray2RGB2(unsigned char* gray, unsigned char* rgb, int width, int height)
- {
- int acc=0;
- if(rgb == NULL){
- rgb = (unsigned char*)malloc(sizeof(unsigned char) * width * height * 3);
- }
- for(int i = 0; i< height*width ;i++){
- rgb[acc] = gray[i];
- rgb[acc+1] = gray[i];
- rgb[acc+2] = gray[i];
- acc += 3;
- }
- return rgb;
- }
- //RGB彩色转换为RGB灰度,直接输出
- //输出位8位灰度
- bool ColorTrans::RGB2Gray8(unsigned char* rgb, unsigned char* gray, int width, int height)
- {
- int k = 0;
- for(int i = 0; i< height*width ;i++){
- 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);
-
- gray[i] = newValue;
- k=k+3;
- }
- return true;
- }
- unsigned char* ColorTrans::RGB2Gray(unsigned char* gray, unsigned char* rgb, int width, int height)
- {
- int acc=0;
- if(gray == NULL){
- rgb = (unsigned char*)malloc(sizeof(unsigned char) * width * height);
- }
- for(int i = 0; i< height*width ;i++){
- gray[i] =rgb[i*3];
- }
- return gray;
- }
- /*
- void ColorTrans::RGBToGray(unsigned char* dib, int width, int height)
- {
- BYTE* bitmap = dib;
- int hHeight = width;
- int hWidth = height;
- int k=0;
- unsigned char newValue=0;
- for(int i = 0;i<hHeight;i++){
- for(int j=0; j <hWidth;j++){
- //直接设置
- newValue = (unsigned char)((long)((double)bitmap[k+2])*0.299 + (long)((double)bitmap[k+1])*0.587 + (long)((double)bitmap[k])*0.114);
- bitmap[k] = newValue;
- bitmap[k+1] = newValue;
- bitmap[k+2] = newValue;
- //dib->SetPixelMatrix(i,j,newValue,newValue,newValue);
- k=k+3;
- }
- }
- }
- */