// SegDemoView.cpp : implementation of the CSegDemoView class // #include "stdafx.h" #include "leadscrollinfo.h" #include "OptionsSheet.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include "math.h" extern CSegDemoApp theApp; ///////////////////////////////////////////////////////////////////////////// // CSegDemoView IMPLEMENT_DYNCREATE(CSegDemoView, CFormView) BEGIN_MESSAGE_MAP(CSegDemoView, CFormView) //{{AFX_MSG_MAP(CSegDemoView) ON_WM_SIZE() ON_WM_DESTROY() ON_WM_ERASEBKGND() ON_COMMAND(ID_FILE_SAVEAS, OnFileSaveAs) ON_COMMAND(ID_FILE_SAVEMRCLEAD, OnFileSaveLeadMrc) ON_COMMAND(ID_FILE_SAVEMRC, OnFileSaveMrc) ON_COMMAND(ID_FILE_EXPORTSEGMENTS, OnFileExportSegments) ON_COMMAND(ID_FILE_IMPORTSEGMENTS, OnFileImportSegments) ON_COMMAND(ID_AUTOSEGMENTATION, OnAutoSegmentation) ON_COMMAND(ID_AUTO_MANUAL_SEGMENTATION, OnAutoManualSegmentation) ON_COMMAND(ID_EDIT_COPY, OnEditCopy) ON_COMMAND(ID_CLEAR_SEGMENTS, OnClearSegments) ON_COMMAND(ID_DRAW_SEGMENT, OnDrawSegment) ON_COMMAND(ID_DRAW_CANCELRECT, OnDrawCancelRect) ON_COMMAND(ID_SHOWSEGMENTTYPE, OnShowSegmentType) ON_UPDATE_COMMAND_UI(ID_SHOWSEGMENTTYPE, OnUpdateShowSegmentType) ON_COMMAND(ID_OPTIONS, OnOptions) ON_COMMAND(ID_COLOR_COLORRES, OnColorRes) ON_COMMAND(ID_COLOR_HISTOGRAM, OnColorHistogram) ON_COMMAND(ID_COLOR_UINQUECOLORS, OnColorUinqueColors) ON_COMMAND(ID_SEGMENTTYPE, OnSegmenttType) ON_COMMAND(ID_SHOWSEGPROP, OnShowSegInfo) ON_COMMAND(ID_SEGMENTHISTOGRAM, OnSegmentHistogram) ON_COMMAND(ID_SEGMENTUNIQUECOLORS, OnSegmentUniqueColors) ON_COMMAND(ID_ZOOMING_NORMAL, OnZoomingNormal) ON_UPDATE_COMMAND_UI(ID_ZOOMING_NORMAL, OnUpdateZoomingNormal) ON_COMMAND(ID_FITTOWINDOW, OnFittowindow) ON_UPDATE_COMMAND_UI(ID_FITTOWINDOW, OnUpdateFittowindow) ON_COMMAND(ID_ZOMMING_IN, OnZommingIn) ON_COMMAND(ID_ZOMMING_OUT, OnZommingOut) ON_COMMAND(ID_SHOWINNEWWINDOW, OnShowInNewWindow) ON_COMMAND(ID_SELECTALLSEGMENTS, OnSelectAllSegments) ON_COMMAND(ID_DELETESEGMENT, OnDeleteSegment) ON_UPDATE_COMMAND_UI(ID_FILE_EXPORTSEGMENTS, OnUpdateFileExportsegments) ON_UPDATE_COMMAND_UI(ID_SELECTALLSEGMENTS, OnUpdateSelectAllSegments) ON_UPDATE_COMMAND_UI(ID_DELETESEGMENT, OnUpdateDeleteSegment) ON_UPDATE_COMMAND_UI(ID_CLEAR_SEGMENTS, OnUpdateClearSegments) ON_UPDATE_COMMAND_UI(ID_DRAW_SEGMENT, OnUpdateDrawSegment) ON_UPDATE_COMMAND_UI(ID_DRAW_CANCELRECT, OnUpdateDrawCancelrect) ON_UPDATE_COMMAND_UI(ID_AUTO_MANUAL_SEGMENTATION, OnUpdateAutoManualSegmentation) ON_COMMAND(ID_COMBINESEGMENTS, OnCombineSegments) ON_UPDATE_COMMAND_UI(ID_COMBINESEGMENTS, OnUpdateCombineSegments) ON_UPDATE_COMMAND_UI(ID_SHOWINNEWWINDOW, OnUpdateShowInNewWindow) ON_UPDATE_COMMAND_UI(ID_SEGMENTTYPE, OnUpdateSegmentType) ON_UPDATE_COMMAND_UI(ID_SHOWSEGPROP, OnUpdateShowSegInfo) ON_UPDATE_COMMAND_UI(ID_SEGMENTHISTOGRAM, OnUpdateSegmentHistogram) ON_UPDATE_COMMAND_UI(ID_SEGMENTUNIQUECOLORS, OnUpdateSegmentUniqueColors) ON_COMMAND(ID_ENLARGESEGMENT, OnEnlargeSegment) ON_COMMAND(ID_FILE_SAVEPDF, OnFileSavePdf) ON_UPDATE_COMMAND_UI(ID_ENLARGESEGMENT, OnUpdateEnlargeSegment) ON_COMMAND(ID_DESELECTALL, OnDeselectAll) ON_WM_SETCURSOR() ON_WM_LBUTTONUP() ON_UPDATE_COMMAND_UI(ID_DESELECTALL, OnUpdateDeselectAll) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSegDemoView construction/destruction BOOL CSegDemoView ::ShowCursor() { HCURSOR hCursor; RECT Rect, rcClient; RECT rcSelectedLeft, rcSelectedRight; RECT rcSelectedUp, rcSelectedDown; RECT rcSelectedLT, rcSelectedRT; RECT rcSelectedLB, rcSelectedRB; int i, x1, y1, x2, y2; POINT pt; // If right mouse is clicked then , change cursor into arrow if (m_nLineSel == RESIZE_CONTEXTMNEU) { m_nLineSel = RESIZE_NOTHING; return FALSE;; } // Find the client area GetClientRect(&rcClient); GetCursorPos(&pt); // Find the cursor position ScreenToClient(&pt); // Check if the cursor position is out of window if ((rcClient.left > pt.x) || (rcClient.top > pt.y) || (rcClient.right < pt.x) || (rcClient.bottom < pt.y)) return FALSE;; // Are we drawing ? m_bMouseMove = FALSE; if (m_bDrawSegment) { SetCursor(LoadCursor(NULL, IDC_CROSS)); return TRUE; } // Check if the there is a segment selected or the manual process is active if (FindSelectionCount() == 0) { // Set the defualt cursor hCursor = LoadCursor(NULL, IDC_ARROW); if (hCursor) SetCursor(hCursor); return FALSE; } if (!m_bDrawSegment) { for (i = 0; i < m_SegInfo.GetSize(); i++) { if(IsSegmentSelected(i)) { CopyRect(&Rect, &m_SegInfo[i].rcRect); ChangeToZoom(&Rect); // Get the selection rectangles position // Find the Top selection rect x1 = (Rect.right + Rect.left) / 2 - 3 - m_RasterView.GetHScrollInfo().GetPos(); y1 = Rect.top - 3 - m_RasterView.GetVScrollInfo().GetPos(); x2 = (Rect.right + Rect.left) / 2 + 3 - m_RasterView.GetHScrollInfo().GetPos(); y2 = Rect.top + 3 - m_RasterView.GetHScrollInfo().GetPos(); SetRect(&rcSelectedUp, x1, y1, x2, y2); // Find the Bottom selection rect x1 = (Rect.right + Rect.left) / 2 - 3 - m_RasterView.GetHScrollInfo().GetPos(); y1 = Rect.bottom - 3 - m_RasterView.GetVScrollInfo().GetPos(); x2 = (Rect.right + Rect.left) / 2 + 3 - m_RasterView.GetHScrollInfo().GetPos(); y2 = Rect.bottom + 3 - m_RasterView.GetVScrollInfo().GetPos(); SetRect(&rcSelectedDown, x1, y1, x2, y2); // Find the Left selection rect x1 = Rect.left - 3 - m_RasterView.GetHScrollInfo().GetPos(); y1 = (Rect.top + Rect.bottom) / 2 - 3 - m_RasterView.GetVScrollInfo().GetPos(); x2 = Rect.left + 3 - m_RasterView.GetHScrollInfo().GetPos(); y2 = (Rect.top + Rect.bottom) / 2 + 3 - m_RasterView.GetVScrollInfo().GetPos(); SetRect(&rcSelectedLeft, x1, y1, x2, y2); // Find the Right selection rect x1 = Rect.right - 3 - m_RasterView.GetHScrollInfo().GetPos(); y1 = (Rect.top + Rect.bottom) / 2 - 3 - m_RasterView.GetVScrollInfo().GetPos(); x2 = Rect.right + 3 - m_RasterView.GetHScrollInfo().GetPos(); y2 = (Rect.top + Rect.bottom) / 2 + 3 - m_RasterView.GetVScrollInfo().GetPos(); SetRect(&rcSelectedRight, x1, y1, x2, y2); // Find the Left-Top selection rect x1 = Rect.left - 3 - m_RasterView.GetHScrollInfo().GetPos(); y1 = Rect.top - 3 - m_RasterView.GetVScrollInfo().GetPos(); x2 = Rect.left + 3 - m_RasterView.GetHScrollInfo().GetPos(); y2 = Rect.top + 3 - m_RasterView.GetVScrollInfo().GetPos(); SetRect(&rcSelectedLT, x1, y1, x2, y2); // Find the Right-Top selection rect x1 = Rect.right - 3 - m_RasterView.GetHScrollInfo().GetPos(); y1 = Rect.top - 3 - m_RasterView.GetVScrollInfo().GetPos(); x2 = Rect.right + 3 - m_RasterView.GetHScrollInfo().GetPos(); y2 = Rect.top + 3 - m_RasterView.GetVScrollInfo().GetPos(); SetRect(&rcSelectedRT, x1, y1, x2, y2); // Find the Left-Bottom selection rect x1 = Rect.left - 3 - m_RasterView.GetHScrollInfo().GetPos(); y1 = Rect.bottom - 3 - m_RasterView.GetVScrollInfo().GetPos(); x2 = Rect.left + 3 - m_RasterView.GetHScrollInfo().GetPos(); y2 = Rect.bottom + 3 - m_RasterView.GetVScrollInfo().GetPos(); SetRect(&rcSelectedLB, x1, y1, x2, y2); // Find the Right-Bottom selection rect x1 = Rect.right - 3 - m_RasterView.GetHScrollInfo().GetPos(); y1 = Rect.bottom - 3 - m_RasterView.GetVScrollInfo().GetPos(); x2 = Rect.right + 3 - m_RasterView.GetHScrollInfo().GetPos(); y2 = Rect.bottom + 3 - m_RasterView.GetVScrollInfo().GetPos(); SetRect(&rcSelectedRB, x1, y1, x2, y2); if (PtInRect(&rcSelectedUp, pt) || PtInRect(&rcSelectedDown, pt)) { hCursor = LoadCursor(NULL, IDC_SIZENS); if (hCursor) SetCursor(hCursor); m_bMouseMove = TRUE; return TRUE; } else if (PtInRect(&rcSelectedLeft, pt) || PtInRect(&rcSelectedRight, pt)) { hCursor = LoadCursor(NULL, IDC_SIZEWE); if (hCursor) SetCursor(hCursor); m_bMouseMove = TRUE; return TRUE; } else if (PtInRect(&rcSelectedLT, pt) || PtInRect(&rcSelectedRB, pt)) { hCursor = LoadCursor(NULL, IDC_SIZENWSE); if (hCursor) SetCursor(hCursor); m_bMouseMove = TRUE; return TRUE; } else if (PtInRect(&rcSelectedRT, pt) || PtInRect(&rcSelectedLB, pt)) { hCursor = LoadCursor(NULL, IDC_SIZENESW); if (hCursor) SetCursor(hCursor); m_bMouseMove = TRUE; return TRUE; } else { OffsetRect(&Rect, -m_RasterView.GetHScrollInfo().GetPos(), -m_RasterView.GetVScrollInfo().GetPos()); if (PtInRect(&Rect, pt)) { hCursor = LoadCursor(NULL, IDC_SIZEALL); if (hCursor) SetCursor(hCursor); m_bMouseMove = TRUE; return TRUE; } else { // Set the default cursor hCursor = LoadCursor(NULL, IDC_ARROW); if (hCursor) SetCursor(hCursor); } } } } } return FALSE; } CSegDemoView::CSegDemoView() : CFormView(CSegDemoView::IDD), TEXT_BACKGRND(TEXT("BG")), TEXT_ONECOLOR(TEXT("1C")), TEXT_1BIT_BW(TEXT("T1BW")), TEXT_2BIT_BW(TEXT("T2BW")), TEXT_1BIT_COLOR(TEXT("T1C")), TEXT_2BIT_COLOR(TEXT("T2C")), TEXT_2BIT_GRAYSCALE(TEXT("G2")), TEXT_8BIT_GRAYSCALE(TEXT("G8")), TEXT_PICTURE(TEXT("P")) { m_bManual = FALSE; m_bMouseMove = FALSE; m_bLBDown = FALSE; m_nLineSel = -1; m_bDrawSegment = FALSE; m_bShowSegmentType = TRUE; m_nSelSeg = -1; m_bFitToWindow = FALSE; m_nZoomPercent = 100; //{{AFX_DATA_INIT(CSegDemoView) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // TODO: add construction code here } CSegDemoView::~CSegDemoView() { theApp.m_pRasterFileDlg->PutBitmap(0); } void CSegDemoView::DoDataExchange(CDataExchange* pDX) { CFormView::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSegDemoView) DDX_Control(pDX, IDC_LEADRASTERVIEW1, m_RasterView); //}}AFX_DATA_MAP } BOOL CSegDemoView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CFormView::PreCreateWindow(cs); } void CSegDemoView::OnInitialUpdate() { CFormView::OnInitialUpdate(); CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); ILEADRaster *pRaster =pDoc->m_pRaster; pRaster->RefBitmap = TRUE; m_RasterView.GetRaster().SetBitmap(pRaster->Bitmap); int xExt = (int)pRaster->BitmapWidth, yExt = (int)pRaster->BitmapHeight; // Make the CLead control to fit the bitmap and disable CLead's scrolling // Make the form scroll if the window becomes smaller than the bitmap SetScrollSizes(MM_TEXT,CSize(xExt,yExt)); // Make the window fit the image GetParentFrame()->RecalcLayout(); ResizeParentToFit(); // now disable the scroll bars SetScrollSizes(MM_TEXT,CSize(1,1)); m_RasterView.SetBackErase(FALSE); m_RasterView.SetDoubleBuffer(TRUE); } ///////////////////////////////////////////////////////////////////////////// // CSegDemoView diagnostics #ifdef _DEBUG void CSegDemoView::AssertValid() const { CFormView::AssertValid(); } void CSegDemoView::Dump(CDumpContext& dc) const { CFormView::Dump(dc); } CSegDemoDoc* CSegDemoView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSegDemoDoc))); return (CSegDemoDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CSegDemoView message handlers void CSegDemoView::OnSize(UINT nType, int cx, int cy) { CFormView::OnSize(nType, cx, cy); if( IsWindow(m_RasterView.m_hWnd) ) { m_RasterView.SetBackErase(FALSE); m_RasterView.MoveWindow(0, 0, cx, cy); } } void CSegDemoView::OnDestroy() { CFormView::OnDestroy(); theApp.m_pRasterFileDlg->Bitmap = 0; } BOOL CSegDemoView::OnEraseBkgnd(CDC* pDC) { return TRUE; } void CSegDemoView :: UnSelectSegments() { int i, nCount; RECT SegRect; nCount = m_SegInfo.GetSize(); for (i = 0; i < nCount; i++) { CopyRect(&SegRect, &m_SegInfo[i].rcRect); ChangeToZoom(&SegRect); // Remove the selection m_SegInfo[i].bSelected = FALSE; } } BOOL CSegDemoView :: IsSegmentSelected(int nSegment) { return m_SegInfo[nSegment].bSelected; } void CSegDemoView :: SelectSegments(int x, int y) { short uCtrlPressed =0; POINT pt; RECT rcSelected; int j, i, nCount; BOOL bSelected = FALSE; m_RasterView.SetCapture(); pt.x = x + m_RasterView.GetHScrollInfo().GetPos(); pt.y = y + m_RasterView.GetVScrollInfo().GetPos(); for (i = 0; i < m_SegInfo.GetSize(); i++) { CopyRect(&rcSelected, &m_SegInfo[i].rcRect); ChangeToZoom(&rcSelected); if (PtInRect(&rcSelected, pt)) { bSelected = TRUE; if (!m_SegInfo[i].bSelected) { uCtrlPressed = GetAsyncKeyState(VK_CONTROL); if (uCtrlPressed & 0x8000) { CopyRect(&rcSelected, &m_SegInfo[i].rcRect); ChangeToZoom(&rcSelected); OffsetRect(&rcSelected, -m_RasterView.GetHScrollInfo().GetPos(), -m_RasterView.GetVScrollInfo().GetPos()); m_SegInfo[i].bSelected = TRUE; InflateRect(&rcSelected, 50, 50); } else { nCount = m_SegInfo.GetSize(); for (j = 0; j < nCount; j++) { if(IsSegmentSelected(j)) { CopyRect(&rcSelected, &m_SegInfo[j].rcRect); ChangeToZoom(&rcSelected); OffsetRect(&rcSelected, -m_RasterView.GetHScrollInfo().GetPos(), -m_RasterView.GetVScrollInfo().GetPos()); m_SegInfo[j].bSelected = FALSE; m_nSelSeg = -1; InflateRect(&rcSelected, 50, 50); } } } } m_SegInfo[i].bSelected = TRUE; m_nSelSeg = i; CopyRect(&rcSelected, &m_SegInfo[i].rcRect); ChangeToZoom(&rcSelected); OffsetRect(&rcSelected, -m_RasterView.GetHScrollInfo().GetPos(), -m_RasterView.GetVScrollInfo().GetPos()); InflateRect(&rcSelected, 50, 50); break; } } if (!bSelected) { UnSelectSegments(); } m_RasterView.ForceRepaint(); } void CSegDemoView::UnSelectCurrentSegment(int x, int y) { POINT pt; RECT rcSelected; int i, nCount; m_RasterView.SetCapture(); pt.x = x + m_RasterView.GetHScrollInfo().GetPos(); pt.y = y + m_RasterView.GetVScrollInfo().GetPos(); nCount = m_SegInfo.GetSize(); for (i = 0; i < nCount; i++) { CopyRect(&rcSelected, &m_SegInfo[i].rcRect); ChangeToZoom(&rcSelected); if (PtInRect(&rcSelected, pt)) { CopyRect(&rcSelected, &m_SegInfo[i].rcRect); m_SegInfo[i].bSelected = FALSE; ChangeToZoom(&rcSelected); break; } } } int CSegDemoView::FindSelectionCount() { int i, nSelCount = 0, nSegments = 0; nSegments = m_SegInfo.GetSize(); if (!nSegments) return 0; for (i = 0 ; i < nSegments; i++) { if (m_SegInfo[i].bSelected) nSelCount++; } return nSelCount; } void CSegDemoView::OnFileSaveAs() { theApp.m_pRasterFileDlg->PutBitmap (m_RasterView.GetRaster().GetBitmap()); theApp.m_pRasterFileDlg->PutEnableMethodErrors(FALSE); theApp.m_pRasterFileDlg->PutSaveQFactor(2); theApp.m_pRasterFileDlg->PutFileFormatsCount(0); theApp.m_pRasterFileDlg->PutFileDlgFlags(OFN_OVERWRITEPROMPT); theApp.m_pRasterFileDlg->PutUIFlags(SAVE_SHOW_FILEOPTIONS_STAMP| SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS| SAVE_SHOW_FILEOPTIONS_J2KOPTIONS| SAVE_ENABLESIZING| SAVE_AUTOPROCESS| SAVE_SHOW_FILEOPTIONS_MULTIPAGE | SAVE_SHOW_FILEOPTIONS_PROGRESSIVE | SAVE_SHOW_FILEOPTIONS_QFACTOR); theApp.m_pRasterFileDlg->PutDialogTitle("Save File"); int nRet = theApp.m_pRasterFileDlg->ShowSaveDlg((long)this->m_hWnd); if(nRet !=0) { theApp.DisplayLEADError(nRet); } else { if(theApp.m_pRasterFileDlg->GetDialogStatus()!= DLG_OK) { theApp.m_pRasterFileDlg->PutBitmap(0); return; } } theApp.m_pRasterFileDlg->PutBitmap(0); } void CSegDemoView::OnFileSaveLeadMrc() { int nRet; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); theApp.m_pRasterFileDlg->PutFilter("ALL|*.*|MRC|*.mrc"); theApp.m_pRasterFileDlg->PutBitmap (m_RasterView.GetRaster().GetBitmap()); theApp.m_pRasterFileDlg->PutEnableMethodErrors(FALSE); theApp.m_pRasterFileDlg->PutSubTypeIndex(0); theApp.m_pRasterFileDlg->PutSaveQFactor(2); theApp.m_pRasterFileDlg->PutFileDlgFlags(OFN_OVERWRITEPROMPT); theApp.m_pRasterFileDlg->PutUIFlags(SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS| SAVE_SHOW_FILEOPTIONS_J2KOPTIONS | SAVE_SHOW_FILEOPTIONS_MULTIPAGE | SAVE_SHOW_FILEOPTIONS_QFACTOR); theApp.m_pRasterFileDlg->PutFileFormatsCount(2); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutFormat(FF_SAVE_MRC); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutBitsPerPixelCount(1); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutBitsPerPixelFormat(0, 24); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutSubFormat(0, FF_SAVE_SUB_LEAD_MRC); theApp.m_pRasterFileDlg->GetFileFormats(1)->PutFormat(FF_SAVE_TIFF); theApp.m_pRasterFileDlg->GetFileFormats(1)->PutBitsPerPixelCount(1); theApp.m_pRasterFileDlg->GetFileFormats(1)->PutBitsPerPixelFormat(0, 24); theApp.m_pRasterFileDlg->GetFileFormats(1)->PutSubFormat(0, FF_SAVE_SUB_TIFF24_LEAD_MRC); theApp.m_pRasterFileDlg->PutSaveMulti(MULTIPAGE_APPEND); theApp.m_pRasterFileDlg->PutDialogTitle("Save as LEAD MRC"); nRet = theApp.m_pRasterFileDlg->ShowSaveDlg((long)this->m_hWnd); if(nRet !=0) { theApp.DisplayLEADError(nRet); } else { if(theApp.m_pRasterFileDlg->GetDialogStatus()!= DLG_OK) { theApp.m_pRasterFileDlg->PutBitmap(0); return; } } theApp.m_pRasterFileDlg->PutBitmap(0); int nFotmat = theApp.m_pRasterFileDlg->GetSaveFormat(); int nPage = theApp.m_pRasterFileDlg->GetPageNumber(); pDoc->m_pRasterMrc->PutPictureQFactor(theApp.m_MrcOptions.nPictureQFactor); pDoc->m_pRasterMrc->PutMaskCoder((MRCMaskCoderConstants)theApp.m_MrcOptions.nMaskCoder); pDoc->m_pRasterMrc->PutPictureCoder((MRCPictureCoderConstants)theApp.m_MrcOptions.nPictureCoder); pDoc->m_pRasterMrc->PutText2BitCoder((MRCText2BitCoderConstants)theApp.m_MrcOptions.nTextCoder2Bit); pDoc->m_pRasterMrc->PutGrayscale8BitQFactor(theApp.m_MrcOptions.nGrayscale8BitFactor); pDoc->m_pRasterMrc->PutGrayscale2BitCoder((MRCGrayscale2BitCoderConstants)theApp.m_MrcOptions.nGrayscale2BitCoder); pDoc->m_pRasterMrc->PutGrayscale8BitCoder((MRCGrayscale8BitCoderConstants)theApp.m_MrcOptions.nGrayscale8BitCoder); // Check for manual segmentation if (m_bManual) { if (!ManualSegmentation()) return; } BeginWaitCursor(); nRet = pDoc->m_pRasterMrc->Save(m_RasterView.GetRaster(), theApp.m_pRasterFileDlg->GetFileName(), nFotmat, nPage, 0); if (nRet != 0) { theApp.DisplayLEADError(nRet); } EndWaitCursor(); } void CSegDemoView::OnFileSaveMrc() { int nRet; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); theApp.m_pRasterFileDlg->PutFilter("ALL|*.*|MRC|*.mrc"); theApp.m_pRasterFileDlg->PutBitmap (m_RasterView.GetRaster().GetBitmap()); theApp.m_pRasterFileDlg->PutEnableMethodErrors(FALSE); theApp.m_pRasterFileDlg->PutSubTypeIndex(0); theApp.m_pRasterFileDlg->PutSaveQFactor(2); theApp.m_pRasterFileDlg->PutFileDlgFlags(OFN_OVERWRITEPROMPT); theApp.m_pRasterFileDlg->PutUIFlags(SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS| SAVE_SHOW_FILEOPTIONS_J2KOPTIONS | SAVE_SHOW_FILEOPTIONS_MULTIPAGE | SAVE_SHOW_FILEOPTIONS_QFACTOR); theApp.m_pRasterFileDlg->PutFileFormatsCount(2); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutFormat(FF_SAVE_MRC); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutBitsPerPixelCount(1); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutBitsPerPixelFormat(0, 24); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutSubFormat(0, FF_SAVE_SUB_MRC); theApp.m_pRasterFileDlg->GetFileFormats(1)->PutFormat(FF_SAVE_TIFF); theApp.m_pRasterFileDlg->GetFileFormats(1)->PutBitsPerPixelCount(1); theApp.m_pRasterFileDlg->GetFileFormats(1)->PutBitsPerPixelFormat(0, 24); theApp.m_pRasterFileDlg->GetFileFormats(1)->PutSubFormat(0, FF_SAVE_SUB_TIFF24_MRC); theApp.m_pRasterFileDlg->PutSaveMulti(MULTIPAGE_APPEND); theApp.m_pRasterFileDlg->PutDialogTitle("Save as MRC"); nRet = theApp.m_pRasterFileDlg->ShowSaveDlg((long)this->m_hWnd); if(nRet !=0) { theApp.DisplayLEADError(nRet); } else { if(theApp.m_pRasterFileDlg->GetDialogStatus()!= DLG_OK) { theApp.m_pRasterFileDlg->PutBitmap(0); return; } } theApp.m_pRasterFileDlg->PutBitmap(0); int nFotmat = theApp.m_pRasterFileDlg->GetSaveFormat(); int nPage = theApp.m_pRasterFileDlg->GetPageNumber(); int nModify = theApp.m_pRasterFileDlg->GetSaveMulti(); pDoc->m_pRasterMrc->PutPictureQFactor(theApp.m_MrcOptions.nPictureQFactor); pDoc->m_pRasterMrc->PutMaskCoder((MRCMaskCoderConstants)theApp.m_MrcOptions.nMaskCoder); pDoc->m_pRasterMrc->PutPictureCoder((MRCPictureCoderConstants)theApp.m_MrcOptions.nPictureCoder); if (theApp.m_MrcOptions.nPictureCoder != MRC_PICTURE_COMPRESSION_JPEG) { MessageBox(TEXT("Invalid picture compression, it will be replaced with \"JPEG compression\"."), TEXT("Warning"), MB_OK); pDoc->m_pRasterMrc->PutPictureCoder((MRCPictureCoderConstants)MRC_PICTURE_COMPRESSION_JPEG); } pDoc->m_pRasterMrc->PutGrayscale8BitQFactor(theApp.m_MrcOptions.nGrayscale8BitFactor); pDoc->m_pRasterMrc->PutGrayscale2BitCoder((MRCGrayscale2BitCoderConstants)theApp.m_MrcOptions.nGrayscale2BitCoder); pDoc->m_pRasterMrc->PutGrayscale8BitCoder((MRCGrayscale8BitCoderConstants)theApp.m_MrcOptions.nGrayscale8BitCoder); BeginWaitCursor(); // Check for manual segmentation if (m_bManual) { if (!ManualSegmentation()) return; } nRet = pDoc->m_pRasterMrc->SaveT44(m_RasterView.GetRaster(), theApp.m_pRasterFileDlg->GetFileName(), nFotmat, nPage, nModify); if (nRet != 0) { theApp.DisplayLEADError(nRet); } EndWaitCursor(); } void CSegDemoView::OnFileExportSegments() { OPENFILENAME OpenFileName; TCHAR szOpenFileFilter[] = TEXT("Segments files\0")TEXT("*.sgm\0"); TCHAR szFileName[MAX_PATH]; int nRet = 0; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); memset(&OpenFileName, 0, sizeof(OPENFILENAME)); memset(szFileName, 0, MAX_PATH); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = this->GetSafeHwnd(); OpenFileName.lpstrFile = szFileName; OpenFileName.nMaxFile = MAX_PATH; OpenFileName.Flags = OFN_OVERWRITEPROMPT; OpenFileName.lpstrFilter = szOpenFileFilter; OpenFileName.lpstrTitle = TEXT("Export Segments"); OpenFileName.nFilterIndex = 0; if (!GetSaveFileName(&OpenFileName)) return; SetFileNameExt(OpenFileName.lpstrFile, TEXT(".sgm")); BeginWaitCursor(); nRet = pDoc->m_pRasterMrc->SaveSegmentation(OpenFileName.lpstrFile); EndWaitCursor(); if (nRet != 0) { MessageBox(TEXT("Error exporting segments."), TEXT("Error"), MB_OK); } } void CSegDemoView::OnUpdateFileExportsegments(CCmdUI* pCmdUI) { pCmdUI->Enable(m_SegInfo.GetSize()); } void CSegDemoView::SetFileNameExt(TCHAR * pszFileName, TCHAR * pszExt) { int nStrLen = lstrlen(pszFileName); TCHAR * pTemp = pszFileName + nStrLen - 4; if (_tcsicmp(pTemp, pszExt) != 0) lstrcat(pszFileName, pszExt); } void CSegDemoView::OnFileImportSegments() { OPENFILENAME OpenFileName; TCHAR szOpenFileFilter[] = TEXT("Segments files\0")TEXT("*.sgm\0"); TCHAR szFileName[MAX_PATH]; int nRet; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); memset(szFileName, 0, MAX_PATH); memset(&OpenFileName, 0, sizeof(OPENFILENAME)); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = this->GetSafeHwnd(); OpenFileName.lpstrFile = szFileName; OpenFileName.nMaxFile = MAX_PATH; OpenFileName.lpstrFilter = szOpenFileFilter; OpenFileName.lpstrCustomFilter = NULL; OpenFileName.nMaxCustFilter = 0; OpenFileName.nFilterIndex = 1; OpenFileName.lpstrFileTitle = NULL; OpenFileName.nMaxFileTitle = 0; OpenFileName.lpstrInitialDir = NULL; OpenFileName.lpstrTitle = TEXT("Import Segments Files"); OpenFileName.nFileOffset = 0; OpenFileName.nFileExtension = 0; OpenFileName.lpstrDefExt = NULL; OpenFileName.lpfnHook = NULL; OpenFileName.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; if (!GetOpenFileName(&OpenFileName)) return; ClearSegments(); pDoc->m_pRasterMrc->StopSegmentation(); BeginWaitCursor(); nRet = pDoc->m_pRasterMrc->LoadSegmentation(m_RasterView.GetRaster(), OpenFileName.lpstrFile); EndWaitCursor(); if (nRet != 0) { pDoc->m_pRasterMrc->StopSegmentation(); MessageBox(TEXT("Error importing segments."), TEXT("Error"), MB_OK); } // Enumerate current segments ClearSegments(); pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); } void CSegDemoView::OnAutoSegmentation() { CString szError; int nRet =0; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); ClearSegments(); // Free any existing regions m_RasterView.GetRaster().FreeRgn(); BeginWaitCursor(); nRet = pDoc->m_pRasterMrc->AutoSegmentExt(m_RasterView.GetRaster(), theApp.m_SegementOptions.uCleanSize, theApp.m_SegementOptions.uSegmentQuality, theApp.m_SegementOptions.uColorThreshold, theApp.m_SegementOptions.uBackgroundThreshold, theApp.m_SegementOptions.uCombineThreshold, theApp.m_SegementOptions.uFlags); if (nRet != 0) { szError.Format(TEXT("An error occurred while trying to segment the bitmap: %d"), nRet); MessageBox(szError, TEXT("Error"), MB_OK); return; } // Enumerate current segments pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); EndWaitCursor(); } void CSegDemoView::OnAutoManualSegmentation() { CString szError; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); int nRet; BeginWaitCursor(); if (!ManualSegmentation()) MessageBox(TEXT("Manual Segmentation wasn't completed successfully"), TEXT("Error"), MB_OK); m_RasterView.GetRaster().FreeRgn(); nRet = pDoc->m_pRasterMrc->SegmentExt(m_RasterView.GetRaster(), theApp.m_SegementOptions.uCleanSize, theApp.m_SegementOptions.uSegmentQuality, theApp.m_SegementOptions.uColorThreshold, theApp.m_SegementOptions.uBackgroundThreshold, theApp.m_SegementOptions.uCombineThreshold, theApp.m_SegementOptions.uFlags); if (nRet != 0) { szError.Format(TEXT("An error occurred while trying to segment the bitmap: %d"), nRet); MessageBox(szError, TEXT("Error"), MB_OK); // Enumerate current segments pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); return; } if (m_bManual) ClearSegments(); // Enumerate current segments pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); } BEGIN_EVENTSINK_MAP(CSegDemoView, CFormView) //{{AFX_EVENTSINK_MAP(CSegDemoView) ON_EVENT(CSegDemoView, IDC_LEADRASTERVIEW1, 8 /* Paint */, OnPaintLeadRasterView, VTS_I4) ON_EVENT(CSegDemoView, IDC_LEADRASTERVIEW1, 4 /* MouseDown */, OnMouseDownLeadRasterView1, VTS_I2 VTS_I2 VTS_R4 VTS_R4) ON_EVENT(CSegDemoView, IDC_LEADRASTERVIEW1, 5 /* MouseUp */, OnMouseUpLeadRasterView1, VTS_I2 VTS_I2 VTS_R4 VTS_R4) ON_EVENT(CSegDemoView, IDC_LEADRASTERVIEW1, 3 /* MouseMove */, OnMouseMoveLeadRasterView1, VTS_I2 VTS_I2 VTS_R4 VTS_R4) ON_EVENT(CSegDemoView, IDC_LEADRASTERVIEW1, 16 /* KeyDown */, OnKeyDownLeadRasterView1, VTS_I2 VTS_I2) //}}AFX_EVENTSINK_MAP END_EVENTSINK_MAP() void CSegDemoView::OnPaintLeadRasterView(long hDC) { RECT rc; CSegDemoView ::SEGMENTINFO SegInfo; CDC *pDC = CDC::FromHandle((HDC)hDC); int nSegCount = m_SegInfo.GetSize(); CBrush Brush, *pOldBrush; CPen SegmentPen(PS_SOLID, 1,RGB(255,0,0)), *pOldPen; Brush.Attach(GetStockObject(NULL_BRUSH)); pOldBrush = pDC->SelectObject(&Brush); pOldPen = pDC->SelectObject(&SegmentPen); if (nSegCount) { for(int nSegment = 0; nSegment < nSegCount; nSegment++) { SegInfo = m_SegInfo.GetAt(nSegment); rc = SegInfo.rcRect; ChangeToZoom(&rc); OffsetRect (&rc, -m_RasterView.GetHScrollInfo().GetPos() , -m_RasterView.GetVScrollInfo().GetPos()); pDC->Rectangle(rc.left, rc.top, rc.right, rc.bottom); if (!IsSegmentSelected(nSegment) && m_bShowSegmentType) { if (SegInfo.SegType == SEGTYPE_BACKGROUND) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_BACKGRND, TEXT_BACKGRND.GetLength()); if (SegInfo.SegType == SEGTYPE_ONE_COLORED) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_ONECOLOR, TEXT_ONECOLOR.GetLength()); if (SegInfo.SegType == SEGTYPE_TEXT_2BIT_BW) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_2BIT_BW, TEXT_2BIT_BW.GetLength()); if (SegInfo.SegType == SEGTYPE_TEXT_1BIT_BW) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_1BIT_BW, TEXT_1BIT_BW.GetLength()); else if (SegInfo.SegType == SEGTYPE_TEXT_1BIT_COLORED) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_1BIT_COLOR, TEXT_1BIT_COLOR.GetLength()); else if (SegInfo.SegType == SEGTYPE_TEXT_2BIT_COLORED) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_2BIT_COLOR, TEXT_2BIT_COLOR.GetLength()); else if (SegInfo.SegType == SEGTYPE_GRAYSCALE_2BIT) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_2BIT_GRAYSCALE, TEXT_2BIT_GRAYSCALE.GetLength()); else if (SegInfo.SegType == SEGTYPE_GRAYSCALE_8BIT) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_8BIT_GRAYSCALE, TEXT_2BIT_GRAYSCALE.GetLength()); else if (SegInfo.SegType == SEGTYPE_PICTURE) pDC->TextOut(rc.left + 5, rc.top + 5, TEXT_PICTURE, TEXT_PICTURE.GetLength()); } } } pDC->SelectObject(pOldBrush); pDC->SelectObject(pOldPen); Brush.Detach(); for (int i = 0; i < m_SegInfo.GetSize(); i++) { if(IsSegmentSelected(i)) { CopyRect(&rc, &m_SegInfo[i].rcRect); ChangeToZoom(&rc); DrawSelectedLines(pDC ,&rc); } } } void CSegDemoView::ChangeToZoom(RECT* pRect) { float fFactor = m_RasterView.GetPaintZoomFactor() / 100; pRect->left = (long)(pRect->left * fFactor + 0.5); pRect->top = (long)(pRect->top * fFactor + 0.5); pRect->right = (long)(pRect->right * fFactor + 0.5); pRect->bottom = (long)(pRect->bottom * fFactor + 0.5); } void CSegDemoView::OnEditCopy() { BeginWaitCursor(); m_RasterView.GetRaster().Copy(COPY_DIB | COPY_DDB | COPY_PALETTE); EndWaitCursor(); } void CSegDemoView::ClearSegments() { if(m_SegInfo.GetSize()) { m_SegInfo.RemoveAll(); } m_bManual = FALSE; m_bMouseMove = FALSE; m_bLBDown = FALSE; m_nLineSel = -1; m_bDrawSegment = FALSE; m_nSelSeg = -1; m_RasterView.ForceRepaint(); } void CSegDemoView::OnClearSegments() { ClearSegments(); } void CSegDemoView::OnUpdateClearSegments(CCmdUI* pCmdUI) { pCmdUI->Enable(m_SegInfo.GetSize()); } void CSegDemoView::OnDrawSegment() { m_bManual = TRUE; m_bDrawSegment = TRUE; } void CSegDemoView::OnUpdateDrawSegment(CCmdUI* pCmdUI) { pCmdUI->Enable(!m_bDrawSegment); pCmdUI->SetCheck(m_bDrawSegment ? MF_CHECKED : MF_UNCHECKED); } void CSegDemoView::OnDrawCancelRect() { m_bManual = FALSE; m_bDrawSegment = FALSE; } void CSegDemoView::OnUpdateDrawCancelrect(CCmdUI* pCmdUI) { pCmdUI->Enable(m_bDrawSegment); pCmdUI->SetCheck(!m_bDrawSegment ? MF_CHECKED : MF_UNCHECKED); } void CSegDemoView::OnShowSegmentType() { m_bShowSegmentType = !m_bShowSegmentType; m_RasterView.ForceRepaint(); } void CSegDemoView::OnUpdateShowSegmentType(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_bShowSegmentType); } void CSegDemoView::OnOptions() { CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); COptionsSheet OptionsSheet(TEXT("Segmentation and Compression Options")); OptionsSheet.SetMRCPageOptions(&theApp.m_MrcOptions); OptionsSheet.SetPDFPageOptions(&theApp.m_PdfOptions); OptionsSheet.SetSegmentationPageOptions(&theApp.m_SegementOptions); OptionsSheet.SetCombinePageOptions(&theApp.m_CombineOptions); OptionsSheet.SetColorsPageOptions(&theApp.m_ColorsOptions); if(OptionsSheet.DoModal() == IDOK) { pDoc->m_pRasterMrc->PutBaseBackColor(theApp.m_ColorsOptions.crBackground); pDoc->m_pRasterMrc->PutBaseForeColor(theApp.m_ColorsOptions.crForeground); } } void CSegDemoView::OnColorRes() { int nRet = 0; theApp.m_pRasterClrDlg->PutBitmap(m_RasterView.GetRaster().GetBitmap()); theApp.m_pRasterClrDlg->PutBitsPerPixel(m_RasterView.GetRaster().GetBitmapBits()); theApp.m_pRasterClrDlg->PutUIFlags(COLORRES_AUTOPROCESS | COLORRES_SHOW_PREVIEW| COLORRES_SHOW_ORDER | COLORRES_SHOW_TOOL_ZOOMLEVEL | COLORRES_SHOW_OPENPALFILE) ; theApp.m_pRasterClrDlg->PutClrResFlags(COLORRES_SHOW_BITALL | COLORRES_SHOW_DITHER_ALL | COLORRES_SHOW_PAL_ALL) ; nRet = theApp.m_pRasterClrDlg->ShowColorResDlg((long)this->GetSafeHwnd()); if (nRet != 0) { MessageBox(TEXT("Error changing the color resolution"), TEXT("ERROR"), MB_OK); return; } theApp.m_pRasterClrDlg->PutBitmap(0); } void CSegDemoView::OnColorHistogram() { theApp.m_pRasterImgDlg->PutBitmap(m_RasterView.GetRaster().GetBitmap()); theApp.m_pRasterImgDlg->PutBlueChannelPenColor(RGB (0, 0, 255)); theApp.m_pRasterImgDlg->PutGreenChannelPenColor(RGB (0, 255, 0)); theApp.m_pRasterImgDlg->PutRedChannelPenColor(RGB (255, 0, 0)); theApp.m_pRasterImgDlg->PutMasterChannelPenColor(RGB (0, 0, 0)); theApp.m_pRasterImgDlg->PutUIFlags(HISTOGRAM_SHOW_VIEWSTYLE | HISTOGRAM_USERPENCOLORS); theApp.m_pRasterImgDlg->ShowHistogramDlg((long)this->GetSafeHwnd()); theApp.m_pRasterImgDlg->PutBitmap(0); } void CSegDemoView::OnColorUinqueColors() { CString strColor; unsigned int uCount; uCount = theApp.m_pRasterProc->GetColorCount(m_RasterView.GetRaster()); strColor.Format(TEXT("Bitmap contains %lu unique colors."), uCount); MessageBox(strColor, TEXT("Unique Colors"), MB_OK); } void CSegDemoView::OnMouseDownLeadRasterView1(short Button, short Shift, float x, float y) { POINT pt; float fFactor; short uCtrlPressed; RECT rcSeg; if(Button == LEFT_BUTTON) { ShowCursor(); if ((x >= -3) && (x < m_RasterView.GetDstWidth() + 3) && (y >= -3) && (y < m_RasterView.GetDstHeight() + 3)) m_bLBDown = TRUE; ShowCursor(); m_bUpdateSegment = FALSE; fFactor = m_RasterView.GetPaintZoomFactor() / 100.0f; m_ptClickMove.x = (long)(x / fFactor); m_ptClickMove.y = (long)(y / fFactor); // Are we drawing a segment? if (m_bDrawSegment) { m_bManual = TRUE; m_RasterView.SetCapture(); m_ptManualStart.x = (long)x ; m_ptManualStart.y = (long)y; m_ptManualEnd.x = (long)x; m_ptManualEnd.y = (long)y; m_RasterView.SetRgnMarkingMode (RGNMARK_RECT); if (FindSelectionCount()) UnSelectCurrentSegment((long)x, (long)y); return; } // Find the segment that will be selected if (!m_bMouseMove) { m_nSelSeg = -1; SelectSegments((long)x, (long)y); } else { // Check if the Ctrl pressed uCtrlPressed = GetAsyncKeyState(VK_CONTROL); if (uCtrlPressed & 0x8000) { if (FindSelectionCount() < 1) return; UnSelectCurrentSegment((long)x, (long)y); return; } if (FindSelectionCount() != 1) return; m_RasterView.SetCapture(); pt.x = (long)(x + m_RasterView.GetHScrollInfo().GetPos()); pt.y = (long)(y + m_RasterView.GetVScrollInfo().GetPos()); m_nLineSel = RESIZE_NOTHING; CopyRect(&rcSeg, &m_SegInfo[m_nSelSeg].rcRect); // Find the selection line FindSelectionLine(&rcSeg, pt); } } } void CSegDemoView::DrawSelectedLines(CDC *pDC,RECT* pRect) { HDC hDC = pDC->GetSafeHdc(); int x1, y1, x2, y2; // Draw the selection rectangles x1 = (pRect->right + pRect->left) / 2 - 3 -m_RasterView.GetHScrollInfo().GetPos(); y1 = pRect->top - 3 -m_RasterView.GetVScrollInfo().GetPos(); x2 = (pRect->right + pRect->left) / 2 + 3 -m_RasterView.GetHScrollInfo().GetPos(); y2 = pRect->top + 3 -m_RasterView.GetVScrollInfo().GetPos(); Rectangle(hDC, x1, y1, x2, y2); x1 = (pRect->right + pRect->left) / 2 - 3 -m_RasterView.GetHScrollInfo().GetPos(); y1 = pRect->bottom - 3 -m_RasterView.GetVScrollInfo().GetPos(); x2 = (pRect->right + pRect->left) / 2 + 3 -m_RasterView.GetHScrollInfo().GetPos(); y2 = pRect->bottom + 3 -m_RasterView.GetVScrollInfo().GetPos(); Rectangle(hDC, x1, y1, x2, y2); x1 = pRect->left - 3 -m_RasterView.GetHScrollInfo().GetPos(); y1 = (pRect->top + pRect->bottom) / 2 - 3 -m_RasterView.GetVScrollInfo().GetPos(); x2 = pRect->left + 3 -m_RasterView.GetHScrollInfo().GetPos(); y2 = (pRect->top + pRect->bottom) / 2 + 3 -m_RasterView.GetVScrollInfo().GetPos(); Rectangle(hDC, x1, y1, x2, y2); x1 = pRect->right - 3 -m_RasterView.GetHScrollInfo().GetPos(); y1 = (pRect->top + pRect->bottom) / 2 - 3 -m_RasterView.GetVScrollInfo().GetPos(); x2 = pRect->right + 3 -m_RasterView.GetHScrollInfo().GetPos(); y2 = (pRect->top + pRect->bottom) / 2 + 3 -m_RasterView.GetVScrollInfo().GetPos(); Rectangle(hDC, x1, y1, x2, y2); x1 = pRect->left - 3 -m_RasterView.GetHScrollInfo().GetPos(); y1 = pRect->top - 3 -m_RasterView.GetVScrollInfo().GetPos(); x2 = pRect->left + 3 -m_RasterView.GetHScrollInfo().GetPos(); y2 = pRect->top + 3 -m_RasterView.GetVScrollInfo().GetPos(); Rectangle(hDC, x1, y1, x2, y2); x1 = pRect->right - 3 -m_RasterView.GetHScrollInfo().GetPos(); y1 = pRect->top - 3 -m_RasterView.GetVScrollInfo().GetPos(); x2 = pRect->right + 3 -m_RasterView.GetHScrollInfo().GetPos(); y2 = pRect->top + 3 -m_RasterView.GetVScrollInfo().GetPos(); Rectangle(hDC, x1, y1, x2, y2); x1 = pRect->left - 3 -m_RasterView.GetHScrollInfo().GetPos(); y1 = pRect->bottom - 3 -m_RasterView.GetVScrollInfo().GetPos(); x2 = pRect->left + 3 -m_RasterView.GetHScrollInfo().GetPos(); y2 = pRect->bottom + 3 -m_RasterView.GetVScrollInfo().GetPos(); Rectangle(hDC, x1, y1, x2, y2); x1 = pRect->right - 3 -m_RasterView.GetHScrollInfo().GetPos(); y1 = pRect->bottom - 3 -m_RasterView.GetVScrollInfo().GetPos(); x2 = pRect->right + 3 -m_RasterView.GetHScrollInfo().GetPos(); y2 = pRect->bottom + 3 -m_RasterView.GetVScrollInfo().GetPos(); Rectangle(hDC, x1, y1, x2, y2); } void CSegDemoView::OnMouseUpLeadRasterView1(short Button, short Shift, float x, float y) { POINT pt; if (GetCapture() == this) ReleaseCapture(); pt.x = (long)x; pt.y = (long)y; if (m_RasterView.GetCapture()->GetSafeHwnd() == (HWND)m_RasterView.GetWindow()) ReleaseCapture(); if(Button == RIGHT_BUTTON && m_nSelSeg >= 0) { CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd; CMDIChildWnd *pChild = (CMDIChildWnd *) pFrame->GetActiveFrame(); CSegDemoView *pView = (CSegDemoView *) pChild->GetActiveView(); if (pView != this) return; int i; RECT rcSelected; CMenu ContextMenu; CMenu* pMenu = NULL; if (FindSelectionCount() > 1) { for (i = 0; i < m_SegInfo.GetSize(); i++) { if(m_SegInfo[i].bSelected) { POINT ptCombine; CopyRect(&rcSelected, &m_SegInfo[i].rcRect); ChangeToZoom(&rcSelected); ptCombine.x = (long)(x + m_RasterView.GetHScrollInfo().GetPos()); ptCombine.y = (long)(y + m_RasterView.GetVScrollInfo().GetPos()); if (PtInRect(&rcSelected, ptCombine)) { if (ContextMenu.LoadMenu (IDR_COMBINE)) { if (pMenu = ContextMenu.GetSubMenu (0)) { /* Display the pop-up menu */ ClientToScreen(&pt); pMenu->TrackPopupMenu (TPM_LEFTALIGN, pt.x, pt.y, this); } } } } } } else /* Load the context menu. */ if (ContextMenu.LoadMenu (IDR_CONTEXT_MENU)) { /* Retrieves a pointer to a pop-up menu. */ if (pMenu = ContextMenu.GetSubMenu (0)) { /* Display the pop-up menu */ ClientToScreen(&pt); pMenu->TrackPopupMenu (TPM_LEFTALIGN, pt.x, pt.y, this); } } return; } if(Button == LEFT_BUTTON) { if (!m_bLBDown) return; m_bLBDown = FALSE; LButtonUp((long)x, (long)y); } } void CSegDemoView::LButtonUp(int x, int y) { CSegmentTypeDlg SegmentTypeDlg; /*Are we drawing a Segment ? */ if (m_bDrawSegment && m_bManual) { SEGMENTINFO SegInfo; float fZoomFactorX; float fZoomFactorY; float fNewX; float fNewY; float fNewWidth; float fNewHeight; // Remove Selected Segments for(int i =0; i < m_SegInfo.GetSize(); ++i) m_SegInfo[i].bSelected = FALSE; fZoomFactorX = m_RasterView.GetDstWidth()/ m_RasterView.GetSrcWidth(); fZoomFactorY = m_RasterView.GetDstHeight()/ m_RasterView.GetSrcHeight(); fNewY = ((m_RasterView.GetRaster().GetRgnTop() - m_RasterView.GetSrcTop()) * fZoomFactorY) /*+ m_RasterView.GetDstTop() */; fNewX = ((m_RasterView.GetRaster().GetRgnLeft() - m_RasterView.GetSrcLeft()) * fZoomFactorX) /*+ m_RasterView.GetDstLeft() */; fNewWidth = m_RasterView.GetRaster().GetRgnWidth() * fZoomFactorX; fNewHeight = m_RasterView.GetRaster().GetRgnHeight() * fZoomFactorY; SetRect(&SegInfo.rcRect, (int)fNewX, (int)fNewY, (int)(fNewX + fNewWidth), (int)(fNewY + fNewHeight)); m_RasterView.GetRaster().FreeRgn(); ChangeToImage(&SegInfo.rcRect); if (((SegInfo.rcRect.right - SegInfo.rcRect.left) < 2) || ((SegInfo.rcRect.bottom - SegInfo.rcRect.top) < 2)) return; // Set the active segment SegInfo.bSelected = TRUE; // Set segment type SegInfo.SegType = SEGTYPE_TEXT_1BIT_BW; SegmentTypeDlg.m_pSegInfo = &SegInfo; if(SegmentTypeDlg.DoModal() == IDOK) { m_nSelSeg = m_SegInfo.GetSize(); m_SegInfo.Add(SegInfo); ManualSegmentation(); m_bLBDown = FALSE; } m_RasterView.SetRgnMarkingMode (RGNMARK_NONE); } /* Did Auto/Manual segmentation rectangles change? */ if (m_bMouseMove && m_bUpdateSegment && (FindSelectionCount() == 1)) { UpdateSegments(); } // End the moveing m_bUpdateSegment = TRUE; m_bMouseMove = FALSE; m_RasterView.ForceRepaint(); } void CSegDemoView::OnMouseMoveLeadRasterView1(short Button, short Shift, float x, float y) { int x1 ,y1, x2, y2, nActX, nActY; RECT rc; RECT Rect, *pRect; float fFactor; BOOL bEnum = FALSE; if (!m_bLBDown) return; // Are we resizing a segment? if (FindSelectionCount() != 1) return; if ((m_bMouseMove) && (FindSelectionCount() > 0)) { if (x >= m_RasterView.GetDstWidth()) x = m_RasterView.GetDstWidth(); if (y >= m_RasterView.GetDstHeight()) y = m_RasterView.GetDstHeight(); if (x <= 0) x = 0; if (y <= 0) y = 0; fFactor = (float)(m_RasterView.GetPaintZoomFactor() / 100.0f); x = x + m_RasterView.GetHScrollInfo().GetPos(); y = y + m_RasterView.GetVScrollInfo().GetPos(); nActX = (int)min((int)(ceil((float)x / fFactor)), m_RasterView.GetRaster().GetBitmapWidth()); nActY = (int)min((int)(ceil((float)y / fFactor)), m_RasterView.GetRaster().GetBitmapHeight()); // Find the segment rect CopyRect(&Rect, &m_SegInfo[m_nSelSeg].rcRect); ChangeToZoom(&Rect); m_bUpdateSegment = TRUE; switch(m_nLineSel) { case RESIZE_TOP: // top if (y > Rect.bottom) { bEnum = TRUE; break; } x1 = Rect.left; y1 = (int)min(Rect.top, y); x2 = Rect.right; y2 = Rect.bottom; SetRect(&rc, x1, y1, x2, y2); m_SegInfo[m_nSelSeg].rcRect.top = nActY; break; case RESIZE_BOTTOM: // down if (y < Rect.top) { bEnum = TRUE; break; } x1 = Rect.left; y1 = Rect.top; x2 = Rect.right; y2 = (int)max(Rect.bottom , y); SetRect(&rc, x1, y1, x2, y2); m_SegInfo[m_nSelSeg].rcRect.bottom = nActY; break; case RESIZE_LEFT: // left if (x > Rect.right) { bEnum = TRUE; break; } x1 = (int)min(Rect.left, x); y1 = Rect.top; x2 = Rect.right; y2 = Rect.bottom; SetRect(&rc, x1, y1, x2, y2); m_SegInfo[m_nSelSeg].rcRect.left = nActX; break; case RESIZE_RIGHT: // right if (x < Rect.left) { bEnum = TRUE; break; } x1 = Rect.left; y1 = Rect.top; x2 = (int)max(Rect.right, x); y2 = Rect.bottom; SetRect(&rc, x1, y1, x2, y2); m_SegInfo[m_nSelSeg].rcRect.right = nActX; break; case RESIZE_LEFT_TOP: // left - top if ((x > Rect.right) || (y > Rect.bottom)) { bEnum = TRUE; break; } x1 = (int)min(Rect.left, x); y1 = (int)min(Rect.top, y); x2 = Rect.right; y2 = Rect.bottom; SetRect(&rc, x1, y1, x2, y2); m_SegInfo[m_nSelSeg].rcRect.left = nActX; m_SegInfo[m_nSelSeg].rcRect.top = nActY; break; case RESIZE_RIGHT_TOP: // right - top if ((x < Rect.left) || (y > Rect.bottom)) { bEnum = TRUE; break; } x1 = Rect.left; y1 = (int)min(Rect.top, y); x2 = (int)max(Rect.right, x); y2 = Rect.bottom; SetRect(&rc, x1, y1, x2, y2); m_SegInfo[m_nSelSeg].rcRect.right = nActX; m_SegInfo[m_nSelSeg].rcRect.top = nActY; break; case RESIZE_LEFT_BOTTOM: // left - bottom if ((x > Rect.right) || (y < Rect.top)) { bEnum = TRUE; break; } x1 = (int)min(Rect.left, x); y1 = Rect.top; x2 = Rect.right; y2 = (int)max(Rect.bottom, y); SetRect(&rc, x1, y1, x2, y2); m_SegInfo[m_nSelSeg].rcRect.left = nActX; m_SegInfo[m_nSelSeg].rcRect.bottom = nActY; break; case RESIZE_RIGHT_BOTTOM: // right - bottom if ((x < Rect.left) || (y < Rect.top)) { bEnum = TRUE; break; } x1 = Rect.left; y1 = Rect.top; x2 = (int)max(Rect.right, x); y2 = (int)max(Rect.bottom, y); SetRect(&rc, x1, y1, x2, y2); m_SegInfo[m_nSelSeg].rcRect.right = nActX; m_SegInfo[m_nSelSeg].rcRect.bottom = nActY; break; case RESIZE_SEGMENT: CopyRect(&rc, &m_SegInfo[m_nSelSeg].rcRect); x -= m_RasterView.GetHScrollInfo().GetPos(); y -= m_RasterView.GetVScrollInfo().GetPos(); nActX = (int)(x / fFactor); nActY = (int)(y / fFactor); rc.right -= (m_ptClickMove.x - nActX); rc.left -= (m_ptClickMove.x - nActX); rc.bottom -= (m_ptClickMove.y - nActY); rc.top -= (m_ptClickMove.y - nActY); if ((rc.right > m_RasterView.GetRaster().GetBitmapWidth()) || (rc.bottom > m_RasterView.GetRaster().GetBitmapHeight()) || (rc.left < 0) || (rc.top < 0)) { if (rc.right > m_RasterView.GetRaster().GetBitmapWidth()) { rc.left -= (long)(rc.right - m_RasterView.GetRaster().GetBitmapWidth()); rc.right = (long)m_RasterView.GetRaster().GetBitmapWidth(); } else if (rc.left < 0) { rc.right -= rc.left; rc.left = 0; } if (rc.bottom > m_RasterView.GetRaster().GetBitmapHeight()) { rc.top -= (long)(rc.bottom - m_RasterView.GetRaster().GetBitmapHeight()); rc.bottom = (long)m_RasterView.GetRaster().GetBitmapHeight(); } else if (rc.top < 0) { rc.bottom -= rc.top; rc.top = 0; } } m_ptClickMove.x = nActX; m_ptClickMove.y = nActY; CopyRect(&m_SegInfo[m_nSelSeg].rcRect, &rc); break; default: m_bUpdateSegment = FALSE; return; } pRect = &m_SegInfo[m_nSelSeg].rcRect; CheckRectCoordinates(m_nLineSel, pRect, m_bManual, m_bManual ? &m_SegInfo[m_nSelSeg].rcRect : NULL, 0, (long)m_RasterView.GetRaster().GetBitmapHeight(), &Rect); m_RasterView.ForceRepaint(); } } void CSegDemoView :: FindSelectionLine(RECT* pRect, POINT pt) { RECT rcSelected; int x1, x2, y1, y2; BOOL bFind = FALSE; // Find the selection rect line // top x1 = (pRect->right + pRect->left) / 2; y1 = pRect->top; x2 = (pRect->right + pRect->left) / 2; y2 = pRect->top; SetRect(&rcSelected, x1, y1, x2, y2); ChangeToZoom(&rcSelected); InflateRect(&rcSelected, 3, 3); if (PtInRect(&rcSelected, pt)) { m_nLineSel = RESIZE_TOP; bFind = TRUE; } // bottom x1 = (pRect->right + pRect->left) / 2; y1 = pRect->bottom; x2 = (pRect->right + pRect->left) / 2; y2 = pRect->bottom; SetRect(&rcSelected, x1, y1, x2, y2); ChangeToZoom(&rcSelected); InflateRect(&rcSelected, 3, 3); if (PtInRect(&rcSelected, pt)) { m_nLineSel = RESIZE_BOTTOM; bFind = TRUE; } // left x1 = pRect->left; y1 = (pRect->top + pRect->bottom) / 2; x2 = pRect->left; y2 = (pRect->top + pRect->bottom) / 2; SetRect(&rcSelected, x1, y1, x2, y2); ChangeToZoom(&rcSelected); InflateRect(&rcSelected, 3, 3); if (PtInRect(&rcSelected, pt)) { m_nLineSel = RESIZE_LEFT; bFind = TRUE; } // right x1 = pRect->right; y1 = (pRect->top + pRect->bottom) / 2; x2 = pRect->right; y2 = (pRect->top + pRect->bottom) / 2; SetRect(&rcSelected, x1, y1, x2, y2); ChangeToZoom(&rcSelected); InflateRect(&rcSelected, 3, 3); if (PtInRect(&rcSelected, pt)) { m_nLineSel = RESIZE_RIGHT; bFind = TRUE; } // Left - Top x1 = pRect->left; y1 = pRect->top; x2 = pRect->left; y2 = pRect->top; SetRect(&rcSelected, x1, y1, x2, y2); ChangeToZoom(&rcSelected); InflateRect(&rcSelected, 3, 3); if (PtInRect(&rcSelected, pt)) { m_nLineSel = RESIZE_LEFT_TOP; bFind = TRUE; } // Right - Top x1 = pRect->right; y1 = pRect->top; x2 = pRect->right; y2 = pRect->top; SetRect(&rcSelected, x1, y1, x2, y2); ChangeToZoom(&rcSelected); InflateRect(&rcSelected, 3, 3); if (PtInRect(&rcSelected, pt)) { m_nLineSel = RESIZE_RIGHT_TOP; bFind = TRUE; } // Left - Bottom x1 = pRect->left; y1 = pRect->bottom; x2 = pRect->left; y2 = pRect->bottom; SetRect(&rcSelected, x1, y1, x2, y2); ChangeToZoom(&rcSelected); InflateRect(&rcSelected, 3, 3); if (PtInRect(&rcSelected, pt)) { m_nLineSel = RESIZE_LEFT_BOTTOM; bFind = TRUE; } // Right - bottom x1 = pRect->right; y1 = pRect->bottom; x2 = pRect->right; y2 = pRect->bottom; SetRect(&rcSelected, x1, y1, x2, y2); ChangeToZoom(&rcSelected); InflateRect(&rcSelected, 3, 3); if (PtInRect(&rcSelected, pt)) { m_nLineSel = RESIZE_RIGHT_BOTTOM; bFind = TRUE; } if (!bFind) { ChangeToZoom(pRect); if (PtInRect(pRect, pt)) m_nLineSel = RESIZE_SEGMENT; } } void CSegDemoView :: ChangeToImage(RECT* pRect) { float fFactor; fFactor = m_RasterView.GetPaintZoomFactor() / 100.0f; pRect->left = (long)(pRect->left / fFactor + 0.5); pRect->top = (long)(pRect->top / fFactor + 0.5); pRect->right = (long)(pRect->right / fFactor + 0.5); pRect->bottom = (long)(pRect->bottom / fFactor + 0.5); } void CSegDemoView :: CheckRectCoordinates(int nLineSel, RECT* pRect, BOOL bManual, RECT* pManualSeg, int nStartRow, int nEndRow, RECT* pOldManualRect) { // We do not want segments less than 10X10 switch(nLineSel) { case RESIZE_TOP: // Top if ((pRect->bottom - pRect->top) < 10) pRect->top = pRect->bottom - 10; break; case RESIZE_BOTTOM:// Down if ((pRect->bottom - pRect->top) < 10) pRect->bottom = pRect->top + 10; break; case RESIZE_LEFT: // left if ((pRect->right - pRect->left) < 10) pRect->left = pRect->right - 10; break; case RESIZE_RIGHT: // right if ((pRect->right - pRect->left) < 10) pRect->right = pRect->left + 10; break; case RESIZE_LEFT_TOP: // left - top if ((pRect->right - pRect->left) < 10) pRect->left = pRect->right - 10; if ((pRect->bottom - pRect->top) < 10) pRect->top = pRect->bottom - 10; case RESIZE_RIGHT_TOP: // right - top if ((pRect->bottom - pRect->top) < 10) pRect->top = pRect->bottom - 10; if ((pRect->right - pRect->left) < 10) pRect->right = pRect->left + 10; break; case RESIZE_LEFT_BOTTOM: // left - bottom if ((pRect->right - pRect->left) < 10) pRect->left = pRect->right - 10; if ((pRect->bottom - pRect->top) < 10) pRect->bottom = pRect->top + 10; break; case RESIZE_RIGHT_BOTTOM: // right - bottom if ((pRect->right - pRect->left) < 10) pRect->right = pRect->left + 10; if ((pRect->bottom - pRect->top) < 10) pRect->bottom = pRect->top + 10; break; } // We are in manual mode? if (bManual) { if ((pRect->top < nStartRow) || (pRect->bottom > nEndRow)) { if (pManualSeg->top < nStartRow) { if (nLineSel == RESIZE_SEGMENT) pManualSeg->bottom += (nStartRow - pManualSeg->top); pManualSeg->top = nStartRow; } else if (pManualSeg->bottom > nEndRow) { if (nLineSel == RESIZE_SEGMENT) pManualSeg->top -= (pManualSeg->bottom - nEndRow); pManualSeg->bottom = nEndRow; } pManualSeg->right = pOldManualRect->right; pManualSeg->left = pOldManualRect->left; } } } void CSegDemoView::OnSegmenttType() { CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); CSegmentTypeDlg SegmentTypeDlg; if(m_nSelSeg != -1) { SegmentTypeDlg.m_pSegInfo = &m_SegInfo[m_nSelSeg]; if(SegmentTypeDlg.DoModal() == IDOK) { int nRet = pDoc->m_pRasterMrc->SetSegmentData(m_RasterView.GetRaster(), m_SegInfo[m_nSelSeg].nSegId, m_SegInfo[m_nSelSeg].rcRect.left, m_SegInfo[m_nSelSeg].rcRect.top, m_SegInfo[m_nSelSeg].rcRect.right, m_SegInfo[m_nSelSeg].rcRect.bottom, m_SegInfo[m_nSelSeg].SegType); if (nRet != 0) { MessageBox(TEXT("Could not update segment."), TEXT("Error"), MB_OK); return; } // Enumerate current segments ClearSegments(); pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); } } } void CSegDemoView::OnShowSegInfo() { CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); CSegmentInfoDlg SegmentInfoDlg; CSegDemoView::SEGMENTINFO SegInfo; if(m_nSelSeg != -1) { SegInfo = m_SegInfo[m_nSelSeg]; SegmentInfoDlg.m_pSegInfo = &SegInfo; if(SegmentInfoDlg.DoModal() == IDOK) { int nStart = 0; int nEnd = (int)m_RasterView.GetRaster().GetBitmapWidth(); int nTop = SegInfo.rcRect.top; int nLeft = SegInfo.rcRect.left; int nBottom = SegInfo.rcRect.bottom; int nRight = SegInfo.rcRect.right; if (nLeft < 0) { MessageBox(TEXT("Invalid x-coordinate of the rectangle's upper-left corner"), TEXT("Error"), MB_OK); return; } if (m_bManual) { if (nTop < nStart) nTop = nStart; } if (nTop < 0) { MessageBox(TEXT("Invalid y-coordinate of the rectangle's upper-left corner"), TEXT("Error"), MB_OK); return; } if (nRight > nEnd) { MessageBox(TEXT("Invalid x-coordinate of the rectangle's lower-right corner"), TEXT("Error"), MB_OK); return; } if (m_bManual) { if (nBottom > nEnd) nBottom = nEnd; } if (nBottom > m_RasterView.GetRaster().GetBitmapHeight()) { MessageBox(TEXT("Invalid y-coordinate of the rectangle's lower-right corner."), TEXT("Error"), MB_OK); return; } if ((nRight - nLeft) < 2) { MessageBox(TEXT("Invalid rectangle width."), TEXT("Error"), MB_OK); return; } if ((nBottom - nTop) < 2) { MessageBox(TEXT("Invalid rectangle height."), TEXT("Error"), MB_OK); return; } SegInfo.rcRect.left = nLeft; SegInfo.rcRect.top = nTop; SegInfo.rcRect.right = nRight; SegInfo.rcRect.bottom = nBottom; NormalizeRect(&SegInfo.rcRect); int nRet = pDoc->m_pRasterMrc->SetSegmentData(m_RasterView.GetRaster(), SegInfo.nSegId, SegInfo.rcRect.left, SegInfo.rcRect.top, SegInfo.rcRect.right, SegInfo.rcRect.bottom, SegInfo.SegType); if (nRet != 0) { MessageBox(TEXT("Could not update segment."), TEXT("Error"), MB_OK); return; } // Enumerate current segments ClearSegments(); pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); } } } void CSegDemoView::NormalizeRect(RECT* pRect) { int nTemp; if (pRect->left > pRect->right) { nTemp = pRect->left; pRect->left = pRect->right; pRect->right = nTemp; } if (pRect->top > pRect->bottom) { nTemp = pRect->top; pRect->top = pRect->bottom; pRect->bottom = nTemp; } } void CSegDemoView::OnSegmentHistogram() { if(m_nSelSeg != -1) { ILEADRaster *pRaster = NULL; CoCreateInstance( CLSID_LEADRaster, NULL, CLSCTX_ALL, IID_ILEADRaster, (void**)&pRaster ); if(!pRaster) return; int nWidth = abs(m_SegInfo[m_nSelSeg].rcRect.right - m_SegInfo[m_nSelSeg].rcRect.left); int nHeight = abs(m_SegInfo[m_nSelSeg].rcRect.bottom - m_SegInfo[m_nSelSeg].rcRect.top); pRaster->CreateBitmap((float)nWidth, (float)nHeight, m_RasterView.GetRaster().GetBitmapBits()); theApp.m_pRasterProc->CombineExt(pRaster, 0, 0, (float)nWidth, (float)nHeight, m_RasterView.GetRaster(), (float)m_SegInfo[m_nSelSeg].rcRect.left, (float)m_SegInfo[m_nSelSeg].rcRect.top, CB_OP_ADD); theApp.m_pRasterImgDlg->PutBitmap(pRaster->GetBitmap()); theApp.m_pRasterImgDlg->PutBlueChannelPenColor(RGB (0, 0, 255)); theApp.m_pRasterImgDlg->PutGreenChannelPenColor(RGB (0, 255, 0)); theApp.m_pRasterImgDlg->PutRedChannelPenColor(RGB (255, 0, 0)); theApp.m_pRasterImgDlg->PutMasterChannelPenColor(RGB (0, 0, 0)); theApp.m_pRasterImgDlg->PutUIFlags(HISTOGRAM_SHOW_VIEWSTYLE | HISTOGRAM_USERPENCOLORS); int nRet = theApp.m_pRasterImgDlg->ShowHistogramDlg((long)this->GetSafeHwnd()); pRaster->Release(); } } void CSegDemoView::OnSegmentUniqueColors() { CString strColor; unsigned int uCount; ILEADRaster *pRaster = NULL; CoCreateInstance( CLSID_LEADRaster, NULL, CLSCTX_ALL, IID_ILEADRaster, (void**)&pRaster ); if(!pRaster) return; int nWidth = abs(m_SegInfo[m_nSelSeg].rcRect.right - m_SegInfo[m_nSelSeg].rcRect.left); int nHeight = abs(m_SegInfo[m_nSelSeg].rcRect.bottom - m_SegInfo[m_nSelSeg].rcRect.top); pRaster->CreateBitmap((float)nWidth, (float)nHeight, m_RasterView.GetRaster().GetBitmapBits()); theApp.m_pRasterProc->CombineExt(pRaster, 0, 0, (float)nWidth, (float)nHeight, m_RasterView.GetRaster(), (float)m_SegInfo[m_nSelSeg].rcRect.left, (float)m_SegInfo[m_nSelSeg].rcRect.top, CB_OP_ADD); uCount = theApp.m_pRasterProc->GetColorCount(pRaster); strColor.Format(TEXT("Bitmap contains %lu unique colors."), uCount); MessageBox(strColor, TEXT("Unique Colors"), MB_OK); pRaster->Release(); } void CSegDemoView::OnZoomingNormal() { m_bFitToWindow = FALSE; m_nZoomPercent = 100; m_RasterView.SetPaintSizeMode(PAINTSIZEMODE_NORMAL); SetZoom(m_nZoomPercent); } void CSegDemoView::OnUpdateZoomingNormal(CCmdUI* pCmdUI) { pCmdUI->Enable(m_RasterView.GetRaster().GetBitmap() != 0); pCmdUI->SetCheck(!m_bFitToWindow && (m_nZoomPercent == 100)); } void CSegDemoView::SetZoom(int percent) { m_nZoomPercent = percent; if (m_RasterView.GetRaster().GetBitmap()) { m_RasterView.SetPaintZoomFactor((float)m_nZoomPercent); } } void CSegDemoView::OnFittowindow() { m_bFitToWindow = TRUE; m_RasterView.SetPaintSizeMode(PAINTSIZEMODE_FIT); } void CSegDemoView::OnUpdateFittowindow(CCmdUI* pCmdUI) { pCmdUI->Enable(m_RasterView.GetRaster().GetBitmap() != 0); pCmdUI->SetCheck(m_bFitToWindow); } void CSegDemoView::OnZommingIn() { m_bFitToWindow = FALSE; m_nZoomPercent += 10; if (m_nZoomPercent > 500) { m_nZoomPercent = 500; } SetZoom(m_nZoomPercent); } void CSegDemoView::OnZommingOut() { m_bFitToWindow = FALSE; m_nZoomPercent -= 10; if (m_nZoomPercent < 10) { m_nZoomPercent = 10; } SetZoom(m_nZoomPercent); } void CSegDemoView::OnShowInNewWindow() { ILEADRaster *pRaster = NULL; CoCreateInstance( CLSID_LEADRaster, NULL, CLSCTX_ALL, IID_ILEADRaster, (void**)&pRaster ); if(!pRaster) return; int nWidth = abs(m_SegInfo[m_nSelSeg].rcRect.right - m_SegInfo[m_nSelSeg].rcRect.left); int nHeight = abs(m_SegInfo[m_nSelSeg].rcRect.bottom - m_SegInfo[m_nSelSeg].rcRect.top); pRaster->CreateBitmap((float)nWidth, (float)nHeight, m_RasterView.GetRaster().GetBitmapBits()); theApp.m_pRasterProc->CombineExt(pRaster, 0, 0, (float)nWidth, (float)nHeight, m_RasterView.GetRaster(), (float)m_SegInfo[m_nSelSeg].rcRect.left, (float)m_SegInfo[m_nSelSeg].rcRect.top, CB_OP_ADD); theApp.m_pBitmap = pRaster->Bitmap; theApp.OpenDocument(OPENMODE_BITMAP); pRaster->Release(); } void CSegDemoView::OnSelectAllSegments() { for(int i=0; i < m_SegInfo.GetSize(); ++i) m_SegInfo[i].bSelected = TRUE; m_RasterView.ForceRepaint(); } void CSegDemoView::OnUpdateSelectAllSegments(CCmdUI* pCmdUI) { pCmdUI->Enable(m_SegInfo.GetSize()); } void CSegDemoView::OnDeleteSegment() { int nRet = 0; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); for(int i=0; i < m_SegInfo.GetSize(); ++i) { if(m_SegInfo[i].bSelected) { nRet = pDoc->m_pRasterMrc->DeleteSegment(m_SegInfo[i].nSegId); } if (nRet != 0) { MessageBox(TEXT("Error deleting segment."), TEXT("Error"), MB_OK); } } // Enumerate current segments ClearSegments(); pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); } void CSegDemoView::OnUpdateDeleteSegment(CCmdUI* pCmdUI) { pCmdUI->Enable(m_SegInfo.GetSize()); } BOOL CSegDemoView::ManualSegmentation() { CString szError; int nRet, nSegment; ILEADRasterMrc *pRasterMrc = NULL; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); CoCreateInstance( CLSID_LEADRasterMrc, NULL, CLSCTX_ALL, IID_ILEADRasterMrc, (void**)&pRasterMrc ); if(!pRasterMrc) return FALSE; pRasterMrc->PutBaseBackColor(theApp.m_ColorsOptions.crBackground); pRasterMrc->PutBaseForeColor(theApp.m_ColorsOptions.crForeground); nRet = pRasterMrc->StartSegmentation(m_RasterView.GetRaster()); for (nSegment = 0; nSegment < m_SegInfo.GetSize(); nSegment++) { // Set the new segment nRet = pRasterMrc->CreateNewSegment(m_RasterView.GetRaster(), m_SegInfo[nSegment].rcRect.left, m_SegInfo[nSegment].rcRect.top, m_SegInfo[nSegment].rcRect.right, m_SegInfo[nSegment].rcRect.bottom, m_SegInfo[nSegment].SegType); if (nRet < 0) { szError.Format(TEXT("An error occurred while trying to add a new segment: %d.")); MessageBox(szError, TEXT("Error"), MB_OK); // Enumerate current segments pRasterMrc->EnumSegments(); return FALSE; } m_SegInfo[nSegment].nSegId = nRet; } pDoc->m_pRasterMrc->StopSegmentation(); pDoc->m_pRasterMrc->CopySegmentationHandle(pRasterMrc); pRasterMrc->StopSegmentation(); m_bManual = TRUE; pRasterMrc->Release(); return TRUE; } void CSegDemoView::OnUpdateAutoManualSegmentation(CCmdUI* pCmdUI) { pCmdUI->Enable(m_SegInfo.GetSize()); } int CSegDemoView::FindNextSelection(int nIndex) { int nNextIndex = -1; for(int i = nIndex + 1; i < m_SegInfo.GetSize(); ++i) { if(m_SegInfo[i].bSelected) { nNextIndex = i; break; } } return nNextIndex; } void CSegDemoView::OnCombineSegments() { int i, nRet; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); if (FindSelectionCount() < 2) return; // Before update the segment, take a copy for undo for (i = 0; i < m_SegInfo.GetSize() - 1; i++) { if(m_SegInfo[i].bSelected) { pDoc->m_pRasterMrc->PutCombineFactor(theApp.m_CombineOptions.uCombineFactor); pDoc->m_pRasterMrc->PutCombineFlags((CombineFlagsConstants)theApp.m_CombineOptions.uCombineFlags); int nNextIndex = FindNextSelection(i); if(nNextIndex > i) { nRet = pDoc->m_pRasterMrc->CombineSegments(m_SegInfo[i].nSegId , m_SegInfo[nNextIndex].nSegId); if (nRet != 0) { MessageBox(TEXT("Error combining segments; the segments are of different types or not adjacent.") TEXT("\nIf you want to force combining segments of different types") TEXT("\ngo to Preferences -> 'Segmentation and Compression Options...'") TEXT("\nand change the combining type to \"Force\""), TEXT("Error"), MB_OK); break; } } } } // Enumerate current segments ClearSegments(); pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); } void CSegDemoView::OnUpdateCombineSegments(CCmdUI* pCmdUI) { pCmdUI->Enable((FindSelectionCount() > 1)); } void CSegDemoView::UpdateSegments() { int nRet; RECT rcSeg; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); nRet = pDoc->m_pRasterMrc->SetSegmentData(m_RasterView.GetRaster(), m_SegInfo[m_nSelSeg].nSegId, m_SegInfo[m_nSelSeg].rcRect.left, m_SegInfo[m_nSelSeg].rcRect.top, m_SegInfo[m_nSelSeg].rcRect.right, m_SegInfo[m_nSelSeg].rcRect.bottom, m_SegInfo[m_nSelSeg].SegType); if (nRet != 0) { MessageBox(TEXT("Could not update segment."), TEXT("Error"), MB_OK); return; } CopyRect(&rcSeg, &m_SegInfo[m_nSelSeg].rcRect); // Enumerate current segments ClearSegments(); pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); } void CSegDemoView::OnUpdateShowInNewWindow(CCmdUI* pCmdUI) { pCmdUI->Enable(m_nSelSeg > -1); } void CSegDemoView::OnUpdateSegmentType(CCmdUI* pCmdUI) { pCmdUI->Enable(m_nSelSeg > -1); } void CSegDemoView::OnUpdateShowSegInfo(CCmdUI* pCmdUI) { pCmdUI->Enable(m_nSelSeg > -1); } void CSegDemoView::OnUpdateSegmentHistogram(CCmdUI* pCmdUI) { pCmdUI->Enable(m_nSelSeg > -1); } void CSegDemoView::OnUpdateSegmentUniqueColors(CCmdUI* pCmdUI) { pCmdUI->Enable(m_nSelSeg > -1); } BOOL CSegDemoView::CanEnlargeFromLeft(LPRECT pLeftRc, LPRECT pSelRect) { if ((pLeftRc->top <= pSelRect->top) && (pLeftRc->bottom > pSelRect->top) && (pLeftRc->bottom < pSelRect->bottom) && (pLeftRc->right <= pSelRect->left)) return TRUE; else if ((pLeftRc->top >= pSelRect->top) && (pLeftRc->top < pSelRect->bottom) && (pLeftRc->bottom > pSelRect->top) && (pLeftRc->bottom <= pSelRect->bottom) && (pLeftRc->right <= pSelRect->left)) return TRUE; else if ((pLeftRc->top > pSelRect->top) && (pLeftRc->top < pSelRect->bottom) && (pLeftRc->bottom >= pSelRect->bottom) && (pLeftRc->right <= pSelRect->left)) return TRUE; else if ((pLeftRc->top <= pSelRect->top) && (pLeftRc->bottom >= pSelRect->bottom) && (pLeftRc->right <= pSelRect->left)) return TRUE; else return FALSE; } BOOL CSegDemoView::CanEnlargeFromTop(LPRECT pTopRc, LPRECT pSelRect) { if ((pTopRc->left <= pSelRect->left) && (pTopRc->right > pSelRect->left) && (pTopRc->right < pSelRect->right) && (pTopRc->bottom <= pSelRect->top)) return TRUE; else if ((pTopRc->left >= pSelRect->left) && (pTopRc->left < pSelRect->right) && (pTopRc->right > pSelRect->left) && (pTopRc->right <= pSelRect->right) && (pTopRc->bottom <= pSelRect->top)) return TRUE; else if ((pTopRc->left > pSelRect->left) && (pTopRc->left < pSelRect->right) && (pTopRc->right >= pSelRect->right) && (pTopRc->bottom < pSelRect->top)) return TRUE; else if ((pTopRc->left <= pSelRect->left) && (pTopRc->right >= pSelRect->right) && (pTopRc->bottom <= pSelRect->top)) return TRUE; else return FALSE; } BOOL CSegDemoView::CanEnlargeFromRight(LPRECT pRightRc, LPRECT pSelRect) { if ((pRightRc->top <= pSelRect->top) && (pRightRc->bottom > pSelRect->top) && (pRightRc->bottom < pSelRect->bottom) && (pRightRc->left >= pSelRect->right)) return TRUE; else if ((pRightRc->top >= pSelRect->top) && (pRightRc->top < pSelRect->bottom) && (pRightRc->bottom > pSelRect->top) && (pRightRc->bottom <= pSelRect->bottom) && (pRightRc->left >= pSelRect->right)) return TRUE; else if ((pRightRc->top > pSelRect->top) && (pRightRc->top < pSelRect->bottom) && (pRightRc->bottom >= pSelRect->bottom) && (pRightRc->left >= pSelRect->right)) return TRUE; else if ((pRightRc->top <= pSelRect->top) && (pRightRc->bottom >= pSelRect->bottom) && (pRightRc->left >= pSelRect->right)) return TRUE; else return FALSE; } BOOL CSegDemoView::CanEnlargeFromBottom(LPRECT pBottomRc, LPRECT pSelRect) { if ((pBottomRc->left <= pSelRect->left) && (pBottomRc->right > pSelRect->left) && (pBottomRc->right < pSelRect->right) && (pBottomRc->top >= pSelRect->bottom)) return TRUE; else if ((pBottomRc->left >= pSelRect->left) && (pBottomRc->left < pSelRect->right) && (pBottomRc->right > pSelRect->left) && (pBottomRc->right <= pSelRect->right) && (pBottomRc->top >= pSelRect->bottom)) return TRUE; else if ((pBottomRc->left > pSelRect->left) && (pBottomRc->left < pSelRect->right) && (pBottomRc->right >= pSelRect->right) && (pBottomRc->top >= pSelRect->bottom)) return TRUE; else if ((pBottomRc->left <= pSelRect->left) && (pBottomRc->right >= pSelRect->right) && (pBottomRc->top >= pSelRect->bottom)) return TRUE; else return FALSE; } void CSegDemoView::OnEnlargeSegment() { int i, nRet, nLeft, nRight, nTop, nBottom; LPRECT pSelRect, pRect; if (FindSelectionCount() != 1) return; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); nLeft = 0; nRight = (int)m_RasterView.GetRaster().GetBitmapWidth(); nTop = 0; nBottom = (int)m_RasterView.GetRaster().GetBitmapHeight(); pSelRect = &m_SegInfo[m_nSelSeg].rcRect; for (i = 0; i < m_SegInfo.GetSize(); i++) { if (i == m_SegInfo[m_nSelSeg].nSegId) continue; pRect = &m_SegInfo[i].rcRect; // Find the left value if (CanEnlargeFromLeft(pRect, pSelRect)) { nLeft = max(nLeft, pRect->right); } } pSelRect->left = nLeft; for (i = 0; i < m_SegInfo.GetSize(); i++) { if (i == m_SegInfo[m_nSelSeg].nSegId) continue; pRect = &m_SegInfo[i].rcRect; // Find the left value if (CanEnlargeFromTop(pRect, pSelRect)) { nTop = max(nTop, pRect->bottom); } } pSelRect->top = nTop; for (i = 0; i < m_SegInfo.GetSize(); i++) { if (i == m_SegInfo[m_nSelSeg].nSegId) continue; pRect = &m_SegInfo[i].rcRect; // Find the left value if (CanEnlargeFromRight(pRect, pSelRect)) { nRight = min(nRight, pRect->left); } } pSelRect->right = nRight; for (i = 0; i < m_SegInfo.GetSize(); i++) { if (i == m_SegInfo[m_nSelSeg].nSegId) continue; pRect = &m_SegInfo[i].rcRect; // Find the left value if (CanEnlargeFromBottom(pRect, pSelRect)) { nBottom = min(nBottom, pRect->top); } } pSelRect->bottom = nBottom; nRet = pDoc->m_pRasterMrc->SetSegmentData(m_RasterView.GetRaster(), m_SegInfo[m_nSelSeg].nSegId, m_SegInfo[m_nSelSeg].rcRect.left, m_SegInfo[m_nSelSeg].rcRect.top, m_SegInfo[m_nSelSeg].rcRect.right, m_SegInfo[m_nSelSeg].rcRect.bottom, m_SegInfo[m_nSelSeg].SegType); if (nRet != 0) { MessageBox(TEXT("Could not update segment."), TEXT("Error"), MB_OK); } // Enumerate current segments ClearSegments(); pDoc->m_pRasterMrc->EnumSegments(); m_RasterView.ForceRepaint(); } void CSegDemoView::OnFileSavePdf() { int nRet; CSegDemoDoc *pDoc = (CSegDemoDoc *)GetDocument(); theApp.m_pRasterFileDlg->PutBitmap (m_RasterView.GetRaster().GetBitmap()); theApp.m_pRasterFileDlg->PutEnableMethodErrors(FALSE); theApp.m_pRasterFileDlg->PutSaveQFactor(2); theApp.m_pRasterFileDlg->PutFileDlgFlags(OFN_OVERWRITEPROMPT); theApp.m_pRasterFileDlg->PutUIFlags(SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS| SAVE_SHOW_FILEOPTIONS_J2KOPTIONS | SAVE_SHOW_FILEOPTIONS_MULTIPAGE | SAVE_SHOW_FILEOPTIONS_QFACTOR); theApp.m_pRasterFileDlg->PutFileFormatsCount(1); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutFormat(FF_SAVE_PDF); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutBitsPerPixelCount(1); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutBitsPerPixelFormat(0, 24); theApp.m_pRasterFileDlg->GetFileFormats(0)->PutSubFormat(0, FF_SAVE_SUB_PDF24_PDF_LEAD_MRC); theApp.m_pRasterFileDlg->PutSaveMulti(MULTIPAGE_APPEND); theApp.m_pRasterFileDlg->PutDialogTitle("Save as PDF"); nRet = theApp.m_pRasterFileDlg->ShowSaveDlg((long)this->m_hWnd); if(nRet !=0) { theApp.DisplayLEADError(nRet); } else { if(theApp.m_pRasterFileDlg->GetDialogStatus()!= DLG_OK) { theApp.m_pRasterFileDlg->PutBitmap(0); return; } } theApp.m_pRasterFileDlg->PutBitmap(0); int nFotmat = theApp.m_pRasterFileDlg->GetSaveFormat(); int nPage = theApp.m_pRasterFileDlg->GetPageNumber(); int nModify = theApp.m_pRasterFileDlg->GetSaveMulti(); pDoc->m_pRasterMrc->PutPictureQFactor(theApp.m_MrcOptions.nPictureQFactor); pDoc->m_pRasterMrc->PutMaskCoder((MRCMaskCoderConstants)(theApp.m_MrcOptions.nMaskCoder + MRC_PDF_ONEBIT_COMPRESSION_ZIP)); pDoc->m_pRasterMrc->PutPictureCoder((MRCPictureCoderConstants)(theApp.m_MrcOptions.nPictureCoder + MRC_PDF_PICTURE_COMPRESSION_JPEG)); pDoc->m_pRasterMrc->PutText2BitCoder((MRCText2BitCoderConstants)(theApp.m_MrcOptions.nTextCoder2Bit + MRC_PDF_TEXT_COMPRESSION_ZIP)); pDoc->m_pRasterMrc->PutGrayscale8BitQFactor(theApp.m_MrcOptions.nGrayscale8BitFactor); pDoc->m_pRasterMrc->PutGrayscale2BitCoder((MRCGrayscale2BitCoderConstants)theApp.m_MrcOptions.nGrayscale2BitCoder); pDoc->m_pRasterMrc->PutGrayscale8BitCoder((MRCGrayscale8BitCoderConstants)theApp.m_MrcOptions.nGrayscale8BitCoder); // Check for manual segmentation if (m_bManual) { if (!ManualSegmentation()) return; } BeginWaitCursor(); nRet = pDoc->m_pRasterMrc->Save(m_RasterView.GetRaster(), theApp.m_pRasterFileDlg->GetFileName(), nFotmat, nPage, nModify); if (nRet != 0) { theApp.DisplayLEADError(nRet); } EndWaitCursor(); } void CSegDemoView::OnUpdateEnlargeSegment(CCmdUI* pCmdUI) { pCmdUI->Enable(m_nSelSeg > -1); } void CSegDemoView::OnDeselectAll() { int i, nCount; BeginWaitCursor(); nCount = m_SegInfo.GetSize(); for (i = 0; i < nCount; i++) { m_SegInfo[i].bSelected = FALSE; } m_RasterView.ForceRepaint(); EndWaitCursor(); } void CSegDemoView::OnUpdateDeselectAll(CCmdUI* pCmdUI) { pCmdUI->Enable(m_SegInfo.GetSize()); } void CSegDemoView::OnKeyDownLeadRasterView1(short KeyCode, short Shift) { if(KeyCode == VK_ESCAPE) OnDeselectAll(); }