BAS:

' ** AFFICHE IMAGES GIF OU IMAGES BMP NON COMPRESSE 16/256 COULEURS **
' ** DISPLAY IMAGES GIF OR IMAGES BMP 16/256 COLORS NOT COMPRESSED **
'<---INPUTS
' ImgName$   =  nom image avec extension
'               image name with extension
' FirstInk   =  numero 1er crayon(pas utilisé avec BMP 256 couleurs)
'               first ink use in palette(not used with BMP 256 colors)
'(BMP only)
' ScreenMode =  mode ecran
'               screen mode
' ximg,yimg  =  coordonnees coin inferieur gauche
'               bottom left image coordinates
' BackGround =  crayon de fond (-1 si non transparent)
'               background pen (-1 if not transparent)
'--->OUTPUTS
' ImgWidth    = largeur image
'               image width
' ImgHeight   = hauteur image
'               image height
' PalSize    =  nombre de couleurs
'               number of inks
'(BMP only)
' Pal        =  tableau palette
'            =  palet array
ImgDisplay:

        er%=0
        adr=himem+1:'buffer
        'load header and palet if BMP image
        |INPUT,ImgName$+":D,&500,0,&"+HEX$(adr),@er%:'block_lenght,adr_block_in_file,adr_in_memory,@error_code
        if er%<>0 then FileInputEr
		if PEEK(adr)=asc("B") then BmpDisplay
		'GIF treatment
		|PALOFFSET,FirstInk:|INPUT,ImgName$:info$=string$(32,"X"):|INFO,@info$
		ImgWidth=val("&"+mid$(info$,13,4)):ImgHeight=val("&"+mid$(info$,17,4))
		PalSize=val("&"+mid$(info$,21,4))-FirstInk
		RETURN

BmpDisplay:
		
        PalSize=0
		if IniPalFlag<>-1 then dim pal(257*3):IniPalFlag=-1
        adrline=PEEK(adr+&A)+256*PEEK(adr+&B):'adresse ligne a traiter dans fichier/adr img line in file
        adrimg=adr+adrline:'adresse image dans fichier/adr img in file
        ImgWidth=PEEK(adr+&12)+256*PEEK(adr+&13):'largeur/with image
        IF PEEK(adr+&E)+256*PEEK(adr+&F)<>12 THEN READ_BITMAPFILEHEADER:'header type

' ** READ BITMAPCOREHEADER **

        x=PEEK(adr+&18)+256*PEEK(adr+&19):'nbr bits/pixel
        ImgHeight=PEEK(adr+&14)+256*PEEK(adr+&15):'hauteur/hight image
        IF x=8 THEN BITMAPCOREHEADER_256:'256 couleurs/colors
        IF x<>4 THEN MODE 2:PRINT"16 or 256 colors in image only accepted.":END

'** DISPLAY IMAGE 16 COULEURS/COLORS **

        GOSUB cpc_inks_ordering
        x=FirstInk:'1er crayon/first pen in color palette
        FOR i=0 TO 15*3 STEP 3
                |INK,x,PEEK(adr+&1C+i),PEEK(adr+&1B+i),PEEK(adr+&1A+i)
                pal(i)=PEEK(adr+&1C+i):  'R
                pal(i+1)=PEEK(adr+&1B+i):'G
                pal(i+2)=PEEK(adr+&1A+i):'B
                x=x+1
        NEXT

DISPLAY16:

        reste=ImgWidth MOD 2:x=INT(ImgWidth/2)+reste
        WHILE x MOD 4 <> 0
                x=x+1
        WEND
        larbloc=x
        incry=2
        if ScreenMode=0 then incrx=4 else if ScreenMode=1 then incrx=2 else incrx=1
        FOR i=0 TO ImgHeight*incry STEP incry
                x=0
                |INPUT,ImgName$+":D,&"+HEX$(larbloc)+","+DEC$(adrline,"########")+",&"+HEX$(adr)
                FOR j=adr TO adr+INT(ImgWidth/2)
                        pencil=FirstInk+(PEEK(j) AND &F0)/16:gosub PLOT_STEP:'plot with step
                        pencil=FirstInk+(PEEK(j) AND &F):gosub PLOT_STEP:'plot with step
                NEXT j
                adrline=adrline+larbloc
        NEXT i
		PalSize=PalSize+1
        RETURN

'** READ BITMAPFILEHEADER 16 COLORS **
BITMAPFILEHEADER_16:

        GOSUB cpc_inks_ordering
        x=0
        FOR i=0 TO 15*4 STEP 4
                |INK,x+FirstInk,PEEK(adr+&38+i),PEEK(adr+&37+i),PEEK(adr+&36+i)
                pal(x*3)=PEEK(adr+&38+i):  'R
                pal(x*3+1)=PEEK(adr+&37+i):'G
                pal(x*3+2)=PEEK(adr+&36+i):'B
                x=x+1
        NEXT
        GOTO DISPLAY16

'** BITMAPCOREHEADER 256 COULEURS/COLORS **
BITMAPCOREHEADER_256:

        x=0
        FOR i=0 TO 255*3 STEP 3
                |INK,x,PEEK(adr+&1C+i),PEEK(adr+&1B+i),PEEK(adr+&1A+i)
                pal(i)=PEEK(adr+&1C+i):  'R
                pal(i+1)=PEEK(adr+&1B+i):'G
                pal(i+2)=PEEK(adr+&1A+i):'B
                x=x+1
        NEXT
        GOTO DISPLAY256

'** BITMAPFILEHEADER **
READ_BITMAPFILEHEADER:

        ImgHeight=PEEK(adr+&16)+256*PEEK(adr+&17)

        IF PEEK(adr+&E)+256*PEEK(adr+&F)<>40 THEN MODE 2:PRINT"Bitmap header error":END
        IF PEEK(adr+&1E)+256*PEEK(adr+&1F)<>0 THEN MODE 2:PRINT"Uncompressed bitmap only accepted":END
        IF PEEK(adr+&1C)+256*PEEK(adr+&1D)=4 THEN BITMAPFILEHEADER_16
        IF PEEK(adr+&1C)+256*PEEK(adr+&1D)<>8 THEN MODE 2:PRINT"256 or 16 colors only accepted":END

'** BITMAPFILEHEADER 256 COLORS **
        GOSUB cpc_inks_ordering
        x=0
        FOR i=0 TO 255*4 STEP 4
                |INK,x,PEEK(adr+&38+i),PEEK(adr+&37+i),PEEK(adr+&36+i)
                pal(x*3)=PEEK(adr+&38+i):  'R
                pal(x*3+1)=PEEK(adr+&37+i):'G
                pal(x*3+2)=PEEK(adr+&36+i):'B
                x=x+1
        NEXT

DISPLAY256:

        IF ImgWidth MOD 4<>0 THEN larbloc=ImgWidth+(4-(ImgWidth MOD 4)) ELSE larbloc=ImgWidth
        incry=2:incrx=2:' (MODE 1)
        FOR i=0 TO ImgHeight*incry STEP incry
                x=0
                |INPUT,ImgName$+":D,&"+HEX$(larbloc)+","+DEC$(adrline,"########")+",&"+HEX$(adr)
                        FOR j=adr TO adr+ImgWidth-1
                                pencil=PEEK(j):gosub PLOT_STEP:'plot with step
                        NEXT j
                adrline=adrline+larbloc
        NEXT i
		PalSize=PalSize+1
        RETURN

'** PLOT WITH STEP ** if BackGround<>-1 then transparent background
PLOT_STEP:
        if BackGround=-1 or pencil-FirstInk<>BackGround THEN PLOT ximg+x,yimg+i,pencil
        x=x+incrx:PalSize=MAX(pencil,PalSize)
        RETURN

'** cpc inks ordering **
cpc_inks_ordering:
		for i=0 to 15:poke i+&B7D5,i:poke i+&B7E6,i:next
		RETURN

FileInputEr:

        mode 2
        if er%=1 then ? "|INPUT:"+ImgName$+" parameters error":end
        if er%=2 then ? ImgName$+" not found":end
        ? "|INPUT:"+ImgName$+" error":end




