[geeks] fti to svg conversion

Joshua D Boyd jdboyd at cs.millersville.edu
Tue Oct 29 13:16:40 CST 2002


On Tue, Oct 29, 2002 at 01:50:16PM -0500, Joshua D Boyd wrote:
> If I find more info, I'll post it. 

I've pasted the entire FLTK FTI loading code after the .sig.  When you
were talking about negative colors, I'd assumed you were loading a
binary file, but now I realize that .ftis are ascii.

And, wow, these colors are strange.  I see what FLTK does to convert the
.fti color to it's own internal icon color format, but that too is
strange.  I see that negative colors mean that the colors is a composite
of two colors.  Assuming that c is an int gotten by an atoi of the
negative number read in from the .fti, then the one color is gotten by
(c >> 4), and the other is gotten by (c & 15).  You blend the two
together evenly to get the final color.  Now, how you turn these into
RGB values, I don't know.

Lets see, when drawing the icon, fltk calls the fl_color function with
the int gotten from the file.

Crikey.  An int.  That is 32 bits, which is plenty of space to store 8bit
RGB.  Find some positive colors, and try converting the int to a
unsigned char[4], and see if any of the values in the char array make
sense.

So, if you see a negative number use the above stuff to seperate it to
two colors.  

For a regular color (of which you have two after processing negatives),
FLTK says:
   FLTK manages colors as 32-bit unsigned integers. Values from 0 to 255 
   represent colors from the FLTK 1.0.x standard colormap and are
   allocated as needed on screens without TrueColor support. The
   Fl_Color enumeration type defines the standard colors and color cube
   for the first 256 colors. All of these are named with symbols in
   <FL/Enumerations.H>. 

   Color values greater than 255 are treated as 24-bit RGB values. These 
   are mapped to the closest color supported by the screen, either from
   one of the 256 colors in the FLTK 1.0.x colormap or a direct RGB
   value on TrueColor screens. You can generate 24-bit RGB color values
   using the fl_rgb_color() function.

And it appears that this is the same as .ftis, which makes sense since
FLTK was initially SGI only, so why not reuse SGI concepts.

I hope something in here works for you.

-- 
Joshua D. Boyd

int		     // O - 0 on success, non-zero on error
Fl_File_Icon::load_fti(const char *fti)	  // I - File to read from
{
  FILE	*fp;			// File pointer
  int	ch;				// Current character
  char	command[255],			   // Command string ("vertex",
etc.)
	params[255],		// Parameter string ("10.0,20.0", etc.)
	*ptr;				     // Pointer into strings
  int	outline;				// Outline polygon


  // Try to open the file...
  if ((fp = fopen(fti, "rb")) == NULL)
  {
    Fl::error("Fl_File_Icon::load_fti(): Unable to open \"%s\" - %s",
              fti, strerror(errno));
    return -1;
  }

  // Read the entire file, adding data as needed...
  outline = 0;

  while ((ch = getc(fp)) != EOF)
  {
    // Skip whitespace
    if (isspace(ch))
      continue;

    // Skip comments starting with "#"...
    if (ch == '#')
    {
      while ((ch = getc(fp)) != EOF)
        if (ch == '\n')
	  break;

      if (ch == EOF)
        break;
      else
        continue;
    }

    // OK, this character better be a letter...
    if (!isalpha(ch))
    {
      Fl::error("Fl_File_Icon::load_fti(): Expected a letter at file
    position %ld (saw '%c')",
                ftell(fp) - 1, ch);
      break;
    }

    // Scan the command name...
    ptr    = command;
    *ptr++ = ch;

    while ((ch = getc(fp)) != EOF)
    {
      if (ch == '(')
        break;
      else if (ptr < (command + sizeof(command) - 1))
        *ptr++ = ch;
    }

    *ptr++ = '\0';

    // Make sure we stopped on a parenthesis...
    if (ch != '(')
    {
      Fl::error("Fl_File_Icon::load_fti(): Expected a ( at file position
    %ld (saw '%c')",
                ftell(fp) - 1, ch);
      break;
    }

    // Scan the parameters...
    ptr = params;

    while ((ch = getc(fp)) != EOF)
    {
      if (ch == ')')
        break;
      else if (ptr < (params + sizeof(params) - 1))
        *ptr++ = ch;
    }

    *ptr++ = '\0';

    // Make sure we stopped on a parenthesis...
    if (ch != ')')
    {
      Fl::error("Fl_File_Icon::load_fti(): Expected a ) at file position
    %ld (saw '%c')",
                ftell(fp) - 1, ch);
      break;
    }

    // Make sure the next character is a semicolon...
    if ((ch = getc(fp)) != ';')
    {
      Fl::error("Fl_File_Icon::load_fti(): Expected a ; at file position
    %ld (saw '%c')",
                ftell(fp) - 1, ch);
      break;
    }

    // Now process the command...
    if (strcmp(command, "color") == 0)
    {
      // Set the color; for negative colors blend the two primaries to
      // produce a composite color.  Also, the following symbolic color
      // names are understood:
      //
      //     name           FLTK color
      //     -------------  ----------
      //     iconcolor      FL_ICON_COLOR; mapped to the icon color in
      //                    Fl_File_Icon::draw()
      //     shadowcolor    FL_DARK3
      //     outlinecolor   FL_BLACK
      if (strcmp(params, "iconcolor") == 0)
        add_color(FL_ICON_COLOR);
      else if (strcmp(params, "shadowcolor") == 0)
        add_color(FL_DARK3);
      else if (strcmp(params, "outlinecolor") == 0)
        add_color(FL_BLACK);
      else
      {
        int c = atoi(params);	// Color value


        if (c < 0)
	{
	  // Composite color; compute average...
	    c = -c;
	      add_color(fl_color_average((Fl_Color)(c >> 4),
	                                   (Fl_Color)(c & 15), 0.5));
					   }
					   else
					     add_color((Fl_Color)c);
      }
    }
    else if (strcmp(command, "bgnline") == 0)
      add(LINE);
    else if (strcmp(command, "bgnclosedline") == 0)
      add(CLOSEDLINE);
    else if (strcmp(command, "bgnpolygon") == 0)
      add(POLYGON);
    else if (strcmp(command, "bgnoutlinepolygon") == 0)
    {
      add(OUTLINEPOLYGON);
      outline = add(0) - data_;
      add(0);
    }
    else if (strcmp(command, "endoutlinepolygon") == 0 && outline)
    {
      unsigned cval; // Color value

      // Set the outline color; see above for valid values...
      if (strcmp(params, "iconcolor") == 0)
        cval = FL_ICON_COLOR;
      else if (strcmp(params, "shadowcolor") == 0)
        cval = FL_DARK3;
      else if (strcmp(params, "outlinecolor") == 0)
        cval = FL_BLACK;
      else
      {
        int c = atoi(params);	// Color value


        if (c < 0)
	{
	  // Composite color; compute average...
	    c = -c;
	      cval = fl_color_average((Fl_Color)(c >> 4), (Fl_Color)(c &
	15), 0.5);
	}
	else
	  cval = c;
      }

      // Store outline color...
      data_[outline]     = cval >> 16;
      data_[outline + 1] = cval;

      outline = 0;
      add(END);
    }
    else if (strncmp(command, "end", 3) == 0)
      add(END);
    else if (strcmp(command, "vertex") == 0)
    {
      float x, y;		// Coordinates of vertex


      if (sscanf(params, "%f,%f", &x, &y) != 2)
        break;

      add_vertex((short)(int)rint(x * 100.0), (short)(int)rint(y *
      100.0));
    }
    else
    {
      Fl::error("Fl_File_Icon::load_fti(): Unknown command \"%s\" at
      file position %ld.",
                command, ftell(fp) - 1);
      break;
    }
  }

  // Close the file and return...
  fclose(fp);

#ifdef DEBUG
  printf("Icon File \"%s\":\n", fti);
  for (int i = 0; i < num_data_; i ++)
    printf("    %d,\n", data_[i]);
#endif /* DEBUG */

  return 0;
}



More information about the geeks mailing list