' Pcx Loader by Gabriel Fernandez
' Program made for Gabasic

DIM Buffer(200000) AS INTEGER    ' WOW 200000 bytes !!!!
DIM Pal(768) AS byte

f$ = COMMAND$

IF f$ = "" THEN
  CLS
  PRINT "PCX Loader"
  PRINT "Gabasic example file"
  PRINT ""
  PRINT "Run Pcxl.exe <filename>"
  waitkey
  END
END IF

SCREEN 14

LoadPcx f$

waitkey
SCREEN 0

END


SUB LoadPcx(file$)

OPEN file$ FOR BINARY AS 1

x% = 0
y% = 0
tmp$ = " "

GET 1, 9, x%
GET 1, , y%

IF x% > 640 THEN
  SCREEN 0
  PRINT "Image Bigger than 640x480"
  PRINT STR$(x%)
  PRINT STR$(y%)
  END
END IF

IF y% > 480 THEN
  SCREEN 0
  PRINT "Image Bigger than 640x480"
  PRINT STR$(x%)
  PRINT STR$(y%)
  END
END IF

poffset = VARPTR(Pal(0))

Xget 1, 768, LOF(1) - 767, poffset
col = 0
FOR i = 0 TO 255
  r = Pal(col) >> 2
  g = Pal(col + 1) >> 2
  b = Pal(col + 2) >> 2
  SetCol i, r, g, b
  col = 3
NEXT i

Xget 1, LOF(1) - 768 - 128, 129, VARPTR(Buffer(0))
Getpos = VARPTR(Buffer(0)) - 1

FOR cury = 0 TO y% - 1
  curx = -1
  DO WHILE NOT curx >= x%
    Getpos = 1
    p = PEEK(Getpos)
    IF p AND 192 = 192 THEN
      Times = p AND 63
      Getpos = 1
      p = PEEK(Getpos)
      FOR i = 1 TO Times
        curx = 1
        Pset curx, cury, p
      NEXT
    ELSE
      curx = 1
      Pset curx, cury, p
    END IF
  LOOP
NEXT cury

CLOSE 1

END SUB
