-
Notifications
You must be signed in to change notification settings - Fork 16
/
Controls.cpp
1376 lines (1326 loc) · 49.6 KB
/
Controls.cpp
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
// //
// PaperBack -- high density backups on the plain paper //
// //
// Copyright (c) 2007 Oleh Yuschuk //
// ollydbg at t-online de (set Subject to 'paperback' or be filtered out!) //
// //
// //
// This file is part of PaperBack. //
// //
// Paperback is free software; you can redistribute it and/or modify it under //
// the terms of the GNU General Public License as published by the Free //
// Software Foundation; either version 3 of the License, or (at your option) //
// any later version. //
// //
// PaperBack is distributed in the hope that it will be useful, but WITHOUT //
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for //
// more details. //
// //
// You should have received a copy of the GNU General Public License along //
// with this program. If not, see <http://www.gnu.org/licenses/>. //
// //
// //
// Note that bzip2 compression/decompression library, which is the part of //
// this project, is covered by different license, which, in my opinion, is //
// compatible with GPL. //
// //
////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <direct.h>
#include <math.h>
#include "twain.h"
#pragma hdrstop
#include "paperbak.h"
#include "resource.h"
static HFONT hfont20; // 20-point bold font
static int font20height; // Real height of hfont20
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// BUTTONS ////////////////////////////////////
#define BTN_PRINT 1000 // Identifier of Print button
#define BTN_SCAN 1001 // Identifier of Scan button
#define BTN_READ 1002 // Identifier of Open bitmap button
#define BTN_STOP 1003 // Identifier of Stop button
#define BTN_PAGE 1004 // Identifier of Page setup button
#define BTN_OPTIONS 1005 // Identifier of Options button
#define BTN_CLOSE 1006 // Identifier of Close button
static HWND hbuttonframe; // Frame that owns the buttons
static HWND hprint; // Print button
static HWND hscan; // Scan button
static HWND hopen; // Open bitmap button
static HWND hstop; // Interrupt current operation
static HWND hpage; // Page setup button
static HWND hoptions; // Options button
static HWND hclose; // Close button
// Windows function of button frame.
LRESULT CALLBACK Buttonframewp(HWND hw,UINT msg,WPARAM wp,LPARAM lp) {
RECT rc;
PAINTSTRUCT ps;
HDC dc;
switch (msg) {
case WM_ERASEBKGND:
return 1; // Erasing is not necessary
case WM_PAINT:
GetClientRect(hw,&rc);
dc=BeginPaint(hw,&ps);
FillRect(dc,&rc,graybrush);
EndPaint(hw,&ps);
break;
case WM_COMMAND:
if (HIWORD(wp)!=BN_CLICKED)
break;
switch (LOWORD(wp)) {
case BTN_PRINT: // Print button pressed
if (Selectinfile()==0)
Printfile(infile,NULL);
break;
case BTN_SCAN: // Scan button pressed
OpenTWAINinterface();
Updatebuttons();
break;
case BTN_READ: // Open bitmap button pressed
Decodebitmap(NULL);
break;
case BTN_STOP: // Stop button pressed
Stopbitmapdecoding(&procdata);
Stopprinting(&printdata);
Clearqueue();
Updatebuttons();
Message("Processing interrupted",0);
break;
case BTN_PAGE: // Page setup button pressed
Setuppage();
break;
case BTN_OPTIONS: // Options button pressed
Options();
break;
case BTN_CLOSE: // Close button pressed
PostMessage(hwmain,WM_CLOSE,0,0);
break;
};
break;
default: return DefWindowProc(hw,msg,wp,lp);
};
return 0L;
};
// Enables or disables buttons according to the processing mode. Button "Close"
// is always enabled.
void Updatebuttons(void) {
if (procdata.step!=0 || printdata.step!=0) {
EnableWindow(hprint,0);
EnableWindow(hscan,0);
EnableWindow(hopen,0);
EnableWindow(hstop,1);
EnableWindow(hpage,0);
EnableWindow(hoptions,0); }
else {
EnableWindow(hprint,1);
if (twainstate<=1) {
EnableWindow(hscan,0);
EnableWindow(hopen,1); }
else if (twainstate<=3) {
EnableWindow(hscan,1);
EnableWindow(hopen,1); }
else {
EnableWindow(hscan,0);
EnableWindow(hopen,0); };
EnableWindow(hstop,0);
EnableWindow(hpage,1);
EnableWindow(hoptions,1);
};
// Now update the whole main window.
RedrawWindow(hwmain,NULL,NULL,RDW_UPDATENOW|RDW_ALLCHILDREN);
SetFocus(hwmain);
};
// Creates button frame and fills it with buttons. Frame owns buttons and so
// receives their BN_CLICKED notifications.
void Createbuttons(RECT *rc) {
int x,dx;
WNDCLASS wc;
// Register window class of button frame.
wc.style=CS_OWNDC;
wc.lpfnWndProc=Buttonframewp;
wc.cbClsExtra=wc.cbWndExtra=0;
wc.hInstance=hinst;
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=graybrush;
wc.lpszMenuName=NULL;
wc.lpszClassName=BUTTONFRAME;
if (!RegisterClass(&wc))
return;
// Create button frame.
hbuttonframe=CreateWindow(
BUTTONFRAME,"",WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,
rc->left,rc->top+2,rc->right-rc->left,BUTTONDY,
hwmain,NULL,hinst,NULL);
if (hbuttonframe==NULL)
return;
rc->top+=(BUTTONDY+DELTA+4);
// Calculate button width. Button frame contains 7 buttons.
dx=(rc->right-rc->left-(7-1)*DELTA)/7;
x=0;
// Create Print button.
hprint=CreateWindow("BUTTON","Print",
WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
x,0,dx,BUTTONDY,
hbuttonframe,(HMENU)BTN_PRINT,hinst,NULL);
SendMessage(hprint,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
x+=dx+DELTA;
// Create Scan button.
hscan=CreateWindow("BUTTON","Scan",
WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
x,0,dx,BUTTONDY,
hbuttonframe,(HMENU)BTN_SCAN,hinst,NULL);
SendMessage(hscan,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
x+=dx+DELTA;
// Create Open bitmap button.
hopen=CreateWindow("BUTTON","Open bitmap",
WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
x,0,dx,BUTTONDY,
hbuttonframe,(HMENU)BTN_READ,hinst,NULL);
SendMessage(hopen,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
x+=dx+DELTA;
// Create Stop button.
hstop=CreateWindow("BUTTON","Stop",
WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
x,0,dx,BUTTONDY,
hbuttonframe,(HMENU)BTN_STOP,hinst,NULL);
SendMessage(hstop,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
x+=dx+DELTA;
// Create Page setup button.
hpage=CreateWindow("BUTTON","Page setup",
WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
x,0,dx,BUTTONDY,
hbuttonframe,(HMENU)BTN_PAGE,hinst,NULL);
SendMessage(hpage,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
x+=dx+DELTA;
// Create Options button.
hoptions=CreateWindow("BUTTON","Options",
WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
x,0,dx,BUTTONDY,
hbuttonframe,(HMENU)BTN_OPTIONS,hinst,NULL);
SendMessage(hoptions,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
x+=dx+DELTA;
// Create Close button. It takes all the remaining space.
hclose=CreateWindow("BUTTON","Close",
WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON,
x,0,rc->right-rc->left-x,BUTTONDY,
hbuttonframe,(HMENU)BTN_CLOSE,hinst,NULL);
SendMessage(hclose,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
// Enable or disable buttons according to the actual state.
Updatebuttons();
};
////////////////////////////////////////////////////////////////////////////////
/////////////////////////// PROGRESS AND MESSAGE BAR ///////////////////////////
static HWND hprogress; // Progress and message bar
static char message[TEXTLEN]; // Text to display
static int showpercent; // Percentage (error if negative)
// Windows function of progress and message bar.
LRESULT CALLBACK Progresswp(HWND hw,UINT msg,WPARAM wp,LPARAM lp) {
int n;
char s[TEXTLEN+32];
RECT rblack,rwhite;
PAINTSTRUCT ps;
HDC dc;
switch (msg) {
case WM_ERASEBKGND:
return 1; // Erasing is not necessary
case WM_PAINT:
// Prepare text and calculate its length.
n=sprintf(s,"%s",message);
if (showpercent>0) n+=sprintf(s+n," - %i %%",showpercent);
// Prepare left and right rectangles.
GetClientRect(hw,&rblack);
rwhite=rblack;
if (showpercent<=0)
rblack.right=rblack.left;
else
rblack.right=rwhite.left=(rblack.right-rblack.left)*showpercent/100;
dc=BeginPaint(hw,&ps);
SelectObject(dc,hfont20);
SetTextAlign(dc,TA_TOP|TA_CENTER);
// Draw left rectangle - white text on black background.
SetTextColor(dc,RGB(255,255,255));
SetBkColor(dc,RGB(0,0,0));
ExtTextOut(dc,(rblack.left+rwhite.right)/2,1,
ETO_CLIPPED|ETO_OPAQUE,&rblack,s,n,NULL);
// Draw second rectangle - black or lightred text on white or gray
// background.
if (showpercent<0)
SetTextColor(dc,RGB(255,48,48));
else
SetTextColor(dc,RGB(0,0,0));
if (showpercent<=0)
SetBkColor(dc,GetSysColor(COLOR_BTNFACE));
else
SetBkColor(dc,RGB(255,255,255));
ExtTextOut(dc,(rblack.left+rwhite.right)/2,1,
ETO_CLIPPED|ETO_OPAQUE,&rwhite,s,n,NULL);
EndPaint(hw,&ps);
break;
default: return DefWindowProc(hw,msg,wp,lp);
};
return 0L;
};
// Creates progress and message bar and places it at the top of the specified
// rectangle.
HWND Createprogressbar(RECT *rc) {
int dy;
WNDCLASS wc;
// Register window class of progress bar.
wc.style=CS_OWNDC;
wc.lpfnWndProc=Progresswp;
wc.cbClsExtra=wc.cbWndExtra=0;
wc.hInstance=hinst;
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=PROGRESSCLASS;
if (!RegisterClass(&wc))
return NULL;
// Calculate required height of progress bar.
dy=font20height+2*GetSystemMetrics(SM_CXFRAME);
// Set initial text to display in progress bar.
sprintf(message,"PaperBack v%i.%02i",VERSIONHI,VERSIONLO);
showpercent=0;
// Create bar.
hprogress=CreateWindowEx(
WS_EX_CLIENTEDGE,PROGRESSCLASS,"",
WS_CHILD|WS_VISIBLE,
rc->left,rc->top,rc->right-rc->left,dy,
hwmain,NULL,hinst,NULL);
if (hprogress!=NULL)
rc->top+=(dy+DELTA);
return hprogress;
};
// Displays error message.
void Reporterror(char *text) {
if (hprogress==NULL || text==NULL) return;
strncpy(message,text,TEXTLEN-1);
message[TEXTLEN-1]='\0';
showpercent=-1;
RedrawWindow(hprogress,NULL,NULL,RDW_INVALIDATE|RDW_UPDATENOW);
};
// Displays progress. Call with percent=0 to display plain message.
void Message(char *text,int percent) {
if (hprogress==NULL || text==NULL) return;
if (percent==showpercent && strncmp(message,text,TEXTLEN-1)==0)
return; // Unchanged appearance
strncpy(message,text,TEXTLEN-1);
message[TEXTLEN-1]='\0';
showpercent=max(0,min(percent,100));
RedrawWindow(hprogress,NULL,NULL,RDW_INVALIDATE|RDW_UPDATENOW);
};
// Destroys progress bar and associated resources. Not absolutely necessary
// because Windows cleans up when application closes.
void Destroyprogressbar(void) {
DestroyWindow(hprogress); hprogress=NULL;
UnregisterClass(PROGRESSCLASS,hinst);
};
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// DATA DISPLAY /////////////////////////////////
#define BSEL_GRID 1000 // Identifier of Show grid box
#define BSEL_BAD 1001 // Identifier of Show bad dots box
#define BSEL_NONE 1002 // Identifier of No overlay box
#define BSEL_POS 1003 // Identifier of coordinate window
#define BSEL_LEFT 1004 // Identifier of L button
#define BSEL_RIGHT 1005 // Identifier of R button
#define BSEL_UP 1006 // Identifier of U button
#define BSEL_DOWN 1007 // Identifier of D button
static HWND hdataframe; // Frame that owns the data tab
static HWND hdatatab; // Data tab control
static HWND hdisplay; // Window that displays decoded data
static int displaymode; // Display mode, one of DISP_xxx
// Viewing parameters used in DISP_QUALITY mode.
static HBRUSH htone[17]; // Quality brushes
static HBRUSH hbad; // Brush for unreadable blocks
static uchar *qualitymap; // Quality map (255: empty, 17: bad)
static int mapnx; // Map size in X, blocks
static int mapny; // Map size in Y, blocks
static int mapscale; // Map scale, pixels per block
static RECT maprect; // Page rectangle, pixels
// Viewing parameters used in DISP_BLOCK mode.
static HDC blockdc; // Memory DC that keeps block image
static HBITMAP blockbmp; // Bitmap that keeps block image
static uchar *blockbits; // Pointer to block image bitmap
static int blockdx; // X size of block image bitmap
static int blockdy; // Y size of block image bitmap
static int blockindex; // Index of displayed block
// Controls in block selection window.
static HWND hblocksel; // Block selection window
static HWND hshowgrid; // Always show full grid
static HWND hshowbad; // Show only frames around bad pixels
static HWND hshownone; // Show neither grid nor frames
static HWND hpos; // Window displaying block coordinates
static HWND hleft; // Move left selector
static HWND hright; // Move right selector
static HWND hup; // Move up selector
static HWND hdown; // Move down selector
static int blockselx; // X index of selected block
static int blocksely; // Y index of selected block
static int blockdotmode; // Overlay mode, one of BSEL_xxx
// Windows function of data frame window. This window is fully covered by the
// data tab, so drawing is not necessary.
LRESULT CALLBACK Dataframewp(HWND hw,UINT msg,WPARAM wp,LPARAM lp) {
int i;
switch (msg) {
case WM_NOTIFY:
if (((NMHDR *)lp)->hwndFrom!=hdatatab ||
((NMHDR *)lp)->code!=TCN_SELCHANGE) break;
i=SendMessage(hdatatab,TCM_GETCURSEL,0,0);
Setdisplaymode(i);
break;
default: return DefWindowProc(hw,msg,wp,lp);
};
return 0;
};
// Windows function of data display window.
LRESULT CALLBACK Displaywp(HWND hw,UINT msg,WPARAM wp,LPARAM lp) {
int i,j,answer;
uchar *pm;
t_data result;
RECT rc;
HDC dc;
PAINTSTRUCT ps;
switch (msg) {
case WM_ERASEBKGND:
return 1; // Erasing is not necessary
case WM_LBUTTONDBLCLK:
// Doubleclick in block display mode changes to quality map.
if (displaymode==DISP_BLOCK) {
Setdisplaymode(DISP_QUALITY);
RedrawWindow(hw,NULL,NULL,RDW_UPDATENOW);
break; };
// Doubleclick on block in quality map mode displays block.
if (displaymode!=DISP_QUALITY || mapnx<=0 || mapny<=0 || mapscale<=0)
break;
i=(LOWORD(lp)-maprect.left)/mapscale;
j=(HIWORD(lp)-maprect.top)/mapscale;
if (i>=0 && i<mapnx && j>=0 && j<mapny) {
Setdisplaymode(DISP_BLOCK);
answer=Decodeblock(&procdata,i,j,&result);
Displayblockimage(&procdata,i,j,answer,&result);
RedrawWindow(hw,NULL,NULL,RDW_UPDATENOW); };
break;
case WM_PAINT:
GetClientRect(hw,&rc);
dc=BeginPaint(hw,&ps);
if (displaymode==DISP_QUALITY && qualitymap!=NULL && mapnx>0 && mapny>0) {
// Draw quality map.
FillRect(dc,&rc,graybrush);
FillRect(dc,&maprect,(HBRUSH)GetStockObject(WHITE_BRUSH));
rc.bottom=maprect.top;
for (j=0; j<mapny; j++) {
rc.top=rc.bottom; rc.bottom+=mapscale;
rc.right=maprect.left;
pm=qualitymap+j*mapnx;
for (i=0; i<mapnx; i++,pm++) {
rc.left=rc.right; rc.right+=mapscale;
if (*pm==0xFF) continue; // Block is not yet processed
if (*pm<=16) FillRect(dc,&rc,htone[*pm]);
else FillRect(dc,&rc,hbad);
};
}; }
else if (displaymode==DISP_BLOCK && blockdc!=NULL && blockbits!=NULL) {
// Draw data block.
BitBlt(dc,0,0,blockdx,blockdy,blockdc,0,0,SRCCOPY); }
else
// Nothing to draw.
FillRect(dc,&rc,graybrush);
EndPaint(hw,&ps);
break;
default: return DefWindowProc(hw,msg,wp,lp);
};
return 0L;
};
// Window function of block selection window.
LRESULT CALLBACK Blockselectionwp(HWND hw,UINT msg,WPARAM wp,LPARAM lp) {
int answer;
t_data result;
RECT rc;
HDC dc;
PAINTSTRUCT ps;
switch (msg) {
case WM_ERASEBKGND:
return 1; // Erasing is not necessary
case WM_PAINT:
GetClientRect(hw,&rc);
dc=BeginPaint(hw,&ps);
SelectObject(dc,GetStockObject(BLACK_PEN));
MoveToEx(dc,rc.left,rc.top,NULL);
LineTo(dc,rc.right,rc.top);
rc.top++;
FillRect(dc,&rc,graybrush);
EndPaint(hw,&ps);
break;
case WM_COMMAND:
if (HIWORD(wp)!=BN_CLICKED)
break;
switch (LOWORD(wp)) {
case BSEL_GRID: // Show grid radio button clicked
case BSEL_BAD: // Show bad dots radio button clicked
case BSEL_NONE: // No overlay radio button clicked
if (blockdotmode!=LOWORD(wp)) {
blockdotmode=LOWORD(wp);
CheckRadioButton(hblocksel,BSEL_GRID,BSEL_NONE,blockdotmode);
answer=Decodeblock(&procdata,blockselx,blocksely,&result);
Displayblockimage(&procdata,blockselx,blocksely,answer,&result);
RedrawWindow(hdisplay,NULL,NULL,RDW_UPDATENOW); };
break;
case BSEL_LEFT: // Move selection left
Changeblockselection(VK_LEFT); break;
case BSEL_RIGHT: // Move selection right
Changeblockselection(VK_RIGHT); break;
case BSEL_UP: // Move selection up
Changeblockselection(VK_UP); break;
case BSEL_DOWN: // Move selection down
Changeblockselection(VK_DOWN);
break;
};
SetFocus(hwmain);
break;
default: return DefWindowProc(hw,msg,wp,lp);
};
return 0;
};
// Creates block selection window that owns block navigation buttons, block
// indicator and grid checkbox.
static HWND Createblockselector(HWND howner) {
RECT rcowner;
WNDCLASS wc;
// Register class of block selection window.
wc.style=CS_OWNDC;
wc.lpfnWndProc=Blockselectionwp;
wc.cbClsExtra=wc.cbWndExtra=0;
wc.hInstance=hinst;
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=NULL;
wc.lpszMenuName=NULL;
wc.lpszClassName=BLOCKSELCLASS;
if (!RegisterClass(&wc))
return NULL;
// Create window. Note that initially this window is hidden.
GetClientRect(howner,&rcowner);
hblocksel=CreateWindow(BLOCKSELCLASS,"",
WS_CHILD|WS_CLIPCHILDREN,
0,rcowner.bottom-75,rcowner.right,75,
howner,NULL,hinst,NULL);
if (hblocksel==NULL)
return NULL;
// Create overlay selection radio buttons.
hshowgrid=CreateWindow("BUTTON","Show grid",
WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON ,
6,7,100,20,
hblocksel,(HMENU)BSEL_GRID,hinst,NULL);
SendMessage(hshowgrid,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
hshowbad=CreateWindow("BUTTON","Show bad dots",
WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON ,
6,29,100,20,
hblocksel,(HMENU)BSEL_BAD,hinst,NULL);
SendMessage(hshowbad,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
hshownone=CreateWindow("BUTTON","No overlay",
WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON ,
6,51,100,20,
hblocksel,(HMENU)BSEL_NONE,hinst,NULL);
SendMessage(hshownone,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
CheckRadioButton(hblocksel,BSEL_GRID,BSEL_NONE,BSEL_BAD);
blockdotmode=BSEL_BAD;
// Create position display.
hpos=CreateWindowEx(WS_EX_CLIENTEDGE,"STATIC","",
WS_CHILD|WS_VISIBLE|WS_BORDER|SS_CENTER,
(rcowner.right-130)/2,14,130,48,
hblocksel,(HMENU)BSEL_POS,hinst,NULL);
SendMessage(hpos,WM_SETFONT,(WPARAM)hfont20,0);
// Create cursor buttons.
hleft=CreateWindow("BUTTON","L",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
rcowner.right-110,23,32,32,
hblocksel,(HMENU)BSEL_LEFT,hinst,NULL);
SendMessage(hleft,WM_SETFONT,(WPARAM)hfont20,0);
hright=CreateWindow("BUTTON","R",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
rcowner.right-36,23,32,32,
hblocksel,(HMENU)BSEL_RIGHT,hinst,NULL);
SendMessage(hright,WM_SETFONT,(WPARAM)hfont20,0);
hup=CreateWindow("BUTTON","U",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
rcowner.right-73,5,32,32,
hblocksel,(HMENU)BSEL_UP,hinst,NULL);
SendMessage(hup,WM_SETFONT,(WPARAM)hfont20,0);
hdown=CreateWindow("BUTTON","D",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
rcowner.right-73,41,32,32,
hblocksel,(HMENU)BSEL_DOWN,hinst,NULL);
SendMessage(hdown,WM_SETFONT,(WPARAM)hfont20,0);
return hblocksel;
};
// Shows or hides block selector.
static void Showblockselector(int show) {
ShowWindow(hblocksel,show?SW_RESTORE:SW_HIDE);
};
// Creates tab with display window (includes quality map and block view) and
// places it in the right half of the specified rectangle.
HWND Createdisplay(RECT *rc) {
int i,leftpos;
uchar buf[sizeof(BITMAPINFO)+256*sizeof(RGBQUAD)];
BITMAPINFO *pbmi;
RECT rcd;
WNDCLASS wc;
TCITEM titem;
HDC dc;
// Register class of display frame window.
wc.style=CS_OWNDC;
wc.lpfnWndProc=Dataframewp;
wc.cbClsExtra=wc.cbWndExtra=0;
wc.hInstance=hinst;
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=NULL;
wc.lpszMenuName=NULL;
wc.lpszClassName=DATAFRAMECLASS;
if (!RegisterClass(&wc))
return NULL;
// Register class of display window.
wc.style=CS_OWNDC|CS_DBLCLKS;
wc.lpfnWndProc=Displaywp;
wc.cbClsExtra=wc.cbWndExtra=0;
wc.hInstance=hinst;
wc.hIcon=NULL;
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=graybrush;
wc.lpszMenuName=NULL;
wc.lpszClassName=DISPLAYCLASS;
if (!RegisterClass(&wc))
return NULL;
// Create data frame - dummy window with sole purpose to intercept tab
// notifications within this source file.
leftpos=(rc->right+rc->left+DELTA)/2;
hdataframe=CreateWindow(DATAFRAMECLASS,"",
WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,
leftpos,rc->top,rc->right-leftpos,rc->bottom-rc->top,
hwmain,NULL,hinst,NULL);
if (hdataframe==NULL) return NULL;
// Create tab wrapper.
hdatatab=CreateWindow(WC_TABCONTROL,"",
WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN|WS_TABSTOP|TCS_TABS,
0,0,rc->right-leftpos,rc->bottom-rc->top,
hdataframe,NULL,hinst,NULL);
if (hdatatab==NULL) {
DestroyWindow(hdataframe);
return NULL; };
SendMessage(hdatatab,WM_SETFONT,(WPARAM)GetStockObject(ANSI_VAR_FONT),0);
// Add tabs for quality map and block view.
memset(&titem,0,sizeof(titem));
titem.mask=TCIF_TEXT;
titem.pszText="Quality map";
SendMessage(hdatatab,TCM_INSERTITEM,DISP_QUALITY,(LPARAM)&titem);
titem.pszText="Blocks";
SendMessage(hdatatab,TCM_INSERTITEM,DISP_BLOCK,(LPARAM)&titem);
// Create brushes to display quality map. 0 errors: green; 16 errors (maximum
// allowed by implemented version of ECC): red; unrecoverable blocks: black.
for (i=0; i<17; i++)
htone[i]=CreateSolidBrush(RGB(119+8*i,119+8*(16-i),64));
hbad=(HBRUSH)GetStockObject(BLACK_BRUSH);
// Set default display parameters.
displaymode=DISP_QUALITY;
mapnx=mapny=0;
// Create display window. The window is same for both tabs, I only redraw its
// contents.
GetClientRect(hdatatab,&rcd);
SendMessage(hdatatab,TCM_ADJUSTRECT,0,(LPARAM)&rcd);
rcd.top+=2;
hdisplay=CreateWindowEx(WS_EX_CLIENTEDGE,DISPLAYCLASS,"",
WS_CHILD|WS_VISIBLE|WS_CLIPCHILDREN,
rcd.left,rcd.top,rcd.right-rcd.left,rcd.bottom-rcd.top,
hdatatab,NULL,hinst,NULL);
// Create memory DC and allocate grayscale bitmap that will be used as a
// buffer for direct block drawing.
dc=GetDC(hdisplay);
blockdc=CreateCompatibleDC(dc);
if (blockdc==NULL) {
blockdx=blockdy=0;
blockbmp=NULL;
blockbits=NULL; }
else {
blockdx= // Bitmap scan lines are DWORD-aligned
(rcd.right-rcd.left) & 0xFFFFFFFC;
blockdy=rcd.bottom-rcd.top;
pbmi=(BITMAPINFO *)buf;
memset(pbmi,0,sizeof(BITMAPINFOHEADER));
pbmi->bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
pbmi->bmiHeader.biWidth=blockdx;
pbmi->bmiHeader.biHeight=blockdy;
pbmi->bmiHeader.biPlanes=1;
pbmi->bmiHeader.biBitCount=8;
pbmi->bmiHeader.biCompression=BI_RGB;
pbmi->bmiHeader.biSizeImage=0;
pbmi->bmiHeader.biClrUsed=256;
pbmi->bmiHeader.biClrImportant=256;
// First 128 colors are different shades of gray, from black to white.
for (i=0; i<128; i++) {
pbmi->bmiColors[i].rgbBlue=(uchar)(i*2);
pbmi->bmiColors[i].rgbGreen=(uchar)(i*2);
pbmi->bmiColors[i].rgbRed=(uchar)(i*2);
pbmi->bmiColors[i].rgbReserved=0; };
// Last 128 colors are gray shades, in same order, under semi-transparent
// red. Alpha-blending for poor?..
for (i=128; i<256; i++) {
pbmi->bmiColors[i].rgbBlue=(uchar)(i-128);
pbmi->bmiColors[i].rgbGreen=(uchar)(i-128);
pbmi->bmiColors[i].rgbRed=(uchar)i;
pbmi->bmiColors[i].rgbReserved=0; };
blockbmp=CreateDIBSection(blockdc,pbmi,DIB_RGB_COLORS,
(void **)&blockbits,NULL,0);
SelectObject(blockdc,blockbmp);
// Fill memory bitmap with 20% gray. Note that this gray is different from
// the button gray, which is by the way not exactly gray in the default
// scheme, but who cares?
if (blockbits!=NULL) {
memset(blockbits,128*80/100,blockdx*blockdy);
};
};
ReleaseDC(hdisplay,dc);
blockindex=0;
// Create block selection window and immediately hide it, because this tool
// is necessary only in DISP_BLOCK view.
Createblockselector(hdisplay);
Showblockselector(0);
// Correct rectangle and report success.
rc->right=leftpos-DELTA;
return hdatatab;
};
// Sets display mode: quality map (DISP_QUALITY) or block picture (DISP_BLOCK).
void Setdisplaymode(int mode) {
if (mode!=displaymode) {
displaymode=mode;
SendMessage(hdatatab,TCM_SETCURSEL,mode,0);
Showblockselector(mode==DISP_BLOCK);
InvalidateRect(hdisplay,NULL,FALSE);
};
};
// Starts new quality map for page with maximal nx*ny blocks.
void Initqualitymap(int nx,int ny) {
int x0,y0;
RECT rc;
// Delete previous quality map.
if (qualitymap!=NULL)
GlobalFree((HGLOBAL)qualitymap);
// Allocate new quality map. Value 0xFF means non-processed element.
if (nx<=0 || ny<=0)
qualitymap=NULL;
else {
qualitymap=(uchar *)GlobalAlloc(GMEM_FIXED,nx*ny);
if (qualitymap==NULL) nx=ny=0;
else memset(qualitymap,0xFF,nx*ny); };
// Calculate map geometry.
if (nx>0 && ny>0) {
GetClientRect(hdisplay,&rc);
mapscale=min((rc.right-1)/nx,(rc.bottom-1)/ny);
if (mapscale<1) mapscale=1;
x0=(rc.right-mapscale*nx-1)/2; if (x0<0) x0=0;
y0=(rc.bottom-mapscale*ny-1)/2; if (y0<0) y0=0;
maprect.left=x0; maprect.right=x0+mapscale*nx+1;
maprect.top=y0; maprect.bottom=y0+mapscale*ny+1; }
else {
mapscale=0;
maprect.left=maprect.right=0;
maprect.top=maprect.bottom=0; };
mapnx=nx;
mapny=ny;
blockselx=blocksely=0;
// Update image on the screen.
if (displaymode==DISP_QUALITY)
InvalidateRect(hdisplay,NULL,FALSE);
;
};
// Adds state of block in column x and row y to quality map.
void Addblocktomap(int x,int y,int nrestored) {
RECT rc;
HDC dc;
if (qualitymap==NULL || x<0 || x>=mapnx || y<0 || y>=mapny)
return; // Bad parameters or uninitialized map
qualitymap[y*mapnx+x]=(uchar)nrestored;
if (displaymode==DISP_QUALITY) {
rc.left=maprect.left+x*mapscale;
rc.right=rc.left+mapscale;
rc.top=maprect.top+y*mapscale;
rc.bottom=rc.top+mapscale;
dc=GetDC(hdisplay);
if (nrestored>=17)
FillRect(dc,&rc,hbad);
else
FillRect(dc,&rc,htone[nrestored]);
ReleaseDC(hdisplay,dc);
};
};
// Draws block image to the memory context and, if block tab is active,
// displays it on the screen.
void Displayblockimage(t_procdata *pdata,int posx,int posy,
int answer,t_data *result) {
int i,j,x,x0,x1,y,y0,y1,n;
int bufx,bufy,bufdx,bufdy,scale,orientation;
ulong baddots[32],u;
float xpeak,xstep,ypeak,ystep,fscale,offsetx,offsety;
char s[128];
uchar *buf,*pbuf,*pblock;
if (blockdc==NULL || blockbits==NULL)
return; // Block display is not initialized
// Preset bitmap to 20% gray.
memset(blockbits,128*80/100,blockdx*blockdy);
// Draw block.
if (pdata!=NULL && pdata->unsharp!=NULL && pdata->xstep>0 && pdata->ystep>0) {
// Get frequently used variables.
bufdx=pdata->bufdx;
bufdy=pdata->bufdy;
if (answer>=0) { // Block recognized
xpeak=pdata->blockxpeak;
xstep=pdata->blockxstep;
ypeak=pdata->blockypeak;
ystep=pdata->blockystep; }
else { // Block not recognized, show center
xstep=pdata->xstep;
xpeak=(bufdx-xstep)/2.0;
ystep=pdata->ystep;
ypeak=(bufdy-ystep)/2.0; };
buf=pdata->unsharp;
orientation=pdata->orientation;
// Determine scaling factor. Integer factor is much easier to handle. I use
// mean block size. Local size differs only slightly but may cause scale
// variations from one block to another.
scale=min(blockdx/(pdata->xstep*1.1),blockdy/(pdata->ystep*1.1));
if (scale<1) scale=1;
fscale=scale;
// Calculate offset between display amd buffer (pdata->unsharp) coordinates.
offsetx=xpeak+(xstep-blockdx/fscale)*0.5;
if (blockdy<blockdx) // Draw in the middle
offsety=ypeak+(ystep-blockdy/fscale)*0.5;
else // Align to top
offsety=ypeak+(ystep-(2*blockdy-blockdx)/fscale)*0.5;
// Draw block.
x0=(xpeak-xstep*0.1-offsetx)*fscale+0.5;
if (x0<0) x0=0;
x1=(xpeak+xstep*1.1-offsetx)*fscale+0.5;
if (x1>blockdx) x1=blockdx;
y0=(ypeak-ystep*0.1-offsety)*fscale+0.5;
if (y0<0) y0=0;
y1=(ypeak+ystep*1.1-offsety)*fscale+0.5;
if (y1>blockdy) y1=blockdy;
for (y=y0; y<y1; y++) {
bufy=y/fscale+offsety;
if (bufy<0) continue;
if (bufy>=bufdy) break;
pbuf=buf+bufy*bufdx;
pblock=blockbits+y*blockdx+x0;
for (x=x0; x<x1; x++,pblock++) {
bufx=x/fscale+offsetx;
if (bufx<0) continue;
if (bufx>=bufdx) break;
*pblock=(uchar)(pbuf[bufx]>>1);
};
};
// Draw block borders.
if (answer>=0 && blockdotmode!=BSEL_NONE) {
x0=(xpeak-offsetx)*fscale+1.5;
x1=(xpeak+xstep-offsetx)*fscale+1.5;
y0=(ypeak-offsety)*fscale+1.5;
y1=(ypeak+ystep-offsety)*fscale+1.5;
for (x=x0; x<=x1; x++) {
// Horizontal borders.
if (x<0) continue;
if (x>=blockdx) break;
if (y0>=0) blockbits[y0*blockdx+x]|=0x80;
if (y1<blockdy) blockbits[y1*blockdx+x]|=0x80; };
for (y=y0; y<=y1; y++) {
// Vertical borders.
if (y<0) continue;
if (y>=blockdy) break;
if (x0>=0) blockbits[y*blockdx+x0]|=0x80;
if (x1<blockdx) blockbits[y*blockdx+x1]|=0x80;
};
};
if (blockdotmode!=BSEL_NONE &&
(result==NULL || answer>=17 || blockdotmode==BSEL_GRID)
) {
// If data can't be restored, or on special request, draw grid. First
// vertical lines.
y0=(ypeak+ystep/(NDOT+3.0)*1.5-offsety)*fscale+1.5;
if (y0<0) y0=0;
y1=(ypeak+ystep/(NDOT+3.0)*(NDOT+1.5)-offsety)*fscale+1.5;
if (y1>=blockdy) y1=blockdy-1;
for (i=0; i<=NDOT; i++) {
x=(xpeak+xstep/(NDOT+3.0)*(i+1.5)-offsetx)*fscale+1.5;
if (x<0 || x>=blockdx) continue;
pblock=blockbits+y0*blockdx+x;
for (y=y0; y<=y1; y++,pblock+=blockdx) *pblock|=0x80; };
// Draw horizontal lines.
x0=(xpeak+xstep/(NDOT+3.0)*1.5-offsetx)*fscale+1.5;
if (x0<0) x0=0;
x1=(xpeak+xstep/(NDOT+3.0)*(NDOT+1.5)-offsetx)*fscale+1.5;
if (x1>=blockdx) x1=blockdx-1;
for (j=0; j<=NDOT; j++) {
y=(ypeak+ystep/(NDOT+3.0)*(j+1.5)-offsety)*fscale+1.5;
if (y<0 || y>=blockdy) continue;
pblock=blockbits+y*blockdx+x0;
for (x=x0; x<=x1; x++) *pblock++|=0x80;
}; }
else if (blockdotmode!=BSEL_NONE && answer>=0) {
// Data restored. Circumfere bad dots.
memcpy(baddots,result,sizeof(t_data));
for (i=0; i<32; i++)
baddots[i]^=((ulong *)&pdata->uncorrected)[i];
for (j=0; j<NDOT; j++) {
for (i=0; i<NDOT; i++) {
switch (orientation) {
case 0: u=baddots[i] & (1<<j); break;
case 1: u=baddots[NDOT-1-j] & (1<<i); break;
case 2: u=baddots[NDOT-1-i] & (1<<(NDOT-1-j)); break;
case 3: u=baddots[j] & (1<<(NDOT-1-i)); break;
case 4: u=baddots[j] & (1<<i); break;
case 5: u=baddots[i] & (1<<(NDOT-1-j)); break;
case 6: u=baddots[NDOT-1-j] & (1<<(NDOT-1-i)); break;
case 7: u=baddots[NDOT-1-i] & (1<<j); break; };
if (u==0)
continue;
x0=(xpeak+xstep/(NDOT+3.0)*(i+1.5)-offsetx)*fscale+1.5;
x1=(xpeak+xstep/(NDOT+3.0)*(i+2.5)-offsetx)*fscale+1.5;
y0=(ypeak+ystep/(NDOT+3.0)*(j+1.5)-offsety)*fscale+1.5;
y1=(ypeak+ystep/(NDOT+3.0)*(j+2.5)-offsety)*fscale+1.5;
if (x0<0 || x1>=blockdx || y0<0 || y1>=blockdy)
continue; // Draw only complete squares
for (x=x0; x<=x1; x++) {
blockbits[y0*blockdx+x]|=0x80;
blockbits[y1*blockdx+x]|=0x80; };
for (y=y0; y<=y1; y++) {
blockbits[y*blockdx+x0]|=0x80;
blockbits[y*blockdx+x1]|=0x80;
};
};
};
};
};
// Request repainting.
if (displaymode==DISP_BLOCK)
InvalidateRect(hdisplay,NULL,FALSE);
// Save coordinates of displayed block.
blockselx=posx;
blocksely=posy;
// Display coordinates and block quality.
if (pdata==NULL)
SetWindowText(hpos,"");
else {
n=sprintf(s,"(%i,%i)\n",posx,posy);
if (answer<0)
sprintf(s+n,"No frame");
else if (answer==0)
sprintf(s+n,"Good");
else if (answer==1)
sprintf(s+n,"1 byte bad");
else if (answer<17)
sprintf(s+n,"%i bytes bad",answer);
else
sprintf(s+n,"Unreadable");
SetWindowText(hpos,s);
};
};
int Changeblockselection(WPARAM wp) {
int x,y,answer;
t_data result;
if (displaymode!=DISP_BLOCK)
return 1; // Keystroke is not processed
x=blockselx;
y=blocksely;
switch (wp) {
case VK_LEFT: x--; break;
case VK_RIGHT: x++; break;
case VK_UP: y--; break;
case VK_DOWN: y++; break;
case VK_HOME: x=0; break;
case VK_END: x=mapnx-1; break;
case VK_PRIOR: y=0; break;
case VK_NEXT: y=mapny-1; break;
case VK_SPACE: // Space toggles grid (NONE - BAD)
if (blockdotmode==BSEL_BAD) blockdotmode=BSEL_NONE;
else blockdotmode=BSEL_BAD;
CheckRadioButton(hblocksel,BSEL_GRID,BSEL_NONE,blockdotmode);
break;
default: return 1; }; // Keystroke is not processed
if (x>=mapnx) x=mapnx-1; if (x<0) x=0;
if (y>=mapny) y=mapny-1; if (y<0) y=0;
if (wp==VK_SPACE || x!=blockselx || y!=blocksely) {
answer=Decodeblock(&procdata,x,y,&result);
Displayblockimage(&procdata,x,y,answer,&result);
RedrawWindow(hdisplay,NULL,NULL,RDW_UPDATENOW); };
return 0; // Keystroke processed
};
// Does... er... anyway, not what its name may suggest.