Hi nv3,
this is the code i am using for loading 8 bit image.
I am using bmpfile.h and .cpp ...
I don know what mistake i have done ,, please someone can help me out from this issue....
Here is the main code ..
int nWidth = 149 ;
int nHeight = 126;
unsigned char * pInputBuffer = new unsigned char[nWidth*nHeight]();
Loading 8 bit bmp image ..
pInputBuffer = bmp.LoadBMP("D:\\MyWorkSpace\\Clipboard01.bmp",nWidth,nHeight,8);
bmp.VertFlipBuf(pInputBuffer,nWidth,nHeight);
BYTE * BMPFile::LoadBMP(char* fileName,
__int16 width,
__int16 height, int bitdepth)
{
BITMAP inBM;
BYTE m1,m2;
long filesize;
short res1,res2;
long pixoff;
long bmisize;
long compression;
unsigned long sizeimage;
long xscale, yscale;
long colors;
long impcol;
BYTE *outBuf=NULL;
width=0; height=0;
bitdepth=0;
m_bytesRead=0;
FILE *fp;
fp=fopen(fileName,"rb");
if (fp==NULL)
{
return NULL;
}
else
{
long rc;
rc=fread((BYTE *)&(m1),1,1,fp); m_bytesRead+=1;
if (rc==-1) {fclose(fp); return NULL;}
rc=fread((BYTE *)&(m2),1,1,fp); m_bytesRead+=1;
if (rc==-1)
if ((m1!='B') || (m2!='M'))
{
fclose(fp);
return NULL;
}
rc=fread((long *)&(filesize),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((int *)&(res1),2,1,fp); m_bytesRead+=2;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((int *)&(res2),2,1,fp); m_bytesRead+=2;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(pixoff),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(bmisize),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(inBM.bmWidth),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(inBM.bmHeight),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((int *)&(inBM.bmPlanes),2,1,fp); m_bytesRead+=2;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((int *)&(inBM.bmBitsPixel),2,1,fp); m_bytesRead+=2;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(compression),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(sizeimage),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(xscale),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(yscale),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(colors),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
rc=fread((long *)&(impcol),4,1,fp); m_bytesRead+=4;
if (rc!=1) { fclose(fp); return NULL;}
bitdepth = inBM.bmBitsPixel;
if (compression!=BI_RGB)
{
fclose(fp);
return NULL;
}
if (colors == 0)
{
colors = 1 << inBM.bmBitsPixel;
}
switch (inBM.bmBitsPixel)
{
case 24:
break;
case 1:
case 4:
case 8:
colormap = new RGBQUAD[colors];
if (colormap==NULL)
{
fclose(fp);
return NULL;
}
int i;
for (i=0;i<colors;i++)>
{
BYTE r,g,b, dummy;
rc=fread((BYTE *)&(b),1,1,fp);
m_bytesRead++;
if (rc!=1)
{
delete [] colormap;
fclose(fp);
return NULL;
}
rc=fread((BYTE *)&(g),1,1,fp);
m_bytesRead++;
if (rc!=1)
{
delete [] colormap;
fclose(fp);
return NULL;
}
rc=fread((BYTE *)&(r),1,1,fp);
m_bytesRead++;
if (rc!=1)
{
delete [] colormap;
fclose(fp);
return NULL;
}
rc=fread((BYTE *)&(dummy),1,1,fp);
m_bytesRead++;
if (rc!=1)
{
delete [] colormap;
fclose(fp);
return NULL;
}
colormap[i].rgbRed=r;
colormap[i].rgbGreen=g;
colormap[i].rgbBlue=b;
}
break;
}
if ((long)m_bytesRead>pixoff)
{
fclose(fp);
delete [] colormap;
fclose(fp);
return NULL;
}
while ((long)m_bytesRead<pixoff)>
{
char dummy;
fread(&dummy,1,1,fp);
m_bytesRead++;
}
int w=inBM.bmWidth;
int h=inBM.bmHeight;
width=w;
height=h;
long row_size = w * 3;
long bufsize = (long)w * 3 * (long)h;
outBuf = new BYTE[bufsize];
if (outBuf==NULL)
{
}
else
{
long row=0;
long rowOffset=0;
for (row=inBM.bmHeight-1;row>=0;row--)
{
rowOffset=(long unsigned)row*row_size;
if (inBM.bmBitsPixel==24)
{
for (int col=0;col<w;col++)>
{
long offset = col * 3;
char pixel[3];
if (fread((void *)(pixel),1,3,fp)==3)
{
*(outBuf + rowOffset + offset + 0)=pixel[2];
*(outBuf + rowOffset + offset + 1)=pixel[1];
*(outBuf + rowOffset + offset + 2)=pixel[0];
}
}
m_bytesRead+=row_size;
while ((m_bytesRead-pixoff)&3)
{
char dummy;
if (fread(&dummy,1,1,fp)!=1)
{
delete [] outBuf;
fclose(fp);
return NULL;
}
m_bytesRead++;
}
}
else
{
int bit_count = 0;
UINT mask = (1 << inBM.bmBitsPixel) - 1;
BYTE inbyte=0;
for (int col=0;col<w;col++)>
{
int pix=0;
if (bit_count <= 0)
{
bit_count = 8;
if (fread(&inbyte,1,1,fp)!=1)
{
delete [] outBuf;
delete [] colormap;
fclose(fp);
return NULL;
}
m_bytesRead++;
}
bit_count -= inBM.bmBitsPixel;
pix = ( inbyte >> bit_count) & mask;
*(outBuf + rowOffset + col * 3 + 2) = colormap[pix].rgbBlue;
*(outBuf + rowOffset + col * 3 + 1) = colormap[pix].rgbGreen;
*(outBuf + rowOffset + col * 3 + 0) = colormap[pix].rgbRed;
}
while ((m_bytesRead-pixoff)&3)
{
char dummy;
if (fread(&dummy,1,1,fp)!=1)
{
delete [] outBuf;
if (colormap)
delete [] colormap;
fclose(fp);
return NULL;
}
m_bytesRead++;
}
}
}
}
fclose(fp);
}
return outBuf;
}