// ImageEditDoc.cpp : implementation of the CImageEditDoc class
//
#include "stdafx.h"
#include "ImageEdit.h"
#include "ImageEditDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CImageEditDoc
IMPLEMENT_DYNCREATE(CImageEditDoc, CDocument)
BEGIN_MESSAGE_MAP(CImageEditDoc, CDocument)
//{{AFX_MSG_MAP(CImageEditDoc)
ON_COMMAND(ID_EFFECTS_GRAY_SCALE, OnGrayScale)
ON_UPDATE_COMMAND_UI(ID_EFFECTS_GRAY_SCALE, OnUpdateGrayScale)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImageEditDoc construction/destruction
CImageEditDoc::CImageEditDoc() :
m_event (FALSE, TRUE) // Manual-reset event, initially unowned
{
m_hThread = NULL;
m_bWorking = FALSE;
}
CImageEditDoc::~CImageEditDoc()
{
}
BOOL CImageEditDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CImageEditDoc diagnostics
#ifdef _DEBUG
void CImageEditDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CImageEditDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CImageEditDoc commands
BOOL CImageEditDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
//
// Return now ifan image is being processed.
//
if (m_bWorking) {
AfxMessageBox (_T ("You can't open an image while another is " \
"being converted"));
return FALSE;
}
//
// Let the base class do its thing.
//
if (!CDocument::OnOpenDocument (lpszPathName))
return FALSE;
//
// Open the file and create a DIB section from its contents.
//
HBITMAP hBitmap = (HBITMAP) ::LoadImage (NULL, lpszPathName,
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
if (hBitmap == NULL) {
CString string;
string.Format (_T ("%s does not contain a DIB"), lpszPathName);
AfxMessageBox (string);
return FALSE;
}
m_bitmap.Attach (hBitmap);
//
// Return now if this device doesn't support palettes.
//
CClientDC dc (NULL);
if ((dc.GetDeviceCaps (RASTERCAPS) & RC_PALETTE) == 0)
return TRUE;
//
// Create a palette to go with the DIB section.
//
if ((HBITMAP) m_bitmap != NULL) {
DIBSECTION ds;
m_bitmap.GetObject (sizeof (DIBSECTION), &ds);
int nColors;
if (ds.dsBmih.biClrUsed != 0)
nColors = ds.dsBmih.biClrUsed;
else
nColors = 1 << ds.dsBmih.biBitCount;
//
// Create a halftone palette if the DIB section contains more
// than 256 colors.
//
if (nColors > 256)
m_palette.CreateHalftonePalette (&dc);
//
// Create a custom palette from the DIB section's color table
// if the number of colors is 256 or less.
//
else {
RGBQUAD* pRGB = new RGBQUAD[nColors];
CDC memDC;
memDC.CreateCompatibleDC (&dc);
CBitmap* pOldBitmap = memDC.SelectObject (&m_bitmap);
::GetDIBColorTable ((HDC) memDC, 0, nColors, pRGB);
memDC.SelectObject (pOldBitmap);
UINT nSize = sizeof (LOGPALETTE) +
(sizeof (PALETTEENTRY) * (nColors - 1));
LOGPALETTE* pLP = (LOGPALETTE*) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for (int i=0; i<nColors; i++) {
pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
pLP->palPalEntry[i].peFlags = 0;
}
m_palette.CreatePalette (pLP);
delete[] pLP;
delete[] pRGB;
}
}
return TRUE;
}
void CImageEditDoc::DeleteContents()
{
if ((HBITMAP) m_bitmap != NULL)
m_bitmap.DeleteObject ();
if ((HPALETTE) m_palette != NULL)
m_palette.DeleteObject ();
CDocument::DeleteContents();
}
CBitmap* CImageEditDoc::GetBitmap()
{
return ((HBITMAP) m_bitmap == NULL) ? NULL : &m_bitmap;
}
CPalette* CImageEditDoc::GetPalette()
{
return ((HPALETTE) m_palette == NULL) ? NULL : &m_palette;
}
void CImageEditDoc::ThreadFinished()
{
ASSERT (m_hThread != NULL);
::WaitForSingleObject (m_hThread, INFINITE);
::CloseHandle (m_hThread);
m_hThread = NULL;
m_bWorking = FALSE;
//
// Replace the current palette with a gray scale palette.
//
if ((HPALETTE) m_palette != NULL) {
m_palette.DeleteObject ();
LOGPALETTE* pLP = CreateGrayScale ();
m_palette.CreatePalette (pLP);
delete[] pLP;
}
//
// Tell the view to repaint.
//
UpdateAllViews (NULL);
}
void CImageEditDoc::ThreadAborted()
{
ASSERT (m_hThread != NULL);
::WaitForSingleObject (m_hThread, INFINITE);
::CloseHandle (m_hThread);
m_hThread = NULL;
m_bWorking = FALSE;
}
void CImageEditDoc::OnGrayScale()
{
if (!m_bWorking) {
m_bWorking = TRUE;
m_event.ResetEvent ();
//
// Package data to pass to the image processing thread.
//
THREADPARMS* ptp = new THREADPARMS;
ptp->pWnd = AfxGetMainWnd ();
ptp->pBitmap = &m_bitmap;
ptp->pPalette = &m_palette;
ptp->pCriticalSection = &m_cs;
ptp->pEvent = &m_event;
//
// Start the image processing thread and duplicate its handle.
//
CWinThread* pThread = AfxBeginThread (ThreadFunc, ptp,
THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
::DuplicateHandle (GetCurrentProcess (),
pThread->m_hThread, GetCurrentProcess (), &m_hThread,
0, FALSE, DUPLICATE_SAME_ACCESS);
pThread->ResumeThread ();
}
else
//
// Kill the image processing thread.
//
m_event.SetEvent ();
}
void CImageEditDoc::OnUpdateGrayScale(CCmdUI* pCmdUI)
{
if (m_bWorking) {
pCmdUI->SetText (_T ("Stop &Gray Scale Conversion"));
pCmdUI->Enable ();
}
else {
pCmdUI->SetText (_T ("Convert to &Gray Scale"));
pCmdUI->Enable ((HBITMAP) m_bitmap != NULL);
}
}
/////////////////////////////////////////////////////////////////////////
// Thread function and other globals
UINT ThreadFunc (LPVOID pParam)
{
THREADPARMS* ptp = (THREADPARMS*) pParam;
CWnd* pWnd = ptp->pWnd;
CBitmap* pBitmap = ptp->pBitmap;
CPalette* pPalette = ptp->pPalette;
CCriticalSection* pCriticalSection = ptp->pCriticalSection;
CEvent* pKillEvent = ptp->pEvent;
delete ptp;
DIBSECTION ds;
pBitmap->GetObject (sizeof (DIBSECTION), &ds);
int nWidth = ds.dsBm.bmWidth;
int nHeight = ds.dsBm.bmHeight;
//
// Initialize one memory DC (memDC2) to hold a color copy of the
// image and another memory DC (memDC1) to hold a gray scale copy.
//
CClientDC dc (pWnd);
CBitmap bitmap1, bitmap2;
bitmap1.CreateCompatibleBitmap (&dc, nWidth, nHeight);
bitmap2.CreateCompatibleBitmap (&dc, nWidth, nHeight);
CDC memDC1, memDC2;
memDC1.CreateCompatibleDC (&dc);
memDC2.CreateCompatibleDC (&dc);
CBitmap* pOldBitmap1 = memDC1.SelectObject (&bitmap1);
CBitmap* pOldBitmap2 = memDC2.SelectObject (&bitmap2);
CPalette* pOldPalette1 = NULL;
CPalette* pOldPalette2 = NULL;
CPalette grayPalette;
if (pPalette->m_hObject != NULL) {
LOGPALETTE* pLP = CreateGrayScale ();
grayPalette.CreatePalette (pLP);
delete[] pLP;
pOldPalette1 = memDC1.SelectPalette (&grayPalette, FALSE);
pOldPalette2 =
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ImageEdit.rar (22个子文件)
ImageEdit
ImageEdit.aps 43KB
StdAfx.cpp 211B
resource.h 595B
ImageEdit.clw 2KB
ImageEdit.dsp 5KB
ImageEditDoc.h 2KB
MainFrm.h 2KB
ImageEditDoc.cpp 11KB
ImageEditView.h 2KB
ImageEdit.rc 9KB
StdAfx.h 1KB
MainFrm.cpp 3KB
ImageEdit.h 1KB
Release
ImageEdit.exe 40KB
SpecialStatusBar.cpp 2KB
res
ImageEdit.ico 1KB
ImageEditDoc.ico 1KB
ImageEdit.rc2 401B
ImageEditView.cpp 3KB
SpecialStatusBar.h 835B
ImageEdit.dsw 541B
ImageEdit.cpp 3KB
共 22 条
- 1
资源评论
- 伶优2014-03-19挺不错的,对我这种初学的菜鸟刚好合适.
wangmm0107
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功