#include "windows.h"
#include<cstdio>
#include<cstdlib>
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow ();
void ReadPicture(HWND hWnd,const char *fname)
{
HDC hdc = GetDC(hWnd);
long size;
int Width, Height ;//Width和Height分别为原图像的宽和高
BITMAPFILEHEADER * pbmfh ;
BITMAPINFO * pbmi ;
BYTE * pBits ,*tmpBits;
FILE *fp=fopen(fname,"r");
fseek( fp, 0, SEEK_END );
size=ftell(fp); //获得文件大小
fseek( fp, 0, 0 ); //跳至文件开头
pbmfh = (BITMAPFILEHEADER*)malloc (size);
fread(pbmfh,size,1,fp);
fclose(fp);
if (pbmfh->bfType != * (WORD *) "BM"){
printf("不是bmp格式!\n");
free (pbmfh) ;
return;
}
pbmi = (BITMAPINFO *) (pbmfh + 1) ;
pBits = (BYTE *) pbmfh + pbmfh->bfOffBits;
//获取图像的高和宽
if (pbmi->bmiHeader.biSize == sizeof (BITMAPCOREHEADER)){
Width = ((BITMAPCOREHEADER *) pbmi)->bcWidth ;
Height = ((BITMAPCOREHEADER *) pbmi)->bcHeight ;
}
else{
Width = pbmi->bmiHeader.biWidth ;
Height = abs(pbmi->bmiHeader.biHeight) ;
}
tmpBits=(BYTE*)malloc(size);
for(int i=0;i<size;i++)
tmpBits[i]=-1*pBits[i];
/* for(int i=0;i<size;i++)
tmpBits[i]=pBits[i] | 0x77;*/
/* for(int i=0;i<size;i++)
tmpBits[i]=pBits[i] & 0x77;*/
//显示变换后的图片(参数tmpBits)
SetDIBitsToDevice (GetDC(hWnd),0,0,Width,Height,
0, 0, 0,Height,tmpBits,pbmi, DIB_RGB_COLORS) ;
/*
//保存变换后的图像
memcpy(pBits,tmpBits,(Width*(pbmi->bmiHeader.biBitCount)+31)/32*4*Height);
free(tmpBits);
FILE *fs=fopen("E:\\qa.bmp","wb");
BITMAPFILEHEADER * fsh; ;
fsh=pbmfh;
fwrite(fsh,size,1,fs);
free(pbmfh);
fclose(fs);*/
}
int main()
{
ReadPicture(GetConsoleWindow(),"E:\\q.bmp");
system("pause");
return 0;
}
DIB图像反色变换
最新推荐文章于 2021-11-21 15:07:04 发布