forked from tholden/AVSConvolutionFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvolution.cpp
1307 lines (1279 loc) · 41.1 KB
/
convolution.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
// convolution filter
// copyright tom holden, 2002
// mail: [email protected]
#include <windows.h>
#include <stdlib.h>
#include <mmintrin.h>
#include "resource.h"
#include "avs_ape.h"
#define MOD_NAME "Trans / Convolution Filter"
#define UNIQUEIDSTRING "Holden03: Convolution Filter"
#define MAX_DRAW_SIZE 16384
#define MAX_FILENAME_SIZE 1024
typedef int (* FunctionType)(void *, void *, void *); // framebuffer, fbout, m64farray
char lastszFile[MAX_FILENAME_SIZE]; // stores the name of the last opened file in any instance of the ape
unsigned int instances = 0; // stores the number of instances currently open
class C_FILTER : public C_RBASE
{
protected:
public:
// standard ape members
C_FILTER();
virtual ~C_FILTER();
virtual int render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h);
virtual HWND conf(HINSTANCE hInstance, HWND hwndParent);
virtual char *get_desc();
virtual void load_config(unsigned char *data, int len);
virtual int save_config(unsigned char *data);
// file access members
OPENFILENAME FileBox; // the file dialogue
char szFile[MAX_FILENAME_SIZE]; // buffer for file name
HANDLE hf; // file handle
// functions
bool test_mmx(void); // test to see if they have mmx, without which this ape will not run
void createdraw(void); // create the draw function
_inline void deletedraw(void); // delete it
FunctionType draw; // the draw function
// main filter data (saved)
bool enabled; // toggles plug-in on and off
bool wraparound; // toggles wrap around subtraction
bool absolute; // toggles absolute values in subtraction results
bool twopass; // toggles two pass mode
int farray[51]; // box data
// non-saved values
__m64 m64farray[50]; // abs(farray) packed into the four unsigned shorts that make up an m64
bool mmx; // true if mmx is supported
int width,height; // does what it says on the tin
DWORD oldprotect; // needed for virtual protect stuff
int drawstate; // 0 not allocated 1 creating 2 created
bool updatedraw; // true if draw needs updating
unsigned int codelength; // length of draw
};
// global configuration dialog pointer
static C_FILTER *g_Filter;
// global DLL instance pointer
static HINSTANCE g_hDllInstance;
// configuration screen
static BOOL CALLBACK g_DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
{
char value[16];
int val;
unsigned int objectcode, objectmessage;
HWND hwndEdit;
switch (uMsg)
{
case WM_INITDIALOG: //init e.g. fill in boxes
CheckDlgButton(hwndDlg,IDC_CHECK1,g_Filter->enabled);
CheckDlgButton(hwndDlg,IDC_CHECK2,g_Filter->wraparound);
CheckDlgButton(hwndDlg,IDC_CHECK3,g_Filter->absolute);
CheckDlgButton(hwndDlg,IDC_CHECK4,g_Filter->twopass);
if (g_Filter->farray[50] == 0) g_Filter->farray[50] = 1;
for(int i=0;i<51;i++)
{
hwndEdit = GetDlgItem(hwndDlg,1001+i);
_itoa(g_Filter->farray[i],value,10);
SetWindowText(hwndEdit,value);
}
// file dialogue initialisation.
g_Filter->FileBox.lStructSize = sizeof(OPENFILENAME);
g_Filter->FileBox.hwndOwner = hwndDlg;
g_Filter->FileBox.hInstance = NULL;
g_Filter->FileBox.lpstrFilter = "Convolution Filter File (*.cff)\0*.cff\0All Files (*.*)\0*.*\0";
g_Filter->FileBox.lpstrCustomFilter = NULL;
g_Filter->FileBox.nMaxCustFilter = 0;
g_Filter->FileBox.nFilterIndex = 1;
g_Filter->FileBox.nMaxFile = sizeof(g_Filter->szFile);
g_Filter->FileBox.lpstrFileTitle = NULL;
g_Filter->FileBox.nMaxFileTitle = 0;
if (lastszFile[0] != NULL)
{
for (int i=0; i<MAX_FILENAME_SIZE;i++) g_Filter->szFile[i] = lastszFile[i];
g_Filter->FileBox.lpstrInitialDir = NULL;
}
else g_Filter->FileBox.lpstrInitialDir = "C:\\Program Files\\Winamp3\\Wacs\\data\\avs";
g_Filter->FileBox.lpstrFile = g_Filter->szFile;
g_Filter->FileBox.lpstrTitle = NULL;
g_Filter->FileBox.lpfnHook = NULL;
g_Filter->FileBox.lpstrDefExt = "cff";
return 1;
case WM_COMMAND:
objectcode = LOWORD(wParam);
objectmessage = HIWORD(wParam);
//see if the auto button's been clicked and if so do the calculations to work out scale's value
if ((objectcode == IDC_BUTTON1)&&(objectmessage == BN_CLICKED))
{
hwndEdit = GetDlgItem(hwndDlg,IDC_EDIT51);
val = 0;
for (int i=0;i<50;i++) val+=g_Filter->farray[i];
val = (g_Filter->twopass)?(2*val):val;
if (val==0) val = 1;
g_Filter->farray[50] = val;
_itoa(val,value,10);
SetWindowText(hwndEdit,value);
g_Filter->updatedraw = true;
return 0;
}
// see if the clear button's been clicked
if ((objectcode == IDC_BUTTON4)&&(objectmessage == BN_CLICKED))
{
if(MessageBox(hwndDlg, "Really clear all data?", "convolution", MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2|MB_APPLMODAL|MB_SETFOREGROUND)==IDYES)
{
// set box array values
for (int i=0;i<50;i++) g_Filter->farray[i]=0;
g_Filter->farray[50] = 1;
g_Filter->farray[24] = 1;
// enable
g_Filter->enabled = true;
g_Filter->wraparound = false;
g_Filter->twopass = false;
g_Filter->absolute = false;
g_Filter->width = 0;
g_Filter->height = 0;
g_Filter->updatedraw = true;
CheckDlgButton(hwndDlg,IDC_CHECK1,BST_CHECKED);
CheckDlgButton(hwndDlg,IDC_CHECK2,BST_UNCHECKED);
CheckDlgButton(hwndDlg,IDC_CHECK3,BST_UNCHECKED);
CheckDlgButton(hwndDlg,IDC_CHECK4,BST_UNCHECKED);
for(int i=0;i<51;i++)
{
hwndEdit = GetDlgItem(hwndDlg,1001+i);
_itoa(g_Filter->farray[i],value,10);
SetWindowText(hwndEdit,value);
}
}
return 0;
}
// see if the load button's been clicked
if ((objectcode == IDC_BUTTON3)&&(objectmessage == BN_CLICKED))
{
g_Filter->FileBox.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | 0x02000000 | OFN_HIDEREADONLY; // 0x02000000 is OFN_DONTADDTORECENT for some reason the compiler is not recognising it.
if (GetOpenFileName(&g_Filter->FileBox))
{
g_Filter->hf = CreateFile(g_Filter->FileBox.lpstrFile, GENERIC_READ, 0, (LPSECURITY_ATTRIBUTES) NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);
if (g_Filter->hf == INVALID_HANDLE_VALUE)
{
// display the error
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Unexpected file error.", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND );
LocalFree( lpMsgBuf );
return 0;
}
else
{
unsigned char *data;
data = new unsigned char[MAX_FILENAME_SIZE];
DWORD numbytesread;
g_Filter->FileBox.lpstrInitialDir = NULL;
for (int i=0;i<MAX_FILENAME_SIZE;i++) lastszFile[i] = g_Filter->szFile[i];
if (ReadFile(g_Filter->hf, data, MAX_FILENAME_SIZE, &numbytesread, NULL)==0 && GetLastError()!=ERROR_HANDLE_EOF)
{
// display the error
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Unexpected file error.", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND );
LocalFree( lpMsgBuf );
}
g_Filter->load_config(data,(unsigned int) numbytesread);
// refresh the dialogue with the new data
CheckDlgButton(hwndDlg,IDC_CHECK1,g_Filter->enabled);
CheckDlgButton(hwndDlg,IDC_CHECK2,g_Filter->wraparound);
CheckDlgButton(hwndDlg,IDC_CHECK3,g_Filter->absolute);
CheckDlgButton(hwndDlg,IDC_CHECK4,g_Filter->twopass);
if (g_Filter->farray[50] == 0) g_Filter->farray[50] = 1;
for(int i=0;i<51;i++)
{
hwndEdit = GetDlgItem(hwndDlg,1001+i);
_itoa(g_Filter->farray[i],value,10);
SetWindowText(hwndEdit,value);
}
if (CloseHandle(g_Filter->hf)==0)
{
// display the error
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Unexpected file error.", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND );
LocalFree( lpMsgBuf );
}
}
}
return 0;
}
// see if the save button's been clicked
if ((objectcode == IDC_BUTTON2)&&(objectmessage == BN_CLICKED))
{
g_Filter->FileBox.Flags = 0x02000000 | OFN_HIDEREADONLY; // 0x02000000 is OFN_DONTADDTORECENT for some reason the compiler is not recognising it.
if (GetSaveFileName(&(g_Filter->FileBox)))
{
g_Filter->hf = CreateFile(g_Filter->FileBox.lpstrFile, GENERIC_WRITE, 0, (LPSECURITY_ATTRIBUTES) NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);
if ((g_Filter->hf == INVALID_HANDLE_VALUE) && (GetLastError()==ERROR_FILE_EXISTS) && (MessageBox(hwndDlg, "This file already exists. Do you want to overwrite it?", "convolution", MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2|MB_APPLMODAL|MB_SETFOREGROUND)==IDYES))
g_Filter->hf = CreateFile(g_Filter->FileBox.lpstrFile, GENERIC_WRITE, 0, (LPSECURITY_ATTRIBUTES) NULL, TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL,(HANDLE) NULL);
if (g_Filter->hf == INVALID_HANDLE_VALUE)
{
// display the error
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Unexpected file error.", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND );
LocalFree( lpMsgBuf );
}
else
{
unsigned char *data;
data = new unsigned char[MAX_FILENAME_SIZE];
int numbytestowrite = g_Filter->save_config(data);
DWORD numbyteswritten;
g_Filter->FileBox.lpstrInitialDir = NULL;
for (int i=0;i<MAX_FILENAME_SIZE;i++) lastszFile[i] = g_Filter->szFile[i];
if (WriteFile(g_Filter->hf, data, numbytestowrite, &numbyteswritten, NULL)==0)
{
// display the error
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Unexpected file error.", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND );
LocalFree( lpMsgBuf );
}
if (CloseHandle(g_Filter->hf)==0)
{
// display the error
LPVOID lpMsgBuf;
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Unexpected file error.", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND );
LocalFree( lpMsgBuf );
}
}
}
return 0;
}
// see if enable checkbox is checked
if (objectcode == IDC_CHECK1)
{
g_Filter->enabled = IsDlgButtonChecked(hwndDlg,IDC_CHECK1)==1;
g_Filter->updatedraw = true;;
return 0;
}
// see if wraparound checkbox is checked
if (objectcode == IDC_CHECK2)
{
if(IsDlgButtonChecked(hwndDlg,IDC_CHECK2)==1)
{
g_Filter->wraparound = true;
g_Filter->absolute = false;
CheckDlgButton(hwndDlg,IDC_CHECK3,BST_UNCHECKED);
}
else g_Filter->wraparound = false;
g_Filter->updatedraw = true;;
return 0;
}
// see if absolute checkbox is checked
if (objectcode == IDC_CHECK3)
{
if(IsDlgButtonChecked(hwndDlg,IDC_CHECK3)==1)
{
g_Filter->absolute = true;
g_Filter->wraparound = false;
CheckDlgButton(hwndDlg,IDC_CHECK2,BST_UNCHECKED);
}
else g_Filter->absolute = false;
g_Filter->updatedraw = true;;
return 0;
}
// see if twopass checkbox is checked
if (objectcode == IDC_CHECK4)
{
g_Filter->twopass = IsDlgButtonChecked(hwndDlg,IDC_CHECK4)==1;
g_Filter->updatedraw = true;;
return 0;
}
//get and put data from the boxes to farray
{
int i = objectcode - 1001;
if (0<=i&&i<51)
{
hwndEdit = GetDlgItem(hwndDlg,1001+i);
if (objectmessage == EN_CHANGE)
{
GetWindowText(hwndEdit,value,16);
val = atoi(value);
if (i==50)
{
g_Filter->farray[i] = (val==0)?1:val;
}
else g_Filter->farray[i] = val;
g_Filter->updatedraw = true;;
}
else if (objectmessage == EN_KILLFOCUS)
{
_itoa(g_Filter->farray[i],value,10);
SetWindowText(hwndEdit,value);
}
return 0;
}
}
}
return 0;
}
// set up default configuration
C_FILTER::C_FILTER()
{
szFile[0] = NULL;
if (instances==0) lastszFile[0] = NULL;
instances++;
// set box array values
for (int i=0;i<50;i++) farray[i]=0;
farray[50] = 1;
farray[24] = 1;
// enable
drawstate = 0;
enabled = true;
wraparound = false;
twopass = false;
absolute = false;
width = 0;
height = 0;
mmx = test_mmx();
updatedraw = true;
}
// virtual destructor
C_FILTER::~C_FILTER()
{
instances--;
while (drawstate!=0) if (drawstate == 2) deletedraw();
}
_inline void C_FILTER::deletedraw(void)
{
delete [](LPBYTE)draw;
drawstate = 0;
}
bool C_FILTER::test_mmx(void)
{
__try
{
__asm emms // only available with MMX
return TRUE; // no exception
}
__except( EXCEPTION_EXECUTE_HANDLER )
{
// exception
return FALSE;
}
}
// RENDER FUNCTION:
// render should return 0 if it only used framebuffer, or 1 if the new output data is in fbout
// w and h are the-width and height of the screen, in pixels.
// isBeat is 1 if a beat has been detected.
// visdata is in the format of [spectrum:0,wave:1][channel][band].
#define data(xx,yy) framebuffer[c[yy]+min(max(i+xx,0),lastcol)]
//#define scale(out) min(max((long)((out+farray[25])/farray[50]),0),255)
#define bound(xx) min(max(xx,0),255)
int C_FILTER::render(char visdata[2][2][576], int isBeat, int *framebuffer, int *fbout, int w, int h)
{
int ret;
if ((w!=width)||(h!=height))
{
width = w;
height = h;
updatedraw = true;
}
if (updatedraw) // if we need to update the draw, do it
{
while (true)
{
while (drawstate!=0) if (drawstate==2) deletedraw();
createdraw();
if (drawstate==2) break;
}
}
ret = draw(framebuffer,fbout,m64farray);
return ret;
}
HWND C_FILTER::conf(HINSTANCE hInstance, HWND hwndParent) // return NULL if no config dialog possible
{
g_Filter = this;
return CreateDialog(hInstance,MAKEINTRESOURCE(IDD_CONFIG),hwndParent,g_DlgProc);
}
char *C_FILTER::get_desc(void)
{
return MOD_NAME;
}
// load_/save_config are called when saving and loading presets (.avs files)
#define GET_INT() (data[pos]|(data[pos+1]<<8)|(data[pos+2]<<16)|(data[pos+3]<<24))
void C_FILTER::load_config(unsigned char *data, int len) // read configuration of max length "len" from data.
{
int pos=0;
// always ensure there is data to be loaded
if (len-pos >= 4)
{
// load activation toggle
enabled=(GET_INT()==1);
pos+=4;
}
if (len-pos >= 4)
{
// load wraparound toggle
wraparound=(GET_INT()==1);
pos+=4;
}
if (len-pos >= 4)
{
// load absolute toggle
absolute=(GET_INT()==1);
pos+=4;
}
if (len-pos >= 4)
{
// load twopass toggle
twopass=(GET_INT()==1);
pos+=4;
}
for (int i=0;i<51;i++)
{
if (len-pos >= 4)
{
// load the filter data
farray[i]=GET_INT();
pos+=4;
}
}
if (farray[50]==0) farray[50] = 1;
if (szFile[0] == NULL)
{
// load the last file name
int i = 0;
for (;len-pos > 0;pos++,i++)
{
szFile[i]=data[pos];
lastszFile[i]=data[pos];
}
if (szFile[0] != NULL) FileBox.lpstrInitialDir = NULL;
szFile[i] = NULL; // add the terminating null byte
lastszFile[i] = NULL; // add the terminating null byte
}
updatedraw = true;
}
// write configuration to data, return length. config data should not exceed 64k.
#define PUT_INT(y) data[pos]=(y)&255; data[pos+1]=(y>>8)&255; data[pos+2]=(y>>16)&255; data[pos+3]=(y>>24)&255
int C_FILTER::save_config(unsigned char *data)
{
int pos=0;
PUT_INT((int)enabled);
pos+=4;
PUT_INT((int)wraparound);
pos+=4;
PUT_INT((int)absolute);
pos+=4;
PUT_INT((int)twopass);
pos+=4;
for (int i=0;i<51;i++)
{
PUT_INT(farray[i]);
pos+=4;
}
for(int i=0;szFile[i] != NULL;pos++,i++) data[pos]=szFile[i];
return pos;
}
#define appenddraw(a) { ((LPBYTE)draw)[codelength] = a; codelength++; }
void C_FILTER::createdraw(void)
{
unsigned int possum = 0, negsum = 0; // absolute sums of pos and neg values
int zerostringl = 0; // length of the initial run of zeroes
int numpos = 0; // number of pos entries
int numneg = 0; // number of neg entries
bool zerostring = true; // are we still in an initial zero run
unsigned int fposdata[2][50][3]; // for each pass, will contain the x and y co-ords and the value of the pos data elements. e.g. farray[7*fposdata[pass][i][1]+fposdata[pass][i][0]]=fposdata[pass][i][2]>0
unsigned int fnegdata[2][50][3]; // for each pass, will contain the x and y co-ords and the absolute value of the neg data elements. e.g. abs(farray[7*fnegdata[pass][i][1]+fnegdata[i][0]])=fnegdata[pass][i][2]>0
unsigned char usefbout; // 1 if need to use fbout
int divisionfactor = farray[50];
drawstate = 1;
updatedraw = false;
for (int i=0;i<50;i++)
{
if (farray[i]<0)
{
negsum -= farray[i];
zerostring=false;
m64farray[i] = (i!=49)?_mm_set1_pi16(((unsigned short) (-farray[i]))):_mm_set1_pi16(((unsigned short) (-256*farray[i])));
}
else if (farray[i]>0)
{
possum += farray[i];
zerostring=false;
m64farray[i] = (i!=49)?_mm_set1_pi16(((unsigned short) (farray[i]))):_mm_set1_pi16(((unsigned short) (256*farray[i])));
}
else
{
if (zerostring) zerostringl++;
m64farray[i] = _mm_setzero_si64();
}
}
_mm_empty();
if (divisionfactor>0) // no sign swapping necessary
{
for (int i=0;i<50;i++)
{
if (farray[i]<0)
{
if (i / 7 < 7)
{
fnegdata[0][numneg][0] = i % 7;
fnegdata[0][numneg][1] = i / 7;
fnegdata[0][numneg][2] = -farray[i];
fnegdata[1][numneg][0] = 6 - i / 7;
fnegdata[1][numneg][1] = i % 7;
fnegdata[1][numneg][2] = -farray[i];
numneg++;
}
else
{
fnegdata[0][numneg][0] = 0;
fnegdata[0][numneg][1] = 7;
fnegdata[0][numneg][2] = -farray[i];
fnegdata[1][numneg][0] = 0;
fnegdata[1][numneg][1] = 7;
fnegdata[1][numneg][2] = -farray[i];
numneg++;
}
}
else if (farray[i]>0)
{
if (i / 7 < 7)
{
fposdata[0][numpos][0] = i % 7;
fposdata[0][numpos][1] = i / 7;
fposdata[0][numpos][2] = farray[i];
fposdata[1][numpos][0] = 6 - i / 7;
fposdata[1][numpos][1] = i % 7;
fposdata[1][numpos][2] = farray[i];
numpos++;
}
else
{
fposdata[0][numpos][0] = 0;
fposdata[0][numpos][1] = 7;
fposdata[0][numpos][2] = farray[i];
fposdata[1][numpos][0] = 0;
fposdata[1][numpos][1] = 7;
fposdata[1][numpos][2] = farray[i];
numpos++;
}
}
}
}
else // do sign swapping
{
for (int i=0;i<50;i++)
{
if (farray[i]<0)
{
if (i / 7 < 7)
{
fposdata[0][numpos][0] = i % 7;
fposdata[0][numpos][1] = i / 7;
fposdata[0][numpos][2] = -farray[i];
fposdata[1][numpos][0] = 6 - i / 7;
fposdata[1][numpos][1] = i % 7;
fposdata[1][numpos][2] = -farray[i];
numpos++;
}
else
{
fposdata[0][numpos][0] = 0;
fposdata[0][numpos][1] = 7;
fposdata[0][numpos][2] = -farray[i];
fposdata[1][numpos][0] = 0;
fposdata[1][numpos][1] = 7;
fposdata[1][numpos][2] = -farray[i];
numpos++;
}
}
else if (farray[i]>0)
{
if (i / 7 < 7)
{
fnegdata[0][numneg][0] = i % 7;
fnegdata[0][numneg][1] = i / 7;
fnegdata[0][numneg][2] = farray[i];
fnegdata[1][numneg][0] = 6 - i / 7;
fnegdata[1][numneg][1] = i % 7;
fnegdata[1][numneg][2] = farray[i];
numneg++;
}
else
{
fnegdata[0][numneg][0] = 0;
fnegdata[0][numneg][1] = 7;
fnegdata[0][numneg][2] = farray[i];
fnegdata[1][numneg][0] = 0;
fnegdata[1][numneg][1] = 7;
fnegdata[1][numneg][2] = farray[i];
numneg++;
}
}
}
divisionfactor=-divisionfactor;
}
if (zerostringl<24) usefbout = 1;
else usefbout = 0;
draw = (FunctionType) new BYTE[MAX_DRAW_SIZE];
if (enabled&&mmx)
{
unsigned int iloopstart,jloopstart; // addresses to loop back to
codelength = 0;
// function header (nonstandard stack - see stack.txt)
appenddraw(0x53) // push ebx
appenddraw(0x56) // push esi
appenddraw(0x57) // push edi
appenddraw(0x55) // push ebp
appenddraw(0x8B) // mov ebp, esp
appenddraw(0xEC)
// throughout this code the coments will refference i, j, k and c[.].
// see prog.txt to see the original non-optimised code to which they (approximiately) refer.
// also see registers.txt for correspondances between the registers here and the vars in prog.txt.
// double for loop header j external loop, i internal loop, k internal non reset
// int j = 0;
appenddraw(0x33) // xor ebx, ebx
appenddraw(0xDB)
appenddraw(0x8B) // mov esi, ebx (esi will be permanently used for j)
appenddraw(0xF3)
// int *k = (usefbout)?fbout:framebuffer - this var will serve as a pointer to the address to output to
appenddraw(0x8B) // mov ebx, [ebp+(usefbout)?24:20]
appenddraw(0x5D)
appenddraw((usefbout)?0x18:0x14)
appenddraw(0x8B) // mov edx, ebx (edx will be permanently used for k)
appenddraw(0xD3)
// set up ecx as pointer to farray
appenddraw(0x8B) // mov ecx, [ebp+28] (e.g. mov ecx, m64farray)
appenddraw(0x4D)
appenddraw(0x1C)
// c[0] = framebuffer;
appenddraw(0x8B) // mov ebx, [ebp+20]
appenddraw(0x5D)
appenddraw(0x14)
appenddraw(0x53) // push ebx
// c[1] = framebuffer;
appenddraw(0x53) // push ebx
// c[2] = framebuffer;
appenddraw(0x53) // push ebx
// c[3] = framebuffer;
appenddraw(0x53) // push ebx
// c[4] = framebuffer+4*width;
appenddraw(0x81) // add ebx, 4*width
appenddraw(0xC3)
// 4*width needs to begin with the lowest byte
appenddraw((4*width)&0x000000FF)
appenddraw(((4*width)&0x0000FF00)>>8)
appenddraw(((4*width)&0x00FF0000)>>16)
appenddraw(((4*width)&0xFF000000)>>24)
appenddraw(0x53) // push ebx
// c[5] = framebuffer+8*width;
appenddraw(0x81) // add ebx, 4*width
appenddraw(0xC3)
// 4*width needs to begin with the lowest byte
appenddraw((4*width)&0x000000FF)
appenddraw(((4*width)&0x0000FF00)>>8)
appenddraw(((4*width)&0x00FF0000)>>16)
appenddraw(((4*width)&0xFF000000)>>24)
appenddraw(0x53) // push ebx
// c[6] = framebuffer+12*width;
appenddraw(0x81) // add ebx, 4*width
appenddraw(0xC3)
// 4*width needs to begin with the lowest byte
appenddraw((4*width)&0x000000FF)
appenddraw(((4*width)&0x0000FF00)>>8)
appenddraw(((4*width)&0x00FF0000)>>16)
appenddraw(((4*width)&0xFF000000)>>24)
appenddraw(0x53) // push ebx
// note: c[6] is at esp, c[5] is at esp+4, c[4] is at esp+8,c[3] is at esp+12,c[2] is at esp+16,c[1] is at esp+20,c[0] is at esp+24
appenddraw(0x0F) // pxor mm7, mm7 - mm7 will be permanently set to 0
appenddraw(0xEF)
appenddraw(0xFF)
// start of j loop
jloopstart = codelength;
// int i = 0;
appenddraw(0x33) // xor edi, edi - this register will be permanently assigned to i
appenddraw(0xFF)
// start of i loop
for (int im=0;im<7;im++) // when im=0 this will deal with the i=0 case, im=1, i=1, im=2, i=2, im=3, 2<i<width-3, im=4, i=width-3, im=5, i=width-2, im=6, i=width-1.
{
iloopstart = codelength;
for (int pass=0;pass<(twopass?2:1);pass++)
{
int lasty = -1; // the last f***data[i][1] looked at
appenddraw(0x0F) // movq mm0, mm7
appenddraw(0x7F)
appenddraw(0xF8)
if (numneg>0)
{
appenddraw(0x0F) // movq mm1, mm7
appenddraw(0x7F)
appenddraw(0xF9)
}
// go through the pos values
for (int i=0; i<numpos; i++)
{
if (fposdata[pass][i][1]==7) // see if this the bias entry
{
appenddraw(0x0F) // paddusw mm0, [ecx+49*8] (e.g. paddw mm0, m64farray[49])
appenddraw(0xDD)
appenddraw(0x81)
appenddraw(0x88)
appenddraw(0x01)
appenddraw(0x00)
appenddraw(0x00)
}
else
{
if (lasty!=fposdata[pass][i][1]) //see if we actually need to adjust the value of eax (the start of row address)
{
appenddraw(0x8B) // mov eax, [esp+4*(6-fposdata[pass][i][1])] (e.g. mov eax, c[fposdata[pass][i][1]])
appenddraw(0x44)
appenddraw(0x24)
appenddraw((4*(6-fposdata[pass][i][1]))&0xFF)
lasty = fposdata[pass][i][1];
}
switch (im)
{
case 0:
appenddraw(0x0F) // movq mm2, [eax+max(4*(fposdata[pass][i][0]-3),0)]
appenddraw(0x6F)
appenddraw(0x50)
appenddraw((max((signed)(4*(fposdata[pass][i][0]-3)),0))&0xFF)
break;
case 1:
appenddraw(0x0F) // movq mm2, [eax+max(4*(fposdata[pass][i][0]-2),0)]
appenddraw(0x6F)
appenddraw(0x50)
appenddraw((max((signed)(4*(fposdata[pass][i][0]-2)),0))&0xFF)
break;
case 2:
appenddraw(0x0F) // movq mm2, [eax+max(4*(fposdata[pass][i][0]-1),0)]
appenddraw(0x6F)
appenddraw(0x50)
appenddraw((max((signed)(4*(fposdata[pass][i][0]-1)),0))&0xFF)
break;
case 3:
appenddraw(0x0F) // movq mm2, [eax+edi+4*(fposdata[pass][i][0]-3)]
appenddraw(0x6F)
appenddraw(0x54)
appenddraw(0x38)
appenddraw((4*(fposdata[pass][i][0]-3))&0xFF)
break;
case 4:
{
int xpos;
appenddraw(0x0F) // movq mm2, [eax+4*min(fposdata[pass][i][0]-6+width,width-2)]
appenddraw(0x6F)
appenddraw(0x90)
xpos = (4*min((unsigned)(fposdata[pass][i][0]-6+width),(unsigned)(width-2)));
// xpos needs to begin with the lowest byte
appenddraw(xpos&0x000000FF)
appenddraw((xpos&0x0000FF00)>>8)
appenddraw((xpos&0x00FF0000)>>16)
appenddraw((xpos&0xFF000000)>>24)
break;
}
case 5:
{
int xpos;
appenddraw(0x0F) // movq mm2, [eax+4*min(fposdata[pass][i][0]-5+width,width-2)]
appenddraw(0x6F)
appenddraw(0x90)
xpos = (4*min((unsigned)(fposdata[pass][i][0]-5+width),(unsigned)(width-2)));
// xpos needs to begin with the lowest byte
appenddraw(xpos&0x000000FF)
appenddraw((xpos&0x0000FF00)>>8)
appenddraw((xpos&0x00FF0000)>>16)
appenddraw((xpos&0xFF000000)>>24)
break;
}
case 6:
{
int xpos;
appenddraw(0x0F) // movq mm2, [eax+4*min(fposdata[pass][i][0]-4+width,width-2)]
appenddraw(0x6F)
appenddraw(0x90)
xpos = (4*min((unsigned)(fposdata[pass][i][0]-4+width),(unsigned)(width-2)));
// xpos needs to begin with the lowest byte
appenddraw(xpos&0x000000FF)
appenddraw((xpos&0x0000FF00)>>8)
appenddraw((xpos&0x00FF0000)>>16)
appenddraw((xpos&0xFF000000)>>24)
break;
}
}
appenddraw(0x0F) // punpcklbw mm2, mm7
appenddraw(0x60)
appenddraw(0xD7)
if (fposdata[pass][i][2]!=1) // in which case we can just add
{ // just add
// see if fposdata[pass][i][2] is a power of 2
unsigned int j;
unsigned char k=0;
for (j=1;j<fposdata[pass][i][2];j<<=1) k++;
if (j==fposdata[pass][i][2])
{ // fposdata[pass][i][2]=2^k
appenddraw(0x0F) // psllw mm2, k
appenddraw(0x71)
appenddraw(0xF2)
appenddraw(k)
}
else
{
appenddraw(0x0F) // pmullw mm2, [ecx+8*(7*fposdata[pass][i][1]+fposdata[pass][i][0])] (e.g. pmullw mm2, m64farray[7*fposdata[pass][i][1]+fposdata[pass][i][0]])
appenddraw(0xD5)
appenddraw(0x91)
// 8*(7*fposdata[pass][i][1]+fposdata[pass][i][0]) needs to begin with the lowest byte
appenddraw((8*(7*fposdata[pass][i][1]+fposdata[pass][i][0]))&0x000000FF)
appenddraw(((8*(7*fposdata[pass][i][1]+fposdata[pass][i][0]))&0x0000FF00)>>8)
appenddraw(((8*(7*fposdata[pass][i][1]+fposdata[pass][i][0]))&0x00FF0000)>>16)
appenddraw(((8*(7*fposdata[pass][i][1]+fposdata[pass][i][0]))&0xFF000000)>>24)
}
}
if (possum<256)
{
appenddraw(0x0F) // paddw mm0, mm2
appenddraw(0xFD)
appenddraw(0xC2)
}
else
{
appenddraw(0x0F) // paddw mm0, mm2
appenddraw(0xDD)
appenddraw(0xC2)
}
}
}
// go through the neg values
lasty = -1;
for (int i=0; i<numneg; i++)
{
if (fnegdata[pass][i][1]==7) // see if this the bias entry
{
appenddraw(0x0F) // paddw mm1, [ecx+49*8] (e.g. paddw mm0, m64farray[49])
appenddraw(0xDD)
appenddraw(0x89)
appenddraw(0x88)
appenddraw(0x01)
appenddraw(0x00)
appenddraw(0x00)
}
else
{
if (lasty!=fnegdata[pass][i][1]) //see if we actually need to adjust the value of eax (the start of row address)
{
appenddraw(0x8B) // mov eax, [esp+4*(6-fnegdata[pass][i][1])] (e.g. mov eax, c[fnegdata[pass][i][1]])
appenddraw(0x44)
appenddraw(0x24)
appenddraw((4*(6-fnegdata[pass][i][1]))&0xFF)
lasty = fnegdata[pass][i][1];
}
switch (im)
{
case 0:
appenddraw(0x0F) // movq mm2, [eax+max(4*(fnegdata[pass][i][0]-3),0)]
appenddraw(0x6F)
appenddraw(0x50)
appenddraw((max((signed)(4*(fnegdata[pass][i][0]-3)),0))&0xFF)
break;
case 1:
appenddraw(0x0F) // movq mm2, [eax+max(4*(fnegdata[pass][i][0]-2),0)]
appenddraw(0x6F)
appenddraw(0x50)
appenddraw((max((signed)(4*(fnegdata[pass][i][0]-2)),0))&0xFF)
break;
case 2:
appenddraw(0x0F) // movq mm2, [eax+max(4*(fnegdata[pass][i][0]-1),0)]
appenddraw(0x6F)
appenddraw(0x50)
appenddraw((max((signed)(4*(fnegdata[pass][i][0]-1)),0))&0xFF)
break;
case 3:
appenddraw(0x0F) // movq mm2, [eax+edi+4*(fnegdata[pass][i][0]-3)]
appenddraw(0x6F)
appenddraw(0x54)
appenddraw(0x38)
appenddraw((4*(fnegdata[pass][i][0]-3))&0xFF)
break;
case 4:
{
int xpos;
appenddraw(0x0F) // movq mm2, [eax+4*min(fnegdata[pass][i][0]-6+width,width-2)]
appenddraw(0x6F)
appenddraw(0x90)
xpos = (4*min((unsigned)(fnegdata[pass][i][0]-6+width),(unsigned)(width-2)));
// xpos needs to begin with the lowest byte
appenddraw(xpos&0x000000FF)
appenddraw((xpos&0x0000FF00)>>8)
appenddraw((xpos&0x00FF0000)>>16)
appenddraw((xpos&0xFF000000)>>24)
break;
}
case 5:
{
int xpos;
appenddraw(0x0F) // movq mm2, [eax+4*min(fnegdata[pass][i][0]-5+width,width-2)]
appenddraw(0x6F)
appenddraw(0x90)
xpos = (4*min((unsigned)(fnegdata[pass][i][0]-5+width),(unsigned)(width-2)));
// xpos needs to begin with the lowest byte
appenddraw(xpos&0x000000FF)
appenddraw((xpos&0x0000FF00)>>8)
appenddraw((xpos&0x00FF0000)>>16)
appenddraw((xpos&0xFF000000)>>24)
break;
}
case 6:
{
int xpos;
appenddraw(0x0F) // movq mm2, [eax+4*min(fnegdata[pass][i][0]-4+width,width-2)]
appenddraw(0x6F)
appenddraw(0x90)
xpos = (4*min((unsigned)(fnegdata[pass][i][0]-4+width),(unsigned)(width-2)));
// xpos needs to begin with the lowest byte
appenddraw(xpos&0x000000FF)
appenddraw((xpos&0x0000FF00)>>8)
appenddraw((xpos&0x00FF0000)>>16)
appenddraw((xpos&0xFF000000)>>24)
break;
}
}
appenddraw(0x0F) // punpcklbw mm2, mm7
appenddraw(0x60)
appenddraw(0xD7)
if (fnegdata[pass][i][2]!=1) // in which case we can just add
{ // just add
// see if fnegdata[pass][i][2] is a power of 2
unsigned int j;
unsigned char k=0;
for (j=1;j<fnegdata[pass][i][2];j<<=1) k++;
if (j==fnegdata[pass][i][2])
{ // fnegdata[pass][i][2]=2^k
appenddraw(0x0F) // psllw mm2, k
appenddraw(0x71)
appenddraw(0xF2)
appenddraw(k)
}
else
{
appenddraw(0x0F) // pmullw mm2, [ecx+8*(7*fnegdata[pass][i][1]+fnegdata[pass][i][0])] (e.g. pmullw mm2, m64farray[7*fnegdata[pass][i][1]+fnegdata[pass][i][0]])
appenddraw(0xD5)
appenddraw(0x91)
// 8*(7*fnegdata[pass][i][1]+fnegdata[pass][i][0]) needs to begin with the lowest byte
appenddraw((8*(7*fnegdata[pass][i][1]+fnegdata[pass][i][0]))&0x000000FF)
appenddraw(((8*(7*fnegdata[pass][i][1]+fnegdata[pass][i][0]))&0x0000FF00)>>8)
appenddraw(((8*(7*fnegdata[pass][i][1]+fnegdata[pass][i][0]))&0x00FF0000)>>16)
appenddraw(((8*(7*fnegdata[pass][i][1]+fnegdata[pass][i][0]))&0xFF000000)>>24)
}
}
if (negsum<256)
{
appenddraw(0x0F) // paddw mm1, mm2
appenddraw(0xFD)
appenddraw(0xCA)
}
else
{