home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / CVBMP.ZIP / CVBMP.C < prev    next >
C/C++ Source or Header  |  1990-07-27  |  9KB  |  385 lines

  1. /******************************************************************************
  2.  
  3.    Module Name: cvbmp
  4.    Description: Converts a Windows 3.0 color bitmap to the OS/2 PM bitmap format
  5.    Version:     v1.00
  6.    Authors:     Mike Robert
  7.    Date:        7/27/90
  8.  
  9.    Copyright (C) 1990 by Michael C. Robert.  All rights reserved.
  10.  
  11.    Revision History:
  12.  
  13.    Bugs:
  14.  
  15.    Enhancements:
  16.  
  17.    Note:
  18.  
  19.       This program was written before I could obtain a copy of the
  20.    Windows 3.0 SDK, so it may not work for all possible bitmaps.  I had to
  21.    decode the header of the Windows bitmap and I may not have been 100%
  22.    accurate in this.
  23.       
  24.    So be warned!
  25.  
  26.       Also, the library used to compile this program is LLIBCEC, which may be
  27.    named differently on your computer depending on how you installed the
  28.    compiler.
  29.  
  30.       Oh, and I forgot to mention, I'm using the Microsoft C v5.1 compiler.
  31.  
  32.       To recompile just use "MAKE CVBMP".
  33.  
  34. ******************************************************************************/
  35.  
  36. /* INCLUDE FILES
  37. */
  38.  
  39. #include <fcntl.h>
  40. #include <sys\types.h>
  41. #include <sys\stat.h>
  42. #include <io.h>
  43. #include <malloc.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46.  
  47. /* SYMBOLIC CONSTANTS
  48. */
  49.  
  50. #define  SZB_FILE             128
  51.  
  52. /* MACROS
  53. */
  54.  
  55. /* TYPEDEFS
  56. */
  57.  
  58. typedef char BYTE;
  59.  
  60. typedef unsigned char UBYTE;
  61.  
  62. typedef short WORD;
  63.  
  64. typedef unsigned short UWORD;
  65.  
  66. typedef short SHORT;
  67.  
  68. typedef unsigned short USHORT;
  69.  
  70. typedef long LONG;
  71.  
  72. typedef unsigned long ULONG;
  73.  
  74. typedef int INT;
  75.  
  76. typedef unsigned int UINT;
  77.  
  78. typedef struct _OS2BITMAPINFOHEADER          /* os2bmp */
  79. {
  80.    ULONG          cbFix;
  81.    USHORT         cx;
  82.    USHORT         cy;
  83.    USHORT         cPlanes;
  84.    USHORT         cBitCount;
  85. }
  86.    OS2BITMAPINFOHEADER;
  87.  
  88. typedef struct _OS2BITMAPFILEHEADER          /* os2bfh */
  89. {
  90.    USHORT         usType;
  91.    ULONG          cbSize;
  92.    INT            xHotspot;
  93.    INT            yHotspot;
  94.    ULONG          offBits;
  95.    OS2BITMAPINFOHEADER os2bmp;
  96. }
  97.    OS2BITMAPFILEHEADER;
  98.  
  99. typedef struct _OS2RGB                       /* os2rgb */
  100. {
  101.    BYTE           bBlue;
  102.    BYTE           bGreen;
  103.    BYTE           bRed;
  104. }
  105.    OS2RGB;
  106.  
  107. typedef struct _BITMAPHDR                    /* bmp */
  108. {
  109.    ULONG          ulBitMapHdrSize;
  110.  
  111.    ULONG          ulWidth,
  112.                   ulHeight;
  113.  
  114.    USHORT         usPlanes,
  115.                   usBitsPerPlane;
  116.  
  117.    ULONG          ulFoo1,
  118.                   ulFoo2,
  119.                   ulFoo3,
  120.                   ulFoo4,
  121.                   ulFoo5,
  122.                   ulFoo6;
  123. }
  124.    BITMAPHDR;
  125.  
  126. typedef struct _BITMAPFILEHDR                /* bmfh */
  127. {
  128.    USHORT         usType;
  129.  
  130.    ULONG          ulBitmapLength;
  131.  
  132.    USHORT         usHotSpotX,
  133.                   usHotSpotY;
  134.  
  135.    ULONG          ulBitsOffset;
  136.  
  137.    BITMAPHDR      bmp;
  138.  
  139. }
  140.    BITMAPFILEHDR;
  141.  
  142. /* GLOBAL VARIABLES
  143. */
  144.  
  145. /* FUNCTION PROTOYPES
  146. */
  147.  
  148. int main(
  149.  
  150.    int iArgC,           /* Argument count.
  151.                         */
  152.  
  153.    char *apszArgV[]     /* Argument string pointer array.
  154.                         */
  155.  
  156.    );
  157.  
  158. /******************************************************************************
  159.  
  160.    Function Name: main
  161.  
  162.    Description:
  163.  
  164.       Performs main processing.
  165.  
  166.    Control Flow:
  167.  
  168.    Return Values:
  169.  
  170.       Success: 0
  171.       Failure: 1
  172.  
  173.    Notes:
  174.  
  175. ******************************************************************************/
  176.  
  177. int main(
  178.  
  179.    int iArgC,           /* Argument count.
  180.                         */
  181.  
  182.    char *apszArgV[]     /* Argument string pointer array.
  183.                         */
  184.  
  185.    )
  186.  
  187. {
  188.    /*  Variable definition.  */
  189.  
  190.    char           szFile[ SZB_FILE ];        /* File name */
  191.  
  192.    int            fhIn, fhOut;               /* File handles */
  193.  
  194.    BITMAPFILEHDR  bmfh;                      /* Windows bitmap file header */
  195.  
  196.    OS2BITMAPFILEHEADER os2bfh;               /* OS/2 bitmap file header */
  197.  
  198.    int            iC, iMax;                  /* Counting variables */
  199.  
  200.    ULONG          ulColor;                   /* Color entry */
  201.  
  202.    USHORT         usScan;                    /* Scan line length */
  203.  
  204.    char           *pBuffer;                  /* Buffer pointer */
  205.  
  206.    /*  Check for invalid number of arguments.  */
  207.  
  208.    if ( iArgC != 2 )
  209.    {
  210.       printf( "CVBMP.EXE Invokation: \n\n" );
  211.  
  212.       printf( "   CVBMP bitmapfile (no extension)\n" );
  213.  
  214.       return ( 1 );
  215.  
  216.    }
  217.  
  218.    /*  Open the bitmap files.  */
  219.  
  220.    strcpy( szFile, apszArgV[ 1 ] );
  221.    strcat( szFile, ".BMP" );
  222.  
  223.    strupr( szFile );
  224.  
  225.    fhIn = open( szFile, O_RDONLY | O_BINARY );
  226.    if ( fhIn == -1 )
  227.    {
  228.       printf( "Can't open input file: %s\n", szFile );
  229.       return ( 1 );
  230.    }
  231.  
  232.    strcpy( szFile, apszArgV[ 1 ] );
  233.    strcat( szFile, ".OS2" );
  234.  
  235.    strupr( szFile );
  236.  
  237.    fhOut = open( szFile, O_CREAT | O_RDWR | O_BINARY, S_IREAD | S_IWRITE );
  238.    if ( fhOut == -1 )
  239.    {
  240.       close( fhIn );
  241.       printf( "Can't open output file: %s\n", szFile );
  242.       return ( 1 );
  243.    }
  244.  
  245.    /*  Read, translate, and write the header.  */
  246.  
  247.    if ( read( fhIn,
  248.               (char *) &bmfh,
  249.               sizeof ( BITMAPFILEHDR )
  250.             ) != sizeof ( BITMAPFILEHDR )
  251.       )
  252.    {
  253.       close( fhIn );
  254.       close( fhOut );
  255.       printf( "Can't read bitmap file header\n" );
  256.       return ( 1 );
  257.    }
  258.  
  259.    os2bfh.usType   = bmfh.usType;
  260.    os2bfh.cbSize   = bmfh.ulBitmapLength - bmfh.ulBitsOffset;
  261.    os2bfh.xHotspot = bmfh.usHotSpotX;
  262.    os2bfh.yHotspot = bmfh.usHotSpotY;
  263.    os2bfh.offBits  = sizeof ( OS2BITMAPFILEHEADER );
  264.  
  265.    if ( bmfh.bmp.usBitsPerPlane < 24 )
  266.       os2bfh.offBits += sizeof ( OS2RGB ) * ( 1 << bmfh.bmp.usBitsPerPlane );
  267.  
  268.    os2bfh.os2bmp.cbFix     = sizeof ( OS2BITMAPINFOHEADER );
  269.    os2bfh.os2bmp.cx        = (USHORT) bmfh.bmp.ulWidth;
  270.    os2bfh.os2bmp.cy        = (USHORT) bmfh.bmp.ulHeight;
  271.    os2bfh.os2bmp.cPlanes   = bmfh.bmp.usPlanes;
  272.    os2bfh.os2bmp.cBitCount = bmfh.bmp.usBitsPerPlane;
  273.  
  274.    if ( write( fhOut,
  275.                (char *) &os2bfh,
  276.                sizeof ( OS2BITMAPFILEHEADER )
  277.              ) != sizeof ( OS2BITMAPFILEHEADER )
  278.       )
  279.    {
  280.       close( fhIn );
  281.       close( fhOut );
  282.       printf( "Can't write bitmap file header\n" );
  283.       return ( 1 );
  284.    }
  285.  
  286.    /*  Read and translate the color table if one is present.  */
  287.  
  288.    if ( bmfh.bmp.usBitsPerPlane < 24 )
  289.    {
  290.       /*  Calculate the number of entries.  */
  291.  
  292.       iMax = 1 << bmfh.bmp.usBitsPerPlane;
  293.  
  294.       for ( iC = 0; iC < iMax; ++iC )
  295.       {
  296.          /*  Read the color and write the new one.  */
  297.  
  298.          if ( read( fhIn,
  299.                     (char *) &ulColor,
  300.                     sizeof ( ULONG )
  301.                   ) != sizeof ( ULONG )
  302.             )
  303.          {
  304.             close( fhIn );
  305.             close( fhOut );
  306.             printf( "Can't read bitmap color table entry\n" );
  307.             return ( 1 );
  308.          }
  309.  
  310.          if ( write( fhOut,
  311.                      (char *) &ulColor,
  312.                      sizeof ( OS2RGB )
  313.                    ) != sizeof ( OS2RGB )
  314.             )
  315.          {
  316.             close( fhIn );
  317.             close( fhOut );
  318.             printf( "Can't write bitmap color table entry\n" );
  319.             return ( 1 );
  320.          }
  321.  
  322.       }
  323.  
  324.    }
  325.  
  326.    /*  Finally, read and translate the bits.  */
  327.  
  328.    usScan = ( (USHORT) ( bmfh.bmp.ulWidth * bmfh.bmp.usBitsPerPlane / 8 ) + 3 ) / 4 * 4;
  329.  
  330.    pBuffer = malloc( usScan );
  331.    if ( pBuffer == NULL )
  332.    {
  333.       close( fhIn );
  334.       close( fhOut );
  335.       printf( "Can't allocate scan line buffer\n" );
  336.       return ( 1 );
  337.    }
  338.  
  339.    iMax = (USHORT) bmfh.bmp.ulHeight * bmfh.bmp.usPlanes;
  340.  
  341.    for ( iC = 0; iC < iMax; ++iC )
  342.    {
  343.       /*  Read a scan line in and then write it out.  */
  344.  
  345.       printf( "%d \r", iMax - iC );
  346.  
  347.       if ( read( fhIn,
  348.                  pBuffer,
  349.                  usScan
  350.                ) != usScan
  351.          )
  352.       {
  353.          close( fhIn );
  354.          close( fhOut );
  355.          printf( "Can't read bitmap scan line\n" );
  356.          return ( 1 );
  357.       }
  358.  
  359.       if ( write( fhOut,
  360.                   pBuffer,
  361.                   usScan
  362.                 ) != usScan
  363.          )
  364.       {
  365.          close( fhIn );
  366.          close( fhOut );
  367.          printf( "Can't write bitmap scan line\n" );
  368.          return ( 1 );
  369.       }
  370.  
  371.    }
  372.  
  373.    free( pBuffer );
  374.  
  375.    /*  Close the files and exit.  */
  376.  
  377.    close( fhIn );
  378.    close( fhOut );
  379.  
  380.    printf( "Conversion complete.  File %s created.\n", szFile );
  381.  
  382.    return ( 0 );
  383.  
  384. }
  385.