home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xicon05.zip / xicon.c < prev    next >
C/C++ Source or Header  |  1993-05-09  |  10KB  |  360 lines

  1. /* This file is xicon.c (part of XIcon)
  2.  *
  3.  * Copyright (C) 1993 by Norman Walsh
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 2 of the License, or
  8.  *   (at your option) any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * Norman Walsh can be reached electronically at <walsh@cs.umass.edu>
  20.  * or via paper mail to:
  21.  *  
  22.  *  Norman Walsh
  23.  *  Small Planet Software
  24.  *  87 South Silver Lane, #2
  25.  *  Sunderland, MA 01375
  26.  ************************************************************************/
  27.  
  28. #define EMXGCC
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #ifdef EMXGCC
  33. /*
  34.   #define INCL_BASE
  35.   #define INCL_PM
  36.   #define SKIPTYPES
  37.   #include <os2emx.h>
  38. */
  39. #include <getopt.h>
  40. #endif /* EMXGCC */
  41. #include "macread.h"
  42. #include "winread.h"
  43. #include "bitmap.h"
  44. #include "icondata.h"
  45. #include "os2write.h"
  46. #include "xbmread.h"
  47. #include "palette.h"
  48. #include "xbmdata.h"
  49. #include "winwrite.h"
  50.  
  51. /* Optional arguments */
  52.  
  53. #ifndef EMXGCC /* not */
  54. extern char *optarg;
  55. extern int optind, opterr;
  56. #endif /* not EMXGCC */
  57.  
  58. int generic_count = 0;
  59. generic_info *generic = NULL;
  60. int dump_input = 0;
  61. int write_20_format = 0;
  62. int write_win_format = 0;
  63.  
  64. rgb_info *pc_palette = NULL;
  65. rgb_info *mac_palette = NULL;
  66. rgb_info *bw_palette = NULL;
  67.  
  68. extern int cvtICNp;
  69. extern int cvticsp;
  70. extern int cvticl4;
  71. extern int cvtics4;
  72. extern int MacPalette;
  73.  
  74. extern int XBMrevbits;
  75. extern int XBMforg;
  76. extern int XBMback;
  77.  
  78. FILE *input;
  79. FILE *output;
  80. FILE *maskfile;
  81. int rc;
  82.  
  83. void error (char *msg)
  84. {
  85.   printf("%s\n", msg);
  86.   exit(1);
  87. }
  88.  
  89. void init_palettes()
  90. {
  91.   pc_palette = (rgb_info *) malloc (sizeof (rgb_info) * 16);
  92.   mac_palette = (rgb_info *) malloc (sizeof (rgb_info) * 16);
  93.   bw_palette = (rgb_info *) malloc (sizeof (rgb_info) * 2);
  94.  
  95.   pc_palette[ 0] = palette_color (255, 255, 255);
  96.   pc_palette[ 1] = palette_color (128,   0,   0);
  97.   pc_palette[ 2] = palette_color (  0, 128,   0);
  98.   pc_palette[ 3] = palette_color (128, 128,   0);
  99.   pc_palette[ 4] = palette_color (  0,   0, 128);
  100.   pc_palette[ 5] = palette_color (128,   0, 128);
  101.   pc_palette[ 6] = palette_color (  0, 128, 128);
  102.   pc_palette[ 7] = palette_color (128, 128, 128);
  103.   pc_palette[ 8] = palette_color (204, 204, 204);
  104.   pc_palette[ 9] = palette_color (255,   0,   0);
  105.   pc_palette[10] = palette_color (  0, 255,   0);
  106.   pc_palette[11] = palette_color (255, 255,   0);
  107.   pc_palette[12] = palette_color (  0,   0, 255);
  108.   pc_palette[13] = palette_color (255,   0, 255);
  109.   pc_palette[14] = palette_color (  0, 255, 255);
  110.   pc_palette[15] = palette_color (  0,   0,   0);
  111.   
  112.   mac_palette[ 0] = palette_color (255, 255, 255);
  113.   mac_palette[ 1] = palette_color (  0, 255, 255);
  114.   mac_palette[ 2] = palette_color ( 16, 128, 255);
  115.   mac_palette[ 3] = palette_color (  0,   0, 255);
  116.   mac_palette[ 4] = palette_color (128,   0, 255);
  117.   mac_palette[ 5] = palette_color (177,  17,  20);
  118.   mac_palette[ 6] = palette_color (212,   0,   0);
  119.   mac_palette[ 7] = palette_color (194, 194,   0);
  120.   mac_palette[ 8] = palette_color (  0, 255,   0);
  121.   mac_palette[ 9] = palette_color (  0, 162,   0);
  122.   mac_palette[10] = palette_color ( 75,  97, 193);
  123.   mac_palette[11] = palette_color ( 25, 159, 192);
  124.   mac_palette[12] = palette_color (224, 224, 224);
  125.   mac_palette[13] = palette_color (161, 161, 161);
  126.   mac_palette[14] = palette_color ( 82,  82,  82);
  127.   mac_palette[15] = palette_color (  0,   0,   0);
  128.   
  129.   bw_palette[0] = palette_color(  0,   0,    0);
  130.   bw_palette[1] = palette_color(255, 255, 255);
  131. }
  132.  
  133. int main (int argc, char **argv)
  134. {
  135.   int count;
  136.   char *inputfn, *outputfn, *maskfn;
  137.   char *arg, c;
  138.  
  139.   inputfn = NULL;
  140.   outputfn = NULL;
  141.   maskfn = NULL;
  142.   maskfile = NULL;
  143.  
  144.   cvtICNp = 1;
  145.   cvticsp = 1;
  146.   cvticl4 = 1;
  147.   cvtics4 = 1;
  148.   MacPalette = 0;
  149.   XBMrevbits = 1;
  150.   XBMforg = 15;
  151.   XBMback = XBM_TRANSPARENT;
  152.  
  153.   init_palettes();
  154.  
  155.   /* Options:
  156.      /f: foreground color
  157.      /b: background color
  158.      /d  dump
  159.      /p  Mac Palette
  160.      /2  Output OS/2 v2.0 icon format
  161.      /r  Don't reverse bits in XBM file
  162.      /m  Mask file
  163.      /w  write windows format
  164.      
  165.      /I  Don't include ICN# resources
  166.      /#  Don't include ics# resources
  167.      /l  Don't include icl4 resources
  168.      /s  Don't include ics4 resources
  169.   */
  170.  
  171.   fprintf(stderr, "XIcon  v0.5  Copyright (c) 1993 by Norman Walsh\n");
  172.   if (argc < 2)
  173.     {
  174.       fprintf(stderr, "Usage: xicon <options> <inputfile> <outputfile>\n");
  175.       fprintf(stderr, "Where:\n");
  176.       fprintf(stderr, "  <inputfile>  is an icon file in Mac format (raw resource fork), Windows \n");
  177.       fprintf(stderr, "               format, or X11 XBM format.\n");
  178.       fprintf(stderr, "  <outputfile> is the name of the OS/2 icon file that will be created.\n");
  179.       fprintf(stderr, "  <options>    -2  write an OS/2 v2.0 icon file rather than a v1.2 icon file.\n");
  180.       fprintf(stderr, "               -w  write a Windows icon file rather than an OS/2 icon file.\n");
  181.       fprintf(stderr, "               For XBM files only:\n");
  182.       fprintf(stderr, "                 -f C  set foreground color to 'C'.  C=0..15 or 't' for \n");
  183.       fprintf(stderr, "                       transparent, or 'r' for reverse of background.\n");
  184.       fprintf(stderr, "                 -b C  set background color to 'C'\n");
  185.       fprintf(stderr, "                 -r    don't reverse the bit order of the bytes in the XBM file.\n");
  186.       fprintf(stderr, "                 -m f  Set mask file to 'f'.  The mask file is ANDed with the\n");
  187.       fprintf(stderr, "                       background and the image.  Set the bits in the mask area.\n");
  188.       fprintf(stderr, "\n");
  189.       fprintf(stderr, "               For Mac files only:\n");
  190.       fprintf(stderr, "                 -p    Use Mac palette rather than OS/2 palette.\n");
  191.       fprintf(stderr, "                 -I    Don't include ICN# resources (32x32x1 images)\n");
  192.       fprintf(stderr, "                 -#    Don't include ics# resources (16x16x1 images)\n");
  193.       fprintf(stderr, "                 -l    Don't include icl4 resources (32x32x4 images)\n");
  194.       fprintf(stderr, "                 -s    Don't include ics4 resources (16x16x4 images)\n");
  195.       fprintf(stderr, "\n");
  196.     }
  197.  
  198.   opterr = 0;
  199. #ifdef EMXGCC
  200.   optswchar = "-/";
  201.   optmode = GETOPT_ANY;
  202. #endif /* EMXGCC */
  203.     
  204.   while ((c = getopt(argc, argv, "f:b:m:dpw2rI#ls")) != EOF)
  205.     {
  206.       switch (c)
  207.     {
  208.     case 'f':
  209.       if (strcmp(optarg, "t") == 0 || strcmp(optarg, "T") == 0)
  210.         XBMforg = XBM_TRANSPARENT;
  211.       else
  212.         if (strcmp(optarg, "r") == 0 || strcmp(optarg, "R") == 0)
  213.           XBMforg = XBM_REVERSEVID;
  214.         else
  215.           {
  216.         XBMforg = (atoi(optarg) & 0x0F);
  217.           }
  218.       break;
  219.  
  220.     case 'b':
  221.       if (strcmp(optarg, "t") == 0 || strcmp(optarg, "T") == 0)
  222.         XBMback = XBM_TRANSPARENT;
  223.       else
  224.         if (strcmp(optarg, "r") == 0 || strcmp(optarg, "R") == 0)
  225.           XBMback = XBM_REVERSEVID;
  226.         else
  227.           {
  228.         XBMback = (atoi(optarg) & 0x0F);
  229.           }
  230.       break;
  231.  
  232.     case 'm':
  233.       maskfn = optarg;
  234.       if (access (maskfn, R_OK))
  235.         {
  236.           fprintf(stderr, "Cannot read mask file, mask ignored.\n");
  237.           maskfn = NULL;
  238.         }
  239.       break;
  240.  
  241.     case 'd': /*  dump */
  242.       dump_input = 1;
  243.       break;
  244.  
  245.     case 'p': /*  Mac Palette */
  246.       MacPalette = 1;
  247.       break;
  248.  
  249.     case '2': /*  Output OS/2 v2.0 icon format */
  250.       write_20_format = 1;
  251.       write_win_format = 0;
  252.       break;
  253.  
  254.     case 'w': /* Output Windows format */
  255.       write_win_format = 1;
  256.       write_20_format = 0;
  257.       break;
  258.  
  259.     case 'r': /*  Don't reverse bits in XBM file */
  260.       XBMrevbits = 0;
  261.       break;
  262.       
  263.     case 'I': /*  Don't include ICN# resources */
  264.       cvtICNp = 0;
  265.       break;
  266.       
  267.     case '#': /*  Don't include ics# resources */
  268.       cvticsp = 0;
  269.       break;
  270.  
  271.     case 'l': /*  Don't include icl4 resources */
  272.       cvticl4 = 0;
  273.       break;
  274.  
  275.     case 's': /*  Don't include ics4 resources */
  276.       cvtics4 = 0;
  277.       break;
  278.  
  279.     default:
  280.       fprintf(stderr, "Illegal option ignored.\n");
  281.       break;
  282.     }
  283.     }
  284.  
  285.   for ( ; optind < argc; optind++) 
  286.     {
  287.       if (inputfn == NULL)
  288.     inputfn = argv[optind];
  289.       else
  290.     if (outputfn == NULL)
  291.       outputfn = argv[optind];
  292.     else
  293.       error("Too many filenames.");
  294.     }
  295.  
  296.   if (inputfn == NULL || outputfn == NULL)
  297.     error("You must specify input and output filenames.");
  298.  
  299.   if (access (inputfn, R_OK))
  300.     error("Cannot read input file.");
  301.  
  302.   input = fopen(inputfn, "rb");
  303.   output = fopen(outputfn, "wb");
  304.  
  305.   if (output == NULL)
  306.     error("Cannot create output file.");
  307.  
  308.   if (maskfn)
  309.     maskfile = fopen(maskfn, "rb");
  310.  
  311.   if (maskfile != NULL)
  312.     fprintf(stderr, "Using mask from %s.\n", maskfn);
  313.  
  314.   switch (QueryFileType())
  315.     {
  316.     case MacIcon:
  317.       read_mac_file();
  318.       fprintf(stderr, "Read Macintosh icon file from %s.\n", inputfn);
  319.       break;
  320.     case OS2Icon12:
  321.     case OS2Icon20:
  322.     case WinIcon:
  323.       read_win_file();
  324.       fprintf(stderr, "Read Windows icon file from %s.\n", inputfn);
  325.       break;
  326.     case XBMicon:
  327.       read_xbm_file();
  328.       fprintf(stderr, "Read X11 XBM icon file from %s.\n", inputfn);
  329.       break;
  330.     default:
  331.       error("Can't read that type of file.");
  332.     }
  333.  
  334.   if (write_win_format)
  335.     {
  336.       write_win_file();
  337.       fprintf(stderr,"Wrote Windows icon file on %s.\n", outputfn);
  338.     }
  339.   else
  340.     if (write_20_format)
  341.       {
  342.     write_os2_file20();
  343.     fprintf(stderr,"Wrote OS/2 version 2.0 icon file on %s.\n", outputfn);
  344.       }
  345.     else
  346.       {
  347.     write_os2_file12();
  348.     fprintf(stderr,"Wrote OS/2 version 1.2 icon file on %s.\n", outputfn);
  349.       }
  350.  
  351.   fclose(input);
  352.   fclose(output);
  353.  
  354.   if (maskfile)
  355.     fclose(maskfile);
  356. }
  357.  
  358.  
  359.  
  360.