lazarus/formats/dcc/dcc.go

202 lines
4.9 KiB
Go
Raw Normal View History

2019-01-09 02:12:53 +00:00
package dcc
2019-01-22 02:43:21 +00:00
import (
"encoding/binary"
2019-02-10 19:41:33 +00:00
"errors"
2019-01-22 02:43:21 +00:00
"io"
2019-01-23 02:49:23 +00:00
"github.com/FooSoft/lazarus/streaming"
2019-01-22 02:43:21 +00:00
)
2019-01-09 02:12:53 +00:00
2019-01-22 02:43:21 +00:00
type Sprite struct {
2019-01-09 02:12:53 +00:00
}
2019-02-10 19:41:33 +00:00
type bounds struct {
x1 int
y1 int
x2 int
y2 int
2019-01-17 03:04:49 +00:00
}
type fileHeader struct {
Signature uint8
Version uint8
DirCount uint8
FramesPerDir uint32
Tag uint32
FinalDc6Size uint32
}
type directionHeader struct {
2019-02-10 18:36:01 +00:00
CodedSize uint32
2019-01-17 03:04:49 +00:00
HasRawPixelEncoding bool
CompressEqualCells bool
2019-02-10 18:36:01 +00:00
Variable0Bits uint8
WidthBits uint8
HeightBits uint8
OffsetXBits uint8
OffsetYBits uint8
OptionalBytesBits uint8
CodedBytesBits uint8
2019-01-17 03:04:49 +00:00
}
2019-02-10 19:41:33 +00:00
type direction struct {
header directionHeader
frames []frame
bounds bounds
}
2019-01-17 03:04:49 +00:00
type frameHeader struct {
2019-02-10 18:36:01 +00:00
Variable0 uint32
Width uint32
Height uint32
OffsetX int32
OffsetY int32
OptionalBytes uint32
CodedBytes uint32
2019-01-17 03:04:49 +00:00
FrameBottomUp bool
2019-02-10 19:41:33 +00:00
}
type frame struct {
header frameHeader
bounds bounds
2019-01-17 03:04:49 +00:00
}
2019-01-22 02:43:21 +00:00
func NewFromReader(reader io.ReadSeeker) (*Sprite, error) {
2019-02-03 22:08:45 +00:00
var fileHead fileHeader
if err := binary.Read(reader, binary.LittleEndian, &fileHead); err != nil {
2019-01-22 02:43:21 +00:00
return nil, err
}
2019-02-10 19:41:33 +00:00
var directions []direction
2019-02-03 22:08:45 +00:00
for i := 0; i < int(fileHead.DirCount); i++ {
2019-01-23 02:49:23 +00:00
var offsetDir uint32
if err := binary.Read(reader, binary.LittleEndian, &offsetDir); err != nil {
return nil, err
}
offset, err := reader.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
if _, err := reader.Seek(int64(offsetDir), io.SeekStart); err != nil {
return nil, err
}
2019-02-10 19:41:33 +00:00
dirData, err := readDirection(reader, fileHead)
if err != nil {
2019-01-23 02:49:23 +00:00
return nil, err
}
2019-02-10 19:41:33 +00:00
directions = append(directions, *dirData)
2019-01-23 02:49:23 +00:00
if _, err := reader.Seek(offset, io.SeekStart); err != nil {
2019-01-22 03:34:53 +00:00
return nil, err
}
}
2019-01-09 02:12:53 +00:00
return nil, nil
}
2019-01-23 02:49:23 +00:00
2019-02-10 19:41:33 +00:00
func readDirectionHeader(bitReader *streaming.BitReader) directionHeader {
2019-02-10 16:55:14 +00:00
var dirHead directionHeader
2019-02-10 18:36:01 +00:00
dirHead.CodedSize = uint32(bitReader.ReadUint(32))
2019-02-10 16:55:14 +00:00
dirHead.HasRawPixelEncoding = bitReader.ReadBool()
dirHead.CompressEqualCells = bitReader.ReadBool()
2019-02-10 18:36:01 +00:00
dirHead.Variable0Bits = uint8(bitReader.ReadUint(4))
dirHead.WidthBits = uint8(bitReader.ReadUint(4))
dirHead.HeightBits = uint8(bitReader.ReadUint(4))
dirHead.OffsetXBits = uint8(bitReader.ReadInt(4))
dirHead.OffsetYBits = uint8(bitReader.ReadInt(4))
dirHead.OptionalBytesBits = uint8(bitReader.ReadUint(4))
dirHead.CodedBytesBits = uint8(bitReader.ReadUint(4))
2019-02-10 16:55:14 +00:00
2019-02-10 19:41:33 +00:00
return dirHead
2019-01-23 02:49:23 +00:00
}
2019-02-10 19:41:33 +00:00
func readFrameHeader(bitReader *streaming.BitReader, dirHead directionHeader) frameHeader {
2019-02-10 16:55:14 +00:00
var frameHead frameHeader
2019-02-03 22:08:45 +00:00
2019-02-10 18:36:01 +00:00
frameHead.Variable0 = uint32(bitReader.ReadUintPacked(int(dirHead.Variable0Bits)))
frameHead.Width = uint32(bitReader.ReadUintPacked(int(dirHead.WidthBits)))
frameHead.Height = uint32(bitReader.ReadUintPacked(int(dirHead.HeightBits)))
frameHead.OffsetX = int32(bitReader.ReadIntPacked(int(dirHead.OffsetXBits)))
frameHead.OffsetY = int32(bitReader.ReadIntPacked(int(dirHead.OffsetYBits)))
frameHead.OptionalBytes = uint32(bitReader.ReadUintPacked(int(dirHead.OptionalBytesBits)))
frameHead.CodedBytes = uint32(bitReader.ReadUintPacked(int(dirHead.CodedBytesBits)))
2019-02-10 16:55:14 +00:00
frameHead.FrameBottomUp = bitReader.ReadBool()
2019-02-03 22:08:45 +00:00
2019-02-10 19:41:33 +00:00
return frameHead
2019-02-03 22:08:45 +00:00
}
2019-02-10 19:41:33 +00:00
func readFrameHeaders(bitReader *streaming.BitReader, fileHead fileHeader, dirHead directionHeader) ([]frameHeader, error) {
var frameHeads []frameHeader
for i := 0; i < int(fileHead.FramesPerDir); i++ {
frameHead := readFrameHeader(bitReader, dirHead)
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")
}
frameHeads = append(frameHeads, frameHead)
}
return frameHeads, nil
}
func readDirection(reader io.ReadSeeker, fileHead fileHeader) (*direction, error) {
2019-02-10 04:06:09 +00:00
bitReader := streaming.NewBitReader(reader)
2019-02-10 16:55:14 +00:00
dirHead := readDirectionHeader(bitReader)
2019-02-10 16:56:45 +00:00
if err := bitReader.Error(); err != nil {
2019-02-10 19:41:33 +00:00
return nil, err
2019-02-10 16:56:45 +00:00
}
2019-02-10 19:41:33 +00:00
frameHeads, err := readFrameHeaders(bitReader, fileHead, dirHead)
if err != nil {
return nil, err
2019-02-10 16:56:45 +00:00
}
2019-02-03 22:08:45 +00:00
2019-02-10 19:41:33 +00:00
var dirData direction
for i, frameHead := range frameHeads {
frameData := frame{
header: frameHead,
bounds: bounds{
x1: int(frameHead.OffsetX),
y1: int(frameHead.OffsetY) - int(frameHead.Height) + 1,
x2: int(frameHead.OffsetX) + int(frameHead.Width),
y2: int(frameHead.OffsetY) + 1,
},
}
dirData.frames = append(dirData.frames, frameData)
if i == 0 {
dirData.bounds = frameData.bounds
} else {
if dirData.bounds.x1 > frameData.bounds.x1 {
dirData.bounds.x1 = frameData.bounds.x1
}
if dirData.bounds.y1 > frameData.bounds.y1 {
dirData.bounds.y1 = frameData.bounds.y1
}
if dirData.bounds.x2 < frameData.bounds.x2 {
dirData.bounds.x2 = frameData.bounds.x2
}
if dirData.bounds.y2 < frameData.bounds.y2 {
dirData.bounds.y2 = frameData.bounds.y2
}
}
}
2019-02-03 22:08:45 +00:00
2019-02-10 19:41:33 +00:00
return &dirData, nil
2019-01-23 02:49:23 +00:00
}