基于平面格式YUV422的图像缩放

    技术2022-05-19  20

    void __fastcall TForm1::N3202401Click(TObject *Sender){        FILE *fp;        fp = fopen("E://±àÂëÏîÄ¿//YUVË«ÏßÐԱ任//320&240YUV422.yuv", "rb");        unsigned char *YUV=new unsigned char[320*240*2];        fread(YUV, sizeof(char), 320*240*2, fp);

            double Y,U,V;

            unsigned short height,width;        unsigned short heightNew,widthNew;  //ÐÂͼÏñµÄ¸ß¶ÈºÍ¿í¶È

            float k=0.525;        float widthScale=(float)(1.0/k),heightScale=(float)(1.0/k);        float xx,yy;        int a,b;

            width=320;                    //ԭͼÏñ´óС        height=240;        widthNew=(int)(width*k);      //µÃµ½ÐÂͼÏñµÄ¸ß¶ÈºÍ¿í¶È        heightNew =(int)(height*k);        unsigned char *YUVnew=new unsigned char[widthNew*heightNew*2];

            for(int y=(int)k;y<heightNew-k;y++)        {           for(int x=(int)k; x<widthNew-k;x++)           {               xx=x*widthScale ;               yy=y*heightScale ;               if(xx<=1e-8)               {                  xx=0;               }               if(xx>width-2)               {                  xx=(float)(width-2);               }               if(yy<=1e-8)               {                  yy=0;               }               if(yy>height-2)               {                  yy=(float)(height-2);               }               a=(int)xx;               b=(int)yy;

                   //·Ö±ðµÃµ½¶ÔÓ¦ÏñËØµÄYUVÖµ²¢ÓÃË«ÏßÐÔ²åÖµµÃµ½ÐÂÏñËØµÄYUVÖµ               double y11,y12,y21,y22;               double u11,u12,u21,u22;               double v11,v12,v21,v22;               y11=YUV[b*320+a];               y12=YUV[b*320+a+1];               y21=YUV[(b+1)*320+a];               y22=YUV[(b+1)*320+a+1];               Y=y11*(a+1-xx)*(b+1-yy)+y12*(a+1-xx)*(yy-b)                        +y21*(xx-a)*(b+1-yy)+y22*(xx-a)*(yy-b);  //Y               YUVnew[x+y*widthNew]=(char)Y;

                   u11=YUV[320*240+b*320/2+a];               u12=YUV[320*240+b*320/2+a+1];               u21=YUV[(b+1)*320/2+320*240+a];               u22=YUV[(b+1)*320/2+320*240+a+1];               U=u11*(a+1-xx)*(b+1-yy)+u12*(a+1-xx)*(yy-b)                        +u21*(xx-a)*(b+1-yy)+u22*(xx-a)*(yy-b);   //U

                   v11=YUV[320*240+320*240/2+b*320/2+a];               v12=YUV[320*240+320*240/2+b*320/2+a+1];               v21=YUV[(b+1)*320/2+320*240+320*240/2+a];               v22=YUV[(b+1)*320/2+320*240+320*240/2+a+1];               V=v11*(a+1-xx)*(b+1-yy)+v12*(a+1-xx)*(yy-b)                        +v21*(xx-a)*(b+1-yy)+v22*(xx-a)*(yy-b);   //V

                   YUVnew[x+y*widthNew]=(char)Y;               YUVnew[widthNew*heightNew+x+y*widthNew/2]=(char)U;               YUVnew[widthNew*heightNew+widthNew*heightNew/2+x+y*widthNew/2]=(char)V;                          }        }        /*Ëõ·ÅºóµÄYUVתRGB²¢ÏÔʾ*/        int col,row;        double red,blue,green;        unsigned char *RGB = new unsigned char [widthNew*heightNew*3]; for (row=0; row<heightNew; row++) {  int idx=((heightNew-row-1)*3)*widthNew;  int rowptr=row*widthNew;  for (col=0; col<widthNew; col++)  {   int colhalf=col>>1;   Y=YUVnew[rowptr+col];   U=YUVnew[rowptr+colhalf+widthNew*heightNew-row*widthNew/2];   V=YUVnew[rowptr+colhalf+widthNew*heightNew-row*widthNew/2+widthNew*heightNew/2];

       red=(Y+(U-128)*1.4022)+0.5;   green=(Y-(U-128)*0.3456-(V-128)*0.7145)+0.5;   blue=(Y+(V-128)*1.7710)+0.5;

       if (red>255) red=255;   else if (red<0) red=0;   if (green>255) green=255;   else if (green<0) green=0;   if (blue>255) blue=255;   else if (blue<0) blue=0;

       RGB[idx++]=(char)red;   RGB[idx++]=(char)green;   RGB[idx++]=(char)blue;  } }        Graphics::TBitmap *pBitNew;        pBitNew = new Graphics::TBitmap();        pBitNew->Width=widthNew;        pBitNew->Height=heightNew;        Byte *ptr;        pBitNew->PixelFormat=pf24bit; //¶¨Òå24λµÄλͼ        for(int i=0; i<heightNew;i++)        {           ptr= (Byte*)(pBitNew->ScanLine[i]);           int RGBIndex=3*widthNew*(heightNew-i);           for(int j=0;j<widthNew*3;j+=3)           {               int B=RGB[RGBIndex+j];               int G=RGB[RGBIndex+j+1];               int R=RGB[RGBIndex+j+2];               *(ptr+j)= B;               *(ptr+j+1)= G;               *(ptr+j+2)= R;           }        }        Image2->Picture->Bitmap->Assign(pBitNew);        delete pBitNew;}


    最新回复(0)