 Include "equ.h"
 Include "impequ.h"

CR	equ	$d
LF	equ	$a

;modes used to copy the data in PICT code $98 group
SRCCOPY         equ     $0
SRCOR           equ     $1
SRCXOR          equ     $2
SRCBIC          equ     $3
NOTSRCCOPY      equ     $4
NOTSRCOR        equ     $5
NOTSRCXOR       equ     $6
NOTSRCBIC       equ     $7


 SECTION maceps,CODE,PUBLIC
*******************************************
***					***
*******************************************
dumbentry:
	clr.l	d0
	rts

	dc.l	"IMPO"
	dc.w	201
	dc.l	0

table:	dc.l	0
	dc.l	name,special,check,eps


*******************************************************
* check file for Encapsulated PostScript in Data Fork *
*******************************************************
check:	move.l	table,a0
	move.l	buff1(a0),a1
	move.l	(a1),a1
	add.w	#128,a1
	lea	epsfhdr,a2
chk1:	cmp.b	(a2)+,(a1)+
	bne	never
	tst.b	(a2)
	bne	chk1
	moveq	#2,d0
rrts:	rts


*********************************
*                               *
*********************************
eps:	clr.l   ehandle         ;zero pointers to malloc'ed mem used later
        clr.l   rhandle
        move.w  #7,abit
        
        bsr	geteps
	beq	rrts

	move.b	#teps,object+ob_Type
	clr.b	object+ob_Flag
	clr.w	object+ob_Slant
	clr.w	object+ob_Twist
	move.w	#50,object+ob_LWidth
	clr.b	object+ob_LType
	move.b	#1,object+ob_LColor
	clr.b	object+ob_FType
	move.b	#1,object+ob_FColor
	clr.b	object+ob_LBegin
	clr.b	object+ob_LEnd
	move.w	#900,object+ob_HStandOff
	move.w	#900,object+ob_VStandOff

	clr.l	object+ob_Left
	clr.l	object+ob_Top
	move.l	eright,d0
	sub.l	eleft,d0
	bpl	eps1
	neg.l	d0
eps1:	move.l	etop,d1
	sub.l	ebottom,d1
	bpl	eps2
	neg.l	d1
eps2:	mulu	#100,d0
	mulu	#100,d1
	move.l	d0,object+ob_Right
	move.l	d1,object+ob_Bottom

	move.l	table,a0
	move.l	o_open(a0),a0
	jsr	(a0)

	move.l	table,a1
	move.l	o_obj(a1),a1
	lea	object,a0
	jsr	(a1)
	beq	abortobj

	move.l	elength,d0              ;alloc mem for eps obj
	add.l	#eps_Sizeof,d0
	add.l	#16,d0
	move.l	table,a0
	move.l	o_malloc(a0),a0
	jsr	(a0)
	beq	abortobj
        
        move.l	table,a1                ;save an index to the eps obj
	move.l	copybuf(a1),a1
	move.l	(a1),a1
	move.l	a0,d0
	sub.l	(a1),d0
        move.l  d0,eobj

	move.w	#3,eps_Type(a0)
	move.l	object+ob_Right,eps_Width(a0)
	move.l	object+ob_Bottom,eps_Height(a0)
	clr.l	eps_PicPtr(a0)
	clr.l	eps_PlyPtr(a0)
	move.l	elength,d0
	add.l	#16,d0
	move.l	d0,eps_DataLen(a0)

	lea	eps_Sizeof(a0),a0
	move.l	eleft,(a0)+
	move.l	ebottom,(a0)+
	move.l	eright,(a0)+
	move.l	etop,(a0)+

	move.l	ehandle,a1
	move.l	(a1),a1
	move.l	elength,d0
	move.l	d0,d1
	swap	d1
	bra	ce2
ce1:	move.b	(a1)+,(a0)+
ce2:	dbf	d0,ce1
	dbf	d1,ce1

	move.l	ehandle,a0      ;free the mem for the eps read (data fork)
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)
        
        move.l  table,a0        ;did they want the PICT stuff?
        move.l  spclnum(a0),a0
        cmp.w   #1,(a0)
        beq     eps3            ;no, do NOT show picture - so exit

*********************************************        
;check for PICT resource and if found use it
*********************************************
        
        bsr     getpict         ;get the PICT data
        beq     eps3            ;if no PICT data found do not use it
        
	lea	pic,a0
	clr.l	pc_ID(a0)
	clr.l	pc_ID+4(a0)
	clr.l	pc_Version(a0)
	move.w	#-1,pc_SFreq(a0)
	move.w	#-1,pc_SAngle(a0)
	clr.w	pc_SSpot(a0)
	clr.l	pc_SMap(a0)
	clr.l	pc_SMap+4(a0)
	move.w	#72,pc_XDpi(a0)
	move.w	#72,pc_YDpi(a0)
        
        move.w  pc_W(a0),d0
	add.w	#15,d0
	lsr.w	#3,d0
	bclr	#0,d0
	move.w	d0,pc_Width(a0)
        
	clr.l	pc_DPtr(a0)
	clr.w	pc_DScale(a0)
        move.w	pc_Palet(a0),d0
	mulu	#pl_Sizeof,d0
	add.l	#pc_Sizeof,d0
	move.l	d0,pc_Ptr(a0)           ;bitmap data begins n-bytes from object

	move.w	pc_Width(a0),d0         ;alloc mem for bitmap obj
	mulu	pc_BitPln(a0),d0
	mulu	pc_H(a0),d0
        move.w	pic+pc_Palet,d1
	mulu	#pl_Sizeof,d1
	add.l	d1,d0
	add.l	#pc_Sizeof,d0
	move.l	table,a0
	move.l	o_malloc(a0),a0
	jsr	(a0)
        beq     abortobj

        move.l	table,a1                ;link pic to eps obj
	move.l	copybuf(a1),a1
	move.l	(a1),a1
	move.l	(a1),a1
	add.l	eobj,a1
        move.l  #1,eps_PicPtr(a1)

	lea	pic,a1                  ;copy pic obj data into allocated struct
	move.w	#pc_Sizeof/2-1,d0
pic1:	move.w	(a1)+,(a0)+
	dbf	d0,pic1
        
        cmp.b   #1,vers                 ;is this a version 1 PICT?
        beq     pic2                    ;yes
        
        move.l  a0,palptr               ;pointer to PgS color pal data
        move.l  picture,a0              ;pointer to start of file color pal data
        bsr     putpal
        move.l  palptr,a0
        move.w  pic+pc_Palet,d0
        mulu    #pl_Sizeof,d0
        add.l   d0,a0
        
pic2:   move.l	a0,picptr               ;save pointer to bitmap data start
        move.l  a0,picptrsave
        
        clr.l   d0
        move.w	pic+pc_Width,d0         ;set amiga bitmap to all white ($00)
        mulu	pic+pc_H,d0
        move.l  d0,planesize            ;save for later
	mulu	pic+pc_BitPln,d0
	move.l	d0,d1
	swap	d1
	move.l	picptr,a0
	bra	pp2
pp1:	move.b	#$00,(a0)+
pp2:	dbf	d0,pp1
	dbf	d1,pp1

        move.l  sp,spsave       ;save the stack pointer incase of error in putpic
        move.l  picture,a0
        bsr     putpic          ;get the picture data and put it into the pic obj
        
        move.l	rhandle,d0      ;free mem required for rsrc fork (getpict)
        beq     eps3
        move.l  d0,a0  
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)

eps3:	move.l	table,a0
	move.l	o_close(a0),a0
	jmp	(a0)
        
        
***********************************
***                             ***
***********************************
abortobj:
	move.l	ehandle,d0
        beq     ab1
        move.l  d0,a0  
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)
        
ab1:    move.l	rhandle,d0
        beq     ab2
        move.l  d0,a0  
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)

ab2:	move.l	table,a0
	move.l	o_abort(a0),a0
	jmp	(a0)


***********************************
***                             ***
***********************************
rtok:	moveq	#1,d0
	rts


***********************************
***                             ***
***********************************
rtf0:	move.l	ehandle,d0
        beq     rtf1
        move.l  d0,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)
        
rtf1:   move.l	rhandle,d0
        beq     rtfail
        move.l  d0,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)

rtfail:	moveq	#0,d0
	rts


***********************************
***				***
***********************************
geteps:	move.l	table,a0
	move.l	f_openr(a0),a0
	jsr	(a0)
	beq	rrts

	move.l	table,a1
	move.l	buff1(a1),a0
	move.l	(a0),a0
	move.l	#128,d0
	move.l	f_getr(a1),a1
	jsr	(a1)

	move.l	table,a1
	move.l	buff1(a1),a0
	move.l	(a0),a0
	add.w	#83,a0
	move.b	(a0)+,elength
	move.b	(a0)+,elength+1
	move.b	(a0)+,elength+2
	move.b	(a0)+,elength+3

	move.l	table,a1
	move.l	m_alloc(a1),a1
	move.l	elength,d0
	clr.w	d1
	clr.w	d2
	jsr	(a1)
	beq	rtfail

	move.l	a0,ehandle

	move.l	table,a1
	move.l	f_getr(a1),a1
	move.l	(a0),a0
	move.l	elength,d0
	jsr	(a1)

	move.l	table,a0
	move.l	f_closer(a0),a0
	jsr	(a0)

	move.l	ehandle,a0
	move.l	(a0),a0
	move.l	elength,d0

	move.l	#0,eleft
	move.l	#0,etop
	move.l	#72,eright
	move.l	#72,ebottom
	bra	geps9
geps6:	lea	bbstr,a1
geps7:	cmp.b	(a0)+,(a1)+
	bne	geps9
	subq.l	#1,d0
	tst.b	(a1)
	bne	geps7
	bra	geps10

geps9:	subq.l	#1,d0
	bcc	geps6
	bra	geps11
*
* got a match
*
geps10:	bsr	getnum
	move.l	d0,eleft
	bsr	getnum
	move.l	d0,ebottom
	bsr	getnum
	move.l	d0,eright
	bsr	getnum
	move.l	d0,etop

geps11:	bsr	putzero
	beq	rrts

	move.l	elength,d0
	btst	#0,d0
	beq	even
	bsr	putzero
	beq	rrts

even:
;	rts

*
*
*
	move.l	ehandle,a0
	move.l	(a0),a0
	move.l	elength,d0
	bra	gclr3
gclr1:	lea	ccstr,a1
gclr2:	cmp.b	(a0)+,(a1)+
	bne	gclr3
	subq.l	#1,d0
	tst.b	(a1)
	bne	gclr2
	bra	gclr4

gclr3:	subq.l	#1,d0
	bcc	gclr1
	bra	rtok
*
* got a match
*
gclr4:	bsr	getclr
	move.w	d0,color+cl_Cyan
	bsr	getclr
	move.w	d0,color+cl_Magenta
	bsr	getclr
	move.w	d0,color+cl_Yellow
	bsr	getclr
	move.w	d0,color+cl_Black
	clr.w	color+cl_Map
	clr.w	color+cl_Type
gclr5:	move.b	(a0)+,d0
	cmp.b	#"(",d0
	beq	gclr6
	cmp.b	#" ",d0
	beq	gclr5
	bra	rtok

gclr6:	lea	color+cl_Name,a1
	lea	color+cl_Name+23,a2
gclr7:	move.b	(a0)+,d0
	beq	rtok
	cmp.b	#")",d0
	beq	gclr8
	cmp.b	#CR,d0
	beq	rtok
	cmp.b	#LF,d0
	beq	rtok
	move.b	d0,(a1)+
	cmp.l	a2,a1
	bcs	gclr7
	subq.l	#1,a1
	bra	gclr7

gclr8:	clr.b	(a1)

	move.l	ehandle,a1
	sub.l	(a1),a0
	move.l	a0,-(sp)

	lea	color,a0
	move.l	table,a1
	move.l	newcolor(a1),a1
	jsr	(a1)

	move.l	ehandle,a0
	move.l	(a0),a0
	add.l	(sp)+,a0

gclr9:	move.b	(a0)+,d0
	beq	rtok
	cmp.b	#CR,d0
	beq	gclr9
	cmp.b	#LF,d0
	beq	gclr9
	cmp.b	#" ",d0
	beq	gclr9
	cmp.b	#"%",d0
	bne	rtok
	cmp.b	#"%",(a0)+
	bne	rtok
	cmp.b	#"+",(a0)+
	beq	gclr4
	bra	rtok
*
*
*


putzero:
	move.l	#1,d0
	move.l	ehandle,a0
	move.l	table,a1
	move.l	m_grow(a1),a1
	jsr	(a1)
	beq	rtf0

	move.l	ehandle,a0
	move.l	(a0),a0
	add.l	elength,a0
	clr.b	(a0)
	addq.l	#1,elength
	moveq	#1,d0
	rts


getnum:	move.b	(a0),d1
	moveq	#0,d0
	moveq	#1,d2
	cmp.b	#"-",d1
	beq	gtnm2
	cmp.b	#"0",d1
	bcs	gtnm0
	cmp.b	#"9",d1
	bls	gtnm1
gtnm0:	addq.l	#1,a0
	bra	getnum
gtnm1:	moveq	#0,d0
	moveq	#0,d2
gtnm2:	move.b	(a0)+,d1
	beq	gtnm9
	cmp.b	#"-",d1
	bne	gtnm3
	moveq	#1,d2
	bra	gtnm2
gtnm3:	cmp.b	#" ",d1
	beq	gtnm9
	cmp.b	#CR,d1
	beq	gtnm9
	cmp.b	#LF,d1
	beq	gtnm9
	cmp.b	#".",d1
	beq	gtnm10
	sub.b	#"0",d1
	bcs	gtnm9
	cmp.b	#9,d1
	bhi	gtnm9
	move.l	d0,-(sp)
	lsl.l	#2,d0
	add.l	(sp)+,d0
	lsl.l	#1,d0
	and.l	#$ff,d1
	add.l	d1,d0
	bra	gtnm2
gtnm9:	tst.w	d2
	beq	rrts
	neg.l	d0
	rts

gtnm10:	move.b	(a0)+,d1
	beq	gtnm9
	sub.b	#"0",d1
	bcs	gtnm9
	cmp.w	#9,d1
	bhi	gtnm9
	bra	gtnm10



getclr:	moveq	#0,d0
	move.w	#10000,d1
gtcl1:	move.b	(a0)+,d2
	cmp.b	#" ",d2
	beq	gtcl1
	bra	gtcl3

gtcl2:	move.b	(a0)+,d2
gtcl3:	cmp.b	#".",d2
	beq	gtcl5
	cmp.b	#" ",d2
	beq	gtcl10
	sub.b	#"0",d2
	bcs	gtcl10
	cmp.b	#9,d2
	bhi	gtcl10
	and.w	#$ff,d2
	cmp.w	#10000,d1
	beq	gtcl4
*
* decimal portion
*
	mulu	d1,d2
	add.w	d2,d0
	and.l	#$ffff,d1
	divu	#10,d1
	bra	gtcl2
*
* integer portion (should ONLY be a 0 or a 1)
*
gtcl4:	cmp.b	#2,d2
	bcc	gtcl10
	mulu	d1,d2
	move.w	d2,d0
	bra	gtcl2
*
* decimal point
*
gtcl5:	cmp.w	#10000,d1
	bne	gtcl10
	move.w	#1000,d1
	bra	gtcl2
*
* return number 0-10000
*
gtcl10:	cmp.w	#10000,d0
	bcs	rrts
	move.w	#10000,d0
	rts

*******************************************
***					***
*******************************************
getpict:
        move.l	table,a0                ;open the file for reading
	move.l	f_openr(a0),a0
	jsr	(a0)
	beq     gp10

	move.l	table,a1                ;read the Mac file header into buff1
	move.l	buff1(a1),a0
	move.l	(a0),a0
	move.l	#128,d0
	move.l	f_getr(a1),a1
	jsr	(a1)
        beq     gpe1                    ;if error, close file and rts w/ failure

	move.l	table,a1                ;get the length of the RSRC fork
	move.l	buff1(a1),a0
	move.l	(a0),a0
	add.l	#87,a0
	move.b	(a0)+,rlength
	move.b	(a0)+,rlength+1
	move.b	(a0)+,rlength+2
	move.b	(a0)+,rlength+3
        tst.l   rlength                 ;is there a rsrc fork?
        bne     gp1                     ;yes, so...
        
gpe1:   move.l	table,a0                ;close file if there is no RSRC fork
	move.l	f_closer(a0),a0
	jsr	(a0)
        moveq   #0,d0                   ;signal failure
        rts                             ;and return

gp1:    move.l	table,a1                ;alloc the mem for the rsrc fork read
	move.l	m_alloc(a1),a1
	move.l	rlength,d0
	clr.w	d1
	clr.w	d2
	jsr	(a1)
	beq     gpe1                    ;if I didn't get the memory...
        
	move.l	a0,rhandle
        
        move.l  elength,d0              ;skip to where the rsrc stuff starts
        and.l   #$ffffff80,d0
        add.l   #128,d0
        move.l  table,a0
        move.l  f_skip(a0),a0
        jsr     (a0)
        
        move.l  rhandle,a0              ;read in the RSRC fork
        move.l	(a0),a0
        move.l	rlength,d0
	move.l	table,a1
	move.l	f_getr(a1),a1
	jsr	(a1)
        bne     gp2                     ;if the read goes well...
        
gpe2:   move.l  table,a1                ;else, free the mem
        move.l  m_delete(a1),a1
        move.l  rhandle,a0
        jsr     (a1)
        bra     gpe1                    ;close the file and exit w/ failure

gp2:	move.l	table,a0
	move.l	f_closer(a0),a0
	jsr	(a0)

;seek the PICT rsrc data
;and set picture to point to it

        clr.l   d1
        move.l  rhandle,a0
        move.l  (a0),a0
        move.l  (a0),q                  ;q = offset to start of rsrc data
        move.l  4(a0),d0                ;y = offset to the rsrc map
        move.l  d0,y
        move.b  24(a0,d0.l),d1          ;z = offset to the type list
        lsl.w   #8,d1
        move.b  25(a0,d0.l),d1
        move.l  d1,z
        add.l   d1,d0
        move.b  0(a0,d0.l),d2           ;w = number of items in the ref list
        lsl.w   #8,d2
        move.b  1(a0,d0.l),d2
        move.w  d2,w
        
        move.l  d0,s                    ;s = y+z
        addq.l  #2,d0
    
        ;loop until I hit a PICT rsrc
gp3:    move.b  0(a0,d0.l),d3           ;d3 = type of rsrc
        lsl.l   #8,d3
        move.b  1(a0,d0.l),d3
        lsl.l   #8,d3
        move.b  2(a0,d0.l),d3
        lsl.l   #8,d3
        move.b  3(a0,d0.l),d3

        cmp.l   #'PICT',d3
        beq     gp4
        addq.l  #8,d0
        dbf     d2,gp3
        bra     gpe2                    ;error - no PICT rsrc found so...
        
gp4:    move.b  6(a0,d0.l),d1
        lsl.w   #8,d1
        move.b  7(a0,d0.l),d1

        move.l  s,d0
        add.l   d1,d0
        move.b  5(a0,d0.l),d1
        lsl.l   #8,d1
        move.b  6(a0,d0.l),d1
        lsl.l   #8,d1
        move.b  7(a0,d0.l),d1
        and.l   #$00ffffff,d1
        move.l  q,d0
        add.l   d1,d0
        move.l  d0,d2
        add.l   #14,d2
        move.l  d2,pictoffset           ;= offset from rhandle to PICT data
        lea     0(a0,d0.l),a1
        add.l   #14,a1
        move.l  a1,picture              ;= address of the PICT data in rhandle
        
        move.b  6(a0,d0.l),d1
        lsl.w   #8,d1
        move.b  7(a0,d0.l),d1
        
        move.b  8(a0,d0.l),d2
        lsl.w   #8,d2
        move.b  9(a0,d0.l),d2
        
        move.b  10(a0,d0.l),d3
        lsl.w   #8,d3
        move.b  11(a0,d0.l),d3
        
        move.b  12(a0,d0.l),d4
        lsl.w   #8,d4
        move.b  13(a0,d0.l),d4
        
        sub.w   d1,d3
        sub.w   d2,d4
        move.w  d4,pic+pc_W
        move.w  d3,pic+pc_H
        
        move.l  picture,a0
gp5:    cmp.b   #$11,(a0)+              ;look for the version code ($11)
        bne     gp5
        move.b  (a0)+,vers              ;store the PICT version #
        
        cmp.b   #1,vers
        beq     gp6
        add.l   #1,a0                   ;if vers==2 skip the next byte
gp6:    bsr     getcode                 ;this will get the PICT code into d0
        cmp.w   #$98,d0
        beq     gp9
        bsr     skipcode
        bra     gp6
        
gp9:    move.w  #1,d0                   ;default to 1 bit-per-pixel
        move.w	#PC_BW,pic+pc_Type      ;default to a BW PICT
        clr.w   pic+pc_Palet            ;default to no palette
        cmp.b   #1,vers                 ;is it a version 1 PICT?
        beq     gp90                    ;yes, only 1 bpp
        
        move.w  #PC_PALET,pic+pc_Type
        
        move.b  28(a0),d0               ;get the bits-per-pixel for this image
        lsl.w   #8,d0
        move.b  29(a0),d0
        move.b  52(a0),d1               ;get the number of palette entries
        lsl.w   #8,d1
        move.b  53(a0),d1
        move.w  d1,pic+pc_Palet
        
gp90:   move.w  d0,pic+pc_BitPln
        
        sub.l   #1,a0                   ;rewind to start of $98 code
        cmp.b   #1,vers
        beq     gp91
        sub.l   #1,a0
gp91:   move.l  a0,picture
        moveq   #1,d0                   ;signal PICT is OK    
gp10:   rts

        
*******************************************
***					***
*******************************************
getcode:
        clr.w   d0                      ;clear space for next command
        cmp.b   #1,vers                 ;is this a version 1 PICT
        bne     gc2                     ;nope
        move.b  (a0)+,d0                ;if yes, get q 1 byte code
        bra     gc3
gc2:    move.l  a0,d0                   ;check for word-align in version 2 PICT
        btst    #0,d0
        beq     gc21                    ;yes it is aligned
        add.l   #1,a0                   ;if not, align it
gc21:   move.w  (a0)+,d0                ;else, get a 2 byte code
gc3:    rts


*******************************************
***					***
*******************************************
skipcode:
        lea     codetable,a1
        
sc1:    tst.w   (a1)                    ;end of table?
        beq     sc4                     ;yes
        cmp.w   (a1),d0                 ;look for the code contained in d0
        beq     sc2
        add.l   #4,a1
        bra     sc1
        
sc2:    move.w  2(a1),d1
        cmp.w   #$ffff,d1               ;-1 signals the len is in the next word
        bne     sc3
        move.b  (a0)+,d1                ;get the length from the data (word)
        lsl.w   #8,d1
        move.b  (a0)+,d1
        sub.w   #2,d1
        
sc3:    cmp.w   #$fffe,d1               ;-2 signals the len is in the next long
        beq     sc6
        
        add.w   d1,a0                   ;skip the data for this code
        rts

sc4:    cmp.w   #$7fff,d0               ;is it greater than $7fff?
        bcc     sc5                     ;yes
        lsr.w   #8,d0
        and.l   #$000000ff,d0
        moveq   #1,d1
        lsl.l   d1,d0
        add.w   d1,a0
        rts
        
sc5:    cmp.w   #$80ff,d0               ;$8000-$80ff is 0 length
        bcc     sc6
        rts
        
sc6:    move.b  (a0)+,d1                ;get the length from the data (long)
        lsl.l   #8,d1
        move.b  (a0)+,d1
        lsl.l   #8,d1
        move.b  (a0)+,d1
        lsl.l   #8,d1
        move.b  (a0)+,d1
        subq    #4,d1
        add.l   d1,a0
        rts
        
        
*******************************************
***					***
*******************************************
putpal:
        move.l  palptr,a1
        add.l   #56,a0                  ;point to start of palette data
        move.w  pic+pc_Palet,d0
        
ptpl1:  move.b  (a0)+,d1                 ;get the color number
        lsl.w   #8,d1
        move.b  (a0)+,d1
        mulu    #pl_Sizeof,d1           ;claculate its pos in PgS obj palette
        
        move.b  (a0)+,0(a1,d1.w)       ;red
        move.b  (a0)+,1(a1,d1.w)
        move.w  0(a1,d1.w),d2
        
        move.b  (a0)+,2(a1,d1.w)       ;green
        move.b  (a0)+,3(a1,d1.w)
        move.w  2(a1,d1.w),d3
        
        move.b  (a0)+,4(a1,d1.w)       ;blue
        move.b  (a0)+,5(a1,d1.w)
        move.w  4(a1,d1.w),d4
        
        mulu	#19661,d2
	mulu	#38666,d3
	mulu	#7209,d4
	add.l	d3,d2
	add.l	d4,d2
	swap	d2
	cmp.w	#$8000,d2
	bcc	ge1
	moveq	#1,d2
	bra	ge2
ge1:	moveq	#0,d2
ge2:	move.w	d2,6(a1,d1.w)		;screen color
        
        dbf     d0,ptpl1
        rts
        
        
*******************************************
***					***
*******************************************
putpic:
        bsr     getcode
        
        cmp.w   #$98,d0
        bne     ptpc1
        
        move.b  (a0)+,rowbytes
        move.b  (a0)+,rowbytes+1
        move.w  rowbytes,d0
        bclr    #15,d0                  ;clear the high bit of word
        move.w  d0,rowbytes
        
        move.b  (a0)+,bby1
        move.b  (a0)+,bby1+1
        
        move.b  (a0)+,bbx1
        move.b  (a0)+,bbx1+1
        
        move.b  (a0)+,bby2
        move.b  (a0)+,bby2+1
        
        move.b  (a0)+,bbx2
        move.b  (a0)+,bbx2+1
        
        cmp.b   #1,vers
        beq     pp10
        
        ;version 2 stuff goes here
        add.l   #16,a0                  ;skip the next 8 words
        
        move.b  (a0)+,pixeltype
        move.b  (a0)+,pixeltype+1
        
        move.b  (a0)+,bpp
        move.b  (a0)+,bpp+1
        
        add.l   #22,a0                  ;skip the next 10 words
        
        clr.l   d0
        move.b  (a0)+,d0                ;the number of colors in the color table
        lsl.l   #8,d0
        move.b  (a0)+,d0
        addq    #1,d0
        lsl.l   #3,d0                   ;mulu by 8
        add.l   d0,a0                   ;skip it
        
pp10:   move.b  (a0)+,srcy1
        move.b  (a0)+,srcy1+1
        
        move.b  (a0)+,srcx1
        move.b  (a0)+,srcx1+1
        
        move.b  (a0)+,srcy2
        move.b  (a0)+,srcy2+1
        
        move.b  (a0)+,srcx2
        move.b  (a0)+,srcx2+1
        
        move.b  (a0)+,desty1
        move.b  (a0)+,desty1+1
        
        move.b  (a0)+,destx1
        move.b  (a0)+,destx1+1
        
        move.b  (a0)+,desty2
        move.b  (a0)+,desty2+1
        
        move.b  (a0)+,destx2
        move.b  (a0)+,destx2+1
        
        move.b  (a0)+,copymode          ;get the mode of byte copy used
        move.b  (a0)+,copymode+1
        
        move.w  bby2,d1                 ;calc number of rows in this code group
        sub.w   bby1,d1
        move.w  d1,numrows
        
        move.l  picptrsave,a1
        clr.l   d0
        move.w  pic+pc_Width,d0         ;calc bitmap addr of starting dest row
        mulu    desty1,d0
        add.l   d0,a1
        move.w  destx1,d0               ;get the starting x value
        addq.w  #7,d0   
        lsr.w   #3,d0
        add.w   d0,a1
        move.l  a1,picptr               ;store it away for pckcomp
        
        cmp.b   #1,vers
        bne     pp12
        bsr     pckcompv1               ;decompress the data and put it vers 1
        bra     putpic                  ;time to get the next code
pp12:   bsr     pckcompv2               ;decompress the data and put it vers 2
        bra     putpic                  ;time to get the next code
        
ptpc1:  cmp.w   #$ff,d0                 ;end of PICT data
        beq     ptpc2                   ;so exit
        bsr     skipcode
        bra     putpic                  ;get the next code
                
ptpc2:  rts
        
**********************************
**                              **
**********************************
pckcompv1:
        clr.l   d6
        clr.l   d0
        
        move.w  numrows,d5      ;d5 = bitmap height in this group
        move.w  rowbytes,d6     ;get the # of bytes in this row in this group
        move.w  d6,d0           ;d0 = save d6
	bra	pk8

pk1:	move.w	d0,d6           ;reset d6 to number of bytes in row
	move.l	picptr,a1       ;a1 = adress in amiga bitmap
        clr.l   d1
        move.b  (a0)+,d1        ;throw away packed byte count at start of line

pk2:    clr.l   d1
        move.b  (a0)+,d1        ;d1 = one byte code
        cmp.b   #128,d1
        beq     pk2             ;code 128 = do nothing, so get next byte
        
        btst    #7,d1           ;check the high bit
        beq     op2             ;if 0 copy the next d1+1 bytes as is
        
op1:    neg.b   d1              ;else copy the next byte -d1+1 times
        sub.b   d1,d6           ;decrement row byte count
        subq.w  #1,d6
        
        move.b  (a0)+,d2        ;get the byte to copy
op12:   move.b  (a1),d3
        bsr     domode          ;convert d2 and d3 according to the copy mode
        move.b  d3,(a1)+        ;copy the byte
        dbf     d1,op12         ;loop until done (d1=-1)
        bra     pk6

op2:    sub.b   d1,d6           ;decrement row byte count
        subq.w  #1,d6

op22:   move.b  (a0)+,d2
        move.b  (a1),d3
        bsr     domode          ;convert d2 and d3 according to the copy mode
        move.b  d3,(a1)+
	dbf     d1,op22

pk6:    tst.w   d6              ;are we done with a row yet
        bne     pk2             ;no - keep going
        
        move.l  a1,d6
        btst    #0,d6           ;did we end on an odd address
        beq     pk7
        move.b  #0,(a1)+

pk7:	move.l	picptr,a1
	add.w	pic+pc_Width,a1
	move.l	a1,picptr
        
pk8:    dbf     d5,pk1
        rts

**********************************
**                              **
**********************************
pckcompv2:
        clr.l   d6
        clr.l   d0
        
        move.w  numrows,d5      ;d5 = bitmap height in this group
        move.w  rowbytes,d6     ;get the # of bytes in this row in this group
        move.w  d6,d0           ;d0 = save d6
	bra	p2k8

p2k1:	move.w	d0,d6           ;reset d6 to number of bytes in row
	move.l	picptr,a1       ;a1 = adress in amiga bitmap
        clr.l   d1
        move.b  (a0)+,d1        ;throw away packed byte count at start of line

p2k2:   clr.l   d1
        move.b  (a0)+,d1        ;d1 = one byte code
        cmp.b   #128,d1
        beq     p2k2            ;code 128 = do nothing, so get next byte
        
        btst    #7,d1           ;check the high bit
        beq     o2p2            ;if 0 copy the next d1+1 bytes as is
        
o2p1:   neg.b   d1              ;else copy the next byte -d1+1 times
        sub.b   d1,d6           ;decrement row byte count
        subq.w  #1,d6
        
        move.b  (a0)+,d2        ;get the byte to copy
o2p12:  move.b  (a1),d3
        bsr     domode          ;convert d2 and d3 according to the copy mode
        bsr     copyone
        dbf     d1,o2p12
        bra     p2k6

o2p2:   sub.b   d1,d6           ;decrement row byte count
        subq.w  #1,d6

o2p22:  move.b  (a0)+,d2
        move.b  (a1),d3
        bsr     domode          ;convert d2 and d3 according to the copy mode
        bsr     copyone
        dbf     d1,o2p22

p2k6:   tst.w   d6              ;are we done with a row yet
        bne     p2k2            ;no - keep going
        
        move.l  a1,d6
        btst    #0,d6           ;did we end on an odd address
        beq     p2k7
        move.b  #0,(a1)+

p2k7:   move.l	picptr,a1
        move.w  pic+pc_Width,d4
	add.w	d4,a1
	move.l	a1,picptr
        move.w  #7,abit
        
p2k8:   dbf     d5,p2k1
        rts

**********************************
**                              **
**********************************
domode:
        cmp.w   #SRCCOPY,copymode
        bne     dm1
        move.b  d2,d3                   ;copy the value exactly
        rts
        
dm1:    cmp.w   #SRCOR,copymode         ;or d2 bits with d3 bits
        bne     dm2
        or.b    d2,d3
        rts
        
dm2:    cmp.w   #SRCXOR,copymode        ;xor d2 bits with d3 bits
        bne     dm3
        eor.b   d2,d3
        rts
        
dm3:    cmp.w   #SRCBIC,copymode        ;erase d2 bits from d3 bits
        bne     dm4
        not.b   d2
        and.b   d2,d3
        rts
        
dm4:    cmp.w   #NOTSRCCOPY,copymode    ;copy the opposite of d2 bits to d3
        bne     dm5
        not.b   d2
        move.b  d2,d3
        rts
        
dm5:    cmp.w   #NOTSRCOR,copymode      ;or not d2 bits with d3 bits
        bne     dm6
        not.b   d2
        or.b    d2,d3
        rts
        
dm6:    cmp.w   #NOTSRCXOR,copymode     ;xor not d2 bits with d3 bits
        bne     dm7
        not.b   d2
        eor.b   d2,d3
        rts
        
dm7:    cmp.w   #NOTSRCBIC,copymode     ;copy only the same bits in d2 to d3
        bne     dm8
        and.b   d2,d3
        rts
        
dm8:    move.b  d2,d3                   ;default to SRCCOPY
        rts

**********************************
**                              **
**********************************        
copyone:
        movem.l d0-d2,-(a7)
        
        move.w  bpp,d0
        subq.w  #1,d0
        move.w  #0,d1
        move.l  a1,a2
        move.w  abit,d2
        
cp0:    btst    d1,d3
        bne     cp1
        bclr    d2,(a2)
        bra     cp2
cp1:    bset    d2,(a2)
cp2:    dbf     d0,cp3          ;decrement source bitplane counter
        move.l  a1,a2           ;reset dest to plane one
        move.w  bpp,d0          ;reset source bitplane counter
        subq.w  #1,d0

        dbf     d2,cp4          ;decrement amiga bitplane bit pointer
        move.w  #7,d2           ;reset to high bit
        add.l   #1,a1           ;move dest to next byte
        move.l  a1,a2           ;reset dest to plane one
        bra     cp4

cp3:    add.l   planesize,a2    ;move to next dest bitplane
cp4:    ;dbf     d1,cp0          ;decrement source bit pointer
        addq.w  #1,d1
        cmp.w   bpp,d1
        bcs     cp0
        
cp10:   move.w  d2,abit
        movem.l (a7)+,d0-d2
        rts
        
*******************************************************************
***	called when the import routine choosen finds something	***
***	wrong with the file loaded.				***
*******************************************************************
notcorrect:
	rts

never:	moveq	#0,d0
	rts

maybe:	moveq	#1,d0
	rts

correct:
	moveq	#2,d0
	rts


*******************************************
***					***
*******************************************
 SECTION maceps,DATA,PUBLIC

special:
	dc.w	2
	dc.l	spcl1,spcl2
        
codetable:      dc.w    $0001,$ffff
                dc.w    $0002,8
                dc.w    $0003,2
                dc.w    $0004,1
                dc.w    $0005,2
                dc.w    $0006,4
                dc.w    $0007,4
                dc.w    $0008,2
                dc.w    $0009,8
                dc.w    $000a,8
                dc.w    $000b,4
                dc.w    $000c,4
                dc.w    $000d,2
                dc.w    $000e,4
                dc.w    $000f,4
                
                dc.w    $0010,8
                dc.w    $0011,1
                dc.w    $0012,0         ;?
                dc.w    $0013,0         ;?
                dc.w    $0014,0         ;?
                dc.w    $0015,2
                dc.w    $0016,2
                dc.w    $0017,0
                dc.w    $0018,0
                dc.w    $0019,0
                dc.w    $001a,0         ;?
                dc.w    $001b,0         ;?
                dc.w    $001c,0
                dc.w    $001d,0         ;?
                dc.w    $001e,8
                dc.w    $001f,4
                
                dc.w    $0020,8
                dc.w    $0021,4
                dc.w    $0022,6
                dc.w    $0023,2
                dc.w    $0024,$ffff
                dc.w    $0025,$ffff
                dc.w    $0026,$ffff
                dc.w    $0027,$ffff
                dc.w    $0028,0         ;? 5+text
                dc.w    $0029,$ffff
                dc.w    $002a,0         ;? 2+text
                dc.w    $002b,0         ;? 3+text
                dc.w    $002c,$ffff
                dc.w    $002d,$ffff
                dc.w    $002e,$ffff
                dc.w    $002f,$ffff
                                
                dc.w    $0030,8
                dc.w    $0031,8
                dc.w    $0032,8
                dc.w    $0033,8
                dc.w    $0034,8
                dc.w    $0035,8
                dc.w    $0036,8
                dc.w    $0037,8
                dc.w    $0038,0
                dc.w    $0039,0
                dc.w    $003a,0
                dc.w    $003b,0
                dc.w    $003c,0
                dc.w    $003d,0
                dc.w    $003e,0
                dc.w    $003f,0
                
                dc.w    $0040,8
                dc.w    $0041,8
                dc.w    $0042,8
                dc.w    $0043,8
                dc.w    $0044,8
                dc.w    $0045,8
                dc.w    $0046,8
                dc.w    $0047,8
                dc.w    $0048,0
                dc.w    $0049,0
                dc.w    $004a,0
                dc.w    $004b,0
                dc.w    $004c,0
                dc.w    $004d,0
                dc.w    $004e,0
                dc.w    $004f,0
                
                dc.w    $0050,8
                dc.w    $0051,8
                dc.w    $0052,8
                dc.w    $0053,8
                dc.w    $0054,8
                dc.w    $0055,8
                dc.w    $0056,8
                dc.w    $0057,8
                dc.w    $0058,0
                dc.w    $0059,0
                dc.w    $005a,0
                dc.w    $005b,0
                dc.w    $005c,0
                dc.w    $005d,0
                dc.w    $005e,0
                dc.w    $005f,0
                
                dc.w    $0060,12
                dc.w    $0061,12
                dc.w    $0062,12
                dc.w    $0063,12
                dc.w    $0064,12
                dc.w    $0065,12
                dc.w    $0066,12
                dc.w    $0067,12
                dc.w    $0068,4
                dc.w    $0069,4
                dc.w    $006a,4
                dc.w    $006b,4
                dc.w    $006c,4
                dc.w    $006d,4
                dc.w    $006e,4
                dc.w    $006f,4
                
                dc.w    $0070,0         ;poly
                dc.w    $0071,0         ;poly
                dc.w    $0072,0         ;poly
                dc.w    $0073,0         ;poly
                dc.w    $0074,0         ;poly
                dc.w    $0075,0         ;poly
                dc.w    $0076,0         ;poly
                dc.w    $0077,0         ;poly
                dc.w    $0078,0
                dc.w    $0079,0
                dc.w    $007a,0
                dc.w    $007b,0
                dc.w    $007c,0
                dc.w    $007d,0
                dc.w    $007e,0
                dc.w    $007f,0
                
                dc.w    $0080,0         ;region
                dc.w    $0081,0         ;region
                dc.w    $0082,0         ;region
                dc.w    $0083,0         ;region
                dc.w    $0084,0         ;region
                dc.w    $0085,0         ;region
                dc.w    $0086,0         ;region
                dc.w    $0087,0         ;region
                dc.w    $0088,0
                dc.w    $0089,0
                dc.w    $008a,0
                dc.w    $008b,0
                dc.w    $008c,0
                dc.w    $008d,0
                dc.w    $008e,0
                dc.w    $008f,0
                
                dc.w    $0090,0         ;?
                dc.w    $0091,0         ;?
                dc.w    $0092,$ffff
                dc.w    $0093,$ffff
                dc.w    $0094,$ffff
                dc.w    $0095,$ffff
                dc.w    $0096,$ffff
                dc.w    $0097,$ffff
                dc.w    $0098,0         ;? - need not worry about this one
                dc.w    $0099,0         ;?
                dc.w    $009a,$ffff
                dc.w    $009b,$ffff
                dc.w    $009c,$ffff
                dc.w    $009d,$ffff
                dc.w    $009e,$ffff
                dc.w    $009f,$ffff
                
                dc.w    $00a0,2
                dc.w    $00a1,0         ;? 2+2+text
                dc.w    $00a2,$ffff
                dc.w    $00a3,$ffff
                dc.w    $00a4,$ffff
                dc.w    $00a5,$ffff
                dc.w    $00a6,$ffff
                dc.w    $00a7,$ffff
                dc.w    $00a8,$ffff
                dc.w    $00a9,$ffff
                dc.w    $00aa,$ffff
                dc.w    $00ab,$ffff
                dc.w    $00ac,$ffff
                dc.w    $00ad,$ffff
                dc.w    $00ae,$ffff
                dc.w    $00af,$ffff
                
                dc.w    $00b0,0
                dc.w    $00b1,0
                dc.w    $00b2,0
                dc.w    $00b3,0
                dc.w    $00b4,0
                dc.w    $00b5,0
                dc.w    $00b6,0
                dc.w    $00b7,0
                dc.w    $00b8,0
                dc.w    $00b9,0
                dc.w    $00ba,0
                dc.w    $00bb,0
                dc.w    $00bc,0
                dc.w    $00bd,0
                dc.w    $00be,0
                dc.w    $00bf,0
                
                dc.w    $00c0,0
                dc.w    $00c1,0
                dc.w    $00c2,0
                dc.w    $00c3,0
                dc.w    $00c4,0
                dc.w    $00c5,0
                dc.w    $00c6,0
                dc.w    $00c7,0
                dc.w    $00c8,0
                dc.w    $00c9,0
                dc.w    $00ca,0
                dc.w    $00cb,0
                dc.w    $00cc,0
                dc.w    $00cd,0
                dc.w    $00ce,0
                dc.w    $00cf,0
                
                dc.w    $00d0,$fffe
                dc.w    $00d1,$fffe
                dc.w    $00d2,$fffe
                dc.w    $00d3,$fffe
                dc.w    $00d4,$fffe
                dc.w    $00d5,$fffe
                dc.w    $00d6,$fffe
                dc.w    $00d7,$fffe
                dc.w    $00d8,$fffe
                dc.w    $00d9,$fffe
                dc.w    $00da,$fffe
                dc.w    $00db,$fffe
                dc.w    $00dc,$fffe
                dc.w    $00dd,$fffe
                dc.w    $00de,$fffe
                dc.w    $00df,$fffe
                
                dc.w    $00f0,$fffe
                dc.w    $00f1,$fffe
                dc.w    $00f2,$fffe
                dc.w    $00f3,$fffe
                dc.w    $00f4,$fffe
                dc.w    $00f5,$fffe
                dc.w    $00f6,$fffe
                dc.w    $00f7,$fffe
                dc.w    $00f8,$fffe
                dc.w    $00f9,$fffe
                dc.w    $00fa,$fffe
                dc.w    $00fb,$fffe
                dc.w    $00fc,$fffe
                dc.w    $00fd,$fffe
                dc.w    $00fe,$fffe
                dc.w    $00ff,2
                
                dc.w    0,0


version:        dc.b    0,"$VER: "
name:	        dc.b	"Mac EPSF v2.1.2",0

spcl1:	        dc.b	"Show pic",0
spcl2:          dc.b    "Do not show pic",0

bbstr:		dc.b	"%%BoundingBox:",0
ccstr:		dc.b	"%%CMYKCustomColor:",0
epsfhdr:	dc.b	"%!PS-Adobe",0

vers:           dc.b    0


******************************************************
******************************************************
 SECTION maceps,BSS,PUBLIC

data:		ds.w	1
object:		ds.w	ob_SizeOf/2
color:	        ds.w	cl_Sizeof/2
pic:	        ds.w	pc_Sizeof/2

array:		ds.w	3

eleft:		ds.l	1
etop:		ds.l	1
eright:		ds.l	1
ebottom:	ds.l	1

ehandle:	ds.l	1
elength:	ds.l	1
rhandle:        ds.l    1
rlength:        ds.l    1

picture:        ds.l    1
pictoffset:     ds.l    1
picptr:         ds.l    1
picptrsave:     ds.l    1
palptr:         ds.l    1        
spsave:         ds.l    1
eobj:           ds.l    1
q:              ds.l    1
y:              ds.l    1
z:              ds.l    1
s:              ds.l    1
planesize:      ds.l    1
w:              ds.w    1
rowbytes:       ds.w    1
numrows:        ds.w    1
bbx1:           ds.w    1
bby1:           ds.w    1
bbx2:           ds.w    1
bby2:           ds.w    1
srcx1:          ds.w    1
srcy1:          ds.w    1
srcx2:          ds.w    1
srcy2:          ds.w    1
destx1:         ds.w    1
desty1:         ds.w    1
destx2:         ds.w    1
desty2:         ds.w    1
copymode:       ds.w    1
pixeltype:      ds.w    1
bpp:            ds.w    1
abit:           ds.w    1


;***************************** Changes **************************
;
;       version 2.1.2
;
;       - added a "version" string
;
;       - adding support for PICT headers
