Browse Source

did a vhs

*put a donk on it*

Signed-off-by: gryffyn <me@neveris.one>
main
gryffyn 4 months ago
parent
commit
4635494b09
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 2
      go.mod
  2. 37
      main.go
  3. 81
      models/glitch/glitch.go
  4. 44
      models/pixelSort/pixelSort.go
  5. 5
      utils/utils.go

2
go.mod

@ -3,6 +3,6 @@ module git.neveris.one/gryffyn/snowcrash
go 1.15
require (
github.com/AlessandroPomponio/hsv v1.1.0
github.com/davecgh/go-spew v1.1.1
gocv.io/x/gocv v0.26.0
)

37
main.go

@ -1,44 +1,47 @@
package main
import (
"fmt"
"image"
"image/png"
"log"
"os"
"strconv"
"git.neveris.one/gryffyn/snowcrash/models/glitch"
"git.neveris.one/gryffyn/snowcrash/models/pixelSort"
"git.neveris.one/gryffyn/snowcrash/utils"
"github.com/davecgh/go-spew/spew"
)
func main() {
arg := os.Args[1:]
if len(arg) < 4 {
log.Fatal("Usage: snowcrash <png file> <int x> <int y>")
}
_, _ = strconv.Atoi(arg[1])
_, _ = strconv.Atoi(arg[2])
pngName := "test/image0"
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
img := new(utils.Image)
img.Open(arg[0])
img.Open(pngName + ".png")
err := img.GetPixelsRGB()
sorted := pixelSort.NewSorted(img)
fmt.Println("Image dimensions: ")
spew.Dump(img.Bounds)
// pixelSort.SortRowsQuick(img, pixelSort.Value)
img.Write(arg[3], sorted)
// sort pixels
_ = pixelSort.NewSorted(img)
// diag stuff
/* fmt.Print("PixelRGBA at " + strconv.Itoa(x) + "," + strconv.Itoa(y) + " is: ")
fmt.Print(img.PixelsRGBA[x][y])
fmt.Println("\nHex: #" + utils.RgbaToHex(img.PixelsRGBA[x][y], false))
fmt.Print("PixelHSV at " + strconv.Itoa(x) + "," + strconv.Itoa(y) + " is: ")
fmt.Print(img.Pixels.HSV[y][x]) */
// test shifting pixels
s := glitch.RandScaledShift(img.Bounds)
log.Println(s)
shifted := glitch.ShiftChannel(img.Image, 0, 3, 72)
// shifted2 := glitch.ShiftChannel(shifted, 3, 1, s.X)
// write final image
img.Write(pngName+"_shifted.png", shifted) // shifted
// img.Write(pngName+ "_out_sorted.png", sorted) // sorted
log.Println("Written file")
if err != nil {
log.Fatal("Error: Image could not be decoded")
}

81
models/glitch/glitch.go

@ -0,0 +1,81 @@
package glitch
import (
"image"
"image/color"
"math/rand"
"time"
_ "git.neveris.one/gryffyn/snowcrash/utils"
)
type Shift struct {
X int
Y int
}
type RGBA struct {
R uint8
G uint8
B uint8
A uint8
}
func colToRGBA(c color.Color) RGBA {
r, g, b, a := c.RGBA()
return RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)}
}
// Shifts image direction by amount.
// Up:0 Down:1 Left:2 Right:3 / R:0 G:1 B:2
func ShiftChannel(i image.Image, chann uint8, direction uint8, shift int) image.Image {
b := i.Bounds()
nir := image.NewRGBA(b)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
pixcol := i.At(x, y)
col := colToRGBA(pixcol)
if chann == 0 {
if direction == 0 {
col.R = colToRGBA(i.At(x, y-shift).(color.Color)).R
} else if direction == 1 {
col.R = colToRGBA(i.At(x, y+shift).(color.Color)).R
} else if direction == 2 {
col.R = colToRGBA(i.At(x-shift, y).(color.Color)).R
} else if direction == 3 {
col.R = colToRGBA(i.At(x+shift, y).(color.Color)).R
}
} else if chann == 1 {
if direction == 0 {
col.G = colToRGBA(i.At(x, y-shift).(color.Color)).G
} else if direction == 1 {
col.G = colToRGBA(i.At(x, y+shift).(color.Color)).G
} else if direction == 2 {
col.G = colToRGBA(i.At(x-shift, y).(color.Color)).G
} else if direction == 3 {
col.G = colToRGBA(i.At(x+shift, y).(color.Color)).G
}
} else if chann == 2 {
if direction == 0 {
col.B = colToRGBA(i.At(x, y-shift).(color.Color)).B
} else if direction == 1 {
col.B = colToRGBA(i.At(x, y+shift).(color.Color)).B
} else if direction == 2 {
col.B = colToRGBA(i.At(x-shift, y).(color.Color)).B
} else if direction == 3 {
col.B = colToRGBA(i.At(x+shift, y).(color.Color)).B
}
}
nir.SetRGBA(x, y, color.RGBA(col))
}
}
return nir
}
func RandScaledShift(r image.Rectangle) Shift {
rand.Seed(time.Now().UnixNano())
return Shift{
X: rand.Intn((r.Max.X/20)-1) + 1,
Y: rand.Intn((r.Max.Y/20)-1) + 1,
}
}

44
models/pixelSort/pixelSort.go

@ -26,11 +26,11 @@ def threshold(image, lower_threshold, upper_threshold, **kwargs):
*/
// Function is from above python code.
func threshold(i utils.Image, lower, upper uint32) [][]int {
func thresholdGrid(i utils.Image, lower, upper uint32) [][]int {
var intervals [][]int
for y := 0; y < i.Bounds.Max.Y; y++ {
intervals = append(intervals, []int{})
for x := 0; x < i.Bounds.Max.X; y++ {
for x := 0; x < i.Bounds.Max.X; x++ {
val := b(i.Pixels[x][y])
if val < lower || val > upper {
intervals[y] = append(intervals[y], x)
@ -40,6 +40,17 @@ func threshold(i utils.Image, lower, upper uint32) [][]int {
return intervals
}
func thresholdRow(pa []utils.PixelRGBA, d image.Rectangle, lower, upper uint32) []int {
var intervals []int
for x := 0; x < d.Max.X; x++ {
val := b(pa[x])
if val < lower || val > upper {
intervals = append(intervals, x)
}
}
return intervals
}
func QSort(arr []utils.PixelRGBA) []utils.PixelRGBA {
newArr := make([]utils.PixelRGBA, len(arr))
@ -78,6 +89,35 @@ func qsort(arr []utils.PixelRGBA, start, end int) {
qsort(arr, splitIndex+1, end)
}
func qsort_thresh(arr []utils.PixelRGBA, start, end int, lower, upper uint32) {
if (end - start) < 1 {
return
}
pivot := arr[end]
splitIndex := start
for i := start; i < end; i++ {
if b(arr[i]) < lower || b(arr[i]) > upper {
}
if b(arr[i]) < b(pivot) {
temp := arr[splitIndex]
arr[splitIndex] = arr[i]
arr[i] = temp
splitIndex++
}
}
arr[end] = arr[splitIndex]
arr[splitIndex] = pivot
qsort(arr, start, splitIndex-1)
qsort(arr, splitIndex+1, end)
}
func SortRowsQuick(i *utils.Image) [][]utils.PixelRGBA {
//spew.Dump(i.Pixels.HSV)
var sorted [][]utils.PixelRGBA

5
utils/utils.go

@ -20,6 +20,10 @@ func (p PixelRGBA) RGBA() (r, g, b, a uint32) {
return p.R, p.G, p.B, p.A
}
func NewRBGA(r, g, b, a uint32) PixelRGBA {
return PixelRGBA{R: r, G: g, B: b, A: a}
}
type Image struct {
File io.Reader
Image image.Image
@ -56,6 +60,7 @@ func (i *Image) GetPixelsRGB() error {
}
func (i *Image) Open(path string) {
log.Println("Opening file " + path)
file, err := os.Open(path)
if err != nil {
log.Fatalln("Error: File could not be opened")

Loading…
Cancel
Save