Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.

How to open a BMP file with IPP?

wwang
Beginner
1,008 Views
I am new to IPP. Before I can do any image processing, I need to open the images that are stored in .bmp files. Is there a function in IPP that opens a .bmp file or do I need to do it outside the library and feed the resulting data matrix into IPP functions? Thanks.
0 Kudos
1 Reply
Vladimir_Dudnik
Employee
1,008 Views

Hello,

For such a basic questions you can find answers on that forum, for example:

Loading an image
Loading an Ippi image
Image processing
Need a simple way to read and display image files

Well, after review of those question I see that probably piece of simple code to read BMP file can help

#define DIB_ALIGN (sizeof(int) - 1)
#define DIB_UWIDTH(width,nchannels) 
((width) * (nchannels))
#define DIB_AWIDTH(width,nchannels) 
( ((DIB_UWIDTH(width,nchannels) + DIB_ALIGN) & (~DIB_ALIGN)) )
#define DIB_PAD_BYTES(width,nchannels) 
( DIB_AWIDTH(width,nchannels) - DIB_UWIDTH(width,nchannels) )
/* Open BMP file */

int read_bmp(
char* fname,
Ipp8u** bmBits,
int* bmStep,
IppiSize* bmSize,
int* nchannels)
{
int i, j;
int res = 0;
int pad;
int step;
int size;
size_t cnt;
Ipp8u* ptr;
Ipp8u* buff = NULL;
FILE * bmpf = NULL;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
RGBQUAD palette[256];
 bmpf = fopen(fname,"rb");
if(NULL == bmpf)
{
fprintf(stderr,"ERROR: can't open file %s ",fname);
res = -1;
goto Exit;
}
 cnt = fread(&bmfh,sizeof(char),sizeof(BITMAPFILEHEADER),bmpf);
if(cnt != sizeof(BITMAPFILEHEADER))
{
fprintf(stderr,"ERROR: can't read from file ");
res = -1;
goto Exit;
}
 if(bmfh.bfType != 0x4d42)
{
fprintf(stderr,"ERROR: invalid file format ");
res = -1;
goto Exit;
}
 cnt = fread(&bmih,sizeof(char),sizeof(BITMAPINFOHEADER),bmpf);
if(cnt != sizeof(BITMAPINFOHEADER))
{
fprintf(stderr,"ERROR: can't read from file ");
res = -1;
goto Exit;
}
 if(bmih.biBitCount != 8 && bmih.biBitCount != 24)
{
fprintf(stderr,"ERROR: sorry, unsupported format for this example ");
res = -1;
goto Exit;
}
 if(bmih.biBitCount == 8)
{
cnt = fread(palette,sizeof(char),sizeof(palette),bmpf);
if(cnt != sizeof(palette))
{
fprintf(stderr,"ERROR: can't read from file ");
res = -1;
goto Exit;
}
}
 res = fseek(bmpf,bmfh.bfOffBits,SEEK_SET);
if(0 != res)
{
fprintf(stderr,"Error: can't seek in file n");
goto Exit;
}
 pad = DIB_PAD_BYTES(bmih.biWidth,bmih.biBitCount >> 3);
step = (bmih.biWidth * (bmih.biBitCount >> 3) + pad);
size = step * abs(bmih.biHeight);
 buff = (Ipp8u*)ippMalloc(size);
if(NULL == buff)
{
fprintf(stderr,"ERROR: can't allocate memory ");
res = -1;
goto Exit;
}
 if(bmih.biHeight > 0)
{
ptr = buff + (bmih.biHeight-1)*step;
}
else
{
ptr = buff;
}
 for(i = 0; i < abs(bmih.biHeight); i++)
{
cnt = fread(ptr,sizeof(char),step,bmpf);
if(cnt != (size_t)step)
{
fprintf(stderr,"ERROR: can't read from file ");
res = -1;
goto Exit;
}
 if(bmih.biHeight > 0)
{
ptr -= step;
}
else
{
ptr += step;
}
}
 if(bmih.biBitCount == 8)
{
for(i = 0; i < abs(bmih.biHeight); i++)
{
ptr = buff + i*step;
for(j = 0; j < bmih.biWidth; j++)
{
// for Gray all palette entries is equivalent
int pal_index = ptr;
Ipp8u gray = palette[pal_index].rgbBlue;
ptr = gray;
}
}
}
 *bmBits = buff;
bmSize->width = bmih.biWidth;
bmSize->height = abs(bmih.biHeight);
*bmStep = step;
*nchannels = bmih.biBitCount >> 3;
Exit:
 if(NULL != bmpf)
{
fclose(bmpf);
}
 if(res != 0)
{
if(NULL != buff)
{
ippFree(buff);
}
}
 return res;
} /* read_bmp() */

Regards,
Vladimir

0 Kudos
Reply