Login  Register

Re: Float.floatToIntBits & decrements in byte values

Posted by Stephan Saalfeld on Dec 06, 2010; 9:38am
URL: http://imagej.273.s1.nabble.com/Float-floatToIntBits-decrements-in-byte-values-tp3686270p3686275.html

Hi,

not seeing the responsible code makes it complicated to guess.  Instead,
you could look at working code and compare to yours.  We've done similar
once for a simple import/export into the POV-Ray df3 format:

Reader:

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=src-plugins/IO_/io/Open_DF3.java;h=d8cfe4bb33f45942290adaca3a3af930121bf2d0;hb=HEAD#l100

Writer:

http://pacific.mpi-cbg.de/cgi-bin/gitweb.cgi?p=fiji.git;a=blob;f=src-plugins/IO_/io/Save_DF3.java;h=1d91b24dc491f22779fbe4c18ed72244296ba905;hb=HEAD#l198

The major difference that I can see is that we worked with long instead
int which might be to avoid sign-bit madness.  It's long ago that we've
done this.

Good luck,
Stephan



On Fri, 2010-12-03 at 17:24 +0000, Thomson Neil (East Kent Hospitals NHS
Trust) wrote:

> Dear fellow listers
>
> I'm writing a plugin to export a medical image format. Some of the header values are floats. The values are in a hash table and I then save the floats into my header-block by doing a Float.floatToIntBits conversion and then save the 4 byte values, which is (more or less) copied and pasted from the IJ tiff encoder.
>
> I currently have this code snippet in my format encoder:
>
> System.out.println("" + o_value);  //o_value is float value of some hashmap entry
> int v = Float.floatToIntBits((Float) o_value);System.out.println("" + v);
> System.out.println("" + (byte) ((v >>> 24) & 255));
> System.out.println("" + (byte) ((v >>> 16) & 255));
> System.out.println("" + (byte) ((v >>> 8) & 255));
> System.out.println("" + (byte) (v & 255));
> When float values of o_value these are small (say 1E5) everything seems fine, but when they get larger (say 1E7) the byte output does not seem to be as I would expect. If I import and then export ann image without manipulation, the float value decreases, which I've traced to the byte values: if I start by importing an image where this particular float header field has bytes reading (in big-endian)...
>
> 76,-46,-20,-18
>
> ... I then export the image and re-import it I get:
>
> 75,-47,-21,-18
>
> ... and again...:
>
> 74,-48,-22,-18
>
> A decrement in three of the four bytes. I'm at a loss as to where to go next and I would greatly appreciate some wise words.
>
> Many thanks, Neil
>
> Neil Thomson,
> Nuclear Medicine Physics Section,
> Medical Physics,
> Kent and Canterbury Hospital,
> UK. CT1 3NG
> +44 (0) 1227 766877
>
> ********************************************************************************************************************
>
> This message may contain confidential information. If you are not the intended recipient please inform the
> sender that you have received the message in error before deleting it.
> Please do not disclose, copy or distribute information in this e-mail or take any action in reliance on its contents:
> to do so is strictly prohibited and may be unlawful.
>
> Thank you for your co-operation.
>
> NHSmail is the secure email and directory service available for all NHS staff in England and Scotland
> NHSmail is approved for exchanging patient data and other sensitive information with NHSmail and GSi recipients
> NHSmail provides an email address for your career in the NHS and can be accessed anywhere
> For more information and to find out how you can switch, visit www.connectingforhealth.nhs.uk/nhsmail
>
> ********************************************************************************************************************