[comp.os.os9] icapos9.shr1 Part 1 of 2

jimomura@lsuc.uucp (Jim Omura) (05/16/89)

     This is the latest version kit for the Imagewise Framegrabber.
It includes replacements for all of the previous support programs
I've posted.  All the programs were originally written on the QT-20X
OS-9 68K system, but all the files except the printout module have
now been ported to the Color Computer 3 under Level II.  Slightly
more complete documentation is included with other minor fixes
and cleanups.

     I should note that I generally do *not* post identical kits
on any two systems.  What I post on Usenet is not necessarily the
same as what I upload to RDS-09 or BIX.  In each case I generally
make small changes and corrections.  In theory, if you want the
latest of any program I've written you'd have to check all three.
In practicality that's not worth the effort since the differences
between the latest version and the one immediately preceding it
are usually pretty small.  But it means the differences from one
Net posting to the next are usually reasonably big.

     Anyway, if you have any of my old 'icap' kits or 'iwprint'
kits, throw them away and keep this one (for now :-).

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:    Shell Archiver
#	Run the following text with /bin/sh to create:
#	Dirnote
#	gs_rdy.a
#	iana.c
#	iana09.mk
#	ianaosk.mk
#	icap.c
#	icap.h
#	icap09.mk
#	icaposk.mk
#	icapture.c
#	idegcon.c
#	ideg09.mk
#	idegosk.mk
# This archive created: 
# By:	Jim Omura ()
cat << \SHAR_EOF > Dirnote
ICAPOS9.SHR

1989/05/12

OVERVIEW

     This kit includes:

1.  'iana', a module to analyse an Imagewise expanded file
2.  'icap', a module to capture an ImageWise Framegrabber field
3.  'idegcon' a module to convert an Imagewise expanded file
         to a Degas .PI1 file
4.  'iex', a module to expand an ImageWise run length encode file
5.  'iwprint' to print out an expanded Imagewise file directly
         on a Toshiba P-321 or compatible printer

     All modules EXCEPT 'iwprint' were written on an OS-9 68K system
and have been ported to OS-9 6809.  'iwprint' has NOT yet been
ported to OS-9 6809.

     The modules have been designed for portability and use
in particular with Unix-like systems such as OS-9.  As such,
each module has been designed to be used as a filter and data
may be PIPED between the modules.  The modules may also be called
by a custom application shell.  Generally though, they are most
conveniently used as they stand since OS-9 style internal help
has also been designed in.  That is to say, calling the modules
with '-?' parameters will give short usage summaries.

     This file set uses fairly common extensions for organization.

 .a -- Assembly source
 .c -- C source code
 .h -- Header file to be included by C compiler
 .mk -- A "makefile" for usage with a 'make' utility
 .r -- (made at compile time) a Relocatable object file to be linked

I would also suggest using the following standard extensions:

 .an -- 'Iana' report output
 .ex -- Expanded or long format ImageWise file
 .iw -- ImageWise file in runlength encoded form
 .pi1 -- Degas .PI1 file
 .prt -- Printer output file

     Therefore, the following commands would be commonly used:

icap /t2 filename.iw

  -- capture the file from device /t2 and store in 'file.iw'

iex filename.iw filename.ex

  -- expand the file from 'filename.iw' and store in 'filename.ex'

iana filename.ex >filename.an

  -- analyse 'filename.ex' and store report in 'filename.an'

idegcon filename.ex filename.pi1

  -- convert 'filename.ex' to Degas .PI1 and store in 'filename.pi1'

iwprint filename.ex >filename.prt

  -- convert expanded ImageWise file to Toshiba P-321 printer file
  and store in 'filename.prt'

1987/11/12

ICAP

     It is specifically designed to work with the Radio Shack Color
Computer 3 under OS-9 Level II with 512K and RAM Disk.  Specifically,
you need a RAM Disk with at least 64K since it writes constantly to
the disk without shutting off Flow of the Imagewise.  Previous
experience has shown me that there is risk of dropping more data
if you use flow control with the Framegrabber than if you don't, so
if you try to add it, beware that it may not do much good.  Otherwise
it should work on any 6809 OS-9 system.

     Please note that the maximum file size of the ImageWise is
62710 bytes uncompress, so there's some difficulty rewriting this to
save to memory without the RAM Disk.  It requires usage of a "data
module", which I haven't tried working with yet.

     The current program set consists of icap.c, gs_rdy.a,
icapture.c, ilonghelp.c, imenu.c, isetpath.c and iterm.c.  A
'makefile' is provided to aid in compiling.  If you don't have
"make", compile each file as:

cc filename -r

and then link them with

cc icap.r gs_rdy.r icapture.r ilonghelp.r imenu.r isetpath.r iterm.r -f=/dd/cmds/icap

USAGE:

     There is a -? switch to give a short help and longer
help files are accessible via a menu.  The most common usage
for CoCoist will be as follows:  BE SURE YOUR CURRENT WORK
DIRECTORY IS /R0.  Use a maximum baud rate of 1200 set on the
Imagewise and the /t2 port (I have not tested this program
with /t1 or other ports or higher speeds).  Enter the following:

icap /t2 -v filename

BUGS and LIMITATIONS:

87/11/13

     I have implimented Xon/Xoff flow control override
by a system call to 'xmode' at the beginning of the program.
It does not maintain the old setting and does not restore
it upon exit.  You *must* have 'xmode' available.

87/10/27

     Still some bugs!  The output file must be specified
in the command line.  It is *not* currently optional.

     Multiple sequential captures not yet implimented.

IDEGCON

89/05/12

     'IDegCon' converts an expanded Imagewise file to a
Degas .PI1 file.  This is a file most commonly found in Atari ST
programs and originated in Tom Hudson's program "Degas" originally
distributed by Batteries Included Inc., but is often usable in other
programs on the Atari ST and on other computers.  Degas .PI1 files
have 16 colors and I have used these to simulate 16 grey scale
levels, exceeding the true grey scale limitations of the Atari ST.

Usage:

idegcon [-?v] [-c=nn] [-b=nn] [infile] [outfile]

See sources for details.

BUGS and LIMITATIONS:

     I have found that in the 6809 version where "file short"
type error occurred while using on a text file on the 88th Imagewise
input line when -c=120.  If Contrast is left standard (100%) then
the error didn't occur.  I have not identified the cause of the
problem.

IEX

89/05/12

     Iex.C expands an Imagewise compressed file to a
full size Imagewise file.  This utility defaults to
Standard IO and can be used as a filter between Icap
and a conversion utility.

Compiling IEX

     Makefiles have been provided.

Using IEX

     The most common usage of 'iex' is

iex infile outfile

IWPRINT

     The 'IWPrint' utility works similar to the 'IDegCon' utility
see the sources for 'IWPrint' for instructions.

SHAR_EOF
cat << \SHAR_EOF > gs_rdy.a
 psect gs_rdy_c,0,0,0,0,0
 nam gs_rdy_c
 ttl _gs_rdy
_gs_rdy: pshs u
 ldd #_1
 lbsr _stkcheck
 leas -2,s  Set stack pointer at "waiting"
 lda 7,s    load "path" -- actually ldd 6 and shift left 8.
 ldb #1
 os9 $8d    I$GetStt
 bcs errhand
 clra clear upper byte to make sense.
 bra cleanexit
errhand ldd #-1
cleanexit std 0,s Store the Result in "waiting"
 ldd 0,s (Is this necessary?)
 leas 2,s
 puls u,pc
_1 equ -66
 endsect
SHAR_EOF
cat << \SHAR_EOF > iana.c
/* Iana.c */

/* Imagewise data analyser */

/* 1987/11/13
 * This version for OS-9 6809 and OS-9 68K
 * by Jim Omura
 * 2A King George's Drive
 * Toronto, Ontario
 * M6M 2G9, Canada
 */

#include <stdio.h>
#include <modes.h>

#ifdef OS9
#define short /* Unsigned is Unsigned 16 bit int */
#endif

#define ERROR -1

main(argc,argv)

int argc;
char **argv;

{
	char	cchar[1];
    register int     cval;      /* Character value holder */
	int		inpath;
	char	inpathname;
	int		lncntr;
	long	charcntr;
	int		report;
	unsigned short	greyvals[64];
#ifdef OS9
pflinit();    /* Necessary to print Long Ints */
#endif
	for (lncntr = 0 ; lncntr < 64 ; ++lncntr) /* Just a handy var. */
	{
		greyvals[lncntr] = 0;
	}

	if (argc < 2)
	{
		printf("Insufficient arguments.\n");
		exit(0);
	}

	++argv;
	inpath=open(*argv,S_IREAD);

	if (inpath == ERROR)
	{
		exit(errno);
	}

	lncntr=0;
	charcntr=0;

	for (;;)
	{
		report = read(inpath,cchar,1);

		if (report == -1)
		{
			exit(errno);
		}

        cval=*cchar;
        cval=cval & 0xff;            /* Unsign extend it */
		charcntr = charcntr + report;

		if ((report == 0) || (cval == 0x042))
		{
#ifdef OS9
			printf(" Chrs. %3ld\n",charcntr);
#else
            printf(" Chrs. %3d\n",charcntr);
#endif
			break;
		}

		if((cval > -1) && (cval < 64))
		{
			++greyvals[cval];
		}	/* Endcondition Grey Value */
		else
		{

			if (cval == 0x041)		/* New Line Character */
			{
#ifdef OS9
				printf(" Chrs. %3ld",charcntr);
#else
                printf(" Chrs. %3d",charcntr);
#endif
				if ((4 + lncntr) == ((4 + lncntr) / 4) * 4)
				{
					printf("\n");
				}	/* Endcondition line terminate */
				else
				{
					printf("   ");
				}	/* Continue the line */

				charcntr = 0;
				++lncntr;
				printf("Ln ");
				printf("%3d",lncntr);
			}	/* Endif Newline */

		}	/* /Endif greyval */

		if (cval == 0x040)
		{
			printf("Starting");
		}
	}		/* Endloop */

	for (lncntr = 0 ; lncntr < 64 ; ++ lncntr) /* Just an handy var. */
	{
		printf("GVal %2d no. %5d\n",lncntr,greyvals[lncntr]);
	}

}	/* End iana.c */
SHAR_EOF
cat << \SHAR_EOF > iana09.mk
* Makefile for 'iana.c' modules of 'icap6809.shr'
* 87/11/13
HFILES = stdio.h

iana: iana.r
 cc1 iana.r
 del c.com

iana.r: iana.c $(HFILES)
 cc1 iana.c -dOS9 -r
SHAR_EOF
cat << \SHAR_EOF > ianaosk.mk
* Makefile for OS-9 68000 Version 'iana'
* 89/05/12
HFILES = stdio.h

iana: iana.r
 cc iana.r

iana.r: iana.c $(HFILES)
 cc iana.c -dOSK -rg
SHAR_EOF
cat << \SHAR_EOF > icap.c
/* icap.c */

/* OS-9 6809 ImageWise Framegrabber support module */

/* 1987/10/27
 * Public Domain
 *
 * By Jim Omura
 * 2A King George's Drive
 * Toronto, Ontario, Canada
 */

#include "icap.h"

/* Program Specific Constants */

/* Declare Globals */

int    files;      /* Number sequential files to capture */
char   pathname[80];
int    verbose;    /* Verbose flag */

main(argc,argv)
int argc;
char **argv;
{

/* Declare Local Variables */

    char   *flow1;    /* 1st message to stop flow control */
    char   *flow2;    /* 2nd message to stop flow control */
    int    inpath;
    int    menuflag;
    int    outpath;

          flow1 = "xmode ";         /* 6 chars. */
          flow2 = " xon=0 xoff=0";

/* Set Defaults */

    inpath   = STDIN;
    menuflag = TRUE;
    outpath  = STDOUT;
    verbose  = FALSE;

    for( ;argc > 1;)

    {
        ++argv;
        --argc;
        if(**argv == '-')
        {

            for (;;)
            {
                ++*argv;
                switch((int) **argv) 
                {
                    case '\0':
                        break;
                    case '?':
                        shorthelp();
                        exit(0);
                    case 'n':
                        ++*argv;
                        if (**argv == '=')
                        {
                            ++*argv;
                        }
                        files = atoi(*argv);
                        continue;
                    case 'v':
                        verbose=TRUE;
                        continue;
                    default:
                        continue;  /* Next Char. */
                }   /* Endswitch */
                break;  /* Next argv */

             }   /* Endloop more chars */

             continue; /* Next argv */

         }   /* Endif dash handler */

         if (inpath == STDIN)
         {
            strcpy(pathname,flow1);           /* Shut off flow control */
            strcpy(pathname+6,*argv);
            strcpy(pathname+6+strlen(*argv),flow2);
            system(pathname);
            inpath=open(*argv,S_IREAD | S_IWRITE);

            if (inpath == ERROR)
            {
                fprintf(stderr,"Input file error\n");
                exit(0);
            }   /* Endif open error */

            continue;  /* Next argv */

        }   /* Endif open input file */

        if (outpath == STDOUT)
        {
            strcpy(pathname,*argv);

            if(verbose)
            {
                fprintf(stderr,"Creating %s.\n",pathname);
            }
            
            outpath = creat(pathname,S_IWRITE);

            if (outpath == ERROR)
            {
                fprintf(stderr,"Output file error\n");
                exit(0);
            }
            continue; /* Next argv */

        }   /* Endif make outpath */

    }   /* Endloop arghandler */

    if (inpath == STDIN)
    {
         fprintf(stderr,"Icap:  Must specify Imagewise device path.\n");
         shorthelp();
         exit(0);
    }
    setinpath(inpath);
    menu(inpath,outpath);
    exit(0);

}   /* End of main() */

shorthelp()
{
    fprintf(stderr,"Icap: ImageWise Digitizer support.\n");
    fprintf(stderr,"Usage:  icap inpath [outpath] -[flags]\n");
    fprintf(stderr,"Flags:\n");
    fprintf(stderr," ? Short help (this message\n");
    fprintf(stderr," e Execute (no menu)\n");
    fprintf(stderr," n Number\n");
    fprintf(stderr," v Verbose\n");
}   /* End of shorthelp() */

/* End of icap.c */
SHAR_EOF
cat << \SHAR_EOF > icap.h
/* icap.h */

/* OS-9 6809 ImageWise Framegrabber support module */

/* By Jim Omura
 * 2A King George's Drive
 * Toronto, Ontario, Canada
 */

#include <stdio.h>
#include <modes.h>
/* #include <stdmenu.h>
/* */
#include <wind.h>
/* */
#define ERROR   -1

#define FALSE   0

#define STDIN   0
#define STDOUT  1
#define STDERR  2

#define TRUE    1

/* End of icap.h */
SHAR_EOF
cat << \SHAR_EOF > icap09.mk
# Makefile for icap.c 6809

* Reserved Macros:
CFLAGS = -dDEBUG
ODIR = /dd/cmds
* RDIR doesn't work with Microware C
* because the Compiler doesn't redirect
* the .r files.
* RDIR = RELS
* RFLAGS =

* User Macros:
AFILES = gs_rdy.a
CFILES = icap.c icapture.c ilonghelp.c imenu.c isetpath.c iterm.c
RFILES = icap.r icapture.r ilonghelp.r imenu.r isetpath.r iterm.r
HFILES = icap.h /dd/defs/stdio.h
LIBS = -l=/dd/lib/cstart.r -l=/dd/lib/clib.l

icap: $(RFILES) $(AFILES)
  cc1  $(RFILES) $(AFILES) -f=$(ODIR)/icap
  del c.com

$(RFILES): $(HFILES)
  cc1 $*.c -r $(CFLAGS)

isetpath.r: /dd/defs/sgstat.h
  cc1 $*.c -r $(CFLAGS)

SHAR_EOF
cat << \SHAR_EOF > icaposk.mk
# Icap.c Makefile

icap: icap.r
	cc icap.r

icap.r: icap.c, stdio.h
	cc icap.c -gr
SHAR_EOF
cat << \SHAR_EOF > icapture.c
/* icapture.c */

/* OS-9 6809 ImageWise Framegrabber support module */

/* By Jim Omura
 * 2A King George's Drive
 * Toronto, Ontario, Canada
 */

/* capture()
 *
 *     This is the actual capture routine.
 *
 * Receives: input and output file paths as 'inpath' and 'outpath'
 *
 * Returns: successful capture as TRUE/FALSE integer.
 *
 * Uses Globals: 'verbose'
 *
 *     Keyboard abort is available with ^J (LESC).
 */

#include "icap.h"

#define C_XOFF 0x13
#define C_XON  0x11
#define SS_RDY 1

/* Program Specific Constants */

#ifdef SLOW
#define BUFFSIZE   1
#else
#define BUFFSIZE   32
#endif

#define ENDFIELD  0x42  /* End of Imagewise field */

#define HIRES     0x80  /* Imagewise 256 * 244 */
#define LESC      0x0a  /* Local escape is ^J */
#define STARTFLD  0x40  /* Start of an image field */
#define STARTLN   0x41  /* Start of video line */

int capture(inpath,outpath)

int inpath;
int outpath;

{

    int  bufpntr;
    int  endpntr;     /* Actually points to 1 past last char. */
    char inbuff[BUFFSIZE];
    char outbuff[2];
    register int  report;
    char *shortbuff;
    int  startpntr;
    int  stopflag;    /* Signals end of field detected */
    int  successflag;
    extern int verbose;    /* Verbose Flag */
    int  writeflag;
    int  writelen;

    successflag = TRUE;
    stopflag    = FALSE;
    writeflag   = FALSE;

/* Send start package */

    if (verbose)
    {
        fprintf(stderr,"Sending Start packet\n");
    }

    outbuff[0]=HIRES;
    outbuff[1]=C_XON;
    report=write(inpath,outbuff,2);

    if (report == ERROR)
    {
        fprintf(stderr,"Write Error at start packet\n");
        return(ERROR);
    }

    if (verbose)
    {
        fprintf(stderr,"Waiting\n");
    }

    outbuff[0] = C_XOFF; /* outbuff = xoff,xon re-synch pair */

/* Receive loop */
    for(;;)
    {
        report=getstat(SS_RDY,STDIN);

        if (report > -1)
        {
            report = read(STDIN,inbuff,1);

            if(report == ERROR)
            {
                 fprintf(stderr,"Keyboard read error\n");
                 exit(0);
            }    /* Endif read error */

            if(inbuff[0] == LESC)
            {
                if(verbose)
                {
                    fprintf(stderr,"Keyboard Abort\n");
                }
                successflag=FALSE;
                stopflag=TRUE;

            }   /* Endif local escape */

        }   /* Endif Keyboard abort */

        report=_gs_rdy(inpath);

        if(report > -1)
        {
            if (report > BUFFSIZE)
            {
                report = BUFFSIZE;
            }
            report = read(inpath,inbuff,report);

            if (report == ERROR)
            {
                fprintf(stderr,"Pic data read error\n");
                successflag = FALSE;
                continue; /* Skip it & hope for best */
            }

            startpntr = 0;
            endpntr   = report;

            for(bufpntr=0 ; bufpntr < report ; ++bufpntr)
            {
                switch ((int) inbuff[bufpntr])
                {
                    case STARTFLD:
                        if (verbose)
                        {
                            fprintf(stderr,"Field Started\n");
                        }   /* Endif report */
                        writeflag = TRUE;
                        startpntr = bufpntr;
                        break;    /* Exit switch */
                    case ENDFIELD:
                        if (verbose)
                        {
                            fprintf(stderr,"End received.\n");
                        }
                        stopflag = TRUE;
                        endpntr = bufpntr + 1;
                        break;
                    default:
                        break;
                }   /* Endswitch */

            }   /* Endloop */

            if (writeflag)
            {
                shortbuff = inbuff + startpntr;
                writelen = ((int) endpntr - (int) startpntr);
                write(outpath,shortbuff,writelen);
            }
        }   /* Endif image data waiting */
        if (stopflag)
        {
            break;
        }   /* End if stopflag */

    }   /* Endloop Capture */

    return(successflag);

}   /* End capture() */

/* End of icapture.c */
SHAR_EOF
cat << \SHAR_EOF > idegcon.c
/* Idegcon.c
 * 1987/10/29
 * Version 0.6 -- Public Domain
 */

/* by Jim Omura, 2A King George's Drive,
 * Toronto, Ontario, M6M 2G9
 * BIX 'jimomura'
 */

/* Converts Imagewise uncompressed file to Degas .PI1
 * format for Atari ST.
 *
 * idegcon [-?sv] [-b=nn] [-c=nn] [infile] [outfile]
 * Dash Flags:
 *
 *  ?   Short Help
 *  b   Bright +/-255
 *  c   Contrast %
 *  s   Shorten image:  Skips every 5th line.
 *  v   Verbose.
 */

/* 1989/05/12 Version 0.6 for OS-9 68000 and OS-9 6809
 *
 * Changes:
 *    shorthelp() added.
 *    Improved Error reporting.
 *    Improved Verbose mode.
 *
 * Known Bugs:
 * *** There seems to be a bug that causes occasion
 * *** erroneous "short line" errors.  It showed up in
 * *** a test where -c=120, but not when contrast was
 * *** left at default.  Since Contrast adjustment comes
 * *** after the line building, this discrepancy should
 * *** not be possible.
 *
 * Future Changes:
 *    It should be possible to use the OS9 version for OSK
 *    making the #ifdefs unnecessary, but this has not
 *    been tested.  I'd prefer a better 6809 compiler since
 *    the OSK version was properly written and the changes
 *    for the OS9 version were mainly due to compiler
 *    limitations.
 *
 * 1987/10/29 Version 0.5 for OS-9 68000
 */

#include <stdio.h>
#include <modes.h>

#define STDIN   0
#define STDOUT  1
#define STDERR  2

#define FALSE   0
#define TRUE    1
#define ERROR   -1

/* Program Specific Definitions */

#define DEGLOW  0       /* Degas Low Res */
#define ENDFLD  0x042   /* Imagewise End of Field */
#define NEWFLD  0x040   /* Imagewise New Field */
#define NEWLINE 0x041   /* Imagewise New Line */

/* ------------------------------------------------------- */

/* Global Variables */

int             sfflag;             /* Shorten File Flag */ 
int             bright;
int             contrast;
int             verbose;

/* ------------------------------------------------------- */

main(argc,argv)

int argc;
char **argv;

{

/* Degas .PI1 file header buffer */

#ifdef OS9
    static unsigned deghdr[17] =
#endif
#ifdef OSK
    static unsigned short deghdr[17] =
#endif
    {

/* Resolution Variable */
        DEGLOW,

/* Grey Scale 16 Levels: */
        0x0000, 0x0011, 0x0112, 0x0122,
        0x0222, 0x0223, 0x0233, 0x0333,
        0x0344, 0x0444, 0x0455, 0x0555,
        0x0566, 0x0667, 0x0677, 0x0777,
    };

#ifdef OS9
    char            cbyte[1];       /* Conversion Byte */
    unsigned        conbuf[4];      /* Conversion Buffer */
    char            inbuf[257];     /* Input File buffer */
    unsigned        outbuf[80];     /* Output File buffer */
#endif
#ifdef OSK
    unsigned char   cbyte[1];       /* Conversion Byte */
    unsigned short  conbuf[4];      /* Conversion Buffer */
    unsigned char   inbuf[257];     /* Input File buffer */
    unsigned short  outbuf[80];     /* Output File buffer */
#endif
    int             cntr;           /* General counter */
    int             conpntr;        /* Conversion Buffer Pointer */

    int             fdone;          /* Field done */
    register int    greyval;        /* An Imagewise Grey value */
    int             inlncntr;       /* Input Line Counter */
    int             inpath;         /* Input File pointer */
    int             lncntr;         /* Line Counter */
    int             newfflag;       /* New Field Flag */

    int             outcntr;        /* Output buffer counter */
    int             outpath;        /* Output File pointer */
    int             offset;         /* Input buffer offset */

    int             report;         /* Function return codes */

/* Set Defaults */

    bright      = 0;
    contrast    = 100;
    inpath      = STDIN;
    outpath     = STDOUT;
    sfflag      = FALSE;
    verbose     = FALSE;

/* Clear the output file buffer */
    for (outcntr=0 ; (outcntr < 80) ; ++outcntr)
    {
        outbuf[outcntr]=0;
    }

    for ( --argc ; (argc > 0) ; --argc )
    {
        ++argv;

        if(**argv == '-')
        {
            dashflag(*argv);
        }
        else
        {

            if (inpath == STDIN)
            {
                inpath=open(*argv,(S_IREAD));

                if (inpath == ERROR)
                {
                    exit(errno);
                }               /* Endif Inpath Error */
            }       /* End branch Open inpath */
            else
            {
                outpath=(creat(*argv,(_READ | _WRITE)));

                if (outpath == ERROR)
                {
                    exit(errno);
                }   /* Endif output file error */
            }   /* Endif Infile/Outfile openning */
        }   /* Endif Dash/Not dash parameters */
    }   /* Endloop Get Arguments */

/* **If multiple files implimented then loop will start here.** */

    inlncntr = 0;
    lncntr   = 0;
    fdone    = FALSE;
    newfflag = TRUE;

    write(outpath,deghdr,34);

    for(; (fdone == FALSE) ;)
    {
        outcntr = 0;

/* Get an Imagewise Line */

        offset  = 0;
        for(; (offset < 257) ;)
        {
#ifdef OS9
            report=read(inpath,(inbuf+offset),1);
#endif
#ifdef OSK
            report=read(inpath,&((char)inbuf[offset]),1);
#endif
            if (report == ERROR)
            {
                exit(errno);
            }

            if (inbuf[offset] == NEWFLD)
            {
                if (verbose)
                {
                    fprintf(stderr,"Starting Field.\n");
                }
                newfflag = TRUE;
                lncntr   = 0;
                offset   = 0;
                continue;
            }   /* Endif New Field */

            if (inbuf[offset] == NEWLINE)
            {

                if (newfflag)
                {
                    newfflag=FALSE;
                    continue;
                }
                else
                {
                    offset=259;
                    if (verbose)
                    {
                        printf("IW line %d\n",inlncntr);
                    }
                }
            }       /* Endif New Line */

            if (inbuf[offset] == ENDFLD)
            {
            fprintf(stderr,"IDEGCON: File Short input line %d\n",inlncntr);
                offset = 259;
            }       /* Endif End of Field */
            ++offset;
        }       /* Endloop Get Line */
        ++inlncntr;
        if (sfflag)
        {
            if (inlncntr == ( 5 * (inlncntr/5)))
            {
                continue;
            }
        }
/* Convert The Line */

        for (offset=0 ; (offset < 256) ;)
        {

            for (conpntr=0 ; (conpntr < 4) ; ++conpntr)
            {                           /* Clear the Conversion Buffer */
                conbuf[conpntr]=0;
            }                           /* Endloop Clear Con Buffer */

            for(cntr=0 ; (cntr < 16) ; ++cntr) /* 16 bytes to be converted */
            {
                *cbyte=inbuf[offset+cntr];  /* Get a byte. */
/* Adjust Grey Scale */
#ifdef OS9
                greyval=(((unsigned)*cbyte + bright) * contrast / 400);
#endif
#ifdef OSK
                greyval=(((int)*cbyte + bright) * contrast / 400);
#endif
                if(greyval > 0x0f)      /* Keep it in boundaries */
                {
                    greyval = 0x0f;
                }   /* Endcondition too high */
                else
                {

                    if(greyval < 0)
                    {
                        greyval = 0;
                    }   /* Endif too low */

                }   /* Endcondition Upper bounadary */

                conpntr=0;
                for (;;)
                {                       /* Convert 1 byte */

                    if (greyval > 0)
                    {                   /* Set bit and continue */
                        conbuf[conpntr]=conbuf[conpntr]+ (greyval & 1);
                        greyval = (greyval>>1);
                        ++conpntr;
                    }
                    else
                    {                   /* Skip rest of byte */
                        break;
                    }   /* Endcondition greyval done */

                }   /* Endloop Convert 1 byte */

                if (cntr < 15)
                {
/* Shift the words left */
                    for (conpntr=0 ; (conpntr < 4) ; ++conpntr)
                    {
                        conbuf[conpntr]=conbuf[conpntr]<<1;
                    }   /* Endloop Shift Words */

                }   /* Endif More Bytes */

            }   /* Endloop Convert 16 Bytes */

            for (conpntr=0 ; (conpntr < 4) ; ++conpntr)
            {                       /* Copy conbuf to outbuf */
                outbuf[outcntr+conpntr]=conbuf[conpntr];
            }                       /* Endloop copy conbuf to outbuf */

            offset=offset+cntr;     /* Set the pointers */
            outcntr=outcntr+conpntr;

        }
        write(outpath,outbuf,160);      /* Store the line. */
        ++lncntr;

        if (lncntr > 199)
        {
            fdone = TRUE;
        }
    }
    exit(0);

} /* End of main() */

/* -------------------------------------------------------- */

dashflag(param)

char    *param;

{
    extern int  bright;
    extern int  contrast;
    extern int  sfflag;
    extern int  verbose;

    for (; (*param != '\0') ; ++param)
    {
        switch ((int)*param)
        {
            case '?':
                shorthelp();
                exit(0);
            case 'b':
                ++param;
                if (*param == '=')
                {
                    ++param;
                }
                bright = atoi(param);
                continue;
            case 'c':
                ++param;
                if (*param == '=')
                {
                    ++param;
                }
                contrast = atoi(param);
                continue;
            case 's':
                sfflag=TRUE;
                continue;
            case 'v':
                verbose=TRUE;
                continue;
            default:
                continue;
        }   /* Endswitch */
    }   /* Endloop */
}   /* End of dashflag() */

/* -------------------------------------------- */

shorthelp()
{
    printf("Converts Imagewise uncompressed file to Degas .PI1\n");
    printf("format for Atari ST\n\n");
    printf("idegcon [-?sv] [-b=nn] [-c=nn] [infile] [outfile]\n\n");
    printf("Dash Flags:\n");
    printf(" ?   Short Help\n");
    printf(" b   Bright +/-255\n");
    printf(" c   Contrast %\n");
    printf(" s   Shorten image:  Skips every 5th line.\n");
    printf(" v   Verbose.\n");

}  /* End of shorthelp() */

/* End of Idegcon.c */
SHAR_EOF
cat << \SHAR_EOF > ideg09.mk
# makefile for Idegcon.c OS-9 6809

idegcon: idegcon.r
    cc1 idegcon.r
    del c.com

idegcon.r: idegcon.c
    cc1 -r -dOS9 idegcon.c
SHAR_EOF
cat << \SHAR_EOF > idegosk.mk
# makefile for Idegcon.c OS-9 68000

idegcon: idegcon.r
    cc idegcon.r

idegcon.r: idegcon.c
    cc -gr -m=4 idegcon.c

SHAR_EOF
#	End of shell archive
exit 0
-- 
Jim Omura, 2A King George's Drive, Toronto, (416) 652-3880
lsuc!jimomura
Byte Information eXchange: jimomura