[comp.sys.amiga] Egad!! I forgot "image.c", Here it is...

crunch@well.UUCP (12/11/86)

/***************************** Image.c ************************************
*
*  Image editor for the Gadget Editor.
*
*  This module by
*  Ray R. Larson        This version: Sept. 28, 1986
*
*  This module contains the main Image Editor IDCMP loop and logical control
*  source code for the Image_Ed routines. The main routine Image_Ed
*  is invoked as follows:
*
*   (struct Image *) = Image_Ed( Screen *, Width, Height, Depth, Image *);
*
*   Where:
*     Screen is a pointer to the Screen where the editor window will
*        be opened.
*
*     Width is the number of pixels wide the image is to be (a SHORT value).
*
*     Height is the number of pixels high the image is to be (a SHORT value).
*
*     Depth is the number of bitplanes to be used in the image (a SHORT value).
*
*     Image* is a pointer to an existing Image structure to be edited.
*        If it is NULL, a new Image is created.
*
*     The value returned is a pointer to the Image structure for the edited
*     image, or NULL if the editing was cancelled by the user.
*
*     The memory allocated dynamically for image data and their associated
*     Image structures can be freed by the calling  program using the
*     FreeImage routine included in Imagfunc.c, I.e.,
*
*                  FreeImage(imageptr);
*
*     Where imageptr is a pointer to an Image Structure.
*
*     The declarations for the menus, gadgets and window used in the
*     Image editor are in Imagdcl.c and ImagImag.c. The routines called
*     by the Image Editor in this module are in Imagfunc.c.
*
****************************************************************************/
 
/*  The usual header files to be inserted later  */
#include <intuition/intuition.h>
#include <libraries/dosextens.h>
#include <graphics/gfxbase.h>
#include <graphics/gfx.h>
#include <graphics/display.h>
#include <exec/memory.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <devices/narrator.h>
#include <devices/audio.h>
#include <libraries/translator.h>
 
/*------------ External function declarations ------------*/
 
extern struct ViewPort *ViewPortAddress();
extern struct Window *OpenWindow();
extern struct IntuiMessage *GetMsg();
extern struct ColorMap *GetColorMap();
extern struct Preferences *GetPrefs();
 
/*------------------- global variables -------------------*/
 
extern struct GfxBase *GfxBase;
extern struct IntuitionBase *IntuitionBase;
 
/*------------------- work rasters and bitmaps -----------*/
extern struct RastPort smallraster;
extern struct BitMap smallbitmap;
 
 
/*------------------- macro definitions ------------------*/
#define inrange(n,nl,nh) ((n >= nl) && (n <= nh) ?1:0)
#define inbox(x,y,xt,yt,xb,yb) ((inrange(x,xt,xb) && inrange(y,yt,yb)) ?1:0)
#define MIN(a,b) ((a < b) ? a:b)
#define MAX(a,b) ((a > b) ? a:b)
 
 
/*------------------- color table definitions ------------------*/
 
extern UWORD defaultCT[32], CTable[32];
extern struct ColorMap *cmap;
 
/**************************************************************************
* Definitions and declarations for all them gadgets
***************************************************************************/
/* image editor window dimensions */
#define WINDWIDTH  640
#define WINDHEIGHT 200
 
/* size of the image editing area */
#define FRAMETOP    11
#define FRAMELEFT    3
#define FRAMEBOT   173
#define FRAMERIGHT 401
#define FRAMEHEIGHT  FRAMEBOT-FRAMETOP
#define FRAMEWIDTH   FRAMERIGHT-FRAMELEFT
 
/* size of small image display area */
#define SMALLTOP 50
#define SMALLLEFT 450
#define SMALLBOT 105
#define SMALLRIGHT 610
#define SMALLHEIGHT (SMALLBOT-SMALLTOP)
#define SMALLWIDTH  (SMALLRIGHT-SMALLLEFT)
 
/* gadget numbers */
#define PALETTEG   1
#define MAGMINUSG  2
#define MAGPLUSG   3
#define UPDOWNG    4
#define LEFTRIGHTG 5
#define BIGEDG     6
#define RESETG     7 /* no longer used put in menu */
#define BLUECTLG   8
#define GREENCTLG  9
#define REDCTLG    10
#define FREETG     11
#define LINETG     12
#define BOXTG      13
#define CIRCTG     14
#define FILLBOXTG  15
 
/* menu items  - menu declarations follow the gadgets */
#define PROJMEN 0
#define EDITMEN 1
#define OPTMEN  2
#define TOOLMEN 3
#define RETURNM 0
#define CANCELM 1
#define CLREDWIN 0
#define CLREDALL 1
#define RESETIMAG 2
#define RESETCOLOR 3
#define SHOWSMALL 0
#define HIDESMALL 1
#define FREEHTOOL 0
#define LINETOOL 1
#define BOXTOOL 2
#define CIRCLETOOL 3
#define FILLEDBOX 4
#define FILLTOOL 5
 
/* Flags for various purposes */
#define CLEARIM -1
#define NOCLEARIM 0
 
/* text for the brush color box */
extern struct IntuiText brushtxt;
extern struct Border boxborder,smallimborder, toolgadbord;
 
/* Proportional info for the color prop gadgets */
extern struct PropInfo r_prop, g_prop, b_prop;
 
/* The various gadgets that need to be directly accessed */
/* -- These are all defined in imagdcl.c --              */
 
extern struct Gadget blue_control, green_control, red_control;
 
extern struct Gadget paletteGadg;
 
extern struct PropInfo UpDownProp ;
extern struct Gadget UpDownGadg ;
 
extern struct PropInfo L_R_Prop ;
extern struct Gadget L_R_Gadg;
 
extern struct Gadget freehandGadg, linetoolGadg, boxtoolGadg,
                     circtoolGadg, recttoolGadg;
 
/* the newwindow structure for the editor screen */
extern struct NewWindow gwind;
extern struct Menu im_Titles[];  /* in imagdcl.c */
extern struct MenuItem im_Items[];
 
/* External functions that don't return integers */
extern struct Image *bitmapTOimage();
 
/***************************************************************************
*
*  Image_Ed - the main image editing routine follows.
*
***************************************************************************/
struct Image *Image_Ed(screen, imageWidth, imageHeight, imageDepth, ModImage)
     struct Screen *screen;
     SHORT imageWidth, imageHeight, imageDepth;
     struct Image *ModImage;
  {
    struct Window *edwin;
    struct RastPort *edrp; /* rasterport for editor window */
    struct BitMap *tmpbitmap;
    struct Image *outImage;
    struct IntuiMessage *inmsg;
    struct Gadget *curgadget, *prevtoolgad;
    struct ViewPort *vport;
    LONG curpen, color;
    ULONG class;
    SHORT i, mousex, mousey, magnification, selected, curtool;
    USHORT code, gadposition;
    SHORT bigmax_x, bigmax_y, smallmax_x, smallmax_y;
    SHORT smallimW, smallimH, temp_x, temp_y, current_x, current_y;
    SHORT anchorx, anchory, lastx, lasty, circflag;
    BOOL go_on, cancel, zap_props, mousetrap, showsmallim;
    FLOAT maxpot, fltemp, flpot;
    UWORD *ctabptr;
 
 
    /* set the flags and body sizes for the proportional gadgets */
 
    r_prop.Flags = g_prop.Flags = b_prop.Flags = FREEHORIZ | AUTOKNOB;
    r_prop.HorizBody = g_prop.HorizBody = b_prop.HorizBody = 0x1000;
 
    UpDownProp.Flags = FREEVERT | AUTOKNOB;
    UpDownProp.VertBody =  0x1000; /* calc from height */
    L_R_Prop.Flags = FREEHORIZ | AUTOKNOB;
    L_R_Prop.HorizBody =  0x1000; /* calc from width */
    maxpot = (FLOAT)0xFFFF;
 
    /* modify image size parameters based on the input image */
    if(ModImage)
      { imageHeight = MAX(imageHeight,ModImage->Height);
        imageWidth = MAX(imageWidth,ModImage->Width);
        imageDepth = MAX(imageDepth,ModImage->Depth);
      }
 
    /* allocate the work rasters */
    if (initrasters((LONG)imageWidth,(LONG)imageHeight,(LONG)imageDepth)
         == FALSE)
        return (NULL); /* quit */
 
#ifdef DEBUG
    kprintf("Width %d, Height %d, Depth %d",
            imageWidth,imageHeight,imageDepth);
#endif
 
    /* Figure out whether this is a custom screen by */
    /* peeking at the screen's bitmap                */
    if ((screen != NULL)
         &&(screen->RastPort.BitMap->Depth > 2))
       gwind.Type = CUSTOMSCREEN;
    gwind.Screen = screen;
 
    /* Try opening up the editor window */
    if((edwin = (struct Window *)OpenWindow(&gwind)) == NULL)
      {
        FreeEdRas((LONG)imageWidth,(LONG)imageHeight,(LONG)imageDepth);
        FreeColorMap(cmap);
        return(NULL);
      }
 
    /* set the viewport address  */
    vport = (struct ViewPort *)ViewPortAddress(edwin);
 
    /* copy the default colortable */
    cmap = vport->ColorMap;
    ctabptr = (UWORD *) cmap->ColorTable;
    for (i=0; i < 32; i++)
        defaultCT[i] = CTable[i] = ctabptr[i];
 
    /* set the main rastports */
    edrp = edwin->RPort;
 
    /* initialize the palette */
    initpalette(edrp,imageDepth);
 
    /* add the brush color box */
    DrawBorder(edrp, &boxborder,0L,0L);
 
    /* set the menu strip  */
    SetMenuStrip(edwin,im_Titles);
 
    /* initialize stuff for the main loop */
    go_on = zap_props = TRUE;
    cancel = FALSE;
    showsmallim = FALSE;
    magnification = 0; /* start with pixel image size at 1:1 */
    bigmax_x = FRAMERIGHT;
    bigmax_y = FRAMEBOT;
    smallmax_x = imageWidth-1;
    smallmax_y = imageHeight-1;
    current_x = 0;
    current_y = 0;
 
    /* set the image clipping variables */
    setclip(edrp,&bigmax_x, &bigmax_y,magnification,imageWidth,imageHeight);
    setprops(edwin,magnification,imageWidth,imageHeight);
 
    /* clear out the small raster work area */
    curpen = 0;
    SetAPen(&smallraster,curpen);
    RectFill(&smallraster,0L,0L,(LONG)smallmax_x,(LONG)smallmax_y);
    SetAPen(&smallraster,1L);
    /* If there is an input image, draw it into the small raster */
    if (ModImage)
        {
          DrawImage(&smallraster,ModImage,0L,0L);
          redrawim (&smallraster, edrp,
                /* src x */ 0,
                /* src y */ 0,
                /* max x */ smallmax_x,
                /* max y */ smallmax_y,
                /* dest width */
                FRAMEWIDTH,
                /* dest height*/
                FRAMEHEIGHT,0,CLEARIM);
 
           setclip(edrp,&bigmax_x,&bigmax_y,
                magnification,
                imageWidth - current_x,
                imageHeight - current_y);
         }
    curpen = 1; /*set preselected color for the pen */
    SetAPen(edrp,curpen);
    SetAPen(&smallraster,curpen); /* got to keep these in sync */
    RectFill(edrp,450L,135L,610L,149L); /* fill in brush color box */
    PrintIText(edrp,&brushtxt,1L,1L);
    RefreshGadgets(&recttoolGadg,edwin,NULL);
 
    /* set the initial drawing tool */
    curtool = FREEHTOOL;
    prevtoolgad = &freehandGadg;
    SetDrMd(edrp,COMPLEMENT);
    RectFill(edrp,
               (LONG)prevtoolgad->LeftEdge-2,
               (LONG)prevtoolgad->TopEdge-1,
               (LONG)prevtoolgad->LeftEdge+52,
               (LONG)prevtoolgad->TopEdge+26);
    SetDrMd(edrp,JAM1);
 
    circflag = 0;
    /* generally don't want to deluged with mouse reports, so... */
    ReportMouse(edwin,FALSE);
    mousetrap = FALSE;
 
/* The main loop */
while (go_on)
    {
 
    /* zap the prop gadgets to reflect the current color value */
    if (zap_props)
      { r_prop.HorizPot = (CTable[curpen] & 0xf00) << 4;
        g_prop.HorizPot = (CTable[curpen] & 0x0f0) << 8;
        b_prop.HorizPot = CTable[curpen] << 12;
        RefreshGadgets(&red_control,edwin,NULL);
        zap_props = FALSE;
       }
 
   wait(1L << edwin->UserPort->mp_SigBit);
 
   while (inmsg =(struct IntuiMessage *)GetMsg(edwin->UserPort))
      {  class = inmsg->Class;
         code  = inmsg->Code;
         /* catch the gadget */
         curgadget = (struct Gadget *)inmsg->IAddress;
         mousex = inmsg->MouseX;
         mousey = inmsg->MouseY;
         ReplyMsg(inmsg);
 
 
         selected = curgadget->GadgetID;
         switch (class)
           { case MOUSEMOVE:
                if(inbox(mousex,mousey,
                FRAMELEFT,FRAMETOP,bigmax_x,bigmax_y) == 0) break;
 
                switch (curtool) /* When a drawing tool is being used */
                   {             /* draw a complemented outline of it */
                    case LINETOOL:
                             im_draw_line(edrp,anchorx,anchory,lastx,lasty,
                                          curpen,-1);
                             im_draw_line(edrp,anchorx,anchory,mousex,mousey,
                                          curpen,-1);
                             lastx = mousex;
                             lasty = mousey;
                             break;
 
                    case BOXTOOL:
                    case FILLEDBOX:
                             im_draw_box(edrp,anchorx,anchory,
                                        lastx-anchorx,lasty-anchory,
                                         curpen,-1);
 
                             im_draw_box(edrp,anchorx,anchory,
                                        mousex-anchorx,mousey-anchory,
                                         curpen,-1);
                             lastx = mousex;
                             lasty = mousey;
 
                             break;
 
                    case CIRCLETOOL:
                             /* for speed only draw alternate circles */
 
                           if (circflag)
                             { im_draw_circ(edrp,anchorx,anchory,anchorx-lastx,
                                          anchory-lasty,curpen,-1);
                               im_draw_circ(edrp,anchorx,anchory,anchorx-mousex,
                                          anchory-mousey,curpen,-1);
                               lastx = mousex;
                               lasty = mousey;
                               circflag = ~circflag;/* toggle the flag */
                              }
                            else circflag = ~circflag;
                            break;
 
                   }
 
                if (curtool!=FREEHTOOL) break;
                if (ReadPixel(edrp,(LONG)mousex,(LONG)mousey) == curpen)
                  break; /* ignore if the mouse is over the same color*/
                         /* otherwise treat a move like GADGETDOWN    */
                mousetrap = TRUE;
 
             case GADGETDOWN:
                if (selected == BIGEDG || class == MOUSEMOVE)
                  {
                   if(curtool)  /* if not just drawing pixels */
                     { /* ignore if outside drawing area */
                       if(inbox(mousex,mousey,
                                FRAMELEFT,FRAMETOP,
                                bigmax_x,bigmax_y) == 0) break;
                        /* set the "anchor point" for the tool*/
                       anchorx = mousex;
                       anchory = mousey;
                       lastx = mousex;
                       lasty = mousey;
                       ReportMouse(edwin,TRUE);
                       break; /* rest of the work is done mousemove and */
                              /* gadgetup                               */
                     }
                   /* set editing area 'pixel' when doing freehand stuff*/
                   bigpixel(edrp,mousex,mousey,magnification,
                          bigmax_x, bigmax_y);
 
                   /* set offscreen small bitmap pixel */
                   smallpixel(&smallraster,mousex,mousey,magnification,
                            smallmax_x, smallmax_y, current_x, current_y);
                                           /* offsets */
                   if(showsmallim)
                     {
                        /* set onscreen small pixel */
                        temp_x =((mousex - FRAMELEFT)/(1<<magnification))+SMALLL
EFT;
                        temp_y =((mousey - FRAMETOP)/(1<<magnification))+SMALLTO
P;
 
                      if (inbox( temp_x, temp_y, /* clip the small image */
                        SMALLLEFT,SMALLTOP,SMALLRIGHT,SMALLBOT) == 1)
                        smallpixel(edrp,mousex,mousey,magnification,
                            smallmax_x-current_x, smallmax_y-current_y,
                            SMALLLEFT + current_x,
                            SMALLTOP + current_y);
                                           /* offsets */
                     }
                   /* tell me about mouse movements until the */
                   /* gadget is released                      */
                   ReportMouse(edwin,TRUE);
                  }
 
                 break;
 
             case GADGETUP:
                 switch (selected)
                   {
                    case BIGEDG:
                        /* enough of the mouse tracks */
                        ReportMouse(edwin,FALSE);
                        mousetrap = FALSE;
 
                        switch (curtool)
                          {
                            case LINETOOL:
                                     /* remove the last complement line */
                                     im_draw_line(edrp,anchorx,anchory,
                                        lastx,lasty,
                                         curpen,-1);
 
                                     /* if button was released outside the */
                                     /* drawing area 'clip' the whole image*/
                                     if(inbox(mousex,mousey,
                                        FRAMELEFT,FRAMETOP,
                                        bigmax_x,bigmax_y) == 0) break;
 
                                     /* otherwise write it into the small ras*/
                                     smalline(&smallraster,
                                              anchorx,anchory,
                                              mousex,mousey,magnification,
                                              smallmax_x, smallmax_y,
                                              current_x, current_y,curpen);
                                                /* offsets */
                                     break;
 
                            case BOXTOOL:
                            case FILLEDBOX:
                                   /* remove the last complement box */
                                   im_draw_box(edrp,anchorx,anchory,
                                         lastx-anchorx,lasty-anchory,
                                         curpen,-1);
 
                                     /* clip if mouse is out of drawing area */
                                     if(inbox(mousex,mousey,
                                        FRAMELEFT,FRAMETOP,
                                        bigmax_x,bigmax_y) == 0) break;
 
                                    /* otherwise render the box into the */
                                    /* small raster.                     */
                                    if (curtool == BOXTOOL)
                                       smallbox(&smallraster,
                                              anchorx,anchory,
                                              mousex-anchorx,mousey-anchory,
                                              magnification,
                                              smallmax_x, smallmax_y,
                                              current_x, current_y,curpen);
                                                /* offsets */
                                    if (curtool == FILLEDBOX)
                                       smallrect(&smallraster,
                                              anchorx,anchory,
                                              mousex,mousey,
                                              magnification,
                                              smallmax_x, smallmax_y,
                                              current_x, current_y,curpen);
                                                /* offsets */
 
                                     break;
 
                            case CIRCLETOOL:
                                     /* remove the last complement circle */
                                     im_draw_circ(edrp,anchorx,anchory,
                                        anchorx-lastx, anchory-lasty,curpen,-1);
 
                                     /* clip if the mouse is outside the */
                                     /* drawing area                     */
                                     if(inbox(mousex,mousey,
                                        FRAMELEFT,FRAMETOP,
                                        bigmax_x,bigmax_y) == 0) break;
 
                                     /* write the circle into the small ras */
                                     smallcirc(&smallraster,
                                              anchorx,anchory,
                                              anchorx-mousex,anchory-mousey,
                                              magnification,
                                              smallmax_x, smallmax_y,
                                              current_x, current_y,curpen);
                                                /* offsets */
                                     break;
 
 
                                case FILLTOOL:
                                     /* clip if mouse is out of drawing area */
                                     if(inbox(mousex,mousey,
                                        FRAMELEFT,FRAMETOP,
                                        bigmax_x,bigmax_y) == 0) break;
 
                                     /* fill the area */
                                     smallfill(&smallraster,mousex,mousey,
                                           magnification,current_x,current_y);
 
                                     break;
 
                           }
                           /* if the user is drawing freehand we're done */
                           if (curtool == FREEHTOOL) break;
 
                           /* otherwise copy the small image to the edit */
                           /* area on the edit screen                    */
                           SetDrMd(edrp,JAM1);
                           redrawim (&smallraster, edrp,
                                       /* src x */ current_x ,
                                       /* src y */ current_y ,
                                       /* max x */ smallmax_x,
                                       /* max y */ smallmax_y,
                                       /* dest width */
                                       FRAMEWIDTH,
                                       /* dest height*/
                                       FRAMEHEIGHT,
                                       magnification,
                                        (curpen ? NOCLEARIM:CLEARIM));
 
                            SetAPen(edrp,curpen);
                            SetAPen(&smallraster,curpen);
 
                            /* If the small image option is in effect, blit */
                            /* the new image over from the small raster     */
                            if(showsmallim)
                                ClipBlit (&smallraster,0L,0L,
                                        edrp,(LONG)SMALLLEFT,(LONG)SMALLTOP,
                                        (LONG)smallimW,
                                        (LONG)smallimH,
                                        (LONG)0xc0 /* copy all planes */);
 
 
                        break;
 
                    case PALETTEG:
                        /* ignore if the mouse is over the same color*/
                        if(( color = (LONG)
                             ReadPixel(edrp,(LONG)mousex,(LONG)mousey))
                             == curpen) break;
                        SetAPen(edrp,color);
                        SetAPen(&smallraster,color);
                        curpen = color;
                        /* fill in brush color box */
                        RectFill(edrp,450L,135L,610L,149L);
                        PrintIText(edrp,&brushtxt,1L,1L);
                        zap_props = TRUE;
                        break;
 
                   case UPDOWNG:   /* shift image up or down */
                        /* figure out how far up or down they moved */
                        flpot = (FLOAT)UpDownProp.VertPot;
                        fltemp = flpot/maxpot;
                        fltemp = (FLOAT)imageHeight * fltemp;
                        temp_y = (SHORT)fltemp;
                        if(temp_y == current_y) break;
                        current_y = temp_y;
 
                        /* redraw the editing image from the small image      */
                        /* at the current magnification with the new position */
                        SetDrMd(edrp,JAM1); /* just in case */
                        redrawim (&smallraster, edrp,
                           /* src x */ current_x ,
                           /* src y */ current_y ,
                           /* max x */ smallmax_x,
                           /* max y */ smallmax_y,
                           /* dest width */
                               FRAMEWIDTH,
                           /* dest height*/
                               FRAMEHEIGHT,
                               magnification,CLEARIM);
 
                        /* set the clipping variables and outline of the new */
                        /* editing area.                                     */
 
                        setclip(edrp,&bigmax_x,&bigmax_y,
                                magnification,
                                imageWidth - current_x,
                                imageHeight - current_y);
                        SetAPen(edrp,curpen);
                        SetAPen(&smallraster,curpen);
                        break;
 
 
                  case LEFTRIGHTG:   /* shift image left or right */
                        /* same idea as updown */
                        flpot = (FLOAT)L_R_Prop.HorizPot;
                        fltemp = flpot/maxpot;
                        fltemp = (FLOAT)imageWidth * fltemp;
                        temp_x = (SHORT)fltemp;
 
                        if(temp_x == current_x) break;
                        current_x = temp_x;
                        SetDrMd(edrp,JAM1);
                        redrawim (&smallraster, edrp,
                                /* src x */ current_x ,
                                /* src y */ current_y ,
                                /* max x */ smallmax_x,
                                /* max y */ smallmax_y,
                                /* dest width */
                                        FRAMEWIDTH,
                                /* dest height*/
                                        FRAMEHEIGHT,
                                        magnification,CLEARIM);
 
                        setclip(edrp,&bigmax_x,&bigmax_y,
                                magnification,
                                imageWidth - current_x,
                                imageHeight - current_y);
 
                        SetAPen(edrp,curpen);
                        SetAPen(&smallraster,curpen);
                        break;
 
                   case FREETG:
                   case LINETG:
                   case BOXTG:
                   case CIRCTG:
                   case FILLBOXTG:
                        /* do some 'mutual exclude" highlighting */
                        SetDrMd(edrp,COMPLEMENT);
                        RectFill(edrp,
                                (LONG)prevtoolgad->LeftEdge-2,
                                (LONG)prevtoolgad->TopEdge-1,
                                (LONG)prevtoolgad->LeftEdge+52,
                                (LONG)prevtoolgad->TopEdge+26);
                        RectFill(edrp,
                                (LONG)curgadget->LeftEdge-2,
                                (LONG)curgadget->TopEdge-1,
                                (LONG)curgadget->LeftEdge+52,
                                (LONG)curgadget->TopEdge+26);
                        SetDrMd(edrp,JAM1);
 
                        prevtoolgad = curgadget;
                        curtool = selected - 11; /* which gadget */
                        /* set the proper ckeckmark in the tools menu */
                        ClearMenuStrip(edwin);
                        for (i=8; i<=13; i++) im_Items[i].Flags &= ~CHECKED;
                        im_Items[selected-3].Flags |= CHECKED;
 
                        /* reset the menu strip  */
                        SetMenuStrip(edwin,im_Titles);
 
                        break;
 
                   case BLUECTLG:
                   case GREENCTLG:
                   case REDCTLG:
                        /*Set the colors*/
                        SetRGB4(vport,curpen,
                                (LONG)(r_prop.HorizPot >> 12),
                                (LONG)(g_prop.HorizPot >> 12),
                                (LONG)(b_prop.HorizPot >> 12));
                        /* save color in CTable  - can be output */
                        /* if the user wants a custom color table*/
                        CTable[curpen] =
                                ((r_prop.HorizPot >> 4) & 0xf00) +
                                ((g_prop.HorizPot >> 8) & 0xf0) +
                                (b_prop.HorizPot >> 12);
                         break;
 
 
                   case MAGMINUSG:
                        if (magnification == 0) break;
                        else /* shrink the image */
                            { magnification--;
                               SetDrMd(edrp,JAM1);
                               redrawim (&smallraster, edrp,
                                       /* src x */ current_x ,
                                       /* src y */ current_y ,
                                       /* max x */ smallmax_x,
                                       /* max y */ smallmax_y,
                                       /* dest width */
                                       FRAMEWIDTH,
                                       /* dest height*/
                                       FRAMEHEIGHT,
                                       magnification,CLEARIM);
 
                             setclip(edrp,&bigmax_x,&bigmax_y,
                                     magnification,
                                     imageWidth - current_x,
                                     imageHeight - current_y);
 
                             /* make the propgadgets show the amount of */
 
                             /* the image displayed                     */
                             setprops(edwin,
                                        magnification,
                                        imageWidth,imageHeight);
                             SetAPen(edrp,curpen);
                             SetAPen(&smallraster,curpen);
                           }
                        break;
 
                  case MAGPLUSG:
                        if (magnification == 5 ) break;
                        else /* make the image grow */
                           { magnification++;
                             SetDrMd(edrp,JAM1);
                             redrawim (&smallraster, edrp,
                                       /* src x */ current_x,
                                       /* src y */ current_y,
                                       /* max x */ smallmax_x,
                                       /* max y */ smallmax_y,
                                       /* dest width */
                                       FRAMEWIDTH,
                                       /* dest height*/
                                       FRAMEHEIGHT,
                                       magnification,CLEARIM);
                             setclip(edrp,&bigmax_x,&bigmax_y,
                                     magnification,
                                     imageWidth - current_x,
                                     imageHeight - current_y);
                             setprops(edwin,
                                        magnification,
                                        imageWidth,imageHeight);
                             SetAPen(edrp,curpen);
                             SetAPen(&smallraster,curpen);
 
                           }
 
                        break;
                        }
 
                     break;
 
                case CLOSEWINDOW:
                    /* treated like choosing RETURN from the menus */
                    go_on = FALSE;
                    break;
 
                case MENUPICK:
                  if(MENUNUM(code) == MENUNULL) break;
                  switch (MENUNUM(code))
                    { case PROJMEN:
                         switch (ITEMNUM(code))
                           {
                             case RETURNM:
                                  go_on = FALSE;
                                  break;
 
                             case CANCELM: /* don't send back the image */
                                  go_on = FALSE;
                                  cancel = TRUE;
                                  break;
                            }
 
                          break;
 
                      case EDITMEN:
                       switch (ITEMNUM(code))
                        {
                         case CLREDWIN: /* clear the editing window */
                              temp_x = (FRAMEWIDTH)/(1<<magnification);
                              temp_y = (FRAMEHEIGHT)/(1<<magnification);
                              if ((temp_x +current_x) > smallmax_x)
                                 temp_x = smallmax_x - current_x;
                              if ((temp_y +current_y) > smallmax_y)
                                 temp_y = smallmax_y - current_y;
 
                              /* clear the image drawing area */
                              SetAPen(edrp,0L);
                              RectFill(edrp,((LONG)FRAMELEFT),((LONG)FRAMETOP),
                                        ((LONG)FRAMERIGHT),((LONG)FRAMEBOT));
                              /* clear the offscreen raster */
                              SetAPen(&smallraster,0L);
                              RectFill(&smallraster,(LONG)current_x,
                                        (LONG)current_y,
                                        (LONG)current_x + temp_x,
                                        (LONG)current_y + temp_y);
                              if (showsmallim)
                                 RectFill(edrp,
                                        (LONG)SMALLLEFT+current_x,
                                        (LONG)SMALLTOP+current_y,
                                        (LONG)SMALLLEFT+current_x+temp_x,
                                        (LONG)SMALLTOP+current_y+temp_y);
 
                              setclip(edrp,&bigmax_x,&bigmax_y,
                                magnification,
                                imageWidth - current_x,
                                imageHeight - current_y);
 
 
                              SetAPen(edrp,curpen);
                              SetAPen(&smallraster,curpen);
                              break;
 
                         case CLREDALL:
                              /* clear the entire image  */
                              SetAPen(edrp,0L);
                              RectFill(edrp,((LONG)FRAMELEFT),((LONG)FRAMETOP),
                                        ((LONG)FRAMERIGHT),((LONG)FRAMEBOT));
                              /* clear the offscreen raster */
                              SetAPen(&smallraster,0L);
                              RectFill(&smallraster,0L,0L,
                                        (LONG)smallmax_x,(LONG)smallmax_y);
                              /* if the small image is active clear it too */
                              if (showsmallim)
                                RectFill(edrp,(LONG)SMALLLEFT,(LONG)SMALLTOP,
                                        (LONG)SMALLRIGHT,(LONG)SMALLBOT);
 
                              setclip(edrp,&bigmax_x,&bigmax_y,
                                magnification,
                                imageWidth - current_x,
                                imageHeight - current_y);
 
 
                              SetAPen(edrp,curpen);
                              SetAPen(&smallraster,curpen);
                              break;
 
                         case RESETIMAG:
                              /* restore the input image (if there is one) */
 
                              /* first clear out the small raster work area*/
                              SetAPen(&smallraster,0L);
                              RectFill(&smallraster,0L,0L,
                                        (LONG)smallmax_x,(LONG)smallmax_y);
 
                              /* If there is an input image,
                                draw it into the small raster */
                             if (ModImage)
                                 DrawImage(&smallraster,ModImage,0L,0L);
                             SetDrMd(edrp,JAM1);
                             redrawim (&smallraster, edrp,
                                        /* src x */ current_x,
                                        /* src y */ current_y,
                                        /* max x */ smallmax_x,
                                        /* max y */ smallmax_y,
                                        /* dest width */
                                        FRAMEWIDTH,
                                        /* dest height*/
                                        FRAMEHEIGHT,magnification,CLEARIM);
 
                              setclip(edrp,&bigmax_x,&bigmax_y,
                                        magnification,
                                        imageWidth - current_x,
                                        imageHeight - current_y);
 
                              if (showsmallim)
                                { SetAPen(edrp,0L);
                                  RectFill(edrp,
                                        (LONG)SMALLLEFT,
                                        (LONG)SMALLTOP,
                                        (LONG)SMALLRIGHT,
                                        (LONG)SMALLBOT);
                                  ClipBlit (&smallraster,0L,0L,
                                        edrp,(LONG)SMALLLEFT,(LONG)SMALLTOP,
                                        (LONG)smallimW,
                                        (LONG)smallimH,
                                        (LONG)0xc0 /* copy all planes */);
                                }
 
                              SetAPen(edrp,curpen);
                              SetAPen(&smallraster,curpen);
                              break;
 
                         case RESETCOLOR:
                              zap_props = TRUE;
                              /* change the registers to default colors */
                              LoadRGB4(vport,&defaultCT,32L);
 
                              /* set the default brush color, etc. */
                              curpen = 1;
                              SetAPen(edrp,curpen);
                              SetAPen(&smallraster,curpen);
                              RectFill(edrp,450L,135L,610L,149L);
                              PrintIText(edrp,&brushtxt,1L,1L);
                              break;
                        }
 
 
                     break;
 
                case OPTMEN:
                     switch (ITEMNUM(code))
                       {
 
                        case SHOWSMALL:
                             /* remove the tool gadgets to give room */
                             RemoveGadget(edwin,&recttoolGadg);
                             RemoveGadget(edwin,&circtoolGadg);
                             RemoveGadget(edwin,&boxtoolGadg);
                             RemoveGadget(edwin,&linetoolGadg);
                             RemoveGadget(edwin,&freehandGadg);
 
                             /* clear the image area */
                             SetAPen(edrp,0L);
                             RectFill(edrp,
                                (LONG)SMALLLEFT-4,
                                (LONG)SMALLTOP-2,
                                (LONG)SMALLRIGHT+4,
                                (LONG)SMALLBOT+2);
                             SetAPen(edrp,curpen);
 
                             /* add the box showing the real-size image */
                             DrawBorder(edrp, &smallimborder,0L,0L);
                             smallimW = MIN(imageWidth,SMALLWIDTH);
                             smallimH = MIN(imageHeight,SMALLHEIGHT);
                             ClipBlit (&smallraster,0L,0L,
                                 edrp,(LONG)SMALLLEFT,(LONG)SMALLTOP,
                                (LONG)smallimW,
                                (LONG)smallimH,
                                (LONG)0xc0 /* copy all planes */);
                             showsmallim = TRUE;
                             break;
 
                        case HIDESMALL: /* remove the real-size image */
                             SetAPen(edrp,0L);
                             RectFill(edrp,
                                (LONG)SMALLLEFT-2,
                                (LONG)SMALLTOP-2,
                                (LONG)SMALLRIGHT+2,
                                (LONG)SMALLBOT+2);
                             showsmallim = FALSE;
                             SetAPen(edrp,curpen);
 
                             /* replace the tool gadgets */
                             AddGadget(edwin,&freehandGadg,1L);
                             AddGadget(edwin,&linetoolGadg,1L);
                             AddGadget(edwin,&boxtoolGadg,1L);
                             AddGadget(edwin,&circtoolGadg,1L);
                             AddGadget(edwin,&recttoolGadg,1L);
                             RefreshGadgets(&recttoolGadg,edwin,NULL);
 
                             /* do some 'mutual exclude" highlighting */
                                SetDrMd(edrp,COMPLEMENT);
                                RectFill(edrp,
                                        (LONG)prevtoolgad->LeftEdge-2,
                                        (LONG)prevtoolgad->TopEdge-1,
                                        (LONG)prevtoolgad->LeftEdge+52,
                                        (LONG)prevtoolgad->TopEdge+26);
                                SetDrMd(edrp,JAM1);
 
                             break;
                        }
                     break;
 
 
                case TOOLMEN:/* set the current drawing tool type */
                     curtool = ITEMNUM(code);
                     switch (ITEMNUM(code))
                        {
                          case FREEHTOOL:
                                curgadget = &freehandGadg;
                                break;
                          case LINETOOL:
                                curgadget = &linetoolGadg;
                                break;
                          case BOXTOOL:
                                curgadget = &boxtoolGadg;
                                break;
                          case CIRCLETOOL:
                                curgadget = &circtoolGadg;
                                break;
                          case FILLEDBOX:
                                curgadget = &recttoolGadg;
                                break;
 
                        }
                        /* do some 'mutual exclude" highlighting */
                        if (showsmallim) break;
 
                        SetDrMd(edrp,COMPLEMENT);
                        RectFill(edrp,
                                (LONG)prevtoolgad->LeftEdge-2,
                                (LONG)prevtoolgad->TopEdge-1,
                                (LONG)prevtoolgad->LeftEdge+52,
                                (LONG)prevtoolgad->TopEdge+26);
                        RectFill(edrp,
                                (LONG)curgadget->LeftEdge-2,
                                (LONG)curgadget->TopEdge-1,
                                (LONG)curgadget->LeftEdge+52,
                                (LONG)curgadget->TopEdge+26);
                        SetDrMd(edrp,JAM1);
 
                        prevtoolgad = curgadget;
 
                     break;
 
                } /* end of menunum switch */
            } /* end of class switch */
         } /* end of GetMsg while loop */
      } /* end while-go_on  main loop */
 
    /* create the image structure and transfer the bitmap */
    /* unless the user chose to cancel.                   */
    if (!cancel) outImage = bitmapTOimage(&smallbitmap,imageWidth,
                        imageHeight, imageDepth);
    else outImage = NULL;
 
    /* give back the space used for the small raster */
    FreeEdRas((LONG)imageWidth,(LONG)imageHeight,(LONG)imageDepth);
 
    /* change the registers back to default colors              */
    LoadRGB4(vport,&defaultCT,32L);
    /* NOTE - the calling program may want to set               */
    /* the colors again, do so by... LoadRGB4(vport,&CTable,32L)*/
    /* where vport is the viewport for the window. CTable is    */
    /* an external array declared in imagdcl.c                  */
 
    ClearMenuStrip(edwin);
    CloseWindow(edwin);
    return(outImage);
   } /* end of Image_Ed */