to save up a progress

This commit is contained in:
2026-09-19 23:21:09 +03:00
parent 78cdfa3beb
commit e51ffde305
16 changed files with 267 additions and 13 deletions
+25
View File
@@ -0,0 +1,25 @@
package main
type FileWithOptions struct {
Options map[string]string
io.Reader
}
func mapToStruct[T any](m map[string]string, a T) T {
t := reflect.TypeOf(a)
for _, v := range reflect.VisibleFields(t) {
v.
}
}
func Handle(img image.Image) http.Handler {
func(w http.ResponseWriter,
r *http.Request) {
err := png.Encode(w, img)
if err != nil {
log.Fatal(err)
w.WriteHeader(500)
return
}
}
}
+2
View File
@@ -0,0 +1,2 @@
input: image with modifier options
output: png image
+3
View File
@@ -0,0 +1,3 @@
package main
}
+91
View File
@@ -0,0 +1,91 @@
package main
func parseMap(fields []string) (map[string]string, error) {
m := map[string]string{}
var k, v string
for i, v := range fields {
if v == "=" {
return nil, errors.New("bad field")
}
if len(v) > 1 {
// check if "=" is the last or first character
if "=" == v[0] {
return nil, errors.New("no key")
}
if "=" == v[len(v)-1] {
return nil, errors.New("no value")
}
}
if strings.Contains(v, "=") {
splitted := strings.Split(v, "=")
k, v = splitted[0], splitted[1]
m[k] = v
if i == len(fields)-1 {
return m, nil
}
continue
}
return nil, errors.New("no equality sign in field")
}
return nil, errors.New("everything is wrong!")
}
type Map map[color.Color]color.Color
type repalette struct {
image.Image
Map
}
func (r repalette) ColorModel() color.Model {
return r.Image.ColorModel()
}
func (r repalette) Bounds() image.Rectangle {
return r.Image.Bounds()
}
func (r repalette) At(x, y int) color.Color {
v := r.Image.At(x, y)
newColor, ok := r.Map[v]
if !ok {
return v
}
return newColor
}
func main() {
fields := os.Args[1:]
m, err := parseMap(fields)
if err != nil {
panic("parseMap:", err)
}
errParseHexColor := errors.New("parse hex color:")
newPalette, err :=
func(m map[string]string) (newMap map[color.Color]color.Color, err error) {
for k, v := range m {
newKey, err := util.ParseHexColor(k)
if err != nil {
return nil, errors.Join(
errors.New("bad key hex value."),
errors.Join(errParseHexColor, err),
)
}
newValue, err := util.ParseHexColor(v)
if err != nil {
return nil, errors.Join(
errors.New("bad value hex value."),
errors.Join(errParseHexColor, err),
)
}
newMap[newKey] = newValue
}
return
}(fields)
if err != nil {
panic(errors.Join(errors.New("palette parsing:"), err))
}
err = util.ProcessStdio(func(img image.Image) image.Image { return repalette{img, newPalette} })
if err != nil {
panic(errors.Join(errors.New("process stdio:"), err))
}
}