diff --git a/glyphbanner.go b/glyphbanner.go index 5328cd1..0eaa540 100644 --- a/glyphbanner.go +++ b/glyphbanner.go @@ -23,19 +23,40 @@ var opt int * note first of the 2 bits sets the layout, the second bit sets the glyph color */ func CreateBanner(portalhex string, savename string, opt int) (err error) { + out, err := GenRawBanner(portalhex, opt) + if err != nil { + return + } + gg.SavePNG(savename+".png", out) + //gg.SaveJPG(savename+".jpg", out, 50) + return +} + +/*GenRawBanner - Returns image.Image of genorated image +Requires 2 vars and returns image.Image (raw image data) and an error +var portalex string: Portal Glyph hex string +var opt int + 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 + 11 sets vertical banner in dark mode + + * note first of the 2 bits sets the layout, the second bit sets the glyph color +*/ +func GenRawBanner(portalhex string, layout int) (portalbanner image.Image, err error) { var GlyphHex [12]int64 var glyphImg [12]string var mode string var imgArray [12]image.Image //Set light or dark mode glyphs - if opt == 00 || opt == 10 { + if layout == 00 || layout == 10 { mode = "light" - } else if opt == 01 || opt == 11 { + } else if layout == 01 || layout == 11 { mode = "dark" } else { - // if opt is an invalid option (ie. not a 2 bit binary number default to classic mode/layout - opt = 0 + // if layout is an invalid option (ie. not a 2 bit binary number default to classic mode/layout + layout = 0 mode = "light" } @@ -45,7 +66,7 @@ func CreateBanner(portalhex string, savename string, opt int) (err error) { for i := 0; i < len(portalhex); i++ { GlyphHex[i], err = strconv.ParseInt(portalhex[i:int(i+1)], 16, 16) if err != nil { - return errors.New("string provided is not hexadecimal ([0-9][A-F])") + return nil, errors.New("string provided is not hexadecimal ([0-9][A-F])") } } @@ -58,11 +79,11 @@ func CreateBanner(portalhex string, savename string, opt int) (err error) { for k := 0; k < len(glyphImg); k++ { data, err := Asset(glyphImg[k]) if err != nil { - return errors.New("can not load images from assets: " + glyphImg[k]) + return nil, errors.New("can not load images from assets: " + glyphImg[k]) } err = ioutil.WriteFile(NmsTemp+"/"+glyphImg[k], []byte(data), 0644) if err != nil { - return errors.New("can not write file to temp directory") + return nil, errors.New("can not write file to temp directory") } } @@ -70,7 +91,7 @@ func CreateBanner(portalhex string, savename string, opt int) (err error) { for iter := 0; iter < 12; iter++ { imgArray[iter], err = gg.LoadPNG(NmsTemp + "/" + glyphImg[iter]) if err != nil { - return errors.New("can not read glyph " + glyphImg[iter]) + return nil, errors.New("can not read glyph " + glyphImg[iter]) } } @@ -78,28 +99,30 @@ func CreateBanner(portalhex string, savename string, opt int) (err error) { // 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() + //Set Layout //classic horizontal layout - if opt == 01 || opt == 00 { + if layout == 01 || layout == 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) + return concat.Image(), nil } + //Vertical layout - if opt == 10 || opt == 11 { + if layout == 10 || layout == 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) + return concat.Image(), nil } } else { - return errors.New("portalcode must be exactly 12 chars") + return nil, errors.New("portalcode must be exactly 12 chars") } return } diff --git a/go.mod b/go.mod index 75409c4..b29febb 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module gitlab.com/Raum0x2A/nmslib go 1.16 require ( - github.com/fogleman/gg v1.3.0 + github.com/fogleman/gg v1.3.1-0.20210131172831-af4cd580789b github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect golang.org/x/image v0.0.0-20210504121937-7319ad40d33e // indirect ) diff --git a/go.sum b/go.sum index bfa0ddc..3971499 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/fogleman/gg v1.3.0 h1:/7zJX8F6AaYQc57WQCyN9cAIz+4bCJGO9B+dyW29am8= -github.com/fogleman/gg v1.3.0/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= +github.com/fogleman/gg v1.3.1-0.20210131172831-af4cd580789b h1:gqOBIAmkc/ZxXzFrM4wTub7tD0xYaOsaOQ5wOA74lJQ= +github.com/fogleman/gg v1.3.1-0.20210131172831-af4cd580789b/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= golang.org/x/image v0.0.0-20210504121937-7319ad40d33e h1:PzJMNfFQx+QO9hrC1GwZ4BoPGeNGhfeQEgcQFArEjPk= diff --git a/nmslib-glyphbanner_test.go b/nmslib-glyphbanner_test.go index 64d713e..f372f10 100644 --- a/nmslib-glyphbanner_test.go +++ b/nmslib-glyphbanner_test.go @@ -10,7 +10,7 @@ func TestCreateBanner(t *testing.T) { rp := "21F2F8EDB94D" //rp := RndPortal() - fileout := "./test.png" + fileout := "./test" err := CreateBanner(rp, fileout, 0) if err != nil {