forked from ImpulseAdventure/JPEGsnoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JPEGsnoopDoc.cpp
2227 lines (1812 loc) · 60.7 KB
/
JPEGsnoopDoc.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
// JPEGsnoop - JPEG Image Decoder & Analysis Utility
// Copyright (C) 2017 - Calvin Hass
// http://www.impulseadventure.com/photo/jpeg-snoop.html
//
// This program 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 2 of the License, or
// (at your option) any later version.
//
// This program 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/>.
//
// JPEGsnoopDoc.cpp : implementation of the CJPEGsnoopDoc class
//
#include "stdafx.h"
#include "JPEGsnoop.h"
#include "JPEGsnoopDoc.h"
#include "CntrItem.h"
#include "OffsetDlg.h"
#include "DbSubmitDlg.h"
#include "NoteDlg.h"
#include "OverlayBufDlg.h"
#include "LookupDlg.h"
#include "ExportDlg.h"
#include "DecodeDetailDlg.h"
#include "ExportTiffDlg.h"
//#include "OperationDlg.h"
#include "General.h"
#include "FileTiff.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CJPEGsnoopDoc
IMPLEMENT_DYNCREATE(CJPEGsnoopDoc, CRichEditDoc)
BEGIN_MESSAGE_MAP(CJPEGsnoopDoc, CRichEditDoc)
// Enable default OLE container implementation
ON_COMMAND(ID_OLE_EDIT_LINKS, CRichEditDoc::OnEditLinks)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, CRichEditDoc::OnUpdateEditLinksMenu)
ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST, CRichEditDoc::OnUpdateObjectVerbMenu)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
//ON_COMMAND(ID_FILE_OPENIMAGE, OnFileOpenimage)
ON_COMMAND(ID_FILE_OFFSET, OnFileOffset)
ON_COMMAND(ID_FILE_REPROCESS, OnFileReprocess)
ON_COMMAND(ID_TOOLS_ADDCAMERATODB, OnToolsAddcameratodb)
ON_COMMAND(ID_TOOLS_SEARCHFORWARD, OnToolsSearchforward)
ON_COMMAND(ID_TOOLS_SEARCHREVERSE, OnToolsSearchreverse)
ON_UPDATE_COMMAND_UI(ID_TOOLS_ADDCAMERATODB, OnUpdateToolsAddcameratodb)
ON_UPDATE_COMMAND_UI(ID_TOOLS_SEARCHFORWARD, OnUpdateToolsSearchforward)
ON_UPDATE_COMMAND_UI(ID_TOOLS_SEARCHREVERSE, OnUpdateToolsSearchreverse)
ON_COMMAND_RANGE(ID_PREVIEW_RGB,ID_PREVIEW_CR,OnPreviewRng)
ON_UPDATE_COMMAND_UI_RANGE(ID_PREVIEW_RGB,ID_PREVIEW_CR,OnUpdatePreviewRng)
ON_COMMAND_RANGE(ID_IMAGEZOOM_ZOOMIN,ID_IMAGEZOOM_800,OnZoomRng)
ON_UPDATE_COMMAND_UI_RANGE(ID_IMAGEZOOM_ZOOMIN,ID_IMAGEZOOM_800,OnUpdateZoomRng)
ON_COMMAND(ID_TOOLS_SEARCHEXECUTABLEFORDQT, OnToolsSearchexecutablefordqt)
ON_UPDATE_COMMAND_UI(ID_FILE_REPROCESS, OnUpdateFileReprocess)
ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, OnUpdateFileSaveAs)
ON_COMMAND(ID_TOOLS_EXTRACTEMBEDDEDJPEG, OnToolsExtractembeddedjpeg)
ON_UPDATE_COMMAND_UI(ID_TOOLS_EXTRACTEMBEDDEDJPEG, OnUpdateToolsExtractembeddedjpeg)
ON_COMMAND(ID_TOOLS_FILEOVERLAY, OnToolsFileoverlay)
ON_UPDATE_COMMAND_UI(ID_TOOLS_FILEOVERLAY, OnUpdateToolsFileoverlay)
ON_COMMAND(ID_TOOLS_LOOKUPMCUOFFSET, OnToolsLookupmcuoffset)
ON_UPDATE_COMMAND_UI(ID_TOOLS_LOOKUPMCUOFFSET, OnUpdateToolsLookupmcuoffset)
ON_COMMAND(ID_OVERLAYS_MCUGRID, OnOverlaysMcugrid)
ON_UPDATE_COMMAND_UI(ID_OVERLAYS_MCUGRID, OnUpdateOverlaysMcugrid)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_YCC, OnUpdateIndicatorYcc)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_MCU, OnUpdateIndicatorMcu)
ON_UPDATE_COMMAND_UI(ID_INDICATOR_FILEPOS, OnUpdateIndicatorFilePos)
ON_COMMAND(ID_SCANSEGMENT_DETAILEDDECODE, OnScansegmentDetaileddecode)
ON_UPDATE_COMMAND_UI(ID_SCANSEGMENT_DETAILEDDECODE, OnUpdateScansegmentDetaileddecode)
ON_COMMAND(ID_TOOLS_EXPORTTIFF, OnToolsExporttiff)
ON_UPDATE_COMMAND_UI(ID_TOOLS_EXPORTTIFF, OnUpdateToolsExporttiff)
END_MESSAGE_MAP()
// CJPEGsnoopDoc construction/destruction
// Constructor allocates dynamic structures and resets state
CJPEGsnoopDoc::CJPEGsnoopDoc()
: m_pView(NULL)
{
// Ideally this would be passed by constructor, but simply access
// directly for now.
CJPEGsnoopApp* pApp;
pApp = (CJPEGsnoopApp*)AfxGetApp();
ASSERT(pApp);
m_pAppConfig = pApp->m_pAppConfig;
ASSERT(m_pAppConfig);
if (DEBUG_EN) m_pAppConfig->DebugLogAdd(_T("CJPEGsnoopDoc::CJPEGsnoopDoc() Begin"));
// Allocate the processing core
m_pCore = new CJPEGsnoopCore;
ASSERT(m_pCore);
if (!m_pCore) {
AfxMessageBox(_T("ERROR: Not enough memory for Processing Core"));
exit(1);
}
// Setup link to CDocument for document log
ASSERT(glb_pDocLog);
glb_pDocLog->SetDoc(this);
if (DEBUG_EN) m_pAppConfig->DebugLogAdd(_T("CJPEGsnoopDoc::CJPEGsnoopDoc() Checkpoint 1"));
// Reset all members
Reset();
if (DEBUG_EN) m_pAppConfig->DebugLogAdd(_T("CJPEGsnoopDoc::CJPEGsnoopDoc() Checkpoint 5"));
if (DEBUG_EN) m_pAppConfig->DebugLogAdd(_T("CJPEGsnoopDoc::CJPEGsnoopDoc() End"));
}
// Cleanup all of the allocated classes
CJPEGsnoopDoc::~CJPEGsnoopDoc()
{
if (m_pCore != NULL) {
delete m_pCore;
m_pCore = NULL;
}
// Sever link to CDocument in log
ASSERT(glb_pDocLog);
glb_pDocLog->SetDoc(NULL);
}
// Reset is only called by the constructor, New and Open
// - Clear all major document state
// - Clear log
//
void CJPEGsnoopDoc::Reset()
{
// Reset all members
m_pFile = NULL;
m_lFileSize = 0L;
// No log data available until we open & process a file
m_strPathNameOpened = _T("");
// Indicate to JFIF ProcessFile() that document has changed
// and that the scan decode needs to be redone if it
// is to be displayed.
m_pCore->J_ImgSrcChanged();
// Clean up the quick log
glb_pDocLog->Clear();
}
// NOTE
// Currently, the status bar assignment is done in both the
// OnNewDocument() and OnOpenDocument(). There is probably a
// single entrypoint that would be more suitable to use (one
// that guarantees that the window has been created, not in
// the constructor).
// Main entry point for creation of a new document
BOOL CJPEGsnoopDoc::OnNewDocument()
{
if (DEBUG_EN) m_pAppConfig->DebugLogAdd(_T("CJPEGsnoopDoc::OnNewDocument() Start"));
if (!CRichEditDoc::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
Reset();
// ---------------------------------------
// Get the status bar and configure the decoder to link up to it
CStatusBar* pStatBar;
pStatBar = GetStatusBar();
// Hook up the status bar
m_pCore->J_SetStatusBar(pStatBar);
m_pCore->I_SetStatusBar(pStatBar);
// ---------------------------------------
if (DEBUG_EN) m_pAppConfig->DebugLogAdd(_T("CJPEGsnoopDoc::OnNewDocument() End"));
return TRUE;
}
// Default CreateClientItem() implementation
CRichEditCntrItem* CJPEGsnoopDoc::CreateClientItem(REOBJECT* preo) const
{
return new CJPEGsnoopCntrItem(preo, const_cast<CJPEGsnoopDoc*>(this));
}
// CJPEGsnoopDoc serialization
//
// NOTE:
// - This is called during the standard OnOpenDocument() and presumably
// OnSaveDocument(). Currently it is not implemented.
//
void CJPEGsnoopDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
// Calling the base class CRichEditDoc enables serialization
// of the container document's COleClientItem objects.
// TODO: set CRichEditDoc::m_bRTF = FALSE if you are serializing as text
CRichEditDoc::Serialize(ar);
}
// CJPEGsnoopDoc diagnostics
#ifdef _DEBUG
void CJPEGsnoopDoc::AssertValid() const
{
CRichEditDoc::AssertValid();
}
void CJPEGsnoopDoc::Dump(CDumpContext& dc) const
{
CRichEditDoc::Dump(dc);
}
#endif //_DEBUG
// CJPEGsnoopDoc commands
// Add a line to the end of the log
//
// PRE:
// - m_pView (view from RichEdit must already be established)
//
int CJPEGsnoopDoc::AppendToLog(CString strTxt, COLORREF sColor)
{
ASSERT(m_pView);
if (!m_pView) return -1;
CRichEditCtrl* pCtrl = &m_pView->GetRichEditCtrl();
ASSERT(pCtrl);
if (!pCtrl) return -1;
int nOldLines = 0, nNewLines = 0, nScroll = 0;
long nInsertionPoint = 0;
CHARFORMAT cf;
// Save number of lines before insertion of new text
nOldLines = pCtrl->GetLineCount();
// Initialize character format structure
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR;
cf.dwEffects = 0; // To disable CFE_AUTOCOLOR
cf.crTextColor = sColor;
// Set insertion point to end of text
nInsertionPoint = pCtrl->GetWindowTextLength();
pCtrl->SetSel(nInsertionPoint, -1);
// Set the character format
pCtrl->SetSelectionCharFormat(cf);
// Replace selection. Because we have nothing
// selected, this will simply insert
// the string at the current caret position.
pCtrl->ReplaceSel(strTxt);
// Get new line count
nNewLines = pCtrl->GetLineCount();
// Scroll by the number of lines just inserted
nScroll = nNewLines - nOldLines;
//pCtrl->LineScroll(nScroll);
// **********************************************************
// Very important that we mark the RichEdit log as not
// being modified. Otherwise we will be asked to save
// changes... If the user hit Yes, they may overwrite their
// image file with the log file!
SetModifiedFlag(false); // Mark as not modified
// **********************************************************
return 0;
}
// Call RedrawWindow() on RichEdit window
void CJPEGsnoopDoc::RedrawLog()
{
ASSERT(m_pView);
if (!m_pView) return;
CRichEditCtrl* pCtrl = &m_pView->GetRichEditCtrl();
ASSERT(pCtrl);
pCtrl->RedrawWindow();
}
// Transfer the contents of the QuickLog buffer to the RichEdit control
// for display.
//
// In command-line nogui mode we skip this step as there may not be a
// RichEdit control assigned (m_pView will be NULL). Instead, we will
// later call DoLogSave() to release the buffer.
//
// PRE:
// - m_pView (view from RichEdit must already be established)
//
int CJPEGsnoopDoc::InsertQuickLog()
{
// Assume we are in GUI mode
if (!theApp.m_pAppConfig->bGuiMode) {
ASSERT(false);
return 0;
}
ASSERT(m_pView);
if (!m_pView) return -1;
CRichEditCtrl* pCtrl = &m_pView->GetRichEditCtrl();
ASSERT(pCtrl);
if (!pCtrl) return -1;
int nOldLines = 0, nNewLines = 0, nScroll = 0;
long nInsertionPoint = 0;
CHARFORMAT cf;
// Save number of lines before insertion of new text
nOldLines = pCtrl->GetLineCount();
// Set insertion point to end of text
nInsertionPoint = pCtrl->GetWindowTextLength();
pCtrl->SetSel(nInsertionPoint, -1);
// Replace selection. Because we have nothing
// selected, this will simply insert
// the string at the current caret position.
pCtrl->SetRedraw(false);
unsigned nQuickLines = glb_pDocLog->GetNumLinesLocal();
COLORREF nCurCol = RGB(0,0,0);
COLORREF nLastCol = RGB(255,255,255);
for (unsigned nLine=0;nLine<nQuickLines;nLine++)
{
CString strTxt;
COLORREF sCol;
glb_pDocLog->GetLineLogLocal(nLine,strTxt,sCol);
nCurCol = sCol;
if (nCurCol != nLastCol) {
// Initialize character format structure
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR;
cf.dwEffects = 0; // To disable CFE_AUTOCOLOR
cf.crTextColor = nCurCol;
// Set the character format
pCtrl->SetSelectionCharFormat(cf);
}
pCtrl->ReplaceSel(strTxt);
}
pCtrl->SetRedraw(true);
pCtrl->RedrawWindow();
// Empty the quick log since we've used it now
glb_pDocLog->Clear();
// Get new line count
nNewLines = pCtrl->GetLineCount();
// Scroll by the number of lines just inserted
nScroll = nNewLines - nOldLines;
//pCtrl->LineScroll(nScroll);
// Scroll to the top of the window
pCtrl->LineScroll(-nNewLines);
// **********************************************************
// Very important that we mark the RichEdit log as not
// being modified. Otherwise we will be asked to save
// changes... If the user hit Yes, they may overwrite their
// image file with the log file!
SetModifiedFlag(false); // Mark as not modified
// **********************************************************
return 0;
}
// Save the view pointer (from View init)
void CJPEGsnoopDoc::SetupView(CRichEditView* pView)
{
m_pView = pView;
}
// ***************************************
// Fetch a pointer to the frame's status bar
//
// RETURN:
// - Pointer to the main frame's status bar
//
CStatusBar* CJPEGsnoopDoc::GetStatusBar()
{
CWnd *pMainWnd = AfxGetMainWnd();
if (!pMainWnd) return NULL;
if (pMainWnd->IsKindOf(RUNTIME_CLASS(CFrameWnd)))
{
CWnd* pMessageBar = ((CFrameWnd*)pMainWnd)->GetMessageBar();
return DYNAMIC_DOWNCAST(CStatusBar,pMessageBar);
}
else
return DYNAMIC_DOWNCAST(CStatusBar,pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR));
}
// NOTE:
// - When calling AnalyzeClose() from CDocument, may need to address the following:
// // Mark the doc as clean so that we don't get questioned to save anytime
// // we change the file or quit.
// SetModifiedFlag(false);
// Read a line from the current opened file (m_pFile)
// UNUSED
BOOL CJPEGsnoopDoc::ReadLine(CString& strLine,
int nLength,
LONG lOffset /* = -1L */)
{
ULONGLONG lPosition;
if (lOffset != -1L)
lPosition = m_pFile->Seek(lOffset,CFile::begin);
else
lPosition = m_pFile->GetPosition();
if (lPosition == -1L)
{
TRACE2("CJPEGsnoopDoc::ReadLine returns FALSE Seek (%8.8lX, %8.8lX)\n",
lOffset, lPosition);
return FALSE;
}
BYTE* pszBuffer = new BYTE[nLength];
if (!pszBuffer) {
AfxMessageBox(_T("ERROR: Not enough memory for Document ReadLine"));
exit(1);
}
int nReturned = m_pFile->Read(pszBuffer, nLength);
// The Read is supposed to only return a maximum of nLength bytes
// This check helps avoid a Read Overrun Code Analysis warning below
// warning : C6385: Reading invalid data from 'pszBuffer': the readable size
// is 'nLength*1' bytes, but '2' bytes may be read.
if (nReturned > nLength) {
return FALSE;
}
if (nReturned <= 0)
{
TRACE2("CJPEGsnoopDoc::ReadLine returns FALSE Read (%d, %d)\n",
nLength,
nReturned);
if (pszBuffer) {
delete [] pszBuffer;
}
return FALSE;
}
CString strTemp;
CString strCharsIn;
strTemp.Format(_T("%8.8I64X - "), lPosition);
strLine = strTemp;
for (int nIndex = 0; nIndex < nReturned; nIndex++)
{
if (nIndex == 0)
strTemp.Format(_T("%2.2X"), pszBuffer[nIndex]);
else if (nIndex %16 == 0)
strTemp.Format(_T("=%2.2X"), pszBuffer[nIndex]);
else if (nIndex %8 == 0)
strTemp.Format(_T("-%2.2X"), pszBuffer[nIndex]);
else
strTemp.Format(_T(" %2.2X"), pszBuffer[nIndex]);
if (_istprint(pszBuffer[nIndex]))
strCharsIn += pszBuffer[nIndex];
else
strCharsIn += _T('.');
strLine += strTemp;
}
if (nReturned < nLength)
{
CString strPadding(_T(' '),3*(nLength-nReturned));
strLine += strPadding;
}
strLine += _T(" ");
strLine += strCharsIn;
if (pszBuffer) {
delete [] pszBuffer;
}
return TRUE;
}
// --------------------------------------------------------------------
// --- START OF BATCH PROCESSING
// --------------------------------------------------------------------
// The root of the batch recursion. It simply jumps into the
// recursion loop but initializes the search to start with an
// empty search result.
//
// INPUT:
// - strBatchDir The root folder of the batch process
// - bRecSubdir Flag to descend recursively into sub-directories
// - bExtractAll Flag to extract all JPEGs from files
//
void CJPEGsnoopDoc::DoBatchProcess(CString strBatchDir,bool bRecSubdir,bool bExtractAll)
{
bRecSubdir; // Unreferenced param
CFolderDialog myFolderDlg(NULL);
CString strRootDir;
CString strDir;
bool bSubdirs = false;
CString strDirSrc;
CString strDirDst;
// Bring up dialog to select subdir recursion
bool bAllOk= true;
// Here is where we ask about recursion
// We also ask whether "extract all" mode is desired
CBatchDlg myBatchDlg;
myBatchDlg.m_bProcessSubdir = false;
myBatchDlg.m_strDirSrc = m_pAppConfig->strBatchLastInput;
myBatchDlg.m_strDirDst = m_pAppConfig->strBatchLastOutput;
myBatchDlg.m_bExtractAll = bExtractAll;
if (myBatchDlg.DoModal() == IDOK) {
// Fetch the settings from the dialog
bSubdirs = (myBatchDlg.m_bProcessSubdir != 0);
bExtractAll = (myBatchDlg.m_bExtractAll != 0);
strDirSrc = myBatchDlg.m_strDirSrc;
strDirDst = myBatchDlg.m_strDirDst;
// Update the config
m_pAppConfig->strBatchLastInput = myBatchDlg.m_strDirSrc;
m_pAppConfig->strBatchLastOutput = myBatchDlg.m_strDirDst;
m_pAppConfig->Dirty(true);
} else {
bAllOk = false;
}
// In Batch Processing mode, we'll temporarily turn off the Interactive mode
// so that any errors that arise in decoding are only output to the log files
// and not to an alert dialog box.
bool bInteractiveSaved = m_pAppConfig->bInteractive;
m_pAppConfig->bInteractive = false;
if (bAllOk) {
// Allocate core engine
CJPEGsnoopCore* pSnoopCore = NULL;
pSnoopCore = new CJPEGsnoopCore;
ASSERT(pSnoopCore);
if (!pSnoopCore) {
return;
}
// Enable and hook up the status bar
CStatusBar* pStatBar;
pStatBar = GetStatusBar();
pSnoopCore->SetStatusBar(pStatBar);
// Indicate long operation ahead!
CWaitCursor wc;
// TODO: Add a "Cancel Dialog" for the batch operation
// Example code that I can use for single-stepping the operation:
//
// // === START
// COperationDlg LengthyOp(this);
// LengthyOp.SetFunctions( PrepareOperation, NextIteration, GetProgress );
//
// // Until-done based
// BOOL bOk = LengthyOp.RunUntilDone( true );
// // === END
// Generate the batch file list
pSnoopCore->GenBatchFileList(strDirSrc,strDirDst,bSubdirs,bExtractAll);
// Now that we've created the list of files, start processing them
unsigned nBatchFileCount;
nBatchFileCount = pSnoopCore->GetBatchFileCount();
// TODO: Clear the current RichEdit log
DeleteContents();
CString strStat;
for (unsigned nFileInd=0;nFileInd<nBatchFileCount;nFileInd++) {
// Update the status report in window
CString strSrcFname;
strSrcFname = pSnoopCore->GetBatchFileInfo(nFileInd);
strStat.Format(_T("Batch processing file %6u of %6u (%6.2f %%): [%s]"),
nFileInd+1,nBatchFileCount,(100.0*(nFileInd+1)/nBatchFileCount),
(LPCTSTR)strSrcFname);
// Inject progress message into main log window
AppendToLog(strStat+_T("\n"),RGB(1,1,1));
RedrawLog();
// Process file
pSnoopCore->DoBatchFileProcess(nFileInd,true,bExtractAll);
}
AppendToLog(_T("Batch processing complete"),RGB(1,255,1));
RedrawLog();
// TODO: Add the most recent file to the current RichEdit log
// TODO: Clean up after last log output
// Alert the user that we are done
// TODO: Is this the right flag to check?
if (bInteractiveSaved) {
AfxMessageBox(_T("Batch Processing Complete!"));
}
// Deallocate the core
if (pSnoopCore) {
delete pSnoopCore;
pSnoopCore = NULL;
}
}
// Restore the Interactive mode setting
m_pAppConfig->bInteractive = bInteractiveSaved;
}
// --------------------------------------------------------------------
// --- END OF BATCH PROCESSING
// --------------------------------------------------------------------
// Menu command for specifying a file offset to start decoding process
// - Present a dialog box that allows user to specify the address
//
// TODO: Perhaps this menu item should be disabled when no file has been analyzed
//
void CJPEGsnoopDoc::OnFileOffset()
{
COffsetDlg offsetDlg;
// NOTE: This function assumes that we've previously opened a file
// Otherwise, there isn't much point in setting the offset value
// since it gets reset to 0 when we open a new file manually!
if (!m_pCore->IsAnalyzed()) {
return;
}
if (!m_pCore->AnalyzeOpen()) {
return;
}
offsetDlg.SetOffset(theApp.m_pAppConfig->nPosStart);
if (offsetDlg.DoModal() == IDOK) {
theApp.m_pAppConfig->nPosStart = offsetDlg.GetOffset();
// Clean out document first (especially buffer)
DeleteContents();
// Process the file from new offset
m_pCore->AnalyzeFileDo();
InsertQuickLog();
// Indicate that scan data needs to be re-analyzed
m_pCore->J_ImgSrcChanged();
// Now try to update all views. This is particularly important
// for View 2 (ScrollView) as it does not automatically invalidate
UpdateAllViews(NULL);
}
if (m_pCore->IsAnalyzed()) {
m_pCore->AnalyzeClose();
}
}
// Menu command for Adding camera signature to the signature database
// - This command brings up a dialog box to specify the
// characteristics of the selected image
// - Upon completion, the current signature is added to the database
//
void CJPEGsnoopDoc::OnToolsAddcameratodb()
{
CDbSubmitDlg submitDlg;
unsigned nUserSrcPre;
teSource eUserSrc;
CString strUserSoftware;
CString strQual;
CString strUserNotes;
// Return values from JfifDec
CString strDecHash;
CString strDecHashRot;
CString strDecImgExifMake;
CString strDecImgExifModel;
CString strDecImgQualExif;
CString strDecSoftware;
teDbAdd eDecDbReqSuggest;
m_pCore->J_GetDecodeSummary(strDecHash,strDecHashRot,strDecImgExifMake,strDecImgExifModel,strDecImgQualExif,strDecSoftware,eDecDbReqSuggest);
if (strDecHash == _T("NONE"))
{
// No valid signature, can't submit!
CString strTmp = _T("No valid signature could be created, so DB submit is temporarily disabled");
glb_pDocLog->AddLineErr(strTmp);
if (m_pAppConfig->bInteractive)
AfxMessageBox(strTmp);
return;
}
submitDlg.m_strExifMake = strDecImgExifMake;
submitDlg.m_strExifModel = strDecImgExifModel;
submitDlg.m_strExifSoftware = strDecSoftware;
submitDlg.m_strUserSoftware = strDecSoftware;
submitDlg.m_strSig = strDecHash; // Only show unrotated sig
submitDlg.m_strQual = strDecImgQualExif;
// Does the image appear to be edited? If so, warn
// the user before submission...
if (eDecDbReqSuggest == DB_ADD_SUGGEST_CAM) {
submitDlg.m_nSource = 0; // Camera
} else if (eDecDbReqSuggest == DB_ADD_SUGGEST_SW) {
submitDlg.m_nSource = 1; // Software
} else {
submitDlg.m_nSource = 2; // I don't know!
}
if (submitDlg.DoModal() == IDOK) {
strQual = submitDlg.m_strQual;
nUserSrcPre = submitDlg.m_nSource;
strUserSoftware = submitDlg.m_strUserSoftware;
strUserNotes = submitDlg.m_strNotes;
// Prior to v1.8.0 the nUserSrcPre was compared against the ENUM_SOURCE_* values
// - This led to an incorrect mapping to the database value
// - As of v1.8.0 (and DB signature version "03"), this has been corrected
// and a "js_vers" parameter is now passed to the external DB to help resolve
// the error for older versions.
switch (nUserSrcPre) {
case 0:
eUserSrc = ENUM_SOURCE_CAM;
break;
case 1:
eUserSrc = ENUM_SOURCE_SW;
break;
case 2:
eUserSrc = ENUM_SOURCE_UNSURE;
break;
default:
eUserSrc = ENUM_SOURCE_UNSURE;
break;
}
m_pCore->J_PrepareSendSubmit(strQual,eUserSrc,strUserSoftware,strUserNotes);
}
}
// Menu enable status for Tools -> Add camera to database
//
void CJPEGsnoopDoc::OnUpdateToolsAddcameratodb(CCmdUI *pCmdUI)
{
pCmdUI->Enable(m_pCore->IsAnalyzed());
}
// Menu command for searching forward in file for JPEG
// - The search process looks for the JFIF_SOI marker
//
void CJPEGsnoopDoc::OnToolsSearchforward()
{
// Search for start:
unsigned long nSearchPos = 0;
unsigned long nStartPos;
bool bSearchResult;
CString strTmp;
// Get status bar pointer
CStatusBar* pStatBar;
pStatBar = GetStatusBar();
// NOTE: m_bFileAnalyzed doesn't actually refer to underlying file....
// so therefore it is not reliable.
// If file got renamed or moved, AnalyzeOpen() will return false
if (m_pCore->AnalyzeOpen() == false) {
return;
}
nStartPos = theApp.m_pAppConfig->nPosStart;
m_pCore->J_ImgSrcChanged();
// Update status bar
if (pStatBar) {
strTmp.Format(_T("Searching forward..."));
pStatBar->SetPaneText(0,strTmp);
}
m_pCore->B_SetStatusBar(GetStatusBar());
bSearchResult = m_pCore->B_BufSearch(nStartPos,0xFFD8FF,3,true,nSearchPos);
// Update status bar
if (pStatBar) {
strTmp.Format(_T("Done"));
pStatBar->SetPaneText(0,strTmp);
}
if (bSearchResult) {
theApp.m_pAppConfig->nPosStart = nSearchPos;
Reprocess();
} else {
if (m_pAppConfig->bInteractive)
AfxMessageBox(_T("No SOI Marker found in Forward search"));
}
m_pCore->AnalyzeClose();
}
// Menu command for searching reverse in file for JPEG image
// - The search process looks for the JFIF_SOI marker
//
void CJPEGsnoopDoc::OnToolsSearchreverse()
{
// Search for start:
unsigned long nSearchPos = 0;
unsigned long nStartPos;
bool bSearchResult;
CString strTmp;
// Get status bar pointer
CStatusBar* pStatBar;
pStatBar = GetStatusBar();
// If file got renamed or moved, AnalyzeOpen() will return false
if (m_pCore->AnalyzeOpen() == false) {
return;
}
nStartPos = theApp.m_pAppConfig->nPosStart;
m_pCore->J_ImgSrcChanged();
// Don't attempt to search past start of file
if (nStartPos > 0) {
// Update status bar
if (pStatBar) {
strTmp.Format(_T("Searching reverse..."));
pStatBar->SetPaneText(0,strTmp);
}
m_pCore->B_SetStatusBar(GetStatusBar());
bSearchResult = m_pCore->B_BufSearch(nStartPos,0xFFD8FF,3,false,nSearchPos);
// Update status bar
if (pStatBar) {
strTmp.Format(_T("Done"));
pStatBar->SetPaneText(0,strTmp);
}
if (bSearchResult) {
theApp.m_pAppConfig->nPosStart = nSearchPos;
Reprocess();
} else {
theApp.m_pAppConfig->nPosStart = 0;
if (m_pAppConfig->bInteractive)
AfxMessageBox(_T("No SOI Marker found in Reverse search"));
Reprocess();
}
}
m_pCore->AnalyzeClose();
}
// Menu enable status for Tools -> Search forward
//
void CJPEGsnoopDoc::OnUpdateToolsSearchforward(CCmdUI *pCmdUI)
{
pCmdUI->Enable(m_pCore->IsAnalyzed());
}
// Menu enable status for Tools -> Search reverse
//
void CJPEGsnoopDoc::OnUpdateToolsSearchreverse(CCmdUI *pCmdUI)
{
pCmdUI->Enable(m_pCore->IsAnalyzed());
}
// Reprocess the current file
// - This command will initiate all processing of the currently-opened file
// - Show a coach message for first time ever
// - Update both views (invalidate and redraw)
//
BOOL CJPEGsnoopDoc::Reprocess()
{
BOOL bRet = false;
// ------------------------------------------------
// Give coach message
// ------------------------------------------------
// Show coach message once
if (theApp.m_pAppConfig->bDecodeScanImg &&
theApp.m_pAppConfig->bCoachDecodeIdct) {
// Show the coaching dialog
CNoteDlg dlg;
if (theApp.m_pAppConfig->bDecodeScanImgAc) {
dlg.strMsg = COACH_DECODE_IDCT_AC;
} else {
dlg.strMsg = COACH_DECODE_IDCT_DC;
}
dlg.DoModal();
theApp.m_pAppConfig->bCoachDecodeIdct = !dlg.bCoachOff;
theApp.m_pAppConfig->Dirty();
}
// ------------------------------------------------
// Reprocess file
// ------------------------------------------------
// Clean out document first (especially buffer)
DeleteContents();
// Now we can reprocess the file
bRet = m_pCore->AnalyzeFile(m_strPathName);
// Insert the log
InsertQuickLog();
// ------------------------------------------------
// Update views
// ------------------------------------------------
// Now force a redraw (especially for Img View window)
// Force a redraw so that we can see animated previews (e.g. AVI)
// when holding down Fwd/Rev Search hotkey
POSITION pos = GetFirstViewPosition();
if (pos != NULL) {
CView* pFirstView = GetNextView( pos );
pFirstView->Invalidate();
pFirstView->RedrawWindow(NULL,0,RDW_UPDATENOW);
}