Overview Image Sources Components XML Config Slow Control Image File Formats API Source Repository VSv2 Conference Submissions Contact

AVINE Video System

File Formats

IMC2 (BKC2) File Format

Following a request of being able to save individual scaling factors (pixel to millimetre) for horizontal (x) and vertical (y) dimension of an image to file, the file format IMC2 has been created. It can store a single image or an image sequence (meant for multiple images taken very close after one another or directly after each other). Colour (RGB 24 bits per pixel) and grayscale (8 to 16 bit per pixel) images are supported. It stores many header fields of images in human readable format (simple file view as text) as metadata inside the file. In order to distinguish whether a file contains signal or background images, the file extension is used. One shall use '.imc2' for signal images and '.bkc2' for background images. The structure of the file is identical in both cases, just the file extension differs.

Identical to dated file format IMC/BKC, lossless compression (zlib) is applied for each image. If compression is ineffective, uncompressed image data is stored instead.


Video System 3 and IMC2 Header Fields

There are four different type of metadata in IMC2: uint32, int32, float and string. Some metadata is optional, most is mandatory.

Video System 3 Image Header Field(s) IMC2 header field
    Example Global or Per Image
n/a uint32 number_of_images number_of_images=20 global
int32 aoiWidth or
int32 sourceWidth
uint32 width_px width_px=1360 global
int32 aoiHeight or
int32 sourceHeight
uint32 height_px height_px=1024 global
float xScale float scale_x_mm_px scale_x_mm/px=0.035714 global
float yScale float scale_y_mm/px scale_y_mm/px=0.035714 global
int32 sourceWidth int32 source_width_px source_width_px=1360 global
int32 sourceHeight int32 source_height_px source_height_px=1024 global
int32 aoiWidth int32 aoi_width_px aoi_width_px=-1 global
int32 aoiHeight int32 aoi_height_px aoi_height_px=-1 global
int32 xStart int32 x_start_px x_start_px=0 global
int32 yStart int32 y_start_px y_start_px=0 global
int32 bytesPerPixel int32 bytes_per_pixel bytes_per_pixel=2 global
int32 effectiveBitsPerPixel int32 effective_bits_per_pixel effective_bits_per_pixel=12 global
int32 horizontalBinning int32 horizontal_binning horizontal_binning=0 global
int32 verticalBinning int32 vertical_binning vertical_binning=0 global
int32 sourceFormat1 string source_format source_format=GRAY global
int32 imageFormat1,2 string image_format image_format=GRAY global
float imageRotation float image_rotation image_rotation=0.000000 global
float scale_x_offset float scale_x_offset scale_x_offset=-1.000000 global
float scale_y_offset float scale_y_offset scale_y_offset=-1.000000 global
char[64] cameraPortName string camera_port_name camera_port_name=High3.Scr1 (Full) global, optional
uint32 cameraPortId uint32 camera_port_id camera_port_id=188 global, optional
n/a string image_start image_start=image 1 of 20 per-image
int32 timestampSeconds, int32 timestampMicroseconds string timestamp_utc timestamp_utc=2022-10-13 2:17:18.382212 PM UTC per-image, optional
int32 imageFlags3 string image_flags image_flags=LITTLE_ENDIAN LOSSLESS HORIZONTAL_FLIP_180
XYSTART_ZERO_BASED GLOBAL_TIMESTAMP
per-image
uint32 frameNumber uint32 framenumber framenumber=1143 per-image, optional
uint32 eventNumber uint32 eventnumber eventnumber=1509094577 per-image, optional

Footnotes:
1 valid source image formats (source_format is an informal parameter) are: 'GRAY', 'RGB', 'RGBA', 'RGB15', 'RGB16', 'JPEG', 'TIFF', 'YUV411', 'YUV422', 'YUV444', 'HUFFYUV', 'BMP', 'BAYER'
2 for image_format, valid is 'GRAY' or 'RGB'
3 Image Flags: LITTLE_ENDIAN or BIG_ENDIAN, LOSSLESS or LOSSLESS_BIT_NOT_SET, HORIZONTAL_FLIP_180, VERTICAL_FLIP_180, XYSTART_ONE_BASED, XYSTART_ZERO_BASED, EVENTNUMBER_OUT_OF_SYNC, GLOBAL_TIMESTAMP or LOCAL_TIMESTAMP, TIMESTAMP_EXTRAPOLATED, SCALE_OFFSET, SOFTWARE_ROI

IMC2 File Format Layout

Fixed Position1 (Bytes) Count (Bytes) Type2 Short Name Description
0 4 uint32_t magic13 magic value 0x0
4 4 uint32_t magic23 magic value 0x1
8 4 uint32_t nMG number of global metadata sets in file, nMG>=20, nMG<=100
12 1 uint8_t r carriage return (13)
13 1 uint8_t n newline feed (10)
14 252 METASTRUCT MG.1 global metadata set 1 (first global set)
...
--- 252 METASTRUCT MG.nMG global metadata set nMG (last global set)
The items below repeat for as many images as encoded in global metadata key-value struct 'number_of_images'.
--- 8 uint64_t len_u4 uncompressed length of image bits, in bytes, >0, len_u must be >= len_c
--- 8 uint64_t len_c4 compressed length of image bits, in bytes, >0
--- 4 uint32_t nMI number of local metadata for image, MIn is [2..10]
--- 1 uint8_t r carriage return (13)
--- 1 uint8_t n newline feed (10)
--- 252 METASTRUCT MI.1 per-image metadata set 1 (first per-image set)
...
--- 252 METASTRUCT nMI per-image metadata set nMI (last per-image set)
--- len_c uint8_t image bits image bits

Footnotes:
1 position starting from beginning of file, zero-based index, in bytes
2 data is encoded as little endian format (LSB first, MSB last)
3 initial header values magic1 and magic2 are existing to easily distinguish from imm/bkg/imc/bkc file format (and reject file by already existing code loading in imm/bkg/imc/bkc files)
4 if len_u equals len_c, image data was saved uncompressed (ineffective compression)

METASTRUCT Layout

Count (Bytes) Type Short Name Description
250 uint8_t keyvalue c-string containing key=value, up to 249 characters (including equal sign), padded with zeroes, last byte is always zero
1 uint8_t r carriage return (13)
1 uint8_t n newline feed (10)

Remarks:
A metadata set in file consists of 252 bytes.
The key string must not contain equal sign ('='), as it is used to distinguish key part of string from value part. The value part may consist of further equal signs.
It is recommended to use only ASCII inside keyvalue string.
There are four types of metadata: uint32, int32, float and string.



Dated File Formats (simple, deprecated, still used)

IMM File Format Layout

Position1 (Bytes) Count (Bytes) Type2 Description Repetition
0 2 UINT16 width of video frame, 0 is illegal value, values above 2048 have not been used in practice (as of year 2011) The whole structure can be repeated multiple times (as many times as there are images in the file)
2 2 UINT16 physical bits per pixel of pixels, if 0 then physical bits per pixel is 83, only 8 and 16 are valid here
4 2 UINT16 height of video frame, 0 is illegal value, values above 2048 have not been used in practice (as of year 2011)
6 2 UINT16 effective bits per pixel of pixels, if 0 then effective bits per pixel is equal to physical bits per pixel setting, for physical bits per pixel setting of 8, values of 1 to 8 are supported, for physical bits per pixel setting of 16, values of 1 to 16 are supported
8 width*height*physical_bits_per_pixel/8 Array of pixel value size (either array of UINT8 or UINT16) raw image data
width*height+8 8 Double scale factor value (mm/pixel), default(unset) value is 1.0000

Footnotes:
1 position starting from beginning of file, zero-based index, in bytes (1 is first byte in file)
2 The data is encoded as little endian format (LSB first, MSB last).
3 Bytes per pixel setting was introducted later on. Before that the field was set to '0'. If field is set to 0 in file, one can assume that 8 bits per pixel setting is used (each pixel is 1 byte).

Either one or multipe images can be stored inside an IMM file. If multiple images are contained within the file, the whole structure (from width to scale) can be repeated multiple times in the IMM file. By this simple technique one is able to store a sequence of images in file. In practice the images that are contained in such files came from the same image source and are guaranteed to be in time-ascending sequence, i.e. later image (higher file index position) in file was acquired later in time than image which was placed earlier in file (lower file index position). Another convention is that the values of width, height, physical and effective bits per pixel and scale factor stay the same for each image inside the file. Only the raw image data should change for each image. If varying values are found within the file, one should present a warning. An algorithm for loading in an IMM file is as follows:

  • open the file
  • determine file length
  • read in first two bytes and store as width
  • read in next two bytes and store as physical bits per pixel
  • read in next two bytes and store as height
  • read in next two bytes and store as effective bits per pixel
  • validate values
  • calculated number of images inside file: num_images = length_of_file / ((width*height*physical_bits_per_pixel/8)+16)
  • validate that num is integer value (if there is a fractional part then file is not wellformed)
  • seek to start of file
  • repeat for every frame in file (num_images)
    • read in 8 bytes
    • read in frame bits (width*height*physical_bits_per_pixel/8)
    • read in 8 bytes (scale factor)
  • close file


BKG File Format Layout

Position1 (Bytes) Count (Bytes) Type2 Description Repetition
0 2 UINT16 width of video image, 0 is illegal value, values above 2048 have not been used in practice (as of year 2011) no4
2 2 UINT16 physical bits per pixel of pixels, if 0 then physical bits per pixel is 83, only 8 and 16 are valid here
4 2 UINT16 height of video image, 0 is illegal value, values above 2048 have not been used in practice (as of year 2011)
6 2 UINT16 effective bits per pixel of pixels, if 0 then effective bits per pixel is equal to physical bits per pixel setting, for physical bits per pixel setting of 8, values of 1 to 8 are supported, for physical bits per pixel setting of 16, values of 1 to 16 are supported
8 width*height*physical_bits_per_pixel/8 Array of UINT8 or UINT16 (depending of physical bits per pixel) raw image data

Footnotes:
1 position starting from beginning of file, zero-based index, in bytes (1 is first byte in file)
2 The data is encoded as little endian format (LSB first, MSB last).
3 Bytes per pixel setting was introducted later on. Before that the field was set to '0'. If field is set to 0 in file, one can assume that 8 bits per pixel setting is used (each pixel is 1 byte).
4 In the wild, there are some nonstandard bkg files with more than one background image per file. These files are in principle not supported but usually follow the same layout like in IMM file format, except that no scale factor is present. Based on the BKG image file format one can say that the whole structure is repeated for any image in the file (as if a bunch of bkg files have been concatenated).

Only one image can be stored inside an BKG file. An algorithm for loading in is as follows:

  • open the file
  • read in first two bytes and store as width
  • read in next two bytes and store as physical bits per pixel
  • read in next two bytes and store as height
  • read in next two bytes and store as effective bits per pixel
  • validate values
  • read in frame bits (width*height*physical_bits_per_pixel/8)
  • close file


IMC/BKC File Format Layout

Position1 (Bytes) Count (Bytes) Type2 Description Repetition
0 4 UINT32 width of video image, 0 is illegal value, values above 2048 have not been used in practice (as of year 2011) no
4 4 UINT32 height of video image, 0 is illegal value, values above 2048 have not been used in practice (as of year 2011)
8 2 UINT16 physical bits per pixel of pixels, if 0 then physical bits per pixel is 8, only 8 and 16 are valid here
10 2 UINT16 effective bits per pixel of pixels, if 0 then effective bits per pixel is equal to physical bits per pixel setting, for physical bits per pixel setting of 8, values of 1 to 8 are supported, for physical bits per pixel setting of 16, values of 1 to 16 are supported
12 4 UINT32 number of images stored in file. value must be bigger or equal 1, a value bigger 500 is unlikely to be correct although theoretical value is unlimited
X+0 8 Double scale factor (mm/pixel ratio of image that follows this header) as many times as there are images in file
X+8 4 UINT32 len_compressed:=length in bytes of compressed image data that follows this header
X+12 4 UINT32 length in bytes of image data after it was uncompressed (should be equal to width*height*physical_bits_per_pixel/8)
X+16 len_compressed Array of Bytes compressed image data (zlib v1.2.3 (or newer) z_compress/z_uncompress

Footnotes:
1 position starting from beginning of file, zero-based index, in bytes (1 is first byte in file)
2 The data is encoded as little endian format (LSB first, MSB last).

IMC and BKC files contain the same layout inside. Just the file extension is different. When compared to IMM/BKG files, the main difference is that in IMC/BKC files, image data is lossless compressed using zlib library. A compression ratio of about 1:10 is realistic, although it may vary significant on the image content. A BKC file with more than one image is used to store source background images which will be combined to a single background image using certain algorithms (Envelope, Average) in order to produce a single background image out of it, which is used for background subtraction later on. Basically the structure defines that each image in file can have a separate scale factor. But this is not true in practice. Usually all images come from the same image source and are kept in sequence (time ascending order). So they share the same characteristics like width, height, physical and effective bits per pixel as well as the scale factor. If varying scale factors are found inside the file, one should display a warning. An algorithm for loading in an IMC/BKC file is as follows:

  • open the file
  • read in the file header (first 16 bytes)
  • validate values
  • repeat for as many images as the file header tells
    • read in single image header (16 bytes)
    • validate values
    • read in compressed frame bits
    • uncompress frame bits
  • close file

Last modified: Feb 2023