Skip to content
Snippets Groups Projects
Commit 7b9d3fed authored by Ophestra's avatar Ophestra
Browse files

first working thingy

parents
No related branches found
No related tags found
No related merge requests found
/ImageModulator
/output
/output.png
/.idea
package main
import (
"image"
"os"
)
var inFile *os.File
var inImage image.Image
var format string
var outSlice []byte
func de() {
inFile, err = os.Open(inFilePath)
if err != nil {
println("Error while opening input,", err)
os.Exit(1)
}
inImage, format, err = image.Decode(inFile)
if err != nil {
println("Error while decoding input,", err)
os.Exit(1)
}
if format != "png" {
println("Expected png, got", format)
os.Exit(1)
}
for y := 0; y < inImage.Bounds().Dy(); y++ {
for x := 0; x < inImage.Bounds().Dx(); x++ {
r, g, b, a := inImage.At(x, y).RGBA()
if r == g && g == b && b == 0 {
outSlice = append(outSlice, byte(a))
}
}
}
println("Output", len(outSlice), "bytes")
err = os.WriteFile("output", outSlice, 0644)
if err != nil {
println("Error while writing output,", err)
os.Exit(1)
}
}
go.mod 0 → 100644
main.go 0 → 100644
package main
import (
"flag"
"image"
"image/color"
"image/png"
"io"
"math"
"os"
)
var err error
var (
inFilePath string
demodulate bool
)
var (
in []byte
out io.Writer
)
func init() {
flag.BoolVar(&demodulate, "d", false, "De-modulate data from a modulated image.")
}
func main() {
flag.Parse()
if flag.NArg() != 1 {
println("Expected argument: infile.")
os.Exit(1)
}
inFilePath = flag.Arg(0)
if demodulate {
de()
os.Exit(0)
}
in, err = os.ReadFile(inFilePath)
if err != nil {
println("Error while reading input,", err)
os.Exit(1)
}
l := int(math.Ceil(math.Sqrt(float64(len(in)))))
h := int(math.Ceil(float64(len(in)) / float64(l)))
println("Image length", l, "height", h)
img := image.NewRGBA64(image.Rect(0, 0, l, h))
c := 0
for y := 0; y < h; y++ {
for x := 0; x < l; x++ {
if c >= len(in) {
img.Set(x, y, color.RGBA{
R: 255,
G: 255,
B: 255,
A: 255,
})
continue
}
img.Set(x, y, color.RGBA{
R: 0,
G: 0,
B: 0,
A: in[c],
})
c++
}
}
out, err = os.OpenFile("output.png", os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
println("Error while opening output,", err)
os.Exit(1)
}
err = png.Encode(out, img)
if err != nil {
println("Error while encoding output,", err)
os.Exit(1)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment