Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 3 Mar 2005 15:09:00 -0800
From:      "Douglas Pan" <pan@catalyticinc.com>
To:        <jedgar@FreeBSD.org>
Cc:        ports@FreeBSD.org
Subject:   FreeBSD Port: icod-1.0
Message-ID:  <0a5a01c52045$fc53f4c0$fd0114ac@panda>

next in thread | raw e-mail | index | archive | help
This is a multi-part message in MIME format.

------=_NextPart_000_0A56_01C52002.EE225CE0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

First, thanks for making the program available.  I tried compiling it =
and running it against the attached cc1.ico file and got two SIGFPEs.  I =
modified icod.c a bit (attached) and got through, but I am not sure that =
the generated xpm files are faithful to the original.  Would you care to =
comment?

My platform is:

[pan@beernut _jvm]$ uname -a
Linux beernut 2.4.21-27.0.2.EL #1 Wed Jan 12 23:46:37 EST 2005 i686 i686 =
i386 GNU/Linux

[pan@beernut _jvm]$ gcc --version
gcc (GCC) 3.2.3 20030502 (Red Hat Linux 3.2.3-49)
Copyright (C) 2002 Free Software Foundation, Inc.=20

Thanks,
--Doug
------=_NextPart_000_0A56_01C52002.EE225CE0
Content-Type: application/octet-stream;
	name="icod.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="icod.c"

/* File : icod.c=0A=
=0A=
   Simon Drabble	02/05/99=0A=
   madlather@syspac.com=0A=
=0A=
   (C) 1998 - 1999  Simon Drabble=0A=
=0A=
   This program and its associated code are released under=0A=
   the Gnu Public Licence (GPL). This basically states you=0A=
   are free to modify and distribute the software provided=0A=
   you do not prevent anyone else doing the same.        =0A=
   For a full copy of the licence, see www.gnu.org/copyleft/gpl.html=0A=
=0A=
*/=0A=
=0A=
=0A=
#include <stdlib.h>=0A=
#include <stdio.h>=0A=
#include <string.h>=0A=
#include "global.h"=0A=
#include "icod.h"=0A=
=0A=
=0A=
BYTE *global_colour_map =3D NULL;=0A=
char *global_file_name =3D NULL;=0A=
const char *ok_chars =3D =
"!@#$%^&*(){}[]|+=3D-,.<>/?;:abcdefghjiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR=
STUVWXYZ0123456789";=0A=
=0A=
=0A=
#define USAGE	"Usage: icod file1.ico [file2.ico .. filen.ico]"=0A=
=0A=
int main(int argc, char *argv[])=0A=
{=0A=
  int i;=0A=
=0A=
#ifdef DEBUG=0A=
  fprintf(stderr, "BYTE (%d)\nWORD (%d)\nDWORD (%d)\nLONG (%d)\nICONDIR =
(%d)\nICONDIRENTRY (%d)\n",=0A=
          sizeof(BYTE), sizeof(WORD), sizeof(DWORD), sizeof(LONG),=0A=
          sizeof(ICONDIR), sizeof(ICONDIRENTRY));=0A=
=0A=
  fprintf(stderr, "BITMAPINFOHEADER (%d)\nRGBQUAD (%d)\n",=0A=
          sizeof(BITMAPINFOHEADER), sizeof(RGBQUAD));=0A=
#endif=0A=
=0A=
  if (argc =3D=3D 1) {=0A=
    printf("%s\n", USAGE);=0A=
    exit(0);=0A=
  }=0A=
=0A=
  for (i =3D 1; i < argc; ++i) =0A=
    load_icon(argv[i]);=0A=
=0A=
  return 0;=0A=
  =0A=
}=0A=
=0A=
=0A=
int init_colour_map(ICONIMAGE *ii)=0A=
{=0A=
  register int i, j, k;=0A=
  int cpp =3D 2,=0A=
      bd =3D ii->icHeader.biBitCount,=0A=
      bc =3D 1 << bd;=0A=
=0A=
  long size =3D bc * cpp;=0A=
=0A=
  int b =3D strlen(ok_chars);=0A=
  =0A=
  free(global_colour_map);=0A=
  CREATE(global_colour_map, char, size + 1);=0A=
=0A=
  memset(global_colour_map, 0, size);=0A=
=0A=
  for (i =3D 0; i < b; ++i)=0A=
    for (j =3D 0; j < b; ++j) {=0A=
      k =3D (i*b+j) * cpp;=0A=
      if (k < size) {=0A=
        *(global_colour_map + k) =3D ok_chars[j];=0A=
        *(global_colour_map + k + 1) =3D ok_chars[i];=0A=
      } else=0A=
        return 0;=0A=
    }=0A=
=0A=
}=0A=
=0A=
  =0A=
=0A=
int load_icon(char *fname)=0A=
{=0A=
  FILE *iconf;=0A=
=0A=
  if (!(iconf =3D fopen(fname, "r b"))) =0A=
    return -1;=0A=
=0A=
  free(global_file_name);=0A=
  CREATE(global_file_name, char, strlen(fname) + 1);=0A=
  strcpy(global_file_name, fname);=0A=
=0A=
  if ((strrchr(global_file_name, '.')))=0A=
    *(strrchr(global_file_name, '.')) =3D '\0';=0A=
  =0A=
  load_icon_file(iconf);=0A=
}=0A=
=0A=
=0A=
=0A=
int load_icon_file(FILE *iconf)=0A=
{  =0A=
  ICONDIR *idir, temp;=0A=
=0A=
  int i, tot =3D 0;=0A=
=0A=
=0A=
  CREATE(idir, ICONDIR, 1);=0A=
=0A=
  memset(idir, 0, sizeof(ICONDIR));=0A=
=0A=
  tot +=3D fread(&idir->idReserved, sizeof(WORD), 1, iconf);=0A=
  tot +=3D fread(&idir->idType, sizeof(WORD), 1, iconf);=0A=
  tot +=3D fread(&idir->idCount, sizeof(WORD), 1, iconf);=0A=
  tot *=3D sizeof(WORD);=0A=
=0A=
=0A=
  CREATE(idir->idEntries, ICONDIRENTRY, idir->idCount); =0A=
  tot =3D fread(idir->idEntries, sizeof(ICONDIRENTRY), idir->idCount, =
iconf);=0A=
  printf("\n%s: %d icons.\n", global_file_name, idir->idCount);=0A=
=0A=
=0A=
  for (i =3D 0; i < idir->idCount; ++i)=0A=
    load_icon_image(iconf, i, idir);=0A=
=0A=
}=0A=
=0A=
=0A=
int load_icon_image(FILE *inf, int i, ICONDIR *idir)=0A=
{ =0A=
  ICONIMAGE *ii;=0A=
  int tot =3D 0;=0A=
  size_t nc;=0A=
  size_t c;=0A=
  s_xpm *xpm;=0A=
=0A=
  CREATE(ii, ICONIMAGE, 1);=0A=
  memset(ii, 0, sizeof(*ii));=0A=
=0A=
  /* Am I really doing this? */=0A=
  fseek(inf, idir->idEntries[i].dwImageOffset, SEEK_SET);=0A=
=0A=
  tot =3D fread(&ii->icHeader, sizeof(BITMAPINFOHEADER), 1, inf);=0A=
=0A=
  dump_iconimage(ii);=0A=
=0A=
  nc =3D 1 << ii->icHeader.biBitCount;=0A=
  if (ii->icHeader.biBitCount !=3D 24) {=0A=
    CREATE(ii->icColours, RGBQUAD, nc);=0A=
    tot =3D fread(ii->icColours, sizeof(RGBQUAD), nc, inf);=0A=
    c =3D (ii->icHeader.biWidth * ii->icHeader.biHeight / 2) /=0A=
         (8.0 / ii->icHeader.biBitCount);=0A=
=0A=
  } else {=0A=
/*=0A=
    CREATE(ii->icColours, RGBQUAD,=0A=
           ii->icHeader.biWidth * ii->icHeader.biHeight / 2);=0A=
*/=0A=
    ii->icColours =3D NULL;=0A=
    c =3D (ii->icHeader.biWidth * ii->icHeader.biHeight / 2) * 3;=0A=
    =0A=
  }=0A=
=0A=
  CREATE(ii->icXOR, BYTE, c);=0A=
  tot =3D fread(ii->icXOR, sizeof(BYTE), c, inf);=0A=
=0A=
=0A=
  /* really * 2 / 8 */=0A=
/*=0A=
  c =3D ii->icHeader.biWidth * ii->icHeader.biHeight >> 2;=0A=
  CREATE(ii->icAND, BYTE, c); =0A=
  tot =3D fread(ii->icAND, sizeof(BYTE), c, inf);=0A=
*/=0A=
=0A=
  CREATE(xpm, s_xpm, 1); =0A=
  xpm->cpp =3D 2;=0A=
=0A=
  if (ii->icHeader.biBitCount =3D=3D 24) {=0A=
    process_24bit(ii, xpm);=0A=
  } else {=0A=
    init_colour_map(ii);=0A=
    ico2xpm(ii, xpm); =0A=
    write_xpm(ii, xpm);=0A=
  }=0A=
=0A=
=0A=
  free(xpm->data);  =0A=
  free(xpm);=0A=
  free(ii->icAND);=0A=
  free(ii->icXOR);=0A=
  free(ii->icColours);=0A=
  free(ii);=0A=
  =0A=
  return 0;=0A=
}=0A=
=0A=
=0A=
=0A=
int process_24bit(ICONIMAGE *ii, s_xpm *xpm)=0A=
{=0A=
  register int i, j, k, l, m, n, p, q, r;=0A=
  int b =3D strlen(ok_chars);=0A=
  int mult =3D 3 + xpm->cpp;=0A=
  int size =3D (ii->icHeader.biWidth * ii->icHeader.biHeight / 2) * mult;=0A=
  int offset;=0A=
=0A=
  struct s_rgb {=0A=
    BYTE red, grn, blu;=0A=
  } rgb;=0A=
=0A=
  FILE *xpmf;=0A=
  char *fname;=0A=
=0A=
  CREATE(fname, char, strlen(global_file_name) + 64);=0A=
  strcpy(fname, global_file_name);=0A=
  =0A=
  xpm->width =3D ii->icHeader.biWidth;=0A=
  xpm->height =3D ii->icHeader.biHeight / 2;=0A=
=0A=
  CREATE(xpm->data, char, (xpm->width * xpm->height * xpm->cpp));=0A=
=0A=
  =0A=
=0A=
  /* Build the colour_table */=0A=
  =0A=
  CREATE(global_colour_map, char, size);=0A=
  memset(global_colour_map, 0, size);=0A=
=0A=
  n =3D p =3D q =3D 0;=0A=
=0A=
  l =3D ii->icHeader.biHeight / 2;=0A=
  r =3D ii->icHeader.biWidth * 3;=0A=
=0A=
  for (i =3D 0; i < l; ++i)=0A=
    for (j =3D 0; j < r; j +=3D 3) {=0A=
      offset =3D i * r + j;=0A=
      rgb.blu =3D *(ii->icXOR + offset);=0A=
      rgb.grn =3D *(ii->icXOR + offset + 1);=0A=
      rgb.red =3D *(ii->icXOR + offset + 2);=0A=
=0A=
      for (k =3D 0; k < n; k +=3D mult) =0A=
        if (!memcmp(&rgb, global_colour_map + k + xpm->cpp,=0A=
                    sizeof(struct s_rgb)))=0A=
            break;=0A=
=0A=
      if (k >=3D n) {=0A=
        n +=3D mult;=0A=
        *(global_colour_map + k) =3D ok_chars[p];=0A=
        *(global_colour_map + k + 1) =3D ok_chars[q];=0A=
        if (++p >=3D b) {=0A=
          p =3D 0; ++q;=0A=
        }=0A=
        if (q >=3D b) {=0A=
          printf("Ahaarrr matey, ran out of colours! Shiver me =
timbers!\n");=0A=
          return -1;=0A=
        }=0A=
        *(global_colour_map + k + xpm->cpp) =3D rgb.red;=0A=
        *(global_colour_map + k + xpm->cpp + 1) =3D rgb.grn;=0A=
        *(global_colour_map + k + xpm->cpp + 2) =3D rgb.blu;=0A=
      }=0A=
=0A=
      m =3D i * xpm->width + (j / 3);=0A=
      *(xpm->data + (m * xpm->cpp)) =3D *(global_colour_map + k);=0A=
      *(xpm->data + (m * xpm->cpp) + 1) =3D *(global_colour_map + k + 1);=0A=
    }=0A=
=0A=
  xpm->num_cols =3D n / mult;=0A=
=0A=
  sprintf(fname, "%s%02dX%02d_%d.xpm",=0A=
          global_file_name, xpm->width, xpm->height, =
ii->icHeader.biBitCount);=0A=
=0A=
  if (!(xpmf =3D fopen(fname, "w")))=0A=
    return -1; =0A=
=0A=
  free(fname);=0A=
=0A=
  write_xpm_hdr(xpm, xpmf);=0A=
=0A=
  k =3D (xpm->num_cols * mult);=0A=
  for (i =3D 0; i < k; i +=3D mult)=0A=
    fprintf(xpmf, "\"%c%c\tc #%02x%02x%02x\",\n",=0A=
            *(global_colour_map + i), *(global_colour_map + i + 1),=0A=
            *(global_colour_map + i + xpm->cpp),=0A=
            *(global_colour_map + i + xpm->cpp + 1),=0A=
            *(global_colour_map + i + xpm->cpp + 2));=0A=
=0A=
  write_xpm_data(xpm, xpmf);=0A=
=0A=
  fclose(xpmf);=0A=
  return 0;=0A=
}=0A=
=0A=
=0A=
=0A=
=0A=
int ico2xpm(ICONIMAGE *ii, s_xpm *xpm)=0A=
{=0A=
  register int i, j, k, l, m, n, p, q;=0A=
  BYTE pixel, byte;=0A=
=0A=
  k =3D ii->icHeader.biBitCount;=0A=
  xpm->width =3D ii->icHeader.biWidth;=0A=
  xpm->height =3D ii->icHeader.biHeight / 2;=0A=
  xpm->num_cols =3D (1 << k);=0A=
=0A=
  CREATE(xpm->data, char, xpm->width * xpm->height * xpm->cpp);=0A=
=0A=
=0A=
=0A=
  p =3D 8 / k;=0A=
  q =3D (1 << k) - 1;=0A=
  =0A=
  for (i =3D 0; i < xpm->height; ++i) =0A=
    for (j =3D 0; j < xpm->width; ++j) {=0A=
=0A=
      int offset =3D (i * xpm->width + j);=0A=
=0A=
       /* This is where we use the bit-depth info */=0A=
      byte =3D *(ii->icXOR + (p !=3D 0 ? offset / p : 0));=0A=
      for (l =3D 8 - k, n =3D 0; l >=3D 0; l -=3D k, ++n) {=0A=
        m =3D offset + n;=0A=
        pixel =3D (byte >> l) & q;=0A=
=0A=
        *(xpm->data + m * xpm->cpp) =3D=0A=
          global_colour_map[pixel * xpm->cpp];=0A=
=0A=
        *(xpm->data + m * xpm->cpp + 1) =3D=0A=
          global_colour_map[pixel * xpm->cpp + 1];=0A=
      }=0A=
=0A=
    }=0A=
=0A=
  return 0;=0A=
}=0A=
=0A=
=0A=
int write_xpm_hdr(s_xpm *xpm, FILE *outf)=0A=
{=0A=
  fprintf(outf, "/* XPM */\n");=0A=
  fprintf(outf, "char * image_data[] =3D {\n");=0A=
  fprintf(outf, "\"%d  %d  %d  %d\",\n",=0A=
          xpm->width, xpm->height, xpm->num_cols, xpm->cpp);=0A=
}=0A=
=0A=
=0A=
int write_xpm(ICONIMAGE *ii, s_xpm *xpm)=0A=
{=0A=
  register int i, j;=0A=
  int base =3D 0;=0A=
  FILE *xpmf;=0A=
  char *fname;=0A=
=0A=
  CREATE(fname, char, strlen(global_file_name) + 64);=0A=
  strcpy(fname, global_file_name);=0A=
  =0A=
  sprintf(fname, "%s%02dX%02d_%d.xpm",=0A=
          global_file_name, xpm->width, xpm->height, =
ii->icHeader.biBitCount);=0A=
=0A=
  if (!(xpmf =3D fopen(fname, "w")))=0A=
    return -1; =0A=
=0A=
  free(fname);=0A=
=0A=
  write_xpm_hdr(xpm, xpmf);=0A=
  write_colour_table(ii, xpmf);=0A=
=0A=
  write_xpm_data(xpm, xpmf);=0A=
=0A=
  fclose(xpmf);=0A=
  return 0;=0A=
}=0A=
=0A=
=0A=
=0A=
int write_xpm_data(s_xpm *xpm, FILE *xpmf)=0A=
{=0A=
  register int i, j, offset;=0A=
=0A=
  /* For some reason data is stored upside down.. */=0A=
  for (i =3D xpm->height - 1; i >=3D 0; --i) {=0A=
    fprintf(xpmf, "\"");=0A=
    for (j =3D 0; j < xpm->width; ++j) {=0A=
      offset =3D i * xpm->width + j;=0A=
      fprintf(xpmf, "%1c%1c",=0A=
              *(xpm->data + offset * xpm->cpp),=0A=
              *(xpm->data + offset * xpm->cpp + 1));=0A=
    }=0A=
    fprintf(xpmf, "\",\n");=0A=
  }=0A=
  fprintf(xpmf, "};\n\n");=0A=
=0A=
  return 0;=0A=
}=0A=
=0A=
=0A=
int dump_andmask(ICONIMAGE *ii)=0A=
{=0A=
  register int i, j, k;=0A=
  int base =3D 0;=0A=
  for (i =3D 0; i < ii->icHeader.biHeight ; i +=3D 2) {=0A=
    for (j =3D 0; j < ii->icHeader.biWidth; j +=3D 8) {=0A=
      for (k =3D 7; k >=3D 0; --k) {=0A=
        base =3D *(ii->icAND + ((i * ii->icHeader.biWidth + j) / 8)) & =
(1 << k);=0A=
        fprintf(stderr, "%c",=0A=
                base ? '1' : '0');=0A=
      }=0A=
    }=0A=
    fputc('\n', stderr);=0A=
        =0A=
  }=0A=
  fputc('\n', stderr);=0A=
} =0A=
 =0A=
=0A=
int write_colour_table(ICONIMAGE *ii, FILE *outf)=0A=
{=0A=
  long i; =0A=
=0A=
  for (i =3D 0; i < (long) (1 << ii->icHeader.biBitCount); ++i) {=0A=
    fprintf(outf, "\"%1c%1c\tc #%02x%02x%02x\",\n",=0A=
            global_colour_map[i * 2], =0A=
            global_colour_map[i * 2 + 1], =0A=
            ii->icColours[i].red,=0A=
            ii->icColours[i].green,=0A=
            ii->icColours[i].blue);=0A=
  }=0A=
  fputc('\n', outf);=0A=
=0A=
}=0A=
  =0A=
=0A=
=0A=
int dump_iconimage(ICONIMAGE *ii)=0A=
{=0A=
  printf("ICON: %2ldx%2ld %2ldbpp Sz:%ld\n", =0A=
         ii->icHeader.biWidth, ii->icHeader.biHeight / 2,=0A=
         ii->icHeader.biBitCount,=0A=
         ii->icHeader.biSizeImage);=0A=
=0A=
}=0A=
  =0A=
int dump_icondir(ICONDIR *idir)=0A=
{=0A=
  int i;=0A=
  fprintf(stderr, "%4ld : Reserved\n%4ld : Type\n%4ld : Count\n",=0A=
          idir->idReserved, idir->idType, idir->idCount);=0A=
  dump_icondirent(0, idir);=0A=
  =0A=
}=0A=
=0A=
=0A=
int write_icondir(char *name, ICONDIR *idir)=0A=
{=0A=
  FILE *fout;=0A=
  fout =3D fopen(name, "w b");=0A=
  fwrite(idir, sizeof(ICONDIR), 1, fout);=0A=
  fclose(fout);=0A=
}=0A=
  =0A=
int dump_icondirent(int i, ICONDIR *idir)=0A=
{ =0A=
=0A=
  fprintf(stderr, "%2d: Wd(%d) Ht(%d) CC(%d) Bc(%d) Sz(%04lx) =
Of(%04lx)\n", i,=0A=
          idir->idEntries[i].bWidth,=0A=
          idir->idEntries[i].bHeight,=0A=
          idir->idEntries[i].bColourCount,=0A=
          idir->idEntries[i].wBitCount,=0A=
          idir->idEntries[i].dwBytesInRes,=0A=
          idir->idEntries[i].dwImageOffset);=0A=
  =0A=
}=0A=
=0A=
int dump_xormask(ICONIMAGE *ii, size_t c)=0A=
{=0A=
  int i;=0A=
  fprintf(stderr, "Dumping %d bytes.\n", c);=0A=
  for (i =3D 0; i < c; ++i) {=0A=
    if (!(i % 48)) fputc('\n', stderr);=0A=
    if (!(i % 3)) fputc(' ', stderr);=0A=
    fprintf(stderr, "%02x", *(ii->icXOR + i));=0A=
  }=0A=
  fputc('\n', stderr);=0A=
}=0A=

------=_NextPart_000_0A56_01C52002.EE225CE0--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?0a5a01c52045$fc53f4c0$fd0114ac>