Commits

Hugo Visser  committed 68ab3b6

Change the destination file handling some more and create a scaled up/down version of the image asset when it doesn't exists.

  • Participants
  • Parent commits a05ca53

Comments (0)

Files changed (1)

File src/main/java/nl/qbusict/ninetool/NineTool.java

 
 import nl.qbusict.ninetool.NinePatchUtilities.NinePatch;
 
+import org.imgscalr.Scalr;
 import org.kohsuke.args4j.Argument;
 import org.kohsuke.args4j.CmdLineException;
 import org.kohsuke.args4j.CmdLineParser;
     private static final String NINE_PATCH_SUFFIX = ".9.png";
 
     private enum Operation {
-        PATCH, PNG
+        COPY, PNG
     };
 
     private enum Dpi {
         public float getDpi() {
             return dpi;
         }
+
+        @Override
+        public String toString() {
+            return name().toLowerCase();
+        }
     }
 
     private static class OperationOptions {
 
         public Operation command;
 
-        @Argument(index = 0, required = true, metaVar = "command", usage="patch, png")
+        // work around a bug in args4j, would have liked to set the enum directly...
+        @Argument(index = 0, required = true, metaVar = "command", usage="copy, png")
         public void setCommand(String op) {
             try {
                 command = Operation.valueOf(op.toUpperCase());
     }
 
     private static class PatchOptions {
-        @Argument(index = 0, required = true, metaVar = "destination", usage = "file.png or file.9.png", handler=RequiredPngFileOptionHandler.class)
+        @Argument(index = 0, required = true, metaVar = "source", usage = "file.9.png", handler=NinePatchFileOptionHandler.class)
+        public File srcFile;
+        @Argument(index = 1, required = true, metaVar = "destination", usage = "file.png or file.9.png")
         public File destFile;
-        @Argument(index = 1, required = true, metaVar = "source", usage = "file.9.png", handler=NinePatchFileOptionHandler.class)
-        public File srcFile;
         @Option(name = "-s", metaVar = "dpi", usage = "dpi of the source file, ldpi, mdpi, ..., xxhdpi")
         public Dpi srcDpi;
         @Option(name = "-t", metaVar = "dpi", usage = "dpi of the source file, ldpi, mdpi, ..., xxhdpi")
         } catch (CmdLineException ex) {
             printMainUsage(parser);
         }
-        if (opts.command == Operation.PATCH) {
+        if (opts.command == Operation.COPY) {
             PatchOptions patchOpts = new PatchOptions();
             parser = new CmdLineParser(patchOpts);
             try {
             } catch (CmdLineException ex) {
                 System.err.println(ex.getMessage());
                 System.err.println();
-                System.err.println("Usage: patch <destination> <source> [options]");
+                System.err.println("Usage: patch <source> <destination> [options]");
                 parser.printUsage(System.err);
                 System.exit(1);
             }
     }
 
     private static void patch(PatchOptions opts) throws IOException {
-        boolean isNinepatch = opts.destFile.getAbsolutePath().toLowerCase().endsWith(NINE_PATCH_SUFFIX);
-        BufferedImage srcImage = ImageIO.read(opts.srcFile);
-        BufferedImage dstImage = ImageIO.read(opts.destFile);
-        NinePatch patch = NinePatchUtilities.parse(srcImage);
-        if (patch.isEmpty()) {
-            System.err.println("Source file does not have nine patch information.");
+        boolean createdNewFile = false;
+
+        File destFile = opts.destFile;
+        // dest file can be a directory so you can use copy bla.png drawable-mdpi and not have to repeat the file
+        if (destFile.isDirectory()) {
+            // complete the file
+            destFile = new File(destFile, opts.srcFile.getName());
+        }
+
+        if (opts.srcFile.equals(destFile)) {
+            System.err.println("source file must be different from destination file");
             System.exit(1);
         }
 
+        boolean isNinepatch = destFile.getAbsolutePath().toLowerCase().endsWith(NINE_PATCH_SUFFIX);
+
         if (opts.srcDpi == null) {
             opts.srcDpi = guessDpiBasedOnFile(opts.srcFile);
             if (opts.srcDpi != null) {
         }
 
         if (opts.dstDpi == null) {
-            opts.dstDpi = guessDpiBasedOnFile(opts.destFile);
+            opts.dstDpi = guessDpiBasedOnFile(destFile);
             if (opts.dstDpi != null) {
                 System.out.println("Destination dpi is "+opts.dstDpi.toString());
             }
             scale = opts.srcDpi.getScalingFactor(opts.dstDpi);
         }
 
+        BufferedImage srcImage = ImageIO.read(opts.srcFile);
+        BufferedImage dstImage;
+        if (destFile.exists()) {
+            dstImage = ImageIO.read(destFile);
+        } else {
+            createdNewFile = true;
+            isNinepatch = false;
+            dstImage = NinePatchUtilities.crop(srcImage);
+            dstImage = Scalr.resize(dstImage, (int)(dstImage.getWidth()*scale), (int)(dstImage.getHeight()*scale));
+
+        }
+
+
+        NinePatch patch = NinePatchUtilities.parse(srcImage);
+        if (patch.isEmpty()) {
+            System.err.println("Source file does not have nine patch information.");
+            System.exit(1);
+        }
+
+
         patch.setScale(scale);
 
         BufferedImage result = NinePatchUtilities.apply(dstImage, patch, isNinepatch);
         File deleteOld = null;
-        if (!isNinepatch) {
-            deleteOld = opts.destFile;
-            String path = opts.destFile.getAbsolutePath();
-            opts.destFile = new File(path.substring(0, path.length() - 4) + NINE_PATCH_SUFFIX);
+        if (!isNinepatch && !createdNewFile) {
+            deleteOld = destFile;
+            String path = destFile.getAbsolutePath();
+            destFile = new File(path.substring(0, path.length() - 4) + NINE_PATCH_SUFFIX);
         }
-        ImageIO.write(result, "png", opts.destFile);
+        ImageIO.write(result, "png", destFile);
         if (deleteOld != null) {
             deleteOld.delete();
         }