home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / xpm / converters / xpm1to3.pl < prev    next >
Perl Script  |  2000-07-29  |  3KB  |  91 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # Usage: xpm1to3.pl xpmv1-file > xpmv3-file
  4. #
  5. # Note:  perl (available by ftp on prep.ai.mit.edu) script to convert 
  6. #        "enhanced" xpm v1 X11 pixmap files to xpm v3 (C includable format)
  7. #        pixmap files...
  8. # +---------------------------------------------------------------------------
  9. # WHO:    Richard Hess                    CORP:   Consilium
  10. # TITLE:  Staff Engineer                  VOICE:  [415] 691-6342
  11. #     [ X-SWAT Team:  Special Projects ]  USNAIL: 640 Clyde Court
  12. # UUCP:   ...!uunet!cimshop!rhess                 Mountain View, CA 94043
  13. # +---------------------------------------------------------------------------
  14.  
  15. sub checkname {
  16.     if ($_[0] ne $_[1]) {
  17.     printf STDERR "warning, name inconsitencies in %s %s!=%s\n", 
  18.                       $_[2], $_[0], $_[1];
  19.     }
  20. }
  21.  
  22. sub checkmono {
  23.     if ($_[0] ne $_[1]) { return 0; }
  24.     return 1;
  25. }
  26.  
  27. printf "/* XPM */\n";
  28. ($name, $format) = (<> =~ /^#define\s+(\w+)_format\s+(\d+)\s*$/);
  29. ($name2, $width) = (<> =~ /^#define\s+(\w+)_width\s+(\d+)\s*$/);
  30. &checkname($name, $name2, "width");
  31. ($name2, $height) = (<> =~ /^#define\s+(\w+)_height\s+(\d+)\s*$/);
  32. &checkname($name, $name2, "height");
  33. ($name2, $ncolors) = (<> =~ /^#define\s+(\w+)_ncolors\s+(\d+)\s*$/);
  34. &checkname($name, $name2, "ncolors");
  35. ($name2, $chars_per_pixel) = (<> =~
  36. /^#define\s+(\w+)_chars_per_pixel\s+(\d+)\s*$/);
  37. &checkname($name, $name2, "chars per pixel");
  38.  
  39. ($name2) = (<> =~ /^static char \*\s*(\w+)_mono\[]\s+=\s+{\s*$/);
  40. $mono = &checkmono($name, $name2);
  41.  
  42. if ($mono) {
  43.   $idx = 0;
  44.   while ( ($_ = <>) =~ m/^\s*"[^"]+"\s*,\s*"[^"]+"(,)?\s*$/ ) {
  45.     ($codes[$idx], $mono_name[$idx]) = /^\s*"([^"]+)"\s*,\s*"([^"]+)"(,)?\s*$/;
  46.     $idx++;
  47.   }
  48.   if ($idx != $ncolors) {
  49.     printf STDERR "Warning, ncolors mismatch reading mono %d != %d\n",
  50. $ncolors, $idx;
  51.   }
  52.  
  53.   ($name2) = (<> =~ /^static char \*\s*(\w+)_colors\[]\s+=\s+{\s*$/);
  54.   &checkname($name, $name2, "colors");
  55. }
  56.  
  57. printf "static char * %s[] = {\n", $name;
  58. printf "/* %s pixmap\n * width height ncolors chars_per_pixel */\n", $name;
  59. printf "\"%s %s %s %s \",\n", $width, $height, $ncolors, $chars_per_pixel;
  60.  
  61. $idx = 0;
  62. while ( ($_ = <>) =~ m/^\s*"[^"]+"\s*,\s*"[^"]+"(,)?\s*$/ ) {
  63.   ($codes[$idx], $color_name[$idx]) = /^\s*"([^"]+)"\s*,\s*"([^"]+)"(,)?\s*$/;
  64.   $idx++;
  65. }
  66. if ($idx != $ncolors) {
  67.   printf STDERR "Warning, ncolors mismatch reading color %d != %d\n",
  68. $ncolors, $idx;
  69. }
  70.  
  71. for ($idx=0; $idx<$ncolors; $idx++) {
  72.   if ($mono) {
  73.     printf "\"%s m %s c %s \t s c%d \",\n", $codes[$idx],
  74. $mono_name[$idx], $color_name[$idx], $idx;
  75.   }
  76.   else {
  77.     printf "\"%s c %s \t s c%d \",\n", $codes[$idx], $color_name[$idx], $idx;
  78.   }
  79. }
  80.  
  81. ($name2) = ( <> =~ /^static char \*\s*(\w+)_pixels\[]\s+=\s+{\s*$/);
  82. &checkname($name, $name2, "pixels");
  83.  
  84. printf "/* pixels */\n";
  85. while ( ! ( ($_ = <>) =~ /^}\s*;\s*$/) ) {
  86.     printf "%s", $_;
  87. }
  88. printf "} ;\n";
  89.  
  90. # -----------------------------------------------------------------------<eof>
  91.