better glitching algorithm
This commit is contained in:
parent
24b11a4033
commit
9871e574d5
28
glitch.go
28
glitch.go
@ -7,8 +7,16 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func fillSliceWithASCII(slice []byte) {
|
||||||
|
const asciiMin = 16
|
||||||
|
const asciiMax = 150
|
||||||
|
|
||||||
|
for i := range slice {
|
||||||
|
slice[i] = byte(rand.Int32N(asciiMax-asciiMin+1) + asciiMin)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func glitchImage(inputFile string, outputFile string, shitSize int64, iterations int64) error {
|
func glitchImage(inputFile string, outputFile string, shitSize int64, iterations int64) error {
|
||||||
// Prepend the "output" directory to the output file
|
|
||||||
outputFilePath := filepath.Join("output", outputFile)
|
outputFilePath := filepath.Join("output", outputFile)
|
||||||
|
|
||||||
// Open the source file for reading
|
// Open the source file for reading
|
||||||
@ -31,6 +39,10 @@ func glitchImage(inputFile string, outputFile string, shitSize int64, iterations
|
|||||||
|
|
||||||
// Get the size of the destination file
|
// Get the size of the destination file
|
||||||
fileSize := fileInfo.Size()
|
fileSize := fileInfo.Size()
|
||||||
|
if fileSize == 0 {
|
||||||
|
fmt.Println("Cannot glitch file with size of zero bytes!")
|
||||||
|
return fmt.Errorf("input file is zero bytes")
|
||||||
|
}
|
||||||
|
|
||||||
// Generate a random offset within the file
|
// Generate a random offset within the file
|
||||||
var i int64
|
var i int64
|
||||||
@ -43,6 +55,7 @@ func glitchImage(inputFile string, outputFile string, shitSize int64, iterations
|
|||||||
|
|
||||||
// Generate random data and write it in
|
// Generate random data and write it in
|
||||||
shit := make([]byte, shitSize)
|
shit := make([]byte, shitSize)
|
||||||
|
fillSliceWithASCII(shit)
|
||||||
_, err = destination.Write(shit)
|
_, err = destination.Write(shit)
|
||||||
check(err, "failed to write shit data: %w")
|
check(err, "failed to write shit data: %w")
|
||||||
|
|
||||||
@ -68,21 +81,28 @@ func copyImage(inputFile string, outputFile string) error {
|
|||||||
_, err = destination.ReadFrom(source)
|
_, err = destination.ReadFrom(source)
|
||||||
check(err, "failed to copy file contents: %w")
|
check(err, "failed to copy file contents: %w")
|
||||||
|
|
||||||
|
// no errors
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateGlitchedSequence(inputFile string, fileExt string, totalImages int, shitSize int64, iterations int64, chanceToRandom int32) {
|
func generateGlitchedSequence(inputFile string, fileExt string, totalImages int, shitSize int64, iterations int64, chanceToRandom int32) error {
|
||||||
for i := 1; i <= totalImages; i++ {
|
for i := 1; i <= totalImages; i++ {
|
||||||
outputFileName := fmt.Sprintf("img_glitched_%d.%s", i, fileExt)
|
outputFileName := fmt.Sprintf("img_glitched_%d.%s", i, fileExt)
|
||||||
|
|
||||||
if rand.Int32N(100) < chanceToRandom {
|
if rand.Int32N(100) < chanceToRandom {
|
||||||
// Glitched image
|
// Glitched image
|
||||||
err := glitchImage(inputFile, outputFileName, shitSize, iterations)
|
err := glitchImage(inputFile, outputFileName, shitSize, iterations)
|
||||||
check(err, "failed to glitch image: %w")
|
if err != nil {
|
||||||
|
return fmt.Errorf("Cannot glitch image")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Normal image
|
// Normal image
|
||||||
err := copyImage(inputFile, outputFileName)
|
err := copyImage(inputFile, outputFileName)
|
||||||
check(err, "failed to copy normal image: %w")
|
if err != nil {
|
||||||
|
return fmt.Errorf("Cannot glitch image")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
// no errors
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user