-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
fixIconAlpha.pl
174 lines (146 loc) · 3.95 KB
/
fixIconAlpha.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!perl --
use strict;
use warnings;
use feature qw( say );
use File::Find;
use File::Path qw(make_path remove_tree);
use GD;
my $dry_run = grep{ $_ eq '--dry-run'} @ARGV;
my $resdir = 'app/src/main/res';
my $dstdir = 'tmp';
my $trimdir = 'trim24';
my $tmpfile = 'tmp/tmp.png';
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 $!";
}
sub fix($){
my($relpath) = @_;
my $dstfile = "$dstdir/$relpath";
return if not $dstfile =~ s/(png|webp)\z/png/i;
my $ext = lc $1;
my $inData;
if( $ext eq "webp" ){
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 : $!";
}
my $image = GD::Image->newFromPngData($inData,1);
my $width = $image->width;
my $height = $image->height;
my $alphaMax = 0;
my $isColor = 0 ;
Loop: 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;
if( abs($r-$g) >= 2 or abs($r-$b) >= 2 or abs($g-$b) >= 2 ){
$isColor = 1;
last Loop;
}
my $alpha= 127-$c0;
if( $alpha > $alphaMax){
$alphaMax = $alpha;
}
}
}
$relpath=~ /drawable-(\w+)/;
my $density = $density{ $1 };
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);
}
}
}
}
return if $isColor;
return if $alphaMax == 0;
return if $alphaMax >= 126;
say "${width}x${height}, alphaMax=$alphaMax, isColor=${isColor}, $relpath";
my $scale = 127.0/$alphaMax;
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);
}
}
savePng($image,$dstfile);
return if $dry_run;
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);
}
}
my @files;
find(
{
no_chdir => 1
,wanted =>sub{
my $relpath = $File::Find::name;
return if -d $relpath;
return if not $relpath =~ m|/drawable-|;
return if $relpath =~ m%/(media_type_|emoji_|media_bg_dark)%;
push @files,$relpath;
}
}
,$resdir
);
for my $relpath(@files){
fix($relpath);
}