*******************************************************************************
* NewIFFDepack(source,cmap_dest,bitmap_dest)		   D0-D7/A0-A6 Trashed!
* ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
* 	This function will correctly convert an AGA IFF-ILBM picture from its 
* standard IFF chunks (& byterun compressed forms) into a normal ILBM-Bitmap.
* It is fully compatible with AGA depths, hence works for AGA Pictures & works
* fully with standard IFF picture formats... And fully PC-Relative ofcourse :)
*
* $Inputs:	a0.l = <source> (ptr to "iff picture")
*		a3.l = <dest>   (ptr to "colourmap buffer")
*		a4.l = <dest>   (ptr to "bitmap buffer")
*		a5.l = <dest>   (ptr to "iff header buffer")
*
* $Outputs:	d0.l = success  (0 = successful)
*				(-1= failure, buffer was not an iff-ilbm)
*******************************************************************************
FindChunk	macro
		movea.l	a0,a1
\@.get_chunk:	cmpi.l	#\1,(a1)
		beq.s	\@.got_chunk
		addq.w	#2,a1
		bra.s	\@.get_chunk
\@.got_chunk:
		endm

		rsreset
bm.pixelwidth	rs.w	1			;bitmap width  (pixels)
bm.pixelheight	rs.w	1			;bitmap height (pixels)
bm.bytewidth	rs.w	1			;bitmap width  (bytes)
bm.depth	rs.b	1			;bitmap depth  (usually 0-8)
bm.iffpacked	rs.b	1			;if picture was byterun packed
		rsreset



ExampleTest:	lea	iff_picture,a0		;<source> pic from dpaint,etc.
		lea	Copper_cols(pc),a3	;<dest> for colour palette..
		lea	ilbm_buffer(pc),a4	;<dest> for ilbm bitmap..
		lea	iff_header(pc),a5	;<dest> for ilbm informations..
		bsr.b	_LVONewIFFDepack
		rts


		cnop	0,4
_LVONewIFFDepack
		cmpi.l	#'FORM',(a0)		;is this picture really IFF?
		bne.w	NotIFF			;if not this ID its not iff...

*-------------- get picture bitmap info (from IFF-BMHD Bitmap Header)

		FindChunk "BMHD"
		addq.l	#8,a1			;skip chunk id & chunk size
		move.w	(a1)+,d0		;get pixel width
		move.w	d0,bm.pixelwidth(a5)	;save pixel width
		asr.w	#3,d0			;divide by 8 (to get bytewidth)
		move.w	d0,bm.bytewidth(a5)	;save byte width

		move.w	(a1)+,bm.pixelheight(a5);get pixel height
		addq.l	#4,a1			;skip unimportant flags
		move.b	(a1)+,bm.depth(a5)	;get picture`s depth
		addq.l	#1,a1			;skip unimportant flags
		move.b	(a1)+,bm.iffpacked(a5)	;get byte containing packstatus

*-------------- get picture colourmap info (from IFF-CMAP Colourmap Header)

		FindChunk "CMAP"
		addq.w	#4,a1			;skip chunk id
		movea.l	(a1)+,a2		;size of cmap chunk
		adda.l	a1,a2			;a2.l = end of cmap chunk..

.copycols:	move.b	(a1)+,(a3)+		;copy colour/hue...
		move.b	(a1)+,(a3)+
		move.b	(a1)+,(a3)+
		cmpa.l	a1,a2			;keep extracting until all
		bgt.s	.copycols		;colours are copied

*-------------- now extract bitmap (from IFF-BODY picture body header)

		FindChunk "BODY"
		addq.l	#4,a1			;skip chunk id
		movea.l	(a1)+,a2		;get length of body chunk
		adda.l	a1,a2			;a2.l = end of body chunk...

*-------------- is this picture`s bitmap Byterun-Compressed?

		tst.b	bm.iffpacked(a5)	;test if we need to unpack ilbm
		bne.s	byterun_loop

*-------------- simply bytecopy the unpack ilbm...

byteloop	move.b	(a1)+,(a4)+		;simply copy it byte-for-byte
		cmpa.l	a1,a2			;until whole picture is done...
		bgt.s	byteloop
		rts

*-------------- now we can byterun depack the ILBM buffer...

		cnop	0,4
byterun_loop	move.b	(a1)+,d0	;get byte
		ext.w	d0
		tst.b	d0
		bpl.b	copybytes
		cmpi.b	#128,d0
		bne.b	byterun
		cmpa.l	a1,a2		;finished yet??
		bgt.s	byterun_loop
		moveq	#0,d0		;return success (0)
		rts

		cnop	0,4
byterun		move.b	(a1)+,d2	;get byte
		move.w	d0,d7
		neg.w	d7
.literal	move.b	d2,(a4)+	;save unpacked byte
		dbra	d7,.literal
		cmpa.l	a1,a2		;finished yet??
		bgt.s	byterun_loop
		moveq	#0,d0		;return success (0)
		rts

		cnop	0,4
copybytes:	move.w	d0,d7
.bytecopy	move.b	(a1)+,(a4)+	;save unpacked byte
		dbra	d7,.bytecopy
		cmpa.l	a1,a2		;finished yet??
		bgt.s	byterun_loop
		moveq	#0,d0		;return success (0)
		rts

NotIFF		moveq	#-1,d0		;return failure (-1)
		rts

*************** storage for our variables..

iff_header:	ds.w	1		;pixel width of bitmap (eg. 640)
pixelheight	ds.w	1		;pixel height of bitmap (eg. 256)
bytewidth	ds.w	1		;bitmap bytewidth (eg. 80)
depth		ds.b	1		;no.of bitplanes (picture depth, eg. 7)
iffpacked	ds.b	1		;1=byterun packed, 0=raw ilbm

Copper_cols	ds.w	384		;storage for our colours..
ilbm_buffer	ds.b	80*256*7	;storage for our bitmap..

iff_picture:	incbin	tmp:backatro.iff	;our iff picture...
