// JPEG2000Doc.cpp : implementation of the CJPEG2000Doc class // #include "stdafx.h" #include "JPEG2000.h" #include "JPEG2000Doc.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif extern CJPEG2000App theApp; extern FILESAVEFORMAT gaFormats[]; typedef struct _MYRATIO { L_INT nRatio; L_INT nQFactor; } MYRATIO; MYRATIO aJPEGRatios[]= { 4, 2, 7, 5, 10, 10, 12, 15, 14, 20, 16, 25, 18, 30, 20, 35, 22, 40, 24, 45, 25, 50, 26, 55, 28, 60, 30, 65, 31, 70, 32, 75, 33, 80, 34, 85, 35, 90, 36, 95, 37, 100, 38, 105, 39, 110, 40, 115, 41, 120, 42, 125, 43, 130, 44, 135, 45, 140, 46, 145, 47, 150, 48, 155, 49, 160, 50, 165, 51, 170, 52, 175, 53, 180, 54, 185, 55, 190, 56, 195, 57, 200, 58, 205, 59, 210, 60, 215, 61, 220, 62, 225, 63, 230, 64, 235, 65, 240, 66, 245, 67, 250, 68, 255, }; #define JPEGRATIOCOUNT sizeof(aJPEGRatios)/sizeof(MYRATIO) MYRATIO aLEADRatios[]= { 7, 2, 9, 5, 14, 10, 17, 15, 19, 20, 21, 25, 23, 30, 25, 35, 27, 40, 29, 45, 33, 50, 35, 55, 37, 60, 38, 65, 40, 70, 41, 75, 43, 80, 45, 85, 46, 90, 47, 95, 48, 100, 52, 105, 55, 110, 56, 115, 58, 120, 59, 125, 61, 130, 62, 135, 64, 140, 65, 145, 67, 150, 68, 155, 70, 160, 71, 165, 72, 170, 73, 175, 75, 180, 76, 185, 77, 190, 78, 195, 79, 200, 81, 205, 82, 210, 83, 215, 84, 220, 86, 225, 87, 230, 88, 235, 90, 240, 91, 245, 93, 250, 94, 255, }; #define LEADRATIOCOUNT sizeof(aLEADRatios)/sizeof(MYRATIO) ///////////////////////////////////////////////////////////////////////////// // CJPEG2000Doc IMPLEMENT_DYNCREATE(CJPEG2000Doc, CDocument) BEGIN_MESSAGE_MAP(CJPEG2000Doc, CDocument) //{{AFX_MSG_MAP(CJPEG2000Doc) ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs) //}}AFX_MSG_MAP END_MESSAGE_MAP() /*****************************************************************************/ /* /*****************************************************************************/ CJPEG2000Doc::CJPEG2000Doc() { m_pDimensions = NULL; m_DimensionCount = 0; memset(&m_fi, 0, sizeof(FILEINFO)); m_fi.uStructSize = sizeof(FILEINFO); } /*****************************************************************************/ /* /*****************************************************************************/ CJPEG2000Doc::~CJPEG2000Doc() { if(m_pDimensions != NULL) free(m_pDimensions); } /*****************************************************************************/ /* /*****************************************************************************/ BOOL CJPEG2000Doc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // add reinitialization code here // (SDI documents will reuse this document) return TRUE; } /*****************************************************************************/ /* /*****************************************************************************/ void CJPEG2000Doc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here } } #ifdef _DEBUG /*****************************************************************************/ /* /*****************************************************************************/ void CJPEG2000Doc::AssertValid() const { CDocument::AssertValid(); } /*****************************************************************************/ /* /*****************************************************************************/ void CJPEG2000Doc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG /*****************************************************************************/ /* /*****************************************************************************/ BOOL CJPEG2000Doc::OnOpenDocument(LPCTSTR lpszPathName) { L_INT nRet; L_SetLoadResolution( FILE_J2K, 800, 600 ); L_InitBitmap(&m_LEADBitmap, sizeof(BITMAPHANDLE), 0, 0, 0); m_fi.uStructSize = sizeof(FILEINFO); nRet = L_FileInfo((LPTSTR)lpszPathName, &m_fi, sizeof(FILEINFO), 0, &theApp.m_lfo); m_csCurrentFilename = lpszPathName; if( IS_MULTIRES_FORMAT(m_fi.Format) ) { nRet = L_ReadLoadResolutions((TCHAR*)lpszPathName, NULL, &m_DimensionCount, NULL); if( SUCCESS == nRet) { m_pDimensions = (pDIMENSION)malloc(sizeof(DIMENSION)* m_DimensionCount); nRet = L_ReadLoadResolutions((TCHAR*)lpszPathName, m_pDimensions, &m_DimensionCount, NULL); if( SUCCESS != nRet) return FALSE; } } else { m_pDimensions = NULL; m_DimensionCount = 0; } if(nRet == SUCCESS) { if(!theApp.m_bPaintWhileLoad) { CRect rcMainFrame; theApp.m_pMainWnd->GetClientRect(&rcMainFrame); L_SetLoadResolution( FILE_J2K, rcMainFrame.Width(), rcMainFrame.Height() ); HCURSOR hCur = SetCursor( LoadCursor(NULL,IDC_WAIT) ); nRet = L_LoadBitmap((LPTSTR) lpszPathName, &m_LEADBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGRORGRAY, &theApp.m_lfo, NULL); if( SUCCESS != nRet) return FALSE; SetCursor( hCur ); } else { m_csFilename = lpszPathName; nRet = SUCCESS; } } if(nRet != SUCCESS) { theApp.DisplayLEADError(nRet); return(FALSE); } return (TRUE); } /*****************************************************************************/ /* /*****************************************************************************/ BOOL CJPEG2000Doc::LoadResolution( CRect rcDstView ) { L_INT nRet; L_BOOL bIsLoad = FALSE; if( (!theApp.m_bPaintWhileLoad) && IS_MULTIRES_FORMAT(m_fi.Format) && (m_LEADBitmap.Flags.Allocated) ) { DIMENSION sDIM, sWND; sDIM.nWidth = m_LEADBitmap.Width; sDIM.nHeight = m_LEADBitmap.Height; sWND.nWidth = rcDstView.Width(); sWND.nHeight = rcDstView.Height(); if( (sWND.nWidth>sDIM.nWidth) || (sWND.nHeight>sDIM.nHeight) ) { if(sDIM.nWidth < m_pDimensions[0].nWidth) { const TCHAR *pName = (LPCTSTR)m_csCurrentFilename; L_SetLoadResolution( FILE_J2K, sWND.nWidth, sWND.nHeight ); L_FreeBitmap(&m_LEADBitmap); HCURSOR hCur = SetCursor( LoadCursor(NULL,IDC_WAIT) ); nRet = L_LoadBitmap( (TCHAR*)pName, &m_LEADBitmap, sizeof(BITMAPHANDLE), 0, ORDER_BGRORGRAY, &theApp.m_lfo, NULL); SetCursor( hCur ); bIsLoad = TRUE; } } } return(bIsLoad); } /*****************************************************************************/ /* /*****************************************************************************/ void CJPEG2000Doc::OnCloseDocument() { m_csCurrentFilename.Empty(); if(m_pDimensions) { free(m_pDimensions); m_pDimensions = NULL; } if(m_LEADBitmap.Flags.Allocated) L_FreeBitmap(&m_LEADBitmap); CDocument::OnCloseDocument(); } /*****************************************************************************/ /* /*****************************************************************************/ void CJPEG2000Doc::OnFileSaveAs() { HCURSOR hCursor; L_INT nRet; theApp.m_FSParm.nBitsPerPixel = m_LEADBitmap.BitsPerPixel; #if(WINVER >= 0x0400) theApp.m_OpenFileName.Flags |= OFN_ENABLESIZING; #endif /* WINVER >= 0x0400 */ theApp.m_FSParm.uStructSize = sizeof(SAVEDLGPARAMS); theApp.m_FSParm.nQFactor = 2; theApp.m_FSParm.nPageNumber = 1; theApp.m_FSParm.uSaveMulti = MULTIPAGE_OPERATION_OVERWRITE; theApp.m_FSParm.uDlgFlags = DLG_SAVE_ENABLESIZING | DLG_SAVE_SHOW_FILEOPTIONS_MULTIPAGE| DLG_SAVE_SHOW_FILEOPTIONS_PROGRESSIVE | DLG_SAVE_SHOW_FILEOPTIONS_QFACTOR | DLG_SAVE_SHOW_FILEOPTIONS_STAMP| DLG_SAVE_SHOW_FILEOPTIONS_BASICJ2KOPTIONS| DLG_SAVE_SHOW_FILEOPTIONS_J2KOPTIONS; nRet = L_DlgSave( theApp.GetMainWnd()->m_hWnd, &theApp.m_OpenFileName, &theApp.m_FSParm ); if(nRet == SUCCESS_DLG_OK) { lstrcpy(theApp.m_szFileName,theApp.m_FSParm.szFileName); if(IS_MULTIRES_FORMAT(theApp.m_FSParm.nFormat)) { if( theApp.m_bEnableROI && (FILE_CMW != theApp.m_FSParm.nFormat) ) { theApp.m_FSParm.FileJ2KOptions.uROIControl = J2K_USE_LEAD_REGION; theApp.m_FSParm.FileJ2KOptions.bUseROI = TRUE; } else { theApp.m_FSParm.FileJ2KOptions.bUseROI = FALSE; } L_SetJ2KOptions( &theApp.m_FSParm.FileJ2KOptions ); } hCursor = SetCursor (LoadCursor (NULL, IDC_WAIT)); theApp.m_SaveFileOption.uStructSize = sizeof(theApp.m_SaveFileOption); theApp.m_SaveFileOption.Passes = theApp.m_FSParm.nPasses; theApp.m_SaveFileOption.Flags = 0; theApp.m_SaveFileOption.PageNumber = theApp.m_FSParm.nPageNumber ; if ( theApp.m_FSParm.bSaveWithStamp ) { theApp.m_SaveFileOption.Flags |= ESO_SAVEWITHSTAMP ; theApp.m_SaveFileOption.StampHeight = theApp.m_FSParm.nStampHeight ; theApp.m_SaveFileOption.StampWidth = theApp.m_FSParm.nStampWidth ; } nRet = L_SaveBitmap((LPTSTR)theApp.m_szFileName, &m_LEADBitmap, theApp.m_FSParm.nFormat, theApp.m_FSParm.nBitsPerPixel, theApp.m_FSParm.nQFactor, &theApp.m_SaveFileOption); if (nRet != SUCCESS) { theApp.DisplayLEADError(nRet); SetCursor (hCursor); return; } else { SetModifiedFlag(FALSE); AddImageToBrowse(theApp.m_szFileName); } OnSaveDocument((LPCTSTR)theApp.m_szFileName); SetCursor (hCursor); } return; } /*****************************************************************************/ /* /*****************************************************************************/ void CJPEG2000Doc::AddImageToBrowse(LPCTSTR lpszFile) { CMainFrame * pMain = (CMainFrame*)AfxGetMainWnd(); if(pMain && pMain->m_bBrowse) { L_TCHAR path[_MAX_PATH]=TEXT(""); L_TCHAR L_FAR *p=NULL; GetFullPathName(lpszFile, _MAX_PATH, (LPTSTR)&path, &p); if(p) { p--; *p=0; } CString cs; cs.Format(TEXT("%s"), path); if(pMain->m_pBrowseDlg->m_szImageDir == cs) { pMain->m_pBrowseDlg->SendMessage(WM_INSERTNEWIMAGE, 0, (LPARAM)lpszFile); } } } /*****************************************************************************/ /* /*****************************************************************************/ BOOL CJPEG2000Doc::OnSaveDocument(LPCTSTR lpszPathName) { SetModifiedFlag(FALSE); return(TRUE); }