2018-11-17 22:01:06 +01:00
|
|
|
#!perl --
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use feature qw( say );
|
|
|
|
use File::Find;
|
|
|
|
use File::Path qw(make_path remove_tree);
|
|
|
|
use GD;
|
|
|
|
|
2018-12-10 04:46:38 +01:00
|
|
|
my $dry_run = grep{ $_ eq '--dry-run'} @ARGV;
|
|
|
|
|
2018-11-17 22:01:06 +01:00
|
|
|
my $resdir = 'app/src/main/res';
|
|
|
|
my $dstdir = 'tmp';
|
2019-01-15 14:43:56 +01:00
|
|
|
my $trimdir = 'trim24';
|
|
|
|
|
2018-11-17 22:01:06 +01:00
|
|
|
my $tmpfile = 'tmp/tmp.png';
|
|
|
|
|
2019-01-15 14:43:56 +01:00
|
|
|
my %density = (
|
|
|
|
mdpi => 1,
|
|
|
|
hdpi => 1.5,
|
|
|
|
xhdpi=> 2,
|
|
|
|
xxhdpi => 3,
|
|
|
|
xxxhdpi => 4,
|
|
|
|
);
|
|
|
|
|
|
|
|
sub savePng($$){
|
|
|
|
my($image,$path)=@_;
|
|
|
|
|
|
|
|
my $dir = $path;
|
|
|
|
$dir =~ s|/[^/]+\z||;
|
|
|
|
make_path($dir);
|
|
|
|
|
|
|
|
$image->saveAlpha(1);
|
|
|
|
open(my $fh,">",$path) or die "$path $!";
|
|
|
|
binmode $fh;
|
|
|
|
print $fh $image->png;
|
|
|
|
close($fh) or die "$path $!";
|
|
|
|
}
|
|
|
|
|
2018-11-17 22:01:06 +01:00
|
|
|
sub fix($){
|
|
|
|
my($relpath) = @_;
|
|
|
|
|
|
|
|
my $dstfile = "$dstdir/$relpath";
|
|
|
|
return if not $dstfile =~ s/(png|webp)\z/png/i;
|
|
|
|
my $ext = lc $1;
|
|
|
|
|
2018-11-20 10:32:30 +01:00
|
|
|
my $inData;
|
2018-11-17 22:01:06 +01:00
|
|
|
if( $ext eq "webp" ){
|
2018-11-20 10:32:30 +01:00
|
|
|
open(my $fh,"dwebp $relpath -quiet -o -|") or die "dwebp : $!";
|
|
|
|
binmode($fh);
|
|
|
|
local $/ = undef;
|
|
|
|
$inData = <$fh>;
|
|
|
|
close($fh) or die "dwebp: $!";
|
|
|
|
}else{
|
|
|
|
open(my $fh,"<",$relpath) or die "$relpath : $!";
|
|
|
|
binmode($fh);
|
|
|
|
local $/ = undef;
|
|
|
|
$inData = <$fh>;
|
|
|
|
close($fh) or die "$relpath : $!";
|
2018-11-17 22:01:06 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 10:32:30 +01:00
|
|
|
my $image = GD::Image->newFromPngData($inData,1);
|
2018-11-17 22:01:06 +01:00
|
|
|
|
|
|
|
my $width = $image->width;
|
|
|
|
my $height = $image->height;
|
|
|
|
my $alphaMax = 0;
|
2018-11-18 03:38:52 +01:00
|
|
|
my $isColor = 0 ;
|
|
|
|
Loop: for( my $y = 0; $y < $height ;++$y){
|
2018-11-17 22:01:06 +01:00
|
|
|
for( my $x = 0; $x < $width ;++$x){
|
|
|
|
my $c = $image->getPixel($x,$y);
|
|
|
|
my $c0 = ($c >> 24)&255; # GDのalphaは0が不透明、127が完全透明
|
2018-11-18 03:38:52 +01:00
|
|
|
my $r = ($c >> 16)&255;
|
|
|
|
my $g = ($c >> 8)&255;
|
|
|
|
my $b = ($c >> 0)&255;
|
|
|
|
if( abs($r-$g) >= 2 or abs($r-$b) >= 2 or abs($g-$b) >= 2 ){
|
|
|
|
$isColor = 1;
|
|
|
|
last Loop;
|
|
|
|
}
|
2018-11-17 22:01:06 +01:00
|
|
|
my $alpha= 127-$c0;
|
|
|
|
if( $alpha > $alphaMax){
|
|
|
|
$alphaMax = $alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 14:43:56 +01:00
|
|
|
|
|
|
|
$relpath=~ /drawable-(\w+)/;
|
|
|
|
my $density = $density{ $1 };
|
|
|
|
|
2019-01-16 13:33:07 +01:00
|
|
|
if( $density and !$isColor){
|
|
|
|
my $dp_w = int(0.5 + $width/$density);
|
|
|
|
my $dp_h = int(0.5 + $height/$density);
|
|
|
|
if( $dp_w == 32 && $dp_h == 32 && 0 ){
|
|
|
|
say "${dp_w}x${dp_h}dp, alphaMax=$alphaMax, isColor=${isColor}, $relpath";
|
|
|
|
|
|
|
|
my $dstfile = "$trimdir/$relpath";
|
|
|
|
if( $dstfile =~ s/(png|webp)\z/png/i ){
|
|
|
|
if(0){
|
|
|
|
# create trimmed
|
|
|
|
my $trim_w = int(0.5 + $width * 24/32);
|
|
|
|
my $trim_h = int(0.5 + $height * 24/32);
|
|
|
|
my $offsetX = int($width-$trim_w)/2;
|
|
|
|
my $offsetY = int($width-$trim_w)/2;
|
|
|
|
|
|
|
|
my $i2 = GD::Image->newTrueColor($trim_w,$trim_h);
|
|
|
|
$i2->alphaBlending(0);
|
|
|
|
$i2->filledRectangle(0,0,$trim_w,$trim_h, $image->colorAllocateAlpha(0,0,0,127));
|
|
|
|
$i2->alphaBlending(1);
|
|
|
|
$i2->copy($image,0,0,$offsetX,$offsetY,$trim_w,$trim_h);
|
|
|
|
|
|
|
|
savePng($i2,$dstfile);
|
|
|
|
}else{
|
|
|
|
savePng($image,$dstfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-15 14:43:56 +01:00
|
|
|
|
2018-11-18 03:38:52 +01:00
|
|
|
return if $isColor;
|
|
|
|
return if $alphaMax == 0;
|
2018-11-20 10:32:30 +01:00
|
|
|
return if $alphaMax >= 126;
|
2018-11-17 22:01:06 +01:00
|
|
|
|
2019-01-15 14:43:56 +01:00
|
|
|
say "${width}x${height}, alphaMax=$alphaMax, isColor=${isColor}, $relpath";
|
2018-11-17 22:01:06 +01:00
|
|
|
|
2018-11-20 10:32:30 +01:00
|
|
|
my $scale = 127.0/$alphaMax;
|
2018-11-18 03:38:52 +01:00
|
|
|
|
2018-11-20 10:32:30 +01:00
|
|
|
for( my $y = 0; $y < $height ;++$y){
|
|
|
|
for( my $x = 0; $x < $width ;++$x){
|
|
|
|
my $c = $image->getPixel($x,$y);
|
|
|
|
my $c0 = ($c >> 24)&255; # GDのalphaは0が不透明、127が完全透明
|
|
|
|
my $r = ($c >> 16)&255;
|
|
|
|
my $g = ($c >> 8)&255;
|
|
|
|
my $b = ($c >> 0)&255;
|
|
|
|
next if $c0 == 127;
|
|
|
|
my $alpha= 127-$c0;
|
|
|
|
$c0 = 127 - int( 0.5 + $alpha * $scale );
|
|
|
|
my($rgb) = ($r+$g+$b >= 555 ? 0xffffff : 0x000000 );
|
|
|
|
$c = $rgb | ($c0 << 24 );
|
|
|
|
$image->setPixel($x,$y,$c);
|
2018-11-17 22:01:06 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-20 10:32:30 +01:00
|
|
|
|
2019-01-15 14:43:56 +01:00
|
|
|
savePng($image,$dstfile);
|
2018-12-10 04:46:38 +01:00
|
|
|
return if $dry_run;
|
|
|
|
|
2018-11-18 03:38:52 +01:00
|
|
|
if(1){
|
|
|
|
my $relpath_webp = $relpath;
|
|
|
|
$relpath_webp =~ s/(png|webp)\z/webp/i;
|
|
|
|
if( "png" eq lc $1 ){
|
|
|
|
unlink $relpath;
|
|
|
|
}
|
|
|
|
system qq(cwebp $dstfile -quiet -o $relpath_webp);
|
2018-11-17 22:01:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
my @files;
|
|
|
|
find(
|
|
|
|
{
|
|
|
|
no_chdir => 1
|
|
|
|
,wanted =>sub{
|
|
|
|
my $relpath = $File::Find::name;
|
|
|
|
return if -d $relpath;
|
|
|
|
return if not $relpath =~ m|/drawable-|;
|
2018-11-18 22:15:07 +01:00
|
|
|
return if $relpath =~ m%/(media_type_|emoji_|media_bg_dark)%;
|
2018-11-17 22:01:06 +01:00
|
|
|
push @files,$relpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
,$resdir
|
|
|
|
);
|
|
|
|
|
|
|
|
for my $relpath(@files){
|
|
|
|
fix($relpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
|