;****************************************************************
;
;                     MDA  To Iff Converter
;
;                        DarkMark 1996
;
;
;  Yes, I know that Repeat Loops are faster than For Next,but
;  this listing is a much simplified version of the program I
;  actually use. It's purpose is only to show how the MDA
;  format works, and although this program works, don't use it.
;  The complied program included is faster and has a Header
;  Checker to make sure the file is an actual MDA image.
;
;****************************************************************

WBStartup
FindScreen 0
leg.l=0
.BEGIN
fi$=""
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;  Load File Request - Store in 'fname$'
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
fname$=RTEZLoadFile("Load MDA File",fi$)

;=-=-=-=-=
; Cancel
;=-=-=-=-=
If fname$=""
   End
EndIf

;=-=-=-=-=-=-=-=-=-=
;Does File Exist ?
;=-=-=-=-=-=-=-=-=-=
If Exists(fname$)
   leg=Exists(fname$)
   success=Reserve(10,leg)
 Else
   bodytext$+"File Does Not Exist!"
   ret=RTEZRequest("Error!",bodytext$,"Okay")
   Goto BEGIN
EndIf

;=-=-=-=-=-=-=-=-=-=-=-=-=
; Load MDA into Bank 10
;=-=-=-=-=-=-=-=-=-=-=-=-=

BLoad fname$,10

star.l=Start(10)

;=-=-=-=-=-=-=-=-=-=-=-=-=
; Calculate Screen Size
;=-=-=-=-=-=-=-=-=-=-=-=-=
;Bytes 128,129 & 130 contain the MDA screen size

dat.w=Asc(Peeks$(star+128,1))
dat2.w=Asc(Peeks$(star+129,1))
dat3.w=Asc(Peeks$(star+130,1))

height.w=(dat2*256)+dat
width.w=(dat3*4)*2

;=-=-=-=-=-=-=-=-=-=-=-=-=
; Open Screen
;=-=-=-=-=-=-=-=-=-=-=-=-=
Screen 1,0,0,width,height,1,$8000,"",1,2
BitMap 1,width,height,1
ScreensBitMap 1,1
BitMapOutput 1
InitPalette 1,2
RGB 0,15,15,15
RGB 1,0,0,0
Boxf 0,0,width,10,0

;=-=-=-=-=-=-=-=-=-=-=-=-=
; Program Start
;=-=-=-=-=-=-=-=-=-=-=-=-=
address.l=star+132
x.w=0:y.w=0
number.l=0:pix.w=0:repeet.w=0

;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;Read MDA file byte by byte starting at byte 132
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
For number=address To address+leg
   ; Read in a Byte from BANK 10
   pix=Asc(Peeks$(number,1))

   ;If the screen height has been exceeded then exit and save IFF
   If y>=height
      Goto exit
   EndIf

   ;>>>>>>>>>>>>>>>>>>>> Decrunch MDA <<<<<<<<<<<<<<<<<<<<
   ;
   ;  Only bytes 00 and FF are crunched in MDA format.
   ;
   ;The byte following 00 or FF tells us how many times to
   ;repeat that group of pixels. FF is space. 00 is solid pixels.
   ;
   ; E.G.         --          --
   ;     A1 5A 00 09 45 12 FF 06
   ;           -----       -----
   ;            becomes
   ;
   ;     A1 5A 00 00 00 00 00 00 00 00 00 45 12 FF FF FF FF FF FF
   ;           (         9 times        )       (   6 times     )
   ;
   If pix=255 OR pix=0
      ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      ;If byte is 00 or FF, read in next byte to find how many
      ;times to repeat the number
      ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      times=Asc(Peeks$(number+1,1))
      ;
      For repeet=1 To times
         ; Draw a line of 8 pixels if byte is 00 or just leave 8 spaces if FF
         If pix=0
            Line x,y,x+8,y,1
         EndIf
         ;
         x=x+8
         ;
         ; If the current pixel's x position = width of image
         ; then move down to the beginning of the next line
         If x=width
            y=y+1
            x=0
         EndIf
         ;
      Next
      ;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      ; Skip the REPEAT byte after 00 or FF
      number=number+1
      ;
   Else
      ;If byte is neither 00 or FF
      ;Decode Hex byte into Binary sequence (8 bits)
      ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      binary$=Mid$(Bin$(pix),25,8)

      ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      ;Split Binary byte into bits and plot each bit on screen.
      ;E.G. 01000110 = pixel,space,pixel,pixel,pixel,space,space,pixel
      ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      For digit.w=1 To 8
          bit$=Mid$(binary$,digit,1)
          ;If bit is 0 then plot a pixel (else skip a space)
          If bit$="0"
             Plot x,y,1
          EndIf
          ;
          x=x+1
         ;
         ; If the current pixel's x position = width of image
         ; then move down to the beginning of the next line
          If x=width
             x=0
             y=y+1
          EndIf
      Next
   EndIf
Next

.exit
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
;  Save File Request - Store in 'sname$'
;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
fi$=""
name$=RTEZSaveFile("Save As IFF",fi$)

;=-=-=-=-=
; Cancel
;=-=-=-=-=
If name$=""
   End
EndIf

SaveScreen 1,name$

End
