4

I have a 32-bit tiff file which stores 4-byte values per pixel. I used BitConverter.toSingle() to convert each 4-byte values to the pixel value and it works fine. But the problem is I have 24 and 16-bit tiff files which there are 3 and 2 bytes for each pixel value. I can't use BitConverter.toSingle() method because it only supports an array of 4 bytes. How can I convert these byte values to float numbers?

Update 1:

I added an image just for clarifying that the pixel values are the floating number (look at the comments). enter image description here

Nasser Tahani
  • 725
  • 12
  • 33
  • You need to do it byte-by-byte - read each byte from the input stream (pixel data) and then convert it to a 32-bit in-memory pixel. – xxbbcc May 17 '17 at 15:06
  • 2
    manually, basically: process the raw bytes, and get familiar with shifting, bit-masks, etc; for example, processing 3 bytes in little endian order would be `data[index++] | (data[index++] << 8) | (data[index++] << 16)`; if you need to convert between integer and floating point, then it'll be trickier, since there is no inbuilt 3-byte floating point value in .NET - are you sure it isn't just an integer? – Marc Gravell May 17 '17 at 15:10
  • @xxbbcc thanks for your comment. May you give an example of how to do it, please. – Nasser Tahani May 17 '17 at 15:26
  • @nassertahani Marc just did, read his comment. – xxbbcc May 17 '17 at 15:27
  • @MarcGravell sorry but I couldn't understand how it works after surfing the web. If I have a byte array like "byte[ ] b = {0,88,68}" which I know the value is 43, how can convert it with the bitwise operator? "b[0] | b[1] << 8 | b[2] << 16" gives me the wrong value. – Nasser Tahani May 18 '17 at 07:36
  • @nasser well, what makes that 43? What storage format is that? It isn't big or little endian twos complement, which is what I discussed – Marc Gravell May 18 '17 at 07:40
  • @MarcGravell, it is a floating number 43.0 and little endian. The pixel values of tiff file are 24bit depth. – Nasser Tahani May 18 '17 at 07:46
  • @nassertahani yeah, but that still doesn't tell me how the `0,88,68` becomes 43; if we're talking abut floating point, then it becomes a question of knowing which bits are the mantissa, which bits are the exponent, and that the *bias* is; I can't tell you those things - only the format specification can tell you that; this related question may help: http://stackoverflow.com/questions/26930117/float24-24-bit-floating-point-to-hex – Marc Gravell May 18 '17 at 08:34
  • @MarcGravell thanks for your reply, those values (0,88,68) are gained when reading the 24-bit Tiff using Libtiff.Net. The library store 3 bytes for each pixel (It's grayscale, not RGB). I should tell that the mantissa is always 0 and the float numbers are like int values (43.0, 385,0). – Nasser Tahani May 18 '17 at 09:13
  • @nassertahani if the mantissa is always zero, then ... every value is zero. So, I doubt that's right. Also, pixel values are usually integers, not floating point. – Marc Gravell May 18 '17 at 09:52
  • @MarcGravell I mean the fractional part is zero, sorry for that. I also added an image to the question to show that pixel values are floating number. – Nasser Tahani May 18 '17 at 11:31

0 Answers0