home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / sgiimage / spec / sgiimage.txt < prev   
Text File  |  1994-06-01  |  15KB  |  416 lines

  1.                 Draft version  0.95
  2.  
  3.             The SGI Image File Format
  4.  
  5.                  Paul Haeberli
  6.  
  7.                   paul@sgi.com
  8.  
  9.             Silicon Graphics Computer Systems
  10.  
  11.  
  12.  
  13. This is the definitive document describing the SGI image file format.  This 
  14. is a low level spec that describes the actual byte level format of SGI image
  15. files.  On SGI machines the preferred way of reading and writing SGI image
  16. files is to use the image library -limage.  This library provides a set
  17. of functions that make it easy to read and write SGI images.  If you are 
  18. on an SGI workstation you can get info on -limage by doing:
  19.  
  20.     % man 4 rgb
  21.  
  22. A note on byte order of values in the SGI image files
  23.  
  24.     In the following description a notation like bits[7..0] is used to denote
  25.     a range of bits in a binary value.   Bit 0 is the lowest order bit in a
  26.     the value.
  27.  
  28.     All short values are represented by 2 bytes.  The first byte stores the
  29.     high order 8 bits of the value: bits[15..8].  The second byte stores
  30.     the low order 8 bits of the value: bits[7..0].  
  31.  
  32.     So, this function will read a short value from the file:
  33.  
  34.         unsigned short getshort(inf)
  35.         FILE *inf;
  36.         {
  37.         unsigned char buf[2];
  38.  
  39.         fread(buf,2,1,inf);
  40.         return (buf[0]<<8)+(buf[1]<<0);
  41.         }
  42.  
  43.     All long values are represented by 4 bytes.  The first byte stores the
  44.     high order 8 bits of the value: bits[31..24].  The second byte stores
  45.     bits[23..16].  The third byte stores bits[15..8]. The forth byte stores
  46.     the low order 8 bits of the value: bits[7..0].  
  47.  
  48.     So, this function will read a long value from the file:
  49.  
  50.         static long getlong(inf)
  51.         FILE *inf;
  52.         {
  53.         unsigned char buf[4];
  54.  
  55.         fread(buf,4,1,inf);
  56.         return (buf[0]<<24)+(buf[1]<<16)+(buf[2]<<8)+(buf[3]<<0);
  57.         }
  58.  
  59.  
  60. The general structure of an SGI image file is as shown below:
  61.  
  62.     The header indicates whether the image is run length encoded (RLE).
  63.  
  64.     If the image is not run length encoded, this is the structure:
  65.  
  66.     The Header
  67.     The Image Data
  68.  
  69.     If the image is run length encoded, this is the structure:
  70.  
  71.     The Header
  72.     The Offset Tables 
  73.     The Image Data
  74.  
  75.  
  76. The Header
  77.  
  78.     The header consists of the following:
  79.  
  80.        Size  | Type   | Name      | Description   
  81.  
  82.      2 bytes | short  | MAGIC     | IRIS image file magic number
  83.      1 byte  | char   | STORAGE   | Storage format
  84.      1 byte  | char   | BPC       | Number of bytes per pixel channel 
  85.      2 bytes | ushort | DIMENSION | Number of dimensions
  86.      2 bytes | ushort | XSIZE     | X size in pixels 
  87.      2 bytes | ushort | YSIZE     | Y size in pixels 
  88.      2 bytes | ushort | ZSIZE     | Number of channels
  89.      4 bytes | long   | PIXMIN    | Minimum pixel value
  90.      4 bytes | long   | PIXMAX    | Maximum pixel value
  91.      4 bytes | char   | DUMMY     | Ignored
  92.     80 bytes | char   | IMAGENAME | Image name
  93.      4 bytes | long   | COLORMAP  | Colormap ID
  94.    404 bytes | char   | DUMMY     | Ignored
  95.  
  96.  
  97.     Here is a description of each field in the image file header:
  98.  
  99.     MAGIC - This is the decimal value 474 saved as a short. This
  100.     identifies the file as an SGI image file.
  101.  
  102.     STORAGE - specifies whether the image is stored using run
  103.     length encoding (RLE) or not (VERBATIM).   If RLE is used, the value 
  104.         of this byte will be 1.  Otherwise the value of this byte will be 0.
  105.     The only allowed values for this field are  0 and 1.
  106.  
  107.     BPC - describes the precision that is used to store each
  108.     channel of an image.  This is the number of bytes per pixel
  109.     component.  The majority of SGI image files use 1 byte per 
  110.     pixel component, giving 256 levels.  Some SGI image files use 
  111.     2 bytes per component.  The only allowed values for this field 
  112.     are  1 and 2.
  113.  
  114.     DIMENSION - described the number of dimensions in the data stored
  115.     in the image file.  The only allowed values are 1, 2, or 3.  If
  116.     this value is 1, the image file consists of only 1 channel and 
  117.     only 1 scanline.  The length of this scan line is given by the 
  118.     value of XSIZE below.  If this value is 2, the file consists of a 
  119.     single channel with a number of scan lines. The width and height
  120.     of the image are given by the values of XSIZE and YSIZE below.
  121.     If this value is 3, the file consists of a number of channels.
  122.     The width and height of the image are given by the values of 
  123.     XSIZE and YSIZE below.  The number of channels is given by the 
  124.     value of ZSIZE below.  
  125.  
  126.     XSIZE - The width of the image in pixels
  127.  
  128.     YSIZE - The height of the image in pixels
  129.  
  130.     ZSIZE - The number of channels in the image.  B/W (greyscale) images 
  131.     are stored as 2 dimensional images with a ZSIZE or 1.  RGB color 
  132.     images are stored as 3 dimensional images with a ZSIZE of 3.  An RGB 
  133.     image with an ALPHA channel is stored as a 3 dimensional image with 
  134.     a ZSIZE of 4.  There are no inherent limitations in the SGI image 
  135.     file format that would preclude the creation of image files with more 
  136.     than 4 channels.
  137.  
  138.     PINMIN - The minimum pixel value in the image.  The value of
  139.     0 may be used if no pixel has a value that is smaller than 0.
  140.  
  141.     PINMAX - The maximum pixel value in the image.  The value of
  142.     255 may be used if no pixel has a value that is greater than 255.
  143.     This is the value that is considered to be full brightness in 
  144.     the image.  
  145.  
  146.     DUMMY - This 4 bytes of data should be set to 0. 
  147.  
  148.     IMAGENAME - An null terminated ascii string of up to 79 characters 
  149.     terminated by a null may be included here.  This is not commonly
  150.     used.
  151.  
  152.     COLORMAP - This controls how the pixel values in the file should be
  153.     interpreted.  It can have one of these four values:
  154.  
  155.         0:  NORMAL - The data in the channels represent B/W values
  156.         for images with 1 channel, RGB values for images with 3
  157.         channels, and RGBA values for images with 4 channels.
  158.         Almost all the SGI image files are of this type. 
  159.  
  160.         1:  DITHERED - The image will have only 1 channel of data.
  161.         For each pixel, RGB data is packed into one 8 bit value.
  162.         3 bits are used for red and green, while blue uses 2 bits.
  163.         Red data is found in bits[2..0], green data in bits[5..3],
  164.         and blue data in bits[7..6].  This format is obsolete.
  165.  
  166.         2:  SCREEN - The image will have only 1 channel of data.
  167.         This format was used to store color-indexed pixels.
  168.         To convert the pixel values into RGB values a colormap
  169.         must be used.  The appropriate color map varies from
  170.         image to image.  This format is obsolete.
  171.  
  172.         3:  COLORMAP - The image is used to store a color map from
  173.         an SGI machine.  In this case the image is not displayable
  174.         in the conventional sense.
  175.  
  176.     DUMMY - This 404 bytes of data should be set to 0. This makes the
  177.     header exactly 512 bytes. 
  178.  
  179.  
  180. The Image Data (if not RLE)
  181.  
  182.     If the image is stored verbatim (without RLE), the image data directly
  183.     follows the 512 byte header.  The data for each scanline in the first
  184.     channel is written first.  If the image has more than 1 channel the 
  185.     remaining channels follow the first channel in numerical order.  If the
  186.     BPC value is 1, then each scan line is written as XSIZE bytes.  If the BPC 
  187.     value is 2, then each scanline is written as XSIZE shorts.  These shorts 
  188.     are stored in the byte order described above.
  189.  
  190.  
  191. The Offset Tables (if RLE)
  192.  
  193.     If the image is stored using run length encoding, offset tables
  194.     follow the header that describe what the file offsets are to the 
  195.     RLE for each scanline.  This information only applies if the value 
  196.     for STORAGE above is 1.
  197.  
  198.             Size  | Type   | Name      | Description   
  199.  
  200.      tablen longs | long   | STARTTAB  | Start table
  201.      tablen longs | long   | LENGTHTAB | Length table
  202.  
  203.     One entry in each table is needed for each scan line of RLE data.  The 
  204.     total number of scanlines in the image (tablen) is determined by the
  205.     product of the YSIZE and ZSIZE.  There are two tables of longs that 
  206.     are written. Each consists of tablen longs of data.  The first
  207.     table has the file offsets to the RLE data for each scan line in the
  208.     image.  In a file with more than 1 channel (ZSIZE > 1) this table first 
  209.     has all the offsets for the scanlines in the first channel, followed
  210.     be offsets for the scanlines in the second channel, etc.  The second 
  211.     table has the RLE data length for each scan line in the image.  In a 
  212.     file with more than 1 channel (ZSIZE > 1) this table first has all the 
  213.     RLE data lengths for the scanlines in the first channel, followed
  214.     be RLE data lengths for the scanlines in the second channel, etc.
  215.  
  216.     To find the the file offset, and the number of bytes in the RLE data 
  217.     for a particular scanline, these two arrays may be read in and indexed as
  218.     follows: 
  219.  
  220.     To read in the tables:
  221.  
  222.         unsigned long *starttab, *lengthtab;
  223.  
  224.         tablen = ysize*zsize*sizeof(long);
  225.         starttab = (unsigned long *)mymalloc(tablen);
  226.         lengthtab = (unsigned long *)mymalloc(tablen);
  227.         fseek(inf,512,SEEK_SET);
  228.         readlongtab(inf,starttab);
  229.         readlongtab(ing,lengthtab);
  230.  
  231.  
  232.     To find the file offset and RLE data length for a scanline:
  233.  
  234.         rowno is an integer in the range 0 to YSIZE-1
  235.         channo is an integer in the range 0 to ZSIZE-1
  236.  
  237.         rleoffset = starttab[rowno+channo*YSIZE]
  238.         rlelength = lengthtab[rowno+channo*YSIZE]
  239.     
  240.     It is possible for two identical rows to share compressed scanline
  241.     data.  A completely white image could be written as a single compressed
  242.     scanrow and having all table entries point to that scanrow.
  243.  
  244.     Another little hack that should work is if you are writing out a RGB 
  245.     RLE file, and a particular scan line is achromatic (greyscale), you 
  246.     could just make the r, g and b rows point to the same data!!
  247.  
  248. The Image Data (if RLE)
  249.  
  250.     This information only applies if the value for STORAGE above is 1.  If
  251.     the image is stored using run length encoding, the image data follows
  252.     the offset tables above.  The RLE data is not in any particular order.
  253.     The offset tables above are used to locate the rle data for any scanline.
  254.   
  255.     The RLE data must be read in from the file and expanded into pixel 
  256.     data in the following manner:
  257.  
  258.     If BPC is 1, then there is one byte per pixel.  In this case the 
  259.     RLE data should be read into an array of chars.  To expand
  260.     data, the low order seven bits of the first byte: bits[6..0]
  261.     are used to form a count.  If the high order bit of the first
  262.     byte is 1: bit[7], then the count is used to specify how many
  263.     bytes to copy from the RLE data buffer to the destination.
  264.     Otherwise, if the high order bit of the first byte is 0: bit[7],
  265.     then the count is used to specify how many times to repeat the 
  266.     value of the following byte, in the destination.  This process
  267.     continues until a count of 0 is found.  This should decompress
  268.     exactly XSIZE pixels.  
  269.  
  270.     Here is example code to decompress a scanline:
  271.  
  272.         expandrow(optr,iptr,z)
  273.         unsigned char *optr, *iptr;
  274.         int z;
  275.         {
  276.         unsigned char pixel, count;
  277.         
  278.         optr += z;
  279.         while(1) {
  280.             pixel = *iptr++;
  281.             if ( !(count = (pixel & 0x7f)) )
  282.             return;
  283.             if(pixel & 0x80) {
  284.             while(count--) {
  285.                 *optr = *iptr++;
  286.                 optr+=4;
  287.             }
  288.             } else {
  289.             pixel = *iptr++;
  290.             while(count--) {
  291.                 *optr = pixel;
  292.                 optr+=4;
  293.             }
  294.             }
  295.         }
  296.         }
  297.  
  298.     If BPC is 2, there is one short (2 bytes) per pixel.  In this 
  299.     case the RLE data should be read into an array of shorts.  To 
  300.     expand data, the low order seven bits of the first short: bits[6..0]
  301.     are used to form a count.  If bit[7] of the first short is 1, then 
  302.     the count is used to specify how many shorts to copy from the RLE 
  303.     data buffer to the destination.  Otherwise, if bit[7] of the first 
  304.     short is 0, then the count is used to specify how many times to 
  305.     repeat the value of the following short, in the destination.  This 
  306.     process proceeds until a count of 0 is found.  This should decompress
  307.     exactly XSIZE pixels.  Note that the byte order of short data in
  308.     on the input file should be observed, as described above.
  309.  
  310.  
  311. Implementation notes
  312.  
  313.     Implementation of both RLE and VERBATIM format for images with
  314.     BPC of 1 is required since the great majority of SGI images are in
  315.     this format.  Support for images with a 2 BPC is encouraged.
  316.  
  317.     If the ZSIZE of an image is 1, it is assumed to represent B/W
  318.     values.  If the ZSIZE is 3, it is assumed to represent RGB data,
  319.     and if ZSIZE is 4, it is assumed to contain RGB data with alpha.
  320.  
  321.     The origin for all SGI images is the lower left hand corner.  The
  322.     first scan line (row 0) is always the bottom row of the image.   
  323.  
  324. Naming Conventions
  325.  
  326.     On SGI systems, SGI image files end with the extension .bw if
  327.     they are B/W images, they end in .rgb if they contain RGB image
  328.     data, and end in .rgba if they are RGB images with alpha channel.
  329.  
  330.     Sometimes the .sgi extension is used as well.
  331.  
  332. An example
  333.  
  334.     This program will write out a valid B/W SGI image file:
  335.  
  336.     #include "stdio.h"
  337.  
  338.     #define IXSIZE      (23)
  339.     #define IYSIZE      (15)
  340.  
  341.     putbyte(outf,val)
  342.     FILE *outf;
  343.     unsigned char val;
  344.     {
  345.         unsigned char buf[1];
  346.  
  347.         buf[0] = val;
  348.         fwrite(buf,1,1,outf);
  349.     }
  350.  
  351.     putshort(outf,val)
  352.     FILE *outf;
  353.     unsigned short val;
  354.     {
  355.         unsigned char buf[2];
  356.  
  357.         buf[0] = (val>>8);
  358.         buf[1] = (val>>0);
  359.         fwrite(buf,2,1,outf);
  360.     }
  361.  
  362.     static int putlong(outf,val)
  363.     FILE *outf;
  364.     unsigned long val;
  365.     {
  366.     unsigned char buf[4];
  367.  
  368.     buf[0] = (val>>24);
  369.     buf[1] = (val>>16);
  370.     buf[2] = (val>>8);
  371.     buf[3] = (val>>0);
  372.     return fwrite(buf,4,1,outf);
  373.     }
  374.  
  375.     main()
  376.     {
  377.     FILE *of;
  378.         char iname[80];
  379.         unsigned char outbuf[IXSIZE];
  380.         int i, x, y;
  381.  
  382.         of = fopen("example.rgb","w");
  383.         if(!of) {
  384.             fprintf(stderr,"sgiimage: can't open output file\n");
  385.             exit(1);
  386.         }
  387.         putshort(of,474);       /* MAGIC                */
  388.         putbyte(of,0);          /* STORAGE is VERBATIM     */
  389.         putbyte(of,1);          /* BPC is 1                */
  390.         putshort(of,2);         /* DIMENSION is 2          */
  391.         putshort(of,IXSIZE);    /* XSIZE                   */
  392.         putshort(of,IYSIZE);    /* YSIZE                   */
  393.         putshort(of,1);         /* ZSIZE                   */
  394.         putlong(of,0);          /* PIXMIN is 0             */
  395.         putlong(of,255);        /* PIXMAX is 255           */
  396.         for(i=0; i<4; i++)      /* DUMMY 4 bytes     */
  397.             putbyte(of,0);
  398.         strcpy(iname,"No Name");
  399.         fwrite(iname,80,1,of);  /* IMAGENAME          */
  400.         putlong(of,0);          /* COLORMAP is 0     */
  401.         for(i=0; i<404; i++)    /* DUMMY 404 bytes     */
  402.             putbyte(of,0);
  403.  
  404.         for(y=0; y<IYSIZE; y++) {
  405.             for(x=0; x<IXSIZE; x++) 
  406.                 outbuf[x] = (255*x)/(IXSIZE-1);
  407.         fwrite(outbuf,IXSIZE,1,of);
  408.         }
  409.         fclose(of);
  410.     }
  411.  
  412.  
  413.  
  414.  
  415.  
  416.