2019-11-15 07:41:53 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
jpeg.go contains constants, structure and functions specific to the JPEG.
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mjpeg
|
|
|
|
|
2019-11-15 08:55:35 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
2019-11-15 08:11:02 +03:00
|
|
|
|
2019-11-15 07:41:53 +03:00
|
|
|
// JPEG marker codes.
|
|
|
|
const (
|
|
|
|
soi = 0xd8 // Start of image.
|
|
|
|
dri = 0xdd // Define restart interval.
|
|
|
|
dqt = 0xdb // Define quantization tables.
|
|
|
|
dht = 0xde // Define hierarchical progression.
|
|
|
|
sos = 0xda // Start of scan.
|
|
|
|
app0 = 0xe0 // TODO: find out what this is.
|
|
|
|
sof0 = 0xc0 // Baseline
|
|
|
|
)
|
2019-11-15 08:11:02 +03:00
|
|
|
|
2019-11-15 08:55:35 +03:00
|
|
|
// writeMarker writes an JPEG marker with code to w.
|
|
|
|
func writeMarker(w io.Writer, code byte) error {
|
2019-11-15 08:11:02 +03:00
|
|
|
_, err := w.Write([]byte{0xff, code})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-15 08:55:35 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|