0765d06a8165 — Laurens Holst 5 years ago
PNGViewer: Support palette transparency.
2 files changed, 92 insertions(+), 12 deletions(-)

M src/PNG.asm
M src/PNGViewer.asm
M src/PNG.asm +50 -0
@@ 30,6 30,8 @@ PNG: MACRO
 		dw 0
 	paletteData:
 		ds 256 * 3
+	transparencyData:
+		ds 256,255
 	ENDM
 
 ; de = reader

          
@@ 112,6 114,15 @@ PNG_GetPalette:
 	ret
 
 ; ix = this
+; hl <- transparency
+PNG_GetTransparency:
+	ld e,ixl
+	ld d,ixh
+	ld hl,PNG.transparencyData
+	add hl,de
+	ret
+
+; ix = this
 ; ix <- mapped buffer
 PNG_GetDataBuffer:
 	ld e,(ix + PNG.dataBuffer)

          
@@ 501,6 512,8 @@ PNG_ProcessChunk: PROC
 	jr z,PrefixI
 	cp "P"
 	jr z,PrefixP
+	cp "t"
+	jr z,Prefixt
 	jp PNG_ProcessUnknownChunk
 PrefixI:
 	ld a,e

          
@@ 514,6 527,11 @@ PrefixP:
 	cp "L"
 	jr z,PrefixPL
 	jp PNG_ProcessUnknownChunk
+Prefixt:
+	ld a,e
+	cp "R"
+	jr z,PrefixtR
+	jp PNG_ProcessUnknownChunk
 PrefixID:
 	ld a,h
 	cp "A"

          
@@ 529,6 547,11 @@ PrefixPL:
 	cp "T"
 	jr z,PrefixPLT
 	jp PNG_ProcessUnknownChunk
+PrefixtR:
+	ld a,h
+	cp "N"
+	jr z,PrefixtRN
+	jp PNG_ProcessUnknownChunk
 PrefixIDA:
 	ld a,l
 	cp "T"

          
@@ 544,6 567,11 @@ PrefixPLT:
 	cp "E"
 	jr z,PNG_ProcessPLTEChunk
 	jp PNG_ProcessUnknownChunk
+PrefixtRN:
+	ld a,l
+	cp "S"
+	jr z,PNG_ProcessTRNSChunk
+	jp PNG_ProcessUnknownChunk
 	ENDP
 
 ; dehl = name

          
@@ 580,6 608,28 @@ PNG_ProcessPLTEChunk:
 ; iy = reader
 ; ix = this
 ; f <- c: not set (end not reached)
+PNG_ProcessTRNSChunk:
+	exx
+	ld a,e
+	or d
+	jp nz,PNG_TerminateWithInvalidError
+	ex de,hl
+	ld hl,100H
+	sbc hl,de
+	jp c,PNG_TerminateWithInvalidError
+	ld c,e
+	ld b,d
+	call PNG_GetTransparency
+	ex de,hl
+	call Reader_ReadBlock_IY
+	and a
+	ret
+
+; dehl = name
+; dehl' = size
+; iy = reader
+; ix = this
+; f <- c: not set (end not reached)
 PNG_ProcessIDATChunk:
 	push ix
 	call PNG_GetDataWriter

          
M src/PNGViewer.asm +42 -12
@@ 148,6 148,7 @@ PNGViewer_GetScanner:
 PNGViewer_InitializePalette: PROC
 	push ix
 	call PNGViewer_GetPNG
+	call PNG_GetTransparency
 	call PNG_GetPalette
 	pop ix
 	ex de,hl

          
@@ 156,22 157,51 @@ PNGViewer_InitializePalette: PROC
 	xor a
 Loop:
 	push af
-	ld d,PNGViewer_rgbLUTs >> 8
-	ld e,(hl)
+	push bc
 	ld a,(de)
+	inc de
+	push de
+	ld d,(hl)
+	inc hl
+	ld b,(hl)
+	inc hl
+	ld c,(hl)
 	inc hl
-	inc d
-	ld e,(hl)
-	ex de,hl
+	push hl
+	inc a
+	jr z,Opaque
+	dec a
+	jr z,Transparent
+	ld e,a
+	ld h,b
+	push de
+	call PNGViewer_MultiplyAlpha
+	pop de
+	ld b,h
+	ld h,c
+	push de
+	call PNGViewer_MultiplyAlpha
+	pop de
+	ld c,h
+	ld h,d
+	push de
+	call PNGViewer_MultiplyAlpha
+	pop de
+	ld d,h
+Opaque:
+	ld h,PNGViewer_rgbLUTs >> 8
+	ld l,d
+	ld a,(hl)
+	ld l,b
+	inc h
 	or (hl)
-	ex de,hl
-	inc hl
-	inc d
-	ld e,(hl)
-	ex de,hl
+	ld l,c
+	inc h
 	or (hl)
-	ex de,hl
-	inc hl
+Transparent:
+	pop hl
+	pop de
+	pop bc
 	ld (bc),a
 	inc bc
 	pop af