NEW
PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF ROOT/BMP2PIX
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/GUI.I
				.INB INC/LIBGUI.I
*--------------------------------------
ZPPTR1			.EQ ZPBIN
ZPInputBufPtr	.EQ ZPBIN+2
ZPInputBufLen	.EQ ZPBIN+4
ZPInputDataPtr	.EQ ZPBIN+6
ZPBMPRowBytes	.EQ ZPBIN+8
ZPPIXRowBytes	.EQ ZPBIN+10
ZPBufPtr		.EQ ZPBIN+12
ZPLineCount		.EQ ZPBIN+14
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA 0
				.DA CS.END-CS.START		CS
				.DA DS.END-DS.START		DS
				.DA #64					SS
				.DA #16					ZP
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------				
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT		
				.DA	CS.QUIT
L.MSG.USAGE		.DA MSG.USAGE	
L.MSG.FILE.OK	.DA MSG.FILE.OK			
L.MSG.HEADER.OK	.DA MSG.HEADER.OK
L.MSG.HEADER.KO	.DA MSG.HEADER.KO
L.MSG.DONE		.DA MSG.DONE
				.DA 0
*--------------------------------------
CS.INIT			
.1				>INC.G ArgCount
				>SYSCALL ArgV
				bcs .7
				
				>STYA ZPPtr1
				
				lda (ZPPtr1)
				cmp #'-'
				bne .4
				
				ldy #1 
				lda (ZPPtr1),y

				ldx OptionList
				
.2				cmp OptionList,x
				beq .3
				dex
				bne .2
				
.99				>LDYA L.MSG.USAGE
				>SYSCALL printf
				lda #E.SYN
				sec
				rts
				
.3				ldy OptionVars-1,x
				lda #$80
				sta (pData),y
				bra .1
				
.4				>LDA.G hInputBuf
				bne .5
				jsr CS.INIT.OPENINPUT
				bcc .1					scan for any other args
				rts
				
.5				>LDA.G hOutputFile
				bne .99
				jsr CS.INIT.CREATEOUTPUT
				bcc .1					scan for any other args
				rts
				
.7				>LDA.G hOutputFile
				beq .99
				
				>LDYAI 256
				>SYSCALL getmem
				bcs CS.INIT.RTS
				
				>STYA ZPBufPtr
				txa
				>STA.G hBuf
				
.8				clc
CS.INIT.RTS		rts
*--------------------------------------
CS.INIT.OPENINPUT
				>PUSHWI 0				Aux type
				>PUSHBI 0				Type
				>PUSHBI	O.RDONLY	
				>LDYA ZPPtr1
				>SYSCALL LoadFile
				bcs CS.INIT.RTS
				
				>STYA ZPInputBufLen
				>PUSHYA
				
				txa
				>STA.G hInputBuf
				>SYSCALL GetMemPtr
				>STYA ZPInputBufPtr
				
				>LDYA L.MSG.FILE.OK
				>SYSCALL printf				
				
				lda (ZPInputBufPtr)
				cmp #'B'
				bne .99
				
				ldy #1
				lda (ZPInputBufPtr),y
				cmp #'M'
				bne .99
				
				ldy #$1C				BBP LO
				lda (ZPInputBufPtr),y
				cmp #1
				bne .99
				iny						BBP HI
				lda (ZPInputBufPtr),y
				bne .99
				
				ldy #$16				H LO
				lda (ZPInputBufPtr),y
				>STA.G PIX.Header+S.BM.H
				pha
				eor #$ff
				sta ZPLineCount
				
				ldy #$17				H HI
				lda (ZPInputBufPtr),y
				>STA.G PIX.Header+S.BM.H+1
				>PUSHA
				eor #$ff
				sta ZPLineCount+1
				
				pla
				>PUSHA
								
				ldy #$12				W LO
				lda (ZPInputBufPtr),y
				>STA.G PIX.Header+S.BM.W
				pha
				
				ldy #$13				W HI
				lda (ZPInputBufPtr),y
				>STA.G PIX.Header+S.BM.W+1
				>PUSHA
				pla
				>PUSHA
				
				>LDYA L.MSG.HEADER.OK
				>SYSCALL printf
				clc
				rts
				
.99				>LDYA L.MSG.HEADER.KO
				>SYSCALL printf
				lda #E.IBIN
				sec
				rts				
*--------------------------------------
CS.INIT.CREATEOUTPUT
				>PUSHWI 0				Aux type
				>PUSHBI $CB				PIX Type
				>PUSHBI	O.WRONLY+O.CREATE	
				>LDYA ZPPtr1
				>SYSCALL FOpen
				bcs .9
				>STA.G hOutputFile
.9				rts				
*--------------------------------------
CS.RUN			lda #S.BM.F.BBP1
				>STA.G PIX.Header+S.BM.F

				>LDA.G PIX.Header+S.BM.W
				sta ZPBMPRowBytes
				sta ZPPIXRowBytes
				
				iny
				lda (pData),y
				sta ZPBMPRowBytes+1
				sta ZPPIXRowBytes+1
				
				lda ZPBMPRowBytes
				and #%00011111
				beq .1
				lda ZPBMPRowBytes
				and #%11100000
				clc
				adc #%00100000
				bcc .1
				
				inc ZPBMPRowBytes+1
				
.1				lsr ZPBMPRowBytes+1
				ror
				lsr ZPBMPRowBytes+1
				ror
				lsr ZPBMPRowBytes+1
				ror
				sta ZPBMPRowBytes
				
				lda ZPPIXRowBytes
				and #%00000111
				beq .2
				lda ZPPIXRowBytes
				and #%11111000
				clc
				adc #%00001000
				bcc .2
				
				inc ZPPIXRowBytes+1
								
.2				lsr ZPPIXRowBytes+1
				ror
				lsr ZPPIXRowBytes+1
				ror
				lsr ZPPIXRowBytes+1
				ror
				sta ZPPIXRowBytes
				>STA.G PIX.Header+S.BM.RowBytes
				
				lda ZPInputBufPtr
				clc
				adc	ZPInputBufLen
				sta ZPInputDataPtr
				lda ZPInputBufPtr+1
				adc	ZPInputBufLen+1
				sta ZPInputDataPtr+1
				
				>PUSHWI S.BM
				>PUSHEA.G PIX.Header
				>LDA.G hOutputFile
				>SYSCALL FWrite
				bcs CS.RUN.RTS
				
CS.RUN.LOOP		inc ZPLineCount
				bne .1
				inc ZPLineCount+1
				beq .8
				
.1				lda ZPInputDataPtr
				sec
				sbc ZPBMPRowBytes
				sta ZPInputDataPtr
				lda ZPInputDataPtr+1
				sbc ZPBMPRowBytes+1
				sta ZPInputDataPtr+1
				
				>LDYA ZPBufPtr
				>STYA ZPPTR1
				
				ldy #0
				
.2				lda (ZPInputDataPtr),y
				tax
				lda TWIST,x
				sta (ZPPTR1)
				inc ZPPTR1
				bne .3
				
				inc ZPPTR1+1
				
.3				iny
				cpy ZPPIXRowBytes
				bne .2
				
				>PUSHW ZPPIXRowBytes
				>PUSHW ZPBufPtr
				>LDA.G hOutputFile
				>SYSCALL FWrite
				bcs CS.RUN.RTS
				
				
				
				bra CS.RUN.LOOP
				
				
.8				>LDYA L.MSG.DONE
				>SYSCALL printf
				
				lda #0
				sec
CS.RUN.RTS		rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			>LDA.G hBuf
				beq .1
				>SYSCALL FreeMem

.1				>LDA.G hOutputFile
				beq .8

				>SYSCALL fclose
				
.8				clc
				rts
*--------------------------------------
CS.END
*--------------------------------------
				.INB ROOT/X.TWIST.G
*--------------------------------------		
OptionList		>PSTR "Vv"
OptionVars		.DA #bVerbose,#bVerbose
MSG.USAGE		.AZ "Usage : BMP2PIX BMP-File \r\n   -V : Verbose Mode\r\n"
MSG.FILE.OK		.AZ "BMP Loaded, %D Bytes Read\r\n"		
MSG.HEADER.OK	.AZ "BMP Size : %D x %D pixels\r\n"		
MSG.HEADER.KO	.AZ "Invalid Input File Format\r\n"		
MSG.DONE		.AZ "All Done!!!\r\n"		
*--------------------------------------
* LOWRES Patette : https://comp.sys.apple2.narkive.com/lTSrj2ZI/apple-ii-colour-rgb
*--------------------------------------
0 #000000
1 #e31e60
2 #604ebd
3 #ff44fd
4 #00a360
5 #9c9c9c
6 #14cffd
7 #d0c3ff
8 #607203
9 #ff6a3c
a #9c9c9c (yes it is the same as #5!)
b #ffa0d0
c #14f53c
d #d0dd8d
e #72ffd0
f #ffffff
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
PIX.Header		.BS	S.BM
ArgCount		.BS 1
hInputBuf		.BS 1
hOutputFile		.BS 1
bVerbose		.BS 1
hBuf			.BS 1
DS.END
				.ED
*--------------------------------------
MAN
SAVE /A2OSX.BUILD/ROOT/BMP2PIX.S
ASM
