Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Forked from kkosuge/glitch.pl
Created June 24, 2012 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shinpeim/2983299 to your computer and use it in GitHub Desktop.
Save Shinpeim/2983299 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use Compress::Raw::Zlib;
my $file_name = shift;
my $opt_name = "glitched_".$file_name;
open (FH, $file_name) or die "$!";
binmode FH;
my $hoge = *FH;
my $len = (-s $file_name);
my $buf;
read($hoge, $buf, $len);
close FH;
sub jpeglitch{
my $jpg = shift;
$jpg =~ s/1/2/g;
return $jpg
}
sub pnglitch{
my $png = shift;
my @data = split(/IDAT/,$png);
my ($crc,$data,$head,@idat,$tail,$raw,$cmp);
my $size = 0;
foreach my $d (@data){
if ($size > 0) {
push(@idat,substr($d,0,$size));
}
if ($size == 0){
$head = substr($d,0,length($d) - 4);
}
$tail = substr($d,($size + 4),(-$size + 4));
$size = unpack('Na',substr($d,length($d) - 4,4));
}
my $x = new Compress::Raw::Zlib::Inflate();
$x->inflate(join("",@idat), $raw);
$raw =~ s/a/b/g;
my $d = new Compress::Raw::Zlib::Deflate();
$d->deflate($raw, $cmp);
$size = pack('N',[length($cmp)]);
$data = $size.'IDAT'.$cmp;
$crc = pack('N',[crc32($cmp,crc32('IDAT'))]);
my $glt = $head.$data.$crc.$tail;
return $glt
}
open(OUTFILE, '>'.$opt_name);
binmode(OUTFILE);
if ($file_name =~ /\.jpe?g$/){
$buf = &jpeglitch($buf);
}elsif($file_name =~ /\.png$/){
$buf = &pnglitch($buf);
}
print OUTFILE $buf;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment