home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / CVTICO.ZIP / CVTICO.C next >
Text File  |  1990-10-11  |  12KB  |  411 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define  INCL_GPIBITMAPS
  5. #include <os2.h>
  6. #include <pmbitmap.h>
  7.  
  8. typedef  unsigned int        WORD;
  9. typedef  unsigned long       DWORD;
  10.  
  11. typedef struct winicohead
  12. {
  13.    WORD                      icoReserved;
  14.    WORD                      icoResourceType;
  15.    WORD                      icoResourceCount;
  16. }  WinBitMapFileHeader;
  17.  
  18. typedef struct winbitmapinfo
  19. {
  20.    BYTE                      Width;
  21.    BYTE                      Height;
  22.    BYTE                      ColorCount;
  23.    BYTE                      res1;
  24.    WORD                      res2;
  25.    WORD                      res3;
  26.    DWORD                     icoDIBSize;
  27.    DWORD                     icoDIBOffset;
  28. }  WinBitMapInfo;
  29.  
  30. typedef struct windib
  31. {
  32.    DWORD                     size;
  33.    DWORD                     width;
  34.    DWORD                     height;
  35.    WORD                      planes;
  36.    WORD                      bitcount;
  37.    DWORD                     compress;
  38.    DWORD                     sizeimage;
  39.    DWORD                     xpelspermeter;
  40.    DWORD                     ypelspermeter;
  41.    DWORD                     colorused;
  42.    DWORD                     clrimportant;
  43. }  WinDIBHead;
  44.  
  45. typedef struct winrgb
  46. {
  47.    BYTE                      blue;
  48.    BYTE                      green;
  49.    BYTE                      red;
  50.    BYTE                      res;
  51. }  wRGB;
  52.  
  53. void  disp_wbmfh ( WinBitMapFileHeader * );
  54. void  disp_wbmi  ( WinBitMapInfo * );
  55. void  disp_wdib  ( WinDIBHead * );
  56. void  disp_rgb   ( wRGB * );
  57. void  disp_colormap ( BYTE *, WinBitMapInfo * );
  58. void  disp_monomap  ( BYTE *, WinBitMapInfo * );
  59. void  disp_byte ( BYTE );
  60. int   bitsperpel ( BYTE );
  61. void  write_mono_rgb ( FILE * );
  62. void  write_os2_rgb ( FILE *, wRGB *, int );
  63.  
  64. /* OS/2 structures */
  65. BITMAPARRAYFILEHEADER        bafh;
  66. BITMAPFILEHEADER             bfh;
  67. RGB                          rgb;
  68.  
  69.  
  70. void  main 
  71. (
  72.    int                       argc,
  73.    char                      *argv []
  74. )
  75.  
  76. {
  77.    FILE                      *fp;
  78.    FILE                      *ofp;
  79.    WinBitMapFileHeader       wbmfh;
  80.    WinBitMapInfo             wbmi;
  81.    WinDIBHead                dibhead;
  82.    BYTE                      colormap [64 * 64];
  83.    BYTE                      monomap  [64 * 64];
  84.    wRGB                      rgbtable [64];
  85.    int                       i;
  86.    int                       j;
  87.    int                       pels;
  88.    fpos_t                    curpos;
  89.    long                      last_bafh;
  90.  
  91.    if ( argc != 3 )
  92.    {
  93.       fprintf ( stderr, "The correct call is:\n   cvtico <infile> <outfile>" );
  94.       exit ( 1 );
  95.    }
  96.  
  97.    if ( NULL == (fp = fopen ( argv [1], "rb" )) )
  98.    {
  99.       perror ( argv [1] );
  100.       exit ( 1 );
  101.    }
  102.  
  103.    if ( NULL == (ofp = fopen ( argv [2], "w+b" )) )
  104.    {
  105.       perror ( argv [2]);
  106.       exit ( 1 );
  107.    }
  108.    fseek ( ofp, 0L, SEEK_SET );
  109.  
  110.    fread ( &wbmfh, sizeof ( wbmfh ), 1, fp );
  111.    #if defined (DEBUG)
  112.       disp_wbmfh ( &wbmfh );
  113.    #endif
  114.  
  115.    for ( i = 0; i < wbmfh. icoResourceCount; i++ )
  116.    {
  117.       fread ( &wbmi, sizeof ( wbmi ), 1, fp );
  118.       #if defined (DEBUG)
  119.          disp_wbmi ( &wbmi );
  120.       #endif
  121.  
  122.       fgetpos ( fp, &curpos );
  123.  
  124.          fseek ( fp, wbmi.icoDIBOffset, SEEK_SET );
  125.          pels                =  8 / bitsperpel ( wbmi. ColorCount );
  126.          fread ( &dibhead, sizeof ( dibhead ), 1, fp );
  127.          #if defined (DEBUG)
  128.             disp_wdib ( &dibhead );
  129.          #endif
  130.          fread ( rgbtable, sizeof ( wRGB ), wbmi. ColorCount, fp );
  131.          #if defined (DEBUG)
  132.             for ( j = 0; j < wbmi. ColorCount; j++ )
  133.                disp_rgb ( &rgbtable [j] );
  134.          #endif
  135.  
  136.          fread ( colormap, sizeof ( BYTE ), wbmi.Width * wbmi.Height / pels, fp );
  137.          #if defined (DEBUG)
  138.             disp_colormap ( colormap, &wbmi );
  139.          #endif
  140.          fread ( monomap,  sizeof ( BYTE ), wbmi.Width * wbmi.Height / 8, fp );
  141.          #if defined (DEBUG)
  142.             disp_monomap ( monomap, &wbmi );
  143.          #endif
  144.  
  145.          last_bafh           =  ftell ( ofp );
  146.  
  147.          /* Write it out now */
  148.          bafh.usType         =  BFT_BITMAPARRAY;
  149.          bafh.cbSize         =  sizeof ( bafh );
  150.          bafh.offNext        =  0L;
  151.          bafh.cxDisplay      =  0;
  152.          bafh.cyDisplay      =  0;
  153.  
  154.          /* The first bfh is for the AndXor mask */
  155.          bafh.bfh.usType     =  BFT_COLORICON;
  156.          bafh.bfh.cbSize     =  sizeof ( bafh. bfh );
  157.          bafh.bfh.xHotspot   =  0;
  158.          bafh.bfh.yHotspot   =  0;
  159.          bafh.bfh.offBits    =  last_bafh            + 
  160.                                 sizeof ( bafh )      +
  161.                                 2 * 3                +
  162.                                 sizeof ( bfh )       +
  163.                                 wbmi. ColorCount * 3;
  164.  
  165.          bafh.bfh.bmp.cbFix     =  sizeof ( bafh. bfh. bmp );
  166.          bafh.bfh.bmp.cx        =  wbmi.Width;
  167.          bafh.bfh.bmp.cy        =  wbmi.Height * 2;
  168.          bafh.bfh.bmp.cPlanes   =  1;
  169.          bafh.bfh.bmp.cBitCount =  1;
  170.  
  171.          /* The second bfh is for the Color mask */
  172.          bfh.usType          =  BFT_COLORICON;
  173.          bfh.cbSize          =  sizeof ( bafh. bfh );
  174.          bfh.xHotspot        =  0;
  175.          bfh.yHotspot        =  0;
  176.          bfh.offBits         =  last_bafh               + 
  177.                                 sizeof ( bafh )         + 
  178.                                 2 * 3                   +
  179.                                 sizeof ( bfh )          +
  180.                                 wbmi. ColorCount * 3    +
  181.                                 wbmi.Width * wbmi.Height / 4;
  182.  
  183.          bfh.bmp.cbFix       =  sizeof ( bafh. bfh. bmp );
  184.          bfh.bmp.cx          =  wbmi.Width;
  185.          bfh.bmp.cy          =  wbmi.Height;
  186.          bfh.bmp.cPlanes     =  1;
  187.          bfh.bmp.cBitCount   =  bitsperpel ( wbmi. ColorCount );
  188.  
  189.          fwrite ( &bafh, sizeof ( bafh ), 1, ofp );
  190.          write_mono_rgb ( ofp );
  191.          fwrite ( &bfh,  sizeof ( bfh ),  1, ofp );
  192.          write_os2_rgb ( ofp, rgbtable, wbmi. ColorCount );
  193.  
  194.          fwrite ( monomap,  sizeof ( BYTE ), wbmi.Width * wbmi.Height / 8, ofp );
  195.          fwrite ( monomap,  sizeof ( BYTE ), wbmi.Width * wbmi.Height / 8, ofp );
  196.          fwrite ( colormap, sizeof ( BYTE ), wbmi.Width * wbmi.Height / pels, ofp );
  197.  
  198.       fsetpos ( fp, &curpos );
  199.    }
  200.  
  201.    fclose ( fp );
  202.    fclose ( ofp );
  203.  
  204.    exit ( 0 );
  205. }
  206.  
  207. /**/
  208. /**************************************************************************************************/
  209.  
  210. void  disp_wbmfh 
  211.    WinBitMapFileHeader       *wbmfh
  212. )
  213. {
  214.    printf ( "icoReserved      = %x\n", wbmfh -> icoReserved      );
  215.    printf ( "icoResourceType  = %x\n", wbmfh -> icoResourceType  );
  216.    printf ( "icoResourceCount = %x\n", wbmfh -> icoResourceCount );
  217. }
  218.  
  219. /**/
  220. /**************************************************************************************************/
  221.  
  222. void  disp_wbmi  
  223.    WinBitMapInfo             *wbmi
  224. )
  225. {
  226.    printf ( "Width        = %x\n",  wbmi -> Width        );
  227.    printf ( "Height       = %x\n",  wbmi -> Height       ); 
  228.    printf ( "ColorCount   = %x\n",  wbmi -> ColorCount   ); 
  229.    printf ( "res1         = %x\n",  wbmi -> res1         ); 
  230.    printf ( "res2         = %x\n",  wbmi -> res2         ); 
  231.    printf ( "res3         = %x\n",  wbmi -> res3         ); 
  232.    printf ( "icoDIBSize   = %lx\n", wbmi -> icoDIBSize   ); 
  233.    printf ( "icoDIBOffset = %lx\n", wbmi -> icoDIBOffset ); 
  234. }
  235.  
  236. /**/
  237. /**************************************************************************************************/
  238.  
  239. void  disp_wdib  
  240.    WinDIBHead                *dib
  241. )
  242. {
  243.    printf ( "size          = %lx\n", dib -> size          );
  244.    printf ( "width         = %lx\n", dib -> width         );
  245.    printf ( "height        = %lx\n", dib -> height        );
  246.    printf ( "planes        = %x\n",  dib -> planes        );
  247.    printf ( "bitcount      = %x\n",  dib -> bitcount      );
  248.    printf ( "compress      = %lx\n", dib -> compress      );
  249.    printf ( "sizeimage     = %lx\n", dib -> sizeimage     );
  250.    printf ( "xpelspermeter = %lx\n", dib -> xpelspermeter );
  251.    printf ( "ypelspermeter = %lx\n", dib -> ypelspermeter );
  252.    printf ( "colorused     = %lx\n", dib -> colorused     );
  253.    printf ( "clrimportant  = %lx\n", dib -> clrimportant  );
  254. }
  255.  
  256. /**/
  257. /**************************************************************************************************/
  258.  
  259. void  disp_rgb  
  260.    wRGB                       *rgb
  261. )
  262. {
  263.    printf ( "%02x %02x %02x %02x\n", rgb -> blue, rgb -> green, rgb -> red, rgb -> res );
  264. }
  265.  
  266. /**/
  267. /**************************************************************************************************/
  268.  
  269. int   bitsperpel 
  270.    BYTE                      colorcount
  271. )
  272. {
  273.    if ( colorcount == 2 )
  274.       return ( 1 );
  275.    else if ( colorcount == 8 )
  276.       return ( 3 );
  277.    else
  278.       return ( 4 );
  279. }
  280.  
  281. /**/
  282. /**************************************************************************************************/
  283.  
  284. void  disp_colormap 
  285.    BYTE                      *colormap,
  286.    WinBitMapInfo             *wbmi
  287. )
  288. {
  289.    int                       w;
  290.    int                       h;
  291.    int                       width;
  292.    int                       height;
  293.    int                       pels;
  294.  
  295.    width                     =  wbmi -> Width;
  296.    height                    =  wbmi -> Height;
  297.    pels                      =  8 / bitsperpel ( wbmi -> ColorCount );
  298.  
  299.    for ( h = 0; h < height; h++ )
  300.    {
  301.       for ( w = 0; w < width / pels; w++ )
  302.          printf ( "%02x", colormap [ h * width / pels + w ] );
  303.       printf ( "\n" );
  304.    }
  305. }
  306.  
  307. /**/
  308. /**************************************************************************************************/
  309.  
  310. void  disp_monomap
  311.    BYTE                      *monomap,
  312.    WinBitMapInfo             *wbmi
  313. )
  314. {
  315.    int                       w;
  316.    int                       h;
  317.    int                       width;
  318.    int                       height;
  319.  
  320.    width                     =  wbmi -> Width;
  321.    height                    =  wbmi -> Height;
  322.  
  323.    for ( h = 0; h < height; h++ )
  324.    {
  325.       for ( w = 0; w < width / 8; w++ )
  326.          disp_byte ( monomap [ h * width / 8 + w ] );
  327.       printf ( "\n" );
  328.    }
  329. }
  330.  
  331. /**/
  332. /**************************************************************************************************/
  333.  
  334. void  disp_byte 
  335.    BYTE                      b 
  336. )
  337. {
  338.    int                       i;
  339.  
  340.    for ( i = 0; i < 8; i++ )
  341.    {
  342.       printf ( "%d", ( 0 == (b & 0x80) ) ? 0 : 1 );
  343.       b                      =  b << 1;
  344.    }
  345. }
  346.  
  347. /**/
  348. /**************************************************************************************************/
  349.  
  350. char  os2_rgb_defaults [] =
  351. {
  352.    0x00, 0x00, 0x00,
  353.    0xff, 0xff, 0xff,
  354.    0xff, 0x00, 0x00,
  355.    0x00, 0x00, 0xff,
  356.    0xff, 0x00, 0xff,
  357.    0x00, 0xff, 0x00,
  358.    0xff, 0xff, 0x00,
  359.    0x00, 0xff, 0xff,
  360.    0x40, 0x40, 0x40,
  361.    0x80, 0x00, 0x00,
  362.    0x00, 0x00, 0x80,
  363.    0x80, 0x00, 0x80,
  364.    0x00, 0x80, 0x00,
  365.    0x80, 0x80, 0x00,
  366.    0x00, 0x34, 0x78,
  367.    0xc0, 0xc0, 0xc0
  368. };
  369. void  write_os2_rgb 
  370.    FILE                      *ofp,
  371.    wRGB                      *rgbtable,
  372.    int                       colorcount
  373. )
  374. {
  375.    int                       i;
  376.  
  377.    for ( i = 0; i < colorcount; i++ )
  378.    {
  379.       fwrite ( &rgbtable [i].blue,  sizeof ( BYTE ), 1, ofp );
  380.       fwrite ( &rgbtable [i].green, sizeof ( BYTE ), 1, ofp );
  381.       fwrite ( &rgbtable [i].red,   sizeof ( BYTE ), 1, ofp );
  382.    }
  383.  
  384. // fwrite ( os2_rgb_defaults, sizeof ( os2_rgb_defaults ), 1, ofp );
  385. }
  386.  
  387. /**/
  388. /**************************************************************************************************/
  389.  
  390. char  mono_rgb_defaults [] =
  391. {
  392.    0x00, 0x00, 0x00,
  393.    0xff, 0xff, 0xff
  394. };
  395. void  write_mono_rgb 
  396.    FILE                      *ofp
  397. )
  398. {
  399.    fwrite ( mono_rgb_defaults, sizeof ( mono_rgb_defaults ), 1, ofp );
  400. }
  401.