grayscale - Copying 16bit grascale image to 48 bit rgb image -
i have raw data of 16 bit grayscale image. wanted make 48 bit rgb image copying same value 3 channels. presently i'm doing:
byte *bit=new byte[512*512*6]; int j=-1; int i=0; for(int k=0;k<512*512;k++) { bit[i]=ptr[++j]; bit[++i]=ptr[j]; bit[++i]=ptr[j]; bit[++i]=ptr[++j]; bit[++i]=ptr[j]; bit[++i]=ptr[j]; i++; } this code results in yellowish tinge. shifting bits required?
try this:
byte *bit=new byte[512*512*6]; int j=-2; int i=-1; for(int k=0;k<512*512;k++) { bit[++i]=ptr[++++j]; bit[++i]=ptr[j+1]; bit[++i]=ptr[j]; bit[++i]=ptr[j+1]; bit[++i]=ptr[j]; bit[++i]=ptr[j+1]; } this following.
1st round:
bit[0]=ptr[0]; bit[1]=ptr[1]; bit[2]=ptr[0]; bit[3]=ptr[1]; bit[4]=ptr[0]; bit[5]=ptr[1]; 2nd round:
bit[6]=ptr[2]; bit[7]=ptr[3]; bit[8]=ptr[2]; bit[9]=ptr[3]; bit[10]=ptr[2]; bit[11]=ptr[3]; ...etc.
actually don't need i , j:
byte *bit=new byte[512*512*6]; for(int k=0;k<512*512;k++) { bit[6*k]=ptr[2*k]; bit[6*k+1]=ptr[2*k+1]; bit[6*k+2]=ptr[2*k]; bit[6*k+3]=ptr[2*k+1]; bit[6*k+4]=ptr[2*k]; bit[6*k+5]=ptr[2*k+1]; } the reverse operation (48-bit 16-bit):
byte *bit=new byte[512*512*6]; for(int k=0;k<512*512;k++) { ptr[2*k]=bit[6*k]; ptr[2*k+1]=bit[6*k+1]; } if rgb image not grayscale, have calculate luminance of every pixel , copy luminance 16-bit grayscale image:
byte *bit=new byte[512*512*6]; for(int k=0;k<512*512;k++) { unsigned int r=(bit[6*k]<<8)+bit[6*k+1]; unsigned int g=(bit[6*k+2]<<8)+bit[6*k+3]; unsigned int b=(bit[6*k+4]<<8)+bit[6*k+5]; unsigned int y=(unsigned int)round(0.2126*r+0.7152*g+0.0722*b); ptr[2*k]=y>>8; ptr[2*k+1]=y&0xff; } this code assumes significant byte stored first (big-endian). if use little-endian storage:
byte *bit=new byte[512*512*6]; for(int k=0;k<512*512;k++) { unsigned int r=(bit[6*k+1]<<8)+bit[6*k]; unsigned int g=(bit[6*k+3]<<8)+bit[6*k+2]; unsigned int b=(bit[6*k+5]<<8)+bit[6*k+4]; unsigned int y=(unsigned int)round(0.2126*r+0.7152*g+0.0722*b); ptr[2*k+1]=y>>8; ptr[2*k]=y&0xff; }
Comments
Post a Comment