Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.image.*;
- import javax.swing.*;
- public class OFImage extends BufferedImage
- {
- /**
- * Buat OFImage yang merupakan salinan BufferedImage.
- * @param image Gambar yang akan disalin.
- */
- public OFImage(BufferedImage image)
- {
- super(image.getColorModel(), image.copyData(null), image.isAlphaPremultiplied(), null);
- }
- /**
- * Membuat OFImange dengan ukuran yang telah dispesifikasi dan konten yang belum dispesifikasi
- * @param width Lebar dari gambar
- * @param height Tinggi dari gambar
- */
- public OFImage(int width, int height)
- {
- super(width, height, TYPE_INT_RGB);
- }
- /**
- * Atur pixel gambar dengan warna yang spesifik (r,g,b).
- * @param x Posisi x dalam pixel
- * @param y Posisi y dalam pixel
- * @param col Warna pixel
- */
- public void setPixel(int x, int y, Color col)
- {
- int pixel = col.getRGB();
- setRGB(x, y, pixel);
- }
- /**
- * Dapatkan value dari warna pada posisi pixel.
- * @param x Posisi x dalam pixel
- * @param y Posisi y dalam pixel
- * @return Warna pixel pada posisi yang sudah ditentukan
- */
- public Color getPixel(int x, int y)
- {
- int pixel = getRGB(x, y);
- return new Color(pixel);
- }
- // Buat gambar lebih gelap
- public void darker()
- {
- 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());
- }
- }
- // Buat gambar lebih cerah
- public void lighter()
- {
- 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());
- }
- }
- /**
- * Tampilkan three level threshold operation.
- * Untuk mewarnai ulang gambar dengan tiga warna: putih, abu-abu, hitam
- */
- 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);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment