Bytes per pixel

Thanks to Hedy Kober

 

An image file: header and data

Bytes and bits

Data types

Bits

Swapping

An image file: header and data

An image file consists of a header and the image itself. The header contains information about the image, for example how many pixels there are, what the resolution is, etc. This is the case for images of the brain in BrainVoyager image file types (*.vmr, *.fmr, *.dmr, *.amr), but also for image files in general, for example *.bmp, *.jpg or *.png. The data are the pixels or voxels. Each pixel or voxel contains one intensity value.

Figure: an image file (inspired by Kate Fissell's HBM 2004 lecture)

Bytes and bits

For VMR, FMR and DMR projects the number of bytes per pixel needs to be specified. The number of bytes per pixel refers to how much information can be stored in each pixel of an image. Usually this amount of information is counted in bytes. Each byte consists of 8 bits. So in each byte it is possible to store 8 pieces (bits) of information (0's and 1's).  

 

Data types

In one byte it is for example possible to store a character, 'a', 'Z'. For a small whole number '1', '5', or '100', one needs a one-byte integer. '10000' for example, one needs one or two bytes (this is called integer; there are types of one byte, two bytes and four bytes, depending on the range of numbers). For numbers with fractions, one needs minimally 4 bytes, for example '152.6789' (these data types are called floats).

The questions "number of bytes per pixel" refers to this number of bytes. The more information needs to be stored, the larger the data type that will be used.

Usually MRI data are originally stored in 2-byte integers. You can check this in the DICOM header in the field 'BitsAllocated' or 'bit depth', for example BitsAllocated 16. Divide the number of bits by 8 to get the number of bytes: 16 / 8 = 2.

 

 

In case the parameter would have the value '12', this would be rounded to the ceiling of modulus 8 bits, so the bits with a bit depth of 12 would be rounded to 16, and (16 bits / 8) = 2 bytes.

 

When the images are imported to a BrainVoyager file, the number of bytes is sometimes modified. For VMR images, the pixels are converted to 1 byte ("char" data type). FMR, DMR and VTC data are saved in units of 2 bytes ("short integer"). One pixel of VMP data is stored in 4 bytes ("float" data type). When the data type is "signed", the value can be positive and negative. One of the bits is used to indicate the sign (+ or -), so that the value range is smaller because one bit is already used. When a data type is "unsigned", the value can only be positive. "Float" values can be positive and negative.  

 

File type in BrainVoyager

Data type (C++)

Value range of data type

Number of bytes

 

 

 

 

AMR

char

signed: -127 ~ 127

unsigned: 0-255

1

VMR

char

signed: -127 ~ 127

unsigned: 0-255

1

STC (FMR)

short integer

signed: -32767 ~ 32767

unsigned: 0 ~ 65534

2

DMR

short integer

signed: -32767 ~ 32767

unsigned: 0 ~ 65534

2

MAP

float

depends

4

VTC

short integer

signed: -32767 ~ 32767

unsigned: 0 ~ 65534

2

VMP

float

depends

4

GLM

diverse, mostly float

div

4

SRF

diverse, mostly integer

div

4

Table: datatypes per BrainVoyager file type

 

(The file types are specified in detail in the BrainVoyager QX User's Guide).

Bits

In the NIfTI-1 file type, there are a few fields in the header that even compress the information such that the smallest units are not bytes, but bits. For example the information about the time and spatial units of the data is saved in the header field xyzt_units.

The spatial unit for that file (whether the data are saved in millimeter, centimeter or meter) is saved in the first 3 bits of the byte; in the second 3 bits temporal units are saved, for example whether the data are in seconds, minutes, hertz, etc.

 

Swapping

Swapping is necessary when the byte order of the data is different of the order that BrainVoyager expects.

Dependent from the operating system/processor, the bits in one data type entity can be stored with "least significant byte first" (LSB) c.q. "little endian" or "most significant byte first" (MSB) or "big endian". BrainVoyager expects little endian, so when your data are in big endian byte order, use the value true; otherwise use false. Most systems are now little endian; the "old" Mac PowerPC was big endian.

If the images look inverted, i.e. black is white and white is black, the data probably need to be swapped.

For more information on byte order, see for example Wikipedia on endianness.