木马编写--屏幕控制之BMP转JPG

 木马的一个重功能是屏幕控制

而为了在网络上传输方便要把截取的BMP图像转为体积较小的JPG格式

在IDE中有些控件可以直接完成(如BC中的控件)

现在要提供的是一个纯C写的代码

在TC2.0下编译通过

#include <stdio.h>
#include <alloc.h>
#include <math.h>
#include <stdlib.h>

#define PI 3.1415927
#define widthbytes(i) ((i+31)/32*4)
#define round(x) (((x)<0)?(int)((x)-0.5):(int)((x)+0.5))

unsigned char shiftbits=0x80,tempcode=0;
int sampleYH,sampleYV,sampleUH,sampleUV,sampleVH,sampleVV;
int HYtoU,VYtoU,HYtoV,VYtoV,YinMCU,UinMCU,VinMCU,compressnum;
unsigned char YDCindex,YACindex,UVDCindex,UVACindex;
unsigned char compressindex[3],HufTabindex;
int blockbuffer[64],QtZMCUbuffer[10*64];
int ycoef=0,ucoef=0,vcoef=0,restart=0;
long Y[4*64],U[4*64],V[4*64],MCUbuffer[10*64];
unsigned long imgwidth=0,imgheight=0,width=0,height=0,linebytes;
unsigned char *YQt,*UQt,*VQt;
unsigned char Qt[2][64],qt[2][64]=
     {{16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,
       14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,
       18,22,37,56,68,109,103,77,24,35,55,64,81,104,113,92,
       49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99},
      {17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,
       24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99,
       99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
       99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99}};
int Z[64]={0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,
    26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,
    49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,
    59,52,45,38,31,39,46,53,60,61,54,47,55,62,63};
unsigned char codesize[4][16]={
  {0x0,0x1,0x5,0x1,0x1,0x1,0x1,0x1,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x0},
  {0x0,0x3,0x1,0x1,0x1,0x1,0x1,0x1,0x1,0x1,0x1,0x0,0x0,0x0,0x0,0x0},
  {0x0,0x2,0x1,0x3,0x3,0x2,0x4,0x3,0x5,0x5,0x4,0x4,0x0,0x0,0x1,0x7d},
  {0x0,0x2,0x1,0x2,0x4,0x4,0x3,0x4,0x7,0x5,0x4,0x4,0x0,0x1,0x2,0x77}};
unsigned char hindex[4][256]={
  {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0x0,0x0,0x0,0x0},
  {0x0,0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xa,0xb,0x0,0x0,0x0,0x0},
  {0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,
   0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,
   0x14,0x32,0x81,0x91,0xa1,0x08,0x23,0x42,0xb1,
   0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,
   0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,
   0x26,0x27,0x28,0x29,0x2a,0x34,0x35,0x36,0x37,
   0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,
   0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
   0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,
   0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,
   0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x92,0x93,
   0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,
   0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,
   0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,
   0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,
   0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2,
   0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,
   0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa},
  {0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,
   0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,
   0x32,0x81,0x08,0x14,0x42,0x91,0xa1,0xb1,0xc1,
   0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,
   0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,
   0x19,0x1a,0x26,0x27,0x28,0x29,0x2a,0x35,0x36,
   0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,
   0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,
   0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,
   0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,
   0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,
   0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,
   0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,
   0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,
   0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,
   0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,
   0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,
   0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa}};
int hsize[4][256],hcode[4][256],hufsize[4][256],hufcode[4][256],codenum[4];

struct{
 unsigned char type[2];
 long size;
 long reserved;
 long offset;
 }head;

struct{
 long size;
 long width;
 long height;
 int plane;
 int bitcount;
 long compression;
 long imagesize;
 long xpels;
 long ypels;
 long colorused;
 long colorimportant;
 }bmp;

void error(char *s)
 {
  printf("%s/n",s);
  exit(1);
 }

void readheader(FILE *fp)
 {
  fread(&head,sizeof(head),1,fp);
  fread(&bmp,sizeof(bmp),1,fp);
  if((bmp.bitcount!=24)||(bmp.compression!=0))
   error("Can not handle with this Bmp file!/n");
  imgwidth=bmp.width;
  imgheight=bmp.height;
  linebytes=widthbytes(24*imgwidth);
 }

void makehufsize( )
 {
  int i,j,k,n;
  for(n=0;n<4;n++)  {
   k=0;
   for(i=1;i<=16;i++)
    for(j=1;j<=codesize[n][i-1];j++)  {
     hsize[n][k]=i;
     k++;
    }
   hsize[n][k]=0;
   codenum[n]=k;
  }
 }

void makehufcode( )
 {
  int n,k,size,code;
  for(n=0;n<4;n++)  {
   k=0;
   code=0;
   size=hsize[n][0];
   for(;;)  {
    do  {
     hcode[n][k]=code;
     code++;
     k++;
    }while(hsize[n][k]==size);
    if(hsize[n][k]==0)
     break;
    do  {
     code=(code<<1);
     size++;
    }while(hsize[n][k]!=size);
   }
  }
 }

void order( )
 {
  int k,n,index;
  for(n=0;n<4;n++)
   for(k=0;k<codenum[n];k++)  {
    index=hindex[n][k];
    hufcode[n][index]=hcode[n][k];
    hufsize[n][index]=hsize[n][k];
   }
 }

void initialize( )
 {
  int i,j;
  sampleYH=2;sampleYV=2;
  sampleUH=1;sampleUV=1;
  sampleVH=1;sampleVV=1;
  compressnum=3;restart=0;
  for(i=0;i<2;i++)
   for(j=0;j<64;j++)
    Qt[i][j]=(qt[i][j]+1)>>1;
  makehufsize( );
  makehufcode( );
  order( );
 }

void maketag(FILE *fp)
 {
  unsigned char index,sample;
  int i,tag,length,size;
  struct  {
   int tag;
   int length;
   unsigned char signature[5];
   int version;
   unsigned char unit;
   int xdensity;
   int ydensity;
   unsigned char xthumbnail;
   unsigned char ythumbnail;
  }application;
  tag=0xd8ff;
  fwrite(&tag,sizeof(int),1,fp);
  application.tag=0xe0ff;
  application.length=0x1000;
  memcpy(application.signature,"JFIF",5);
  application.version=0x0101;
  application.unit=0;
  application.xdensity=0x0100;
  application.ydensity=0x0100;
  application.xthumbnail=0;
  application.ythumbnail=0;
  fwrite(&application,sizeof(application),1,fp);
  for(index=0;index<2;index++)  {
   tag=0xdbff;
   length=0x4300;
   fwrite(&tag,sizeof(int),1,fp);
   fwrite(&length,sizeof(int),1,fp);
   fputc(index,fp);
   for(i=0;i<64;i++)  {
     tag=Z[i];
     fputc(Qt[index][tag],fp);
   }
  }
  tag=0xc0ff;
  fwrite(&tag,sizeof(int),1,fp);
  length=0x1100;
  fwrite(&length,sizeof(int),1,fp);
  fputc(0x8,fp);
  size=(imgheight<<8)+(imgheight>>8);
  fwrite(&size,sizeof(int),1,fp);
  size=(imgwidth<<8)+(imgwidth>>8);
  fwrite(&size,sizeof(int),1,fp);
  fputc(compressnum,fp);
  fputc(1,fp);
  sample=(unsigned char)((sampleYH<<4)+sampleYV);
  fputc(sample,fp);
  fputc(0,fp);
  YQt=Qt[0];
  fputc(2,fp);
  sample=(unsigned char)((sampleUH<<4)+sampleUV);
  fputc(sample,fp);
  fputc(1,fp);
  UQt=Qt[1];
  fputc(3,fp);
  sample=(unsigned char)((sampleVH<<4)+sampleVV);
  fputc(sample,fp);
  fputc(1,fp);
  VQt=Qt[1];
  tag=0xc4ff;
  fwrite(&tag,sizeof(int),1,fp);
  length=0x1f00;
  fwrite(&length,sizeof(int),1,fp);
  fputc(0,fp);
  for(i=0;i<16;i++)
   fputc(codesize[0][i],fp);
  for(i=0;i<codenum[0];i++)
   fputc(hindex[0][i],fp);
  fwrite(&tag,sizeof(int),1,fp);
  length=0xb500;
  fwrite(&length,sizeof(int),1,fp);
  fputc(0x10,fp);
  for(i=0;i<16;i++)
   fputc(codesize[2][i],fp);
  for(i=0;i<codenum[2];i++)
   fputc(hindex[2][i],fp);
  fwrite(&tag,sizeof(int),1,fp);
  length=0x1f00;
  fwrite(&length,sizeof(int),1,fp);
  fputc(1,fp);
  for(i=0;i<16;i++)
   fputc(codesize[1][i],fp);
  for(i=0;i<codenum[1];i++)
   fputc(hindex[1][i],fp);
  fwrite(&tag,sizeof(int),1,fp);
  length=0xb500;
  fwrite(&length,sizeof(int),1,fp);
  fputc(0x11,fp);
  for(i=0;i<16;i++)
   fputc(codesize[3][i],fp);
  for(i=0;i<codenum[3];i++)
   fputc(hindex[3][i],fp);
  tag=0xdaff;
  fwrite(&tag,sizeof(int),1,fp);
  length=0x0c00;
  fwrite(&length,sizeof(int),1,fp);
  fputc(compressnum,fp);
  compressindex[0]=1;
  fputc(compressindex[0],fp);
  index=0;
  fputc(index,fp);
  YDCindex=index>>4;
  YACindex=(index&0x0f)+2;
  compressindex[1]=2;
  fputc(compressindex[1],fp);
  index=0x11;
  fputc(index,fp);
  UVDCindex=index>>4;
  UVACindex=(index&0x0f)+2;
  compressindex[2]=3;
  fputc(compressindex[2],fp);
  fputc(index,fp);
  fputc(0,fp);
  fputc(0x3f,fp);
  fputc(0,fp);
 }

void readbmp(FILE *fp)
 {
  int i,j;
  unsigned char r,g,b;
  long y,u,v,rr,gg,bb;
  for(i=0;i<sampleYV*8;i++)  {
   if((height+i)<imgheight)
    fseek(fp,(unsigned long)(imgheight-height-i-1)*linebytes+3*width+54,0);
   else
    fseek(fp,(unsigned long)(3*width+54),0);
   for(j=0;j<sampleYH*8;j++)  {
    if((width+j)<imgwidth)  {
     b=fgetc(fp);g=fgetc(fp);r=fgetc(fp);
     rr=(long)r;gg=(long)g;bb=(long)b;
     y=(77*rr+150*gg+29*bb)>>8;
     u=(-43*rr-85*gg+128*bb)>>8;
     v=(128*rr-107*gg-21*bb)>>8;
     if(y&0xffffff00) if (y>255) y=255; else if (y<0) y=0;
     if(u&0xffffff00) if (u>127) u=127; else if (u<-128) u=-128;
     if(v&0xffffff00) if (v>127) v=127; else if (v<-128) v=-128;
    }
    Y[i*8*sampleYH+j]=y;
    U[(i/VYtoU)*8*sampleYH+j/HYtoU]=u;
    V[(i/VYtoV)*8*sampleYH+j/HYtoV]=v;
   }
  }
 }

void writebits(FILE *fp,int codevalue,int bits)
 {
  unsigned int mask;
  mask=1L<<(bits-1);
  while(mask!=0)  {
   if(mask&codevalue)
    tempcode|=shiftbits;
   shiftbits>>=1;
   if(shiftbits==0)  {
    fputc(tempcode,fp);
    if(tempcode==0xff)
     fputc(0x00,fp);
    tempcode=0;
    shiftbits=0x80;
   }
   mask>>=1;
  }
 }

int getbits(int value)
 {
  int i,absvalue;
  i=0;absvalue=abs(value);
  while(absvalue)  {
   absvalue>>=1;
   i++;
  }
  return(i);
 }

void encodeAC(FILE *fp)
 {
  int k,r,bits,hufindex,value;
  k=0;r=0;
  while(k!=63)  {
   k++;
   value=blockbuffer[k];
   if(value==0)  {
    if(k==63)  {
     writebits(fp,hufcode[HufTabindex][0],
    hufsize[HufTabindex][0]);
     return;
    }
    else
     r++;
   }
   else  {
    while(r>15)  {
     writebits(fp,hufcode[HufTabindex][15*16],
    hufsize[HufTabindex][15*16]);
     r-=16;
    }
    bits=getbits(value);
    hufindex=16*r+bits;
    writebits(fp,hufcode[HufTabindex][hufindex],
   hufsize[HufTabindex][hufindex]);
    if(value<0)
     value=~(abs(value));
    writebits(fp,value,bits);
    r=0;
   }
  }
 }

void HufBlock(FILE *fp,unsigned char dchufindex,
             unsigned char achufindex)
 {
  int value,bits;
  HufTabindex=dchufindex;
  value=blockbuffer[0];
  bits=getbits(value);
  if(value<0)
   value=~(abs(value));
  writebits(fp,hufcode[HufTabindex][bits],
        hufsize[HufTabindex][bits]);
  writebits(fp,value,bits);
  HufTabindex=achufindex;
  encodeAC(fp);
 }

void writejpg(FILE *fp)
 {
  int i,j,*pQtZMCUbuffer;
  pQtZMCUbuffer=QtZMCUbuffer;
  for(i=0;i<sampleYH*sampleYV;i++)  {
   blockbuffer[0]=*pQtZMCUbuffer-ycoef;
   ycoef=*pQtZMCUbuffer++;
   for(j=1;j<64;j++)
    blockbuffer[j]=*pQtZMCUbuffer++;
   HufBlock(fp,YDCindex,YACindex);
  }
  for(i=0;i<sampleUH*sampleUV;i++)  {
   blockbuffer[0]=*pQtZMCUbuffer-ucoef;
   ucoef=*pQtZMCUbuffer++;
   for(j=1;j<64;j++)
    blockbuffer[j]=*pQtZMCUbuffer++;
   HufBlock(fp,UVDCindex,UVACindex);
  }
  for(i=0;i<sampleVH*sampleVV;i++)  {
   blockbuffer[0]=*pQtZMCUbuffer-vcoef;
   vcoef=*pQtZMCUbuffer++;
   for(j=1;j<64;j++)
    blockbuffer[j]=*pQtZMCUbuffer++;
   HufBlock(fp,UVDCindex,UVACindex);
  }
 }

void fdct(long *p,int k)
 {
  long x,x0,x1,x2,x3,x4,x5,x6,x7;
  x0=p[k*2]+p[k*5];x1=p[0]-p[k*7];
  x2=p[k*1]-p[k*6];x3=p[k*1]+p[k*6];
  x4=p[k*2]-p[k*5];x5=p[k*3]+p[k*4];
  x6=p[k*3]-p[k*4];x7=p[0]+p[k*7];
  x=x2;x2=x3-x0;x0+=x3;x3=x7-x5;x7+=x5;
  x5=(181*(x-x4)+128)>>8;x4=(181*(x+x4)+128)>>8;
  x=x7;x7=x6-x5;x5+=x6;x6=x1-x4;x4+=x1;
  x1=(x-x0)<<11;x0=((x+x0)<<11)+1024;
  x=1108*(x2+x3);x2=x-3784*x2;x3=x+1568*x3;
  x=565*(x4+x5);x4=x+2276*x4;x5=x-3406*x5;
  x=2408*(x6+x7);x6=x-799*x6;x7=x-4017*x7;
  p[0]=x0>>11;p[k*1]=x4>>11;p[k*2]=x3>>11;p[k*3]=x7>>11;
  p[k*4]=x1>>11;p[k*5]=x6>>11;p[k*6]=x2>>11;p[k*7]=x5>>11;
}

void FDCTint(long *metrix)
 {
  int i;
  for(i=0;i<8;i++)
   fdct(metrix+8*i,1);
  for(i=0;i<8;i++)
   fdct(metrix+i,8);
 }

void QtZBlock(long *s,int *d,unsigned char *pQt,int correct)
 {
  int i,j,tag;
  long *pbuffer,buffer[8][8];
  for(i=0;i<8;i++)
   for(j=0;j<8;j++)
    buffer[i][j]=(long)(s[8*i+j]-correct);
  pbuffer=(long *)buffer;
  FDCTint(pbuffer);
  pbuffer=(long *)buffer;
  for(i=0;i<8;i++)
   for(j=0;j<8;j++) {
    tag=Z[8*i+j];
    d[8*i+j]=round((float)(pbuffer[tag]>>3)/pQt[tag]);
   }
 }

void QtZMCU(int xx,int yy,int offset,unsigned char *pQt,int correct)
 {
  int i,j,*pQtZMCUBuffer;
  long *pMCUBuffer;
  pMCUBuffer=MCUbuffer+offset;
  pQtZMCUBuffer=QtZMCUbuffer+offset;
  for(i=0;i<yy;i++)
   for(j=0;j<xx;j++)
    QtZBlock(pMCUBuffer+(i*xx+j)*64,pQtZMCUBuffer+(i*xx+j)*64,pQt,correct);
 }

void getMCU(int xx,int yy,long *buf,int offset)
 {
  int i,j,k,n;
  long *pMCU;
  pMCU=MCUbuffer+offset;
  for(i=0;i<yy;i++)
   for(j=0;j<xx;j++)
    for(k=0;k<8;k++)
     for(n=0;n<8;n++)
      *pMCU++=buf[(i*8+k)*sampleYH*8+j*8+n];
 }

void encode(FILE *fp1,FILE *fp2)
 {
  int Yinbuf,Uinbuf,Vinbuf,tag;
  YinMCU=sampleYH*sampleYV;
  UinMCU=sampleUH*sampleUV;
  VinMCU=sampleVH*sampleVV;
  HYtoU=sampleYH/sampleUH;
  VYtoU=sampleYV/sampleUV;
  HYtoV=sampleYH/sampleVH;
  VYtoV=sampleYV/sampleVV;
  Yinbuf=0;Uinbuf=YinMCU*64;
  Vinbuf=(YinMCU+UinMCU)*64;
  while(1)  {
   readbmp(fp1);
   getMCU(sampleYH,sampleYV,Y,Yinbuf);
   getMCU(sampleUH,sampleUV,U,Uinbuf);
   getMCU(sampleVH,sampleVV,V,Vinbuf);
   QtZMCU(sampleYH,sampleYV,Yinbuf,YQt,128);
   QtZMCU(sampleUH,sampleUV,Uinbuf,UQt,0);
   QtZMCU(sampleVH,sampleVV,Vinbuf,VQt,0);
   writejpg(fp2);
   width+=sampleYH*8;
   if(width>=imgwidth) {
    width=0;
    height+=sampleYV*8;
   }
   if((width==0)&&(height>=imgheight))  {
    if(shiftbits!=0)
     writebits(fp2,0xff,8);
    break;
   }
  }
  tag=0xd9ff;
  fwrite(&tag,sizeof(int),1,fp2);
 }

main( )
 {
  FILE *fp1,*fp2;
  char name[]=" ";
  printf("please input the bmp file name");
  scanf("%s",name);
  if((fp1=fopen(name,"rb"))==NULL)
   error("Can not open Bmp File!");
  if((fp2=fopen("jpg.jpg","wb"))==NULL)
   error("Can not create Jpg File!");
  initialize( );
  readheader(fp1);
  maketag(fp2);
  encode(fp1,fp2);
  fclose(fp1);fclose(fp2);
 }

 

 本文代码来自电脑编程与维护
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值