-
Notifications
You must be signed in to change notification settings - Fork 2
/
BitmapToPicDialog.cpp
1398 lines (1217 loc) · 44 KB
/
BitmapToPicDialog.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
// BitmapToPicDialog.cpp : implementation file
//
#include "stdafx.h"
#include "SCIPicEditor.h"
#include "BitmapToPicDialog.h"
#include "PicResource.h"
#include "BitmapToPicDialog.h"
#include "PicDrawManager.h"
#include "PicDoc.h"
#define FROMCLIPBOARD_TIMER 2345
int g_iAlgorithm = 1; // RGB euclidian
int g_iPalette = 3; // Smooth half-tone
int g_iScaleImage = 1;
int g_iIgnoreWhite = 0;
int g_nColors = 10;
struct THREADINFO
{
HWND hwndDlg;
int iAlgorithm;
int iPalette;
BOOL fIgnoreWhite;
int nColors;
COLORREF *pCRBitmap;
SIZE size;
HANDLE hEvent;
EGACOLOR picPalette[40];
};
struct THREADRESPONSE
{
CArray<PicCommand, PicCommand> *pcommands;
HBITMAP hbm;
BOOL fTooBig;
THREADRESPONSE() { hbm = NULL; }
~THREADRESPONSE() { DeleteObject(hbm); }
};
void FreeThreadInfo(THREADINFO *pInfo)
{
if (pInfo->pCRBitmap)
{
delete [] pInfo->pCRBitmap;
pInfo->pCRBitmap = NULL;
}
if (pInfo->hEvent)
{
CloseHandle(pInfo->hEvent);
pInfo->hEvent = NULL;
}
}
BOOL InitThreadInfo(THREADINFO *pInfo, HWND hwnd, BOOL fIgnoreWhite, int iAlgorithm, int iPalette, int nColors, CSize &size, const COLORREF *pCRBitmap, EGACOLOR *pPicPalette)
{
BOOL fRet = FALSE;
ZeroMemory(pInfo, sizeof(*pInfo));
pInfo->hwndDlg = hwnd;
pInfo->iAlgorithm = iAlgorithm;
pInfo->iPalette = iPalette;
pInfo->nColors = nColors;
pInfo->pCRBitmap = new COLORREF[size.cx * size.cy];
pInfo->size = size;
pInfo->fIgnoreWhite = fIgnoreWhite;
if (pInfo->pCRBitmap)
{
// Bitmap data
CopyMemory(pInfo->pCRBitmap, pCRBitmap, size.cx * size.cy * sizeof(*pCRBitmap));
// Optional palette.
CopyMemory(pInfo->picPalette, pPicPalette, sizeof(pInfo->picPalette));
pInfo->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (pInfo->hEvent)
{
fRet = TRUE;
}
else
{
FreeThreadInfo(pInfo);
}
}
return fRet;
}
// CBitmapToPicDialog dialog
#define UWM_PICREADY (WM_APP + 0)
#define UWM_CONVERTPROGRESS (WM_APP + 1) // LPARAM is int from 0 to 100
#define UWM_CONVERTSTATUS (WM_APP + 2) // LPARAM is text
CBitmapToPicDialog::CBitmapToPicDialog(CWnd* pParent /*=NULL*/)
: CExtNCW<CExtResizableDialog>(CBitmapToPicDialog::IDD, pParent)
{
_pCRBitmap = NULL;
_nColors = g_nColors;
_iAlgorithm = g_iAlgorithm;
_iPalette = g_iPalette;
_iScaleImage = g_iScaleImage;
_iIgnoreWhite = g_iIgnoreWhite;
_size.SetSize(0, 0);
_pThread = NULL;
_hEvent = NULL;
_fConverting = FALSE;
_pcommands = NULL;
_fTooBig = FALSE;
_pbmpNormal = NULL;
_pbmpScaled = NULL;
_pbmpCurrent = NULL;
_bContrastCenterNormal = 0x80;
_bContrastCenterScaled = 0x80;
_nBrightness = 50;
_nContrast = 50;
_nSaturation = 50;
_fInitializedControls = false;
}
CBitmapToPicDialog::~CBitmapToPicDialog()
{
delete [] _pCRBitmap;
_DeleteCommands();
delete _pbmpNormal;
delete _pbmpScaled;
delete _pbmpCurrent;
g_nColors = _nColors;
g_iAlgorithm = _iAlgorithm;
g_iPalette = _iPalette;
g_iScaleImage = _iScaleImage;
g_iIgnoreWhite = _iIgnoreWhite;
}
void CBitmapToPicDialog::_SetBitmap(HBITMAP hbmp)
{
HBITMAP hOld = m_wndPic.SetBitmap(hbmp);
if (hOld)
{
DeleteObject(hOld);
}
}
void CBitmapToPicDialog::DoDataExchange(CDataExchange* pDX)
{
int nBrightness = _nBrightness;
int nContrast = _nContrast;
int nSaturation = _nSaturation;
int iScale = _iScaleImage;
CExtNCW<CExtResizableDialog>::DoDataExchange(pDX);
if( m_wndSlider1.GetSafeHwnd() != NULL )
{
m_wndSlider1.UpdateSliderWnd();
}
if (!_fInitializedControls)
{
// Necessary for prof-uis
DDX_Control(pDX, IDC_SLIDERNUMCOLORS, m_wndSlider1);
m_wndSlider1.SetStyle(CExtSliderWnd::e_style_t::ES_PROFUIS);
DDX_Control(pDX, IDC_SLIDERBRIGHTNESS, m_wndSlider2);
m_wndSlider2.SetStyle(CExtSliderWnd::e_style_t::ES_PROFUIS);
DDX_Control(pDX, IDC_SLIDERCONTRAST, m_wndSlider3);
m_wndSlider3.SetStyle(CExtSliderWnd::e_style_t::ES_PROFUIS);
DDX_Control(pDX, IDC_SLIDERSATURATION, m_wndSlider4);
m_wndSlider4.SetStyle(CExtSliderWnd::e_style_t::ES_PROFUIS);
DDX_Control(pDX, IDC_BUTTONCONVERT, m_wndButton1);
DDX_Control(pDX, IDC_BUTTONBROWSE, m_wndButton2);
DDX_Control(pDX, IDOK, m_wndButton3);
DDX_Control(pDX, IDCANCEL, m_wndButton4);
DDX_Control(pDX, IDC_RADIO1, m_wndRadio1);
DDX_Control(pDX, IDC_RADIO2, m_wndRadio2);
DDX_Control(pDX, IDC_RADIO3, m_wndRadio3);
DDX_Control(pDX, IDC_RADIO4, m_wndRadio4);
DDX_Control(pDX, IDC_RADIO5, m_wndRadio5);
DDX_Control(pDX, IDC_RADIOHALFTONE, m_wndRadio6);
DDX_Control(pDX, IDC_RADIOSOLID, m_wndRadio7);
DDX_Control(pDX, IDC_RADIOHALFTONESMOOTH, m_wndRadio8);
DDX_Control(pDX, IDC_RADIOPICPALETTE, m_wndRadio9);
DDX_Control(pDX, IDC_STATICGROUP1, m_wndGroup1);
DDX_Control(pDX, IDC_STATICGROUP2, m_wndGroup2);
DDX_Control(pDX, IDC_STATICGROUP3, m_wndGroup3);
DDX_Control(pDX, IDC_STATICGROUP4, m_wndGroup4);
DDX_Control(pDX, IDC_CHECKSCALE, m_wndCheck1);
DDX_Control(pDX, IDC_CHECKIGNOREWHITE, m_wndCheck2);
DDX_Control(pDX, IDC_STATIC1, m_wndLabel1);
DDX_Control(pDX, IDC_STATIC2, m_wndLabel2);
DDX_Control(pDX, IDC_STATIC3, m_wndLabel3);
_fInitializedControls = true;
}
DDX_Control(pDX, IDC_EDITSTATUS, m_wndEditStatus);
m_wndEditStatus.FmtLines(TRUE);
DDX_Control(pDX, IDC_STATICPIC, m_wndPic);
DDX_Control(pDX, IDC_STATICORIG, m_wndOrig);
DDX_Control(pDX, IDC_BUTTONCLIPBOARD, m_wndFromClipboard);
m_wndFromClipboard.EnableWindow(IsClipboardFormatAvailable(CF_BITMAP));
DDX_Control(pDX, IDC_PROGRESSCONVERT, m_wndProgress);
DDX_Check(pDX, IDC_CHECKSCALE, _iScaleImage);
DDX_Check(pDX, IDC_CHECKIGNOREWHITE, _iIgnoreWhite);
// MinMaxSlider busted in MFC 7.0
// DDV_MinMaxSlider(pDX, _nColors, 1, 40);
GetDlgItem(IDC_SLIDERNUMCOLORS)->SendMessage(TBM_SETRANGEMIN, FALSE, (LPARAM) 1);
GetDlgItem(IDC_SLIDERNUMCOLORS)->SendMessage(TBM_SETRANGEMAX, TRUE, (LPARAM) PALETTE_SIZE);
GetDlgItem(IDC_SLIDERNUMCOLORS)->SendMessage(TBM_SETTICFREQ, 10, 0);
DDX_Slider(pDX, IDC_SLIDERNUMCOLORS, _nColors);
GetDlgItem(IDC_SLIDERBRIGHTNESS)->SendMessage(TBM_SETRANGEMIN, FALSE, (LPARAM) 0);
GetDlgItem(IDC_SLIDERBRIGHTNESS)->SendMessage(TBM_SETRANGEMAX, TRUE, (LPARAM) 100);
GetDlgItem(IDC_SLIDERBRIGHTNESS)->SendMessage(TBM_SETTICFREQ, 10, 0);
DDX_Slider(pDX, IDC_SLIDERBRIGHTNESS, _nBrightness);
GetDlgItem(IDC_SLIDERCONTRAST)->SendMessage(TBM_SETRANGEMIN, FALSE, (LPARAM) 0);
GetDlgItem(IDC_SLIDERCONTRAST)->SendMessage(TBM_SETRANGEMAX, TRUE, (LPARAM) 100);
GetDlgItem(IDC_SLIDERCONTRAST)->SendMessage(TBM_SETTICFREQ, 10, 0);
DDX_Slider(pDX, IDC_SLIDERCONTRAST, _nContrast);
GetDlgItem(IDC_SLIDERSATURATION)->SendMessage(TBM_SETRANGEMIN, FALSE, (LPARAM) 0);
GetDlgItem(IDC_SLIDERSATURATION)->SendMessage(TBM_SETRANGEMAX, TRUE, (LPARAM) 100);
GetDlgItem(IDC_SLIDERSATURATION)->SendMessage(TBM_SETTICFREQ, 10, 0);
DDX_Slider(pDX, IDC_SLIDERSATURATION, _nSaturation);
// Enable the convert button if we have a bitmap
((CButton*)GetDlgItem(IDC_BUTTONCONVERT))->EnableWindow(_pbmpCurrent != NULL);
int nID;
switch(_iAlgorithm)
{
case 1:
nID = IDC_RADIO1;
break;
case 2:
nID = IDC_RADIO2;
break;
case 3:
nID = IDC_RADIO3;
break;
case 4:
nID = IDC_RADIO4;
break;
case 5:
nID = IDC_RADIO5;
break;
default:
ASSERT(FALSE);
}
((CButton*)GetDlgItem(nID))->SetCheck(1);
switch(_iPalette)
{
case 1:
nID = IDC_RADIOHALFTONE;
break;
case 2:
nID = IDC_RADIOSOLID;
break;
case 3:
nID = IDC_RADIOHALFTONESMOOTH;
break;
case 4:
nID = IDC_RADIOPICPALETTE;
break;
default:
ASSERT(FALSE);
}
((CButton*)GetDlgItem(nID))->SetCheck(1);
m_wndProgress.SetRange(0, 100);
if ((_nBrightness != nBrightness) ||
(_nContrast != nContrast) ||
(_nSaturation != nSaturation) ||
(_iScaleImage != iScale))
{
_UpdateOrigBitmap();
}
SetTimer(FROMCLIPBOARD_TIMER, 1000, NULL);
}
void CBitmapToPicDialog::OnCancel()
{
if (_fConverting)
{
if (_hEvent)
{
SetEvent(_hEvent);
_hEvent = NULL;
}
}
else
{
__super::OnCancel();
}
}
void CBitmapToPicDialog::OnOK()
{
ASSERT(!_fConverting);
BOOL fClose = TRUE;
if (_fTooBig)
{
if (IDNO == AfxMessageBox("The pic is too big to be saved as an SCI resource. Accept it anyway?", MB_APPLMODAL | MB_ICONQUESTION | MB_YESNO))
{
fClose = FALSE;
}
}
if (fClose)
{
__super::OnOK();
}
}
BEGIN_MESSAGE_MAP(CBitmapToPicDialog, CExtNCW<CExtResizableDialog>)
ON_BN_CLICKED(IDC_BUTTONCONVERT, OnConvert)
ON_MESSAGE(UWM_PICREADY, _OnConvertDone)
ON_MESSAGE(UWM_CONVERTSTATUS, _OnConvertStatus)
ON_MESSAGE(UWM_CONVERTPROGRESS, _OnConvertProgress)
ON_BN_CLICKED(IDC_RADIO4, OnBnClickedRadio4)
ON_BN_CLICKED(IDC_RADIO1, OnBnClickedRadio1)
ON_BN_CLICKED(IDC_RADIO2, OnBnClickedRadio2)
ON_BN_CLICKED(IDC_RADIO3, OnBnClickedRadio3)
ON_BN_CLICKED(IDC_RADIO5, OnBnClickedRadio5)
ON_BN_CLICKED(IDC_RADIOHALFTONE, OnBnClickedRadiohalftone)
ON_BN_CLICKED(IDC_RADIOSOLID, OnBnClickedRadiosolid)
ON_BN_CLICKED(IDC_RADIOHALFTONESMOOTH, OnBnClickedRadiohalftonesmooth)
ON_BN_CLICKED(IDC_CHECKSCALE, OnScaleClicked)
ON_BN_CLICKED(IDC_BUTTONBROWSE, OnBrowse)
ON_BN_CLICKED(IDC_BUTTONCLIPBOARD, OnPasteFromClipboard)
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_RADIOPICPALETTE, OnBnClickedRadiopicpalette)
ON_BN_CLICKED(IDC_CHECKIGNOREWHITE, OnBnClickedCheckignorewhite)
ON_WM_TIMER()
END_MESSAGE_MAP()
// CBitmapToPicDialog message handlers
BOOL IsOneOf(EGACOLOR b, EGACOLOR *rgBytes, int cBytes)
{
for (int i = 0; i < cBytes; i++)
{
if ((rgBytes[i].color1 == b.color1) && (rgBytes[i].color2 == b.color2))
{
return TRUE;
}
}
return FALSE;
}
void _SendStatus(HWND hwnd, PCTSTR pszText)
{
SendMessage(hwnd, UWM_CONVERTSTATUS, NULL, (LPARAM)pszText);
}
//
// Send progress, and bail out if the handle is set.
//
BOOL _SendProgressAndCheckAbort(HWND hwnd, HANDLE hEvent, int nCurrent, int nMax)
{
if (WAIT_OBJECT_0 != WaitForSingleObject(hEvent, 0))
{
SendMessage(hwnd, UWM_CONVERTPROGRESS, NULL, (LPARAM) (100 * nCurrent / nMax));
return FALSE;
}
else
{
return TRUE; // Abort!
}
}
UINT CBitmapToPicDialog::s_ThreadWorker(void *pParam)
{
THREADINFO *pInfo = (THREADINFO*)pParam;
CSize size = pInfo->size;
int cPixels = pInfo->size.cx * pInfo->size.cy;
int iAlgorithm = pInfo->iAlgorithm;
int iPalette = pInfo->iPalette;
BOOL fIgnoreWhite = pInfo->fIgnoreWhite;
EGACOLOR *pPicPalette = pInfo->picPalette;
int cPicPalette = sizeof(pInfo->picPalette);
COLORREF *pCRBitmap = pInfo->pCRBitmap;
CArray<PicCommand, PicCommand> *pcommands = new CArray<PicCommand, PicCommand>;
HWND hwnd = pInfo->hwndDlg;
int nColors = pInfo->nColors;
BOOL fAbort = FALSE;
TCHAR szMsg[MAX_PATH];
BOOL fSuccess = FALSE;
if (pcommands)
{
EGACOLOR *pegaTemp = new EGACOLOR[cPixels];
if (pegaTemp)
{
int rgColorCounts[256]; // Indexed by EGACOLOR_TO_BYTE
ZeroMemory(rgColorCounts, sizeof(rgColorCounts));
_SendStatus(hwnd, TEXT("Mapping image colors to SCI colors"));
// Figure out which EGACOLOR each pixel maps to.
int iInterval = cPixels / 100;
for (int i = 0; !fAbort && (i < cPixels); i++)
{
EGACOLOR curColor;
if (iPalette == 4)
{
// iPalette = 4 means that we use the current pic's palette.
curColor = GetClosestEGAColorFromSet(iAlgorithm, *(pCRBitmap + i), pPicPalette, cPicPalette);
}
else
{
curColor = GetClosestEGAColor(iAlgorithm, iPalette, *(pCRBitmap + i));
}
*(pegaTemp + i) = curColor;
// Keep a count of the number of times each colour appears.
rgColorCounts[EGACOLOR_TO_BYTE(curColor)]++;
if ((i % iInterval) == 0)
{
fAbort = _SendProgressAndCheckAbort(hwnd, pInfo->hEvent, i, cPixels);
}
}
int cMostCommonColors = 0;
EGACOLOR rgMostCommonColors[256];
if (!fAbort)
{
if (iPalette == 4)
{
// This is easy, just copy the pic's palette.
CopyMemory(rgMostCommonColors, pPicPalette, cPicPalette);
// We have 40 "most common colours"
cMostCommonColors = cPicPalette;
}
else
{
// Count unique colors for status purposes
int nUniqueColors = 0;
int i = 0;
for (; i < ARRAYSIZE(rgColorCounts); i++)
{
if (rgColorCounts[i])
{
nUniqueColors++;
}
}
StringCchPrintf(szMsg, ARRAYSIZE(szMsg), TEXT("Finding most common of %d colors"), nUniqueColors);
_SendStatus(hwnd, szMsg);
// Then figure out the most common MAXCONVERTEDCOLORS
for (int i = 0; !fAbort && (i < nColors); i++)
{
int cCount = 0;
int bBest = -1;
for (int j = 0; j < ARRAYSIZE(rgColorCounts); j++)
{
if (rgColorCounts[j] > cCount)
{
cCount = rgColorCounts[j];
bBest = j;
}
}
if (bBest != -1)
{
rgMostCommonColors[cMostCommonColors] = EGAColorFromByte(bBest);
rgColorCounts[bBest] = 0; // Set it to zero so it won't be included in future counts
cMostCommonColors++;
}
else
{
// We're done.
break;
}
fAbort = _SendProgressAndCheckAbort(hwnd, pInfo->hEvent, i, nColors);
}
}
}
if (!fAbort)
{
StringCchPrintf(szMsg, ARRAYSIZE(szMsg), TEXT("Remapping to most common"), cMostCommonColors);
_SendStatus(hwnd, szMsg);
// Now figure out which of the most common MAXCONVERTEDCOLORS each pixel belongs to.
EGACOLOR *pegaTempI = pegaTemp;
COLORREF *pcrTempI = pCRBitmap;
for (int i = 0; !fAbort && (i < cPixels); pegaTempI++, pcrTempI++, i++)
{
if (!IsOneOf(*pegaTempI, rgMostCommonColors, cMostCommonColors))
{
// It wasn't one of our most common colors, so re-evaluate it.
*pegaTempI = GetClosestEGAColorFromSet(iAlgorithm, *pcrTempI, rgMostCommonColors, cMostCommonColors);
}
if ((i % iInterval) == 0)
{
fAbort = _SendProgressAndCheckAbort(hwnd, pInfo->hEvent, i, cPixels);
}
}
}
if (!fAbort)
{
// 4 == use pic's palette
fAbort = s_ConvertToPic(hwnd, pInfo->hEvent, pcommands, pegaTemp, size, fIgnoreWhite, rgMostCommonColors, cMostCommonColors, (iPalette == 4));
}
if (!fAbort)
{
// Put the pic in the static, and give stats on it.
PicResource *pepic = new PicResource();
if (pepic)
{
if (SUCCEEDED(pepic->InitNew()))
{
pepic->InsertCommands(-1, pcommands->GetCount(), pcommands->GetData());
// Make sure the pic is small enough.
BOOL fTooBig = FALSE;
sci::istream serial;
if (pepic->Serialize(&serial))
{
DWORD cMax = 0xff00; // Provide safe boundary, since we have other pic commands in pic
if (serial.GetDataSize() > cMax)
{
fTooBig = TRUE;
StringCchPrintf(szMsg, ARRAYSIZE(szMsg), TEXT("Warning! Pic size: %d bytes (%d too many)"), serial.GetDataSize(), (serial.GetDataSize() - cMax));
}
else
{
StringCchPrintf(szMsg, ARRAYSIZE(szMsg), TEXT("Success! Pic size: %d bytes"), serial.GetDataSize());
}
_SendStatus(hwnd, szMsg);
}
// We're good.
PicDrawManager pdm(pepic);
HBITMAP hbm = pdm.CreateVisualBitmap(320, 190);
if (hbm)
{
THREADRESPONSE *pResponse = new THREADRESPONSE;
if (pResponse)
{
pResponse->hbm = hbm;
pResponse->pcommands = pcommands;
pResponse->fTooBig = fTooBig;
fSuccess = TRUE;
if (::SendMessage(hwnd, UWM_PICREADY, 0, (LPARAM)pResponse))
{
// The information has been transfered.
hbm = NULL;
pcommands = NULL;
}
}
if (hbm)
{
DeleteObject(hbm);
}
}
}
delete pepic;
}
}
delete [] pegaTemp;
}
delete pcommands;
}
if (!fSuccess)
{
// Send null response.
::SendMessage(hwnd, UWM_PICREADY, 0, 0);
}
// Clean up the struct that was passed to us.
FreeThreadInfo(pInfo);
delete pInfo;
return 0;
}
LRESULT CBitmapToPicDialog::_OnConvertDone(WPARAM wParam, LPARAM lParam)
{
_fConverting = FALSE;
_hEvent = NULL;
THREADRESPONSE *pResponse = (THREADRESPONSE*)lParam;
if (pResponse)
{
CRect rectBitmap;
m_wndPic.GetClientRect(&rectBitmap);
m_wndPic.FromBitmap(pResponse->hbm, rectBitmap.Width(), rectBitmap.Height());
ASSERT(_pcommands == NULL);
_pcommands = pResponse->pcommands;
pResponse->pcommands = NULL;
// We got a response, re-enable Ok.
GetDlgItem(IDOK)->EnableWindow();
_fTooBig = pResponse->fTooBig;
delete pResponse;
}
else
{
// It failed for some other reason
}
// Re-enable the convert button, now that we're done.
GetDlgItem(IDC_BUTTONCONVERT)->EnableWindow();
// Reset progress
m_wndProgress.SetPos(0);
return TRUE; // Important, so the caller knows we handled it.
}
LRESULT CBitmapToPicDialog::_OnConvertStatus(WPARAM wParam, LPARAM lParam)
{
_AddToEdit((PCTSTR)lParam);
return TRUE;
}
LRESULT CBitmapToPicDialog::_OnConvertProgress(WPARAM wParam, LPARAM lParam)
{
m_wndProgress.SetPos((int)lParam);
return TRUE;
}
void CBitmapToPicDialog::_DeleteCommands()
{
if (_pcommands)
{
delete _pcommands;
_pcommands = NULL;
}
}
void CBitmapToPicDialog::OnConvert()
{
// Delete any old commands
_DeleteCommands();
// Get the most recent values.
UpdateData(TRUE);
// Do some validation
if ((_nColors < 1) || (_nColors > PALETTE_SIZE))
{
AfxMessageBox(TEXT("Number of colours must be between 1 and 40"), MB_ERRORFLAGS);
return;
}
// Disable the Convert and Ok buttons
GetDlgItem(IDC_BUTTONCONVERT)->EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(FALSE);
// Clear out the status box
m_wndEditStatus.SetWindowText(TEXT(""));
_PrepareBitmapForConversion();
CSize size = _size;
COLORREF *pCRBitmap = _pCRBitmap;
THREADINFO *pInfo = new THREADINFO; // Will be deleted by thread.
if (InitThreadInfo(pInfo, GetSafeHwnd(), (_iIgnoreWhite != 0), _iAlgorithm, _iPalette, _nColors, size, _pCRBitmap, _picPalette))
{
_fConverting = TRUE;
_hEvent = pInfo->hEvent;
_pThread = AfxBeginThread(s_ThreadWorker, pInfo, 0, 0, 0, NULL);
if (_pThread)
{
pInfo = NULL;
}
else
{
FreeThreadInfo(pInfo);
_fConverting = FALSE;
}
if (pInfo)
{
delete pInfo;
}
}
}
void CBitmapToPicDialog::OnBrowse()
{
CFileDialog dialog(TRUE, NULL, NULL, NULL, g_szGdiplusFilter);
if (IDOK == dialog.DoModal())
{
CString strFileName = dialog.GetFileName();
#ifdef UNICODE
Gdiplus::Image *pImage = Bitmap::FromFile(strFileName);
#else
// GDI+ only deals with unicode.
int a = lstrlenA(strFileName);
BSTR unicodestr = SysAllocStringLen(NULL, a);
MultiByteToWideChar(CP_ACP, 0, strFileName, a, unicodestr, a);
Gdiplus::Image *pImage = Bitmap::FromFile(unicodestr);
//... when done, free the BSTR
SysFreeString(unicodestr);
#endif
if (pImage)
{
if (_Init(pImage))
{
_UpdateOrigBitmap();
}
delete pImage;
}
}
}
void CBitmapToPicDialog::InsertIntoPic(CPicDoc *pDoc)
{
ASSERT(_pcommands);
pDoc->InsertCommands(_pcommands->GetCount(), _pcommands->GetData());
}
void CBitmapToPicDialog::_AddToEdit(PCTSTR pszText)
{
m_wndEditStatus.SetWindowText(pszText);
}
BOOL CanConnect(EGACOLOR colorScreen, const EGACOLOR *rgBestColors, int iColorIndex, BOOL *pfExact)
{
*pfExact = FALSE;
// The colorScreen
BYTE bScreen = EGACOLOR_TO_BYTE(colorScreen);
/*if (bScreen == 0xff)
{
return FALSE; // pure white
}*/
// REVIEW: possible optimization: do a fill color at the beginning for the most popular color.
// And then remove that from the list of sorted colours we do. And we'll need *include* it above.
if (EGACOLOR_EQUAL(rgBestColors[iColorIndex], colorScreen))
{
*pfExact = TRUE;
return TRUE; // Yes, this is our color.
}
// We can still connect, as long as it isn't one of the colors we're already used.
for (int i = 0; i < iColorIndex; i++)
{
if (EGACOLOR_EQUAL(colorScreen, rgBestColors[i]))
{
return FALSE;
}
}
return TRUE;
}
BYTE GetPaletteIndexOf(EGACOLOR color, EGACOLOR *rgColor, int cColors)
{
for (int i = 0; i < cColors; i++)
{
if (EGACOLOR_EQUAL(color, rgColor[i]))
{
return (BYTE)i;
}
}
ASSERT(FALSE);
return 0;
}
#define IS_WHITE(color) (EGACOLOR_TO_BYTE(color) == 0xff)
//
// Does the meat of the conversion
// Returns TRUE if it was aborted.
//
BOOL CBitmapToPicDialog::s_ConvertToPic(HWND hwnd, HANDLE hEvent, CArray<PicCommand, PicCommand> *pcommands, EGACOLOR *pegaTemp, CSize &size, BOOL fIgnoreWhite, EGACOLOR *rgColors, int cColors, BOOL fDontSetPalette)
{
BOOL fAbort = FALSE;
// Clean out the commands array.
pcommands->RemoveAll();
if (!fDontSetPalette)
{
// Prepare our palette
EGACOLOR palette[40];
ZeroMemory(palette, sizeof(palette));
ASSERT(cColors <= PALETTE_SIZE);
for (int paletteColor = 0; paletteColor < cColors; paletteColor++)
{
palette[paletteColor] = rgColors[paletteColor];
}
pcommands->Add(PicCommand::CreateSetPalette(0, palette));
}
// An array of EGACOLOR arrays, each of which is cColors long
EGACOLOR *rgOrderedColorsPerLine[190];
int rgNumberOfColorsPerLine[190]; // From 1 to cColors
ZeroMemory(rgOrderedColorsPerLine, sizeof(rgOrderedColorsPerLine));
// Allocate one big blob for all the sub-arrays. We'll point the members of
// rgOrderedColorsPerLine in the next for loop.
ASSERT(size.cy <= ARRAYSIZE(rgOrderedColorsPerLine));
ASSERT(size.cy <= ARRAYSIZE(rgNumberOfColorsPerLine));
_SendStatus(hwnd, TEXT("Analyzing lines"));
EGACOLOR *rgAllColors = new EGACOLOR[cColors * ARRAYSIZE(rgOrderedColorsPerLine)];
if (rgAllColors)
{
for (int line = 0; !fAbort && (line < size.cy); line++)
{
// Assign the array:
rgOrderedColorsPerLine[line] = rgAllColors + (cColors * line);
// An array to count how many strips there are of each on this line.
int fragmentsPerColor[256]; // Uses real EGA numbers
ZeroMemory(fragmentsPerColor, sizeof(fragmentsPerColor));
{
const EGACOLOR *pLine = pegaTemp + size.cx * line;
EGACOLOR curColor = *pLine;
int cFragments = 0;
int startFragment = 0;
int endFragment = 0;
for (int x = 1; x < size.cx; x++)
{
EGACOLOR color = *(pLine + x);
if (!EGACOLOR_EQUAL(color, curColor))
{
if (startFragment == endFragment)
{
// It was just a single dot. That doesn't counts as much as a regular
// fragment - since we care less about optimizing it into a big line, since
// a bunch of little dots are cheaper than a bunch of little lines.
// I have found that the best thing is just to not count it at all.
// However, since we must count this color at least once (or else it will be
// excluded from the line completely), then count the first one.
if (fragmentsPerColor[EGACOLOR_TO_BYTE(curColor)] == 0)
{
fragmentsPerColor[EGACOLOR_TO_BYTE(curColor)]++;
}
}
else
{
fragmentsPerColor[EGACOLOR_TO_BYTE(curColor)]++;
}
// Start a new fragment.
cFragments++;
startFragment = x;
endFragment = x;
curColor = color;
}
else
{
endFragment = x;
}
}
fragmentsPerColor[EGACOLOR_TO_BYTE(curColor)]++;
}
if (fIgnoreWhite)
{
// If we make white the "most common" on every line, then it will be drawn first,
// which is essential if we're "ignoring white"
fragmentsPerColor[0xff] = sPIC_WIDTH + 1;
}
// Now we have a count of the most popular colours on this line, and how many fragments there are of each.
EGACOLOR *rgOrderedColorsForThisLine = rgOrderedColorsPerLine[line];
if (rgOrderedColorsForThisLine)
{
int i = 0;
for (; i < cColors; i++)
{
int cFragCount = 0;
BYTE bestColorIndex = 0;
for (WORD j = 0; j < cColors; j++)
{
BYTE indexIntoFPC = EGACOLOR_TO_BYTE(rgColors[j]);
if (fragmentsPerColor[indexIntoFPC] > cFragCount)
{
cFragCount = fragmentsPerColor[indexIntoFPC];
bestColorIndex = (BYTE)j;
}
}
if (cFragCount == 0)
{
break;
}
else
{
rgOrderedColorsForThisLine[i] = rgColors[bestColorIndex];
// zero out, so we don't count this again.
fragmentsPerColor[EGACOLOR_TO_BYTE(rgColors[bestColorIndex])] = 0;
}
}
rgNumberOfColorsPerLine[line] = i;
ASSERT(i > 0);
}
fAbort = _SendProgressAndCheckAbort(hwnd, hEvent, line, size.cy);
}
}
if (rgAllColors && !fAbort)
{
_SendStatus(hwnd, (TEXT("Generating commands...")));
// Algorithm:
// 1) Have a number for each line, that represents where we are in the ordered array of colors for that line:
int rgCurrentColorPosPerLine[190];
ZeroMemory(rgCurrentColorPosPerLine, sizeof(rgCurrentColorPosPerLine));
// 2) Have a number that indicates, in general, what are we drawing right now.
int currentColorPosWereDrawing = 0;
// 3) for each color pos, 0 to cColors, we do:
for ( ; !fAbort && (currentColorPosWereDrawing < cColors); currentColorPosWereDrawing++)
{
fAbort = _SendProgressAndCheckAbort(hwnd, hEvent, currentColorPosWereDrawing, cColors);
// 4) for each line, we do:
for (int line = 0; line < size.cy; line++)
{
if ((currentColorPosWereDrawing < rgNumberOfColorsPerLine[line]) && // Verify we don't start looking at garbage colours in this line
(rgCurrentColorPosPerLine[line] == currentColorPosWereDrawing)) // Is this line current at the same "color pos" that we're drawing?
{
// This is the colour we'll be using:
EGACOLOR currentColor = rgOrderedColorsPerLine[line][currentColorPosWereDrawing];
// Make this the current color - set it as color 0, and set the visual to taht.
// TODO: PERF: improve size, by setting a palette @ the beginning.
pcommands->Add(PicCommand::CreateSetVisual(0, GetPaletteIndexOf(currentColor, rgColors, cColors)));
for (int realLine = line; realLine < size.cy; realLine++)
{
// Decide to draw this line if it is the same color as THE line
EGACOLOR *rgOrderedColorsForThisLine = rgOrderedColorsPerLine[realLine];
if (EGACOLOR_EQUAL(currentColor, rgOrderedColorsForThisLine[currentColorPosWereDrawing]))
{
const EGACOLOR *pLine = pegaTemp + size.cx * realLine;
for (int x = 0; x < size.cx; x++)
{
if (EGACOLOR_EQUAL(currentColor, *(pLine + x)))
{
// We've hit one of our colours. Now continue until we can't anymore.
int xStart = x;
int xLastExact = xStart;
BOOL fExact;
while ((x < size.cx) && CanConnect(*(pLine + x), rgOrderedColorsForThisLine, currentColorPosWereDrawing, &fExact))
{
if (fExact)
{
xLastExact = x; // This is the last colour that was an exact match.
}
x++;
}
ASSERT(xStart < x); // We must always hit the above loop once.
//int xEnd = x - 1;
int xEnd = xLastExact;
if (fIgnoreWhite && IS_WHITE(currentColor) && ((xStart == 0) || (xEnd == (size.cx - 1))))
{
// Don't do anything.
}
else
{
// Draw line to xStart to xEnd;
if (xStart == xEnd)
{
// Just a dot
pcommands->Add(PicCommand::CreatePattern((WORD)xStart, (WORD)realLine, 0, 0, FALSE, FALSE));
}
else
{
// A line
pcommands->Add(PicCommand::CreateLine((WORD)xStart, (WORD)realLine, (WORD)xEnd, (WORD)realLine));
}
}
}
}
// Increment the colorpos for this line.