Moved from gim to gg for image creation
This commit is contained in:
117
glyphbanner.go
117
glyphbanner.go
@@ -1,110 +1,123 @@
|
||||
package nmslib
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image/png"
|
||||
"image"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
gim "github.com/ozankasikci/go-image-merge"
|
||||
"github.com/fogleman/gg"
|
||||
)
|
||||
|
||||
/*CreateBanner - Output PNG of Portal Glyphs
|
||||
Requires 3 vars and returns 0 var, Outputs a PNG file
|
||||
Requires 3 vars and returns 1 err, Outputs a PNG file
|
||||
var portalex string
|
||||
Portal Glyph hex string
|
||||
var savename string
|
||||
Output name of PNG file
|
||||
var opt int
|
||||
00 sets horizontal banner in light mode
|
||||
00 sets horizontal banner in light mode [ 00 || 0 ]
|
||||
01 sets horizontal banner in dark mode [ 01 || 1 ]
|
||||
10 sets vertical banner in light mode
|
||||
01 sets horizontal banner in dark mode
|
||||
11 sets vertical banner in dark mode
|
||||
|
||||
* note first of the 2 bits sets the layout, the second bit sets the glyph color
|
||||
*/
|
||||
func CreateBanner(portalhex string, savename string, opt int) {
|
||||
var err error
|
||||
func CreateBanner(portalhex string, savename string, opt int) (err error) {
|
||||
var GlyphHex [12]int64
|
||||
var glyphImg [12]string
|
||||
var mode string
|
||||
var vert int
|
||||
var horz int
|
||||
var imgArray [12]image.Image
|
||||
|
||||
// Setup temp dir
|
||||
tempdir, err := ioutil.TempDir("", "nmslib-")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return errors.New("can not create temp directory")
|
||||
}
|
||||
// don't forget to clean up afterwords
|
||||
defer os.RemoveAll(tempdir)
|
||||
|
||||
//Set light or dark mode glyphs
|
||||
if opt == 00 || opt == 10 {
|
||||
mode = "light"
|
||||
} else if opt == 01 || opt == 11 {
|
||||
mode = "dark"
|
||||
} else {
|
||||
// if opt is an invalid option (ie. not a 2 bit binary number default to classic mode/layout
|
||||
opt = 0
|
||||
mode = "light"
|
||||
}
|
||||
//defer os.RemoveAll(tempdir)
|
||||
|
||||
// verify len of portalhex
|
||||
if len(portalhex) == 12 {
|
||||
// get hex value from each digit in given string to an array of int64
|
||||
// get hex value from each digit in given string and add it to an array of int64
|
||||
for i := 0; i < len(portalhex); i++ {
|
||||
GlyphHex[i], err = strconv.ParseInt(portalhex[i:int(i+1)], 16, 16)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return errors.New("string provided is not hexadecimal ([0-9][A-F])")
|
||||
}
|
||||
}
|
||||
|
||||
// set options
|
||||
if opt == 00 { // set vertical rendering in light mode
|
||||
vert, horz = 12, 1
|
||||
mode = "light"
|
||||
} else if opt == 01 { // set vertival in dark mode
|
||||
vert, horz = 12, 1
|
||||
mode = "dark"
|
||||
} else if opt == 11 { // set horizontal rendering in dark mode
|
||||
vert, horz = 1, 12
|
||||
mode = "dark"
|
||||
} else { // set horizontal rendering in light mode (classic/default)
|
||||
vert, horz = 1, 12
|
||||
mode = "light"
|
||||
}
|
||||
|
||||
// assign image location of its glyph hex value to an array of strings
|
||||
// assign image location of its glyph-hex value to an array of strings
|
||||
for j := 0; j < len(glyphImg); j++ {
|
||||
glyphImg[j] = fmt.Sprintf("assets/glyphs/%s/PORTALSYMBOL.%X.png", mode, GlyphHex[j])
|
||||
}
|
||||
// pull images need from glyph.go and saved them to ./glyphs/
|
||||
|
||||
// pull images need from assets.go and saved them to the temp directory
|
||||
for k := 0; k < len(glyphImg); k++ {
|
||||
data, err := Asset(glyphImg[k])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return errors.New("can not load images from assets: " + glyphImg[k])
|
||||
}
|
||||
_, err2 := os.Stat(tempdir + "/assets/glyphs/" + mode + "/")
|
||||
if os.IsNotExist(err2) {
|
||||
errDir := os.MkdirAll(tempdir+"/assets/glyphs/"+mode, 0755)
|
||||
if errDir != nil {
|
||||
panic(errDir)
|
||||
return errors.New("can not create temp directory")
|
||||
}
|
||||
}
|
||||
// fmt.Println(tempdir + "/" + glyphImg[k]) // for debugging only
|
||||
err = ioutil.WriteFile(tempdir+"/"+glyphImg[k], []byte(data), 0644)
|
||||
|
||||
if err != nil {
|
||||
return errors.New("can not write file to temp directory")
|
||||
}
|
||||
}
|
||||
|
||||
// load images for processing using github.com/ozankasikci/go-image-merge
|
||||
grids := []*gim.Grid{
|
||||
{ImageFilePath: tempdir + "/" + glyphImg[0]}, {ImageFilePath: tempdir + "/" + glyphImg[1]},
|
||||
{ImageFilePath: tempdir + "/" + glyphImg[2]}, {ImageFilePath: tempdir + "/" + glyphImg[3]},
|
||||
{ImageFilePath: tempdir + "/" + glyphImg[4]}, {ImageFilePath: tempdir + "/" + glyphImg[5]},
|
||||
{ImageFilePath: tempdir + "/" + glyphImg[6]}, {ImageFilePath: tempdir + "/" + glyphImg[7]},
|
||||
{ImageFilePath: tempdir + "/" + glyphImg[8]}, {ImageFilePath: tempdir + "/" + glyphImg[9]},
|
||||
{ImageFilePath: tempdir + "/" + glyphImg[10]}, {ImageFilePath: tempdir + "/" + glyphImg[11]},
|
||||
//Load/open images needed
|
||||
for iter := 0; iter < 12; iter++ {
|
||||
imgArray[iter], err = gg.LoadPNG(tempdir + "/" + glyphImg[iter])
|
||||
if err != nil {
|
||||
return errors.New("can not read glyph " + glyphImg[iter])
|
||||
}
|
||||
}
|
||||
|
||||
// create new image from grids
|
||||
rgba, err := gim.New(grids, vert, horz).Merge()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//begin concatenating images
|
||||
// get image size of first glyph, all glyphs have the same X && Y dimension so we only need to measure one.
|
||||
imgDim := imgArray[0].Bounds().Size()
|
||||
|
||||
// save the output to png
|
||||
file, err := os.Create(savename)
|
||||
err = png.Encode(file, rgba)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
//classic horizontal layout
|
||||
if opt == 01 || opt == 00 {
|
||||
imgWidth := imgDim.X * 12
|
||||
imgHeight := imgDim.Y
|
||||
concat := gg.NewContext(imgWidth, imgHeight)
|
||||
for a := 0; a < 12; a++ {
|
||||
concat.DrawImage(imgArray[a], imgDim.X*a, 0)
|
||||
}
|
||||
concat.SavePNG(savename)
|
||||
}
|
||||
//Vertical layout
|
||||
if opt == 10 || opt == 11 {
|
||||
imgWidth := imgDim.X
|
||||
imgHeight := imgDim.Y * 12
|
||||
concat := gg.NewContext(imgWidth, imgHeight)
|
||||
for a := 0; a < 12; a++ {
|
||||
concat.DrawImage(imgArray[a], 0, imgDim.Y*a)
|
||||
}
|
||||
concat.SavePNG(savename)
|
||||
}
|
||||
} else {
|
||||
return errors.New("portalcode must be exactly 12 chars")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user