--- /dev/null
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit;\r
+\r
+import java.awt.Dimension;\r
+import java.awt.Image;\r
+import java.awt.image.BufferedImage;\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.FilenameFilter;\r
+import java.io.IOException;\r
+import java.util.Iterator;\r
+\r
+import javax.imageio.ImageIO;\r
+import javax.imageio.ImageReader;\r
+import javax.imageio.stream.ImageInputStream;\r
+\r
+import com.beust.jcommander.JCommander;\r
+import com.beust.jcommander.Parameter;\r
+import com.beust.jcommander.ParameterException;\r
+import com.beust.jcommander.Parameters;\r
+\r
+public class Thumbnailer {\r
+\r
+ public static void main(String[] args) {\r
+ Params params = new Params();\r
+ JCommander jc = new JCommander(params);\r
+ try {\r
+ jc.parse(args);\r
+ } catch (ParameterException t) {\r
+ System.err.println(t.getMessage());\r
+ jc.usage();\r
+ }\r
+ createImageThumbnail(params.sourceFolder, params.destinationFolder, params.maximumDimension);\r
+ }\r
+\r
+ public static void createImageThumbnail(String sourceFolder, String destinationFolder,\r
+ int maxDimension) {\r
+ if (maxDimension <= 0)\r
+ return;\r
+ File source = new File(sourceFolder);\r
+ File destination = new File(destinationFolder);\r
+ destination.mkdirs();\r
+ File[] sourceFiles = source.listFiles(new FilenameFilter() {\r
+ @Override\r
+ public boolean accept(File dir, String name) {\r
+ return name.toLowerCase().endsWith(".png");\r
+ }\r
+ });\r
+\r
+ for (File sourceFile : sourceFiles) {\r
+ File destinationFile = new File(destination, sourceFile.getName());\r
+ try {\r
+ Dimension sz = getImageDimensions(sourceFile);\r
+ int w = 0;\r
+ int h = 0;\r
+ if (sz.width > maxDimension) {\r
+ // Scale to Width\r
+ w = maxDimension;\r
+ float f = maxDimension;\r
+ h = (int) ((f / sz.width) * sz.height); // normalize height\r
+ } else if (sz.height > maxDimension) {\r
+ // Scale to Height\r
+ h = maxDimension;\r
+ float f = maxDimension;\r
+ w = (int) ((f / sz.height) * sz.width); // normalize width\r
+ } else {\r
+ // No thumbnail\r
+ return;\r
+ }\r
+ System.out.println("Generating thumbnail for " + sourceFile.getName() + " as (" + w\r
+ + "," + h + ")");\r
+ BufferedImage image = ImageIO.read(sourceFile);\r
+ Image scaledImage = image.getScaledInstance(w, h, BufferedImage.SCALE_SMOOTH);\r
+ BufferedImage destImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);\r
+ destImage.createGraphics().drawImage(scaledImage, 0, 0, null);\r
+ FileOutputStream fos = new FileOutputStream(destinationFile);\r
+ ImageIO.write(destImage, "png", fos);\r
+ fos.flush();\r
+ fos.getFD().sync();\r
+ fos.close();\r
+ } catch (Throwable t) {\r
+ t.printStackTrace();\r
+ }\r
+ }\r
+ }\r
+\r
+ public static Dimension getImageDimensions(File file) throws IOException {\r
+ ImageInputStream in = ImageIO.createImageInputStream(file);\r
+ try {\r
+ final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);\r
+ if (readers.hasNext()) {\r
+ ImageReader reader = readers.next();\r
+ try {\r
+ reader.setInput(in);\r
+ return new Dimension(reader.getWidth(0), reader.getHeight(0));\r
+ } finally {\r
+ reader.dispose();\r
+ }\r
+ }\r
+ } finally {\r
+ if (in != null)\r
+ in.close();\r
+ }\r
+ return null;\r
+ }\r
+\r
+ @Parameters(separators = " ")\r
+ private static class Params {\r
+\r
+ @Parameter(names = { "--sourceFolder" }, description = "Source folder for raw images", required = true)\r
+ public String sourceFolder;\r
+\r
+ @Parameter(names = { "--destinationFolder" }, description = "Destination folder for thumbnails", required = true)\r
+ public String destinationFolder;\r
+\r
+ @Parameter(names = { "--maxDimension" }, description = "Maximum width or height for thumbnail", required = true)\r
+ public int maximumDimension;\r
+\r
+ }\r
+}\r