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

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

	dc.l	"IMPP"
	dc.w	200
	dc.l	0

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


* tny format
*
* the first 32k of the picture is in buff1
*
check:	move.l	table,a0
	move.l	buff1(a0),a1
	move.l	(a1),a1
	moveq	#5,d2
	move.b	(a1)+,d0
	cmp.b	#6,d0
	bcc	never
	cmp.b	#3,d0
	bcs	chk1
	addq.l	#4,a1
	addq.l	#4,d2
chk1:	move.w	#15,d1
chk2:	move.b	(a1)+,d0
	and.b	#%11111000,d0
	bne	never
	move.b	(a1)+,d0
	and.b	#%10001000,d0
	bne	never
	addq.l	#2,d2
	dbf	d1,chk2
	move.b	(a1)+,d0
	lsl.w	#8,d0
	move.b	(a1)+,d0
	add.w	d0,d2
	move.b	(a1)+,d0
	lsl.w	#8,d0
	move.b	(a1)+,d0
	add.w	d0,d2
	add.w	d0,d2
	move.l	flen(a0),a1
	cmp.l	(a1),d2
	bne	never

;	move.l	fname(a0),a1
;ctny2:	move.b	(a1)+,d0
;	beq	maybe
;	cmp.b	#".",d0
;	bne	ctny2
;	cmp.b	#"T",(a1)+
;	bne	maybe
;	cmp.b	#"N",(a1)+
;	bne	maybe
;	cmp.b	#"Y",(a1)
;	bne	maybe
	moveq	#2,d0
irts:	rts
*
*
*
tny:	move.l	table,a0
	move.l	f_openr(a0),a0
	jsr	(a0)
	beq	irts

	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	irts

	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

	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	irts

	bsr	getpicinfo

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

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

	move.w	pic+pc_H,d1
	move.l	#7200,d0
	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		;end of input file.
	move.l	o_close(a0),a0		; close and flush buffer
	jsr	(a0)

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


abortobj:
	move.l	table,a0		;end of input file.
	move.l	o_abort(a0),a0		; close and flush buffer
	jsr	(a0)

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

*
*
*
getpicinfo:
	move.l	table,a0
	move.l	f_get(a0),a0
	jsr	(a0)

	cmp.b	#3,d0
	bcs	gpi1
	subq.b	#3,d0
	move.w	d0,-(sp)
	moveq	#4,d0
	move.l	table,a0
	move.l	f_skip(a0),a0
	jsr	(a0)
	move.w	(sp)+,d0
gpi1:	lea	tnytbl,a0
	and.w	#$ff,d0
	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
tnyc2:	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	tnyc2

	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)
*
*

putpic:
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	move.w	d0,cmdsize
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	move.w	d0,datasize

	move.l	table,a0
	move.l	buff2(a0),a1
	move.l	(a1),a1
	move.l	a1,t1ptr
	move.l	a1,cmdptr
	move.w	cmdsize,temp1
ppic1:	move.l	table,a0
	move.l	f_get(a0),a0
	jsr	(a0)
	move.l	t1ptr,a0
	move.b	d0,(a0)+
	move.l	a0,t1ptr
	subq.w	#1,temp1
	bne	ppic1

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

	cmp.w	#4,pic+pc_BitPln
	beq	lores
	cmp.w	#2,pic+pc_BitPln
	beq	medres
*
*
hires:	move.w	#4,temp1
	move.l	picptr,t1ptr
hi1:	move.w	#20,temp2
	move.l	t1ptr,t2ptr
hi2:	move.w	#200,temp3
	move.l	t2ptr,t3ptr
hi3:	bsr	getnext
	move.l	t3ptr,a0
	move.w	d0,(a0)
	add.l	#160,a0
	move.l	a0,t3ptr
	subq.w	#1,temp3
	bne	hi3
	add.l	#8,t2ptr
	subq.w	#1,temp2
	bne	hi2
	add.l	#2,t1ptr
	subq.w	#1,temp1
	bne	hi1
	rts
*
*
medres:	move.w	#2,temp0
med0:	move.w	#2,temp1
	move.l	picptr,t1ptr
med1:	move.w	#20,temp2
	move.l	t1ptr,t2ptr
med2:	move.w	#200,temp3
	move.l	t2ptr,t3ptr
med3:	bsr	getnext
	move.l	t3ptr,a0
	move.w	d0,(a0)
	add.l	#80,a0
	move.l	a0,t3ptr
	subq.w	#1,temp3
	bne	med3
	add.l	#4,t2ptr
	subq.w	#1,temp2
	bne	med2
	add.l	#2,t1ptr
	subq.w	#1,temp1
	bne	med1
	move.l	plane,d0
	add.l	d0,picptr
	subq.w	#1,temp0
	bne	med0
	rts
*
*
lores:	move.w	#4,temp1
	move.l	picptr,t1ptr
lo1:	move.w	#20,temp2
	move.l	t1ptr,t2ptr
lo2:	move.w	#200,temp3
	move.l	t2ptr,t3ptr
lo3:	bsr	getnext
	move.l	t3ptr,a0
	move.w	d0,(a0)
	add.l	#40,a0
	move.l	a0,t3ptr
	subq.w	#1,temp3
	bne	lo3
	add.l	#2,t2ptr
	subq.w	#1,temp2
	bne	lo2
	move.l	plane,d0
	add.l	d0,t1ptr
	subq.w	#1,temp1
	bne	lo1
	rts
*
*
*
* TNY Compressed getnext byte from run length encoded? picture
*
*
getnext:
	tst.w	rlecmd
	beq	nextcmd
	cmp.w	#1,rlecmd
	bne	gnxt1
	move.w	rledata,d0
	subq.w	#1,rlecount
	bcc	irts
gnxt2:	clr.w	rlecmd
	bra	getnext
gnxt1:	subq.w	#1,rlecount
	bcs	gnxt2
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	rts

nextcmd:
	move.l	cmdptr,a0
	moveq	#0,d7
	move.b	(a0)+,d7
	move.l	a0,cmdptr

	tst.b	d7
	beq	many_r
	bmi	few_u
	cmp.b	#1,d7
	beq	many_u
	move.w	d7,rlecount
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	move.w	d0,rledata
	move.w	#1,rlecmd
	bra	getnext

many_r:	move.l	cmdptr,a0
	move.b	(a0)+,d7
	lsl.w	#8,d7
	move.b	(a0)+,d7
	move.l	a0,cmdptr
	move.w	d7,rlecount
	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	move.w	d0,rledata
	move.w	#1,rlecmd
	bra	getnext

few_u:	neg.b	d7
	and.w	#$7f,d7
	move.w	d7,rlecount
	move.w	#2,rlecmd
	bra	getnext

many_u:	move.l	cmdptr,a0
	move.b	(a0)+,d7
	lsl.w	#8,d7
	move.b	(a0)+,d7
	move.l	a0,cmdptr
	move.w	d7,rlecount
	move.w	#2,rlecmd
	bra	getnext

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 tny,DATA,PUBLIC
*
tnytbl:	dc.l	tnylow,tnymed,tnyhi
tnylow:	dc.w	16,4,320,200,40,36,36
tnymed:	dc.w	4,2,640,200,80,72,36
tnyhi:	dc.w	2,1,640,400,80,77,77


special:
	dc.w	2
	dc.l	spcl1,spcl2

*
*
*
name:	dc.b	"TNY Compressed v2.1.0",0

spcl1:	dc.b	"Picture Window",0
spcl2:	dc.b	"Object",0
*
*
*

 SECTION tny,BSS,PUBLIC

temp0:	ds.w	1
temp1:	ds.w	1
temp2:	ds.w	1
temp3:	ds.w	1

rlecmd:		ds.w	1
rlecount:	ds.w	1
rledata:	ds.w	1

plane:	ds.l	1
t1ptr:	ds.l	1
t2ptr:	ds.l	1
t3ptr:	ds.l	1
picptr:	ds.l	1

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

cmdptr:		ds.l	1
cmdsize:	ds.w	1
datasize:	ds.w	1
