Package dmd implements well-known formats for dot-matrix display (DMD) art.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.2 KiB

package container
import (
"encoding/binary"
"io"
)
// DMD1Chunk is a named chunk.
type DMD1Chunk struct {
// Name of the chunk.
Name [4]byte
// Size of the chunk, including the name of the chunk.
Size uint32
// Data is the payload of the chunk.
Data []byte
}
func (chunk *DMD1Chunk) ReadFrom(r io.Reader) (n int64, err error) {
if err = binary.Read(r, binary.LittleEndian, &chunk.Name); err != nil {
return
}
if err = binary.Read(r, binary.LittleEndian, &chunk.Size); err != nil {
return
}
chunk.Data = make([]byte, chunk.Size)
if chunk.Size > 0 {
if _, err = io.ReadFull(r, chunk.Data); err != nil {
return
}
}
return 8 + int64(chunk.Size), nil
}
func (chunk *DMD1Chunk) WriteTo(w io.Writer) (n int64, err error) {
if err = binary.Write(w, binary.LittleEndian, chunk.Name); err != nil {
return
}
n += 4
chunk.Size = uint32(len(chunk.Data))
if err = binary.Write(w, binary.LittleEndian, chunk.Size); err != nil {
return
}
n += 4
if chunk.Size > 0 {
var m int
if m, err = w.Write(chunk.Data); err != nil {
return
}
n += int64(m)
}
return
}
var (
_ io.ReaderFrom = (*DMD1Chunk)(nil)
_ io.WriterTo = (*DMD1Chunk)(nil)
)