Skip to content

Instantly share code, notes, and snippets.

/CODE

Created February 20, 2011 14:39
Show Gist options
  • Save anonymous/836014 to your computer and use it in GitHub Desktop.
Save anonymous/836014 to your computer and use it in GitHub Desktop.
直接找ONCHANGE就可以了
// ImageReadingDoc.cpp : implementation of the CImageReadingDoc class
//
#include "stdafx.h"
#include "ImageReading.h"
#include "ImageReadingDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CImageReadingDoc
IMPLEMENT_DYNCREATE(CImageReadingDoc, CDocument)
BEGIN_MESSAGE_MAP(CImageReadingDoc, CDocument)
//{{AFX_MSG_MAP(CImageReadingDoc)
ON_COMMAND(IDC_OPENIMAGE, OnOpenimage)
ON_COMMAND(IDC_COLOR_TO_GRAY, OnColorToGray)
ON_COMMAND(ID_SUB, OnSub)
ON_COMMAND(ID_OPENIMAGE2, OnOpenimage2)
ON_COMMAND(ID_CHANGE, OnChange)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CImageReadingDoc construction/destruction
CImageReadingDoc::CImageReadingDoc()
{
Image = NULL;
Image2 = NULL;
Image3 = NULL;
}
CImageReadingDoc::~CImageReadingDoc()
{
if(Image)
{
delete Image;
Image = NULL;
}
if(Image2)
{
delete Image2;
Image2 = NULL;
}
if(Image3)
{
delete Image3;
Image3 = NULL;
}
}
BOOL CImageReadingDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CImageReadingDoc diagnostics
#ifdef _DEBUG
void CImageReadingDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CImageReadingDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CImageReadingDoc commands
static char BASED_CODE szFilter[] =
"Support Image Files (*.bmp *.jpg *.gif *.ico)|*.bmp;*.jpg;*.gif;*.ico|BMP Files (*.bmp)|*.bmp|\
JPEG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|ICON Files (*.ico)|*.ico||";
void CImageReadingDoc::OnOpenimage() // 開啟圖檔的主要程式
{
// 呼叫MFC內建開啟檔案的DIALOG執行,相關資訊參考MSDN
CFileDialog filedlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
// 檢查是否按下OK按鈕,否則跳出
if(filedlg.DoModal() != IDOK)
return;
// 呼叫CxImage宣告一個記憶體指標以提供相關影像解碼動作
if(Image)
delete Image;
Image = new CxImage();
CString Temp;
Image->Load(filedlg.GetPathName());
/* *\
經過上述動作,o_ImageUL內以存放解碼後的影像資料,會用到的有
o_ImageUL->GetWidth() => 獲得影像寬度
o_ImageUL->GetHeight() => 獲得影像高度
o_ImageUL->GetBits() => 主要影像資料 以BYTE儲存);//利用物件本身所提供之成員函數取得一個像素的 R,G,B(指標用法)
o_ImageUL->GetDIB() => 影像HEADER,FOR 秀圖的時候會需要
o_ImageUL->GetEffWidth() => 影像單一一列的總長度(四的倍數) EffW約為width*3 因為ㄧ個RGB3個BYTE
o_ImageUL->GetBpp() => 影像位元數(1,4,8,24) bpp = bit per pixel
其中 o_ImageUL->GetBits() 內的編碼方式以 BYTE 為單位
以 B G R 三組代表影像的單一點
\* */
// 秀圖方法:
// 呼叫MFC內建FUNC. Refresh All Views
UpdateAllViews(FALSE);
}
void CImageReadingDoc::OnColorToGray()
{
// 檢查是否以讀取影像進入記憶體,若無則跳出
if(!Image) return;
// 檢查是否是24BITS(彩色),若不是則跳出
if(Image->GetBpp() != 24) return;
// 指標直接指向影像資料的記憶體位置
BYTE *p_Image = Image->GetBits();
int i, j, k,
BPP = Image->GetBpp() / 8;
for(i=0; i < Image->GetHeight(); i++, p_Image+=Image->GetEffWidth())
for(j=0, k=0; j < Image->GetWidth(); j++, k+=BPP){
p_Image[k] = p_Image[k + 1] = p_Image[k + 2] = (p_Image[k] + p_Image[k + 1] + p_Image[k + 2]) / 3;
if(p_Image[k] >=128){
p_Image[k] = p_Image[k + 1] = p_Image[k + 2] =255;
}
else
{
p_Image[k] = p_Image[k + 1] = p_Image[k + 2] =0;
}
}
// 呼叫MFC內建FUNC. Refresh All Views
UpdateAllViews(FALSE);
}
void CImageReadingDoc::OnOpenimage2()
{
// 呼叫MFC內建開啟檔案的DIALOG執行,相關資訊參考MSDN
CFileDialog filedlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);
// 檢查是否按下OK按鈕,否則跳出
if(filedlg.DoModal() != IDOK)
return;
// 呼叫CxImage宣告一個記憶體指標以提供相關影像解碼動作
if(Image2)
delete Image2;
Image2 = new CxImage();
CString Temp;
Image2->Load(filedlg.GetPathName());
UpdateAllViews(FALSE);
}
void CImageReadingDoc::OnSub()
{
// TODO: Add your command handler code here
}
void CImageReadingDoc::OnChange()
{
/*
int BPP ;
if(!Image)
return;
BYTE *Buffer = new BYTE[Image->GetHeight() * Image->GetEffWidth()];
BPP = Image->GetBpp()/8;
BYTE *p_Image = Image->GetBits();
BYTE *p_Buffer = Buffer;
int i , j , k , l;
for(i=0; i<Image->GetHeight(); i++, p_Image+=Image->GetEffWidth(), p_Buffer+=Image->GetEffWidth())
for(j=0, k=0, l=(Image->GetWidth() - 1) * BPP; j < Image->GetWidth(); j++, k+=BPP, l-=BPP)
{
p_Buffer[l ] = p_Image[k ];
p_Buffer[l + 1] = p_Image[k + 1];
p_Buffer[l + 2] = p_Image[k + 2];
}
memcpy(Image->GetBits(), Buffer, sizeof(BYTE) * Image->GetHeight() * Image->GetEffWidth());
delete [] Buffer;
Buffer = NULL;
UpdateAllViews(FALSE);
*/
/*p_image[k]=藍色
p_image[k+1]=綠色
p_image[k+2]=紅色*/
if(!Image)
return;
int BPP,k;
BYTE *p_Image = Image->GetBits();
BPP = Image->GetBpp();
BPP/=8;
BYTE gray[1000][1000];
int i,j,a;
for(i=0;i<Image->GetHeight();i++, p_Image=p_Image+Image->GetEffWidth())
for(j=0,k=0;j<Image->GetWidth();j++,k+=BPP)
{
p_Image[k]=0.299*p_Image[k+2]+0.587*p_Image[k+1]+0.114*p_Image[k];
p_Image[k+2] = p_Image[k+1] = p_Image[k];
gray[i][j]=p_Image[k];
//將彩色的圖轉為灰階,並把資料存入gray陣列中
/* if(p_Image[k] >= 128){
p_Image[k] = p_Image[k+1] = p_Image[k+2]= 255;
//p_Image[k] = 255;
}
else{
p_Image[k] = p_Image[k+1] = p_Image[k+2]= 0;
}*/
}
//以下是演算法QQ
/*for(i=1;i<Image->GetHeight();i++, p_Image=p_Image+Image->GetEffWidth())
for(j=1,k=0;j<Image->GetWidth();j++,k+=BPP)
{
BYTE a = gray[i-1][j-1];
BYTE b = gray[i][j-1];
BYTE c = gray[i+1][j-1];
BYTE d = gray[i-1][j];
BYTE e = gray[i+1][j];
BYTE f = gray[i-1][j+1];
BYTE g = gray[i][j+1];
BYTE h = gray[i+1][j+1];
BYTE hor = (a+d+f) - (c+e+h);
BYTE vert = (a+b+c) - (f+g+h);
if(hor<0)
hor=-(hor);
if(vert<0)
vert=-(vert);
BYTE gxy = hor + vert;
if (gxy>128)
{
p_Image[k] = 255;
p_Image[k+1] = 255;
p_Image[k+2]= 255;
}
else
{
p_Image[k] = 0;
p_Image[k+1] = 0;
p_Image[k+2]= 0;
}
}*/
UpdateAllViews(FALSE);
}
/*
int BPP ;
if(!Image)
return;
BYTE *Buffer = new BYTE[Image->GetHeight() * Image->GetEffWidth()];
BPP = Image->GetBpp()/8;
BYTE *p_Image = Image->GetBits();
BYTE *p_Buffer = Buffer;
int i , j , k , l;
for(i=0; i<Image->GetHeight(); i++, p_Image+=Image->GetEffWidth(), p_Buffer+=Image->GetEffWidth())
for(j=0, k=0, l=(Image->GetWidth() - 1) * BPP; j < Image->GetWidth(); j++, k+=BPP, l-=BPP)
{
p_Buffer[l ] = p_Image[k ];
p_Buffer[l + 1] = p_Image[k + 1];
p_Buffer[l + 2] = p_Image[k + 2];
}
memcpy(Image->GetBits(), Buffer, sizeof(BYTE) * Image->GetHeight() * Image->GetEffWidth());
delete [] Buffer;
Buffer = NULL;
UpdateAllViews(FALSE);
*/
/*
BYTE *p_Image = Image->GetBits();
BYTE *temp_Image = Image->GetBits();
int i, j, k;
int a,b;
a = Image->GetEffWidth();
b = a/2;
for(i=0; i < Image->GetHeight(); i++, p_Image+=Image->GetEffWidth(),temp_Image+=Image->GetEffWidth())
{
for(j=0,k=0; j < b,k < b; j+=3,k-=3)
{
temp_Image[j] = p_Image[k];
temp_Image[j+1] = p_Image[k+1];
temp_Image[j+2] = p_Image[k+2];
}
}
UpdateAllViews(FALSE);
}
/* temp_Image[a-j-1] = p_Image[j];
p_Image[j] = p_Image[a-j-1] ;
p_Image[a-j-1] = temp_Image[a-j-1];*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment