
/* --------------------------------------------------------------------------
 *
 * Program Title :
 *
 *      FixIcon - by Robert Lang 15/6/91.
 *
 * Description :
 *
 *      Fixes an icon so that it doesn't show as messy when used on a
 *      more than 4 colour screen. (Particularly since WB2 has this option).
 *
 * Notes :
 *
 *      Compiled on an Amiga 3000UX using Lattice/SAS C V5.10a !
 *
 *      This source is supplied with the program for reference or learning
 *      only, and may not be used within a commercial program or any money-
 *      making project that makes any more than the current Megadisc PD
 *      prices (about $5 a disk).
 *
 *      FixIcon may be distributed freely (ie. not on ANY commercial product
 *      without written consent from the author), provided that the document
 *      file, source file, and executable are all included UNALTERED.
 *
 *      This program does not check what sort of image rendering etc. is
 *      ocurring for this icon...could be a problem with some icons ???
 *
 *      PlaneOnOff may be the source of problems as well, although rare,
 *      so its left out of this program...easy to modify.
 *
 *              Robert Lang
 *              EdgeWare
 *              P.O. Box 127
 *              CARDIFF NSW 2285
 *              AUSTRALIA
 *
 * Revision :
 *
 * 15th June 1991 - V1.0 Release, Robert Lang.
 * 7th July 1991  - V1.1 Fixed...was setting PlanePick wrong... Robert Lang.
 *
 * -------------------------------------------------------------------------*/

#include <workbench/workbench.h>
#include <workbench/icon.h>
#include <clib/icon_protos.h>
#include <clib/exec_protos.h>
#include <dos.h>

struct Library __chip *IconBase = NULL;

#define TODAY "15th June 1991"
#define VERSION "V1.1"

/* a simple macro */

#define GAD(f) ((f)->do_Gadget)
#define IMG(f) (((f)->do_Gadget).GadgetRender)

main(int argc, int *argv[])
{
    struct DiskObject __chip *infofile;
    struct Image __chip *iconimage,*backimage;
    BOOL stat, write_out = FALSE;
    int power,i;

    /* a little brag */
    printf("\nFixIcon "VERSION" by Robert Lang, "TODAY". © Edgeware 1991.\n\n");

    if ((argc == 1) || ((strcmp(argv[1],"?")) == 0)) /* usage output */
    {
        printf("Usage : %s icon\n"
               "\nWhere icon is the icon with the graphical problem "
               "(without the .info extension).\n"
               "FixIcon will attempt to repair the problem.\n"
               "\n\tRobert Lang\n\tP.O. Box 127\n\tEdgeWorth NSW 2285\n\t"
               "AUSTRALIA\n\n",argv[0]);
        exit(RETURN_OK);
    }

    if ((IconBase = OpenLibrary(ICONNAME,0L)) == NULL)
    {
        printf("Error...can't open library "ICONNAME" !\n");
        exit(RETURN_ERROR);
    }

    infofile = GetDiskObject((UBYTE *)argv[1]); /* load the .info file */

    if (!infofile)
    {
        printf("Error...can't open %s.info file !\n",argv[1]);
        CloseLibrary(IconBase);
        exit(RETURN_ERROR);
    }

    iconimage = GAD(infofile).GadgetRender; /* these are the actual images */
    backimage = GAD(infofile).SelectRender;

    if (iconimage == NULL) /* what ? no image ?? */
    {
        printf("Error...Icon has no image !\n");
        goto abort;
    }

    for (power = 0, i = 0; i<=iconimage->Depth; i++)
        power += 2;

    printf("%d,%d\n",iconimage->Depth,power);

    if (iconimage->PlanePick != (power-1)) /* do the repair */
    {
        printf("Front Image PlanePick has an error...fixed !\n");
/*        iconimage->PlanePick = iconimage->Depth; */
        iconimage->PlanePick = power-1; /* RL 3rd July 1991 */
        iconimage->PlaneOnOff = 0;
        write_out = TRUE;
    }
    else
    {
        printf("No error found with PlanePick for front image, no action taken.\n");
    }

    if (backimage == NULL) goto exit; /* no second image */

    for (power = 0, i = 0; i<=backimage->Depth; i++)
        power += 2;

    if (backimage->PlanePick != (power-1)) /* repair second image */
    {
        printf("Back Image PlanePick has an error...fixed !\n");
        backimage->PlanePick  = backimage->Depth;
        backimage->PlaneOnOff = 0;
        write_out = TRUE;
    }
    else
    {
        printf("No error found with PlanePick for back image, no action taken.\n");
    }

exit:
    if (write_out) /* write the .info file back to disk ! */
    {
        stat = PutDiskObject((UBYTE *)argv[1],infofile);
        if (!stat)
            printf("Error ocurred in writing %s.info file...change may not be successful.\n",argv[1]);
    }

abort:
    FreeDiskObject(infofile);
    CloseLibrary(IconBase);

    printf("\n");
}

/* end of fixicon.c --------------------------------------------------------*/