XPM variable names are over-mangled
Posted: 2010-11-01T14:38:32-07:00
I was trying to convert a file to CPM format with the filename "logo-50.xpm". I was expecting the XPM header to read:
Instead, I got
However, the filename gave no indication that the variable name in the XPM file was mangled in this way (I knew that the hyphen would be turned to an underscore, but GIMP saves with the numbers intact and suffixed with "_xpm". Thus the GIMP-generated file is:
The conversion of the numeric characters to underscores in very annoying when you want to convert files like "logo-50.xpm" and "logo-70.xpm", since both are mangled to "logo___". I fixed it for my purposes by changing coders/xpm.c and recompiling from source. The change I made was at line 972-975:
It may or may not be desirable to add some logic to look out for an initial numeric character as this wouldn't compile into a C program, but it would again provide different results to GIMP which may not be expected by a user. It may also be preferable to get the "_xpm" suffix by scanning the filename, but this would only be useful when people are saving XPM files with strange extensions.
Either way, the simple fix above prevents name collision of file names with digits in the same place, which is very common.
Code: Select all
/* XPM */
static char *logo_50_xpm[] = {
Code: Select all
/* XPM */
static char *logo___[] = {
Code: Select all
/* XPM */
static char *logo_50_xpm[] = {
Code: Select all
if (isalpha((int) ((unsigned char) basename[i])) == 0)
basename[i]='_';
(void) FormatMagickString(buffer,MaxTextExtent,
"static char *%s[] = {\n",basename);
Code: Select all
if (isalnum((int) ((unsigned char) basename[i])) == 0)
basename[i]='_';
(void) FormatMagickString(buffer,MaxTextExtent,
"static char *%s_xpm[] = {\n",basename);
Either way, the simple fix above prevents name collision of file names with digits in the same place, which is very common.