Selasa, 07 Juli 2009

Jawaban Tugas 1

Void CTgs1View::OnTestOpenfile( )
{
// TODO: Add your command handler code here
static char BASED_CODE szFilter]="Bitmap Files (*.bmp)|*.bmp||";
CFileDialog m_ldFile(TRUE, "*.bmp", name, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilter);
if(m_ldFile.DoModal()==IDOK)
{
name=m_ldFile.GetPathName();
LoadGambar();
}}
// merubah data pixel ke RGB
void WarnaToRGB(long int warna,int *Red, int *Green, int *Blue)
{
*Red = warna & 0x000000FF;
*Green = (warna & 0x0000FF00) >> 8;
*Blue = (warna & 0x00FF0000) >> 16;
}
//merubah RGB ke data pixel
long int RGBToWarna(int Red, int Green, int Blue)
{
return(Red+(Green<<8)+(blue<<16));>
}

// Menampilkan gambar hasil dari open file
void CEdgeRobertView::LoadGambar(void)
{
CDC* pDC = GetDC();
CDC dcMem;
int i,j,r,g,b,s;
long int w;
HBITMAP hBitmap=(HBITMAP)::LoadImage(AfxGetInstanceHandle(), name,
IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
if(hBitmap)
{
if(m_bmpBitmap.DeleteObject())
m_bmpBitmap.Detach();
m_bmpBitmap.Attach(hBitmap);
}
dcMem.CreateCompatibleDC(pDC);
dcMem.SelectObject(&m_bmpBitmap);

// Proses RGB to GRAY-SCALE
f0r(i=0;i<250;i++)>
f0r(j=0;j<250;j++)>
w=dcMem.GetPixel(j,i);
WarnaToRGB(w,&r,&g,&b);
s=int((r+g+b)/3);
w=RGBToWarna(s,s,s);
dcMem.SetPixel(j,i,w);
}
pDC->BitBlt(0,0,250,250,&dcMem,0,0,SRCCOPY);
}

void CEdgeRobertView::OnTestEdgerobert()
{
// TODO: Add your command handler code here
CDC* pDC = GetDC();
CDC dcMem;
int i,j,r,g,b;
int resultr,resultg,resultb;
long int w,mat[1][2];
int h[1][2],hr,hg,hb;
dcMem.CreateCompatibleDC(pDC);
dcMem.SelectObject(&m_bmpBitmap);
// Proses Konvolusi
int nh=2; // Menyatakan ukuran filter
// Penentuan kernel filter
h[0][0]=-1; h[0][1]=1;
int u=0;//Menyatakan ukuran filter orizontal
f0r(i=0;i<250;i++)>
f0r(j=0;j<250;j++){>
mat[0][0]=dcMem.GetPixel(j,i);
mat[0][1]=dcMem.GetPixel(j,i+1);
hr=0;hg=0;hb=0;
//Menyatakan ukuran filter vertikal
f0r(int v=0;v<2;v++){>
WarnaToRGB(mat[u][v],&r,&g,&b);
hr+=r*h[u][v];
hg+=g*h[u][v];
hb+=b*h[u][v];
}
resultr=hr;
resultg=hg;
resultb=hb;
if(resultr>255)resultr=255;
if(resultg>255)resultg=255;
if(resultb>255)resultb=255;
w=RGBToWarna(resultr,resultg,resultb);
dcMem.SetPixel(j,i,w);

void CTgs1View::OnTestEdgeprewitt()
{
// TODO: Add your command handler code here
CDC* pDC = GetDC();
CDC dcMem;
int i,j,r,g,b,u,v;
int resultr,resultg,resultb;
long int mat[3][3],temp[250][250];
int hy[3][3],hx[3][3],hrx,hgx,hbx,hry,hgy,hby;
dcMem.CreateCompatibleDC(pDC);
dcMem.SelectObject(&m_bmpBitmap);
// Proses Konvolusi
int nh=3; // Menyatakan ukuran filter
// Penentuan kernel filter hy
hy[0][0]=-1; hy[0][1]=0; hy[0][2]=1;
hy[1][0]=-1; hy[1][1]=0; hy[1][2]=1;
hy[2][0]=-1; hy[2][1]=0; hy[2][2]=1;
// Penentuan kernel filter hx
hx[0][0]=-1; hx[0][1]=-1; hx[0][2]=-1;
hx[1][0]=0; hx[1][1]=0; hx[1][2]=0;
hx[2][0]=1; hx[2][1]=1; hx[2][2]=1;
f0r(i=0;i<250;i++)>
f0r(j=0;j<250;j++){>
mat[0][0]=dcMem.GetPixel(j-1,i-1);
mat[0][1]=dcMem.GetPixel(j,i-1);
mat[0][2]=dcMem.GetPixel(j+1,i-1);
mat[1][0]=dcMem.GetPixel(j-1,i);
mat[1][1]=dcMem.GetPixel(j,i);
mat[1][2]=dcMem.GetPixel(j+1,i);
mat[2][0]=dcMem.GetPixel(j-1,i+1);
mat[2][1]=dcMem.GetPixel(j,i+1);
mat[2][2]=dcMem.GetPixel(j+1,i+1);
hrx=0;hgx=0;hbx=0;
hry=0;hgy=0;hby=0;
f0r(u=0;u
f0r(v=0;v
WarnaToRGB(mat[u][v],&r,&g,&b);
hrx+= r*hx[u][v];
hgx+= g*hx[u][v];
hbx+= b*hx[u][v];
hry+= r*hy[u][v];
hgy+= g*hy[u][v];
hby+= b*hy[u][v];
}
resultr=abs(hrx)+abs(hry);
resultg=abs(hgx)+abs(hgy);
resultb=abs(hbx)+abs(hby);
if(resultr>255)resultr=255;
if(resultg>255)resultg=255;
if(resultb>255)resultb=255;
temp[j][i]=RGBToWarna(resultr,resultg,resultb);
}
f0r(i=0;i<250;i++)>
f0r(j=0;j<250;j++)>
dcMem.SetPixel(j,i,temp[j][i]);
pDC->BitBlt(250,0,500,250,&dcMem,0,0,SRCCOPY);
}

void CTgs1View::OnTestEdgesobel ()
{
// TODO: Add your command handler code here
CDC* pDC = GetDC();
CDC dcMem;
int i,j,r,g,b,u,v;
int resultr,resultg,resultb;
long int mat[3][3],temp[150][150];
int hy[3][3],hx[3][3],hrx,hgx,hbx,hry,hgy,hby;
dcMem.CreateCompatibleDC(pDC);
dcMem.SelectObject(&m_bmpBitmap);
// Proses Konvolusi
int nh=3; // Menyatakan ukuran filter
// Penentuan kernel filter hy
hy[0][0]=-1; hy[0][1]=0; hy[0][2]=1;
hy[1][0]=-2; hy[1][1]=0; hy[1][2]=2;
hy[2][0]=-1; hy[2][1]=0; hy[2][2]=1;
// Penentuan kernel filter hx
hx[0][0]=-1; hx[0][1]=-2; hx[0][2]=-1;
hx[1][0]=0; hx[1][1]=0; hx[1][2]=0;
hx[2][0]=1; hx[2][1]=2; hx[2][2]=1;
f0r(i=0;i<250;i++)>
f0r(j=0;j<250;j++){>
mat[0][0]=dcMem.GetPixel(j-1,i-1);
mat[0][1]=dcMem.GetPixel(j,i-1);
mat[0][2]=dcMem.GetPixel(j+1,i-1);
mat[1][0]=dcMem.GetPixel(j-1,i);
mat[1][1]=dcMem.GetPixel(j,i);
mat[1][2]=dcMem.GetPixel(j+1,i);
mat[2][0]=dcMem.GetPixel(j-1,i+1);
mat[2][1]=dcMem.GetPixel(j,i+1);
mat[2][2]=dcMem.GetPixel(j+1,i+1);
hrx=0;hgx=0;hbx=0;
hry=0;hgy=0;hby=0;
f0r(u=0;u
f0r(v=0;v
WarnaToRGB(mat[u][v],&r,&g,&b);
hrx+= r*hx[u][v];
hgx+= g*hx[u][v];
hbx+= b*hx[u][v];
hry+= r*hy[u][v];
hgy+= g*hy[u][v];
hby+= b*hy[u][v];
}
resultr=abs(hrx)+abs(hry);
resultg=abs(hgx)+abs(hgy);
resultb=abs(hbx)+abs(hby);
if(resultr>255)resultr=255;
if(resultg>255)resultg=255;
if(resultb>255)resultb=255;
temp[j][i]=RGBToWarna(resultr,resultg,resultb);
}
f0r(i=0;i<250;i++)>
f0r(j=0;j<250;j++)>
dcMem.SetPixel(j,i,temp[j][i]);
pDC->BitBlt(250,0,500,250,&dcMem,0,0,SRCCOPY);

Tidak ada komentar:

Posting Komentar