improved error handling, make functions into methods where appropriate
This commit is contained in:
parent
ea282171fc
commit
40c09c36a6
@ -19,13 +19,7 @@ type directionHeader struct {
|
|||||||
CodedBytesBits uint8
|
CodedBytesBits uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
type direction struct {
|
func readDirectionHeader(bitReader *streaming.BitReader) (*directionHeader, error) {
|
||||||
header directionHeader
|
|
||||||
frames []frame
|
|
||||||
bounds bounds
|
|
||||||
}
|
|
||||||
|
|
||||||
func readDirectionHeader(bitReader *streaming.BitReader) directionHeader {
|
|
||||||
var dirHead directionHeader
|
var dirHead directionHeader
|
||||||
|
|
||||||
dirHead.CodedSize = uint32(bitReader.ReadUint(32))
|
dirHead.CodedSize = uint32(bitReader.ReadUint(32))
|
||||||
@ -39,18 +33,22 @@ func readDirectionHeader(bitReader *streaming.BitReader) directionHeader {
|
|||||||
dirHead.OptionalBytesBits = uint8(bitReader.ReadUint(4))
|
dirHead.OptionalBytesBits = uint8(bitReader.ReadUint(4))
|
||||||
dirHead.CodedBytesBits = uint8(bitReader.ReadUint(4))
|
dirHead.CodedBytesBits = uint8(bitReader.ReadUint(4))
|
||||||
|
|
||||||
return dirHead
|
if err := bitReader.Error(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &dirHead, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readDirection(reader io.ReadSeeker, fileHead fileHeader) (*direction, error) {
|
func readDirection(reader io.ReadSeeker, fileHead fileHeader) (*direction, error) {
|
||||||
bitReader := streaming.NewBitReader(reader)
|
bitReader := streaming.NewBitReader(reader)
|
||||||
|
|
||||||
dirHead := readDirectionHeader(bitReader)
|
dirHead, err := readDirectionHeader(bitReader)
|
||||||
if err := bitReader.Error(); err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
frameHeads, err := readFrameHeaders(bitReader, fileHead, dirHead)
|
frameHeads, err := readFrameHeaders(bitReader, fileHead, *dirHead)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -89,12 +87,12 @@ func readDirection(reader io.ReadSeeker, fileHead fileHeader) (*direction, error
|
|||||||
|
|
||||||
var entries []pixelBufferEntry
|
var entries []pixelBufferEntry
|
||||||
|
|
||||||
entries, err = decodeDirectionStage1(bitReader, &dirData, entries)
|
entries, err = dirData.decodeStage1(bitReader, entries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
entries, err = decodeDirectionStage2(bitReader, &dirData, entries)
|
entries, err = dirData.decodeStage2(bitReader, entries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -102,10 +100,16 @@ func readDirection(reader io.ReadSeeker, fileHead fileHeader) (*direction, error
|
|||||||
return &dirData, nil
|
return &dirData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeDirectionStage1(bitReader *streaming.BitReader, dirData *direction, entries []pixelBufferEntry) ([]pixelBufferEntry, error) {
|
type direction struct {
|
||||||
|
header directionHeader
|
||||||
|
frames []frame
|
||||||
|
bounds bounds
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *direction) decodeStage1(bitReader *streaming.BitReader, entries []pixelBufferEntry) ([]pixelBufferEntry, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeDirectionStage2(bitReader *streaming.BitReader, dirData *direction, entries []pixelBufferEntry) ([]pixelBufferEntry, error) {
|
func (d *direction) decodeStage2(bitReader *streaming.BitReader, entries []pixelBufferEntry) ([]pixelBufferEntry, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ type frame struct {
|
|||||||
bounds bounds
|
bounds bounds
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFrameHeader(bitReader *streaming.BitReader, dirHead directionHeader) frameHeader {
|
func readFrameHeader(bitReader *streaming.BitReader, dirHead directionHeader) (*frameHeader, error) {
|
||||||
var frameHead frameHeader
|
var frameHead frameHeader
|
||||||
|
|
||||||
frameHead.Variable0 = uint32(bitReader.ReadUintPacked(int(dirHead.Variable0Bits)))
|
frameHead.Variable0 = uint32(bitReader.ReadUintPacked(int(dirHead.Variable0Bits)))
|
||||||
@ -34,30 +34,34 @@ func readFrameHeader(bitReader *streaming.BitReader, dirHead directionHeader) fr
|
|||||||
frameHead.CodedBytes = uint32(bitReader.ReadUintPacked(int(dirHead.CodedBytesBits)))
|
frameHead.CodedBytes = uint32(bitReader.ReadUintPacked(int(dirHead.CodedBytesBits)))
|
||||||
frameHead.FrameBottomUp = bitReader.ReadBool()
|
frameHead.FrameBottomUp = bitReader.ReadBool()
|
||||||
|
|
||||||
return frameHead
|
if err := bitReader.Error(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if frameHead.OptionalBytes != 0 {
|
||||||
|
return nil, errors.New("optional frame data not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
if frameHead.FrameBottomUp {
|
||||||
|
return nil, errors.New("bottom-up frames are not supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
if frameHead.Width == 0 || frameHead.Height == 0 {
|
||||||
|
return nil, errors.New("invalid frame dimensions")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &frameHead, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFrameHeaders(bitReader *streaming.BitReader, fileHead fileHeader, dirHead directionHeader) ([]frameHeader, error) {
|
func readFrameHeaders(bitReader *streaming.BitReader, fileHead fileHeader, dirHead directionHeader) ([]frameHeader, error) {
|
||||||
var frameHeads []frameHeader
|
var frameHeads []frameHeader
|
||||||
for i := 0; i < int(fileHead.FramesPerDir); i++ {
|
for i := 0; i < int(fileHead.FramesPerDir); i++ {
|
||||||
frameHead := readFrameHeader(bitReader, dirHead)
|
frameHead, err := readFrameHeader(bitReader, dirHead)
|
||||||
if err := bitReader.Error(); err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if frameHead.OptionalBytes != 0 {
|
frameHeads = append(frameHeads, *frameHead)
|
||||||
return nil, errors.New("optional frame data not supported")
|
|
||||||
}
|
|
||||||
|
|
||||||
if frameHead.FrameBottomUp {
|
|
||||||
return nil, errors.New("bottom-up frames are not supported")
|
|
||||||
}
|
|
||||||
|
|
||||||
if frameHead.Width == 0 || frameHead.Height == 0 {
|
|
||||||
return nil, errors.New("invalid frame dimensions")
|
|
||||||
}
|
|
||||||
|
|
||||||
frameHeads = append(frameHeads, frameHead)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return frameHeads, nil
|
return frameHeads, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user