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

 SECTION IFF,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,IFF


*****************************
* Is it really IFF ILBM?    *
*****************************
check:	move.l	table,a0
	move.l	buff1(a0),a1
	move.l	(a1),a1
	cmp.l	#"FORM",(a1)
	bne	never
	cmp.l	#"ILBM",8(a1)
	beq	right
        cmp.l   #"ACBM",8(a1)
        bne     never
	bra	right



***********************************
***                             ***
***********************************
IFF:	move.l	sp,savesp
        clr.l   rtable
        clr.l   gtable
        clr.l   btable
        clr.l   palhndl
        clr.l   ctblptr
        clr.b   flag

	move.l	table,a0
	move.l	f_openr(a0),a0
	jsr	(a0)
	beq	rrts

	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	rrts

	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
        
        btst    #6,flag                 ;is this an ACBM picture?
        beq     ip1                     ;no
        bsr     putACBM
        bra     ip2
ip1:	bsr	putILBM

ip2:	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)

abort0:	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	abort0

	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
        btst    #6,flag
        beq     io1
        bsr     putACBM
        bra     io2
io1:	bsr	putILBM

io2:	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:
	lea	pic,a0
	move.w	#pc_Sizeof-1,d0
gpi1:	clr.b	(a0)+
	dbra	d0,gpi1

        move.w	#-1,pic+pc_SFreq
	move.w	#-1,pic+pc_SAngle

	clr.b	flag
	clr.l	palhndl
	clr.l	vport

	bsr	pgetl		; "FORM"
	bsr	pgetl		; length of form
	move.l	d0,length
	bsr	pgetl		; "ILBM"
	sub.l	#4,length
        cmp.l   #"ILBM",d0
        beq     getchunk
        bset    #6,flag         ;set the ACBM flag

getchunk:
	bsr	pgetl		;get chunk type in temp (CMAP, BODY, etc.)
	move.l	d0,temp
	bsr	pgetl           ;get chunk length in clength
	move.l	d0,clength
        beq     getchunk        ;skip 0 length chunk
	sub.l	#8,length       ;sub 8 from length of file left
	move.l	temp,d0
        
	lea	chunks-4,a0     ;get address of chunk table
	move.w	numchunks,d1    ;number of chunks in table

gc1:	lea	4(a0),a0        ;get address of next chunk type
	cmp.l	(a0)+,d0        
	dbeq	d1,gc1          ;loop until end of table or a match

	tst.l	(a0)            ;did i find one? or is it the last one (BODY)
	beq	gc2             ;no

	move.l	(a0),a0         ;get address of routine to use for this chunk
	jsr	(a0)            ;do it

	tst.l	length          ;is there any data left in the file?
	bne	getchunk        ;yes
	bra	errrts          ;no - so get out

gc2:	btst	#0,flag         ;did i find a BMHD?
	beq	errrts		;no - so get out
        
        btst    #6,flag         ;is this an ACBM picture?
        bne     rrts            ;yes
        
        btst    #3,flag         ;is this a Digi-View picture?
        bne     rrts            ;yes!
        
        btst    #4,flag         ;is this a dynamic hires picture?
        bne     rrts            ;yes!
        
        btst    #2,flag         ;did i find a CLUT?
        bne     rrts            ;yes!
	
        ;btst	#1,flag         ;did i find a CMAP?
	;beq	errrts		;no - so don't load image
        
rrts:	rts                     ;everything is OK


*********************************
*                               *
*********************************
bmhd:	bset	#0,flag			; found a BMHD

	bsr	pgetw
	move.w	d0,pc_W+pic		; width in pixels
	add.w	#15,d0
	lsr.w	#3,d0
	bclr	#0,d0
	move.w	d0,pc_Width+pic		; width in bytes (even)

	bsr	pgetw
	move.w	d0,pc_H+pic		; height in pixels

	mulu	pc_Width+pic,d0
	move.l	d0,planesize

	bsr	pgetl			; skip x,y

	bsr	pget			; number of bitplanes
	clr.w	pic+pc_BitPln
	move.b	d0,pic+pc_BitPln+1
	move.w	pic+pc_BitPln,truebitpln
        
        btst    #6,flag                 ;is this a ACBM picture?
        beq     bmhd0                   ;no
        cmp.w   #32,pic+pc_BitPln       ;is it a toaster file?
        bne     bmhd0                   ;no
        move.w  #24,pic+pc_BitPln       ;if yes then set it to 24 bitplanes

bmhd0:	bsr	pget
	bsr	pget			; type of compression
	move.b	d0,compression

	bsr	pget			; pad
	bsr	pgetw			; transparent color

	bsr	pgetw                   ;skip the XAspect & YAspect
        bsr     pgetl                   ;skip the pageWidth and pageHeight
        
bmhd01: move.w	#65,pc_XDpi+pic         ;default to 640 x 400
	move.w	#55,pc_YDpi+pic

	cmp.w	#320,pc_W+pic
	bne	bmhd1

	move.w	#32,pc_XDpi+pic

bmhd1:	cmp.w	#200,pc_H+pic
	bne	bmhd2

	move.w	#27,pc_YDpi+pic

bmhd2:	cmp.w	#800,pc_H+pic
	bcc	bmhd2a

	cmp.w	#1000,pc_W+pic
	bcs	bmhd3

bmhd2a: move.w	#300,pc_XDpi+pic
	move.w	#300,pc_YDpi+pic

bmhd3:	move.l	table,a0
	move.l	spclname(a0),a0
	lea	4(a0),a0
	move.w	-2(a0),d0
	clr.b	0(a0,d0.w)
	lea	dpistr1,a1
        lea     dpistr2,a2
        
bmh3a:  move.b  (a0)+,d0
        move.b  (a1)+,d1
        cmp.b	d0,d1
	bne	bmh3ab
	tst.b	(a1)
	bne	bmh3a
        bra     bmh3ac
        
bmh3ab: move.b  (a2)+,d1
        cmp.b	d0,d1
	bne	bmh3b
	tst.b	(a2)
	bne	bmh3a

bmh3ac: bset    #5,flag                 ;set resolution override flag
	bsr	getnum
	move.w	d0,pc_XDpi+pic
	bsr	getnum
	move.w	d0,pc_YDpi+pic

bmh3b:	clr.b	truecolor

        cmp.w   #12,truebitpln
        bcs     bmhd4

        ;cmp.w   #18,pc_BitPln+pic
        ;beq     bmhd3c
        ;cmp.w   #21,pc_BitPln+pic
        ;beq     bmhd3c
	;cmp.w	 #24,pc_BitPln+pic
	;bne	 bmhd4
	
        move.b	#1,truecolor
	clr.w	pc_Palet+pic
	move.w	#PC_RGB,pc_Type+pic
        bra     adjlen

bmhd4:	move.w	#PC_PALET,pc_Type+pic
	move.w	pc_BitPln+pic,d0
	moveq	#1,d1
	lsl.w	d0,d1
	move.w	d1,pc_Palet+pic

	move.l	vport,d0
	btst	#11,d0
	beq	bmhd5
        
	move.w	#12,pc_BitPln+pic
	clr.w	pc_Palet+pic
	move.w	#PC_RGB,pc_Type+pic
	
bmhd5:  cmp.l   #20,clength
        beq     adjlen
        sub.l   #20,clength
        sub.l   #20,length
        bra     skip
        
        
*********************************
*                               *
*********************************
camg:	bsr	pgetl
	move.l	d0,vport

        btst    #5,flag                 ;override the resolution?
        bne     camg2                   ;yes
        
        btst    #15,d0                  ;is it HIRES (640)
        bne     camg1                   ;yes!
        move.w  #32,pic+pc_XDpi
        
camg1:  btst    #2,d0                   ;is it interlaced?
        bne     camg2                   ;yes!
        move.w  #27,pic+pc_YDpi                 

camg2:	btst	#0,flag                 ;did i find a BMHD before this?
	beq	adjlen                  ;no

	btst	#11,d0                  ;is it a HAM picture?
	beq	adjlen                  ;no
	move.w	#12,pc_BitPln+pic       ;convert HAM to 12 bit picture
	clr.w	pc_Palet+pic
	move.w	#PC_RGB,pc_Type+pic
	bra	adjlen


***********************************
***				***
***********************************
clut:
        bsr     pgetl           ;get clut type (1=r, 2=g, 3=b, 0,4-7 skip)
        subq.l  #4,length       
        
clut1:  cmp.l   #1,d0           ;is it the red table?
        bne     clut2           ;no
        lea     rtable,a4       ;get the pointer to the red table into a4
        bra     clut5
        
clut2:  cmp.l   #2,d0           ;is it the green table?
        bne     clut3           ;no
        lea     gtable,a4       ;get the pointer to the green table into a4
        bra     clut5
        
clut3:  cmp.l   #3,d0           ;is it the blue table
        bne     clut4
        lea     btable,a4       ;get the pointer to the blue table into a4
        bra     clut5
        
clut4:  subq.l  #4,clength      ;take off for the data i just read
        bra     skip            ;skip will rts for me
        
clut5:  bsr     pgetl           ;skip the reserved long word
        subq.l  #4,length
        subq.l  #4,clength
        
        move.l  a4,-(sp)        ;save it
        bset    #2,flag         ;yes i found a CLUT! (r, g, or b)
        move.w  #256,d0         ;length of table is always 256
        clr.l   d1              ;allocate memory for table data
        clr.l   d2
        move.l  table,a0
        move.l  m_alloc(a0),a0
        jsr     (a0)
        bne     clut6
        
        move.l  (sp)+,a4
        move.l  #0,(a4)         ;clear table pointer
        bra     skip            ;skip the rest
        
clut6:  move.l  (sp)+,a4
        move.l  a0,(a4)         ;store memory handle into table pointer
        move.l  (a0),a4         ;a4 = address of data
        move.w  #256,temp
        
clut7:  bsr     pget            ;get a byte into d0
        move.b  d0,(a4)+
        subq.w  #1,temp
        bne     clut7   
        
        sub.l   #256,length  
        rts
        

*********************************
*                               *
*********************************
cmap:	bset	#1,flag			; found a CMAP

	tst.b	truecolor
	bne	cmaptc

	move.l	clength,d0
	divu	#3,d0			; # of cmap entries
	move.w	d0,temp
        move.l  d0,d1
        swap    d1
        move.w  d1,cmapleftover
	mulu	#pl_Sizeof,d0
	jsr	allocpalet

getentry:
	bsr	pget			; red
	and.w	#$ff,d0
	mulu	#65535,d0
	divu	#240,d0
	move.w	d0,(a4)
	bsr	pget			; green
	and.w	#$ff,d0
	mulu	#65535,d0
	divu	#240,d0
	move.w	d0,2(a4)
	bsr	pget			; blue
	and.w	#$ff,d0
	mulu	#65535,d0
	divu	#240,d0
	move.w	d0,4(a4)

	move.w	(a4)+,d0
	move.w	(a4)+,d1
	move.w	(a4)+,d2
	mulu	#19661,d0
	mulu	#38666,d1
	mulu	#7209,d2
	add.l	d1,d0
	add.l	d2,d0
	swap	d0
	cmp.w	#$8000,d0
	bcc	ge1
	moveq	#1,d0
	bra	ge2
ge1:	moveq	#0,d0
ge2:	move.w	d0,(a4)+		; black
	sub.w	#1,temp
	bne	getentry
        
        tst.w   cmapleftover
        beq     ge4
ge3:    bsr     pget
        subq.w  #1,cmapleftover
        bne     ge3     
ge4:    bra	adjlen

cmaptc:	move.l	clength,d0
	move.l	d0,temp
	bsr	allocpalet

cmtc1:	bsr	pget
	move.b	d0,(a4)+
	sub.l	#1,temp
	bne	cmtc1
	bra	adjlen

        
***********************************
***				***
***********************************
ctbl:
        bset    #4,flag                 ;this is a dynamic hires/ham picture!
        clr.l   ctblptr
        
        btst    #1,flag                 ;did i find a CMAP before
        beq     ctbl0                   ;no
      
        move.l  palhndl,a0              ;if yes, then delete it
        move.l  table,a1
        move.l  m_delete(a1),a1
        jsr     (a1)
        bclr    #1,flag
	clr.w	pc_Palet+pic
        move.w  #12,pic+pc_BitPln
	move.w	#PC_RGB,pic+pc_Type
          
ctbl0:  move.l  clength,d0
        clr.l   d1
        clr.l   d2
        move.l  table,a0
        move.l  m_alloc(a0),a0
        jsr     (a0)
        bne     ctbl1
        
        bra     errrts          ;get out        

ctbl1:  move.l  a0,ctblptr
        move.l  (a0),a0
        move.l  a0,temppal      ;save incase this is a dynamic HAM picture
        move.l  clength,d1
        sub.l   d1,length       ;adjust file length down
        
ctbl2:  move.l  a0,-(sp)
        move.l  d1,-(sp)    
        bsr     pget
        move.l  (sp)+,d1
        move.l  (sp)+,a0
        move.b  d0,(a0)+
        subq.l  #1,d1
        bne     ctbl2
        
        bsr     fakepal         ;set up a fake palette for later
        rts

                
***********************************
***				***
***********************************
dgvw:
        bset    #3,flag         ;this is a digi-view picture!
        bsr     skip            ;skip the data
        bsr     fakepal         ;set up a "fake" palette for later
        rts


***********************************
***                             ***
***********************************        
fakepal:                
        move.w  #768,d0         ;length of table is always 768 (256*3)
        clr.l   d1              ;allocate memory for table data
        clr.l   d2
        move.l  table,a0
        move.l  m_alloc(a0),a0
        jsr     (a0)
        bne     fkpl1
        
        clr.l   palhndl         ;clear table pointer - (a4) ?????
        rts
        
fkpl1:  move.l  a0,palhndl      ;store memory handle into table pointer
        move.l  (a0),a4         ;a4 = address of data
        move.b  #0,temp         ;for the red data
        
fkpl2:  move.b  temp,(a4)+
        add.b   #1,temp
        cmp.b   #255,temp
        bne     fkpl2
                 
        move.b  #0,temp         ;for the green data
        
fkpl4:  move.b  temp,(a4)+
        add.b   #1,temp
        cmp.b   #255,temp
        bne     fkpl4

        move.b  #0,temp         ;for the blue data
        
fkpl6:  move.b  temp,(a4)+
        add.b   #1,temp
        cmp.b   #255,temp
        bne     fkpl6
     
        rts     
               
***********************************
***				***
***********************************
allocpalet:
	move.l	table,a1
	tst.l	palhndl
	beq	apal1

	move.l	palhndl,a0
	move.l	m_realloc(a1),a1	; already had a color map
	jsr	(a1)
	move.l	palhndl,a4
	move.l	(a4),a4
	rts

apal1:	clr.w	d1
	clr.w	d2
	move.l	m_alloc(a1),a1
	jsr	(a1)			; allocate memory for the color map
	move.l	a0,palhndl
	move.l	(a0),a4
	rts


***********************************
***				***
***********************************
adjlen:	move.l	clength,d0
	btst	#0,d0
	beq	al1
	bsr	pget
	move.l	clength,d0
	add.l	#1,d0

al1:	sub.l	d0,length
	rts


***********************************
**                               **
***********************************
getnum:	moveq	#0,d0
gn1:	move.b	(a0)+,d1
	sub.b	#"0",d1
	bcs	gn2
	cmp.b	#10,d1
	bcc	gn2
	and.w	#$ff,d1
	mulu	#10,d0
	add.w	d1,d0
	bra	gn1

gn2:    rts

***********************************
***				***
***********************************
skip:	move.l	clength,d0
	add.l	#1,d0
	bclr	#0,d0
	sub.l	d0,length

sk1:	move.l	d0,-(sp)
	bsr	pgetw
	move.l	(sp)+,d0
	sub.l	#2,d0
	bgt	sk1
	rts


*********************************
*                               *
*********************************
pgetl:	move.l	table,a0
	move.l	f_getl(a0),a0
	jsr	(a0)
	beq	errrts
	rts

*********************************
*                               *
*********************************
pgetw:	move.l	table,a0
	move.l	f_getw(a0),a0
	jsr	(a0)
	beq	errrts
	rts

*********************************
*                               *
*********************************
pget:	move.l	table,a0
	move.l	f_get(a0),a0
	jsr	(a0)
	beq	errrts
	rts


***********************************
***				***
***********************************
errrts:	tst.l	palhndl
	beq	er1
	move.l	palhndl,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)

er1:    tst.l	rtable
	beq	er2
	move.l	rtable,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)

er2:    tst.l	gtable
	beq	er3
	move.l	gtable,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)

er3:    tst.l	btable
	beq	er4
	move.l	btable,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)
        
er4:    tst.l	ctblptr
	beq	er5
	move.l	ctblptr,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)
        
er5:    move.l	savesp,sp
        
	move.l	table,a0        
	move.l	spclnum(a0),a0  
	cmp.w	#1,(a0)         
	beq	abortobj        
        bra     abortpic        
        
;rts
        

***********************************************************
***							***
***********************************************************
putcolor:
	move.w	pc_Palet+pic,d1
	mulu	#pl_Sizeof,d1
        beq     rrts                    ;incase no pal exist

	move.l	picptr,a0
	move.l	palhndl,a1
	move.l	(a1),a1
	bra	pc2
pc1:	move.b	(a1)+,(a0)+
pc2:	dbf	d1,pc1
	move.l	a0,picptr

	rts


***********************************************************
***							***
***********************************************************
putILBM:
        move.l  vport,d0
        btst    #11,d0                  ;is this a ham picture?
        bne     pp09                    ;yes
        
        btst    #4,flag                 ;is this a dynamic hires picture?
        beq     pp00
        
        move.w  #4,truebitpln           ;read in as a 4bit picture
        
pp00:   btst    #2,flag                 ;is this a CLUT picture?
        beq     pp0
        
        bsr     cluttopal               ;change clut into a palette
        
pp0:    btst    #3,flag                 ;is this is digi-view (RGB) picture
        beq     pp09                    ;no
        cmp.w   #21,truebitpln          ;is this a 21 bit rgb picture?
        bne     pp09                    ;no

        move.w  pic+pc_H,theight
        move.w  #7,bitplanes
        move.l  picptr,a4
        move.l  a4,pptr
        
        move.l  planesize,d0
        lsl.l   #3,d0
        sub.l   planesize,d0    ;multiply by seven
        move.l  d0,planeoffset
        
        add.l   d0,a4
        sub.l   planesize,a4
        move.l  a4,picptr
        move.l  a4,pptr2
        move.w  #3,ccount
        
pp01:   bsr     getrow
        add.l   planeoffset,a4
        sub.w   #1,ccount               ;do the x row for each color (rgb)
        bne     pp01
        
        move.w  #3,ccount               ;reset color count
        move.l  picptr,a4
        sub.l   planesize,a4            ;move to next plane back
        move.l  a4,picptr
        sub.w   #1,bitplanes
        bne     pp01
           
        move.l  pptr2,a4
        add.w   pic+pc_Width,a4
        move.l  a4,pptr2
        move.l  a4,picptr
        move.w  #3,ccount
        move.w  #7,bitplanes
        
        sub.w   #1,theight
        bne     pp01
        
        bra     pp2
        
pp09:   move.w	truebitpln,bitplanes
	move.w	pc_H+pic,theight
	move.l	picptr,a4
	move.l	a4,pptr

pp1:	bsr	getrow          ;put a row into the amiga bitmap
	add.l	planesize,a4    ;one for each bit plane
	sub.w	#1,bitplanes
	bne	pp1

	move.w	truebitpln,bitplanes    ;get next row
	move.l	picptr,a4
	add.w	pc_Width+pic,a4
	move.l	a4,picptr
	sub.w	#1,theight
	bgt	pp1
        
*
* possibly convert into ham
*
pp2:    move.l	vport,d0                ;is it a HAM picture?
	btst	#11,d0
	beq	notham                  ;no!

	clr.w	curcolor
	move.w	pc_H+pic,theight
pp3:	bsr	unhamrow                ;change HAM into 12 bit data
	move.l	pptr,a0
	add.w	pc_Width+pic,a0
	move.l	a0,pptr
	subq.w	#1,theight
	bhi	pp3
	bra	nottrue
*
* possible needs a truecolor lookup
*
notham: btst    #4,flag                 ;is it a dynamic hires picture?
        beq     pp31                    ;no
        
        bsr     undynam                 ;change from dynamic hires into 12bit
          
pp31:   tst.b	truecolor
	beq	nottrue

	move.w	pc_H+pic,theight
pp4:	bsr	truecolorlookup
	move.l	pptr,a0
	add.w	pc_Width+pic,a0
	move.l	a0,pptr
	subq.w	#1,theight
	bhi	pp4

nottrue:
        tst.l   palhndl                 ;clean up memory
        beq     pp5
	move.l	palhndl,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)

pp5:    tst.l   rtable
        beq     pp6
	move.l	rtable,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)
    
pp6:    tst.l   gtable
        beq     pp7
	move.l	gtable,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)
    
pp7:    tst.l   btable
        beq     pp8
	move.l	btable,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)
    
pp8:    tst.l   ctblptr
        beq     pp9
	move.l	ctblptr,a0
	move.l	table,a1
	move.l	m_delete(a1),a1
	jsr	(a1)   
         
pp9:    bra	adjlen          ;will rts for me


*******************************************
***					***
*******************************************
putACBM:
        clr.l   d0
        move.w  pic+pc_Width,d0
        mulu    pic+pc_H,d0
        move.l  d0,planesize
        
        cmp.w   #6,pic+pc_BitPln
        bcc     pa1
    
        mulu    pic+pc_BitPln,d0
        move.l  picptr,a0
        move.l  table,a1
        move.l  f_getr(a1),a1
        jsr     (a1)
        beq     errrts            
        bra     pa9     

pa1:    lsl.l   #1,d0                   ;skip the first 3 bitplanes
        add.l   planesize,d0
        move.l  picptr,a0
        move.l  table,a1
        move.l  f_getr(a1),a1
        jsr     (a1)
        beq     errrts
        
        clr.l   d1                      ;red plane?
        move.l  planesize,d0
        move.w  pic+pc_BitPln,d1
        divu    #3,d1
        mulu    d1,d0
        move.l  picptr,a0
        add.l   d0,a0
        add.l   d0,a0    
        move.l  table,a1
        move.l  f_getr(a1),a1
        jsr     (a1)
        beq     errrts

        move.l  planesize,d0
        lsl.l   #1,d0                   ;skip the next 2 bitplanes
        move.l  picptr,a0
        move.l  table,a1
        move.l  f_getr(a1),a1
        jsr     (a1)
        beq     errrts
        
        clr.l   d1                      ;green planes?
        move.l  planesize,d0
        move.w  pic+pc_BitPln,d1
        divu    #3,d1
        mulu    d1,d0
        move.l  picptr,a0
        add.l   d0,a0
        move.l  table,a1
        move.l  f_getr(a1),a1
        jsr     (a1)
        beq     errrts

        move.l  planesize,d0
        lsl.l   #1,d0                   ;skip the next 2 bitplanes
        move.l  picptr,a0
        move.l  table,a1
        move.l  f_getr(a1),a1
        jsr     (a1)
        beq     errrts

        clr.l   d1                      ;blue planes?
        move.l  planesize,d0
        move.w  pic+pc_BitPln,d1
        divu    #3,d1
        mulu    d1,d0
        move.l  picptr,a0
        move.l  table,a1
        move.l  f_getr(a1),a1
        jsr     (a1)
        beq     errrts

pa9:    rts
        
*******************************************
***					***
*******************************************
unhamrow:
        btst    #4,flag                 ;is this a dynamic HAM picture?
        beq     uhr0
        move.l  temppal,a1
        move.l  a1,a2
        add.l   #32,a2
        move.l  a2,temppal              ;set temppal to next pal for row
        bra     uhr01
        
uhr0:   move.l	palhndl,a1
	move.l	(a1),a1
	
uhr01:  move.w	pc_Width+pic,d0
	lsl.w	#3,d0
	move.w	d0,twidth
	move.l	pptr,a4
	moveq	#7,d7
        move.w  (a1),curcolor   ;set the starting color to the background (0)

uhr1:	move.l	a4,a2
	move.l	a4,a3
	moveq	#0,d1
	moveq	#0,d0

	btst	d7,(a2)
	beq	uhr10
	bset	#0,d0
uhr10:	add.l	planesize,a2
	btst	d7,(a2)
	beq	uhr11
	bset	#1,d0
uhr11:	add.l	planesize,a2
	btst	d7,(a2)
	beq	uhr12
	bset	#2,d0
uhr12:	add.l	planesize,a2
	btst	d7,(a2)
	beq	uhr13
	bset	#3,d0
uhr13:	add.l	planesize,a2
	btst	d7,(a2)
	beq	uhr14
	bset	#2,d1
uhr14:	add.l	planesize,a2
	btst	d7,(a2)
	beq	uhr15
	bset	#3,d1

uhr15:	lea	ham,a0
	jsr	0(a0,d1.w)

	move.w	curcolor,d0

	move.w	#0,d1
	move.w	#11,d2
uhr20:	btst	d1,d0
	beq	uhr21
	bset	d7,(a3)
	bra	uhr22
uhr21:	bclr	d7,(a3)
uhr22:	add.l	planesize,a3
	addq.w	#1,d1
	dbf	d2,uhr20

	dbf	d7,uhr2
	moveq	#7,d7
	addq.l	#1,a4
uhr2:	sub.w	#1,twidth
	bhi	uhr1
	rts

ham:	bra.w	selpal
	bra.w	selblue
	bra.w	selred
	bra.w	selgreen

selpal: btst    #4,flag                 ;is it a dynamic HAM picture?
        beq     selp1
        lsl.w   #1,d0
        move.w  0(a1,d0.w),curcolor
        rts
        
selp1:  lsl.w	#3,d0
	move.w	pl_Red(a1,d0.w),d1
	mulu	#15,d1
	divu	#65535,d1
	and.w	#$f,d1
	lsl.w	#8,d1

	move.w	pl_Green(a1,d0.w),d2
	mulu	#15,d2
	divu	#65535,d2
	and.w	#$f,d2
	lsl.w	#4,d2
	or.w	d2,d1

	move.w	pl_Blue(a1,d0.w),d2
	mulu	#15,d2
	divu	#65535,d2
	and.w	#$f,d2
	or.w	d2,d1

	move.w	d1,curcolor
	rts

selblue:
	move.w	curcolor,d1
	and.w	#$ff0,d1
	or.w	d0,d1
	move.w	d1,curcolor
	rts

selgreen:
	move.w	curcolor,d1
	and.w	#$f0f,d1
	lsl.w	#4,d0
	or.w	d0,d1
	move.w	d1,curcolor
	rts

selred:
	move.w	curcolor,d1
	and.w	#$0ff,d1
	lsl.w	#8,d0
	or.w	d0,d1
	move.w	d1,curcolor
	rts

*******************************************
***					***
*******************************************
truecolorlookup:
	move.w	pc_Width+pic,d0
	lsl.w	#3,d0
	move.w	d0,twidth
	move.l	pptr,a4
	moveq	#7,d7
        
        move.w  truebitpln,d3
        divu    #3,d3
        subq.w  #1,d3
        move.w  d3,numplane

tcl1:	move.l	a4,a2
	move.l	a4,a3

	moveq	#0,d0			;red
	moveq	#0,d1			;green
	moveq	#0,d2			;blue

	move.w  numplane,d3             ;get the red plane's value
        
tcl2:	lsr.b	#1,d0
	btst	d7,(a2)
	beq	tcl3
	bset	#7,d0
tcl3:	add.l	planesize,a2
	dbf	d3,tcl2

        move.w  numplane,d3             ;get the green planes's value
        
tcl4:	lsr.b	#1,d1
	btst	d7,(a2)
	beq	tcl5
	bset	#7,d1
tcl5:	add.l	planesize,a2
	dbf	d3,tcl4

        move.w  numplane,d3             ;get the blue plane's value

tcl6:	lsr.b	#1,d2
	btst	d7,(a2)
	beq	tcl7
	bset	#7,d2
tcl7:	add.l	planesize,a2
	dbf	d3,tcl6

        move.w  #7,d3
        sub.w   numplane,d3
        beq     tcl8
        
        lsr.b   d3,d0
        lsr.b   d3,d1
        lsr.b   d3,d2
        
tcl8:	tst.l   palhndl
        beq     tcl80
        
        move.l	palhndl,a1
	move.l	(a1),a1
	move.b	0(a1,d0.w),d0		;red lookup
	lea	256(a1),a1
	move.b	0(a1,d1.w),d1		;green lookup
	lea	256(a1),a1
	move.b	0(a1,d2.w),d2		;blue lookup
        
tcl80:  btst    #3,flag                 ;is this a digi-view picture?
        beq     tcl9                    ;no - needs no color adjusting
        
        cmp.w   #21,truebitpln          ;is this a 21 bit digi view picture?
        beq     tcl81                   ;nope
        cmp.w   #24,truebitpln          ;is this a 24 bit digi view picture?
        bne     tcl9                    ;nope
        
        lea     contrast24,a1           ;get the contrast adjusted values
        move.b  0(a1,d0.w),d0
        move.b  0(a1,d1.w),d1
        move.b  0(a1,d2.w),d2
        bra     tcl9
        
tcl81:  lea     contrast21,a1           ;get the contrast adjusted values
        move.b  0(a1,d0.w),d0
        move.b  0(a1,d1.w),d1
        move.b  0(a1,d2.w),d2
        
tcl9:	move.w	#0,d3			;copy blue back
        move.w  numplane,d4
        
tcl10:	btst	d3,d2
	beq	tcl11
	bset	d7,(a3)
	bra	tcl12
tcl11:	bclr	d7,(a3)
tcl12:	add.l	planesize,a3
	addq.w	#1,d3
	dbf	d4,tcl10

	move.w	#0,d3			;copy green back
        move.w  numplane,d4
        
tcl13:	btst	d3,d1
	beq	tcl14
        bset	d7,(a3)
	bra	tcl15
tcl14:	bclr	d7,(a3)
tcl15:	add.l	planesize,a3
	addq.w	#1,d3
	dbf	d4,tcl13

	move.w	#0,d3			;copy red back
        move.w  numplane,d4
        
tcl16:	btst	d3,d0
	beq	tcl17
	bset	d7,(a3)
	bra	tcl18
tcl17:	bclr	d7,(a3)
tcl18:	add.l	planesize,a3
	addq.w	#1,d3
	dbf	d4,tcl16


	dbf	d7,tcl20
	moveq	#7,d7
	addq.l	#1,a4
tcl20:	sub.w	#1,twidth
	bhi	tcl1
	rts


*******************************************
***					***
*******************************************
getrow:	move.l	a4,temp
	move.w	pc_Width+pic,twidth

	tst.b	compression
	beq	nocomp
	cmp.b	#1,compression
	bne	errrts

gr1:	tst.w	twidth
	beq	grtn
	bsr	pget
        cmp.b   #$80,d0
        beq     gr1
        
	tst.b	d0
	bmi	gr3
	move.b	d0,temp1
gr2:	bsr	pget
	move.b	d0,(a4)+
	sub.w	#1,twidth
	sub.b	#1,temp1
	bge	gr2
	bra	gr1

gr3:	clr.w	temp1
	neg.b	d0
	move.b	d0,temp1+1
	bsr	pget
	move.w	temp1,d1
gr4:	move.b	d0,(a4)+
	sub.w	#1,twidth
	dbra	d1,gr4
	bra	gr1
	
nocomp: bsr	pget
	move.b	d0,(a4)+
	sub.w	#1,twidth
	bne	nocomp

grtn:	move.l	temp,a4
	rts


*******************************************
***					***
*******************************************
cluttopal:
        move.w  #768,d0
        clr.l   d1
        clr.l   d2
        move.l  table,a0
        move.l  m_alloc(a0),a0
        jsr     (a0)
        beq     errrts                  ;can i do this?
        
        move.l  a0,palhndl 
        move.l  (a0),a0
        move.l  rtable,a1
        move.l  (a1),a1
        move.w  #255,d0
        
clpt1:  move.b  (a1)+,(a0)+
        dbf     d0,clpt1

        move.l  #255,d0
        move.l  gtable,a1
        move.l  (a1),a1
        
clpt2:  move.b  (a1)+,(a0)+
        dbf     d0,clpt2

        move.l  #255,d0
        move.l  btable,a1
        move.l  (a1),a1

clpt3:  move.b  (a1)+,(a0)+
        dbf     d0,clpt3
     
        rts
        
***********************************
***                             ***
***********************************
undynam:
        move.l  ctblptr,a0
        move.l  (a0),a0                 ;a0 = address of palettes
        move.w  pic+pc_H,d0             ;d0 = number of rows
        subq.w  #1,d0
        move.w  pic+pc_Width,d4         ;d4 = width of picture in bytes
        subq.w  #1,d4
        move.l  pptr,a1                 ;a1 = pointer into amiga bitmap
        moveq   #7,d1                   ;d1 = bit counter into amiga bitmap  
        
undy1:  clr.w   d2                      ;d2= pixel value
        moveq   #0,d3                   ;d3 = counter
        move.l  a1,a2                   ;a2 = temp hold for a1
                
undy2:  btst    d1,(a2)                 ;get 4bit value from amiga bitmap
        beq     undy3
        bset    d3,d2
undy3:  add.l   planesize,a2
        addq.w  #1,d3
        cmp.w   #4,d3
        bcs     undy2
        
        lsl.w   #1,d2
        move.w  0(a0,d2.w),d2           ;get pixel value from palette
        
        move.l  a1,a2
        moveq   #0,d3                   ;put 12bit value back
        
undy4:  btst    d3,d2
        beq     undy5
        bset    d1,(a2)
        bra     undy6
undy5:  bclr    d1,(a2)
undy6:  add.l   planesize,a2
        addq.w  #1,d3
        cmp.w   #12,d3
        bcs     undy4
      
        dbf     d1,undy1
        moveq   #7,d1
        addq.l  #1,a1
        
        dbf     d4,undy1
        move.l  pptr,a1
        add.w   pic+pc_Width,a1
        move.l  a1,pptr
        add.l   #32,a0          ;move to next palette
        move.w  pic+pc_Width,d4 ;reset width
        
        dbf     d0,undy1
        
        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.				***
*******************************************************************
never:	clr.w	d0
	rts

maybe:	move.w	#1,d0
	rts

right:	move.w	#2,d0
	rts


*************************************************************
*************************************************************
 SECTION IFF,DATA,PUBLIC

chunks:	dc.l    "BEAM",ctbl
        dc.l	"BMHD",bmhd
        dc.l    "CAMG",camg
        dc.l    "CLUT",clut
        dc.l    "CMAP",cmap
        dc.l    "CTBL",ctbl
        dc.l    "DGVW",dgvw
        dc.l    "BODY",0
        dc.l    "ABIT",0
        dc.l    0,skip
                
numchunks:      
        dc.w    9


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

vers:   dc.b    "$VER: "
name:	dc.b	"IFF ILBM/ACBM v2.0.9",0

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

dpistr1:        dc.b	"dpi=",0
dpistr2:        dc.b    "DPI=",0

contrast21:       
        dc.b    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        dc.b    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        dc.b    2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32
        dc.b    34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64
        dc.b    68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98
        dc.b    100,102,104,106,108,110,112,114,116,118,120,122,124,126,127,127
        dc.b    127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127
        dc.b    127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127


contrast24:       
        dc.b    0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5
        dc.b    6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,11
        dc.b    11,11,12,12,12,13,13,13
        dc.b    14,14,14,15,15,15,16,16,18,20,22,24,26,28,30,32
        dc.b    34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64
        dc.b    68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98
        dc.b    100,102,104,106,108,110,112,114,116,118,120,122,124,126,128,130
        dc.b    132,134,136,138,140,142,144,146,148,150,152,154,156,158,160,162
        dc.b    164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194
        dc.b    196,198,200,202,204,206,208,210,212,214,216,218,220,222,224,226
        dc.b    228,230,232,234,236,238,240,242,246,248,250,252,254,255,255,255
        dc.b    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
        dc.b    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
        dc.b    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
        dc.b    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
        dc.b    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
        dc.b    255,255,255,255,255,255,255,255


**************************************************************
**************************************************************
 SECTION IFF,BSS,PUBLIC
savesp:		ds.l	1
length:		ds.l	1
clength:	ds.l	1
temp:		ds.l	1
temp1:		ds.w	1
palhndl:	ds.l	1
picptr:		ds.l	1
pptr:		ds.l	1
planesize:	ds.l	1
twidth:		ds.w	1
theight:	ds.w	1
bitplanes:	ds.w	1
pic:		ds.w	pc_Sizeof/2
object:		ds.w	ob_SizeOf/2
compression:	ds.b	1
flag:		ds.b	1
vport:		ds.l	1
truebitpln:	ds.w	1
curcolor:	ds.w	1
truecolor:	ds.w	1
rtable:         ds.l    1
gtable:         ds.l    1
btable:         ds.l    1
xpage:          ds.w    1
ypage:          ds.w    1
ccount:         ds.l    1
planeoffset:    ds.l    1
pptr2:          ds.l    1
ctblptr:        ds.l    1
temppal:        ds.l    1
numplane:       ds.w    1
cmapleftover:   ds.w    1

******************************* CHANGES ************************************
;
;       version 2.0.9
;
;       - removed support for extended BMHD (xdpi and ydpi), although it will
;         still skip the extra chunk data if there is any.
;
;       - added a version string
;
;       - misc fixes for atari st
;
;       
;       version 2.0.8   (uploaded 4/19/91)
;
;       - fixes bug when loading a picture with no cmap or clut tables
;
;
;       version 2.0.7
;
;       - handles an extended BMHD and uses the xdpi and ydpi info
;
;
;       version 2.0.6   (uploaded 02/19/91)
;
;       - now handles correctly 0 length chunks (nasty programs)
;
;       - handles bad cmap chunk lengths (chunklength%3 != 0)
;
;
;       version 2.0.5   (uploaded 1/16/91)
;
;       - added support for IFF's without CMAPs (24 bit)
;
;       
;       version 2.0.4   (shipped with PgS 2.1  12/7/90)
;
;       - added support for IFF ACBM files (video toaster)
;
; 
;       version 2.0.3   (uploaded 10/12/90)
;
;       - adding color correction for digi view 24 bit files 
;
;
;       version 2.0.2   (uploaded 9/xx/90)
;
;        - bug fix: now correctly handles the 'dpi=' option
;        
;        - supports images in 15 and 18 bit standard IFF format (deep ilbm)
;          (this has not been tested - only works in theory)
;          
;        - supports the BEAM chunk as a CTBL chunk
;
;       version 2.0.1
;
;       - shipped with pagestream2.0
