BMP zoom code (BMP bitmap to enlarge and shrink arbitrarily) _c language

Source: Internet
Author: User
Tags bmp image reserved

A few days ago encountered the need to scale BMP bitmaps,

Call API functions, although it can achieve bitmap scaling, but the effect of amplification is good, narrowing will cause distortion, the image has a flower point, it is difficult to accept

, because I used to learn easy language, easy language has a piece of code, BMP image scaling effect is very good,

Yesterday, the time to translate it into C + + code, verified, very easy to use, post code:

This section is scaled to width:

void __stdcall Bmpseth (unsigned char *bitmap,int h,unsigned char *bitmap2) {int I,j,k,bitmaph1,bitmaph2,bitmapv1,bitmap
V2,bitmapsize1,bitmapsize2,bitmaplen1,bitmaplen2;	
int starth1,starth2,r,g,b,r1,g1,b1;
  Bitmap width 1 = fetch byte set data (bitmap data, #整数型, 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 scaled to 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&LT;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;
}

See this is really not easy, here cloud Habitat Community small make up for everyone to share another code

Suofang.cpp: Defines the entry point for a 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 bytes) DWORD bfsize;//bitmap file size, in bytes (2- 5 bytes) WORD bfReserved1; Bitmap file reserved word, must be 0 (6-7 bytes) word bfReserved2; Bitmap file reserved words must be 0 (8-9 bytes) DWORD bfoffbits; 

 The starting position of the bitmap data, expressed as an offset relative to the bitmap (10-13 bytes)//header, in bytes Bitmapfileheader; typedef struct tagbitmapinfoheader{DWORD bisize//The number of bytes (14-17 bytes) long biwidth//bitmap width, in pixels (18-21 bytes) long Biheight; The height of the bitmap, in pixels (22-25 bytes) WORD biplanes; Target device level, must be 1 (26-27 bytes) WORD bibitcount;//the number of digits required for each pixel must be 1 (two-color), (28-29 bytes)//4 (16 colors), 8 (256) or 24 (true color) of one DWORD bicompression; Bitmap compression type, must be 0 (not compressed), (30-33 bytes)//1 (bi_rle8 compression type) or 2 (bi_rle4 compression type) One of the DWORD bisizeimage; The size of the bitmap, in bytes (34-37 bytes) LONG bixpelspermeter; Bitmap horizontal resolution, per metre pixel (38-41 bytes) LONG biypelspermeter;
  Bitmap vertical resolution, number of pixels per metre (42-45 bytes) DWORD biclrused;//Bitmap actually uses the color in the table (46-49 bytes) DWORD biclrimportant;//Bitmap Display the number of important colors in the process (50-53 bytes)

  } Bitmapinfoheader; typedef struct TAGRGBQUAD {byte rgbblue;//blue brightness (value range is 0-255) byte Rgbgreen;//green Brightness (value range is 0-255) byte rgbred;//red light 
Degree (value range is 0-255) BYTE rgbreserved;//reserved, must be 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 (&AMP;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 +)/32*4*bi1.biheight; 
  Pbyte pBuf1 = new BYTE[DWSIZE1]; 

  BYTE *psrc,*pdest;

Ofstream file1 ("D:\\tmp.log"); #if 0///////////////////////////////////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; }////////////////////////////////////Zoom End///////////////////////////////////////////////#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 points correspond to the floating point coordinates n, ordinate B, the target integer horizontal axis x, the ordinate y int npixelsize = BI.BIBITCOUNT/8 in the source image;            Pixel size BYTE * plineprev, *plinenext;
  The line start coordinates in the source image and the next line start coordinates//byte * pdest;              BYTE PA, *PB, *pc, *PD; 
    The nearest four dots in the source image for (int i = 0; I <= dh; ++i)//height increments {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)//width increment {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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.