Move RLE decoding into a separate package
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
|||||||
"ur.gs/ordoor/internal/data"
|
"ur.gs/ordoor/internal/data"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var transparent = color.RGBA{0, 0, 0, 0}
|
||||||
|
|
||||||
// Important conversions:
|
// Important conversions:
|
||||||
//
|
//
|
||||||
// * Width & height now stored using int
|
// * Width & height now stored using int
|
||||||
@@ -46,57 +48,18 @@ func ConvertObject(rawObj *data.Object, name string) *Object {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
var transparent = color.RGBA{0, 0, 0, 0}
|
|
||||||
|
|
||||||
// WIP. Try to convert the pixeldata into a picture.
|
|
||||||
func spriteToPic(name string, idx int, sprite *data.Sprite) *pixel.PictureData {
|
func spriteToPic(name string, idx int, sprite *data.Sprite) *pixel.PictureData {
|
||||||
pic := pixel.MakePictureData(pixel.R(float64(0), float64(0), float64(sprite.Width), float64(sprite.Height)))
|
pic := pixel.MakePictureData(pixel.R(float64(0), float64(0), float64(sprite.Width), float64(sprite.Height)))
|
||||||
|
width := int(sprite.Width)
|
||||||
|
height := int(sprite.Height)
|
||||||
|
|
||||||
log.Printf("%v %v: width=%v height=%v", name, idx, sprite.Width, sprite.Height)
|
log.Printf("%v %v: width=%v height=%v", name, idx, width, height)
|
||||||
|
|
||||||
for y := 0; y < int(sprite.Height); y++ {
|
for y := 0; y < height; y++ {
|
||||||
encoded := sprite.Rows[y]
|
for x := 0; x < width; x++ {
|
||||||
decoded := make([]byte, 0, int(sprite.Width))
|
b := sprite.Data[y*width+x]
|
||||||
|
|
||||||
// Start with all bytes transparent
|
// Update the picture
|
||||||
for x := 0; x < int(sprite.Width); x++ {
|
|
||||||
pic.Pix[pic.Index(pixel.V(float64(x), float64(y)))] = transparent
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(encoded); i++ {
|
|
||||||
b := encoded[i]
|
|
||||||
|
|
||||||
// This appears to be a kind of RLE
|
|
||||||
if b == 0 {
|
|
||||||
continue // finished
|
|
||||||
} else if b < 0x80 {
|
|
||||||
// repeat the next byte this many times
|
|
||||||
for j := 0; j < int(b); j++ {
|
|
||||||
decoded = append(decoded, encoded[i+1])
|
|
||||||
}
|
|
||||||
|
|
||||||
i++ // skip the repeat byte
|
|
||||||
} else if b == 0x80 {
|
|
||||||
// transparent value, skip forward *x+1 rows
|
|
||||||
skip := int(encoded[i+1])
|
|
||||||
for i := 0; i < skip; i++ {
|
|
||||||
decoded = append(decoded, byte(0x00))
|
|
||||||
}
|
|
||||||
|
|
||||||
i++ // skip the count byte
|
|
||||||
} else {
|
|
||||||
// take the next b-0x80 bytes literally
|
|
||||||
literals := int(b) - 0x80
|
|
||||||
for j := i + 1; j <= i+literals; j++ {
|
|
||||||
decoded = append(decoded, encoded[j])
|
|
||||||
}
|
|
||||||
|
|
||||||
i = i + literals
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the picture
|
|
||||||
for x, b := range decoded {
|
|
||||||
if err := setPaletteColor(pic, x, y, b); err != nil {
|
if err := setPaletteColor(pic, x, y, b); err != nil {
|
||||||
log.Printf("%s %d: %d,%d: %v", name, idx, x, y, err)
|
log.Printf("%s %d: %d,%d: %v", name, idx, x, y, err)
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package data
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -9,12 +8,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"ur.gs/ordoor/internal/data/rle"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SpriteHeader struct {
|
type SpriteHeader struct {
|
||||||
Unknown0 uint32
|
Unknown0 uint32
|
||||||
Width uint16 // FIXME: I'm not certain this is what these are.
|
Width uint16
|
||||||
Height uint16 // FIXME: If they are, they may be the wrong way around
|
Height uint16
|
||||||
Padding1 uint32 // I don't think this is used. Could be wrong.
|
Padding1 uint32 // I don't think this is used. Could be wrong.
|
||||||
PixelSize uint32 // Size of PixelData, excluding this sprite header
|
PixelSize uint32 // Size of PixelData, excluding this sprite header
|
||||||
Padding2 uint64 // I don't think this is used either. Could be wrong.
|
Padding2 uint64 // I don't think this is used either. Could be wrong.
|
||||||
@@ -36,7 +37,7 @@ func (s SpriteHeader) Check(expectedSize uint32) error {
|
|||||||
type Sprite struct {
|
type Sprite struct {
|
||||||
SpriteHeader
|
SpriteHeader
|
||||||
|
|
||||||
Rows [][]byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type dirEntry struct {
|
type dirEntry struct {
|
||||||
@@ -132,18 +133,12 @@ func LoadObject(filename string) (*Object, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// The pixeldata seems to be formed of Y null-terminated records, with
|
buf := io.LimitReader(f, int64(sprite.PixelSize))
|
||||||
// varying numbers of bytes in each row. I don't know the internal
|
sprite.Data = make([]byte, sprite.Height*sprite.Width)
|
||||||
// structure yet, but there's definitely working pixel data in there
|
|
||||||
buf := bufio.NewReader(io.LimitReader(f, int64(sprite.PixelSize)))
|
|
||||||
sprite.Rows = make([][]byte, sprite.Height)
|
|
||||||
|
|
||||||
for y := 0; y < int(sprite.Height); y++ {
|
// The pixel data is RLE-compressed. Uncompress it here.
|
||||||
if row, err := buf.ReadBytes(0x00); err != nil {
|
if err := rle.Expand(buf, sprite.Data); err != nil {
|
||||||
return nil, fmt.Errorf("Reading row %v for sprite %v: %v", y, i, err)
|
return nil, err
|
||||||
} else {
|
|
||||||
sprite.Rows[y] = row
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out.Sprites = append(out.Sprites, sprite)
|
out.Sprites = append(out.Sprites, sprite)
|
||||||
|
60
internal/data/rle/rle.go
Normal file
60
internal/data/rle/rle.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
// Package rle implements the run-length encoding scheme discovered in Chaos
|
||||||
|
// Gate .obj files. It may be used for other data too - to be determined.
|
||||||
|
package rle
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Expand converts the compressed input data into uncompressed output data. The
|
||||||
|
// output buffer should be of at least the expected size. We will read until
|
||||||
|
// io.EOF is encountered.
|
||||||
|
func Expand(in io.Reader, out []byte) error {
|
||||||
|
buf := bufio.NewReader(in)
|
||||||
|
idx := 0
|
||||||
|
|
||||||
|
for {
|
||||||
|
b, err := buf.ReadByte()
|
||||||
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if b == 0x00 { // NOP
|
||||||
|
continue
|
||||||
|
} else if b < 0x80 { // repeat the next byte this many times
|
||||||
|
repeat := b
|
||||||
|
value, err := buf.ReadByte()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for x := 0; x < int(repeat); x++ {
|
||||||
|
out[idx+x] = value
|
||||||
|
}
|
||||||
|
idx = idx + int(repeat)
|
||||||
|
} else if b == 0x80 { // skip forward in the output by the next byte's value
|
||||||
|
skip, err := buf.ReadByte()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
idx = idx + int(skip)
|
||||||
|
} else { // Take the next b-0x80 bytes literally
|
||||||
|
lr := io.LimitReader(buf, int64(b-0x80))
|
||||||
|
literals, err := ioutil.ReadAll(lr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
copy(out[idx:], literals)
|
||||||
|
idx = idx + len(literals)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Reference in New Issue
Block a user