📄 dctwaterview.cpp
字号:
if(!SetCurrentDirectory(temp))
{
if(!CreateDirectory(temp,NULL))
strcpy(temp,pDoc->m_CurrentDirectory);
}
ofn.lpstrInitialDir=temp;
//The title
ofn.lpstrTitle="另存为256灰度BMP图象";
//The flags
ofn.Flags=OFN_CREATEPROMPT|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR;
//Default extension
ofn.lpstrDefExt="BMP";
ofn.lCustData=NULL;
ofn.lpfnHook=NULL;
//User defined dialog
ofn.lpTemplateName=NULL;
/////Initialize the FileOpenDialog structure ends////
//If cancle or error,then return
if(GetSaveFileName(&ofn)==0)
{
GlobalFree(hbit);
return;
}
//Search the filename
CFileFind cff;
if(cff.FindFile(FileTitle,0))
{
cff.FindNextFile();
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);
//Confirm to overwrite
if(MessageBox(temp,"Save the File",MB_YESNO|MB_ICONQUESTION)==IDYES)
SetFileAttributes(FileTitle,FILE_ATTRIBUTE_NORMAL);
//else return
else
{
GlobalFree(hbit);
cff.Close();
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);
sprintf(temp,"Sorry! Failed to Open File[%s]",FileTitle);
MessageBox(temp,"Save the File",MB_ICONSTOP|MB_OK);
cff.Close();
return;
}
memcpy(lpbit,pDoc->m_StandardBmpInfo,1078);
////Save file
if(_write(fh,lpbit,lenbit+1078)==-1)
{
MessageBox("Errors While Saving!","Save the File",MB_ICONSTOP|MB_OK);
}
_close(fh);//Close file
GlobalFree(hbit);//Release memory
cff.Close();//end the file search
}
/////////The functions for menu items of Original/DECODE/////////
void CDCTWaterView::OnEmbedEmbed()
{
// TODO: Add your command handler code here
CDCTWaterDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//The memory handle
HGLOBAL hMem;
//The temp string
char temp[256];
//The blocks and counters
unsigned long int blocks,p;
unsigned long int wmblocks,q;
long int i,j,k,l;
//The dimension, blockwidth and blockheight
unsigned short int dimension,blockwidth,blockheight;
unsigned short int wmblockwidth,wmblockheight;
//The pointer for Encode image vectors
unsigned char *SourceVector[MAXENCODEVECTORS];
unsigned char *SourceVector1[MAXENCODEVECTORS/4];
unsigned char *SourceVector2[MAXENCODEVECTORS/4];
unsigned char *SourceVector3[MAXENCODEVECTORS/4];
unsigned char *SourceVector4[MAXENCODEVECTORS/4];
unsigned char *WmSourceVector[MAXENCODEVECTORS];
unsigned char *WmSourceVector1[MAXENCODEVECTORS/4];
unsigned char *WmSourceVector2[MAXENCODEVECTORS/4];
unsigned char *TerminalVector[MAXENCODEVECTORS];
unsigned char *TerminalVector1[MAXENCODEVECTORS/4];
unsigned char *TerminalVector2[MAXENCODEVECTORS/4];
unsigned char *TerminalVector3[MAXENCODEVECTORS/4];
unsigned char *TerminalVector4[MAXENCODEVECTORS/4];
//The number of blocks in width and height
unsigned short int wb,hb;
//Clear the Decode Finished flag
pDoc->m_Embeded0=FALSE;
pDoc->m_Embeded1=FALSE;
pDoc->m_Extracted=FALSE;
pDoc->m_AttackedImageOpened=FALSE;
pDoc->m_AttackExtracked=FALSE;
pDoc->m_AttackExtracked1=FALSE;
switch(pDoc->m_EmbedMethod)
{
case 0:
//Get the required parameters
dimension=64;
blocks=512*512/dimension;
wmblocks=64*64;
blockwidth=8;
blockheight=8;
//Judge the image size and width
wb=512/blockwidth;
hb=512/blockheight;
wmblockheight=128/hb;
wmblockwidth=128/wb;
//Apply memory
hMem=NULL;
hMem=GlobalAlloc(GMEM_FIXED,2*blocks*dimension+blocks*4);
if(hMem==NULL)
{
sprintf(temp,"Sorry,Not Enough Memory %fm Bytes,Can Not Embed!",(double)(MAXIMAGESIZE)/1024.0/1024.0);
MessageBox(temp,"Embed",MB_ICONSTOP|MB_OK);
return;
}
//Alloc the memory
for(p=0;p<blocks;p++)
SourceVector[p]=(unsigned char *)hMem+p*dimension;
for(q=0;q<blocks;q++)
WmSourceVector[q]=(unsigned char *)hMem+blocks*dimension+q*4;
for(p=0;p<blocks;p++)
TerminalVector[p]=(unsigned char *)hMem+blocks*4+blocks*dimension+p*dimension;
//block the source image
for(l=0;l<hb;l++)
for(k=0;k<wb;k++)
for(j=0;j<blockheight;j++)
for(i=0;i<blockwidth;i++)
SourceVector[k+l*wb][i+j*blockwidth]=pDoc->m_OriginalImageBytes[i+k*blockwidth+(j+l*blockheight)*512];
for(l=0;l<hb;l++)
for(k=0;k<wb;k++)
for(j=0;j<wmblockheight;j++)
for(i=0;i<wmblockwidth;i++)
WmSourceVector[k+l*wb][i+j*wmblockwidth]=pDoc->m_PermutedWatermark[i+k*wmblockwidth+(j+l*wmblockheight)*128];
//DisplayIcon
SetCursor(LoadCursor(NULL, IDC_WAIT));
pDoc->watermethod.AddBased(blocks,blockwidth,blockheight,wb,hb,SourceVector,WmSourceVector,TerminalVector,pDoc->key1,pDoc->key2);
for(l=0;l<hb;l++)
for(k=0;k<wb;k++)
for(j=0;j<blockheight;j++)
for(i=0;i<blockwidth;i++)
pDoc->m_EmbededImageBytes[i+k*blockwidth+(j+l*blockheight)*512]=TerminalVector[k+l*wb][i+j*blockwidth];
SetCursor(LoadCursor(NULL,IDC_ARROW));
/////////////////Save the VQ compressed image///////////
//The structure to open a FileDialog
GlobalFree(hMem);
////Change to bmp style to display starts//////////
LPBITMAPFILEHEADER bitfile2;
LPBITMAPINFOHEADER bitinfo2;
bitfile2=(LPBITMAPFILEHEADER)pDoc->m_StandardBmpInfo;
bitinfo2=(LPBITMAPINFOHEADER)(pDoc->m_StandardBmpInfo+sizeof(BITMAPFILEHEADER));
bitfile2->bfSize=pDoc->m_OriginalImageSize+1078;
bitinfo2->biHeight=pDoc->m_OriginalImageHeight;
bitinfo2->biWidth=pDoc->m_OriginalImageWidth;
//Copy the 256 gray bmp image header information
memcpy(pDoc->m_DisplayImageBytes1,pDoc->m_StandardBmpInfo,1078);
for(i=0;i<pDoc->m_OriginalImageHeight;i++)
memcpy(pDoc->m_DisplayImageBytes1+1078+i*pDoc->m_OriginalImageWidth,pDoc->m_EmbededImageBytes+(pDoc->m_OriginalImageHeight-i-1)*pDoc->m_OriginalImageWidth,pDoc->m_OriginalImageWidth);
////Change to bmp style to display ends//////////
pDoc->m_Embeded1=TRUE;
Invalidate(FALSE);
break;
case 1:
//Subsample the Original Image
for(i=0;i<256;i++)
{
for(j=0;j<256;j++)
{
pDoc->m_OriginalImageBytes1[j+256*i]=pDoc->m_OriginalImageBytes[2*j+2*i*512];
pDoc->m_OriginalImageBytes2[j+256*i]=pDoc->m_OriginalImageBytes[2*j+1+2*i*512];
pDoc->m_OriginalImageBytes3[j+256*i]=pDoc->m_OriginalImageBytes[2*j+(2*i+1)*512];
pDoc->m_OriginalImageBytes4[j+256*i]=pDoc->m_OriginalImageBytes[2*j+1+(2*i+1)*512];
}
}
////Change to bmp style to display starts//////////
//Display the Subsampled Image
LPBITMAPFILEHEADER bitfile;
LPBITMAPINFOHEADER bitinfo;
bitfile=(LPBITMAPFILEHEADER)pDoc->m_StandardBmpInfo;
bitinfo=(LPBITMAPINFOHEADER)(pDoc->m_StandardBmpInfo+sizeof(BITMAPFILEHEADER));
bitfile->bfSize=256*256+1078;
bitinfo->biHeight=256;
bitinfo->biWidth=256;
//Copy the 256 gray bmp image header information
memcpy(pDoc->m_DisplayImageBytes11,pDoc->m_StandardBmpInfo,1078);
memcpy(pDoc->m_DisplayImageBytes12,pDoc->m_StandardBmpInfo,1078);
memcpy(pDoc->m_DisplayImageBytes13,pDoc->m_StandardBmpInfo,1078);
memcpy(pDoc->m_DisplayImageBytes14,pDoc->m_StandardBmpInfo,1078);
for(i=0;i<256;i++)
memcpy(pDoc->m_DisplayImageBytes11+1078+i*256,pDoc->m_OriginalImageBytes1+(256-i-1)*256,256);
pDoc->m_Subsampled1=TRUE;
Invalidate(FALSE);
for(i=0;i<256;i++)
memcpy(pDoc->m_DisplayImageBytes12+1078+i*256,pDoc->m_OriginalImageBytes2+(256-i-1)*256,256);
pDoc->m_Subsampled2=TRUE;
Invalidate(FALSE);
for(i=0;i<256;i++)
memcpy(pDoc->m_DisplayImageBytes13+1078+i*256,pDoc->m_OriginalImageBytes3+(256-i-1)*256,256);
pDoc->m_Subsampled3=TRUE;
Invalidate(FALSE);
for(i=0;i<256;i++)
memcpy(pDoc->m_DisplayImageBytes14+1078+i*256,pDoc->m_OriginalImageBytes4+(256-i-1)*256,256);
pDoc->m_Subsampled4=TRUE;
Invalidate(FALSE);
//Display end
//Get the required parameters
dimension=64;
blocks=256*256/dimension;
wmblocks=32*32;
blockwidth=8;
blockheight=8;
//Judge the image size and width
wb=256/blockwidth;
hb=256/blockheight;
wmblockheight=128/hb;
wmblockwidth=128/wb;
//Apply memory
hMem=NULL;
hMem=GlobalAlloc(GMEM_FIXED,2*4*blocks*dimension+blocks*16*2);
if(hMem==NULL)
{
sprintf(temp,"Sorry,Not Enough Memory %f Bytes,Unable To Embed!",(double)(MAXIMAGESIZE)/1024.0/1024.0);
MessageBox(temp,"Embed",MB_ICONSTOP|MB_OK);
return;
}
//Alloc the memory
for(p=0;p<blocks;p++)
{
SourceVector1[p]=(unsigned char *)hMem+p*dimension;
SourceVector2[p]=(unsigned char *)hMem+p*dimension+blocks*dimension;
SourceVector3[p]=(unsigned char *)hMem+p*dimension+blocks*2*dimension;
SourceVector4[p]=(unsigned char *)hMem+p*dimension+blocks*3*dimension;
}
for(q=0;q<blocks;q++)
{
WmSourceVector1[q]=(unsigned char *)hMem+4*blocks*dimension+q*16;
WmSourceVector2[q]=(unsigned char *)hMem+4*blocks*dimension+q*16+blocks*16;
}
for(p=0;p<blocks;p++)
{
TerminalVector1[p]=(unsigned char *)hMem+blocks*16+blocks*dimension*4+p*dimension;
TerminalVector2[p]=(unsigned char *)hMem+blocks*16+blocks*dimension*4+p*dimension+blocks*dimension;
TerminalVector3[p]=(unsigned char *)hMem+blocks*16+blocks*dimension*4+p*dimension+blocks*2*dimension;
TerminalVector4[p]=(unsigned char *)hMem+blocks*16+blocks*dimension*4+p*dimension+blocks*3*dimension;
}
//block the source image
for(l=0;l<hb;l++)
for(k=0;k<wb;k++)
for(j=0;j<blockheight;j++)
for(i=0;i<blockwidth;i++)
{
SourceVector1[k+l*wb][i+j*blockwidth]=pDoc->m_OriginalImageBytes1[i+k*blockwidth+(j+l*blockheight)*256];
SourceVector2[k+l*wb][i+j*blockwidth]=pDoc->m_OriginalImageBytes2[i+k*blockwidth+(j+l*blockheight)*256];
SourceVector3[k+l*wb][i+j*blockwidth]=pDoc->m_OriginalImageBytes3[i+k*blockwidth+(j+l*blockheight)*256];
SourceVector4[k+l*wb][i+j*blockwidth]=pDoc->m_OriginalImageBytes4[i+k*blockwidth+(j+l*blockheight)*256];
}
for(l=0;l<hb;l++)
for(k=0;k<wb;k++)
for(j=0;j<wmblockheight;j++)
for(i=0;i<wmblockwidth;i++)
WmSourceVector1[k+l*wb][i+j*wmblockwidth]=pDoc->m_PermutedWatermark[i+k*wmblockwidth+(j+l*wmblockheight)*128];
//DisplayIcon
SetCursor(LoadCursor(NULL, IDC_WAIT));
//Embed
pDoc->watermethod.Subsamplingmethod(blocks,blockwidth,blockheight,wb,hb,SourceVector1,SourceVector2,SourceVector3,SourceVector4,WmSourceVector1,0,TerminalVector1,TerminalVector2,TerminalVector3,TerminalVector4,1);
//Transmit the image information
for(l=0;l<hb;l++)
for(k=0;k<wb;k++)
for(j=0;j<blockheight;j++)
for(i=0;i<blockwidth;i++)
{
pDoc->m_EmbededImageBytes1[i+k*blockwidth+(j+l*blockheight)*256]=TerminalVector1[k+l*wb][i+j*blockwidth];
pDoc->m_EmbededImageBytes2[i+k*blockwidth+(j+l*blockheight)*256]=TerminalVector2[k+l*wb][i+j*blockwidth];
pDoc->m_EmbededImageBytes3[i+k*blockwidth+(j+l*blockheight)*256]=TerminalVector3[k+l*wb][i+j*blockwidth];
pDoc->m_EmbededImageBytes4[i+k*blockwidth+(j+l*blockheight)*256]=TerminalVector4[k+l*wb][i+j*blockwidth];
}
//Compose the Embeded Image
for(i=0;i<256;i++)
for(j=0;j<256;j++)
{
pDoc->m_EmbededImageBytes[2*j+2*i*512]=pDoc->m_EmbededImageBytes1[j+256*i];
pDoc->m_EmbededImageBytes[2*j+1+2*i*512]=pDoc->m_EmbededImageBytes2[j+256*i];
pDoc->m_EmbededImageBytes[2*j+(2*i+1)*512]=pDoc->m_EmbededImageBytes3[j+256*i];
pDoc->m_EmbededImageBytes[2*j+1+(2*i+1)*512]=pDoc->m_EmbededImageBytes4[j+256*i];
}
SetCursor(LoadCursor(NULL,IDC_ARROW));
GlobalFree(hMem);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -