Friday, September 21, 2012

RGB file: Packed or Planar?



Save RGB color images requires specific strategies - compared to the gray-level images - to store the three red, green, and blue values defining a pixel. Two main strategies exist: Packed and Planar.


1- Planar format

If a color image is saved in the planar file format, the three red, green, and blue channels composing the pixels are stored separately  in the file.
Fig.1: Planar storage of a 4x3 color image. The three Red, Green, and Blue values of a pixel of coordinates (xi,yi)  are located at (xi,yi,1), (xi,yi,2), and (xi,yi,3), respectively.

It is like a stack of three slices containing the red, green, and blue values for each pixel.
Here is an example of planar file format (Fig.2).
  • Download the image 'importPlanar.raw' [Link] and import this file in ImageJ (File > Import > Raw...) as a 8-bit image of size 320x600, you'll get the image of Fig. 2. 
  • Then, go to Image > Stacks > Tools > Montage to Stack... (Parameters of Stack maker; Fig. 2B). 
  • Finally, convert the resulting stack (Fig. 2C) in a RGB image with Image > Color > Stack to RGB.

Fig.2: Planar RGB image.


2- Packed format

By default, ImageJ doesn't use the planar format to save color images, it uses the packed format.
Open the IJ clown sample in  File > Open Samples > Clown (14 K) and  save it as a raw image with File > Save As > Raw. Now, let's see how the pixel values are stored in this file.

Import the file with File > Open > Raw... and choose as parameters a 8-bit image of dimension 960x200. Why a width of 960 pixels? Because it corresponds to three times the genuine RGB image width (320 pixels). In Fig. 3, the clown is visible, however he appears stretched in the horizontal direction.

Fig. 3: Clown image imported as a raw image of size 960 ( = 320x3) X 200.
This pattern is typical of a packed RGB format. Indeed, the red, green, and blue channels are interleaved in the file. Thus, the file contains a succession of red, green, and  blue values corresponding to each pixel. Each value is stored as a 8-bit number (see the scheme of Fig. 4).

Fig.4: Example of storage of two pixels in a packed format. A pixel of coordinates (xi,yi) has its RGB values located in the file at (xi,yi), (xi+1,yi), and (xi+2,yi).


To be sure, the image of Fig. 3 is a color image, run the following script which converts the values in a stack composed of three slices corresponding to the red, green, and blue channels...

+++ IJ JavaScript snippet +++ +++ End of IJ JavaScript snippet +++

To get the color image, go to Image > Color > Stack to RGB, et VoilĂ !

3- Other formats

ImageJ can directly import the packed 24-RGB  and the planar 24-RGB formats as shown in the screenshot of Fig. 5.
Other formats are available like the packed 24-BGR (blue value, first and then, green, and finally red). Two other file formats can be read:  32-bit ARGB and 32-bit ABGR, they are packed formats but the image contains a fourth 8-bit channel named 'alpha' (A) dedicated to opacity. As transparency is not supported by ImageJ, this channel is skipped during the import.

Fig.5: Color formats available in Import Dialog Box.

Hope that helps !

4- Links

  • Series of posts Image File [TOC]
  • Crazybiocomputing mini-games levels #8 and 9 [Link]



No comments:

Post a Comment