-
see this code i am using it for 24 bit to 8 bit convertion but its is crashing
//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;
}
-
You didn't give us the whole listing so I assumed some things based on your code, anyway I don't need to use ClosestColor func for this to work (hopefuly)
/*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);
}
AfxMessageBox("before 2 for loop");
//adjust the pixels to correspond to the 24 bit rgb
for(i=0; i < h; i++)
{
dPtr = GetLinePtr(i);
sPtr = dib.GetLinePtr(i);
for(j=0 ; j < w; j++,dPtr++,sPtr+=3)
{
c = (*sptr + (*(sptr+1)) + (*(sptr+2))/3; //(r+g+b)/3
*dPtr = c;
}
}
//adjust the color table for the 8 bit greyscale
RGBQUAD* rgbquadptr;
dptr = GetLinePtr(0);
dptr -= (256 * 4); //I assumed this now points to the first RGBQUAD struct of the 8 bit greyscale image; if not change it
rgbquadptr = (RQBQUAD*)dptr;
for (i=0; i < 256; i++) {
rgbquadptr->red = (rgbquadptr->red + rgbquadptr->green + rgbquadptr->blue)/3;
rgbquadptr->green = (rgbquadptr->red + rgbquadptr->green + rgbquadptr->blue)/3;
rgbquadptr->blue = (rgbquadptr->red + rgbquadptr->green + rgbquadptr->blue)/3;
rgbquadptr++;
}
return TRUE;
}
-
thank kyou very much for your repls
i have been suffering for the past one month t convert a 24 bit image into 8 bit grey scale ..
inee dyour help
i will be in touch with you..
thks in advance
i will send whole listing od my class to you..
can you give your mal id..
-
My mail is ibalen@net.hr, maybe you can post the listing at devx.com so that others might help too
Similar Threads
-
By Chaitanya Marvici in forum ASP.NET
Replies: 6
Last Post: 07-21-2003, 09:15 AM
-
By Glen Kunene in forum Java
Replies: 25
Last Post: 04-22-2002, 05:45 PM
-
By michael s in forum XML
Replies: 0
Last Post: 04-03-2001, 04:32 PM
-
Replies: 1
Last Post: 03-12-2001, 12:37 PM
-
Replies: 1
Last Post: 09-22-2000, 09:11 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|