📄 dctwaterview.cpp
字号:
{
_close(fh);
GlobalFree(hbit);
sprintf(temp,"Sorry!Failure In Reading File[%s]!",FileName);
MessageBox(temp,"Open the File",MB_ICONSTOP|MB_OK);
return;
}
_close(fh); //Close the file
bitfile=(LPBITMAPFILEHEADER)lpbit;//Get the fileheader
bitinfo=(LPBITMAPINFOHEADER)(lpbit+sizeof(BITMAPFILEHEADER));//Get the info header
/////////Judge the file type starts////////
////If the image is a bmp or not
if(bitfile->bfType!=0x4d42)
{
GlobalFree(hbit);//Free the memory
sprintf(temp,"Sorry!File[%s]Is Not a Bmp File!",FileName);//Get the information
MessageBox(temp,"Open the File",MB_ICONSTOP|MB_OK);//Display the info
return;
}
////If the image is a 256-gray image or not
if(bitinfo->biBitCount!=8)
{
GlobalFree(hbit);//Free the memory
sprintf(temp,"Sorry!File[%s]Is Not A 256-gray Image!",FileName);//Get the information
MessageBox(temp,"Open the File",MB_ICONSTOP|MB_OK);//Display the info
return;
}
////If the image is compressed or not
if(bitinfo->biCompression!=0)
{
GlobalFree(hbit);//Free the memory
sprintf(temp,"Sorry!File[%s]Is A Compressed Image!",FileName);//Get the information
MessageBox(temp,"Open the File",MB_ICONSTOP|MB_OK);//Display the info
return;
}
/////////Judge the file type ends////////
////Get the important information
width=(long int)bitinfo->biWidth;//Width
height=(long int)bitinfo->biHeight;//Height
off=(long int)bitfile->bfOffBits;//Offset
linebytes=(width+3)/4*4;//Bytes in each line of image
sprintf(temp,"off=%ld",bitfile->bfOffBits);
MessageBox(temp);
////Change the default bmp filename into raw filename
offset=strlen(FileTitle);//The filename length
FileTitle[offset-3]='r';//change b to r
FileTitle[offset-2]='a';//change m to a
FileTitle[offset-1]='w';//change p to w
strcpy(FileName,FileTitle);//The name with new ext.
/////Initialize the FileSaveDialog structure begins////
ofn.lStructSize=sizeof(OPENFILENAME); //The structure size
ofn.hwndOwner=m_hWnd; //The Owner window handle
ofn.hInstance=NULL; //None user diloag style
////The filter
ofn.lpstrFilter=TEXT("RAW Image *.raw\0*.raw\0All File *.*\0*.*\0\0");
////None user filter
ofn.lpstrCustomFilter=NULL;
ofn.nMaxCustFilter=0;
ofn.nFilterIndex=1;//Select the item 'RAW Image *.raw'
////Initial the full name without path
//then save the selected filename with full path
ofn.lpstrFile=FileTitle;
ofn.nMaxFile=_MAX_PATH;
////The filename without path
ofn.lpstrFileTitle=FileName;
ofn.nMaxFileTitle=_MAX_PATH;
strcpy(temp,pDoc->m_CurrentDirectory);
strcat(temp,"\\raw");
if(!SetCurrentDirectory(temp))
{
if(!CreateDirectory(temp,NULL))
strcpy(temp,pDoc->m_CurrentDirectory);
}
ofn.lpstrInitialDir=temp;
ofn.lpstrTitle="Save 256-gray RAW Image As"; //The title
ofn.Flags=OFN_CREATEPROMPT|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR; //The flags
ofn.lpstrDefExt="RAW"; //Default extension
ofn.lCustData=NULL; //None
ofn.lpfnHook=NULL; //None
ofn.lpTemplateName=NULL; //None user defined dialog
/////Initialize the FileOpenDialog structure ends////
////If cancle or error,then return
if(GetSaveFileName(&ofn)==0)
{
GlobalFree(hbit);//Free the memory
return;
}
////Search the filename
CFileFind cff;//define a class
////If found
if(cff.FindFile(FileTitle,0))
{
cff.FindNextFile();//Find next
////compare the filename,if equal
if(cff.GetFilePath().CompareNoCase(FileTitle)==0)
{
//Judge if the file attribute is read only
if(cff.IsReadOnly())
{
sprintf(temp,"File [%s] is Read Only,Are You Sure to Cover It?",FileTitle);//Display info
////Confirm to overwrite the read only file
if(MessageBox(temp,"Save the File",MB_YESNO|MB_ICONQUESTION)==IDYES)
SetFileAttributes(FileTitle,FILE_ATTRIBUTE_NORMAL);//Modify the file attribute
////else return
else
{
GlobalFree(hbit);//free Memory
cff.Close();//Close the findfile process
return;
}
}
}
}
////Open the file in WriteOnly Mode
fh=_open(FileTitle,_O_BINARY|_O_WRONLY|_O_CREAT|_O_TRUNC,_S_IREAD|_S_IWRITE);
////If the file cannot be opened or created,return directly
if(fh==-1)
{
GlobalFree(hbit);//Free memory
sprintf(temp,"Sorry! Failed to Open File[%s]",FileTitle);//Get info
MessageBox(temp,"Save the File",MB_ICONSTOP|MB_OK);//Display Info
cff.Close();//End the findfile process
return;
}
////Save file
for(i=0;i<height;i++)
{
////write
if(_write(fh,lpbit+off+(height-i-1)*linebytes,width)==-1)
{
MessageBox("Errors While Saving!","Save the File",MB_ICONSTOP|MB_OK);//If error,display info
break;
}
}
_close(fh);//Close file
GlobalFree(hbit);//Release memory
cff.Close();//end the file search
}
//This function to Transform a raw(256grays and <=640x480)file into Bmpfile
void CDCTWaterView::OnTransformRawbmp()
{
// TODO: Add your command handler code here
CDCTWaterDoc* pDoc = GetDocument();//Get the pDoc
ASSERT_VALID(pDoc);//Set the pDoc valid
OPENFILENAME ofn;//The structure to open a FileDialog
HGLOBAL hbit;//The handle for memory
LPBITMAPFILEHEADER bitfile;//The structures for bmp file header
LPBITMAPINFOHEADER bitinfo;//The structures for bmp info header
////The infomation of bmp
int width;
int height;
int offset;
long int fh;//The file handle
long int i;
unsigned char *lpbit;//The pointer of the file data
char temp[_MAX_PATH+50];//The temperal string
long int lenbit;//The length of the opened file
char FileName[_MAX_PATH],FileTitle[_MAX_PATH];//The Filename and FileTitle
FileName[0]='\0';//NULL
FileTitle[0]='\0';//NULL
/////Initialize the FileOpenDialog structure begins////
ofn.lStructSize=sizeof(OPENFILENAME); //The structure size
ofn.hwndOwner=m_hWnd; //The Owner window handle
ofn.hInstance=NULL; //None User defined
////The filter
ofn.lpstrFilter=TEXT("RAW Image *.raw\0*.raw\0All File *.*\0*.*\0\0");
////None user filter
ofn.lpstrCustomFilter=NULL;
ofn.nMaxCustFilter=0;
ofn.nFilterIndex=1; //Select the item 'RAW Image *.raw'
////The filename with full path
ofn.lpstrFile=FileName;
ofn.nMaxFile=_MAX_PATH;
////The filename without path
ofn.lpstrFileTitle=FileTitle;
ofn.nMaxFileTitle=_MAX_PATH;
strcpy(temp,pDoc->m_CurrentDirectory);
strcat(temp,"\\raw");
if(!SetCurrentDirectory(temp))
{
strcpy(temp,pDoc->m_CurrentDirectory);
}
ofn.lpstrInitialDir=temp;
ofn.lpstrTitle="请打开要转换的256灰度RAW图象"; //The title
ofn.Flags=OFN_FILEMUSTEXIST; //The flags
ofn.nFileOffset; //The offset of the filename in the full path
ofn.nFileExtension; //The offset of the extension in the full path
ofn.lpstrDefExt="RAW"; //Default extension
ofn.lCustData=NULL; //None
ofn.lpfnHook=NULL; //None
ofn.lpTemplateName=NULL; //None User defined dialog
/////Initialize the FileOpenDialog structure ends////
//Open the Dialog, and save the structure including the filename
if(GetOpenFileName(&ofn)==0)return;//If cancle or error,then return
fh=_open(FileName,_O_RDONLY|_O_BINARY);//Open the file in ReadOnly Mode
////If the file cannot be opened,return directly
if(fh==-1)
{
sprintf(temp,"Sorry!无法打开File[%s]!",FileName);//Get the info
MessageBox(temp,"Open the File",MB_ICONSTOP|MB_OK);//Display the info
return;
}
lenbit=_filelength(fh);//Get the length of the opened file
////If the file length is not suitable,return directly either
if(lenbit!=512*512&&lenbit!=640*480&&lenbit!=320*240&&lenbit!=256*256)
{
_close(fh);//Close file
sprintf(temp,"Sorry!File[%s]大小不符合本软件规范!",FileName);//Get Info
MessageBox(temp,"Open the File",MB_ICONSTOP|MB_OK);//Display Info
return;
}
////Get the image width and height
switch(lenbit)
{
case 512*512:
width=height=512;
break;
case 256*256:
width=height=256;
break;
case 640*480:
width=640;
height=480;
break;
case 320*240:
width=320;
height=240;
break;
}
hbit=GlobalAlloc(GMEM_FIXED,lenbit+1078);//Apply the required memory
////If memory is not enough, return directly
if(hbit==NULL)
{
_close(fh); //Close the file
MessageBox("Sorry!Not Enough Memory!","Open the File",MB_ICONSTOP|MB_OK);//Display Info
return;
}
lpbit=(unsigned char *)hbit;//Give the memory pointer to the file data pointer
////Read file into memory
for(i=0;i<height;i++)
{
if(_read(fh,lpbit+1078+(height-i-1)*width,width)==-1)
{
sprintf(temp,"Sorry!Failure In Reading[%s]",FileName);
MessageBox(temp,"Open the File",MB_ICONSTOP|MB_OK);
break;
}
}
//Close the file
_close(fh);
bitfile=(LPBITMAPFILEHEADER)pDoc->m_StandardBmpInfo;
bitinfo=(LPBITMAPINFOHEADER)(pDoc->m_StandardBmpInfo+sizeof(BITMAPFILEHEADER));
bitfile->bfSize=lenbit+1078;
bitinfo->biHeight=height;
bitinfo->biWidth=width;
//Change the default raw filename into bmp filename
offset=strlen(FileTitle);
FileTitle[offset-3]='b';
FileTitle[offset-2]='m';
FileTitle[offset-1]='p';
strcpy(FileName,FileTitle);
/////Initialize the FileSaveDialog structure begins////
//The structure size
ofn.lStructSize=sizeof(OPENFILENAME);
//The Owner window handle
ofn.hwndOwner=m_hWnd;
ofn.hInstance=NULL;
//The filter
ofn.lpstrFilter=TEXT("BMP Image *.bmp\0*.bmp\0All File *.*\0*.*\0\0");
//None user filter
ofn.lpstrCustomFilter=NULL;
ofn.nMaxCustFilter=0;
//Select the item 'BMP Image *.bmp'
ofn.nFilterIndex=1;
//Initial the full name without path
//then save the selected filename with full path
ofn.lpstrFile=FileTitle;
ofn.nMaxFile=_MAX_PATH;
//The filename without path
ofn.lpstrFileTitle=FileName;
ofn.nMaxFileTitle=_MAX_PATH;
strcpy(temp,pDoc->m_CurrentDirectory);
strcat(temp,"\\bmp");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -