some redesign
This commit is contained in:
43
pkg/dithering/8x8grid.go
Normal file
43
pkg/dithering/8x8grid.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package dithering
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
)
|
||||||
|
|
||||||
|
type grid struct {
|
||||||
|
color.Gray
|
||||||
|
}
|
||||||
|
|
||||||
|
func (_ grid) ColorModel() color.Model {
|
||||||
|
return color.RGBAModel
|
||||||
|
}
|
||||||
|
|
||||||
|
func (_ grid) Bounds() image.Rectangle {
|
||||||
|
return image.Rect(0, 0, 8, 8)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g grid) At(x, y int) color.Color {
|
||||||
|
step := int(64 / (g.Gray.Y / 4))
|
||||||
|
if (y*8+x)%step == 0 {
|
||||||
|
return color.Black
|
||||||
|
}
|
||||||
|
return color.White
|
||||||
|
}
|
||||||
|
|
||||||
|
type uniformGrid struct {
|
||||||
|
image.Image
|
||||||
|
image.Rectangle
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g uniformGrid) ColorModel() color.Model {
|
||||||
|
return g.Image.ColorModel()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g uniformGrid) Bounds() image.Rectangle {
|
||||||
|
return g.Rectangle
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g uniformGrid) At(x, y int) color.Color {
|
||||||
|
return color.Color(nil)
|
||||||
|
}
|
||||||
35
pkg/dithering/8x8grid_test.go
Normal file
35
pkg/dithering/8x8grid_test.go
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package dithering
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
"image/png"
|
||||||
|
"math/rand"
|
||||||
|
"os/exec"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.nkpl.cc/twocookedfaggots/imageutils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGrid(t *testing.T) {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
|
command := exec.Command("imv", "-")
|
||||||
|
w, err := command.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
img := imageutils.Scale(1<<6, grid{color.Gray{uint8(rand.Int() % 64)}})
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
err = png.Encode(w, img)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
err = command.Start()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
2
scale.go
2
scale.go
@@ -38,7 +38,7 @@ func (r rescaled) At(x, y int) color.Color {
|
|||||||
// Scale scales image.Image to a given scale
|
// Scale scales image.Image to a given scale
|
||||||
// and then returns image.Image.
|
// and then returns image.Image.
|
||||||
// For now it will work only with positive integers.
|
// For now it will work only with positive integers.
|
||||||
func Scale(img image.Image, scale int) image.Image {
|
func Scale(scale int, img image.Image) image.Image {
|
||||||
if scale < 1 {
|
if scale < 1 {
|
||||||
scale = 1
|
scale = 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ func Render(img image.Image, rect image.Rectangle) image.Image {
|
|||||||
func BenchmarkScale(b *testing.B) {
|
func BenchmarkScale(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
err := png.Encode(io.Discard,
|
err := png.Encode(io.Discard,
|
||||||
Scale(Render(
|
Scale(64, Render(
|
||||||
texture.New(color.White,
|
texture.New(color.White,
|
||||||
color.Black, 2),
|
color.Black, 2),
|
||||||
image.Rect(0, 0, 64, 64),
|
image.Rect(0, 0, 64, 64),
|
||||||
), 64),
|
)),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@@ -51,7 +51,7 @@ func (s SinglePixel) Bounds() image.Rectangle {
|
|||||||
func BenchmarkSinglePixel(b *testing.B) {
|
func BenchmarkSinglePixel(b *testing.B) {
|
||||||
instance := SinglePixel{}
|
instance := SinglePixel{}
|
||||||
err := png.Encode(io.Discard,
|
err := png.Encode(io.Discard,
|
||||||
Scale(instance, b.N),
|
Scale(b.N, instance),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user