bmp zoom code of BMP bitmap zoom in and out arbitrarily
- 2020-05-09 19:03:06
- OfStack
A few days ago, I encountered the need to zoom the bmp bitmap.
Call API function, although it can achieve bitmap zoom, but it has a good effect on the zoom, zoom will cause distortion, there are flowers on the image, it is difficult to accept
, because I have learned yi language before, and yi language has 1 code, which has a very good effect on the image scaling of bmp.
Yesterday, I took the time to translate it into c++ code.
This section is to scale the width:
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;
// Bitmap width 1 = Fetch byte set data ( The bitmap data , # integer , 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;
}
}
This section is to scale the height:
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;
}
}
Example:
int main(int argc, char* argv[])
{
unsigned char *bmp,*bmp2;
bmp=(unsigned char*)malloc(1244214);
bmp2=(unsigned char*)malloc(1244214);
FILE *fp=fopen("D:\\ Boot screen .bmp","rb");
fread(bmp,1,304182,fp);
fclose(fp);
//bmpsetH(bmp,720,bmp2);
// memcpy(bmp,bmp2,1244214);
BmpSetV(bmp,290,bmp2);
fp=fopen("D:\\ boot .bmp","wb");
fwrite(bmp2,1,1244214,fp);
fclose(fp);
free(bmp);
free(bmp2);
return 0;
}
It's not easy to see this, but here's a site where you can share another piece of code
// suofang.cpp : Define the entry point for the console application.
//
#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; // Bitmap file type, must be BM(0-1 byte )
DWORD bfSize; // The size of a bitmap file in bytes (2-5 byte )
WORD bfReserved1; // Bitmap file reserved word, must be 0(6-7 byte )
WORD bfReserved2; // Bitmap file reserved word, must be 0(8-9 byte )
DWORD bfOffBits; // The starting position of a bitmap data relative to a bitmap (10-13 byte )
// The offset of the file header is represented in bytes
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize; // The number of bytes consumed by this structure (14-17 byte )
LONG biWidth; // The width of a bitmap in pixels (18-21 byte )
LONG biHeight; // The height of a bitmap in pixels (22-25 byte )
WORD biPlanes; // The level of the target device must be 1(26-27 byte )
WORD biBitCount;// The number of bits required per pixel must be 1( Double color ),(28-29 byte )
// 4(16 color ) . 8(256 color ) or 24( True color ) the 1
DWORD biCompression; // Bitmap compression type, must be 0( Without compression ),(30-33 byte )
// 1(BI_RLE8 The compression type ) or 2(BI_RLE4 The compression type ) the 1
DWORD biSizeImage; // The size of a bitmap in bytes (34-37 byte )
LONG biXPelsPerMeter; // Bitmap horizontal resolution, number of pixels per meter (38-41 byte )
LONG biYPelsPerMeter; // Bitmap vertical resolution, number of pixels per meter (42-45 byte )
DWORD biClrUsed;// The number of colors in the actual color table used by the bitmap (46-49 byte )
DWORD biClrImportant;// A bitmap shows the number of important colors in the process (50-53 byte )
} BITMAPINFOHEADER;
typedef struct tagRGBQUAD {
BYTE rgbBlue;// The brightness of blue ( Range of values for 0-255)
BYTE rgbGreen; // The brightness of green ( Range of values for 0-255)
BYTE rgbRed; // The brightness of red ( Range of values for 0-255)
BYTE rgbReserved;// Reservations must be made 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
/////////////////////////////////// Nearest neighbor value sampling method (fast) ////////////////////////////////////////////////
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;
}
}
//////////////////////////////////// End of the zoom ///////////////////////////////////////////////
#endif
#if 1
///////////////////////////////////// Bilinear interpolation (optimized) /////////////////////////////////////////////////////
int sw = bi.biWidth - 1, sh = bi.biHeight - 1, dw = bi1.biWidth - 1, dh = bi1.biHeight - 1; // Source image width, target image width
int B, N, x, y; // The calculated target point corresponds to the abscissa of the floating point number in the source image N And the vertical B, Target integer abscissa x And the vertical y
int nPixelSize = bi.biBitCount/8; // Pixel size
BYTE * pLinePrev, *pLineNext; // The row in the source image starts with coordinates and down 1 Line start coordinates
//BYTE * pDest;
BYTE * pA, *pB, *pC, *pD; // The closest in the source image 4 A point
for ( int i = 0; i <= dh; ++i ) // Increasing height
{
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 ) // Increasing the width of the
{
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;
}