;/*
FailAt 1
LC -v -iINCLUDE:CompactH/ icon2c.c
Blink FROM lib:c.o icon2c.o TO Icon2c LIB lib:lc.lib SC SD ND
Quit


    Icon2c - created by Ray Lambert on 10-23-89


    Purpose: read an icon file (Workbench DiskObject) and convert it to
              c labguage constant data statements for inclusion in programs
              that would like to save an icon with data files.

    Usage (CLI only): Icon2c <iconfile[.info]> <cfile[.c]>

*/


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>
#include <intuition/intuition.h>
#include <graphics/gfx.h> /* for RASSIZE macro definition */
#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <proto/exec.h>
#include <proto/icon.h>


struct Library *IconBase  = NULL;
struct DiskObject *dO     = NULL;
FILE *file                = NULL;

struct DrawerData *dd;
struct Gadget *g;
char *c;
char **cp;
int i;


void bustit(void)
{
  if (file) fclose(file);
  if (dO) FreeDiskObject(dO);
  if (IconBase) CloseLibrary(IconBase);
  exit(0);
}

#define WORDSPERROW 10

void dumpimg(struct Image *img, char *namepfx)
{
  int imgwords = ( RASSIZE(img->Width,img->Height) * img->Depth );
  int rows, last, cnt;
  USHORT *s = img->ImageData;

/* dump the image data array */
  fprintf(file,"USHORT %sImageData[] =\n\t{\n",namepfx);
  rows = (imgwords / WORDSPERROW); /* number of rows of hex words to dump */
  last = (imgwords % WORDSPERROW); /* number of hex words on the last line*/
  for(; rows>0; rows--)
    {
      fprintf(file,"\t\t");
      for(cnt = 0; cnt < WORDSPERROW; cnt++)
        {
          if (cnt > 0) fprintf(file,",");
          fprintf(file,"0x%04X", *s);
          s++;
        }
      if (rows > 1)
        {
          fprintf(file,",\n");
        }
      else
        {
          if (last != 0) fprintf(file,",");
          fprintf(file,"\n");
        }
    }
  if (last > 0)
    {
      fprintf(file,"\t\t");
      for(; last>0; last--, s++)
        {
          fprintf(file,"0x%04X", *s);
          if (last > 1) fprintf(file,",");
        }
      fprintf(file,"\n");
    }
  fprintf(file,"\t};\n\n");

/* dump the struct Image */
  fprintf(file,"struct Image %sImage =\n\t{\n", namepfx);
  fprintf(file,"\t\t%hd, %hd, %hd, %hd, %hd,\n",
      img->LeftEdge, img->TopEdge, img->Width, img->Height, img->Depth );
  fprintf(file,"\t\t%sImageData,\n", namepfx);
  fprintf(file,"\t\t%hd, %hd,\n", (USHORT)img->PlanePick,
                                  (USHORT)img->PlaneOnOff );
  fprintf(file,"\t\tNULL\n\t};\n\n");
}


void main(int argc, char *argv[])
{

/* check for Workbench */
  if (argc == 0)  /* Workbench! */
    {
      printf("\nCLI ONLY!!\n"); /* just in case someone's listening */
      exit(0);
    }

  printf("\n›33;40mIcon2c Version 1.0 - Created by Ray Lambert›0m\n\n");

/* check args */
  if (argc != 3)
    {
      printf("Usage: Icon2c <iconfile[.info]> <c file>\n\n");
      exit(5);
    }

/* open icon library */
  IconBase = (struct Library *)OpenLibrary("icon.library",0);
  if (!IconBase)
    {
      printf("\"icon.library\" is on vacation!\n\n");
      exit(20);
    }

/* read the icon */
  /* check for & remove file extension (.info) */
  c = (char *)(argv[1] + strlen(argv[1]) - 5);
  if (stricmp(c,".info") == 0) *c = '\0';
  /* ok now, go get it */
  dO = GetDiskObject(argv[1]);
  if (!dO)
    {
      printf("Sorry, that icon (\"%s\") isn't around...\n\n",argv[1]);
      bustit();
    }

/* open the output file */
  file = fopen(argv[2],"wt");
  if (!file)
    {
      printf("Can't open your c file (\"%s\")\n\n",argv[2]);
      bustit();
    }

/* ok, got everything at our fingertips now */

  printf("Working...");

  /* say hello... */
  fprintf(file,"/*\n");
  fprintf(file,"** \"%s.info\" - dumped by Icon2c!\n",argv[1]);
  fprintf(file,"*/\n\n");

  /* some headers they may need... */
  fprintf(file,"#include <workbench/workbench.h>\n");
  fprintf(file,"#include <intuition/intuition.h>\n\n");

/* start with the loose stuff... */

/* Render imagery */
  g = &dO->do_Gadget;
  if (g->GadgetRender)
    {
      dumpimg((struct Image *)g->GadgetRender,"Render");
    }

/* Select imagery */
  if (g->SelectRender)
    {
      dumpimg((struct Image *)g->SelectRender,"Select");
    }

/* the ToolTypes array */
  cp = dO->do_ToolTypes;
  if ( (cp) && (*cp) )
    {
      fprintf(file,"char *ToolTypes[] = \n\t{\n");
      while(*cp)
        {
          fprintf(file,"\t\t\"%s\",\n",*cp);
          cp++;
        }
      fprintf(file,"\t\tNULL\n\t};\n\n");
    }

/* the DrawerData structure (for drawers only) */
  dd = dO->do_DrawerData;
  if (dd)
    {
      register struct NewWindow *nw = &dd->dd_NewWindow;
      fprintf(file,"struct DrawerData drawerData =\n\t{\n");
      fprintf(file,"\t\t{  /* struct NewWindow */\n");
      fprintf(file,"\t\t\t%hd, %hd, %hd, %hd,\n", nw->LeftEdge, nw->TopEdge,
                                                  nw->Width,    nw->Height );
      fprintf(file,"\t\t\t%hd, %hd,\n", (USHORT)nw->DetailPen,
                                        (USHORT)nw->BlockPen  );
      fprintf(file,"\t\t\t0x%08X,\n", nw->IDCMPFlags );
      fprintf(file,"\t\t\t0x%08X,\n", nw->Flags );
      fprintf(file,"\t\t\tNULL,\n");
      fprintf(file,"\t\t\tNULL,\n");
      fprintf(file,"\t\t\tNULL,\n");
      fprintf(file,"\t\t\tNULL,\n");
      fprintf(file,"\t\t\tNULL,\n");
      fprintf(file,"\t\t\t%hd, %hd, %hd, %hd,\n",
                            nw->MinWidth, nw->MinHeight,
                            nw->MaxWidth, nw->MaxHeight );
      fprintf(file,"\t\t\t%hd\n\t\t},\n", nw->Type);

      fprintf(file,"\t\t%ld,\n", dd->dd_CurrentX);
      fprintf(file,"\t\t%ld\n\t};\n\n", dd->dd_CurrentY);
    }

/* ok, on to the big guy! */

  /* figure a name for the struct based on icon's name */
  c = (char *)(argv[1] + strlen(argv[1]) - 1);
  while( (c > argv[1]) && (*c != '/') && (*c != ':') )
    {
      if (*c == '.') *c = '_';
      c--;
    }
  if ( (*c == '/') || (*c == ':') ) c++;
  fprintf(file,"struct DiskObject do_%s =\n\t{\n", c);
  fprintf(file,"\t\t0x%04X,\n", (ULONG)dO->do_Magic);
  fprintf(file,"\t\t0x%04X,\n", (ULONG)dO->do_Version);

  fprintf(file,"\t\t{ /* struct Gadget */\n");
  fprintf(file,"\t\t\tNULL,\n");
  fprintf(file,"\t\t\t%hd, %hd, %hd, %hd,\n", g->LeftEdge, g->TopEdge,
                                              g->Width,    g->Height  );
  fprintf(file,"\t\t\t0x%04X,\n", (ULONG)g->Flags);
  fprintf(file,"\t\t\t0x%04X,\n", (ULONG)g->Activation);
  fprintf(file,"\t\t\t0x%04X,\n", (ULONG)g->GadgetType);
  fprintf(file,"\t\t\t");
  if (g->GadgetRender != NULL)
    {
      fprintf(file,"(APTR)&RenderImage,\n");
    }
  else
    {
      fprintf(file,"NULL,\n");
    }
  fprintf(file,"\t\t\t");
  if (g->SelectRender != NULL)
    {
      fprintf(file,"(APTR)&SelectImage,\n");
    }
  else
    {
      fprintf(file,"NULL,\n");
    }
  fprintf(file,"\t\t\tNULL,\n");
  fprintf(file,"\t\t\t0x%08X,\n", g->MutualExclude);
  fprintf(file,"\t\t\tNULL,\n");
  fprintf(file,"\t\t\t0x%04X,\n", (ULONG)g->GadgetID);
  fprintf(file,"\t\t\tNULL\n\t\t},\n");

  fprintf(file,"\t\t%d,\n", dO->do_Type);
  fprintf(file,"\t\t\"%s\",\n", dO->do_DefaultTool);
  fprintf(file,"\t\t");
  if ( (dO->do_ToolTypes != NULL) && (*dO->do_ToolTypes != NULL) )
    {
      fprintf(file,"ToolTypes,\n");
    }
  else
    {
      fprintf(file,"NULL,\n");
    }
  fprintf(file,"\t\t%ld,\n", dO->do_CurrentX);
  fprintf(file,"\t\t%ld,\n", dO->do_CurrentY);
  fprintf(file,"\t\t");
  if (dO->do_DrawerData != NULL)
    {
      fprintf(file,"&drawerData,\n");
    }
  else
    {
      fprintf(file,"NULL,\n");
    }
  fprintf(file,"\t\t\"%s\",\n", dO->do_ToolWindow);
  fprintf(file,"\t\t%ld\n\t};\n\n", dO->do_StackSize);

/* welp, guess that's it! */
  printf(" ok dude!  you got it...\n\n");
  bustit();
}




