home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / graphic / xicon / src / xicon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-04  |  11.3 KB  |  394 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 <norm@ora.com>
  20.  * or via paper mail to:
  21.  *  
  22.  *  Norman Walsh
  23.  *  32 Spencer Rd, Apt 32M
  24.  *  Boxborough, MA 01719
  25.  ************************************************************************/
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #ifdef __EMX__
  31. #include <getopt.h>
  32. #endif /* __EMX__ */
  33. #include "macread.h"
  34. #include "winread.h"
  35. #include "bitmap.h"
  36. #include "icondata.h"
  37. #include "os2read.h"
  38. #include "os2write.h"
  39. #include "xbmread.h"
  40. #include "palette.h"
  41. #include "xbmdata.h"
  42. #include "winwrite.h"
  43. #include "xpm3write.h"
  44.  
  45. /* Optional arguments */
  46.  
  47. #ifndef __EMX__
  48. extern char *optarg;
  49. extern int optind, opterr;
  50. #endif /* not __EMX__ */
  51.  
  52. int generic_count = 0;
  53. generic_info *generic = NULL;
  54. int dump_input = 0;
  55.  
  56. #define FORMAT_OS212 0
  57. #define FORMAT_OS220 1
  58. #define FORMAT_WIN   2
  59. #define FORMAT_XPM3  3
  60.  
  61. int write_format = FORMAT_OS212;
  62. int write_all    = 0;
  63.  
  64. rgb_info *pc_palette = NULL;
  65. rgb_info *mac_palette = NULL;
  66. rgb_info *bw_palette = NULL;
  67.  
  68. char *color_db_file = "/usr/local/lib/X11/rgb.txt";
  69.  
  70. extern int cvtICNp;
  71. extern int cvticsp;
  72. extern int cvticl4;
  73. extern int cvtics4;
  74. extern int MacPalette;
  75.  
  76. extern int XBMrevbits;
  77. extern int XBMforg;
  78. extern int XBMback;
  79.  
  80. FILE *input;
  81. FILE *output;
  82. FILE *maskfile;
  83. int rc;
  84.  
  85. void error (char *msg)
  86. {
  87.   printf("%s\n", msg);
  88.   exit(1);
  89. }
  90.  
  91. void init_palettes()
  92. {
  93.   pc_palette = (rgb_info *) malloc (sizeof (rgb_info) * 16);
  94.   mac_palette = (rgb_info *) malloc (sizeof (rgb_info) * 16);
  95.   bw_palette = (rgb_info *) malloc (sizeof (rgb_info) * 2);
  96.  
  97.   pc_palette[ 0] = palette_color (255, 255, 255);
  98.   pc_palette[ 1] = palette_color (128,   0,   0);
  99.   pc_palette[ 2] = palette_color (  0, 128,   0);
  100.   pc_palette[ 3] = palette_color (128, 128,   0);
  101.   pc_palette[ 4] = palette_color (  0,   0, 128);
  102.   pc_palette[ 5] = palette_color (128,   0, 128);
  103.   pc_palette[ 6] = palette_color (  0, 128, 128);
  104.   pc_palette[ 7] = palette_color (128, 128, 128);
  105.   pc_palette[ 8] = palette_color (204, 204, 204);
  106.   pc_palette[ 9] = palette_color (255,   0,   0);
  107.   pc_palette[10] = palette_color (  0, 255,   0);
  108.   pc_palette[11] = palette_color (255, 255,   0);
  109.   pc_palette[12] = palette_color (  0,   0, 255);
  110.   pc_palette[13] = palette_color (255,   0, 255);
  111.   pc_palette[14] = palette_color (  0, 255, 255);
  112.   pc_palette[15] = palette_color (  0,   0,   0);
  113.   
  114.   mac_palette[ 0] = palette_color (255, 255, 255);
  115.   mac_palette[ 1] = palette_color (  0, 255, 255);
  116.   mac_palette[ 2] = palette_color ( 16, 128, 255);
  117.   mac_palette[ 3] = palette_color (  0,   0, 255);
  118.   mac_palette[ 4] = palette_color (128,   0, 255);
  119.   mac_palette[ 5] = palette_color (177,  17,  20);
  120.   mac_palette[ 6] = palette_color (212,   0,   0);
  121.   mac_palette[ 7] = palette_color (194, 194,   0);
  122.   mac_palette[ 8] = palette_color (  0, 255,   0);
  123.   mac_palette[ 9] = palette_color (  0, 162,   0);
  124.   mac_palette[10] = palette_color ( 75,  97, 193);
  125.   mac_palette[11] = palette_color ( 25, 159, 192);
  126.   mac_palette[12] = palette_color (224, 224, 224);
  127.   mac_palette[13] = palette_color (161, 161, 161);
  128.   mac_palette[14] = palette_color ( 82,  82,  82);
  129.   mac_palette[15] = palette_color (  0,   0,   0);
  130.   
  131.   bw_palette[0] = palette_color(  0,   0,    0);
  132.   bw_palette[1] = palette_color(255, 255, 255);
  133. }
  134.  
  135. int main (int argc, char **argv)
  136. {
  137.   int count;
  138.   char *inputfn, *outputfn, *maskfn;
  139.   char *arg, c;
  140.  
  141.   inputfn = NULL;
  142.   outputfn = NULL;
  143.   maskfn = NULL;
  144.   maskfile = NULL;
  145.  
  146.   cvtICNp = 1;
  147.   cvticsp = 1;
  148.   cvticl4 = 1;
  149.   cvtics4 = 1;
  150.   MacPalette = 0;
  151.   XBMrevbits = 1;
  152.   XBMforg = 15;
  153.   XBMback = XBM_TRANSPARENT;
  154.  
  155.   init_palettes();
  156.  
  157.   fprintf(stderr, "XIcon  v1.0  Copyright (c) 1993,94 by Norman Walsh\n");
  158.   if (argc < 2)
  159.     {
  160.       fprintf(stderr, "Usage: xicon <options> <inputfile> <outputfile>\n");
  161.       fprintf(stderr, "Where:\n");
  162.       fprintf(stderr, "  <inputfile>  is an icon file in Mac format (raw resource fork), Windows \n");
  163.       fprintf(stderr, "               format, or X11 XBM format.\n");
  164.       fprintf(stderr, "  <outputfile> is the name of the OS/2 icon file that will be created.\n");
  165.       fprintf(stderr, "  <options>    -2  write an OS/2 v2.0 icon file rather than a v1.2 icon file.\n");
  166.       fprintf(stderr, "               -w  write a Windows icon file rather than an OS/2 icon file.\n");
  167.       fprintf(stderr, "               -x  write an XPM3 icon file rather than an OS/2 icon file.\n");
  168.       fprintf(stderr, "\n");
  169.       fprintf(stderr, "               For XPM files only:\n");
  170.       fprintf(stderr, "                 -a    write all the icons rather than just the first.\n");
  171.       fprintf(stderr, "                 -R f  Use 'f' instead of standard rgb.txt file.\n");
  172.       fprintf(stderr, "\n");
  173.       fprintf(stderr, "               For XBM files only:\n");
  174.       fprintf(stderr, "                 -f C  set foreground color to 'C'.  C=0..15 or 't' for \n");
  175.       fprintf(stderr, "                       transparent, or 'r' for reverse of background.\n");
  176.       fprintf(stderr, "                 -b C  set background color to 'C'\n");
  177.       fprintf(stderr, "                 -r    don't reverse the bit order of the bytes in the file.\n");
  178.       fprintf(stderr, "                 -m f  Set mask file to 'f'.  The mask file is ANDed with the\n");
  179.       fprintf(stderr, "                       background and the image.  Set the bits in the mask area.\n");
  180.       fprintf(stderr, "\n");
  181.       fprintf(stderr, "               For Mac files only:\n");
  182.       fprintf(stderr, "                 -p    Use Mac palette rather than OS/2 palette.\n");
  183.       fprintf(stderr, "                 -I    Don't include ICN# resources (32x32x1 images)\n");
  184.       fprintf(stderr, "                 -#    Don't include ics# resources (16x16x1 images)\n");
  185.       fprintf(stderr, "                 -l    Don't include icl4 resources (32x32x4 images)\n");
  186.       fprintf(stderr, "                 -s    Don't include ics4 resources (16x16x4 images)\n");
  187.       fprintf(stderr, "\n");
  188.     }
  189.  
  190.   opterr = 0;
  191. #ifdef __EMX__
  192.   optswchar = "-/";
  193.   optmode = GETOPT_ANY;
  194. #endif /* __EMX__ */
  195.     
  196.   while ((c = getopt(argc, argv, "f:b:m:R:dpwx2rI#lsa")) != EOF)
  197.     {
  198.       switch (c)
  199.     {
  200.     case 'f':
  201.       if (strcmp(optarg, "t") == 0 || strcmp(optarg, "T") == 0)
  202.         XBMforg = XBM_TRANSPARENT;
  203.       else
  204.         if (strcmp(optarg, "r") == 0 || strcmp(optarg, "R") == 0)
  205.           XBMforg = XBM_REVERSEVID;
  206.         else
  207.           {
  208.         XBMforg = (atoi(optarg) & 0x0F);
  209.           }
  210.       break;
  211.  
  212.     case 'b':
  213.       if (strcmp(optarg, "t") == 0 || strcmp(optarg, "T") == 0)
  214.         XBMback = XBM_TRANSPARENT;
  215.       else
  216.         if (strcmp(optarg, "r") == 0 || strcmp(optarg, "R") == 0)
  217.           XBMback = XBM_REVERSEVID;
  218.         else
  219.           {
  220.         XBMback = (atoi(optarg) & 0x0F);
  221.           }
  222.       break;
  223.  
  224.     case 'm':
  225.       maskfn = optarg;
  226.       if (access (maskfn, R_OK))
  227.         {
  228.           fprintf(stderr, "Cannot read mask file, mask ignored.\n");
  229.           maskfn = NULL;
  230.         }
  231.       break;
  232.  
  233.     case 'R':
  234.       color_db_file = optarg;
  235.       if (access (color_db_file, R_OK))
  236.         {
  237.           fprintf(stderr, "Cannot read color database file...\n");
  238.           exit(1);
  239.         }
  240.       break;
  241.  
  242.     case 'd': /*  dump */
  243.       dump_input = 1;
  244.       break;
  245.  
  246.     case 'p': /*  Mac Palette */
  247.       MacPalette = 1;
  248.       break;
  249.  
  250.     case '2': /*  Output OS/2 v2.0 icon format */
  251.       write_format = FORMAT_OS220;
  252.       break;
  253.  
  254.     case 'w': /* Output Windows format */
  255.       write_format = FORMAT_WIN;
  256.       break;
  257.  
  258.     case 'x': /* Output XPM3 format */
  259.       write_format = FORMAT_XPM3;
  260.       break;
  261.  
  262.     case 'r': /*  Don't reverse bits in XBM file */
  263.       XBMrevbits = 0;
  264.       break;
  265.       
  266.     case 'I': /*  Don't include ICN# resources */
  267.       cvtICNp = 0;
  268.       break;
  269.       
  270.     case '#': /*  Don't include ics# resources */
  271.       cvticsp = 0;
  272.       break;
  273.  
  274.     case 'l': /*  Don't include icl4 resources */
  275.       cvticl4 = 0;
  276.       break;
  277.  
  278.     case 's': /*  Don't include ics4 resources */
  279.       cvtics4 = 0;
  280.       break;
  281.  
  282.     case 'a': /*  Write all the icons */
  283.       write_all = 1;
  284.       break;
  285.  
  286.     default:
  287.       fprintf(stderr, "Illegal option ignored.\n");
  288.       break;
  289.     }
  290.     }
  291.  
  292.   for ( ; optind < argc; optind++) 
  293.     {
  294.       if (inputfn == NULL)
  295.     inputfn = argv[optind];
  296.       else
  297.     if (outputfn == NULL)
  298.       outputfn = argv[optind];
  299.     else
  300.       error("Too many filenames.");
  301.     }
  302.  
  303.   if (inputfn == NULL || outputfn == NULL)
  304.     error("You must specify input and output filenames.");
  305.  
  306.   if (access (inputfn, R_OK))
  307.     error("Cannot read input file.");
  308.  
  309.   input = fopen(inputfn, "rb");
  310.   
  311.   if (write_format != FORMAT_XPM3 || ! write_all) {
  312.     output = fopen(outputfn, "wb");
  313.  
  314.     if (output == NULL)
  315.       error("Cannot create output file.");
  316.   }
  317.  
  318.   if (maskfn)
  319.     maskfile = fopen(maskfn, "rb");
  320.  
  321.   if (maskfile != NULL)
  322.     fprintf(stderr, "Using mask from %s.\n", maskfn);
  323.  
  324.   switch (QueryFileType())
  325.     {
  326.     case MacIcon:
  327.       fprintf(stderr, "Reading Macintosh icon file from %s.\n", inputfn);
  328.       read_mac_file();
  329.       break;
  330.     case OS2Icon12:
  331.     case OS2Icon20:
  332.       fprintf(stderr, "Reading OS/2 icon file from %s.\n", inputfn);
  333.       read_os2_icon_file();
  334.       break;
  335.     case WinIcon:
  336.       fprintf(stderr, "Reading Windows icon file from %s.\n", inputfn);
  337.       read_win_file();
  338.       break;
  339.     case XBMicon:
  340.       fprintf(stderr, "Reading X11 XBM icon file from %s.\n", inputfn);
  341.       read_xbm_file();
  342.       break;
  343.     default:
  344.       error("Can't read that type of file.");
  345.     }
  346.  
  347.   fprintf(stderr, "%d icons in file.\n", generic_count);
  348.  
  349.   switch (write_format) {
  350.   case FORMAT_OS220:
  351.     write_os2_file20();
  352.     fprintf(stderr,"Wrote OS/2 version 2.0 icon file on %s.\n", outputfn);
  353.     break;
  354.   case FORMAT_WIN:
  355.     write_win_file();
  356.     fprintf(stderr,"Wrote Windows icon file on %s.\n", outputfn);
  357.     break;
  358.   case FORMAT_XPM3:
  359.     if (write_all) {
  360.       char fn[1024];
  361.       int count = 1;
  362.  
  363.       for (count = 0; count < generic_count; count++) {
  364.     sprintf(fn, "%s%d.xpm", outputfn, count+1);
  365.     output = fopen(fn, "wb");
  366.     write_xpm3_file(count);
  367.     fclose(output);
  368.     fprintf(stderr,"Wrote XPM3 icon file on %s.\n", fn);
  369.       }
  370.     } else {    
  371.       write_xpm3_file(0);
  372.       fprintf(stderr,"Wrote XPM3 icon file on %s.\n", outputfn);
  373.     }
  374.  
  375.     break;
  376.   case FORMAT_OS212:
  377.   default:
  378.     write_os2_file12();
  379.     fprintf(stderr,"Wrote OS/2 version 1.2 icon file on %s.\n", outputfn);
  380.     break;
  381.   }
  382.  
  383.   fclose(input);
  384.  
  385.   if (write_format != FORMAT_XPM3 || ! write_all)
  386.     fclose(output);
  387.  
  388.   if (maskfile)
  389.     fclose(maskfile);
  390. }
  391.  
  392.  
  393.  
  394.