cmd
This commit is contained in:
58
cmd/downscale2x/downscale.go
Normal file
58
cmd/downscale2x/downscale.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
var errOutOfBounds error = errors.New("downscale: out of bounds")
|
||||
|
||||
type Downscaled struct {
|
||||
image.Image
|
||||
Factor int
|
||||
}
|
||||
|
||||
func isOutOfBounds(pt image.Point, img image.Image) bool {
|
||||
x, y := img.Bounds().Dx(), img.Bounds().Dy()
|
||||
if pt.X > x || pt.Y > y {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func meanColor(palette color.Palette) color.Color {
|
||||
var r, g, b int
|
||||
for _, v := range palette {
|
||||
c := color.RGBAModel.Convert(v).(color.RGBA)
|
||||
r += int(c.R)
|
||||
g += int(c.G)
|
||||
b += int(c.B)
|
||||
}
|
||||
r, g, b = r/len(palette), g/len(palette), b/len(palette)
|
||||
return color.RGBA{uint8(r), uint8(g), uint8(b), 0xff}
|
||||
}
|
||||
|
||||
func (d Downscaled) ColorModel() color.Model { return d.Image.ColorModel() }
|
||||
func (d Downscaled) Bounds() image.Rectangle {
|
||||
return image.Rect(0, 0, d.Image.Bounds().Dx()/d.Factor, d.Image.Bounds().Dy()/d.Factor)
|
||||
}
|
||||
func (d Downscaled) At(x, y int) color.Color {
|
||||
palette := color.Palette{}
|
||||
pt := image.Point{x * d.Factor, y * d.Factor}
|
||||
if isOutOfBounds(pt, d.Image) {
|
||||
panic(errOutOfBounds)
|
||||
}
|
||||
for i := 0; i < d.Factor; i++ {
|
||||
for j := 0; j < d.Factor; j++ {
|
||||
currentPt := image.Point{pt.X + i, pt.Y + j}
|
||||
if i > 0 || j > 0 {
|
||||
if isOutOfBounds(currentPt, d.Image) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
palette = append(palette, d.Image.At(currentPt.X, currentPt.Y))
|
||||
}
|
||||
}
|
||||
return palette.Convert(meanColor(palette))
|
||||
}
|
||||
21
cmd/downscale2x/main.go
Normal file
21
cmd/downscale2x/main.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// open image
|
||||
img, err := jpeg.Decode(os.Stdin)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// no pre-processing needed so we just
|
||||
// write image
|
||||
err = png.Encode(os.Stdout, Downscaled{img, 3})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
BIN
cmd/mesh/8.clock.png
Normal file
BIN
cmd/mesh/8.clock.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 MiB |
41
cmd/mesh/9p.go
Normal file
41
cmd/mesh/9p.go
Normal file
@@ -0,0 +1,41 @@
|
||||
//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
func ListenAndServe(addr string, filesystem fs.FS) error {
|
||||
styx.HandlerFunc(func(s *styx.Session) {
|
||||
for s.Next() {
|
||||
switch msg := s.Request.(type) {
|
||||
case styx.Twalk:
|
||||
msg.Rwalk(fs.Stat(filesystem, msg.Path()))
|
||||
case styx.Topen:
|
||||
msg.Ropen(filesystem.Open(msg.Path()))
|
||||
case styx.Tstat:
|
||||
msg.Rstat(fs.Stat(filesystem, msg.Path()))
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
type FS struct {
|
||||
meshFile
|
||||
}
|
||||
|
||||
type meshFile struct {
|
||||
}
|
||||
|
||||
type meshFileInfo struct {
|
||||
bufio.Reader
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (f meshFileInfo) Name() string { return "mesh" }
|
||||
func (f meshFileInfo) Size() int64 { return f.Reader.Size() }
|
||||
func (f meshFileInfo) Mode() fs.FileMode { return fs.ModePerm }
|
||||
func (f meshFileInfo) ModTime() time.Time { return f.Time }
|
||||
func (f meshFileInfo) IsDir() bool { return false }
|
||||
func (f meshFileInfo) Sys() any { return nil }
|
||||
|
||||
func (f meshFile) Stat() (fs.FileInfo, error) {
|
||||
}
|
||||
91
cmd/mesh/mesh.go
Normal file
91
cmd/mesh/mesh.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/png"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/golang/freetype/truetype"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/font/gofont/gomono"
|
||||
"golang.org/x/image/math/fixed"
|
||||
// "github.com/droyo/styx"
|
||||
// "io/fs"
|
||||
)
|
||||
|
||||
type Mesh struct {
|
||||
image.Image
|
||||
X, Y int // divide
|
||||
}
|
||||
|
||||
// using source's color.Model
|
||||
func (mesh Mesh) ColorModel() color.Model { return mesh.Image.ColorModel() }
|
||||
|
||||
// using source's image.Rectangle
|
||||
func (mesh Mesh) Bounds() image.Rectangle { return mesh.Image.Bounds() }
|
||||
func (mesh Mesh) At(x, y int) color.Color {
|
||||
dx, dy := mesh.Image.Bounds().Dx(), mesh.Image.Bounds().Dy()
|
||||
if (x%(dx/mesh.X)) == 0 || (y%(dy/mesh.Y)) == 0 {
|
||||
return color.Black
|
||||
}
|
||||
return mesh.Image.At(x, y)
|
||||
}
|
||||
|
||||
func render(img image.Image) draw.Image {
|
||||
dst := image.NewRGBA(img.Bounds())
|
||||
draw.Draw(dst, dst.Bounds(), img, image.Point{}, draw.Src)
|
||||
return dst
|
||||
}
|
||||
|
||||
func main() {
|
||||
// x := flag.Int("x", 2, "use to grid by x axis")
|
||||
// y := flag.Int("y", 2, "use to grid by y axis")
|
||||
flag.Parse() // now flags is ready to use
|
||||
|
||||
img, err := png.Decode(os.Stdin)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// Mesh{ // our image modifyer
|
||||
// Image: img,
|
||||
// X: *x,
|
||||
// Y: *y,
|
||||
// },
|
||||
|
||||
dst := render(
|
||||
img,
|
||||
)
|
||||
|
||||
f, err := truetype.Parse(gomono.TTF)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
face := truetype.NewFace(f, &truetype.Options{
|
||||
Size: float64(img.Bounds().Dx() / 19),
|
||||
DPI: 72,
|
||||
Hinting: font.HintingNone,
|
||||
})
|
||||
|
||||
d := &font.Drawer{
|
||||
Dst: dst,
|
||||
Src: image.NewUniform(color.Black),
|
||||
Face: face,
|
||||
Dot: fixed.Point26_6{fixed.Int26_6(img.Bounds().Dx() /
|
||||
9*64,
|
||||
),
|
||||
fixed.Int26_6(img.Bounds().Dy() /
|
||||
5*64,
|
||||
),
|
||||
},
|
||||
}
|
||||
d.DrawString(time.Now().Format(time.Kitchen))
|
||||
|
||||
err = png.Encode(os.Stdout, dst)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
67
cmd/mkpalette/palette.go
Normal file
67
cmd/mkpalette/palette.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"maps"
|
||||
"os"
|
||||
"slices"
|
||||
|
||||
imgutil "git.niplace.ru/XoxJlopeZi4BB/imageutils/pkg"
|
||||
)
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Set[S comparable] map[S]struct{}
|
||||
|
||||
func (s Set[S]) Add(v S) {
|
||||
s[v] = struct{}{}
|
||||
}
|
||||
|
||||
func (s Set[S]) Contains(v S) bool {
|
||||
_, ok := s[v]
|
||||
return ok
|
||||
}
|
||||
|
||||
func Map[S1 ~[]T1, S2 ~[]T2, T1, T2 any](s S1, f func(T1) T2) S2 {
|
||||
result := make(S2, len(s))
|
||||
for i := range s {
|
||||
result[i] = f(s[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
type hex string
|
||||
|
||||
func (h hex) String() string {
|
||||
return string(fmt.Sprintf("hex value: %s\n", string(h)))
|
||||
}
|
||||
|
||||
func main() {
|
||||
img, err := png.Decode(os.Stdin)
|
||||
check(err)
|
||||
p := Set[color.Color]{}
|
||||
for y := 0; y < img.Bounds().Dy(); y++ {
|
||||
for x := 0; x < img.Bounds().Dx(); x++ {
|
||||
p.Add(img.At(x, y))
|
||||
}
|
||||
}
|
||||
w := csv.NewWriter(os.Stdout)
|
||||
s := Map[color.Palette, []string](
|
||||
slices.Collect(maps.Keys(p)), imgutil.ColorToHex,
|
||||
)
|
||||
/*
|
||||
h := make([]hex, 0, len(s))
|
||||
for i := range s {
|
||||
h = append(h, hex(s[i]))
|
||||
}
|
||||
*/
|
||||
err = w.Write(s)
|
||||
check(err)
|
||||
}
|
||||
BIN
cmd/negate/8.absolutecinema.png
Normal file
BIN
cmd/negate/8.absolutecinema.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
BIN
cmd/negate/minus19blue
Executable file
BIN
cmd/negate/minus19blue
Executable file
Binary file not shown.
39
cmd/negate/minus19blue.go
Normal file
39
cmd/negate/minus19blue.go
Normal file
@@ -0,0 +1,39 @@
|
||||
//go:build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Reshade struct{ image.Image }
|
||||
|
||||
func (rs Reshade) ColorModel() color.Model { return rs.Image.ColorModel() }
|
||||
func (rs Reshade) Bounds() image.Rectangle { return rs.Image.Bounds() }
|
||||
func (rs Reshade) At(x, y int) color.Color {
|
||||
c := rs.Image.At(x, y)
|
||||
r, g, b, a := c.RGBA()
|
||||
// minus 19 blue
|
||||
return color.RGBA{
|
||||
uint8(r >> 8),
|
||||
uint8(g >> 8),
|
||||
uint8(b>>8) - 8*8,
|
||||
uint8(a >> 8),
|
||||
}
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
img, err := png.Decode(os.Stdin)
|
||||
check(err)
|
||||
err = png.Encode(os.Stdout, Reshade{img})
|
||||
check(err)
|
||||
}
|
||||
70
cmd/negate/mixed.go
Normal file
70
cmd/negate/mixed.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"math/rand"
|
||||
"os"
|
||||
)
|
||||
|
||||
type MixedNegation struct {
|
||||
src image.Image
|
||||
counter int
|
||||
}
|
||||
|
||||
func toRGBA(c color.Color) color.RGBA {
|
||||
r, g, b, a := c.RGBA()
|
||||
return color.RGBA{
|
||||
R: uint8(r >> 8),
|
||||
G: uint8(g >> 8),
|
||||
B: uint8(b >> 8),
|
||||
A: uint8(a >> 8),
|
||||
}
|
||||
}
|
||||
|
||||
func negateRGBA(c color.RGBA) color.RGBA {
|
||||
c.R = 255 - c.R
|
||||
c.G = 255 - c.G
|
||||
c.B = 255 - c.B
|
||||
return c
|
||||
}
|
||||
|
||||
func Negate(c color.Color) color.Color {
|
||||
return negateRGBA(toRGBA(c))
|
||||
}
|
||||
|
||||
func randomBool() bool {
|
||||
return rand.Intn(3) == 0
|
||||
}
|
||||
|
||||
func (mn MixedNegation) ColorModel() color.Model { return mn.src.ColorModel() }
|
||||
func (mn MixedNegation) Bounds() image.Rectangle { return mn.src.Bounds() }
|
||||
func (mn *MixedNegation) At(x, y int) color.Color {
|
||||
if (x%1 != 0) && (y%8 != 0) {
|
||||
if randomBool() {
|
||||
return Negate(mn.src.At(x, y))
|
||||
}
|
||||
|
||||
}
|
||||
return mn.src.At(x, y)
|
||||
}
|
||||
|
||||
func pixelCount(img image.Image) int {
|
||||
return img.Bounds().Dx() * img.Bounds().Dy()
|
||||
}
|
||||
|
||||
func main() {
|
||||
img, err := png.Decode(os.Stdin)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
n := pixelCount(img)
|
||||
println(n)
|
||||
|
||||
err = png.Encode(os.Stdout, &MixedNegation{src: img, counter: n})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
BIN
cmd/negate/negate
Executable file
BIN
cmd/negate/negate
Executable file
Binary file not shown.
44
cmd/negate/negate.go
Normal file
44
cmd/negate/negate.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"os"
|
||||
)
|
||||
|
||||
func NegateRGBA(c color.RGBA) color.RGBA {
|
||||
c.R = 255 - c.R
|
||||
c.G = 255 - c.G
|
||||
c.B = 255 - c.B
|
||||
return c
|
||||
}
|
||||
|
||||
type Negate struct{ image.Image }
|
||||
|
||||
func (n Negate) ColorModel() color.Model { return n.Image.ColorModel() }
|
||||
func (n Negate) Bounds() image.Rectangle { return n.Image.Bounds() }
|
||||
func (n Negate) At(x, y int) color.Color {
|
||||
c := n.Image.At(x, y)
|
||||
r, g, b, a := c.RGBA()
|
||||
RGBAColor := color.RGBA{
|
||||
R: uint8(r >> 8),
|
||||
G: uint8(g >> 8),
|
||||
B: uint8(b >> 8),
|
||||
A: uint8(a >> 8),
|
||||
}
|
||||
return NegateRGBA(RGBAColor)
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
img, err := png.Decode(os.Stdin)
|
||||
check(err)
|
||||
err = png.Encode(os.Stdout, Negate{img})
|
||||
check(err)
|
||||
}
|
||||
BIN
cmd/negate/negated5.png
Normal file
BIN
cmd/negate/negated5.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 977 KiB |
BIN
cmd/negate/negated8.png
Normal file
BIN
cmd/negate/negated8.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
BIN
cmd/negate/reshaded,negated8.png
Normal file
BIN
cmd/negate/reshaded,negated8.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
BIN
cmd/showpalette/.show.go.swp
Normal file
BIN
cmd/showpalette/.show.go.swp
Normal file
Binary file not shown.
74
cmd/showpalette/show.go
Normal file
74
cmd/showpalette/show.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/png"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
imgutil "git.niplace.ru/XoxJlopeZi4BB/imageutils/pkg"
|
||||
)
|
||||
|
||||
func nearestHighSqrt(n int) int {
|
||||
if math.Mod(math.Sqrt(float64(n)), 1) == 0 {
|
||||
return n
|
||||
}
|
||||
return nearestHighSqrt(n + 1)
|
||||
}
|
||||
|
||||
type showColors struct {
|
||||
color.Palette
|
||||
}
|
||||
|
||||
func (s showColors) ColorModel() color.Model {
|
||||
return color.RGBAModel
|
||||
}
|
||||
|
||||
func (s showColors) Bounds() image.Rectangle {
|
||||
n := nearestHighSqrt(len(s.Palette))
|
||||
return image.Rect(0, 0, n, n)
|
||||
}
|
||||
|
||||
func (s showColors) At(x, y int) color.Color {
|
||||
n := nearestHighSqrt(len(s.Palette))
|
||||
return s.Palette[y*n+x]
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Must[T any](v T, err error) T {
|
||||
check(err)
|
||||
return v
|
||||
}
|
||||
|
||||
func Map[S1 ~[]T1, S2 ~[]T2, T1, T2 any](s S1, f func(T1) T2) S2 {
|
||||
result := make(S2, len(s))
|
||||
for i := range s {
|
||||
result[i] = f(s[i])
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := csv.NewReader(os.Stdin)
|
||||
r.TrailingComma = true
|
||||
colors, err := r.Read()
|
||||
check(err)
|
||||
palette := color.Palette(
|
||||
Map[[]string, color.Palette](colors,
|
||||
func(v string) color.Color {
|
||||
println(v + "cat-v")
|
||||
return Must(imgutil.ParseHexColor(v))
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
err = png.Encode(os.Stdout, showColors{palette})
|
||||
check(err)
|
||||
}
|
||||
Reference in New Issue
Block a user