Skip to content

Commit

Permalink
Add option digit to the picture
Browse files Browse the repository at this point in the history
  • Loading branch information
rfautier committed Jul 11, 2019
1 parent 648d8f4 commit aa62bc7
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 4 deletions.
Binary file added digits/0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added digits/9.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 64 additions & 0 deletions graphic.go
Expand Up @@ -4,9 +4,11 @@ import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
"strconv"
"time"

"github.com/hajimehoshi/ebiten"
Expand Down Expand Up @@ -128,6 +130,65 @@ func (env *Env) addSquare(x float64, y float64, square *ebiten.Image, screen *eb
opts.GeoM.Translate(x, y)
screen.DrawImage(square, opts)

if env.digit {
var err error

if i != 0 {
square, err = ebiten.NewImageFromImage(env.grid.mapping[i].digitImg, ebiten.FilterDefault)
if err != nil {
log.Fatal("Error new images", err)
}
}
opts := &ebiten.DrawImageOptions{}

// Add the Translate effect to the option struct.
opts.GeoM.Translate(x, y)
screen.DrawImage(square, opts)

}
}

func (env *Env) imgDigit(digit int) image.Image {
f, err := os.Open("digits/" + strconv.Itoa(digit) + ".png")
if err != nil {
log.Fatal("Cannot open Digit file", err)
}
// Accept for now only png
img, err := png.Decode(f)
if err != nil {
log.Fatal("Cannot decode image:", err)
}
return img
}

func (env *Env) mergeTwoImages(img1, img2 image.Image) image.Image {
//starting position of the second image (bottom left)
sp2 := image.Point{img1.Bounds().Dx(), 0}
//new rectangle for the second image
r2 := image.Rectangle{sp2, sp2.Add(img2.Bounds().Size())}
//rectangle for the big image
r := image.Rectangle{image.Point{0, 0}, r2.Max}
rgba := image.NewRGBA(r)
draw.Draw(rgba, img1.Bounds(), img1, image.Point{0, 0}, draw.Src)
draw.Draw(rgba, r2, img2, image.Point{0, 0}, draw.Src)
return rgba
}

func (env *Env) getDigit(digit int) image.Image {

var rgba image.Image

if digit/100 > 0 {
rgba = env.mergeTwoImages(env.imgDigit(digit/100), env.imgDigit((digit%100)/10))
rgba = env.mergeTwoImages(rgba, env.imgDigit(digit%10))
} else if digit/10 > 0 {
rgba = env.mergeTwoImages(env.imgDigit(digit/10), env.imgDigit(digit%10))
} else {
rgba = env.imgDigit(digit)
}

newImage := resize.Resize(uint((env.sizeWindows/env.size)/3), uint((env.sizeWindows/env.size)/3), rgba, resize.Lanczos3)
return newImage
}

func (env *Env) cropImage(images string) {
Expand Down Expand Up @@ -169,6 +230,9 @@ func (env *Env) cropImage(images string) {
// Each cell fill with a square of the image
env.grid.mapping[i].cellImg = cImg

if env.digit {
env.grid.mapping[i].digitImg = env.getDigit(i)
}
if countCell+offset == env.size-1 {
countCell = 0
if countSide%2 == 0 {
Expand Down
8 changes: 5 additions & 3 deletions main.go
Expand Up @@ -18,6 +18,7 @@ type Env struct {
sizeWindows int
autoMode bool
heuristic string
digit bool
}

type grid struct {
Expand All @@ -27,9 +28,10 @@ type grid struct {
}

type cell struct {
X int
Y int
cellImg image.Image
X int
Y int
cellImg image.Image
digitImg image.Image
}

func main() {
Expand Down
5 changes: 4 additions & 1 deletion parser.go
Expand Up @@ -56,12 +56,15 @@ func (env *Env) parseArgs() error {
env.mapFile = os.Args[i+1]
} else if arg == "-i" && i+1 < len(os.Args) &&
strings.HasSuffix(os.Args[i+1], ".png") {
fmt.Println("Here")
env.imgFile = os.Args[i+1]
} else if arg == "-d" && i+1 < len(os.Args) {
env.difficulty = os.Args[i+1]
} else if arg == "-a" && i+1 < len(os.Args) {
env.autoMode = true
env.heuristic = os.Args[i+1]
} else if arg == "-dg" {
env.digit = true
}
}
if env.mapFile == "" {
Expand All @@ -79,7 +82,7 @@ func (env *Env) readSize(reader *bufio.Reader) error {
return errors.New("error missing size value")
}
size, err := strconv.Atoi(firstLine)
if err != nil || size < 3 {
if err != nil || size < 3 || size > 31 {
return errors.New("error invalid size value")
}
env.size = size
Expand Down

0 comments on commit aa62bc7

Please sign in to comment.