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

 SECTION degas,CODE,PUBLIC

***************************************************
***						***
***************************************************
dumbentry:
	clr.l	d0
	rts

	dc.l	"IMPP"		; magik number for pic import
	dc.w	200
	dc.l	0

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

*
* Degas
*
check:	move.l	table,a0
	move.l	flen(a0),a1
	cmp.l	#32128,(a1)
	bhi	never
	cmp.l	#32034,(a1)
	bcs	never
cdgs1:	move.l	buff1(a0),a1
	move.l	(a1),a1
	move.w	(a1),d0
	cmp.w	#3,d0
	bcc	never
;	move.l	fname(a0),a1
;cdgs2:	move.b	(a1)+,d0
;	beq	maybe
;	cmp.b	#'.',d0
;	bne	cdgs2
;	cmp.b	#'P',(a1)+
;	bne	maybe
;	cmp.b	#'I',(a1)+
;	bne	maybe
;	cmp.b	#'1',(a1)
;	bcs	maybe
;	cmp.b	#'3',(a1)
;	bhi	maybe
	moveq	#2,d0
drts:	rts
*
*
* the picture is already in buff1
*
degas:	move.l	table,a0
	move.l	f_openr(a0),a0
	jsr	(a0)
	beq	drts

	move.l	table,a0
	move.l	spclnum(a0),a0
	cmp.w	#1,(a0)
	beq	impobj
*
*
*
imppic:	move.l	table,a0
	move.l	p_open(a0),a0
	jsr	(a0)
	beq	drts

	bsr	getpicinfo

	move.l	table,a4
	move.l	cwptr(a4),a4		;ptr to window handle
	move.l	(a4),a4			;window handle
	move.l	(a4),a4			;ptr to window structure

	lea	pic,a0
	move.w	pc_Type(a0),pi_Type(a4)
	move.w	pc_SFreq(a0),pi_SFreq(a4)
	move.w	pc_SAngle(a0),pi_SAngle(a4)
	move.w	pc_SSpot(a0),pi_SSpot(a4)
	move.l	pc_SMap(a0),pi_SMap(a4)
	move.l	pc_SMap+4(a0),pi_SMap+4(a4)
	move.w	pc_XDpi(a0),pi_XDpi(a4)
	move.w	pc_YDpi(a0),pi_YDpi(a4)
	move.w	pc_W(a0),pi_W(a4)
	move.w	pc_H(a0),pi_H(a4)
	move.w	pc_Width(a0),pi_Width(a4)
	move.w	pc_BitPln(a0),pi_BitPln(a4)
	move.w	pc_Palet(a0),pi_Palet(a4)

	move.w	pi_Palet(a4),d1
	mulu	#pl_Sizeof,d1
	move.w	pi_Width(a4),d0
	mulu	pi_BitPln(a4),d0
	mulu	pi_H(a4),d0
	add.l	d1,d0		;size of bitmap +( 4 words * #colors)

	clr.w	d1
	clr.w	d2
	move.l	table,a0
	move.l	m_alloc(a0),a0
	jsr	(a0)
	beq	abortpic

	move.l	table,a3
	move.l	cwptr(a3),a3
	move.l	(a3),a3
	move.l	(a3),a3
	move.l	a0,pi_Ptr(a3)
	move.l	(a0),a4
	move.l	a4,picptr

	bsr	putcolor
	bsr	putpic

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

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

abortpic:
	move.l	table,a0
	move.l	p_abort(a0),a0
	jsr	(a0)

	move.l	table,a0
	move.l	f_closer(a0),a0
	jmp	(a0)
*
*
*
impobj:	move.l	table,a0
	move.l	o_open(a0),a0
	jsr	(a0)
	beq	drts

	bsr	getpicinfo

	move.b	#tpic,object+ob_Type
	clr.l	object+ob_Left
	clr.l	object+ob_Top

	move.l	#7200,d0
	move.w	pic+pc_W,d1
	bsr	Mulu1632
	move.w	pic+pc_XDpi,d2
	bsr	Divu1648
	move.l	d0,object+ob_Right

	move.l	#7200,d0
	move.w	pic+pc_H,d1
	bsr	Mulu1632
	move.w	pic+pc_YDpi,d2
	bsr	Divu1648
	move.l	d0,object+ob_Bottom

	clr.b	object+ob_Flag
	clr.w	object+ob_Slant
	clr.w	object+ob_Twist
	move.w	#50,object+ob_LWidth		;1 point line
	clr.b	object+ob_LType			;no line
	move.b	#1,object+ob_LColor		;black line
	clr.b	object+ob_FType			;no fill
	move.b	#1,object+ob_FColor		;black fill
	clr.b	object+ob_LBegin
	clr.b	object+ob_LEnd
	move.w	#900,object+ob_HStandOff	;1/8 inch
	move.w	#900,object+ob_VStandOff	;1/8 inch
*
*
	move.l	table,a1
	move.l	o_obj(a1),a1
	lea	object,a0
	jsr	(a1)
	beq	abortobj

	move.w	pic+pc_Width,d0
	mulu	pic+pc_BitPln,d0
	mulu	pic+pc_H,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

	lea	pic,a1
	clr.l	pc_DPtr(a1)
	clr.w	pc_DScale(a1)
	move.w	pc_Palet(a1),d0
	mulu	#pl_Sizeof,d0
	add.l	#pc_Sizeof,d0
	move.l	d0,pc_Ptr(a1)

	move.w	#pc_Sizeof/2-1,d0
oppic1:	move.w	(a1)+,(a0)+
	dbf	d0,oppic1
	move.l	a0,picptr

	bsr	putcolor
	bsr	putpic

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

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

abortobj:
	move.l	table,a0
	move.l	o_abort(a0),a0
	jsr	(a0)

	move.l	table,a0
	move.l	f_closer(a0),a0
	jmp	(a0)
*
*
*
getpicinfo:
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)

	lea	dgstbl,a0
	lsl.w	#2,d0
	move.l	0(a0,d0.w),a0

	lea	pic,a4
	move.w	(a0)+,pc_Palet(a4)	;number of color pallet entries
	move.w	(a0)+,pc_BitPln(a4)
	move.w	(a0)+,pc_W(a4)
	move.w	(a0)+,pc_H(a4)
	move.w	(a0)+,pc_Width(a4)
	move.w	(a0)+,pc_XDpi(a4)
	move.w	(a0),pc_YDpi(a4)

	clr.w	pc_Type(a4)
	move.w	#-1,pc_SFreq(a4)
	move.w	#-1,pc_SAngle(a4)
	clr.w	pc_SSpot(a4)
	clr.l	pc_SMap(a4)
	clr.l	pc_SMap+4(a4)
	rts



putcolor:
	move.w	pic+pc_Palet,temp1		;# of colors in bit map
	cmp.w	#2,temp1
	beq	dgsmono

dgsp2:	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)

	move.w	d0,d1
	move.w	d0,d2
	lsr.w	#8,d0
	lsr.w	#4,d1
	and.w	#%111,d0		;red
	and.w	#%111,d1		;green
	and.w	#%111,d2		;blue
	mulu	#65535,d0
	mulu	#65535,d1
	mulu	#65535,d2
	divu	#7,d0
	divu	#7,d1
	divu	#7,d2

	move.l	picptr,a4
	move.w	d0,(a4)+		;red
	move.w	d1,(a4)+		;green
	move.w	d2,(a4)+		;blue

	mulu	#19661,d0
	mulu	#38666,d1
	mulu	#7209,d2
	add.l	d1,d0
	add.l	d2,d0
	swap	d0
	cmp.w	#$8000,d0
	bcc	pcwhite
	moveq	#1,d0
	bra	pcblack
pcwhite:
	moveq	#0,d0
pcblack:
	move.w	d0,(a4)+
	move.l	a4,picptr

	subq.w	#1,temp1
	bne	dgsp2

	move.l	#16,d0			;skip over unused color pallete entries
	sub.w	pic+pc_Palet,d0
	lsl.w	#1,d0
	move.l	table,a0
	move.l	f_skip(a0),a0
	jmp	(a0)

dgsmono:
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	cmp.w	#$777,d0
	beq	wb
	cmp.w	#$1,d0
	beq	wb

	move.l	picptr,a4
	clr.w	(a4)+
	clr.w	(a4)+
	clr.w	(a4)+
	move.w	#1,(a4)+
	move.w	#$ffff,(a4)+
	move.w	#$ffff,(a4)+
	move.w	#$ffff,(a4)+
	clr.w	(a4)+
	move.l	a4,picptr
	bra	bw

wb:	move.l	picptr,a4
	move.w	#$ffff,(a4)+		;red	;white
	move.w	#$ffff,(a4)+		;green
	move.w	#$ffff,(a4)+		;blue
	clr.w	(a4)+
	clr.w	(a4)+			;red	;black
	clr.w	(a4)+			;green
	clr.w	(a4)+			;blue
	move.w	#1,(a4)+
	move.l	a4,picptr

bw:	move.l	#30,d0			;skip over unused color pallete entries
	move.l	table,a0
	move.l	f_skip(a0),a0
	jmp	(a0)

*
*
*
putpic:	move.w	pic+pc_Width,d0
	lsr.w	#1,d0
	move.w	d0,dwidth

	move.w	pic+pc_Width,d0
	mulu	pic+pc_H,d0
	move.l	d0,plane

dgsuc1:	move.w	dwidth,temp2

dgsuc2:	move.w	pic+pc_BitPln,temp3
	move.l	picptr,a1

dgsuc3:	move.l	a1,-(sp)
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	move.l	(sp)+,a1

	move.w	d0,(a1)
	add.l	plane,a1

	subq.w	#1,temp3
	bne	dgsuc3

	addq.l	#2,picptr
	subq.w	#1,temp2
	bne	dgsuc2

	subq.w	#1,pic+pc_H
	bne	dgsuc1
*
	rts

Mulu1632:
	move.l	table,a0
	move.l	mulu1632(a0),a0
	jmp	(a0)

Divu1648:
	move.l	table,a0
	move.l	divu1648(a0),a0
	jmp	(a0)

*******************************************************************
***	called when the import routine choosen finds something	***
***	wrong with the file loaded.				***
*******************************************************************
notright:
	rts

never:	moveq	#0,d0
	rts

maybe:	moveq	#1,d0
	rts

right:	moveq	#2,d0
	rts

*************************************************************
*************************************************************
 SECTION degas,DATA,PUBLIC
*
special:
	dc.w	2
	dc.l	spcl1,spcl2
*
dgstbl:	dc.l	dgslow,dgsmed,dgshi
dgslow:	dc.w	16,4,320,200,40,36,36
dgsmed:	dc.w	4,2,640,200,80,72,36
dgshi:	dc.w	2,1,640,400,80,77,77

*
*
*
name:	dc.b	"Degas Picture v2.1.1",0
*
spcl1:	dc.b	"Picture Window",0
spcl2:	dc.b	"Object",0
*

 SECTION degas,BSS,PUBLIC
temp1:	ds.w	1
temp2:	ds.w	1
temp3:	ds.w	1

dwidth:	ds.w	1
bitpln:	ds.w	1

plane:	ds.l	1

picptr:	ds.l	1

object:	ds.w	ob_SizeOf/2
pic:	ds.w	pc_Sizeof/2
