Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

png2c: rewrite to GoLang for efficiency #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tools/png2c/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
png2c
png2c.exe
3 changes: 3 additions & 0 deletions tools/png2c/MakeFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
TOPDIR := $(realpath ../..)

include $(TOPDIR)/build/go.mk
36 changes: 36 additions & 0 deletions tools/png2c/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"flag"
"image"
"log"
"os"

"../misc"
)

var printHelp bool
var palParams PaletteParams

func init() {
flag.BoolVar(&printHelp, "help", false,
"print help message and exit")
flag.Var(&palParams, "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 (PaletteParams{}) != palParams {
DoPalette(img, palParams)
}
}
35 changes: 35 additions & 0 deletions tools/png2c/palette.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"../misc"
"image"
"image/color"
"log"
)

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
}
42 changes: 42 additions & 0 deletions tools/png2c/params.go
Original file line number Diff line number Diff line change
@@ -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
}