to save up a progress
This commit is contained in:
@@ -7,10 +7,14 @@ import (
|
||||
|
||||
const side = 16
|
||||
|
||||
type Options struct {
|
||||
Factor float64
|
||||
}
|
||||
|
||||
// tile
|
||||
type grid struct {
|
||||
color.Gray
|
||||
Factor float64
|
||||
Options
|
||||
}
|
||||
|
||||
func (_ grid) ColorModel() color.Model {
|
||||
@@ -22,7 +26,7 @@ func (_ grid) Bounds() image.Rectangle {
|
||||
}
|
||||
|
||||
func (g grid) At(x, y int) color.Color {
|
||||
n := uint8(float64(g.Gray.Y) * g.Factor)
|
||||
n := uint8(float64(g.Gray.Y) * g.Options.Factor)
|
||||
|
||||
if n == 0 {
|
||||
return color.Black
|
||||
@@ -54,13 +58,13 @@ func (g uniformGrid) At(x, y int) color.Color {
|
||||
|
||||
type Dithering struct {
|
||||
image.Image
|
||||
Factor float64
|
||||
Options
|
||||
}
|
||||
|
||||
func (d Dithering) ColorModel() color.Model { return color.GrayModel }
|
||||
func (d Dithering) Bounds() image.Rectangle { return d.Image.Bounds() }
|
||||
func (d Dithering) At(x, y int) color.Color {
|
||||
c := color.GrayModel.Convert(d.Image.At(x, y))
|
||||
return uniformGrid{grid{c.(color.Gray), d.Factor}}.
|
||||
return uniformGrid{grid{c.(color.Gray), d.Options.Factor}}.
|
||||
At(x, y)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
"reflect"
|
||||
|
||||
"git.nkpl.cc/twocookedfaggots/imageutils"
|
||||
)
|
||||
@@ -17,6 +18,8 @@ func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
const factor = 0.75
|
||||
|
||||
func callImage(img image.Image) {
|
||||
for y := 0; y < img.Bounds().Dy(); y++ {
|
||||
for x := 0; x < img.Bounds().Dx(); x++ {
|
||||
@@ -38,14 +41,14 @@ func TestGrid(t *testing.T) {
|
||||
|
||||
n := uint8(rand.Int() % 256)
|
||||
t.Log(n)
|
||||
img := imageutils.Scale(1<<6, grid{color.Gray{n}})
|
||||
img := imageutils.Scale(grid{color.Gray{n}, factor}, 1<<6)
|
||||
err = png.Encode(w, img)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
callImage(grid{color.Gray{4}})
|
||||
callImage(grid{color.Gray{0}})
|
||||
callImage(grid{color.Gray{4}, factor})
|
||||
callImage(grid{color.Gray{0}, factor})
|
||||
}
|
||||
|
||||
func TestDithering(t *testing.T) {
|
||||
@@ -66,8 +69,48 @@ func TestDithering(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = png.Encode(w, Dithering{img})
|
||||
err = png.Encode(w, Dithering{img, factor})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRangeStruct(t *testing.T){
|
||||
s := Dithering{image.Rectangle{},0}
|
||||
value := reflect.ValueOf(s)
|
||||
if value.Kind() == reflect.Ptr {
|
||||
value = value.Elem()
|
||||
}
|
||||
if value.Kind() != reflect.Struct {
|
||||
t.Log("Provided value is not a struct")
|
||||
}
|
||||
tp := value.Type()
|
||||
for i := 0; i < value.NumField(); i++ {
|
||||
fieldName := tp.Field(i).Name
|
||||
fieldValue := value.Field(i)
|
||||
|
||||
if !fieldValue.CanInterface() {
|
||||
t.Logf("Field %s is unexported\n", fieldName)
|
||||
continue
|
||||
}
|
||||
if fieldValue.Kind() == reflect.Ptr {
|
||||
fieldValue = fieldValue.Elem()
|
||||
}
|
||||
/*
|
||||
if fieldValue.Kind() == reflect.Interface {
|
||||
fieldValue = fieldValue.Elem()
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
actualValue := fieldValue.Interface()
|
||||
switch v := actualValue.(type) {
|
||||
case float64:
|
||||
t.Logf("Field '%s' is a float64.\n", fieldName)
|
||||
case image.Image:
|
||||
t.Logf("Field '%s' is a image.Image. Value: %q\n", fieldName, v)
|
||||
default:
|
||||
t.Logf("Field '%s' has an unhandled type: %T\n", fieldName, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
//go:build ignore
|
||||
package dithering
|
||||
|
||||
func valueOfType(a any) reflect.Value {
|
||||
return reflect.TypeOf(a).Elem()
|
||||
}
|
||||
|
||||
func ServeHTTP(w http.ResponseWriter, req* http.Request) {
|
||||
instance := new(Dithering{})
|
||||
value := reflect.ValueOf(instance).Elem()
|
||||
imageType := valueOfType(new(image.Image))
|
||||
floatType := valueOfType
|
||||
|
||||
for i := 0; i < value.NumField(); i++ {
|
||||
v := value.Field(i)
|
||||
switch v.Type() {
|
||||
case imageType
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 249 KiB After Width: | Height: | Size: 264 KiB |
Reference in New Issue
Block a user