bmp 縮放代碼(BMP位圖隨意率性縮小 和減少)。本站提示廣大學習愛好者:(bmp 縮放代碼(BMP位圖隨意率性縮小 和減少))文章只能為提供參考,不一定能成為您想要的結果。以下是bmp 縮放代碼(BMP位圖隨意率性縮小 和減少)正文
頭幾天碰上須要對bmp位圖停止縮放的功效,
挪用API函數,固然能完成位圖縮放,然則對有縮小的後果好,減少會形成掉真,圖象上有花點,讓人難以接收
,由於自己之前學易說話,易說話有一段代碼,對bmp圖象縮放後果異常 好,
昨天抽閒,把它翻譯成c++代碼了,經歷證,異常 好用,帖上代碼:
這一段是對寬度停止縮放:
void __stdcall bmpsetH(unsigned char *bitmap,int h,unsigned char *bitmap2) { int i,j,k,bitmapH1,bitmapH2,bitmapV1,bitmapV2,bitmapSize1,bitmapSize2,bitmapLen1,bitmapLen2; int startH1,startH2,R,G,B,R1,G1,B1; // 位圖寬1 = 取字節集數據 (位圖數據, #整數型, 19) bitmapH1=*((int*)(bitmap+18)); bitmapV1=*((int*)(bitmap+22)); int redress1=bitmapH1%4; int bitmapNH1=bitmapH1*3+redress1; bitmapLen1=bitmapNH1*bitmapV1; bitmapSize1=bitmapLen1+54; int redress2=h%4; int bitmapNH2=h*3+redress2; bitmapLen2=bitmapNH2*bitmapV1; bitmapSize2=bitmapLen2+54; memcpy(bitmap2,bitmap,54); memcpy(bitmap2+2,&bitmapSize2,4); memcpy(bitmap2+18,&h,4); double Hval=(double)bitmapH1/h; double Hvala=0; int Hvalb=0,Hvalc=0; for (i=1;i<=bitmapV1;i++) { startH2=bitmapSize2-i*bitmapNH2; startH1=bitmapSize1-i*bitmapNH1; B1=bitmap[startH1]; G1=bitmap[startH1+1]; R1=bitmap[startH1+2]; for(j=1;j<=h;j++) { startH2=startH2+3; Hvala=Hvala+Hval; if (Hvala<1) { Hvalb=3; } else { Hvalc=Hvala; Hvalb=Hvalc*3; } startH1=bitmapSize1-i*bitmapNH1+Hvalb; B=(bitmap[startH1-3]+B1)/2; G=(bitmap[startH1-2]+G1)/2; R=(bitmap[startH1-1]+R1)/2; bitmap2[startH2-3]=B; bitmap2[startH2-2]=G; bitmap2[startH2-1]=R; B1=bitmap[startH1-3]; G1=bitmap[startH1-2]; R1=bitmap[startH1-1]; } Hvala=0; } }
這一段是對高度停止縮放:
void BmpSetV(unsigned char *bitmap,int v,unsigned char *bitmap2) { double Vval,Vvala=0; int i,j,Vvalb=0,bitH1,bitNH1,bitV1,redress,bitLen1,bitsize1,bitLen2,bitsize2; int start3=0,start2=0,start1=0,G1,B1,R1,R,G,B; bitH1=*((int*)(bitmap+18)); bitV1=*((int*)(bitmap+22)); redress=bitH1%4; bitNH1=bitH1*3+redress; bitLen1=bitNH1*bitV1; bitsize1=bitLen1+54; bitLen2=bitNH1*v; bitsize2=bitLen2+54; memcpy(bitmap2,bitmap,54); memcpy(bitmap2+2,&bitsize2,4); memcpy(bitmap2+22,&v,4); Vval=bitV1/(double)v; for (i=1;i<=bitH1;i++) { start3=bitsize1-bitNH1+i*3; B1=bitmap[start3-3]; G1=bitmap[start3-2]; R1=bitmap[start3-1]; for (j=1;j<=v;j++) { Vvala=Vvala+Vval; if (Vvala<1)Vvalb=1; else Vvalb=Vvala; start2=bitsize2-j*bitNH1+i*3; start1=bitsize1-Vvalb*bitNH1+i*3; if (Vvalb<bitV1) { start3=bitsize1-Vvalb*bitNH1-bitNH1+i*3; if(i==1) { bitmap2[start2-3]=bitmap[start1-3]; bitmap2[start2-2]=bitmap[start1-2]; bitmap2[start2-1]=bitmap[start1-1]; } else { B=(B1+bitmap[start1-3])/2; G=(G1+bitmap[start1-2])/2; R=(R1+bitmap[start1-1])/2; bitmap2[start2-3]=B; bitmap2[start2-2]=G; bitmap2[start2-1]=R; B1=bitmap[start3-3]; G1=bitmap[start3-2]; R1=bitmap[start3-1]; } } else { bitmap2[start2-3]=bitmap[start1-3]; bitmap2[start2-2]=bitmap[start1-2]; bitmap2[start2-1]=bitmap[start1-1]; } } Vvala=0; } }
示例:
int main(int argc, char* argv[]) { unsigned char *bmp,*bmp2; bmp=(unsigned char*)malloc(1244214); bmp2=(unsigned char*)malloc(1244214); FILE *fp=fopen("D:\\開機畫面.bmp","rb"); fread(bmp,1,304182,fp); fclose(fp); //bmpsetH(bmp,720,bmp2); // memcpy(bmp,bmp2,1244214); BmpSetV(bmp,290,bmp2); fp=fopen("D:\\開機.bmp","wb"); fwrite(bmp2,1,1244214,fp); fclose(fp); free(bmp); free(bmp2); return 0; }
看到這確切不輕易,這裡小編就為年夜家分享另外一個代碼
// suofang.cpp : 界說掌握台運用法式的進口點。 // #include "stdafx.h" #include struct header{ short BM; unsigned int filesize; unsigned int recv; unsigned int offset; unsigned int bitmapheadlong; unsigned int bitmapwith; unsigned int bitmaphight; short bitmappageinfo; short bitperpixel; unsigned int compress; unsigned int bitmapsize; unsigned int levelresolution; unsigned int verticalresolution; unsigned int colourmap; unsigned int colourmapsize; }bitmapheader; #if 0 typedef struct tagBITMAPFILEHEADER { WORD bfType; // 位圖文件的類型,必需為BM(0-1字節) DWORD bfSize; // 位圖文件的年夜小,以字節為單元(2-5字節) WORD bfReserved1; // 位圖文件保存字,必需為0(6-7字節) WORD bfReserved2; // 位圖文件保存字,必需為0(8-9字節) DWORD bfOffBits; // 位圖數據的肇端地位,以絕對於位圖(10-13字節) // 文件頭的偏移量表現,以字節為單元 } BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER{ DWORD biSize; // 本構造所占用字節數(14-17字節) LONG biWidth; // 位圖的寬度,以像素為單元(18-21字節) LONG biHeight; // 位圖的高度,以像素為單元(22-25字節) WORD biPlanes; // 目的裝備的級別,必需為1(26-27字節) WORD biBitCount;// 每一個像素所需的位數,必需是1(雙色),(28-29字節) // 4(16色),8(256色)或24(真黑色)之一 DWORD biCompression; // 位圖緊縮類型,必需是 0(不緊縮),(30-33字節) // 1(BI_RLE8緊縮類型)或2(BI_RLE4緊縮類型)之一 DWORD biSizeImage; // 位圖的年夜小,以字節為單元(34-37字節) LONG biXPelsPerMeter; // 位圖程度分辯率,每米像素數(38-41字節) LONG biYPelsPerMeter; // 位圖垂直分辯率,每米像素數(42-45字節) DWORD biClrUsed;// 位圖現實應用的色彩表中的色彩數(46-49字節) DWORD biClrImportant;// 位圖顯示進程中主要的色彩數(50-53字節) } BITMAPINFOHEADER; typedef struct tagRGBQUAD { BYTE rgbBlue;// 藍色的亮度(值規模為0-255) BYTE rgbGreen; // 綠色的亮度(值規模為0-255) BYTE rgbRed; // 白色的亮度(值規模為0-255) BYTE rgbReserved;// 保存,必需為0 } RGBQUAD; #endif void scale(int srcwith,int srcheight,int destwith,int destheight) { } int _tmain(int argc, _TCHAR* argv[4000000]) { BITMAPFILEHEADER bmfHdr; BITMAPINFOHEADER bi,bi1; CFile file("d:\\pic1.bmp",CFile::modeRead); file.Read(&bmfHdr, sizeof(BITMAPFILEHEADER)); file.Read(&bi,sizeof(BITMAPINFOHEADER)); DWORD dwSize = (bi.biWidth*bi.biBitCount+31)/32*4*bi.biHeight; PBYTE pBuf = new BYTE[dwSize]; file.Read(pBuf,dwSize); file.Close(); ///////////////////////////////////////////////////////////////////////// file.Open("d:\\tmp1.bmp",CFile::modeCreate|CFile::modeReadWrite); memcpy(&bi1,&bi, sizeof(BITMAPINFOHEADER)); bi1.biWidth = 4000; //400->200 ,718->359 bi1.biHeight = 4000; //266->133,397->794 DWORD dwSize1 = (bi1.biWidth * bi1.biBitCount + 31)/32*4*bi1.biHeight; PBYTE pBuf1 = new BYTE[dwSize1]; BYTE *pSrc,*pDest; //ofstream file1("D:\\tmp.log"); #if 0 ///////////////////////////////////最臨近值采樣法(速度快)//////////////////////////////////////////////// float m_xscale,m_yscale; m_xscale = (float)bi.biWidth/(float)bi1.biWidth; m_yscale = (float)bi.biHeight/(float)bi1.biHeight; unsigned long k=0; for(int y = 0; y { for(int x=0; x { pSrc = pBuf+(int)(y*m_yscale)*bi.biWidth*3+(int)(x*m_xscale)*3; pDest = pBuf1+(int)(y*bi1.biWidth*3+x*3); memcpy(pDest,pSrc,3); //*(unsigned long*)pDest=k++; //*(unsigned long*)pDest=0x000000ff; } } ////////////////////////////////////縮放停止/////////////////////////////////////////////// #endif #if 1 /////////////////////////////////////雙線性插值法(經由優化)///////////////////////////////////////////////////// int sw = bi.biWidth - 1, sh = bi.biHeight - 1, dw = bi1.biWidth - 1, dh = bi1.biHeight - 1; //源圖象寬度,目的圖象寬度 int B, N, x, y; //盤算出的目的點對應於源圖象中的浮點數橫坐標N、縱坐標B,目的整數橫坐標x、縱坐標y int nPixelSize = bi.biBitCount/8; //像素年夜小 BYTE * pLinePrev, *pLineNext; //源圖象中的行開端坐標和下一行開端坐標 //BYTE * pDest; BYTE * pA, *pB, *pC, *pD; //源圖象中最臨近的四個點 for ( int i = 0; i <= dh; ++i ) //高度遞增 { pDest = ( BYTE * )(pBuf1+bi1.biWidth*i*nPixelSize); y = i * sh / dh; N = dh - i * sh % dh; pLinePrev = ( BYTE * )(pBuf+bi.biWidth*y*nPixelSize); y++; pLineNext = ( N == dh ) ? pLinePrev : ( BYTE * )(pBuf+bi.biWidth*y*nPixelSize); for ( int j = 0; j <= dw; ++j ) //寬度遞增 { x = j * sw / dw * nPixelSize; B = dw - j * sw % dw; pA = pLinePrev + x; pB = pA + nPixelSize; pC = pLineNext + x; pD = pC + nPixelSize; if ( B == dw ) { pB = pA; pD = pC; } for ( int k = 0; k < nPixelSize; ++k ) *pDest++ = ( BYTE )( int )( ( B * N * ( *pA++ - *pB - *pC + *pD ) + dw * N * *pB++ + dh * B * *pC++ + ( dw * dh - dh * B - dw * N ) * *pD++ + dw * dh / 2 ) / (double)( dw * dh ) ); } } #endif file.Write(&bmfHdr,sizeof(BITMAPFILEHEADER)); file.Write(&bi1,sizeof(BITMAPINFOHEADER)); file.Write(pBuf1,dwSize1); file.Close(); //==release p memory== delete pBuf; pBuf = NULL; delete pBuf1; pBuf1 = NULL; //==release p memory=== if (pDest!=NULL) { pDest = NULL; } //if (pSrc!=NULL) //{ //pSrc = NULL; //} ////////////////////////////////////////////////////////////// //MessageBox("Test successful","Info",MB_OKCANCEL); return 0; }