首页 > 编程知识 正文

bmp单色位图,c语言16位和32位

时间:2023-05-04 21:49:22 阅读:62881 作者:2479

监测是否为//位图文件

if(BMP.bitmapfileheader.BFtype!=BITMAP_ID )

检查是否为//MB文件

{

//关闭the file

流(Fp Bmp );

//返回错误

返回(空; //否则直接返回,不处理

} //end if

//继续读取文件并保存到bitmapinfoheader

fread(BMP.bitmapinfoheader,sizeof ) bitmapinfoheader ),1,fpBmp );

if (BMP.bitmapinfo header.bi bit count==8)//像素是否为8位

//继续读取调色板数据。

fread(BMP.palette,sizeof ) paletteentry ),256,fpBmp );

读取//调色板数据并将其保存到palette中。

for (索引=0; 索引256; 索引)

{

//r必须更换b中的数据,但读取是以低字节读取,b先读取,b储存在r中

//但是,因为应该将b设为低字节,所以要更换r、b

inttemp _ color=BMP.palette [ index ].pered;

Bmp.palette[index].peRed=

Bmp.palette[index].peBlue;

BMP.palette [ index ].PE blue=temp _ color;

BMP.palette [ index ].PE flags=PC _ no collapse; //不映射到硬件

} //end for index

} //end if

//位图像像素数据的开头

fseek(FPBMP,--(long ) ) BMP.bitmapinfo header.bisizeimage ),SEEK_END );

//读取实际的位图数据。 在8位或16位、24位且模式为32位、或确实需要24位图像的情况下读取

if (BMP.bitmapinfo header.bi bit count==8| |

BMP.bitmapinfo header.bi bit count==16 |

BMP.bitmapinfo header.bi bit count==24|||BMP.bitmapinfo header.bi bit count==32 )

{

if (! (BMP.buffer=) byte

* malloc (BMP.bitmapinfo header.bisizeimage ) )

{

//关闭the file

流(Fp Bmp );

//返回错误

返回(空;

}

//将位图的实际数据读取到buffer中。

fread(BMP.buffer,BMP.bitmapinfo header.bisizeimage,1,fpBmp );

} //end if

申请//32位临时空间,全部转换为32位图像

if (! (tempbmpbuffer=) DWORD

* malloc (4* BMP.bitmapinfo header.bi width * BMP.bitmapinfo header.biheight ) )

{

//close the file

流(Fp Bmp );

//返回错误

返回(空;

}

国际足联

(BMP.bitmapinfo header.bi bit count==24 )/24转32

{

for (索引=0; 索引

BMP.bitmapinfo header.bi width * BMP.bitmapinfo header.biheight;

索引)

{

UCHAR blue

=(BMP.buffer [索引* 30 ]、

green=(BMP.buffer [索引* 31 ]、

red=(BMP.buffer[index*32]

);

TempBmpBuffer[index]

=RGB24to32(255,red,green,blue );

}

}

国际足联

(BMP.bitmapinfo header.bi bit count==32 )

{

/p>

memcpy (TempBmpBuffer

,Bmp.buffer , Bmp.bitmapinfoheader.biSizeImage) ;

}

if

(Bmp.bitmapinfoheader.biBitCount==8)

{

for (index=0; index <

Bmp.bitmapinfoheader.biWidth*Bmp.bitmapinfoheader.biHeight;

index++)

{

BYTE

r,g,b;  int

i=Bmp.buffer[index];//读取像素数据,像素数据是0-255 是调试版的索引

r=Bmp.palette[i].peRed;

g=Bmp.palette[i].peGreen;

b=Bmp.palette[i].peBlue;//像素值 读取调色板各 RGB

TempBmpBuffer[index]

=RGB24TO32(255,r,g,b);

}

}

if

(Bmp.bitmapinfoheader.biBitCount==16)

{

WORD*Buffer16=(WORD*)Bmp.buffer;

if(Bmp.bitmapinfoheader.biCompression==BI_RGB)//x 555

{

for

(index=0; index <

Bmp.bitmapinfoheader.biWidth*Bmp.bitmapinfoheader.biHeight;

index++)

{

WORD cl= Buffer16[index];

TempBmpBuffer[index] =RGB555TO32(1,cl)

;

}

}else

if(Bmp.bitmapinfoheader.biCompression==BI_BITFIELDS)//555或

565

{

fseek(fpBmp,Bmp.bitmapfileheader.bfOffBits -sizeof(DWORD

)*3,SEEK_SET);//到文件开头偏移

DWORD

rMask;

fread(&rMask,sizeof(DWORD

),1,fpBmp);

if(rMask==0x7C00)

{

// 5 5 5

格式

for (index=0; index <

Bmp.bitmapinfoheader.biWidth*Bmp.bitmapinfoheader.biHeight;

index++)

{

WORD cl= Buffer16[index];

TempBmpBuffer[index] =RGB555TO32(1,cl) ;

}

}

else

if(rMask==0xF800)

{

//5 6 5

格式

for (index=0; index <

Bmp.bitmapinfoheader.biWidth*Bmp.bitmapinfoheader.biHeight;

index++)

{

WORD cl= Buffer16[index];

TempBmpBuffer[index] =RGB565TO32(1,cl) ;

}

}

}

}//结束16位转换

//翻转,位图高大于0 则需要翻转  大多情况

都是大于0

BYTE* FilpBuffer = (BYTE

*)malloc(Bmp.bitmapinfoheader.biWidth*Bmp.bitmapinfoheader.biHeight*4);

memcpy(FilpBuffer,TempBmpBuffer,Bmp.bitmapinfoheader.biWidth*Bmp.bitmapinfoheader.biHeight*4);

for (index=0; index < Bmp.bitmapinfoheader.biHeight;

index++)

memcpy(&TempBmpBuffer[((Bmp.bitmapinfoheader.biHeight-1) -

index)* Bmp.bitmapinfoheader.biWidth],

&FilpBuffer[index*Bmp.bitmapinfoheader.biWidth*4],

Bmp.bitmapinfoheader.biWidth*4);

//创建表面

IDirect3DSurface9* pSurface ;//  根据图片宽度高度

创建表面到内存

if(gpD3dDev->CreateOffscreenPlainSurface (

Bmp.bitmapinfoheader.biWidth ,Bmp.bitmapinfoheader.biHeight,format,

D3DPOOL_SYSTEMMEM ,&pSurface,NULL

)==D3DERR_INVALIDCALL)

{

StdMessageBox(L"错误",L"创建表面出错!");

PostQuitMessage(0); return NULL;

}

//锁定整个表面 并获得指针

D3DLOCKED_RECT lockrect ;

pSurface->LockRect ( &lockrect , NULL , 0 ) ;

DWORD* pbyBuffer = ( DWORD* ) lockrect.pBits ;

//画像素  X,Y 为像素单位

for ( int y = 0 ; y < Bmp.bitmapinfoheader.biHeight ; y ++

)

{

memcpy (pbyBuffer ,TempBmpBuffer ,

Bmp.bitmapinfoheader.biWidth<<2) ;//拷贝一行  32位

TempBmpBuffer+=Bmp.bitmapinfoheader.biWidth;

pbyBuffer+=(lockrect.Pitch>>2);

}

//解锁表面

pSurface->UnlockRect ( ) ;

//返回表面

return ( pSurface ) ;

}

以上是全部代码,当然我还没有释放内存,因为我也是刚完成,就迫不及待的全部给您了,呵呵,释放内存是必须的,这就叫给您来完成此函数了,其实已经完成了哦。呵呵可以成功运行了。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。