mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-24 17:30:59 +00:00
[bugfix] Keep png transparency (#1522)
* keep png transparency * rewrite to switch case
This commit is contained in:
parent
59b2e10787
commit
fe66a2aed2
1 changed files with 24 additions and 4 deletions
|
@ -78,9 +78,29 @@ import (
|
|||
"io"
|
||||
)
|
||||
|
||||
// chunkTypeAncillaryBit is whether the first byte of a big-endian uint32 chunk
|
||||
// type (the first of four ASCII letters) is lower-case.
|
||||
const chunkTypeAncillaryBit = 0x20000000
|
||||
const (
|
||||
chunkTypeIHDR = 0x49484452
|
||||
chunkTypePLTE = 0x504C5445
|
||||
chunkTypeIDAT = 0x49444154
|
||||
chunkTypeIEND = 0x49454E44
|
||||
chunkTypeTRNS = 0x74524e53
|
||||
)
|
||||
|
||||
func isNecessaryChunkType(chunkType uint32) bool {
|
||||
switch chunkType {
|
||||
case chunkTypeIHDR:
|
||||
return true
|
||||
case chunkTypePLTE:
|
||||
return true
|
||||
case chunkTypeIDAT:
|
||||
return true
|
||||
case chunkTypeIEND:
|
||||
return true
|
||||
case chunkTypeTRNS:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// pngAncillaryChunkStripper wraps another io.Reader to strip ancillary chunks,
|
||||
// if the data is in the PNG file format. If the data isn't PNG, it is passed
|
||||
|
@ -179,7 +199,7 @@ func (r *pngAncillaryChunkStripper) Read(p []byte) (int, error) {
|
|||
// byte trailer, a checksum.
|
||||
r.pending = int64(binary.BigEndian.Uint32(r.buffer[:4])) + 4
|
||||
chunkType := binary.BigEndian.Uint32(r.buffer[4:])
|
||||
r.discard = (chunkType & chunkTypeAncillaryBit) != 0
|
||||
r.discard = !isNecessaryChunkType(chunkType)
|
||||
if r.discard {
|
||||
r.rIndex = r.wIndex
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue