00001 package commandline.ImageTools;
00002
00003
00004
00005 import java.awt.Point;
00006 import java.awt.image.BandedSampleModel;
00007 import java.awt.image.Raster;
00008 import java.awt.image.SampleModel;
00009 import java.awt.image.WritableRaster;
00010 import java.io.File;
00011 import java.io.FileNotFoundException;
00012 import java.net.MalformedURLException;
00013 import java.net.URL;
00014
00015 import javax.media.jai.JAI;
00016 import javax.media.jai.PlanarImage;
00017
00018 import commandline.debuggingCode.Debug;
00019
00020 public class ImportImage{
00021
00022 public static WritableRaster getRasterForImage(PlanarImage image) throws NoClassDefFoundError{
00023 WritableRaster r;
00024 r=image.copyData();
00025 return r;
00026 }
00027
00028 public static WritableRaster Kleur_naar_grijs(PlanarImage image) throws NoClassDefFoundError{
00029
00030
00031 Raster original_raster = image.getData();
00032 int breedte = original_raster.getWidth();
00033 int hoogte = original_raster.getHeight();
00034 int aantal = original_raster.getNumDataElements();
00035 if(original_raster.getNumBands()==1) {
00036 if(Debug.DEBUG) Debug.print("was already a gray scale image");
00037 return ImportImage.getRasterForImage(image);}
00038
00039 if(!Debug.DISTIBUTION) System.out.print("color -> gray ....");
00040 SampleModel model = new BandedSampleModel(original_raster
00041 .getSampleModel().getDataType(), breedte, hoogte, 1);
00042 WritableRaster gray_raster = Raster.createWritableRaster(model,
00043 new Point(0, 0));
00044
00045 float[] farray = new float[aantal];
00046 double grijswaarde;
00047 for (int i = 0; i != breedte; i++) {
00048 for (int j = 0; j != hoogte; j++) {
00049 original_raster.getPixel(i, j, farray);
00050 grijswaarde=0;
00051 for(int k=0;k!=aantal;k++){
00052 grijswaarde+=farray[k];
00053 }
00054 grijswaarde/=3d;
00055 gray_raster.setPixel(i,j,new double[]{grijswaarde});
00056 }
00057 }
00058 if(!Debug.DISTIBUTION) Debug.print("......done");
00059 return gray_raster;
00060 }
00061
00067 public static PlanarImage leesBeeld(String path) throws FileNotFoundException{
00068
00069 if(path==null || !(new File(path).isFile())){throw new FileNotFoundException("path is not a file: path= " + path); }
00070
00071 PlanarImage source = null;
00072 URL url=null;
00073 try {
00074 url = new URL("file:///"+path);
00075 if(!(new File(path).exists())) throw new FileNotFoundException("PlanarImage"+ url.toString() + " not found");
00076 if(!Debug.DISTIBUTION) Debug.print("image read");
00077 } catch (MalformedURLException e) {
00078 System.out.print(e.getMessage());
00079 throw new FileNotFoundException(url.toString());
00080 }
00081 try{
00082 source = JAI.create("url", url);
00083 }
00084 catch (Exception e) {
00085 System.err.println("exception in ImportImage "+ path);
00086 e.printStackTrace();
00087 }
00088
00089 return source;
00090 }
00091
00092
00093 }