Skip to content

Commit

Permalink
Merge pull request #2 from Ziemas/main
Browse files Browse the repository at this point in the history
Fix decoding and pl2-to-png
  • Loading branch information
essial authored Aug 29, 2021
2 parents e456c73 + c541f30 commit 466620a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
23 changes: 15 additions & 8 deletions cmd/pl2-to-png/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"image/png"
"io/ioutil"
"os"
"path"

"github.com/OpenDiablo2/pl2/pkg"
)
Expand All @@ -18,7 +19,7 @@ type options struct {
}

func parseOptions(o *options) (terminate bool) {
o.pl2 = flag.String("pl2", "", "input pkg file (required)")
o.pl2 = flag.String("pl2", "", "input pl2 file (required)")
o.pngPath = flag.String("png", "", "path to output png file (optional)")

flag.Parse()
Expand All @@ -43,16 +44,22 @@ func main() {

pl2, err := pkg.FromBytes(data)
if err != nil {
fmt.Print(fmt.Errorf("Error decoding pl2: %w\n", err))
return
}

if *o.pngPath != "" {
img := makeImage(pl2)
pngPath := *o.pngPath

err = writeImage(*o.pngPath, img)
if err != nil {
fmt.Errorf("problem writing image, %w", err)
}
if pngPath == "" {
pngPath, _ = os.Getwd()
pngPath = path.Join(pngPath, "output.png")
}

img := makeImage(pl2)

err = writeImage(pngPath, img)
if err != nil {
fmt.Print(fmt.Errorf("problem writing image, %w", err))
}
}

Expand Down Expand Up @@ -188,4 +195,4 @@ func writeImage(outPath string, img image.Image) error {
}

return nil
}
}
7 changes: 3 additions & 4 deletions pkg/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ func (pl2 *PL2) Decode(rs io.ReadSeeker) (*PL2, error) {
return pl2, nil
}

func (pl2 *PL2) decodeColors(stream *bitstream.Reader, dst color.Palette) error {
func (pl2 *PL2) decodeColors(stream *bitstream.Reader, dst color.Palette, colorBytes int) error {
const (
colorBytes = 4
rOff, gOff, bOff = 0, 1, 2 // rgb offsets in the returned bytes
)

Expand All @@ -55,13 +54,13 @@ func (pl2 *PL2) decodeColors(stream *bitstream.Reader, dst color.Palette) error
func (pl2 *PL2) decodeBasePalette(stream *bitstream.Reader) error {
pl2.BasePalette = make(color.Palette, numPaletteColors)

return pl2.decodeColors(stream, pl2.BasePalette)
return pl2.decodeColors(stream, pl2.BasePalette, 4)
}

func (pl2 *PL2) decodeTextColors(stream *bitstream.Reader) error {
pl2.TextColors = make(color.Palette, numTextColors)

return pl2.decodeColors(stream, pl2.TextColors)
return pl2.decodeColors(stream, pl2.TextColors, 3)
}

func (pl2 *PL2) decodeTransforms(stream *bitstream.Reader) (err error) {
Expand Down

0 comments on commit 466620a

Please sign in to comment.