support decoding M-JPEG streams with any quantization (#566) (#583)

This commit is contained in:
Alessandro Ros
2024-06-22 23:32:31 +02:00
committed by GitHub
parent 3ee5fbdcf7
commit 4750375772
6 changed files with 198 additions and 19 deletions

View File

@@ -7,7 +7,7 @@ import (
type headerQuantizationTable struct {
MBZ uint8
Precision uint8
Tables []byte
Tables [][]byte
}
func (h *headerQuantizationTable) unmarshal(byts []byte) (int, error) {
@@ -18,29 +18,42 @@ func (h *headerQuantizationTable) unmarshal(byts []byte) (int, error) {
h.MBZ = byts[0]
h.Precision = byts[1]
if h.Precision != 0 {
return 0, fmt.Errorf("Precision %d is not supported", h.Precision)
return 0, fmt.Errorf("precision %d is not supported", h.Precision)
}
length := int(byts[2])<<8 | int(byts[3])
switch length {
case 64, 128:
default:
return 0, fmt.Errorf("Quantization table length %d is not supported", length)
return 0, fmt.Errorf("table length %d is not supported", length)
}
if (len(byts) - 4) < length {
return 0, fmt.Errorf("buffer is too short")
}
h.Tables = byts[4 : 4+length]
tableCount := length / 64
h.Tables = make([][]byte, tableCount)
n := 0
for i := 0; i < tableCount; i++ {
h.Tables[i] = byts[4+n : 4+64+n]
n += 64
}
return 4 + length, nil
}
func (h headerQuantizationTable) marshal(byts []byte) []byte {
byts = append(byts, h.MBZ)
byts = append(byts, h.Precision)
l := len(h.Tables)
l := len(h.Tables) * 64
byts = append(byts, []byte{byte(l >> 8), byte(l)}...)
byts = append(byts, h.Tables...)
for i := 0; i < len(h.Tables); i++ {
byts = append(byts, h.Tables[i]...)
}
return byts
}