1

This is now solved. See the code. It works now!!!

The Initial question was:

I need to store an image , received in a byte[] array, into a fits file. I'm using java 8 and fits.jar lib (nom.tam.fits).

The following code seems to work in first instance but when I open the fits file in ds9, it displays a narrow band instead of the image. In the following code, data is the byte[] array for the image (one byte per pixel)

        Fits f = new Fits();
        int[] uBytes = new int[imageData.length];//the byte[] with the pixels
        for (int i = 0; i < data.length; i+=1) {
            uBytes[i] = (int) (imageData[i] & 0xff);

        }
       int dims[] = new int[]{imageSize_x, imageSize_y};
        int [][]  data =  (int[][]) ArrayFuncs.curl(uBytes,dims);

        BasicHDU h= Fits.makeHDU(data);
        f.addHDU(h);
        OutputStream os = new FileOutputStream("testImage.fits");
        BufferedDataOutputStream s= new 
        BufferedDataOutputStream(os);
        f.write(s);
        f.close();

I must confess I was a bit silly since I've already managed to solve the same problem when tried to display the image with JavaFX JavaFX. Displaying an image from byte[]

dummyhead
  • 81
  • 2
  • 9
  • from [fits doc](https://heasarc.gsfc.nasa.gov/docs/heasarc/fits/java/v1.0/FitsLib.pdf): "Java bytes are signed, but FITS bytes are not. If any arithmetic processing is to be done on byte data, users may need to be careful of Java’s automated conversion of bytes to integers which includes sign extension. ..code example" – xerx593 Jan 03 '19 at 18:00
  • 1
    Many thanks for your answer. In addition to your suggestion I had also to curl the byte[] (as I did in the mentioned thread for displaying it into java)...The code has been updated to show the right implementation . – dummyhead Jan 04 '19 at 17:11
  • 1
    rather a comment than "an answer" :) , but I am glad it helped. Welcome! (Just a hint for a newbee: You can also post & accept your own answer;) – xerx593 Jan 04 '19 at 17:36

0 Answers0