#
# Amiga simple .IFF importer
# A Multipaint internal script
#
# Largely based on an examination of a Deluxe Paint III output
#
# FIND from Multipaint 2023.3 onwards
# HEAD from Multipaint 2023.3 onwards
#

LOAD

#
# chunk BHMD
#

A=FIND BMHD

IF A=-1
	ALERT 1
    STOP
ENDIF

A=A+4
SETHEAD A

# chunk length

READL N
ECHO N

# xsize div by 8 for multipaint

READW X
POKE 5,X/8
ECHO X

# ysize div by 8 for multipaint

READW Y
POKE 7,Y/8
ECHO Y

# origin x,y, ignore

READW N
READW N

# no. of bitplanes

READ P
ECHO P
POKE 14,P

C=1
FOR I=1 TO P
	C=C*2
NEXT I
ECHO C

C=C-1
POKE 4,C

# rest of chunk, ignore

# uint8 mask

READ N

# uint8 compression 0=uncomp

# for now no compressed files allowed

READ N
IF N=1
    ALERT 1
    STOP
ENDIF

# uint8 pad1 0

READ N

# uint16be transClr if mask>=2

READW N

# uint8 xAspect, ignore

READ N

# uint8 yAspect, ignore

READ N

# int16be pageWid 320?

READW N
ECHO N

# int16be pageHei 200?

READW N
ECHO N

#
# Chunk CMAP
#

A=FIND CMAP

IF A=-1
	ALERT 1
    STOP
ENDIF

A=A+4
SETHEAD A

READL N
ECHO N

C=COLORS
ECHO C

A=256

FOR I=0 TO C
	READ R
	POKE A,R
	A=A+1

	READ R
	POKE A,R
	A=A+1

	READ R
	POKE A,R
	A=A+1	
NEXT I

# to remember the palette

REFRESH

# clear MP screen

FOR I=1024 TO 87999
	POKE I,0
NEXT I

#
# Chunk BODY
#

A=FIND BODY

IF A=-1
	ALERT 1
    STOP
ENDIF

A=A+4
SETHEAD A

# chunk length, ignore

READL N

Y=Y-1
X=X-1

H=Y
W=X/16

P=P-1

# bitmap data

# lays the screen using existing pixel colors

FOR Y=0 TO H
	J=1
	FOR Z=0 TO P
		FOR X=0 TO W
		
			READW A

			T=$8000
			FOR B=0 TO 15
				Q=X*16
				Q=Q+B
				S=GETPOINT Q,Y
				L=A&T
				IF L>0
					S=S+J
				ENDIF
				PUTPOINT Q,Y,S
				T=T/2
			NEXT B

		NEXT X
		J=J*2
	NEXT Z
NEXT Y

REFRESH

