⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dibdc.cpp

📁 实现JPEG编码功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if(suffix=="fic")
	{
		return GetCrypticFri(pFile);
	}
	return FALSE;
}

void CDIBDC::SaveToFile(CFile *pFile)
{
	CString suffix=pFile->GetFileName();
	int PointAt=suffix.Find('.');
	do
	{
		suffix=suffix.Mid(PointAt+1);
	}
	while((PointAt=suffix.Find('.'))!=-1);
	suffix.MakeLower();
	if(suffix=="fri")
	{
		SaveFreeImage(pFile);
		return;
	}
	if(suffix=="bmp")
	{
		SaveBitmap(pFile);
		return;
	}
	if(suffix=="bmc")
	{
		SaveCrypticBmp(pFile);
		return;
	}
	if(suffix=="fic")
	{
		SaveCrypticFri(pFile);
		return;
	}
	SaveBitmap(pFile);
}

BYTE** CDIBDC::LockIndex()
{
	if(m_Index!=NULL)
	{
		delete[] m_Index;
	}
	LONG height=Height();
	LONG rowBytes=RowBytes();
	if(height>0)
	{
		m_Index=new PBYTE[height];
		for(int i=0;i<height;i++)
			m_Index[height-i-1]=&m_pData[i*rowBytes];
	}
	else
	{
		m_Index=new PBYTE[-height];
		for(int i=0;i<-height;i++)
			m_Index[i]=&m_pData[i*rowBytes];
	}
	return m_Index;
}

void CDIBDC::UnlockIndex()
{
	if(m_Index!=NULL)
	{
		delete[] m_Index;
		m_Index=NULL;
	}
}

BYTE* CDIBDC::GetData()
{
	return m_pData;
}

void CDIBDC::SetData(const void* pData)
{
	memcpy(m_pData,pData,ImageSize());
}

BYTE** CDIBDC::GetIndex()
{
	return m_Index;
}

void CDIBDC::SetAt(int x, int y, ...)
{
	va_list marker;
	va_start(marker,y);
	switch(Bits())
	{
	case 1:
		m_Index[y][x>>3]&=~(0x1<<(7-x%8));
		m_Index[y][x>>3]|=(va_arg(marker,BYTE)&1)<<(7-x%8);
		break;
	case 4:
		m_Index[y][x>>1]&=x&1?0xf0:0xf;
		m_Index[y][x>>1]|=(va_arg(marker,BYTE)&0xf)<<(x&1?0:4);
		break;
	case 8:
		m_Index[y][x]=va_arg(marker,BYTE);
		break;
	case 16:
		((WORD*)(m_Index[y]))[x]=va_arg(marker,WORD);
		break;
	case 24:
		((RGBTRIPLE*)(m_Index[y]))[x]=va_arg(marker,RGBTRIPLE);
		break;
	case 32:
		((DWORD*)(m_Index[y]))[x]=va_arg(marker,DWORD);
		break;
	default:;
	}
	va_end(marker);
}

void CDIBDC::GetAt(int x, int y, ...)
{
	va_list marker;
	va_start(marker,y);
	switch(Bits())
	{
	case 1:
		*va_arg(marker,BYTE*)=(m_Index[y][x>>3]&(0x1<<(7-x%8)))>>(7-x%8);
		break;
	case 4:
		*va_arg(marker,BYTE*)=(m_Index[y][x>>1]&(x&1?0xf:0xf0))>>(x&1?0:4);
		break;
	case 8:
		*va_arg(marker,BYTE*)=m_Index[y][x];
		break;
	case 16:
		*va_arg(marker,WORD*)=((WORD*)(m_Index[y]))[x];
		break;
	case 24:
		*va_arg(marker,RGBTRIPLE*)=((RGBTRIPLE*)(m_Index[y]))[x];
		break;
	case 32:
		*va_arg(marker,DWORD*)=((DWORD*)(m_Index[y]))[x];
		break;
	default:;
	}
	va_end(marker);
}

void CDIBDC::SaveCrypticBmp(CFile* pFile)
{
#ifdef THERE_IS_A_ENCRYPTION_H_FILE

	char bm[3]="BM";
	LONG value=0;
	LONG lHeadLength=0xE+m_BitmapInfo.bmiHeader.biSize;
	if(Bits()<=8)
		lHeadLength+=1<<(Bits()+2);
	LONG TotalSize=ImageSize()+lHeadLength;
	pFile->SeekToBegin();
	DWORD seed=time(NULL);
	pFile->Write(&seed, sizeof(seed));
	m_Encryption.SetSeed(seed);
	m_Encryption.Encrypt(bm, 2);
	pFile->Write(bm,2);
	m_Encryption.Encrypt(&TotalSize, 4);
	pFile->Write(&TotalSize,4);
	m_Encryption.Encrypt(&value, 4);
	pFile->Write(&value,4);
	m_Encryption.Encrypt(&lHeadLength, 4);
	pFile->Write(&lHeadLength,4);
	BITMAPINFOHEADER temp_bmiHeader=m_BitmapInfo.bmiHeader;
	m_Encryption.Encrypt(&temp_bmiHeader,m_BitmapInfo.bmiHeader.biSize);
	pFile->Write(&temp_bmiHeader,m_BitmapInfo.bmiHeader.biSize);

	PBYTE pData;
	if(Bits()<=8)
	{
		pData=new BYTE[1<<(Bits()+2)];
		memcpy(pData, ColorTable(), 1<<(Bits()+2));
		m_Encryption.Encrypt(pData,1<<(Bits()+2));
		pFile->Write(pData,1<<(Bits()+2));
		delete[] pData;
	}

	pData=new BYTE[ImageSize()];
	memcpy(pData, m_pData, ImageSize());
	m_Encryption.Encrypt(pData, ImageSize());
	pFile->Write(pData,ImageSize());
	delete[] pData;

#endif
}

BOOL CDIBDC::GetCrypticBmp(CFile* pFile)
{
#ifdef THERE_IS_A_ENCRYPTION_H_FILE

	char bm[3];
	DWORD value;
	DWORD lHeadLength;
	DWORD TotalSize;
	pFile->SeekToBegin();

	DWORD seed;
	pFile->Read(&seed, sizeof(seed));
	m_Encryption.SetSeed(seed);
	pFile->Read(bm, 2);
	m_Encryption.Decrypt(bm, 2);
	if(bm[0]!='B' || bm[1]!='M')
		return FALSE;
	pFile->Read(&TotalSize,4);
	m_Encryption.Decrypt(&TotalSize, 4);
	if(pFile->GetLength()<TotalSize)
		return FALSE;
	pFile->Read(&value,4);
	m_Encryption.Decrypt(&value,4);
	pFile->Read(&lHeadLength,4);
	m_Encryption.Decrypt(&lHeadLength,4);
	pFile->Read(&m_BitmapInfo.bmiHeader.biSize,4);
	m_Encryption.Decrypt(&m_BitmapInfo.bmiHeader.biSize,4);
	pFile->Read(PBYTE(&m_BitmapInfo.bmiHeader)+4,m_BitmapInfo.bmiHeader.biSize-4l);
	m_Encryption.Decrypt(PBYTE(&m_BitmapInfo.bmiHeader)+4,m_BitmapInfo.bmiHeader.biSize-4l);

	ResetDIB();

	LONG ColorsSize=lHeadLength-m_BitmapInfo.bmiHeader.biSize-0xE;
	if(ColorsSize>0)
	{
		pFile->Read(m_pColors,ColorsSize);
		m_Encryption.Decrypt(m_pColors, ColorsSize);
		SetColorTable(m_pColors);
	}

	pFile->Read(m_pData,ImageSize());
	m_Encryption.Decrypt(m_pData, ImageSize());

	return TRUE;

#endif

	return FALSE;
}

void CDIBDC::SaveCrypticFri(CFile *pFile, BOOL IsGray)
{
#ifdef THERE_IS_A_ENCRYPTION_H_FILE

	if(Bits()!=8 || IsGray==FALSE)
	{
		CDIBDC tempDC;
		tempDC.CreateDIB(Width(),abs(Height()),8);
		tempDC.SetDefaultColors(TRUE);
		this->Paste(&tempDC,0,0);
		*this=tempDC;
	}

	pFile->SeekToBegin();
	WORD width=WORD(Width());
	WORD height=WORD(Height());
	DWORD seed=time(NULL);
	pFile->Write(&seed, sizeof(seed));
	m_Encryption.SetSeed(seed);
	m_Encryption.Encrypt(&width, 2);
	pFile->Write(&width,2);
	m_Encryption.Encrypt(&height, 2);
	pFile->Write(&height,2);

	width=WORD(Width());
	height=WORD(Height());
	PBYTE pData=new BYTE[width];
	for(WORD i=0;i<height;i++)
	{
		memcpy(pData, &(m_pData[(height-i-1)*RowBytes()]), width);
		m_Encryption.Encrypt(pData, width);
		pFile->Write(pData, width);
	}
	delete[] pData;

#endif

}

BOOL CDIBDC::GetCrypticFri(CFile *pFile)
{
#ifdef THERE_IS_A_ENCRYPTION_H_FILE

	WORD width,height;
	DWORD seed;
	pFile->SeekToBegin();
	pFile->Read(&seed, sizeof(seed));
	m_Encryption.SetSeed(seed);
	pFile->Read(&width,2);
	m_Encryption.Decrypt(&width, 2);
	pFile->Read(&height,2);
	m_Encryption.Decrypt(&height, 2);
	LONG currentPos=pFile->Seek(0l, CFile::current);
	if(DWORD(width)*height+currentPos<pFile->GetLength())
		return FALSE;
	CreateDIB(width,height,8);
	SetDefaultColors(TRUE);
	for(WORD i=0;i<height;i++)
	{
		pFile->Read(&(m_pData[(height-i-1)*RowBytes()]),width);
		m_Encryption.Decrypt(&(m_pData[(height-i-1)*RowBytes()]),width);
	}
	return TRUE;

#endif

	return FALSE;
}

void CDIBDC::SetFillColor(COLORREF color)
{
	CDIBDC tempDC;
	tempDC.CreateDIB(Width(), abs(Height()), 8);
	tempDC.SetColors(255, BYTE(color&0xff), BYTE((color>>8)&0xff), BYTE((color>>16)&0xff) );
	memset(tempDC.GetData(), 255, tempDC.ImageSize());
	tempDC.Paste(this, 0, 0, Width(), abs(Height()));
}

DWORD CDIBDC::DIBSize()
{
	DWORD dwTotalSize=ImageSize()+DIBHeaderSize();

	return dwTotalSize;
}

void CDIBDC::SaveToMem(PVOID pDIB)
{
	SaveToDIBHeader(pDIB);

	memcpy((BYTE*)pDIB + DIBHeaderSize(), m_pData,ImageSize());
}

BOOL CDIBDC::LoadFromMem(PVOID pDIB)
{
	memcpy(&m_BitmapInfo,pDIB,((PBITMAPINFO)pDIB)->bmiHeader.biSize);

	if(!ResetDIB())
		return FALSE;

	LPSTR pData=(LPSTR)pDIB+(WORD)( ((PBITMAPINFO)pDIB)->bmiHeader.biSize);
	WORD bits=Bits();
	if(bits<=8)
	{
		LPSTR pColors=pData;
		SetColorTable((RGBQUAD*)pColors);
		pData+=ColorTableSize();
	}
	memcpy(m_pData,pData,ImageSize());

	return TRUE;
}

DWORD CDIBDC::ColorTableSize()
{
	DWORD dwColorSize=0;
	if(Bits()<=8)
	{
		dwColorSize=m_BitmapInfo.bmiHeader.biClrUsed<<2;
		if(dwColorSize==0)
			dwColorSize=1<<(Bits()+2);
	}
	return dwColorSize;
}

HBITMAP CDIBDC::GetBitmapHandle()
{
	return m_hDIBitmap;
}

void CDIBDC::SaveToDIBHeader(PVOID pDIB)
{
	memcpy(pDIB,&m_BitmapInfo,m_BitmapInfo.bmiHeader.biSize);
	PBYTE pData=PBYTE(pDIB)+(WORD)(m_BitmapInfo.bmiHeader.biSize);
	if(Bits()<=8)
	{
		RGBQUAD* pColors = (RGBQUAD*)pData;
		GetColorTable(pColors);
		pData=PBYTE(pColors)+ColorTableSize();
	}
}

DWORD CDIBDC::DIBHeaderSize()
{
	DWORD dwHeaderSize = m_BitmapInfo.bmiHeader.biSize + ColorTableSize();
	return dwHeaderSize;
}

BOOL CDIBDC::SaveImage(LPCTSTR lpszPathName)
{
	CFile file;
	if(!file.Open(lpszPathName, CFile::modeWrite | CFile::modeCreate))
		return FALSE;

	SaveBitmap(&file);

	file.Close();

	return TRUE;
}

BOOL CDIBDC::OpenImage(LPCTSTR lpszPathName)
{
	CFile file;
	if(!file.Open(lpszPathName, CFile::modeRead))
		return FALSE;

	BOOL bRet = GetBitmap(&file);

	file.Close();
	return bRet;
}

VOID CDIBDC::DestroyDIB()
{
	if(m_hDIBitmap != NULL)
	{
		UnlockIndex();
		DeleteDC();
		::DeleteObject(m_hDIBitmap);

		ResetParams();
	}
}

BOOL CDIBDC::Load(LPCTSTR lpszPathName)
{

   LPPICTURE pPicture;
	// open file
	HANDLE hFile = CreateFile(lpszPathName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
	if(INVALID_HANDLE_VALUE == hFile)
		return FALSE;

	// get file size
	DWORD dwFileSize = GetFileSize(hFile, NULL);
	if(-1 == dwFileSize)
	{
		CloseHandle(hFile);
		return FALSE;
	}

	LPVOID pvData = NULL;
	// alloc memory based on file size
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
	if(NULL == hGlobal)
	{
		CloseHandle(hFile);
		return FALSE;		
	}

	pvData = GlobalLock(hGlobal);
	if(NULL == pvData)
	{
		GlobalFree(hGlobal);
		CloseHandle(hFile);
		return FALSE;
	}

	DWORD dwBytesRead = 0;
	// read file and store in global memory
	BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL);
	if(FALSE == bRead)
	{
		GlobalUnlock(hGlobal);
		GlobalFree(hGlobal);
		CloseHandle(hFile);
		return FALSE;		
	}
	GlobalUnlock(hGlobal);

	CloseHandle(hFile);

	LPSTREAM pstm = NULL;
	// create IStream* from global memory
	HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
	if(!(SUCCEEDED(hr) && pstm))
	{
		GlobalFree(hGlobal);
		return FALSE;
	}

	// Create IPicture from image file
	hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&pPicture);
	if(!(SUCCEEDED(hr) && pPicture))
	{
		pstm->Release();
		GlobalFree(hGlobal);
		return FALSE;
	}
	
	pstm->Release();
	GlobalFree(hGlobal);

	//Save Image to DIBDC
	long hmWidth;
	long hmHeight;
	pPicture->get_Width(&hmWidth);
	pPicture->get_Height(&hmHeight);

	OLE_HANDLE hPal;
	if( S_OK == pPicture->get_hPal(&hPal) && hPal)
	{
		CPalette* pPalette = CPalette::FromHandle(HPALETTE(hPal));
		INT a = pPalette->GetEntryCount();
	}
	// convert himetric to pixels
	int nWidth	= MulDiv(hmWidth, ::GetDeviceCaps(m_hDC, LOGPIXELSX), HIMETRIC_INCH);
	int nHeight	= MulDiv(hmHeight, ::GetDeviceCaps(m_hDC, LOGPIXELSY), HIMETRIC_INCH);
	WORD nBits = 32;
	if(!CreateDIB(nWidth, nHeight, nBits))
	{
		pPicture->Release();
		return FALSE;
	}

	// display picture using IPicture::Render
	pPicture->Render(m_hDC, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, NULL);

	pPicture->Release();

	return TRUE;
}


BOOL CDIBDC::Load()
{
	char szExt[] = "图像文件(*.bmp;*.jpg;*.gif;*.png;*.ico)|*.bmp;*.jpg;*.gif;*.png;*.ico|所有文件(*.*)|*.*||";
	CFileDialog dlg(TRUE, NULL, NULL, 
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		szExt);
	
	if(dlg.DoModal() == IDOK)
	{
		return Load(dlg.GetPathName());
	}
	else
		return FALSE;
}

BOOL CDIBDC::Load(BYTE *pData, int FileLength)
{
	LPPICTURE pPicture;


	LPVOID pvData = NULL;
	// alloc memory based on file size
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, FileLength);

	pvData = GlobalLock(hGlobal);
	if(NULL == pvData)
	{
		GlobalFree(hGlobal);
		return FALSE;
	}

	DWORD dwBytesRead = 0;
	// read file and store in global memory
	memcpy(pvData,pData,FileLength);
	GlobalUnlock(hGlobal);


	LPSTREAM pstm = NULL;
	// create IStream* from global memory
	HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm);
	if(!(SUCCEEDED(hr) && pstm))
	{
		GlobalFree(hGlobal);
		return FALSE;
	}

	// Create IPicture from image file
	hr = ::OleLoadPicture(pstm, FileLength, FALSE, IID_IPicture, (LPVOID *)&pPicture);
	if(!(SUCCEEDED(hr) && pPicture))
	{
		pstm->Release();
		GlobalFree(hGlobal);
		return FALSE;
	}
	
	pstm->Release();
	GlobalFree(hGlobal);

	//Save Image to DIBDC
	long hmWidth;
	long hmHeight;
	pPicture->get_Width(&hmWidth);
	pPicture->get_Height(&hmHeight);

	OLE_HANDLE hPal;
	if( S_OK == pPicture->get_hPal(&hPal) && hPal)
	{
		CPalette* pPalette = CPalette::FromHandle(HPALETTE(hPal));
		INT a = pPalette->GetEntryCount();
	}
	// convert himetric to pixels
	int nWidth	= MulDiv(hmWidth, ::GetDeviceCaps(m_hDC, LOGPIXELSX), HIMETRIC_INCH);
	int nHeight	= MulDiv(hmHeight, ::GetDeviceCaps(m_hDC, LOGPIXELSY), HIMETRIC_INCH);
	WORD nBits = 32;
	if(!CreateDIB(nWidth, nHeight, nBits))
	{
		pPicture->Release();
		return FALSE;
	}

	// display picture using IPicture::Render
	pPicture->Render(m_hDC, 0, 0, nWidth, nHeight, 0, hmHeight, hmWidth, -hmHeight, NULL);

	pPicture->Release();

	return TRUE;

}
BOOL CDIBDC::IsValid()
{
	return m_hDIBitmap != NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -