Files
imageutils/cmd/negate/mixed.go
2025-12-14 06:04:57 +03:00

71 lines
1.2 KiB
Go

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)
}
}