6644001999-04-06Per Hedbor inherit Image._XPM; #if 0 #define TE( X ) write("XPM profile: %-20s ... ", (X));
0410041999-04-09Per Hedbor int old_time,start_time; #define TI(X) start_time=old_time=gethrtime();TE(X)
6644001999-04-06Per Hedbor #define TD( X ) do{\
0410041999-04-09Per Hedbor  write("%3.02f (%3.02f)\n", (gethrtime()-old_time)/1000000.0,(gethrtime()-start_time)/1000000.0);\
6644001999-04-06Per Hedbor  TE(X); \ old_time = gethrtime(); \ } while(0); #else
0410041999-04-09Per Hedbor #define TI(X)
6644001999-04-06Per Hedbor #define TD(X) #define TE(X) #endif
b21c051999-04-06Per Hedbor  mapping _decode( string what, void|mapping opts ) {
6644001999-04-06Per Hedbor  array e;
b21c051999-04-06Per Hedbor  array data; mapping retopts = ([ ]); if(!opts) opts = ([]);
0410041999-04-09Per Hedbor  TI("Scan for header");
6644001999-04-06Per Hedbor  if(sscanf(what, "%*s/*%*[ \t]XPM%*[ \t]*/%*s{%s", what) != 5)
b21c051999-04-06Per Hedbor  error("This is not a XPM image (1)\n");
6644001999-04-06Per Hedbor  if(strlen(what)<100000) { TD("Extra scan for small images"); sscanf(what, "%s\n/* XPM */", what ) || sscanf(what, "%s\n/*\tXPM\t*/", what ) || sscanf(what, "%s\n/*\tXPM */", what )|| sscanf(what, "%s\n/* XPM */", what ); /* This is just a sanity check... */ what = reverse(what); if(sscanf(what, "%*s}%s", what) != 2) error("This is not a XPM image (3)\n"); what = reverse(what); }
b21c051999-04-06Per Hedbor 
6644001999-04-06Per Hedbor  TD("Explode"); data = what/"\n"; what = 0; int len = sizeof(data); int r, sp; TD("Trim");
0410041999-04-09Per Hedbor  _xpm_trim_rows( data );
b21c051999-04-06Per Hedbor  array values = (array(int))(replace(data[0], "\t", " ")/" "-({""})); int width = values[0]; int height = values[1];
6644001999-04-06Per Hedbor  if(!width || !height) error("Width or height == 0\n");
b21c051999-04-06Per Hedbor  int ncolors = values[2]; int cpp = values[3]; if(sizeof(values)>5) { retopts->hot_x = values[4]; retopts->hot_y = values[5]; }
6644001999-04-06Per Hedbor  TD("Colors"); mixed colors = ([]); if(sizeof(data) < ncolors+2) error("Too few elements in array to decode color values\n");
b21c051999-04-06Per Hedbor 
6644001999-04-06Per Hedbor  colors = sort(data[1..ncolors]); TD("Creating images");
b21c051999-04-06Per Hedbor  object i = Image.image( width, height ); object a = Image.image( width, height,255,255,255 );
6644001999-04-06Per Hedbor  TD("Decoding image");
0410041999-04-09Per Hedbor // for(int y = 0; y<height && y<sizeof(data); y++) _xpm_write_rows( i,a,cpp,colors,data ); // _xpm_write_row( height, i, a, data[ncolors+y+1], cpp, colors );
6644001999-04-06Per Hedbor  TD("Done");
0410041999-04-09Per Hedbor 
b21c051999-04-06Per Hedbor  retopts->image = i; retopts->alpha = a; return retopts; }
0410041999-04-09Per Hedbor object decode( string what )
b21c051999-04-06Per Hedbor { return _decode(what)->image; }