home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / os / mswindo / programm / misc / 4461 < prev    next >
Encoding:
Internet Message Format  |  1992-12-23  |  12.4 KB

  1. Xref: sparky comp.os.ms-windows.programmer.misc:4461 alt.binaries.pictures.utilities:2251
  2. Newsgroups: comp.os.ms-windows.programmer.misc,alt.binaries.pictures.utilities
  3. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!newsserver.pixel.kodak.com!kodak!sunshine!thomas
  4. From: thomas@sunshine.Kodak.COM (Thomas Kinsman)
  5. Subject: DIB/BMP CREATION GUIDE, as requested.
  6. Message-ID: <1992Dec23.214432.18764@kodak.kodak.com>
  7. Keywords: DIB BMP Bitmaps File Formats
  8. Sender: news@kodak.kodak.com
  9. Organization: Precambiran Electronics, Rochester, NY
  10. Date: Wed, 23 Dec 92 21:44:32 GMT
  11. Lines: 484
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.                GUIDE TO CREATION OF DIB IMAGES
  21.  
  22.  
  23.                   Thomas B. Kinsman
  24.  
  25.                    Precambrian Electronics
  26.                    29 Falstaff Rd.
  27.                  Rochester, NY 14609
  28.                   thomask@kodak.com
  29.                    Copyright March 15, 1991
  30.  
  31.  
  32.  
  33.                      ABSTRACT
  34.  
  35.           The format of images created for use by  MS  Windows  3.0/3.1
  36.           applications  is    documented  in    the MS Windows Programmer's
  37.           Reference guide.    This guide is intended as a  clearify  some
  38.           wording    of   the   specifications,   and  to  put  forth  a
  39.           recommendation among the various alternatives.
  40.  
  41.           Please send updates/corrections/suggestions and revisions     to
  42.           me.   Please  thank  me  for  writing  this on my own time by
  43.           keeping my name  with  the  document.   Thank  you  for  your
  44.           respect.
  45.  
  46.           This document Copyright, 1991, by Thomas B. Kinsman.
  47.           If  desired,  a  PostScript  version  of    this  document     is
  48.           available.
  49.  
  50.           This information is free.     You get more than what you pay for.
  51.           Don't sue me if I'm wrong.
  52.  
  53.  
  54.     OVERVIEW
  55.  
  56.     The DIB image file format is intended to be a "Device Independent  Bitmap"
  57.     file  format.  (What Microsoft means by "Device Independent" is unclear to
  58.     me.) Four pixel resolutions are supported: 1 bit, 4 bit, 8 bit, and 24 bit
  59.     pixels.
  60.  
  61.     These images were intended for use on DOS (IBM, or Little-Endian) systems.
  62.     If    you  are  creating them on another architecture you will have to byte-
  63.     swap all short and long integer values.
  64.  
  65.     By convention, DIB images end in the extension ".BMP".   This  is  because
  66.  
  67.  
  68.  
  69.                   March 15, 1991
  70.  
  71.  
  72.  
  73.  
  74.  
  75.                     - 2 -
  76.  
  77.  
  78.     they  are  a  super-set of earlier "bitmap image files".  Consequently you
  79.     will sometimes hear DIB files referred to as "BMP" files.
  80.  
  81.     Since DIB images files are a super-set, there are three "flavors"  of  DIB
  82.     images:
  83.      o   DOS DIB images.  These are the recommended     convention,  and  the
  84.          form  which I will describe how to create.     They are intended for
  85.          applications running under MS Windows /3.0 in a DOS environment.
  86.      o   OS/2 DIB images.  My understanding is that these are  the    flavor
  87.          of DIB images that were used by the Presentation Manager.
  88.      o   Old-style Bitmap images.
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                   March 15, 1991
  130.  
  131.  
  132.  
  133.  
  134.  
  135.                     - 3 -
  136.  
  137.  
  138.     DIFFERENCES BETWEEN FLAVORS
  139.  
  140.     The DOS DIB images consist of:
  141.      1.  A "BITMAPFILEHEADER" file header which identifies the file     as  a
  142.          DIB  file.      This    header    also gives the total size of the image
  143.          file, and the offset to the image data.
  144.      2.  A "BITMAPINFOHEADER"  image  header  which     specifies  the     image
  145.          attributes.
  146.      3.  An optional palette of colors used by the image.  If  it  exists,
  147.          this  may contain 2, 16, or 256 entries.  Each entry is a Windows
  148.          RGBQUAD structure.
  149.      4.  The image data itself.
  150.  
  151.  
  152.     The OS/2 DIB images consist of:
  153.      1.  The same "BITMAPFILEHEADER" file header which identifies the file
  154.          as     a  DIB     file.     This  header also gives the total size of the
  155.          image file, and the offset to the image data.
  156.      2.  A "BITMAPCOREHEADER"  image  header  which     specifies  the     image
  157.          attributes.
  158.      3.  An optional palette of colors used by the image.  Again, if  this
  159.          exists  it     may  contain  2, 16, or 256 entries.  Each entry is a
  160.          Windows RGBTRIPLE structure.
  161.      4.  The image data itself.
  162.  
  163.  
  164.     The Old Style image bitmap consists of:
  165.      1.  Either a BITMAPINFOHEADER or a  BITMAPCOREHEADER.     Which    header
  166.          type is determined by the first long integer (DWORD) value.
  167.      2.  An optional color palette.      This    palette     may  be  composed  of
  168.          either   Windows    RGBQUAD      structures  (if  the    header    was  a
  169.          BITMAPINFOHEADER) or Windows RGBTRIPLE structures (if the    header
  170.          was a BITMAPCOREHEADER).
  171.      3.  The image data itself.
  172.  
  173.  
  174.     CREATING DIB IMAGE FILES.
  175.  
  176.     Creating a DOS DIB image file consists of several straight forward    steps.
  177.     The headers need to be created and then written to the file.  These header
  178.     structures are defined in the MS  Windows/3.0  "windows.h"    include     file.
  179.     Palette  information  needs     to  be     stored for images that are not 24-bit
  180.     images.  The five general steps are:
  181.      1.  Filling  in  the  BITMAPFILEHEADER      and    the   BITMAPINFOHEADER
  182.          structures the basic image information.
  183.      2.  If the image is a 1-bit, 4-bit, or an 8-bit image,     creating  the
  184.          color palette of RGBQUADS.
  185.      3.  Calculating the total file size and putting this  information  in
  186.  
  187.  
  188.  
  189.                   March 15, 1991
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                     - 4 -
  196.  
  197.  
  198.          the "bfSize" field of the BITMAPFILEHEADER.
  199.      4.  Calculating the offset to the image data from the    start  of  the
  200.          file,   and   putting  this  in  the  "bfOffBits"    field  of  the
  201.          BITMAPFILEHEADER.
  202.      5.  Writing to the file the BITMAPFILEHEADER,    the  BITMAPINFOHEADER,
  203.          the color palette, and the image data (in that order).
  204.  
  205.     The following sections describe structures in terms of MicroSoft  C     type-
  206.     defined  keywords.    The following table describes these keywords for those
  207.     not familiar with MicroSoft C.
  208.  
  209.              _______________________________________
  210.             |                       |
  211.             | Type-defined keywords in MicroSoft C.|
  212.             |______________________________________|
  213.             | typedef |     meaning           |
  214.             |_________|____________________________|
  215.             | BYTE    |     unsigned character       |
  216.             | WORD    |     two byte unsigned integer |
  217.             | DWORD   |     four byte unsigned integer|
  218.             |_________|____________________________|
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.                   March 15, 1991
  252.  
  253.  
  254.  
  255.  
  256.  
  257.                     - 5 -
  258.  
  259.  
  260.     THE BITMAPFILEHEADER
  261.  
  262.     This structure is defined in "windows.h" as:
  263.      typedef struct tagBITMAPFILEHEADER {
  264.          WORD     bfType;
  265.          DWORD     bfSize;
  266.          WORD     bfReserved1;
  267.          WORD     bfReserved2;
  268.          DWORD     bfOffBits;
  269.      } BITMAPFILEHEADER;
  270.  
  271.     The "bfType" field is the two ascii characters 'B' and 'M'.      This    serves
  272.     to    identify  the  file  as     a  DIB     image file.  On an intel byte ordered
  273.     machine (IBM or a compatible), this constant can be formed    in  C  as  the
  274.     quantity ('M' << 8 | 'B') == 0x4d42 on an intel system.
  275.  
  276.     The "bfSize" field is the total size of the file.  For our    purposes  this
  277.     field  is equal to: the size of the BITMAPFILEHEADER, plus the size of the
  278.     BITMAPINFOHEADER, plus the number of entries in the     color    palette     times
  279.     the     size  of  a  RGBQUAD, plus the number of bytes of image data.    If the
  280.     image data is being written out in an uncompressed form, this value can be
  281.     calculated    ahead  of  time.   Otherwise, this field needs to be filled in
  282.     later.
  283.  
  284.     Both the "bfReserved1" and the "bfReserved2"  fields  are  always  set  to
  285.     zero.
  286.  
  287.     The "bfOffBits" field is set to the offset to the start of the image  data
  288.     from  the  start  of the file.  For our purposes, this field should be set
  289.     to:     the  size  of    the   BITMAPFILEHEADER,      plus     the   size   of   the
  290.     BITMAPINFOHEADER,  plus  the  number of entries in the color palette times
  291.     the size of a RGBQUAD.  This works out because the image data  is  written
  292.     write after the color palette if one exists.
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.                   March 15, 1991
  312.  
  313.  
  314.  
  315.  
  316.  
  317.                     - 6 -
  318.  
  319.  
  320.     THE BITMAPINFOHEADER
  321.  
  322.     This structure is defined in "windows.h" as:
  323.      typedef struct tagBITMAPINFOHEADER{
  324.          DWORD     biSize;
  325.          DWORD     biWidth;
  326.          DWORD     biHeight;
  327.          WORD     biPlanes;
  328.          WORD     biBitCount;
  329.          DWORD     biCompression;
  330.          DWORD     biSizeImage;
  331.          DWORD     biXPelsPerMeter;
  332.          DWORD     biYPelsPerMeter;
  333.          DWORD     biClrUsed;
  334.          DWORD     biClrImportant;
  335.      } BITMAPINFOHEADER;
  336.  
  337.     The "biSize" field is set to the size of  the  BITMAPINFOHEADER  structure
  338.     itself.   When  reading  the  image     file,    this  value is what is used to
  339.     determine  that  the  image     contains  a  BITMAPINFOHEADER     and   not   a
  340.     BITMAPCOREHEADER.
  341.  
  342.     The "biWidth" field is the width of the image in image pixels.
  343.  
  344.     The "biHeight" field is the height of the image in image lines.
  345.  
  346.     The "biPlanes" field should always be set to 1.  This data is written  out
  347.     as if there was one color plane.
  348.  
  349.     The "biBitCount" field is the bit-depth of the image.  This must be either
  350.     1, 4, 8, or 24, depending on the bit-depth of the image data.
  351.  
  352.     The "biCompression" field tells how the image data is compressed if it  is
  353.     compressed.       DIB    images    support     two  forms  of     run-length  encoding.
  354.     However, I have never seen any images which use it, and don't know yet how
  355.     it works.  Set this field to zero (long zero, or 0L), to indicate that the
  356.     data is not compressed.
  357.  
  358.     All subsequent fields of the BITMAPINFOHEADER  structure  may  be  set  to
  359.     zero.   A  requirement  of    the  interpretting software that it be able to
  360.     compute these fields as necessary from the previous information.
  361.  
  362.     The field which you might want to explicitly specify might be "biClrUsed".
  363.     For     4-bit    and  8-bit  images  this  field     indicates that not all of the
  364.     possible color entries are used and that the  image     contains  "biClrUsed"
  365.     colors.  If you are using only 32 colors with an 8-bit image, then you may
  366.     only want to save 32 of the possible 256 palette entries.  Generally,  set
  367.     this field to zero.
  368.  
  369.  
  370.  
  371.                   March 15, 1991
  372.  
  373.  
  374.  
  375.  
  376.  
  377.                     - 7 -
  378.  
  379.  
  380.     COLOR PALETTES
  381.  
  382.     Each entry of a  color  palette  is     a  RGBQUAD  structure.      The  RGBQUAD
  383.     structure is defined in the "windows.h" include file as:
  384.      typedef struct tagRGBQUAD {
  385.          BYTE     rgbBlue;
  386.          BYTE     rgbGreen;
  387.          BYTE     rgbRed;
  388.          BYTE     rgbReserved;
  389.      } RGBQUAD;
  390.     The "rgbReserved" field is always zero.  For each color used,  the    amount
  391.     of Blue, Green, and Red are filled into the structure and the structure is
  392.     written to the file.  A value of zero in  the  "rgbBlue",  "rgbGreen",  or
  393.     "rgbRed"   fields  indicates  that    that  particular  component  does  not
  394.     contribute to the color composition.  A value  of  255  in    any  of     these
  395.     fields  indicates  that  the  component  contributes  fully     to  the color
  396.     composition.
  397.  
  398.  
  399.  
  400.     IMAGE DATA
  401.  
  402.     There are three surprises about the ordering of image data    in  DIB     image
  403.     file.   The     creator  of  this  format  was determined to be creative, and
  404.     certainly was.
  405.  
  406.     Within the image data, each line written out is padded to  the  next  four
  407.     byte  quantity.   So,  if  you had an 8-bit image which was only one pixel
  408.     wide, you still have to write out four bytes for every  image  line.   The
  409.     number of bytes per line can be calculated as:
  410.  
  411.          bytes_per_line = (width_in_pix * bit_depth + 31 ) / 32 * 4;
  412.  
  413.     ---or, in terms of the fields of the BITMAPINFOHEADER structure---
  414.  
  415.          bytes_per_line = (biWidth * biBitCount + 31 ) / 32 * 4;
  416.  
  417.  
  418.     When writing out your image data, you must write it out bottom line first.
  419.     The     bottom line of the image as you would look at it on the screen is the
  420.     first line of image data in the file.
  421.  
  422.     For 1-bit, 4-bit, and 8-bit images, information is written    as  you     would
  423.     expect.   One  bit    images    are  padded  eight pixels to a byte.  Four bit
  424.     images are padded two pixels to a byte.  Eight bit images are written  one
  425.     pixel per byte.  Twenty-four bit images are written three bytes per pixel.
  426.     However, for 24-bit images the information must  be     written  out  in  the
  427.     order  blue,  green, red.  While most image file formats write data out in
  428.  
  429.  
  430.  
  431.                   March 15, 1991
  432.  
  433.  
  434.  
  435.  
  436.  
  437.                     - 8 -
  438.  
  439.  
  440.     an "RGB" ordering, a DIB image file     writes     the  data  out     in  an     "BGR"
  441.     ordering.
  442.  
  443.     SUMMARY
  444.  
  445.     This  should  provide  enough  information    to  create  DIB     images      from
  446.     applications  such    as  scanners  or for image exporting routines.    If you
  447.     find out any more about DIB images, please pass the information on    to  me
  448.     so that I can modify this document accordingly.
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.                   March 15, 1991
  492.  
  493.  
  494. --
  495.     Thomas B. Kinsman, Rochester, NY, thomas@acadia.kodak.com
  496.     "Practice random kindness and senseless acts of beauty."    -anon
  497.