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 db877fd934
commit 238771459b
1 changed files with 31 additions and 3 deletions

View File

@ -24,7 +24,10 @@ LICENSE
package mjpeg package mjpeg
import "io" import (
"fmt"
"io"
)
// JPEG marker codes. // JPEG marker codes.
const ( const (
@ -37,11 +40,36 @@ const (
sof0 = 0xc0 // Baseline sof0 = 0xc0 // Baseline
) )
// putMarker writes an JPEG marker with code to w. // writeMarker writes an JPEG marker with code to w.
func putMarker(w io.Writer, code byte) error { func writeMarker(w io.Writer, code byte) error {
_, err := w.Write([]byte{0xff, code}) _, err := w.Write([]byte{0xff, code})
if err != nil { if err != nil {
return err return err
} }
return nil 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
}