Initial commit
authorunknown <marius@varden.info>
Wed, 30 Jul 2014 10:39:50 +0000 (12:39 +0200)
committerunknown <marius@varden.info>
Wed, 30 Jul 2014 10:39:50 +0000 (12:39 +0200)
README.md [new file with mode: 0644]
src/main/java/info/varden/imagetint/ImageTint.java [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..4b3ea5f
--- /dev/null
+++ b/README.md
@@ -0,0 +1,6 @@
+ImageTint
+=========
+
+Simple program for tinting pictures and images with a hue in Java.
+
+This program is public domain; see the LICENSE file. Compiled binary at http://varden.info/~mlindvall/java/ImageTint.jar
\ No newline at end of file
diff --git a/src/main/java/info/varden/imagetint/ImageTint.java b/src/main/java/info/varden/imagetint/ImageTint.java
new file mode 100644 (file)
index 0000000..e04ddbc
--- /dev/null
@@ -0,0 +1,85 @@
+package info.varden.imagetint;
+
+import java.awt.AlphaComposite;
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+
+import javax.imageio.ImageIO;
+
+public class ImageTint {
+
+       public static void main(String[] args) {
+               if (args.length < 4) {
+                       System.out.println("USAGE:");
+                       System.out.println("java -jar ImageTint.jar <AARRGGBB> [-i <image>|-d <directory>] <destination_directory>");
+                       System.out.println("\nExample: java -jar ImageTint.jar 7FFF0000 -i img.png tinted");
+                       System.out.println("Applies 50% red to the image img.png and saves it as a PNG file in a directory named \"tinted\".");
+                       System.out.println("Warning: If there are files in the destination directory, they may be overwritten without warning!");
+                       return;
+               }
+               String color = args[0];
+               String type = args[1];
+               String input = args[2];
+               String format = "png";
+               String dir = args[3];
+               long col = Long.parseLong(color, 16);
+               File inFile = new File(input);
+               File outDir = new File(dir);
+               File[] files = new File[0];
+               
+               if (type.equals("-i")) {
+                       files = new File[] {inFile};
+               } else if (type.equals("-d")) {
+                       files = inFile.listFiles();
+               } else {
+                       System.out.println("Invalid option: " + type);
+                       return;
+               }
+               if (!outDir.exists()) outDir.mkdirs();
+               
+               for (File f : files) {
+                       if (f.isDirectory()) continue;
+                       try {
+                               BufferedImage inputImg = ImageIO.read(f);
+                               int w2 = inputImg.getWidth();
+                               int h2 = inputImg.getHeight();
+                               float alpha = (float) (col >> 24) / 255F;
+                               
+                               System.out.println("Rendering " + f.getName() + " with a=" + alpha + "...");
+                               
+                               BufferedImage result = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_ARGB);
+                               Graphics g = result.getGraphics();
+                               g.drawImage(inputImg, 0, 0, null);
+                               
+                               BufferedImage wm = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_ARGB);
+                               Graphics2D wmg = (Graphics2D) wm.getGraphics();
+                               wmg.setComposite(AlphaComposite.SrcOver.derive(alpha));
+                               Color c = new Color((int) (col & 0xFFFFFF));
+                               wmg.setPaint(c);
+                               wmg.fillRect(0, 0, w2, h2);
+                               wmg.dispose();
+                               g.drawImage(wm, 0, 0, null);
+                               g.dispose();
+                               wm.flush();
+                               String name = f.getName();
+                               int pos = name.lastIndexOf(".");
+                               if (pos > 0) {
+                                   name = name.substring(0, pos);
+                               }
+                               File tf = new File(outDir, name + "." + format);
+                               if (tf.exists()) tf.delete();
+                               ImageIO.write(result, format, tf);
+                               
+                               inputImg.flush();
+                       } catch (IOException e) {
+                               System.out.println("!!! Failed to tint " + f.getName() + "!!!");
+                               e.printStackTrace();
+                       }
+               }
+       }
+
+}