commit | author | age
|
24d08f
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
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 |
*/
|
22fc5e
|
16 |
package com.gitblit.build;
|
24d08f
|
17 |
|
JM |
18 |
import java.awt.Dimension;
|
|
19 |
import java.awt.Image;
|
|
20 |
import java.awt.image.BufferedImage;
|
|
21 |
import java.io.File;
|
|
22 |
import java.io.FileOutputStream;
|
|
23 |
import java.io.FilenameFilter;
|
|
24 |
import java.io.IOException;
|
892570
|
25 |
import java.text.MessageFormat;
|
24d08f
|
26 |
import java.util.Iterator;
|
JM |
27 |
|
|
28 |
import javax.imageio.ImageIO;
|
|
29 |
import javax.imageio.ImageReader;
|
|
30 |
import javax.imageio.stream.ImageInputStream;
|
|
31 |
|
|
32 |
import com.beust.jcommander.JCommander;
|
|
33 |
import com.beust.jcommander.Parameter;
|
|
34 |
import com.beust.jcommander.ParameterException;
|
|
35 |
import com.beust.jcommander.Parameters;
|
|
36 |
|
892570
|
37 |
/**
|
JM |
38 |
* Generates PNG thumbnails of the PNG images from the specified source folder.
|
|
39 |
*
|
|
40 |
* @author James Moger
|
|
41 |
*
|
|
42 |
*/
|
85c2e6
|
43 |
public class BuildThumbnails {
|
24d08f
|
44 |
|
JM |
45 |
public static void main(String[] args) {
|
|
46 |
Params params = new Params();
|
|
47 |
JCommander jc = new JCommander(params);
|
|
48 |
try {
|
|
49 |
jc.parse(args);
|
|
50 |
} catch (ParameterException t) {
|
|
51 |
System.err.println(t.getMessage());
|
|
52 |
jc.usage();
|
|
53 |
}
|
|
54 |
createImageThumbnail(params.sourceFolder, params.destinationFolder, params.maximumDimension);
|
|
55 |
}
|
|
56 |
|
892570
|
57 |
/**
|
JM |
58 |
* Generates thumbnails from all PNG images in the source folder and saves
|
|
59 |
* them to the destination folder.
|
|
60 |
*
|
|
61 |
* @param sourceFolder
|
|
62 |
* @param destinationFolder
|
|
63 |
* @param maxDimension
|
|
64 |
* the maximum height or width of the image.
|
|
65 |
*/
|
24d08f
|
66 |
public static void createImageThumbnail(String sourceFolder, String destinationFolder,
|
JM |
67 |
int maxDimension) {
|
88598b
|
68 |
if (maxDimension <= 0) {
|
24d08f
|
69 |
return;
|
88598b
|
70 |
}
|
24d08f
|
71 |
File source = new File(sourceFolder);
|
JM |
72 |
File destination = new File(destinationFolder);
|
|
73 |
destination.mkdirs();
|
|
74 |
File[] sourceFiles = source.listFiles(new FilenameFilter() {
|
|
75 |
@Override
|
|
76 |
public boolean accept(File dir, String name) {
|
|
77 |
return name.toLowerCase().endsWith(".png");
|
|
78 |
}
|
|
79 |
});
|
|
80 |
|
|
81 |
for (File sourceFile : sourceFiles) {
|
|
82 |
File destinationFile = new File(destination, sourceFile.getName());
|
|
83 |
try {
|
|
84 |
Dimension sz = getImageDimensions(sourceFile);
|
|
85 |
int w = 0;
|
|
86 |
int h = 0;
|
|
87 |
if (sz.width > maxDimension) {
|
|
88 |
// Scale to Width
|
|
89 |
w = maxDimension;
|
|
90 |
float f = maxDimension;
|
892570
|
91 |
// normalize height
|
JM |
92 |
h = (int) ((f / sz.width) * sz.height);
|
24d08f
|
93 |
} else if (sz.height > maxDimension) {
|
JM |
94 |
// Scale to Height
|
|
95 |
h = maxDimension;
|
|
96 |
float f = maxDimension;
|
892570
|
97 |
// normalize width
|
JM |
98 |
w = (int) ((f / sz.height) * sz.width);
|
24d08f
|
99 |
}
|
892570
|
100 |
System.out.println(MessageFormat.format(
|
JM |
101 |
"Generating thumbnail for {0} as ({1,number,#}, {2,number,#})",
|
|
102 |
sourceFile.getName(), w, h));
|
24d08f
|
103 |
BufferedImage image = ImageIO.read(sourceFile);
|
JM |
104 |
Image scaledImage = image.getScaledInstance(w, h, BufferedImage.SCALE_SMOOTH);
|
|
105 |
BufferedImage destImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
|
|
106 |
destImage.createGraphics().drawImage(scaledImage, 0, 0, null);
|
|
107 |
FileOutputStream fos = new FileOutputStream(destinationFile);
|
|
108 |
ImageIO.write(destImage, "png", fos);
|
|
109 |
fos.flush();
|
|
110 |
fos.getFD().sync();
|
|
111 |
fos.close();
|
|
112 |
} catch (Throwable t) {
|
|
113 |
t.printStackTrace();
|
|
114 |
}
|
|
115 |
}
|
|
116 |
}
|
|
117 |
|
892570
|
118 |
/**
|
JM |
119 |
* Return the dimensions of the specified image file.
|
|
120 |
*
|
|
121 |
* @param file
|
|
122 |
* @return dimensions of the image
|
|
123 |
* @throws IOException
|
|
124 |
*/
|
24d08f
|
125 |
public static Dimension getImageDimensions(File file) throws IOException {
|
JM |
126 |
ImageInputStream in = ImageIO.createImageInputStream(file);
|
|
127 |
try {
|
|
128 |
final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
|
|
129 |
if (readers.hasNext()) {
|
|
130 |
ImageReader reader = readers.next();
|
|
131 |
try {
|
|
132 |
reader.setInput(in);
|
|
133 |
return new Dimension(reader.getWidth(0), reader.getHeight(0));
|
|
134 |
} finally {
|
|
135 |
reader.dispose();
|
|
136 |
}
|
|
137 |
}
|
|
138 |
} finally {
|
88598b
|
139 |
if (in != null) {
|
24d08f
|
140 |
in.close();
|
88598b
|
141 |
}
|
24d08f
|
142 |
}
|
JM |
143 |
return null;
|
|
144 |
}
|
|
145 |
|
88598b
|
146 |
/**
|
JM |
147 |
* JCommander Parameters class for BuildThumbnails.
|
|
148 |
*/
|
24d08f
|
149 |
@Parameters(separators = " ")
|
JM |
150 |
private static class Params {
|
|
151 |
|
|
152 |
@Parameter(names = { "--sourceFolder" }, description = "Source folder for raw images", required = true)
|
|
153 |
public String sourceFolder;
|
|
154 |
|
|
155 |
@Parameter(names = { "--destinationFolder" }, description = "Destination folder for thumbnails", required = true)
|
|
156 |
public String destinationFolder;
|
|
157 |
|
85c2e6
|
158 |
@Parameter(names = { "--maximumDimension" }, description = "Maximum width or height for thumbnail", required = true)
|
24d08f
|
159 |
public int maximumDimension;
|
JM |
160 |
|
|
161 |
}
|
|
162 |
}
|