-
Notifications
You must be signed in to change notification settings - Fork 2
/
TIPicViewDlg.cpp
2472 lines (2225 loc) · 69.9 KB
/
TIPicViewDlg.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
// TIPicViewDlg.cpp : implementation file
//
#include "stdafx.h"
#include <Windows.h>
#include "TIPicView.h"
#include "TIPicViewDlg.h"
#include "D:\WORK\imgsource\4.0\islibs40_vs17_unicode\ISource.h"
#include "xbtest.h"
#include "ClipboardRead.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern bool StretchHist;
static bool fInSlideMode=false;
extern int PIXA,PIXB,PIXC,PIXD,PIXE,PIXF;
extern int g_orderSlide;
extern int g_ctrlList;
extern int g_nFilter;
extern int g_PowerPaint;
extern int g_nPortraitMode;
extern int pixeloffset, heightoffset;
extern int g_Perceptual;
extern int g_Cartoon;
extern int g_UseColorOnly;
extern int g_UseHalfMulticolor;
extern int g_UseMulticolorOnly;
extern int g_AccumulateErrors;
extern int g_MaxMultiDiff;
extern int g_MaxColDiff;
extern int g_UsePerLinePalette;
extern int g_UsePalette;
extern int g_GreyPalette;
extern bool g_bDisplayPalette;
extern int g_StaticColors;
extern int g_MatchColors;
extern bool g_bStaticByPopularity;
extern int g_OrderedDither;
extern int g_MapSize;
extern unsigned char scanlinepal[192][16][4];
extern double g_PercepR, g_PercepG, g_PercepB;
extern double g_LumaEmphasis;
extern double g_Gamma;
extern wchar_t *cmdFileIn, *cmdFileOut;
extern unsigned char buf8[256*192];
extern RGBQUAD winpal[256];
extern bool fVerbose;
int nLastValidConversionMode = 0;
int nOutputPSize=6144, nOutputCSize=6144, nOutputMSize=6144;
#if SIZE_OF_XBTEST > 32768
#error XB TEST Is too large!
#endif
unsigned char XBWorkBuf[32768];
// the colors are resorted to improve performance in some cases - this macro changes
// internal color indexes back to TI color indexes
// Currently: 0 - white (15)
// 1 - black (1 - no change)
// 2 - grey (14)
// >2 - subtract 1
#define REMAP_COLOR(x) ((x)==0 ? 15 : ((x)==2) ? 14 : ((x)>2) ? (x)-1 : (x) )
enum {
MODE_BITMAP, // bitmap
MODE_GREY_BITMAP, // greyscale bitmap
MODE_MONO_BITMAP, // black and white bitmap
MODE_MULTICOLOR, // multicolor
MODE_DUAL_MULTICOLOR, // dual-multicolor
MODE_HALF_MULTICOLOR, // Half-multicolor
MODE_COLOR_ONLY, // bitmap with all pattern bytes set to 0 (32x192)
MODE_F18_PALETTE, // F18 Palette
MODE_F18_PALETTE_SCANLINE // F18 Palette per scanline
};
// handle to global shared memory used for multiple instances to share random fileloads
// this just makes it easier to compare different render options side by side, hopefully
// it won't break anything
HANDLE hSharedMemory = NULL;
LPVOID pSharedPointer = NULL;
wchar_t szLastFilename[256];
// Assuming TIAP files which are normally 6144 bytes each, PROGRAM image, no 6 byte header
// nSize must be less than 64k
void DoTIFILES(FILE *fp, int nSize) {
/* create TIFILES header */
unsigned char h[128]; // header
memset(h, 0, 128); // initialize
h[0] = 7;
h[1] = 'T';
h[2] = 'I';
h[3] = 'F';
h[4] = 'I';
h[5] = 'L';
h[6] = 'E';
h[7] = 'S';
h[8] = 0; // length in sectors HB
h[9] = (nSize+255)/256; // LB (24*256=6144)
h[10] = 1; // File type (1=PROGRAM)
h[11] = 0; // records/sector
h[12] = nSize%256; // # of bytes in last sector (0=256)
h[13] = 0; // record length
h[14] = 0; // # of records(FIX)/sectors(VAR) LB!
h[15] = 0; // HB
fwrite(h, 1, 128, fp);
}
// Assuming TIAP files which are 6144 bytes each, PROGRAM image, no 6 byte header
// used only with magic filename V9T9, but we pass it anyway. Must be less
// than 8 chars and have no magic symbols
// nSize has the same restrictions as TIFILES above
void DoV9T9(FILE *fp, CString cs, int nSize) {
/* create V9T9 header */
unsigned char h[128]; // header
memset(h, 0, 128); // initialize
h[0] = cs.GetLength()>0?cs[0]:' ';
h[1] = cs.GetLength()>1?cs[1]:' ';
h[2] = cs.GetLength()>2?cs[2]:' ';
h[3] = cs.GetLength()>3?cs[3]:' ';
h[4] = cs.GetLength()>4?cs[4]:' ';
h[5] = cs.GetLength()>5?cs[5]:' ';
h[6] = cs.GetLength()>6?cs[6]:' ';
h[7] = cs.GetLength()>7?cs[7]:' ';
h[8] = cs.GetLength()>8?cs[8]:' ';
h[9] = cs.GetLength()>9?cs[9]:' ';
h[10] = 0;
h[11] = 0;
h[12] = 1;
h[13] = 0;
h[14] = 0;
h[15] = (nSize+255)/256;
fwrite(h, 1, 128, fp);
}
// update a dv254 TI file (just skipping record markers)
// returns new target
unsigned char*UpdateWorkBuf(unsigned char *src, unsigned char *dest, int cnt) {
while (cnt--) {
if ((*dest==0xff)&&(*(dest+1)==0xfe)) {
dest+=2; // skip end of record and beginning of the next one
}
*dest++ = *src++;
}
return dest;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTIPicViewDlg dialog
CTIPicViewDlg::CTIPicViewDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTIPicViewDlg::IDD, pParent)
, m_nOrderSlide(0)
,m_chkPowerPaint(FALSE)
{
//{{AFX_DATA_INIT(CTIPicViewDlg)
// 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);
StretchHist=false;
PIXA=2;
PIXB=2;
PIXC=2;
PIXD=2;
PIXE=1;
PIXF=1;
}
void CTIPicViewDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTIPicViewDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
DDX_Control(pDX,IDC_COMBO1,m_ctrlList);
DDX_Text(pDX,IDC_EDIT1,m_pixelD);
DDV_MinMaxUInt(pDX,m_pixelD,0,16);
DDX_Text(pDX,IDC_EDIT2,m_pixelC);
DDV_MinMaxUInt(pDX,m_pixelC,0,16);
DDX_Text(pDX,IDC_EDIT3,m_pixelB);
DDV_MinMaxUInt(pDX,m_pixelB,0,16);
DDX_Text(pDX,IDC_EDIT4,m_pixelA);
DDV_MinMaxUInt(pDX,m_pixelA,0,16);
DDX_Text(pDX,IDC_EDIT6,m_pixelE);
DDV_MinMaxUInt(pDX,m_pixelE,0,16);
DDX_Text(pDX,IDC_EDIT5,m_pixelF);
DDV_MinMaxUInt(pDX,m_pixelF,0,16);
DDX_CBIndex(pDX,IDC_FILTER,m_nFilter);
DDX_CBIndex(pDX,IDC_PORTRAIT,m_nPortraitMode);
DDX_CBIndex(pDX,IDC_ERRMODE,m_ErrMode);
DDX_Text(pDX,IDC_MAXDIFF,m_MaxDiff);
DDV_MinMaxInt(pDX,m_MaxDiff,1,100);
DDX_Text(pDX,IDC_COLDIFF,m_MaxColDiff);
DDV_MinMaxInt(pDX,m_MaxColDiff,0,100);
DDX_Text(pDX,IDC_PERPR,m_PercepR);
DDV_MinMaxInt(pDX,m_PercepR,0,100);
DDX_Text(pDX,IDC_PERPG,m_PercepG);
DDV_MinMaxInt(pDX,m_PercepG,0,100);
DDX_Text(pDX,IDC_PERPB,m_PercepB);
DDV_MinMaxInt(pDX,m_PercepB,0,100);
DDX_Text(pDX,IDC_STATICCNT,m_StaticCols);
DDV_MinMaxInt(pDX,m_StaticCols,0,14);
DDX_Text(pDX,IDC_LUMA,m_Luma);
DDX_Text(pDX,IDC_GAMMA,m_Gamma);
DDX_Slider(pDX,IDC_ORDERSLIDE,m_nOrderSlide);
DDV_MinMaxInt(pDX,m_nOrderSlide,0,16);
DDX_Control(pDX,IDC_ORDERSLIDE,ctrlOrderSlide);
DDX_Check(pDX,IDC_CHKPOWERPAINT,m_chkPowerPaint);
}
BEGIN_MESSAGE_MAP(CTIPicViewDlg, CDialog)
//{{AFX_MSG_MAP(CTIPicViewDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
ON_BN_CLICKED(IDC_RND, OnRnd)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
ON_BN_DOUBLECLICKED(IDC_RND, OnDoubleclickedRnd)
ON_WM_LBUTTONDBLCLK()
ON_WM_DROPFILES()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON3, &CTIPicViewDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON5, &CTIPicViewDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_PERCEPT, &CTIPicViewDlg::OnBnClickedPercept)
ON_BN_CLICKED(IDC_CARTOON, &CTIPicViewDlg::OnBnClickedToon)
ON_BN_CLICKED(IDC_RELOAD, &CTIPicViewDlg::OnBnClickedReload)
ON_CBN_SELCHANGE(IDC_COMBO1, &CTIPicViewDlg::OnCbnSelchangeCombo1)
ON_BN_CLICKED(IDC_PALETTE, &CTIPicViewDlg::OnBnClickedPalette)
ON_BN_CLICKED(IDC_DISTDEFAULT, &CTIPicViewDlg::OnBnClickedFloyd)
ON_BN_CLICKED(IDC_PERPRESET, &CTIPicViewDlg::OnBnClickedPerpreset)
ON_BN_CLICKED(IDC_PATTERN, &CTIPicViewDlg::OnBnClickedPattern)
ON_BN_CLICKED(IDC_NODITHER, &CTIPicViewDlg::OnBnClickedNodither)
ON_BN_CLICKED(IDC_DISTDEFAULT2, &CTIPicViewDlg::OnBnClickedAtkinson)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_ORDERED, &CTIPicViewDlg::OnBnClickedOrdered)
ON_BN_CLICKED(IDC_ORDERED2, &CTIPicViewDlg::OnBnClickedOrdered2)
ON_BN_CLICKED(IDC_DIAG, &CTIPicViewDlg::OnBnClickedDiag)
ON_BN_CLICKED(IDC_ORDERED3, &CTIPicViewDlg::OnBnClickedOrdered3)
ON_BN_CLICKED(IDC_ORDERED4, &CTIPicViewDlg::OnBnClickedOrdered4)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTIPicViewDlg message handlers
BOOL CTIPicViewDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// fill in the drop list (order must match the enum!)
m_ctrlList.ResetContent();
m_ctrlList.AddString(_T("Bitmap 9918A"));
m_ctrlList.AddString(_T("Greyscale Bitmap 9918A"));
m_ctrlList.AddString(_T("B&W Bitmap 9918A"));
m_ctrlList.AddString(_T("Multicolor 9918"));
m_ctrlList.AddString(_T("Dual-Multicolor (flicker) 9918"));
m_ctrlList.AddString(_T("Half-Multicolor (flicker) 9918A"));
m_ctrlList.AddString(_T("Bitmap color only 9918A"));
m_ctrlList.AddString(_T("Paletted Bitmap F18A"));
m_ctrlList.AddString(_T("Scanline Palette Bitmap F18A"));
m_ctrlList.SetCurSel(g_ctrlList);
OnCbnSelchangeCombo1();
// set up the slider
ctrlOrderSlide.SetRange(0, 16);
// update configuration pane
m_pixelA=PIXA;
m_pixelB=PIXB;
m_pixelC=PIXC;
m_pixelD=PIXD;
m_pixelE=PIXE;
m_pixelF=PIXF;
m_nOrderSlide=g_orderSlide;
m_nFilter=g_nFilter;
m_chkPowerPaint = g_PowerPaint ? true : false;
m_nPortraitMode=g_nPortraitMode;
m_ErrMode=g_AccumulateErrors?1:0;
m_MaxDiff=g_MaxMultiDiff;
m_MaxColDiff = g_MaxColDiff;
m_PercepR = (int)(g_PercepR*100.0);
m_PercepG = (int)(g_PercepG*100.0);
m_PercepB = (int)(g_PercepB*100.0);
m_Luma = (int)(g_LumaEmphasis*100.0);
m_Gamma = (int)(g_Gamma*100.0);
m_StaticCols = g_StaticColors;
m_Popularity = g_bStaticByPopularity ? 1: 0;
PrepareData();
// interface to the dithering ordered buttons
if (g_OrderedDither != 0) {
EnableDitherCtrls(false);
int option = g_OrderedDither + (g_MapSize==4 ? 2: 0);
switch (option) {
case 1: SendDlgItemMessage(IDC_ORDERED, BM_SETCHECK, BST_CHECKED, 0); break;
case 2: SendDlgItemMessage(IDC_ORDERED2, BM_SETCHECK, BST_CHECKED, 0); break;
case 3: SendDlgItemMessage(IDC_ORDERED3, BM_SETCHECK, BST_CHECKED, 0); break;
case 4: SendDlgItemMessage(IDC_ORDERED4, BM_SETCHECK, BST_CHECKED, 0); break;
}
}
// hacky command line interface
if (cmdFileIn != NULL) {
LaunchMain(2, cmdFileIn); // load
}
if (cmdFileOut != NULL) {
OnButton4(); // save
EndDialog(IDOK); // and quit
}
InterlockedExchange(&cs, 0); // no thread running yet
return TRUE; // return TRUE unless you set the focus to a control
}
void CTIPicViewDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTIPicViewDlg::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
{
CPaintDC dc(this);
if (NULL != buf8) {
int dpi = GetDpiForWindow(GetSafeHwnd());
IS40_StretchDraw8Bit(dc, buf8, 256, 192, 256, winpal, DPIFIX(XOFFSET), DPIFIX(0), DPIFIX(256*2), DPIFIX(192*2));
}
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTIPicViewDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTIPicViewDlg::OnOK() {}
void maincode(int mode, CString pFile, double dark);
// this is actually 'Open' now
void CTIPicViewDlg::OnRnd()
{
CString csPath;
if (fInSlideMode) {
OnDoubleclickedRnd();
return;
}
// Get Path filename
CFileDialog dlg(true);
dlg.m_ofn.lpstrTitle=_T("Select file to open: BMP, GIF, JPG, PNG, PCX or TIFF");
if (IDOK != dlg.DoModal()) {
return;
}
csPath=dlg.GetPathName();
LaunchMain(2, csPath.GetString());
}
void CTIPicViewDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
fInSlideMode=true;
CWnd *pCtl=GetDlgItem(IDC_RND);
if (pCtl) pCtl->SetWindowText(_T("Next"));
SetTimer(0, 500, NULL); // start a regular tick to watch for new files
CDialog::OnLButtonDblClk(nFlags, point);
}
// This function doesn't get called normally, but that's okay, we can call it the long way ;)
void CTIPicViewDlg::OnDoubleclickedRnd()
{
static CString csPath;
if (csPath=="") {
// Get Path image
CFileDialog dlg(true);
dlg.m_ofn.lpstrTitle=_T("Select any file in a folder for random load - filename will be ignored");
if (IDOK != dlg.DoModal()) {
return;
}
csPath=dlg.GetPathName();
if (csPath.ReverseFind('\\') != -1) {
csPath=csPath.Left(csPath.ReverseFind('\\'));
}
}
// make sure the shared memory is available
if (NULL == hSharedMemory) {
hSharedMemory = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4096, _T("Convert9918MappedData"));
if (NULL != hSharedMemory) {
pSharedPointer = MapViewOfFile(hSharedMemory, FILE_MAP_ALL_ACCESS, 0, 0, 4096);
} else {
pSharedPointer = NULL;
}
}
LaunchMain(0,csPath);
// display the picture on the dialog
}
void CTIPicViewDlg::OnCancel()
{
}
void CTIPicViewDlg::OnButton2()
{
if (pixeloffset < 7) {
pixeloffset++;
LaunchMain(1,"");
}
}
void CTIPicViewDlg::OnButton1()
{
if (pixeloffset > -7) {
pixeloffset--;
LaunchMain(1,"");
}
}
// stretch histogram
void CTIPicViewDlg::OnCheck1()
{
CButton *pCtrl=(CButton*)GetDlgItem(IDC_CHECK1);
if (pCtrl) {
if (pCtrl->GetCheck() == BST_CHECKED) {
StretchHist=true;
} else {
StretchHist=false;
}
}
}
void CTIPicViewDlg::OnClose()
{
EndDialog(IDOK);
CDialog::OnClose();
if (pSharedPointer) {
UnmapViewOfFile(pSharedPointer);
pSharedPointer = NULL;
}
if (hSharedMemory) {
CloseHandle(hSharedMemory);
}
}
unsigned char *CTIPicViewDlg::RLEEncode(unsigned char *pbuf, int *nSize, int InSize) {
int nOffset=0;
int nOutputSize=0;
unsigned char *pRet = (unsigned char*)malloc(InSize*2); // worst case
while (nOffset < InSize) {
// decide whether to do a run or data
if ((pbuf[nOffset]==pbuf[nOffset+1]) && (pbuf[nOffset] == pbuf[nOffset+2])) {
// worth doing an RLE run
// work out for how far!
int i=nOffset+3;
while (pbuf[i] == pbuf[nOffset]) {
i++;
if (i>=InSize) break;
if (i-nOffset >= 127) break;
}
pRet[nOutputSize++] = 0x80|(i-nOffset);
pRet[nOutputSize++] = pbuf[nOffset];
nOffset=i;
} else {
// not worth doing RLE -- see for how long
int i=nOffset+1;
while ((pbuf[i]!=pbuf[i+1]) || (pbuf[i] != pbuf[i+2])) {
i++;
if (i>=InSize) break;
if (i-nOffset >= 127) break;
}
pRet[nOutputSize++] = i-nOffset;
for (int c=0; c<i-nOffset; c++) {
pRet[nOutputSize++] = pbuf[nOffset+c];
}
nOffset=i;
}
}
*nSize = nOutputSize;
debug(_T("RLE compress table from %d to %d bytes\n"), InSize, nOutputSize);
return pRet;
}
extern unsigned char buf8[256*192];
extern bool fFirstLoad;
void CTIPicViewDlg::OnButton4()
{
FILE *fP = NULL, *fC = NULL, *fM = NULL;
unsigned char cbuf[6144], pbuf[6144], mbuf[6144];
unsigned char *pRLEC = NULL, *pRLEP = NULL, *pRLEM = NULL;
int nOffset;
memset(cbuf, 0, sizeof(cbuf));
memset(pbuf, 0, sizeof(pbuf));
memset(mbuf, 0, sizeof(mbuf));
if (!fFirstLoad) {
if (fVerbose) {
debug(_T("No file loaded to save.\n"));
}
return;
}
enum FILETYPES {
ftTIFILES = 1,
ftV9T9,
ftRaw,
ftRLE,
ftTIXB,
ftTIXBRLE,
ftMSXSC2,
ftCVPaint,
ftCVPowerPaint,
ftCVHGR,
ftColecoROM,
ftColecoROMRLE,
ftPNG
};
CString csFmt = _T("TIFILES Format Header|*.*|V9T9 Format Header|*.*|Raw Files|*.*|RLE Files|*.*|TI XB Program|*.*|TI XB RLE Program|*.*|MSX SC2|*.SC2|Coleco CVPaint|*.pc|Adam PowerPaint 10k|*.pp|Adam HGR|*.hgr,*.hgrh|ColecoVision Cart|*.ROM|ColecoVision RLE Cart (Broken)|*.ROM|PNG file (PC)|*.PNG||");
CFileDialog dlg(false, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, csFmt);
// Save image
dlg.m_ofn.lpstrTitle=_T("Enter base name - do not include extensions.");
if (!ResetToValidConversion()) {
return;
}
if ((cmdFileOut!=NULL) || (IDOK == dlg.DoModal())) {
CString cs, outfile, rawfile;
HANDLE hFile;
bool bOverwrite=false;
outfile=dlg.GetPathName();
if (cmdFileOut != NULL) {
outfile = cmdFileOut;
}
if (dlg.m_pOFN->nFilterIndex==ftV9T9) {
rawfile=dlg.GetFileName();
if (rawfile.GetLength() > 6) {
printf("Truncating outfile filename for V9T9\n");
int nCount=rawfile.GetLength()-6;
outfile=outfile.Left(outfile.GetLength()-nCount);
rawfile=rawfile.Left(6);
}
}
// strip extension if present
if (outfile.ReverseFind('.') != -1) {
outfile=outfile.Left(outfile.ReverseFind('.'));
}
// check for existance
bool checkTI = false;
switch (dlg.m_pOFN->nFilterIndex) {
case ftTIFILES:
case ftV9T9:
case ftRaw:
case ftRLE:
checkTI = true;
break;
case ftTIXB:
case ftTIXBRLE:
case ftMSXSC2:
case ftCVPaint:
case ftCVPowerPaint:
case ftCVHGR:
case ftColecoROM:
case ftColecoROMRLE:
case ftPNG:
checkTI = false;
break;
}
// TODO: this is probably no longer very good with the multitude of extensions we have
if (checkTI) {
// overwrite test for TIAP filenames
cs=outfile;
cs+=".TIAP";
hFile=CreateFile(cs, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE != hFile) {
CloseHandle(hFile);
if (cmdFileIn == NULL) {
if (IDYES != AfxMessageBox(_T("File(s) exist. Overwrite?"), MB_YESNO)) {
return;
}
}
bOverwrite=true;
}
if (nOutputCSize > 0) {
cs=outfile;
cs+=".TIAC";
hFile=CreateFile(cs, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE != hFile) {
CloseHandle(hFile);
if (!bOverwrite) {
if (cmdFileIn == NULL) {
if (IDYES != AfxMessageBox(_T("File(s) exist. Overwrite?"), MB_YESNO)) {
return;
}
}
}
bOverwrite=true;
}
}
if (g_UseHalfMulticolor) {
cs=outfile;
cs+=".TIAM";
hFile=CreateFile(cs, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE != hFile) {
CloseHandle(hFile);
if (!bOverwrite) {
if (cmdFileIn == NULL) {
if (IDYES != AfxMessageBox(_T("File(s) exist. Overwrite?"), MB_YESNO)) {
return;
}
}
}
bOverwrite=true;
}
}
}
// we now get to process buf8. we have to increment all color indexes by 1 to save
switch (dlg.m_pOFN->nFilterIndex) {
default:
case ftTIFILES: // TIFILES
dlg.m_pOFN->nFilterIndex=ftTIFILES; // just to be sure (because of the default case)
debug(_T("Writing TIFILES headers.\n"));
break;
case ftV9T9: // V9T9
debug(_T("Writing V9T9 headers.\n"));
break;
case ftRaw: // No header
debug(_T("No headers will be written.\n"));
break;
case ftRLE: // RLE
debug(_T("RLE files with no header will be written\n"));
break;
case ftTIXB: // XB Program
if (g_UseHalfMulticolor) {
AfxMessageBox(_T("Half-multicolor not supported with Extended BASIC."));
return ;
}
if (g_UseMulticolorOnly) {
AfxMessageBox(_T("Multicolor not supported with Extended BASIC."));
return ;
}
if (g_UsePerLinePalette) {
AfxMessageBox(_T("Per-Line paletted images not supported with Extended BASIC."));
return ;
}
if (g_UsePalette) {
AfxMessageBox(_T("Paletted images not supported with Extended BASIC."));
return ;
}
debug(_T("XB Program image with TIFILES header will be written\n"));
break;
case ftTIXBRLE: // XB RLE Program
if (g_UseHalfMulticolor) {
AfxMessageBox(_T("Half-multicolor not supported with Extended BASIC."));
return ;
}
if (g_UseMulticolorOnly) {
AfxMessageBox(_T("Multicolor not supported with Extended BASIC."));
return ;
}
if (g_UsePerLinePalette) {
AfxMessageBox(_T("Per-Line paletted images not supported with Extended BASIC."));
return ;
}
if (g_UsePalette) {
AfxMessageBox(_T("Paletted images not supported with Extended BASIC."));
return ;
}
debug(_T("XB RLE Program image with TIFILES header will be written\n"));
break;
case ftMSXSC2: // MSX SC2
if (g_UseHalfMulticolor) {
AfxMessageBox(_T("Half-multicolor not supported with MSX dump."));
return ;
}
if (g_UseMulticolorOnly) {
AfxMessageBox(_T("Multicolor not supported with MSX dump."));
return ;
}
if (g_UsePerLinePalette) {
AfxMessageBox(_T("Per-Line paletted images not supported with MSX dump."));
return ;
}
if (g_UsePalette) {
AfxMessageBox(_T("Paletted images not supported with MSX dump."));
return ;
}
debug(_T("MSX *.SC2 type image will be written\n"));
break;
case ftCVPaint: // No header
case ftCVPowerPaint:
debug(_T("No headers will be written.\n"));
break;
case ftCVHGR:
debug(_T("Type Adam HGR will be written.\n"));
break;
case ftColecoROM: // ColecoVision cartridge
if (g_UseHalfMulticolor) {
AfxMessageBox(_T("Half-multicolor not supported with ColecoVision ROM."));
return ;
}
if (g_UseMulticolorOnly) {
AfxMessageBox(_T("Multicolor not supported with ColecoVision ROM."));
return ;
}
if (g_UsePerLinePalette) {
AfxMessageBox(_T("Per-Line paletted images not supported with ColecoVision ROM."));
return ;
}
if (g_UsePalette) {
AfxMessageBox(_T("Paletted images not supported with ColecoVision ROM."));
return ;
}
debug(_T("ColecoVision cartridge will be written\n"));
break;
case ftColecoROMRLE: // ColecoVision RLE cartridge
if (g_UseHalfMulticolor) {
AfxMessageBox(_T("Half-multicolor not supported with ColecoVision ROM."));
return ;
}
if (g_UseMulticolorOnly) {
AfxMessageBox(_T("Multicolor not supported with ColecoVision ROM."));
return ;
}
if (g_UsePerLinePalette) {
AfxMessageBox(_T("Per-Line paletted images not supported with ColecoVision ROM."));
return ;
}
if (g_UsePalette) {
AfxMessageBox(_T("Paletted images not supported with ColecoVision ROM."));
return ;
}
debug(_T("ColecoVision RLE cartridge will be written\n"));
break;
case ftPNG: // PNG file for PC
// this might work, right now I don't want to figure out how ;)
if (g_UsePerLinePalette) {
AfxMessageBox(_T("Per-Line paletted images not currently supported with PNG export."));
return ;
}
debug(_T("PC PNG file will be written\n"));
break;
}
// WARNING: All file types must open their file in this switch!!
// TODO: the filename changes here mean that we don't always warn about overwrite
// We should also check whether we needed to add the extension...
cs=outfile;
switch (dlg.m_pOFN->nFilterIndex) {
case ftTIFILES:
case ftV9T9:
case ftRaw:
case ftRLE:
if ((cs.Right(2).MakeUpper() != _T("_P")) && (cs.Right(5).MakeUpper() != _T(".TIAP"))) {
cs+="_P";
}
cs.MakeUpper();
{
CString csRealName = cs;
if (csRealName.Right(2).MakeUpper() == _T("_P")) {
csRealName = csRealName.Left(csRealName.GetLength()-2) + _T(".TIAP");
}
_wfopen_s(&fP, csRealName, _T("wb"));
}
if (NULL == fP) {
AfxMessageBox(_T("Failed to open _P file"));
return;
}
break;
case ftTIXB:
case ftTIXBRLE:
// we still need to open the output file - no name change from requested
_wfopen_s(&fP, cs, _T("wb"));
if (NULL == fP) {
AfxMessageBox(_T("Failed to open output file"));
return;
}
break;
case ftMSXSC2:
if (cs.Right(4).MakeUpper() != _T(".SC2")) {
cs +=".sc2";
}
// we still need to open the output file - no name change from requested
_wfopen_s(&fP, cs, _T("wb"));
if (NULL == fP) {
AfxMessageBox(_T("Failed to open output file"));
return;
}
break;
// TODO: we should import CVPaint and PowerPaint too
case ftCVPaint:
// just a single output file for Coleco CVPaint
if (cs.Right(3).MakeUpper() != _T(".PC")) {
cs+=_T(".pc");
}
_wfopen_s(&fP, cs, _T("wb"));
if (NULL == fP) {
AfxMessageBox(_T("Failed to open output file"));
return;
}
break;
case ftCVPowerPaint:
// just a single output file for Coleco Adam Powerpaint
if (cs.Right(3).MakeUpper() != _T(".PP")) {
cs+=_T(".pp");
}
_wfopen_s(&fP, cs, _T("wb"));
if (NULL == fP) {
AfxMessageBox(_T("Failed to open output file"));
return;
}
break;
case ftCVHGR:
// just a single output file for Coleco Adam HGR
if ((cs.Right(3).MakeUpper() != _T(".HGR"))&&((cs.Right(3).MakeUpper() != _T(".HGRH")))) {
cs+=_T(".HGRH");
}
_wfopen_s(&fP, cs, _T("wb"));
if (NULL == fP) {
AfxMessageBox(_T("Failed to open output file"));
return;
}
break;
case ftColecoROM:
case ftColecoROMRLE:
// colecovision
if (cs.Right(4).MakeUpper() != _T(".ROM")) {
cs+=_T(".rom");
}
_wfopen_s(&fP, cs, _T("wb"));
if (NULL == fP) {
AfxMessageBox(_T("Failed to open output file"));
return;
}
break;
case ftPNG:
// PC PNG
if (cs.Right(4).MakeUpper() != _T(".PNG")) {
cs+=_T(".png");
}
_wfopen_s(&fP, cs, _T("wb"));
if (NULL == fP) {
AfxMessageBox(_T("Failed to open output file"));
return;
}
break;
}
// Is this a bug for TIFILES headers? The RLE files write the wrong filesize??
// prepare headers
switch (dlg.m_pOFN->nFilterIndex) {
case ftTIFILES: // TIFILES