⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 display.cpp

📁 h264编解码.用C++实现了图像的编解码功能。
💻 CPP
字号:
#include "Win.h"

T_VDWINDOW vdWindow;

/* Data for ConvertYUVtoRGB*/
long int crv_tab[256];
long int cbu_tab[256];
long int cgu_tab[256];

long int cgv_tab[256];
long int tab_76309[256];

unsigned char *clp;

int initDisplay (int pels, int lines)
{
  int errFlag = 0;
  int i;

  clp=(unsigned char *)malloc(1024);

  clp += 384;

  for (i=-384; i<640; i++)
    clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i);

  init_dither_tab();
  errFlag |= InitDisplayWindowThread (pels, lines);

  return errFlag;
}


int InitDisplayWindowThread (int width, int height)
{
  int errFlag = 0;

  /* now modify the couple that need it */
  vdWindow.width = width;
  vdWindow.height = height;
  vdWindow.biHeader.biWidth = vdWindow.width;
  vdWindow.biHeader.biHeight = vdWindow.height;
  vdWindow.biHeader.biSize = sizeof(BITMAPINFOHEADER);
  vdWindow.biHeader.biCompression = BI_RGB;
  vdWindow.biHeader.biPlanes = 1;
  vdWindow.biHeader.biBitCount = 24;


  vdWindow.biHeader.biSizeImage = 3 * vdWindow.width * vdWindow.height;
  vdWindow.imageIsReady = FALSE;

  /* allocate the memory needed to hold the RGB and visualization information */
  vdWindow.bufRGB = (unsigned char *)malloc (3 * vdWindow.width * vdWindow.height);

  /* Create synchronization event */
  vdWindow.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);

  vdWindow.hThread = 
    CreateThread (
      NULL, 
      0, 
      (LPTHREAD_START_ROUTINE) DisplayWinMain, 
      (LPVOID) NULL, 
      0, 
      &(vdWindow.dwThreadID)
    );

  if (vdWindow.hThread == NULL) {
    errFlag = 1;
    return errFlag;
  }

  return errFlag;
}


/* vddraw.c */

int displayImage (unsigned char *lum, unsigned char *Cr, unsigned char *Cb)
{
  int errFlag = 0;
  DWORD dwRetVal;

  /* wait until we have finished drawing the last frame */
  if (vdWindow.windowDismissed == FALSE) {
    vdWindow.src[0] = lum;
    vdWindow.src[1] = Cb;
    vdWindow.src[2] = Cr;

    /* wait until previous frame has been drawn */
    dwRetVal = WaitForSingleObject(vdWindow.hEvent,INFINITE);

    vdWindow.imageIsReady = TRUE;
    /* Post message to drawing thread's window to draw frame */
    PostMessage (vdWindow.hWnd, VIDEO_DRAW_FRAME, (WPARAM) NULL, (LPARAM) NULL);
	}
	
	return errFlag;
}


int DrawDIB()
{											      
  int errFlag = 0;

  errFlag |= 
    DrawDibDraw (
      vdWindow.hDrawDib, 
      vdWindow.hDC, 
      0, 
      0, 
      vdWindow.zoom * vdWindow.width, 
      vdWindow.zoom * vdWindow.height, 
      &vdWindow.biHeader, 
      vdWindow.bufRGB, 
      0, 
      0, 
      vdWindow.width, 
      vdWindow.height, 
      DDF_SAME_DRAW
    ); 
	return errFlag;
}




/* vdwinman.c */

void DisplayWinMain (void *dummy)
{
  int errFlag = 0;
  DWORD dwStyle;
	
  vdWindow.wc.style =  CS_BYTEALIGNWINDOW;				 
  vdWindow.wc.lpfnWndProc = MainWndProc;
  vdWindow.wc.cbClsExtra = 0;
  vdWindow.wc.cbWndExtra = 0;
  vdWindow.wc.hInstance = 0;
  vdWindow.wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  vdWindow.wc.hCursor = LoadCursor (NULL, IDC_ARROW);
//  vdWindow.wc.hbrBackground = GetStockObject (WHITE_BRUSH);
  vdWindow.wc.lpszMenuName = NULL;
  vdWindow.zoom = 1;
  strcpy (vdWindow.lpszAppName, "H.264 Display");
  vdWindow.wc.lpszClassName = vdWindow.lpszAppName;

  RegisterClass(&vdWindow.wc);


  dwStyle =  WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;

  vdWindow.hWnd = 
    CreateWindow (vdWindow.lpszAppName,
      vdWindow.lpszAppName,
      dwStyle,
      CW_USEDEFAULT,
      CW_USEDEFAULT,
      vdWindow.width + 6,
      vdWindow.height + 25,
      NULL,
      NULL,
      0,
      NULL
    );
	
  if (vdWindow.hWnd == NULL)
    ExitThread (errFlag = 1);
  
  ShowWindow(vdWindow.hWnd, SW_SHOWNOACTIVATE);
  UpdateWindow(vdWindow.hWnd);

  /* Message loop for display window's thread */
  while (GetMessage (&(vdWindow.msg), NULL, 0, 0)) {
    TranslateMessage (&(vdWindow.msg));
    DispatchMessage (&(vdWindow.msg));
  }
  ExitThread (0);
}


LRESULT APIENTRY MainWndProc (HWND hWnd, UINT msg, UINT wParam, LONG lParam)
{
  LPMINMAXINFO lpmmi;

  switch (msg) {
    case VIDEO_BEGIN:
		SetWindowPos( vdWindow.hWnd, 
			HWND_TOP,
			1000, 1000, 
			vdWindow.width + 6, vdWindow.height + 25, 
			SWP_NOMOVE);
      vdWindow.hDC = GetDC (vdWindow.hWnd);
      vdWindow.hDrawDib = DrawDibOpen();
      vdWindow.zoom = 1;
      vdWindow.oldzoom = 0;
      DrawDibBegin (
        vdWindow.hDrawDib, 
        vdWindow.hDC, 
        2*vdWindow.width, 
        2*vdWindow.height, 
        &vdWindow.biHeader, 
        vdWindow.width, 
        vdWindow.height, 
        0
      );
      SetEvent(vdWindow.hEvent);
      vdWindow.windowDismissed = FALSE;
      ReleaseDC (vdWindow.hWnd, vdWindow.hDC);
      break;
    case VIDEO_DRAW_FRAME:
      vdWindow.hDC = GetDC (vdWindow.hWnd);
      ConvertYUVtoRGB(
        vdWindow.src[0],
        vdWindow.src[1],
        vdWindow.src[2],
        vdWindow.bufRGB,
        vdWindow.width,
        vdWindow.height
      );
      /* draw the picture onto the screen*/
      DrawDIB();
      SetEvent(vdWindow.hEvent);
      ReleaseDC (vdWindow.hWnd, vdWindow.hDC);
      break;
    case VIDEO_END:
      /* Window has been closed.  The following lines handle the cleanup. */
      vdWindow.hDC = GetDC (vdWindow.hWnd);
      DrawDibEnd (vdWindow.hDrawDib);
      DrawDibClose (vdWindow.hDrawDib);
      ReleaseDC (vdWindow.hWnd, vdWindow.hDC);

      vdWindow.windowDismissed = TRUE;
      PostQuitMessage(0);
      break;

    case WM_CREATE:
      PostMessage(hWnd, VIDEO_BEGIN, 0, 0);
      break;
    case WM_SIZE:
      switch (wParam) {
        case SIZE_MAXIMIZED:
          vdWindow.zoom = 2;
          break;
        case SIZE_MINIMIZED:
          vdWindow.oldzoom = vdWindow.zoom;
        	break;
        case SIZE_RESTORED:
          if (vdWindow.oldzoom) {
            vdWindow.zoom = vdWindow.oldzoom;
            vdWindow.oldzoom = 0;
          }
          else
            vdWindow.zoom = 1;
          break;
        case SIZE_MAXHIDE:
          break;
        case SIZE_MAXSHOW:
          break;
      }
      PostMessage(hWnd,WM_PAINT,0,0);
      break;  
    case WM_GETMINMAXINFO:
      lpmmi = (LPMINMAXINFO) lParam;

      GetWindowRect (hWnd, &vdWindow.rect);
      lpmmi->ptMaxPosition.x = vdWindow.rect.left;
      lpmmi->ptMaxPosition.y = vdWindow.rect.top;

      lpmmi->ptMaxSize.x = 2 * (vdWindow.width) + 6;
      lpmmi->ptMaxSize.y = 2 * (vdWindow.height) + 25;
      break;
    case WM_DESTROY:
      /* Window has been closed.  The following lines handle the cleanup. */
      DrawDibEnd (vdWindow.hDrawDib);
      ReleaseDC (vdWindow.hWnd, vdWindow.hDC);
      DrawDibClose (vdWindow.hDrawDib);

      vdWindow.windowDismissed = TRUE;
      PostQuitMessage(0);
      break;
    case WM_PAINT:
      if (vdWindow.imageIsReady) {
        vdWindow.hDC = GetDC (vdWindow.hWnd);
        DrawDIB ();
        ReleaseDC (vdWindow.hWnd, vdWindow.hDC);
      }
      break;

  }
  return DefWindowProc (hWnd, msg, wParam, lParam);
}

/* vdclose.c */

int closeDisplay ()
{
  int errFlag = 0;

  if (vdWindow.hWnd) {
    PostMessage (vdWindow.hWnd, VIDEO_END, (WPARAM) NULL, (LPARAM) NULL);
    while (vdWindow.windowDismissed == FALSE)
      ;
  }

  if (vdWindow.hEvent)
    CloseHandle(vdWindow.hEvent);

  if (vdWindow.hThread)
    CloseHandle (vdWindow.hThread);

  free (vdWindow.bufRGB);  
  clp -= 384;
  free(clp);
  return errFlag;
}

void init_dither_tab()
{
  long int crv,cbu,cgu,cgv;
  int i;   
  
  crv = 104597; cbu = 132201;  /* fra matrise i global.h */
  cgu = 25675;  cgv = 53279;
  
  for (i = 0; i < 256; i++) {
    crv_tab[i] = (i-128) * crv;
    cbu_tab[i] = (i-128) * cbu;
    cgu_tab[i] = (i-128) * cgu;
    cgv_tab[i] = (i-128) * cgv;
    tab_76309[i] = 76309*(i-16);
  }
}

/*Converts YUV image to RGB (packed mode)*/

void ConvertYUVtoRGB(unsigned char *src0,unsigned char *src1,unsigned char *src2,
					 unsigned char *dst_ori,int width,int height)
{       
  extern long int crv_tab[];
  extern long int cbu_tab[];
  extern long int cgu_tab[];

  extern long int cgv_tab[];
  extern long int tab_76309[];

  int y11,y21;
  int y12,y22;
  int y13,y23;
  int y14,y24;
  int u,v; 
  int i,j;
  int c11, c21, c31, c41;
  int c12, c22, c32, c42;
  int temp;
  unsigned int DW;
  unsigned int *id1, *id2;
  unsigned char *py1,*py2,*pu,*pv;
  unsigned char *d1, *d2;

  temp = (width<<1)+width;
  d1 = dst_ori;
  d1 += temp *(height-1);//width*height*3 - width*3;
  d2 = d1 - temp;

  temp = ((width<<3)+width)>>2;
  
  py1 = src0; pu = src1; pv = src2;
	py2 = py1 + width;
 
	id1 = (unsigned int *)d1;
	id2 = (unsigned int *)d2;

  for (j = 0; j < height; j += 2) { 
    /* line j + 0 */
    for (i = 0; i < width; i += 4) {
      u = *pu++;
      v = *pv++;
      c11 = crv_tab[v];
      c21 = cgu_tab[u];
      c31 = cgv_tab[v];
      c41 = cbu_tab[u];
      u = *pu++;
      v = *pv++;
      c12 = crv_tab[v];
      c22 = cgu_tab[u];
      c32 = cgv_tab[v];
      c42 = cbu_tab[u];

      y11 = tab_76309[*py1++]; /* (255/219)*65536 */
      y12 = tab_76309[*py1++];
      y13 = tab_76309[*py1++]; /* (255/219)*65536 */
      y14 = tab_76309[*py1++];

      y21 = tab_76309[*py2++];
      y22 = tab_76309[*py2++];
      y23 = tab_76309[*py2++];
      y24 = tab_76309[*py2++];

      /* RGBR*/
      DW = ((clp[(y11 + c41)>>16])) |
           ((clp[(y11 - c21 - c31)>>16])<<8) |
           ((clp[(y11 + c11)>>16])<<16) |  
           ((clp[(y12 + c41)>>16])<<24);
      *id1++ = DW;

      /* GBRG*/
      DW = ((clp[(y12 - c21 - c31)>>16])) |
           ((clp[(y12 + c11)>>16])<<8) |  
           ((clp[(y13 + c42)>>16])<<16) |
           ((clp[(y13 - c22 - c32)>>16])<<24);
      *id1++ = DW;

      /* BRGB*/
      DW = ((clp[(y13 + c12)>>16])) |  
           ((clp[(y14 + c42)>>16])<<8) |
           ((clp[(y14 - c22 - c32)>>16])<<16) |
           ((clp[(y14 + c12)>>16])<<24);  
      *id1++ = DW;

      /* RGBR*/
      DW = ((clp[(y21 + c41)>>16])) |
           ((clp[(y21 - c21 - c31)>>16])<<8) |
           ((clp[(y21 + c11)>>16])<<16) |  
           ((clp[(y22 + c41)>>16])<<24);
      *id2++ = DW;

      /* GBRG*/
      DW = ((clp[(y22 - c21 - c31)>>16])) |
           ((clp[(y22 + c11)>>16])<<8) |  
           ((clp[(y23 + c42)>>16])<<16) |
           ((clp[(y23 - c22 - c32)>>16])<<24);
      *id2++ = DW;

      /* BRGB*/
      DW = ((clp[(y23 + c12)>>16])) |  
           ((clp[(y24 + c42)>>16])<<8) |
           ((clp[(y24 - c22 - c32)>>16])<<16) |
           ((clp[(y24 + c12)>>16])<<24);  
      *id2++ = DW;
    }
    id1 -= temp;
    id2 -= temp;
    py1 += width;
    py2 += width;
  }           
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -