1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
/**
* This class is a wrap-up of the classic BufferedImage class. It provides simplified accessing method for RGB image.
* You may want to display this image by enclosing it into an ImageFrame object.
*
*
*/
public class ImageBuffer extends BufferedImage {
public ImageBuffer(int width, int height) {
super(width, height, BufferedImage.TYPE_INT_ARGB);
}
static public ImageBuffer loadFromDisk( String __filename)
{
ImageBuffer image = null;
try {
BufferedImage bi = ImageIO.read(new File(__filename));
image = new ImageBuffer(bi.getWidth(),bi.getHeight());
for ( int x = 0 ; x != bi.getWidth() ; x++ )
for ( int y = 0 ; y != bi.getHeight() ; y++ )
image.setRGB(x, y, bi.getRGB(x, y));
}
catch ( IOException e )
{
System.err.println("[error] image \""+__filename+"\" could not be loaded.");
}
return image;
}
/**
* save current image buffer to PNG file.
* @param __filename output filename without extension
*/
public void saveToDisk( String __filename )
{
try {
ImageIO.write(this, "png", new File(__filename + ".png"));
}
catch ( IOException e )
{
System.err.println("[error] image \""+__filename+"\" could not be written to disk.");
}
}
/**
* set target pixel color (RGB) -- color values btw 0 and 255
* @param x
* @param y
* @param rgb a 3-values array containing [red,green,blue] values
*/
public void setPixel( int x, int y , int rgb[] )
{
this.setPixel(x, y, rgb[0] , rgb[1] , rgb[2]);
}
/**
* set target pixel color (RGB) -- color values btw 0 and 255
* @param x
* @param y
* @param red
* @param green
* @param blue
*/
public void setPixel( int x, int y , int red, int green, int blue )
{
int colorValues;
colorValues = 0xFF000000; // alpha set non transparent
colorValues += red * 256*256; // red
colorValues += green * 256; // green
colorValues += blue ; // blue
this.setRGB(x, y, colorValues);
}
/**
* return target pixel color
* @param x
* @param y
* @return a 3-values array containing [red,green,blue] values -- color values btw 0 and 255
*/
public int[] getPixel( int x, int y )
{
int [] rgb = new int[3];
int rawvalue = this.getRGB(x, y);
rgb[0] = ( rawvalue & 0x00FF0000 ) / (int)Math.pow(256,2); // red
rgb[1] = ( rawvalue & 0x0000FF00 ) / 256; // green
rgb[2] = ( rawvalue & 0x000000FF ); // blue
return rgb;
}
// HSV methods (same as RGB but handles HSV values in and out) -- all values btw [0..1]
public double[] getPixelHSB( int x, int y )
{
int[] rgb = this.getPixel(x, y);
float[] hsvFloat = java.awt.Color.RGBtoHSB(rgb[0], rgb[1], rgb[2], null);
double[] hsv = new double[3];
for (int i = 0; i < hsv.length; i++)
hsv[i] = hsvFloat[i];
return hsv;
}
public void setPixelHSB( int x, int y , double[] hsb)
{
this.setPixelHSB(x, y, hsb[0], hsb[1], hsb[2]);
}
public void setPixelHSB( int x, int y , double h, double s, double b )
{
int rgb = java.awt.Color.HSBtoRGB((float)h,(float)s,(float)b);
this.setRGB(x, y, rgb);
}
/**
* fill screen with given color values (cls means "clear screen")
* @param r
* @param g
* @param b
*/
public void cls(int r, int g, int b)
{
for ( int x = 0 ; x != this.getWidth() ; x++ )
for ( int y = 0 ; y != this.getHeight() ; y++ )
this.setPixel(x,y,r,g,b);
}
public void writeText(String __text)
{
Graphics2D g = this.createGraphics();
g.setColor(Color.GREEN);
g.setFont(new Font("Lucida Sans Typewriter", Font.PLAIN, 36));
g.drawString("Hello, World!", 10, 50);
g.dispose();
}
public static void main(String[] args) throws IOException {
// build an ImageBuffer
ImageBuffer image = new ImageBuffer(16,16); // method 1 : call constructor
//ImageBuffer image = loadFromDisk("test"); // method 2 : image loading factory
// Set a pixel
int[] rgb = {255,0,0};
image.setPixel(0,0,rgb); // method 1a : wrap-up method from ImageBuffer
//image.setPixel(0,0,255,255,255,255); // method 1b : wrap-up method from ImageBuffer
//image.setRGB(0, 0, 0xFF00FF00); // method 2 : standard method from BufferedImage using only one integer value
// various specific wrap-up methods from ImageBuffer or general accessing method inherited from the BufferedImage object ...
int w = image.getWidth();
int h = image.getHeight();
for ( int j = 0 ; j != h ; j++ )
for ( int i = 0 ; i != w ; i++ )
image.setPixel(i, j, (int)((double)(i)/(double)w*255.),(int)((double)(j)/(double)h*255.),(int)((double)(j)/(double)h*255.));
System.out.println("number of bands : " + image.getRaster().getNumBands());
System.out.println("colors of origin pixel: " + image.getRGB(0, 0));
rgb = image.getPixel(0, 0);
System.out.println("colors of origin pixel (detailed) : ("+rgb[0]+","+rgb[1]+","+rgb[2]+")");
System.out.println("image size is ("+w+","+h+")");
// save to disk
image.saveToDisk("testdev");
System.out.println("file \"testdev.png\" created.");
// display image -- you may prefer using ImageFrame if you want to display an image.
Icon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
final JFrame f = new JFrame("Demo");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(label);
f.pack();
SwingUtilities.invokeLater(new Runnable(){
public void run() {
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
} |
Partager