import java.net.*; import java.io.*; import java.awt.image.*; import java.util.Hashtable; import java.util.Enumeration; public class fitsContentHandler extends ContentHandler { public Object getContent(URLConnection uc) { int width = -1; int height = -1; int bitpix = 16; int[] theData = null; int naxis = 2; Hashtable header = null; try { 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(); } theData = scaleArray(theInput); } else if (bitpix == 32) { int[] theInput = new int[height * width]; for (int i = 0; i < theInput.length; i++) { theInput[i] = dis.readInt(); } theData = scaleArray(theInput); } else if (bitpix == 64) { long[] theInput = new long[height * width]; for (int i = 0; i < theInput.length; i++) { theInput[i] = dis.readLong(); } theData = scaleArray(theInput); } else if (bitpix == -32) { float[] theInput = new float[height * width]; for (int i = 0; i < theInput.length; i++) { theInput[i] = dis.readFloat(); } theData = scaleArray(theInput); } else if (bitpix == -64) { double[] theInput = new double[height * width]; for (int i = 0; i < theInput.length; i++) { theInput[i] = dis.readDouble(); } theData = scaleArray(theInput); } else { System.err.println("Invalid BITPIX"); return null; } // end if-else-if } // end try catch (IOException e) { } return new MemoryImageSource(width, height, theData, 0, width); } // end getContent Hashtable readHeader(DataInputStream dis) throws IOException { int blocksize = 2880; int fieldsize = 80; String key, value; int linesRead = 0; byte[] buffer = new byte[fieldsize]; Hashtable h = new Hashtable(); while (true) { dis.readFully(buffer); key = new String(buffer, 0, 0, 8); linesRead++; if (key.substring(0, 3).equals("END")) break; if (buffer[8] != '=' || buffer[9] != ' ') continue; value = new String(buffer, 0, 10, 20); h.put(key, value); } int linesLeftToRead = (blocksize - ((linesRead * fieldsize) % blocksize))/fieldsize; for (int i = 0; i < linesLeftToRead; i++) dis.readFully(buffer); return h; } int getIntFromHeader(String name, int Default, Hashtable header) { String s = ""; int result = Default; try { s = (String) header.get(name); } catch (NullPointerException e) { return Default; } try { result = Integer.parseInt(s.trim()); } catch (NumberFormatException e) { System.err.println(e); System.err.println(s); return Default; } return result; } int[] scaleArray(short[] theInput) { int theData[] = 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 < theData.length; i++) { int temp = (int) (theInput[i] * a + b); theData[i] = (opaque << 24) | (temp << 16) | (temp << 8) | temp; } return theData; } int[] scaleArray(int[] theInput) { int theData[] = 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 < theData.length; i++) { int temp = (int) (theInput[i] * a + b); theData[i] = (opaque << 24) | (temp << 16) | (temp << 8) | temp; } return theData; } int[] scaleArray(long[] theInput) { int theData[] = 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 < theData.length; i++) { int temp = (int) (theInput[i] * a + b); theData[i] = (opaque << 24) | (temp << 16) | (temp << 8) | temp; } return theData; } int[] scaleArray(double[] theInput) { int theData[] = 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 < theData.length; i++) { int temp = (int) (theInput[i] * a + b); theData[i] = (opaque << 24) | (temp << 16) | (temp << 8) | temp; } return theData; } int[] scaleArray(float[] theInput) { int theData[] = 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 < theData.length; i++) { int temp = (int) (theInput[i] * a + b); theData[i] = (opaque << 24) | (temp << 16) | (temp << 8) | temp; } return theData; } }