-
-
Save Shinpeim/2983299 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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