FileDocCategorySizeDatePackage
x_fits.javaAPI DocExample6292Sun Dec 12 10:55:54 GMT 2004com.macfaq.net.www.content.image

x_fits

public class x_fits extends ContentHandler

Fields Summary
Constructors Summary
Methods Summary
public java.lang.ObjectgetContent(java.net.URLConnection uc)

  
    int width = -1;
    int height = -1;
    int bitpix = 16;
    int[] data = null;
    int naxis = 2;
    Hashtable header = null;
     
    DataInputStream dis = new DataInputStream(uc.getInputStream());
    header = readHeader(dis);

    bitpix = getIntFromHeader("BITPIX  ", -1, header);
    if (bitpix  <= 0) return null;
    naxis = getIntFromHeader("NAXIS   ", -1, header);
    if (naxis  < 1) return null;
    width = getIntFromHeader("NAXIS1  ", -1, header);
    if (width  <= 0) return null;
    if (naxis == 1) height = 1;
    else height = getIntFromHeader("NAXIS2  ", -1, header);
    if (height  <= 0) return null;
    
    if (bitpix == 16) {
      short[] theInput =  new short[height * width];  
      for (int i = 0; i < theInput.length; i++) {
        theInput[i] = dis.readShort();
      }
      data = scaleArray(theInput);
    }
    else if (bitpix == 32) {
      int[] theInput =  new int[height * width];  
      for (int i = 0; i < theInput.length; i++) {
        theInput[i] = dis.readInt();
      }
      data = scaleArray(theInput);
   }
   else if (bitpix == 64) { 
     long[] theInput =  new long[height * width];  
     for (int i = 0; i < theInput.length; i++) {
       theInput[i] = dis.readLong();
     }
     data = scaleArray(theInput);
   }
   else if (bitpix == -32) { 
      float[] theInput =  new float[height * width];  
      for (int i = 0; i < theInput.length; i++) {
        theInput[i] = dis.readFloat();
      }
      data = scaleArray(theInput);
    }
    else if (bitpix == -64) { 
      double[] theInput =  new double[height * width];  
      for (int i = 0; i < theInput.length; i++) {
        theInput[i] = dis.readDouble();
      }
      data = scaleArray(theInput);          
    } 
    else {
      System.err.println("Invalid BITPIX");
      return null;
    } // end if-else-if
 
    return new  MemoryImageSource(width, height, data, 0, width);

  
private intgetIntFromHeader(java.lang.String name, int defaultValue, java.util.Hashtable header)

  
    String s = "";
    int result = defaultValue;
      
    try {
      s = (String) header.get(name);
    }
    catch (NullPointerException ex) {     
      return defaultValue;
    }
    try {
      result = Integer.parseInt(s.trim());
    }
    catch (NumberFormatException ex) {  
      System.err.println(ex);
      System.err.println(s);
      return defaultValue;
    }
      
    return result;
  
  
private java.util.HashtablereadHeader(java.io.DataInputStream dis)

  
    int blocksize = 2880;
    int fieldsize = 80;
    String key, value;
    int linesRead = 0;
    
    byte[] buffer = new byte[fieldsize];
    
    Hashtable header = new Hashtable();
    while (true) {
      dis.readFully(buffer);
      key = new String(buffer, 0, 8, "ASCII");
      linesRead++;
      if (key.substring(0, 3).equals("END")) break;
      if (buffer[8] != '=" || buffer[9] != ' ") continue;
      value = new String(buffer, 10, 20, "ASCII");
      header.put(key, value);
    }
    int linesLeftToRead 
     = (blocksize - ((linesRead * fieldsize) % blocksize))/fieldsize;
    for (int i = 0; i < linesLeftToRead; i++) dis.readFully(buffer);
    
    return header;

  
private int[]scaleArray(short[] theInput)

  
    int data[] = new int[theInput.length];
    int max = 0;
    int min = 0;
    for (int i = 0; i < theInput.length; i++) {
      if (theInput[i] > max) max = theInput[i];
      if (theInput[i] < min) min = theInput[i];
    }
    long r = max - min;
    double a = 255.0/r;
    double b = -a * min;
    int opaque = 255;
    for (int i = 0; i < data.length; i++) {
      int temp = (int) (theInput[i] * a + b);
      data[i] =  (opaque << 24)  | (temp << 16)  | (temp << 8) | temp;
    }
    return data;

  
private int[]scaleArray(int[] theInput)

  
    int data[] = new int[theInput.length];
    int max = 0;
    int min = 0;
    for (int i = 0; i < theInput.length; i++) {
      if (theInput[i] > max) max = theInput[i];
      if (theInput[i] < min) min = theInput[i];
    }
    long r = max - min;
    double a = 255.0/r;
    double b = -a * min;
    int opaque = 255;
    for (int i = 0; i < data.length; i++) {
      int temp = (int) (theInput[i] * a + b);
      data[i] =  (opaque << 24)  | (temp << 16)  | (temp << 8) | temp;
    }
    return data;

  
private int[]scaleArray(long[] theInput)

  
    int data[] = new int[theInput.length];
    long max = 0;
    long min = 0;
    for (int i = 0; i < theInput.length; i++) {
      if (theInput[i] > max) max = theInput[i];
      if (theInput[i] < min) min = theInput[i];
    }
    long r = max - min;
    double a = 255.0/r;
    double b = -a * min;
    int opaque = 255;
    for (int i = 0; i < data.length; i++) {
      int temp = (int) (theInput[i] * a + b);
      data[i] =  (opaque << 24)  | (temp << 16)  | (temp << 8) | temp;
    }
    return data;

  
private int[]scaleArray(double[] theInput)

  
    int data[] = new int[theInput.length];
    double max = 0;
    double min = 0;
    for (int i = 0; i < theInput.length; i++) {
      if (theInput[i] > max) max = theInput[i];
      if (theInput[i] < min) min = theInput[i];
    }
    double r = max - min;
    double a = 255.0/r;
    double b = -a * min;
    int opaque = 255;
    for (int i = 0; i < data.length; i++) {
      int temp = (int) (theInput[i] * a + b);
      data[i] =  (opaque << 24)  | (temp << 16)  | (temp << 8) | temp;
    }
    return data;

  
private int[]scaleArray(float[] theInput)

  
    int data[] = new int[theInput.length];
    float max = 0;
    float min = 0;
    for (int i = 0; i < theInput.length; i++) {
      if (theInput[i] > max) max = theInput[i];
      if (theInput[i] < min) min = theInput[i];
    }
    double r = max - min;
    double a = 255.0/r;
    double b = -a * min;
    int opaque = 255;
    for (int i = 0; i < data.length; i++) {
      int temp = (int) (theInput[i] * a + b);
      data[i] =  (opaque << 24)  | (temp << 16)  | (temp << 8) | temp;
    }
    return data;