00001
00002 package commandline.ImageStuff;
00003
00004 import java.awt.Dimension;
00005 import java.awt.image.WritableRaster;
00006 import java.io.FileNotFoundException;
00007 import java.util.ArrayList;
00008
00009 import commandline.ImageTools.ImportImage;
00010 import commandline.NormalMeshAlgorithm.Configuration;
00011 import commandline.debuggingCode.Debug;
00012
00013
00014 public class Image {
00015
00016 public int xresolutie;
00017
00018 public int yresolutie;
00019
00020 private int MAXZ;
00021
00022 protected Pixel[][] pixelmatrix;
00023
00024 private static Image instance;
00025
00026 public static Image getInstance() {
00027 if (instance != null)
00028 return instance;
00029 Debug.print("eerst beeld zetten alvorens te getten!!");
00030 System.exit(-1);
00031 return null;
00032 }
00033
00034 private static WritableRaster writableRaster;
00035
00036 public static WritableRaster getWritableRaster() {
00037 return writableRaster;
00038 }
00039
00040 private static void setWritableRaster(WritableRaster writableRaster1) {
00041 writableRaster = writableRaster1;
00042 }
00043
00044
00045 public static void setInstance(String path, Configuration conf) {
00046
00047 try {
00048 setInstance(ImportImage.Kleur_naar_grijs(ImportImage.leesBeeld(path)),conf);
00049 } catch (FileNotFoundException e) {
00050
00051 e.printStackTrace();
00052 System.exit(-1);
00053 }
00054 }
00055
00056
00057 public static void setInstance(WritableRaster r, Configuration conf) {
00058 setWritableRaster(r);
00059 int breedte = r.getWidth();
00060 int hoogte = r.getHeight();
00061 Pixel[][] static_pixelmatrix = new Pixel[breedte][hoogte];
00062 double[] grijswaarde = new double[1];
00063 for (int i = 0; i != breedte; i++) {
00064 for (int j = 0; j != hoogte; j++) {
00065 r.getPixel(i, j, grijswaarde);
00066 static_pixelmatrix[i][j] = new Pixel(i, j,
00067 (float) grijswaarde[0]);
00068 }
00069 }
00070 instance = new Image(static_pixelmatrix);
00071 }
00072
00073 public static void setInstance(Dimension d) {
00074 System.out.println("The resolution of the image you want to " +
00075 "load in memory equals" +
00076 " "+d.getWidth() + "x"+d.getHeight());
00077 if(d.getHeight()>2500 || d.getWidth() > 2500){
00078 System.out.println(
00079 " and is probably to high due to memory limitations"
00080 );
00081 System.out.println("The system will now exit");
00082 System.exit(-1);
00083 }
00084
00085
00086 int breedte =(int) d.getWidth();
00087 int hoogte =(int) d.getHeight();
00088 Pixel[][] static_pixelmatrix = new Pixel[breedte][hoogte];
00089 for (int i = 0; i != breedte; i++) {
00090 for (int j = 0; j != hoogte; j++) {
00091 static_pixelmatrix[i][j] = new Pixel(i, j,0);
00092 }
00093 }
00094 instance = new Image(static_pixelmatrix);
00095 }
00096
00097
00098
00099 private Image(Pixel[][] pixelmatrix) {
00100 this.pixelmatrix = pixelmatrix;
00101 xresolutie = pixelmatrix.length;
00102 yresolutie = pixelmatrix[0].length;
00103 MAXZ = 300;
00104 }
00105
00106
00107
00108 public Image(int xresolutie, int yresolutie) {
00109 this.xresolutie = xresolutie;
00110 this.yresolutie = yresolutie;
00111 pixelmatrix = new Pixel[xresolutie][yresolutie];
00112 for (int i = 0; i != pixelmatrix.length; i++) {
00113 for (int j = 0; j != pixelmatrix[0].length; j++) {
00114 pixelmatrix[i][j] = new Pixel(i, j, 0);
00115 }
00116 }
00117
00118 }
00119
00120
00121 public boolean contains(Loc l){
00122 return (l.x>=0 && l.x<=getBreedte()-1
00123 && l.y>=0 && l.y<=getHoogte()-1);
00124 }
00125
00126 public float getSobel(Loc loc){
00133
00134 float a= 1f/4f;
00135
00136 float c= -1f/4f;
00137
00138 float g= -1f/4f;
00139 float i= 1f/4f;
00140
00141 float waarde_a=0;
00142 float waarde_c=0;
00143 float waarde_g=0;
00144 float waarde_i=0;
00145
00146 Loc l=new Loc(loc.x-1,loc.y);
00147
00148
00149 l.setX(loc.x-1);
00150 l.setY(loc.y+1);
00151
00152 if(contains(l)){
00153 waarde_a=getInstance().getPuntOpEnty(l).getZ();
00154 }else{ a=0; i=0; g=1/2;i=-1/2; }
00155
00156 l.setX(loc.x+1);
00157 l.setY(loc.y+1);
00158 if(contains(l)){
00159 waarde_c=getInstance().getPuntOpEnty(l).getZ();
00160 }
00161
00162 l.setX(loc.x-1);
00163 l.setY(loc.y-1);
00164 if(contains(l)){
00165 waarde_g=getInstance().getPuntOpEnty(l).getZ();
00166 }
00167
00168 l.setX(loc.x+1);
00169 l.setY(loc.y-1);
00170 if(contains(l)){
00171 waarde_i=getInstance().getPuntOpEnty(l).getZ();
00172 }
00173 return a*waarde_a+c*waarde_c+g*waarde_g+i*waarde_i;
00174
00175 }
00176
00177
00178 public boolean isOnCorner(Loc l){
00179 if(( l.getX()==0 && l.getY()==this.getHoogte()-1)
00180 || (l.getX()==this.getBreedte()-1 && l.getY()==this.getHoogte()-1)
00181 || (l.getX()==0 && l.getY()==0)
00182 || (l.getX()==this.getBreedte()-1 && l.getY()==0)
00183 ){
00184 return true;
00185 }
00186 return false;
00187 }
00188
00189 public ArrayList<Pixel> getHoekPunten() {
00190 ArrayList<Pixel> hoekpunten = new ArrayList<Pixel>(4);
00191 hoekpunten.add(this.getPuntOpEnty(0, 0));
00192 hoekpunten.add(this.getPuntOpEnty(0, this.getHoogte() - 1));
00193 hoekpunten.add(this.getPuntOpEnty(this.getBreedte() - 1, 0));
00194 hoekpunten.add(this.getPuntOpEnty(this.getBreedte() - 1, this
00195 .getHoogte() - 1));
00196 return hoekpunten;
00197 }
00198
00199 public int getMaxZ() {
00200 return MAXZ;
00201 }
00202
00203 public int getBreedte() {
00204 return this.xresolutie;
00205 }
00206
00207 public int getHoogte() {
00208 return this.yresolutie;
00209 }
00210
00211 public final Pixel getPuntOpEnty(int i, int j) {
00212 return this.pixelmatrix[i][j];
00213 }
00214
00215 public final Pixel getPuntOpEnty(Loc loc) {
00216 Pixel p = this.getPuntOpEnty(loc.getX(), loc.getY());
00217 if (!p.isOn(loc)) {
00218 Debug.print("Major Error in Image-> getpunt op Entry");
00219 System.exit(-1);
00220 }
00221 return this.getPuntOpEnty(loc.getX(), loc.getY());
00222 }
00223
00224 public void setValue(Loc l, float value) {
00225 this.pixelmatrix[l.getX()][l.getY()].setZ(value);
00226 }
00227
00228 }
00229