home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / bmp.h < prev    next >
C/C++ Source or Header  |  1993-10-04  |  4KB  |  196 lines

  1. /*\
  2.  * $Id: bmp.h,v 1.3 1992/11/24 19:39:56 dws Exp dws $
  3.  * 
  4.  * bmp.h - routines to calculate sizes of parts of BMP files
  5.  *
  6.  * Some fields in BMP files contain offsets to other parts
  7.  * of the file.  These routines allow us to calculate these
  8.  * offsets, so that we can read and write BMP files without
  9.  * the need to fseek().
  10.  * 
  11.  * Copyright (C) 1992 by David W. Sanderson.
  12.  * 
  13.  * Permission to use, copy, modify, and distribute this software and its
  14.  * documentation for any purpose and without fee is hereby granted,
  15.  * provided that the above copyright notice appear in all copies and
  16.  * that both that copyright notice and this permission notice appear
  17.  * in supporting documentation.  This software is provided "as is"
  18.  * without express or implied warranty.
  19.  * 
  20.  * $Log: bmp.h,v $
  21.  * Revision 1.3  1992/11/24  19:39:56  dws
  22.  * Added copyright.
  23.  *
  24.  * Revision 1.2  1992/11/17  02:13:37  dws
  25.  * Adjusted a string's name.
  26.  *
  27.  * Revision 1.1  1992/11/16  19:54:44  dws
  28.  * Initial revision
  29.  *
  30. \*/
  31.  
  32. #ifndef _BMP_H_
  33. #define _BMP_H_
  34.  
  35. #include    "pbmplus.h"
  36.  
  37. /* prototypes */
  38. static unsigned long BMPlenfileheader ARGS((int class));
  39. static unsigned long BMPleninfoheader ARGS((int class));
  40. static unsigned long BMPlenrgbtable ARGS((int class, unsigned long bitcount));
  41. static unsigned long BMPlenline ARGS((int class, unsigned long bitcount, unsigned long x));
  42. static unsigned long BMPlenbits ARGS((int class, unsigned long bitcount, unsigned long x, unsigned long y));
  43. static unsigned long BMPlenfile ARGS((int class, unsigned long bitcount, unsigned long x, unsigned long y));
  44. static unsigned long BMPoffbits ARGS((int class, unsigned long bitcount));
  45. /*
  46.  * Classes of BMP files
  47.  */
  48.  
  49. #define C_WIN    1
  50. #define C_OS2    2
  51.  
  52. static char     er_internal[] = "%s: internal error!";
  53.  
  54. static unsigned long
  55. BMPlenfileheader(class)
  56.     int             class;
  57. {
  58.     switch (class)
  59.     {
  60.     case C_WIN:
  61.         return 14;
  62.     case C_OS2:
  63.         return 14;
  64.     default:
  65.         pm_error(er_internal, "BMPlenfileheader");
  66.         return 0;
  67.     }
  68. }
  69.  
  70. static unsigned long
  71. BMPleninfoheader(class)
  72.     int             class;
  73. {
  74.     switch (class)
  75.     {
  76.     case C_WIN:
  77.         return 40;
  78.     case C_OS2:
  79.         return 12;
  80.     default:
  81.         pm_error(er_internal, "BMPleninfoheader");
  82.         return 0;
  83.     }
  84. }
  85.  
  86. static unsigned long
  87. BMPlenrgbtable(class, bitcount)
  88.     int             class;
  89.     unsigned long   bitcount;
  90. {
  91.     unsigned long   lenrgb;
  92.  
  93.     if (bitcount < 1)
  94.     {
  95.         pm_error(er_internal, "BMPlenrgbtable");
  96.         return 0;
  97.     }
  98.     switch (class)
  99.     {
  100.     case C_WIN:
  101.         lenrgb = 4;
  102.         break;
  103.     case C_OS2:
  104.         lenrgb = 3;
  105.         break;
  106.     default:
  107.         pm_error(er_internal, "BMPlenrgbtable");
  108.         return 0;
  109.     }
  110.  
  111.     return (1 << bitcount) * lenrgb;
  112. }
  113.  
  114. /*
  115.  * length, in bytes, of a line of the image
  116.  * 
  117.  * Evidently each row is padded on the right as needed to make it a
  118.  * multiple of 4 bytes long.  This appears to be true of both
  119.  * OS/2 and Windows BMP files.
  120.  */
  121. static unsigned long
  122. BMPlenline(class, bitcount, x)
  123.     int             class;
  124.     unsigned long   bitcount;
  125.     unsigned long   x;
  126. {
  127.     unsigned long   bitsperline;
  128.  
  129.     switch (class)
  130.     {
  131.     case C_WIN:
  132.         break;
  133.     case C_OS2:
  134.         break;
  135.     default:
  136.         pm_error(er_internal, "BMPlenline");
  137.         return 0;
  138.     }
  139.  
  140.     bitsperline = x * bitcount;
  141.  
  142.     /*
  143.      * if bitsperline is not a multiple of 32, then round
  144.      * bitsperline up to the next multiple of 32.
  145.      */
  146.     if ((bitsperline % 32) != 0)
  147.     {
  148.         bitsperline += (32 - (bitsperline % 32));
  149.     }
  150.  
  151.     if ((bitsperline % 32) != 0)
  152.     {
  153.         pm_error(er_internal, "BMPlenline");
  154.         return 0;
  155.     }
  156.  
  157.     /* number of bytes per line == bitsperline/8 */
  158.     return bitsperline >> 3;
  159. }
  160.  
  161. /* return the number of bytes used to store the image bits */
  162. static unsigned long
  163. BMPlenbits(class, bitcount, x, y)
  164.     int             class;
  165.     unsigned long   bitcount;
  166.     unsigned long   x;
  167.     unsigned long   y;
  168. {
  169.     return y * BMPlenline(class, bitcount, x);
  170. }
  171.  
  172. /* return the offset to the BMP image bits */
  173. static unsigned long
  174. BMPoffbits(class, bitcount)
  175.     int             class;
  176.     unsigned long   bitcount;
  177. {
  178.     return BMPlenfileheader(class)
  179.         + BMPleninfoheader(class)
  180.         + BMPlenrgbtable(class, bitcount);
  181. }
  182.  
  183. /* return the size of the BMP file in bytes */
  184. static unsigned long
  185. BMPlenfile(class, bitcount, x, y)
  186.     int             class;
  187.     unsigned long   bitcount;
  188.     unsigned long   x;
  189.     unsigned long   y;
  190. {
  191.     return BMPoffbits(class, bitcount)
  192.         + BMPlenbits(class, bitcount, x, y);
  193. }
  194.  
  195. #endif /* _BMP_H_ */
  196.