Solved

Binary IMA files through MATLAB

  • 4 August 2020
  • 3 replies
  • 284 views

Advanced Analysis features in Zemax like Partial Coherence Analysis claims that binary IMA files are suitable for reducing computation times. Zemax has a KB article on how to create Ascii and binary IMA files through C++. I am more familiar with MATLAB and tried various methods and none of those work to create a simple IMA for ellipse of 40x40 pixels. MATLAB claims that 'fwrite' or 'save' to MAT files would create binary files, but they are not accepted by IMA viewer and throws an Error message like 'Number of pixels must be greater than 1 and no more than 8000', eventhough I am creating a simple square encapsulated ellipse in 40x40 pixels. Can anyone help?

icon

Best answer by Csilla Timar-Fulep 5 August 2020, 12:53

View original

3 replies

Userlevel 5
Badge +2

Hello Krishna,


Thanks for your question here!


The binary IMA file format is more complicated than the text format, and binary IMA files cannot be edited with a text editor. However, as you mentioned, the binary IMA files are dramatically more powerful. When writing binary IMA files, have to pay attention to the correct file format, which is the following. Each pixel in the binary IMA file is represented by an unsigned byte, which means there are 256 'gray-scale' levels of intensity. Furthermore, each wavelength can be assigned a separate pixel map. Therefore, very realistic photograph like extended sources can be modeled. Furthermore, the binary IMA file format requires 3 16-bit header values. The first 16-bit value is a signed integer that must be equal to zero. The second 16-bit signed integer is the width of the pixel map in pixels, which can be any number from 1 to 8000. The third 16-bit signed integer is the number of pixel maps, which correspond to the number of colors (or wavelengths) represented in the file.

For example, a 3-color binary pixel map of a 50 by 50 image would have 6 bytes of header (0, 50, and 3), followed by 2500 bytes for color 1, then 2500 bytes for color 2, then 2500 bytes for color 3, for a total of 7506 bytes. The data for each color is stored by columns for each row (the column index changes faster than the row index).


You may find more information on the IMA and BIM file formats in the Hel file under:

The Analyze Tab (sequential ui mode) > Image Quality Group > Extended Scene Analysis > Geometric Image Analysis


Based on the instruction given in the knowledge base article How to create binary IMA and BIM files, I wrote a short Matlab code, which generates a 1-color, 7 by 7, binary IMA file, with a decentered square. Please find the code below:



%linear size of the matrix, i.e. number of rows and columns
linearSize = 7;
%test matrix with square
matrix = zeros(linearSize);
matrix(2:4,2:4) = 1;
%writing to binary IMA file
fileID = fopen('*\Documents\Zemax\IMAFiles\decenteredSquare.IMA', 'wb');
%writing the header: 16-bit signed integer
%line 1: 0 to show it is binary
fwrite(fileID,0,'int16');
%line 2: linear size
fwrite(fileID,linearSize,'int16');
%line 3: number of maps
fwrite(fileID,1,'int16');
%writing the pixels: unsigned byte
fwrite(fileID,matrix,'uchar');
fclose(fileID);

The key is to define the precision for fwrite as discussed here:

https://uk.mathworks.com/help/matlab/ref/fwrite.html


In OpticStudio's Ima/BIM File Viewer it looks like this:



If you have any further questions, please let us know and we will be happy to help!


Best,


Csilla

Hi Csilla, thanks for the much needed help! Your suggestions and sample MATLAB code for creation of binary IMAs gave me some good insight on how to use them for other custom shapes.  Regards.

Userlevel 6
Badge +2

Just updating this post. A Python converter to convert a JPG image to a BIM Binary Image File is available here: 

Login to access this space.

Reply