File: maindlg.cpp

package info (click to toggle)
paintlib 2.6.2-14
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,920 kB
  • ctags: 3,874
  • sloc: cpp: 25,209; sh: 10,605; ansic: 1,891; makefile: 120
file content (507 lines) | stat: -rw-r--r-- 12,335 bytes parent folder | download | duplicates (2)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
/--------------------------------------------------------------------
|
|      $Id: maindlg.cpp,v 1.28 2004/09/11 12:41:37 uzadow Exp $
|
|      Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/

#include "stdafx.h"
#include "testdec.h"
#include "MainDlg.h"

#include "plpixelformat.h"
#include "planydec.h"
#include "plexcept.h"
#include "planybmp.h"
#include "pldibsect.h"
#include "plbmpenc.h"
#include "filter/PLFilterQuantize.h"

#ifdef TEST_DDRAW
#include "plddrawbmp.h"
#include "plddhelper.h"
#endif

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////
// Worker thread to display the pics.

UINT FadePic (LPVOID pParam)
{
  CDibtestDlg * pDlg = (CDibtestDlg *) pParam;

  pDlg->DoWork();

  return 0;
}


/////////////////////////////////////////////////////////////////////
// CDibtestDlg dialog

CDibtestDlg::CDibtestDlg (char * pszDirName)
    : CDialog(CDibtestDlg::IDD, NULL)
{
  //{{AFX_DATA_INIT(CDibtestDlg)
  // NOTE: the ClassWizard will add member initialization here
  //}}AFX_DATA_INIT
  // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

  m_pBmp = new PLDIBSection();
  m_pBigBmp = new PLWinBmp();

  InitializeCriticalSection(&m_Critical);
  m_pDIBWnd = new CMTDIBWnd (m_pBmp, &m_Critical);

  m_sDirName = pszDirName;
  if (m_sDirName.Right(1) != '\\')
    m_sDirName += '\\';

#ifdef TEST_DDRAW
  // Create the main DirectDraw object
  HRESULT hr = DirectDrawCreate(NULL, &m_pDDraw, NULL);
  CheckForDDError (hr);

  hr = m_pDDraw->SetCooperativeLevel( m_hWnd, DDSCL_NORMAL );
  CheckForDDError (hr);
#endif
}

CDibtestDlg::~CDibtestDlg ()
{
  delete m_pDIBWnd;

  delete m_pBmp;
  delete m_pBigBmp;

  delete m_pThreadCritical;

  DeleteCriticalSection(&m_Critical);

#ifdef TEST_DDRAW
  m_pDDraw->Release();
  m_pDDraw = NULL;
#endif
}

void CDibtestDlg::DoWork ()
{

  //---------------------- CBmp tests. ---------------------------

  PLAnyPicDecoder Decoder;
  Decoder.SetTraceConfig (2, NULL);  //"c:\paintlib.log");

  m_bStopDecoding = false;

  m_pThreadCritical = new CCriticalSection ();
  m_pThreadCritical->Lock();

  // Basic bitmap tests
  doBmpTest (98, 98, PLPixelFormat::L1, IDB_TEST3);
  doBmpTest (100, 100, PLPixelFormat::I8, IDB_TEST8);
  doBmpTest (101, 101, PLPixelFormat::I8, 0);
  doBmpTest (103, 103, PLPixelFormat::X8R8G8B8, 0);

  // This can be used if a specific file should be tested:
  //  displayFile (&Decoder, "c:\\prog\\PAINTLIB\\testpic\\tiff\\pics\\fax2d.tif");
  //  displayFile (&Decoder, "c:\\prog\\PAINTLIB\\testpic\\rgb32.tga");
//  displayDir (&Decoder, "f:\\prog\\paintlib\\testpic\\");
  displayDir (&Decoder, m_sDirName);

  if (!m_bStopDecoding)
    SetDlgItemText (IDS_FNAME, "Test finnished.");

  m_pThreadCritical->Unlock ();

}


void CDibtestDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CDibtestDlg)
  // NOTE: the ClassWizard will add DDX and DDV calls here
  //}}AFX_DATA_MAP

}
BEGIN_MESSAGE_MAP(CDibtestDlg, CDialog)
//{{AFX_MSG_MAP(CDibtestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CREATE()
ON_WM_DESTROY()
	ON_WM_MOVE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////
// CDibtestDlg message handlers

BOOL CDibtestDlg::OnInitDialog()
{
  CDialog::OnInitDialog();

  // Set the icon for this dialog.
  SetIcon(m_hIcon, true);      // Set big icon
  SetIcon(m_hIcon, false);    // Set small icon

  CRect Rect;

  // Resize bitmap window so it fills the frame.
  CWnd * pWnd = GetDlgItem(IDS_BMPFRAME);
  if (pWnd)
  {
    pWnd->ShowWindow(SW_HIDE);
	pWnd->GetWindowRect(Rect);
    ScreenToClient (Rect);
    Rect.InflateRect (-2, -2);
    m_pDIBWnd->MoveWindow (Rect, true);
  }

  // Create DIBs as canvas.
  int Width = Rect.right-Rect.left;
  int Height = Rect.bottom-Rect.top;

  m_pBigBmp->Create (Width, Height, PLPixelFormat::DONTCARE);
  m_pBmp->Create (Width, Height, PLPixelFormat::DONTCARE);

  m_pThread = AfxBeginThread (FadePic, (LPVOID) this);
  m_pDIBWnd->NewDIBNotify();

  return true;  // return true  unless you set the focus to a control
}



void CDibtestDlg::OnPaint()
{
  if (IsIconic())
  {
    CPaintDC dc(this); // device context for painting

    SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

    // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2;

    // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
  }
  else
  {
    CDialog::OnPaint();
  }
}

HCURSOR CDibtestDlg::OnQueryDragIcon()
{
  return (HCURSOR) m_hIcon;
}

int CDibtestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  if (CDialog::OnCreate(lpCreateStruct) == -1)
    return -1;

  m_pDIBWnd->Create (NULL, "DIBWnd", 0, CRect (0,0,10,10),
                     this, 2001);

  return 0;
}

void CDibtestDlg::OnMove(int x, int y)
{
	CDialog::OnMove(x, y);

}

void CDibtestDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	// Resize bitmap window so it fills the frame.
	CWnd * pWnd = GetDlgItem(IDS_BMPFRAME);
	CRect Rect;

	if(pWnd)
	{
		m_pDIBWnd->GetWindowRect(Rect);

		ScreenToClient(Rect);

		Rect.bottom = cy -10;
		Rect.right  = cx -10;

		m_pDIBWnd->MoveWindow (Rect, true);
	}
}

/////////////////////////////////////////////////////////////////////
// CDibtestDlg locals

void CDibtestDlg::displayDir (PLPicDecoder * pDecoder, const char * pszDirName)
// Tries to display all files in a directory and its
// subdirectories.
{
  WIN32_FIND_DATA FindData;
  HANDLE hSearch;
  bool bOK;
  char sz[256];

  TRACE ("Searching directory %s.\n", pszDirName);

  displayFilesInDir (pDecoder, pszDirName, "*.bmp");
  displayFilesInDir (pDecoder, pszDirName, "*.eps");
  displayFilesInDir (pDecoder, pszDirName, "*.jpg");
  displayFilesInDir (pDecoder, pszDirName, "*.pct");
  displayFilesInDir (pDecoder, pszDirName, "*.png");
  displayFilesInDir (pDecoder, pszDirName, "*.tga");
  displayFilesInDir (pDecoder, pszDirName, "*.tif");
  displayFilesInDir (pDecoder, pszDirName, "*.pcx");
  displayFilesInDir (pDecoder, pszDirName, "*.gif");

  // Recurse directories

  sprintf (sz, "%s*.*", pszDirName);
  hSearch = FindFirstFile (sz, &FindData);
  bOK = (hSearch != INVALID_HANDLE_VALUE);
  while (bOK)
  {
    if (FindData.cFileName[0] != '.' &&
        (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
      sprintf (sz, "%s%s\\", pszDirName, FindData.cFileName);
      displayDir (pDecoder, sz);
    }
    if (m_bStopDecoding)
      return;
    bOK = (FindNextFile (hSearch, &FindData) != 0);
  }
  FindClose (hSearch);

}

void CDibtestDlg::displayFilesInDir (PLPicDecoder * pDecoder, const char * pszDirName,
                                     const char * pszFName)
// Tries to display all files in a directory matching
// the specified file name.
{
  WIN32_FIND_DATA FindData;
  HANDLE hSearch;
  bool bOK;
  char sz[256];

  sprintf (sz, "%s%s", pszDirName, pszFName);

  hSearch = FindFirstFile (sz, &FindData);
  bOK = (hSearch != INVALID_HANDLE_VALUE);
  while (bOK)
  {
    if (FindData.cFileName[0] != '.' &&
        !(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {
      sprintf (sz, "%s%s", pszDirName, FindData.cFileName);
      displayFile (pDecoder, sz);
    }
    if (m_bStopDecoding)
      return;
    bOK = (FindNextFile (hSearch, &FindData) != 0);
  }
  FindClose (hSearch);
}

void CDibtestDlg::displayFile (PLPicDecoder * pDecoder, char * pszFName)
{
#ifdef TEST_DDRAW
  PLDDrawBmp NewBmp (m_pDDraw);
#else
  PLWinBmp NewBmp;
#endif

  try
  {
    pDecoder->MakeBmpFromFile (pszFName, &NewBmp);
    if (m_bStopDecoding)
      return;
    EnterCriticalSection (&m_Critical);

    *m_pBmp = NewBmp;
    LeaveCriticalSection (&m_Critical);

    m_pDIBWnd->NewDIBNotify ();
    m_pDIBWnd->UpdateWindow ();

    CString s ("Decoding Image: ");
    s += pszFName;
    SetWindowText(s);
  }
  catch (PLTextException& e)
  {
    char sz[1024];
    sprintf (sz, "Error decoding %s: %s", pszFName, (const char *)e);
    PLPicDecoder::Trace (1, sz);

  }
  catch (CMemoryException*)
  {
    TRACE ("Error decoding %s: Out of memory.\n", pszFName);
  }
}

void CDibtestDlg::doBmpTest (int Width, int Height, const PLPixelFormat& pf, int ID)
// Tests the bitmap class used for defects by calling its methods
// with one type of bitmap.
{
  // Create empty
  EnterCriticalSection (&m_Critical);
  m_pBmp->Create (Width, Height, pf);
  LeaveCriticalSection (&m_Critical);

  m_pDIBWnd->NewDIBNotify ();
  m_pDIBWnd->UpdateWindow ();

  // Load from resource
  if (ID)
    m_pBigBmp->CreateRes (AfxGetInstanceHandle(), ID);
  else
    m_pBigBmp->Create (Width, Height, pf);

  // Copy constructor
  {
    PLWinBmp WinBmp (*m_pBigBmp);
    PLDIBSection DIBSect (*m_pBigBmp);
//    PLAnyBmp AnyBmp (*m_pBigBmp);
  }

  // Assignment
  {
    EnterCriticalSection (&m_Critical);
    *m_pBmp = *m_pBigBmp;
    PLAnyBmp AnyBmp;
    AnyBmp = *m_pBigBmp;
    *m_pBigBmp = AnyBmp;
    LeaveCriticalSection (&m_Critical);
  }

  m_pDIBWnd->NewDIBNotify ();
  m_pDIBWnd->UpdateWindow ();

  // Put in clipboard
  m_pBmp->ToClipboard();

  HBITMAP hBmp;

  // Create a PLWinBmp from a HBITMAP
  hBmp = m_pBmp->GetHandle();
  PLWinBmp TestBmp;
  TestBmp.CreateFromHBitmap(hBmp);

  // PLDIBSection tests
  hBmp = m_pBmp->GetHandle();
  BITMAPINFOHEADER * pBMI = m_pBmp->GetBMI();
  m_pBmp->Detach();
  DeleteObject (hBmp);
  free (pBMI);

}

void CDibtestDlg::OnDestroy()
{
  CDialog::OnDestroy();

  m_bStopDecoding = true;

  // Wait for the decoder thread to terminate.
  m_pThreadCritical->Lock (5000);
}
/*
/--------------------------------------------------------------------
|
|      $Log: maindlg.cpp,v $
|      Revision 1.28  2004/09/11 12:41:37  uzadow
|      removed plstdpch.h
|
|      Revision 1.27  2003/11/21 23:37:04  uzadow
|      no message
|
|      Revision 1.26  2002/08/04 20:08:01  uzadow
|      Added PLBmpInfo class, ability to extract metainformation from images without loading the whole image and proper greyscale support.
|
|      Revision 1.25  2002/03/31 13:36:42  uzadow
|      Updated copyright.
|
|      Revision 1.24  2001/10/21 17:12:40  uzadow
|      Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
|
|      Revision 1.23  2001/09/16 19:03:23  uzadow
|      Added global name prefix PL, changed most filenames.
|
|      Revision 1.22  2001/01/15 15:05:32  uzadow
|      Added CBmp::ApplyFilter() and CBmp::CreateFilteredCopy()
|
|      Revision 1.21  2001/01/15 12:10:13  uzadow
|      Added gif to list of file types.
|
|      Revision 1.20  2000/11/21 20:27:28  uzadow
|      Added crop test.
|
|      Revision 1.19  2000/11/02 21:28:47  uzadow
|      Fixed copy constructors.
|
|      Revision 1.18  2000/10/28 12:00:35  uzadow
|      Added AnyBmp copy constructor tests.
|
|      Revision 1.17  2000/09/26 12:14:50  Administrator
|      Refactored quantization.
|
|      Revision 1.16  2000/09/21 14:35:12  Administrator
|      Fixed bug in pcx decoder
|
|      Revision 1.15  2000/09/01 14:13:49  Administrator
|      Removed MFC from paintX, added MSCV paintX sample.
|
|      Revision 1.14  2000/08/13 12:11:44  Administrator
|      Added experimental DirectDraw-Support
|
|      Revision 1.13  2000/07/19 12:23:47  Ulrich von Zadow
|      Added CreateFromHBitmap test.
|
|      Revision 1.12  2000/05/28 11:10:18  Ulrich von Zadow
|      Enhanced test routines.
|
|      Revision 1.11  2000/03/15 17:31:27  Ulrich von Zadow
|      Added resizing code and icon by
|      Amos Di Gennaro.
|
|      Revision 1.10  2000/01/18 00:03:01  Ulrich von Zadow
|      Bugs.
|
|      Revision 1.9  2000/01/17 23:38:08  Ulrich von Zadow
|      MFC removal aftermath.
|
|      Revision 1.8  2000/01/16 20:43:18  anonymous
|      Removed MFC dependencies
|
|      Revision 1.7  2000/01/10 23:53:03  Ulrich von Zadow
|      Changed formatting & removed tabs.
|
|
\--------------------------------------------------------------------
*/