#include <stdio.h>
#include <sys/types.h>

#define GETLONG(buf) (((((((buf)[3]<<8)|(buf)[2])<<8)|(buf)[1])<<8)|(buf)[0])
#define GETSHORT(buf) ((((buf)[1])<<8)|(buf)[0])

main(int argc, char *argv[])
{
  FILE *fp;
  unsigned char buf[512],c;
  u_long topaddr,len,type;
  u_short dataoff, descoff, iconoff;
  int i,j;
  if(argc <2){
    exit(-1);
  }
  fp = fopen(argv[1],"r");
  fread(buf,512,1,fp);
  type = buf[1];
  dataoff = GETSHORT(&buf[2]);
  descoff = GETSHORT(&buf[4]);
  iconoff = GETSHORT(&buf[6]);
  topaddr = GETLONG(&buf[12]);
  len = GETLONG(&buf[16]);

  printf("Type %d data=%x descoff=%x iconoff=%x topaddr =%x len=%x\n",
	 type, dataoff, descoff, iconoff, topaddr,len);
  if(descoff <512){
    printf("Desc:%s\n",&buf[descoff]);
  }
  printf("-----icon:cut here----\n");
  printf("P2\n");
  printf("32 32\n");
  printf("3\n");
  if(iconoff <256){
    for(i=0;i<256;i++){
      if((i&7)==0)
	printf("\n");
      c = buf[i+iconoff];
#if 1
      for(j=3;j>=0;j--){
	printf(" %1d", 3-((c>>(j*2))&3));
      }
#endif
    }
  }
  printf("\n-------end icon---------");
}
