// DemoView.cpp : implementation of the CDemoView class // #include "stdafx.h" #include "DemoView.h" CDemo* pTheApp=0; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //extern CDemo theApp; ///////////////////////////////////////////////////////////////////////////// // CDemoView IMPLEMENT_DYNCREATE(CDemoView, CView) BEGIN_MESSAGE_MAP(CDemoView, CView) //{{AFX_MSG_MAP(CDemoView) ON_WM_CREATE() ON_WM_SIZE() ON_COMMAND(ID_EDIT_COPY, OnEditCopy) ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy) ON_COMMAND(IDM_VIEW_NORMAL, OnViewNormal) ON_UPDATE_COMMAND_UI(IDM_VIEW_NORMAL, OnUpdateViewNormal) ON_COMMAND(IDM_VIEW_ZOOMIN, OnViewZoomin) ON_UPDATE_COMMAND_UI(IDM_VIEW_ZOOMIN, OnUpdateViewZoomin) ON_COMMAND(IDM_VIEW_ZOOMOUT, OnViewZoomout) ON_UPDATE_COMMAND_UI(IDM_VIEW_ZOOMOUT, OnUpdateViewZoomout) ON_COMMAND(IDM_FIT_FITWIN, OnFitFitwin) ON_UPDATE_COMMAND_UI(IDM_FIT_FITWIN, OnUpdateFitFitwin) ON_COMMAND(IDM_FIT_HEIGHT, OnFitHeight) ON_UPDATE_COMMAND_UI(IDM_FIT_HEIGHT, OnUpdateFitHeight) ON_COMMAND(IDM_FIT_WIDTH, OnFitWidth) ON_UPDATE_COMMAND_UI(IDM_FIT_WIDTH, OnUpdateFitWidth) ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs) ON_COMMAND(ID_FILE_PRINT, OnFilePrint) ON_COMMAND(IDM_VIEW_ZOOM_CENTER, OnViewZoomCenter) ON_UPDATE_COMMAND_UI(IDM_VIEW_ZOOM_CENTER, OnUpdateViewZoomCenter) ON_UPDATE_COMMAND_UI(ID_APP_ABOUT, OnUpdateAppAbout) ON_UPDATE_COMMAND_UI(ID_FILE_PRINT, OnUpdateFilePrint) ON_UPDATE_COMMAND_UI(ID_FILE_PRINT_SETUP, OnUpdateFilePrintSetup) ON_UPDATE_COMMAND_UI(ID_WINDOW_ARRANGE, OnUpdateWindowArrange) ON_UPDATE_COMMAND_UI(ID_WINDOW_CASCADE, OnUpdateWindowCascade) ON_UPDATE_COMMAND_UI(ID_WINDOW_TILE_HORZ, OnUpdateWindowTileHorz) ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS, OnUpdateFileSaveAs) ON_UPDATE_COMMAND_UI(ID_INDICATOR_FILENAME, OnUpdateIndicatorFileName) ON_UPDATE_COMMAND_UI(ID_INDICATOR_SIZE, OnUpdateIndicatorSize) ON_UPDATE_COMMAND_UI(ID_INDICATOR_BPP, OnUpdateIndicatorBPP) ON_UPDATE_COMMAND_UI(ID_INDICATOR_ZOOMPERCENT, OnUpdateIndicatorZoomPercent) ON_UPDATE_COMMAND_UI(ID_INDICATOR_MEMSIZE, OnUpdateIndicatorMemSize) ON_COMMAND(ID_EDIT_PASTE_FLOATER, OnEditPasteFloater) ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_FLOATER, OnUpdateEditPasteFloater) ON_COMMAND(ID_EDIT_COPY_FLOATER, OnEditCopyFloater) ON_UPDATE_COMMAND_UI(ID_EDIT_COPY_FLOATER, OnUpdateEditCopyFloater) ON_COMMAND(IDM_PREVIEW, OnPreview) ON_UPDATE_COMMAND_UI(IDM_PREVIEW, OnUpdatePreview) ON_COMMAND(ID_SEGMENTATION_STARTAUTOANDCLEARMANUALSEGMENTS, OnSegmentationStartautoandclearmanualsegments) ON_UPDATE_COMMAND_UI(ID_PREFERENCES_SHOWSEGMENTSTYPES, OnUpdatePreferencesShowsegmentstypes) ON_COMMAND(ID_PREFERENCES_SHOWSEGMENTSTYPES, OnPreferencesShowsegmentstypes) ON_COMMAND(ID_SEGMENTATION_CLEARSEGMENTS, OnSegmentationClearsegments) ON_UPDATE_COMMAND_UI(ID_SEGMENTATION_CLEARSEGMENTS, OnUpdateSegmentationClearsegments) ON_COMMAND(ID_EDIT_SELECTALLSEGMENTS, OnEditSelectallsegments) ON_UPDATE_COMMAND_UI(ID_EDIT_SELECTALLSEGMENTS, OnUpdateEditSelectallsegments) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_COMMAND(ID_SEGMENTATION_DRAW_DRAWSEGMENT, OnSegmentationDrawDrawsegment) ON_COMMAND(ID_SEGMENTATION_DRAW_STOPDRAWING, OnSegmentationDrawStopdrawing) ON_UPDATE_COMMAND_UI(ID_SEGMENTATION_DRAW_DRAWSEGMENT, OnUpdateSegmentationDrawDrawsegment) ON_UPDATE_COMMAND_UI(ID_SEGMENTATION_DRAW_STOPDRAWING, OnUpdateSegmentationDrawStopdrawing) ON_COMMAND(ID_EDIT_DESELECTALLSEGMENTS, OnEditDeselectallsegments) ON_COMMAND(ID_FILE_EXPORTSEGMENTS, OnFileExportsegments) ON_COMMAND(ID_FILE_IMPORTSEGMENTS, OnFileImportsegments) ON_COMMAND(ID_EDIT_DELETESELECTEDSEGMENTS, OnEditDeleteselectedsegments) ON_UPDATE_COMMAND_UI(ID_FILE_EXPORTSEGMENTS, OnUpdateFileExportsegments) ON_UPDATE_COMMAND_UI(ID_EDIT_DESELECTALLSEGMENTS, OnUpdateEditDeselectallsegments) ON_UPDATE_COMMAND_UI(ID_EDIT_DELETESELECTEDSEGMENTS, OnUpdateEditDeleteselectedsegments) ON_COMMAND(ID_SEGMENTATION_STARTAUTOANDPRESERVEMANUALSEGMENTS, OnSegmentationStartautoandpreservemanualsegments) ON_UPDATE_COMMAND_UI(ID_SEGMENTATION_STARTAUTOANDPRESERVEMANUALSEGMENTS, OnUpdateSegmentationStartautoandpreservemanualsegments) ON_COMMAND(ID_FILE_SAVEMULTIPAGE, OnFileSavemultipage) ON_COMMAND(ID_PREFERENCES_SEGMENTATIONANDCOMPRESSIONOPTIONS, OnPreferencesSegmentationandcompressionoptions) ON_COMMAND(ID_COLOR_COLORRESOLUTION, OnColorColorresolution) ON_COMMAND(ID_COLOR_VIEWHISTOGRAM, OnColorViewhistogram) ON_COMMAND(ID_COLOR_UNIQUECOLORS, OnColorUniquecolors) ON_COMMAND(ID_FILE_SAVEASLEADMRC, OnFileSaveasleadmrc) ON_COMMAND(ID_FILE_SAVEASMRC, OnFileSaveasmrc) ON_COMMAND(ID_FILE_SAVEASPDF, OnFileSaveaspdf) ON_WM_CONTEXTMENU() ON_COMMAND(ID_VIEW_SELECTEDSEGMENT_ENLARGESEGMENT, OnViewSelectedsegmentEnlargesegment) ON_COMMAND(ID_VIEW_SELECTEDSEGMENT_UNIQUECOLORS, OnViewSelectedsegmentUniquecolors) ON_COMMAND(ID_VIEW_SELECTEDSEGMENT_SHOWHISTOGRAM, OnViewSelectedsegmentShowhistogram) ON_COMMAND(ID_VIEW_SELECTEDSEGMENT_SHOWTYPE, OnViewSelectedsegmentShowtype) ON_COMMAND(ID_VIEW_SELECTEDSEGMENT_SHOWPROPERTIES, OnViewSelectedsegmentShowproperties) ON_UPDATE_COMMAND_UI(ID_VIEW_SELECTEDSEGMENT_SHOWHISTOGRAM, OnUpdateViewSelectedsegmentShowhistogram) ON_UPDATE_COMMAND_UI(ID_VIEW_SELECTEDSEGMENT_SHOWPROPERTIES, OnUpdateViewSelectedsegmentShowproperties) ON_UPDATE_COMMAND_UI(ID_VIEW_SELECTEDSEGMENT_SHOWTYPE, OnUpdateViewSelectedsegmentShowtype) ON_UPDATE_COMMAND_UI(ID_VIEW_SELECTEDSEGMENT_UNIQUECOLORS, OnUpdateViewSelectedsegmentUniquecolors) ON_COMMAND(ID_VIEW_SELECTEDSEGMENT_COMBINESEGMENTS, OnViewSelectedsegmentCombinesegments) ON_COMMAND(ID_VIEW_SELECTEDSEGMENT_SHOWINNEWWINDOW, OnViewSelectedsegmentShowinnewwindow) ON_COMMAND(ID_SEGMENTATION_REFRESHSEGMENTS, OnSegmentationRefreshsegments) ON_UPDATE_COMMAND_UI(ID_VIEW_SELECTEDSEGMENT_ENLARGESEGMENT, OnUpdateViewSelectedsegmentEnlargeSegment) ON_WM_RBUTTONDOWN() ON_UPDATE_COMMAND_UI(ID_VIEW_SELECTEDSEGMENT_COMBINESEGMENTS, OnUpdateViewSelectedsegmentCombinesegments) ON_UPDATE_COMMAND_UI(ID_VIEW_SELECTEDSEGMENT_SHOWINNEWWINDOW, OnUpdateViewSelectedsegmentShowinnewwindow) ON_UPDATE_COMMAND_UI(ID_SEGMENTATION_REFRESHSEGMENTS, OnUpdateSegmentationRefreshsegments) ON_WM_KEYDOWN() ON_WM_SETFOCUS() ON_WM_KEYUP() //}}AFX_MSG_MAP // Standard printing commands ON_MESSAGE(WM_HANDLEPALETTE,OnHandlePalette) ON_MESSAGE(WM_LOAD_FILE,OnLoadFile) ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() //BEGIN: External Functions (Defined in Demo.cpp)// L_VOID NormalizeRect(RECT* pRect); L_VOID ChangeToImage(LUserBitmapWindow* pbitmap, RECT* pRect); L_VOID ChangeToZoom(LUserBitmapWindow* pbitmap, RECT* pRect); //END: External Functions (Defined in Demo.cpp)// ///////////////////////////////////////////////////////////////////////////// // LUserSegment construction/destruction and member definitions LUserSegment::LUserSegment() { m_bStarted = FALSE; m_nCurrentSelection = -1; m_nPreviousSelection = -1; } L_INT LUserSegment::Start(LBitmapBase * pBitmap, COLORREF clrBackground, COLORREF clrForeground) { L_INT nRet = SUCCESS; if (!IsStarted()) { nRet = MrcStartBitmapSegmentation(pBitmap, clrBackground, clrForeground); if(nRet != SUCCESS) return nRet; m_bStarted = TRUE; } return nRet; } L_INT LUserSegment::Stop() { L_INT nRet = SUCCESS; if (IsStarted()) { nRet = MrcStopBitmapSegmentation(); if(nRet != SUCCESS) return nRet; m_bStarted = FALSE; } return nRet; } L_BOOL LUserSegment::IsStarted() { return m_bStarted; } L_INT LUserSegment::MrcEnumSegmentsCallBack(const pSEGMENTDATA pSegmentData, L_INT nSegId) { CopyRect(&(m_pView->m_Segment.m_pSegmentData[nSegId].m_Data.rcBitmapSeg), &(pSegmentData->rcBitmapSeg)); m_pView->m_Segment.m_pSegmentData[nSegId].m_Data.uStructSize = pSegmentData->uStructSize ; m_pView->m_Segment.m_pSegmentData[nSegId].m_Data.uType = pSegmentData->uType ; return SUCCESS; } ///////////////////////////////////////////////////////////////////////////// // CDemoView construction/destruction CDemoView::CDemoView() { if(pTheApp==0) pTheApp = (CDemo*)AfxGetApp(); m_LBitmapWnd.SetAutoFloaterToRgn(FALSE); m_bManualDraw = FALSE ; m_bCTRLKeyUp = FALSE ; m_bImportedSegments = FALSE ; m_bLBDown = FALSE; } CDemoView::~CDemoView() { } void CDemoView::OnInitialUpdate() { CView::OnInitialUpdate(); m_Segment.m_nSegmentsCount = 0 ; m_Segment.m_pSegmentData = NULL ; m_LBitmapWnd.EnableCenterOnZoom(FALSE); OnFitFitwin(); m_bShowSegmentType = TRUE; m_LBitmapWnd.SetDisplayMode(DISPLAYMODE_RESAMPLE,DISPLAYMODE_RESAMPLE); LBitmapSettings::SetDisplayMode(DISPLAYMODE_RESAMPLE|DISPLAYMODE_BICUBIC, DISPLAYMODE_RESAMPLE); m_LBitmapWnd.SetZoomFactor(1.1f); m_LBitmapWnd.m_pView = this; m_Segment.m_pView = this; m_LBitmapWnd.Region()->Free(); m_LBitmapWnd.CancelRgn(); } BOOL CDemoView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.style|=(WS_CLIPCHILDREN|WS_CLIPSIBLINGS); return CView::PreCreateWindow(cs); } LRESULT CDemoView::OnHandlePalette(WPARAM wParam,LPARAM lParam) { if(lParam==TRUE) { m_LBitmapWnd.HandlePalette(WM_PALETTECHANGED,wParam,0); m_LBitmapWnd.Repaint(); } else { if(m_LBitmapWnd.HandlePalette(WM_QUERYNEWPALETTE,0,0)==FALSE) m_LBitmapWnd.Repaint(); } return TRUE; } void CDemoView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) { if(bActivate==TRUE&&pActivateView==this) { m_LBitmapWnd.HandlePalette(WM_QUERYNEWPALETTE,0,0); ::SetFocus(m_LBitmapWnd.GetBitmapWnd()); } if (!bActivate) Invalidate(); UpdateWindow(); } void CDemoView::OnDraw(CDC* pDC) { CDemoDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); } ///////////////////////////////////////////////////////////////////////////// // CDemoView printing BOOL CDemoView::OnPreparePrinting(CPrintInfo* pInfo) { return DoPreparePrinting(pInfo); } void CDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CDemoView diagnostics #ifdef _DEBUG void CDemoView::AssertValid() const { CView::AssertValid(); } void CDemoView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CDemoDoc* CDemoView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDemoDoc))); return (CDemoDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CDemoView message handlers int CDemoView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; if(pTheApp->m_pMainWnd->IsIconic()==TRUE) ((CMDIChildWnd*)GetParent())->MoveWindow(0,0,100,100); L_INT nRetCode = SUCCESS; //.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.> // // There are two way to use LEAD controls : // 1.As a control: // Enable A and Disable B // 2.As a Window : // Enable B and Disable As // // A : (LBitmapWindow works as a Window ) nRetCode = m_LBitmapWnd.SetWndHandle(m_hWnd); //End of A. // B : (LBitmapWindow works as a Control) /* HWND hWnd = NULL; hWnd = m_LBitmapWnd.CreateWnd(m_hWnd,0, WS_VISIBLE|L_BS_CENTER| L_BS_PROCESSKEYBOARD, 0,0,200,200); */ //End of B. if(nRetCode!=SUCCESS/*||hWnd==NULL*/) { LBase::DisplayErrorFromList(); return -1; } return 0; } void CDemoView::OnSize(UINT nType, int cx, int cy) { RECT rcDest; m_LBitmapWnd.GetRects(NULL,NULL,&rcDest,NULL); m_LBitmapWnd.m_fZoom = (L_FLOAT)((((L_FLOAT) (rcDest.right-rcDest.left) / (L_FLOAT)m_LBitmapWnd.GetWidth()) * 100.0)/* + 0.5*/); if(!m_LBitmapWnd.IsAutoScrollEnabled()) m_LBitmapWnd.FitToParent(TRUE); CView::OnSize(nType, cx, cy); } /////////////////////////////////////////////////////////////////////////////// // Copy // /////////////////////////////////////////////////////////////////////////////// void CDemoView::OnEditCopy() { m_LBitmapWnd.CopyToClipboard(m_hWnd,COPY2CB_EMPTY|COPY2CB_DIB|COPY2CB_DDB|COPY2CB_PALETTE); } void CDemoView::OnUpdateEditCopy(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnEditPasteFloater() { m_LBitmapWnd.CopyFromClipboard(m_hWnd); L_BOOL isHas = m_LBitmapWnd.HasFloater(); isHas = m_LBitmapWnd.HasRgn(); } void CDemoView::OnUpdateEditPasteFloater(CCmdUI* pCmdUI) { pCmdUI->Enable(m_LBitmapWnd.IsClipboardReady()); } void CDemoView::OnEditCopyFloater() { m_LBitmapWnd.CopyToClipboard(m_hWnd); } void CDemoView::OnUpdateEditCopyFloater(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } /////////////////////////////////////////////////////////////////////////////// // View Menu // /////////////////////////////////////////////////////////////////////////////// void CDemoView::OnViewZoomin() { m_LBitmapWnd.ZoomIn(); } void CDemoView::OnUpdateViewZoomin(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnViewZoomout() { m_LBitmapWnd.ZoomOut(); } void CDemoView::OnUpdateViewZoomout(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnViewNormal() { m_LBitmapWnd.SetZoomMode(ZOOM_NORMAL); //syncronizing segments rectangles to correct zooming: RECT rcClient; GetClientRect(&rcClient); SendNotifyMessage( WM_SIZE, SIZE_RESTORED, MAKELPARAM(rcClient.right,rcClient.bottom)); Invalidate(TRUE); UpdateWindow(); } void CDemoView::OnUpdateViewNormal(CCmdUI* pCmdUI) { pCmdUI->Enable(m_LBitmapWnd.GetZoomMode()!=ZOOM_NORMAL); } void CDemoView::OnFitFitwin() { m_LBitmapWnd.SetZoomMode(ZOOM_FIT); //syncronizing segments rectangles to correct zooming: RECT rcClient; GetClientRect(&rcClient); SendNotifyMessage( WM_SIZE, SIZE_RESTORED, MAKELPARAM(rcClient.right,rcClient.bottom)); Invalidate(TRUE); UpdateWindow(); } void CDemoView::OnUpdateFitFitwin(CCmdUI* pCmdUI) { pCmdUI->Enable(m_LBitmapWnd.GetZoomMode()!=ZOOM_FIT); } void CDemoView::OnFitHeight() { m_LBitmapWnd.SetZoomMode(ZOOM_FITHEIGHT); //syncronizing segments rectangles to correct zooming: RECT rcClient; GetClientRect(&rcClient); SendNotifyMessage( WM_SIZE, SIZE_RESTORED, MAKELPARAM(rcClient.right,rcClient.bottom)); Invalidate(TRUE); UpdateWindow(); } void CDemoView::OnUpdateFitHeight(CCmdUI* pCmdUI) { pCmdUI->Enable(m_LBitmapWnd.GetZoomMode()!=ZOOM_FITHEIGHT); } void CDemoView::OnFitWidth() { m_LBitmapWnd.SetZoomMode(ZOOM_FITWIDTH); //syncronizing segments rectangles to correct zooming: RECT rcClient; GetClientRect(&rcClient); SendNotifyMessage( WM_SIZE, SIZE_RESTORED, MAKELPARAM(rcClient.right,rcClient.bottom)); Invalidate(TRUE); UpdateWindow(); } void CDemoView::OnUpdateFitWidth(CCmdUI* pCmdUI) { pCmdUI->Enable(m_LBitmapWnd.GetZoomMode()!=ZOOM_FITWIDTH); } void CDemoView::OnViewZoomCenter() { m_LBitmapWnd.EnableCenterOnZoom(!m_LBitmapWnd.IsCenterOnZoomEnabled()); } void CDemoView::OnUpdateViewZoomCenter(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_LBitmapWnd.IsCenterOnZoomEnabled()); } /////////////////////////////////////////////////////////////////////////////// // File Save // /////////////////////////////////////////////////////////////////////////////// void CDemoView::OnFileSaveAs() { SAVEDLGPARAMS SaveParms ; OPENFILENAME OpenFileName ; memset ( &SaveParms, 0, sizeof ( SAVEDLGPARAMS ) ) ; OpenFileName.lStructSize = sizeof ( OPENFILENAME ) ; OpenFileName.lpstrInitialDir = NULL; OpenFileName.lpstrTitle = _T("Save a File"); OpenFileName.nFilterIndex = 0 ; SaveParms.uStructSize = sizeof ( SAVEDLGPARAMS) ; SaveParms.nQFactor = 2 ; SaveParms.nPageNumber = 1 ; SaveParms.nStampBits = 24; SaveParms.nStampWidth = 120; SaveParms.nStampHeight = 120; SaveParms.nBitsPerPixel = m_LBitmapWnd.GetBitsPerPixel(); SaveParms.nFormat = FILE_BMP; SaveParms.uSaveMulti = MULTIPAGE_OPERATION_REPLACE ; SaveParms.uDlgFlags = DLG_SAVE_SHOW_FILEOPTIONS_PROGRESSIVE | DLG_SAVE_SHOW_FILEOPTIONS_MULTIPAGE | DLG_SAVE_SHOW_FILEOPTIONS_STAMP | DLG_SAVE_SHOW_FILEOPTIONS_QFACTOR | DLG_SAVE_SHOW_FILEOPTIONS_J2KOPTIONS | DLG_SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS ; m_LBitmapWnd.DialogFile()->SetSaveParams(&SaveParms); m_LBitmapWnd.DialogFile()->EnableAutoProcess(); if(m_LBitmapWnd.DialogFile()->DoModalSave(m_hWnd)==SUCCESS_DLG_OK) { SAVEDLGPARAMS sp; m_LBitmapWnd.DialogFile()->GetSaveParams(&sp, sizeof(sp)); L_TCHAR zsFileName[255]; L_TCHAR zsName[50]; L_TCHAR zsExt[5]; memset(zsFileName,0,sizeof(zsFileName)); memset(zsName,0,sizeof(zsName)); memset(zsExt,0,sizeof(zsExt)); L_UINT uSize = sizeof(zsFileName); m_LBitmapWnd.GetFileName(zsFileName,&uSize); _tsplitpath(zsFileName,NULL,NULL,zsName,zsExt); lstrcat(zsName,zsExt); GetDocument()->SetTitle(zsName); m_LBitmapWnd.Load(); } } /////////////////////////////////////////////////////////////////////////////// // Print // /////////////////////////////////////////////////////////////////////////////// void CDemoView::OnFilePrint() { HDC hPrintDC = m_LBitmapWnd.Print()->GetPrinterDC(); HCURSOR hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); L_INT nWidth = MulDiv(m_LBitmapWnd.GetWidth(),GetDeviceCaps(hPrintDC,LOGPIXELSX),m_LBitmapWnd.GetXResolution()); L_INT nHeight = MulDiv(m_LBitmapWnd.GetHeight(),GetDeviceCaps(hPrintDC,LOGPIXELSY),m_LBitmapWnd.GetYResolution()); m_LBitmapWnd.Print()->Print(hPrintDC,1,1,nWidth,nHeight,TRUE); SetCursor (hCursor); } void CDemoView::OnUpdateFilePrint(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateFilePrintSetup(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } /////////////////////////////////////////////////////////////////////////////// // About & Window // /////////////////////////////////////////////////////////////////////////////// void CDemoView::OnUpdateAppAbout(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateWindowArrange(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateWindowCascade(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateWindowTileHorz(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateFileSaveAs(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } /////////////////////////////////////////////////////////////////////////////// // Status Bar // /////////////////////////////////////////////////////////////////////////////// void CDemoView::OnUpdateIndicatorFileName(CCmdUI* pCmdUI) { L_TCHAR szBuffer[256] ; L_UINT uBuffSize = sizeof(szBuffer); memset(szBuffer,0,sizeof(szBuffer)); m_LBitmapWnd.GetFileName( szBuffer, &uBuffSize); pCmdUI->SetText(szBuffer) ; } void CDemoView::OnUpdateIndicatorSize(CCmdUI* pCmdUI) { L_INT nHeight = m_LBitmapWnd.GetHeight(); L_INT nWidth = m_LBitmapWnd.GetWidth(); L_INT nViewPerspictive = m_LBitmapWnd.GetViewPerspective(); L_TCHAR szBuffer[50] ; memset(szBuffer,0,sizeof(szBuffer)) ; wsprintf(szBuffer,_T("%d X %d, %s\n"), nWidth, nHeight, (LPTSTR)(nViewPerspictive == TOP_LEFT ? _T("TOP_LEFT") : nViewPerspictive == TOP_RIGHT ? _T("TOP_RIGHT") : nViewPerspictive == BOTTOM_RIGHT ? _T("BOTTOM_RIGHT") : nViewPerspictive == TOP_LEFT90 ? _T("TOP_LEFT90") : nViewPerspictive == TOP_LEFT270? _T("TOP_LEFT270") : _T("BOTTOM_LEFT"))); pCmdUI->SetText(szBuffer) ; } void CDemoView::OnUpdateIndicatorBPP(CCmdUI* pCmdUI) { L_TCHAR szBuffer[50] ; L_INT nBitsPerPixel = m_LBitmapWnd.GetBitsPerPixel(); memset(szBuffer,0,sizeof(szBuffer)) ; wsprintf(szBuffer,_T("%d BPP\n"),nBitsPerPixel) ; pCmdUI->SetText(szBuffer) ; } void CDemoView::OnUpdateIndicatorZoomPercent(CCmdUI* pCmdUI) { L_TCHAR szBuffer[50] ; L_UINT uZoomPercent; L_UINT uZoomMode = m_LBitmapWnd.GetZoomMode(); memset(szBuffer,0,sizeof(szBuffer)) ; switch (uZoomMode) { case ZOOM_FIT: wsprintf(szBuffer,_T("Fit To Window")); break ; case ZOOM_FITWIDTH: wsprintf(szBuffer,_T("Fit To Width")); break ; case ZOOM_FITHEIGHT: wsprintf(szBuffer,_T("Fit To Height")); break ; default: uZoomPercent = m_LBitmapWnd.GetZoomPercent(); wsprintf(szBuffer,_T("%u%%"),uZoomPercent); break; } pCmdUI->SetText(szBuffer) ; } void CDemoView::OnUpdateIndicatorMemSize(CCmdUI* pCmdUI) { CString strBuf ; CString str ; L_UINT32 ulSize = m_LBitmapWnd.GetMemSize(); if (m_LBitmapWnd.Is1BitCompressed ()) { strBuf = _T("COMP") ; } else { if (m_LBitmapWnd.IsSuperCompressed ()) { strBuf = _T("SUP.CMP") ; } else { if (m_LBitmapWnd.IsTiled ( ) ) { strBuf = _T("TILED") ; } else { if (m_LBitmapWnd.IsUsingDiskMemory ()) { strBuf = _T("DISK") ; } else { strBuf = _T("MEMORY") ; } } } } // bitmap size < 10MB ? if(ulSize < 10000000) str.Format ( _T("%lu bytes, %s"), ulSize, strBuf); else // > 100MB str.Format ( _T("%lu KB, %s"), (ulSize >> 10), strBuf); pCmdUI->SetText(str) ; } LRESULT CDemoView::OnLoadFile(WPARAM wParam,LPARAM lParam) { CDemoDoc* pDoc = GetDocument(); if(pDoc==NULL) { DestroyWindow(); return 0; } pDoc->OnLoadFile(TRUE); return 0; } /****************************************************** /* /* File Menu /* /******************************************************/ void CDemoView::OnPreview() { L_INT nRet = SUCCESS_DLG_OK ; PRINTPREVIEWDLGPARAMS DlgParams; memset(&DlgParams, 0, sizeof(DlgParams)); DlgParams.uStructSize = sizeof ( PRINTPREVIEWDLGPARAMS ) ; DlgParams.nCmdShow = SW_SHOW ; m_LBitmapWnd.DialogFile()->SetPrintPreviewParams(&DlgParams); nRet = m_LBitmapWnd.DialogFile()->DoModalPrintPreview(m_hWnd); } void CDemoView::OnUpdatePreview(CCmdUI* pCmdUI) { pCmdUI->Enable(TRUE); } void CDemoView::OnSegmentationStartautoandclearmanualsegments() { L_INT nRet = FAILURE ; memset(&pTheApp->SegExtOptions,0,sizeof(pTheApp->SegExtOptions)); /* Specify the minimum segment width and height*/ pTheApp->SegExtOptions.uStructSize = sizeof(pTheApp->SegExtOptions); pTheApp->SegExtOptions.uBackGroundThreshold = pTheApp->Options.SEGM_nBkgndThreshold; pTheApp->SegExtOptions.uSegmentQuality = pTheApp->Options.SEGM_nQuality; pTheApp->SegExtOptions.uColorThreshold = pTheApp->Options.SEGM_nColorThreshold; pTheApp->SegExtOptions.uCleanSize = pTheApp->Options.SEGM_nCleanSize; pTheApp->SegExtOptions.uCombineThreshold = pTheApp->Options.SEGM_nCombineThreshold; pTheApp->SegExtOptions.uFlags = pTheApp->Options.SEGM_uFlags; // stop it in case it was started for any reason nRet = m_Segment.Stop(); if(nRet != SUCCESS) { MessageBox( TEXT("ERR: could not stop segmentation!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } BeginWaitCursor(); OnSegmentationClearsegments(); EndWaitCursor(); // Start the segmentation process nRet = m_Segment.Start(&m_LBitmapWnd, pTheApp->Options.CLR_crBackground, pTheApp->Options.CLR_crForeground); if(nRet != SUCCESS) { MessageBox( TEXT("ERR: could not start segmentation!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } // do the auto-segmentation BeginWaitCursor(); nRet = m_Segment.MrcSegmentBitmapExt(&m_LBitmapWnd, &pTheApp->SegExtOptions); EndWaitCursor(); if(nRet != SUCCESS) { MessageBox( TEXT("ERR: could not do segmentation!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } // get number of segments generated m_Segment.EnableCallBack(FALSE); BeginWaitCursor(); m_Segment.m_nSegmentsCount = m_Segment.MrcEnumSegments(0); EndWaitCursor(); if (m_Segment.m_nSegmentsCount < 1) { MessageBox( TEXT("ERR: could get segments count!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } // allocate buffer to hold segments data m_Segment.m_pSegmentData = (SEGMENTDATACHILD*)GlobalAllocPtr(GHND,sizeof(SEGMENTDATACHILD)*(m_Segment.m_nSegmentsCount)); if (!m_Segment.m_pSegmentData) { MessageBox( TEXT("ERR: no memory, could not allocate buffer for segments data!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } // get segment data structures m_Segment.EnableCallBack(TRUE); BeginWaitCursor(); nRet = m_Segment.MrcEnumSegments(0); EndWaitCursor(); if (nRet < 1) { MessageBox( TEXT("ERR: could enumerate segments!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } // we will not stop the segmentation process now, // because we will need the LSegment class object's handle in other places Invalidate(); } void CDemoView::OnPreferencesShowsegmentstypes() { m_bShowSegmentType = !m_bShowSegmentType ; Invalidate(); } void CDemoView::OnUpdatePreferencesShowsegmentstypes(CCmdUI* pCmdUI) { if(m_bShowSegmentType) pCmdUI->SetCheck(1); else pCmdUI->SetCheck(0); } void CDemoView::OnSegmentationClearsegments() { L_INT nRet ; if (m_Segment.m_pSegmentData != NULL) { GlobalFreePtr(m_Segment.m_pSegmentData); m_Segment.m_pSegmentData = NULL ; m_Segment.m_nSegmentsCount = 0 ; m_bImportedSegments = FALSE; nRet = m_Segment.Stop(); if(nRet != SUCCESS) { MessageBox( TEXT("ERR: could not stop segmentation!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } OnSegmentationDrawStopdrawing(); Invalidate(); } } void CDemoView::OnUpdateSegmentationClearsegments(CCmdUI* pCmdUI) { if (m_Segment.m_pSegmentData != NULL) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } void CDemoView::OnEditSelectallsegments() { if (!m_Segment.m_pSegmentData) return; for (int i = 0; i < m_Segment.m_nSegmentsCount ; i++) m_Segment.m_pSegmentData[i].m_bSelected = TRUE ; Invalidate(); } void CDemoView::OnEditDeselectallsegments() { if (!m_Segment.m_pSegmentData) return; for (int i = 0; i < m_Segment.m_nSegmentsCount ; i++) m_Segment.m_pSegmentData[i].m_bSelected = FALSE ; if (m_Segment.m_nCurrentSelection != -1) m_Segment.m_nPreviousSelection = m_Segment.m_nCurrentSelection; m_Segment.m_nCurrentSelection = -1; } void CDemoView::OnUpdateEditSelectallsegments(CCmdUI* pCmdUI) { if (m_Segment.m_pSegmentData) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } void CDemoView::OnEditDeleteselectedsegments() { if (!m_Segment.m_pSegmentData) return; int limit = m_Segment.m_nSegmentsCount; for (int i = 0; i < limit; i++) { if (m_Segment.m_pSegmentData[i].m_bSelected) { m_Segment.m_pSegmentData[i].m_bSelected = FALSE; L_INT nRet = m_Segment.MrcDeleteSegment(i); --m_Segment.m_nSegmentsCount; } } //OnSegmentationRefreshsegments(); m_Segment.ReEnumerateSegments(this); OnEditDeselectallsegments(); Invalidate(TRUE); } void CDemoView::OnUpdateEditDeleteselectedsegments(CCmdUI* pCmdUI) { if (m_Segment.m_pSegmentData) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } void CDemoView::OnUpdateEditDeselectallsegments(CCmdUI* pCmdUI) { if (m_Segment.m_pSegmentData) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); Invalidate(TRUE); } L_VOID CheckRectCoordinates(L_INT nLineSel,RECT* pRect) { // 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; } } L_VOID FindSelectionLine(LUserBitmapWindow* pbitmap, RECT* pRect, POINT pt, L_INT& nLineSel) { RECT rcSelected; L_INT x1, x2, y1, y2; L_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); InflateRect(&rcSelected, 3, 3); ChangeToZoom(pbitmap, &rcSelected); if (PtInRect(&rcSelected, pt)) { 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); InflateRect(&rcSelected, 3, 3); ChangeToZoom(pbitmap, &rcSelected); if (PtInRect(&rcSelected, pt)) { 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); InflateRect(&rcSelected, 3, 3); ChangeToZoom(pbitmap, &rcSelected); if (PtInRect(&rcSelected, pt)) { 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); InflateRect(&rcSelected, 3, 3); ChangeToZoom(pbitmap, &rcSelected); if (PtInRect(&rcSelected, pt)) { 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); InflateRect(&rcSelected, 3, 3); ChangeToZoom(pbitmap, &rcSelected); if (PtInRect(&rcSelected, pt)) { 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); InflateRect(&rcSelected, 3, 3); ChangeToZoom(pbitmap, &rcSelected); if (PtInRect(&rcSelected, pt)) { 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); InflateRect(&rcSelected, 3, 3); ChangeToZoom(pbitmap, &rcSelected); if (PtInRect(&rcSelected, pt)) { 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); InflateRect(&rcSelected, 3, 3); ChangeToZoom(pbitmap, &rcSelected); if (PtInRect(&rcSelected, pt)) { nLineSel = RESIZE_RIGHT_BOTTOM; bFind = TRUE; } if (!bFind) { ChangeToZoom(pbitmap, pRect); if (PtInRect(pRect, pt)) nLineSel = RESIZE_SEGMENT; } ChangeToImage(pbitmap, pRect); InflateRect(pRect, 3, 3); } L_BOOL CDemoView::SelectSegment(CPoint point) { RECT rcDest; RECT rcSegment ; L_BOOL bDeselectAll = TRUE; L_BOOL bRet = FALSE ; m_LBitmapWnd.GetRects(NULL,NULL,&rcDest,NULL); if (rcDest.right <= point.x || rcDest.bottom <= point.y) return TRUE; // if you are selecting a segment rcSegment ; POINT pt; pt.x = point.x; pt.y = point.y; for (int k = 0; k < m_Segment.m_nSegmentsCount; k++) { CopyRect(&rcSegment,&m_Segment.m_pSegmentData[k].m_Data.rcBitmapSeg); ChangeToZoom(&m_LBitmapWnd, &rcSegment); OffsetRect(&rcSegment,rcDest.left,rcDest.top); if(PtInRect(&rcSegment,pt)) { if(!(GetAsyncKeyState(VK_CONTROL) & 0x8000)) { OnEditDeselectallsegments(); bRet = TRUE; } m_Segment.m_pSegmentData[k].m_bSelected = TRUE; if (m_Segment.m_nCurrentSelection != -1) m_Segment.m_nPreviousSelection = m_Segment.m_nCurrentSelection; m_Segment.m_nCurrentSelection = k ; bDeselectAll = FALSE; } } if (bDeselectAll) { OnEditDeselectallsegments(); bRet = TRUE; } return bRet ; } L_VOID CDemoView::SelectSegment(L_INT nID) { for(L_INT i = 0 ; i < m_Segment.m_nSegmentsCount ; i ++ ) m_Segment.m_pSegmentData[i].m_bSelected = FALSE ; m_Segment.m_pSegmentData[nID].m_bSelected = TRUE; } void CDemoView::OnRButtonDown(UINT nFlags, CPoint point) { //SelectSegment(point); CView::OnRButtonDown(nFlags, point); } void CDemoView::OnLButtonDown(UINT nFlags, CPoint point) { RECT rcDest, rcSeg; L_FLOAT fFactor ; POINT pt; m_ptStart.x = point.x; m_ptStart.y = point.y; m_ptPrev.x = point.x ; m_ptPrev.y = point.y ; m_LBitmapWnd.GetRects(NULL,NULL,&rcDest,NULL); if (rcDest.right <= m_ptStart.x || rcDest.bottom <= m_ptStart.y) return; pt.x = point.x; pt.y = point.y; fFactor = m_LBitmapWnd.GetZoomPercent() / 100.0f; m_ptClickMove.x = (L_INT)(point.x / fFactor); m_ptClickMove.y = (L_INT)(point.y / fFactor); m_bLBDown = TRUE; if (m_bManualDraw) // if you are drawing manually { SetCapture(); m_ptEnd.x = point.x; m_ptEnd.y = point.y; CView::OnLButtonDown(nFlags, point); return; } // selecting a segment: SelectSegment(point); // resizing(or moving): if ( m_Segment.m_pSegmentData && m_Segment.m_nCurrentSelection != -1) { pt.x += abs(rcDest.left) ; pt.y += abs(rcDest.top) ; m_nLineSel = RESIZE_NOTHING; CopyRect(&rcSeg, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); CopyRect(&m_rcPrev, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); // Find the selection line FindSelectionLine(&m_LBitmapWnd, &rcSeg, pt, m_nLineSel); } CView::OnLButtonDown(nFlags, point); } void CDemoView::OnMouseMove(UINT nFlags, CPoint point) { POINT ptResize; HPEN hPen, hOldPen; HBRUSH hBrush, hOldBrush; L_INT nOldMode; RECT rcDest; HDC hdc; L_INT nActX, nActY; L_BOOL bEnum = FALSE; RECT Rect,rc; L_FLOAT fFactor; if (m_Segment.m_pSegmentData) SetMouseCursor(point); if (!m_bLBDown) return; m_LBitmapWnd.GetRects(NULL,NULL,&rcDest,NULL); if (rcDest.right <= point.x ) point.x = rcDest.right ; if ( rcDest.bottom <= point.y) point.y = rcDest.bottom; // if you are drawing manually if (m_bManualDraw) { SetCursor(LoadCursor(NULL,IDC_CROSS)); if ((nFlags & MK_LBUTTON) == MK_LBUTTON) { if (rcDest.right <= m_ptStart.x || rcDest.bottom <= m_ptStart.y) return; hPen = CreatePen(PS_SOLID, 1, RGB(255,0,0)); hBrush = GetStockBrush(NULL_BRUSH); if (!hPen) { return; } hdc = GetDC()->GetSafeHdc(); hOldPen = SelectPen(hdc, hPen); hOldBrush = SelectBrush(hdc, hBrush); nOldMode = SetROP2(hdc, R2_NOTXORPEN); Rectangle(hdc, m_ptStart.x, m_ptStart.y, m_ptPrev.x, m_ptPrev.y); Rectangle(hdc, m_ptStart.x, m_ptStart.y, min(rcDest.right, point.x) , min(rcDest.bottom, point.y)); m_ptPrev.x = min(rcDest.right, point.x) ; m_ptPrev.y = min(rcDest.bottom, point.y); SelectObject(hdc, hOldPen); SelectObject(hdc, hOldBrush); SetROP2(hdc, nOldMode); ::ReleaseDC(m_hWnd,hdc); DeleteObject(hPen); DeleteObject(hBrush); } CView::OnMouseMove(nFlags, point); return; // manual drawing ended. } else //RESIZING { if ((nFlags & MK_LBUTTON) == MK_LBUTTON) { if ((!m_Segment.m_pSegmentData) || (m_Segment.m_nCurrentSelection == -1)) { CView::OnMouseMove(nFlags, point); return; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_bMoving = TRUE; if (point.x >= rcDest.right) point.x = rcDest.right ; if (point.y >= rcDest.bottom) point.y = rcDest.bottom; if (point.x <= 0) point.x = 0; if (point.y <= 0) point.y = 0; fFactor = (L_FLOAT)(m_LBitmapWnd.GetZoomPercent() / 100.0f); ptResize = point; point.x += abs(rcDest.left); point.y += abs(rcDest.top); nActX = min((L_INT)(ceil((L_FLOAT)point.x / fFactor)), m_LBitmapWnd.GetWidth()); nActY = min((L_INT)(ceil((L_FLOAT)point.y / fFactor)), m_LBitmapWnd.GetHeight()); // Find the segment rect CopyRect(&Rect, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); ChangeToZoom(&m_LBitmapWnd, &Rect); switch(m_nLineSel) { case RESIZE_TOP: // top if (point.y > Rect.bottom) { bEnum = TRUE; break; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.top = nActY; break; case RESIZE_BOTTOM: // down if (point.y < Rect.top) { bEnum = TRUE; break; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.bottom = nActY; break; case RESIZE_LEFT: // left if (point.x > Rect.right) { bEnum = TRUE; break; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.left = nActX; break; case RESIZE_RIGHT: // right if (point.x < Rect.left) { bEnum = TRUE; break; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.right = nActX; break; case RESIZE_LEFT_TOP: // left - top if ((point.x > Rect.right) || (point.y > Rect.bottom)) { bEnum = TRUE; break; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.left = nActX; m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.top = nActY; break; case RESIZE_RIGHT_TOP: // right - top if ((point.x < Rect.left) || (point.y > Rect.bottom)) { bEnum = TRUE; break; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.right = nActX; m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.top = nActY; break; case RESIZE_LEFT_BOTTOM: // left - bottom if ((point.x > Rect.right) || (point.y < Rect.top)) { bEnum = TRUE; break; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.left = nActX; m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.bottom = nActY; break; case RESIZE_RIGHT_BOTTOM: // right - bottom if ((point.x < Rect.left) || (point.y < Rect.top)) { bEnum = TRUE; break; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.right = nActX; m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.bottom = nActY; break; case RESIZE_SEGMENT: CopyRect(&rc, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); nActX = (L_INT)(ptResize.x / fFactor); nActY = (L_INT)(ptResize.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_LBitmapWnd.GetWidth()) || (rc.bottom > m_LBitmapWnd.GetHeight()) || (rc.left < 0) || (rc.top < 0)) { if (rc.right > m_LBitmapWnd.GetWidth()) { rc.left -= (rc.right - m_LBitmapWnd.GetWidth()); rc.right = m_LBitmapWnd.GetWidth(); } else if (rc.left < 0) { rc.right -= rc.left; rc.left = 0; } if (rc.bottom > m_LBitmapWnd.GetHeight()) { rc.top -= (rc.bottom - m_LBitmapWnd.GetHeight()); rc.bottom = m_LBitmapWnd.GetHeight(); } else if (rc.top < 0) { rc.bottom -= rc.top; rc.top = 0; } } m_ptClickMove.x = nActX; m_ptClickMove.y = nActY; CopyRect(&m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg, &rc); ChangeToZoom(&m_LBitmapWnd, &rc); InflateRect(&rc, 4, 4); CopyRect(&rc, &Rect); break; } ChangeToImage(&m_LBitmapWnd, &Rect); //BEGIN: drawing moved ( or resized) rectangle +++// hPen = CreatePen(PS_SOLID, 1, RGB(255,0,0)); hBrush = GetStockBrush(NULL_BRUSH); if (!hPen ) { return; } hdc = GetDC()->GetSafeHdc(); hOldPen = SelectPen(hdc, hPen); hOldBrush = SelectBrush(hdc, hBrush); nOldMode = SetROP2(hdc, R2_NOTXORPEN); RECT rcTemp; CopyRect(&rcTemp,&Rect); ChangeToZoom(&m_LBitmapWnd, &m_rcPrev); ChangeToZoom(&m_LBitmapWnd, &Rect); OffsetRect(&m_rcPrev,rcDest.left,rcDest.top); OffsetRect(&Rect,rcDest.left,rcDest.top); Rectangle(hdc, m_rcPrev.left,m_rcPrev.top,m_rcPrev.right,m_rcPrev.bottom); Rectangle(hdc, Rect.left,Rect.top,Rect.right,Rect.bottom); CopyRect(&m_rcPrev,&rcTemp); SelectObject(hdc, hOldPen); SelectObject(hdc, hOldBrush); SetROP2(hdc, nOldMode); ::ReleaseDC(m_hWnd,hdc); DeleteObject(hPen); //END : drawing moved ( or resized) rectangle ---// } } if (m_Segment.m_pSegmentData) SetMouseCursor(point); } void CDemoView::OnLButtonUp(UINT nFlags, CPoint point) { L_INT nRet = FAILURE ; CSegmentType dlgSegmentType ; RECT rcDest; m_LBitmapWnd.GetRects(NULL,NULL,&rcDest,NULL); m_bLBDown = FALSE; if (m_bManualDraw) { if (GetCapture() == this) ReleaseCapture(); else return; m_ptEnd.x = min(rcDest.right, point.x); m_ptEnd.y = min(rcDest.bottom, point.y); m_ptEnd.x = max( 0, m_ptEnd.x); m_ptEnd.y = max( 0, m_ptEnd.y); RECT rcSegDest; SetRect(&rcSegDest, m_ptStart.x + abs(rcDest.left), m_ptStart.y + abs(rcDest.top), m_ptEnd.x + abs(rcDest.left), m_ptEnd.y + abs(rcDest.top)); // ignore 'zero'-size rectangles if ((m_ptStart.x == m_ptEnd.x) || (m_ptStart.y == m_ptEnd.y)) return; NormalizeRect(&rcSegDest); ChangeToImage(&m_LBitmapWnd,&rcSegDest); OnEditDeselectallsegments(); // begin: manual creation of a new segment +++++++++++++++++++++++++++++++++++++++// if (!m_Segment.m_pSegmentData) m_Segment.m_pSegmentData = (SEGMENTDATACHILD*)GlobalAllocPtr( GHND, sizeof(SEGMENTDATACHILD)*(++m_Segment.m_nSegmentsCount)); else m_Segment.m_pSegmentData = (SEGMENTDATACHILD*)GlobalReAllocPtr( m_Segment.m_pSegmentData, sizeof(SEGMENTDATACHILD)*(++m_Segment.m_nSegmentsCount), GHND); memset(&m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1],0, sizeof(m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1])); m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1].m_bSelected = TRUE; CopyRect(&m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1].m_Data.rcBitmapSeg, &rcSegDest); m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1].m_Data.uStructSize = sizeof(m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1].m_Data) ; m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1].m_Data.uType = SEGTYPE_BACKGROUND; nRet = m_Segment.MrcCreateNewSegment(&m_LBitmapWnd, &m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1].m_Data ); // end : manual creation of a new segment ---------------------------------------// if (nRet < 0) { MessageBox(TEXT("Error: could not create a new segment or mismatch in segment indexing!"), TEXT("Manual segment error"), MB_OK|MB_ICONERROR); --m_Segment.m_nSegmentsCount; m_LBitmapWnd.Repaint(); return; } if (dlgSegmentType.DoModal() == IDOK) { m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1].m_Data.uType = dlgSegmentType.uType ; nRet = m_Segment.MrcSetSegmentData(&m_LBitmapWnd,nRet,&m_Segment.m_pSegmentData[m_Segment.m_nSegmentsCount-1].m_Data); Invalidate(); } else { m_Segment.MrcDeleteSegment(m_Segment.m_nSegmentsCount-1); m_Segment.m_pSegmentData = (SEGMENTDATACHILD*)GlobalReAllocPtr( m_Segment.m_pSegmentData, sizeof(SEGMENTDATACHILD)*(--m_Segment.m_nSegmentsCount), GHND); Invalidate(); } } else { if(m_Segment.m_pSegmentData && m_Segment.m_nCurrentSelection != -1 ) { //if you have been resizing (or moving) if(m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_bMoving == TRUE) { m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_bMoving = FALSE ; m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.uStructSize = sizeof(m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data); int nRet = m_Segment.MrcSetSegmentData(&m_LBitmapWnd,m_Segment.m_nCurrentSelection, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data); if (nRet != SUCCESS) { MessageBox(TEXT("Error: an error occured when setting segment's position!"), TEXT("Error setting segment's data!"), MB_OK); return; } // updating segments on the bitmap OnSegmentationRefreshsegments(); if(m_bImportedSegments) SelectSegment(m_Segment.m_nCurrentSelection); else SelectSegment(point); Invalidate(TRUE); } else { if((GetAsyncKeyState(VK_CONTROL) & 0x8000) || m_bCTRLKeyUp) { m_bCTRLKeyUp = FALSE; Invalidate(TRUE); CView::OnLButtonUp(nFlags, point); return; } if (m_Segment.m_nCurrentSelection != -1) { RECT rcInvalid; CopyRect(&rcInvalid,&m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); ChangeToZoom(&m_LBitmapWnd,&rcInvalid); OffsetRect(&rcInvalid,rcDest.left,rcDest.top); InflateRect(&rcInvalid,4,4); InvalidateRect(&rcInvalid,TRUE); } if (m_Segment.m_nPreviousSelection != -1) { RECT rcInvalidPrev; CopyRect(&rcInvalidPrev,&m_Segment.m_pSegmentData[m_Segment.m_nPreviousSelection].m_Data.rcBitmapSeg); ChangeToZoom(&m_LBitmapWnd,&rcInvalidPrev); OffsetRect(&rcInvalidPrev,rcDest.left,rcDest.top); InflateRect(&rcInvalidPrev,4,4); InvalidateRect(&rcInvalidPrev,TRUE); } } } } CView::OnLButtonUp(nFlags, point); } void CDemoView::OnSegmentationDrawDrawsegment() { // Start the segmentation process L_INT nRet = m_Segment.Start(&m_LBitmapWnd, pTheApp->Options.CLR_crBackground, pTheApp->Options.CLR_crForeground); if(nRet != SUCCESS) { MessageBox( TEXT("ERR: could not start segmentation!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } m_bManualDraw = TRUE ; } void CDemoView::OnSegmentationDrawStopdrawing() { // end the segmentation process m_bManualDraw = FALSE ; OnEditDeselectallsegments(); Invalidate(TRUE); } void CDemoView::OnUpdateSegmentationDrawDrawsegment(CCmdUI* pCmdUI) { if (m_bManualDraw) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateSegmentationDrawStopdrawing(CCmdUI* pCmdUI) { if (m_bManualDraw) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } void CDemoView::OnFileExportsegments() { HCURSOR hOldCur; L_INT nRet; OPENFILENAME OpenFileName; L_TCHAR szOpenFileFilter[] = TEXT("Segments files\0")TEXT("*.sgm\0"); L_TCHAR szFileName[MAX_PATH]; L_INT nStrLen; L_TCHAR L_FAR* pTemp; memset(&OpenFileName, 0, sizeof(OPENFILENAME)); memset(szFileName, 0, MAX_PATH); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = 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; nStrLen = lstrlen(OpenFileName.lpstrFile); pTemp = OpenFileName.lpstrFile + nStrLen - 4; if (_tcsicmp(pTemp, TEXT(".sgm")) != 0) lstrcat(OpenFileName.lpstrFile, TEXT(".sgm")); hOldCur = SetCursor(LoadCursor(NULL, IDC_WAIT)); nRet = m_Segment.MrcSaveSegmentation(OpenFileName.lpstrFile); if (nRet != SUCCESS) MessageBox(TEXT("Error exporting segments."), TEXT("Error"), MB_OK); SetCursor(hOldCur); } void CDemoView::OnUpdateFileExportsegments(CCmdUI* pCmdUI) { if (m_Segment.IsStarted()) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } void CDemoView::OnFileImportsegments() { HCURSOR hOldCur; OPENFILENAME OpenFileName; L_TCHAR szOpenFileFilter[] = TEXT("Segments files\0")TEXT("*.sgm\0"); L_TCHAR szFileName[MAX_PATH]; L_INT nRet; memset(szFileName, 0, MAX_PATH); memset(&OpenFileName, 0, sizeof(OPENFILENAME)); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = 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_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if (!GetOpenFileName(&OpenFileName)) return; OnSegmentationClearsegments(); hOldCur = SetCursor(LoadCursor(NULL, IDC_WAIT)); nRet = m_Segment.MrcLoadSegmentation(&m_LBitmapWnd, OpenFileName.lpstrFile); SetCursor(hOldCur); if (nRet != SUCCESS) MessageBox(TEXT("Error importing segments."), TEXT("Error"), MB_OK); m_bImportedSegments = TRUE; m_Segment.ReEnumerateSegments(this,FALSE); ManualSegmentation(); Invalidate(TRUE); } void CDemoView::OnSegmentationStartautoandpreservemanualsegments() { L_INT nRet = FAILURE ; memset(&pTheApp->SegExtOptions,0,sizeof(pTheApp->SegExtOptions)); /* Specify the minimum segment width and height*/ pTheApp->SegExtOptions.uStructSize = sizeof(pTheApp->SegExtOptions); pTheApp->SegExtOptions.uBackGroundThreshold = pTheApp->Options.SEGM_nBkgndThreshold; pTheApp->SegExtOptions.uSegmentQuality = pTheApp->Options.SEGM_nQuality; pTheApp->SegExtOptions.uColorThreshold = pTheApp->Options.SEGM_nColorThreshold; pTheApp->SegExtOptions.uCleanSize = pTheApp->Options.SEGM_nCleanSize; pTheApp->SegExtOptions.uCombineThreshold = pTheApp->Options.SEGM_nCombineThreshold; // do the auto-segmentation BeginWaitCursor(); nRet = m_Segment.MrcSegmentBitmapExt(&m_LBitmapWnd, &pTheApp->SegExtOptions); EndWaitCursor(); if(nRet != SUCCESS) { MessageBox( TEXT("ERR: could not do segmentation!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } m_bImportedSegments = FALSE; // get number of segments generated m_Segment.EnableCallBack(FALSE); BeginWaitCursor(); m_Segment.m_nSegmentsCount = m_Segment.MrcEnumSegments(0); EndWaitCursor(); if (m_Segment.m_nSegmentsCount < 1) { MessageBox( TEXT("ERR: could get segments count!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } // allocate buffer to hold segments data m_Segment.m_pSegmentData = (SEGMENTDATACHILD*)GlobalReAllocPtr(m_Segment.m_pSegmentData, sizeof(SEGMENTDATACHILD)*(m_Segment.m_nSegmentsCount), GHND); if (!m_Segment.m_pSegmentData) { MessageBox( TEXT("ERR: no memory, could not allocate buffer for segments data!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } // get segment data structures m_Segment.EnableCallBack(TRUE); BeginWaitCursor(); nRet = m_Segment.MrcEnumSegments(0); EndWaitCursor(); if (nRet < 1) { MessageBox( TEXT("ERR: could enumerate segments!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return; } // we will not stop the segmentation process now, // because we will need the LSegment class object's handle in other places Invalidate(); } void CDemoView::OnUpdateSegmentationStartautoandpreservemanualsegments(CCmdUI* pCmdUI) { if (m_Segment.IsStarted() && m_Segment.m_pSegmentData) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); if(m_Segment.m_nSegmentsCount == 0) pCmdUI->Enable(FALSE); } void CDemoView::OnPreferencesSegmentationandcompressionoptions() { CAllOptions AllOptionsSheet(TEXT("MRC Segmentation and Compression Options")); if ( AllOptionsSheet.DoModal() == IDOK) { // nothing takes place here, // everything happens in the ::OnOK() event of each page of this sheet. } } void CDemoView::OnColorColorresolution() { LDialogColor DlgColor; COLORRESDLGPARAMS DlgParams; L_INT nRet; DlgColor.SetBitmap(&m_LBitmapWnd); memset ( &DlgParams, 0, sizeof ( COLORRESDLGPARAMS ) ) ; DlgParams.uStructSize = sizeof ( COLORRESDLGPARAMS ) ; DlgParams.uDlgFlagsEx = DLG_COLORRES_SHOW_BITALL | DLG_COLORRES_SHOW_DITHER_ALL | DLG_COLORRES_SHOW_PAL_ALL; DlgParams.uDlgFlags = DLG_COLORRES_SHOW_PREVIEW| DLG_COLORRES_SHOW_ORDER | DLG_COLORRES_SHOW_TOOL_ZOOMLEVEL | DLG_COLORRES_SHOW_OPENPALFILE ; DlgColor.EnableCallBack (FALSE); DlgColor.EnablePreview(TRUE); DlgColor.EnableAutoProcess(TRUE); DlgColor.EnableToolbar(TRUE); DlgColor.SetColorResParams(&DlgParams) ; nRet = DlgColor.DoModalColorRes(GetSafeHwnd()); if( nRet == SUCCESS_DLG_OK ) { // Gets the updated values for the structure DlgColor.GetColorResParams(&DlgParams, sizeof(DlgParams)) ; nRet = m_LBitmapWnd.ColorRes(DlgParams.nBitsPerPixel, DlgParams.uColorResFlags,NULL,DlgParams.hpalCustom,0); if (nRet != SUCCESS) { MessageBox(TEXT("Error changing the color resolution"), TEXT("ERROR"), MB_OK); return; } InvalidateRect(NULL, FALSE); } } void CDemoView::OnColorViewhistogram() { LDialogImage DlgImage; HISTOGRAMDLGPARAMS DlgParams; DlgImage.SetBitmap(&m_LBitmapWnd); memset ( &DlgParams, 0, sizeof ( HISTOGRAMDLGPARAMS ) ) ; DlgParams.uStructSize = sizeof ( HISTOGRAMDLGPARAMS ) ; DlgParams.crBlueChannelPen = RGB (0, 0, 255); DlgParams.crGreenChannelPen = RGB (0, 255, 0); DlgParams.crRedChannelPen = RGB (255, 0, 0); DlgParams.crMasterPen = RGB (0, 0, 0); DlgParams.uDlgFlags = DLG_HISTOGRAM_SHOW_VIEWSTYLE | DLG_HISTOGRAM_USERPENCOLORS ; DlgImage.EnableCallBack(FALSE); DlgImage.EnablePreview(TRUE); DlgImage.EnableAutoProcess(TRUE); DlgImage.EnableToolbar(TRUE); DlgImage.SetHistogramParams(&DlgParams) ; DlgImage.DoModalHistogram(GetSafeHwnd()); // Gets the updated values for the structure DlgImage.GetHistogramParams(&DlgParams, sizeof(DlgParams)) ; } void CDemoView::OnColorUniquecolors() { L_UINT32 uCount; CString msg; L_INT nRet = m_LBitmapWnd.GetColorCount(&uCount); if (nRet ==SUCCESS) { msg.Format(TEXT("Bitmap contains %lu unique colors."), uCount); MessageBox(msg, TEXT("Unique Colors"), MB_OK); } else MessageBox(TEXT("An error occurred while trying to get the bitmap color count."), TEXT("Error"), MB_OK); } void CDemoView::OnFileSaveasleadmrc() { COMPRESSIONOPTIONS CmpOption; SAVEFILEOPTION SaveOpt; FILESAVEFORMAT Formats[2]; OPENFILENAME OpenFileName; L_TCHAR szOpenFileFilter[] = TEXT("ALL\0")TEXT("*.*\0")TEXT("MRC\0")TEXT("*.mrc\0"); SAVEDLGPARAMS FSParm; FILESAVEFORMATBPP MrcSub[1] = {sizeof(FILESAVEFORMATBPP),24, DLG_FF_SAVE_SUB_LEAD_MRC}; FILESAVEFORMATBPP MrcTiffSub[1] = {sizeof(FILESAVEFORMATBPP),24, DLG_FF_SAVE_SUB_TIFF24_LEAD_MRC}; SET_SIZE(&Formats[0]); Formats[0].nFormat = DLG_FF_SAVE_MRC; Formats[0].nBppCount = 1; Formats[0].pFileSaveFormatBpp = MrcSub; SET_SIZE(&Formats[1]); Formats[1].nFormat = DLG_FF_SAVE_TIFF; Formats[1].nBppCount = 1; Formats[1].pFileSaveFormatBpp = MrcTiffSub; memset(&OpenFileName, 0, sizeof(OPENFILENAME)); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = GetSafeHwnd(); OpenFileName.lpstrFile = TEXT(""); OpenFileName.nMaxFile = MAX_PATH; OpenFileName.Flags = OFN_OVERWRITEPROMPT; OpenFileName.lpstrFilter = szOpenFileFilter; OpenFileName.lpstrTitle = TEXT("Save as LEAD MRC"); OpenFileName.nFilterIndex = 0; memset(&FSParm, 0, sizeof(SAVEDLGPARAMS)); SET_SIZE(&FSParm); FSParm.pFileFormats = Formats; FSParm.uFileFormatsCount = 2; FSParm.nQFactor = 2; FSParm.nPageNumber = 1; FSParm.uSaveMulti = TRUE; FSParm.uDlgFlags = DLG_SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS| DLG_SAVE_SHOW_FILEOPTIONS_J2KOPTIONS | DLG_SAVE_SHOW_FILEOPTIONS_MULTIPAGE | DLG_SAVE_SHOW_FILEOPTIONS_QFACTOR; m_LBitmapWnd.DialogFile()->SetOpenFileName(&OpenFileName); m_LBitmapWnd.DialogFile()->SetSaveParams(&FSParm); if(m_LBitmapWnd.DialogFile()->DoModalSave(m_hWnd)!=SUCCESS_DLG_OK) return; m_LBitmapWnd.DialogFile()->GetOpenFileName(&OpenFileName); m_LBitmapWnd.DialogFile()->GetSaveParams(&FSParm, sizeof(FSParm)); memset(&CmpOption, 0, sizeof(CmpOption)); SET_SIZE(&CmpOption); CmpOption.nPictureQFactor = pTheApp->Options.MRC_nQFactor; CmpOption.nMaskCoder = pTheApp->Options.MRC_nMaskCoder; CmpOption.nPictureCoder = pTheApp->Options.MRC_nPictureCoder; CmpOption.nText2BitCoder = pTheApp->Options.MRC_nText2BitCoder; CmpOption.nPictureQFactor = pTheApp->Options.MRC_nQFactor; CmpOption.nGrayscale2BitCoder = pTheApp->Options.MRC_nGrayScale2BitCoder; CmpOption.nGrayscale8BitCoder = pTheApp->Options.MRC_nGrayScaleCoder; CmpOption.nGrayscale8BitFactor = pTheApp->Options.MRC_nQFactorGrayScale; if(pTheApp->Options.MRC_nPictureCoder==MRC_PICTURE_COMPRESSION_LOSSLESS_JPEG || pTheApp->Options.MRC_nPictureCoder==MRC_PICTURE_COMPRESSION_LOSSLESS_CMW) CmpOption.nPictureQFactor = 0; if(pTheApp->Options.MRC_nGrayScaleCoder==MRC_GRAYSCALE_COMPRESSION_LOSSLESS_CMW_8BIT || pTheApp->Options.MRC_nGrayScaleCoder==MRC_GRAYSCALE_COMPRESSION_LOSSLESS_JPEG_8BIT) CmpOption.nGrayscale8BitFactor = 0; memset(&SaveOpt, 0, sizeof(SAVEFILEOPTION)); SET_SIZE(&SaveOpt); LBaseFile::GetDefaultSaveFileOption(&SaveOpt, sizeof(SAVEFILEOPTION)); SaveOpt.PageNumber = FSParm.nPageNumber; SaveOpt.Flags = 0; switch(FSParm.uSaveMulti) { case MULTIPAGE_OPERATION_REPLACE: SaveOpt.Flags|=ESO_REPLACEPAGE; break; case MULTIPAGE_OPERATION_INSERT: SaveOpt.Flags|=ESO_INSERTPAGE; break; case MULTIPAGE_OPERATION_APPEND: SaveOpt.PageNumber = 2; break; case MULTIPAGE_OPERATION_OVERWRITE: SaveOpt.PageNumber = 1; break; } SaveOpt.Passes = FSParm.nPasses; // re-create current segments to be aware of background/foreground colors if (m_Segment.m_pSegmentData) { if (!ManualSegmentation()) return ; } int nRet = m_Segment.MrcSaveBitmap(&m_LBitmapWnd, &CmpOption, OpenFileName.lpstrFile, FSParm.nFormat, &SaveOpt); if (nRet != SUCCESS) { CString sErr; sErr.Format(TEXT("An error occurred while trying to save the bitmap as LEAD MRC: %d"), nRet); MessageBox(sErr, TEXT("Error"), MB_OK); } } void CDemoView::OnFileSaveasmrc() { COMPRESSIONOPTIONS CmpOption; SAVEFILEOPTION SaveOpt; FILESAVEFORMAT Formats[2]; OPENFILENAME OpenFileName; L_TCHAR szOpenFileFilter[] = TEXT("ALL\0")TEXT("*.*\0")TEXT("MRC\0")TEXT("*.mrc\0"); SAVEDLGPARAMS FSParm; FILESAVEFORMATBPP MrcSub[1] = {sizeof(FILESAVEFORMATBPP),24, DLG_FF_SAVE_SUB_MRC}; FILESAVEFORMATBPP MrcTiffSub[1] = {sizeof(FILESAVEFORMATBPP),24, DLG_FF_SAVE_SUB_TIFF24_MRC}; CString sErr; int nRet; SET_SIZE(&Formats[0]); Formats[0].nFormat = DLG_FF_SAVE_MRC; Formats[0].nBppCount = 1; Formats[0].pFileSaveFormatBpp = MrcSub; SET_SIZE(&Formats[1]); Formats[1].nFormat = DLG_FF_SAVE_TIFF; Formats[1].nBppCount = 1; Formats[1].pFileSaveFormatBpp = MrcTiffSub; memset(&OpenFileName, 0, sizeof(OPENFILENAME)); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = GetSafeHwnd(); OpenFileName.lpstrFile = TEXT(""); OpenFileName.nMaxFile = MAX_PATH; OpenFileName.Flags = OFN_OVERWRITEPROMPT; OpenFileName.lpstrFilter = szOpenFileFilter; OpenFileName.lpstrTitle = TEXT("Save as MRC"); OpenFileName.nFilterIndex = 0; memset(&FSParm, 0, sizeof(SAVEDLGPARAMS)); SET_SIZE(&FSParm); FSParm.pFileFormats = Formats; FSParm.uFileFormatsCount = 2; FSParm.nQFactor = 2; FSParm.nPageNumber = 1; FSParm.uSaveMulti = TRUE; FSParm.uDlgFlags = DLG_SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS| DLG_SAVE_SHOW_FILEOPTIONS_J2KOPTIONS | DLG_SAVE_SHOW_FILEOPTIONS_MULTIPAGE | DLG_SAVE_SHOW_FILEOPTIONS_QFACTOR; m_LBitmapWnd.DialogFile()->SetOpenFileName(&OpenFileName); m_LBitmapWnd.DialogFile()->SetSaveParams(&FSParm); if(m_LBitmapWnd.DialogFile()->DoModalSave(m_hWnd)!=SUCCESS_DLG_OK) return; m_LBitmapWnd.DialogFile()->GetOpenFileName(&OpenFileName); m_LBitmapWnd.DialogFile()->GetSaveParams(&FSParm, sizeof(FSParm)); memset(&CmpOption, 0, sizeof(CmpOption)); SET_SIZE(&CmpOption); CmpOption.nMaskCoder = pTheApp->Options.MRC_nMaskCoder; CmpOption.nPictureQFactor = pTheApp->Options.MRC_nQFactor; CmpOption.nPictureCoder = pTheApp->Options.MRC_nPictureCoder; if (CmpOption.nPictureCoder != MRC_PICTURE_COMPRESSION_JPEG) { MessageBox(TEXT("Invalid picture compression, it will be replaced with \"JPEG compression\"."), TEXT("Warning"), MB_OK); CmpOption.nPictureCoder = MRC_PICTURE_COMPRESSION_JPEG; } memset(&SaveOpt, 0, sizeof(SAVEFILEOPTION)); SET_SIZE(&SaveOpt); LBaseFile::GetDefaultSaveFileOption(&SaveOpt, sizeof(SAVEFILEOPTION)); SaveOpt.PageNumber = FSParm.nPageNumber; SaveOpt.Flags = 0; switch(FSParm.uSaveMulti) { case MULTIPAGE_OPERATION_REPLACE: SaveOpt.Flags|=ESO_REPLACEPAGE; break; case MULTIPAGE_OPERATION_INSERT: SaveOpt.Flags|=ESO_INSERTPAGE; break; case MULTIPAGE_OPERATION_APPEND: SaveOpt.PageNumber = 2; break; case MULTIPAGE_OPERATION_OVERWRITE: SaveOpt.PageNumber = 1; break; } SaveOpt.Passes = FSParm.nPasses; // re-create current segments to be aware of background/foreground colors if (m_Segment.m_pSegmentData) { if (!ManualSegmentation()) return ; } nRet = m_Segment.MrcSaveBitmapT44(&m_LBitmapWnd,&CmpOption, OpenFileName.lpstrFile, FSParm.nFormat, &SaveOpt); if (nRet != SUCCESS) { sErr.Format(TEXT("An error occurred while trying to save the bitmap as T44 MRC: %d"), nRet); MessageBox(sErr, TEXT("Error"), MB_OK); } } void CDemoView::OnFileSaveaspdf() { COMPRESSIONOPTIONS CmpOption; SAVEFILEOPTION SaveOpt; OPENFILENAME OpenFileName; L_TCHAR szOpenFileFilter[] = TEXT("ALL\0")TEXT("*.*\0")TEXT("PDF\0")TEXT("*.pdf\0"); SAVEDLGPARAMS FSParm; FILESAVEFORMAT Formats[1]; FILESAVEFORMATBPP MrcSub[1] = {sizeof(FILESAVEFORMATBPP),24, DLG_FF_SAVE_SUB_PDF1_UNCOMPRESSED}; int nRet; CString sErr; SET_SIZE(&Formats[0]); Formats[0].nFormat = DLG_FF_SAVE_PDF; Formats[0].nBppCount = 1; Formats[0].pFileSaveFormatBpp = MrcSub; memset(&OpenFileName, 0, sizeof(OPENFILENAME)); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = GetSafeHwnd(); OpenFileName.lpstrFile = TEXT(""); OpenFileName.nMaxFile = MAX_PATH; OpenFileName.Flags = OFN_OVERWRITEPROMPT; OpenFileName.lpstrFilter = szOpenFileFilter; OpenFileName.lpstrTitle = TEXT("Save as PDF"); OpenFileName.nFilterIndex = 0; memset(&FSParm, 0, sizeof(SAVEDLGPARAMS)); SET_SIZE(&FSParm); FSParm.pFileFormats = Formats; FSParm.uFileFormatsCount = 1; FSParm.nQFactor = 2; FSParm.nPageNumber = 1; FSParm.uSaveMulti = TRUE; m_LBitmapWnd.DialogFile()->SetOpenFileName(&OpenFileName); m_LBitmapWnd.DialogFile()->SetSaveParams(&FSParm); if(m_LBitmapWnd.DialogFile()->DoModalSave(m_hWnd)!=SUCCESS_DLG_OK) return; m_LBitmapWnd.DialogFile()->GetOpenFileName(&OpenFileName); m_LBitmapWnd.DialogFile()->GetSaveParams(&FSParm, sizeof(FSParm)); memset(&CmpOption, 0, sizeof(CmpOption)); SET_SIZE(&CmpOption); CmpOption.nPictureQFactor = pTheApp->Options.PDF_nQFactor ; CmpOption.nMaskCoder = pTheApp->Options.PDF_nMaskCoder ;//+ MRC_PDF_ONEBIT_COMPRESSION_ZIP ; CmpOption.nPictureCoder = pTheApp->Options.PDF_nPictureCoder ;//+ MRC_PDF_PICTURE_COMPRESSION_JPEG; CmpOption.nText2BitCoder = pTheApp->Options.PDF_nText2BitCoder ;//+ MRC_PDF_TEXT_COMPRESSION_ZIP; memset(&SaveOpt, 0, sizeof(SAVEFILEOPTION)); SET_SIZE(&SaveOpt); LBaseFile::GetDefaultSaveFileOption(&SaveOpt, sizeof(SAVEFILEOPTION)); SaveOpt.PageNumber = FSParm.nPageNumber; SaveOpt.Flags = 0; switch(FSParm.uSaveMulti) { case MULTIPAGE_OPERATION_REPLACE: SaveOpt.Flags|=ESO_REPLACEPAGE; break; case MULTIPAGE_OPERATION_INSERT: SaveOpt.Flags|=ESO_INSERTPAGE; break; case MULTIPAGE_OPERATION_APPEND: SaveOpt.PageNumber = 2; break; case MULTIPAGE_OPERATION_OVERWRITE: SaveOpt.PageNumber = 1; break; } SaveOpt.Passes = FSParm.nPasses; nRet = m_Segment.MrcSaveBitmap(&m_LBitmapWnd, &CmpOption, OpenFileName.lpstrFile, FILE_PDF_LEAD_MRC, &SaveOpt); if (nRet != SUCCESS) { sErr.Format(TEXT("An error occurred while trying to save the bitmap as PDF: %d"), nRet); MessageBox(sErr, TEXT("Error"), MB_OK); } } void CDemoView::OnFileSavemultipage() { CArray arrViews; pTheApp->EnumViews(arrViews); CSaveListDlg dlg(arrViews); if (dlg.DoModal() != IDOK) return; COMPRESSIONOPTIONS CmpOption; SAVEFILEOPTION SaveOpt; OPENFILENAME OpenFileName; L_TCHAR szOpenFileFilter[] = TEXT("ALL\0")TEXT("*.*\0")TEXT("TIF\0")TEXT("*.tif\0")TEXT("PDF\0")TEXT("*.pdf\0"); SAVEDLGPARAMS FSParm; FILESAVEFORMAT Formats[1]; FILESAVEFORMATBPP MrcSub[1] = {sizeof(FILESAVEFORMATBPP),24, DLG_FF_SAVE_SUB_TIFF24_LEAD_MRC}; int nRet; CString sErr; LBitmap DummyBitmap; LBitmapList BitmapList; L_UINT uListCount; HSEGMENTATION* SegmentsHandlesList; L_INT i; SET_SIZE(&CmpOption); switch(dlg.m_nFormat) { case 0: MrcSub[0].uSubFormats = DLG_FF_SAVE_SUB_TIFF24_MRC; SET_SIZE(&Formats[0]); Formats[0].nFormat = DLG_FF_SAVE_TIFF; Formats[0].nBppCount = 2; Formats[0].pFileSaveFormatBpp = MrcSub; CmpOption.nPictureCoder = MRC_PICTURE_COMPRESSION_JPEG; CmpOption.nPictureQFactor = pTheApp->Options.MRC_nQFactor; CmpOption.nMaskCoder = pTheApp->Options.MRC_nMaskCoder; OpenFileName.lpstrTitle = TEXT("Save as Standard MRC"); if(pTheApp->Options.MRC_nPictureCoder==MRC_PICTURE_COMPRESSION_LOSSLESS_JPEG || pTheApp->Options.MRC_nPictureCoder==MRC_PICTURE_COMPRESSION_LOSSLESS_CMW) CmpOption.nPictureQFactor = 0; break; case 1: MrcSub[0].uSubFormats = DLG_FF_SAVE_SUB_TIFF24_LEAD_MRC; SET_SIZE(&Formats[0]); Formats[0].nFormat = DLG_FF_SAVE_TIFF; Formats[0].nBppCount = 2; Formats[0].pFileSaveFormatBpp = MrcSub; CmpOption.nPictureQFactor = pTheApp->Options.MRC_nQFactor; CmpOption.nMaskCoder = pTheApp->Options.MRC_nMaskCoder; CmpOption.nPictureCoder = pTheApp->Options.MRC_nPictureCoder; CmpOption.nText2BitCoder = pTheApp->Options.MRC_nText2BitCoder; OpenFileName.lpstrTitle = TEXT("Save as LEAD MRC"); if(pTheApp->Options.MRC_nPictureCoder==MRC_PICTURE_COMPRESSION_LOSSLESS_JPEG || pTheApp->Options.MRC_nPictureCoder==MRC_PICTURE_COMPRESSION_LOSSLESS_CMW) CmpOption.nPictureQFactor = 0; CmpOption.nGrayscale2BitCoder = pTheApp->Options.MRC_nGrayScale2BitCoder; CmpOption.nGrayscale8BitCoder = pTheApp->Options.MRC_nGrayScaleCoder; CmpOption.nGrayscale8BitFactor = pTheApp->Options.MRC_nQFactorGrayScale; if(pTheApp->Options.MRC_nGrayScaleCoder==MRC_GRAYSCALE_COMPRESSION_LOSSLESS_CMW_8BIT || pTheApp->Options.MRC_nGrayScaleCoder==MRC_GRAYSCALE_COMPRESSION_LOSSLESS_JPEG_8BIT) CmpOption.nGrayscale8BitFactor = 0; break; case 2: MrcSub[0].uSubFormats = DLG_FF_SAVE_PDF; SET_SIZE(&Formats[0]); Formats[0].nFormat = DLG_FF_SAVE_PDF; Formats[0].nBppCount = 2; Formats[0].pFileSaveFormatBpp = MrcSub; CmpOption.nPictureQFactor = pTheApp->Options.PDF_nQFactor; CmpOption.nMaskCoder = pTheApp->Options.PDF_nMaskCoder ;//+ MRC_PDF_ONEBIT_COMPRESSION_ZIP; CmpOption.nPictureCoder = pTheApp->Options.PDF_nPictureCoder ;//+ MRC_PDF_PICTURE_COMPRESSION_JPEG; CmpOption.nText2BitCoder = pTheApp->Options.PDF_nText2BitCoder ;//+ MRC_PDF_TEXT_COMPRESSION_ZIP; break; } memset(&OpenFileName, 0, sizeof(OPENFILENAME)); OpenFileName.lStructSize = sizeof(OPENFILENAME); OpenFileName.hwndOwner = GetSafeHwnd(); OpenFileName.lpstrFile = TEXT(""); OpenFileName.nMaxFile = MAX_PATH; OpenFileName.Flags = OFN_OVERWRITEPROMPT; OpenFileName.lpstrFilter = szOpenFileFilter; OpenFileName.nFilterIndex = 0; memset(&FSParm, 0, sizeof(SAVEDLGPARAMS)); SET_SIZE(&FSParm); FSParm.pFileFormats = Formats; FSParm.uFileFormatsCount = 1; FSParm.nQFactor = 2; FSParm.nPageNumber = 1; FSParm.uSaveMulti = TRUE; FSParm.uDlgFlags = DLG_SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS | DLG_SAVE_SHOW_FILEOPTIONS_J2KOPTIONS | DLG_SAVE_SHOW_FILEOPTIONS_MULTIPAGE | DLG_SAVE_SHOW_FILEOPTIONS_QFACTOR ; m_LBitmapWnd.DialogFile()->SetOpenFileName(&OpenFileName); m_LBitmapWnd.DialogFile()->SetSaveParams(&FSParm); if(m_LBitmapWnd.DialogFile()->DoModalSave(m_hWnd)!=SUCCESS_DLG_OK) return; m_LBitmapWnd.DialogFile()->GetOpenFileName(&OpenFileName); m_LBitmapWnd.DialogFile()->GetSaveParams(&FSParm, sizeof(FSParm)); BitmapList.Create(); uListCount = arrViews.GetSize(); SegmentsHandlesList = (HSEGMENTATION*) GlobalAllocPtr( GHND, uListCount * sizeof(HSEGMENTATION)); for(i = 0; i < (L_INT)uListCount; i++) { DummyBitmap.Copy(arrViews[i]->m_LBitmapWnd); BitmapList.InsertItem( &DummyBitmap); SegmentsHandlesList[i] = *(arrViews[i]->m_Segment.MrcGetHandle()); DummyBitmap.Free(); } memset(&SaveOpt, 0, sizeof(SAVEFILEOPTION)); SET_SIZE(&SaveOpt); LBaseFile::GetDefaultSaveFileOption(&SaveOpt, sizeof(SAVEFILEOPTION)); SaveOpt.PageNumber = FSParm.nPageNumber; SaveOpt.Flags = 0; switch(FSParm.uSaveMulti) { case MULTIPAGE_OPERATION_REPLACE: SaveOpt.Flags|=ESO_REPLACEPAGE; break; case MULTIPAGE_OPERATION_INSERT: SaveOpt.Flags|=ESO_INSERTPAGE; break; case MULTIPAGE_OPERATION_APPEND: SaveOpt.PageNumber = 2; break; case MULTIPAGE_OPERATION_OVERWRITE: SaveOpt.PageNumber = 1; break; } SaveOpt.Passes = FSParm.nPasses; if(dlg.m_nFormat == 2) FSParm.nFormat = FILE_PDF_LEAD_MRC; nRet = m_Segment.MrcSaveBitmapList(SegmentsHandlesList, uListCount, BitmapList.GetHandle(), &CmpOption, OpenFileName.lpstrFile, FSParm.nFormat); BitmapList.Destroy(); if (nRet != SUCCESS) { switch(dlg.m_nFormat) { case 0: sErr.Format(TEXT("An error occurred while trying to save the bitmap as MRC: %d"), nRet); break; case 1: sErr.Format(TEXT("An error occurred while trying to save the bitmap as LEAD MRC: %d"), nRet); break; case 2: sErr.Format(TEXT("An error occurred while trying to save the bitmap as PDF: %d"), nRet); break; } MessageBox(sErr, TEXT("Error"), MB_OK); } } void CDemoView::OnViewSelectedsegmentUniquecolors() { L_INT nRet; RECT rcSeg; LBitmap SegBitmap; CString sColor; L_UINT32 uCount; SegBitmap.Initialize(); ::CopyRect(&rcSeg, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); m_LBitmapWnd.RectToBitmap(TOP_LEFT, &rcSeg); nRet = SegBitmap.CopyRect(m_LBitmapWnd, rcSeg); if (nRet != SUCCESS) { MessageBox(TEXT("could not get this segment's rectangle!"), TEXT("Error"), MB_OK); return; } nRet = SegBitmap.GetColorCount(&uCount); if (nRet != SUCCESS) { MessageBox(TEXT("An error occurred while trying to get this segment's color count"), TEXT("Error"), MB_OK); return; } sColor.Format(TEXT("this segment contains %lu unique colors."), uCount); MessageBox(sColor, TEXT("Segment'sUnique Colors"), MB_OK); SegBitmap.Free(); } void CDemoView::OnViewSelectedsegmentShowhistogram() { L_INT nRet; RECT rcSeg; LBitmap SegBitmap; LDialogImage DlgImage; HISTOGRAMDLGPARAMS DlgParams; SegBitmap.Initialize(); ::CopyRect(&rcSeg, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); m_LBitmapWnd.RectToBitmap(TOP_LEFT, &rcSeg); nRet = SegBitmap.CopyRect(m_LBitmapWnd, rcSeg); if (nRet != SUCCESS) { MessageBox(TEXT("could not get this segment's rectangle!"), TEXT("Error"), MB_OK); return; } DlgImage.SetBitmap(&SegBitmap); memset ( &DlgParams, 0, sizeof ( HISTOGRAMDLGPARAMS ) ) ; DlgParams.uStructSize = sizeof ( HISTOGRAMDLGPARAMS ) ; DlgParams.crBlueChannelPen = RGB (0, 0, 255); DlgParams.crGreenChannelPen = RGB (0, 255, 0); DlgParams.crRedChannelPen = RGB (255, 0, 0); DlgParams.crMasterPen = RGB (0, 0, 0); DlgParams.uDlgFlags = DLG_HISTOGRAM_SHOW_VIEWSTYLE | DLG_HISTOGRAM_USERPENCOLORS ; DlgImage.EnableCallBack(FALSE); DlgImage.EnablePreview(TRUE); DlgImage.EnableAutoProcess(TRUE); DlgImage.EnableToolbar(TRUE); DlgImage.SetHistogramParams(&DlgParams) ; DlgImage.DoModalHistogram(GetSafeHwnd()); // Gets the updated values for the structure DlgImage.GetHistogramParams(&DlgParams, sizeof(DlgParams)) ; if (nRet != SUCCESS) { MessageBox(TEXT("An error occurred while trying to get this segment's histogram"), TEXT("Error"), MB_OK); return; } SegBitmap.Free(); } void CDemoView::OnViewSelectedsegmentShowtype() { CSegmentType dlgSegmentType(m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.uType); if (dlgSegmentType.DoModal() == IDOK) { m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.uType = dlgSegmentType.uType ; int nRet = m_Segment.MrcSetSegmentData(&m_LBitmapWnd,m_Segment.m_nCurrentSelection,&m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data); } } void CDemoView::OnViewSelectedsegmentShowproperties() { CSegmentProperties dlgSegmentProperties (m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.left, m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.top, m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.right, m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.bottom, m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.uType); if (dlgSegmentProperties.DoModal() == IDOK) { if(dlgSegmentProperties.m_nLeft < 0 || dlgSegmentProperties.m_nLeft >= m_LBitmapWnd.GetWidth()|| dlgSegmentProperties.m_nTop < 0 || dlgSegmentProperties.m_nTop >= m_LBitmapWnd.GetHeight()|| dlgSegmentProperties.m_nRight <= 0 || dlgSegmentProperties.m_nRight > m_LBitmapWnd.GetWidth()|| dlgSegmentProperties.m_nBottom <= 0 || dlgSegmentProperties.m_nBottom > m_LBitmapWnd.GetHeight()) { MessageBox(TEXT("Invalid coordinates!"),TEXT("Error"),MB_OK); return; } m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.left = dlgSegmentProperties.m_nLeft; m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.top = dlgSegmentProperties.m_nTop; m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.right = dlgSegmentProperties.m_nRight; m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg.bottom = dlgSegmentProperties.m_nBottom; NormalizeRect(&m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); int nRet = m_Segment.MrcSetSegmentData(&m_LBitmapWnd,m_Segment.m_nCurrentSelection,&m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data); Invalidate(TRUE); } } void CDemoView::OnUpdateViewSelectedsegmentShowhistogram(CCmdUI* pCmdUI) { if (m_Segment.m_nCurrentSelection == -1 || !m_Segment.m_pSegmentData) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateViewSelectedsegmentShowproperties(CCmdUI* pCmdUI) { if (m_Segment.m_nCurrentSelection == -1 || !m_Segment.m_pSegmentData) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateViewSelectedsegmentShowtype(CCmdUI* pCmdUI) { if (m_Segment.m_nCurrentSelection == -1 || !m_Segment.m_pSegmentData) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateViewSelectedsegmentUniquecolors(CCmdUI* pCmdUI) { if (m_Segment.m_nCurrentSelection == -1 || !m_Segment.m_pSegmentData) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); } void CDemoView::OnViewSelectedsegmentCombinesegments() { L_INT i, nRet; L_INT* pnSelectedSegmentsIDs = NULL; L_INT nSelectedSegmentsCount = 0; LUserSegment SegmentCopy; for (i = 0; i < m_Segment.m_nSegmentsCount; i++) { if(!m_Segment.m_pSegmentData[i].m_bSelected) continue; if (pnSelectedSegmentsIDs) pnSelectedSegmentsIDs = (L_INT*)GlobalReAllocPtr(pnSelectedSegmentsIDs, ++nSelectedSegmentsCount * sizeof(L_INT), GHND); else pnSelectedSegmentsIDs = (L_INT*)GlobalAllocPtr( GHND, ++nSelectedSegmentsCount * sizeof(L_INT)); pnSelectedSegmentsIDs[nSelectedSegmentsCount-1] = i ; } if (nSelectedSegmentsCount < 2) { MessageBox(TEXT("At least two segments has to be selected to combine!"), TEXT("Error"), MB_OK); return; } // Before update the segment, take a copy for undo nRet = SegmentCopy.Stop(); nRet = SegmentCopy.MrcCopySegmentationHandle(m_Segment); for (i = 0; i < nSelectedSegmentsCount - 1; i++ ) { nRet = m_Segment.MrcCombineSegments(pnSelectedSegmentsIDs[i], pnSelectedSegmentsIDs[i+1], pTheApp->Options.COMB_nCombineType, pTheApp->Options.COMB_nCombineFactor); if (nRet != SUCCESS) { MessageBox(L_TEXT("Error combining segments; the segments are of different types or not adjacent.") L_TEXT("\nIf you want to force combining segments of different types") L_TEXT("\ngo to Preferences -> 'Segmentation and Compression Options...'") L_TEXT("\nand change the combining type to \"Force\""), L_TEXT("Error"), MB_OK); // Make the Undo m_Segment.Stop(); m_Segment.MrcCopySegmentationHandle(SegmentCopy); SegmentCopy.Stop(); break; } } GlobalFreePtr(pnSelectedSegmentsIDs); m_Segment.ReEnumerateSegments(this); Invalidate(TRUE); } L_BOOL 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; } L_BOOL 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; } L_BOOL 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; } L_BOOL 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 CDemoView::OnViewSelectedsegmentEnlargesegment() { SEGMENTDATA SegmentData; L_INT i, nRet, nLeft, nRight, nTop, nBottom; LPRECT pSelRect, pRect; L_INT* pnSelectedSegmentsIDs = NULL; L_INT nSelectedSegmentsCount = 0; L_INT nSelIndex; LUserSegment SegmentCopy; for (i = 0; i < m_Segment.m_nSegmentsCount; i++) { if(!m_Segment.m_pSegmentData[i].m_bSelected) continue; if (pnSelectedSegmentsIDs) pnSelectedSegmentsIDs = (L_INT*)GlobalReAllocPtr(pnSelectedSegmentsIDs, ++nSelectedSegmentsCount * sizeof(L_INT), GHND); else pnSelectedSegmentsIDs = (L_INT*)GlobalAllocPtr( GHND, ++nSelectedSegmentsCount * sizeof(L_INT)); pnSelectedSegmentsIDs[nSelectedSegmentsCount-1] = i ; } if (nSelectedSegmentsCount != 1) { MessageBox(TEXT("Enlarging works on one and only one segment!"), TEXT("Error"), MB_OK); return; } nSelIndex = pnSelectedSegmentsIDs[0]; GlobalFreePtr(pnSelectedSegmentsIDs); nLeft = 0; nRight = m_LBitmapWnd.GetWidth(); nTop = 0; nBottom = m_LBitmapWnd.GetHeight(); pSelRect = &m_Segment.m_pSegmentData[nSelIndex].m_Data.rcBitmapSeg; for (i = 0; i < m_Segment.m_nSegmentsCount; i++) { if (nSelIndex == i) continue; pRect = &m_Segment.m_pSegmentData[i].m_Data.rcBitmapSeg; // Find the left value if (CanEnlargeFromLeft(pRect, pSelRect)) nLeft = max(nLeft, pRect->right); } pSelRect->left = nLeft; for (i = 0; i < m_Segment.m_nSegmentsCount; i++) { if (nSelIndex == i) continue; pRect = &m_Segment.m_pSegmentData[i].m_Data.rcBitmapSeg; // Find the top value if (CanEnlargeFromTop(pRect, pSelRect)) nTop = max(nTop, pRect->bottom); } pSelRect->top = nTop; for (i = 0; i < m_Segment.m_nSegmentsCount; i++) { if (nSelIndex == i) continue; pRect = &m_Segment.m_pSegmentData[i].m_Data.rcBitmapSeg; // Find the right value if (CanEnlargeFromRight(pRect, pSelRect)) nRight = min(nRight, pRect->left); } pSelRect->right = nRight; for (i = 0; i < m_Segment.m_nSegmentsCount; i++) { if (nSelIndex == i) continue; pRect = &m_Segment.m_pSegmentData[i].m_Data.rcBitmapSeg; // Find the bottom value if (CanEnlargeFromBottom(pRect, pSelRect)) nBottom = min(nBottom, pRect->top); } pSelRect->bottom = nBottom; memcpy(&SegmentData, &m_Segment.m_pSegmentData[nSelIndex].m_Data, sizeof(SEGMENTDATA)); SET_SIZE(&SegmentData); // Before update the segment, take a copy for undo SegmentCopy.Stop(); SegmentCopy.MrcCopySegmentationHandle(m_Segment); nRet = m_Segment.MrcSetSegmentData(&m_LBitmapWnd,nSelIndex,&SegmentData); if (nRet != SUCCESS) { MessageBox(TEXT("Could not update segment."), TEXT("Error"), MB_OK); m_Segment.MrcCopySegmentationHandle(SegmentCopy); } Invalidate(TRUE); } void CDemoView::OnSegmentationRefreshsegments() { if ((!m_Segment.m_pSegmentData) || (!m_Segment.m_nSegmentsCount) || (m_Segment.m_nCurrentSelection == -1)) return; L_INT nRet; SEGMENTDATACHILD TempSegment; memset(&TempSegment,0,sizeof(TempSegment)); CopyRect(&TempSegment.m_Data.rcBitmapSeg, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); TempSegment.m_Data.uStructSize = sizeof(TempSegment.m_Data); TempSegment.m_Data.uType = m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.uType; TempSegment.m_bMoving = m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_bMoving; TempSegment.m_bSelected = m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_bSelected; m_Segment.ReEnumerateSegments(this); nRet = m_Segment.MrcSetSegmentData(&m_LBitmapWnd,m_Segment.m_nCurrentSelection,&TempSegment.m_Data); if (nRet != SUCCESS) { Invalidate(TRUE); } } void CDemoView::OnViewSelectedsegmentShowinnewwindow() { L_INT nRet; RECT rcSeg; LBitmap SegBitmap; L_UINT uBuffSize = 0 ; L_TCHAR* szBuffer = NULL ; SegBitmap.Initialize(); ::CopyRect(&rcSeg, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); m_LBitmapWnd.RectToBitmap(TOP_LEFT, &rcSeg); nRet = SegBitmap.CopyRect(m_LBitmapWnd, rcSeg); if (nRet != SUCCESS) { MessageBox(TEXT("could not get this segment's rectangle!"), TEXT("Error"), MB_OK); return; } m_LBitmapWnd.GetFileName(szBuffer, &uBuffSize); pTheApp->CreateNewBitmapWindow(szBuffer,&SegBitmap); SegBitmap.Free(); } void CDemoView::OnContextMenu(CWnd* pWnd, CPoint point) { BOOL bRet; CMenu menu; CMenu* pContextMenu ; // Load the context menu if((GetAsyncKeyState(VK_CONTROL) & 0x8000) || m_bCTRLKeyUp) bRet = menu.LoadMenu(IDR_CONTEXTMENU_COMBINE); else bRet = menu.LoadMenu(IDR_CONTEXTMENU); // Get the first sub menu (the real menu) pContextMenu = menu.GetSubMenu(0); // Display the context menu for the user bRet = pContextMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON , point.x, point.y, AfxGetMainWnd()); } void CDemoView::OnUpdateViewSelectedsegmentEnlargeSegment(CCmdUI* pCmdUI) { if (m_Segment.m_nCurrentSelection == -1 || !m_Segment.m_pSegmentData) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateViewSelectedsegmentCombinesegments(CCmdUI* pCmdUI) { if (m_Segment.m_nCurrentSelection == -1 || !m_Segment.m_pSegmentData) { pCmdUI->Enable(FALSE); return; } else { int nSelections = 0 ; for (int i = 0 ; i < m_Segment.m_nSegmentsCount ; i++) if (m_Segment.m_pSegmentData[i].m_bSelected) nSelections++; if (nSelections == 2) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } } void CDemoView::OnUpdateViewSelectedsegmentShowinnewwindow(CCmdUI* pCmdUI) { if (m_Segment.m_nCurrentSelection == -1 || !m_Segment.m_pSegmentData) pCmdUI->Enable(FALSE); else pCmdUI->Enable(TRUE); } void CDemoView::OnUpdateSegmentationRefreshsegments(CCmdUI* pCmdUI) { if (m_Segment.m_pSegmentData) pCmdUI->Enable(TRUE); else pCmdUI->Enable(FALSE); } void CDemoView::SetMouseCursor( CPoint point) { RECT rcDest; RECT Rect; if (!m_Segment.m_pSegmentData ) return; if(m_Segment.m_nCurrentSelection == -1) return; //iterating all segments to check whether all segments are selected or not for (int i = 0; i < m_Segment.m_nSegmentsCount ; i++) if (!m_Segment.m_pSegmentData[i].m_bSelected) break; //if all of them are selected; the do nothing if (i == m_Segment.m_nSegmentsCount) return; m_LBitmapWnd.GetRects(NULL,NULL,&rcDest,NULL); if (rcDest.right <= point.x || rcDest.bottom <= point.y) return; //point.x += abs(rcDest.left); //point.y += abs(rcDest.top); // Find the segment rect CopyRect(&Rect, &m_Segment.m_pSegmentData[m_Segment.m_nCurrentSelection].m_Data.rcBitmapSeg); POINT pt; L_INT nLineSelect = RESIZE_NOTHING; pt.x = point.x + abs(rcDest.left); pt.y = point.y + abs(rcDest.top); // Find the selection line FindSelectionLine(&m_LBitmapWnd, &Rect, pt, nLineSelect); ChangeToZoom(&m_LBitmapWnd, &Rect); switch(nLineSelect) { case RESIZE_TOP: // top SetCursor(LoadCursor(NULL,IDC_SIZENS)); break; case RESIZE_BOTTOM: // down SetCursor(LoadCursor(NULL,IDC_SIZENS)); break; case RESIZE_LEFT: // left SetCursor(LoadCursor(NULL,IDC_SIZEWE)); break; case RESIZE_RIGHT: // right SetCursor(LoadCursor(NULL,IDC_SIZEWE)); break; case RESIZE_LEFT_TOP: // left - top SetCursor(LoadCursor(NULL,IDC_SIZENWSE)); break; case RESIZE_RIGHT_TOP: // right - top SetCursor(LoadCursor(NULL,IDC_SIZENESW)); break; case RESIZE_LEFT_BOTTOM: // left - bottom SetCursor(LoadCursor(NULL,IDC_SIZENESW)); break; case RESIZE_RIGHT_BOTTOM: // right - bottom SetCursor(LoadCursor(NULL,IDC_SIZENWSE)); break; case RESIZE_SEGMENT: SetCursor(LoadCursor(NULL,IDC_SIZEALL)); break; } } void CDemoView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default if(nChar == 0x6b || nChar == 0x6d) { RECT rcClient; GetClientRect(&rcClient); SendNotifyMessage( WM_SIZE, SIZE_RESTORED, MAKELPARAM(rcClient.right,rcClient.bottom)); Invalidate(TRUE); } CView::OnKeyDown(nChar, nRepCnt, nFlags); } void CDemoView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) { // TODO: Add your message handler code here and/or call default if (nChar == 0x11) m_bCTRLKeyUp = TRUE; CView::OnKeyUp(nChar, nRepCnt, nFlags); } L_BOOL CDemoView::ManualSegmentation() { SEGMENTDATACHILD TempSegment; CString sError; L_INT nRet; nRet = m_Segment.Stop(); nRet = m_Segment.Start(&m_LBitmapWnd, pTheApp->Options.CLR_crBackground, pTheApp->Options.CLR_crForeground); if (nRet != SUCCESS) { sError.Format(TEXT("An error occurred while trying to initialize segmentation process: %d"), nRet); MessageBox(sError, TEXT("Error"), MB_OK); return FALSE; } for (L_INT nID = 0; nID < m_Segment.m_nSegmentsCount; nID++) { CopyRect(&TempSegment.m_Data.rcBitmapSeg, &m_Segment.m_pSegmentData[nID].m_Data.rcBitmapSeg); TempSegment.m_Data.uType = m_Segment.m_pSegmentData[nID].m_Data.uType; TempSegment.m_bSelected = m_Segment.m_pSegmentData[nID].m_bSelected; TempSegment.m_Data.uStructSize = sizeof(TempSegment.m_Data); // Set the new segment nRet = m_Segment.MrcCreateNewSegment(&m_LBitmapWnd, &TempSegment.m_Data); if (nRet < 0) { sError.Format(TEXT("An error occurred while trying to add a new segment: %d."), nRet); MessageBox(sError, TEXT("Error"), MB_OK); //OnSegmentationRefreshsegments(); m_Segment.ReEnumerateSegments(this); Invalidate(TRUE); return FALSE; } } return TRUE; } L_INT LUserSegment::ReEnumerateSegments(CDemoView* pView, L_BOOL bValidation) { if(bValidation) { if ((!m_pSegmentData) || (!m_nSegmentsCount)) return TRUE; } L_INT nCurSel = m_nCurrentSelection; L_INT nRet = FALSE; // get number of segments generated EnableCallBack(FALSE); m_nSegmentsCount = MrcEnumSegments(0); if (m_nSegmentsCount < 1) { MessageBox( pView->GetSafeHwnd(), TEXT("ERR: could get segments count!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return FALSE; } GlobalFreePtr(m_pSegmentData); // allocate buffer to hold segments data m_pSegmentData = (SEGMENTDATACHILD*)GlobalAllocPtr(GHND,sizeof(SEGMENTDATACHILD)*(m_nSegmentsCount)); if (!m_pSegmentData) { MessageBox( pView->GetSafeHwnd(), TEXT("ERR: no memory, could not allocate buffer for segments data!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return FALSE; } // get segment data structures EnableCallBack(TRUE); nRet = MrcEnumSegments(0); if (nRet < 1) { MessageBox( pView->GetSafeHwnd(), TEXT("ERR: could enumerate segments!"), TEXT("ERR"), MB_OK|MB_ICONERROR); return FALSE; } m_nCurrentSelection = nCurSel; return TRUE; }