#
# ZX Next NXI importer 256/320 modes
# A Multipaint internal script
#

LOAD

# error checking

# 256 or 320 mode (value 32 or 40)

H = PEEK 5

# file length

Q = LENGTH

IF H=32
    IF Q=81920
        ALERT 2
        STOP
    ENDIF
    IF Q=82432
        ALERT 2
        STOP
    ENDIF
    IF Q<>49664
	    IF Q<>49152
            ALERT 1
            STOP
	    ENDIF
    ENDIF
ENDIF

IF H=40
    IF Q=49664
        ALERT 2
        STOP
    ENDIF
    IF Q=49152
        ALERT 2
        STOP
    ENDIF
    IF Q<>81920
	    IF Q<>82432
            ALERT 1
            STOP
	    ENDIF
    ENDIF
ENDIF

# if length = 49664 or 82432 contains 256 color palette
# if not, just load bitmap

B=0

IF Q=49664
    B=1
ENDIF
IF Q=82432
    B=1
ENDIF

IF B=1

	# 9-bit palette

	# RRRGGGBB .......B

	A=256
	R=0
	G=0
	B=0

	FOR I=0 TO 255
		READ H
		READ L

		R=H&$E0
		R=R/32
		G=H&$1C
		G=G/4
		B=H&$03
		B=B*2
		B=B+L

		#0 0
		#1 0
		#2 1
		#3 1
		#4 2
		#5 2
		#6 3
		#7 3

		P=R/2
		R=R*36
		R=R+P

		P=G/2
		G=G*36
		G=G+P	

		P=B/2
		B=B*36
		B=B+P	

		POKE A,R
		A=A+1
		POKE A,G
		A=A+1
		POKE A,B
		A=A+1
	NEXT I

ENDIF

# bitmap

IF Q<50000
    FOR Y=0 TO 191
	    FOR X=0 TO 255
		    READ P
		    PUTPOINT X,Y,P
	    NEXT X
    NEXT Y
ENDIF

IF Q>80000
	FOR X=0 TO 319
        FOR Y=0 TO 255
		    READ P
		    PUTPOINT X,Y,P
	    NEXT Y
    NEXT X
ENDIF


REFRESH
