I have a binary file of 12 bits integers obtained from a camera software. It can produce FITS files from these but the processing time on the large data set I have is excessive. What I have is the disk cache file that contains the same information.

How do I import a binary file/stream by reading 12 bits at at time? I know the size of the array (2560*2160) and the excess length at the end of the file that isn't actual data. I can configure the same camera for 16 bits image and here is how I proceed in this case.

   BinaryReadList[path,"Integer16"], -5120]], {2160,2560}]

It works very well and is quite fast. I'm looking to do the same for 12 bits data.

  • $\begingroup$ Maybe Import[path, "Bit"]? $\endgroup$ – Rohit Namjoshi Mar 4 '20 at 2:27
  • $\begingroup$ I guess, the best and fastest way is reading bytes then compose your 12-bit numbers from two successive bytes and so on.. $\endgroup$ – Rom38 Mar 4 '20 at 3:44
  • 1
    $\begingroup$ You might consider using IntegerDigits[#,16,2] to split bytes into nibbles and then you may convert to 12-bit values by simply using Partition to group as three nibbles. Please be careful of the bit ordering within the word. Depending on whether it is stored big or little endian, you may have to do some reversing of things. $\endgroup$ – Mark R Mar 4 '20 at 6:35
  • 3
    $\begingroup$ Can you share an example file of your 12-bit data? $\endgroup$ – halirutan Mar 4 '20 at 7:06
  • 1
    $\begingroup$ You probably already considered this, but is the image format one that MMA can directly read? Many native image formats may be read without needing to go through a BinaryReadList process. $\endgroup$ – Mark R Mar 4 '20 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.