File: win.c

package info (click to toggle)
vic 2.8ucl4-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,864 kB
  • ctags: 9,033
  • sloc: ansic: 56,989; cpp: 44,560; tcl: 5,550; sh: 1,382; perl: 1,329; makefile: 357
file content (382 lines) | stat: -rw-r--r-- 10,243 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382





/************************************************************************
 *
 *  win.c, display routines for Win32 for tmndecode (H.263 decoder)
 *
 ************************************************************************/

/* Disclaimer of Warranty
 * 
 * These software programs are available to the user without any license fee
 * or royalty on an "as is" basis. The University of British Columbia
 * disclaims any and all warranties, whether express, implied, or
 * statuary, including any implied warranties or merchantability or of
 * fitness for a particular purpose.  In no event shall the
 * copyright-holder be liable for any incidental, punitive, or
 * consequential damages of any kind whatsoever arising from the use of
 * these programs.
 * 
 * This disclaimer of warranty extends to the user of these programs and
 * user's customers, employees, agents, transferees, successors, and
 * assigns.
 * 
 * The University of British Columbia does not represent or warrant that the
 * programs furnished hereunder are free of infringement of any
 * third-party patents.
 * 
 * Commercial implementations of H.263, including shareware, are subject to
 * royalty fees to patent holders.  Many of these patents are general
 * enough such that they are unavoidable regardless of implementation
 * design.
 * 
 */



/* Copyright  1996 Intel Corporation All Rights Reserved
 * 
 * Permission is granted to use, copy and distribute the software in this
 * file for any purpose and without fee, provided, that the above
 * copyright notice and this statement appear in all copies.  Intel makes
 * no representations about the suitability of this software for any
 * purpose.  This software is provided "AS IS."
 * 
 * Intel specifically disclaims all warranties, express or implied, and all
 * liability, including consequential and other indirect damages, for the
 * use of this software, including liability for infringement of any
 * proprietary rights, and including the warranties of merchantability and
 * fitness for a particular purpose.  Intel does not assume any
 * responsibility for any errors which may appear in this software nor any
 * responsibility to update it.  */

/************************************************************************
 * These routines were written by Intel Architecture Labs (IAL)
 *
 * v1 : Michael Third
 * v2 : Karl (removed unnecessary code, collected routines in one file)
 *           (moved and changed parts of message loop)
 *           (changed YUVtoRGB routine)
 *           (added synchronization with events
 *           instead of empty while loop)
 ************************************************************************/

#include "config.h"
#include "tmndec.h"
#include "global.h"
#include "win.h"

#ifdef WINDOWS

/* vdinit.c */

T_VDWINDOW vdWindow;

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

  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;


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

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

  }
  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.263 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:
      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);

  return errFlag;
}




#endif