package main import ( "fmt" "math" "image" "image/jpeg" "github.com/antha-lang/antha/antha/anthalib/num" "os" "io" "time" "strconv" "syscall/js" ) //**********************// // Structs // //**********************// type Coord struct { X float64 Y float64 } const PINS = 300 const MIN_DISTANCE = 30 const MAX_LINES = 4000 const LINE_WEIGHT = 8 var IMG_SIZE = 500 var IMG_SIZE_FL = float64(500) var IMG_SIZE_SQ = 250000 var Pin_coords = []Coord{} var SourceImage = []float64{} var Line_cache_y = [][]float64{} var Line_cache_x = [][]float64{} //**********************// // Main // //**********************// func init(){ image.RegisterFormat("jpeg", "jpeg", jpeg.Decode, jpeg.DecodeConfig) } func main() { SourceImage = importPictureAndGetPixelArray() fmt.Println("Hello, world.") startTime := time.Now() calculatePinCoords() precalculateAllPotentialLines() calculateLines() endTime := time.Now() diff := endTime.Sub(startTime) fmt.Println(" precalculateAllPotentialLines Taken, " + strconv.FormatFloat(diff.Seconds(), 'f', 6, 64)) fmt.Println("End") } func generateStringArt() func importPictureAndGetPixelArray() []float64 { imgfile, _ := os.Open("./ae300.jpg") defer imgfile.Close() pixels, _ := getPixels(imgfile) return pixels } func getPixels(file io.Reader) ([]float64, error) { img, _, err := image.Decode(file) if err != nil { return nil, err } bounds := img.Bounds() width, height := bounds.Max.X, bounds.Max.Y IMG_SIZE = width IMG_SIZE_FL = float64(IMG_SIZE) IMG_SIZE_SQ = IMG_SIZE * IMG_SIZE var pixels []float64 for y := 0; y < height; y++ { for x := 0; x < width; x++ { pixels = append(pixels, rgbaToPixel(img.At(x, y).RGBA())) } } return pixels, nil } func rgbaToPixel(r uint32, g uint32, b uint32, a uint32) float64 { return float64(r / 257) } func calculatePinCoords() { pin_coords := [PINS]Coord{} center := float64(IMG_SIZE / 2) radius := float64(IMG_SIZE/2 - 1) for i:=0;i max_err){ max_err = line_err best_pin = test_pin index = inner_index } } } line_sequence = append(line_sequence, best_pin) coords1:=Line_cache_y[index] coords2:=Line_cache_x[index] for i := range coords1 { v := int((coords1[i] * IMG_SIZE_FL) + coords2[i]) error[v] = error[v] - LINE_WEIGHT } last_pins = append(last_pins, best_pin) last_pins = last_pins[1:] current_pin = best_pin } fmt.Println(line_sequence) } func getLineErr(err, coords1, coords2 []float64) float64 { sum := float64(0) for i:=0;i