import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
//Class yang mendefinisikan sebuah gambar dalam OF (Objects First) format
public class OF extends BufferedImage
{
//Membuat gambar yang dicopy dari sebuah BufferedImage
public OF(BufferedImage img)
{
super(img.getColorModel(), img.copyData(null), img.isAlphaPremultiplied(), null);
}
//Membuat gambar OF dengan ukuran tertentu
public OF(int width, int height)
{
super(width, height, TYPE_INT_RGB);
}
//Mengatur pixel ke warna RGB
//dengan x dan y adalah posisi pixel, dan color adalah warna pixel
public void setPixel (int x, int y, Color color)
{
int pixel=color.getRGB();
setRGB(x, y, pixel);
}
//Mendapatkan value color pada pixel tertentu
public Color getPixel(int x, int y)
{
int pixel = getRGB(x, y);
return new Color (pixel);
}
//Untuk membuat gambar menjadi lebih gelap
public void dark ()
{
int height = getHeight();
int width = getWidth();
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
setPixel(x,y,getPixel(x,y).darker());
}
}
}
//Untuk membuat gambar menjadi lebih terang
public void bright ()
{
int height = getHeight();
int width = getWidth();
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
setPixel(x,y,getPixel(x,y).brighter());
}
}
}
//Threshold: membuat gambar hanya berwarna hitam, putih, abu-abu
public void threshold()
{
int height = getHeight();
int width = getWidth();
for (int y=0; y<height; y++)
{
for (int x=0; x<width; x++)
{
Color pixel = getPixel(x,y);
int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen())/3;
if (brightness <= 85)
{
setPixel (x,y,Color.BLACK);
}
else if (brightness <= 170)
{
setPixel (x, y, Color.GRAY);
}
else
setPixel (x, y, Color.WHITE);
}
}
}
}