import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.PixelGrabber;
public class optik extends Applet
{
Image
img;
int
lebar, tinggi;
int
warna, red, green, blue, alpha, abuabu, tempwarna;
String
tampil;
int
k=100; //brightness
int
hasilbitmapR, hasilbitmapG, hasilbitmapB, hasilabu;
int
P=190; //pusat
int G=3;
//penguat kontras
public
void init()
{
img=this.getImage(this.getDocumentBase(),
"wtp.jpg");
lebar
= img.getWidth(this);
tinggi
= img.getHeight(this);
}
public
void paint (Graphics g)
{
//beri
warna pada panel
g.setColor(Color.lightGray);
g.fillRect(0,0,2800,800);
//ambil
lebar dan tinggi citra
lebar
= img.getWidth(this);
tinggi
= img.getHeight(this);
//tampilkan
informasi lebar dan tinggi citra
g.setColor(Color.black);
tampil
= String.valueOf(lebar);
g.drawString("lebar:
"+tampil,10,20);
tampil
= String.valueOf(tinggi);
g.drawString("tinggi:
"+tampil,10,40);
//tampilkan
citra dengan drawImage
g.drawImage(img,10,50,this);
//tentukan
panjang array pixels
int
[] pixels = new int[lebar*tinggi];
PixelGrabber
pg = new PixelGrabber(img,0,0,lebar,tinggi, pixels,0,lebar);
try
{
pg.grabPixels();
//grab piksel citra ke object pg.
}
catch
(InterruptedException ie)
{
System.out.println("Terjadi
kesalahan saat mengambil data pixels");
ie.printStackTrace();
return;
}
g.drawString("Hasil
Pengolahan",lebar+100,20);
g.drawString("Normal",lebar+100,40);
g.drawString("Cerah",lebar+100+lebar+50,40);
g.drawString("Gelap",lebar+100+lebar+50+lebar+50,40);
g.drawString("Invers",lebar+100+lebar+50+lebar+50+lebar+50,40);
//bawah
g.drawString("Grayscale",lebar+100,40+tinggi+30);
g.drawString("Cerah",lebar+100+lebar+50,40+tinggi+30);
g.drawString("Gelap",lebar+100+lebar+50+lebar+50,40+tinggi+30);
g.drawString("Invers",lebar+100+lebar+50+lebar+50+lebar+50,40+tinggi+30);
//baris
3
g.drawString("rumus
1", lebar+100, 100+tinggi+70);
g.drawString("rumus 2", lebar+100+lebar+50, 100+tinggi+70);
g.setColor(Color.black);
//pembentukan
bitmap
int[][]
bitmapR = new int[lebar][tinggi];
int[][]
bitmapG = new int[lebar][tinggi];
int[][]
bitmapB = new int[lebar][tinggi];
for(int
j=0;j<tinggi;j++)
{
for(int
i=0;i<lebar;i++)
{
warna=pixels[j*lebar+i];
red=(warna
>> 16) & 0xff;
bitmapR[i][j]=red;
green=(warna
>> 8) & 0xff;
bitmapG[i][j]=green;
blue=(warna)
& 0xff;
bitmapB[i][j]=blue;
}
}
//brightness
for(int
j=0;j<tinggi;j++)
{
for(int
i=0;i<lebar;i++)
{
//normal
atas
g.setColor(new
Color(bitmapR[i][j], bitmapG[i][j], bitmapB[i][j]));
g.drawLine(i+lebar+100,
j+50, i+lebar+100, j+50);
//cerah
atas
hasilbitmapR
= bitmapR[i][j] + k;
hasilbitmapG = bitmapG[i][j] + k;
hasilbitmapB = bitmapB[i][j] + k;
if((hasilbitmapR
> 255))
{
hasilbitmapR
= 255;
}
if((hasilbitmapG
> 255))
{
hasilbitmapG
= 255;
}
if((hasilbitmapB
> 255))
{
hasilbitmapB
= 255;
}
g.setColor(new
Color(hasilbitmapR, hasilbitmapG, hasilbitmapB));
g.drawOval(i+lebar+100+lebar+50,
j+50, 1,1);
//gelap
atas
hasilbitmapR
= bitmapR[i][j] - k;
hasilbitmapG = bitmapG[i][j] - k;
hasilbitmapB = bitmapB[i][j] - k;
if((hasilbitmapR
< 0))
{
hasilbitmapR
= 0;
}
if((hasilbitmapG
< 0))
{
hasilbitmapG
= 0;
}
if((hasilbitmapB
< 0))
{
hasilbitmapB
= 0;
}
g.setColor(new
Color(hasilbitmapR, hasilbitmapG, hasilbitmapB));
g.drawOval(i+lebar+100+lebar+50+lebar+50,
j+50, 1,1);
//invers
atas
hasilbitmapR
= 255 - bitmapR[i][j];
hasilbitmapG = 255 - bitmapG[i][j];
hasilbitmapB = 255 - bitmapB[i][j];
g.setColor(new
Color(hasilbitmapR, hasilbitmapG, hasilbitmapB));
g.drawLine(i+lebar+100+lebar+50+lebar+50+lebar+50,
j+50, i+lebar+100+lebar+50+lebar+50+lebar+50, j+50);
//abu-abu
bawah
abuabu
= (bitmapR[i][j]+bitmapB[i][j]+bitmapB[i][j]) / 3;
g.setColor(new
Color(abuabu, abuabu, abuabu));
g.drawLine(i+lebar+100,
j+40+tinggi+40, i+lebar+100, j+40+tinggi+40);
//cerah
bawah
hasilabu
= abuabu + k;
if((hasilabu
> 255))
{
hasilabu
= 255;
}
g.setColor(new
Color(hasilabu, hasilabu, hasilabu));
g.drawOval(i+lebar+100+lebar+50,
j+40+tinggi+40, 1,1);
//gelap
bawah
hasilabu
= abuabu - k;
if((hasilabu
< 0))
{
hasilabu
= 0;
}
g.setColor(new
Color(hasilabu, hasilabu, hasilabu));
g.drawOval(i+lebar+100+lebar+50+lebar+50,
j+40+tinggi+40, 1,1);
//invers
bawah
hasilabu
= 255 - abuabu;
g.setColor(new
Color(hasilabu, hasilabu, hasilabu));
g.drawLine(i+lebar+100+lebar+50+lebar+50+lebar+50,
j+40+tinggi+40, i+lebar+100+lebar+50+lebar+50+lebar+50, j+40+tinggi+40);
}
}
//grayscale
kontras rumus 1
for(int
j=0;j<tinggi;j++)
{
for(int
i=0;i<lebar;i++)
{
abuabu
= (bitmapR[i][j]+bitmapB[i][j]+bitmapB[i][j]) / 3;
abuabu
= (G * (abuabu-P))+P;
if
(abuabu < 0)
{abuabu
= 0;}
if (abuabu > 255)
{abuabu
= 255;}
//cek
rumus 1
g.setColor(new
Color(abuabu, abuabu, abuabu));
g.drawLine(i+lebar+100,
j+100+tinggi+80, i+lebar+100, j+100+tinggi+80);
}
}
//grayscale
kontras rumus 2
int
max=0;
for(int
j=0;j<tinggi;j++)
{
for(int
i=0;i<lebar;i++)
{
abuabu
= (bitmapR[i][j]+bitmapB[i][j]+bitmapB[i][j]) / 3;
abuabu
= (G * (abuabu-P))+P;
if
(abuabu > max)
{max
= abuabu;}
}
}
for(int
j=0;j<tinggi;j++)
{
for(int
i=0;i<lebar;i++)
{
abuabu
= (bitmapR[i][j]+bitmapB[i][j]+bitmapB[i][j]) / 3;
abuabu
= abuabu+abuabu*((255-max)/max);
if(abuabu<0)
{
abuabu=0;
}
if(abuabu>255)
{
abuabu=255;
}
//cek
rumus 2
g.setColor(new
Color(abuabu, abuabu, abuabu));
g.drawLine(i+lebar+100+lebar+50,
j+100+tinggi+80, i+lebar+100+lebar+50, j+100+tinggi+80);
}
}
}
public
void repaint (Graphics g)
{
//kosongkan
saja
}
}
0 komentar:
Posting Komentar