00001 package commandline.dataToImage;
00002
00003 import java.awt.image.BufferedImage;
00004 import java.awt.image.WritableRaster;
00005 import java.io.File;
00006 import java.io.IOException;
00007 import java.io.PrintStream;
00008
00009 import javax.imageio.ImageIO;
00010
00011 import commandline.debuggingCode.Debug;
00012
00013
00018 public class GrayImage {
00019
00020 BufferedImage image;
00021
00022 WritableRaster raster;
00023
00027 public GrayImage(int width, int height) {
00028
00029
00030
00031
00032 image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
00033
00034 raster = image.getRaster();
00035
00036 }
00037
00038 public void setSample(int x, int y, int grijswaarde) {
00039
00040
00041
00042 if(grijswaarde<0){
00043 if(!Debug.DISTIBUTION) Debug.print("grijswaarde " + grijswaarde);
00044 grijswaarde=0;
00045 }
00046 if(grijswaarde>255){
00047 if(!Debug.DISTIBUTION) Debug.print("grijswaarde " + grijswaarde);
00048 grijswaarde=255;
00049 }
00050
00051
00052
00053 raster.setSample(x, y, 0, grijswaarde);
00054 }
00055
00056
00057 public BufferedImage getBufferedImage(){
00058 return this.image;
00059 }
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 public void writePNG(File toFile) throws IOException {
00071
00072 ImageIO.write(image, "PNG", toFile);
00073
00074 }
00075
00076 public void print(PrintStream p){
00077 for(int x=0;x!=raster.getWidth();x++){
00078 for(int y=0;y!=raster.getHeight();y++){
00079 p.print(raster.getSample(x,y,0)+ " ");
00080 }
00081 p.println("");
00082 }
00083
00084 }
00085
00086 public GrayImage getAbsoluteDifference(GrayImage grayImage) throws ImageNotEquallySizedException{
00087 int height=grayImage.getBufferedImage().getHeight();
00088 int width=grayImage.getBufferedImage().getWidth();
00089 int depth=grayImage.getBufferedImage().getRaster().getNumBands();
00090 if(this.getBufferedImage().getHeight()!=height || this.getBufferedImage().getWidth()!=width ||
00091 this.getBufferedImage().getRaster().getNumBands()!=depth){
00092 throw new ImageNotEquallySizedException();
00093 }
00094 GrayImage difference= new GrayImage(width,height);
00095 for(int i=0;i!=width;i++){
00096 for (int k=0;k!=height;k++) {
00097 difference.setSample(i,k,Math.abs(this.getBufferedImage().getRaster().getSample(i,k,0)
00098 -grayImage.getBufferedImage().getRaster().getSample(i,k,0)));
00099 }
00100 }
00101 return difference;
00102 }
00103
00104 }