資源簡介
計算機圖形學(xué)貓變虎作業(yè) 代碼及示例結(jié)果,c++語言編程
代碼片段和文件信息
#include?
#include?
#include?
using?namespace?std;
unsigned?char?*pBmpBuf;
int?bmpWidthbmpHeight;????//長寬
unsigned?int?biBitCount;????????????//每像素位數(shù)
RGBQUAD?*pColorTable;??????//顏色表指
int?offbits;
int?bitPerLine;
///圖片格式為bmp格式/////
//圖片讀入函數(shù)///
bool?Inputbmp(char?*bmpName){
FILE?*fp=fopen(bmpName“rb“);
if(!fp){
cout<<“read?fail\n“;
return?0;
}
//跳過位圖文件頭結(jié)構(gòu)BITMAPFILEHEADER
fseek(fp?sizeof(BITMAPFILEHEADER)0);?
//BITMAPINFOHEADER?head;
BITMAPINFOHEADER?head;
fread(&head?sizeof(BITMAPINFOHEADER)?1fp);?
bmpWidth?=?head.biWidth;??//獲取圖像寬、高、每像素所占位數(shù)等信息
bmpHeight?=?head.biHeight;
biBitCount?=?head.biBitCount;?//定義變量,計算圖像每行像素所占的字節(jié)數(shù)
int?lineByte=(bmpWidth?*?biBitCount/8+3)/4*4;??//灰度圖像有顏色
if(biBitCount==8)
cout<<“can‘t?show?8?bits\n“;?
????//申請位圖數(shù)據(jù)所需要的空間,讀位圖數(shù)據(jù)進內(nèi)存
pBmpBuf=new?unsigned?char[lineByte?*?bmpHeight];
????fread(pBmpBuf1lineByte?*?bmpHeightfp);
fclose(fp);
cout<<“read?success\n“;
return?1;
}
///圖片儲存函數(shù)////
bool?saveBmp(char?*bmpNameunsigned?char?*imgBuf?int?width?int?height?int?biBitCount?RGBQUAD?*pColorTable){
//如果位圖數(shù)據(jù)指針為0,則沒有數(shù)據(jù)傳入,函數(shù)返回
if(!imgBuf)
return?0;
int?colorTablesize=0;
if(biBitCount==8)
cout<<“can‘t?show?8?bits\n“;?
int?lineByte=(width?*?biBitCount/8+3)/4*4;
//以二進制寫的方式打開文件
FILE?*fp=fopen(bmpName“wb“);
if(fp==0)
return?0;
//申請位圖文件頭結(jié)構(gòu)變量,填寫文件頭信息
BITMAPFILEHEADER?fileHead;
fileHead.bfType?=?0x4D42;//bmp類型
//bfSize是圖像文件4個組成部分之和
fileHead.bfSize=?sizeof(BITMAPFILEHEADER)?+?sizeof(BITMAPINFOHEADER)?+?colorTablesize?+?lineByte*height;
fileHead.bfReserved1?=?0;
fileHead.bfReserved2?=?0;?//bfOffBits是圖像文件前3個部分所需空間之和
fileHead.bfOffBits=54+colorTablesize;//寫文件頭進文件
fwrite(&fileHead?sizeof(BITMAPFILEHEADER)1?fp);
BITMAPINFOHEADER?head;
head.biBitCount=biBitCount;
head.biClrImportant=0;
head.biClrUsed=0;
head.biCompression=0;
head.biHeight=height;
head.biPlanes=1;
head.biSize=40;
head.biSizeImage=lineByte*height;
head.biWidth=width;
head.biXPelsPerMeter=0;
head.biYPelsPerMeter=0;//寫位圖信息頭進內(nèi)存
fwrite(&head?sizeof(BITMAPINFOHEADER)1?fp);//如果灰度圖像,有顏色表,寫入文件
fwrite(imgBuf?height*lineByte?1?fp);
fclose(fp);
return?1;
}
//貓變虎函數(shù)
void?change(){
int?cat_i[20000]cat_ib[20000]cat_ig[20000];
int tiger_i[20000]tiger_ib[20000]tiger_ig[20000];
char?address_tiger[998];
char?address_cat[999];
cout<<“Input?picture_begin:\n“;?///輸入變化前圖片名?必須加后綴(.bmp)!!!!
cin>>address_cat;
cout<<“Input?picture_end:\n“?;??////輸入變化后圖片名
cin>>address_tiger;
Inputbmp(address_tiger);
int?lineByte=(bmpWidth*biBitCount/8+3)/4*4;
int?m=0n=0count_xiang_su=0;
if(biBitCount==24)
{
for(int?i=0;i {
for(int?j=0;j {?
n++;
m=*(pBmpBuf+i*lineByte+j*3);
tiger_i[n]=m;
m=*(pBmpBuf+i*lineByte+j*3+1);
tiger_ib[n]=m;
m=*(pBmpBuf+i*lineByte+j*3+2);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????30054??2012-10-09?21:36??Cattotiger\0.bmp
?????文件??????30054??2012-10-09?21:36??Cattotiger\1.bmp
?????文件??????30054??2012-10-09?21:36??Cattotiger\2.bmp
?????文件??????30054??2012-10-09?21:36??Cattotiger\3.bmp
?????文件??????30054??2012-10-09?21:36??Cattotiger\4.bmp
?????文件??????30054??2012-10-09?21:36??Cattotiger\5.bmp
?????文件??????30054??2012-10-09?21:36??Cattotiger\6.bmp
?????文件??????30054??2012-10-09?21:36??Cattotiger\7.bmp
?????文件??????30054??2012-10-09?21:36??Cattotiger\8.bmp
?????文件??????30054??2012-10-08?20:20??Cattotiger\cat.bmp
?????文件???27807744??2012-10-09?21:37??Cattotiger\Cattotiger.sdf
?????文件????????886??2012-10-08?19:11??Cattotiger\Cattotiger.sln
????..A..H.?????63488??2012-10-09?21:37??Cattotiger\Cattotiger.suo
?????文件???????3919??2012-10-08?19:46??Cattotiger\Cattotiger.vcxproj
?????文件????????941??2012-10-08?19:46??Cattotiger\Cattotiger.vcxproj.filters
?????文件????????143??2012-10-08?19:11??Cattotiger\Cattotiger.vcxproj.user
?????文件??????58880??2012-10-09?21:36??Cattotiger\Debug\Cattotiger.exe
?????文件????????406??2012-10-08?19:46??Cattotiger\Debug\Cattotiger.exe.em
?????文件????????472??2012-10-08?19:46??Cattotiger\Debug\Cattotiger.exe.em
?????文件????????381??2012-10-09?21:36??Cattotiger\Debug\Cattotiger.exe.intermediate.manifest
?????文件?????694032??2012-10-09?21:36??Cattotiger\Debug\Cattotiger.ilk
?????文件?????????88??2012-10-09?21:36??Cattotiger\Debug\Cattotiger.lastbuildstate
?????文件???????3402??2012-10-09?21:36??Cattotiger\Debug\Cattotiger.log
?????文件?????863232??2012-10-09?21:36??Cattotiger\Debug\Cattotiger.pdb
?????文件????????210??2012-10-08?19:13??Cattotiger\Debug\Cattotiger_manifest.rc
?????文件????????726??2012-10-09?21:36??Cattotiger\Debug\cl.command.1.tlog
?????文件??????20680??2012-10-09?21:36??Cattotiger\Debug\CL.read.1.tlog
?????文件????????452??2012-10-09?21:36??Cattotiger\Debug\CL.write.1.tlog
?????文件??????????2??2012-10-09?21:36??Cattotiger\Debug\li
?????文件??????????2??2012-10-09?21:36??Cattotiger\Debug\li
............此處省略34個文件信息
評論
共有 條評論