Advertisement
mbah_bejo

OFImage

Nov 23rd, 2020
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.image.BufferedImage;
  3.  
  4.  
  5. /**
  6.  * OFImage ialah Class yang mendefinisikan image dalam format Object First
  7.  */
  8. public class OFImage extends BufferedImage {
  9.  
  10.     /**
  11.      * membuat Copy-an OFimage dari BufferedImage
  12.      * @param image image yang mau di-copy
  13.      */
  14.     public OFImage(BufferedImage image){
  15.         super(image.getColorModel(), image.copyData(null),
  16.                 image.isAlphaPremultiplied(), null);
  17.     }
  18.  
  19.     /**
  20.      * memmbuat OFImage dengan ukuran yang spesifik dan
  21.      * konten yang tak spesifik
  22.      * @param width lebar gambar
  23.      * @param height tinggi gambar
  24.      */
  25.     public OFImage( int width, int height){
  26.         super(width,height,TYPE_INT_RGB);
  27.     }
  28.  
  29.     /**
  30.      * mengatur pixel yang diberikan dari image ke warna yang spesifik
  31.      * color merepresentasikan nilai RGB
  32.      * @param x posisi-x dari pixel
  33.      * @param y posisi-y dari pixel
  34.      * @param col warna dari pixel
  35.      */
  36.     public void setPixel(int x, int y, Color col){
  37.         int pixel =col.getRGB();
  38.         setRGB(x, y, pixel);
  39.     }
  40.  
  41.     /**
  42.      * mendapatkan nilai warna pada posisi spesifik pada pixel
  43.      * @param x posisi-x dari pixel
  44.      * @param y posisi-y dari pixel
  45.      * @return warna dari posisi [ixel yang diberikan
  46.      */
  47.     public Color getPixel(int x, int y){
  48.         int pixel = getRGB(x, y);
  49.         return new Color(pixel);
  50.     }
  51.  
  52.     /**
  53.      * membuat gambar jadi sedikit lebih gelap
  54.      */
  55.     public void darker()
  56.     {
  57.         int height = getHeight();
  58.         int width = getWidth();
  59.         for(int y=0; y < height; y++){
  60.             for (int x=0; x < width; x++){
  61.                 setPixel(x,y, getPixel(x,y).darker());
  62.             }
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * membuat gambar jadi sedikit lebih terang
  68.      */
  69.     public void lighter(){
  70.         int height = getHeight();
  71.         int width = getWidth();
  72.         for(int y=0; y < height; y++){
  73.             for (int x=0; x < width; x++){
  74.                 setPixel(x,y, getPixel(x,y).brighter());
  75.             }
  76.         }
  77.     }
  78.  
  79.     /**
  80.      * melakukan tiga level opereasi thershold
  81.      */
  82.     public void threshold() {
  83.         int height = getHeight();
  84.         int width = getWidth();
  85.         for (int y = 0; y < height; y++) {
  86.             for (int x = 0; x < width; x++) {
  87.                 Color pixel = getPixel(x, y);
  88.                 int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen())/3;
  89.  
  90.                 if (brightness <=85){
  91.                     setPixel(x,y, Color.BLACK);
  92.                 } else if(brightness<=170){
  93.                     setPixel(x,y, Color.GRAY);
  94.                 } else {
  95.                     setPixel(x,y, Color.WHITE);
  96.                 }
  97.             }
  98.             }
  99.         }
  100.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement