Skip to content

Commit

Permalink
Improve nearestNeighbor. 💩
Browse files Browse the repository at this point in the history
  • Loading branch information
msqtt committed May 9, 2023
1 parent e1428fa commit 7c25bcf
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 113 deletions.
32 changes: 24 additions & 8 deletions bobibo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package bobibo

import (
"errors"
"image"
"io"
"runtime"

"github.com/orzation/bobibo/img"
u "github.com/orzation/bobibo/util"
Expand Down Expand Up @@ -55,9 +57,22 @@ func BoBiBo(ima io.Reader, isGif, isInverse bool, opts ...Option) (<-chan Art, e
}
}

var maxCpu = runtime.NumCPU()

// clone ResizeAndGray fn then sort it.
ragFns := func(in <-chan img.Img) <-chan img.Gray {
num := 1
if cap(in) > 1 {
// test
num = maxCpu - 3
}
return u.SortChan(
u.CloneChanFn(img.ResizeAndGray(params.Scale), num, in))
}

mix := u.Multiply(img.ArtotBin(params.Inverse),
u.Multiply(img.BinotImg(params.Threshold),
img.ResizeAndGray(params.Scale),
ragFns,
))

inStream, delays, err := analyzeImage(params)
Expand All @@ -75,14 +90,14 @@ var wrapOut = func(delays []int) func(<-chan []string) <-chan Art {
if delays == nil || len(delays) == 0 {
flag = false
}
return u.GenChanFunc(func(out <-chan []string, wrapOut chan<- Art) {
return u.GenChanFn(func(in <-chan []string, out chan<- Art) {
cnt := 0
for o := range out {
for i := range in {
if flag {
wrapOut <- Art{Content: o, Delay: delays[cnt]}
out <- Art{Content: i, Delay: delays[cnt]}
cnt++
} else {
wrapOut <- Art{Content: o, Delay: 0}
out <- Art{Content: i, Delay: 0}
}
}
})
Expand All @@ -108,11 +123,12 @@ func analyzeImage(params *Params) (<-chan img.Img, []int, error) {
return inChan, delays, nil
}

func newInStream[T img.Img](ims ...T) <-chan img.Img {
func newInStream[T image.Image](ims ...T) <-chan img.Img {
in := make(chan img.Img, len(ims))
defer close(in)
for _, v := range ims {
in <- v
for i, v := range ims {
imgV := img.NewImg(i, v)
in <- imgV
}
return in
}
Binary file removed bobibo.test
Binary file not shown.
4 changes: 2 additions & 2 deletions cli/makefile_cross
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
OS = windows
ARCH = amd64
OS = darwin
ARCH = arm64
EXE = bobibo_$(OS)_$(ARCH)
IS_STATIC = 0
VERSION=V1.2.0
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module github.com/orzation/bobibo

go 1.20
go 1.18

require (
golang.org/x/image v0.6.0
golang.org/x/term v0.5.0
)
require golang.org/x/term v0.5.0

require golang.org/x/sys v0.5.0 // indirect
31 changes: 0 additions & 31 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.6.0 h1:bR8b5okrPI3g/gyZakLZHeWxAR8Dn5CyxXv1hLH5g/4=
golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
67 changes: 39 additions & 28 deletions img/img.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@ import (
_ "image/jpeg"
_ "image/png"
"io"
"math"
"strings"

u "github.com/orzation/bobibo/util"
xdraw "golang.org/x/image/draw"
)

type Img = image.Image
type Pale = *image.Paletted

// use braille chars to draw arts.
var brailleMap = []rune("⠀⢀⡀⣀⠠⢠⡠⣠⠄⢄⡄⣄⠤⢤⡤⣤" +
const braille = "⠀⢀⡀⣀⠠⢠⡠⣠⠄⢄⡄⣄⠤⢤⡤⣤" +
"⠐⢐⡐⣐⠰⢰⡰⣰⠔⢔⡔⣔⠴⢴⡴⣴⠂⢂⡂⣂⠢⢢⡢⣢⠆⢆⡆⣆⠦⢦⡦⣦⠒⢒⡒⣒⠲⢲⡲⣲⠖⢖⡖⣖⠶⢶⡶⣶" +
"⠈⢈⡈⣈⠨⢨⡨⣨⠌⢌⡌⣌⠬⢬⡬⣬⠘⢘⡘⣘⠸⢸⡸⣸⠜⢜⡜⣜⠼⢼⡼⣼⠊⢊⡊⣊⠪⢪⡪⣪⠎⢎⡎⣎⠮⢮⡮⣮⠚⢚⡚⣚⠺⢺⡺⣺⠞⢞⡞⣞⠾⢾⡾⣾" +
"⠁⢁⡁⣁⠡⢡⡡⣡⠅⢅⡅⣅⠥⢥⡥⣥⠑⢑⡑⣑⠱⢱⡱⣱⠕⢕⡕⣕⠵⢵⡵⣵⠃⢃⡃⣃⠣⢣⡣⣣⠇⢇⡇⣇⠧⢧⡧⣧⠓⢓⡓⣓⠳⢳⡳⣳⠗⢗⡗⣗⠷⢷⡷⣷" +
"⠉⢉⡉⣉⠩⢩⡩⣩⠍⢍⡍⣍⠭⢭⡭⣭⠙⢙⡙⣙⠹⢹⡹⣹⠝⢝⡝⣝⠽⢽⡽⣽⠋⢋⡋⣋⠫⢫⡫⣫⠏⢏⡏⣏⠯⢯⡯⣯⠛⢛⡛⣛⠻⢻⡻⣻⠟⢟⡟⣟⠿⢿⡿⣿")
"⠉⢉⡉⣉⠩⢩⡩⣩⠍⢍⡍⣍⠭⢭⡭⣭⠙⢙⡙⣙⠹⢹⡹⣹⠝⢝⡝⣝⠽⢽⡽⣽⠋⢋⡋⣋⠫⢫⡫⣫⠏⢏⡏⣏⠯⢯⡯⣯⠛⢛⡛⣛⠻⢻⡻⣻⠟⢟⡟⣟⠿⢿⡿⣿"

var brailleMap = []rune(braille)

// loading an image, only support png and jpeg.
// if pass a gif, return the first embedded image.
// if there is any thing wrong, panic.
func LoadAImage(f io.Reader) (Img, error) {
func LoadAImage(f io.Reader) (image.Image, error) {
i, _, err := image.Decode(f)
if err != nil {
return nil, err
Expand All @@ -44,61 +41,75 @@ func LoadAGif(f io.Reader) ([]Pale, []int, error) {
return g.Image, g.Delay, nil
}

// resizing the image with scale value, it won't change the ratio.
// resize the image with scale value, using nearestNeighbor.
// return a stream chan function.
var ResizeAndGray = func(scale float64) func(<-chan Img) <-chan *image.Gray {
return u.GenChanFunc(func(in <-chan Img, out chan<- *image.Gray) {
var ResizeAndGray = func(scale float64) func(<-chan Img) <-chan Gray {
return u.GenChanFn(func(in <-chan Img, out chan<- Gray) {
for i := range in {
dx := int(math.Floor(scale * float64(i.Bounds().Dx())))
dy := int(math.Floor(scale * float64(i.Bounds().Dy())))
dst := image.NewGray(image.Rect(0, 0, dx, dy))
xdraw.NearestNeighbor.Scale(dst, dst.Rect, i, i.Bounds(), xdraw.Over, nil)
out <- dst
out <- grayNearestNeighbor(scale, i)
}
})
}

func grayNearestNeighbor(scale float64, src Img) Gray {
DY, DX := src.value.Bounds().Dy(), src.value.Bounds().Dx()
dy := int(scale * float64(DY))
dx := int(scale * float64(DX))
tgt := make([][]uint8, dy)

for i := 0; i < dy; i++ {
tgt[i] = make([]uint8, dx)
for j := 0; j < dx; j++ {
x, y := int((float64(j) / scale)), int((float64(i) / scale))
r, g, b, _ := src.value.At(x, y).RGBA()
grayColor := (299*r + 587*g + 114*b) / 1000
tgt[i][j] = uint8(grayColor >> 8)
}
}
return Gray{id: src.id, value: tgt}
}

// turning image to 2d binary matrix.
// use threshold to adjust the binarization.
var BinotImg = func(threshold int) func(<-chan *image.Gray) <-chan [][]bool {
return u.GenChanFunc(func(in <-chan *image.Gray, out chan<- [][]bool) {
var BinotImg = func(threshold int) func(<-chan Gray) <-chan [][]bool {
return u.GenChanFn(func(in <-chan Gray, out chan<- [][]bool) {
for im := range in {
out <- img2bin(im, &threshold)
}
})
}

func img2bin(im *image.Gray, th *int) [][]bool {
func img2bin(im Gray, th *int) [][]bool {
if *th < 0 || *th > 255 {
*th = int(otsu(im))
}
dx, dy := im.Bounds().Dx(), im.Bounds().Dy()
dy, dx := im.size()
reB := make([][]bool, dy)
for i := range reB {
reB[i] = make([]bool, dx)
for j := range reB[i] {
r, _, _, _ := im.At(j, i).RGBA()
reB[i][j] = uint8(r>>8) >= uint8(*th)
grayValue := im.value[i][j]
reB[i][j] = grayValue >= uint8(*th)
}
}
return reB
}

// return the best threshold to binarize.
func otsu(im *image.Gray) uint8 {
func otsu(im Gray) uint8 {
var threshold int = 0
const grayScale = 256
var u float32
var w0, u0 float32

dx, dy := im.Bounds().Dx(), im.Bounds().Dy()
dy, dx := im.size()
hist := make([]float32, grayScale)
sumPixel := dx * dy
sumPixel := dy * dx

for i := 0; i < dy; i++ {
for j := 0; j < dx; j++ {
r, _, _, _ := im.At(j, i).RGBA()
hist[uint8(r>>8)]++
grayValue := im.value[i][j]
hist[grayValue]++
}
}

Expand Down Expand Up @@ -127,7 +138,7 @@ func otsu(im *image.Gray) uint8 {
// turning 2d binary matrix to string array.
// whether reverse color.
var ArtotBin = func(w bool) func(<-chan [][]bool) <-chan []string {
return u.GenChanFunc(func(in <-chan [][]bool, out chan<- []string) {
return u.GenChanFn(func(in <-chan [][]bool, out chan<- []string) {
for e := range in {
out <- bin2art(e, w)
}
Expand Down
29 changes: 29 additions & 0 deletions img/img_h.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package img

import (
"image"
)

type Pale = *image.Paletted

type Img struct {
id int
value image.Image
}

func NewImg(id int, value image.Image) Img {
return Img{id: id, value: value}
}

type Gray struct {
id int
value [][]uint8
}

func (g Gray) Id() int {
return g.id
}

func (g Gray) size() (int, int) {
return len(g.value), len(g.value[0])
}
35 changes: 0 additions & 35 deletions img/img_test.go

This file was deleted.

11 changes: 11 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: build block cpu mem

test:
go test -bench=. -cpu=4 -blockprofile=block.pprof -cpuprofile=cpu.pprof -memprofile=mem.pprof
block: block.pprof
go tool pprof -http=:9999 block.pprof
cpu: cpu.pprof
go tool pprof -http=:9999 cpu.pprof
mem: mem.pprof
go tool pprof -http=:9999 mem.pprof

Loading

0 comments on commit 7c25bcf

Please sign in to comment.