Initial commit

This commit is contained in:
Ian Gulliver
2022-08-15 20:23:55 -07:00
parent 0a155a1e77
commit 36ff9c2e36
57 changed files with 157 additions and 0 deletions

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/firestuff/goplate
go 1.18

154
main.go Normal file
View File

@@ -0,0 +1,154 @@
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"math"
"net/http"
"os"
_ "image/jpeg"
)
func max16(a, b uint16) uint16 {
if a > b {
return a
} else {
return b
}
}
func max32(a, b uint32) uint32 {
if a > b {
return a
} else {
return b
}
}
func maxDiff(a, d1, d2 uint32) uint32 {
var d uint32
if d1 > d2 {
d = d1 - d2
} else {
d = d2 - d1
}
return max32(a, d)
}
func detectEdges(src image.Image) *image.Gray16 {
bounds := src.Bounds()
ret := image.NewGray16(bounds)
for y := bounds.Min.Y + 1; y < bounds.Max.Y; y++ {
for x := bounds.Min.X + 1; x < bounds.Max.X; x++ {
inr, ing, inb, _ := src.At(x, y).RGBA()
tempr, tempg, tempb, _ := src.At(x - 1, y - 1).RGBA()
var c uint32
c = maxDiff(c, inr, tempr)
c = maxDiff(c, ing, tempg)
c = maxDiff(c, inb, tempb)
outc := color.Gray16{Y: uint16(c)}
ret.Set(x, y, outc)
}
}
return ret
}
func loadImage(path string) (image.Image, error) {
fh, err := os.Open(path)
if err != nil {
return nil, err
}
defer fh.Close()
img, _, err := image.Decode(fh)
if err != nil {
return nil, err
}
return img, nil
}
func normalize(src *image.Gray16) *image.Gray16 {
bounds := src.Bounds()
var m uint16
for y := bounds.Min.Y + 1; y < bounds.Max.Y; y++ {
for x := bounds.Min.X + 1; x < bounds.Max.X; x++ {
m = max16(m, src.Gray16At(x, y).Y)
}
}
scale := math.MaxUint16 / m
ret := image.NewGray16(bounds)
for y := bounds.Min.Y + 1; y < bounds.Max.Y; y++ {
for x := bounds.Min.X + 1; x < bounds.Max.X; x++ {
c := src.Gray16At(x, y)
c.Y *= scale
ret.Set(x, y, c)
}
}
return ret
}
func find(shape, img *image.Gray16) {
sb := shape.Bounds()
imgb := img.Bounds()
for y := imgb.Min.Y; y < imgb.Max.Y - (sb.Max.Y - sb.Min.Y); y++ {
for x := imgb.Min.X; x < imgb.Max.X - (sb.Max.X - sb.Min.X); x++ {
score(shape, img, x, y)
}
}
}
func score(shape, img *image.Gray16, ox, oy int) {
sb := shape.Bounds()
imgb := img.Bounds()
for y := sb.Min.Y; y < sb.Max.Y; y++ {
for x := sb.Min.X; x < sb.Max.X; x++ {
// imgy := oy + y
//
}
}
}
func test(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "image/png")
t, err := loadImage("shapes/T.png")
if err != nil {
panic(err)
}
te := detectEdges(t)
src, err := loadImage("samples/big.png")
if err != nil {
panic(err)
}
srce := detectEdges(src)
find(te, srce)
err = png.Encode(w, srce)
if err != nil {
panic(err)
}
}
func main() {
fmt.Printf("starting...\n")
http.HandleFunc("/test", test)
http.ListenAndServe(":8090", nil)
}

BIN
samples/Alabama.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Alaska.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Arizona.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
samples/Arkansas.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
samples/California.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Colorado.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
samples/Connecticut.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.7 KiB

BIN
samples/Delaware.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Florida.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Georgia.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Guam.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
samples/Hawaii.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Idaho.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
samples/Illinois.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Indiana.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
samples/Iowa.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
samples/Kansas.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Kentucky.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Louisiana.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Maine.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
samples/Maryland.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Massachusetts.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Michigan.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
samples/Minnesota.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
samples/Mississippi.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Missouri.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
samples/Montana.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Nebraska.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Nevada.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
samples/NewHampshire.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
samples/NewJersey.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
samples/NewMexico.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/NewYork.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/NorthCarolina.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
samples/NorthDakota.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
samples/Ohio.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
samples/Oklahoma.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
samples/Oregon.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
samples/Pennsylvania.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/PuertoRico.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/RhodeIsland.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
samples/SouthCarolina.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/SouthDakota.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
samples/Tennessee.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
samples/Texas.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Utah.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
samples/Vermont.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Virginia.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

BIN
samples/Washington.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/WestVirginia.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
samples/Wisconsin.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
samples/Wyoming.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
samples/big.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

BIN
shapes/T.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 947 B