Re: read 12 bit array [message #75564] |
Sat, 02 April 2011 11:55 |
Wout De Nolf
Messages: 194 Registered: October 2008
|
Senior Member |
|
|
On Thu, 31 Mar 2011 08:00:38 -0700 (PDT), alx
<lecacheux.alain@wanadoo.fr> wrote:
> Read it as [3,N] byte array b, if the file contains an array of 2*N 12
> bits words.
> Then transform the result to an integer (16 bits) array by b = fix(b,
> 0, 3, N)
> Finally concatenate the first and last bytes of each triple with
> corresponding half parts of the middle byte, that is :
> [ b[0,*] or (ishft(b[1,*] and '0F'x,8), ishft(b[2,*],4) or
> ishft((b[1,*] and 'F0'x), -4) ]
> You should get your 12 bits array packed in a 16-bits array of same
> size.
> alx.
Thanks. I came up with something like this.
; 8bit array with "bigedian" endianness
arr=bindgen(48)
nbytes=n_elements(arr)
nadd=nbytes mod 6
if nadd ne 0 then arr=[arr,bytarr(nadd)]
; convert to 16bit array (endianness of machine)
arr=fix(arr,0,3,nbytes/6)
SWAP_ENDIAN_INPLACE, arr, SWAP_IF_BIG_ENDIAN=~bigendian,
SWAP_IF_LITTLE_ENDIAN=bigendian
; convert to 12bit array
; Each tripple of 16bit numbers: [4 4 4][4 \ 4 4][4 4 \ 4][4 4 4]
arr=[ishft(arr[0,*],-4),$
ishft(arr[0,*] and '000F'x,8) or ishft(arr[1,*] and fix('FF00'x),-8),$
ishft(arr[1,*] and '00FF'x,4) or ishft(arr[2,*] andfix('F000'x),-12),$
arr[2,*] and '0FFF'x]
arr=arr[0:n_elements(arr)-1-nadd*8/12]
|
|
|
Re: read 12 bit array [message #75569 is a reply to message #75564] |
Fri, 01 April 2011 03:41  |
Wout De Nolf
Messages: 194 Registered: October 2008
|
Senior Member |
|
|
On Thu, 31 Mar 2011 08:18:25 -0600, David Fanning <news@idlcoyote.com>
wrote:
> Wox writes:
>
>> Does anyone have an idea how to read a 12bit array from a file (not
>> ASCII, just a binary dump).
>
> Was it written with some kind of 12-bit data type, or is
> there just 12 bits of information in a 16-bit data type?
>
> Cheers,
>
> David
It is a 12bit tiff file. I got at the point where I needed to read a
N*12bit raw datablock.
|
|
|
Re: read 12 bit array [message #75585 is a reply to message #75569] |
Thu, 31 March 2011 08:00  |
lecacheux.alain
Messages: 325 Registered: January 2008
|
Senior Member |
|
|
On 31 mar, 15:52, Wox <s...@nomail.com> wrote:
> Hi all,
>
> Does anyone have an idea how to read a 12bit array from a file (not
> ASCII, just a binary dump).
>
> Thanks,
>
> Wout
Read it as [3,N] byte array b, if the file contains an array of 2*N 12
bits words.
Then transform the result to an integer (16 bits) array by b = fix(b,
0, 3, N)
Finally concatenate the first and last bytes of each triple with
corresponding half parts of the middle byte, that is :
[ b[0,*] or (ishft(b[1,*] and '0F'x,8), ishft(b[2,*],4) or
ishft((b[1,*] and 'F0'x), -4) ]
You should get your 12 bits array packed in a 16-bits array of same
size.
alx.
|
|
|
Re: read 12 bit array [message #75586 is a reply to message #75585] |
Thu, 31 March 2011 07:54  |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
On Mar 31, 9:52 am, Wox <s...@nomail.com> wrote:
> Hi all,
>
> Does anyone have an idea how to read a 12bit array from a file (not
> ASCII, just a binary dump).
>
> Thanks,
>
> Wout
You can read the bytes one by one with readu and then manually
split each set of 2 bytes into it's corresponding 12 bits components
following the patter 12+4, 8+8, 4+12 etc.
Ciao,
Paolo
|
|
|
Re: read 12 bit array [message #75587 is a reply to message #75586] |
Thu, 31 March 2011 07:18  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Wox writes:
> Does anyone have an idea how to read a 12bit array from a file (not
> ASCII, just a binary dump).
Was it written with some kind of 12-bit data type, or is
there just 12 bits of information in a 16-bit data type?
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|