
*****************************************************************
*								*
*	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)
hneo:
	move.b	#-1,picres	;set resolution to invalid
	cmpi.b	#1,pictype	;check pictype
	bne	hdeg
	move.l	savebuff,a5
	cmpi.w	#0,2(a5)	;check resolution descriptor
	beq	stlowneo
	cmpi.w	#4,2(a5)	;is it TT med
	bne	ttlowneo
	move.b	#4,picres	;set resolution to TT med
	bra	exiterror
*	move.b	#4,picres	;set resolution to TT med
*	bra	dottmedneo	;take care of it
ttlowneo:
	cmpi.w	#7,2(a5)	;is it TT med
	bne	exiterror
	move.b	#7,picres	;set resolution to TT low
	bra	dottlowneo	;take care of it
stlowneo:			;just a normal old neo
	move.b	#0,picres	;set resolution to ST low
	lea	temppalette,a1
	move.l	savebuff,a5
	lea	4(a5),a0	;from pal address
	bsr	cvrtrev
	lea	128(a5),a0	;screen data
	move.l	picplace,a1	;screen address in card
	bsr	putscrdat
	bra	exitok
hdeg:
	cmpi.b	#5,pictype
	bne	hdegcomp
	move.b	#0,picres	;set resolution to ST low
	lea	temppalette,a1
	move.l	savebuff,a5
	lea	2(a5),a0	;from pal address
	bsr	cvrtrev
	lea	34(a5),a0	;screen data
	move.l	picplace,a1	;screen address in card
	bsr	putscrdat
	bra	exitok

hdegcomp:
	cmpi.b	#8,pictype
	bne	hiff
	move.b	#0,picres	;set resolution to ST low
	lea	temppalette,a1
	move.l	savebuff,a5
	lea	2(a5),a0	;from pal address
	bsr	cvrtrev
	lea	34(a5),a0	;screen data
	move.l	picplace,a1	;screen address in card
	move.w	#40,bpline
	move.w	#200,height
	move.w	#4,planes
	bsr	iffdecomp
*	move.l	_loadlength,d0
*	sub.l	#34+32,d0	;length of packed data
*	bsr	degas
	bra	exitok

hiff:
	cmpi.b	#3,pictype	;is it iff ?
	bne	hpnt
	bsr	doiff
	tst.w	d0
	beq	exiterror
	lea	temppalette,a3
	lea	cmapcopy,a2
	rept	8
	move.l	(a2)+,(a3)+
	endr
	bra	exitok
hpnt:
	cmpi.b	#6,pictype	;is it pnt ?
	bne	hnext
	bra	dopnt
hnext:
	bra	exiterror	;other ftypes not yet implemented
exiterror:
	moveq	#0,d0
	movem.l	(sp)+,d1-a6
	rts
exitok:
	moveq	#1,d0
	movem.l	(sp)+,d1-a6
	rts



dopnt:
	move.l	savebuff,a0		;address of PNT
	cmpi.l	#$504e5400,(a0)		;is it actually a PNT file
	bne	exiterror
	cmpi.w	#4,iplanes(a0)		;only handle 16 color or greater pics
	beq	plane4
	cmpi.w	#8,iplanes(a0)		;only handle 16 color or greater pics
	beq	plane8
	bra	exiterror
plane4:
	cmpi.w	#320,iwidth(a0)
	bne	is640
	cmpi.w	#200,iheight(a0)
	bne	exiterror		;4plane 320 has to be 200 high
	move.b	#0,picres	;set resolution to ST low
	bra	okpnt
is640:
	cmpi.w	#640,iwidth(a0)
	bne	exiterror		;doesn't mach one of our 4 plane widths
	cmpi.w	#480,iheight(a0)
	bne	exiterror		;4plane 640 has to be 480 high
	move.b	#4,picres		;set resolution to TT med
	bra	exiterror
*	bra	okpnt
plane8:
	cmpi.w	#320,iwidth(a0)
	bne	exiterror		;8 plane con only be 320*480
	cmpi.w	#480,iheight(a0)
	bne	exiterror		;4plane 320 has to be 200 high
	move.b	#7,picres		;set resolution to TT low
okpnt:
*****************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	exiterror		;new compression method

	move.l	picplace,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:
	move.l	picplace,a1	;where to put it a0 already at image data
	cmpi.w	#0,picres
	beq	dosm
	bsr	move153600
	bra	exitok
dosm:
	bsr	putscrdat
	bra	exitok


dottmedneo:
	lea	temppalette,a1
	move.l	savebuff,a5
	lea	4(a5),a0	;from pal address
	bsr	cvrtrev
	lea	128(a5),a0	;start of screen dat
	move.l	picplace,a1	;screen address in card
	bsr	move153600	;shift that data
	bra	exitok
dottlowneo:
	lea	temppalette,a1
	move.l	savebuff,a5
	lea	128(a5),a0	;from pal address
	rept	256/2		;in the actual hardware registers
	move.l	(a0)+,(a1)+	;This should work.......
	endr
	move.l	picplace,a1	;screen address in card
	bsr	move153600	;shift that data
	bra	exitok


move153600:
	move.w	#479,d0
mloopa:
	rept	320/4		;number of longs per screen line
	move.l	(a0)+,(a1)+
	endr
	dbf	d0,mloopa
	rts

putscrdat:
	move	#199,d0
psdl:
	rept	40
	move.l	(a0)+,(a1)+
	endr
	dbf	d0,psdl
	rts

doiff:
	move.l	savebuff,iffpointer
getiff:
	move.l	iffpointer,a0
	cmpi.l	#'FORM',(a0)	; chunktype proper iff ?
	beq.s	iff_ok		; yes! now check 1st chunk
	cmpi.l	#'ILBM',8(a0)	; or chunksubtype?
	bne	freeiffbuff	; not iff - do your own error routines
iff_ok:
	lea	12(a0),a2
readilbm:
	lea.l	chunkbuff(pc),a0	;copy chunkinfo to buffer
	moveq	#8,d3
	bsr	transfer		;transfer 1st chunkname+length
ckbmhd:
	move.l	chunktype(pc),d0      ;what sort of chunk ?
	cmp.l	#'BMHD',d0             ;is it a bitmap header
	bne.s	ckcmap                ;no check if cmap
	lea.l	bmhdcopy,a0            ;if bmhd then copy it
	moveq	#_sizeof,d3        ;# of bytes to read
	bsr	transfer
	bra.s	readilbm               ;get next piece of data
ckcmap:
	cmpi.l	#'CMAP',d0		;is it a color map?
	bne.s	ckbody			;no
	move.l	chunklen(pc),d4		;# of colors to read * 3
	move.l	d4,d3			;# of data to read
	lea.l	coltable(pc),a0		;color table
	bsr	transfer		;copy chunk to coltable
	divu	#3,d4			;divide by 3 to get color count
	move.w	d4,numcols		;save count of colors in map
	subq.w	#1,d4
	lea.l	cmapcopy,a3		;a3 = ptr to cmap buffer
	lea.l	coltable(pc),a0		;a0 = color table
nextcol:
	move.b	(a0)+,d1
	lsl.w	#4,d1
	or.b	(a0)+,d1
	lsl.w	#4,d1
	or.b	(a0)+,d1
	lsr.w	#4,d1
	move.w	d1,(a3)+
	dbra	d4,nextcol
*	bsr	convertpal
	bra	readilbm		;get next piece of data
ckbody:
	cmp.l	#'BODY',d0		;is it the ilbm body?
	beq.s	continue		;
	move.l	chunklen(pc),d2		;weird chunk !!
	add.l	d2,a2			;ptr to next chunk
	bra	readilbm
continue:
	move.l	a2,picbodyptr
*************************************************
*	check for res here			*
*	ste version				*
*************************************************
	lea.l	bmhdcopy,a3		;use values from bmhd to initialize
	cmpi.b	#4,_nplanes(a3)		;only consider 16 color iff's
	bne	quit
	cmpi.w	#320,_w(a3)		;we only handle 2 specific widths
	beq	islow			;is it one of them
	cmpi.w	#640,_w(a3)
	bne	quit
	cmpi.w	#480,_h(a3)		;is it the right height for this
	beq	okiff			;width
	cmpi.w	#400,_h(a3)		;Also pass 400 line pics for people who
	beq	okiff			;like to waste the extra 80 lines
	bra	quit
islow:
	cmpi.w	#200,_h(a3)	;check that it is low res
	bne	quit		;nah, so piss of quick
	move.b	#0,picres	;set resolution to ST low
	bra	okiff2
okiff:
	move.b	#4,picres	;set resolution to TT med
okiff2:
	moveq	#0,d0
	move.w	_w(a3),d0
	divu	#8,d0		;gives number bytes per plane per line
	move.w	d0,bpline
	move.w	_h(a3),height
	moveq	#0,d0
	move.b	_nplanes(a3),d0
	move.w	d0,planes
	move.b	_compression(a3),d3
	cmp.b	#2,d3			;do i know this kind of compression?
	bge	quit			;no, give up...
decomp:
	move.l	picbodyptr,a0
	move.l	picplace,a1
*****************************************************
* 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	#66,d1
	divu	#66,d2
	divu	#66,d3
	and.w	#$000f,d1
	and.w	#$000f,d2
	and.w	#$000f,d3
	lsl.w	#8,d1
	lsl.w	#4,d2
	or.w	d2,d1
	or.w	d3,d1
	move.w	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.w	510(a0),d0
	move.w	2(a0),510(a0)
	MOVE.W	D0,2(A0)
skipi:
	lea	gemtable,a2
	lea	pcolbuf2,a1
	lea	pcolbuf,a0
	bsr	convertbank
	lea	pcolbuf+32,a0
	lea	pcolbuf2+32,a1
	rept	240/2
	move.l	(a0)+,(a1)+
	endr
	lea	temppalette,a1
	lea	pcolbuf2,a0		;do the first 16 in STE format
	rept	8
	move.l	(a0)+,(a1)+
	endr
	move.l	savebuff,a2
	cmpi.w	#8,iplanes(a2)
	bne	allcover
	rept	240/2
	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	(a0,d1.w),d3	;get the color
	move.w	(a2,d1.w),d4
	add.w	d4,d4
	move.w	d3,(a1,d4.w)
	addq.w	#1,d0
	dbf	d7,dcl		;now pcolbuf2 has proper format
	rts


* ==========================================================================
*****************************************
*	color converter			*
*	takes sensible color format	*
*	ie:	1,2,3,4-a,b,c,d,e,f	*
*	and turns it into the ste's 	*
*	stupid format			*
*	ie:	0,8,1,9,2,a,3,b,....	*
*****************************************
convertpal:
	movem.l	d0-a6,-(sp)
	lea	cmapcopy,a0
	lea	64(a0),a6	;32 colors max
	lea	redcolors,a2
	lea	greencolors,a3
	lea	bluecolors,a4
	lea	palconvert,a1		;where the new pal goes
conloop:
	moveq	#0,d0		;for construction
	moveq	#0,d1
	moveq	#0,d2
	moveq	#0,d3
	moveq	#0,d4
	move.w	(a0)+,d1	;get the color
	move.w	d1,d2		;copy it
	move.w	d2,d3
	and.w	#$0f00,d1	;isolate the red
	lsr.w	#8,d1		;now use it to index
	add.w	d1,d1
	or.w	(a2,d1.w),d0
	and.w	#$00f0,d2	;isolate the red
	lsr.w	#4,d2
	add.w	d2,d2
	or.w	(a3,d2.w),d0
	and.w	#$000f,d3	;isolate the red
	add.w	d3,d3
	or.w	(a4,d3.w),d0
	move.w	d0,(a1)+
	cmp.l	a6,a0
	blt	conloop
	movem.l	(sp)+,d0-a6
	rts
cpal2:
	lea	redcolors,a2
	lea	greencolors,a3
	lea	bluecolors,a4
	lea	palconvert,a1		;where the new pal goes
conloop2:
	moveq	#0,d0		;for construction
	moveq	#0,d1
	moveq	#0,d2
	moveq	#0,d3
	moveq	#0,d4
	move.w	(a0)+,d1	;get the color
	move.w	d1,d2		;copy it
	move.w	d2,d3
	and.w	#$0f00,d1	;isolate the red
	lsr.w	#8,d1		;now use it to index
	add.w	d1,d1
	or.w	(a2,d1.w),d0
	and.w	#$00f0,d2	;isolate the red
	lsr.w	#4,d2
	add.w	d2,d2
	or.w	(a3,d2.w),d0
	and.w	#$000f,d3	;isolate the red
	add.w	d3,d3
	or.w	(a4,d3.w),d0
	move.w	d0,(a1)+
	cmp.l	a6,a0
	blt	conloop2
	rts


*****************************************
*	color converter			*
*	takes stupid ste color format	*
*	ie:	8,1,9,2.......		*
*	and turns it into sensible 	*
*	format				*
*	ie:	0,1,2,3,4,5,6,7,....	*
*	expects address of from pal in	*
*	a0 and to in a1			*
*****************************************
cvrtrev:
	movem.l	d0-a6,-(sp)
	lea	32(a0),a6	;16 colors max
	lea	redr,a2
	lea	greenr,a3
	lea	bluer,a4
conloopr:
	moveq	#0,d0		;for construction
	moveq	#0,d1
	moveq	#0,d2
	moveq	#0,d3
	moveq	#0,d4
	move.w	(a0)+,d1	;get the color
	move.w	d1,d2		;copy it
	move.w	d2,d3
	and.w	#$0f00,d1	;isolate the red
	lsr.w	#8,d1		;now use it to index
	add.w	d1,d1
	or.w	(a2,d1.w),d0
	and.w	#$00f0,d2	;isolate the red
	lsr.w	#4,d2
	add.w	d2,d2
	or.w	(a3,d2.w),d0
	and.w	#$000f,d3	;isolate the red
	add.w	d3,d3
	or.w	(a4,d3.w),d0
	move.w	d0,(a1)+
	cmp.l	a6,a0
	blt	conloopr
	movem.l	(sp)+,d0-a6
	rts
* ==========================================================================
redr:
	dc.w	$0000,$0300,$0500,$0700,$0900,$0b00,$0d00,$0f00
	dc.w	$0100,$0300,$0500,$0700,$0900,$0b00,$0d00,$0f00
greenr:
*	dc.w	$0000,$0020,$0040,$0060,$0080,$00a0,$00c0,$00e0
	dc.w	$0000,$0030,$0050,$0070,$0090,$00b0,$00d0,$00f0
	dc.w	$0010,$0030,$0050,$0070,$0090,$00b0,$00d0,$00f0
bluer:
*	dc.w	$0000,$0002,$0004,$0006,$0008,$000a,$000c,$000e
	dc.w	$0000,$0003,$0005,$0007,$0009,$000b,$000d,$000f
	dc.w	$0001,$0003,$0005,$0007,$0009,$000b,$000d,$000f
* ==========================================================================


redcolors:
	dc.w	$0000,$0800,$0100,$0900,$0200,$0a00,$0300,$0b00
	dc.w	$0400,$0c00,$0500,$0d00,$0600,$0e00,$0700,$0f00
greencolors:
	dc.w	$0000,$0080,$0010,$0090,$0020,$00a0,$0030,$00b0
	dc.w	$0040,$00c0,$0050,$00d0,$0060,$00e0,$0070,$00f0
bluecolors:
	dc.w	$0000,$0008,$0001,$0009,$0002,$000a,$0003,$000b
	dc.w	$0004,$000c,$0005,$000d,$0006,$000e,$0007,$000f


pcolbuf:	ds.w	256
pcolbuf2:	ds.w	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	_sizeof
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

