Skip to content

Commit

Permalink
fix bug where palette size must be at least 17 colors and stop app fr…
Browse files Browse the repository at this point in the history
…om closing if file failed to convert (#2)
  • Loading branch information
silbinarywolf authored Oct 12, 2020
1 parent b63d886 commit 1ed25ea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
10 changes: 9 additions & 1 deletion cmd/rm2kfixwatcher/rm2kfixwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func main() {
args := flag.Args()
if len(args) == 0 {
log.Printf(`
rm2kfixwatcher is a tool for auto-fixing PNG files so they work in RPG Maker. It will "watch" an RPG Maker project folder for changes and automatically convert PNG files to an 8-bit PNG (if they do not exceed 255 colors)
rm2kfixwatcher is a tool for auto-fixing PNG files so they work in RPG Maker. It will "watch" an RPG Maker project folder for changes and automatically convert PNG files to an 8-bit PNG (if they do not exceed 256 colors)
How it works
-------------------------
Expand Down Expand Up @@ -216,6 +216,10 @@ This tool exists so that users can work in paint tools they're comfortable in wi
case rm2kpng.ErrRm2kCompatiblePNG:
// if file is already compatible, ignore and move on
continue
case rm2kpng.ErrRm2kPaletteTooBig:
// if palette too big,
log.Printf("Skipping file: %s, error: %s", path, err)
continue
default:
// unhandled error
log.Printf("Skipping file: %s, error: %s", path, err)
Expand Down Expand Up @@ -300,6 +304,10 @@ This tool exists so that users can work in paint tools they're comfortable in wi
case rm2kpng.ErrRm2kCompatiblePNG:
// if file is already compatible, ignore and move on
continue FileUpdateLoop
case rm2kpng.ErrRm2kPaletteTooBig:
// If file is incompatible, skip but give info to user
log.Printf("Failed to convert changed file: %s, error: %s", path, err)
continue FileUpdateLoop
case
// When testing on Windows and saving with MS-Paint
// we get a file in use error for san indeterminate amount of time,
Expand Down
32 changes: 27 additions & 5 deletions rm2kpng.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ func (err ErrRm2kDecode) Error() string {
return err.err.Error()
}

// ErrRm2kPaletteTooBig is an error that occurs if the image given exceeds 256 colors.
// This can occur if the file cannot be converted.
type ErrRm2kPaletteTooBig struct {
paletteLen int
}

// PaletteLen is the size of the palette of the image you tried to convert
func (err ErrRm2kPaletteTooBig) PaletteLen() int {
return err.paletteLen
}

func (err ErrRm2kPaletteTooBig) Error() string {
return fmt.Sprintf("Palette size is %d, which is too big", err.paletteLen)
}

const (
// maxPaletteLen is the expected palette size of RPG Maker assets
maxPaletteLen = 256
Expand Down Expand Up @@ -111,13 +126,20 @@ func getRm2kPaletteList(src image.Image) (color.Palette, error) {
}
}
if len(paletteList) > maxPaletteLen {
return nil, fmt.Errorf("Palette size is %d, which is too big", len(paletteList))
return nil, ErrRm2kPaletteTooBig{
paletteLen: len(paletteList),
}
}

// NOTE(Jae): Most Rm2k assets I've seen have 256 colors in its palette
// regardless of whether they're all used or not but our conversion process
// can go lower and still work, so we leave it for now.
// We may need to pad out the paletteList with 256 colors latter though.
// NOTE(Jae): 2020-10-11
// The Rm2k3 editor will blackout / not be able to interpret a Charset if
// the palette contains less than 17 colors. My first guess was 16 but that still
// didn't work, bumping to 17 worked. My guess is the reasoning is 1 transparent color
// and 16 other colors?
// Anyway, we pad the remaining colors to be black.
for len(paletteList) < 17 {
paletteList = append(paletteList, color.RGBA{R: 0, G: 0, B: 0, A: 0})
}

return paletteList, nil
}
Expand Down

0 comments on commit 1ed25ea

Please sign in to comment.