Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import java.awt.*;  
  2. import java.awt.*;  
  3. import java.awt.image.*;  
  4. import javax.swing.*;  
  5.  
  6.  public class ofImage extends BufferedImage  
  7.  {  
  8.    /**  
  9.     * Create an OFImage copied from a BufferedImage.  
  10.     * @parameter image The image to copy.  
  11.     */  
  12.    public ofImage (BufferedImage image)  
  13.    {  
  14.       super(image.getColorModel(), image.copyData(null),  
  15.          image.isAlphaPremultiplied(), null);  
  16.    }  
  17.    /**  
  18.     * Create an ofImage with specified size and unspecified content.  
  19.     * @parameter width The width of the image.  
  20.     * @parameter height The height of the image.  
  21.     */  
  22.    public ofImage(int width, int height)  
  23.    {  
  24.      super(width, height, TYPE_INT_RGB);  
  25.    }  
  26.    /**  
  27.     * Set a given pixel of this image to a specified color. The  
  28.     * color is represented as an (r,g,b) value.  
  29.     * @parameter x The x position of the pixel.  
  30.     * @parameter y The y position of the pixel.  
  31.     * @parameter col The color of the pixel.  
  32.     */  
  33.    public void setPixel(int x, int y, Color col)  
  34.    {  
  35.      int pixel = col.getRGB();  
  36.      setRGB(x, y, pixel);  
  37.    }  
  38.    /**  
  39.     * Get the color value at a specified pixel position.  
  40.     * @parameter x The x position of the pixel.  
  41.     * @parameter y The y position of the pixel.  
  42.     * @return The color of the pixel at the given position.  
  43.     */  
  44.    public Color getPixel(int x, int y)  
  45.    {  
  46.      int pixel = getRGB(x, y);  
  47.      return new Color(pixel);  
  48.    }  
  49.    public void darker()  
  50.    {  
  51.      int height = getHeight();  
  52.      int width = getWidth();  
  53.      for(int y = 0; y < height; y++) {  
  54.        for(int x = 0; x < width; x++) {  
  55.          setPixel(x, y, getPixel(x, y).darker());  
  56.        }  
  57.      }  
  58.    }  
  59.    /**  
  60.     * Make this image a bit lighter.  
  61.     */  
  62.    public void lighter()  
  63.    {  
  64.      int height = getHeight();  
  65.      int width = getWidth();  
  66.      for(int y = 0; y < height; y++) {  
  67.        for(int x = 0; x < width; x++) {  
  68.          setPixel(x, y, getPixel(x, y).brighter());  
  69.        }  
  70.      }  
  71.    }  
  72.  
  73.    public void threshold()  
  74.    {  
  75.      int height = getHeight();  
  76.      int width = getWidth();  
  77.      for(int y = 0; y < height; y++) {  
  78.        for(int x = 0; x < width; x++) {  
  79.          Color pixel = getPixel(x, y);  
  80.          int brightness = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3;  
  81.          if(brightness <= 85) {  
  82.            setPixel(x, y, Color.BLACK);  
  83.          }  
  84.          else if(brightness <= 170) {  
  85.            setPixel(x, y, Color.GRAY);  
  86.          }  
  87.          else {  
  88.            setPixel(x, y, Color.WHITE);  
  89.          }  
  90.        }  
  91.      }  
  92.    }  
  93.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement