/*
**      $VER: stscan.c 3.01D (16.8.97)
**
**      STScan main module
**
**      Written by Frank-Christian Kruegel, Henning Peters, Andreas R. Kleinert
**      GNU General Public License V2
*/

#include "stscan.h"

#define PRG_VERSION (UBYTE *)"Version 3.01D"
#define ST400ID 3
#define PREFNAME "PROGDIR:StScan.pref"

const char version[]="\0$VER: StScan 3.01D ("__DATE__")\0";

char *_procname="StScan";
long _BackGroundIO=0;
long _stack=50000;
long _priority=0;

struct Window *win;
struct IOStdReq *diskreq;
struct ViewPort *vp;
struct RastPort *rp;
ULONG memneed;
UWORD memwidth, memheight;
UWORD membpl; /*needed for thinning*/
UBYTE memgray;
UBYTE *memptr;
UBYTE *bufptr;
UWORD screenwidth,screenheight,viewwidth,viewheight;
ULONG viewoffset,lineoffset;
UBYTE numcols;

static UBYTE mf,cmp;
static struct RastPort *wrp;
struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct DosBase *DosBase;
struct ReqToolsBase *ReqToolsBase;
static struct IntuiMessage *Message;
static struct Screen *scr;
static struct MsgPort *diskport;
static long openerror;
static UBYTE noscan;
static UWORD *imgptrf, *imgptrz;
static UWORD wx1,wy1,wx2,wy2;
static UBYTE numbits_16[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
static struct DimensionInfo diminfo;

static int pal2024=0;
static int laceon=0;
static int screenmode=0;
static char scsidevice[50];
static const char defaultdevice[]="scsi.device\000";

UBYTE numbits[256];

UBYTE graystep4[]={0,12,8,4};
UBYTE graystep16[]={0,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
UBYTE pal4_2024[4][3]={  { 0, 0, 0},{15,15,15},{ 8, 0, 8},{ 0, 0, 8}};
UBYTE pal16_2024[16][3]={{ 0, 0, 0},{15,15,15},{15,15,15},{15,15, 8},
                         {15,15, 8},{ 8, 0, 9},{ 8, 0, 9},{ 8, 0, 8},
                         { 8, 0, 8},{ 0, 8, 8},{ 0, 8, 8},{ 0, 0, 9},
                         { 0, 0, 9},{ 0, 0, 8},{ 0, 0, 8},{ 0, 0, 0}};
UBYTE a2024step[]={0,15,4,8};
UBYTE bitval[]={128,64,32,16,8,4,2,1};
UBYTE invbitval[]={127,191,223,239,247,251,253,254};
UBYTE revbit[]={
   0, 128,  64, 192,  32, 160,  96, 224,  16, 144,  80, 208,  48, 176, 112, 240,
   8, 136,  72, 200,  40, 168, 104, 232,  24, 152,  88, 216,  56, 184, 120, 248,
   4, 132,  68, 196,  36, 164, 100, 228,  20, 148,  84, 212,  52, 180, 116, 244,
  12, 140,  76, 204,  44, 172, 108, 236,  28, 156,  92, 220,  60, 188, 124, 252,
   2, 130,  66, 194,  34, 162,  98, 226,  18, 146,  82, 210,  50, 178, 114, 242,
  10, 138,  74, 202,  42, 170, 106, 234,  26, 154,  90, 218,  58, 186, 122, 250,
   6, 134,  70, 198,  38, 166, 102, 230,  22, 150,  86, 214,  54, 182, 118, 246,
  14, 142,  78, 206,  46, 174, 110, 238,  30, 158,  94, 222,  62, 190, 126, 254,
   1, 129,  65, 193,  33, 161,  97, 225,  17, 145,  81, 209,  49, 177, 113, 241,
   9, 137,  73, 201,  41, 169, 105, 233,  25, 153,  89, 217,  57, 185, 121, 249,
   5, 133,  69, 197,  37, 165, 101, 229,  21, 149,  85, 213,  53, 181, 117, 245,
  13, 141,  77, 205,  45, 173, 109, 237,  29, 157,  93, 221,  61, 189, 125, 253,
   3, 131,  67, 195,  35, 163,  99, 227,  19, 147,  83, 211,  51, 179, 115, 243,
  11, 139,  75, 203,  43, 171, 107, 235,  27, 155,  91, 219,  59, 187, 123, 251,
   7, 135,  71, 199,  39, 167, 103, 231,  23, 151,  87, 215,  55, 183, 119, 247,
  15, 143,  79, 207,  47, 175, 111, 239,  31, 159,  95, 223,  63, 191, 127, 255
};

static ULONG viewmodes[]=
{ HIRES_KEY, SUPER_KEY,
  NTSC_MONITOR_ID|HIRES_KEY, NTSC_MONITOR_ID|SUPER_KEY,
  PAL_MONITOR_ID|HIRES_KEY, PAL_MONITOR_ID|SUPER_KEY,
  VGAPRODUCT_KEY,A2024TENHERTZ_KEY
};
static UBYTE modecols[]=
{ 16,4,
  16,4,
  16,4,
  4,4
};

struct DefWindow winpar= /*default values */
{  0,0,32,      /*dummy1, dummy2, wpsize*/
   1,0,         /*winnr, dummy3*/
   200,200,     /*resx, resy*/
   0,0,0,0,     /*cornerx, cornery, width, height*/
   0,32,6,      /*dummy4, threshold, size*/
   0,1,         /*halftone, bitspixel*/
   0,0,0        /*dummy5,6,7*/
};

UWORD imagedataf[]=
{ 0xfff0,
  0xfff0,
  0xc030,
  0xdff0,
  0xdff0,
  0xc0f0,
  0xdff0,
  0xdff0,
  0xdff0,
  0xdff0,
  0xfff0,
  0xfff0,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000};
UWORD imagedataz[]=
{ 0xfff0,
  0xfff0,
  0xc030,
  0xff70,
  0xfef0,
  0xfdf0,
  0xfbf0,
  0xf7f0,
  0xeff0,
  0xc030,
  0xfff0,
  0xfff0,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000,
  0x0000};

/* Struktures for Windows and Menus */

struct TextAttr textattr=
{ "topaz.font",
   8,
   FS_NORMAL,
   FPF_ROMFONT
};

/* Struktures for Gadgets */

WORD zvect2[]={0,0, 0,12, 12,12, 12,0, 0,0,
                -1,-1, -1,11, 11,11, 11,-1, -1,-1};
WORD zvect1[]={0,0, 0,10, 10,10, 10,0, 0,0};

struct Image fullimage=
{ 0,0,
  16,16,4,
  NULL,
  15,0,
  NULL
};

struct Image zoomimage=
{ 0,0,
  16,16,4,
  NULL,
  15,0,
  NULL
};

struct Border vborder;

struct Border hborder;


struct PropInfo hprop=
{ FREEHORIZ|AUTOKNOB,
  0,0,
  0xffff,0xffff,
  0,0,
  0,0,
  0,0
};

struct PropInfo vprop=
{ FREEVERT|AUTOKNOB,
  0,0,
  0xffff,0xffff,
  0,0,
  0,0,
  0,0
};

struct Gadget hgad=
{ NULL,
  0,371, /*0,VIEWHEIGHT+13*/
  624,15,/*SCREENWIDTH-16,15*/
  0,
  GADGIMMEDIATE|RELVERIFY,
  PROPGADGET,
  &hborder,
  NULL,
  NULL,
  0,
  &hprop,
  2,
  NULL
};

struct Gadget vgad=
{ &hgad,
  625,11, /*SCREENWIDTH-15,11*/
  15,358, /*15, VIEWHEIGHT*/
  0,
  GADGIMMEDIATE|RELVERIFY,
  PROPGADGET,
  &vborder,
  NULL,
  NULL,
  0,
  &vprop,
  1,
  NULL
};

struct Gadget zgad=
{ &vgad,
  627,373, /*SCREENWIDTH-13,VIEWHEIGHT+15*/
  12,12,
  GADGHIMAGE|GADGIMAGE,
  TOGGLESELECT|GADGIMMEDIATE|RELVERIFY,
  BOOLGADGET,
  &fullimage,
  &zoomimage,
  NULL,
  0,
  NULL,
  0,
  NULL
};

/* Struktures for Window and Screen */

UWORD pendata16[]=
{8,1,1,4,12,0,1,0,1,0xFFFF};
UWORD pendata4[]=
{3,1,1,2,3,0,1,0,1, 0xFFFF};

struct NewScreen ns=
{
  0,0,
  640,400, /*SCREENHEIGHT*/
  4,
  8,1,
  HIRES,
  CUSTOMSCREEN,
  &textattr,
  (UBYTE *)"Siemens ST 400 Scanner Program © FChK 1991",
  NULL,
  NULL
};

struct NewWindow nw=
{ 0,10,
  640,388, /*SCREENHEIGHT-12*/
  8,1,
  CLOSEWINDOW|MENUPICK|GADGETUP|GADGETDOWN|MOUSEBUTTONS,
  WINDOWCLOSE|BACKDROP|BORDERLESS|ACTIVATE|SMART_REFRESH,
  &zgad,
  NULL,
  (UBYTE *)"",
  NULL,
  NULL,
  0,0,
  0,0,
  CUSTOMSCREEN
};

/* Menüdaten und -strukturen */


struct IntuiText text627=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   A2024 spezial  ",
  NULL
};

struct IntuiText text626=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   Productivity   ",
  NULL
};

struct IntuiText text625=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   PAL SuperHires ",
  NULL
};

struct IntuiText text624=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   PAL Hires      ",
  NULL
};

struct IntuiText text623=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   NTSC SuperHires",
  NULL
};

struct IntuiText text622=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   NTSC Hires     ",
  NULL
};

struct IntuiText text621=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   SuperHires     ",
  NULL
};

struct IntuiText text620=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   Hires          ",
  NULL
};

struct IntuiText text611=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   aus",
  NULL
};

struct IntuiText text610=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   an ",
  NULL
};

struct IntuiText text131=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   Gepackt ",
  NULL
};

struct IntuiText text130=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   Entpackt",
  NULL
};


struct IntuiText text23f=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   15",
  NULL
};

struct IntuiText text23e=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   14",
  NULL
};

struct IntuiText text23d=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   13",
  NULL
};

struct IntuiText text23c=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   12",
  NULL
};

struct IntuiText text23b=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   11",
  NULL
};

struct IntuiText text23a=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   10",
  NULL
};

struct IntuiText text239=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    9",
  NULL
};

struct IntuiText text238=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    8",
  NULL
};

struct IntuiText text237=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    7",
  NULL
};

struct IntuiText text236=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    6",
  NULL
};

struct IntuiText text235=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    5",
  NULL
};

struct IntuiText text234=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    4",
  NULL
};

struct IntuiText text233=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    3",
  NULL
};

struct IntuiText text232=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    2",
  NULL
};

struct IntuiText text231=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    1",
  NULL
};

struct IntuiText text230=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    0",
  NULL
};

struct IntuiText text221=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   16",
  NULL
};

struct IntuiText text220=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"    2",
  NULL
};

struct IntuiText text212=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   DIN A4",
  NULL
};

struct IntuiText text211=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   DIN A5",
  NULL
};

struct IntuiText text210=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   DIN A6",
  NULL
};

struct IntuiText text202=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   400 DPI",
  NULL
};

struct IntuiText text201=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   300 DPI",
  NULL
};

struct IntuiText text200=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"   200 DPI",
  NULL
};

struct IntuiText text63=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"A2024 Grau ",
  NULL
};

struct IntuiText text62=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Grafikmodus",
  NULL
};

struct IntuiText text61=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Interlace  ",
  NULL
};

struct IntuiText text60=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"SCSI Device",
  NULL
};

struct IntuiText text53=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Ausschneiden",
  NULL
};

struct IntuiText text52=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Spiegeln V",
  NULL
};

struct IntuiText text51=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Spiegeln H",
  NULL
};

struct IntuiText text50=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Invers",
  NULL
};

struct IntuiText text43=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Genauigkeit ",
  NULL
};

struct IntuiText text42=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Vectorisiere",
  NULL
};

struct IntuiText text41=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Ausdünnen   ",
  NULL
};

struct IntuiText text40=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"In Graustufen",
  NULL
};

struct IntuiText text3b=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Median",
  NULL
};

struct IntuiText text3a=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Maximum",
  NULL
};

struct IntuiText text39=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Minimum",
  NULL
};

struct IntuiText text38=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Relief",
  NULL
};

struct IntuiText text37=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Tiefpaß stark",
  NULL
};

struct IntuiText text36=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Hochpaß",
  NULL
};

struct IntuiText text35=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Tiefpaß ",
  NULL
};

struct IntuiText text34=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Kontrastverst.",
  NULL
};

struct IntuiText text33=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Histogramm",
  NULL
};

struct IntuiText text32=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Binarisieren",
  NULL
};

struct IntuiText text31=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Dithern Ord",
  NULL
};

struct IntuiText text30=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Dithern F-S",
  NULL
};

struct IntuiText text23=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Schwellwert",
  NULL
};

struct IntuiText text22=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Graustufen",
  NULL
};

struct IntuiText text21=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Größe",
  NULL
};

struct IntuiText text20=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Auflösung",
  NULL
};

struct IntuiText text16=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Programmende",
  NULL
};

struct IntuiText text15=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Test Scanner",
  NULL
};

struct IntuiText text14=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Wie speichern?",
  NULL
};

struct IntuiText text13=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Speichern IFF",
  NULL
};

struct IntuiText text12=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Laden IFF",
  NULL
};

struct IntuiText text11=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Scan->Disk",
  NULL
};

struct IntuiText text10=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Einscannen",
  NULL
};

struct IntuiText text03=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Nur für privaten Gebrauch",
  NULL
};

struct IntuiText text02=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  PRG_VERSION,
  NULL
};


struct IntuiText text01=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Frank-Christian Krügel",
  NULL
};

struct IntuiText text00=
{ 0,1,
  JAM2,
  1,1,
  &textattr,
  (UBYTE *)"Program geschrieben von",
  NULL
};


struct MenuItem item631=
{ NULL,
  -48,10,
  48,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffd,
  (APTR)&text611,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item630=
{ &item631,
  -48,0,
  48,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffe,
  (APTR)&text610,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item627=
{ NULL,
  -144,70,
  144,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xff7f,
  (APTR)&text627,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item626=
{ &item627,
  -144,60,
  144,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xffbf,
  (APTR)&text626,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item625=
{ &item626,
  -144,50,
  144,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xffdf,
  (APTR)&text625,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item624=
{ &item625,
  -144,40,
  144,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xffef,
  (APTR)&text624,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item623=
{ &item624,
  -144,30,
  144,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfff7,
  (APTR)&text623,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item622=
{ &item623,
  -144,20,
  144,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffb,
  (APTR)&text622,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item621=
{ &item622,
  -144,10,
  144,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffd,
  (APTR)&text621,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item620=
{ &item621,
  -144,0,
  144,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffe,
  (APTR)&text620,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item611=
{ NULL,
  -48,10,
  48,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffd,
  (APTR)&text611,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item610=
{ &item611,
  -48,0,
  48,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffe,
  (APTR)&text610,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item131=
{ NULL,
  112,10,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffd,
  (APTR)&text131,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item130=
{ &item131,
  112,0,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|CHECKED,
  0xfffe,
  (APTR)&text130,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item23f=
{ NULL,
  120,70,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0x7fff,
  (APTR)&text23f,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item23e=
{ &item23f,
  120,60,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xbfff,
  (APTR)&text23e,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item23d=
{ &item23e,
  120,50,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xdfff,
  (APTR)&text23d,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item23c=
{ &item23d,
  120,40,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xefff,
  (APTR)&text23c,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item23b=
{ &item23c,
  120,30,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xf7ff,
  (APTR)&text23b,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item23a=
{ &item23b,
  120,20,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfbff,
  (APTR)&text23a,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item239=
{ &item23a,
  120,10,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfdff,
  (APTR)&text239,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item238=
{ &item239,
  120,0,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|CHECKED,
  0xfeff,
  (APTR)&text238,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item237=
{ &item238,
  80,70,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xff7f,
  (APTR)&text237,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item236=
{ &item237,
  80,60,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xffbf,
  (APTR)&text236,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item235=
{ &item236,
  80,50,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xffdf,
  (APTR)&text235,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item234=
{ &item235,
  80,40,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xffef,
  (APTR)&text234,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item233=
{ &item234,
  80,30,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfff7,
  (APTR)&text233,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item232=
{ &item233,
  80,20,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffb,
  (APTR)&text232,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item231=
{ &item232,
  80,10,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffd,
  (APTR)&text231,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item230=
{ &item231,
  80,0,
  40,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT,
  0xfffe,
  (APTR)&text230,
  NULL,
  0,
  NULL,
  0
};


struct MenuItem item221=
{ NULL,
  80,10,
  80,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|COMMSEQ,
  0xfffd,
  (APTR)&text221,
  NULL,
  'g',
  NULL,
  0
};

struct MenuItem item220=
{ &item221,
  80,0,
  80,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|CHECKED|COMMSEQ,
  0xfffe,
  (APTR)&text220,
  NULL,
  'z',
  NULL,
  0
};


struct MenuItem item212=
{ NULL,
  80,20,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|COMMSEQ,
  0xfffb,
  (APTR)&text212,
  NULL,
  'c',
  NULL,
  0
};

struct MenuItem item211=
{ &item212,
  80,10,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|COMMSEQ,
  0xfffd,
  (APTR)&text211,
  NULL,
  'b',
  NULL,
  0
};

struct MenuItem item210=
{ &item211,
  80,0,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|CHECKED|COMMSEQ,
  0xfffe,
  (APTR)&text210,
  NULL,
  'a',
  NULL,
  0
};


struct MenuItem item202=
{ NULL,
  80,20,
  120,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|COMMSEQ,
  0xfffb,
  (APTR)&text202,
  NULL,
  '4',
  NULL,
  0
};

struct MenuItem item201=
{ &item202,
  80,10,
  120,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|COMMSEQ,
  0xfffd,
  (APTR)&text201,
  NULL,
  '3',
  NULL,
  0
};


struct MenuItem item200=
{ &item201,
  80,0,
  120,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|CHECKIT|CHECKED|COMMSEQ,
  0xfffe,
  (APTR)&text200,
  NULL,
  '2',
  NULL,
  0
};


struct MenuItem item63=
{ NULL,
  0,30,
  88,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text63,
  NULL,
  0,
  &item630,
  0
};

struct MenuItem item62=
{ &item63,
  0,20,
  88,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text62,
  NULL,
  0,
  &item620,
  0
};

struct MenuItem item61=
{ &item62,
  0,10,
  88,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text61,
  NULL,
  0,
  &item610,
  0
};

struct MenuItem item60=
{ &item61,
  0,0,
  88,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text60,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item53=
{ NULL,
  0,30,
  104,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text53,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item52=
{ &item53,
  0,20,
  104,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text52,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item51=
{ &item52,
  0,10,
  104,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text51,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item50=
{ &item51,
  0,0,
  104,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text50,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item43=
{ NULL,
  0,30,
  104,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text43,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item42=
{ &item43,
  0,20,
  104,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text42,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item41=
{ &item42,
  0,10,
  104,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text41,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item40=
{ &item41,
  0,0,
  104,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text40,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item3b=
{ NULL,
  0,110,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text3b,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item3a=
{ &item3b,
  0,100,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text3a,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item39=
{ &item3a,
  0,90,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text39,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item38=
{ &item39,
  0,80,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text38,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item37=
{ &item38,
  0,70,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text37,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item36=
{ &item37,
  0,60,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text36,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item35=
{ &item36,
  0,50,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text35,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item34=
{ &item35,
  0,40,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text34,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item33=
{ &item34,
  0,30,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text33,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item32=
{ &item33,
  0,20,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text32,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item31=
{ &item32,
  0,10,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text31,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item30=
{ &item31,
  0,0,
  112,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text30,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item23=
{ NULL,
  0,30,
  120,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text23,
  NULL,
  0,
  &item230,
  0
};

struct MenuItem item22=
{ &item23,
  0,20,
  120,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text22,
  NULL,
  0,
  &item220,
  0
};

struct MenuItem item21=
{ &item22,
  0,10,
  120,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text21,
  NULL,
  0,
  &item210,
  0
};

struct MenuItem item20=
{ &item21,
  0,0,
  120,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text20,
  NULL,
  0,
  &item200,
  0
};

struct MenuItem item16=
{ NULL,
  0,60,
  176,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|COMMSEQ,
  0,
  (APTR)&text16,
  NULL,
  'x',
  NULL,
  0
};

struct MenuItem item15=
{ &item16,
  0,50,
  176,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|COMMSEQ,
  0,
  (APTR)&text15,
  NULL,
  't',
  NULL,
  0
};

struct MenuItem item14=
{ &item15,
  0,40,
  176,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text14,
  NULL,
  0,
  &item130,
  0
};

struct MenuItem item13=
{ &item14,
  0,30,
  176,10,
  ITEMTEXT|HIGHCOMP|COMMSEQ,
  0,
  (APTR)&text13,
  NULL,
  's',
  NULL,
  0
};

struct MenuItem item12=
{ &item13,
  0,20,
  176,20,
  ITEMTEXT|HIGHCOMP|COMMSEQ|ITEMENABLED,
  0,
  (APTR)&text12,
  NULL,
  'l',
  NULL,
  0
};

struct MenuItem item11=
{ &item12,
  0,10,
  176,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED,
  0,
  (APTR)&text11,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item10=
{ &item11,
  0,0,
  176,10,
  ITEMTEXT|HIGHCOMP|ITEMENABLED|COMMSEQ,
  0,
  (APTR)&text10,
  NULL,
  'r',
  NULL,
  0
};

struct MenuItem item03=
{ NULL,
  0,30,
  160,10,
  ITEMTEXT|HIGHNONE|ITEMENABLED,
  0,
  (APTR)&text03,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item02=
{ &item03,
  0,20,
  160,10,
  ITEMTEXT|HIGHNONE|ITEMENABLED,
  0,
  (APTR)&text02,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item01=
{ &item02,
  0,10,
  160,10,
  ITEMTEXT|HIGHNONE|ITEMENABLED,
  0,
  (APTR)&text01,
  NULL,
  0,
  NULL,
  0
};

struct MenuItem item00=
{ &item01,
  0,0,
  160,10,
  ITEMTEXT|HIGHNONE|ITEMENABLED,
  0,
  (APTR)&text00,
  NULL,
  0,
  NULL,
  0
};

struct Menu menu6=
{ NULL,
  528,0,
  72,8,
  MENUENABLED,
  "Konfig.",
  &item60
};

struct Menu menu5=
{ &menu6,
  432,0,
  80,8,
  NULL,
  "Transform",
  &item50
};

struct Menu menu4=
{ &menu5,
  328,0,
  80,8,
  NULL,
  "SW-Prozeß",
  &item40
};

struct Menu menu3=
{ &menu4,
  216,0,
  96,8,
  NULL,
  "Grau-Prozeß",
  &item30
};

struct Menu menu2=
{ &menu3,
  136,0,
  72,8,
  MENUENABLED,
  "Vorgaben",
  &item20
};

struct Menu menu1=
{ &menu2,
  56,0,
  72,8,
  MENUENABLED,
  "Programm",
  &item10
};

struct Menu menu0=
{ &menu1,
  0,0,
  40,8,
  MENUENABLED,
  "Info",
  &item00
};

void ende(void);
void ende(void)
{ if (memptr) free(memptr);
  if (!(noscan))
  { if((openerror==0)&&(!(diskreq))) CloseDevice((struct IORequest *)diskreq);
    if(diskport)DeletePort(diskport);
    if(diskreq)DeleteStdIO(diskreq);
  }
  if (win) { ClearMenuStrip(win); CloseWindow(win); }
  if (scr) CloseScreen(scr);
  if (bufptr) FreeMem(bufptr,32768);
  if (imgptrf) FreeMem(imgptrf,sizeof(imagedataf));
  if (imgptrz) FreeMem(imgptrz,sizeof(imagedataz));
  if (ReqToolsBase) CloseLibrary((struct Library *)ReqToolsBase);
  if (DosBase) CloseLibrary((struct Library *)DosBase);
  if (GfxBase) CloseLibrary((struct Library *)GfxBase);
  if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
}

void saveprefs(void);
void saveprefs(void)
{ FILE *preffile;
  preffile=fopen(PREFNAME,"w");
  fprintf(preffile,"%s\n%d\n%d\n%d\n",scsidevice,laceon,screenmode,pal2024);
  fclose(preffile);
}

void redisplay(void);
void redisplay(void)
{ if (memneed) view((memwidth>viewwidth)?hprop.HorizPot*(memwidth-viewwidth)/65536:0,
                    (memheight>viewheight)?vprop.VertPot*(memheight-viewheight)/65536:0,
                    (zgad.Flags&SELECTED));
  if (mf==2) drawbox(wx1,wy1,wx2,wy2,wrp);
}

void menu(UWORD Code);
void menu(UWORD Code)
{
  UWORD num,item,subitem;
  num=MENUNUM(Code);
  item=ITEMNUM(Code);
  subitem=SUBNUM(Code);
  switch(num)
  {  case 0: break;                    /* Info */
     case 1: switch(item)              /* Programm */
             { case 0: scan();         /* Einlesen */
                       vprop.VertBody=(zgad.Flags&SELECTED)?0xffff:((memheight>viewheight)?viewheight*65536/memheight:0xffff);
                       hprop.HorizBody=(zgad.Flags&SELECTED)?0xffff:((memwidth>viewwidth)?viewwidth*65536/memwidth:0xffff);
                       vprop.VertPot=0;
                       hprop.HorizPot=0;
                       RefreshGadgets(&zgad,win,NULL);
                       if (memneed)
                       { view(0,0,(zgad.Flags&SELECTED));
                         mf=0;
                       }
                       break;
               case 1: diskscan();
                       break;
               case 2: load();
                       vprop.VertBody=((!(memheight))||(memheight<viewheight)||(zgad.Flags&SELECTED))?0xffff:viewheight*65536/memheight;
                       hprop.HorizBody=((!(memwidth))||(memwidth<viewwidth)||(zgad.Flags&SELECTED))?0xffff:viewwidth*65536/memwidth;
                       vprop.VertPot=0;
                       hprop.HorizPot=0;
                       RefreshGadgets(&zgad,win,NULL);
                       if (memneed)
                       { view(0,0,(zgad.Flags&SELECTED));
                         mf=0;
                       }
                       break;
               case 3: save(wx1,wy1,wx2,wy2,cmp,mf); /* Speichern */
                       break;
               case 4: cmp=subitem;
                       break;
               case 5: inquiry();
                       break;
               case 6: ende();         /* Ende */
                       exit(0L);
                       break;
             }
             break;
     case 2: switch(item)
             { case 0: winpar.resx=100*(subitem+2);
                       winpar.resy=winpar.resx;
                       break;
               case 1: winpar.size=6-subitem;
                       break;
               case 2: winpar.halftone=subitem<<1;
                       winpar.bitspixel=subitem ? 8 : 1;
                       break;
               case 3: winpar.threshold=subitem<<2;
                       break;
             }
             break;
     case 3: switch(item)
             { case 0: if ((memgray)&&(memneed))
                       { floyd();
                         redisplay();
                       }
                       break;
               case 1: if ((memgray)&&(memneed))
                       { ordered();
                         redisplay();
                       }
                       break;
               case 2: if ((memgray)&&(memneed))
                       { thresh();
                         redisplay();
                       }
                       break;
               case 3: if ((memgray)&&(memneed)) viewhisto(wrp);
                       break;
               case 4: if ((memgray)&&(memneed))
                       { stretchhisto();
                         redisplay();
                       }
                       break;
               case 5: if ((memgray)&&(memneed))
                       { lowpass();
                         redisplay();
                       }
                       break;
               case 6: if ((memgray)&&(memneed))
                       { highpass();
                         redisplay();
                       }
                       break;
               case 7: if ((memgray)&&(memneed))
                       { blowpass();
                         redisplay();
                       }
                       break;
               case 8: if ((memgray)&&(memneed))
                       { relief();
                         redisplay();
                       }
                       break;
               case 9: if ((memgray)&&(memneed))
                       { minop();
                         redisplay();
                       }
                       break;
               case 10:if ((memgray)&&(memneed))
                       { maxop();
                         redisplay();
                       }
                       break;
               case 11:if ((memgray)&&(memneed))
                       { median();
                         redisplay();
                       }
                       break;
             }
             break;
     case 4: switch(item)
             { case 0: if ((!(memgray))&&(memneed))
                       { togray();
                         redisplay();
                       }
                       break;
               case 1: if ((!(memgray))&&(memneed))
                       { thin();
                         redisplay();
                       }
                       break;
               case 2: if ((!(memgray))&&(memneed))
                       { vectorize();
                         redisplay();
                       }
                       break;
               case 3: chngmaxdiff();
                       break;
             }
             break;
     case 5: switch(item)
             { case 0: if (memneed)
                       { invert();
                         redisplay();
                       }
                       break;
               case 1: if (memneed)
                       { mirrorh();
                         redisplay();
                       }
                       break;
               case 2: if (memneed)
                       { mirrorv();
                         redisplay();
                       }
                       break;
               case 3: if (memneed)
                       { cut(wx1,wy1,wx2,wy2,mf);
                         hprop.HorizPot=0;
                         vprop.VertPot=0;
                         vprop.VertBody=(zgad.Flags&SELECTED)?0xffff:((viewheight<memheight)?viewheight*65536/memheight:0xffff);
                         hprop.HorizBody=(zgad.Flags&SELECTED)?0xffff:((viewwidth<memwidth)?viewwidth*65536/memwidth:0xffff);
                         RefreshGadgets(&vgad,win,NULL);
                         if (memneed) view(0,0,(zgad.Flags&SELECTED));
                         mf=0;
                       }
                       break;
             }
             break;
     case 6: switch(item)
             { case 0: if (rtGetString(scsidevice,50,"SCSI device name",
                                        NULL, TAG_END))
                         saveprefs();
                       break;
               case 1: laceon=1-subitem;
                       saveprefs();
                       break;
               case 2: screenmode=subitem;
                       saveprefs();
                       break;
               case 3: pal2024=1-subitem;
                       saveprefs();
                       break;
             }
             break;
  }
}

int main(long argc, char **argv)
{ static ULONG MessageClass;
  static UWORD Code;
  UWORD i;
  UWORD mx,my;
  FILE *preffile;
  ULONG display;

  strcpy(scsidevice,defaultdevice);
  preffile=fopen(PREFNAME,"r");
  if (preffile!=NULL)
  { fscanf(preffile,"%s",&scsidevice);
    fscanf(preffile,"%d",&laceon);
    fscanf(preffile,"%d",&screenmode);
    fscanf(preffile,"%d",&pal2024);
    fclose(preffile);
  }
  switch(laceon)
  { case 0: item611.Flags|=CHECKED;
            break;
    case 1: item610.Flags|=CHECKED;
            break;
  }
  switch(screenmode)
  { case 0: item620.Flags|=CHECKED;
            break;
    case 1: item621.Flags|=CHECKED;
            break;
    case 2: item622.Flags|=CHECKED;
            break;
    case 3: item623.Flags|=CHECKED;
            break;
    case 4: item624.Flags|=CHECKED;
            break;
    case 5: item625.Flags|=CHECKED;
            break;
    case 6: item626.Flags|=CHECKED;
            break;
    case 7: item627.Flags|=CHECKED;
            break;
  }
  switch(pal2024)
  { case 0: item631.Flags|=CHECKED;
            break;
    case 1: item630.Flags|=CHECKED;
            break;
  }
  if ((IntuitionBase = (struct IntuitionBase *)
       OpenLibrary("intuition.library",37)) == 0L)
  {    ende();
       exit(100L);
  }
  if ((GfxBase = (struct GfxBase *)
       OpenLibrary("graphics.library",37))==0L)
  {    ende();
       exit(100L);
  }
  if ((DosBase = (struct DosBase *)
       OpenLibrary("dos.library",37))==0L)
  {    ende();
       exit(100L);
  }
  if ((ReqToolsBase = (struct ReqToolsBase *)
       OpenLibrary("reqtools.library",38))==0L)
  {    ende();
       exit(100L);
  }
  if (!(imgptrf=AllocMem(sizeof(imagedataf),MEMF_CHIP)))
  {    ende();
       exit(85L);
  }
  if (!(imgptrz=AllocMem(sizeof(imagedataz),MEMF_CHIP)))
  {    ende();
       exit(85L);
  }
  if (!(bufptr=AllocMem(32768,MEMF_PUBLIC)))
  {    ende();
       exit(85L);
  }
  memneed=0;
  noscan=0;
  memcpy(imgptrf,&imagedataf,sizeof(imagedataf));
  memcpy(imgptrz,&imagedataz,sizeof(imagedataz));
  fullimage.ImageData=(UWORD *)imgptrf;
  zoomimage.ImageData=(UWORD *)imgptrz;

  if (IntuitionBase->LibNode.lib_Version<=35)
  { screenwidth=GfxBase->NormalDisplayColumns;
    screenheight=GfxBase->NormalDisplayRows*((laceon)?2:1);
    ns.Width=screenwidth;
    ns.Height=screenheight;
    item62.Flags&=~ITEMENABLED; /*no screenmodes in Kick 1.3*/
    ns.ViewModes=((laceon)?HIRES|LACE:HIRES);
    if (!(scr=(struct Screen*)OpenScreen(&ns)))
    {    ende();
         exit(90L);
    }
    numcols=16;
    display=0;
  }
  else
  { numcols=modecols[screenmode];
    display=viewmodes[screenmode];
    if (laceon)
    { if (display==A2024TENHERTZ_KEY)   display=A2024FIFTEENHERTZ_KEY;
      else if (display==VGAPRODUCT_KEY) display=VGAPRODUCTLACE_KEY;
      else display|=LORESLACE_KEY;
    }
/*
    if (!(GetDisplayInfoData(0,(struct DimensionInfo*)&diminfo,
*/
    if (!(GetDisplayInfoData(0,(UBYTE *)&diminfo,
          sizeof(diminfo),DTAG_DIMS,display)))
       { ende();
         exit(90);
       }
    if (!(scr=(struct Screen*)OpenScreenTags(&ns,
          SA_Width,  (int)(diminfo.Nominal.MaxX-diminfo.Nominal.MinX+1),
          SA_Height, (int)(diminfo.Nominal.MaxY-diminfo.Nominal.MinY+1),
          SA_Depth, ((numcols==16)?4:2),
          SA_Pens, ((numcols==16)?pendata16:pendata4),
          SA_Font, &textattr,
          SA_DisplayID, display,
          SA_SysFont,0,
          TAG_DONE)))
    {    ende();
         exit(90L);
    }
    screenheight=scr->Height;
    screenwidth=scr->Width;
  }

  viewheight=screenheight-42;
  viewwidth=screenwidth-16;
  viewoffset=(ULONG)(screenwidth>>3)*21;
  lineoffset=(ULONG)(screenwidth>>3);
  hgad.TopEdge=viewheight+13;
  hgad.Width=viewwidth;
  vgad.LeftEdge=viewwidth+1;
  vgad.Height=viewheight;
  zgad.LeftEdge=viewwidth+3;
  zgad.TopEdge=viewheight+15;

  vp=&(scr->ViewPort);
  rp=&(scr->RastPort);
  if ((display==A2024TENHERTZ_KEY)||(display==A2024FIFTEENHERTZ_KEY))
  { for (i=0;i<4;i++)
    { SetRGB4(vp,i,graystep4[i],graystep4[i],graystep4[i]); }
  }
  else
    if (pal2024)
      if (numcols==16)
      { for (i=0;i<16;i++)
        { SetRGB4(vp,i,pal16_2024[i][0],pal16_2024[i][1],pal16_2024[i][2]); }
      }
      else
      { for (i=0;i<4;i++)
        { SetRGB4(vp,i,pal4_2024[i][0],pal4_2024[i][1],pal4_2024[i][2]); }
      }
    else
      if (numcols==16)
      { for (i=0;i<16;i++)
        { SetRGB4(vp,i,graystep16[i],graystep16[i],graystep16[i]); }
      }
      else
      { for (i=0;i<4;i++)
        { SetRGB4(vp,i,graystep4[i],graystep4[i],graystep4[i]); }
      }
  nw.Screen=scr;
  nw.Width=screenwidth;
  nw.Height=screenheight-12;
  if (!(win=(struct Window *)OpenWindow(&nw)))
  {    ende();
       exit(90L);
  }

  wrp=win->RPort;
  if(!(diskport=CreatePort(0L,0L)))
  {    noscan=1;
  }
  if(!(diskreq=(struct IOStdReq *)CreateStdIO(diskport)))
  {    noscan=1;
       if(diskport)DeletePort(diskport);
  }
  if(openerror=OpenDevice((char *)scsidevice,ST400ID,diskreq,0L))
  {    noscan=1;
       if(diskport)DeletePort(diskport);
       if(diskreq)DeleteStdIO(diskreq);
  }
  if (noscan)
  { item10.Flags&=(~ITEMENABLED);
    item14.Flags&=(~ITEMENABLED);
  }
  memptr=NULL;
  cmp=mf=0;
  SetMenuStrip(win,&menu0);
  for (i=0;i<256;i++) numbits[i]=numbits_16[i/16]+numbits_16[i&15];
  SetWindowTitles(win,"Kein Bereich selektiert!                  ",(UBYTE *)-1);
  for(;;)
  { Wait((long)(1L<<win->UserPort->mp_SigBit));
    while (Message=(struct IntuiMessage *)GetMsg(win->UserPort))
    {
      MessageClass=Message->Class;
      Code=Message->Code;
      ReplyMsg(Message);
      if (MessageClass==MENUPICK) menu(Code);
      if (((MessageClass==GADGETUP))&&(memneed))
      { if (!(((struct Gadget *)(Message->IAddress))->GadgetID))
        { vprop.VertBody=(zgad.Flags&SELECTED)?0xffff:((viewheight<memheight)?viewheight*65536/memheight:0xffff);
          hprop.HorizBody=(zgad.Flags&SELECTED)?0xffff:((viewwidth<memwidth)?viewwidth*65536/memwidth:0xffff);
          vprop.VertPot=0;
          hprop.HorizPot=0;
        }
        redisplay();
        RefreshGadgets(&vgad,win,NULL);
      }
      if ((MessageClass==MOUSEBUTTONS)&&(Code&128)&&(memneed))
      { mx=win->MouseX; my=win->MouseY;
        if ((mx<viewwidth)&&(my>=10)&&(my<viewheight))
        switch(mf)
        { case 0: wx1=s2px(mx)&0xfff8; wy1=s2py(my-11); mf=1; break;
          case 1: wx2=s2px(mx)&0xfff8; wy2=s2py(my-11); mf=2;
                  drawbox(wx1,wy1,wx2,wy2,wrp); break;
          case 2: mf=0; drawbox(wx1,wy1,wx2,wy2,wrp); break;
        }
      }
      if (MessageClass==CLOSEWINDOW)
      { ende();
        exit(0L);
      } /* if */
      if ((!(memgray))&&(memneed)) menu4.Flags|=MENUENABLED;
      else menu4.Flags&=(~MENUENABLED);
      if ((memgray)&&(memneed)) menu3.Flags|=MENUENABLED;
      else menu3.Flags&=(~MENUENABLED);
      if (memneed)
      { item12.Flags|=ITEMENABLED;
        menu5.Flags|=MENUENABLED;
      }
      else
      { item12.Flags&=(~ITEMENABLED);
        menu5.Flags&=(~MENUENABLED);
      }
      switch(mf)
      { case 0: SetWindowTitles(win,"Kein Bereich selektiert                   ",(UBYTE *)-1);
                item53.Flags&=(~ITEMENABLED);
                break;
        case 1: SetWindowTitles(win,"Nächsten Punkt wählen                     ",(UBYTE *)-1);
                item53.Flags&=(~ITEMENABLED);
                break;
        case 2: SetWindowTitles(win,"Bereich gewählt                           ",(UBYTE *)-1);
                item53.Flags|=ITEMENABLED;
                break;
      }
    } /* while */
  } /* for */
  return 0;
} /* main */

