From 1ee564a4723b9c1e382ff397950667cb08d8ba08 Mon Sep 17 00:00:00 2001 From: Krzysztof Jaroma Date: Fri, 22 Jan 2021 22:40:00 +0100 Subject: [PATCH 1/3] Golang version of png2c, palette export. --- tools/png2c/MakeFile | 3 ++ tools/png2c/main.go | 44 +++++++++++++++++++++++++++++ tools/png2c/palette.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 tools/png2c/MakeFile create mode 100644 tools/png2c/main.go create mode 100644 tools/png2c/palette.go diff --git a/tools/png2c/MakeFile b/tools/png2c/MakeFile new file mode 100644 index 00000000..fd307179 --- /dev/null +++ b/tools/png2c/MakeFile @@ -0,0 +1,3 @@ +TOPDIR := $(realpath ../..) + +include $(TOPDIR)/build/go.mk \ No newline at end of file diff --git a/tools/png2c/main.go b/tools/png2c/main.go new file mode 100644 index 00000000..6176df5e --- /dev/null +++ b/tools/png2c/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "flag" + "image" + "log" + "os" + + "../misc" +) + +var printHelp bool +var paletteFlag string + +func init() { + flag.BoolVar(&printHelp, "help", false, + "print help message and exit") + flag.StringVar(&paletteFlag, "palette", "", + "Output Amiga palette 'name,color'") +} + +func main() { + flag.Parse() + + if len(flag.Args()) < 1 || printHelp { + flag.PrintDefaults() + os.Exit(1) + } + + img, ok := misc.LoadPNG(flag.Arg(0)).(*image.Paletted) + if !ok { + log.Fatal("Only 8-bit images with palette supported.") + } + + if len(paletteFlag) != 0 { + params, err := ParseDoPaletteParams(paletteFlag) + if err != nil { + flag.PrintDefaults() + os.Exit(1) + } + DoPalette(img, params) + } + +} diff --git a/tools/png2c/palette.go b/tools/png2c/palette.go new file mode 100644 index 00000000..5697d6c5 --- /dev/null +++ b/tools/png2c/palette.go @@ -0,0 +1,64 @@ +package main + +import ( + "../misc" + "image" + "image/color" + "log" + "strconv" + "strings" +) + +type PaletteParams struct { + Name string + Colors int + StoreUnused bool +} + +func ParseDoPaletteParams(paletteFlag string) (PaletteParams, error) { + params := strings.Split(paletteFlag, ",") + if len(params) < 2 { + return PaletteParams{}, nil + } + colors, err := strconv.Atoi(params[1]) + if err != nil { + return PaletteParams{}, err + } + storeUnused := false + if len(params) == 3 { + storeUnused, err = strconv.ParseBool(params[2]) + return PaletteParams{}, err + } + return PaletteParams{ + Name: params[0], + Colors: colors, + StoreUnused: storeUnused, + }, nil +} + +func DoPalette(img *image.Paletted, params PaletteParams) { + palette := misc.Palette{} + colorsCount := len(img.Palette) + + for _, col := range img.Palette { + r, g, b, a := col.RGBA() + palette = append(palette, color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)}) + } + + if colorsCount == 0 { + log.Fatal("Image has no palette!") + } + if params.Colors > colorsCount { + log.Fatalf("Image has %d colors, expected at most %d!", len(palette), params.Colors) + } + if params.StoreUnused { + if params.Colors < colorsCount { + params.Colors = colorsCount + } + } + err := palette.Export(params.Name) + if err != nil { + log.Fatal("Could not export palette to C source.") + } + return +} From c89487e98b29db718e637db3f19c4c4194fb5797 Mon Sep 17 00:00:00 2001 From: Krzysztof Jaroma Date: Sun, 14 Feb 2021 00:00:56 +0100 Subject: [PATCH 2/3] Define custom flag to store do-palette parameters --- tools/png2c/main.go | 16 ++++------------ tools/png2c/palette.go | 29 ----------------------------- tools/png2c/params.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 tools/png2c/params.go diff --git a/tools/png2c/main.go b/tools/png2c/main.go index 6176df5e..4645fcab 100644 --- a/tools/png2c/main.go +++ b/tools/png2c/main.go @@ -10,13 +10,12 @@ import ( ) var printHelp bool -var paletteFlag string +var palParams PaletteParams func init() { flag.BoolVar(&printHelp, "help", false, "print help message and exit") - flag.StringVar(&paletteFlag, "palette", "", - "Output Amiga palette 'name,color'") + flag.Var(&palParams, "palette", "Output Amiga palette 'name,color'") } func main() { @@ -31,14 +30,7 @@ func main() { if !ok { log.Fatal("Only 8-bit images with palette supported.") } - - if len(paletteFlag) != 0 { - params, err := ParseDoPaletteParams(paletteFlag) - if err != nil { - flag.PrintDefaults() - os.Exit(1) - } - DoPalette(img, params) + if (PaletteParams{}) != palParams { + DoPalette(img, palParams) } - } diff --git a/tools/png2c/palette.go b/tools/png2c/palette.go index 5697d6c5..69283354 100644 --- a/tools/png2c/palette.go +++ b/tools/png2c/palette.go @@ -5,37 +5,8 @@ import ( "image" "image/color" "log" - "strconv" - "strings" ) -type PaletteParams struct { - Name string - Colors int - StoreUnused bool -} - -func ParseDoPaletteParams(paletteFlag string) (PaletteParams, error) { - params := strings.Split(paletteFlag, ",") - if len(params) < 2 { - return PaletteParams{}, nil - } - colors, err := strconv.Atoi(params[1]) - if err != nil { - return PaletteParams{}, err - } - storeUnused := false - if len(params) == 3 { - storeUnused, err = strconv.ParseBool(params[2]) - return PaletteParams{}, err - } - return PaletteParams{ - Name: params[0], - Colors: colors, - StoreUnused: storeUnused, - }, nil -} - func DoPalette(img *image.Paletted, params PaletteParams) { palette := misc.Palette{} colorsCount := len(img.Palette) diff --git a/tools/png2c/params.go b/tools/png2c/params.go new file mode 100644 index 00000000..98ea12b9 --- /dev/null +++ b/tools/png2c/params.go @@ -0,0 +1,42 @@ +package main + +import ( + "errors" + "fmt" + "strconv" + "strings" +) + +type PaletteParams struct { + Name string + Colors int + StoreUnused bool +} + +func (pp *PaletteParams) String() string { + return fmt.Sprintf("%s,%d,%t", pp.Name, pp.Colors, pp.StoreUnused) +} + +func (pp *PaletteParams) Set(value string) error { + params := strings.Split(value, ",") + if len(params) < 2 { + return errors.New("not enough comma separated parameters") + } + colors, err := strconv.Atoi(params[1]) + if err != nil { + return errors.New("could not parse colors parameter to integer") + } + storeUnused := false + if len(params) == 3 { + storeUnused, err = strconv.ParseBool(params[2]) + if err != nil { + return errors.New("could not parse store-unused param to boolean") + } + } + *pp = PaletteParams{ + Name: params[0], + Colors: colors, + StoreUnused: storeUnused, + } + return nil +} From dcfdfa77e6eb694cb3431ca354f25dc60b175d99 Mon Sep 17 00:00:00 2001 From: Krzysztof Jaroma Date: Sun, 14 Feb 2021 00:01:23 +0100 Subject: [PATCH 3/3] Add .gitignore --- tools/png2c/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 tools/png2c/.gitignore diff --git a/tools/png2c/.gitignore b/tools/png2c/.gitignore new file mode 100644 index 00000000..9c6f5c73 --- /dev/null +++ b/tools/png2c/.gitignore @@ -0,0 +1,2 @@ +png2c +png2c.exe \ No newline at end of file