ICO to XPM conventer

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
dsk
Experienced Solver
Experienced Solver
Posts: 91
Joined: Sat Jun 25, 2005 12:10 pm
Location: Poland

ICO to XPM conventer

Post by dsk » Mon Jun 27, 2005 6:31 pm

if someone needs ico to xpm conventer just compile this code
this program was done by Lee Ki Dong and I done some small modifications

Code: Select all

/*

  icotoxpm - convert MS-Windows icon file to XPM file

  version 1.1
  
  Copyright 1996 Lee Ki Dong

   modified by dsk :)
  
*/


#include <stdio.h>
#include <string.h>
#include <errno.h>

#define TRANSPARENT 16

void usage(void);
void exit(int);

int main(int argc, char **argv)
{
  const char color_sym[17] = "0rgybmcaARGYBMCW";
  const char header[5] = {0, 0, 1, 0};
  char *filename;
  int array[32][32];
  int n, i, x, y, color, mask;
  FILE *fpin, *fpout;

  if (argc < 2)
    {
      usage();
      exit(1);
    }

  /* multiple argument */
  for (n = 1; n < argc; n++)
    {
      
      /* open icon(*.ico) file */
      fpin = fopen(argv[n], "r");
      if (fpin == NULL)
        {
          fprintf(stderr, "%s: %s: %s.\n", argv[0], argv[1], strerror(errno));
          exit(1);
        }
      
      /* identify icon file */      
      for (i = 0; i < 4 ;i++)
        {
          if(fgetc(fpin) != header[i])
            {
              fprintf(stderr, "%s: %s is not a icon file.\n", argv[0], 
argv[n]);
              goto next;
            }
        }
      
      /* generate xpm filename */
      filename = argv[n];
      filename[strlen(argv[n]) - 4] = '\0';
      strcat(filename, ".xpm");
      
      /* open xpm file */
      fpout = fopen(filename, "w");
      if (fpout == NULL) 
        {
          fprintf(stderr, "icotoxpm: %s.\n", strerror(errno));
          exit(1);
        }
      printf("Converting to %s ...\n", filename);
      
      /* write xpm header */
      filename[strlen(argv[n]) - 4] = '\0';
      fprintf(fpout, "/* XPM */\n\
static char *%s[] = {\n\
/* %s pixmap\n\
 * width height ncolors chars_per_pixel */\n\
\"32 32 17 1 \",\n\
/* colors */\n\
\"%c c black       m black         s s_black \",\n\
\"%c c red4        m black         s s_darkred \",\n\
\"%c c green4      m black         s s_darkgreen \",\n\
\"%c c yellow4     m black         s s_darkyellow \",\n\
\"%c c blue4       m black         s s_darkblue \",\n\
\"%c c magenta4    m black         s s_darkmagenta \",\n\
\"%c c cyan4       m black         s s_darkcyan \",\n\
\"%c c gray50      m black         s s_darkgray \",\n\
\"%c c gray75      m white         s s_gray \",\n\
\"%c c red         m white         s s_red \",\n\
\"%c c green       m white         s s_green \",\n\
\"%c c yellow      m white         s s_yellow \",\n\
\"%c c blue        m white         s s_blue \",\n\
\"%c c magenta     m white         s s_magenta \",\n\
\"%c c cyan        m white         s s_cyan \",\n\
\"%c c white       m white         s s_white \",\n\
\"%c c none        m white         s none \",\n\
/* pixels */\n", filename, filename, color_sym[0], color_sym[1], color_sym[2],
              color_sym[3], color_sym[4], color_sym[5], color_sym[6],
              color_sym[7], color_sym[8], color_sym[9], color_sym[10],
              color_sym[11], color_sym[12], color_sym[13], color_sym[14],
              color_sym[15], color_sym[16]);

      /* read icon file (XOR mask) */
      for (y = 0; y < 32; y++)
        {
          fseek(fpin, 126 + 16 * (31 - y), SEEK_SET);
          
          for (x = 0; x < 32; x += 2)
            {
              color = fgetc(fpin);
              
              array[x][y] = color >> 4;
              array[x + 1][y] = color & 0x0f;
            }
        }

      /* transparent area (AND mask) */
      for (y = 0; y < 32; y++)
        {
          fseek(fpin, 126 + (32*32/2) + 4 * (31 - y), SEEK_SET);
          
          for (x = 0; x < 32; x += 8)
            {
              mask = fgetc(fpin);
              
              for (i = 0; i < 8; i++)
                if ((mask >> (7 - i)) & 1 == 1)
                  array [x + i][y] = TRANSPARENT;
            }
        }
  
      /* write xpm file */
      for (y = 0; y < 32; y++)
        {
          fprintf(fpout, "\"");
          for (x = 0; x < 32; x += 2) {
            color =  fgetc(fpin);
            
            fputc(color_sym[array[x][y]], fpout);
            fputc(color_sym[array[x+1][y]], fpout);
          }
          fprintf(fpout,"\"");
          if (y != 1)
            fprintf(fpout, ",\n");
          else
            fprintf(fpout, "\n");
        }
      fprintf(fpout, "};");
      
      /* close files */
      fclose(fpout);
      next:
      fclose(fpin);
    }
  exit(0);
}

void usage(void)
{
  fprintf(stderr, "usage : icotoxpm [filename]\n");
  exit(1);
}
tested on MVC++ 6.0

Have a nice day,
Back to wxWidgets and c++, from long time with php projects

C_Bastian
Earned some good credits
Earned some good credits
Posts: 102
Joined: Thu Sep 29, 2005 3:47 pm

Post by C_Bastian » Thu Nov 24, 2005 9:33 pm

cool. Unfortunately I always get a "XPM: Malformed pixel data" error :?

Sebastian

Post Reply