; GnarlPlot colour conversion code V0.31 28/8/04
; Copyright 2008 Jeffrey Lee
; This file is part of GnarlPlot.
; GnarlPlot is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; GnarlPlot is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
; You should have received a copy of the GNU General Public License
; along with GnarlPlot.  If not, see <http://www.gnu.org/licenses/>.

	GET gp.s

	AREA gp_col, CODE, READONLY

	EXPORT gp_col_conv
	EXPORT gp_col_to_tbgr
	EXPORT gp_col_from_tbgr
	EXPORT gp_col_24_grey
	EXPORT gp_col_24_rgb
	EXPORT gp_col_convx
	EXPORT gp_col_trueps

gp_col_conv
	; r0=in format, r1=in col, r2=out format
	CMP R0,R2
	MOVEQ R0,R1
	MOVEQ PC,R14 ; Return if formats equal
	CMP R0,#2
	CMPLS R2,#2
	MOVHI PC,R14 ; Return if bad format
	ORR R2,R0,R2,LSL #2 ; Encode formats
	ADD PC,PC,R2,LSL #2 ; branch
	MOV R0,R0
	MOV PC,R14 ; 0 -> 0
	B conv_10 ; 1 -> 0
	B conv_20 ; 2 -> 0
	MOV PC,R14 ; 3 -> 0
	B conv_01 ; 0 -> 1
	MOV PC,R14 ; 1 -> 1
	B conv_21 ; 2 -> 1
	MOV PC,R14 ; 3 -> 1
	B conv_02 ; 0 -> 2
	B conv_12 ; 1 -> 2
	MOV PC,R14 ; 2 -> 2
conv_10
	GP_16_TO_8 R1,R0,R2
	MOV PC,R14
conv_20
	GP_24_TO_8 R1,R0,R2
	MOV PC,R14
conv_01
	GP_8_TO_16 R1,R0,R2
	MOV PC,R14
conv_21
	GP_24_TO_16 R1,R0,R2
	MOV PC,R14
conv_02
	GP_8_TO_24 R1,R0,R2
	MOV PC,R14
conv_12
	GP_16_TO_24 R1,R0,R2
	MOV PC,R14

gp_col_convx
	; r0=in format, r1=in col, r2=out format
	; returns with the colour expanded to fill the word
	STMFD	R13!,{R2,R14}
	CMP	R0,R2
	MOVEQ	R0,R1
	BLNE	gp_col_conv ; blah
	LDMFD	R13!,{R2,R14}
	CMP	R2,#1
	MOVLT	R0,R0,LSL #24 ; Clear any top bits
	ORRLT	R0,R0,R0,LSR #8 ; Make 2 bytes
	MOVEQ	R0,R0,LSL #16
	ORRLE	R0,R0,R0,LSR #16 ; Make 4 bytes
	MOV	PC,R14 ; Return

; Would these two be quicker using lookups?

gp_col_to_tbgr
	; r0=in 8bpp BGGRBRTT
	; returns it in TTBBGGRR format, top bits clear
	AND R1,R0,#3 ; Tint
	AND R2,R0,#&70 ; Green and high red
	AND R3,R0,#&80 ; High blue
	AND R12,R0,#&08 ; Low blue
	AND R0,R0,#&04 ; Low red
	MOV R0,R0,LSR #2 ; Low red
	ORR R0,R0,R1,LSL #6 ; Tint
	ORR R0,R0,R2,LSR #3 ; Green and high red
	ORR R0,R0,R3,LSR #2 ; High blue
	ORR R0,R0,R12,LSL #1 ; Low blue
	MOV PC,R14

gp_col_from_tbgr
	; r0=in 8bpp TTBBGGRR
	; returns it in BGGRBRTT format, top bits clear
	AND R1,R0,#&C0 ; Tint
	AND R2,R0,#&0E ; Green and high red
	AND R3,R0,#&20 ; High blue
	AND R12,R0,#&10 ; Low blue
	AND R0,R0,#1 ; Low red
	MOV R0,R0,LSL #2 ; Low red
	ORR R0,R0,R1,LSR #6 ; Tint
	ORR R0,R0,R2,LSL #3 ; Green and high red
	ORR R0,R0,R3,LSL #2 ; High blue
	ORR R0,R0,R12,LSR #1 ; Low blue
	MOV PC,R14

gp_col_24_grey
	; r0=24bpp sprite col & greyscale
	; r1=24bpp screen col
	; Returns combined col in r0
	STMFD R13!,{R14}
	GP_24_GREY R0,R1,R2,R3,R12,R14
	MOV R0,R2
	LDMFD R13!,{PC}

gp_col_24_rgb
	; r0=24bpp sprite col
	; r1=24bpp mask
	; r2=24bpp screen col
	; Returns combind col in r0
	STMFD R13!,{R4,R14}
	GP_24_RGB R0,R1,R2,R3,R4,R12,R14
	MOV R0,R3
	LDMFD R13!,{R4,PC}

gp_col_trueps
	; r0 = ps
	; r1 = mt
	; Returns 'true' pixel size
	ADD R0,R1,R0,LSL #2
	LDRB R0,[PC,R0]
	MOV PC,R14
	DCB 0,1,1,1
	DCB 1,1,2,2
	DCB 2,2,2,3

	END
