//i am using this function fro converting a 24 bit image into 8 bit grey scale image..
b closestcolor() function is crashing in therse
because for 24 bit GetPalletesize() function returns 0
can anyone suggest what to do..
BOOL CDIB::Make8Bit(CDIB& dib)
{
int nBits;
ASSERT(Width() == dib.Width());
ASSERT(Height() == dib.Height());
nBits = dib.GetBitCount();

CString str;
str.Format("n bits %d",nBits);
AfxMessageBox(str);


switch(nBits)
{
case 1:
return SwitchFromOne(dib);
break;
case 4:
return SwitchFromFour(dib);
break;
case 8:
return SwitchPalette(dib);
break;
case 24:
return SwitchFrom24(dib);
break;
default:
return FALSE;
}
return FALSE;
}

/*BOOL CDIB::SwitchFrom24(CDIB& dib)
{



int i,j,w,h,c;
unsigned char *sPtr,*dPtr;
BYTE *index_ptr=NULL;
RGBQUAD rgb;
w = dib.Width();
h =dib. Height();

CString str;
str.Format("w=%d h=%d",w,h);


index_ptr = (BYTE *)malloc(0x7FFF+1);
if(!index_ptr) return FALSE;
memset(CachePtr,0,sizeof(CachePtr));


for(i=0; i <= 0x7FFF; i++)
{
rgb.rgbRed = (((i & 0x7C00)>>10) << 3) | 0x07;
rgb.rgbGreen = (((i & 0x3e0)>>5) << 3) | 0x07;
rgb.rgbBlue = ((i & 0x1F)<<3) | 0x07;

long lsum=rgb.rgbRed*rgb.rgbRed+rgb.rgbGreen*rgb.rgbGreen+rgb.rgbBlue*rgb.rgbBlue;
int grey=(int)sqrt(((double)lsum)/3);




index_ptr[i] =ClosestColor(&rgb);

}
AfxMessageBox("before 2 for loop");
for(i=0; i < h; i++)
{
dPtr = GetLinePtr(i);
sPtr = dib.GetLinePtr(i);
for(j=0 ; j < w; j++,dPtr++,sPtr+=3)
{
c = (*sPtr >> 3) | ((*(sPtr+1) >> 3) << 5) | ((*(sPtr+2) >> 3) << 10);
*dPtr = index_ptr[c];
}
}
free(index_ptr);
return TRUE;
}


int CDIB::ClosestColor(RGBQUAD *pRgb)
{



unsigned int dist=BIG_DISTANCE,i,d,c;
RGBQUAD *pQuad=m_pRGB;


int pSize=GetPaletteSize();


for(i=0; i < pSize;i++)
{
if(CachePtr[i])
{
if(!memcmp((void *)&CacheQuad[i],(void *)pRgb,3))
{
return i;
}
}
}
for(i=0; i < pSize; i++,pQuad++)
{
d = Distance(*pRgb,*pQuad);
if(!d)
{
CacheQuad[i]=*pRgb;
CachePtr[i]=1;
return i;
}
if(dist > d)
{
c = i;
dist = d;
}
}
CacheQuad[c]=*pRgb;
CachePtr[c]=1;
return c;
}