forked from svenwiegand/upod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createImages.scala
184 lines (144 loc) · 5.95 KB
/
createImages.scala
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
175
176
177
178
179
180
181
182
183
import java.io._
import scala.sys.process._
object ImageConverter {
val rootDir = new File("images/export")
var targetResourceDir = new File("app/res")
object DensityFactor {
val mdpi = 1.0
val hdpi = 1.5
val xhdpi = 2
val xxhdpi = 3
}
implicit class DensityAwareSize(val mdpi: Int) {
private def scale(factor: Double): Int = (factor * mdpi).toInt
def hdpi = scale(DensityFactor.hdpi)
def xhdpi = scale(DensityFactor.xhdpi)
def xxhdpi = scale(DensityFactor.xxhdpi)
}
implicit class RichFile(val file: File) {
def absolutePath = file.getAbsolutePath
def name = file.getName
def baseName = file.getName.lastIndexOf('.') match {
case dotIndex if dotIndex > 0 => file.getName.substring(0, dotIndex)
case _ => file.getName
}
def extension: String = file.getName.lastIndexOf('.') match {
case dotIndex if dotIndex > 0 => file.getName.substring(dotIndex)
case _ => ""
}
def tempFile: File = File.createTempFile(name, extension)
}
private def changed(src: File, dest: File): Boolean =
!dest.exists || dest.lastModified() < src.lastModified()
private class ExecutionFailedException(val command: String, val exitCode: Int)
extends Exception("Command '" + command + "' failed with exit code " + exitCode)
private def executeShellCmd(command: String) {
val exitCode = command.!
if (exitCode != 0)
throw new ExecutionFailedException(command, exitCode)
}
private def executeShellCmd(command: List[String]) {
val fullCmd = "cmd" :: "/c" :: command
val exitCode = fullCmd.run().exitValue()
if (exitCode != 0)
throw new ExecutionFailedException(command.mkString(" "), exitCode)
}
private def convert(src: File, dest: File, args: String*) {
executeShellCmd("convert.exe" :: src.absolutePath :: args.toList ::: dest.getAbsolutePath :: Nil)
}
private def scaleImage(src: File, dest: File, targetHeight: Int) {
convert(src, dest,
"-define", "png:color-type=6",
"-resize", s"x$targetHeight")
}
private def overlayImage(background: File, overlay: File, dest: File) {
executeShellCmd(s"""composite.exe "$overlay" "$background" -gravity center "$dest" """)
}
private def convertGrayImage(src: File, dest: File, targetHeight: Int, grayValue: Int, opacity: Double) {
val brightness = 100 * grayValue / 0xff
convert(src, dest,
"-define", "png:color-type=6",
"-resize", s"x$targetHeight",
"-brightness-contrast", brightness.toString,
"-channel", "Alpha", "-evaluate", "Multiply", opacity.toString)
}
class Image(name: String, height: DensityAwareSize)(convert: (File, File, Int, String) => Unit) {
private val typeDir = new File(rootDir, name)
def process() {
typeDir.listFiles filter { file => file.isFile && !file.name.endsWith("dpi.png")} foreach { processImage }
}
private def processImage(img: File) {
println(s"processing ${img.getName}")
convertImage(img, "hdpi", height.hdpi)
convertImage(img, "xhdpi", height.xhdpi)
convertImage(img, "xxhdpi", height.xxhdpi)
}
private def convertImage(src: File, destDpi: String, height: Int) {
print(s" ${destDpi.padTo("xxhdpi".length, ' ')} ... ")
val dest = new File(new File(targetResourceDir, s"drawable-$destDpi"), src.name)
if (changed(src, dest)) {
convert(src, dest, height, destDpi)
println(s"created")
} else {
println("up-to-date")
}
}
}
object GrayImage {
def apply(name: String, height: DensityAwareSize) = new Image(name, height)({
(src, dest, height, densityName) =>
convertGrayImage(src, new File(dest.getParentFile, s"${src.baseName}_light${src.extension}"), height, 0x73, 1)
convertGrayImage(src, dest, height, 0xff, 1)
})
}
object Image {
def apply(name: String, height: DensityAwareSize) = new Image(name, height)({
(src, dest, height, densityName) =>
scaleImage(src, dest, height)
})
}
object NinePatchImage {
def getNinePatch(src: File, densityName: String) =
new File(src.getParentFile, s"${src.baseName}.$densityName.png")
def apply(name: String, height: DensityAwareSize, ninePatch: (File, String) => File) = new Image(name, height)({
(src, dest, height, densityName) =>
val scaledImage = src.tempFile
scaleImage(src, scaledImage, height)
overlayImage(ninePatch(src, densityName), scaledImage, dest)
scaledImage.delete()
})
def apply(name: String, height: DensityAwareSize): Image = apply(name, height, { (src: File, densityName: String) =>
new File(src.getParentFile, s"${src.baseName}.$densityName.png")
})
}
object SelectorImage {
def apply(name: String, height: DensityAwareSize) = NinePatchImage(name, height, { (src, densityName) =>
val baseName = src.baseName.replace("_default", "").replace("_disabled", "").replace("_focused", "").replace("_pressed", "")
new File(src.getParentFile, s"$baseName.$densityName.png")
})
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
def processImages() {
Image("logo", 48).process()
Image("navigation", 24).process()
GrayImage("actions", 24).process()
Image("prefs", 24).process()
Image("actions_raw", 24).process()
Image("notifications", 24).process()
Image("item_indicators", 16).process()
GrayImage("media_control", 48).process()
Image("media_control_overlay", 48).process()
Image("context_menu_button", 22).process()
Image("drag_handle", 18).process()
NinePatchImage("button", 32).process()
NinePatchImage("counter", 32).process()
NinePatchImage("podcast_grid_item_background", 24).process()
NinePatchImage("podcast_image_background", 24).process()
NinePatchImage("pane_shadow", 24).process()
}
}
object Main {
def main(args: Array[String]) {
ImageConverter.processImages()
}
}