diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b03c20 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.gif +*.jpeg +*.png +.DS_Store diff --git a/glitch.go b/glitch.go new file mode 100644 index 0000000..1cafb29 --- /dev/null +++ b/glitch.go @@ -0,0 +1,60 @@ +package main + +import ( + "math/rand/v2" + "os" + "path/filepath" +) + +func makeOutputDir() { + // Default to just "output" + err := os.Mkdir("output", 0755) + check(err, "cannot make directory") +} + +func glitchImage(inputFile string, outputFile string, shitSize int64, iterations int64) error { + // Ensure the output directory exists + makeOutputDir() + + // Prepend the "output" directory to the output file + outputFilePath := filepath.Join("output", outputFile) + + // Open the source file for reading + source, err := os.Open(inputFile) + check(err, "failed to open source file: %w") + defer source.Close() + + // Create a new file for writing the glitched copy + destination, err := os.Create(outputFilePath) + check(err, "failed to create output file: %w") + defer destination.Close() + + // Copy the contents of the source file to the destination + _, err = destination.ReadFrom(source) + check(err, "failed to copy file contents: %w") + + // Get the Stat() of the destination file + fileInfo, err := destination.Stat() + check(err, "failed to get file info: %w") + + // Get the size of the destination file + fileSize := fileInfo.Size() + + // Generate a random offset within the file + var i int64 + for i = 0; i < iterations; i++ { + offset := rand.Int64N(fileSize) + + // Seek to the random offset in the destination file + _, err = destination.Seek(offset, 0) + check(err, "failed to seek to random offset: %w") + + // Generate random data and write it in + shit := make([]byte, shitSize) + _, err = destination.Write(shit) + check(err, "failed to write shit data: %w") + + // fmt.Printf("Successfully wrote %d bytes of shit at offset %d in file %s\n", shitSize, offset, outputFilePath) + } + return nil +} diff --git a/glitch_img b/glitch_img new file mode 100755 index 0000000..7735a77 Binary files /dev/null and b/glitch_img differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f6740bb --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module glitch_img + +go 1.22.3 diff --git a/main.go b/main.go new file mode 100644 index 0000000..0962869 --- /dev/null +++ b/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "strconv" +) + +func main() { + if len(os.Args) < 5 { + fmt.Println("Usage: go run main.go ") + return + } + + sourceFile := os.Args[1] + numCopies, err := strconv.Atoi(os.Args[2]) + if err != nil || numCopies <= 0 { + fmt.Println("Invalid number of copies. Must be a positive integer.") + return + } + + shitSize, err := strconv.ParseInt(os.Args[3], 10, 64) // Parse shitSize as int64 + if err != nil || shitSize <= 0 { + fmt.Println("Invalid shit size. Must be a positive integer.") + return + } + + iterations, err := strconv.ParseInt(os.Args[4], 10, 8) + + for i := 1; i <= numCopies; i++ { + outputFile := fmt.Sprintf("%s_glitched_%d%s", + stripExtension(sourceFile), + i, + filepath.Ext(sourceFile), + ) + + err := glitchImage(sourceFile, outputFile, shitSize, iterations) + if err != nil { + fmt.Printf("Error glitching file %d: %v\n", i, err) + } + } + + fmt.Println("GENREATING GIF YEAHH ") + // now run ffmpeg to make a gif! + cmd := exec.Command("ffmpeg", "-y", "-framerate 60", "-i", "./output/img_glitched_%d.jpeg output.gif") + err = cmd.Run() + check(err, "cannot run ffmpeg command!") + // +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..895ceba --- /dev/null +++ b/util.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "path/filepath" +) + +func check(e error, s string) { + if e != nil { + fmt.Errorf(s) + } +} + +func stripExtension(filename string) string { + ext := filepath.Ext(filename) + return filename[:len(filename)-len(ext)] +}