home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / graphic / xicon / src / xpm3writ.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-05  |  6.0 KB  |  245 lines

  1. /* This file is xpm3write.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.  
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include "icondata.h"
  23. #include "iconvars.h"
  24. #include "iconio.h"
  25. #include "xpm3write.h"
  26.  
  27. rgb_color *color_db = NULL;
  28.  
  29. void write_xpm_map (bitmap_info map)
  30. {
  31.   BYTE *rowptr, *ch;
  32.   int  padbytes, count;
  33.  
  34.   count = map.bytewidth;
  35.   padbytes = 0;
  36.   while (count % 4 != 0)
  37.     {
  38.       count++;
  39.       padbytes++;
  40.     }
  41.  
  42.   rowptr = map.map + map.size - map.bytewidth;
  43.   while (rowptr >= map.map)
  44.     {
  45.       for (count = 0, ch = rowptr; count < map.bytewidth; count++, ch++) 
  46.     {
  47.       char hi, lo;
  48.       hi = (*ch) >> 4;
  49.       lo = (*ch) & 0x0F;
  50.       printf ("%c%c", hi+'a', lo+'a');
  51.     }
  52.       printf ("\n");
  53. /*
  54.       for (count = 0; count < padbytes; count++)
  55.     write_char(0);
  56. */      
  57.       rowptr -= map.bytewidth;
  58.     }
  59. }
  60.  
  61. void dump_bitmap(bitmap_info map) 
  62. {
  63.   int row,col,foo;
  64.  
  65.   printf ("Dumping %dx%d bitmap (%d bits/pixel, %d bytes)\n",
  66.       map.width, map.height, map.bits_per_pixel, map.size);
  67.  
  68.   for (row = 1; row <= map.height; row++) 
  69.     {
  70.       for (col = 1; col <= map.width; col++)
  71.     {
  72.       foo = get_bit(map,row,col);
  73.       printf("%c", foo+'a');
  74.     }
  75.       printf ("\n");
  76.     }
  77. }
  78.  
  79. char *find_color(rgb_info rgb)
  80. {
  81.   int deltared, deltagreen, deltablue;
  82.   int smallred, smallgreen, smallblue;
  83.   rgb_color *c = color_db;
  84.   char *name = NULL;
  85.  
  86.   smallred   = 1024;
  87.   smallgreen = 1024;
  88.   smallblue  = 1024;
  89.  
  90.   for (c = color_db; c->name; c++) {
  91.     deltared   = abs(c->red - rgb.red);
  92.     deltagreen = abs(c->green - rgb.green);
  93.     deltablue  = abs(c->blue - rgb.blue);
  94.  
  95.     if ((deltared <= smallred)
  96.     && (deltagreen <= smallgreen)
  97.     && (deltablue <= smallblue))
  98.       {
  99.     smallred = deltared;
  100.     smallgreen = deltagreen;
  101.     smallblue = deltablue;
  102.     name = c->name;
  103.       }
  104.       
  105.     if (c->red == rgb.red
  106.     && c->green == rgb.green
  107.     && c->blue == rgb.blue) {
  108.       return c->name;
  109.     }
  110.   }
  111.  
  112.   return name;
  113. }
  114.  
  115. void write_xpm3_file(int count)
  116. {
  117.   int rgbcount;
  118.   int row,col,foo;
  119.   rgb_info *rgb;
  120.  
  121.   if (color_db == NULL) {
  122.     fprintf (stderr, "Reading colors database %s...\n", color_db_file);
  123.     color_db = read_rgb_txt (color_db_file);
  124.     if (!color_db) {
  125.       fprintf (stderr, "Cannot read colors database: %s\n", color_db_file);
  126.       exit(1);
  127.     }
  128.   }
  129.  
  130. #ifdef DUMP_BITMAPS
  131.   printf("xor_map:\n");
  132.   dump_bitmap(generic[count].xor_map);
  133.  
  134.   printf("and_map:\n");
  135.   dump_bitmap(generic[count].and_map);
  136.  
  137.   printf("clr_map:\n");
  138.   dump_bitmap(generic[count].clr_map);
  139. #endif /* DUMP_BITMAPS */
  140.  
  141.   fprintf (output, "/* XPM */\n");
  142.   fprintf (output, "static char * image[] = {\n");
  143.   fprintf (output, "\"%d %d %d 1\",\n",
  144.        generic[count].clr_map.width,
  145.        generic[count].clr_map.height,
  146.        generic[count].clr_map.num_colors+1);
  147.   
  148.   fprintf (output, "\"  c none\",\n");
  149.   rgb = generic[count].clr_map.palette;
  150.   for (rgbcount = 0; rgbcount < generic[count].clr_map.num_colors; rgbcount++)
  151.     {
  152.       fprintf (output, "\"%c c %s\",\n", rgbcount+'a', find_color(*rgb));
  153.       rgb++;
  154.     }
  155.  
  156.   for (row = 1; row <= generic[count].clr_map.height; row++) 
  157.     {
  158.       if (row > 1) fprintf (output, ",\n");
  159.       fprintf (output, "\"");
  160.       for (col = 1; col <= generic[count].clr_map.width; col++)
  161.     {
  162.       if ((get_bit(generic[count].xor_map,row,col)
  163.           != get_bit(generic[count].and_map,row,col))
  164.            || (get_bit(generic[count].xor_map,row,col)
  165.            && get_bit(generic[count].and_map,row,col)))
  166.         foo = 32;
  167.       else
  168.         foo = get_bit(generic[count].clr_map,row,col) + 'a';
  169.  
  170.       fprintf(output, "%c", foo);
  171.     }
  172.       fprintf (output, "\"");
  173.     }
  174.   fprintf (output, "};\n");
  175. }
  176.  
  177. rgb_color *read_rgb_txt(char *fn)
  178. {
  179.   rgb_color *colors, *temp_colors;
  180.   int i, cur_color, num_colors;
  181.   FILE *text;
  182.   char line[1024], *name;
  183.   char *red, *green, *blue;
  184.   char *ch;
  185.  
  186.   text = fopen(fn, "r");
  187.   if (text == (FILE *) NULL) {
  188.     return NULL;
  189.   }
  190.  
  191.   cur_color = 0;
  192.   num_colors = 500;
  193.   colors = (rgb_color *) malloc(num_colors * sizeof(rgb_color));
  194.  
  195.   while (fgets(line, 1023, text)) 
  196.     {
  197.       ch = line;
  198.  
  199.       while (*ch && isspace(*ch)) ch++;
  200.       red = ch;
  201.       while (*ch && isdigit(*ch)) ch++;
  202.       *ch = 0;
  203.       ch++;
  204.  
  205.       while (*ch && isspace(*ch)) ch++;
  206.       green = ch;
  207.       while (*ch && isdigit(*ch)) ch++;
  208.       *ch = 0;
  209.       ch++;
  210.  
  211.       while (*ch && isspace(*ch)) ch++;
  212.       blue = ch;
  213.       while (*ch && isdigit(*ch)) ch++;
  214.       *ch = 0;
  215.       ch++;
  216.  
  217.       while (*ch && isspace(*ch)) ch++;
  218.       name = (char *) malloc(strlen(ch) + 1);
  219.       strcpy(name, ch);
  220.  
  221.       name[strlen(name)-1] = 0; /* get rid of the newline */
  222.  
  223.       sscanf(red, "%d", &i);   colors[cur_color].red   = i;
  224.       sscanf(green, "%d", &i); colors[cur_color].green = i;
  225.       sscanf(blue, "%d", &i);  colors[cur_color].blue   = i;
  226.       colors[cur_color].name = name;
  227.  
  228.       cur_color++;
  229.       if (cur_color >= num_colors) { 
  230.     temp_colors = colors;
  231.     colors = (rgb_color *) malloc(num_colors * 2 * sizeof(rgb_color));
  232.     for (i = 0; i < num_colors; i++) {
  233.       colors[i] = temp_colors[i];
  234.     }
  235.     num_colors *= 2;
  236.     free (temp_colors);
  237.       }
  238.     }
  239.   fclose(text);
  240.  
  241.   colors[cur_color].name = NULL;
  242.  
  243.   return colors;
  244. }
  245.