| 1 |
/* |
| 2 |
* Copyright (C) 2016 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
import com.android.tools.pixelprobe.Image; |
| 18 |
import com.android.tools.pixelprobe.Layer; |
| 19 |
import com.android.tools.pixelprobe.PixelProbe; |
| 20 |
|
| 21 |
import javax.imageio.ImageIO; |
| 22 |
import java.awt.*; |
| 23 |
import java.awt.color.ColorSpace; |
| 24 |
import java.awt.image.BufferedImage; |
| 25 |
import java.awt.image.ColorConvertOp; |
| 26 |
import java.io.File; |
| 27 |
import java.io.FileInputStream; |
| 28 |
import java.io.IOException; |
| 29 |
import java.nio.file.*; |
| 30 |
import java.util.stream.Collectors; |
| 31 |
|
| 32 |
@SuppressWarnings("UseOfSystemOutOrSystemErr") |
| 33 |
public class ExtractRasterLayers { |
| 34 |
private static Path outputDir; |
| 35 |
|
| 36 |
public static void main(String[] args) { |
| 37 |
checkArguments(args); |
| 38 |
|
| 39 |
Path inputDir = Paths.get(args[0]).toAbsolutePath().normalize(); |
| 40 |
if (Files.notExists(inputDir)) { |
| 41 |
printHelp(); |
| 42 |
System.exit(1); |
| 43 |
} |
| 44 |
|
| 45 |
outputDir = Paths.get(args[1]).toAbsolutePath().normalize(); |
| 46 |
if (Files.notExists(outputDir)) { |
| 47 |
try { |
| 48 |
Files.createDirectories(outputDir); |
| 49 |
} catch (IOException e1) { |
| 50 |
System.err.println("Could not create output directory " + outputDir); |
| 51 |
System.exit(1); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
try { |
| 56 |
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*.psd"); |
| 57 |
Files.list(inputDir) |
| 58 |
.filter(matcher::matches) |
| 59 |
.collect(Collectors.toList()) |
| 60 |
.parallelStream() |
| 61 |
.forEach(ExtractRasterLayers::extractLayers); |
| 62 |
} catch (IOException e) { |
| 63 |
System.err.println("An error occurred while reading the input directory."); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
private static void extractLayers(Path path) { |
| 68 |
File file = path.toFile(); |
| 69 |
try (FileInputStream in = new FileInputStream(file)) { |
| 70 |
Image image = PixelProbe.probe(in); |
| 71 |
image.getLayers() |
| 72 |
.parallelStream() |
| 73 |
.filter(layer -> layer.getType() == Layer.Type.IMAGE) |
| 74 |
.forEach(layer -> save(layer, file.getName())); |
| 75 |
} catch (IOException e) { |
| 76 |
System.err.println("An error occurred while parsing " + path); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
private static void save(Layer layer, String name) { |
| 81 |
BufferedImage image = layer.getImage(); |
| 82 |
if (image == null) return; |
| 83 |
|
| 84 |
Path path = Paths.get(outputDir.toAbsolutePath().toString(), name); |
| 85 |
try { |
| 86 |
if (Files.notExists(path)) Files.createDirectories(path); |
| 87 |
path = path.resolve(layer.getName() + ".png"); |
| 88 |
|
| 89 |
System.out.println("Writing " + outputDir.relativize(path)); |
| 90 |
|
| 91 |
ImageIO.write(toSRGB(image), "PNG", path.toFile()); |
| 92 |
} catch (IOException e) { |
| 93 |
System.err.println("An error occurred while writing " + path); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
private static BufferedImage toSRGB(BufferedImage bufferedImage) throws IOException { |
| 98 |
if (bufferedImage.getColorModel().getColorSpace().isCS_sRGB()) return bufferedImage; |
| 99 |
ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_sRGB), |
| 100 |
new RenderingHints(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY)); |
| 101 |
return op.filter(bufferedImage, null); |
| 102 |
} |
| 103 |
|
| 104 |
private static void checkArguments(String[] args) { |
| 105 |
if (args.length < 2) { |
| 106 |
printHelp(); |
| 107 |
System.exit(1); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
private static void printHelp() { |
| 112 |
System.out.println("java ExtractRasterLayers <input dir> <output dir>\n"); |
| 113 |
System.out.println("Finds all .psd files in <input dir> and extract the content "); |
| 114 |
System.out.println("of their raster layers in <output dir> as PNG files."); |
| 115 |
} |
| 116 |
} |