#
# Amiga simple .IFF exporter
# A Multipaint internal script
#
# This is largely based on an examination of a Deluxe Paint III output
#

B=BITPLANES

X = PEEK 5
X = X*8

Y = PEEK 7
Y = Y*8

L = X*Y
L = L/8
L = L*B

L=L+274

C=COLORS
C=C+1

C=C*3

L=L+C

# "FORM"

XBYTE $46
XBYTE $4F
XBYTE $52
XBYTE $4D

# uint32be length of what comes after

XLONG L

# "ILBMBMHD"

XBYTE $49
XBYTE $4C
XBYTE $42
XBYTE $4D
XBYTE $42
XBYTE $4D
XBYTE $48
XBYTE $44

# uint32be chunk length

XLONG $14

# uint16be width in pix

XWORD X

# uint16be height in pix

XWORD Y

# int16be origin x

XWORD 0

# int16be origin y

XWORD 0

# uint8 planes

XBYTE B

# uint8 mask

XBYTE 0

# uint8 compression 0=uncomp
# for now no comp

XBYTE 0

# uint8 pad1 0

XBYTE 0

# uint16be transClr if mask>=2

XWORD $1

# uint8 xAspect

XBYTE 1

# uint8 yAspect

XBYTE 1

# int16be pageWid 320?

XWORD X

# int16be pageHei 200?

XWORD Y

# Colormap i.e. Palette

# "CMAP"

XBYTE $43
XBYTE $4D
XBYTE $41
XBYTE $50

# int32be chunk length

C=COLORS
C=C+1
XLONG C*3
C=C-1

A =256
FOR I=0 TO C
	R=PEEK A
	A=A+1
	XBYTE R
	R=PEEK A
	A=A+1
	XBYTE R
	R=PEEK A
	A=A+1
	XBYTE R
NEXT I

# "DPPS"
# dpaint III perspective chunk?
# probably not needed

XBYTE $44
XBYTE $50
XBYTE $50
XBYTE $53

# chunk length

XBYTE $00,$00,$00,$6E

# fake it till you make it

XBYTE $00,$02,$00,$00
XBYTE $00,$00,$00,$00,$00,$00,$00,$00
XBYTE $00,$00,$01,$68,$00,$00,$01,$40
XBYTE $01,$00,$00,$02,$00,$5A,$00,$02
XBYTE $00,$00,$00,$02,$00,$00,$00,$02
XBYTE $00,$00,$00,$00,$00,$00,$00,$00
XBYTE $00,$00,$00,$00,$00,$00,$00,$00
XBYTE $00,$00,$00,$00,$00,$00,$00,$00
XBYTE $00,$00,$00,$00,$00,$00,$00,$00
XBYTE $00,$00,$00,$00,$00,$00,$00,$01
XBYTE $00,$00,$00,$00,$00,$00,$00,$00
XBYTE $00,$00,$00,$00,$00,$00,$00,$01
XBYTE $00,$00,$00,$00,$00,$00,$00,$00
XBYTE $00,$00,$00,$00,$00,$00,$00,$01
XBYTE $00,$00

# Color Ranges
# Multipaint only supports 4 with no rate

FOR I=0 TO 5

	# "CRNG"

	XBYTE $43
	XBYTE $52
	XBYTE $43
	XBYTE $47

	# chunk length

	XLONG $08

	# pad

	XWORD $0

	# rate

	XWORD $0AAA

	# flags

	XWORD $0001

	# first

	XBYTE $0

	# last

	XBYTE $F

NEXT I

# Amiga ILBM modeID
# just copied from DP3 output

# "CAMG"

XBYTE $43
XBYTE $41
XBYTE $4D
XBYTE $47

# chunk length

XLONG $04

# Chunk ID

XBYTE $00
XBYTE $00
XBYTE $40
XBYTE $00

# Actual bitmaps

# "BODY"

XBYTE $42
XBYTE $4F
XBYTE $44
XBYTE $59

# chunk length

L=X*Y
L=L/8
L=L*B

XLONG L

# bitmaps

Y=Y-1
X=X-1

H=Y
W=X/16

B=B-1
L=1
FOR Y=0 TO H
	L=1
	FOR Z=0 TO B
		FOR X=0 TO W
			A=0
			T=$8000
			FOR P=0 TO 15
				Q=X*16
				Q=Q+P
				I=GETPOINT Q,Y
				I=I&L
				IF I<>0
					A=A+T
				ENDIF
				T=T/2
			NEXT P
			
			XWORD A

		NEXT X
		L=L*2
	NEXT Z
NEXT Y

# Write to file

SAVE
