codec/mjpeg/jpeg.go: added writeHuffman function to write JPEG huffman tables to an io.Writer.

This commit is contained in:
Saxon 2019-11-15 16:25:35 +10:30
parent 4781a02ec5
commit 63f694a0a5
1 changed files with 31 additions and 3 deletions

View File

@ -24,7 +24,10 @@ LICENSE
package mjpeg
import "io"
import (
"fmt"
"io"
)
// JPEG marker codes.
const (
@ -37,11 +40,36 @@ const (
sof0 = 0xc0 // Baseline
)
// putMarker writes an JPEG marker with code to w.
func putMarker(w io.Writer, code byte) error {
// writeMarker writes an JPEG marker with code to w.
func writeMarker(w io.Writer, code byte) error {
_, err := w.Write([]byte{0xff, code})
if err != nil {
return err
}
return nil
}
// writeHuffman write a JPEG huffman table to w.
func writeHuffman(w io.Writer, class, id int, bits, values []byte) error {
_, err := w.Write([]byte{byte(class<<4 | id)})
if err != nil {
return fmt.Errorf("could not write class and id: %w", err)
}
var n int
for i := 1; i <= 16; i++ {
n += int(bits[i])
}
_, err = w.Write(bits[1:17])
if err != nil {
return fmt.Errorf("could not write first lot of huffman bytes: %w", err)
}
_, err = w.Write(values[0:n])
if err != nil {
return fmt.Errorf("could not write second lot of huffman bytes: %w", err)
}
return nil
}