iwidth:		equ	8
iplanes:	equ	12
iheight:	equ	10
icolors:	equ	6
icompressed:	equ	14
isize:		equ	16
*****************************************************************
*	Quick and dirty read PNT and save out raw format file	*
*	Raw format goes like this.				*
*								*
*	dc.w	width						*
*	dc.w	height						*
*	dc.w	planes		;2,4,8,0=true color		*
*	ds.w	numcol		;actual palette (not present for*
*				;true color)			*
*	ds.w	x		;image data			*
*****************************************************************
start:
	move.l	4(a7),a3		base page
	move.l	#mystack,a7
	move.l	$c(a3),d0		text len
	add.l	$14(a3),d0		data len
	add.l	$1c(a3),d0		BSS len
	add.l	#$100,d0		basepage
	move.l	d0,-(sp)
	move.l	a3,-(sp)
	clr.w	-(sp)
	move.w	#$4a,-(sp)
	trap	#1			shrink memory
	lea	12(sp),sp


mainloop:
	pea	fseltitle
	pea	fselsp			;get the PNT file
	jsr	_fileselector
	add.l	#8,sp
	cmpi.l	#1,d0
	bne	exit
*	jsr	_openfile		;open the file
	pea	pntfile
	move.l	#308864,-(sp)
	jsr	_loadfile		;read it in
	add.l	#8,sp
	jsr	_closefile

	move.b	#6,pictype
	pea	pntfile
	jsr	_hndlpix
	addq.l	#4,sp

*	pea	fseltitle2
*	pea	fselsp2			;get the PNT file
*	jsr	_fileselector
*	add.l	#8,sp
*	cmpi.l	#1,d0
*	bne	exit

	lea	_myfp,a0
.loop:
	tst.b	(a0)+
	bne	.loop
	subq.l	#4,a0
	move.b	#'P',(a0)+
	move.b	#'T',(a0)+
	move.b	#'1',(a0)+
	moveq	#0,d0

	jsr	_openfile		;open the file

	pea	rawfile
	move.l	#6,-(sp)
	jsr	_writetofile		;read it in
	add.l	#8,sp

	moveq	#1,d0
	move.w	rawplanes,d1
	asl.w	d1,d0
	add.w	d0,d0
	add.w	d0,d0
	pea	rawpal
	move.l	d0,-(sp)
	jsr	_writetofile		;read it in
	add.l	#8,sp
	jsr	_closefile
second:
	lea	_myfp,a0
.loop:
	tst.b	(a0)+
	bne	.loop
	subq.l	#4,a0
	move.b	#'P',(a0)+
	move.b	#'T',(a0)+
	move.b	#'2',(a0)+
	jsr	_openfile		;open the file
bp:
	moveq	#0,d0
	move.w	rawx,d0
	asr.w	#4,d0			;width/16
	mulu.w	rawplanes,d0
	asl.l	#1,d0
	mulu	rawy,d0

	pea	rawdata
	move.l	d0,-(sp)
	jsr	_writetofile		;read it in
	add.l	#8,sp

	jsr	_closefile
	bra	mainloop
exit:
	clr.w	-(sp)
	trap	#1


fseltitle:	dc.b	'Choose a PNT file',0
fselsp:		dc.b	'PNT',0
fseltitle2:	dc.b	'Give me a PT1 file name',0
fselsp2:	dc.b	'PT1',0
pictype:	dc.b	0
	even
*****************************************************************
*								*
*	hndlpix(card.l,rawdat.l)				*
*								*
*	places palette data in correct position of card and	*
*	deals with the various formats.				*
*	also puts the actual screen data at the address pointed	*
*	to by *picmap.						*
*								*
*	filetypes handled so far:	neo 320*200*16		*
*					    320*400*256		*
*					    640*480*16		*
*					pi1 320*200*16		*
*					pc1 320*200*16		*
*					iff 320*200*16		*
*					    640*400*16		*
*					    640*480*16		*
*					pnt 320*480*256		*
*					    640*480*16		*
*					    320*200*16		*
*	to be done:			NONE!!!			*
*								*
*	returns:	0	something not right, possibly	*
*				wrong resolution iff, or neo	*
*				or unknown filetype.		*
*			1	all aok and palette and screen	*
*				data are in place.		*
*								*
*****************************************************************
_hndlpix:
	move.l	4(sp),savebuff
	movem.l	d1-a6,-(sp)



dopnt:
	move.l	savebuff,a0		;address of PNT
	cmpi.l	#$504e5400,(a0)		;is it actually a PNT file
	bne	exitok
	move.w	iwidth(a0),rawx
	move.w	iheight(a0),rawy
	move.w	iplanes(a0),rawplanes
*****************Note put color stuff here**************
	bsr	dopntcolors
	move.l	savebuff,a6
	move.l	a6,a0
	lea	128(a0),a0		;skip header
	moveq	#0,d0
	move.w	icolors(a6),d0		;number of colors
	mulu	#6,d0			;1 word each rgb
	add.l	d0,a0			;a0 should now point at start of image data
 
	cmpi.w	#0,icompressed(a6)
	beq	notcomp
	cmpi.w	#1,icompressed(a6)
	bne	exitok		;new compression method

	lea	rawdata,a1		;screen add to put image
	move.w	iplanes(a6),planes
	moveq	#0,d0
	move.w	iwidth(a6),d0
	divu	#8,d0
	move.w	d0,bpline
	move.w	iheight(a6),height
	bsr	iffdecomp
	bra	exitok
notcomp:
	lea	rawdata,a1		;screen add to put image
*	move.l	picplace,a1	;where to put it a0 already at image data
	move.l	savebuff,a6		;address of PNT
	moveq	#0,d0
	move.w	iwidth(a6),d0
	asr.w	#4,d0			;width/16
	mulu	iplanes(a6),d0
	asl.l	#1,d0
	mulu	iheight(a6),d0
	subq.l	#1,d0
.loop:
	move.b	(a0)+,(a1)+
	subq.l	#1,d0
	bpl	.loop
exitok:
	moveq	#1,d0
	movem.l	(sp)+,d1-a6
	rts

*****************************************************
* iff decompressor				    *
* a1 - destination address                          *
* a0 - source address                               *
*****************************************************
iffdecomp:
	move.w	height,d7	;number of lines
	subq.w	#1,d7
doaline:
	lea	buffer,a6
	move.w	planes,d6
	subq	#1,d6
daloop:
	move.w	bpline,d5
daloop2:
	moveq	#0,d0
	move.b	(a0)+,d0
	cmpi.b	#128,d0
	beq	daloop2
	tst.b	d0
	bmi	decomp2		;-127 <= n <= -1
decomp1:			;for use the next n +1 bytes literally
	move.b	(a0)+,(a6)+
	subq.w	#1,d5		;dec bpl counter
	dbf	d0,decomp1
	tst.w	d5		;finished 1 line of 1 plane
	bne	daloop2		;nope keep going
	dbf	d6,daloop	;planes counter
	bra	finline
decomp2:			;for use the next byte -n + 1 times
	neg.b	d0	;-n
	move.b	(a0)+,d1
dc2l:
	move.b	d1,(a6)+
	subq.w	#1,d5
	dbf	d0,dc2l
	tst.w	d5		;finished 1 line of 1 plane
	bne	daloop2		;nope keep going
	dbf	d6,daloop	;planes counter
finline:
	cmpi.w	#8,planes
	beq	flpnt
	lea	buffer,a6
	move.w	bpline,d0
	lea	(a6,d0.w),a5
	lea	(a5,d0.w),a4
	lea	(a4,d0.w),a3
	move.w	bpline,d0
	asr.w	#1,d0
	subq.w	#1,d0
fnl:
	move.w	(a6)+,(a1)+
	move.w	(a5)+,(a1)+
	move.w	(a4)+,(a1)+
	move.w	(a3)+,(a1)+
	dbf	d0,fnl
contiff:
	dbf	d7,doaline
	cmpi.w	#200,height
	beq	noneed
	cmpi.w	#400,height
	bne	noneed
	moveq	#79,d0
clrbot:
	rept	320/4
	move.l	#0,(a1)+
	endr
	dbf	d0,clrbot

noneed:
	moveq	#1,d0
	bra	exitiff
quit:
freeiffbuff:
	moveq	#0,d0
exitiff:
	rts 

flpnt:
	lea	buffer,a6
	move.w	bpline,d0
	asl.w	#1,d0
	lea	(a6,d0.w),a5
	lea	(a5,d0.w),a4
	lea	(a4,d0.w),a3
	move.w	bpline,d0
	move.w	d0,d1
	asr.w	#1,d0
	subq.w	#1,d0
fnl2:
	move.w	(a6),(a1)+
	move.w	(a6,d1.w),(a1)+
	addq.l	#2,a6
	move.w	(a5),(a1)+
	move.w	(a5,d1.w),(a1)+
	addq.l	#2,a5
	move.w	(a4),(a1)+
	move.w	(a4,d1.w),(a1)+
	addq.l	#2,a4
	move.w	(a3),(a1)+
	move.w	(a3,d1.w),(a1)+
	addq.l	#2,a3

	dbf	d0,fnl2
	bra	contiff



transfer:	move.l  a0,-(sp)
		subq     #1,d3
copyinfo:	move.b   (a2)+,(a0)+
		dbra     d3,copyinfo
		move.l  (sp)+,a0
		rts

****************************************************
dopntcolors:
	move.l	savebuff,a0		;first convert them all from 0-1000
	move.w	icolors(a0),d7		;counter of colors
	subq.w	#1,d7
	lea	pcolbuf,a1
	lea	128(a0),a0		;skip pnt header to color dat
dpcl1:
	moveq	#0,d1
	moveq	#0,d2
	moveq	#0,d3
	move.w	(a0)+,d1	;get red green blue range from 0-1000
	move.w	(a0)+,d2
	move.w	(a0)+,d3
	divu	#4,d1
	divu	#4,d2
	divu	#4,d3
	and.l	#$000000ff,d1
	and.l	#$000000ff,d2
	and.l	#$000000ff,d3
	lsl.l	#8,d1
	lsl.l	#8,d1
	lsl.l	#8,d2
	or.l	d2,d1
	or.l	d3,d1
	move.l	d1,(a1)+
	dbf	d7,dpcl1	;now we convert this bunch of colors
				;from the wierdest format I have ever
				;seen to register format
	move.l	savebuff,a6
	cmpi.w	#16,icolors(a6)	;Bloody hell this format suckssss
	beq	skipi		;aaaarrrghh, I'm going mad.
	lea	pcolbuf,a0
	move.l	1020(a0),d0
	move.l	4(a0),1020(a0)
	MOVE.l	D0,4(A0)
skipi:
	lea	gemtable,a2
	lea	pcolbuf2,a1
	lea	pcolbuf,a0
	bsr	convertbank
	lea	pcolbuf+64,a0
	lea	pcolbuf2+64,a1
	rept	240
	move.l	(a0)+,(a1)+
	endr

	lea	rawpal,a1
	lea	pcolbuf2,a0		;do the first 16 in STE format
	rept	16
	move.l	(a0)+,(a1)+
	endr
	move.l	savebuff,a2
	cmpi.w	#8,iplanes(a2)
	bne	allcover
	rept	240
	move.l	(a0)+,(a1)+
	endr
allcover:
	rts

*****************************************
*	Gemtable in a2			*
*	pcolbuf in a0 (pointing at appr	*
*	bank)				*
*	pcolbuf2 in a1			*
*****************************************
convertbank:
	moveq	#15,d7		;banks of 16
	moveq	#0,d0
dcl:
	move.w	d0,d1
	add.w	d1,d1		;d1*2
	move.w	d1,d2
	add.w	d1,d1		;d1*2
	move.l	(a0,d1.w),d3	;get the color
	move.w	(a2,d2.w),d4
	add.w	d4,d4
	add.w	d4,d4
	move.l	d3,(a1,d4.w)
	addq.w	#1,d0
	dbf	d7,dcl		;now pcolbuf2 has proper format
	rts

	include	'file.s'



pcolbuf:	ds.l	256
pcolbuf2:	ds.l	256
bpline:		dc.w	0
height:		dc.w	0
planes:		dc.w	0
buffer:		ds.b	320		;maximum necessary for 1 line buff
savebuff:	dc.l	0
iffpointer:	dc.l	0
iffsize:	dc.l	0

wdwptr:		dc.l	0
scrptr:		dc.l	0
picbodyptr:	dc.l	0

chunkbuff:
chunktype       dc.l	0	;iff chunk info -- ilbm string
chunklen        dc.l	0	
chunksubtype    dc.l	0
numcols		dc.w	0
gotcmap		dc.w    0
bmhdcopy	ds.b	0
cmapcopy	ds.b	32*2
coltable	ds.b	128
palconvert:	ds.w	32
gemtable:
	DC.W	$0000,$000F,$0001,$0002,$0004,$0006,$0003,$0005,$0007,$0008
	DC.W	$0009,$000A,$000C,$000E,$000B,$000D

	section	bss
		ds.l	1000
mystack:	ds.l	1
savebuf:	ds.l	1
picres:		ds.w	1

pntfile:
		ds.b	308864

rawfile:
rawx:		ds.w	1
rawy:		ds.w	1
rawplanes:	ds.w	1
rawpal:
		ds.l	256

rawdata:
		ds.b	307200
